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.terminate_string();
368 void MotionMain::read_data(KeyFrame *keyframe)
372 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
378 result = input.read_tag();
382 if(input.tag.title_is("MOTION"))
384 config.block_count = input.tag.get_property("BLOCK_COUNT", config.block_count);
385 config.global_positions = input.tag.get_property("GLOBAL_POSITIONS", config.global_positions);
386 config.rotate_positions = input.tag.get_property("ROTATE_POSITIONS", config.rotate_positions);
387 config.global_block_w = input.tag.get_property("GLOBAL_BLOCK_W", config.global_block_w);
388 config.global_block_h = input.tag.get_property("GLOBAL_BLOCK_H", config.global_block_h);
389 config.rotation_block_w = input.tag.get_property("ROTATION_BLOCK_W", config.rotation_block_w);
390 config.rotation_block_h = input.tag.get_property("ROTATION_BLOCK_H", config.rotation_block_h);
391 config.block_x = input.tag.get_property("BLOCK_X", config.block_x);
392 config.block_y = input.tag.get_property("BLOCK_Y", config.block_y);
393 config.global_range_w = input.tag.get_property("GLOBAL_RANGE_W", config.global_range_w);
394 config.global_range_h = input.tag.get_property("GLOBAL_RANGE_H", config.global_range_h);
395 config.rotation_range = input.tag.get_property("ROTATION_RANGE", config.rotation_range);
396 config.rotation_center = input.tag.get_property("ROTATION_CENTER", config.rotation_center);
397 config.magnitude = input.tag.get_property("MAGNITUDE", config.magnitude);
398 config.return_speed = input.tag.get_property("RETURN_SPEED", config.return_speed);
399 config.rotate_magnitude = input.tag.get_property("ROTATE_MAGNITUDE", config.rotate_magnitude);
400 config.rotate_return_speed = input.tag.get_property("ROTATE_RETURN_SPEED", config.rotate_return_speed);
401 config.action_type = input.tag.get_property("ACTION_TYPE", config.action_type);
402 config.global = input.tag.get_property("GLOBAL", config.global);
403 config.rotate = input.tag.get_property("ROTATE", config.rotate);
404 config.tracking_type = input.tag.get_property("TRACKING_TYPE", config.tracking_type);
405 config.draw_vectors = input.tag.get_property("DRAW_VECTORS", config.draw_vectors);
406 config.tracking_object = input.tag.get_property("TRACKING_OBJECT", config.tracking_object);
407 config.track_frame = input.tag.get_property("TRACK_FRAME", config.track_frame);
408 config.bottom_is_master = input.tag.get_property("BOTTOM_IS_MASTER", config.bottom_is_master);
409 config.horizontal_only = input.tag.get_property("HORIZONTAL_ONLY", config.horizontal_only);
410 config.vertical_only = input.tag.get_property("VERTICAL_ONLY", config.vertical_only);
425 void MotionMain::allocate_temp(int w, int h, int color_model)
428 (temp_frame->get_w() != w ||
429 temp_frame->get_h() != h))
435 temp_frame = new VFrame(w, h, color_model);
439 void MotionMain::process_global()
442 if(!engine) engine = new MotionScan(PluginClient::get_project_smp() + 1,
443 PluginClient::get_project_smp() + 1);
445 // Determine if frames changed
446 engine->scan_frame(current_global_ref,
448 config.global_range_w,
449 config.global_range_h,
450 config.global_block_w,
451 config.global_block_h,
454 config.tracking_object,
455 config.tracking_type,
457 config.horizontal_only,
458 config.vertical_only,
459 get_source_position(),
460 config.global_positions,
465 current_dx = engine->dx_result;
466 current_dy = engine->dy_result;
468 // Add current motion vector to accumulation vector.
469 if(config.tracking_object != MotionScan::TRACK_SINGLE)
472 total_dx = (int64_t)total_dx * (100 - config.return_speed) / 100;
473 total_dy = (int64_t)total_dy * (100 - config.return_speed) / 100;
474 total_dx += engine->dx_result;
475 total_dy += engine->dy_result;
476 // printf("MotionMain::process_global total_dx=%d engine->dx_result=%d\n",
478 // engine->dx_result);
481 // Make accumulation vector current
483 total_dx = engine->dx_result;
484 total_dy = engine->dy_result;
487 // Clamp accumulation vector
488 if(config.magnitude < 100)
490 int block_w = (int64_t)config.global_block_w *
491 current_global_ref->get_w() / 100;
492 int block_h = (int64_t)config.global_block_h *
493 current_global_ref->get_h() / 100;
494 int block_x_orig = (int64_t)(config.block_x *
495 current_global_ref->get_w() /
497 int block_y_orig = (int64_t)(config.block_y *
498 current_global_ref->get_h() /
501 int max_block_x = (int64_t)(current_global_ref->get_w() - block_x_orig) *
505 int max_block_y = (int64_t)(current_global_ref->get_h() - block_y_orig) *
509 int min_block_x = (int64_t)-block_x_orig *
513 int min_block_y = (int64_t)-block_y_orig *
518 CLAMP(total_dx, min_block_x, max_block_x);
519 CLAMP(total_dy, min_block_y, max_block_y);
523 printf("MotionMain::process_global 2 total_dx=%.02f total_dy=%.02f\n",
524 (float)total_dx / OVERSAMPLE,
525 (float)total_dy / OVERSAMPLE);
528 if(config.tracking_object != MotionScan::TRACK_SINGLE && !config.rotate)
530 // Transfer current reference frame to previous reference frame and update
531 // counter. Must wait for rotate to compare.
532 prev_global_ref->copy_from(current_global_ref);
533 previous_frame_number = get_source_position();
536 // Decide what to do with target based on requested operation
540 switch(config.action_type)
542 case MotionScan::NOTHING:
543 global_target_dst->copy_from(global_target_src);
545 case MotionScan::TRACK_PIXEL:
546 interpolation = NEAREST_NEIGHBOR;
547 dx = (int)(total_dx / OVERSAMPLE);
548 dy = (int)(total_dy / OVERSAMPLE);
550 case MotionScan::STABILIZE_PIXEL:
551 interpolation = NEAREST_NEIGHBOR;
552 dx = -(int)(total_dx / OVERSAMPLE);
553 dy = -(int)(total_dy / OVERSAMPLE);
556 case MotionScan::TRACK:
557 interpolation = CUBIC_LINEAR;
558 dx = (float)total_dx / OVERSAMPLE;
559 dy = (float)total_dy / OVERSAMPLE;
561 case MotionScan::STABILIZE:
562 interpolation = CUBIC_LINEAR;
563 dx = -(float)total_dx / OVERSAMPLE;
564 dy = -(float)total_dy / OVERSAMPLE;
569 if(config.action_type != MotionScan::NOTHING)
572 overlayer = new OverlayFrame(PluginClient::get_project_smp() + 1);
573 global_target_dst->clear_frame();
574 overlayer->overlay(global_target_dst,
578 global_target_src->get_w(),
579 global_target_src->get_h(),
582 (float)global_target_src->get_w() + dx,
583 (float)global_target_src->get_h() + dy,
592 void MotionMain::process_rotation()
597 // Convert the previous global reference into the previous rotation reference.
598 // Convert global target destination into rotation target source.
602 overlayer = new OverlayFrame(PluginClient::get_project_smp() + 1);
605 if(config.tracking_object == MotionScan::TRACK_SINGLE)
607 dx = (float)total_dx / OVERSAMPLE;
608 dy = (float)total_dy / OVERSAMPLE;
612 dx = (float)current_dx / OVERSAMPLE;
613 dy = (float)current_dy / OVERSAMPLE;
616 prev_rotate_ref->clear_frame();
617 overlayer->overlay(prev_rotate_ref,
621 prev_global_ref->get_w(),
622 prev_global_ref->get_h(),
625 (float)prev_global_ref->get_w() + dx,
626 (float)prev_global_ref->get_h() + dy,
630 // Pivot is destination global position
631 block_x = (int)(prev_rotate_ref->get_w() *
636 block_y = (int)(prev_rotate_ref->get_h() *
641 // Use the global target output as the rotation target input
642 rotate_target_src->copy_from(global_target_dst);
643 // Transfer current reference frame to previous reference frame for global.
644 if(config.tracking_object != MotionScan::TRACK_SINGLE)
646 prev_global_ref->copy_from(current_global_ref);
647 previous_frame_number = get_source_position();
653 block_x = (int)(prev_rotate_ref->get_w() *
656 block_y = (int)(prev_rotate_ref->get_h() *
665 motion_rotate = new RotateScan(this,
666 get_project_smp() + 1,
667 get_project_smp() + 1);
669 current_angle = motion_rotate->scan_frame(prev_rotate_ref,
676 // Add current rotation to accumulation
677 if(config.tracking_object != MotionScan::TRACK_SINGLE)
680 total_angle = total_angle * (100 - config.rotate_return_speed) / 100;
681 // Accumulate current rotation
682 total_angle += current_angle;
684 // Clamp rotation accumulation
685 if(config.rotate_magnitude < 90)
687 CLAMP(total_angle, -config.rotate_magnitude, config.rotate_magnitude);
692 // Transfer current reference frame to previous reference frame and update
694 prev_rotate_ref->copy_from(current_rotate_ref);
695 previous_frame_number = get_source_position();
700 total_angle = current_angle;
704 printf("MotionMain::process_rotation total_angle=%f\n", total_angle);
708 // Calculate rotation parameters based on requested operation
710 switch(config.action_type)
712 case MotionScan::NOTHING:
713 rotate_target_dst->copy_from(rotate_target_src);
715 case MotionScan::TRACK:
716 case MotionScan::TRACK_PIXEL:
719 case MotionScan::STABILIZE:
720 case MotionScan::STABILIZE_PIXEL:
721 angle = -total_angle;
727 if(config.action_type != MotionScan::NOTHING)
730 rotate_engine = new AffineEngine(PluginClient::get_project_smp() + 1,
731 PluginClient::get_project_smp() + 1);
733 rotate_target_dst->clear_frame();
735 // Determine pivot based on a number of factors.
736 switch(config.action_type)
738 case MotionScan::TRACK:
739 case MotionScan::TRACK_PIXEL:
740 // Use destination of global tracking.
741 // rotate_engine->set_pivot(block_x, block_y);
742 rotate_engine->set_in_pivot(block_x, block_y);
743 rotate_engine->set_out_pivot(block_x, block_y);
746 case MotionScan::STABILIZE:
747 case MotionScan::STABILIZE_PIXEL:
750 // Use origin of global stabilize operation
751 // rotate_engine->set_pivot((int)(rotate_target_dst->get_w() *
754 // (int)(rotate_target_dst->get_h() *
757 rotate_engine->set_in_pivot((int)(rotate_target_dst->get_w() *
760 (int)(rotate_target_dst->get_h() *
763 rotate_engine->set_out_pivot((int)(rotate_target_dst->get_w() *
766 (int)(rotate_target_dst->get_h() *
774 // rotate_engine->set_pivot(block_x, block_y);
775 rotate_engine->set_in_pivot(block_x, block_y);
776 rotate_engine->set_out_pivot(block_x, block_y);
782 rotate_engine->rotate(rotate_target_dst, rotate_target_src, angle);
783 // overlayer->overlay(rotate_target_dst,
787 // prev_rotate_ref->get_w(),
788 // prev_rotate_ref->get_h(),
791 // prev_rotate_ref->get_w(),
792 // prev_rotate_ref->get_h(),
796 // overlayer->overlay(rotate_target_dst,
797 // current_rotate_ref,
800 // prev_rotate_ref->get_w(),
801 // prev_rotate_ref->get_h(),
804 // prev_rotate_ref->get_w(),
805 // prev_rotate_ref->get_h(),
824 int MotionMain::process_buffer(VFrame **frame,
825 int64_t start_position,
828 int need_reconfigure = load_configuration();
829 int color_model = frame[0]->get_color_model();
830 w = frame[0]->get_w();
831 h = frame[0]->get_h();
835 printf("MotionMain::process_buffer %d start_position=%lld\n", __LINE__, start_position);
839 // Calculate the source and destination pointers for each of the operations.
840 // Get the layer to track motion in.
841 reference_layer = config.bottom_is_master ?
842 PluginClient::total_in_buffers - 1 :
844 // Get the layer to apply motion in.
845 target_layer = config.bottom_is_master ?
847 PluginClient::total_in_buffers - 1;
850 output_frame = frame[target_layer];
853 // Get the position of previous reference frame.
854 int64_t actual_previous_number;
855 // Skip if match frame not available
856 int skip_current = 0;
859 if(config.tracking_object == MotionScan::TRACK_SINGLE)
861 actual_previous_number = config.track_frame;
862 if(get_direction() == PLAY_REVERSE)
863 actual_previous_number++;
864 if(actual_previous_number == start_position)
869 actual_previous_number = start_position;
870 if(get_direction() == PLAY_FORWARD)
872 actual_previous_number--;
873 if(actual_previous_number < get_source_start())
877 KeyFrame *keyframe = get_prev_keyframe(start_position, 1);
878 if(keyframe->position > 0 &&
879 actual_previous_number < keyframe->position)
885 actual_previous_number++;
886 if(actual_previous_number >= get_source_start() + get_total_len())
890 KeyFrame *keyframe = get_next_keyframe(start_position, 1);
891 if(keyframe->position > 0 &&
892 actual_previous_number >= keyframe->position)
897 // Only count motion since last keyframe
903 if(!config.global && !config.rotate) skip_current = 1;
908 // printf("process_realtime %d %lld %lld\n",
910 // previous_frame_number,
911 // actual_previous_number);
912 // Load match frame and reset vectors
913 int need_reload = !skip_current &&
914 (previous_frame_number != actual_previous_number ||
921 previous_frame_number = actual_previous_number;
938 // Get the global pointers. Here we walk through the sequence of events.
941 // Assume global only. Global reads previous frame and compares
942 // with current frame to get the current translation.
943 // The center of the search area is fixed in compensate mode or
944 // the user value + the accumulation vector in track mode.
946 prev_global_ref = new VFrame(w, h, color_model);
947 if(!current_global_ref)
948 current_global_ref = new VFrame(w, h, color_model);
950 // Global loads the current target frame into the src and
951 // writes it to the dst frame with desired translation.
952 if(!global_target_src)
953 global_target_src = new VFrame(w, h, color_model);
954 if(!global_target_dst)
955 global_target_dst = new VFrame(w, h, color_model);
958 // Load the global frames
961 read_frame(prev_global_ref,
963 previous_frame_number,
967 read_frame(current_global_ref,
971 read_frame(global_target_src,
978 // Global followed by rotate
981 // Must translate the previous global reference by the current global
982 // accumulation vector to match the current global reference.
983 // The center of the search area is always the user value + the accumulation
986 prev_rotate_ref = new VFrame(w, h, color_model);
987 // The current global reference is the current rotation reference.
988 if(!current_rotate_ref)
989 current_rotate_ref = new VFrame(w, h, color_model);
990 current_rotate_ref->copy_from(current_global_ref);
992 // The global target destination is copied to the rotation target source
993 // then written to the rotation output with rotation.
994 // The pivot for the rotation is the center of the search area
995 // if we're tracking.
996 // The pivot is fixed to the user position if we're compensating.
997 if(!rotate_target_src)
998 rotate_target_src = new VFrame(w, h, color_model);
999 if(!rotate_target_dst)
1000 rotate_target_dst = new VFrame(w, h, color_model);
1007 // Rotation reads the previous reference frame and compares it with current
1009 if(!prev_rotate_ref)
1010 prev_rotate_ref = new VFrame(w, h, color_model);
1011 if(!current_rotate_ref)
1012 current_rotate_ref = new VFrame(w, h, color_model);
1014 // Rotation loads target frame to temporary, rotates it, and writes it to the
1015 // target frame. The pivot is always fixed.
1016 if(!rotate_target_src)
1017 rotate_target_src = new VFrame(w, h, color_model);
1018 if(!rotate_target_dst)
1019 rotate_target_dst = new VFrame(w, h, color_model);
1022 // Load the rotate frames
1025 read_frame(prev_rotate_ref,
1027 previous_frame_number,
1030 read_frame(current_rotate_ref,
1034 read_frame(rotate_target_src,
1051 // Get position change from previous frame to current frame
1052 if(config.global) process_global();
1053 // Get rotation change from previous frame to current frame
1054 if(config.rotate) process_rotation();
1055 //frame[target_layer]->copy_from(prev_rotate_ref);
1056 //frame[target_layer]->copy_from(current_rotate_ref);
1064 // Transfer the relevant target frame to the output
1069 frame[target_layer]->copy_from(rotate_target_dst);
1073 frame[target_layer]->copy_from(global_target_dst);
1077 // Read the target destination directly
1079 read_frame(frame[target_layer],
1085 if(config.draw_vectors)
1087 draw_vectors(frame[target_layer]);
1091 printf("MotionMain::process_buffer %d\n", __LINE__);
1098 void MotionMain::draw_vectors(VFrame *frame)
1100 int w = frame->get_w();
1101 int h = frame->get_h();
1102 int global_x1, global_y1;
1103 int global_x2, global_y2;
1104 int block_x, block_y;
1105 int block_w, block_h;
1106 int block_x1, block_y1;
1107 int block_x2, block_y2;
1108 int block_x3, block_y3;
1109 int block_x4, block_y4;
1110 int search_w, search_h;
1111 int search_x1, search_y1;
1112 int search_x2, search_y2;
1113 int search_x3, search_y3;
1114 int search_x4, search_y4;
1120 // Start of vector is center of previous block.
1121 // End of vector is total accumulation.
1122 if(config.tracking_object == MotionScan::TRACK_SINGLE)
1124 global_x1 = (int64_t)(config.block_x *
1127 global_y1 = (int64_t)(config.block_y *
1130 global_x2 = global_x1 + total_dx / OVERSAMPLE;
1131 global_y2 = global_y1 + total_dy / OVERSAMPLE;
1132 //printf("MotionMain::draw_vectors %d %d %d %d %d %d\n", total_dx, total_dy, global_x1, global_y1, global_x2, global_y2);
1135 // Start of vector is center of previous block.
1136 // End of vector is current change.
1137 if(config.tracking_object == MotionScan::PREVIOUS_SAME_BLOCK)
1139 global_x1 = (int64_t)(config.block_x *
1142 global_y1 = (int64_t)(config.block_y *
1145 global_x2 = global_x1 + current_dx / OVERSAMPLE;
1146 global_y2 = global_y1 + current_dy / OVERSAMPLE;
1150 global_x1 = (int64_t)(config.block_x *
1153 (total_dx - current_dx) /
1155 global_y1 = (int64_t)(config.block_y *
1158 (total_dy - current_dy) /
1160 global_x2 = (int64_t)(config.block_x *
1165 global_y2 = (int64_t)(config.block_y *
1172 block_x = global_x1;
1173 block_y = global_y1;
1174 block_w = config.global_block_w * w / 100;
1175 block_h = config.global_block_h * h / 100;
1176 block_x1 = block_x - block_w / 2;
1177 block_y1 = block_y - block_h / 2;
1178 block_x2 = block_x + block_w / 2;
1179 block_y2 = block_y + block_h / 2;
1180 search_w = config.global_range_w * w / 100;
1181 search_h = config.global_range_h * h / 100;
1182 search_x1 = block_x1 - search_w / 2;
1183 search_y1 = block_y1 - search_h / 2;
1184 search_x2 = block_x2 + search_w / 2;
1185 search_y2 = block_y2 + search_h / 2;
1187 // printf("MotionMain::draw_vectors %d %d %d %d %d %d %d %d %d %d %d %d\n",
1201 MotionScan::clamp_scan(w,
1214 draw_arrow(frame, global_x1, global_y1, global_x2, global_y2);
1217 draw_line(frame, block_x1, block_y1, block_x2, block_y1);
1218 draw_line(frame, block_x2, block_y1, block_x2, block_y2);
1219 draw_line(frame, block_x2, block_y2, block_x1, block_y2);
1220 draw_line(frame, block_x1, block_y2, block_x1, block_y1);
1224 draw_line(frame, search_x1, search_y1, search_x2, search_y1);
1225 draw_line(frame, search_x2, search_y1, search_x2, search_y2);
1226 draw_line(frame, search_x2, search_y2, search_x1, search_y2);
1227 draw_line(frame, search_x1, search_y2, search_x1, search_y1);
1229 // Block should be endpoint of motion
1232 block_x = global_x2;
1233 block_y = global_y2;
1238 block_x = (int64_t)(config.block_x * w / 100);
1239 block_y = (int64_t)(config.block_y * h / 100);
1242 block_w = config.rotation_block_w * w / 100;
1243 block_h = config.rotation_block_h * h / 100;
1246 float angle = total_angle * 2 * M_PI / 360;
1247 double base_angle1 = atan((float)block_h / block_w);
1248 double base_angle2 = atan((float)block_w / block_h);
1249 double target_angle1 = base_angle1 + angle;
1250 double target_angle2 = base_angle2 + angle;
1251 double radius = sqrt(block_w * block_w + block_h * block_h) / 2;
1252 block_x1 = (int)(block_x - cos(target_angle1) * radius);
1253 block_y1 = (int)(block_y - sin(target_angle1) * radius);
1254 block_x2 = (int)(block_x + sin(target_angle2) * radius);
1255 block_y2 = (int)(block_y - cos(target_angle2) * radius);
1256 block_x3 = (int)(block_x - sin(target_angle2) * radius);
1257 block_y3 = (int)(block_y + cos(target_angle2) * radius);
1258 block_x4 = (int)(block_x + cos(target_angle1) * radius);
1259 block_y4 = (int)(block_y + sin(target_angle1) * radius);
1261 draw_line(frame, block_x1, block_y1, block_x2, block_y2);
1262 draw_line(frame, block_x2, block_y2, block_x4, block_y4);
1263 draw_line(frame, block_x4, block_y4, block_x3, block_y3);
1264 draw_line(frame, block_x3, block_y3, block_x1, block_y1);
1270 draw_line(frame, block_x, block_y - 5, block_x, block_y + 6);
1271 draw_line(frame, block_x - 5, block_y, block_x + 6, block_y);
1278 void MotionMain::draw_pixel(VFrame *frame, int x, int y)
1280 if(!(x >= 0 && y >= 0 && x < frame->get_w() && y < frame->get_h())) return;
1282 #define DRAW_PIXEL(x, y, components, do_yuv, max, type) \
1284 type **rows = (type**)frame->get_rows(); \
1285 rows[y][x * components] = max - rows[y][x * components]; \
1288 rows[y][x * components + 1] = max - rows[y][x * components + 1]; \
1289 rows[y][x * components + 2] = max - rows[y][x * components + 2]; \
1293 rows[y][x * components + 1] = (max / 2 + 1) - rows[y][x * components + 1]; \
1294 rows[y][x * components + 2] = (max / 2 + 1) - rows[y][x * components + 2]; \
1296 if(components == 4) \
1297 rows[y][x * components + 3] = max; \
1301 switch(frame->get_color_model())
1304 DRAW_PIXEL(x, y, 3, 0, 0xff, unsigned char);
1307 DRAW_PIXEL(x, y, 4, 0, 0xff, unsigned char);
1310 DRAW_PIXEL(x, y, 3, 0, 1.0, float);
1313 DRAW_PIXEL(x, y, 4, 0, 1.0, float);
1316 DRAW_PIXEL(x, y, 3, 1, 0xff, unsigned char);
1319 DRAW_PIXEL(x, y, 4, 1, 0xff, unsigned char);
1322 DRAW_PIXEL(x, y, 3, 0, 0xffff, uint16_t);
1325 DRAW_PIXEL(x, y, 3, 1, 0xffff, uint16_t);
1327 case BC_RGBA16161616:
1328 DRAW_PIXEL(x, y, 4, 0, 0xffff, uint16_t);
1330 case BC_YUVA16161616:
1331 DRAW_PIXEL(x, y, 4, 1, 0xffff, uint16_t);
1337 void MotionMain::draw_line(VFrame *frame, int x1, int y1, int x2, int y2)
1339 int w = labs(x2 - x1);
1340 int h = labs(y2 - y1);
1341 //printf("MotionMain::draw_line 1 %d %d %d %d\n", x1, y1, x2, y2);
1345 draw_pixel(frame, x1, y1);
1350 // Flip coordinates so x1 < x2
1360 int numerator = y2 - y1;
1361 int denominator = x2 - x1;
1362 for(int i = x1; i < x2; i++)
1364 int y = y1 + (int64_t)(i - x1) * (int64_t)numerator / (int64_t)denominator;
1365 draw_pixel(frame, i, y);
1370 // Flip coordinates so y1 < y2
1380 int numerator = x2 - x1;
1381 int denominator = y2 - y1;
1382 for(int i = y1; i < y2; i++)
1384 int x = x1 + (int64_t)(i - y1) * (int64_t)numerator / (int64_t)denominator;
1385 draw_pixel(frame, x, i);
1388 //printf("MotionMain::draw_line 2\n");
1391 #define ARROW_SIZE 10
1392 void MotionMain::draw_arrow(VFrame *frame, int x1, int y1, int x2, int y2)
1394 double angle = atan((float)(y2 - y1) / (float)(x2 - x1));
1395 double angle1 = angle + (float)145 / 360 * 2 * 3.14159265;
1396 double angle2 = angle - (float)145 / 360 * 2 * 3.14159265;
1403 x3 = x2 - (int)(ARROW_SIZE * cos(angle1));
1404 y3 = y2 - (int)(ARROW_SIZE * sin(angle1));
1405 x4 = x2 - (int)(ARROW_SIZE * cos(angle2));
1406 y4 = y2 - (int)(ARROW_SIZE * sin(angle2));
1410 x3 = x2 + (int)(ARROW_SIZE * cos(angle1));
1411 y3 = y2 + (int)(ARROW_SIZE * sin(angle1));
1412 x4 = x2 + (int)(ARROW_SIZE * cos(angle2));
1413 y4 = y2 + (int)(ARROW_SIZE * sin(angle2));
1417 draw_line(frame, x1, y1, x2, y2);
1418 // draw_line(frame, x1, y1 + 1, x2, y2 + 1);
1421 if(abs(y2 - y1) || abs(x2 - x1)) draw_line(frame, x2, y2, x3, y3);
1422 // draw_line(frame, x2, y2 + 1, x3, y3 + 1);
1424 if(abs(y2 - y1) || abs(x2 - x1)) draw_line(frame, x2, y2, x4, y4);
1425 // draw_line(frame, x2, y2 + 1, x4, y4 + 1);
1443 RotateScanPackage::RotateScanPackage()
1448 RotateScanUnit::RotateScanUnit(RotateScan *server, MotionMain *plugin)
1449 : LoadClient(server)
1451 this->server = server;
1452 this->plugin = plugin;
1457 RotateScanUnit::~RotateScanUnit()
1463 void RotateScanUnit::process_package(LoadPackage *package)
1465 if(server->skip) return;
1466 RotateScanPackage *pkg = (RotateScanPackage*)package;
1468 if((pkg->difference = server->get_cache(pkg->angle)) < 0)
1470 //printf("RotateScanUnit::process_package %d\n", __LINE__);
1471 int color_model = server->previous_frame->get_color_model();
1472 int pixel_size = BC_CModels::calculate_pixelsize(color_model);
1473 int row_bytes = server->previous_frame->get_bytes_per_line();
1476 rotater = new AffineEngine(1, 1);
1477 if(!temp) temp = new VFrame(0,
1479 server->previous_frame->get_w(),
1480 server->previous_frame->get_h(),
1483 //printf("RotateScanUnit::process_package %d\n", __LINE__);
1486 // Rotate original block size
1487 // rotater->set_viewport(server->block_x1,
1488 // server->block_y1,
1489 // server->block_x2 - server->block_x1,
1490 // server->block_y2 - server->block_y1);
1491 rotater->set_in_viewport(server->block_x1,
1493 server->block_x2 - server->block_x1,
1494 server->block_y2 - server->block_y1);
1495 rotater->set_out_viewport(server->block_x1,
1497 server->block_x2 - server->block_x1,
1498 server->block_y2 - server->block_y1);
1499 // rotater->set_pivot(server->block_x, server->block_y);
1500 rotater->set_in_pivot(server->block_x, server->block_y);
1501 rotater->set_out_pivot(server->block_x, server->block_y);
1502 //printf("RotateScanUnit::process_package %d\n", __LINE__);
1503 rotater->rotate(temp,
1504 server->previous_frame,
1507 // Scan reduced block size
1508 //plugin->output_frame->copy_from(server->current_frame);
1509 //plugin->output_frame->copy_from(temp);
1510 // printf("RotateScanUnit::process_package %d %d %d %d %d\n",
1516 // Clamp coordinates
1517 int x1 = server->scan_x;
1518 int y1 = server->scan_y;
1519 int x2 = x1 + server->scan_w;
1520 int y2 = y1 + server->scan_h;
1521 x2 = MIN(temp->get_w(), x2);
1522 y2 = MIN(temp->get_h(), y2);
1523 x2 = MIN(server->current_frame->get_w(), x2);
1524 y2 = MIN(server->current_frame->get_h(), y2);
1528 if(x2 > x1 && y2 > y1)
1530 pkg->difference = MotionScan::abs_diff(
1531 temp->get_rows()[y1] + x1 * pixel_size,
1532 server->current_frame->get_rows()[y1] + x1 * pixel_size,
1537 //printf("RotateScanUnit::process_package %d\n", __LINE__);
1538 server->put_cache(pkg->angle, pkg->difference);
1541 // 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",
1542 // server->block_x1,
1543 // server->block_y1,
1544 // server->block_x2 - server->block_x1,
1545 // server->block_y2 - server->block_y1,
1551 // pkg->difference);
1576 RotateScan::RotateScan(MotionMain *plugin,
1581 total_clients, total_packages
1584 this->plugin = plugin;
1585 cache_lock = new Mutex("RotateScan::cache_lock");
1589 RotateScan::~RotateScan()
1594 void RotateScan::init_packages()
1596 for(int i = 0; i < get_total_packages(); i++)
1598 RotateScanPackage *pkg = (RotateScanPackage*)get_package(i);
1600 (scan_angle2 - scan_angle1) /
1606 LoadClient* RotateScan::new_client()
1608 return new RotateScanUnit(this, plugin);
1611 LoadPackage* RotateScan::new_package()
1613 return new RotateScanPackage;
1617 float RotateScan::scan_frame(VFrame *previous_frame,
1618 VFrame *current_frame,
1623 this->block_x = block_x;
1624 this->block_y = block_y;
1626 //printf("RotateScan::scan_frame %d\n", __LINE__);
1627 switch(plugin->config.tracking_type)
1629 case MotionScan::NO_CALCULATE:
1630 result = plugin->config.rotation_center;
1634 case MotionScan::LOAD:
1636 char string[BCTEXTLEN];
1637 sprintf(string, "%s%06d", ROTATION_FILE, plugin->get_source_position());
1638 FILE *input = fopen(string, "r");
1641 fscanf(input, "%f", &result);
1647 perror("RotateScan::scan_frame LOAD");
1660 this->previous_frame = previous_frame;
1661 this->current_frame = current_frame;
1662 int w = current_frame->get_w();
1663 int h = current_frame->get_h();
1664 int block_w = w * plugin->config.rotation_block_w / 100;
1665 int block_h = h * plugin->config.rotation_block_h / 100;
1667 if(this->block_x - block_w / 2 < 0) block_w = this->block_x * 2;
1668 if(this->block_y - block_h / 2 < 0) block_h = this->block_y * 2;
1669 if(this->block_x + block_w / 2 > w) block_w = (w - this->block_x) * 2;
1670 if(this->block_y + block_h / 2 > h) block_h = (h - this->block_y) * 2;
1672 block_x1 = this->block_x - block_w / 2;
1673 block_x2 = this->block_x + block_w / 2;
1674 block_y1 = this->block_y - block_h / 2;
1675 block_y2 = this->block_y + block_h / 2;
1678 // Calculate the maximum area available to scan after rotation.
1679 // Must be calculated from the starting range because of cache.
1680 // Get coords of rectangle after rotation.
1681 double center_x = this->block_x;
1682 double center_y = this->block_y;
1683 double max_angle = plugin->config.rotation_range;
1684 double base_angle1 = atan((float)block_h / block_w);
1685 double base_angle2 = atan((float)block_w / block_h);
1686 double target_angle1 = base_angle1 + max_angle * 2 * M_PI / 360;
1687 double target_angle2 = base_angle2 + max_angle * 2 * M_PI / 360;
1688 double radius = sqrt(block_w * block_w + block_h * block_h) / 2;
1689 double x1 = center_x - cos(target_angle1) * radius;
1690 double y1 = center_y - sin(target_angle1) * radius;
1691 double x2 = center_x + sin(target_angle2) * radius;
1692 double y2 = center_y - cos(target_angle2) * radius;
1693 double x3 = center_x - sin(target_angle2) * radius;
1694 double y3 = center_y + cos(target_angle2) * radius;
1696 // Track top edge to find greatest area.
1697 double max_area1 = 0;
1700 for(double x = x1; x < x2; x++)
1702 double y = y1 + (y2 - y1) * (x - x1) / (x2 - x1);
1703 if(x >= center_x && x < block_x2 && y >= block_y1 && y < center_y)
1705 double area = fabs(x - center_x) * fabs(y - center_y);
1706 if(area > max_area1)
1715 // Track left edge to find greatest area.
1716 double max_area2 = 0;
1719 for(double y = y1; y < y3; y++)
1721 double x = x1 + (x3 - x1) * (y - y1) / (y3 - y1);
1722 if(x >= block_x1 && x < center_x && y >= block_y1 && y < center_y)
1724 double area = fabs(x - center_x) * fabs(y - center_y);
1725 if(area > max_area2)
1734 double max_x, max_y;
1738 // Get reduced scan coords
1739 scan_w = (int)(fabs(max_x - center_x) * 2);
1740 scan_h = (int)(fabs(max_y - center_y) * 2);
1741 scan_x = (int)(center_x - scan_w / 2);
1742 scan_y = (int)(center_y - scan_h / 2);
1743 // printf("RotateScan::scan_frame center=%d,%d scan=%d,%d %dx%d\n",
1744 // this->block_x, this->block_y, scan_x, scan_y, scan_w, scan_h);
1745 // printf(" angle_range=%f block= %d,%d,%d,%d\n", max_angle, block_x1, block_y1, block_x2, block_y2);
1747 // Determine min angle from size of block
1748 double angle1 = atan((double)block_h / block_w);
1749 double angle2 = atan((double)(block_h - 1) / (block_w + 1));
1750 double min_angle = fabs(angle2 - angle1) / OVERSAMPLE;
1751 min_angle = MAX(min_angle, MIN_ANGLE);
1753 //printf("RotateScan::scan_frame %d min_angle=%f\n", __LINE__, min_angle * 360 / 2 / M_PI);
1755 cache.remove_all_objects();
1760 if(previous_frame->data_matches(current_frame))
1762 //printf("RotateScan::scan_frame: frames match. Skipping.\n");
1763 result = plugin->config.rotation_center;
1770 // Initial search range
1771 float angle_range = max_angle;
1772 result = plugin->config.rotation_center;
1773 total_steps = plugin->config.rotate_positions;
1776 while(angle_range >= min_angle * total_steps)
1778 scan_angle1 = result - angle_range;
1779 scan_angle2 = result + angle_range;
1782 set_package_count(total_steps);
1783 //set_package_count(1);
1786 int64_t min_difference = -1;
1787 for(int i = 0; i < get_total_packages(); i++)
1789 RotateScanPackage *pkg = (RotateScanPackage*)get_package(i);
1790 if(pkg->difference < min_difference || min_difference == -1)
1792 min_difference = pkg->difference;
1793 result = pkg->angle;
1804 //printf("RotateScan::scan_frame %d\n", __LINE__);
1806 if(!skip && plugin->config.tracking_type == MotionScan::SAVE)
1808 char string[BCTEXTLEN];
1812 plugin->get_source_position());
1813 FILE *output = fopen(string, "w");
1816 fprintf(output, "%f\n", result);
1821 perror("RotateScan::scan_frame SAVE");
1825 //printf("RotateScan::scan_frame %d angle=%f\n", __LINE__, result);
1832 int64_t RotateScan::get_cache(float angle)
1834 int64_t result = -1;
1835 cache_lock->lock("RotateScan::get_cache");
1836 for(int i = 0; i < cache.total; i++)
1838 RotateScanCache *ptr = cache.values[i];
1839 if(fabs(ptr->angle - angle) <= MIN_ANGLE)
1841 result = ptr->difference;
1845 cache_lock->unlock();
1849 void RotateScan::put_cache(float angle, int64_t difference)
1851 RotateScanCache *ptr = new RotateScanCache(angle, difference);
1852 cache_lock->lock("RotateScan::put_cache");
1854 cache_lock->unlock();
1865 RotateScanCache::RotateScanCache(float angle, int64_t difference)
1867 this->angle = angle;
1868 this->difference = difference;