4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
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.
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.
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
23 #include "bccmodels.h"
26 #include "ivtcwindow.h"
34 static const char *pattern_text[] =
42 REGISTER_PLUGIN(IVTCMain)
44 IVTCConfig::IVTCConfig()
50 pattern = IVTCConfig::PULLDOWN32;
53 IVTCMain::IVTCMain(PluginServer *server)
54 : PluginVClient(server)
58 previous_min = 0x4000000000000000LL;
59 previous_strategy = 0;
68 if(temp_frame[0]) delete temp_frame[0];
69 if(temp_frame[1]) delete temp_frame[1];
76 const char* IVTCMain::plugin_title() { return _("Inverse Telecine"); }
77 int IVTCMain::is_realtime() { return 1; }
81 NEW_WINDOW_MACRO(IVTCMain, IVTCWindow)
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_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);
109 output.tag.set_title("/IVTC");
111 output.append_newline();
112 output.terminate_string();
115 void IVTCMain::read_data(KeyFrame *keyframe)
119 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
122 //float new_threshold;
126 result = input.read_tag();
130 if(input.tag.title_is("IVTC"))
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);
144 void IVTCMain::render_stop()
146 previous_min = 0x4000000000000000LL;
151 // Pattern A B BC CD D
152 int IVTCMain::process_realtime(VFrame *input_ptr, VFrame *output_ptr)
154 load_configuration();
161 engine = new IVTCEngine(this, smp + 1);
164 // Determine position in pattern
165 int pattern_position = (PluginClient::source_position + config.frame_offset) % 5;
167 //printf("IVTCMain::process_realtime %d %d\n", pattern_position, config.first_field);
168 if(!temp_frame[0]) temp_frame[0] = new VFrame(0,
172 input_ptr->get_color_model(),
174 if(!temp_frame[1]) temp_frame[1] = new VFrame(0,
178 input_ptr->get_color_model(),
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;
186 if(config.pattern == IVTCConfig::PULLDOWN32)
188 switch(pattern_position)
193 if(input_ptr->get_rows()[0] != output_ptr->get_rows()[0])
194 output_ptr->copy_from(input_ptr);
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);
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]);
210 // Combine previous field with current field.
211 for(int i = 0; i < input_ptr->get_h(); i++)
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];
218 memcpy(dst, src, row_size);
224 if(config.pattern == IVTCConfig::SHIFTFIELD)
226 temp_frame[1]->copy_from(input_ptr);
228 // Recycle previous bottom or top
229 for(int i = 0; i < input_ptr->get_h(); i++)
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];
236 memcpy(dst, src, row_size);
240 VFrame *temp = temp_frame[0];
241 temp_frame[0] = temp_frame[1];
242 temp_frame[1] = temp;
245 if(config.pattern == IVTCConfig::AUTOMATIC)
247 // Compare averaged rows with original rows and
248 // with previous rows.
249 // Take rows which are most similar to the averaged rows.
251 engine->process_packages();
252 // Copy current for future use
253 temp_frame[1]->copy_from(input_ptr);
261 for(int i = 0; i < engine->get_total_clients(); i++)
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;
276 // Even lines from previous frame are more similar to
277 // averaged even lines in current frame.
278 // Take even lines from previous frame
282 if(even_vs_current < min)
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;
291 if(min > odd_vs_prev)
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
300 if(min > odd_vs_current)
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;
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)
318 // printf("IVTCMain::process_realtime 1: previous_min=%lld min=%lld strategy=%d confident=%d\n",
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",
332 // strategy == 2 && !use_direct_copy);
337 for(int i = 0; i < input_ptr->get_h(); i++)
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];
344 memcpy(dst, src, row_size);
348 for(int i = 0; i < input_ptr->get_h(); i++)
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];
355 memcpy(dst, src, row_size);
359 output_ptr->copy_from(input_ptr);
362 // output_ptr->copy_from(temp_frame[0]);
364 for(int i = 0; i < input_ptr->get_h(); i++)
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];
371 memcpy(dst, src, row_size);
377 previous_strategy = strategy;
378 VFrame *temp = temp_frame[1];
379 temp_frame[1] = temp_frame[0];
380 temp_frame[0] = temp;
387 void IVTCMain::update_gui()
391 load_configuration();
392 ((IVTCWindow*)thread->window)->lock_window();
393 if(config.pattern == IVTCConfig::AUTOMATIC)
395 ((IVTCWindow*)thread->window)->frame_offset->disable();
396 ((IVTCWindow*)thread->window)->first_field->disable();
400 ((IVTCWindow*)thread->window)->frame_offset->enable();
401 ((IVTCWindow*)thread->window)->first_field->enable();
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++)
408 ((IVTCWindow*)thread->window)->pattern[i]->update(config.pattern == i);
410 ((IVTCWindow*)thread->window)->unlock_window();
416 // labs returns different values on x86_64 causing our accumulators to explode
417 #define ABS local_abs
422 static int local_abs(int value)
424 return (value < 0 ? -value : value);
427 static float local_abs(float value)
429 return (value < 0 ? -value : value);
434 static int local_abs(int value)
439 static float local_abs(float value)
450 IVTCPackage::IVTCPackage()
458 IVTCUnit::IVTCUnit(IVTCEngine *server, IVTCMain *plugin)
461 this->server = server;
462 this->plugin = plugin;
465 #define IVTC_MACRO(type, temp_type, components, is_yuv) \
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++) \
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); \
493 /* Do RGB channels */ \
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]); \
504 /* Add to row accumulators */ \
505 current_row += components; \
506 prev_row += components; \
507 input_row1 += components; \
508 input_row2 += components; \
511 /* Store row differences in even or odd variables */ \
512 if(sizeof(type) == 4) \
516 odd_vs_current += (int64_t)(current_difference * 0xffff); \
517 odd_vs_prev += (int64_t)(prev_difference); \
521 even_vs_current += (int64_t)(current_difference); \
522 even_vs_prev += (int64_t)(prev_difference); \
529 odd_vs_current += (int64_t)current_difference; \
530 odd_vs_prev += (int64_t)prev_difference; \
534 even_vs_current += (int64_t)current_difference; \
535 even_vs_prev += (int64_t)prev_difference; \
541 void IVTCUnit::clear_totals()
549 void IVTCUnit::process_package(LoadPackage *package)
551 IVTCPackage *ptr = (IVTCPackage*)package;
552 int w = plugin->input->get_w();
553 int h = plugin->input->get_h();
555 switch(plugin->input->get_color_model())
558 IVTC_MACRO(float, float, 3, 0);
561 IVTC_MACRO(unsigned char, int, 3, 0);
564 IVTC_MACRO(unsigned char, int, 3, 1);
567 IVTC_MACRO(float, float, 4, 0);
570 IVTC_MACRO(unsigned char, int, 4, 0);
573 IVTC_MACRO(unsigned char, int, 4, 1);
576 IVTC_MACRO(uint16_t, int, 3, 0);
579 IVTC_MACRO(uint16_t, int, 3, 1);
581 case BC_RGBA16161616:
582 IVTC_MACRO(uint16_t, int, 4, 0);
584 case BC_YUVA16161616:
585 IVTC_MACRO(uint16_t, int, 4, 1);
595 IVTCEngine::IVTCEngine(IVTCMain *plugin, int cpus)
596 : LoadServer(cpus, cpus)
598 this->plugin = plugin;
601 IVTCEngine::~IVTCEngine()
605 void IVTCEngine::init_packages()
607 int increment = plugin->input->get_h() / get_total_packages();
610 if(!increment) increment = 2;
612 for(int i = 0; i < get_total_packages(); i++)
614 IVTCPackage *package = (IVTCPackage*)get_package(i);
617 if(y1 > plugin->input->get_h()) y1 = plugin->input->get_h();
620 for(int i = 0; i < get_total_clients(); i++)
622 IVTCUnit *unit = (IVTCUnit*)get_client(i);
623 unit->clear_totals();
627 LoadClient* IVTCEngine::new_client()
629 return new IVTCUnit(this, plugin);
632 LoadPackage* IVTCEngine::new_package()
634 return new IVTCPackage;