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;
182 save_dx = load_dx = 0;
183 save_dy = load_dy = 0;
184 save_dt = load_dt = 0;
187 current_rotate_ref = 0;
188 rotate_target_src = 0;
189 rotate_target_dst = 0;
192 MotionCVMain::~MotionCVMain()
197 delete [] search_area;
199 delete rotate_engine;
200 delete motion_rotate;
202 delete prev_global_ref;
203 delete current_global_ref;
204 delete global_target_src;
205 delete global_target_dst;
207 if( active_fp ) fclose(active_fp);
209 delete prev_rotate_ref;
210 delete current_rotate_ref;
211 delete rotate_target_src;
212 delete rotate_target_dst;
215 const char* MotionCVMain::plugin_title() { return _("MotionCV"); }
216 int MotionCVMain::is_realtime() { return 1; }
217 int MotionCVMain::is_multichannel() { return 1; }
220 NEW_WINDOW_MACRO(MotionCVMain, MotionCVWindow)
222 LOAD_CONFIGURATION_MACRO(MotionCVMain, MotionCVConfig)
226 void MotionCVMain::update_gui()
230 if(load_configuration())
232 thread->window->lock_window("MotionCVMain::update_gui");
233 MotionCVWindow *window = (MotionCVWindow *)thread->window;
235 char string[BCTEXTLEN];
236 sprintf(string, "%d", config.global_positions);
237 window->global_search_positions->set_text(string);
238 sprintf(string, "%d", config.rotate_positions);
239 window->rotation_search_positions->set_text(string);
241 window->global_block_w->update(config.global_block_w);
242 window->global_block_h->update(config.global_block_h);
243 window->rotation_block_w->update(config.rotation_block_w);
244 window->rotation_block_h->update(config.rotation_block_h);
245 window->block_x->update(config.block_x);
246 window->block_y->update(config.block_y);
247 window->block_x_text->update((float)config.block_x);
248 window->block_y_text->update((float)config.block_y);
249 window->magnitude->update(config.magnitude);
250 window->return_speed->update(config.return_speed);
253 window->track_single->update(config.mode3 == MotionCVConfig::TRACK_SINGLE);
254 window->track_frame_number->update(config.track_frame);
255 window->track_previous->update(config.mode3 == MotionCVConfig::TRACK_PREVIOUS);
256 window->previous_same->update(config.mode3 == MotionCVConfig::PREVIOUS_SAME_BLOCK);
257 if(config.mode3 != MotionCVConfig::TRACK_SINGLE)
258 window->track_frame_number->disable();
260 window->track_frame_number->enable();
262 window->mode1->set_text(
263 Mode1::to_text(config.mode1));
264 window->mode2->set_text(
265 Mode2::to_text(config.mode2));
266 window->mode3->set_text(
267 Mode3::to_text(config.horizontal_only, config.vertical_only));
268 window->master_layer->set_text(
269 MasterLayer::to_text(config.bottom_is_master));
272 window->update_mode();
273 window->unlock_window();
281 void MotionCVMain::save_data(KeyFrame *keyframe)
285 // cause data to be stored directly in text
286 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
287 output.tag.set_title("MOTION");
289 output.tag.set_property("BLOCK_COUNT", config.block_count);
290 output.tag.set_property("GLOBAL_POSITIONS", config.global_positions);
291 output.tag.set_property("ROTATE_POSITIONS", config.rotate_positions);
292 output.tag.set_property("GLOBAL_BLOCK_W", config.global_block_w);
293 output.tag.set_property("GLOBAL_BLOCK_H", config.global_block_h);
294 output.tag.set_property("ROTATION_BLOCK_W", config.rotation_block_w);
295 output.tag.set_property("ROTATION_BLOCK_H", config.rotation_block_h);
296 output.tag.set_property("BLOCK_X", config.block_x);
297 output.tag.set_property("BLOCK_Y", config.block_y);
298 output.tag.set_property("GLOBAL_RANGE_W", config.global_range_w);
299 output.tag.set_property("GLOBAL_RANGE_H", config.global_range_h);
300 output.tag.set_property("ROTATION_RANGE", config.rotation_range);
301 output.tag.set_property("MAGNITUDE", config.magnitude);
302 output.tag.set_property("RETURN_SPEED", config.return_speed);
303 output.tag.set_property("MODE1", config.mode1);
304 output.tag.set_property("GLOBAL", config.global);
305 output.tag.set_property("ROTATE", config.rotate);
306 output.tag.set_property("ADDTRACKEDFRAMEOFFSET", config.addtrackedframeoffset);
307 output.tag.set_property("TRACKING_FILE", config.tracking_file);
308 output.tag.set_property("MODE2", config.mode2);
309 output.tag.set_property("DRAW_VECTORS", config.draw_vectors);
310 output.tag.set_property("MODE3", config.mode3);
311 output.tag.set_property("TRACK_FRAME", config.track_frame);
312 output.tag.set_property("BOTTOM_IS_MASTER", config.bottom_is_master);
313 output.tag.set_property("HORIZONTAL_ONLY", config.horizontal_only);
314 output.tag.set_property("VERTICAL_ONLY", config.vertical_only);
316 output.tag.set_title("/MOTION");
318 output.terminate_string();
321 void MotionCVMain::read_data(KeyFrame *keyframe)
325 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
331 result = input.read_tag();
335 if(input.tag.title_is("MOTION"))
337 config.block_count = input.tag.get_property("BLOCK_COUNT", config.block_count);
338 config.global_positions = input.tag.get_property("GLOBAL_POSITIONS", config.global_positions);
339 config.rotate_positions = input.tag.get_property("ROTATE_POSITIONS", config.rotate_positions);
340 config.global_block_w = input.tag.get_property("GLOBAL_BLOCK_W", config.global_block_w);
341 config.global_block_h = input.tag.get_property("GLOBAL_BLOCK_H", config.global_block_h);
342 config.rotation_block_w = input.tag.get_property("ROTATION_BLOCK_W", config.rotation_block_w);
343 config.rotation_block_h = input.tag.get_property("ROTATION_BLOCK_H", config.rotation_block_h);
344 config.block_x = input.tag.get_property("BLOCK_X", config.block_x);
345 config.block_y = input.tag.get_property("BLOCK_Y", config.block_y);
346 config.global_range_w = input.tag.get_property("GLOBAL_RANGE_W", config.global_range_w);
347 config.global_range_h = input.tag.get_property("GLOBAL_RANGE_H", config.global_range_h);
348 config.rotation_range = input.tag.get_property("ROTATION_RANGE", config.rotation_range);
349 config.magnitude = input.tag.get_property("MAGNITUDE", config.magnitude);
350 config.return_speed = input.tag.get_property("RETURN_SPEED", config.return_speed);
351 config.mode1 = input.tag.get_property("MODE1", config.mode1);
352 config.global = input.tag.get_property("GLOBAL", config.global);
353 config.rotate = input.tag.get_property("ROTATE", config.rotate);
354 config.addtrackedframeoffset = input.tag.get_property("ADDTRACKEDFRAMEOFFSET", config.addtrackedframeoffset);
355 input.tag.get_property("TRACKING_FILE", config.tracking_file);
356 config.mode2 = input.tag.get_property("MODE2", config.mode2);
357 config.draw_vectors = input.tag.get_property("DRAW_VECTORS", config.draw_vectors);
358 config.mode3 = input.tag.get_property("MODE3", config.mode3);
359 config.track_frame = input.tag.get_property("TRACK_FRAME", config.track_frame);
360 config.bottom_is_master = input.tag.get_property("BOTTOM_IS_MASTER", config.bottom_is_master);
361 config.horizontal_only = input.tag.get_property("HORIZONTAL_ONLY", config.horizontal_only);
362 config.vertical_only = input.tag.get_property("VERTICAL_ONLY", config.vertical_only);
377 void MotionCVMain::allocate_temp(int w, int h, int color_model)
380 (temp_frame->get_w() != w ||
381 temp_frame->get_h() != h))
387 temp_frame = new VFrame(w, h, color_model);
392 void MotionCVMain::process_global()
394 if(!engine) engine = new MotionCVScan(this,
395 PluginClient::get_project_smp() + 1,
396 PluginClient::get_project_smp() + 1);
398 // Get the current motion vector between the previous and current frame
399 engine->scan_frame(current_global_ref, prev_global_ref);
400 current_dx = engine->dx_result;
401 current_dy = engine->dy_result;
403 // Add current motion vector to accumulation vector.
404 if(config.mode3 != MotionCVConfig::TRACK_SINGLE)
407 total_dx = (int64_t)total_dx * (100 - config.return_speed) / 100;
408 total_dy = (int64_t)total_dy * (100 - config.return_speed) / 100;
409 total_dx += engine->dx_result;
410 total_dy += engine->dy_result;
413 // Make accumulation vector current
415 total_dx = engine->dx_result;
416 total_dy = engine->dy_result;
419 // Clamp accumulation vector
420 if(config.magnitude < 100)
422 //int block_w = (int64_t)config.global_block_w *
423 // current_global_ref->get_w() / 100;
424 //int block_h = (int64_t)config.global_block_h *
425 // current_global_ref->get_h() / 100;
426 int block_x_orig = (int64_t)(config.block_x *
427 current_global_ref->get_w() /
429 int block_y_orig = (int64_t)(config.block_y *
430 current_global_ref->get_h() /
433 int max_block_x = (int64_t)(current_global_ref->get_w() - block_x_orig) *
437 int max_block_y = (int64_t)(current_global_ref->get_h() - block_y_orig) *
441 int min_block_x = (int64_t)-block_x_orig *
445 int min_block_y = (int64_t)-block_y_orig *
450 CLAMP(total_dx, min_block_x, max_block_x);
451 CLAMP(total_dy, min_block_y, max_block_y);
455 printf("MotionCVMain::process_global 2 total_dx=%.02f total_dy=%.02f\n",
456 (float)total_dx / OVERSAMPLE,
457 (float)total_dy / OVERSAMPLE);
460 if(config.mode3 != MotionCVConfig::TRACK_SINGLE && !config.rotate)
462 // Transfer current reference frame to previous reference frame and update
463 // counter. Must wait for rotate to compare.
464 prev_global_ref->copy_from(current_global_ref);
465 previous_frame_number = get_source_position();
468 // Decide what to do with target based on requested operation
469 int interpolation = NEAREST_NEIGHBOR;
474 case MotionCVConfig::NOTHING:
475 global_target_dst->copy_from(global_target_src);
477 case MotionCVConfig::TRACK_PIXEL:
478 interpolation = NEAREST_NEIGHBOR;
479 dx = (int)(total_dx / OVERSAMPLE);
480 dy = (int)(total_dy / OVERSAMPLE);
482 case MotionCVConfig::STABILIZE_PIXEL:
483 interpolation = NEAREST_NEIGHBOR;
484 dx = -(int)(total_dx / OVERSAMPLE);
485 dy = -(int)(total_dy / OVERSAMPLE);
488 case MotionCVConfig::TRACK:
489 interpolation = CUBIC_LINEAR;
490 dx = (float)total_dx / OVERSAMPLE;
491 dy = (float)total_dy / OVERSAMPLE;
493 case MotionCVConfig::STABILIZE:
494 interpolation = CUBIC_LINEAR;
495 dx = -(float)total_dx / OVERSAMPLE;
496 dy = -(float)total_dy / OVERSAMPLE;
501 if(config.mode1 != MotionCVConfig::NOTHING)
504 overlayer = new OverlayFrame(PluginClient::get_project_smp() + 1);
505 global_target_dst->clear_frame();
506 overlayer->overlay(global_target_dst,
510 global_target_src->get_w(),
511 global_target_src->get_h(),
514 (float)global_target_src->get_w() + dx,
515 (float)global_target_src->get_h() + dy,
524 void MotionCVMain::process_rotation()
529 // Convert the previous global reference into the previous rotation reference.
530 // Convert global target destination into rotation target source.
534 overlayer = new OverlayFrame(PluginClient::get_project_smp() + 1);
537 if(config.mode3 == MotionCVConfig::TRACK_SINGLE)
539 dx = (float)total_dx / OVERSAMPLE;
540 dy = (float)total_dy / OVERSAMPLE;
544 dx = (float)current_dx / OVERSAMPLE;
545 dy = (float)current_dy / OVERSAMPLE;
548 prev_rotate_ref->clear_frame();
549 overlayer->overlay(prev_rotate_ref,
553 prev_global_ref->get_w(),
554 prev_global_ref->get_h(),
557 (float)prev_global_ref->get_w() + dx,
558 (float)prev_global_ref->get_h() + dy,
562 // Pivot is destination global position
563 block_x = (int)(prev_rotate_ref->get_w() *
568 block_y = (int)(prev_rotate_ref->get_h() *
573 // Use the global target output as the rotation target input
574 rotate_target_src->copy_from(global_target_dst);
575 // Transfer current reference frame to previous reference frame for global.
576 if(config.mode3 != MotionCVConfig::TRACK_SINGLE)
578 prev_global_ref->copy_from(current_global_ref);
579 previous_frame_number = get_source_position();
585 block_x = (int)(prev_rotate_ref->get_w() *
588 block_y = (int)(prev_rotate_ref->get_h() *
597 motion_rotate = new RotateCVScan(this,
598 get_project_smp() + 1,
599 get_project_smp() + 1);
601 current_angle = motion_rotate->scan_frame(prev_rotate_ref,
608 // Add current rotation to accumulation
609 if(config.mode3 != MotionCVConfig::TRACK_SINGLE)
612 total_angle = total_angle * (100 - config.return_speed) / 100;
613 total_angle += current_angle;
617 // Transfer current reference frame to previous reference frame and update
619 prev_rotate_ref->copy_from(current_rotate_ref);
620 previous_frame_number = get_source_position();
625 total_angle = current_angle;
629 printf("MotionCVMain::process_rotation total_angle=%f\n", total_angle);
633 // Calculate rotation parameters based on requested operation
637 case MotionCVConfig::NOTHING:
638 rotate_target_dst->copy_from(rotate_target_src);
640 case MotionCVConfig::TRACK:
641 case MotionCVConfig::TRACK_PIXEL:
644 case MotionCVConfig::STABILIZE:
645 case MotionCVConfig::STABILIZE_PIXEL:
646 angle = -total_angle;
652 if(config.mode1 != MotionCVConfig::NOTHING)
655 rotate_engine = new AffineEngine(PluginClient::get_project_smp() + 1,
656 PluginClient::get_project_smp() + 1);
658 rotate_target_dst->clear_frame();
660 // Determine pivot based on a number of factors.
663 case MotionCVConfig::TRACK:
664 case MotionCVConfig::TRACK_PIXEL:
665 // Use destination of global tracking.
666 rotate_engine->set_pivot(block_x, block_y);
669 case MotionCVConfig::STABILIZE:
670 case MotionCVConfig::STABILIZE_PIXEL:
673 // Use origin of global stabilize operation
674 rotate_engine->set_pivot((int)(rotate_target_dst->get_w() *
677 (int)(rotate_target_dst->get_h() *
685 rotate_engine->set_pivot(block_x, block_y);
691 rotate_engine->rotate(rotate_target_dst, rotate_target_src, angle);
692 // overlayer->overlay(rotate_target_dst,
696 // prev_rotate_ref->get_w(),
697 // prev_rotate_ref->get_h(),
700 // prev_rotate_ref->get_w(),
701 // prev_rotate_ref->get_h(),
705 // overlayer->overlay(rotate_target_dst,
706 // current_rotate_ref,
709 // prev_rotate_ref->get_w(),
710 // prev_rotate_ref->get_h(),
713 // prev_rotate_ref->get_w(),
714 // prev_rotate_ref->get_h(),
733 int MotionCVMain::process_buffer(VFrame **frame,
734 int64_t start_position,
737 int prev_config_mode2 = config.mode2;
738 int need_reconfigure = load_configuration();
739 int color_model = frame[0]->get_color_model();
740 w = frame[0]->get_w();
741 h = frame[0]->get_h();
745 printf("MotionCVMain::process_buffer 1 start_position=%jd\n", start_position);
749 // Calculate the source and destination pointers for each of the operations.
750 // Get the layer to track motion in.
751 reference_layer = config.bottom_is_master ?
752 PluginClient::total_in_buffers - 1 :
754 // Get the layer to apply motion in.
755 target_layer = config.bottom_is_master ?
757 PluginClient::total_in_buffers - 1;
760 output_frame = frame[target_layer];
763 // Get the position of previous reference frame.
764 int64_t actual_previous_number;
765 // Skip if match frame not available
766 int skip_current = 0;
769 if(config.mode3 == MotionCVConfig::TRACK_SINGLE)
771 actual_previous_number = config.track_frame;
772 if(get_direction() == PLAY_REVERSE)
773 actual_previous_number++;
774 if(actual_previous_number == start_position)
779 actual_previous_number = start_position;
780 if(get_direction() == PLAY_FORWARD)
782 actual_previous_number--;
783 if(actual_previous_number < get_source_start())
787 KeyFrame *keyframe = get_prev_keyframe(start_position, 1);
788 if(keyframe->position > 0 &&
789 actual_previous_number < keyframe->position)
795 actual_previous_number++;
796 if(actual_previous_number >= get_source_start() + get_total_len())
800 KeyFrame *keyframe = get_next_keyframe(start_position, 1);
801 if(keyframe->position > 0 &&
802 actual_previous_number >= keyframe->position)
807 // Only count motion since last keyframe
813 if(!config.global && !config.rotate) skip_current = 1;
818 // printf("process_realtime %d %lld %lld\n",
820 // previous_frame_number,
821 // actual_previous_number);
822 // Load match frame and reset vectors
823 int need_reload = !skip_current &&
824 (previous_frame_number != actual_previous_number ||
831 previous_frame_number = actual_previous_number;
833 if( prev_config_mode2 != MotionCVConfig::SAVE &&
834 config.mode2 == MotionCVConfig::SAVE ) {
836 printf("MotionCVMain::process_buffer 2 remove tracking file: %s\n", config.tracking_file);
838 ::remove(config.tracking_file);
855 // Get the global pointers. Here we walk through the sequence of events.
858 // Assume global only. Global reads previous frame and compares
859 // with current frame to get the current translation.
860 // The center of the search area is fixed in compensate mode or
861 // the user value + the accumulation vector in track mode.
863 prev_global_ref = new VFrame(w, h, color_model);
864 if(!current_global_ref)
865 current_global_ref = new VFrame(w, h, color_model);
867 // Global loads the current target frame into the src and
868 // writes it to the dst frame with desired translation.
869 if(!global_target_src)
870 global_target_src = new VFrame(w, h, color_model);
871 if(!global_target_dst)
872 global_target_dst = new VFrame(w, h, color_model);
875 // Load the global frames
878 read_frame(prev_global_ref,
880 previous_frame_number,
885 read_frame(current_global_ref,
890 read_frame(global_target_src,
898 // Global followed by rotate
901 // Must translate the previous global reference by the current global
902 // accumulation vector to match the current global reference.
903 // The center of the search area is always the user value + the accumulation
906 prev_rotate_ref = new VFrame(w, h, color_model);
907 // The current global reference is the current rotation reference.
908 if(!current_rotate_ref)
909 current_rotate_ref = new VFrame(w, h, color_model);
910 current_rotate_ref->copy_from(current_global_ref);
912 // The global target destination is copied to the rotation target source
913 // then written to the rotation output with rotation.
914 // The pivot for the rotation is the center of the search area
915 // if we're tracking.
916 // The pivot is fixed to the user position if we're compensating.
917 if(!rotate_target_src)
918 rotate_target_src = new VFrame(w, h, color_model);
919 if(!rotate_target_dst)
920 rotate_target_dst = new VFrame(w,h , color_model);
927 // Rotation reads the previous reference frame and compares it with current
930 prev_rotate_ref = new VFrame(w, h, color_model);
931 if(!current_rotate_ref)
932 current_rotate_ref = new VFrame(w, h, color_model);
934 // Rotation loads target frame to temporary, rotates it, and writes it to the
935 // target frame. The pivot is always fixed.
936 if(!rotate_target_src)
937 rotate_target_src = new VFrame(w, h, color_model);
938 if(!rotate_target_dst)
939 rotate_target_dst = new VFrame(w,h , color_model);
942 // Load the rotate frames
945 read_frame(prev_rotate_ref,
947 previous_frame_number,
951 read_frame(current_rotate_ref,
956 read_frame(rotate_target_src,
966 if( config.mode2 == MotionCVConfig::LOAD ) {
967 char line[BCTEXTLEN];
968 int64_t frame_no, no; int dx, dy; float dt;
969 if( config.addtrackedframeoffset && config.track_frame != tracking_frame ) {
970 tracking_frame = frame_no = config.track_frame;
971 if( !get_line_key(config.tracking_file, frame_no, line, sizeof(line)) &&
972 sscanf(line, "%jd %d %d %f", &no, &dx, &dy, &dt) == 4 ) {
973 dx_offset = dx; dy_offset = dy;
977 printf("MotionCVMain::process_buffer: no offset data frame %jd\n", frame_no);
979 dx_offset = 0; dy_offset = 0;
982 // Load result from disk
984 frame_no = get_source_position();
985 if( !get_line_key(config.tracking_file, frame_no, line, sizeof(line)) &&
986 sscanf(line, "%jd %d %d %f", &frame_no, &dx, &dy, &dt) == 4 ) {
987 load_ok= 1; load_dx = dx; load_dy = dy; load_dt = dt;
991 printf("MotionCVMain::process_buffer: no tracking data frame %jd\n", frame_no);
996 // Get position change from previous frame to current frame
997 if(config.global) process_global();
998 // Get rotation change from previous frame to current frame
999 if(config.rotate) process_rotation();
1000 //frame[target_layer]->copy_from(prev_rotate_ref);
1001 //frame[target_layer]->copy_from(current_rotate_ref);
1003 // write results to disk
1004 if( config.mode2 == MotionCVConfig::SAVE ) {
1005 FILE *output = fopen(config.tracking_file, "aw");
1007 int64_t frame_no = get_source_position();
1008 fprintf(output, "%jd %d %d %f\n", frame_no, save_dx, save_dy, save_dt);
1012 perror("MotionCVMain::process buffer save");
1018 // Transfer the relevant target frame to the output
1023 frame[target_layer]->copy_from(rotate_target_dst);
1027 frame[target_layer]->copy_from(global_target_dst);
1031 // Read the target destination directly
1033 read_frame(frame[target_layer],
1040 if(config.draw_vectors)
1042 draw_vectors(frame[target_layer]);
1046 printf("MotionCVMain::process_buffer 100\n");
1052 void MotionCVMain::clamp_scan(int w,
1064 // printf("MotionCVMain::clamp_scan 1 w=%d h=%d block=%d %d %d %d scan=%d %d %d %d absolute=%d\n",
1079 // scan is always out of range before block.
1082 int difference = -*scan_x1;
1083 *block_x1 += difference;
1089 int difference = -*scan_y1;
1090 *block_y1 += difference;
1096 int difference = *scan_x2 - w;
1097 *block_x2 -= difference;
1098 *scan_x2 -= difference;
1103 int difference = *scan_y2 - h;
1104 *block_y2 -= difference;
1105 *scan_y2 -= difference;
1108 CLAMP(*scan_x1, 0, w);
1109 CLAMP(*scan_y1, 0, h);
1110 CLAMP(*scan_x2, 0, w);
1111 CLAMP(*scan_y2, 0, h);
1117 int difference = -*scan_x1;
1118 *block_x1 += difference;
1119 *scan_x2 += difference;
1125 int difference = -*scan_y1;
1126 *block_y1 += difference;
1127 *scan_y2 += difference;
1131 if(*scan_x2 - *block_x1 + *block_x2 > w)
1133 int difference = *scan_x2 - *block_x1 + *block_x2 - w;
1134 *block_x2 -= difference;
1137 if(*scan_y2 - *block_y1 + *block_y2 > h)
1139 int difference = *scan_y2 - *block_y1 + *block_y2 - h;
1140 *block_y2 -= difference;
1143 // CLAMP(*scan_x1, 0, w - (*block_x2 - *block_x1));
1144 // CLAMP(*scan_y1, 0, h - (*block_y2 - *block_y1));
1145 // CLAMP(*scan_x2, 0, w - (*block_x2 - *block_x1));
1146 // CLAMP(*scan_y2, 0, h - (*block_y2 - *block_y1));
1149 // Sanity checks which break the calculation but should never happen if the
1150 // center of the block is inside the frame.
1151 CLAMP(*block_x1, 0, w);
1152 CLAMP(*block_x2, 0, w);
1153 CLAMP(*block_y1, 0, h);
1154 CLAMP(*block_y2, 0, h);
1156 // printf("MotionCVMain::clamp_scan 2 w=%d h=%d block=%d %d %d %d scan=%d %d %d %d absolute=%d\n",
1172 void MotionCVMain::draw_vectors(VFrame *frame)
1174 int w = frame->get_w();
1175 int h = frame->get_h();
1176 int global_x1, global_y1;
1177 int global_x2, global_y2;
1178 int block_x, block_y;
1179 int block_w, block_h;
1180 int block_x1, block_y1;
1181 int block_x2, block_y2;
1182 int block_x3, block_y3;
1183 int block_x4, block_y4;
1184 int search_w, search_h;
1185 int search_x1, search_y1;
1186 int search_x2, search_y2;
1187 //int search_x3, search_y3;
1188 //int search_x4, search_y4;
1193 // Start of vector is center of previous block.
1194 // End of vector is total accumulation.
1195 if(config.mode3 == MotionCVConfig::TRACK_SINGLE)
1197 global_x1 = (int64_t)(config.block_x *
1200 global_y1 = (int64_t)(config.block_y *
1203 global_x2 = global_x1 + total_dx / OVERSAMPLE;
1204 global_y2 = global_y1 + total_dy / OVERSAMPLE;
1205 //printf("MotionCVMain::draw_vectors %d %d %d %d %d %d\n", total_dx, total_dy, global_x1, global_y1, global_x2, global_y2);
1208 // Start of vector is center of previous block.
1209 // End of vector is current change.
1210 if(config.mode3 == MotionCVConfig::PREVIOUS_SAME_BLOCK)
1212 global_x1 = (int64_t)(config.block_x *
1215 global_y1 = (int64_t)(config.block_y *
1218 global_x2 = global_x1 + current_dx / OVERSAMPLE;
1219 global_y2 = global_y1 + current_dy / OVERSAMPLE;
1223 global_x1 = (int64_t)(config.block_x *
1226 (total_dx - current_dx) /
1228 global_y1 = (int64_t)(config.block_y *
1231 (total_dy - current_dy) /
1233 global_x2 = (int64_t)(config.block_x *
1238 global_y2 = (int64_t)(config.block_y *
1245 block_x = global_x1;
1246 block_y = global_y1;
1247 block_w = config.global_block_w * w / 100;
1248 block_h = config.global_block_h * h / 100;
1249 block_x1 = block_x - block_w / 2;
1250 block_y1 = block_y - block_h / 2;
1251 block_x2 = block_x + block_w / 2;
1252 block_y2 = block_y + block_h / 2;
1253 search_w = config.global_range_w * w / 100;
1254 search_h = config.global_range_h * h / 100;
1255 search_x1 = block_x1 - search_w / 2;
1256 search_y1 = block_y1 - search_h / 2;
1257 search_x2 = block_x2 + search_w / 2;
1258 search_y2 = block_y2 + search_h / 2;
1260 // printf("MotionCVMain::draw_vectors %d %d %d %d %d %d %d %d %d %d %d %d\n",
1287 draw_arrow(frame, global_x1, global_y1, global_x2, global_y2);
1290 draw_line(frame, block_x1, block_y1, block_x2, block_y1);
1291 draw_line(frame, block_x2, block_y1, block_x2, block_y2);
1292 draw_line(frame, block_x2, block_y2, block_x1, block_y2);
1293 draw_line(frame, block_x1, block_y2, block_x1, block_y1);
1297 draw_line(frame, search_x1, search_y1, search_x2, search_y1);
1298 draw_line(frame, search_x2, search_y1, search_x2, search_y2);
1299 draw_line(frame, search_x2, search_y2, search_x1, search_y2);
1300 draw_line(frame, search_x1, search_y2, search_x1, search_y1);
1302 // Block should be endpoint of motion
1305 block_x = global_x2;
1306 block_y = global_y2;
1311 block_x = (int64_t)(config.block_x * w / 100);
1312 block_y = (int64_t)(config.block_y * h / 100);
1315 block_w = config.rotation_block_w * w / 100;
1316 block_h = config.rotation_block_h * h / 100;
1319 float angle = total_angle * 2 * M_PI / 360;
1320 double base_angle1 = atan((float)block_h / block_w);
1321 double base_angle2 = atan((float)block_w / block_h);
1322 double target_angle1 = base_angle1 + angle;
1323 double target_angle2 = base_angle2 + angle;
1324 double radius = sqrt(block_w * block_w + block_h * block_h) / 2;
1325 block_x1 = (int)(block_x - cos(target_angle1) * radius);
1326 block_y1 = (int)(block_y - sin(target_angle1) * radius);
1327 block_x2 = (int)(block_x + sin(target_angle2) * radius);
1328 block_y2 = (int)(block_y - cos(target_angle2) * radius);
1329 block_x3 = (int)(block_x - sin(target_angle2) * radius);
1330 block_y3 = (int)(block_y + cos(target_angle2) * radius);
1331 block_x4 = (int)(block_x + cos(target_angle1) * radius);
1332 block_y4 = (int)(block_y + sin(target_angle1) * radius);
1334 draw_line(frame, block_x1, block_y1, block_x2, block_y2);
1335 draw_line(frame, block_x2, block_y2, block_x4, block_y4);
1336 draw_line(frame, block_x4, block_y4, block_x3, block_y3);
1337 draw_line(frame, block_x3, block_y3, block_x1, block_y1);
1343 draw_line(frame, block_x, block_y - 5, block_x, block_y + 6);
1344 draw_line(frame, block_x - 5, block_y, block_x + 6, block_y);
1351 void MotionCVMain::draw_pixel(VFrame *frame, int x, int y)
1353 if(!(x >= 0 && y >= 0 && x < frame->get_w() && y < frame->get_h())) return;
1355 #define DRAW_PIXEL(x, y, components, do_yuv, max, type) \
1357 type **rows = (type**)frame->get_rows(); \
1358 rows[y][x * components] = max - rows[y][x * components]; \
1361 rows[y][x * components + 1] = max - rows[y][x * components + 1]; \
1362 rows[y][x * components + 2] = max - rows[y][x * components + 2]; \
1366 rows[y][x * components + 1] = (max / 2 + 1) - rows[y][x * components + 1]; \
1367 rows[y][x * components + 2] = (max / 2 + 1) - rows[y][x * components + 2]; \
1369 if(components == 4) \
1370 rows[y][x * components + 3] = max; \
1374 switch(frame->get_color_model())
1377 DRAW_PIXEL(x, y, 3, 0, 0xff, unsigned char);
1380 DRAW_PIXEL(x, y, 4, 0, 0xff, unsigned char);
1383 DRAW_PIXEL(x, y, 3, 0, 1.0, float);
1386 DRAW_PIXEL(x, y, 4, 0, 1.0, float);
1389 DRAW_PIXEL(x, y, 3, 1, 0xff, unsigned char);
1392 DRAW_PIXEL(x, y, 4, 1, 0xff, unsigned char);
1395 DRAW_PIXEL(x, y, 3, 0, 0xffff, uint16_t);
1398 DRAW_PIXEL(x, y, 3, 1, 0xffff, uint16_t);
1400 case BC_RGBA16161616:
1401 DRAW_PIXEL(x, y, 4, 0, 0xffff, uint16_t);
1403 case BC_YUVA16161616:
1404 DRAW_PIXEL(x, y, 4, 1, 0xffff, uint16_t);
1410 void MotionCVMain::draw_line(VFrame *frame, int x1, int y1, int x2, int y2)
1412 int w = labs(x2 - x1);
1413 int h = labs(y2 - y1);
1414 //printf("MotionCVMain::draw_line 1 %d %d %d %d\n", x1, y1, x2, y2);
1418 draw_pixel(frame, x1, y1);
1423 // Flip coordinates so x1 < x2
1433 int numerator = y2 - y1;
1434 int denominator = x2 - x1;
1435 for(int i = x1; i < x2; i++)
1437 int y = y1 + (int64_t)(i - x1) * (int64_t)numerator / (int64_t)denominator;
1438 draw_pixel(frame, i, y);
1443 // Flip coordinates so y1 < y2
1453 int numerator = x2 - x1;
1454 int denominator = y2 - y1;
1455 for(int i = y1; i < y2; i++)
1457 int x = x1 + (int64_t)(i - y1) * (int64_t)numerator / (int64_t)denominator;
1458 draw_pixel(frame, x, i);
1461 //printf("MotionCVMain::draw_line 2\n");
1464 #define ARROW_SIZE 10
1465 void MotionCVMain::draw_arrow(VFrame *frame, int x1, int y1, int x2, int y2)
1467 double angle = atan((float)(y2 - y1) / (float)(x2 - x1));
1468 double angle1 = angle + (float)145 / 360 * 2 * 3.14159265;
1469 double angle2 = angle - (float)145 / 360 * 2 * 3.14159265;
1476 x3 = x2 - (int)(ARROW_SIZE * cos(angle1));
1477 y3 = y2 - (int)(ARROW_SIZE * sin(angle1));
1478 x4 = x2 - (int)(ARROW_SIZE * cos(angle2));
1479 y4 = y2 - (int)(ARROW_SIZE * sin(angle2));
1483 x3 = x2 + (int)(ARROW_SIZE * cos(angle1));
1484 y3 = y2 + (int)(ARROW_SIZE * sin(angle1));
1485 x4 = x2 + (int)(ARROW_SIZE * cos(angle2));
1486 y4 = y2 + (int)(ARROW_SIZE * sin(angle2));
1490 draw_line(frame, x1, y1, x2, y2);
1491 // draw_line(frame, x1, y1 + 1, x2, y2 + 1);
1494 if(abs(y2 - y1) || abs(x2 - x1)) draw_line(frame, x2, y2, x3, y3);
1495 // draw_line(frame, x2, y2 + 1, x3, y3 + 1);
1497 if(abs(y2 - y1) || abs(x2 - x1)) draw_line(frame, x2, y2, x4, y4);
1498 // draw_line(frame, x2, y2 + 1, x4, y4 + 1);
1504 #define ABS_DIFF(type, temp_type, multiplier, components) \
1506 temp_type result_temp = 0; \
1507 for(int i = 0; i < h; i++) \
1509 type *prev_row = (type*)prev_ptr; \
1510 type *current_row = (type*)current_ptr; \
1511 for(int j = 0; j < w; j++) \
1513 for(int k = 0; k < 3; k++) \
1515 temp_type difference; \
1516 difference = *prev_row++ - *current_row++; \
1517 if(difference < 0) \
1518 result_temp -= difference; \
1520 result_temp += difference; \
1522 if(components == 4) \
1528 prev_ptr += row_bytes; \
1529 current_ptr += row_bytes; \
1531 result = (int64_t)(result_temp * multiplier); \
1534 int64_t MotionCVMain::abs_diff(unsigned char *prev_ptr,
1535 unsigned char *current_ptr,
1545 ABS_DIFF(unsigned char, int64_t, 1, 3)
1548 ABS_DIFF(unsigned char, int64_t, 1, 4)
1551 ABS_DIFF(float, double, 0x10000, 3)
1554 ABS_DIFF(float, double, 0x10000, 4)
1557 ABS_DIFF(unsigned char, int64_t, 1, 3)
1560 ABS_DIFF(unsigned char, int64_t, 1, 4)
1563 ABS_DIFF(uint16_t, int64_t, 1, 3)
1565 case BC_YUVA16161616:
1566 ABS_DIFF(uint16_t, int64_t, 1, 4)
1574 #define ABS_DIFF_SUB(type, temp_type, multiplier, components) \
1576 temp_type result_temp = 0; \
1577 temp_type y2_fraction = sub_y * 0x100 / OVERSAMPLE; \
1578 temp_type y1_fraction = 0x100 - y2_fraction; \
1579 temp_type x2_fraction = sub_x * 0x100 / OVERSAMPLE; \
1580 temp_type x1_fraction = 0x100 - x2_fraction; \
1581 for(int i = 0; i < h_sub; i++) \
1583 type *prev_row1 = (type*)prev_ptr; \
1584 type *prev_row2 = (type*)prev_ptr + components; \
1585 type *prev_row3 = (type*)(prev_ptr + row_bytes); \
1586 type *prev_row4 = (type*)(prev_ptr + row_bytes) + components; \
1587 type *current_row = (type*)current_ptr; \
1588 for(int j = 0; j < w_sub; j++) \
1590 for(int k = 0; k < 3; k++) \
1592 temp_type difference; \
1593 temp_type prev_value = \
1594 (*prev_row1++ * x1_fraction * y1_fraction + \
1595 *prev_row2++ * x2_fraction * y1_fraction + \
1596 *prev_row3++ * x1_fraction * y2_fraction + \
1597 *prev_row4++ * x2_fraction * y2_fraction) / \
1599 temp_type current_value = *current_row++; \
1600 difference = prev_value - current_value; \
1601 if(difference < 0) \
1602 result_temp -= difference; \
1604 result_temp += difference; \
1607 if(components == 4) \
1616 prev_ptr += row_bytes; \
1617 current_ptr += row_bytes; \
1619 result = (int64_t)(result_temp * multiplier); \
1625 int64_t MotionCVMain::abs_diff_sub(unsigned char *prev_ptr,
1626 unsigned char *current_ptr,
1641 ABS_DIFF_SUB(unsigned char, int64_t, 1, 3)
1644 ABS_DIFF_SUB(unsigned char, int64_t, 1, 4)
1647 ABS_DIFF_SUB(float, double, 0x10000, 3)
1650 ABS_DIFF_SUB(float, double, 0x10000, 4)
1653 ABS_DIFF_SUB(unsigned char, int64_t, 1, 3)
1656 ABS_DIFF_SUB(unsigned char, int64_t, 1, 4)
1659 ABS_DIFF_SUB(uint16_t, int64_t, 1, 3)
1661 case BC_YUVA16161616:
1662 ABS_DIFF_SUB(uint16_t, int64_t, 1, 4)
1668 int MotionCVMain::get_line_key(const char *filename, int64_t key, char *line, int len)
1670 if( active_fp && strcmp(active_file, filename) ) {
1671 fclose(active_fp); active_fp = 0; active_file[0] = 0;
1674 if( !(active_fp = fopen(filename, "r")) ) {
1675 perror("open motion file");
1676 fprintf(stderr,"err reading key %jd\n", key);
1679 strcpy(active_file, filename);
1680 tracking_frame = -1;
1683 if( fgets(line, len, active_fp) && (recd=strtol(line,0,0)) == key )
1685 // binary search file
1686 fseek(active_fp, 0, SEEK_END);
1687 int64_t l = -1, r = ftell(active_fp);
1688 while( (r-l) > 1 ) {
1689 int64_t m = (l+r) / 2;
1690 fseek(active_fp, m, SEEK_SET);
1691 if( m > 0 && !fgets(line, len, active_fp) ) {
1692 fprintf(stderr,"err reading key %jd\n", key);
1695 if( fgets(line, len, active_fp) ) {
1696 recd = strtol(line,0,0);
1697 if( recd == key ) return 0;
1698 if( recd < key ) { l = m; continue; }
1707 MotionCVScanPackage::MotionCVScanPackage()
1714 MotionCVScanUnit::MotionCVScanUnit(MotionCVScan *server,
1715 MotionCVMain *plugin)
1716 : LoadClient(server)
1718 this->plugin = plugin;
1719 this->server = server;
1720 cache_lock = new Mutex("MotionCVScanUnit::cache_lock");
1723 MotionCVScanUnit::~MotionCVScanUnit()
1730 void MotionCVScanUnit::process_package(LoadPackage *package)
1732 MotionCVScanPackage *pkg = (MotionCVScanPackage*)package;
1733 //int w = server->current_frame->get_w();
1734 //int h = server->current_frame->get_h();
1735 int color_model = server->current_frame->get_color_model();
1736 int pixel_size = BC_CModels::calculate_pixelsize(color_model);
1737 int row_bytes = server->current_frame->get_bytes_per_line();
1740 if(!server->subpixel)
1742 int search_x = pkg->scan_x1 + (pkg->pixel % (pkg->scan_x2 - pkg->scan_x1));
1743 int search_y = pkg->scan_y1 + (pkg->pixel / (pkg->scan_x2 - pkg->scan_x1));
1746 pkg->difference1 = server->get_cache(search_x, search_y);
1747 if(pkg->difference1 < 0)
1749 //printf("MotionCVScanUnit::process_package 1 %d %d\n",
1750 //search_x, search_y, pkg->block_x2 - pkg->block_x1, pkg->block_y2 - pkg->block_y1);
1751 // Pointers to first pixel in each block
1752 unsigned char *prev_ptr = server->previous_frame->get_rows()[
1754 search_x * pixel_size;
1755 unsigned char *current_ptr = server->current_frame->get_rows()[
1757 pkg->block_x1 * pixel_size;
1759 pkg->difference1 = plugin->abs_diff(prev_ptr,
1762 pkg->block_x2 - pkg->block_x1,
1763 pkg->block_y2 - pkg->block_y1,
1765 //printf("MotionCVScanUnit::process_package 2\n");
1766 server->put_cache(search_x, search_y, pkg->difference1);
1772 int sub_x = pkg->pixel % (OVERSAMPLE * 2 - 1) + 1;
1773 int sub_y = pkg->pixel / (OVERSAMPLE * 2 - 1) + 1;
1775 if(plugin->config.horizontal_only)
1780 if(plugin->config.vertical_only)
1785 int search_x = pkg->scan_x1 + sub_x / OVERSAMPLE;
1786 int search_y = pkg->scan_y1 + sub_y / OVERSAMPLE;
1787 sub_x %= OVERSAMPLE;
1788 sub_y %= OVERSAMPLE;
1791 unsigned char *prev_ptr = server->previous_frame->get_rows()[
1793 search_x * pixel_size;
1794 unsigned char *current_ptr = server->current_frame->get_rows()[
1796 pkg->block_x1 * pixel_size;
1798 // With subpixel, there are two ways to compare each position, one by shifting
1799 // the previous frame and two by shifting the current frame.
1800 pkg->difference1 = plugin->abs_diff_sub(prev_ptr,
1803 pkg->block_x2 - pkg->block_x1,
1804 pkg->block_y2 - pkg->block_y1,
1808 pkg->difference2 = plugin->abs_diff_sub(current_ptr,
1811 pkg->block_x2 - pkg->block_x1,
1812 pkg->block_y2 - pkg->block_y1,
1816 // printf("MotionCVScanUnit::process_package sub_x=%d sub_y=%d search_x=%d search_y=%d diff1=%lld diff2=%lld\n",
1821 // pkg->difference1,
1822 // pkg->difference2);
1839 int64_t MotionCVScanUnit::get_cache(int x, int y)
1841 int64_t result = -1;
1842 cache_lock->lock("MotionCVScanUnit::get_cache");
1843 for(int i = 0; i < cache.total; i++)
1845 MotionCVScanCache *ptr = cache.values[i];
1846 if(ptr->x == x && ptr->y == y)
1848 result = ptr->difference;
1852 cache_lock->unlock();
1856 void MotionCVScanUnit::put_cache(int x, int y, int64_t difference)
1858 MotionCVScanCache *ptr = new MotionCVScanCache(x, y, difference);
1859 cache_lock->lock("MotionCVScanUnit::put_cache");
1861 cache_lock->unlock();
1874 MotionCVScan::MotionCVScan(MotionCVMain *plugin,
1879 total_clients, total_packages
1882 this->plugin = plugin;
1883 cache_lock = new Mutex("MotionCVScan::cache_lock");
1886 MotionCVScan::~MotionCVScan()
1892 void MotionCVScan::init_packages()
1894 // Set package coords
1895 for(int i = 0; i < get_total_packages(); i++)
1897 MotionCVScanPackage *pkg = (MotionCVScanPackage*)get_package(i);
1899 pkg->block_x1 = block_x1;
1900 pkg->block_x2 = block_x2;
1901 pkg->block_y1 = block_y1;
1902 pkg->block_y2 = block_y2;
1903 pkg->scan_x1 = scan_x1;
1904 pkg->scan_x2 = scan_x2;
1905 pkg->scan_y1 = scan_y1;
1906 pkg->scan_y2 = scan_y2;
1907 pkg->pixel = (int64_t)i * (int64_t)total_pixels / (int64_t)total_steps;
1908 pkg->difference1 = 0;
1909 pkg->difference2 = 0;
1916 LoadClient* MotionCVScan::new_client()
1918 return new MotionCVScanUnit(this, plugin);
1921 LoadPackage* MotionCVScan::new_package()
1923 return new MotionCVScanPackage;
1927 void MotionCVScan::scan_frame(VFrame *previous_frame,
1928 VFrame *current_frame)
1930 this->previous_frame = previous_frame;
1931 this->current_frame = current_frame;
1934 cache.remove_all_objects();
1937 // Single macroblock
1938 int w = current_frame->get_w();
1939 int h = current_frame->get_h();
1941 // Initial search parameters
1942 int scan_w = w * plugin->config.global_range_w / 100;
1943 int scan_h = h * plugin->config.global_range_h / 100;
1944 int block_w = w * plugin->config.global_block_w / 100;
1945 int block_h = h * plugin->config.global_block_h / 100;
1947 // Location of block in previous frame
1948 block_x1 = (int)(w * plugin->config.block_x / 100 - block_w / 2);
1949 block_y1 = (int)(h * plugin->config.block_y / 100 - block_h / 2);
1950 block_x2 = (int)(w * plugin->config.block_x / 100 + block_w / 2);
1951 block_y2 = (int)(h * plugin->config.block_y / 100 + block_h / 2);
1953 // Offset to location of previous block. This offset needn't be very accurate
1954 // since it's the offset of the previous image and current image we want.
1955 if(plugin->config.mode3 == MotionCVConfig::TRACK_PREVIOUS)
1957 block_x1 += plugin->total_dx / OVERSAMPLE;
1958 block_y1 += plugin->total_dy / OVERSAMPLE;
1959 block_x2 += plugin->total_dx / OVERSAMPLE;
1960 block_y2 += plugin->total_dy / OVERSAMPLE;
1965 switch(plugin->config.mode2)
1968 case MotionCVConfig::NO_CALCULATE:
1974 case MotionCVConfig::LOAD:
1976 if( plugin->load_ok ) {
1977 dx_result = plugin->load_dx;
1978 dy_result = plugin->load_dy;
1984 // Scan from scratch
1993 // Location of block in current frame
1994 int x_result = block_x1;
1995 int y_result = block_y1;
1997 // printf("MotionCVScan::scan_frame 1 %d %d %d %d %d %d %d %d\n",
1998 // block_x1 + block_w / 2,
1999 // block_y1 + block_h / 2,
2009 scan_x1 = x_result - scan_w / 2;
2010 scan_y1 = y_result - scan_h / 2;
2011 scan_x2 = x_result + scan_w / 2;
2012 scan_y2 = y_result + scan_h / 2;
2016 // Zero out requested values
2017 if(plugin->config.horizontal_only)
2020 scan_y2 = block_y1 + 1;
2022 if(plugin->config.vertical_only)
2025 scan_x2 = block_x1 + 1;
2028 // printf("MotionCVScan::scan_frame 1 %d %d %d %d %d %d %d %d\n",
2037 // Clamp the block coords before the scan so we get useful scan coords.
2038 MotionCVMain::clamp_scan(w,
2049 // 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",
2062 // Give up if invalid coords.
2063 if(scan_y2 <= scan_y1 ||
2064 scan_x2 <= scan_x1 ||
2065 block_x2 <= block_x1 ||
2066 block_y2 <= block_y1)
2069 // For subpixel, the top row and left column are skipped
2072 if(plugin->config.horizontal_only ||
2073 plugin->config.vertical_only)
2075 total_pixels = 4 * OVERSAMPLE * OVERSAMPLE - 4 * OVERSAMPLE;
2079 total_pixels = 4 * OVERSAMPLE;
2082 total_steps = total_pixels;
2084 set_package_count(total_steps);
2087 // Get least difference
2088 int64_t min_difference = -1;
2089 for(int i = 0; i < get_total_packages(); i++)
2091 MotionCVScanPackage *pkg = (MotionCVScanPackage*)get_package(i);
2092 if(pkg->difference1 < min_difference || min_difference == -1)
2094 min_difference = pkg->difference1;
2096 if(plugin->config.vertical_only)
2097 x_result = scan_x1 * OVERSAMPLE;
2099 x_result = scan_x1 * OVERSAMPLE +
2100 (pkg->pixel % (OVERSAMPLE * 2 - 1)) + 1;
2102 if(plugin->config.horizontal_only)
2103 y_result = scan_y1 * OVERSAMPLE;
2105 y_result = scan_y1 * OVERSAMPLE +
2106 (pkg->pixel / (OVERSAMPLE * 2 - 1)) + 1;
2110 dx_result = block_x1 * OVERSAMPLE - x_result;
2111 dy_result = block_y1 * OVERSAMPLE - y_result;
2114 if(pkg->difference2 < min_difference)
2116 min_difference = pkg->difference2;
2118 if(plugin->config.vertical_only)
2119 x_result = scan_x1 * OVERSAMPLE;
2121 x_result = scan_x2 * OVERSAMPLE -
2122 ((pkg->pixel % (OVERSAMPLE * 2 - 1)) + 1);
2124 if(plugin->config.horizontal_only)
2125 y_result = scan_y1 * OVERSAMPLE;
2127 y_result = scan_y2 * OVERSAMPLE -
2128 ((pkg->pixel / (OVERSAMPLE * 2 - 1)) + 1);
2130 dx_result = block_x1 * OVERSAMPLE - x_result;
2131 dy_result = block_y1 * OVERSAMPLE - y_result;
2135 //printf("MotionCVScan::scan_frame 1 %d %d %d %d\n", block_x1, block_y1, x_result, y_result);
2140 total_pixels = (scan_x2 - scan_x1) * (scan_y2 - scan_y1);
2141 total_steps = MIN(plugin->config.global_positions, total_pixels);
2143 set_package_count(total_steps);
2146 // Get least difference
2147 int64_t min_difference = -1;
2148 for(int i = 0; i < get_total_packages(); i++)
2150 MotionCVScanPackage *pkg = (MotionCVScanPackage*)get_package(i);
2151 if(pkg->difference1 < min_difference || min_difference == -1)
2153 min_difference = pkg->difference1;
2154 x_result = scan_x1 + (pkg->pixel % (scan_x2 - scan_x1));
2155 y_result = scan_y1 + (pkg->pixel / (scan_x2 - scan_x1));
2156 x_result *= OVERSAMPLE;
2157 y_result *= OVERSAMPLE;
2161 // printf("MotionCVScan::scan_frame 10 total_steps=%d total_pixels=%d subpixel=%d\n",
2166 // printf(" scan w=%d h=%d scan x1=%d y1=%d x2=%d y2=%d\n",
2174 // printf("MotionCVScan::scan_frame 2 block x1=%d y1=%d x2=%d y2=%d result x=%.2f y=%.2f\n",
2179 // (float)x_result / 4,
2180 // (float)y_result / 4);
2183 // If a new search is required, rescale results back to pixels.
2184 if(total_steps >= total_pixels)
2186 // Single pixel accuracy reached. Now do exhaustive subpixel search.
2187 if(plugin->config.mode1 == MotionCVConfig::STABILIZE ||
2188 plugin->config.mode1 == MotionCVConfig::TRACK ||
2189 plugin->config.mode1 == MotionCVConfig::NOTHING)
2191 x_result /= OVERSAMPLE;
2192 y_result /= OVERSAMPLE;
2199 // Fill in results and quit
2200 dx_result = block_x1 * OVERSAMPLE - x_result;
2201 dy_result = block_y1 * OVERSAMPLE - y_result;
2206 // Reduce scan area and try again
2208 scan_w = (scan_x2 - scan_x1) / 2;
2209 scan_h = (scan_y2 - scan_y1) / 2;
2210 x_result /= OVERSAMPLE;
2211 y_result /= OVERSAMPLE;
2216 // Add offsets from the "tracked single frame"
2217 dx_result = plugin->dx_offset - dx_result;
2218 dy_result = plugin->dy_offset - dy_result;
2220 if(plugin->config.mode2 == MotionCVConfig::SAVE)
2222 plugin->save_dx = dx_result;
2223 plugin->save_dy = dy_result;
2228 printf("MotionCVScan::scan_frame 10 dx=%.2f dy=%.2f\n",
2229 (float)this->dx_result / OVERSAMPLE,
2230 (float)this->dy_result / OVERSAMPLE);
2250 int64_t MotionCVScan::get_cache(int x, int y)
2252 int64_t result = -1;
2253 cache_lock->lock("MotionCVScan::get_cache");
2254 for(int i = 0; i < cache.total; i++)
2256 MotionCVScanCache *ptr = cache.values[i];
2257 if(ptr->x == x && ptr->y == y)
2259 result = ptr->difference;
2263 cache_lock->unlock();
2267 void MotionCVScan::put_cache(int x, int y, int64_t difference)
2269 MotionCVScanCache *ptr = new MotionCVScanCache(x, y, difference);
2270 cache_lock->lock("MotionCVScan::put_cache");
2272 cache_lock->unlock();
2279 MotionCVScanCache::MotionCVScanCache(int x, int y, int64_t difference)
2283 this->difference = difference;
2299 RotateCVScanPackage::RotateCVScanPackage()
2304 RotateCVScanUnit::RotateCVScanUnit(RotateCVScan *server, MotionCVMain *plugin)
2305 : LoadClient(server)
2307 this->server = server;
2308 this->plugin = plugin;
2313 RotateCVScanUnit::~RotateCVScanUnit()
2319 void RotateCVScanUnit::process_package(LoadPackage *package)
2321 if(server->skip) return;
2322 RotateCVScanPackage *pkg = (RotateCVScanPackage*)package;
2324 if((pkg->difference = server->get_cache(pkg->angle)) < 0)
2326 //printf("RotateCVScanUnit::process_package 1\n");
2327 int color_model = server->previous_frame->get_color_model();
2328 int pixel_size = BC_CModels::calculate_pixelsize(color_model);
2329 int row_bytes = server->previous_frame->get_bytes_per_line();
2332 rotater = new AffineEngine(1, 1);
2333 if(!temp) temp = new VFrame(
2334 server->previous_frame->get_w(),
2335 server->previous_frame->get_h(),
2339 // RotateCV original block size
2340 rotater->set_viewport(server->block_x1,
2342 server->block_x2 - server->block_x1,
2343 server->block_y2 - server->block_y1);
2344 rotater->set_pivot(server->block_x, server->block_y);
2346 rotater->rotate(temp,
2347 server->previous_frame,
2349 // Clamp coordinates
2350 int x1 = server->scan_x;
2351 int y1 = server->scan_y;
2352 int x2 = x1 + server->scan_w;
2353 int y2 = y1 + server->scan_h;
2354 x2 = MIN(temp->get_w(), x2);
2355 y2 = MIN(temp->get_h(), y2);
2356 x2 = MIN(server->current_frame->get_w(), x2);
2357 y2 = MIN(server->current_frame->get_h(), y2);
2361 if(x2 > x1 && y2 > y1)
2363 pkg->difference = plugin->abs_diff(
2364 temp->get_rows()[y1] + x1 * pixel_size,
2365 server->current_frame->get_rows()[y1] + x1 * pixel_size,
2370 //printf("RotateCVScanUnit::process_package %d\n", __LINE__);
2371 server->put_cache(pkg->angle, pkg->difference);
2374 // 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",
2375 // server->block_x1,
2376 // server->block_y1,
2377 // server->block_x2 - server->block_x1,
2378 // server->block_y2 - server->block_y1,
2384 // pkg->difference);
2409 RotateCVScan::RotateCVScan(MotionCVMain *plugin,
2414 total_clients, total_packages
2417 this->plugin = plugin;
2418 cache_lock = new Mutex("RotateCVScan::cache_lock");
2422 RotateCVScan::~RotateCVScan()
2427 void RotateCVScan::init_packages()
2429 for(int i = 0; i < get_total_packages(); i++)
2431 RotateCVScanPackage *pkg = (RotateCVScanPackage*)get_package(i);
2433 (scan_angle2 - scan_angle1) /
2439 LoadClient* RotateCVScan::new_client()
2441 return new RotateCVScanUnit(this, plugin);
2444 LoadPackage* RotateCVScan::new_package()
2446 return new RotateCVScanPackage;
2450 float RotateCVScan::scan_frame(VFrame *previous_frame,
2451 VFrame *current_frame,
2456 this->block_x = block_x;
2457 this->block_y = block_y;
2459 switch(plugin->config.mode2)
2461 case MotionCVConfig::NO_CALCULATE:
2466 case MotionCVConfig::LOAD:
2468 if( plugin->load_ok ) {
2469 result = plugin->load_dt;
2477 this->previous_frame = previous_frame;
2478 this->current_frame = current_frame;
2479 int w = current_frame->get_w();
2480 int h = current_frame->get_h();
2481 int block_w = w * plugin->config.rotation_block_w / 100;
2482 int block_h = h * plugin->config.rotation_block_h / 100;
2484 if(this->block_x - block_w / 2 < 0) block_w = this->block_x * 2;
2485 if(this->block_y - block_h / 2 < 0) block_h = this->block_y * 2;
2486 if(this->block_x + block_w / 2 > w) block_w = (w - this->block_x) * 2;
2487 if(this->block_y + block_h / 2 > h) block_h = (h - this->block_y) * 2;
2489 block_x1 = this->block_x - block_w / 2;
2490 block_x2 = this->block_x + block_w / 2;
2491 block_y1 = this->block_y - block_h / 2;
2492 block_y2 = this->block_y + block_h / 2;
2495 // Calculate the maximum area available to scan after rotation.
2496 // Must be calculated from the starting range because of cache.
2497 // Get coords of rectangle after rotation.
2498 double center_x = this->block_x;
2499 double center_y = this->block_y;
2500 double max_angle = plugin->config.rotation_range;
2501 double base_angle1 = atan((float)block_h / block_w);
2502 double base_angle2 = atan((float)block_w / block_h);
2503 double target_angle1 = base_angle1 + max_angle * 2 * M_PI / 360;
2504 double target_angle2 = base_angle2 + max_angle * 2 * M_PI / 360;
2505 double radius = sqrt(block_w * block_w + block_h * block_h) / 2;
2506 double x1 = center_x - cos(target_angle1) * radius;
2507 double y1 = center_y - sin(target_angle1) * radius;
2508 double x2 = center_x + sin(target_angle2) * radius;
2509 double y2 = center_y - cos(target_angle2) * radius;
2510 double x3 = center_x - sin(target_angle2) * radius;
2511 double y3 = center_y + cos(target_angle2) * radius;
2513 // Track top edge to find greatest area.
2514 double max_area1 = 0;
2515 //double max_x1 = 0;
2517 for(double x = x1; x < x2; x++)
2519 double y = y1 + (y2 - y1) * (x - x1) / (x2 - x1);
2520 if(x >= center_x && x < block_x2 && y >= block_y1 && y < center_y)
2522 double area = fabs(x - center_x) * fabs(y - center_y);
2523 if(area > max_area1)
2532 // Track left edge to find greatest area.
2533 double max_area2 = 0;
2535 //double max_y2 = 0;
2536 for(double y = y1; y < y3; y++)
2538 double x = x1 + (x3 - x1) * (y - y1) / (y3 - y1);
2539 if(x >= block_x1 && x < center_x && y >= block_y1 && y < center_y)
2541 double area = fabs(x - center_x) * fabs(y - center_y);
2542 if(area > max_area2)
2551 double max_x, max_y;
2555 // Get reduced scan coords
2556 scan_w = (int)(fabs(max_x - center_x) * 2);
2557 scan_h = (int)(fabs(max_y - center_y) * 2);
2558 scan_x = (int)(center_x - scan_w / 2);
2559 scan_y = (int)(center_y - scan_h / 2);
2560 // printf("RotateCVScan::scan_frame center=%d,%d scan=%d,%d %dx%d\n",
2561 // this->block_x, this->block_y, scan_x, scan_y, scan_w, scan_h);
2562 // printf(" angle_range=%f block= %d,%d,%d,%d\n", max_angle, block_x1, block_y1, block_x2, block_y2);
2564 // Determine min angle from size of block
2565 double angle1 = atan((double)block_h / block_w);
2566 double angle2 = atan((double)(block_h - 1) / (block_w + 1));
2567 double min_angle = fabs(angle2 - angle1) / OVERSAMPLE;
2568 min_angle = MAX(min_angle, MIN_ANGLE);
2571 printf("RotateCVScan::scan_frame min_angle=%f\n", min_angle * 360 / 2 / M_PI);
2574 cache.remove_all_objects();
2577 // Initial search range
2578 float angle_range = (float)plugin->config.rotation_range;
2580 total_steps = plugin->config.rotate_positions;
2583 while(angle_range >= min_angle * total_steps)
2585 scan_angle1 = result - angle_range;
2586 scan_angle2 = result + angle_range;
2589 set_package_count(total_steps);
2590 //set_package_count(1);
2593 int64_t min_difference = -1;
2594 for(int i = 0; i < get_total_packages(); i++)
2596 RotateCVScanPackage *pkg = (RotateCVScanPackage*)get_package(i);
2597 if(pkg->difference < min_difference || min_difference == -1)
2599 min_difference = pkg->difference;
2600 result = pkg->angle;
2610 if(plugin->config.mode2 == MotionCVConfig::SAVE) {
2611 plugin->save_dt = result;
2616 printf("RotateCVScan::scan_frame 10 angle=%f\n", result);
2622 int64_t RotateCVScan::get_cache(float angle)
2624 int64_t result = -1;
2625 cache_lock->lock("RotateCVScan::get_cache");
2626 for(int i = 0; i < cache.total; i++)
2628 RotateCVScanCache *ptr = cache.values[i];
2629 if(fabs(ptr->angle - angle) <= MIN_ANGLE)
2631 result = ptr->difference;
2635 cache_lock->unlock();
2639 void RotateCVScan::put_cache(float angle, int64_t difference)
2641 RotateCVScanCache *ptr = new RotateCVScanCache(angle, difference);
2642 cache_lock->lock("RotateCVScan::put_cache");
2644 cache_lock->unlock();
2655 RotateCVScanCache::RotateCVScanCache(float angle, int64_t difference)
2657 this->angle = angle;
2658 this->difference = difference;