2 #include "colormodels.h"
5 #include "ivtcwindow.h"
12 static char *pattern_text[] =
20 REGISTER_PLUGIN(IVTCMain)
22 IVTCConfig::IVTCConfig()
28 pattern = IVTCConfig::PULLDOWN32;
31 IVTCMain::IVTCMain(PluginServer *server)
32 : PluginVClient(server)
34 PLUGIN_CONSTRUCTOR_MACRO
36 previous_min = 0x4000000000000000LL;
37 previous_strategy = 0;
42 PLUGIN_DESTRUCTOR_MACRO
46 if(temp_frame[0]) delete temp_frame[0];
47 if(temp_frame[1]) delete temp_frame[1];
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);
65 defaults = new BC_Hash(directory);
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);
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);
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);
106 void IVTCMain::save_data(KeyFrame *keyframe)
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);
119 output.terminate_string();
122 void IVTCMain::read_data(KeyFrame *keyframe)
126 input.set_shared_string(keyframe->data, strlen(keyframe->data));
133 result = input.read_tag();
137 if(input.tag.title_is("IVTC"))
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);
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();
168 engine = new IVTCEngine(this, smp + 1);
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,
178 input_ptr->get_color_model(),
180 if(!temp_frame[1]) temp_frame[1] = new VFrame(0,
183 input_ptr->get_color_model(),
186 int row_size = VFrame::calculate_bytes_per_pixel(input_ptr->get_color_model()) * input_ptr->get_w();
191 this->input = input_ptr;
192 this->output = output_ptr;
195 if(config.pattern == IVTCConfig::PULLDOWN32)
197 switch(pattern_position)
202 if(input_ptr->get_rows()[0] != output_ptr->get_rows()[0])
203 output_ptr->copy_from(input_ptr);
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);
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]);
219 // Combine previous field with current field.
220 for(int i = 0; i < input_ptr->get_h(); i++)
222 if((i + config.first_field) & 1)
223 memcpy(output_ptr->get_rows()[i],
224 input_ptr->get_rows()[i],
227 memcpy(output_ptr->get_rows()[i],
228 temp_frame[1]->get_rows()[i],
235 if(config.pattern == IVTCConfig::SHIFTFIELD)
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++)
242 if((i + config.first_field) & 1)
243 memcpy(output_ptr->get_rows()[i],
244 input_ptr->get_rows()[i],
247 memcpy(output_ptr->get_rows()[i],
248 temp_frame[0]->get_rows()[i],
253 VFrame *temp = temp_frame[0];
254 temp_frame[0] = temp_frame[1];
255 temp_frame[1] = temp;
258 if(config.pattern == IVTCConfig::AUTOMATIC)
260 // Compare averaged rows with original rows and
261 // with previous rows.
262 // Take rows which are most similar to the averaged rows.
264 engine->process_packages();
265 // Copy current for future use
266 temp_frame[1]->copy_from(input_ptr);
274 for(int i = 0; i < engine->get_total_clients(); i++)
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;
289 // Even lines from previous frame are more similar to
290 // averaged even lines in current frame.
291 // Take even lines from previous frame
295 if(even_vs_current < min)
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;
304 if(min > odd_vs_prev)
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
313 if(min > odd_vs_current)
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;
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)
331 // printf("IVTCMain::process_realtime 1: previous_min=%lld min=%lld strategy=%d confident=%d\n",
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",
345 // strategy == 2 && !use_direct_copy);
350 for(int i = 0; i < input_ptr->get_h(); i++)
353 memcpy(output_ptr->get_rows()[i],
354 temp_frame[0]->get_rows()[i],
357 memcpy(output_ptr->get_rows()[i],
358 input_ptr->get_rows()[i],
363 for(int i = 0; i < input_ptr->get_h(); i++)
366 memcpy(output_ptr->get_rows()[i],
367 temp_frame[0]->get_rows()[i],
370 memcpy(output_ptr->get_rows()[i],
371 input_ptr->get_rows()[i],
376 output_ptr->copy_from(input_ptr);
379 // output_ptr->copy_from(temp_frame[0]);
381 for(int i = 0; i < input_ptr->get_h(); i++)
384 memcpy(output_ptr->get_rows()[i],
385 input_ptr->get_rows()[i - 1],
388 memcpy(output_ptr->get_rows()[i],
389 input_ptr->get_rows()[i],
396 previous_strategy = strategy;
397 VFrame *temp = temp_frame[1];
398 temp_frame[1] = temp_frame[0];
399 temp_frame[0] = temp;
406 void IVTCMain::update_gui()
410 load_configuration();
411 thread->window->lock_window();
412 if(config.pattern == IVTCConfig::AUTOMATIC)
414 thread->window->frame_offset->disable();
415 thread->window->first_field->disable();
419 thread->window->frame_offset->enable();
420 thread->window->first_field->enable();
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++)
427 thread->window->pattern[i]->update(config.pattern == i);
429 thread->window->unlock_window();
435 // labs returns different values on x86_64 causing our accumulators to explode
436 #define ABS local_abs
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);
453 static int local_abs(int value)
458 static float local_abs(float value)
469 IVTCPackage::IVTCPackage()
477 IVTCUnit::IVTCUnit(IVTCEngine *server, IVTCMain *plugin)
480 this->server = server;
481 this->plugin = plugin;
484 #define IVTC_MACRO(type, temp_type, components, is_yuv) \
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++) \
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++) \
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 */ \
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]); \
528 /* Add to row accumulators */ \
529 current_row += components; \
530 prev_row += components; \
531 input_row1 += components; \
532 input_row2 += components; \
535 /* Store row differences in even or odd variables */ \
536 if(sizeof(type) == 4) \
540 odd_vs_current += (int64_t)(current_difference * 0xffff); \
541 odd_vs_prev += (int64_t)(prev_difference); \
545 even_vs_current += (int64_t)(current_difference); \
546 even_vs_prev += (int64_t)(prev_difference); \
553 odd_vs_current += (int64_t)current_difference; \
554 odd_vs_prev += (int64_t)prev_difference; \
558 even_vs_current += (int64_t)current_difference; \
559 even_vs_prev += (int64_t)prev_difference; \
565 void IVTCUnit::clear_totals()
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())
582 IVTC_MACRO(float, float, 3, 0);
585 IVTC_MACRO(unsigned char, int, 3, 0);
588 IVTC_MACRO(unsigned char, int, 3, 1);
591 IVTC_MACRO(float, float, 4, 0);
594 IVTC_MACRO(unsigned char, int, 4, 0);
597 IVTC_MACRO(unsigned char, int, 4, 1);
600 IVTC_MACRO(uint16_t, int, 3, 0);
603 IVTC_MACRO(uint16_t, int, 3, 1);
605 case BC_RGBA16161616:
606 IVTC_MACRO(uint16_t, int, 4, 0);
608 case BC_YUVA16161616:
609 IVTC_MACRO(uint16_t, int, 4, 1);
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();
634 if(!increment) increment = 2;
636 for(int i = 0; i < get_total_packages(); i++)
638 IVTCPackage *package = (IVTCPackage*)get_package(i);
641 if(y1 > plugin->input->get_h()) y1 = plugin->input->get_h();
644 for(int i = 0; i < get_total_clients(); i++)
646 IVTCUnit *unit = (IVTCUnit*)get_client(i);
647 unit->clear_totals();
651 LoadClient* IVTCEngine::new_client()
653 return new IVTCUnit(this, plugin);
656 LoadPackage* IVTCEngine::new_package()
658 return new IVTCPackage;