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 "colormodels.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->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.terminate_string();
112 void IVTCMain::read_data(KeyFrame *keyframe)
116 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
119 //float new_threshold;
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);
141 void IVTCMain::render_stop()
143 previous_min = 0x4000000000000000LL;
148 // Pattern A B BC CD D
149 int IVTCMain::process_realtime(VFrame *input_ptr, VFrame *output_ptr)
151 load_configuration();
158 engine = new IVTCEngine(this, smp + 1);
161 // Determine position in pattern
162 int pattern_position = (PluginClient::source_position + config.frame_offset) % 5;
164 //printf("IVTCMain::process_realtime %d %d\n", pattern_position, config.first_field);
165 if(!temp_frame[0]) temp_frame[0] = new VFrame(0,
169 input_ptr->get_color_model(),
171 if(!temp_frame[1]) temp_frame[1] = new VFrame(0,
175 input_ptr->get_color_model(),
178 int row_size = VFrame::calculate_bytes_per_pixel(input_ptr->get_color_model()) * input_ptr->get_w();
179 this->input = input_ptr;
180 this->output = output_ptr;
183 if(config.pattern == IVTCConfig::PULLDOWN32)
185 switch(pattern_position)
190 if(input_ptr->get_rows()[0] != output_ptr->get_rows()[0])
191 output_ptr->copy_from(input_ptr);
195 temp_frame[0]->copy_from(input_ptr);
196 if(input_ptr->get_rows()[0] != output_ptr->get_rows()[0])
197 output_ptr->copy_from(input_ptr);
201 // Save one field for next frame. Reuse previous frame.
202 temp_frame[1]->copy_from(input_ptr);
203 output_ptr->copy_from(temp_frame[0]);
207 // Combine previous field with current field.
208 for(int i = 0; i < input_ptr->get_h(); i++)
210 unsigned char *dst = output_ptr->get_rows()[i];
211 unsigned char *src = ((i + config.first_field) & 1) ?
212 input_ptr->get_rows()[i] :
213 temp_frame[1]->get_rows()[i];
215 memcpy(dst, src, row_size);
221 if(config.pattern == IVTCConfig::SHIFTFIELD)
223 temp_frame[1]->copy_from(input_ptr);
225 // Recycle previous bottom or top
226 for(int i = 0; i < input_ptr->get_h(); i++)
228 unsigned char *dst = output_ptr->get_rows()[i];
229 unsigned char *src = ((i + config.first_field) & 1) ?
230 input_ptr->get_rows()[i] :
231 temp_frame[0]->get_rows()[i];
233 memcpy(dst, src, row_size);
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 averaged rows with original rows and
245 // with previous rows.
246 // Take rows which are most similar to the averaged rows.
248 engine->process_packages();
249 // Copy current for future use
250 temp_frame[1]->copy_from(input_ptr);
258 for(int i = 0; i < engine->get_total_clients(); i++)
260 IVTCUnit *unit = (IVTCUnit*)engine->get_client(i);
261 even_vs_current += unit->even_vs_current;
262 even_vs_prev += unit->even_vs_prev;
263 odd_vs_current += unit->odd_vs_current;
264 odd_vs_prev += unit->odd_vs_prev;
273 // Even lines from previous frame are more similar to
274 // averaged even lines in current frame.
275 // Take even lines from previous frame
279 if(even_vs_current < min)
281 // Even lines from current frame are more similar to averaged
282 // even lines in current frame than previous combinations.
283 // Take all lines from current frame
284 min = even_vs_current;
288 if(min > odd_vs_prev)
290 // Odd lines from previous frame are more similar to averaged
291 // odd lines in current frame than previous combinations.
292 // Take odd lines from previous frame
297 if(min > odd_vs_current)
299 // Odd lines from current frame are more similar to averaged
300 // odd lines in current frame than previous combinations.
301 // Take odd lines from current frame.
302 min = odd_vs_current;
306 // int confident = 1;
307 // Do something if not confident.
308 // Sometimes we never get the other field.
309 // Currently nothing is done because it doesn't fix the timing.
310 // if(min > previous_min * 4 && previous_strategy == 2)
315 // printf("IVTCMain::process_realtime 1: previous_min=%lld min=%lld strategy=%d confident=%d\n",
323 // 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",
329 // strategy == 2 && !use_direct_copy);
334 for(int i = 0; i < input_ptr->get_h(); i++)
336 unsigned char *dst = output_ptr->get_rows()[i];
337 unsigned char *src = (!(i & 1)) ?
338 temp_frame[0]->get_rows()[i] :
339 input_ptr->get_rows()[i];
341 memcpy(dst, src, row_size);
345 for(int i = 0; i < input_ptr->get_h(); i++)
347 unsigned char *dst = output_ptr->get_rows()[i];
348 unsigned char *src = (i & 1) ?
349 temp_frame[0]->get_rows()[i] :
350 input_ptr->get_rows()[i];
352 memcpy(dst, src, row_size);
356 output_ptr->copy_from(input_ptr);
359 // output_ptr->copy_from(temp_frame[0]);
361 for(int i = 0; i < input_ptr->get_h(); i++)
363 unsigned char *dst = output_ptr->get_rows()[i];
364 unsigned char *src = (i & 1) ?
365 input_ptr->get_rows()[i - 1] :
366 input_ptr->get_rows()[i];
368 memcpy(dst, src, row_size);
374 previous_strategy = strategy;
375 VFrame *temp = temp_frame[1];
376 temp_frame[1] = temp_frame[0];
377 temp_frame[0] = temp;
384 void IVTCMain::update_gui()
388 load_configuration();
389 ((IVTCWindow*)thread->window)->lock_window();
390 if(config.pattern == IVTCConfig::AUTOMATIC)
392 ((IVTCWindow*)thread->window)->frame_offset->disable();
393 ((IVTCWindow*)thread->window)->first_field->disable();
397 ((IVTCWindow*)thread->window)->frame_offset->enable();
398 ((IVTCWindow*)thread->window)->first_field->enable();
400 ((IVTCWindow*)thread->window)->frame_offset->update((int64_t)config.frame_offset);
401 ((IVTCWindow*)thread->window)->first_field->update(config.first_field);
402 // ((IVTCWindow*)thread->window)->automatic->update(config.automatic);
403 for(int i = 0; i < TOTAL_PATTERNS; i++)
405 ((IVTCWindow*)thread->window)->pattern[i]->update(config.pattern == i);
407 ((IVTCWindow*)thread->window)->unlock_window();
413 // labs returns different values on x86_64 causing our accumulators to explode
414 #define ABS local_abs
419 static int local_abs(int value)
421 return (value < 0 ? -value : value);
424 static float local_abs(float value)
426 return (value < 0 ? -value : value);
431 static int local_abs(int value)
436 static float local_abs(float value)
447 IVTCPackage::IVTCPackage()
455 IVTCUnit::IVTCUnit(IVTCEngine *server, IVTCMain *plugin)
458 this->server = server;
459 this->plugin = plugin;
462 #define IVTC_MACRO(type, temp_type, components, is_yuv) \
464 type **curr_rows = (type**)plugin->input->get_rows(); \
465 type **prev_rows = (type**)plugin->temp_frame[0]->get_rows(); \
466 /* int skip = components - 1; // Components to skip for YUV */\
467 for(int i = ptr->y1; i < ptr->y2; i++) \
468 { /* Rows to average in the input frame */ \
469 int input_row1_number = i - 1; \
470 int input_row2_number = i + 1; \
471 input_row1_number = MAX(0, input_row1_number); \
472 input_row2_number = MIN(h - 1, input_row2_number); \
473 type *input_row1 = curr_rows[input_row1_number]; \
474 type *input_row2 = curr_rows[input_row2_number]; \
475 /* Rows to compare the averaged rows to */ \
476 type *current_row = curr_rows[i]; \
477 type *prev_row = prev_rows[i]; \
478 temp_type current_difference = 0; \
479 temp_type prev_difference = 0; \
480 for(int j = 0; j < w; j++) \
482 /* This only compares luminance */ \
483 /* Get average of current rows */ \
484 temp_type average = ((temp_type)*input_row1 + *input_row2) / 2; \
485 /* Difference between averaged current rows and original inbetween row */ \
486 current_difference += ABS(average - *current_row); \
487 /* Difference between averaged current rows and previous inbetween row */ \
488 prev_difference += ABS(average - *prev_row); \
490 /* Do RGB channels */ \
493 average = ((temp_type)input_row1[1] + input_row2[1]) / 2; \
494 current_difference += ABS(average - current_row[1]); \
495 prev_difference += ABS(average - prev_row[1]); \
496 average = ((temp_type)input_row1[2] + input_row2[2]) / 2; \
497 current_difference += ABS(average - current_row[2]); \
498 prev_difference += ABS(average - prev_row[2]); \
501 /* Add to row accumulators */ \
502 current_row += components; \
503 prev_row += components; \
504 input_row1 += components; \
505 input_row2 += components; \
508 /* Store row differences in even or odd variables */ \
509 if(sizeof(type) == 4) \
513 odd_vs_current += (int64_t)(current_difference * 0xffff); \
514 odd_vs_prev += (int64_t)(prev_difference); \
518 even_vs_current += (int64_t)(current_difference); \
519 even_vs_prev += (int64_t)(prev_difference); \
526 odd_vs_current += (int64_t)current_difference; \
527 odd_vs_prev += (int64_t)prev_difference; \
531 even_vs_current += (int64_t)current_difference; \
532 even_vs_prev += (int64_t)prev_difference; \
538 void IVTCUnit::clear_totals()
546 void IVTCUnit::process_package(LoadPackage *package)
548 IVTCPackage *ptr = (IVTCPackage*)package;
549 int w = plugin->input->get_w();
550 int h = plugin->input->get_h();
552 switch(plugin->input->get_color_model())
555 IVTC_MACRO(float, float, 3, 0);
558 IVTC_MACRO(unsigned char, int, 3, 0);
561 IVTC_MACRO(unsigned char, int, 3, 1);
564 IVTC_MACRO(float, float, 4, 0);
567 IVTC_MACRO(unsigned char, int, 4, 0);
570 IVTC_MACRO(unsigned char, int, 4, 1);
573 IVTC_MACRO(uint16_t, int, 3, 0);
576 IVTC_MACRO(uint16_t, int, 3, 1);
578 case BC_RGBA16161616:
579 IVTC_MACRO(uint16_t, int, 4, 0);
581 case BC_YUVA16161616:
582 IVTC_MACRO(uint16_t, int, 4, 1);
592 IVTCEngine::IVTCEngine(IVTCMain *plugin, int cpus)
593 : LoadServer(cpus, cpus)
595 this->plugin = plugin;
598 IVTCEngine::~IVTCEngine()
602 void IVTCEngine::init_packages()
604 int increment = plugin->input->get_h() / get_total_packages();
607 if(!increment) increment = 2;
609 for(int i = 0; i < get_total_packages(); i++)
611 IVTCPackage *package = (IVTCPackage*)get_package(i);
614 if(y1 > plugin->input->get_h()) y1 = plugin->input->get_h();
617 for(int i = 0; i < get_total_clients(); i++)
619 IVTCUnit *unit = (IVTCUnit*)get_client(i);
620 unit->clear_totals();
624 LoadClient* IVTCEngine::new_client()
626 return new IVTCUnit(this, plugin);
629 LoadPackage* IVTCEngine::new_package()
631 return new IVTCPackage;