4 * Copyright (C) 2016 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-hv.h"
31 #include "motionscan-hv.h"
32 #include "motionwindow-hv.h"
34 #include "overlayframe.h"
35 #include "rotateframe.h"
36 #include "transportque.h"
42 REGISTER_PLUGIN(MotionHVMain)
52 MotionHVConfig::MotionHVConfig()
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;
66 // rotate_positions = 4;
68 rotate_magnitude = 90;
70 rotate_return_speed = 0;
71 action_type = MotionHVScan::STABILIZE;
74 tracking_type = MotionHVScan::NO_CALCULATE;
76 tracking_object = MotionHVScan::TRACK_SINGLE;
83 void MotionHVConfig::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 MotionHVConfig::equivalent(MotionHVConfig &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 MotionHVConfig::copy_from(MotionHVConfig &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 MotionHVConfig::interpolate(MotionHVConfig &prev,
158 MotionHVConfig &next,
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 MotionHVMain::MotionHVMain(PluginServer *server)
213 : PluginVClient(server)
217 // motion_rotate = 0;
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 MotionHVMain::~MotionHVMain()
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* MotionHVMain::plugin_title() { return _("MotionHV"); }
261 int MotionHVMain::is_realtime() { return 1; }
262 int MotionHVMain::is_multichannel() { return 1; }
265 NEW_WINDOW_MACRO(MotionHVMain, MotionHVWindow)
267 LOAD_CONFIGURATION_MACRO(MotionHVMain, MotionHVConfig)
271 void MotionHVMain::update_gui()
275 if(load_configuration())
277 thread->window->lock_window("MotionHVMain::update_gui");
279 // char string[BCTEXTLEN];
280 // sprintf(string, "%d", config.global_positions);
281 // ((MotionHVWindow*)thread->window)->global_search_positions->set_text(string);
282 // sprintf(string, "%d", config.rotate_positions);
283 // ((MotionHVWindow*)thread->window)->rotation_search_positions->set_text(string);
285 ((MotionHVWindow*)thread->window)->global_block_w->update(config.global_block_w);
286 ((MotionHVWindow*)thread->window)->global_block_h->update(config.global_block_h);
287 // ((MotionHVWindow*)thread->window)->rotation_block_w->update(config.rotation_block_w);
288 // ((MotionHVWindow*)thread->window)->rotation_block_h->update(config.rotation_block_h);
289 ((MotionHVWindow*)thread->window)->block_x->update(config.block_x);
290 ((MotionHVWindow*)thread->window)->block_y->update(config.block_y);
291 ((MotionHVWindow*)thread->window)->block_x_text->update((float)config.block_x);
292 ((MotionHVWindow*)thread->window)->block_y_text->update((float)config.block_y);
293 ((MotionHVWindow*)thread->window)->magnitude->update(config.magnitude);
294 ((MotionHVWindow*)thread->window)->return_speed->update(config.return_speed);
295 ((MotionHVWindow*)thread->window)->rotate_magnitude->update(config.rotate_magnitude);
296 ((MotionHVWindow*)thread->window)->rotate_return_speed->update(config.rotate_return_speed);
297 ((MotionHVWindow*)thread->window)->rotation_range->update(config.rotation_range);
298 ((MotionHVWindow*)thread->window)->rotation_center->update(config.rotation_center);
301 ((MotionHVWindow*)thread->window)->track_single->update(config.tracking_object == MotionHVScan::TRACK_SINGLE);
302 ((MotionHVWindow*)thread->window)->track_frame_number->update(config.track_frame);
303 ((MotionHVWindow*)thread->window)->track_previous->update(config.tracking_object == MotionHVScan::TRACK_PREVIOUS);
304 ((MotionHVWindow*)thread->window)->previous_same->update(config.tracking_object == MotionHVScan::PREVIOUS_SAME_BLOCK);
305 if(config.tracking_object != MotionHVScan::TRACK_SINGLE)
306 ((MotionHVWindow*)thread->window)->track_frame_number->disable();
308 ((MotionHVWindow*)thread->window)->track_frame_number->enable();
310 ((MotionHVWindow*)thread->window)->action_type->set_text(
311 ActionType::to_text(config.action_type));
312 ((MotionHVWindow*)thread->window)->tracking_type->set_text(
313 TrackingType::to_text(config.tracking_type));
314 ((MotionHVWindow*)thread->window)->track_direction->set_text(
315 TrackDirection::to_text(config.horizontal_only, config.vertical_only));
316 ((MotionHVWindow*)thread->window)->master_layer->set_text(
317 MasterLayer::to_text(config.bottom_is_master));
320 ((MotionHVWindow*)thread->window)->update_mode();
321 thread->window->unlock_window();
329 void MotionHVMain::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("MOTIONHV");
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("/MOTIONHV");
367 output.terminate_string();
370 void MotionHVMain::read_data(KeyFrame *keyframe)
374 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
380 result = input.read_tag();
384 if(input.tag.title_is("MOTIONHV"))
386 config.block_count = input.tag.get_property("BLOCK_COUNT", config.block_count);
387 // config.global_positions = input.tag.get_property("GLOBAL_POSITIONS", config.global_positions);
388 // config.rotate_positions = input.tag.get_property("ROTATE_POSITIONS", config.rotate_positions);
389 config.global_block_w = input.tag.get_property("GLOBAL_BLOCK_W", config.global_block_w);
390 config.global_block_h = input.tag.get_property("GLOBAL_BLOCK_H", config.global_block_h);
391 // config.rotation_block_w = input.tag.get_property("ROTATION_BLOCK_W", config.rotation_block_w);
392 // config.rotation_block_h = input.tag.get_property("ROTATION_BLOCK_H", config.rotation_block_h);
393 config.block_x = input.tag.get_property("BLOCK_X", config.block_x);
394 config.block_y = input.tag.get_property("BLOCK_Y", config.block_y);
395 config.global_range_w = input.tag.get_property("GLOBAL_RANGE_W", config.global_range_w);
396 config.global_range_h = input.tag.get_property("GLOBAL_RANGE_H", config.global_range_h);
397 config.rotation_range = input.tag.get_property("ROTATION_RANGE", config.rotation_range);
398 config.rotation_center = input.tag.get_property("ROTATION_CENTER", config.rotation_center);
399 config.magnitude = input.tag.get_property("MAGNITUDE", config.magnitude);
400 config.return_speed = input.tag.get_property("RETURN_SPEED", config.return_speed);
401 config.rotate_magnitude = input.tag.get_property("ROTATE_MAGNITUDE", config.rotate_magnitude);
402 config.rotate_return_speed = input.tag.get_property("ROTATE_RETURN_SPEED", config.rotate_return_speed);
403 config.action_type = input.tag.get_property("ACTION_TYPE", config.action_type);
404 // config.global = input.tag.get_property("GLOBAL", config.global);
405 config.rotate = input.tag.get_property("ROTATE", config.rotate);
406 config.tracking_type = input.tag.get_property("TRACKING_TYPE", config.tracking_type);
407 config.draw_vectors = input.tag.get_property("DRAW_VECTORS", config.draw_vectors);
408 config.tracking_object = input.tag.get_property("TRACKING_OBJECT", config.tracking_object);
409 config.track_frame = input.tag.get_property("TRACK_FRAME", config.track_frame);
410 config.bottom_is_master = input.tag.get_property("BOTTOM_IS_MASTER", config.bottom_is_master);
411 config.horizontal_only = input.tag.get_property("HORIZONTAL_ONLY", config.horizontal_only);
412 config.vertical_only = input.tag.get_property("VERTICAL_ONLY", config.vertical_only);
427 void MotionHVMain::allocate_temp(int w, int h, int color_model)
430 (temp_frame->get_w() != w ||
431 temp_frame->get_h() != h))
437 temp_frame = new VFrame(w, h, color_model);
441 void MotionHVMain::process_global()
443 int w = current_global_ref->get_w();
444 int h = current_global_ref->get_h();
447 if(!engine) engine = new MotionHVScan(PluginClient::get_project_smp() + 1,
448 PluginClient::get_project_smp() + 1);
450 // Determine if frames changed
451 // printf("MotionHVMain::process_global %d block_y=%f total_dy=%d\n",
452 // __LINE__, config.block_y * h / 100, total_dy);
453 engine->scan_frame(current_global_ref,
455 config.global_range_w * w / 100,
456 config.global_range_h * h / 100,
457 config.global_block_w * w / 100,
458 config.global_block_h * h / 100,
459 config.block_x * w / 100,
460 config.block_y * h / 100,
461 config.tracking_object,
462 config.tracking_type,
464 config.horizontal_only,
465 config.vertical_only,
466 get_source_position(),
472 config.rotate, // do_rotate
473 config.rotation_center,
474 config.rotation_range);
476 current_dx = engine->dx_result;
477 current_dy = engine->dy_result;
479 // Add current motion vector to accumulation vector.
480 if(config.tracking_object != MotionHVScan::TRACK_SINGLE)
483 total_dx = (int64_t)total_dx * (100 - config.return_speed) / 100;
484 total_dy = (int64_t)total_dy * (100 - config.return_speed) / 100;
485 total_dx += engine->dx_result;
486 total_dy += engine->dy_result;
487 // printf("MotionHVMain::process_global %d total_dy=%d engine->dy_result=%d\n",
488 // __LINE__, total_dy, engine->dy_result);
491 // Make accumulation vector current
493 total_dx = engine->dx_result;
494 total_dy = engine->dy_result;
497 // Clamp accumulation vector
498 if(config.magnitude < 100)
500 //int block_w = (int64_t)config.global_block_w * w / 100;
501 //int block_h = (int64_t)config.global_block_h * h / 100;
502 int block_x_orig = (int64_t)(config.block_x * w / 100);
503 int block_y_orig = (int64_t)(config.block_y *
504 current_global_ref->get_h() / h / 100);
506 int max_block_x = (int64_t)(w - block_x_orig) *
507 OVERSAMPLE * config.magnitude / 100;
508 int max_block_y = (int64_t)(h - block_y_orig) *
509 OVERSAMPLE * config.magnitude / 100;
510 int min_block_x = (int64_t)-block_x_orig *
511 OVERSAMPLE * config.magnitude / 100;
512 int min_block_y = (int64_t)-block_y_orig *
513 OVERSAMPLE * config.magnitude / 100;
515 CLAMP(total_dx, min_block_x, max_block_x);
516 CLAMP(total_dy, min_block_y, max_block_y);
519 // printf("MotionHVMain::process_global %d total_dx=%d total_dy=%d\n",
520 // __LINE__, total_dx, total_dy);
522 if(config.tracking_object != MotionHVScan::TRACK_SINGLE && !config.rotate)
524 // Transfer current reference frame to previous reference frame and update
525 // counter. Must wait for rotate to compare.
526 prev_global_ref->copy_from(current_global_ref);
527 previous_frame_number = get_source_position();
530 // Decide what to do with target based on requested operation
531 int interpolation = NEAREST_NEIGHBOR;
534 switch(config.action_type)
536 case MotionHVScan::NOTHING:
537 global_target_dst->copy_from(global_target_src);
539 case MotionHVScan::TRACK_PIXEL:
540 interpolation = NEAREST_NEIGHBOR;
541 dx = (int)(total_dx / OVERSAMPLE);
542 dy = (int)(total_dy / OVERSAMPLE);
544 case MotionHVScan::STABILIZE_PIXEL:
545 interpolation = NEAREST_NEIGHBOR;
546 dx = -(int)(total_dx / OVERSAMPLE);
547 dy = -(int)(total_dy / OVERSAMPLE);
549 case MotionHVScan::TRACK:
550 interpolation = CUBIC_LINEAR;
551 dx = (float)total_dx / OVERSAMPLE;
552 dy = (float)total_dy / OVERSAMPLE;
554 case MotionHVScan::STABILIZE:
555 interpolation = CUBIC_LINEAR;
556 dx = -(float)total_dx / OVERSAMPLE;
557 dy = -(float)total_dy / OVERSAMPLE;
562 if(config.action_type != MotionHVScan::NOTHING)
565 overlayer = new OverlayFrame(PluginClient::get_project_smp() + 1);
566 global_target_dst->clear_frame();
567 overlayer->overlay(global_target_dst,
571 global_target_src->get_w(),
572 global_target_src->get_h(),
575 (float)global_target_src->get_w() + dx,
576 (float)global_target_src->get_h() + dy,
585 void MotionHVMain::process_rotation()
590 // Always require global
591 // Convert the previous global reference into the previous rotation reference.
592 // Convert global target destination into rotation target source.
597 overlayer = new OverlayFrame(PluginClient::get_project_smp() + 1);
600 if(config.tracking_object == MotionHVScan::TRACK_SINGLE)
602 dx = (float)total_dx / OVERSAMPLE;
603 dy = (float)total_dy / OVERSAMPLE;
607 dx = (float)current_dx / OVERSAMPLE;
608 dy = (float)current_dy / OVERSAMPLE;
611 prev_rotate_ref->clear_frame();
612 overlayer->overlay(prev_rotate_ref,
616 prev_global_ref->get_w(),
617 prev_global_ref->get_h(),
620 (float)prev_global_ref->get_w() + dx,
621 (float)prev_global_ref->get_h() + dy,
625 // Pivot is destination global position
626 block_x = (int)(prev_rotate_ref->get_w() *
631 block_y = (int)(prev_rotate_ref->get_h() *
636 // Use the global target output as the rotation target input
637 rotate_target_src->copy_from(global_target_dst);
638 // Transfer current reference frame to previous reference frame for global.
639 if(config.tracking_object != MotionHVScan::TRACK_SINGLE)
641 prev_global_ref->copy_from(current_global_ref);
642 previous_frame_number = get_source_position();
648 block_x = (int)(prev_rotate_ref->get_w() *
651 block_y = (int)(prev_rotate_ref->get_h() *
659 // if(!motion_rotate)
660 // motion_rotate = new RotateScan(this,
661 // get_project_smp() + 1,
662 // get_project_smp() + 1);
664 // current_angle = motion_rotate->scan_frame(prev_rotate_ref,
665 // current_rotate_ref,
669 current_angle = engine->dr_result;
671 // Add current rotation to accumulation
672 if(config.tracking_object != MotionHVScan::TRACK_SINGLE)
675 total_angle = total_angle * (100 - config.rotate_return_speed) / 100;
676 // Accumulate current rotation
677 total_angle += current_angle;
679 // Clamp rotation accumulation
680 if(config.rotate_magnitude < 90)
682 CLAMP(total_angle, -config.rotate_magnitude, config.rotate_magnitude);
685 // if(!config.global)
687 // Transfer current reference frame to previous reference frame and update
689 // prev_rotate_ref->copy_from(current_rotate_ref);
690 // previous_frame_number = get_source_position();
695 total_angle = current_angle;
699 printf("MotionHVMain::process_rotation total_angle=%f\n", total_angle);
703 // Calculate rotation parameters based on requested operation
705 switch(config.action_type)
707 case MotionHVScan::NOTHING:
708 rotate_target_dst->copy_from(rotate_target_src);
710 case MotionHVScan::TRACK:
711 case MotionHVScan::TRACK_PIXEL:
714 case MotionHVScan::STABILIZE:
715 case MotionHVScan::STABILIZE_PIXEL:
716 angle = -total_angle;
722 if(config.action_type != MotionHVScan::NOTHING)
725 rotate_engine = new AffineEngine(PluginClient::get_project_smp() + 1,
726 PluginClient::get_project_smp() + 1);
728 rotate_target_dst->clear_frame();
730 // Determine pivot based on a number of factors.
731 switch(config.action_type)
733 case MotionHVScan::TRACK:
734 case MotionHVScan::TRACK_PIXEL:
735 // Use destination of global tracking.
736 // rotate_engine->set_pivot(block_x, block_y);
737 rotate_engine->set_in_pivot(block_x, block_y);
738 rotate_engine->set_out_pivot(block_x, block_y);
741 case MotionHVScan::STABILIZE:
742 case MotionHVScan::STABILIZE_PIXEL:
746 // Use origin of global stabilize operation
747 // rotate_engine->set_pivot((int)(rotate_target_dst->get_w() *
750 // (int)(rotate_target_dst->get_h() *
753 rotate_engine->set_in_pivot((int)(rotate_target_dst->get_w() *
756 (int)(rotate_target_dst->get_h() *
759 rotate_engine->set_out_pivot((int)(rotate_target_dst->get_w() *
762 (int)(rotate_target_dst->get_h() *
770 // rotate_engine->set_pivot(block_x, block_y);
771 rotate_engine->set_in_pivot(block_x, block_y);
772 rotate_engine->set_out_pivot(block_x, block_y);
778 printf("MotionHVMain::process_rotation angle=%f\n", angle);
779 rotate_engine->rotate(rotate_target_dst, rotate_target_src, angle);
780 // overlayer->overlay(rotate_target_dst,
784 // prev_rotate_ref->get_w(),
785 // prev_rotate_ref->get_h(),
788 // prev_rotate_ref->get_w(),
789 // prev_rotate_ref->get_h(),
793 // overlayer->overlay(rotate_target_dst,
794 // current_rotate_ref,
797 // prev_rotate_ref->get_w(),
798 // prev_rotate_ref->get_h(),
801 // prev_rotate_ref->get_w(),
802 // prev_rotate_ref->get_h(),
821 int MotionHVMain::process_buffer(VFrame **frame,
822 int64_t start_position,
825 int need_reconfigure = load_configuration();
826 int color_model = frame[0]->get_color_model();
827 w = frame[0]->get_w();
828 h = frame[0]->get_h();
832 printf("MotionHVMain::process_buffer %d start_position=%lld\n", __LINE__, start_position);
836 // Calculate the source and destination pointers for each of the operations.
837 // Get the layer to track motion in.
838 reference_layer = config.bottom_is_master ?
839 PluginClient::total_in_buffers - 1 :
841 // Get the layer to apply motion in.
842 target_layer = config.bottom_is_master ?
844 PluginClient::total_in_buffers - 1;
847 output_frame = frame[target_layer];
850 // Get the position of previous reference frame.
851 int64_t actual_previous_number;
852 // Skip if match frame not available
853 int skip_current = 0;
856 if(config.tracking_object == MotionHVScan::TRACK_SINGLE)
858 actual_previous_number = config.track_frame;
859 if(get_direction() == PLAY_REVERSE)
860 actual_previous_number++;
861 if(actual_previous_number == start_position)
866 actual_previous_number = start_position;
867 if(get_direction() == PLAY_FORWARD)
869 actual_previous_number--;
870 if(actual_previous_number < get_source_start())
874 KeyFrame *keyframe = get_prev_keyframe(start_position, 1);
875 if(keyframe->position > 0 &&
876 actual_previous_number < keyframe->position)
882 actual_previous_number++;
883 if(actual_previous_number >= get_source_start() + get_total_len())
887 KeyFrame *keyframe = get_next_keyframe(start_position, 1);
888 if(keyframe->position > 0 &&
889 actual_previous_number >= keyframe->position)
894 // Only count motion since last keyframe
900 // if(!config.global && !config.rotate) skip_current = 1;
905 // printf("process_realtime %d %lld %lld\n",
907 // previous_frame_number,
908 // actual_previous_number);
909 // Load match frame and reset vectors
910 int need_reload = !skip_current &&
911 (previous_frame_number != actual_previous_number ||
918 previous_frame_number = actual_previous_number;
935 // Get the global pointers. Here we walk through the sequence of events.
939 // Assume global only. Global reads previous frame and compares
940 // with current frame to get the current translation.
941 // The center of the search area is fixed in compensate mode or
942 // the user value + the accumulation vector in track mode.
944 prev_global_ref = new VFrame(w, h, color_model);
945 if(!current_global_ref)
946 current_global_ref = new VFrame(w, h, color_model);
948 // Global loads the current target frame into the src and
949 // writes it to the dst frame with desired translation.
950 if(!global_target_src)
951 global_target_src = new VFrame(w, h, color_model);
952 if(!global_target_dst)
953 global_target_dst = new VFrame(w, h, color_model);
956 // Load the global frames
959 read_frame(prev_global_ref,
961 previous_frame_number,
966 read_frame(current_global_ref,
971 read_frame(global_target_src,
979 // Global followed by rotate
982 // Must translate the previous global reference by the current global
983 // accumulation vector to match the current global reference.
984 // The center of the search area is always the user value + the accumulation
987 prev_rotate_ref = new VFrame(w, h, color_model);
988 // The current global reference is the current rotation reference.
989 if(!current_rotate_ref)
990 current_rotate_ref = new VFrame(w, h, color_model);
991 current_rotate_ref->copy_from(current_global_ref);
993 // The global target destination is copied to the rotation target source
994 // then written to the rotation output with rotation.
995 // The pivot for the rotation is the center of the search area
996 // if we're tracking.
997 // The pivot is fixed to the user position if we're compensating.
998 if(!rotate_target_src)
999 rotate_target_src = new VFrame(w, h, color_model);
1000 if(!rotate_target_dst)
1001 rotate_target_dst = new VFrame(w, h, color_model);
1008 // Rotation reads the previous reference frame and compares it with current
1010 if(!prev_rotate_ref)
1011 prev_rotate_ref = new VFrame(w, h, color_model);
1012 if(!current_rotate_ref)
1013 current_rotate_ref = new VFrame(w, h, color_model);
1015 // Rotation loads target frame to temporary, rotates it, and writes it to the
1016 // target frame. The pivot is always fixed.
1017 if(!rotate_target_src)
1018 rotate_target_src = new VFrame(w, h, color_model);
1019 if(!rotate_target_dst)
1020 rotate_target_dst = new VFrame(w, h, color_model);
1023 // Load the rotate frames
1026 read_frame(prev_rotate_ref,
1028 previous_frame_number,
1032 read_frame(current_rotate_ref,
1037 read_frame(rotate_target_src,
1051 //printf("skip_current=%d config.global=%d\n", skip_current, config.global);
1056 // Get position change from previous frame to current frame
1057 /* if(config.global) */ process_global();
1058 // Get rotation change from previous frame to current frame
1059 if(config.rotate) process_rotation();
1060 //frame[target_layer]->copy_from(prev_rotate_ref);
1061 //frame[target_layer]->copy_from(current_rotate_ref);
1069 // Transfer the relevant target frame to the output
1074 frame[target_layer]->copy_from(rotate_target_dst);
1078 frame[target_layer]->copy_from(global_target_dst);
1082 // Read the target destination directly
1084 read_frame(frame[target_layer],
1091 if(config.draw_vectors)
1093 draw_vectors(frame[target_layer]);
1097 printf("MotionHVMain::process_buffer %d\n", __LINE__);
1104 void MotionHVMain::draw_vectors(VFrame *frame)
1106 int w = frame->get_w();
1107 int h = frame->get_h();
1108 int global_x1, global_y1;
1109 int global_x2, global_y2;
1110 int block_x, block_y;
1111 int block_w, block_h;
1112 int block_x1, block_y1;
1113 int block_x2, block_y2;
1114 int block_x3, block_y3;
1115 int block_x4, block_y4;
1116 int search_w, search_h;
1117 int search_x1, search_y1;
1118 int search_x2, search_y2;
1121 // always processing global
1122 // if(config.global)
1126 // Start of vector is center of previous block.
1127 // End of vector is total accumulation.
1128 if(config.tracking_object == MotionHVScan::TRACK_SINGLE)
1130 global_x1 = (int64_t)(config.block_x *
1133 global_y1 = (int64_t)(config.block_y *
1136 global_x2 = global_x1 + total_dx / OVERSAMPLE;
1137 global_y2 = global_y1 + total_dy / OVERSAMPLE;
1138 //printf("MotionHVMain::draw_vectors %d %d %d %d %d %d\n", total_dx, total_dy, global_x1, global_y1, global_x2, global_y2);
1141 // Start of vector is center of previous block.
1142 // End of vector is current change.
1143 if(config.tracking_object == MotionHVScan::PREVIOUS_SAME_BLOCK)
1145 global_x1 = (int64_t)(config.block_x *
1148 global_y1 = (int64_t)(config.block_y *
1151 global_x2 = global_x1 + current_dx / OVERSAMPLE;
1152 global_y2 = global_y1 + current_dy / OVERSAMPLE;
1156 global_x1 = (int64_t)(config.block_x *
1159 (total_dx - current_dx) /
1161 global_y1 = (int64_t)(config.block_y *
1164 (total_dy - current_dy) /
1166 global_x2 = (int64_t)(config.block_x *
1171 global_y2 = (int64_t)(config.block_y *
1178 block_x = global_x1;
1179 block_y = global_y1;
1180 block_w = config.global_block_w * w / 100;
1181 block_h = config.global_block_h * h / 100;
1182 block_x1 = block_x - block_w / 2;
1183 block_y1 = block_y - block_h / 2;
1184 block_x2 = block_x + block_w / 2;
1185 block_y2 = block_y + block_h / 2;
1186 search_w = config.global_range_w * w / 100;
1187 search_h = config.global_range_h * h / 100;
1188 search_x1 = block_x1 - search_w / 2;
1189 search_y1 = block_y1 - search_h / 2;
1190 search_x2 = block_x2 + search_w / 2;
1191 search_y2 = block_y2 + search_h / 2;
1193 // printf("MotionHVMain::draw_vectors %d %d %d %d %d %d %d %d %d %d %d %d\n",
1207 MotionHVScan::clamp_scan(w,
1220 draw_arrow(frame, global_x1, global_y1, global_x2, global_y2);
1223 draw_line(frame, block_x1, block_y1, block_x2, block_y1);
1224 draw_line(frame, block_x2, block_y1, block_x2, block_y2);
1225 draw_line(frame, block_x2, block_y2, block_x1, block_y2);
1226 draw_line(frame, block_x1, block_y2, block_x1, block_y1);
1230 draw_line(frame, search_x1, search_y1, search_x2, search_y1);
1231 draw_line(frame, search_x2, search_y1, search_x2, search_y2);
1232 draw_line(frame, search_x2, search_y2, search_x1, search_y2);
1233 draw_line(frame, search_x1, search_y2, search_x1, search_y1);
1235 // Block should be endpoint of motion
1238 block_x = global_x2;
1239 block_y = global_y2;
1244 block_x = (int64_t)(config.block_x * w / 100);
1245 block_y = (int64_t)(config.block_y * h / 100);
1248 block_w = config.global_block_w * w / 100;
1249 block_h = config.global_block_h * h / 100;
1252 float angle = total_angle * 2 * M_PI / 360;
1253 double base_angle1 = atan((float)block_h / block_w);
1254 double base_angle2 = atan((float)block_w / block_h);
1255 double target_angle1 = base_angle1 + angle;
1256 double target_angle2 = base_angle2 + angle;
1257 double radius = sqrt(block_w * block_w + block_h * block_h) / 2;
1258 block_x1 = (int)(block_x - cos(target_angle1) * radius);
1259 block_y1 = (int)(block_y - sin(target_angle1) * radius);
1260 block_x2 = (int)(block_x + sin(target_angle2) * radius);
1261 block_y2 = (int)(block_y - cos(target_angle2) * radius);
1262 block_x3 = (int)(block_x - sin(target_angle2) * radius);
1263 block_y3 = (int)(block_y + cos(target_angle2) * radius);
1264 block_x4 = (int)(block_x + cos(target_angle1) * radius);
1265 block_y4 = (int)(block_y + sin(target_angle1) * radius);
1267 draw_line(frame, block_x1, block_y1, block_x2, block_y2);
1268 draw_line(frame, block_x2, block_y2, block_x4, block_y4);
1269 draw_line(frame, block_x4, block_y4, block_x3, block_y3);
1270 draw_line(frame, block_x3, block_y3, block_x1, block_y1);
1276 draw_line(frame, block_x, block_y - 5, block_x, block_y + 6);
1277 draw_line(frame, block_x - 5, block_y, block_x + 6, block_y);
1284 void MotionHVMain::draw_pixel(VFrame *frame, int x, int y)
1286 if(!(x >= 0 && y >= 0 && x < frame->get_w() && y < frame->get_h())) return;
1288 #define DRAW_PIXEL(x, y, components, do_yuv, max, type) \
1290 type **rows = (type**)frame->get_rows(); \
1291 rows[y][x * components] = max - rows[y][x * components]; \
1294 rows[y][x * components + 1] = max - rows[y][x * components + 1]; \
1295 rows[y][x * components + 2] = max - rows[y][x * components + 2]; \
1299 rows[y][x * components + 1] = (max / 2 + 1) - rows[y][x * components + 1]; \
1300 rows[y][x * components + 2] = (max / 2 + 1) - rows[y][x * components + 2]; \
1302 if(components == 4) \
1303 rows[y][x * components + 3] = max; \
1307 switch(frame->get_color_model())
1310 DRAW_PIXEL(x, y, 3, 0, 0xff, unsigned char);
1313 DRAW_PIXEL(x, y, 4, 0, 0xff, unsigned char);
1316 DRAW_PIXEL(x, y, 3, 0, 1.0, float);
1319 DRAW_PIXEL(x, y, 4, 0, 1.0, float);
1322 DRAW_PIXEL(x, y, 3, 1, 0xff, unsigned char);
1325 DRAW_PIXEL(x, y, 4, 1, 0xff, unsigned char);
1328 DRAW_PIXEL(x, y, 3, 0, 0xffff, uint16_t);
1331 DRAW_PIXEL(x, y, 3, 1, 0xffff, uint16_t);
1333 case BC_RGBA16161616:
1334 DRAW_PIXEL(x, y, 4, 0, 0xffff, uint16_t);
1336 case BC_YUVA16161616:
1337 DRAW_PIXEL(x, y, 4, 1, 0xffff, uint16_t);
1343 void MotionHVMain::draw_line(VFrame *frame, int x1, int y1, int x2, int y2)
1345 int w = labs(x2 - x1);
1346 int h = labs(y2 - y1);
1347 //printf("MotionHVMain::draw_line 1 %d %d %d %d\n", x1, y1, x2, y2);
1351 draw_pixel(frame, x1, y1);
1356 // Flip coordinates so x1 < x2
1366 int numerator = y2 - y1;
1367 int denominator = x2 - x1;
1368 for(int i = x1; i < x2; i++)
1370 int y = y1 + (int64_t)(i - x1) * (int64_t)numerator / (int64_t)denominator;
1371 draw_pixel(frame, i, y);
1376 // Flip coordinates so y1 < y2
1386 int numerator = x2 - x1;
1387 int denominator = y2 - y1;
1388 for(int i = y1; i < y2; i++)
1390 int x = x1 + (int64_t)(i - y1) * (int64_t)numerator / (int64_t)denominator;
1391 draw_pixel(frame, x, i);
1394 //printf("MotionHVMain::draw_line 2\n");
1397 #define ARROW_SIZE 10
1398 void MotionHVMain::draw_arrow(VFrame *frame, int x1, int y1, int x2, int y2)
1400 double angle = atan((float)(y2 - y1) / (float)(x2 - x1));
1401 double angle1 = angle + (float)145 / 360 * 2 * 3.14159265;
1402 double angle2 = angle - (float)145 / 360 * 2 * 3.14159265;
1409 x3 = x2 - (int)(ARROW_SIZE * cos(angle1));
1410 y3 = y2 - (int)(ARROW_SIZE * sin(angle1));
1411 x4 = x2 - (int)(ARROW_SIZE * cos(angle2));
1412 y4 = y2 - (int)(ARROW_SIZE * sin(angle2));
1416 x3 = x2 + (int)(ARROW_SIZE * cos(angle1));
1417 y3 = y2 + (int)(ARROW_SIZE * sin(angle1));
1418 x4 = x2 + (int)(ARROW_SIZE * cos(angle2));
1419 y4 = y2 + (int)(ARROW_SIZE * sin(angle2));
1423 draw_line(frame, x1, y1, x2, y2);
1424 // draw_line(frame, x1, y1 + 1, x2, y2 + 1);
1427 if(abs(y2 - y1) || abs(x2 - x1)) draw_line(frame, x2, y2, x3, y3);
1428 // draw_line(frame, x2, y2 + 1, x3, y3 + 1);
1430 if(abs(y2 - y1) || abs(x2 - x1)) draw_line(frame, x2, y2, x4, y4);
1431 // draw_line(frame, x2, y2 + 1, x4, y4 + 1);