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"
31 #include "motionscan.h"
32 #include "motionwindow.h"
34 #include "overlayframe.h"
35 #include "rotateframe.h"
36 #include "transportque.h"
42 REGISTER_PLUGIN(MotionMain)
52 MotionConfig::MotionConfig()
59 global_block_w = MIN_BLOCK;
60 global_block_h = MIN_BLOCK;
61 rotation_block_w = MIN_BLOCK;
62 rotation_block_h = MIN_BLOCK;
65 global_positions = 256;
68 rotate_magnitude = 90;
70 rotate_return_speed = 0;
71 action_type = MotionScan::STABILIZE;
74 tracking_type = MotionScan::NO_CALCULATE;
76 tracking_object = MotionScan::TRACK_SINGLE;
83 void MotionConfig::boundaries()
85 CLAMP(global_range_w, MIN_RADIUS, MAX_RADIUS);
86 CLAMP(global_range_h, MIN_RADIUS, MAX_RADIUS);
87 CLAMP(rotation_range, MIN_ROTATION, MAX_ROTATION);
88 CLAMP(rotation_center, -MAX_ROTATION, MAX_ROTATION);
89 CLAMP(block_count, MIN_BLOCKS, MAX_BLOCKS);
90 CLAMP(global_block_w, MIN_BLOCK, MAX_BLOCK);
91 CLAMP(global_block_h, MIN_BLOCK, MAX_BLOCK);
92 CLAMP(rotation_block_w, MIN_BLOCK, MAX_BLOCK);
93 CLAMP(rotation_block_h, MIN_BLOCK, MAX_BLOCK);
96 int MotionConfig::equivalent(MotionConfig &that)
98 return global_range_w == that.global_range_w &&
99 global_range_h == that.global_range_h &&
100 rotation_range == that.rotation_range &&
101 rotation_center == that.rotation_center &&
102 action_type == that.action_type &&
103 global == that.global &&
104 rotate == that.rotate &&
105 draw_vectors == that.draw_vectors &&
106 block_count == that.block_count &&
107 global_block_w == that.global_block_w &&
108 global_block_h == that.global_block_h &&
109 rotation_block_w == that.rotation_block_w &&
110 rotation_block_h == that.rotation_block_h &&
111 EQUIV(block_x, that.block_x) &&
112 EQUIV(block_y, that.block_y) &&
113 global_positions == that.global_positions &&
114 rotate_positions == that.rotate_positions &&
115 magnitude == that.magnitude &&
116 return_speed == that.return_speed &&
117 rotate_return_speed == that.rotate_return_speed &&
118 rotate_magnitude == that.rotate_magnitude &&
119 tracking_object == that.tracking_object &&
120 track_frame == that.track_frame &&
121 bottom_is_master == that.bottom_is_master &&
122 horizontal_only == that.horizontal_only &&
123 vertical_only == that.vertical_only;
126 void MotionConfig::copy_from(MotionConfig &that)
128 global_range_w = that.global_range_w;
129 global_range_h = that.global_range_h;
130 rotation_range = that.rotation_range;
131 rotation_center = that.rotation_center;
132 action_type = that.action_type;
133 global = that.global;
134 rotate = that.rotate;
135 tracking_type = that.tracking_type;
136 draw_vectors = that.draw_vectors;
137 block_count = that.block_count;
138 block_x = that.block_x;
139 block_y = that.block_y;
140 global_positions = that.global_positions;
141 rotate_positions = that.rotate_positions;
142 global_block_w = that.global_block_w;
143 global_block_h = that.global_block_h;
144 rotation_block_w = that.rotation_block_w;
145 rotation_block_h = that.rotation_block_h;
146 magnitude = that.magnitude;
147 return_speed = that.return_speed;
148 rotate_magnitude = that.rotate_magnitude;
149 rotate_return_speed = that.rotate_return_speed;
150 tracking_object = that.tracking_object;
151 track_frame = that.track_frame;
152 bottom_is_master = that.bottom_is_master;
153 horizontal_only = that.horizontal_only;
154 vertical_only = that.vertical_only;
157 void MotionConfig::interpolate(MotionConfig &prev,
161 int64_t current_frame)
163 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
164 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
165 this->block_x = prev.block_x;
166 this->block_y = prev.block_y;
167 global_range_w = prev.global_range_w;
168 global_range_h = prev.global_range_h;
169 rotation_range = prev.rotation_range;
170 rotation_center = prev.rotation_center;
171 action_type = prev.action_type;
172 global = prev.global;
173 rotate = prev.rotate;
174 tracking_type = prev.tracking_type;
175 draw_vectors = prev.draw_vectors;
176 block_count = prev.block_count;
177 global_positions = prev.global_positions;
178 rotate_positions = prev.rotate_positions;
179 global_block_w = prev.global_block_w;
180 global_block_h = prev.global_block_h;
181 rotation_block_w = prev.rotation_block_w;
182 rotation_block_h = prev.rotation_block_h;
183 magnitude = prev.magnitude;
184 return_speed = prev.return_speed;
185 rotate_magnitude = prev.rotate_magnitude;
186 rotate_return_speed = prev.rotate_return_speed;
187 tracking_object = prev.tracking_object;
188 track_frame = prev.track_frame;
189 bottom_is_master = prev.bottom_is_master;
190 horizontal_only = prev.horizontal_only;
191 vertical_only = prev.vertical_only;
212 MotionMain::MotionMain(PluginServer *server)
213 : PluginVClient(server)
225 previous_frame_number = -1;
228 current_global_ref = 0;
229 global_target_src = 0;
230 global_target_dst = 0;
233 current_rotate_ref = 0;
234 rotate_target_src = 0;
235 rotate_target_dst = 0;
238 MotionMain::~MotionMain()
243 delete [] search_area;
245 delete rotate_engine;
246 delete motion_rotate;
249 delete prev_global_ref;
250 delete current_global_ref;
251 delete global_target_src;
252 delete global_target_dst;
254 delete prev_rotate_ref;
255 delete current_rotate_ref;
256 delete rotate_target_src;
257 delete rotate_target_dst;
260 const char* MotionMain::plugin_title() { return _("Motion"); }
261 int MotionMain::is_realtime() { return 1; }
262 int MotionMain::is_multichannel() { return 1; }
265 NEW_WINDOW_MACRO(MotionMain, MotionWindow)
267 LOAD_CONFIGURATION_MACRO(MotionMain, MotionConfig)
271 void MotionMain::update_gui()
275 if(load_configuration())
277 thread->window->lock_window("MotionMain::update_gui");
279 char string[BCTEXTLEN];
280 sprintf(string, "%d", config.global_positions);
281 ((MotionWindow*)thread->window)->global_search_positions->set_text(string);
282 sprintf(string, "%d", config.rotate_positions);
283 ((MotionWindow*)thread->window)->rotation_search_positions->set_text(string);
285 ((MotionWindow*)thread->window)->global_block_w->update(config.global_block_w);
286 ((MotionWindow*)thread->window)->global_block_h->update(config.global_block_h);
287 ((MotionWindow*)thread->window)->rotation_block_w->update(config.rotation_block_w);
288 ((MotionWindow*)thread->window)->rotation_block_h->update(config.rotation_block_h);
289 ((MotionWindow*)thread->window)->block_x->update(config.block_x);
290 ((MotionWindow*)thread->window)->block_y->update(config.block_y);
291 ((MotionWindow*)thread->window)->block_x_text->update((float)config.block_x);
292 ((MotionWindow*)thread->window)->block_y_text->update((float)config.block_y);
293 ((MotionWindow*)thread->window)->magnitude->update(config.magnitude);
294 ((MotionWindow*)thread->window)->return_speed->update(config.return_speed);
295 ((MotionWindow*)thread->window)->rotate_magnitude->update(config.rotate_magnitude);
296 ((MotionWindow*)thread->window)->rotate_return_speed->update(config.rotate_return_speed);
297 ((MotionWindow*)thread->window)->rotation_range->update(config.rotation_range);
298 ((MotionWindow*)thread->window)->rotation_center->update(config.rotation_center);
301 ((MotionWindow*)thread->window)->track_single->update(config.tracking_object == MotionScan::TRACK_SINGLE);
302 ((MotionWindow*)thread->window)->track_frame_number->update(config.track_frame);
303 ((MotionWindow*)thread->window)->track_previous->update(config.tracking_object == MotionScan::TRACK_PREVIOUS);
304 ((MotionWindow*)thread->window)->previous_same->update(config.tracking_object == MotionScan::PREVIOUS_SAME_BLOCK);
305 if(config.tracking_object != MotionScan::TRACK_SINGLE)
306 ((MotionWindow*)thread->window)->track_frame_number->disable();
308 ((MotionWindow*)thread->window)->track_frame_number->enable();
310 ((MotionWindow*)thread->window)->action_type->set_text(
311 ActionType::to_text(config.action_type));
312 ((MotionWindow*)thread->window)->tracking_type->set_text(
313 TrackingType::to_text(config.tracking_type));
314 ((MotionWindow*)thread->window)->track_direction->set_text(
315 TrackDirection::to_text(config.horizontal_only, config.vertical_only));
316 ((MotionWindow*)thread->window)->master_layer->set_text(
317 MasterLayer::to_text(config.bottom_is_master));
320 ((MotionWindow*)thread->window)->update_mode();
321 thread->window->unlock_window();
329 void MotionMain::save_data(KeyFrame *keyframe)
333 // cause data to be stored directly in text
334 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
335 output.tag.set_title("MOTION");
337 output.tag.set_property("BLOCK_COUNT", config.block_count);
338 output.tag.set_property("GLOBAL_POSITIONS", config.global_positions);
339 output.tag.set_property("ROTATE_POSITIONS", config.rotate_positions);
340 output.tag.set_property("GLOBAL_BLOCK_W", config.global_block_w);
341 output.tag.set_property("GLOBAL_BLOCK_H", config.global_block_h);
342 output.tag.set_property("ROTATION_BLOCK_W", config.rotation_block_w);
343 output.tag.set_property("ROTATION_BLOCK_H", config.rotation_block_h);
344 output.tag.set_property("BLOCK_X", config.block_x);
345 output.tag.set_property("BLOCK_Y", config.block_y);
346 output.tag.set_property("GLOBAL_RANGE_W", config.global_range_w);
347 output.tag.set_property("GLOBAL_RANGE_H", config.global_range_h);
348 output.tag.set_property("ROTATION_RANGE", config.rotation_range);
349 output.tag.set_property("ROTATION_CENTER", config.rotation_center);
350 output.tag.set_property("MAGNITUDE", config.magnitude);
351 output.tag.set_property("RETURN_SPEED", config.return_speed);
352 output.tag.set_property("ROTATE_MAGNITUDE", config.rotate_magnitude);
353 output.tag.set_property("ROTATE_RETURN_SPEED", config.rotate_return_speed);
354 output.tag.set_property("ACTION_TYPE", config.action_type);
355 output.tag.set_property("GLOBAL", config.global);
356 output.tag.set_property("ROTATE", config.rotate);
357 output.tag.set_property("TRACKING_TYPE", config.tracking_type);
358 output.tag.set_property("DRAW_VECTORS", config.draw_vectors);
359 output.tag.set_property("TRACKING_OBJECT", config.tracking_object);
360 output.tag.set_property("TRACK_FRAME", config.track_frame);
361 output.tag.set_property("BOTTOM_IS_MASTER", config.bottom_is_master);
362 output.tag.set_property("HORIZONTAL_ONLY", config.horizontal_only);
363 output.tag.set_property("VERTICAL_ONLY", config.vertical_only);
365 output.tag.set_title("/MOTION");
367 output.append_newline();
368 output.terminate_string();
371 void MotionMain::read_data(KeyFrame *keyframe)
375 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
381 result = input.read_tag();
385 if(input.tag.title_is("MOTION"))
387 config.block_count = input.tag.get_property("BLOCK_COUNT", config.block_count);
388 config.global_positions = input.tag.get_property("GLOBAL_POSITIONS", config.global_positions);
389 config.rotate_positions = input.tag.get_property("ROTATE_POSITIONS", config.rotate_positions);
390 config.global_block_w = input.tag.get_property("GLOBAL_BLOCK_W", config.global_block_w);
391 config.global_block_h = input.tag.get_property("GLOBAL_BLOCK_H", config.global_block_h);
392 config.rotation_block_w = input.tag.get_property("ROTATION_BLOCK_W", config.rotation_block_w);
393 config.rotation_block_h = input.tag.get_property("ROTATION_BLOCK_H", config.rotation_block_h);
394 config.block_x = input.tag.get_property("BLOCK_X", config.block_x);
395 config.block_y = input.tag.get_property("BLOCK_Y", config.block_y);
396 config.global_range_w = input.tag.get_property("GLOBAL_RANGE_W", config.global_range_w);
397 config.global_range_h = input.tag.get_property("GLOBAL_RANGE_H", config.global_range_h);
398 config.rotation_range = input.tag.get_property("ROTATION_RANGE", config.rotation_range);
399 config.rotation_center = input.tag.get_property("ROTATION_CENTER", config.rotation_center);
400 config.magnitude = input.tag.get_property("MAGNITUDE", config.magnitude);
401 config.return_speed = input.tag.get_property("RETURN_SPEED", config.return_speed);
402 config.rotate_magnitude = input.tag.get_property("ROTATE_MAGNITUDE", config.rotate_magnitude);
403 config.rotate_return_speed = input.tag.get_property("ROTATE_RETURN_SPEED", config.rotate_return_speed);
404 config.action_type = input.tag.get_property("ACTION_TYPE", config.action_type);
405 config.global = input.tag.get_property("GLOBAL", config.global);
406 config.rotate = input.tag.get_property("ROTATE", config.rotate);
407 config.tracking_type = input.tag.get_property("TRACKING_TYPE", config.tracking_type);
408 config.draw_vectors = input.tag.get_property("DRAW_VECTORS", config.draw_vectors);
409 config.tracking_object = input.tag.get_property("TRACKING_OBJECT", config.tracking_object);
410 config.track_frame = input.tag.get_property("TRACK_FRAME", config.track_frame);
411 config.bottom_is_master = input.tag.get_property("BOTTOM_IS_MASTER", config.bottom_is_master);
412 config.horizontal_only = input.tag.get_property("HORIZONTAL_ONLY", config.horizontal_only);
413 config.vertical_only = input.tag.get_property("VERTICAL_ONLY", config.vertical_only);
428 void MotionMain::allocate_temp(int w, int h, int color_model)
431 (temp_frame->get_w() != w ||
432 temp_frame->get_h() != h))
438 temp_frame = new VFrame(w, h, color_model);
442 void MotionMain::process_global()
445 if(!engine) engine = new MotionScan(PluginClient::get_project_smp() + 1,
446 PluginClient::get_project_smp() + 1);
448 // Determine if frames changed
449 engine->scan_frame(current_global_ref,
451 config.global_range_w,
452 config.global_range_h,
453 config.global_block_w,
454 config.global_block_h,
457 config.tracking_object,
458 config.tracking_type,
460 config.horizontal_only,
461 config.vertical_only,
462 get_source_position(),
463 config.global_positions,
468 current_dx = engine->dx_result;
469 current_dy = engine->dy_result;
471 // Add current motion vector to accumulation vector.
472 if(config.tracking_object != MotionScan::TRACK_SINGLE)
475 total_dx = (int64_t)total_dx * (100 - config.return_speed) / 100;
476 total_dy = (int64_t)total_dy * (100 - config.return_speed) / 100;
477 total_dx += engine->dx_result;
478 total_dy += engine->dy_result;
479 // printf("MotionMain::process_global total_dx=%d engine->dx_result=%d\n",
481 // engine->dx_result);
484 // Make accumulation vector current
486 total_dx = engine->dx_result;
487 total_dy = engine->dy_result;
490 // Clamp accumulation vector
491 if(config.magnitude < 100)
493 int block_w = (int64_t)config.global_block_w *
494 current_global_ref->get_w() / 100;
495 int block_h = (int64_t)config.global_block_h *
496 current_global_ref->get_h() / 100;
497 int block_x_orig = (int64_t)(config.block_x *
498 current_global_ref->get_w() /
500 int block_y_orig = (int64_t)(config.block_y *
501 current_global_ref->get_h() /
504 int max_block_x = (int64_t)(current_global_ref->get_w() - block_x_orig) *
508 int max_block_y = (int64_t)(current_global_ref->get_h() - block_y_orig) *
512 int min_block_x = (int64_t)-block_x_orig *
516 int min_block_y = (int64_t)-block_y_orig *
521 CLAMP(total_dx, min_block_x, max_block_x);
522 CLAMP(total_dy, min_block_y, max_block_y);
526 printf("MotionMain::process_global 2 total_dx=%.02f total_dy=%.02f\n",
527 (float)total_dx / OVERSAMPLE,
528 (float)total_dy / OVERSAMPLE);
531 if(config.tracking_object != MotionScan::TRACK_SINGLE && !config.rotate)
533 // Transfer current reference frame to previous reference frame and update
534 // counter. Must wait for rotate to compare.
535 prev_global_ref->copy_from(current_global_ref);
536 previous_frame_number = get_source_position();
539 // Decide what to do with target based on requested operation
543 switch(config.action_type)
545 case MotionScan::NOTHING:
546 global_target_dst->copy_from(global_target_src);
548 case MotionScan::TRACK_PIXEL:
549 interpolation = NEAREST_NEIGHBOR;
550 dx = (int)(total_dx / OVERSAMPLE);
551 dy = (int)(total_dy / OVERSAMPLE);
553 case MotionScan::STABILIZE_PIXEL:
554 interpolation = NEAREST_NEIGHBOR;
555 dx = -(int)(total_dx / OVERSAMPLE);
556 dy = -(int)(total_dy / OVERSAMPLE);
559 case MotionScan::TRACK:
560 interpolation = CUBIC_LINEAR;
561 dx = (float)total_dx / OVERSAMPLE;
562 dy = (float)total_dy / OVERSAMPLE;
564 case MotionScan::STABILIZE:
565 interpolation = CUBIC_LINEAR;
566 dx = -(float)total_dx / OVERSAMPLE;
567 dy = -(float)total_dy / OVERSAMPLE;
572 if(config.action_type != MotionScan::NOTHING)
575 overlayer = new OverlayFrame(PluginClient::get_project_smp() + 1);
576 global_target_dst->clear_frame();
577 overlayer->overlay(global_target_dst,
581 global_target_src->get_w(),
582 global_target_src->get_h(),
585 (float)global_target_src->get_w() + dx,
586 (float)global_target_src->get_h() + dy,
595 void MotionMain::process_rotation()
600 // Convert the previous global reference into the previous rotation reference.
601 // Convert global target destination into rotation target source.
605 overlayer = new OverlayFrame(PluginClient::get_project_smp() + 1);
608 if(config.tracking_object == MotionScan::TRACK_SINGLE)
610 dx = (float)total_dx / OVERSAMPLE;
611 dy = (float)total_dy / OVERSAMPLE;
615 dx = (float)current_dx / OVERSAMPLE;
616 dy = (float)current_dy / OVERSAMPLE;
619 prev_rotate_ref->clear_frame();
620 overlayer->overlay(prev_rotate_ref,
624 prev_global_ref->get_w(),
625 prev_global_ref->get_h(),
628 (float)prev_global_ref->get_w() + dx,
629 (float)prev_global_ref->get_h() + dy,
633 // Pivot is destination global position
634 block_x = (int)(prev_rotate_ref->get_w() *
639 block_y = (int)(prev_rotate_ref->get_h() *
644 // Use the global target output as the rotation target input
645 rotate_target_src->copy_from(global_target_dst);
646 // Transfer current reference frame to previous reference frame for global.
647 if(config.tracking_object != MotionScan::TRACK_SINGLE)
649 prev_global_ref->copy_from(current_global_ref);
650 previous_frame_number = get_source_position();
656 block_x = (int)(prev_rotate_ref->get_w() *
659 block_y = (int)(prev_rotate_ref->get_h() *
668 motion_rotate = new RotateScan(this,
669 get_project_smp() + 1,
670 get_project_smp() + 1);
672 current_angle = motion_rotate->scan_frame(prev_rotate_ref,
679 // Add current rotation to accumulation
680 if(config.tracking_object != MotionScan::TRACK_SINGLE)
683 total_angle = total_angle * (100 - config.rotate_return_speed) / 100;
684 // Accumulate current rotation
685 total_angle += current_angle;
687 // Clamp rotation accumulation
688 if(config.rotate_magnitude < 90)
690 CLAMP(total_angle, -config.rotate_magnitude, config.rotate_magnitude);
695 // Transfer current reference frame to previous reference frame and update
697 prev_rotate_ref->copy_from(current_rotate_ref);
698 previous_frame_number = get_source_position();
703 total_angle = current_angle;
707 printf("MotionMain::process_rotation total_angle=%f\n", total_angle);
711 // Calculate rotation parameters based on requested operation
713 switch(config.action_type)
715 case MotionScan::NOTHING:
716 rotate_target_dst->copy_from(rotate_target_src);
718 case MotionScan::TRACK:
719 case MotionScan::TRACK_PIXEL:
722 case MotionScan::STABILIZE:
723 case MotionScan::STABILIZE_PIXEL:
724 angle = -total_angle;
730 if(config.action_type != MotionScan::NOTHING)
733 rotate_engine = new AffineEngine(PluginClient::get_project_smp() + 1,
734 PluginClient::get_project_smp() + 1);
736 rotate_target_dst->clear_frame();
738 // Determine pivot based on a number of factors.
739 switch(config.action_type)
741 case MotionScan::TRACK:
742 case MotionScan::TRACK_PIXEL:
743 // Use destination of global tracking.
744 // rotate_engine->set_pivot(block_x, block_y);
745 rotate_engine->set_in_pivot(block_x, block_y);
746 rotate_engine->set_out_pivot(block_x, block_y);
749 case MotionScan::STABILIZE:
750 case MotionScan::STABILIZE_PIXEL:
753 // Use origin of global stabilize operation
754 // rotate_engine->set_pivot((int)(rotate_target_dst->get_w() *
757 // (int)(rotate_target_dst->get_h() *
760 rotate_engine->set_in_pivot((int)(rotate_target_dst->get_w() *
763 (int)(rotate_target_dst->get_h() *
766 rotate_engine->set_out_pivot((int)(rotate_target_dst->get_w() *
769 (int)(rotate_target_dst->get_h() *
777 // rotate_engine->set_pivot(block_x, block_y);
778 rotate_engine->set_in_pivot(block_x, block_y);
779 rotate_engine->set_out_pivot(block_x, block_y);
785 rotate_engine->rotate(rotate_target_dst, rotate_target_src, angle);
786 // overlayer->overlay(rotate_target_dst,
790 // prev_rotate_ref->get_w(),
791 // prev_rotate_ref->get_h(),
794 // prev_rotate_ref->get_w(),
795 // prev_rotate_ref->get_h(),
799 // overlayer->overlay(rotate_target_dst,
800 // current_rotate_ref,
803 // prev_rotate_ref->get_w(),
804 // prev_rotate_ref->get_h(),
807 // prev_rotate_ref->get_w(),
808 // prev_rotate_ref->get_h(),
827 int MotionMain::process_buffer(VFrame **frame,
828 int64_t start_position,
831 int need_reconfigure = load_configuration();
832 int color_model = frame[0]->get_color_model();
833 w = frame[0]->get_w();
834 h = frame[0]->get_h();
838 printf("MotionMain::process_buffer %d start_position=%lld\n", __LINE__, start_position);
842 // Calculate the source and destination pointers for each of the operations.
843 // Get the layer to track motion in.
844 reference_layer = config.bottom_is_master ?
845 PluginClient::total_in_buffers - 1 :
847 // Get the layer to apply motion in.
848 target_layer = config.bottom_is_master ?
850 PluginClient::total_in_buffers - 1;
853 output_frame = frame[target_layer];
856 // Get the position of previous reference frame.
857 int64_t actual_previous_number;
858 // Skip if match frame not available
859 int skip_current = 0;
862 if(config.tracking_object == MotionScan::TRACK_SINGLE)
864 actual_previous_number = config.track_frame;
865 if(get_direction() == PLAY_REVERSE)
866 actual_previous_number++;
867 if(actual_previous_number == start_position)
872 actual_previous_number = start_position;
873 if(get_direction() == PLAY_FORWARD)
875 actual_previous_number--;
876 if(actual_previous_number < get_source_start())
880 KeyFrame *keyframe = get_prev_keyframe(start_position, 1);
881 if(keyframe->position > 0 &&
882 actual_previous_number < keyframe->position)
888 actual_previous_number++;
889 if(actual_previous_number >= get_source_start() + get_total_len())
893 KeyFrame *keyframe = get_next_keyframe(start_position, 1);
894 if(keyframe->position > 0 &&
895 actual_previous_number >= keyframe->position)
900 // Only count motion since last keyframe
906 if(!config.global && !config.rotate) skip_current = 1;
911 // printf("process_realtime %d %lld %lld\n",
913 // previous_frame_number,
914 // actual_previous_number);
915 // Load match frame and reset vectors
916 int need_reload = !skip_current &&
917 (previous_frame_number != actual_previous_number ||
924 previous_frame_number = actual_previous_number;
941 // Get the global pointers. Here we walk through the sequence of events.
944 // Assume global only. Global reads previous frame and compares
945 // with current frame to get the current translation.
946 // The center of the search area is fixed in compensate mode or
947 // the user value + the accumulation vector in track mode.
949 prev_global_ref = new VFrame(w, h, color_model);
950 if(!current_global_ref)
951 current_global_ref = new VFrame(w, h, color_model);
953 // Global loads the current target frame into the src and
954 // writes it to the dst frame with desired translation.
955 if(!global_target_src)
956 global_target_src = new VFrame(w, h, color_model);
957 if(!global_target_dst)
958 global_target_dst = new VFrame(w, h, color_model);
961 // Load the global frames
964 read_frame(prev_global_ref,
966 previous_frame_number,
970 read_frame(current_global_ref,
974 read_frame(global_target_src,
981 // Global followed by rotate
984 // Must translate the previous global reference by the current global
985 // accumulation vector to match the current global reference.
986 // The center of the search area is always the user value + the accumulation
989 prev_rotate_ref = new VFrame(w, h, color_model);
990 // The current global reference is the current rotation reference.
991 if(!current_rotate_ref)
992 current_rotate_ref = new VFrame(w, h, color_model);
993 current_rotate_ref->copy_from(current_global_ref);
995 // The global target destination is copied to the rotation target source
996 // then written to the rotation output with rotation.
997 // The pivot for the rotation is the center of the search area
998 // if we're tracking.
999 // The pivot is fixed to the user position if we're compensating.
1000 if(!rotate_target_src)
1001 rotate_target_src = new VFrame(w, h, color_model);
1002 if(!rotate_target_dst)
1003 rotate_target_dst = new VFrame(w, h, color_model);
1010 // Rotation reads the previous reference frame and compares it with current
1012 if(!prev_rotate_ref)
1013 prev_rotate_ref = new VFrame(w, h, color_model);
1014 if(!current_rotate_ref)
1015 current_rotate_ref = new VFrame(w, h, color_model);
1017 // Rotation loads target frame to temporary, rotates it, and writes it to the
1018 // target frame. The pivot is always fixed.
1019 if(!rotate_target_src)
1020 rotate_target_src = new VFrame(w, h, color_model);
1021 if(!rotate_target_dst)
1022 rotate_target_dst = new VFrame(w, h, color_model);
1025 // Load the rotate frames
1028 read_frame(prev_rotate_ref,
1030 previous_frame_number,
1033 read_frame(current_rotate_ref,
1037 read_frame(rotate_target_src,
1054 // Get position change from previous frame to current frame
1055 if(config.global) process_global();
1056 // Get rotation change from previous frame to current frame
1057 if(config.rotate) process_rotation();
1058 //frame[target_layer]->copy_from(prev_rotate_ref);
1059 //frame[target_layer]->copy_from(current_rotate_ref);
1067 // Transfer the relevant target frame to the output
1072 frame[target_layer]->copy_from(rotate_target_dst);
1076 frame[target_layer]->copy_from(global_target_dst);
1080 // Read the target destination directly
1082 read_frame(frame[target_layer],
1088 if(config.draw_vectors)
1090 draw_vectors(frame[target_layer]);
1094 printf("MotionMain::process_buffer %d\n", __LINE__);
1101 void MotionMain::draw_vectors(VFrame *frame)
1103 int w = frame->get_w();
1104 int h = frame->get_h();
1105 int global_x1, global_y1;
1106 int global_x2, global_y2;
1107 int block_x, block_y;
1108 int block_w, block_h;
1109 int block_x1, block_y1;
1110 int block_x2, block_y2;
1111 int block_x3, block_y3;
1112 int block_x4, block_y4;
1113 int search_w, search_h;
1114 int search_x1, search_y1;
1115 int search_x2, search_y2;
1116 int search_x3, search_y3;
1117 int search_x4, search_y4;
1123 // Start of vector is center of previous block.
1124 // End of vector is total accumulation.
1125 if(config.tracking_object == MotionScan::TRACK_SINGLE)
1127 global_x1 = (int64_t)(config.block_x *
1130 global_y1 = (int64_t)(config.block_y *
1133 global_x2 = global_x1 + total_dx / OVERSAMPLE;
1134 global_y2 = global_y1 + total_dy / OVERSAMPLE;
1135 //printf("MotionMain::draw_vectors %d %d %d %d %d %d\n", total_dx, total_dy, global_x1, global_y1, global_x2, global_y2);
1138 // Start of vector is center of previous block.
1139 // End of vector is current change.
1140 if(config.tracking_object == MotionScan::PREVIOUS_SAME_BLOCK)
1142 global_x1 = (int64_t)(config.block_x *
1145 global_y1 = (int64_t)(config.block_y *
1148 global_x2 = global_x1 + current_dx / OVERSAMPLE;
1149 global_y2 = global_y1 + current_dy / OVERSAMPLE;
1153 global_x1 = (int64_t)(config.block_x *
1156 (total_dx - current_dx) /
1158 global_y1 = (int64_t)(config.block_y *
1161 (total_dy - current_dy) /
1163 global_x2 = (int64_t)(config.block_x *
1168 global_y2 = (int64_t)(config.block_y *
1175 block_x = global_x1;
1176 block_y = global_y1;
1177 block_w = config.global_block_w * w / 100;
1178 block_h = config.global_block_h * h / 100;
1179 block_x1 = block_x - block_w / 2;
1180 block_y1 = block_y - block_h / 2;
1181 block_x2 = block_x + block_w / 2;
1182 block_y2 = block_y + block_h / 2;
1183 search_w = config.global_range_w * w / 100;
1184 search_h = config.global_range_h * h / 100;
1185 search_x1 = block_x1 - search_w / 2;
1186 search_y1 = block_y1 - search_h / 2;
1187 search_x2 = block_x2 + search_w / 2;
1188 search_y2 = block_y2 + search_h / 2;
1190 // printf("MotionMain::draw_vectors %d %d %d %d %d %d %d %d %d %d %d %d\n",
1204 MotionScan::clamp_scan(w,
1217 draw_arrow(frame, global_x1, global_y1, global_x2, global_y2);
1220 draw_line(frame, block_x1, block_y1, block_x2, block_y1);
1221 draw_line(frame, block_x2, block_y1, block_x2, block_y2);
1222 draw_line(frame, block_x2, block_y2, block_x1, block_y2);
1223 draw_line(frame, block_x1, block_y2, block_x1, block_y1);
1227 draw_line(frame, search_x1, search_y1, search_x2, search_y1);
1228 draw_line(frame, search_x2, search_y1, search_x2, search_y2);
1229 draw_line(frame, search_x2, search_y2, search_x1, search_y2);
1230 draw_line(frame, search_x1, search_y2, search_x1, search_y1);
1232 // Block should be endpoint of motion
1235 block_x = global_x2;
1236 block_y = global_y2;
1241 block_x = (int64_t)(config.block_x * w / 100);
1242 block_y = (int64_t)(config.block_y * h / 100);
1245 block_w = config.rotation_block_w * w / 100;
1246 block_h = config.rotation_block_h * h / 100;
1249 float angle = total_angle * 2 * M_PI / 360;
1250 double base_angle1 = atan((float)block_h / block_w);
1251 double base_angle2 = atan((float)block_w / block_h);
1252 double target_angle1 = base_angle1 + angle;
1253 double target_angle2 = base_angle2 + angle;
1254 double radius = sqrt(block_w * block_w + block_h * block_h) / 2;
1255 block_x1 = (int)(block_x - cos(target_angle1) * radius);
1256 block_y1 = (int)(block_y - sin(target_angle1) * radius);
1257 block_x2 = (int)(block_x + sin(target_angle2) * radius);
1258 block_y2 = (int)(block_y - cos(target_angle2) * radius);
1259 block_x3 = (int)(block_x - sin(target_angle2) * radius);
1260 block_y3 = (int)(block_y + cos(target_angle2) * radius);
1261 block_x4 = (int)(block_x + cos(target_angle1) * radius);
1262 block_y4 = (int)(block_y + sin(target_angle1) * radius);
1264 draw_line(frame, block_x1, block_y1, block_x2, block_y2);
1265 draw_line(frame, block_x2, block_y2, block_x4, block_y4);
1266 draw_line(frame, block_x4, block_y4, block_x3, block_y3);
1267 draw_line(frame, block_x3, block_y3, block_x1, block_y1);
1273 draw_line(frame, block_x, block_y - 5, block_x, block_y + 6);
1274 draw_line(frame, block_x - 5, block_y, block_x + 6, block_y);
1281 void MotionMain::draw_pixel(VFrame *frame, int x, int y)
1283 if(!(x >= 0 && y >= 0 && x < frame->get_w() && y < frame->get_h())) return;
1285 #define DRAW_PIXEL(x, y, components, do_yuv, max, type) \
1287 type **rows = (type**)frame->get_rows(); \
1288 rows[y][x * components] = max - rows[y][x * components]; \
1291 rows[y][x * components + 1] = max - rows[y][x * components + 1]; \
1292 rows[y][x * components + 2] = max - rows[y][x * components + 2]; \
1296 rows[y][x * components + 1] = (max / 2 + 1) - rows[y][x * components + 1]; \
1297 rows[y][x * components + 2] = (max / 2 + 1) - rows[y][x * components + 2]; \
1299 if(components == 4) \
1300 rows[y][x * components + 3] = max; \
1304 switch(frame->get_color_model())
1307 DRAW_PIXEL(x, y, 3, 0, 0xff, unsigned char);
1310 DRAW_PIXEL(x, y, 4, 0, 0xff, unsigned char);
1313 DRAW_PIXEL(x, y, 3, 0, 1.0, float);
1316 DRAW_PIXEL(x, y, 4, 0, 1.0, float);
1319 DRAW_PIXEL(x, y, 3, 1, 0xff, unsigned char);
1322 DRAW_PIXEL(x, y, 4, 1, 0xff, unsigned char);
1325 DRAW_PIXEL(x, y, 3, 0, 0xffff, uint16_t);
1328 DRAW_PIXEL(x, y, 3, 1, 0xffff, uint16_t);
1330 case BC_RGBA16161616:
1331 DRAW_PIXEL(x, y, 4, 0, 0xffff, uint16_t);
1333 case BC_YUVA16161616:
1334 DRAW_PIXEL(x, y, 4, 1, 0xffff, uint16_t);
1340 void MotionMain::draw_line(VFrame *frame, int x1, int y1, int x2, int y2)
1342 int w = labs(x2 - x1);
1343 int h = labs(y2 - y1);
1344 //printf("MotionMain::draw_line 1 %d %d %d %d\n", x1, y1, x2, y2);
1348 draw_pixel(frame, x1, y1);
1353 // Flip coordinates so x1 < x2
1363 int numerator = y2 - y1;
1364 int denominator = x2 - x1;
1365 for(int i = x1; i < x2; i++)
1367 int y = y1 + (int64_t)(i - x1) * (int64_t)numerator / (int64_t)denominator;
1368 draw_pixel(frame, i, y);
1373 // Flip coordinates so y1 < y2
1383 int numerator = x2 - x1;
1384 int denominator = y2 - y1;
1385 for(int i = y1; i < y2; i++)
1387 int x = x1 + (int64_t)(i - y1) * (int64_t)numerator / (int64_t)denominator;
1388 draw_pixel(frame, x, i);
1391 //printf("MotionMain::draw_line 2\n");
1394 #define ARROW_SIZE 10
1395 void MotionMain::draw_arrow(VFrame *frame, int x1, int y1, int x2, int y2)
1397 double angle = atan((float)(y2 - y1) / (float)(x2 - x1));
1398 double angle1 = angle + (float)145 / 360 * 2 * 3.14159265;
1399 double angle2 = angle - (float)145 / 360 * 2 * 3.14159265;
1406 x3 = x2 - (int)(ARROW_SIZE * cos(angle1));
1407 y3 = y2 - (int)(ARROW_SIZE * sin(angle1));
1408 x4 = x2 - (int)(ARROW_SIZE * cos(angle2));
1409 y4 = y2 - (int)(ARROW_SIZE * sin(angle2));
1413 x3 = x2 + (int)(ARROW_SIZE * cos(angle1));
1414 y3 = y2 + (int)(ARROW_SIZE * sin(angle1));
1415 x4 = x2 + (int)(ARROW_SIZE * cos(angle2));
1416 y4 = y2 + (int)(ARROW_SIZE * sin(angle2));
1420 draw_line(frame, x1, y1, x2, y2);
1421 // draw_line(frame, x1, y1 + 1, x2, y2 + 1);
1424 if(abs(y2 - y1) || abs(x2 - x1)) draw_line(frame, x2, y2, x3, y3);
1425 // draw_line(frame, x2, y2 + 1, x3, y3 + 1);
1427 if(abs(y2 - y1) || abs(x2 - x1)) draw_line(frame, x2, y2, x4, y4);
1428 // draw_line(frame, x2, y2 + 1, x4, y4 + 1);
1446 RotateScanPackage::RotateScanPackage()
1451 RotateScanUnit::RotateScanUnit(RotateScan *server, MotionMain *plugin)
1452 : LoadClient(server)
1454 this->server = server;
1455 this->plugin = plugin;
1460 RotateScanUnit::~RotateScanUnit()
1466 void RotateScanUnit::process_package(LoadPackage *package)
1468 if(server->skip) return;
1469 RotateScanPackage *pkg = (RotateScanPackage*)package;
1471 if((pkg->difference = server->get_cache(pkg->angle)) < 0)
1473 //printf("RotateScanUnit::process_package %d\n", __LINE__);
1474 int color_model = server->previous_frame->get_color_model();
1475 int pixel_size = BC_CModels::calculate_pixelsize(color_model);
1476 int row_bytes = server->previous_frame->get_bytes_per_line();
1479 rotater = new AffineEngine(1, 1);
1480 if(!temp) temp = new VFrame(0,
1482 server->previous_frame->get_w(),
1483 server->previous_frame->get_h(),
1486 //printf("RotateScanUnit::process_package %d\n", __LINE__);
1489 // Rotate original block size
1490 // rotater->set_viewport(server->block_x1,
1491 // server->block_y1,
1492 // server->block_x2 - server->block_x1,
1493 // server->block_y2 - server->block_y1);
1494 rotater->set_in_viewport(server->block_x1,
1496 server->block_x2 - server->block_x1,
1497 server->block_y2 - server->block_y1);
1498 rotater->set_out_viewport(server->block_x1,
1500 server->block_x2 - server->block_x1,
1501 server->block_y2 - server->block_y1);
1502 // rotater->set_pivot(server->block_x, server->block_y);
1503 rotater->set_in_pivot(server->block_x, server->block_y);
1504 rotater->set_out_pivot(server->block_x, server->block_y);
1505 //printf("RotateScanUnit::process_package %d\n", __LINE__);
1506 rotater->rotate(temp,
1507 server->previous_frame,
1510 // Scan reduced block size
1511 //plugin->output_frame->copy_from(server->current_frame);
1512 //plugin->output_frame->copy_from(temp);
1513 // printf("RotateScanUnit::process_package %d %d %d %d %d\n",
1519 // Clamp coordinates
1520 int x1 = server->scan_x;
1521 int y1 = server->scan_y;
1522 int x2 = x1 + server->scan_w;
1523 int y2 = y1 + server->scan_h;
1524 x2 = MIN(temp->get_w(), x2);
1525 y2 = MIN(temp->get_h(), y2);
1526 x2 = MIN(server->current_frame->get_w(), x2);
1527 y2 = MIN(server->current_frame->get_h(), y2);
1531 if(x2 > x1 && y2 > y1)
1533 pkg->difference = MotionScan::abs_diff(
1534 temp->get_rows()[y1] + x1 * pixel_size,
1535 server->current_frame->get_rows()[y1] + x1 * pixel_size,
1540 //printf("RotateScanUnit::process_package %d\n", __LINE__);
1541 server->put_cache(pkg->angle, pkg->difference);
1544 // printf("RotateScanUnit::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",
1545 // server->block_x1,
1546 // server->block_y1,
1547 // server->block_x2 - server->block_x1,
1548 // server->block_y2 - server->block_y1,
1554 // pkg->difference);
1579 RotateScan::RotateScan(MotionMain *plugin,
1584 total_clients, total_packages
1587 this->plugin = plugin;
1588 cache_lock = new Mutex("RotateScan::cache_lock");
1592 RotateScan::~RotateScan()
1597 void RotateScan::init_packages()
1599 for(int i = 0; i < get_total_packages(); i++)
1601 RotateScanPackage *pkg = (RotateScanPackage*)get_package(i);
1603 (scan_angle2 - scan_angle1) /
1609 LoadClient* RotateScan::new_client()
1611 return new RotateScanUnit(this, plugin);
1614 LoadPackage* RotateScan::new_package()
1616 return new RotateScanPackage;
1620 float RotateScan::scan_frame(VFrame *previous_frame,
1621 VFrame *current_frame,
1626 this->block_x = block_x;
1627 this->block_y = block_y;
1629 //printf("RotateScan::scan_frame %d\n", __LINE__);
1630 switch(plugin->config.tracking_type)
1632 case MotionScan::NO_CALCULATE:
1633 result = plugin->config.rotation_center;
1637 case MotionScan::LOAD:
1639 char string[BCTEXTLEN];
1640 sprintf(string, "%s%06d", ROTATION_FILE, plugin->get_source_position());
1641 FILE *input = fopen(string, "r");
1644 fscanf(input, "%f", &result);
1650 perror("RotateScan::scan_frame LOAD");
1663 this->previous_frame = previous_frame;
1664 this->current_frame = current_frame;
1665 int w = current_frame->get_w();
1666 int h = current_frame->get_h();
1667 int block_w = w * plugin->config.rotation_block_w / 100;
1668 int block_h = h * plugin->config.rotation_block_h / 100;
1670 if(this->block_x - block_w / 2 < 0) block_w = this->block_x * 2;
1671 if(this->block_y - block_h / 2 < 0) block_h = this->block_y * 2;
1672 if(this->block_x + block_w / 2 > w) block_w = (w - this->block_x) * 2;
1673 if(this->block_y + block_h / 2 > h) block_h = (h - this->block_y) * 2;
1675 block_x1 = this->block_x - block_w / 2;
1676 block_x2 = this->block_x + block_w / 2;
1677 block_y1 = this->block_y - block_h / 2;
1678 block_y2 = this->block_y + block_h / 2;
1681 // Calculate the maximum area available to scan after rotation.
1682 // Must be calculated from the starting range because of cache.
1683 // Get coords of rectangle after rotation.
1684 double center_x = this->block_x;
1685 double center_y = this->block_y;
1686 double max_angle = plugin->config.rotation_range;
1687 double base_angle1 = atan((float)block_h / block_w);
1688 double base_angle2 = atan((float)block_w / block_h);
1689 double target_angle1 = base_angle1 + max_angle * 2 * M_PI / 360;
1690 double target_angle2 = base_angle2 + max_angle * 2 * M_PI / 360;
1691 double radius = sqrt(block_w * block_w + block_h * block_h) / 2;
1692 double x1 = center_x - cos(target_angle1) * radius;
1693 double y1 = center_y - sin(target_angle1) * radius;
1694 double x2 = center_x + sin(target_angle2) * radius;
1695 double y2 = center_y - cos(target_angle2) * radius;
1696 double x3 = center_x - sin(target_angle2) * radius;
1697 double y3 = center_y + cos(target_angle2) * radius;
1699 // Track top edge to find greatest area.
1700 double max_area1 = 0;
1703 for(double x = x1; x < x2; x++)
1705 double y = y1 + (y2 - y1) * (x - x1) / (x2 - x1);
1706 if(x >= center_x && x < block_x2 && y >= block_y1 && y < center_y)
1708 double area = fabs(x - center_x) * fabs(y - center_y);
1709 if(area > max_area1)
1718 // Track left edge to find greatest area.
1719 double max_area2 = 0;
1722 for(double y = y1; y < y3; y++)
1724 double x = x1 + (x3 - x1) * (y - y1) / (y3 - y1);
1725 if(x >= block_x1 && x < center_x && y >= block_y1 && y < center_y)
1727 double area = fabs(x - center_x) * fabs(y - center_y);
1728 if(area > max_area2)
1737 double max_x, max_y;
1741 // Get reduced scan coords
1742 scan_w = (int)(fabs(max_x - center_x) * 2);
1743 scan_h = (int)(fabs(max_y - center_y) * 2);
1744 scan_x = (int)(center_x - scan_w / 2);
1745 scan_y = (int)(center_y - scan_h / 2);
1746 // printf("RotateScan::scan_frame center=%d,%d scan=%d,%d %dx%d\n",
1747 // this->block_x, this->block_y, scan_x, scan_y, scan_w, scan_h);
1748 // printf(" angle_range=%f block= %d,%d,%d,%d\n", max_angle, block_x1, block_y1, block_x2, block_y2);
1750 // Determine min angle from size of block
1751 double angle1 = atan((double)block_h / block_w);
1752 double angle2 = atan((double)(block_h - 1) / (block_w + 1));
1753 double min_angle = fabs(angle2 - angle1) / OVERSAMPLE;
1754 min_angle = MAX(min_angle, MIN_ANGLE);
1756 //printf("RotateScan::scan_frame %d min_angle=%f\n", __LINE__, min_angle * 360 / 2 / M_PI);
1758 cache.remove_all_objects();
1763 if(previous_frame->data_matches(current_frame))
1765 //printf("RotateScan::scan_frame: frames match. Skipping.\n");
1766 result = plugin->config.rotation_center;
1773 // Initial search range
1774 float angle_range = max_angle;
1775 result = plugin->config.rotation_center;
1776 total_steps = plugin->config.rotate_positions;
1779 while(angle_range >= min_angle * total_steps)
1781 scan_angle1 = result - angle_range;
1782 scan_angle2 = result + angle_range;
1785 set_package_count(total_steps);
1786 //set_package_count(1);
1789 int64_t min_difference = -1;
1790 for(int i = 0; i < get_total_packages(); i++)
1792 RotateScanPackage *pkg = (RotateScanPackage*)get_package(i);
1793 if(pkg->difference < min_difference || min_difference == -1)
1795 min_difference = pkg->difference;
1796 result = pkg->angle;
1807 //printf("RotateScan::scan_frame %d\n", __LINE__);
1809 if(!skip && plugin->config.tracking_type == MotionScan::SAVE)
1811 char string[BCTEXTLEN];
1815 plugin->get_source_position());
1816 FILE *output = fopen(string, "w");
1819 fprintf(output, "%f\n", result);
1824 perror("RotateScan::scan_frame SAVE");
1828 //printf("RotateScan::scan_frame %d angle=%f\n", __LINE__, result);
1835 int64_t RotateScan::get_cache(float angle)
1837 int64_t result = -1;
1838 cache_lock->lock("RotateScan::get_cache");
1839 for(int i = 0; i < cache.total; i++)
1841 RotateScanCache *ptr = cache.values[i];
1842 if(fabs(ptr->angle - angle) <= MIN_ANGLE)
1844 result = ptr->difference;
1848 cache_lock->unlock();
1852 void RotateScan::put_cache(float angle, int64_t difference)
1854 RotateScanCache *ptr = new RotateScanCache(angle, difference);
1855 cache_lock->lock("RotateScan::put_cache");
1857 cache_lock->unlock();
1868 RotateScanCache::RotateScanCache(float angle, int64_t difference)
1870 this->angle = angle;
1871 this->difference = difference;