edl plugin names eng, fix segv for opengl brender, renderfarm rework strategy, perf...
[goodguy/history.git] / cinelerra-5.1 / plugins / decimate / decimate.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #include "bcdisplayinfo.h"
23 #include "clip.h"
24 #include "bchash.h"
25 #include "filexml.h"
26 #include "guicast.h"
27 #include "keyframe.h"
28 #include "language.h"
29 #include "pluginvclient.h"
30 #include "vframe.h"
31
32 #include <string.h>
33 #include <stdint.h>
34
35
36 #define TOP_FIELD_FIRST 0
37 #define BOTTOM_FIELD_FIRST 1
38 #define TOTAL_FRAMES 10
39
40 class Decimate;
41 class DecimateWindow;
42
43
44 class DecimateConfig
45 {
46 public:
47         DecimateConfig();
48         void copy_from(DecimateConfig *config);
49         int equivalent(DecimateConfig *config);
50
51         double input_rate;
52 // Averaged frames is useless.  Some of the frames are permanently
53 // destroyed in conversion from PAL to NTSC.
54         int averaged_frames;
55         int least_difference;
56 };
57
58
59
60
61 class DecimateRate : public BC_TextBox
62 {
63 public:
64         DecimateRate(Decimate *plugin,
65                 DecimateWindow *gui,
66                 int x,
67                 int y);
68         int handle_event();
69         Decimate *plugin;
70         DecimateWindow *gui;
71 };
72
73 class DecimateRateMenu : public BC_ListBox
74 {
75 public:
76         DecimateRateMenu(Decimate *plugin,
77                 DecimateWindow *gui,
78                 int x,
79                 int y);
80         int handle_event();
81         Decimate *plugin;
82         DecimateWindow *gui;
83 };
84
85 class DecimateDifference : public BC_CheckBox
86 {
87 public:
88         DecimateDifference(Decimate *plugin,
89                 int x,
90                 int y);
91         int handle_event();
92         Decimate *plugin;
93 };
94
95 class DecimateAvgDifference : public BC_CheckBox
96 {
97 public:
98         DecimateAvgDifference(Decimate *plugin,
99                 int x,
100                 int y);
101         int handle_event();
102         Decimate *plugin;
103 };
104
105
106 class DecimateWindow : public PluginClientWindow
107 {
108 public:
109         DecimateWindow(Decimate *plugin);
110         ~DecimateWindow();
111
112         void create_objects();
113
114         ArrayList<BC_ListBoxItem*> frame_rates;
115         Decimate *plugin;
116         DecimateRate *rate;
117         DecimateRateMenu *rate_menu;
118         BC_Title *last_dropped;
119 //      DecimateDifference *difference;
120 //      DecimateAvgDifference *avg_difference;
121 };
122
123
124
125
126
127 class Decimate : public PluginVClient
128 {
129 public:
130         Decimate(PluginServer *server);
131         ~Decimate();
132
133         PLUGIN_CLASS_MEMBERS(DecimateConfig)
134
135         int process_buffer(VFrame *frame,
136                 int64_t start_position,
137                 double frame_rate);
138         int is_realtime();
139         void save_data(KeyFrame *keyframe);
140         void read_data(KeyFrame *keyframe);
141         void update_gui();
142         void render_gui(void *data);
143
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();
148         void init_fdct();
149         void fdct(uint16_t *block);
150         int64_t calculate_fdct(VFrame *frame);
151
152 // fdct coefficients
153         double c[8][8];
154         int fdct_ready;
155
156 // each difference is the difference between the previous frame and the
157 // subscripted frame
158         int64_t differences[TOTAL_FRAMES];
159
160 // read ahead number of frames
161         VFrame *frames[TOTAL_FRAMES];
162 // Number of frames in the lookahead buffer
163         int lookahead_size;
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;
170
171 };
172
173
174
175
176
177
178
179
180
181
182
183
184 DecimateConfig::DecimateConfig()
185 {
186         input_rate = (double)30000 / 1001;
187         least_difference = 1;
188         averaged_frames = 0;
189 }
190
191 void DecimateConfig::copy_from(DecimateConfig *config)
192 {
193         this->input_rate = config->input_rate;
194         this->least_difference = config->least_difference;
195         this->averaged_frames = config->averaged_frames;
196 }
197
198 int DecimateConfig::equivalent(DecimateConfig *config)
199 {
200         return EQUIV(this->input_rate, config->input_rate);
201 }
202
203
204
205
206
207
208
209
210
211 DecimateWindow::DecimateWindow(Decimate *plugin)
212  : PluginClientWindow(plugin,
213         210,
214         160,
215         200,
216         160,
217         0)
218 {
219         this->plugin = plugin;
220 }
221
222 DecimateWindow::~DecimateWindow()
223 {
224         frame_rates.remove_all_objects();
225 }
226
227 void DecimateWindow::create_objects()
228 {
229         int x = 10, y = 10;
230
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"));
244
245         BC_Title *title;
246         add_subwindow(title = new BC_Title(x, y, _("Input frames per second:")));
247         y += 30;
248         add_subwindow(rate = new DecimateRate(plugin,
249                 this,
250                 x,
251                 y));
252         add_subwindow(rate_menu = new DecimateRateMenu(plugin,
253                 this,
254                 x + rate->get_w() + 5,
255                 y));
256         y += 30;
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, ""));
259
260 //      y += 30;
261 //      add_subwindow(difference = new DecimateDifference(plugin,
262 //              x,
263 //              y));
264 //      y += 30;
265 //      add_subwindow(avg_difference = new DecimateAvgDifference(plugin,
266 //              x,
267 //              y));
268         show_window();
269         flush();
270 }
271
272
273
274
275
276
277
278
279
280
281
282
283
284 DecimateRate::DecimateRate(Decimate *plugin,
285         DecimateWindow *gui,
286         int x,
287         int y)
288  : BC_TextBox(x,
289         y,
290         90,
291         1,
292         (float)plugin->config.input_rate)
293 {
294         this->plugin = plugin;
295         this->gui = gui;
296 }
297
298 int DecimateRate::handle_event()
299 {
300         plugin->config.input_rate = Units::atoframerate(get_text());
301         plugin->send_configure_change();
302         return 1;
303 }
304
305
306
307 // DecimateDifference::DecimateDifference(Decimate *plugin,
308 //      int x,
309 //      int y)
310 //  : BC_CheckBox(x, y, plugin->config.least_difference, "Drop least difference")
311 // {
312 //      this->plugin = plugin;
313 // }
314 // int DecimateDifference::handle_event()
315 // {
316 //      plugin->config.least_difference = get_value();
317 //      plugin->send_configure_change();
318 //      return 1;
319 // }
320 //
321 //
322 //
323 //
324 // DecimateAvgDifference::DecimateAvgDifference(Decimate *plugin,
325 //      int x,
326 //      int y)
327 //  : BC_CheckBox(x, y, plugin->config.averaged_frames, "Drop averaged frames")
328 // {
329 //      this->plugin = plugin;
330 // }
331 //
332 // int DecimateAvgDifference::handle_event()
333 // {
334 //      plugin->config.averaged_frames = get_value();
335 //      plugin->send_configure_change();
336 //      return 1;
337 // }
338 //
339
340
341
342 DecimateRateMenu::DecimateRateMenu(Decimate *plugin,
343         DecimateWindow *gui,
344         int x,
345         int y)
346  : BC_ListBox(x,
347         y,
348         100,
349         200,
350         LISTBOX_TEXT,
351         &gui->frame_rates,
352         0,
353         0,
354         1,
355         0,
356         1)
357 {
358         this->plugin = plugin;
359         this->gui = gui;
360 }
361
362 int DecimateRateMenu::handle_event()
363 {
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();
368         return 1;
369 }
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391 REGISTER_PLUGIN(Decimate)
392
393
394
395
396
397
398 Decimate::Decimate(PluginServer *server)
399  : PluginVClient(server)
400 {
401
402         bzero(frames, sizeof(VFrame*) * TOTAL_FRAMES);
403         for(int i = 0; i < TOTAL_FRAMES; i++)
404                 differences[i] = -1;
405         lookahead_size = 0;
406         lookahead_end = -1;
407         last_position = -1;
408         fdct_ready = 0;
409 }
410
411
412 Decimate::~Decimate()
413 {
414
415         if(frames[0])
416         {
417                 for(int i = 0; i < TOTAL_FRAMES; i++)
418                 {
419                         delete frames[i];
420                 }
421         }
422 }
423
424 #define DIFFERENCE_MACRO(type, temp_type, components) \
425 { \
426         temp_type result2 = 0; \
427         for(int i = 0; i < h; i++) \
428         { \
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++) \
432                 { \
433                         temp_type temp = *row1 - *row2; \
434                         result2 += (temp > 0 ? temp : -temp); \
435                         row1++; \
436                         row2++; \
437                 } \
438         } \
439         result = (int64_t)result2; \
440 }
441
442 int64_t Decimate::calculate_difference(VFrame *frame1, VFrame *frame2)
443 {
444         int w = frame1->get_w();
445         int h = frame1->get_h();
446         int64_t result = 0;
447         switch(frame1->get_color_model())
448         {
449                 case BC_RGB888:
450                 case BC_YUV888:
451                         DIFFERENCE_MACRO(unsigned char, int64_t, 3);
452                         break;
453                 case BC_RGB_FLOAT:
454                         DIFFERENCE_MACRO(float, double, 3);
455                         break;
456                 case BC_RGBA8888:
457                 case BC_YUVA8888:
458                         DIFFERENCE_MACRO(unsigned char, int64_t, 4);
459                         break;
460                 case BC_RGBA_FLOAT:
461                         DIFFERENCE_MACRO(float, double, 4);
462                         break;
463                 case BC_RGB161616:
464                 case BC_YUV161616:
465                         DIFFERENCE_MACRO(uint16_t, int64_t, 3);
466                         break;
467                 case BC_RGBA16161616:
468                 case BC_YUVA16161616:
469                         DIFFERENCE_MACRO(uint16_t, int64_t, 4);
470                         break;
471         }
472         return result;
473 }
474
475 void Decimate::init_fdct()
476 {
477   int i, j;
478   double s;
479
480   for (i=0; i<8; i++)
481   {
482     s = (i==0) ? sqrt(0.125) : 0.5;
483
484     for (j=0; j<8; j++)
485       c[i][j] = s * cos((M_PI/8.0)*i*(j+0.5));
486   }
487 }
488
489 void Decimate::fdct(uint16_t *block)
490 {
491         int i, j;
492         double s;
493         double tmp[64];
494
495         for(i = 0; i < 8; i++)
496         for(j = 0; j < 8; j++)
497         {
498                 s = 0.0;
499
500 /*
501  *              for(k = 0; k < 8; k++)
502  *                      s += c[j][k] * block[8 * i + k];
503  */
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];
512
513                 tmp[8 * i + j] = s;
514         }
515
516         for(j = 0; j < 8; j++)
517         for(i = 0; i < 8; i++)
518         {
519                 s = 0.0;
520
521 /*
522  *              for(k = 0; k < 8; k++)
523  *                  s += c[i][k] * tmp[8 * k + j];
524  */
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];
533
534                 block[8 * i + j] = (int)floor(s + 0.499999);
535 /*
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
542  * (if at all)
543  */
544       }
545 }
546
547
548 #define CALCULATE_DCT(type, components) \
549 { \
550         uint16_t *output = temp; \
551         for(int k = 0; k < 8; k++) \
552         { \
553                 type *input = (type*)frame->get_rows()[i + k] + j * components; \
554                 for(int l = 0; l < 8; l++) \
555                 { \
556                         *output = (*input << 8) | *input; \
557                         output++; \
558                         input += components; \
559                 } \
560         } \
561         fdct(temp); \
562 }
563
564 int64_t Decimate::calculate_fdct(VFrame *frame)
565 {
566         if(!fdct_ready)
567         {
568                 init_fdct();
569                 fdct_ready = 1;
570         }
571
572         uint16_t temp[64];
573         uint64_t result[64];
574         bzero(result, sizeof(int64_t) * 64);
575         int w = frame->get_w();
576         int h = frame->get_h();
577
578
579         for(int i = 0; i < h - 8; i += 8)
580         {
581                 for(int j = 0; j < w - 8; j += 8)
582                 {
583                         CALCULATE_DCT(unsigned char, 3)
584 // Add result to accumulation of transforms
585                         for(int k = 0; k < 64; k++)
586                         {
587                                 result[k] += temp[k];
588                         }
589                 }
590         }
591
592         uint64_t max_result = 0;
593         int highest = 0;
594         for(int i = 0; i < 64; i++)
595         {
596                 if(result[i] > max_result)
597                 {
598                         max_result = result[i];
599                         highest = i;
600                 }
601         }
602
603         return highest;
604 }
605
606 void Decimate::decimate_frame()
607 {
608         int64_t min_difference = 0x7fffffffffffffffLL;
609         int result = -1;
610
611         if(!lookahead_size) return;
612
613         for(int i = 0; i < lookahead_size; i++)
614         {
615 // Drop least different frame from sequence
616                 if(config.least_difference &&
617                         differences[i] >= 0 &&
618                         differences[i] < min_difference)
619                 {
620                         min_difference = differences[i];
621                         result = i;
622                 }
623         }
624
625 // If all the frames had differences of 0, like a pure black screen, delete
626 // the first frame.
627         if(result < 0) result = 0;
628
629         VFrame *temp = frames[result];
630         for(int i = result; i < lookahead_size - 1; i++)
631         {
632                 frames[i] = frames[i + 1];
633                 differences[i] = differences[i + 1];
634         }
635
636
637         frames[lookahead_size - 1] = temp;
638         lookahead_size--;
639         send_render_gui(&result);
640 }
641
642 void Decimate::fill_lookahead(double frame_rate,
643         int64_t start_position)
644 {
645 // Lookahead rate changed
646         if(!EQUIV(config.input_rate, lookahead_rate))
647         {
648                 lookahead_size = 0;
649         }
650
651         lookahead_rate = config.input_rate;
652
653 // Start position is not contiguous with last request
654         if(last_position + 1 != start_position)
655         {
656                 lookahead_size = 0;
657         }
658
659         last_position = start_position;
660
661 // Normalize requested position to input rate
662         if(!lookahead_size)
663         {
664                 lookahead_end = (int64_t)((double)start_position *
665                         config.input_rate /
666                         frame_rate);
667         }
668
669         while(lookahead_size < TOTAL_FRAMES)
670         {
671 // Import frame into next lookahead slot
672                 read_frame(frames[lookahead_size],
673                         0,
674                         lookahead_end,
675                         config.input_rate,
676                         0);
677 // Fill difference buffer
678                 if(lookahead_size > 0)
679                         differences[lookahead_size] =
680                                 calculate_difference(frames[lookahead_size - 1],
681                                         frames[lookahead_size]);
682
683 // Increase counters relative to input rate
684                 lookahead_size++;
685                 lookahead_end++;
686
687 // Decimate one if last frame in buffer and lookahead_end is behind predicted
688 // end.
689                 int64_t decimated_end = (int64_t)((double)(start_position + TOTAL_FRAMES) *
690                         config.input_rate /
691                         frame_rate);
692                 if(lookahead_size >= TOTAL_FRAMES &&
693                         lookahead_end < decimated_end)
694                 {
695                         decimate_frame();
696                 }
697         }
698 }
699
700
701 int Decimate::process_buffer(VFrame *frame,
702         int64_t start_position,
703         double frame_rate)
704 {
705
706 //printf("Decimate::process_buffer 1 %lld %f\n", start_position, frame_rate);
707         load_configuration();
708
709         if(!frames[0])
710         {
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);
714                 }
715         }
716
717
718 // Fill lookahead buffer at input rate with decimation
719         fill_lookahead(frame_rate, start_position);
720
721 // printf("Decimate::process_buffer");
722 // for(int i = 0; i < TOTAL_FRAMES; i++)
723 // printf(" %lld", differences[i]);
724 // printf("\n");
725
726
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++)
731         {
732                 frames[i] = frames[i + 1];
733                 differences[i] = differences[i + 1];
734         }
735         frames[TOTAL_FRAMES - 1] = temp;
736         lookahead_size--;
737         return 0;
738 }
739
740
741
742 const char* Decimate::plugin_title() { return N_("Decimate"); }
743 int Decimate::is_realtime() { return 1; }
744
745 NEW_WINDOW_MACRO(Decimate, DecimateWindow)
746
747
748 int Decimate::load_configuration()
749 {
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);
756 }
757
758
759 void Decimate::save_data(KeyFrame *keyframe)
760 {
761         FileXML output;
762
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);
769         output.append_tag();
770         output.tag.set_title("/DECIMATE");
771         output.append_tag();
772         output.append_newline();
773         output.terminate_string();
774 }
775
776 void Decimate::read_data(KeyFrame *keyframe)
777 {
778         FileXML input;
779
780         input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
781
782         while(!input.read_tag())
783         {
784                 if(input.tag.title_is("DECIMATE"))
785                 {
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);
790                 }
791         }
792 }
793
794 void Decimate::update_gui()
795 {
796         if(thread)
797         {
798                 if(load_configuration())
799                 {
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();
805                 }
806         }
807 }
808
809 void Decimate::render_gui(void *data)
810 {
811         if(thread)
812         {
813                 ((DecimateWindow*)thread->window)->lock_window("Decimate::render_gui");
814
815                 int dropped = *(int*)data;
816                 char string[BCTEXTLEN];
817
818                 sprintf(string, "%d", dropped);
819                 ((DecimateWindow*)thread->window)->last_dropped->update(string);
820
821                 ((DecimateWindow*)thread->window)->unlock_window();
822         }
823 }
824
825
826