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