remove old quicktime, replaced with current ffmpeg
[goodguy/history.git] / cinelerra-5.0 / plugins / ivtc / ivtc.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 "clip.h"
23 #include "bccmodels.h"
24 #include "filexml.h"
25 #include "ivtc.h"
26 #include "ivtcwindow.h"
27 #include "language.h"
28
29 #include <stdio.h>
30 #include <string.h>
31
32
33 #if 0
34 static const char *pattern_text[] = 
35 {
36         N_("A  B  BC  CD  D"),
37         N_("AB  BC  CD  DE  EF"),
38         N_("Automatic")
39 };
40 #endif
41
42 REGISTER_PLUGIN(IVTCMain)
43
44 IVTCConfig::IVTCConfig()
45 {
46         frame_offset = 0;
47         first_field = 0;
48         automatic = 1;
49         auto_threshold = 2;
50         pattern = IVTCConfig::PULLDOWN32;
51 }
52
53 IVTCMain::IVTCMain(PluginServer *server)
54  : PluginVClient(server)
55 {
56         
57         engine = 0;
58         previous_min = 0x4000000000000000LL;
59         previous_strategy = 0;
60 }
61
62 IVTCMain::~IVTCMain()
63 {
64         
65
66         if(engine)
67         {
68                 if(temp_frame[0]) delete temp_frame[0];
69                 if(temp_frame[1]) delete temp_frame[1];
70                 temp_frame[0] = 0;
71                 temp_frame[1] = 0;
72                 delete engine;
73         }
74 }
75
76 const char* IVTCMain::plugin_title() { return _("Inverse Telecine"); }
77 int IVTCMain::is_realtime() { return 1; }
78
79
80
81 NEW_WINDOW_MACRO(IVTCMain, IVTCWindow)
82
83
84
85 int IVTCMain::load_configuration()
86 {
87         KeyFrame *prev_keyframe;
88
89         prev_keyframe = get_prev_keyframe(get_source_position());
90 // Must also switch between interpolation between keyframes and using first keyframe
91         read_data(prev_keyframe);
92
93         return 0;
94 }
95
96 void IVTCMain::save_data(KeyFrame *keyframe)
97 {
98         FileXML output;
99
100 // cause data to be stored directly in text
101         output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
102         output.tag.set_title("IVTC");
103         output.tag.set_property("FRAME_OFFSET", config.frame_offset);
104         output.tag.set_property("FIRST_FIELD", config.first_field);
105         output.tag.set_property("AUTOMATIC", config.automatic);
106         output.tag.set_property("AUTO_THRESHOLD", config.auto_threshold);
107         output.tag.set_property("PATTERN", config.pattern);
108         output.append_tag();
109         output.terminate_string();
110 }
111
112 void IVTCMain::read_data(KeyFrame *keyframe)
113 {
114         FileXML input;
115
116         input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
117
118         int result = 0;
119         //float new_threshold;
120
121         while(!result)
122         {
123                 result = input.read_tag();
124
125                 if(!result)
126                 {
127                         if(input.tag.title_is("IVTC"))
128                         {
129                                 config.frame_offset = input.tag.get_property("FRAME_OFFSET", config.frame_offset);
130                                 config.first_field = input.tag.get_property("FIRST_FIELD", config.first_field);
131                                 config.automatic = input.tag.get_property("AUTOMATIC", config.automatic);
132                                 //new_threshold = input.tag.get_property("AUTO_THRESHOLD", config.auto_threshold);
133                                 config.pattern = input.tag.get_property("PATTERN", config.pattern);
134                         }
135                 }
136         }
137 }
138
139
140
141 void IVTCMain::render_stop()
142 {
143         previous_min = 0x4000000000000000LL;
144 }
145
146
147
148 // Pattern A B BC CD D
149 int IVTCMain::process_realtime(VFrame *input_ptr, VFrame *output_ptr)
150 {
151         load_configuration();
152
153         if(!engine)
154         {
155                 temp_frame[0] = 0;
156                 temp_frame[1] = 0;
157         
158                 engine = new IVTCEngine(this, smp + 1);
159         }
160
161 // Determine position in pattern
162         int pattern_position = (PluginClient::source_position + config.frame_offset) % 5;
163
164 //printf("IVTCMain::process_realtime %d %d\n", pattern_position, config.first_field);
165         if(!temp_frame[0]) temp_frame[0] = new VFrame(0,
166                 -1,
167                 input_ptr->get_w(),
168                 input_ptr->get_h(),
169                 input_ptr->get_color_model(),
170                 -1);
171         if(!temp_frame[1]) temp_frame[1] = new VFrame(0,
172                 -1,
173                 input_ptr->get_w(),
174                 input_ptr->get_h(),
175                 input_ptr->get_color_model(),
176                 -1);
177
178         int row_size = VFrame::calculate_bytes_per_pixel(input_ptr->get_color_model()) * input_ptr->get_w();
179         this->input = input_ptr;
180         this->output = output_ptr;
181
182 // Determine pattern
183         if(config.pattern == IVTCConfig::PULLDOWN32)
184         {
185                 switch(pattern_position)
186                 {
187 // Direct copy
188                         case 0:
189                         case 4:
190                                 if(input_ptr->get_rows()[0] != output_ptr->get_rows()[0])
191                                         output_ptr->copy_from(input_ptr);
192                                 break;
193
194                         case 1:
195                                 temp_frame[0]->copy_from(input_ptr);
196                                 if(input_ptr->get_rows()[0] != output_ptr->get_rows()[0])
197                                         output_ptr->copy_from(input_ptr);
198                                 break;
199
200                         case 2:
201 // Save one field for next frame.  Reuse previous frame.
202                                 temp_frame[1]->copy_from(input_ptr);
203                                 output_ptr->copy_from(temp_frame[0]);
204                                 break;
205
206                         case 3:
207 // Combine previous field with current field.
208                                 for(int i = 0; i < input_ptr->get_h(); i++)
209                                 {
210                                         unsigned char *dst = output_ptr->get_rows()[i];
211                                         unsigned char *src = ((i + config.first_field) & 1) ?
212                                                         input_ptr->get_rows()[i] :
213                                                         temp_frame[1]->get_rows()[i];
214                                         if( src != dst )
215                                                 memcpy(dst, src, row_size);
216                                 }
217                                 break;
218                 }
219         }
220         else
221         if(config.pattern == IVTCConfig::SHIFTFIELD)
222         {
223                 temp_frame[1]->copy_from(input_ptr);
224
225 // Recycle previous bottom or top
226                 for(int i = 0; i < input_ptr->get_h(); i++)
227                 {
228                         unsigned char *dst = output_ptr->get_rows()[i];
229                         unsigned char *src = ((i + config.first_field) & 1) ?
230                                         input_ptr->get_rows()[i] :
231                                         temp_frame[0]->get_rows()[i];
232                         if( src != dst )
233                                 memcpy(dst, src, row_size);
234                 }
235
236 // Swap temp frames
237                 VFrame *temp = temp_frame[0];
238                 temp_frame[0] = temp_frame[1];
239                 temp_frame[1] = temp;
240         }
241         else
242         if(config.pattern == IVTCConfig::AUTOMATIC)
243         {
244 // Compare averaged rows with original rows and 
245 // with previous rows.
246 // Take rows which are most similar to the averaged rows.
247 // Process frame.
248                 engine->process_packages();
249 // Copy current for future use
250                 temp_frame[1]->copy_from(input_ptr);
251
252 // Add results
253                 even_vs_current = 0;
254                 even_vs_prev = 0;
255                 odd_vs_current = 0;
256                 odd_vs_prev = 0;
257
258                 for(int i = 0; i < engine->get_total_clients(); i++)
259                 {
260                         IVTCUnit *unit = (IVTCUnit*)engine->get_client(i);
261                         even_vs_current += unit->even_vs_current;
262                         even_vs_prev += unit->even_vs_prev;
263                         odd_vs_current += unit->odd_vs_current;
264                         odd_vs_prev += unit->odd_vs_prev;
265                 }
266
267
268                 int64_t min;
269                 int strategy;
270
271
272 // First strategy.
273 // Even lines from previous frame are more similar to 
274 // averaged even lines in current frame.
275 // Take even lines from previous frame
276                 min = even_vs_prev;
277                 strategy = 0;
278
279                 if(even_vs_current < min)
280                 {
281 // Even lines from current frame are more similar to averaged
282 // even lines in current frame than previous combinations.
283 // Take all lines from current frame
284                         min = even_vs_current;
285                         strategy = 2;
286                 }
287
288                 if(min > odd_vs_prev)
289                 {
290 // Odd lines from previous frame are more similar to averaged
291 // odd lines in current frame than previous combinations.
292 // Take odd lines from previous frame
293                         min = odd_vs_prev;
294                         strategy = 1;
295                 }
296
297                 if(min > odd_vs_current)
298                 {
299 // Odd lines from current frame are more similar to averaged
300 // odd lines in current frame than previous combinations.
301 // Take odd lines from current frame.
302                         min = odd_vs_current;
303                         strategy = 2;
304                 }
305
306 //              int confident = 1;
307 // Do something if not confident.
308 // Sometimes we never get the other field.
309 // Currently nothing is done because it doesn't fix the timing.
310 //              if(min > previous_min * 4 && previous_strategy == 2)
311 //              {
312 //                      confident = 0;
313 //                      strategy = 3;
314 //              }
315 // printf("IVTCMain::process_realtime 1: previous_min=%lld min=%lld strategy=%d confident=%d\n",
316 // previous_min,
317 // min,
318 // strategy,
319 // confident);
320
321
322
323 // printf("IVTCMain::process_realtime:\n    even_vs_current=%lld\n    even_vs_prev=%lld\n    odd_vs_current=%lld\n    odd_vs_prev=%lld\n    strategy=%d confident=%d\n",
324 // even_vs_current,
325 // even_vs_prev,
326 // odd_vs_current,
327 // odd_vs_prev,
328 // strategy,
329 // strategy == 2 && !use_direct_copy);
330
331                 switch(strategy)
332                 {
333                         case 0:
334                                 for(int i = 0; i < input_ptr->get_h(); i++)
335                                 {
336                                         unsigned char *dst = output_ptr->get_rows()[i];
337                                         unsigned char *src = (!(i & 1)) ?
338                                                         temp_frame[0]->get_rows()[i] :
339                                                         input_ptr->get_rows()[i];
340                                         if( src != dst )
341                                                 memcpy(dst, src, row_size);
342                                 }
343                                 break;
344                         case 1:
345                                 for(int i = 0; i < input_ptr->get_h(); i++)
346                                 {
347                                         unsigned char *dst = output_ptr->get_rows()[i];
348                                         unsigned char *src = (i & 1) ?
349                                                         temp_frame[0]->get_rows()[i] :
350                                                         input_ptr->get_rows()[i];
351                                         if( src != dst )
352                                                 memcpy(dst, src, row_size);
353                                 }
354                                 break;
355                         case 2:
356                                 output_ptr->copy_from(input_ptr);
357                                 break;
358                         case 3:
359 //                              output_ptr->copy_from(temp_frame[0]);
360 // Deinterlace
361                                 for(int i = 0; i < input_ptr->get_h(); i++)
362                                 {
363                                         unsigned char *dst = output_ptr->get_rows()[i];
364                                         unsigned char *src = (i & 1) ?
365                                                         input_ptr->get_rows()[i - 1] :
366                                                         input_ptr->get_rows()[i];
367                                         if( src != dst )
368                                                 memcpy(dst, src, row_size);
369                                 }
370                                 break;
371                 }
372
373                 previous_min = min;
374                 previous_strategy = strategy;
375                 VFrame *temp = temp_frame[1];
376                 temp_frame[1] = temp_frame[0];
377                 temp_frame[0] = temp;
378         }
379         return 0;
380 }
381
382
383
384 void IVTCMain::update_gui()
385 {
386         if(thread)
387         {
388                 load_configuration();
389                 ((IVTCWindow*)thread->window)->lock_window();
390                 if(config.pattern == IVTCConfig::AUTOMATIC)
391                 {
392                         ((IVTCWindow*)thread->window)->frame_offset->disable();
393                         ((IVTCWindow*)thread->window)->first_field->disable();
394                 }
395                 else
396                 {
397                         ((IVTCWindow*)thread->window)->frame_offset->enable();
398                         ((IVTCWindow*)thread->window)->first_field->enable();
399                 }
400                 ((IVTCWindow*)thread->window)->frame_offset->update((int64_t)config.frame_offset);
401                 ((IVTCWindow*)thread->window)->first_field->update(config.first_field);
402 //              ((IVTCWindow*)thread->window)->automatic->update(config.automatic);
403                 for(int i = 0; i < TOTAL_PATTERNS; i++)
404                 {
405                         ((IVTCWindow*)thread->window)->pattern[i]->update(config.pattern == i);
406                 }
407                 ((IVTCWindow*)thread->window)->unlock_window();
408         }
409 }
410
411
412
413 // labs returns different values on x86_64 causing our accumulators to explode
414 #define ABS local_abs
415
416
417 #ifdef __x86_64__
418
419 static int local_abs(int value)
420 {
421         return (value < 0 ? -value : value);
422 }
423
424 static float local_abs(float value)
425 {
426         return (value < 0 ? -value : value);
427 }
428
429 #else
430
431 static int local_abs(int value)
432 {
433         return abs(value);
434 }
435
436 static float local_abs(float value)
437 {
438         return fabsf(value);
439 }
440
441
442 #endif
443
444
445
446
447 IVTCPackage::IVTCPackage()
448  : LoadPackage()
449 {
450 }
451
452
453
454
455 IVTCUnit::IVTCUnit(IVTCEngine *server, IVTCMain *plugin)
456  : LoadClient(server)
457 {
458         this->server = server;
459         this->plugin = plugin;
460 }
461
462 #define IVTC_MACRO(type, temp_type, components, is_yuv) \
463 { \
464         type **curr_rows = (type**)plugin->input->get_rows(); \
465         type **prev_rows = (type**)plugin->temp_frame[0]->get_rows(); \
466         /* int skip = components - 1; // Components to skip for YUV */\
467         for(int i = ptr->y1; i < ptr->y2; i++) \
468         { /* Rows to average in the input frame */ \
469                 int input_row1_number = i - 1; \
470                 int input_row2_number = i + 1; \
471                 input_row1_number = MAX(0, input_row1_number); \
472                 input_row2_number = MIN(h - 1, input_row2_number); \
473                 type *input_row1 = curr_rows[input_row1_number]; \
474                 type *input_row2 = curr_rows[input_row2_number]; \
475 /* Rows to compare the averaged rows to */ \
476                 type *current_row = curr_rows[i]; \
477                 type *prev_row = prev_rows[i]; \
478                 temp_type current_difference = 0; \
479                 temp_type prev_difference = 0; \
480                 for(int j = 0; j < w; j++) \
481                 { \
482 /* This only compares luminance */ \
483 /* Get average of current rows */ \
484                         temp_type average = ((temp_type)*input_row1 + *input_row2) / 2; \
485 /* Difference between averaged current rows and original inbetween row */ \
486                         current_difference += ABS(average - *current_row); \
487 /* Difference between averaged current rows and previous inbetween row */ \
488                         prev_difference += ABS(average - *prev_row); \
489  \
490 /* Do RGB channels */ \
491                         if(!is_yuv) \
492                         { \
493                                 average = ((temp_type)input_row1[1] + input_row2[1]) / 2; \
494                                 current_difference += ABS(average - current_row[1]); \
495                                 prev_difference += ABS(average - prev_row[1]); \
496                                 average = ((temp_type)input_row1[2] + input_row2[2]) / 2; \
497                                 current_difference += ABS(average - current_row[2]); \
498                                 prev_difference += ABS(average - prev_row[2]); \
499                         } \
500  \
501 /* Add to row accumulators */ \
502                         current_row += components; \
503                         prev_row += components; \
504                         input_row1 += components; \
505                         input_row2 += components; \
506                 } \
507  \
508 /* Store row differences in even or odd variables */ \
509                 if(sizeof(type) == 4) \
510                 { \
511                         if(i % 2) \
512                         { \
513                                 odd_vs_current += (int64_t)(current_difference * 0xffff); \
514                                 odd_vs_prev += (int64_t)(prev_difference); \
515                         } \
516                         else \
517                         { \
518                                 even_vs_current += (int64_t)(current_difference); \
519                                 even_vs_prev += (int64_t)(prev_difference); \
520                         } \
521                 } \
522                 else \
523                 { \
524                         if(i % 2) \
525                         { \
526                                 odd_vs_current += (int64_t)current_difference; \
527                                 odd_vs_prev += (int64_t)prev_difference; \
528                         } \
529                         else \
530                         { \
531                                 even_vs_current += (int64_t)current_difference; \
532                                 even_vs_prev += (int64_t)prev_difference; \
533                         } \
534                 } \
535         } \
536 }
537
538 void IVTCUnit::clear_totals()
539 {
540         even_vs_current = 0;
541         even_vs_prev = 0;
542         odd_vs_current = 0;
543         odd_vs_prev = 0;
544 }
545
546 void IVTCUnit::process_package(LoadPackage *package)
547 {
548         IVTCPackage *ptr = (IVTCPackage*)package;
549         int w = plugin->input->get_w();
550         int h = plugin->input->get_h();
551
552         switch(plugin->input->get_color_model())
553         {
554                 case BC_RGB_FLOAT:
555                         IVTC_MACRO(float, float, 3, 0);
556                         break;
557                 case BC_RGB888:
558                         IVTC_MACRO(unsigned char, int, 3, 0);
559                         break;
560                 case BC_YUV888:
561                         IVTC_MACRO(unsigned char, int, 3, 1);
562                         break;
563                 case BC_RGBA_FLOAT:
564                         IVTC_MACRO(float, float, 4, 0);
565                         break;
566                 case BC_RGBA8888:
567                         IVTC_MACRO(unsigned char, int, 4, 0);
568                         break;
569                 case BC_YUVA8888:
570                         IVTC_MACRO(unsigned char, int, 4, 1);
571                         break;
572                 case BC_RGB161616:
573                         IVTC_MACRO(uint16_t, int, 3, 0);
574                         break;
575                 case BC_YUV161616:
576                         IVTC_MACRO(uint16_t, int, 3, 1);
577                         break;
578                 case BC_RGBA16161616:
579                         IVTC_MACRO(uint16_t, int, 4, 0);
580                         break;
581                 case BC_YUVA16161616:
582                         IVTC_MACRO(uint16_t, int, 4, 1);
583                         break;
584         }
585         
586 }
587
588
589
590
591
592 IVTCEngine::IVTCEngine(IVTCMain *plugin, int cpus)
593  : LoadServer(cpus, cpus)
594 {
595         this->plugin = plugin;
596 }
597
598 IVTCEngine::~IVTCEngine()
599 {
600 }
601
602 void IVTCEngine::init_packages()
603 {
604         int increment = plugin->input->get_h() / get_total_packages();
605         increment /= 2;
606         increment *= 2;
607         if(!increment) increment = 2;
608         int y1 = 0;
609         for(int i = 0; i < get_total_packages(); i++)
610         {
611                 IVTCPackage *package = (IVTCPackage*)get_package(i);
612                 package->y1 = y1;
613                 y1 += increment;
614                 if(y1 > plugin->input->get_h()) y1 = plugin->input->get_h();
615                 package->y2 = y1;
616         }
617         for(int i = 0; i < get_total_clients(); i++)
618         {
619                 IVTCUnit *unit = (IVTCUnit*)get_client(i);
620                 unit->clear_totals();
621         }
622 }
623
624 LoadClient* IVTCEngine::new_client()
625 {
626         return new IVTCUnit(this, plugin);
627 }
628
629 LoadPackage* IVTCEngine::new_package()
630 {
631         return new IVTCPackage;
632 }
633
634
635
636