4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "bcdisplayinfo.h"
29 #include "pluginvclient.h"
36 #define TOP_FIELD_FIRST 0
37 #define BOTTOM_FIELD_FIRST 1
38 #define TOTAL_FRAMES 10
48 void copy_from(DecimateConfig *config);
49 int equivalent(DecimateConfig *config);
52 // Averaged frames is useless. Some of the frames are permanently
53 // destroyed in conversion from PAL to NTSC.
61 class DecimateRate : public BC_TextBox
64 DecimateRate(Decimate *plugin,
73 class DecimateRateMenu : public BC_ListBox
76 DecimateRateMenu(Decimate *plugin,
85 class DecimateDifference : public BC_CheckBox
88 DecimateDifference(Decimate *plugin,
95 class DecimateAvgDifference : public BC_CheckBox
98 DecimateAvgDifference(Decimate *plugin,
106 class DecimateWindow : public PluginClientWindow
109 DecimateWindow(Decimate *plugin);
112 void create_objects();
114 ArrayList<BC_ListBoxItem*> frame_rates;
117 DecimateRateMenu *rate_menu;
118 BC_Title *last_dropped;
119 // DecimateDifference *difference;
120 // DecimateAvgDifference *avg_difference;
127 class Decimate : public PluginVClient
130 Decimate(PluginServer *server);
133 PLUGIN_CLASS_MEMBERS(DecimateConfig)
135 int process_buffer(VFrame *frame,
136 int64_t start_position,
139 void save_data(KeyFrame *keyframe);
140 void read_data(KeyFrame *keyframe);
142 void render_gui(void *data);
144 int64_t calculate_difference(VFrame *frame1, VFrame *frame2);
145 void fill_lookahead(double frame_rate,
146 int64_t start_position);
147 void decimate_frame();
149 void fdct(uint16_t *block);
150 int64_t calculate_fdct(VFrame *frame);
156 // each difference is the difference between the previous frame and the
158 int64_t differences[TOTAL_FRAMES];
160 // read ahead number of frames
161 VFrame *frames[TOTAL_FRAMES];
162 // Number of frames in the lookahead buffer
164 // Next position beyond end of lookahead buffer relative to input rate
165 int64_t lookahead_end;
166 // Framerate of lookahead buffer
167 double lookahead_rate;
168 // Last requested position
169 int64_t last_position;
184 DecimateConfig::DecimateConfig()
186 input_rate = (double)30000 / 1001;
187 least_difference = 1;
191 void DecimateConfig::copy_from(DecimateConfig *config)
193 this->input_rate = config->input_rate;
194 this->least_difference = config->least_difference;
195 this->averaged_frames = config->averaged_frames;
198 int DecimateConfig::equivalent(DecimateConfig *config)
200 return EQUIV(this->input_rate, config->input_rate);
211 DecimateWindow::DecimateWindow(Decimate *plugin)
212 : PluginClientWindow(plugin,
219 this->plugin = plugin;
222 DecimateWindow::~DecimateWindow()
224 frame_rates.remove_all_objects();
227 void DecimateWindow::create_objects()
231 frame_rates.append(new BC_ListBoxItem("1"));
232 frame_rates.append(new BC_ListBoxItem("5"));
233 frame_rates.append(new BC_ListBoxItem("10"));
234 frame_rates.append(new BC_ListBoxItem("12"));
235 frame_rates.append(new BC_ListBoxItem("15"));
236 frame_rates.append(new BC_ListBoxItem("23.97"));
237 frame_rates.append(new BC_ListBoxItem("24"));
238 frame_rates.append(new BC_ListBoxItem("25"));
239 frame_rates.append(new BC_ListBoxItem("29.97"));
240 frame_rates.append(new BC_ListBoxItem("30"));
241 frame_rates.append(new BC_ListBoxItem("50"));
242 frame_rates.append(new BC_ListBoxItem("59.94"));
243 frame_rates.append(new BC_ListBoxItem("60"));
246 add_subwindow(title = new BC_Title(x, y, _("Input frames per second:")));
248 add_subwindow(rate = new DecimateRate(plugin,
252 add_subwindow(rate_menu = new DecimateRateMenu(plugin,
254 x + rate->get_w() + 5,
257 add_subwindow(title = new BC_Title(x, y, _("Last frame dropped: ")));
258 add_subwindow(last_dropped = new BC_Title(x + title->get_w() + 5, y, ""));
261 // add_subwindow(difference = new DecimateDifference(plugin,
265 // add_subwindow(avg_difference = new DecimateAvgDifference(plugin,
284 DecimateRate::DecimateRate(Decimate *plugin,
292 (float)plugin->config.input_rate)
294 this->plugin = plugin;
298 int DecimateRate::handle_event()
300 plugin->config.input_rate = Units::atoframerate(get_text());
301 plugin->send_configure_change();
307 // DecimateDifference::DecimateDifference(Decimate *plugin,
310 // : BC_CheckBox(x, y, plugin->config.least_difference, "Drop least difference")
312 // this->plugin = plugin;
314 // int DecimateDifference::handle_event()
316 // plugin->config.least_difference = get_value();
317 // plugin->send_configure_change();
324 // DecimateAvgDifference::DecimateAvgDifference(Decimate *plugin,
327 // : BC_CheckBox(x, y, plugin->config.averaged_frames, "Drop averaged frames")
329 // this->plugin = plugin;
332 // int DecimateAvgDifference::handle_event()
334 // plugin->config.averaged_frames = get_value();
335 // plugin->send_configure_change();
342 DecimateRateMenu::DecimateRateMenu(Decimate *plugin,
358 this->plugin = plugin;
362 int DecimateRateMenu::handle_event()
364 char *text = get_selection(0, 0)->get_text();
365 plugin->config.input_rate = atof(text);
366 gui->rate->update(text);
367 plugin->send_configure_change();
391 REGISTER_PLUGIN(Decimate)
398 Decimate::Decimate(PluginServer *server)
399 : PluginVClient(server)
402 bzero(frames, sizeof(VFrame*) * TOTAL_FRAMES);
403 for(int i = 0; i < TOTAL_FRAMES; i++)
412 Decimate::~Decimate()
417 for(int i = 0; i < TOTAL_FRAMES; i++)
424 #define DIFFERENCE_MACRO(type, temp_type, components) \
426 temp_type result2 = 0; \
427 for(int i = 0; i < h; i++) \
429 type *row1 = (type*)frame1->get_rows()[i]; \
430 type *row2 = (type*)frame2->get_rows()[i]; \
431 for(int j = 0; j < w * components; j++) \
433 temp_type temp = *row1 - *row2; \
434 result2 += (temp > 0 ? temp : -temp); \
439 result = (int64_t)result2; \
442 int64_t Decimate::calculate_difference(VFrame *frame1, VFrame *frame2)
444 int w = frame1->get_w();
445 int h = frame1->get_h();
447 switch(frame1->get_color_model())
451 DIFFERENCE_MACRO(unsigned char, int64_t, 3);
454 DIFFERENCE_MACRO(float, double, 3);
458 DIFFERENCE_MACRO(unsigned char, int64_t, 4);
461 DIFFERENCE_MACRO(float, double, 4);
465 DIFFERENCE_MACRO(uint16_t, int64_t, 3);
467 case BC_RGBA16161616:
468 case BC_YUVA16161616:
469 DIFFERENCE_MACRO(uint16_t, int64_t, 4);
475 void Decimate::init_fdct()
482 s = (i==0) ? sqrt(0.125) : 0.5;
485 c[i][j] = s * cos((M_PI/8.0)*i*(j+0.5));
489 void Decimate::fdct(uint16_t *block)
495 for(i = 0; i < 8; i++)
496 for(j = 0; j < 8; j++)
501 * for(k = 0; k < 8; k++)
502 * s += c[j][k] * block[8 * i + k];
504 s += c[j][0] * block[8 * i + 0];
505 s += c[j][1] * block[8 * i + 1];
506 s += c[j][2] * block[8 * i + 2];
507 s += c[j][3] * block[8 * i + 3];
508 s += c[j][4] * block[8 * i + 4];
509 s += c[j][5] * block[8 * i + 5];
510 s += c[j][6] * block[8 * i + 6];
511 s += c[j][7] * block[8 * i + 7];
516 for(j = 0; j < 8; j++)
517 for(i = 0; i < 8; i++)
522 * for(k = 0; k < 8; k++)
523 * s += c[i][k] * tmp[8 * k + j];
525 s += c[i][0] * tmp[8 * 0 + j];
526 s += c[i][1] * tmp[8 * 1 + j];
527 s += c[i][2] * tmp[8 * 2 + j];
528 s += c[i][3] * tmp[8 * 3 + j];
529 s += c[i][4] * tmp[8 * 4 + j];
530 s += c[i][5] * tmp[8 * 5 + j];
531 s += c[i][6] * tmp[8 * 6 + j];
532 s += c[i][7] * tmp[8 * 7 + j];
534 block[8 * i + j] = (int)floor(s + 0.499999);
536 * reason for adding 0.499999 instead of 0.5:
537 * s is quite often x.5 (at least for i and/or j = 0 or 4)
538 * and setting the rounding threshold exactly to 0.5 leads to an
539 * extremely high arithmetic implementation dependency of the result;
540 * s being between x.5 and x.500001 (which is now incorrectly rounded
541 * downwards instead of upwards) is assumed to occur less often
548 #define CALCULATE_DCT(type, components) \
550 uint16_t *output = temp; \
551 for(int k = 0; k < 8; k++) \
553 type *input = (type*)frame->get_rows()[i + k] + j * components; \
554 for(int l = 0; l < 8; l++) \
556 *output = (*input << 8) | *input; \
558 input += components; \
564 int64_t Decimate::calculate_fdct(VFrame *frame)
574 bzero(result, sizeof(int64_t) * 64);
575 int w = frame->get_w();
576 int h = frame->get_h();
579 for(int i = 0; i < h - 8; i += 8)
581 for(int j = 0; j < w - 8; j += 8)
583 CALCULATE_DCT(unsigned char, 3)
584 // Add result to accumulation of transforms
585 for(int k = 0; k < 64; k++)
587 result[k] += temp[k];
592 uint64_t max_result = 0;
594 for(int i = 0; i < 64; i++)
596 if(result[i] > max_result)
598 max_result = result[i];
606 void Decimate::decimate_frame()
608 int64_t min_difference = 0x7fffffffffffffffLL;
611 if(!lookahead_size) return;
613 for(int i = 0; i < lookahead_size; i++)
615 // Drop least different frame from sequence
616 if(config.least_difference &&
617 differences[i] >= 0 &&
618 differences[i] < min_difference)
620 min_difference = differences[i];
625 // If all the frames had differences of 0, like a pure black screen, delete
627 if(result < 0) result = 0;
629 VFrame *temp = frames[result];
630 for(int i = result; i < lookahead_size - 1; i++)
632 frames[i] = frames[i + 1];
633 differences[i] = differences[i + 1];
637 frames[lookahead_size - 1] = temp;
639 send_render_gui(&result);
642 void Decimate::fill_lookahead(double frame_rate,
643 int64_t start_position)
645 // Lookahead rate changed
646 if(!EQUIV(config.input_rate, lookahead_rate))
651 lookahead_rate = config.input_rate;
653 // Start position is not contiguous with last request
654 if(last_position + 1 != start_position)
659 last_position = start_position;
661 // Normalize requested position to input rate
664 lookahead_end = (int64_t)((double)start_position *
669 while(lookahead_size < TOTAL_FRAMES)
671 // Import frame into next lookahead slot
672 read_frame(frames[lookahead_size],
677 // Fill difference buffer
678 if(lookahead_size > 0)
679 differences[lookahead_size] =
680 calculate_difference(frames[lookahead_size - 1],
681 frames[lookahead_size]);
683 // Increase counters relative to input rate
687 // Decimate one if last frame in buffer and lookahead_end is behind predicted
689 int64_t decimated_end = (int64_t)((double)(start_position + TOTAL_FRAMES) *
692 if(lookahead_size >= TOTAL_FRAMES &&
693 lookahead_end < decimated_end)
701 int Decimate::process_buffer(VFrame *frame,
702 int64_t start_position,
706 //printf("Decimate::process_buffer 1 %lld %f\n", start_position, frame_rate);
707 load_configuration();
711 for(int i = 0; i < TOTAL_FRAMES; i++) {
712 frames[i] = new VFrame(frame->get_w(), frame->get_h(),
713 frame->get_color_model(), 0);
718 // Fill lookahead buffer at input rate with decimation
719 fill_lookahead(frame_rate, start_position);
721 // printf("Decimate::process_buffer");
722 // for(int i = 0; i < TOTAL_FRAMES; i++)
723 // printf(" %lld", differences[i]);
727 // Pull first frame off lookahead
728 frame->copy_from(frames[0]);
729 VFrame *temp = frames[0];
730 for(int i = 0; i < TOTAL_FRAMES - 1; i++)
732 frames[i] = frames[i + 1];
733 differences[i] = differences[i + 1];
735 frames[TOTAL_FRAMES - 1] = temp;
742 const char* Decimate::plugin_title() { return N_("Decimate"); }
743 int Decimate::is_realtime() { return 1; }
745 NEW_WINDOW_MACRO(Decimate, DecimateWindow)
748 int Decimate::load_configuration()
750 KeyFrame *prev_keyframe;
751 DecimateConfig old_config;
752 old_config.copy_from(&config);
753 prev_keyframe = get_prev_keyframe(get_source_position());
754 read_data(prev_keyframe);
755 return !old_config.equivalent(&config);
759 void Decimate::save_data(KeyFrame *keyframe)
763 // cause data to be stored directly in text
764 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
765 output.tag.set_title("DECIMATE");
766 output.tag.set_property("INPUT_RATE", config.input_rate);
767 // output.tag.set_property("AVERAGED_FRAMES", config.averaged_frames);
768 // output.tag.set_property("LEAST_DIFFERENCE", config.least_difference);
770 output.tag.set_title("/DECIMATE");
772 output.append_newline();
773 output.terminate_string();
776 void Decimate::read_data(KeyFrame *keyframe)
780 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
782 while(!input.read_tag())
784 if(input.tag.title_is("DECIMATE"))
786 config.input_rate = input.tag.get_property("INPUT_RATE", config.input_rate);
787 // config.averaged_frames = input.tag.get_property("AVERAGED_FRAMES", config.averaged_frames);
788 // config.least_difference = input.tag.get_property("LEAST_DIFFERENCE", config.least_difference);
789 config.input_rate = Units::fix_framerate(config.input_rate);
794 void Decimate::update_gui()
798 if(load_configuration())
800 ((DecimateWindow*)thread->window)->lock_window("Decimate::update_gui");
801 ((DecimateWindow*)thread->window)->rate->update((float)config.input_rate);
802 // ((DecimateWindow*)thread->window)->difference->update(config.least_difference);
803 // ((DecimateWindow*)thread->window)->avg_difference->update(config.averaged_frames);
804 ((DecimateWindow*)thread->window)->unlock_window();
809 void Decimate::render_gui(void *data)
813 ((DecimateWindow*)thread->window)->lock_window("Decimate::render_gui");
815 int dropped = *(int*)data;
816 char string[BCTEXTLEN];
818 sprintf(string, "%d", dropped);
819 ((DecimateWindow*)thread->window)->last_dropped->update(string);
821 ((DecimateWindow*)thread->window)->unlock_window();