2 #include "colormodels.h"
5 #include "ivtcwindow.h"
12 REGISTER_PLUGIN(IVTCMain)
14 IVTCConfig::IVTCConfig()
20 pattern = IVTCConfig::PULLDOWN32;
23 IVTCMain::IVTCMain(PluginServer *server)
24 : PluginVClient(server)
26 PLUGIN_CONSTRUCTOR_MACRO
32 PLUGIN_DESTRUCTOR_MACRO
36 if(temp_frame[0]) delete temp_frame[0];
37 if(temp_frame[1]) delete temp_frame[1];
44 char* IVTCMain::plugin_title() { return N_("Inverse Telecine"); }
45 int IVTCMain::is_realtime() { return 1; }
48 int IVTCMain::load_defaults()
50 char directory[BCTEXTLEN], string[BCTEXTLEN];
51 // set the default directory
52 sprintf(directory, "%sivtc.rc", BCASTDIR);
55 defaults = new Defaults(directory);
58 config.frame_offset = defaults->get("FRAME_OFFSET", config.frame_offset);
59 config.first_field = defaults->get("FIRST_FIELD", config.first_field);
60 config.automatic = defaults->get("AUTOMATIC", config.automatic);
61 config.auto_threshold = defaults->get("AUTO_THRESHOLD", config.auto_threshold);
62 config.pattern = defaults->get("PATTERN", config.pattern);
66 int IVTCMain::save_defaults()
68 defaults->update("FRAME_OFFSET", config.frame_offset);
69 defaults->update("FIRST_FIELD", config.first_field);
70 defaults->update("AUTOMATIC", config.automatic);
71 defaults->update("AUTO_THRESHOLD", config.auto_threshold);
72 defaults->update("PATTERN", config.pattern);
77 #include "picon_png.h"
78 NEW_PICON_MACRO(IVTCMain)
79 SHOW_GUI_MACRO(IVTCMain, IVTCThread)
80 SET_STRING_MACRO(IVTCMain)
81 RAISE_WINDOW_MACRO(IVTCMain)
85 int IVTCMain::load_configuration()
87 KeyFrame *prev_keyframe;
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);
96 void IVTCMain::save_data(KeyFrame *keyframe)
100 // cause data to be stored directly in text
101 output.set_shared_string(keyframe->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);
109 output.terminate_string();
112 void IVTCMain::read_data(KeyFrame *keyframe)
116 input.set_shared_string(keyframe->data, strlen(keyframe->data));
123 result = input.read_tag();
127 if(input.tag.title_is("IVTC"))
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);
142 // Pattern A B BC CD D
143 int IVTCMain::process_realtime(VFrame *input_ptr, VFrame *output_ptr)
145 load_configuration();
152 engine = new IVTCEngine(this, smp + 1);
155 // Determine position in pattern
156 int pattern_position = (PluginClient::source_position + config.frame_offset) % 5;
158 //printf("IVTCMain::process_realtime %d %d\n", pattern_position, config.first_field);
159 if(!temp_frame[0]) temp_frame[0] = new VFrame(0,
162 input_ptr->get_color_model(),
164 if(!temp_frame[1]) temp_frame[1] = new VFrame(0,
167 input_ptr->get_color_model(),
170 int row_size = VFrame::calculate_bytes_per_pixel(input_ptr->get_color_model()) * input_ptr->get_w();
175 this->input = input_ptr;
176 this->output = output_ptr;
179 if(config.pattern == IVTCConfig::PULLDOWN32)
181 switch(pattern_position)
186 if(input_ptr->get_rows()[0] != output_ptr->get_rows()[0])
187 output_ptr->copy_from(input_ptr);
191 temp_frame[0]->copy_from(input_ptr);
192 if(input_ptr->get_rows()[0] != output_ptr->get_rows()[0])
193 output_ptr->copy_from(input_ptr);
197 // Save one field for next frame. Reuse previous frame.
198 temp_frame[1]->copy_from(input_ptr);
199 output_ptr->copy_from(temp_frame[0]);
203 // Combine previous field with current field.
204 for(int i = 0; i < input_ptr->get_h(); i++)
206 if((i + config.first_field) & 1)
207 memcpy(output_ptr->get_rows()[i],
208 input_ptr->get_rows()[i],
211 memcpy(output_ptr->get_rows()[i],
212 temp_frame[1]->get_rows()[i],
219 if(config.pattern == IVTCConfig::SHIFTFIELD)
221 temp_frame[1]->copy_from(input_ptr);
223 // Recycle previous bottom or top
224 for(int i = 0; i < input_ptr->get_h(); i++)
226 if((i + config.first_field) & 1)
227 memcpy(output_ptr->get_rows()[i],
228 input_ptr->get_rows()[i],
231 memcpy(output_ptr->get_rows()[i],
232 temp_frame[0]->get_rows()[i],
237 VFrame *temp = temp_frame[0];
238 temp_frame[0] = temp_frame[1];
239 temp_frame[1] = temp;
242 if(config.pattern == IVTCConfig::AUTOMATIC)
244 // Compare with original rows and with previous rows and averaged rows.
245 // Take rows which are most similar to the averaged rows.
247 engine->process_packages();
248 // Copy current for future use
249 temp_frame[1]->copy_from(input_ptr);
257 for(int i = 0; i < engine->get_total_clients(); i++)
259 IVTCUnit *unit = (IVTCUnit*)engine->get_client(i);
260 even_vs_current += unit->even_vs_current;
261 even_vs_prev += unit->even_vs_prev;
262 odd_vs_current += unit->odd_vs_current;
263 odd_vs_prev += unit->odd_vs_prev;
269 if(even_vs_current > even_vs_prev)
276 min = even_vs_current;
280 if(min > odd_vs_prev)
286 if(min > odd_vs_current)
288 min = odd_vs_current;
300 for(int i = 0; i < input_ptr->get_h(); i++)
303 memcpy(output_ptr->get_rows()[i],
304 temp_frame[0]->get_rows()[i],
307 memcpy(output_ptr->get_rows()[i],
308 input_ptr->get_rows()[i],
313 for(int i = 0; i < input_ptr->get_h(); i++)
316 memcpy(output_ptr->get_rows()[i],
317 temp_frame[0]->get_rows()[i],
320 memcpy(output_ptr->get_rows()[i],
321 input_ptr->get_rows()[i],
326 output_ptr->copy_from(input_ptr);
330 VFrame *temp = temp_frame[1];
331 temp_frame[1] = temp_frame[0];
332 temp_frame[0] = temp;
339 void IVTCMain::update_gui()
343 load_configuration();
344 thread->window->lock_window();
345 if(config.pattern == IVTCConfig::AUTOMATIC)
347 thread->window->frame_offset->disable();
348 thread->window->first_field->disable();
352 thread->window->frame_offset->enable();
353 thread->window->first_field->enable();
355 thread->window->frame_offset->update((int64_t)config.frame_offset);
356 thread->window->first_field->update(config.first_field);
357 // thread->window->automatic->update(config.automatic);
358 for(int i = 0; i < TOTAL_PATTERNS; i++)
360 thread->window->pattern[i]->update(config.pattern == i);
362 thread->window->unlock_window();
368 // labs returns different values on x86_64 causing our accumulators to explode
369 #define ABS local_abs
374 static int local_abs(int value)
376 return (value < 0 ? -value : value);
379 static float local_abs(float value)
381 return (value < 0 ? -value : value);
386 static int local_abs(int value)
391 static float local_abs(float value)
402 IVTCPackage::IVTCPackage()
410 IVTCUnit::IVTCUnit(IVTCEngine *server, IVTCMain *plugin)
413 this->server = server;
414 this->plugin = plugin;
417 #define IVTC_MACRO(type, temp_type, components, is_yuv) \
419 type **curr_rows = (type**)plugin->input->get_rows(); \
420 type **prev_rows = (type**)plugin->temp_frame[0]->get_rows(); \
421 /* Components to skip for YUV */ \
422 int skip = components - 1; \
424 for(int i = ptr->y1; i < ptr->y2; i++) \
426 /* Rows to average in the input frame */ \
427 int input_row1_number = i - 1; \
428 int input_row2_number = i + 1; \
429 input_row1_number = MAX(0, input_row1_number); \
430 input_row2_number = MIN(h - 1, input_row2_number); \
431 type *input_row1 = curr_rows[input_row1_number]; \
432 type *input_row2 = curr_rows[input_row2_number]; \
434 /* Rows to compare the averaged rows to */ \
435 type *current_row = curr_rows[i]; \
436 type *prev_row = prev_rows[i]; \
438 temp_type current_difference = 0; \
439 temp_type prev_difference = 0; \
440 for(int j = 0; j < w; j++) \
442 /* Get pixel average */ \
443 temp_type average = ((temp_type)*input_row1 + *input_row2) / 2; \
444 /* Compare row to current */ \
445 current_difference += ABS(average - *current_row); \
446 /* Compare row to previous */ \
447 prev_difference += ABS(average - *prev_row); \
449 /* Do RGB channels */ \
452 average = ((temp_type)input_row1[1] + input_row2[1]) / 2; \
453 current_difference += ABS(average - current_row[1]); \
454 prev_difference += ABS(average - prev_row[1]); \
455 average = ((temp_type)input_row1[2] + input_row2[2]) / 2; \
456 current_difference += ABS(average - current_row[2]); \
457 prev_difference += ABS(average - prev_row[2]); \
460 /* Add to row accumulators */ \
461 current_row += components; \
462 prev_row += components; \
463 input_row1 += components; \
464 input_row2 += components; \
467 /* Store row differences in even or odd variables */ \
468 if(sizeof(type) == 4) \
472 odd_vs_current += (int64_t)(current_difference * 0xffff); \
473 odd_vs_prev += (int64_t)(prev_difference); \
477 even_vs_current += (int64_t)(current_difference); \
478 even_vs_prev += (int64_t)(prev_difference); \
485 odd_vs_current += (int64_t)current_difference; \
486 odd_vs_prev += (int64_t)prev_difference; \
490 even_vs_current += (int64_t)current_difference; \
491 even_vs_prev += (int64_t)prev_difference; \
497 void IVTCUnit::clear_totals()
505 void IVTCUnit::process_package(LoadPackage *package)
507 IVTCPackage *ptr = (IVTCPackage*)package;
508 int w = plugin->input->get_w();
509 int h = plugin->input->get_h();
511 switch(plugin->input->get_color_model())
514 IVTC_MACRO(float, float, 3, 0);
517 IVTC_MACRO(unsigned char, int, 3, 0);
520 IVTC_MACRO(unsigned char, int, 3, 1);
523 IVTC_MACRO(float, float, 4, 0);
526 IVTC_MACRO(unsigned char, int, 4, 0);
529 IVTC_MACRO(unsigned char, int, 4, 1);
532 IVTC_MACRO(uint16_t, int, 3, 0);
535 IVTC_MACRO(uint16_t, int, 3, 1);
537 case BC_RGBA16161616:
538 IVTC_MACRO(uint16_t, int, 4, 0);
540 case BC_YUVA16161616:
541 IVTC_MACRO(uint16_t, int, 4, 1);
551 IVTCEngine::IVTCEngine(IVTCMain *plugin, int cpus)
552 : LoadServer(cpus, cpus)
554 this->plugin = plugin;
557 IVTCEngine::~IVTCEngine()
561 void IVTCEngine::init_packages()
563 int increment = plugin->input->get_h() / get_total_packages();
566 if(!increment) increment = 2;
568 for(int i = 0; i < get_total_packages(); i++)
570 IVTCPackage *package = (IVTCPackage*)get_package(i);
573 if(y1 > plugin->input->get_h()) y1 = plugin->input->get_h();
576 for(int i = 0; i < get_total_clients(); i++)
578 IVTCUnit *unit = (IVTCUnit*)get_client(i);
579 unit->clear_totals();
583 LoadClient* IVTCEngine::new_client()
585 return new IVTCUnit(this, plugin);
588 LoadPackage* IVTCEngine::new_package()
590 return new IVTCPackage;