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 "bcdisplayinfo.h"
26 #include "bcsignals.h"
30 #include "motion-cv.h"
31 #include "motionwindow-cv.h"
33 #include "overlayframe.h"
34 #include "rotateframe.h"
35 #include "transportque.h"
41 REGISTER_PLUGIN(MotionCVMain)
50 MotionCVConfig::MotionCVConfig()
56 global_block_w = MIN_BLOCK;
57 global_block_h = MIN_BLOCK;
58 rotation_block_w = MIN_BLOCK;
59 rotation_block_h = MIN_BLOCK;
62 global_positions = 256;
69 addtrackedframeoffset = 0;
70 strcpy(tracking_file, TRACKING_FILE);
73 mode3 = MotionCVConfig::TRACK_SINGLE;
80 void MotionCVConfig::boundaries()
82 CLAMP(global_range_w, MIN_RADIUS, MAX_RADIUS);
83 CLAMP(global_range_h, MIN_RADIUS, MAX_RADIUS);
84 CLAMP(rotation_range, MIN_ROTATION, MAX_ROTATION);
85 CLAMP(block_count, MIN_BLOCKS, MAX_BLOCKS);
86 CLAMP(global_block_w, MIN_BLOCK, MAX_BLOCK);
87 CLAMP(global_block_h, MIN_BLOCK, MAX_BLOCK);
88 CLAMP(rotation_block_w, MIN_BLOCK, MAX_BLOCK);
89 CLAMP(rotation_block_h, MIN_BLOCK, MAX_BLOCK);
92 int MotionCVConfig::equivalent(MotionCVConfig &that)
94 return global_range_w == that.global_range_w &&
95 global_range_h == that.global_range_h &&
96 rotation_range == that.rotation_range &&
97 mode1 == that.mode1 &&
98 global == that.global &&
99 rotate == that.rotate &&
100 addtrackedframeoffset == that.addtrackedframeoffset &&
101 !strcmp(tracking_file, that.tracking_file) &&
102 draw_vectors == that.draw_vectors &&
103 block_count == that.block_count &&
104 global_block_w == that.global_block_w &&
105 global_block_h == that.global_block_h &&
106 rotation_block_w == that.rotation_block_w &&
107 rotation_block_h == that.rotation_block_h &&
108 EQUIV(block_x, that.block_x) &&
109 EQUIV(block_y, that.block_y) &&
110 global_positions == that.global_positions &&
111 rotate_positions == that.rotate_positions &&
112 magnitude == that.magnitude &&
113 return_speed == that.return_speed &&
114 mode3 == that.mode3 &&
115 track_frame == that.track_frame &&
116 bottom_is_master == that.bottom_is_master &&
117 horizontal_only == that.horizontal_only &&
118 vertical_only == that.vertical_only;
121 void MotionCVConfig::copy_from(MotionCVConfig &that)
123 global_range_w = that.global_range_w;
124 global_range_h = that.global_range_h;
125 rotation_range = that.rotation_range;
127 global = that.global;
128 rotate = that.rotate;
129 addtrackedframeoffset = that.addtrackedframeoffset;
130 strcpy(tracking_file, that.tracking_file);
132 draw_vectors = that.draw_vectors;
133 block_count = that.block_count;
134 block_x = that.block_x;
135 block_y = that.block_y;
136 global_positions = that.global_positions;
137 rotate_positions = that.rotate_positions;
138 global_block_w = that.global_block_w;
139 global_block_h = that.global_block_h;
140 rotation_block_w = that.rotation_block_w;
141 rotation_block_h = that.rotation_block_h;
142 magnitude = that.magnitude;
143 return_speed = that.return_speed;
145 track_frame = that.track_frame;
146 bottom_is_master = that.bottom_is_master;
147 horizontal_only = that.horizontal_only;
148 vertical_only = that.vertical_only;
151 void MotionCVConfig::interpolate(MotionCVConfig &prev, MotionCVConfig &next,
152 int64_t prev_frame, int64_t next_frame, int64_t current_frame)
157 MotionCVMain::MotionCVMain(PluginServer *server)
158 : PluginVClient(server)
170 previous_frame_number = -1;
173 current_global_ref = 0;
174 global_target_src = 0;
175 global_target_dst = 0;
180 dx_offset = dy_offset = 0;
181 save_dx = load_dx = 0;
182 save_dy = load_dy = 0;
183 save_dt = load_dt = 0;
186 current_rotate_ref = 0;
187 rotate_target_src = 0;
188 rotate_target_dst = 0;
191 MotionCVMain::~MotionCVMain()
196 delete [] search_area;
198 delete rotate_engine;
199 delete motion_rotate;
201 delete prev_global_ref;
202 delete current_global_ref;
203 delete global_target_src;
204 delete global_target_dst;
206 if( active_fp ) fclose(active_fp);
208 delete prev_rotate_ref;
209 delete current_rotate_ref;
210 delete rotate_target_src;
211 delete rotate_target_dst;
214 const char* MotionCVMain::plugin_title() { return _("MotionCV"); }
215 int MotionCVMain::is_realtime() { return 1; }
216 int MotionCVMain::is_multichannel() { return 1; }
219 NEW_WINDOW_MACRO(MotionCVMain, MotionCVWindow)
221 LOAD_CONFIGURATION_MACRO(MotionCVMain, MotionCVConfig)
225 void MotionCVMain::update_gui()
229 if(load_configuration())
231 thread->window->lock_window("MotionCVMain::update_gui");
232 MotionCVWindow *window = (MotionCVWindow *)thread->window;
234 char string[BCTEXTLEN];
235 sprintf(string, "%d", config.global_positions);
236 window->global_search_positions->set_text(string);
237 sprintf(string, "%d", config.rotate_positions);
238 window->rotation_search_positions->set_text(string);
240 window->global_block_w->update(config.global_block_w);
241 window->global_block_h->update(config.global_block_h);
242 window->rotation_block_w->update(config.rotation_block_w);
243 window->rotation_block_h->update(config.rotation_block_h);
244 window->block_x->update(config.block_x);
245 window->block_y->update(config.block_y);
246 window->block_x_text->update((float)config.block_x);
247 window->block_y_text->update((float)config.block_y);
248 window->magnitude->update(config.magnitude);
249 window->return_speed->update(config.return_speed);
252 window->track_single->update(config.mode3 == MotionCVConfig::TRACK_SINGLE);
253 window->track_frame_number->update(config.track_frame);
254 window->track_previous->update(config.mode3 == MotionCVConfig::TRACK_PREVIOUS);
255 window->previous_same->update(config.mode3 == MotionCVConfig::PREVIOUS_SAME_BLOCK);
256 if(config.mode3 != MotionCVConfig::TRACK_SINGLE)
257 window->track_frame_number->disable();
259 window->track_frame_number->enable();
261 window->mode1->set_text(
262 Mode1::to_text(config.mode1));
263 window->mode2->set_text(
264 Mode2::to_text(config.mode2));
265 window->mode3->set_text(
266 Mode3::to_text(config.horizontal_only, config.vertical_only));
267 window->master_layer->set_text(
268 MasterLayer::to_text(config.bottom_is_master));
271 window->update_mode();
272 window->unlock_window();
280 void MotionCVMain::save_data(KeyFrame *keyframe)
284 // cause data to be stored directly in text
285 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
286 output.tag.set_title("MOTION");
288 output.tag.set_property("BLOCK_COUNT", config.block_count);
289 output.tag.set_property("GLOBAL_POSITIONS", config.global_positions);
290 output.tag.set_property("ROTATE_POSITIONS", config.rotate_positions);
291 output.tag.set_property("GLOBAL_BLOCK_W", config.global_block_w);
292 output.tag.set_property("GLOBAL_BLOCK_H", config.global_block_h);
293 output.tag.set_property("ROTATION_BLOCK_W", config.rotation_block_w);
294 output.tag.set_property("ROTATION_BLOCK_H", config.rotation_block_h);
295 output.tag.set_property("BLOCK_X", config.block_x);
296 output.tag.set_property("BLOCK_Y", config.block_y);
297 output.tag.set_property("GLOBAL_RANGE_W", config.global_range_w);
298 output.tag.set_property("GLOBAL_RANGE_H", config.global_range_h);
299 output.tag.set_property("ROTATION_RANGE", config.rotation_range);
300 output.tag.set_property("MAGNITUDE", config.magnitude);
301 output.tag.set_property("RETURN_SPEED", config.return_speed);
302 output.tag.set_property("MODE1", config.mode1);
303 output.tag.set_property("GLOBAL", config.global);
304 output.tag.set_property("ROTATE", config.rotate);
305 output.tag.set_property("ADDTRACKEDFRAMEOFFSET", config.addtrackedframeoffset);
306 output.tag.set_property("TRACKING_FILE", config.tracking_file);
307 output.tag.set_property("MODE2", config.mode2);
308 output.tag.set_property("DRAW_VECTORS", config.draw_vectors);
309 output.tag.set_property("MODE3", config.mode3);
310 output.tag.set_property("TRACK_FRAME", config.track_frame);
311 output.tag.set_property("BOTTOM_IS_MASTER", config.bottom_is_master);
312 output.tag.set_property("HORIZONTAL_ONLY", config.horizontal_only);
313 output.tag.set_property("VERTICAL_ONLY", config.vertical_only);
315 output.tag.set_title("/MOTION");
317 output.terminate_string();
320 void MotionCVMain::read_data(KeyFrame *keyframe)
324 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
330 result = input.read_tag();
334 if(input.tag.title_is("MOTION"))
336 config.block_count = input.tag.get_property("BLOCK_COUNT", config.block_count);
337 config.global_positions = input.tag.get_property("GLOBAL_POSITIONS", config.global_positions);
338 config.rotate_positions = input.tag.get_property("ROTATE_POSITIONS", config.rotate_positions);
339 config.global_block_w = input.tag.get_property("GLOBAL_BLOCK_W", config.global_block_w);
340 config.global_block_h = input.tag.get_property("GLOBAL_BLOCK_H", config.global_block_h);
341 config.rotation_block_w = input.tag.get_property("ROTATION_BLOCK_W", config.rotation_block_w);
342 config.rotation_block_h = input.tag.get_property("ROTATION_BLOCK_H", config.rotation_block_h);
343 config.block_x = input.tag.get_property("BLOCK_X", config.block_x);
344 config.block_y = input.tag.get_property("BLOCK_Y", config.block_y);
345 config.global_range_w = input.tag.get_property("GLOBAL_RANGE_W", config.global_range_w);
346 config.global_range_h = input.tag.get_property("GLOBAL_RANGE_H", config.global_range_h);
347 config.rotation_range = input.tag.get_property("ROTATION_RANGE", config.rotation_range);
348 config.magnitude = input.tag.get_property("MAGNITUDE", config.magnitude);
349 config.return_speed = input.tag.get_property("RETURN_SPEED", config.return_speed);
350 config.mode1 = input.tag.get_property("MODE1", config.mode1);
351 config.global = input.tag.get_property("GLOBAL", config.global);
352 config.rotate = input.tag.get_property("ROTATE", config.rotate);
353 config.addtrackedframeoffset = input.tag.get_property("ADDTRACKEDFRAMEOFFSET", config.addtrackedframeoffset);
354 input.tag.get_property("TRACKING_FILE", config.tracking_file);
355 config.mode2 = input.tag.get_property("MODE2", config.mode2);
356 config.draw_vectors = input.tag.get_property("DRAW_VECTORS", config.draw_vectors);
357 config.mode3 = input.tag.get_property("MODE3", config.mode3);
358 config.track_frame = input.tag.get_property("TRACK_FRAME", config.track_frame);
359 config.bottom_is_master = input.tag.get_property("BOTTOM_IS_MASTER", config.bottom_is_master);
360 config.horizontal_only = input.tag.get_property("HORIZONTAL_ONLY", config.horizontal_only);
361 config.vertical_only = input.tag.get_property("VERTICAL_ONLY", config.vertical_only);
376 void MotionCVMain::allocate_temp(int w, int h, int color_model)
379 (temp_frame->get_w() != w ||
380 temp_frame->get_h() != h))
386 temp_frame = new VFrame(w, h, color_model);
391 void MotionCVMain::process_global()
393 if(!engine) engine = new MotionCVScan(this,
394 PluginClient::get_project_smp() + 1,
395 PluginClient::get_project_smp() + 1);
397 // Get the current motion vector between the previous and current frame
398 engine->scan_frame(current_global_ref, prev_global_ref);
399 current_dx = engine->dx_result;
400 current_dy = engine->dy_result;
402 // Add current motion vector to accumulation vector.
403 if(config.mode3 != MotionCVConfig::TRACK_SINGLE)
406 total_dx = (int64_t)total_dx * (100 - config.return_speed) / 100;
407 total_dy = (int64_t)total_dy * (100 - config.return_speed) / 100;
408 total_dx += engine->dx_result;
409 total_dy += engine->dy_result;
412 // Make accumulation vector current
414 total_dx = engine->dx_result;
415 total_dy = engine->dy_result;
418 // Clamp accumulation vector
419 if(config.magnitude < 100)
421 //int block_w = (int64_t)config.global_block_w *
422 // current_global_ref->get_w() / 100;
423 //int block_h = (int64_t)config.global_block_h *
424 // current_global_ref->get_h() / 100;
425 int block_x_orig = (int64_t)(config.block_x *
426 current_global_ref->get_w() /
428 int block_y_orig = (int64_t)(config.block_y *
429 current_global_ref->get_h() /
432 int max_block_x = (int64_t)(current_global_ref->get_w() - block_x_orig) *
436 int max_block_y = (int64_t)(current_global_ref->get_h() - block_y_orig) *
440 int min_block_x = (int64_t)-block_x_orig *
444 int min_block_y = (int64_t)-block_y_orig *
449 CLAMP(total_dx, min_block_x, max_block_x);
450 CLAMP(total_dy, min_block_y, max_block_y);
454 printf("MotionCVMain::process_global 2 total_dx=%.02f total_dy=%.02f\n",
455 (float)total_dx / OVERSAMPLE,
456 (float)total_dy / OVERSAMPLE);
459 if(config.mode3 != MotionCVConfig::TRACK_SINGLE && !config.rotate)
461 // Transfer current reference frame to previous reference frame and update
462 // counter. Must wait for rotate to compare.
463 prev_global_ref->copy_from(current_global_ref);
464 previous_frame_number = get_source_position();
467 // Decide what to do with target based on requested operation
468 int interpolation = NEAREST_NEIGHBOR;
473 case MotionCVConfig::NOTHING:
474 global_target_dst->copy_from(global_target_src);
476 case MotionCVConfig::TRACK_PIXEL:
477 interpolation = NEAREST_NEIGHBOR;
478 dx = (int)(total_dx / OVERSAMPLE);
479 dy = (int)(total_dy / OVERSAMPLE);
481 case MotionCVConfig::STABILIZE_PIXEL:
482 interpolation = NEAREST_NEIGHBOR;
483 dx = -(int)(total_dx / OVERSAMPLE);
484 dy = -(int)(total_dy / OVERSAMPLE);
487 case MotionCVConfig::TRACK:
488 interpolation = CUBIC_LINEAR;
489 dx = (float)total_dx / OVERSAMPLE;
490 dy = (float)total_dy / OVERSAMPLE;
492 case MotionCVConfig::STABILIZE:
493 interpolation = CUBIC_LINEAR;
494 dx = -(float)total_dx / OVERSAMPLE;
495 dy = -(float)total_dy / OVERSAMPLE;
500 if(config.mode1 != MotionCVConfig::NOTHING)
503 overlayer = new OverlayFrame(PluginClient::get_project_smp() + 1);
504 global_target_dst->clear_frame();
505 overlayer->overlay(global_target_dst,
509 global_target_src->get_w(),
510 global_target_src->get_h(),
513 (float)global_target_src->get_w() + dx,
514 (float)global_target_src->get_h() + dy,
523 void MotionCVMain::process_rotation()
528 // Convert the previous global reference into the previous rotation reference.
529 // Convert global target destination into rotation target source.
533 overlayer = new OverlayFrame(PluginClient::get_project_smp() + 1);
536 if(config.mode3 == MotionCVConfig::TRACK_SINGLE)
538 dx = (float)total_dx / OVERSAMPLE;
539 dy = (float)total_dy / OVERSAMPLE;
543 dx = (float)current_dx / OVERSAMPLE;
544 dy = (float)current_dy / OVERSAMPLE;
547 prev_rotate_ref->clear_frame();
548 overlayer->overlay(prev_rotate_ref,
552 prev_global_ref->get_w(),
553 prev_global_ref->get_h(),
556 (float)prev_global_ref->get_w() + dx,
557 (float)prev_global_ref->get_h() + dy,
561 // Pivot is destination global position
562 block_x = (int)(prev_rotate_ref->get_w() *
567 block_y = (int)(prev_rotate_ref->get_h() *
572 // Use the global target output as the rotation target input
573 rotate_target_src->copy_from(global_target_dst);
574 // Transfer current reference frame to previous reference frame for global.
575 if(config.mode3 != MotionCVConfig::TRACK_SINGLE)
577 prev_global_ref->copy_from(current_global_ref);
578 previous_frame_number = get_source_position();
584 block_x = (int)(prev_rotate_ref->get_w() *
587 block_y = (int)(prev_rotate_ref->get_h() *
596 motion_rotate = new RotateCVScan(this,
597 get_project_smp() + 1,
598 get_project_smp() + 1);
600 current_angle = motion_rotate->scan_frame(prev_rotate_ref,
607 // Add current rotation to accumulation
608 if(config.mode3 != MotionCVConfig::TRACK_SINGLE)
611 total_angle = total_angle * (100 - config.return_speed) / 100;
612 total_angle += current_angle;
616 // Transfer current reference frame to previous reference frame and update
618 prev_rotate_ref->copy_from(current_rotate_ref);
619 previous_frame_number = get_source_position();
624 total_angle = current_angle;
628 printf("MotionCVMain::process_rotation total_angle=%f\n", total_angle);
632 // Calculate rotation parameters based on requested operation
636 case MotionCVConfig::NOTHING:
637 rotate_target_dst->copy_from(rotate_target_src);
639 case MotionCVConfig::TRACK:
640 case MotionCVConfig::TRACK_PIXEL:
643 case MotionCVConfig::STABILIZE:
644 case MotionCVConfig::STABILIZE_PIXEL:
645 angle = -total_angle;
651 if(config.mode1 != MotionCVConfig::NOTHING)
654 rotate_engine = new AffineEngine(PluginClient::get_project_smp() + 1,
655 PluginClient::get_project_smp() + 1);
657 rotate_target_dst->clear_frame();
659 // Determine pivot based on a number of factors.
662 case MotionCVConfig::TRACK:
663 case MotionCVConfig::TRACK_PIXEL:
664 // Use destination of global tracking.
665 rotate_engine->set_pivot(block_x, block_y);
668 case MotionCVConfig::STABILIZE:
669 case MotionCVConfig::STABILIZE_PIXEL:
672 // Use origin of global stabilize operation
673 rotate_engine->set_pivot((int)(rotate_target_dst->get_w() *
676 (int)(rotate_target_dst->get_h() *
684 rotate_engine->set_pivot(block_x, block_y);
690 rotate_engine->rotate(rotate_target_dst, rotate_target_src, angle);
691 // overlayer->overlay(rotate_target_dst,
695 // prev_rotate_ref->get_w(),
696 // prev_rotate_ref->get_h(),
699 // prev_rotate_ref->get_w(),
700 // prev_rotate_ref->get_h(),
704 // overlayer->overlay(rotate_target_dst,
705 // current_rotate_ref,
708 // prev_rotate_ref->get_w(),
709 // prev_rotate_ref->get_h(),
712 // prev_rotate_ref->get_w(),
713 // prev_rotate_ref->get_h(),
732 int MotionCVMain::process_buffer(VFrame **frame,
733 int64_t start_position,
736 int prev_config_mode2 = config.mode2;
737 int need_reconfigure = load_configuration();
738 int color_model = frame[0]->get_color_model();
739 w = frame[0]->get_w();
740 h = frame[0]->get_h();
744 printf("MotionCVMain::process_buffer 1 start_position=%jd\n", start_position);
748 // Calculate the source and destination pointers for each of the operations.
749 // Get the layer to track motion in.
750 reference_layer = config.bottom_is_master ?
751 PluginClient::total_in_buffers - 1 :
753 // Get the layer to apply motion in.
754 target_layer = config.bottom_is_master ?
756 PluginClient::total_in_buffers - 1;
759 output_frame = frame[target_layer];
762 // Get the position of previous reference frame.
763 int64_t actual_previous_number;
764 // Skip if match frame not available
765 int skip_current = 0;
768 if(config.mode3 == MotionCVConfig::TRACK_SINGLE)
770 actual_previous_number = config.track_frame;
771 if(get_direction() == PLAY_REVERSE)
772 actual_previous_number++;
773 if(actual_previous_number == start_position)
778 actual_previous_number = start_position;
779 if(get_direction() == PLAY_FORWARD)
781 actual_previous_number--;
782 if(actual_previous_number < get_source_start())
786 KeyFrame *keyframe = get_prev_keyframe(start_position, 1);
787 if(keyframe->position > 0 &&
788 actual_previous_number < keyframe->position)
794 actual_previous_number++;
795 if(actual_previous_number >= get_source_start() + get_total_len())
799 KeyFrame *keyframe = get_next_keyframe(start_position, 1);
800 if(keyframe->position > 0 &&
801 actual_previous_number >= keyframe->position)
806 // Only count motion since last keyframe
812 if(!config.global && !config.rotate) skip_current = 1;
817 // printf("process_realtime %d %lld %lld\n",
819 // previous_frame_number,
820 // actual_previous_number);
821 // Load match frame and reset vectors
822 int need_reload = !skip_current &&
823 (previous_frame_number != actual_previous_number ||
830 previous_frame_number = actual_previous_number;
832 if( prev_config_mode2 != MotionCVConfig::SAVE &&
833 config.mode2 == MotionCVConfig::SAVE ) {
835 printf("MotionCVMain::process_buffer 2 remove tracking file: %s\n", config.tracking_file);
837 ::remove(config.tracking_file);
854 // Get the global pointers. Here we walk through the sequence of events.
857 // Assume global only. Global reads previous frame and compares
858 // with current frame to get the current translation.
859 // The center of the search area is fixed in compensate mode or
860 // the user value + the accumulation vector in track mode.
862 prev_global_ref = new VFrame(w, h, color_model);
863 if(!current_global_ref)
864 current_global_ref = new VFrame(w, h, color_model);
866 // Global loads the current target frame into the src and
867 // writes it to the dst frame with desired translation.
868 if(!global_target_src)
869 global_target_src = new VFrame(w, h, color_model);
870 if(!global_target_dst)
871 global_target_dst = new VFrame(w, h, color_model);
874 // Load the global frames
877 read_frame(prev_global_ref,
879 previous_frame_number,
884 read_frame(current_global_ref,
889 read_frame(global_target_src,
897 // Global followed by rotate
900 // Must translate the previous global reference by the current global
901 // accumulation vector to match the current global reference.
902 // The center of the search area is always the user value + the accumulation
905 prev_rotate_ref = new VFrame(w, h, color_model);
906 // The current global reference is the current rotation reference.
907 if(!current_rotate_ref)
908 current_rotate_ref = new VFrame(w, h, color_model);
909 current_rotate_ref->copy_from(current_global_ref);
911 // The global target destination is copied to the rotation target source
912 // then written to the rotation output with rotation.
913 // The pivot for the rotation is the center of the search area
914 // if we're tracking.
915 // The pivot is fixed to the user position if we're compensating.
916 if(!rotate_target_src)
917 rotate_target_src = new VFrame(w, h, color_model);
918 if(!rotate_target_dst)
919 rotate_target_dst = new VFrame(w,h , color_model);
926 // Rotation reads the previous reference frame and compares it with current
929 prev_rotate_ref = new VFrame(w, h, color_model);
930 if(!current_rotate_ref)
931 current_rotate_ref = new VFrame(w, h, color_model);
933 // Rotation loads target frame to temporary, rotates it, and writes it to the
934 // target frame. The pivot is always fixed.
935 if(!rotate_target_src)
936 rotate_target_src = new VFrame(w, h, color_model);
937 if(!rotate_target_dst)
938 rotate_target_dst = new VFrame(w,h , color_model);
941 // Load the rotate frames
944 read_frame(prev_rotate_ref,
946 previous_frame_number,
950 read_frame(current_rotate_ref,
955 read_frame(rotate_target_src,
965 if( config.mode2 == MotionCVConfig::LOAD ) {
966 char line[BCTEXTLEN];
967 int64_t frame_no, no; int dx, dy; float dt;
968 if( config.addtrackedframeoffset && config.track_frame != tracking_frame ) {
969 tracking_frame = frame_no = config.track_frame;
970 if( !get_line_key(config.tracking_file, frame_no, line, sizeof(line)) &&
971 sscanf(line, "%jd %d %d %f", &no, &dx, &dy, &dt) == 4 ) {
972 dx_offset = dx; dy_offset = dy;
976 printf("MotionCVMain::process_buffer: no offset data frame %jd\n", frame_no);
980 // Load result from disk
981 frame_no = get_source_position();
982 if( !get_line_key(config.tracking_file, frame_no, line, sizeof(line)) &&
983 sscanf(line, "%jd %d %d %f", &frame_no, &dx, &dy, &dt) == 4 ) {
984 load_dx = dx; load_dy = dy; load_dt = dt;
988 printf("MotionCVMain::process_buffer: no tracking data frame %jd\n", frame_no);
993 // Get position change from previous frame to current frame
994 if(config.global) process_global();
995 // Get rotation change from previous frame to current frame
996 if(config.rotate) process_rotation();
997 //frame[target_layer]->copy_from(prev_rotate_ref);
998 //frame[target_layer]->copy_from(current_rotate_ref);
1000 // write results to disk
1001 if( config.mode2 == MotionCVConfig::SAVE ) {
1002 FILE *output = fopen(config.tracking_file, "aw");
1004 int64_t frame_no = get_source_position();
1005 fprintf(output, "%jd %d %d %f\n", frame_no, save_dx, save_dy, save_dt);
1009 perror("MotionCVMain::process buffer save");
1015 // Transfer the relevant target frame to the output
1020 frame[target_layer]->copy_from(rotate_target_dst);
1024 frame[target_layer]->copy_from(global_target_dst);
1028 // Read the target destination directly
1030 read_frame(frame[target_layer],
1037 if(config.draw_vectors)
1039 draw_vectors(frame[target_layer]);
1043 printf("MotionCVMain::process_buffer 100\n");
1049 void MotionCVMain::clamp_scan(int w,
1061 // printf("MotionCVMain::clamp_scan 1 w=%d h=%d block=%d %d %d %d scan=%d %d %d %d absolute=%d\n",
1076 // scan is always out of range before block.
1079 int difference = -*scan_x1;
1080 *block_x1 += difference;
1086 int difference = -*scan_y1;
1087 *block_y1 += difference;
1093 int difference = *scan_x2 - w;
1094 *block_x2 -= difference;
1095 *scan_x2 -= difference;
1100 int difference = *scan_y2 - h;
1101 *block_y2 -= difference;
1102 *scan_y2 -= difference;
1105 CLAMP(*scan_x1, 0, w);
1106 CLAMP(*scan_y1, 0, h);
1107 CLAMP(*scan_x2, 0, w);
1108 CLAMP(*scan_y2, 0, h);
1114 int difference = -*scan_x1;
1115 *block_x1 += difference;
1116 *scan_x2 += difference;
1122 int difference = -*scan_y1;
1123 *block_y1 += difference;
1124 *scan_y2 += difference;
1128 if(*scan_x2 - *block_x1 + *block_x2 > w)
1130 int difference = *scan_x2 - *block_x1 + *block_x2 - w;
1131 *block_x2 -= difference;
1134 if(*scan_y2 - *block_y1 + *block_y2 > h)
1136 int difference = *scan_y2 - *block_y1 + *block_y2 - h;
1137 *block_y2 -= difference;
1140 // CLAMP(*scan_x1, 0, w - (*block_x2 - *block_x1));
1141 // CLAMP(*scan_y1, 0, h - (*block_y2 - *block_y1));
1142 // CLAMP(*scan_x2, 0, w - (*block_x2 - *block_x1));
1143 // CLAMP(*scan_y2, 0, h - (*block_y2 - *block_y1));
1146 // Sanity checks which break the calculation but should never happen if the
1147 // center of the block is inside the frame.
1148 CLAMP(*block_x1, 0, w);
1149 CLAMP(*block_x2, 0, w);
1150 CLAMP(*block_y1, 0, h);
1151 CLAMP(*block_y2, 0, h);
1153 // printf("MotionCVMain::clamp_scan 2 w=%d h=%d block=%d %d %d %d scan=%d %d %d %d absolute=%d\n",
1169 void MotionCVMain::draw_vectors(VFrame *frame)
1171 int w = frame->get_w();
1172 int h = frame->get_h();
1173 int global_x1, global_y1;
1174 int global_x2, global_y2;
1175 int block_x, block_y;
1176 int block_w, block_h;
1177 int block_x1, block_y1;
1178 int block_x2, block_y2;
1179 int block_x3, block_y3;
1180 int block_x4, block_y4;
1181 int search_w, search_h;
1182 int search_x1, search_y1;
1183 int search_x2, search_y2;
1184 //int search_x3, search_y3;
1185 //int search_x4, search_y4;
1190 // Start of vector is center of previous block.
1191 // End of vector is total accumulation.
1192 if(config.mode3 == MotionCVConfig::TRACK_SINGLE)
1194 global_x1 = (int64_t)(config.block_x *
1197 global_y1 = (int64_t)(config.block_y *
1200 global_x2 = global_x1 + total_dx / OVERSAMPLE;
1201 global_y2 = global_y1 + total_dy / OVERSAMPLE;
1202 //printf("MotionCVMain::draw_vectors %d %d %d %d %d %d\n", total_dx, total_dy, global_x1, global_y1, global_x2, global_y2);
1205 // Start of vector is center of previous block.
1206 // End of vector is current change.
1207 if(config.mode3 == MotionCVConfig::PREVIOUS_SAME_BLOCK)
1209 global_x1 = (int64_t)(config.block_x *
1212 global_y1 = (int64_t)(config.block_y *
1215 global_x2 = global_x1 + current_dx / OVERSAMPLE;
1216 global_y2 = global_y1 + current_dy / OVERSAMPLE;
1220 global_x1 = (int64_t)(config.block_x *
1223 (total_dx - current_dx) /
1225 global_y1 = (int64_t)(config.block_y *
1228 (total_dy - current_dy) /
1230 global_x2 = (int64_t)(config.block_x *
1235 global_y2 = (int64_t)(config.block_y *
1242 block_x = global_x1;
1243 block_y = global_y1;
1244 block_w = config.global_block_w * w / 100;
1245 block_h = config.global_block_h * h / 100;
1246 block_x1 = block_x - block_w / 2;
1247 block_y1 = block_y - block_h / 2;
1248 block_x2 = block_x + block_w / 2;
1249 block_y2 = block_y + block_h / 2;
1250 search_w = config.global_range_w * w / 100;
1251 search_h = config.global_range_h * h / 100;
1252 search_x1 = block_x1 - search_w / 2;
1253 search_y1 = block_y1 - search_h / 2;
1254 search_x2 = block_x2 + search_w / 2;
1255 search_y2 = block_y2 + search_h / 2;
1257 // printf("MotionCVMain::draw_vectors %d %d %d %d %d %d %d %d %d %d %d %d\n",
1284 draw_arrow(frame, global_x1, global_y1, global_x2, global_y2);
1287 draw_line(frame, block_x1, block_y1, block_x2, block_y1);
1288 draw_line(frame, block_x2, block_y1, block_x2, block_y2);
1289 draw_line(frame, block_x2, block_y2, block_x1, block_y2);
1290 draw_line(frame, block_x1, block_y2, block_x1, block_y1);
1294 draw_line(frame, search_x1, search_y1, search_x2, search_y1);
1295 draw_line(frame, search_x2, search_y1, search_x2, search_y2);
1296 draw_line(frame, search_x2, search_y2, search_x1, search_y2);
1297 draw_line(frame, search_x1, search_y2, search_x1, search_y1);
1299 // Block should be endpoint of motion
1302 block_x = global_x2;
1303 block_y = global_y2;
1308 block_x = (int64_t)(config.block_x * w / 100);
1309 block_y = (int64_t)(config.block_y * h / 100);
1312 block_w = config.rotation_block_w * w / 100;
1313 block_h = config.rotation_block_h * h / 100;
1316 float angle = total_angle * 2 * M_PI / 360;
1317 double base_angle1 = atan((float)block_h / block_w);
1318 double base_angle2 = atan((float)block_w / block_h);
1319 double target_angle1 = base_angle1 + angle;
1320 double target_angle2 = base_angle2 + angle;
1321 double radius = sqrt(block_w * block_w + block_h * block_h) / 2;
1322 block_x1 = (int)(block_x - cos(target_angle1) * radius);
1323 block_y1 = (int)(block_y - sin(target_angle1) * radius);
1324 block_x2 = (int)(block_x + sin(target_angle2) * radius);
1325 block_y2 = (int)(block_y - cos(target_angle2) * radius);
1326 block_x3 = (int)(block_x - sin(target_angle2) * radius);
1327 block_y3 = (int)(block_y + cos(target_angle2) * radius);
1328 block_x4 = (int)(block_x + cos(target_angle1) * radius);
1329 block_y4 = (int)(block_y + sin(target_angle1) * radius);
1331 draw_line(frame, block_x1, block_y1, block_x2, block_y2);
1332 draw_line(frame, block_x2, block_y2, block_x4, block_y4);
1333 draw_line(frame, block_x4, block_y4, block_x3, block_y3);
1334 draw_line(frame, block_x3, block_y3, block_x1, block_y1);
1340 draw_line(frame, block_x, block_y - 5, block_x, block_y + 6);
1341 draw_line(frame, block_x - 5, block_y, block_x + 6, block_y);
1348 void MotionCVMain::draw_pixel(VFrame *frame, int x, int y)
1350 if(!(x >= 0 && y >= 0 && x < frame->get_w() && y < frame->get_h())) return;
1352 #define DRAW_PIXEL(x, y, components, do_yuv, max, type) \
1354 type **rows = (type**)frame->get_rows(); \
1355 rows[y][x * components] = max - rows[y][x * components]; \
1358 rows[y][x * components + 1] = max - rows[y][x * components + 1]; \
1359 rows[y][x * components + 2] = max - rows[y][x * components + 2]; \
1363 rows[y][x * components + 1] = (max / 2 + 1) - rows[y][x * components + 1]; \
1364 rows[y][x * components + 2] = (max / 2 + 1) - rows[y][x * components + 2]; \
1366 if(components == 4) \
1367 rows[y][x * components + 3] = max; \
1371 switch(frame->get_color_model())
1374 DRAW_PIXEL(x, y, 3, 0, 0xff, unsigned char);
1377 DRAW_PIXEL(x, y, 4, 0, 0xff, unsigned char);
1380 DRAW_PIXEL(x, y, 3, 0, 1.0, float);
1383 DRAW_PIXEL(x, y, 4, 0, 1.0, float);
1386 DRAW_PIXEL(x, y, 3, 1, 0xff, unsigned char);
1389 DRAW_PIXEL(x, y, 4, 1, 0xff, unsigned char);
1392 DRAW_PIXEL(x, y, 3, 0, 0xffff, uint16_t);
1395 DRAW_PIXEL(x, y, 3, 1, 0xffff, uint16_t);
1397 case BC_RGBA16161616:
1398 DRAW_PIXEL(x, y, 4, 0, 0xffff, uint16_t);
1400 case BC_YUVA16161616:
1401 DRAW_PIXEL(x, y, 4, 1, 0xffff, uint16_t);
1407 void MotionCVMain::draw_line(VFrame *frame, int x1, int y1, int x2, int y2)
1409 int w = labs(x2 - x1);
1410 int h = labs(y2 - y1);
1411 //printf("MotionCVMain::draw_line 1 %d %d %d %d\n", x1, y1, x2, y2);
1415 draw_pixel(frame, x1, y1);
1420 // Flip coordinates so x1 < x2
1430 int numerator = y2 - y1;
1431 int denominator = x2 - x1;
1432 for(int i = x1; i < x2; i++)
1434 int y = y1 + (int64_t)(i - x1) * (int64_t)numerator / (int64_t)denominator;
1435 draw_pixel(frame, i, y);
1440 // Flip coordinates so y1 < y2
1450 int numerator = x2 - x1;
1451 int denominator = y2 - y1;
1452 for(int i = y1; i < y2; i++)
1454 int x = x1 + (int64_t)(i - y1) * (int64_t)numerator / (int64_t)denominator;
1455 draw_pixel(frame, x, i);
1458 //printf("MotionCVMain::draw_line 2\n");
1461 #define ARROW_SIZE 10
1462 void MotionCVMain::draw_arrow(VFrame *frame, int x1, int y1, int x2, int y2)
1464 double angle = atan((float)(y2 - y1) / (float)(x2 - x1));
1465 double angle1 = angle + (float)145 / 360 * 2 * 3.14159265;
1466 double angle2 = angle - (float)145 / 360 * 2 * 3.14159265;
1473 x3 = x2 - (int)(ARROW_SIZE * cos(angle1));
1474 y3 = y2 - (int)(ARROW_SIZE * sin(angle1));
1475 x4 = x2 - (int)(ARROW_SIZE * cos(angle2));
1476 y4 = y2 - (int)(ARROW_SIZE * sin(angle2));
1480 x3 = x2 + (int)(ARROW_SIZE * cos(angle1));
1481 y3 = y2 + (int)(ARROW_SIZE * sin(angle1));
1482 x4 = x2 + (int)(ARROW_SIZE * cos(angle2));
1483 y4 = y2 + (int)(ARROW_SIZE * sin(angle2));
1487 draw_line(frame, x1, y1, x2, y2);
1488 // draw_line(frame, x1, y1 + 1, x2, y2 + 1);
1491 if(abs(y2 - y1) || abs(x2 - x1)) draw_line(frame, x2, y2, x3, y3);
1492 // draw_line(frame, x2, y2 + 1, x3, y3 + 1);
1494 if(abs(y2 - y1) || abs(x2 - x1)) draw_line(frame, x2, y2, x4, y4);
1495 // draw_line(frame, x2, y2 + 1, x4, y4 + 1);
1501 #define ABS_DIFF(type, temp_type, multiplier, components) \
1503 temp_type result_temp = 0; \
1504 for(int i = 0; i < h; i++) \
1506 type *prev_row = (type*)prev_ptr; \
1507 type *current_row = (type*)current_ptr; \
1508 for(int j = 0; j < w; j++) \
1510 for(int k = 0; k < 3; k++) \
1512 temp_type difference; \
1513 difference = *prev_row++ - *current_row++; \
1514 if(difference < 0) \
1515 result_temp -= difference; \
1517 result_temp += difference; \
1519 if(components == 4) \
1525 prev_ptr += row_bytes; \
1526 current_ptr += row_bytes; \
1528 result = (int64_t)(result_temp * multiplier); \
1531 int64_t MotionCVMain::abs_diff(unsigned char *prev_ptr,
1532 unsigned char *current_ptr,
1542 ABS_DIFF(unsigned char, int64_t, 1, 3)
1545 ABS_DIFF(unsigned char, int64_t, 1, 4)
1548 ABS_DIFF(float, double, 0x10000, 3)
1551 ABS_DIFF(float, double, 0x10000, 4)
1554 ABS_DIFF(unsigned char, int64_t, 1, 3)
1557 ABS_DIFF(unsigned char, int64_t, 1, 4)
1560 ABS_DIFF(uint16_t, int64_t, 1, 3)
1562 case BC_YUVA16161616:
1563 ABS_DIFF(uint16_t, int64_t, 1, 4)
1571 #define ABS_DIFF_SUB(type, temp_type, multiplier, components) \
1573 temp_type result_temp = 0; \
1574 temp_type y2_fraction = sub_y * 0x100 / OVERSAMPLE; \
1575 temp_type y1_fraction = 0x100 - y2_fraction; \
1576 temp_type x2_fraction = sub_x * 0x100 / OVERSAMPLE; \
1577 temp_type x1_fraction = 0x100 - x2_fraction; \
1578 for(int i = 0; i < h_sub; i++) \
1580 type *prev_row1 = (type*)prev_ptr; \
1581 type *prev_row2 = (type*)prev_ptr + components; \
1582 type *prev_row3 = (type*)(prev_ptr + row_bytes); \
1583 type *prev_row4 = (type*)(prev_ptr + row_bytes) + components; \
1584 type *current_row = (type*)current_ptr; \
1585 for(int j = 0; j < w_sub; j++) \
1587 for(int k = 0; k < 3; k++) \
1589 temp_type difference; \
1590 temp_type prev_value = \
1591 (*prev_row1++ * x1_fraction * y1_fraction + \
1592 *prev_row2++ * x2_fraction * y1_fraction + \
1593 *prev_row3++ * x1_fraction * y2_fraction + \
1594 *prev_row4++ * x2_fraction * y2_fraction) / \
1596 temp_type current_value = *current_row++; \
1597 difference = prev_value - current_value; \
1598 if(difference < 0) \
1599 result_temp -= difference; \
1601 result_temp += difference; \
1604 if(components == 4) \
1613 prev_ptr += row_bytes; \
1614 current_ptr += row_bytes; \
1616 result = (int64_t)(result_temp * multiplier); \
1622 int64_t MotionCVMain::abs_diff_sub(unsigned char *prev_ptr,
1623 unsigned char *current_ptr,
1638 ABS_DIFF_SUB(unsigned char, int64_t, 1, 3)
1641 ABS_DIFF_SUB(unsigned char, int64_t, 1, 4)
1644 ABS_DIFF_SUB(float, double, 0x10000, 3)
1647 ABS_DIFF_SUB(float, double, 0x10000, 4)
1650 ABS_DIFF_SUB(unsigned char, int64_t, 1, 3)
1653 ABS_DIFF_SUB(unsigned char, int64_t, 1, 4)
1656 ABS_DIFF_SUB(uint16_t, int64_t, 1, 3)
1658 case BC_YUVA16161616:
1659 ABS_DIFF_SUB(uint16_t, int64_t, 1, 4)
1665 int MotionCVMain::get_line_key(const char *filename, int64_t key, char *line, int len)
1667 if( active_fp && strcmp(active_file, filename) ) {
1668 fclose(active_fp); active_fp = 0; active_file[0] = 0;
1671 if( !(active_fp = fopen(filename, "r")) ) {
1672 perror("open motion file");
1673 fprintf(stderr,"err reading key %jd\n", key);
1676 strcpy(active_file, filename);
1677 tracking_frame = -1;
1680 if( fgets(line, len, active_fp) && (recd=strtol(line,0,0)) == key )
1682 // binary search file
1683 fseek(active_fp, 0, SEEK_END);
1684 int64_t l = -1, r = ftell(active_fp);
1685 while( (r-l) > 1 ) {
1686 int64_t m = (l+r) / 2;
1687 fseek(active_fp, m, SEEK_SET);
1688 if( m > 0 && !fgets(line, len, active_fp) ) {
1689 fprintf(stderr,"err reading key %jd\n", key);
1692 if( fgets(line, len, active_fp) ) {
1693 recd = strtol(line,0,0);
1694 if( recd == key ) return 0;
1695 if( recd < key ) { l = m; continue; }
1704 MotionCVScanPackage::MotionCVScanPackage()
1711 MotionCVScanUnit::MotionCVScanUnit(MotionCVScan *server,
1712 MotionCVMain *plugin)
1713 : LoadClient(server)
1715 this->plugin = plugin;
1716 this->server = server;
1717 cache_lock = new Mutex("MotionCVScanUnit::cache_lock");
1720 MotionCVScanUnit::~MotionCVScanUnit()
1727 void MotionCVScanUnit::process_package(LoadPackage *package)
1729 MotionCVScanPackage *pkg = (MotionCVScanPackage*)package;
1730 //int w = server->current_frame->get_w();
1731 //int h = server->current_frame->get_h();
1732 int color_model = server->current_frame->get_color_model();
1733 int pixel_size = BC_CModels::calculate_pixelsize(color_model);
1734 int row_bytes = server->current_frame->get_bytes_per_line();
1737 if(!server->subpixel)
1739 int search_x = pkg->scan_x1 + (pkg->pixel % (pkg->scan_x2 - pkg->scan_x1));
1740 int search_y = pkg->scan_y1 + (pkg->pixel / (pkg->scan_x2 - pkg->scan_x1));
1743 pkg->difference1 = server->get_cache(search_x, search_y);
1744 if(pkg->difference1 < 0)
1746 //printf("MotionCVScanUnit::process_package 1 %d %d\n",
1747 //search_x, search_y, pkg->block_x2 - pkg->block_x1, pkg->block_y2 - pkg->block_y1);
1748 // Pointers to first pixel in each block
1749 unsigned char *prev_ptr = server->previous_frame->get_rows()[
1751 search_x * pixel_size;
1752 unsigned char *current_ptr = server->current_frame->get_rows()[
1754 pkg->block_x1 * pixel_size;
1756 pkg->difference1 = plugin->abs_diff(prev_ptr,
1759 pkg->block_x2 - pkg->block_x1,
1760 pkg->block_y2 - pkg->block_y1,
1762 //printf("MotionCVScanUnit::process_package 2\n");
1763 server->put_cache(search_x, search_y, pkg->difference1);
1769 int sub_x = pkg->pixel % (OVERSAMPLE * 2 - 1) + 1;
1770 int sub_y = pkg->pixel / (OVERSAMPLE * 2 - 1) + 1;
1772 if(plugin->config.horizontal_only)
1777 if(plugin->config.vertical_only)
1782 int search_x = pkg->scan_x1 + sub_x / OVERSAMPLE;
1783 int search_y = pkg->scan_y1 + sub_y / OVERSAMPLE;
1784 sub_x %= OVERSAMPLE;
1785 sub_y %= OVERSAMPLE;
1788 unsigned char *prev_ptr = server->previous_frame->get_rows()[
1790 search_x * pixel_size;
1791 unsigned char *current_ptr = server->current_frame->get_rows()[
1793 pkg->block_x1 * pixel_size;
1795 // With subpixel, there are two ways to compare each position, one by shifting
1796 // the previous frame and two by shifting the current frame.
1797 pkg->difference1 = plugin->abs_diff_sub(prev_ptr,
1800 pkg->block_x2 - pkg->block_x1,
1801 pkg->block_y2 - pkg->block_y1,
1805 pkg->difference2 = plugin->abs_diff_sub(current_ptr,
1808 pkg->block_x2 - pkg->block_x1,
1809 pkg->block_y2 - pkg->block_y1,
1813 // printf("MotionCVScanUnit::process_package sub_x=%d sub_y=%d search_x=%d search_y=%d diff1=%lld diff2=%lld\n",
1818 // pkg->difference1,
1819 // pkg->difference2);
1836 int64_t MotionCVScanUnit::get_cache(int x, int y)
1838 int64_t result = -1;
1839 cache_lock->lock("MotionCVScanUnit::get_cache");
1840 for(int i = 0; i < cache.total; i++)
1842 MotionCVScanCache *ptr = cache.values[i];
1843 if(ptr->x == x && ptr->y == y)
1845 result = ptr->difference;
1849 cache_lock->unlock();
1853 void MotionCVScanUnit::put_cache(int x, int y, int64_t difference)
1855 MotionCVScanCache *ptr = new MotionCVScanCache(x, y, difference);
1856 cache_lock->lock("MotionCVScanUnit::put_cache");
1858 cache_lock->unlock();
1871 MotionCVScan::MotionCVScan(MotionCVMain *plugin,
1876 total_clients, total_packages
1879 this->plugin = plugin;
1880 cache_lock = new Mutex("MotionCVScan::cache_lock");
1883 MotionCVScan::~MotionCVScan()
1889 void MotionCVScan::init_packages()
1891 // Set package coords
1892 for(int i = 0; i < get_total_packages(); i++)
1894 MotionCVScanPackage *pkg = (MotionCVScanPackage*)get_package(i);
1896 pkg->block_x1 = block_x1;
1897 pkg->block_x2 = block_x2;
1898 pkg->block_y1 = block_y1;
1899 pkg->block_y2 = block_y2;
1900 pkg->scan_x1 = scan_x1;
1901 pkg->scan_x2 = scan_x2;
1902 pkg->scan_y1 = scan_y1;
1903 pkg->scan_y2 = scan_y2;
1904 pkg->pixel = (int64_t)i * (int64_t)total_pixels / (int64_t)total_steps;
1905 pkg->difference1 = 0;
1906 pkg->difference2 = 0;
1913 LoadClient* MotionCVScan::new_client()
1915 return new MotionCVScanUnit(this, plugin);
1918 LoadPackage* MotionCVScan::new_package()
1920 return new MotionCVScanPackage;
1924 void MotionCVScan::scan_frame(VFrame *previous_frame,
1925 VFrame *current_frame)
1927 this->previous_frame = previous_frame;
1928 this->current_frame = current_frame;
1931 cache.remove_all_objects();
1934 // Single macroblock
1935 int w = current_frame->get_w();
1936 int h = current_frame->get_h();
1938 // Initial search parameters
1939 int scan_w = w * plugin->config.global_range_w / 100;
1940 int scan_h = h * plugin->config.global_range_h / 100;
1941 int block_w = w * plugin->config.global_block_w / 100;
1942 int block_h = h * plugin->config.global_block_h / 100;
1944 // Location of block in previous frame
1945 block_x1 = (int)(w * plugin->config.block_x / 100 - block_w / 2);
1946 block_y1 = (int)(h * plugin->config.block_y / 100 - block_h / 2);
1947 block_x2 = (int)(w * plugin->config.block_x / 100 + block_w / 2);
1948 block_y2 = (int)(h * plugin->config.block_y / 100 + block_h / 2);
1950 // Offset to location of previous block. This offset needn't be very accurate
1951 // since it's the offset of the previous image and current image we want.
1952 if(plugin->config.mode3 == MotionCVConfig::TRACK_PREVIOUS)
1954 block_x1 += plugin->total_dx / OVERSAMPLE;
1955 block_y1 += plugin->total_dy / OVERSAMPLE;
1956 block_x2 += plugin->total_dx / OVERSAMPLE;
1957 block_y2 += plugin->total_dy / OVERSAMPLE;
1962 switch(plugin->config.mode2)
1965 case MotionCVConfig::NO_CALCULATE:
1971 case MotionCVConfig::LOAD:
1973 dx_result = plugin->load_dx;
1974 dy_result = plugin->load_dy;
1979 // Scan from scratch
1988 // Location of block in current frame
1989 int x_result = block_x1;
1990 int y_result = block_y1;
1992 // printf("MotionCVScan::scan_frame 1 %d %d %d %d %d %d %d %d\n",
1993 // block_x1 + block_w / 2,
1994 // block_y1 + block_h / 2,
2004 scan_x1 = x_result - scan_w / 2;
2005 scan_y1 = y_result - scan_h / 2;
2006 scan_x2 = x_result + scan_w / 2;
2007 scan_y2 = y_result + scan_h / 2;
2011 // Zero out requested values
2012 if(plugin->config.horizontal_only)
2015 scan_y2 = block_y1 + 1;
2017 if(plugin->config.vertical_only)
2020 scan_x2 = block_x1 + 1;
2023 // printf("MotionCVScan::scan_frame 1 %d %d %d %d %d %d %d %d\n",
2032 // Clamp the block coords before the scan so we get useful scan coords.
2033 MotionCVMain::clamp_scan(w,
2044 // printf("MotionCVScan::scan_frame 1\n block_x1=%d block_y1=%d block_x2=%d block_y2=%d\n scan_x1=%d scan_y1=%d scan_x2=%d scan_y2=%d\n x_result=%d y_result=%d\n",
2057 // Give up if invalid coords.
2058 if(scan_y2 <= scan_y1 ||
2059 scan_x2 <= scan_x1 ||
2060 block_x2 <= block_x1 ||
2061 block_y2 <= block_y1)
2064 // For subpixel, the top row and left column are skipped
2067 if(plugin->config.horizontal_only ||
2068 plugin->config.vertical_only)
2070 total_pixels = 4 * OVERSAMPLE * OVERSAMPLE - 4 * OVERSAMPLE;
2074 total_pixels = 4 * OVERSAMPLE;
2077 total_steps = total_pixels;
2079 set_package_count(total_steps);
2082 // Get least difference
2083 int64_t min_difference = -1;
2084 for(int i = 0; i < get_total_packages(); i++)
2086 MotionCVScanPackage *pkg = (MotionCVScanPackage*)get_package(i);
2087 if(pkg->difference1 < min_difference || min_difference == -1)
2089 min_difference = pkg->difference1;
2091 if(plugin->config.vertical_only)
2092 x_result = scan_x1 * OVERSAMPLE;
2094 x_result = scan_x1 * OVERSAMPLE +
2095 (pkg->pixel % (OVERSAMPLE * 2 - 1)) + 1;
2097 if(plugin->config.horizontal_only)
2098 y_result = scan_y1 * OVERSAMPLE;
2100 y_result = scan_y1 * OVERSAMPLE +
2101 (pkg->pixel / (OVERSAMPLE * 2 - 1)) + 1;
2105 dx_result = block_x1 * OVERSAMPLE - x_result;
2106 dy_result = block_y1 * OVERSAMPLE - y_result;
2109 if(pkg->difference2 < min_difference)
2111 min_difference = pkg->difference2;
2113 if(plugin->config.vertical_only)
2114 x_result = scan_x1 * OVERSAMPLE;
2116 x_result = scan_x2 * OVERSAMPLE -
2117 ((pkg->pixel % (OVERSAMPLE * 2 - 1)) + 1);
2119 if(plugin->config.horizontal_only)
2120 y_result = scan_y1 * OVERSAMPLE;
2122 y_result = scan_y2 * OVERSAMPLE -
2123 ((pkg->pixel / (OVERSAMPLE * 2 - 1)) + 1);
2125 dx_result = block_x1 * OVERSAMPLE - x_result;
2126 dy_result = block_y1 * OVERSAMPLE - y_result;
2130 //printf("MotionCVScan::scan_frame 1 %d %d %d %d\n", block_x1, block_y1, x_result, y_result);
2135 total_pixels = (scan_x2 - scan_x1) * (scan_y2 - scan_y1);
2136 total_steps = MIN(plugin->config.global_positions, total_pixels);
2138 set_package_count(total_steps);
2141 // Get least difference
2142 int64_t min_difference = -1;
2143 for(int i = 0; i < get_total_packages(); i++)
2145 MotionCVScanPackage *pkg = (MotionCVScanPackage*)get_package(i);
2146 if(pkg->difference1 < min_difference || min_difference == -1)
2148 min_difference = pkg->difference1;
2149 x_result = scan_x1 + (pkg->pixel % (scan_x2 - scan_x1));
2150 y_result = scan_y1 + (pkg->pixel / (scan_x2 - scan_x1));
2151 x_result *= OVERSAMPLE;
2152 y_result *= OVERSAMPLE;
2156 // printf("MotionCVScan::scan_frame 10 total_steps=%d total_pixels=%d subpixel=%d\n",
2161 // printf(" scan w=%d h=%d scan x1=%d y1=%d x2=%d y2=%d\n",
2169 // printf("MotionCVScan::scan_frame 2 block x1=%d y1=%d x2=%d y2=%d result x=%.2f y=%.2f\n",
2174 // (float)x_result / 4,
2175 // (float)y_result / 4);
2178 // If a new search is required, rescale results back to pixels.
2179 if(total_steps >= total_pixels)
2181 // Single pixel accuracy reached. Now do exhaustive subpixel search.
2182 if(plugin->config.mode1 == MotionCVConfig::STABILIZE ||
2183 plugin->config.mode1 == MotionCVConfig::TRACK ||
2184 plugin->config.mode1 == MotionCVConfig::NOTHING)
2186 x_result /= OVERSAMPLE;
2187 y_result /= OVERSAMPLE;
2194 // Fill in results and quit
2195 dx_result = block_x1 * OVERSAMPLE - x_result;
2196 dy_result = block_y1 * OVERSAMPLE - y_result;
2201 // Reduce scan area and try again
2203 scan_w = (scan_x2 - scan_x1) / 2;
2204 scan_h = (scan_y2 - scan_y1) / 2;
2205 x_result /= OVERSAMPLE;
2206 y_result /= OVERSAMPLE;
2211 // Add offsets from the "tracked single frame"
2212 dx_result = plugin->dx_offset - dx_result;
2213 dy_result = plugin->dy_offset - dy_result;
2215 if(plugin->config.mode2 == MotionCVConfig::SAVE)
2217 plugin->save_dx = dx_result;
2218 plugin->save_dy = dy_result;
2223 printf("MotionCVScan::scan_frame 10 dx=%.2f dy=%.2f\n",
2224 (float)this->dx_result / OVERSAMPLE,
2225 (float)this->dy_result / OVERSAMPLE);
2245 int64_t MotionCVScan::get_cache(int x, int y)
2247 int64_t result = -1;
2248 cache_lock->lock("MotionCVScan::get_cache");
2249 for(int i = 0; i < cache.total; i++)
2251 MotionCVScanCache *ptr = cache.values[i];
2252 if(ptr->x == x && ptr->y == y)
2254 result = ptr->difference;
2258 cache_lock->unlock();
2262 void MotionCVScan::put_cache(int x, int y, int64_t difference)
2264 MotionCVScanCache *ptr = new MotionCVScanCache(x, y, difference);
2265 cache_lock->lock("MotionCVScan::put_cache");
2267 cache_lock->unlock();
2274 MotionCVScanCache::MotionCVScanCache(int x, int y, int64_t difference)
2278 this->difference = difference;
2294 RotateCVScanPackage::RotateCVScanPackage()
2299 RotateCVScanUnit::RotateCVScanUnit(RotateCVScan *server, MotionCVMain *plugin)
2300 : LoadClient(server)
2302 this->server = server;
2303 this->plugin = plugin;
2308 RotateCVScanUnit::~RotateCVScanUnit()
2314 void RotateCVScanUnit::process_package(LoadPackage *package)
2316 if(server->skip) return;
2317 RotateCVScanPackage *pkg = (RotateCVScanPackage*)package;
2319 if((pkg->difference = server->get_cache(pkg->angle)) < 0)
2321 //printf("RotateCVScanUnit::process_package 1\n");
2322 int color_model = server->previous_frame->get_color_model();
2323 int pixel_size = BC_CModels::calculate_pixelsize(color_model);
2324 int row_bytes = server->previous_frame->get_bytes_per_line();
2327 rotater = new AffineEngine(1, 1);
2328 if(!temp) temp = new VFrame(
2329 server->previous_frame->get_w(),
2330 server->previous_frame->get_h(),
2334 // RotateCV original block size
2335 rotater->set_viewport(server->block_x1,
2337 server->block_x2 - server->block_x1,
2338 server->block_y2 - server->block_y1);
2339 rotater->set_pivot(server->block_x, server->block_y);
2341 rotater->rotate(temp,
2342 server->previous_frame,
2344 // Clamp coordinates
2345 int x1 = server->scan_x;
2346 int y1 = server->scan_y;
2347 int x2 = x1 + server->scan_w;
2348 int y2 = y1 + server->scan_h;
2349 x2 = MIN(temp->get_w(), x2);
2350 y2 = MIN(temp->get_h(), y2);
2351 x2 = MIN(server->current_frame->get_w(), x2);
2352 y2 = MIN(server->current_frame->get_h(), y2);
2356 if(x2 > x1 && y2 > y1)
2358 pkg->difference = plugin->abs_diff(
2359 temp->get_rows()[y1] + x1 * pixel_size,
2360 server->current_frame->get_rows()[y1] + x1 * pixel_size,
2365 //printf("RotateCVScanUnit::process_package %d\n", __LINE__);
2366 server->put_cache(pkg->angle, pkg->difference);
2369 // printf("RotateCVScanUnit::process_package 10 x=%d y=%d w=%d h=%d block_x=%d block_y=%d angle=%f scan_w=%d scan_h=%d diff=%lld\n",
2370 // server->block_x1,
2371 // server->block_y1,
2372 // server->block_x2 - server->block_x1,
2373 // server->block_y2 - server->block_y1,
2379 // pkg->difference);
2404 RotateCVScan::RotateCVScan(MotionCVMain *plugin,
2409 total_clients, total_packages
2412 this->plugin = plugin;
2413 cache_lock = new Mutex("RotateCVScan::cache_lock");
2417 RotateCVScan::~RotateCVScan()
2422 void RotateCVScan::init_packages()
2424 for(int i = 0; i < get_total_packages(); i++)
2426 RotateCVScanPackage *pkg = (RotateCVScanPackage*)get_package(i);
2428 (scan_angle2 - scan_angle1) /
2434 LoadClient* RotateCVScan::new_client()
2436 return new RotateCVScanUnit(this, plugin);
2439 LoadPackage* RotateCVScan::new_package()
2441 return new RotateCVScanPackage;
2445 float RotateCVScan::scan_frame(VFrame *previous_frame,
2446 VFrame *current_frame,
2451 this->block_x = block_x;
2452 this->block_y = block_y;
2454 switch(plugin->config.mode2)
2456 case MotionCVConfig::NO_CALCULATE:
2461 case MotionCVConfig::LOAD:
2463 result = plugin->load_dt;
2470 this->previous_frame = previous_frame;
2471 this->current_frame = current_frame;
2472 int w = current_frame->get_w();
2473 int h = current_frame->get_h();
2474 int block_w = w * plugin->config.rotation_block_w / 100;
2475 int block_h = h * plugin->config.rotation_block_h / 100;
2477 if(this->block_x - block_w / 2 < 0) block_w = this->block_x * 2;
2478 if(this->block_y - block_h / 2 < 0) block_h = this->block_y * 2;
2479 if(this->block_x + block_w / 2 > w) block_w = (w - this->block_x) * 2;
2480 if(this->block_y + block_h / 2 > h) block_h = (h - this->block_y) * 2;
2482 block_x1 = this->block_x - block_w / 2;
2483 block_x2 = this->block_x + block_w / 2;
2484 block_y1 = this->block_y - block_h / 2;
2485 block_y2 = this->block_y + block_h / 2;
2488 // Calculate the maximum area available to scan after rotation.
2489 // Must be calculated from the starting range because of cache.
2490 // Get coords of rectangle after rotation.
2491 double center_x = this->block_x;
2492 double center_y = this->block_y;
2493 double max_angle = plugin->config.rotation_range;
2494 double base_angle1 = atan((float)block_h / block_w);
2495 double base_angle2 = atan((float)block_w / block_h);
2496 double target_angle1 = base_angle1 + max_angle * 2 * M_PI / 360;
2497 double target_angle2 = base_angle2 + max_angle * 2 * M_PI / 360;
2498 double radius = sqrt(block_w * block_w + block_h * block_h) / 2;
2499 double x1 = center_x - cos(target_angle1) * radius;
2500 double y1 = center_y - sin(target_angle1) * radius;
2501 double x2 = center_x + sin(target_angle2) * radius;
2502 double y2 = center_y - cos(target_angle2) * radius;
2503 double x3 = center_x - sin(target_angle2) * radius;
2504 double y3 = center_y + cos(target_angle2) * radius;
2506 // Track top edge to find greatest area.
2507 double max_area1 = 0;
2508 //double max_x1 = 0;
2510 for(double x = x1; x < x2; x++)
2512 double y = y1 + (y2 - y1) * (x - x1) / (x2 - x1);
2513 if(x >= center_x && x < block_x2 && y >= block_y1 && y < center_y)
2515 double area = fabs(x - center_x) * fabs(y - center_y);
2516 if(area > max_area1)
2525 // Track left edge to find greatest area.
2526 double max_area2 = 0;
2528 //double max_y2 = 0;
2529 for(double y = y1; y < y3; y++)
2531 double x = x1 + (x3 - x1) * (y - y1) / (y3 - y1);
2532 if(x >= block_x1 && x < center_x && y >= block_y1 && y < center_y)
2534 double area = fabs(x - center_x) * fabs(y - center_y);
2535 if(area > max_area2)
2544 double max_x, max_y;
2548 // Get reduced scan coords
2549 scan_w = (int)(fabs(max_x - center_x) * 2);
2550 scan_h = (int)(fabs(max_y - center_y) * 2);
2551 scan_x = (int)(center_x - scan_w / 2);
2552 scan_y = (int)(center_y - scan_h / 2);
2553 // printf("RotateCVScan::scan_frame center=%d,%d scan=%d,%d %dx%d\n",
2554 // this->block_x, this->block_y, scan_x, scan_y, scan_w, scan_h);
2555 // printf(" angle_range=%f block= %d,%d,%d,%d\n", max_angle, block_x1, block_y1, block_x2, block_y2);
2557 // Determine min angle from size of block
2558 double angle1 = atan((double)block_h / block_w);
2559 double angle2 = atan((double)(block_h - 1) / (block_w + 1));
2560 double min_angle = fabs(angle2 - angle1) / OVERSAMPLE;
2561 min_angle = MAX(min_angle, MIN_ANGLE);
2564 printf("RotateCVScan::scan_frame min_angle=%f\n", min_angle * 360 / 2 / M_PI);
2567 cache.remove_all_objects();
2570 // Initial search range
2571 float angle_range = (float)plugin->config.rotation_range;
2573 total_steps = plugin->config.rotate_positions;
2576 while(angle_range >= min_angle * total_steps)
2578 scan_angle1 = result - angle_range;
2579 scan_angle2 = result + angle_range;
2582 set_package_count(total_steps);
2583 //set_package_count(1);
2586 int64_t min_difference = -1;
2587 for(int i = 0; i < get_total_packages(); i++)
2589 RotateCVScanPackage *pkg = (RotateCVScanPackage*)get_package(i);
2590 if(pkg->difference < min_difference || min_difference == -1)
2592 min_difference = pkg->difference;
2593 result = pkg->angle;
2603 if(plugin->config.mode2 == MotionCVConfig::SAVE) {
2604 plugin->save_dt = result;
2609 printf("RotateCVScan::scan_frame 10 angle=%f\n", result);
2615 int64_t RotateCVScan::get_cache(float angle)
2617 int64_t result = -1;
2618 cache_lock->lock("RotateCVScan::get_cache");
2619 for(int i = 0; i < cache.total; i++)
2621 RotateCVScanCache *ptr = cache.values[i];
2622 if(fabs(ptr->angle - angle) <= MIN_ANGLE)
2624 result = ptr->difference;
2628 cache_lock->unlock();
2632 void RotateCVScan::put_cache(float angle, int64_t difference)
2634 RotateCVScanCache *ptr = new RotateCVScanCache(angle, difference);
2635 cache_lock->lock("RotateCVScan::put_cache");
2637 cache_lock->unlock();
2648 RotateCVScanCache::RotateCVScanCache(float angle, int64_t difference)
2650 this->angle = angle;
2651 this->difference = difference;