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 N_("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->xbuf);
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->xbuf);
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);
169 temp_frame[0] = new VFrame(input_ptr->get_w(), input_ptr->get_h(),
170 input_ptr->get_color_model(), 0);
172 temp_frame[1] = new VFrame(input_ptr->get_w(), input_ptr->get_h(),
173 input_ptr->get_color_model(), 0);
175 int row_size = VFrame::calculate_bytes_per_pixel(input_ptr->get_color_model()) * input_ptr->get_w();
176 this->input = input_ptr;
177 this->output = output_ptr;
180 if(config.pattern == IVTCConfig::PULLDOWN32)
182 switch(pattern_position)
187 if(input_ptr->get_rows()[0] != output_ptr->get_rows()[0])
188 output_ptr->copy_from(input_ptr);
192 temp_frame[0]->copy_from(input_ptr);
193 if(input_ptr->get_rows()[0] != output_ptr->get_rows()[0])
194 output_ptr->copy_from(input_ptr);
198 // Save one field for next frame. Reuse previous frame.
199 temp_frame[1]->copy_from(input_ptr);
200 output_ptr->copy_from(temp_frame[0]);
204 // Combine previous field with current field.
205 for(int i = 0; i < input_ptr->get_h(); i++)
207 unsigned char *dst = output_ptr->get_rows()[i];
208 unsigned char *src = ((i + config.first_field) & 1) ?
209 input_ptr->get_rows()[i] :
210 temp_frame[1]->get_rows()[i];
212 memcpy(dst, src, row_size);
218 if(config.pattern == IVTCConfig::SHIFTFIELD)
220 temp_frame[1]->copy_from(input_ptr);
222 // Recycle previous bottom or top
223 for(int i = 0; i < input_ptr->get_h(); i++)
225 unsigned char *dst = output_ptr->get_rows()[i];
226 unsigned char *src = ((i + config.first_field) & 1) ?
227 input_ptr->get_rows()[i] :
228 temp_frame[0]->get_rows()[i];
230 memcpy(dst, src, row_size);
234 VFrame *temp = temp_frame[0];
235 temp_frame[0] = temp_frame[1];
236 temp_frame[1] = temp;
239 if(config.pattern == IVTCConfig::AUTOMATIC)
241 // Compare averaged rows with original rows and
242 // with previous rows.
243 // Take rows which are most similar to the averaged rows.
245 engine->process_packages();
246 // Copy current for future use
247 temp_frame[1]->copy_from(input_ptr);
255 for(int i = 0; i < engine->get_total_clients(); i++)
257 IVTCUnit *unit = (IVTCUnit*)engine->get_client(i);
258 even_vs_current += unit->even_vs_current;
259 even_vs_prev += unit->even_vs_prev;
260 odd_vs_current += unit->odd_vs_current;
261 odd_vs_prev += unit->odd_vs_prev;
270 // Even lines from previous frame are more similar to
271 // averaged even lines in current frame.
272 // Take even lines from previous frame
276 if(even_vs_current < min)
278 // Even lines from current frame are more similar to averaged
279 // even lines in current frame than previous combinations.
280 // Take all lines from current frame
281 min = even_vs_current;
285 if(min > odd_vs_prev)
287 // Odd lines from previous frame are more similar to averaged
288 // odd lines in current frame than previous combinations.
289 // Take odd lines from previous frame
294 if(min > odd_vs_current)
296 // Odd lines from current frame are more similar to averaged
297 // odd lines in current frame than previous combinations.
298 // Take odd lines from current frame.
299 min = odd_vs_current;
303 // int confident = 1;
304 // Do something if not confident.
305 // Sometimes we never get the other field.
306 // Currently nothing is done because it doesn't fix the timing.
307 // if(min > previous_min * 4 && previous_strategy == 2)
312 // printf("IVTCMain::process_realtime 1: previous_min=%lld min=%lld strategy=%d confident=%d\n",
320 // 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",
326 // strategy == 2 && !use_direct_copy);
331 for(int i = 0; i < input_ptr->get_h(); i++)
333 unsigned char *dst = output_ptr->get_rows()[i];
334 unsigned char *src = (!(i & 1)) ?
335 temp_frame[0]->get_rows()[i] :
336 input_ptr->get_rows()[i];
338 memcpy(dst, src, row_size);
342 for(int i = 0; i < input_ptr->get_h(); i++)
344 unsigned char *dst = output_ptr->get_rows()[i];
345 unsigned char *src = (i & 1) ?
346 temp_frame[0]->get_rows()[i] :
347 input_ptr->get_rows()[i];
349 memcpy(dst, src, row_size);
353 output_ptr->copy_from(input_ptr);
356 // output_ptr->copy_from(temp_frame[0]);
358 for(int i = 0; i < input_ptr->get_h(); i++)
360 unsigned char *dst = output_ptr->get_rows()[i];
361 unsigned char *src = (i & 1) ?
362 input_ptr->get_rows()[i - 1] :
363 input_ptr->get_rows()[i];
365 memcpy(dst, src, row_size);
371 previous_strategy = strategy;
372 VFrame *temp = temp_frame[1];
373 temp_frame[1] = temp_frame[0];
374 temp_frame[0] = temp;
381 void IVTCMain::update_gui()
385 load_configuration();
386 ((IVTCWindow*)thread->window)->lock_window();
387 if(config.pattern == IVTCConfig::AUTOMATIC)
389 ((IVTCWindow*)thread->window)->frame_offset->disable();
390 ((IVTCWindow*)thread->window)->first_field->disable();
394 ((IVTCWindow*)thread->window)->frame_offset->enable();
395 ((IVTCWindow*)thread->window)->first_field->enable();
397 ((IVTCWindow*)thread->window)->frame_offset->update((int64_t)config.frame_offset);
398 ((IVTCWindow*)thread->window)->first_field->update(config.first_field);
399 // ((IVTCWindow*)thread->window)->automatic->update(config.automatic);
400 for(int i = 0; i < TOTAL_PATTERNS; i++)
402 ((IVTCWindow*)thread->window)->pattern[i]->update(config.pattern == i);
404 ((IVTCWindow*)thread->window)->unlock_window();
410 // labs returns different values on x86_64 causing our accumulators to explode
411 #define ABS local_abs
416 static int local_abs(int value)
418 return (value < 0 ? -value : value);
421 static float local_abs(float value)
423 return (value < 0 ? -value : value);
428 static int local_abs(int value)
433 static float local_abs(float value)
444 IVTCPackage::IVTCPackage()
452 IVTCUnit::IVTCUnit(IVTCEngine *server, IVTCMain *plugin)
455 this->server = server;
456 this->plugin = plugin;
459 #define IVTC_MACRO(type, temp_type, components, is_yuv) \
461 type **curr_rows = (type**)plugin->input->get_rows(); \
462 type **prev_rows = (type**)plugin->temp_frame[0]->get_rows(); \
463 /* int skip = components - 1; // Components to skip for YUV */\
464 for(int i = ptr->y1; i < ptr->y2; i++) \
465 { /* Rows to average in the input frame */ \
466 int input_row1_number = i - 1; \
467 int input_row2_number = i + 1; \
468 input_row1_number = MAX(0, input_row1_number); \
469 input_row2_number = MIN(h - 1, input_row2_number); \
470 type *input_row1 = curr_rows[input_row1_number]; \
471 type *input_row2 = curr_rows[input_row2_number]; \
472 /* Rows to compare the averaged rows to */ \
473 type *current_row = curr_rows[i]; \
474 type *prev_row = prev_rows[i]; \
475 temp_type current_difference = 0; \
476 temp_type prev_difference = 0; \
477 for(int j = 0; j < w; j++) \
479 /* This only compares luminance */ \
480 /* Get average of current rows */ \
481 temp_type average = ((temp_type)*input_row1 + *input_row2) / 2; \
482 /* Difference between averaged current rows and original inbetween row */ \
483 current_difference += ABS(average - *current_row); \
484 /* Difference between averaged current rows and previous inbetween row */ \
485 prev_difference += ABS(average - *prev_row); \
487 /* Do RGB channels */ \
490 average = ((temp_type)input_row1[1] + input_row2[1]) / 2; \
491 current_difference += ABS(average - current_row[1]); \
492 prev_difference += ABS(average - prev_row[1]); \
493 average = ((temp_type)input_row1[2] + input_row2[2]) / 2; \
494 current_difference += ABS(average - current_row[2]); \
495 prev_difference += ABS(average - prev_row[2]); \
498 /* Add to row accumulators */ \
499 current_row += components; \
500 prev_row += components; \
501 input_row1 += components; \
502 input_row2 += components; \
505 /* Store row differences in even or odd variables */ \
506 if(sizeof(type) == 4) \
510 odd_vs_current += (int64_t)(current_difference * 0xffff); \
511 odd_vs_prev += (int64_t)(prev_difference); \
515 even_vs_current += (int64_t)(current_difference); \
516 even_vs_prev += (int64_t)(prev_difference); \
523 odd_vs_current += (int64_t)current_difference; \
524 odd_vs_prev += (int64_t)prev_difference; \
528 even_vs_current += (int64_t)current_difference; \
529 even_vs_prev += (int64_t)prev_difference; \
535 void IVTCUnit::clear_totals()
543 void IVTCUnit::process_package(LoadPackage *package)
545 IVTCPackage *ptr = (IVTCPackage*)package;
546 int w = plugin->input->get_w();
547 int h = plugin->input->get_h();
549 switch(plugin->input->get_color_model())
552 IVTC_MACRO(float, float, 3, 0);
555 IVTC_MACRO(unsigned char, int, 3, 0);
558 IVTC_MACRO(unsigned char, int, 3, 1);
561 IVTC_MACRO(float, float, 4, 0);
564 IVTC_MACRO(unsigned char, int, 4, 0);
567 IVTC_MACRO(unsigned char, int, 4, 1);
570 IVTC_MACRO(uint16_t, int, 3, 0);
573 IVTC_MACRO(uint16_t, int, 3, 1);
575 case BC_RGBA16161616:
576 IVTC_MACRO(uint16_t, int, 4, 0);
578 case BC_YUVA16161616:
579 IVTC_MACRO(uint16_t, int, 4, 1);
589 IVTCEngine::IVTCEngine(IVTCMain *plugin, int cpus)
590 : LoadServer(cpus, cpus)
592 this->plugin = plugin;
595 IVTCEngine::~IVTCEngine()
599 void IVTCEngine::init_packages()
601 int increment = plugin->input->get_h() / get_total_packages();
604 if(!increment) increment = 2;
606 for(int i = 0; i < get_total_packages(); i++)
608 IVTCPackage *package = (IVTCPackage*)get_package(i);
611 if(y1 > plugin->input->get_h()) y1 = plugin->input->get_h();
614 for(int i = 0; i < get_total_clients(); i++)
616 IVTCUnit *unit = (IVTCUnit*)get_client(i);
617 unit->clear_totals();
621 LoadClient* IVTCEngine::new_client()
623 return new IVTCUnit(this, plugin);
626 LoadPackage* IVTCEngine::new_package()
628 return new IVTCPackage;