r1008: pt_BR translation update
[cinelerra_cv/mob.git] / plugins / ivtc / ivtc.C
blobc1eda025187831123f8eee45ebfa3e5f4a1d77ed
1 #include "clip.h"
2 #include "colormodels.h"
3 #include "filexml.h"
4 #include "ivtc.h"
5 #include "ivtcwindow.h"
6 #include "language.h"
8 #include <stdio.h>
9 #include <string.h>
12 static char *pattern_text[] = 
14         N_("A  B  BC  CD  D"),
15         N_("AB  BC  CD  DE  EF"),
16         N_("Automatic")
20 REGISTER_PLUGIN(IVTCMain)
22 IVTCConfig::IVTCConfig()
24         frame_offset = 0;
25         first_field = 0;
26         automatic = 1;
27         auto_threshold = 2;
28         pattern = IVTCConfig::PULLDOWN32;
31 IVTCMain::IVTCMain(PluginServer *server)
32  : PluginVClient(server)
34         PLUGIN_CONSTRUCTOR_MACRO
35         engine = 0;
36         previous_min = 0x4000000000000000LL;
37         previous_strategy = 0;
40 IVTCMain::~IVTCMain()
42         PLUGIN_DESTRUCTOR_MACRO
44         if(engine)
45         {
46                 if(temp_frame[0]) delete temp_frame[0];
47                 if(temp_frame[1]) delete temp_frame[1];
48                 temp_frame[0] = 0;
49                 temp_frame[1] = 0;
50                 delete engine;
51         }
54 char* IVTCMain::plugin_title() { return N_("Inverse Telecine"); }
55 int IVTCMain::is_realtime() { return 1; }
58 int IVTCMain::load_defaults()
60         char directory[BCTEXTLEN], string[BCTEXTLEN];
61 // set the default directory
62         sprintf(directory, "%sivtc.rc", BCASTDIR);
64 // load the defaults
65         defaults = new BC_Hash(directory);
66         defaults->load();
68         config.frame_offset = defaults->get("FRAME_OFFSET", config.frame_offset);
69         config.first_field = defaults->get("FIRST_FIELD", config.first_field);
70         config.automatic = defaults->get("AUTOMATIC", config.automatic);
71         config.auto_threshold = defaults->get("AUTO_THRESHOLD", config.auto_threshold);
72         config.pattern = defaults->get("PATTERN", config.pattern);
73         return 0;
76 int IVTCMain::save_defaults()
78         defaults->update("FRAME_OFFSET", config.frame_offset);
79         defaults->update("FIRST_FIELD", config.first_field);
80         defaults->update("AUTOMATIC", config.automatic);
81         defaults->update("AUTO_THRESHOLD", config.auto_threshold);
82         defaults->update("PATTERN", config.pattern);
83         defaults->save();
84         return 0;
87 #include "picon_png.h"
88 NEW_PICON_MACRO(IVTCMain)
89 SHOW_GUI_MACRO(IVTCMain, IVTCThread)
90 SET_STRING_MACRO(IVTCMain)
91 RAISE_WINDOW_MACRO(IVTCMain)
95 int IVTCMain::load_configuration()
97         KeyFrame *prev_keyframe;
99         prev_keyframe = get_prev_keyframe(get_source_position());
100 // Must also switch between interpolation between keyframes and using first keyframe
101         read_data(prev_keyframe);
103         return 0;
106 void IVTCMain::save_data(KeyFrame *keyframe)
108         FileXML output;
110 // cause data to be stored directly in text
111         output.set_shared_string(keyframe->data, MESSAGESIZE);
112         output.tag.set_title("IVTC");
113         output.tag.set_property("FRAME_OFFSET", config.frame_offset);
114         output.tag.set_property("FIRST_FIELD", config.first_field);
115         output.tag.set_property("AUTOMATIC", config.automatic);
116         output.tag.set_property("AUTO_THRESHOLD", config.auto_threshold);
117         output.tag.set_property("PATTERN", config.pattern);
118         output.append_tag();
119         output.terminate_string();
122 void IVTCMain::read_data(KeyFrame *keyframe)
124         FileXML input;
126         input.set_shared_string(keyframe->data, strlen(keyframe->data));
128         int result = 0;
129         float new_threshold;
131         while(!result)
132         {
133                 result = input.read_tag();
135                 if(!result)
136                 {
137                         if(input.tag.title_is("IVTC"))
138                         {
139                                 config.frame_offset = input.tag.get_property("FRAME_OFFSET", config.frame_offset);
140                                 config.first_field = input.tag.get_property("FIRST_FIELD", config.first_field);
141                                 config.automatic = input.tag.get_property("AUTOMATIC", config.automatic);
142                                 new_threshold = input.tag.get_property("AUTO_THRESHOLD", config.auto_threshold);
143                                 config.pattern = input.tag.get_property("PATTERN", config.pattern);
144                         }
145                 }
146         }
151 void IVTCMain::render_stop()
153         previous_min = 0x4000000000000000LL;
158 // Pattern A B BC CD D
159 int IVTCMain::process_realtime(VFrame *input_ptr, VFrame *output_ptr)
161         load_configuration();
163         if(!engine)
164         {
165                 temp_frame[0] = 0;
166                 temp_frame[1] = 0;
167         
168                 engine = new IVTCEngine(this, smp + 1);
169         }
171 // Determine position in pattern
172         int pattern_position = (PluginClient::source_position + config.frame_offset) % 5;
174 //printf("IVTCMain::process_realtime %d %d\n", pattern_position, config.first_field);
175         if(!temp_frame[0]) temp_frame[0] = new VFrame(0,
176                 input_ptr->get_w(),
177                 input_ptr->get_h(),
178                 input_ptr->get_color_model(),
179                 -1);
180         if(!temp_frame[1]) temp_frame[1] = new VFrame(0,
181                 input_ptr->get_w(),
182                 input_ptr->get_h(),
183                 input_ptr->get_color_model(),
184                 -1);
186         int row_size = VFrame::calculate_bytes_per_pixel(input_ptr->get_color_model()) * input_ptr->get_w();
187         int64_t field1;
188         int64_t field2;
189         int64_t field1_sum;
190         int64_t field2_sum;
191         this->input = input_ptr;
192         this->output = output_ptr;
194 // Determine pattern
195         if(config.pattern == IVTCConfig::PULLDOWN32)
196         {
197                 switch(pattern_position)
198                 {
199 // Direct copy
200                         case 0:
201                         case 4:
202                                 if(input_ptr->get_rows()[0] != output_ptr->get_rows()[0])
203                                         output_ptr->copy_from(input_ptr);
204                                 break;
206                         case 1:
207                                 temp_frame[0]->copy_from(input_ptr);
208                                 if(input_ptr->get_rows()[0] != output_ptr->get_rows()[0])
209                                         output_ptr->copy_from(input_ptr);
210                                 break;
212                         case 2:
213 // Save one field for next frame.  Reuse previous frame.
214                                 temp_frame[1]->copy_from(input_ptr);
215                                 output_ptr->copy_from(temp_frame[0]);
216                                 break;
218                         case 3:
219 // Combine previous field with current field.
220                                 for(int i = 0; i < input_ptr->get_h(); i++)
221                                 {
222                                         if((i + config.first_field) & 1)
223                                                 memcpy(output_ptr->get_rows()[i], 
224                                                         input_ptr->get_rows()[i],
225                                                         row_size);
226                                         else
227                                                 memcpy(output_ptr->get_rows()[i], 
228                                                         temp_frame[1]->get_rows()[i],
229                                                         row_size);
230                                 }
231                                 break;
232                 }
233         }
234         else
235         if(config.pattern == IVTCConfig::SHIFTFIELD)
236         {
237                 temp_frame[1]->copy_from(input_ptr);
239 // Recycle previous bottom or top
240                 for(int i = 0; i < input_ptr->get_h(); i++)
241                 {
242                         if((i + config.first_field) & 1)
243                                 memcpy(output_ptr->get_rows()[i], 
244                                         input_ptr->get_rows()[i],
245                                         row_size);
246                         else
247                                 memcpy(output_ptr->get_rows()[i],
248                                         temp_frame[0]->get_rows()[i],
249                                         row_size);
250                 }
252 // Swap temp frames
253                 VFrame *temp = temp_frame[0];
254                 temp_frame[0] = temp_frame[1];
255                 temp_frame[1] = temp;
256         }
257         else
258         if(config.pattern == IVTCConfig::AUTOMATIC)
259         {
260 // Compare averaged rows with original rows and 
261 // with previous rows.
262 // Take rows which are most similar to the averaged rows.
263 // Process frame.
264                 engine->process_packages();
265 // Copy current for future use
266                 temp_frame[1]->copy_from(input_ptr);
268 // Add results
269                 even_vs_current = 0;
270                 even_vs_prev = 0;
271                 odd_vs_current = 0;
272                 odd_vs_prev = 0;
274                 for(int i = 0; i < engine->get_total_clients(); i++)
275                 {
276                         IVTCUnit *unit = (IVTCUnit*)engine->get_client(i);
277                         even_vs_current += unit->even_vs_current;
278                         even_vs_prev += unit->even_vs_prev;
279                         odd_vs_current += unit->odd_vs_current;
280                         odd_vs_prev += unit->odd_vs_prev;
281                 }
284                 int64_t min;
285                 int strategy;
288 // First strategy.
289 // Even lines from previous frame are more similar to 
290 // averaged even lines in current frame.
291 // Take even lines from previous frame
292                 min = even_vs_prev;
293                 strategy = 0;
295                 if(even_vs_current < min)
296                 {
297 // Even lines from current frame are more similar to averaged
298 // even lines in current frame than previous combinations.
299 // Take all lines from current frame
300                         min = even_vs_current;
301                         strategy = 2;
302                 }
304                 if(min > odd_vs_prev)
305                 {
306 // Odd lines from previous frame are more similar to averaged
307 // odd lines in current frame than previous combinations.
308 // Take odd lines from previous frame
309                         min = odd_vs_prev;
310                         strategy = 1;
311                 }
313                 if(min > odd_vs_current)
314                 {
315 // Odd lines from current frame are more similar to averaged
316 // odd lines in current frame than previous combinations.
317 // Take odd lines from current frame.
318                         min = odd_vs_current;
319                         strategy = 2;
320                 }
322                 int confident = 1;
323 // Do something if not confident.
324 // Sometimes we never get the other field.
325 // Currently nothing is done because it doesn't fix the timing.
326                 if(min > previous_min * 4 && previous_strategy == 2)
327                 {
328                         confident = 0;
329 //                      strategy = 3;
330                 }
331 // printf("IVTCMain::process_realtime 1: previous_min=%lld min=%lld strategy=%d confident=%d\n",
332 // previous_min,
333 // min,
334 // strategy,
335 // confident);
339 // 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",
340 // even_vs_current,
341 // even_vs_prev,
342 // odd_vs_current,
343 // odd_vs_prev,
344 // strategy,
345 // strategy == 2 && !use_direct_copy);
347                 switch(strategy)
348                 {
349                         case 0:
350                                 for(int i = 0; i < input_ptr->get_h(); i++)
351                                 {
352                                         if(!(i & 1))
353                                                 memcpy(output_ptr->get_rows()[i], 
354                                                         temp_frame[0]->get_rows()[i],
355                                                         row_size);
356                                         else
357                                                 memcpy(output_ptr->get_rows()[i],
358                                                         input_ptr->get_rows()[i],
359                                                         row_size);
360                                 }
361                                 break;
362                         case 1:
363                                 for(int i = 0; i < input_ptr->get_h(); i++)
364                                 {
365                                         if(i & 1)
366                                                 memcpy(output_ptr->get_rows()[i], 
367                                                         temp_frame[0]->get_rows()[i],
368                                                         row_size);
369                                         else
370                                                 memcpy(output_ptr->get_rows()[i],
371                                                         input_ptr->get_rows()[i],
372                                                         row_size);
373                                 }
374                                 break;
375                         case 2:
376                                 output_ptr->copy_from(input_ptr);
377                                 break;
378                         case 3:
379 //                              output_ptr->copy_from(temp_frame[0]);
380 // Deinterlace
381                                 for(int i = 0; i < input_ptr->get_h(); i++)
382                                 {
383                                         if(i & 1)
384                                                 memcpy(output_ptr->get_rows()[i], 
385                                                         input_ptr->get_rows()[i - 1],
386                                                         row_size);
387                                         else
388                                                 memcpy(output_ptr->get_rows()[i],
389                                                         input_ptr->get_rows()[i],
390                                                         row_size);
391                                 }
392                                 break;
393                 }
395                 previous_min = min;
396                 previous_strategy = strategy;
397                 VFrame *temp = temp_frame[1];
398                 temp_frame[1] = temp_frame[0];
399                 temp_frame[0] = temp;
400         }
401         return 0;
406 void IVTCMain::update_gui()
408         if(thread)
409         {
410                 load_configuration();
411                 thread->window->lock_window();
412                 if(config.pattern == IVTCConfig::AUTOMATIC)
413                 {
414                         thread->window->frame_offset->disable();
415                         thread->window->first_field->disable();
416                 }
417                 else
418                 {
419                         thread->window->frame_offset->enable();
420                         thread->window->first_field->enable();
421                 }
422                 thread->window->frame_offset->update((int64_t)config.frame_offset);
423                 thread->window->first_field->update(config.first_field);
424 //              thread->window->automatic->update(config.automatic);
425                 for(int i = 0; i < TOTAL_PATTERNS; i++)
426                 {
427                         thread->window->pattern[i]->update(config.pattern == i);
428                 }
429                 thread->window->unlock_window();
430         }
435 // labs returns different values on x86_64 causing our accumulators to explode
436 #define ABS local_abs
439 #ifdef __x86_64__
441 static int local_abs(int value)
443         return (value < 0 ? -value : value);
446 static float local_abs(float value)
448         return (value < 0 ? -value : value);
451 #else
453 static int local_abs(int value)
455         return abs(value);
458 static float local_abs(float value)
460         return fabsf(value);
464 #endif
469 IVTCPackage::IVTCPackage()
470  : LoadPackage()
477 IVTCUnit::IVTCUnit(IVTCEngine *server, IVTCMain *plugin)
478  : LoadClient(server)
480         this->server = server;
481         this->plugin = plugin;
484 #define IVTC_MACRO(type, temp_type, components, is_yuv) \
485 { \
486         type **curr_rows = (type**)plugin->input->get_rows(); \
487         type **prev_rows = (type**)plugin->temp_frame[0]->get_rows(); \
488 /* Components to skip for YUV */ \
489         int skip = components - 1; \
491         for(int i = ptr->y1; i < ptr->y2; i++) \
492         { \
493 /* Rows to average in the input frame */ \
494                 int input_row1_number = i - 1; \
495                 int input_row2_number = i + 1; \
496                 input_row1_number = MAX(0, input_row1_number); \
497                 input_row2_number = MIN(h - 1, input_row2_number); \
498                 type *input_row1 = curr_rows[input_row1_number]; \
499                 type *input_row2 = curr_rows[input_row2_number]; \
501 /* Rows to compare the averaged rows to */ \
502                 type *current_row = curr_rows[i]; \
503                 type *prev_row = prev_rows[i]; \
505                 temp_type current_difference = 0; \
506                 temp_type prev_difference = 0; \
507                 for(int j = 0; j < w; j++) \
508                 { \
509 /* This only compares luminance */ \
510 /* Get average of current rows */ \
511                         temp_type average = ((temp_type)*input_row1 + *input_row2) / 2; \
512 /* Difference between averaged current rows and original inbetween row */ \
513                         current_difference += ABS(average - *current_row); \
514 /* Difference between averaged current rows and previous inbetween row */ \
515                         prev_difference += ABS(average - *prev_row); \
517 /* Do RGB channels */ \
518                         if(!is_yuv) \
519                         { \
520                                 average = ((temp_type)input_row1[1] + input_row2[1]) / 2; \
521                                 current_difference += ABS(average - current_row[1]); \
522                                 prev_difference += ABS(average - prev_row[1]); \
523                                 average = ((temp_type)input_row1[2] + input_row2[2]) / 2; \
524                                 current_difference += ABS(average - current_row[2]); \
525                                 prev_difference += ABS(average - prev_row[2]); \
526                         } \
528 /* Add to row accumulators */ \
529                         current_row += components; \
530                         prev_row += components; \
531                         input_row1 += components; \
532                         input_row2 += components; \
533                 } \
535 /* Store row differences in even or odd variables */ \
536                 if(sizeof(type) == 4) \
537                 { \
538                         if(i % 2) \
539                         { \
540                                 odd_vs_current += (int64_t)(current_difference * 0xffff); \
541                                 odd_vs_prev += (int64_t)(prev_difference); \
542                         } \
543                         else \
544                         { \
545                                 even_vs_current += (int64_t)(current_difference); \
546                                 even_vs_prev += (int64_t)(prev_difference); \
547                         } \
548                 } \
549                 else \
550                 { \
551                         if(i % 2) \
552                         { \
553                                 odd_vs_current += (int64_t)current_difference; \
554                                 odd_vs_prev += (int64_t)prev_difference; \
555                         } \
556                         else \
557                         { \
558                                 even_vs_current += (int64_t)current_difference; \
559                                 even_vs_prev += (int64_t)prev_difference; \
560                         } \
561                 } \
562         } \
565 void IVTCUnit::clear_totals()
567         even_vs_current = 0;
568         even_vs_prev = 0;
569         odd_vs_current = 0;
570         odd_vs_prev = 0;
573 void IVTCUnit::process_package(LoadPackage *package)
575         IVTCPackage *ptr = (IVTCPackage*)package;
576         int w = plugin->input->get_w();
577         int h = plugin->input->get_h();
579         switch(plugin->input->get_color_model())
580         {
581                 case BC_RGB_FLOAT:
582                         IVTC_MACRO(float, float, 3, 0);
583                         break;
584                 case BC_RGB888:
585                         IVTC_MACRO(unsigned char, int, 3, 0);
586                         break;
587                 case BC_YUV888:
588                         IVTC_MACRO(unsigned char, int, 3, 1);
589                         break;
590                 case BC_RGBA_FLOAT:
591                         IVTC_MACRO(float, float, 4, 0);
592                         break;
593                 case BC_RGBA8888:
594                         IVTC_MACRO(unsigned char, int, 4, 0);
595                         break;
596                 case BC_YUVA8888:
597                         IVTC_MACRO(unsigned char, int, 4, 1);
598                         break;
599                 case BC_RGB161616:
600                         IVTC_MACRO(uint16_t, int, 3, 0);
601                         break;
602                 case BC_YUV161616:
603                         IVTC_MACRO(uint16_t, int, 3, 1);
604                         break;
605                 case BC_RGBA16161616:
606                         IVTC_MACRO(uint16_t, int, 4, 0);
607                         break;
608                 case BC_YUVA16161616:
609                         IVTC_MACRO(uint16_t, int, 4, 1);
610                         break;
611         }
612         
619 IVTCEngine::IVTCEngine(IVTCMain *plugin, int cpus)
620  : LoadServer(cpus, cpus)
622         this->plugin = plugin;
625 IVTCEngine::~IVTCEngine()
629 void IVTCEngine::init_packages()
631         int increment = plugin->input->get_h() / get_total_packages();
632         increment /= 2;
633         increment *= 2;
634         if(!increment) increment = 2;
635         int y1 = 0;
636         for(int i = 0; i < get_total_packages(); i++)
637         {
638                 IVTCPackage *package = (IVTCPackage*)get_package(i);
639                 package->y1 = y1;
640                 y1 += increment;
641                 if(y1 > plugin->input->get_h()) y1 = plugin->input->get_h();
642                 package->y2 = y1;
643         }
644         for(int i = 0; i < get_total_clients(); i++)
645         {
646                 IVTCUnit *unit = (IVTCUnit*)get_client(i);
647                 unit->clear_totals();
648         }
651 LoadClient* IVTCEngine::new_client()
653         return new IVTCUnit(this, plugin);
656 LoadPackage* IVTCEngine::new_package()
658         return new IVTCPackage;