4 * Copyright (C) 2012 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 "mainerror.h"
32 #include "motionscan.h"
33 #include "motionwindow.h"
35 #include "overlayframe.h"
36 #include "rotateframe.h"
37 #include "transportque.h"
43 REGISTER_PLUGIN(MotionMain)
48 MotionConfig::MotionConfig()
50 global_range_w = 25; //5;
51 global_range_h = 25; //5;
52 rotation_range = 8; //5;
55 global_block_w = 33; //MIN_BLOCK;
56 global_block_h = 33; //MIN_BLOCK;
59 global_positions = 256;
60 rotate_positions = 8; // 4;
62 rotate_magnitude = 30;
63 return_speed = 5; //0;
64 rotate_return_speed = 5; //0;
65 action_type = MotionScan::STABILIZE;
68 addtrackedframeoffset = 0;
69 strcpy(tracking_file, TRACKING_FILE);
70 tracking_type = MotionScan::SAVE; //MotionScan::NO_CALCULATE;
71 tracking_object = MotionScan::TRACK_PREVIOUS; //TRACK_SINGLE;
72 draw_vectors = 1; //0;
80 void MotionConfig::boundaries()
82 CLAMP(global_range_w, MIN_RADIUS, MAX_RADIUS);
83 CLAMP(global_range_h, MIN_RADIUS, MAX_RADIUS);
84 CLAMP(rotation_range, MIN_ROTATION, MAX_ROTATION);
85 CLAMP(rotation_center, -MAX_ROTATION, MAX_ROTATION);
86 CLAMP(block_count, MIN_BLOCKS, MAX_BLOCKS);
87 CLAMP(global_block_w, MIN_BLOCK, MAX_BLOCK);
88 CLAMP(global_block_h, MIN_BLOCK, MAX_BLOCK);
91 int MotionConfig::equivalent(MotionConfig &that)
93 return global_range_w == that.global_range_w &&
94 global_range_h == that.global_range_h &&
95 rotation_range == that.rotation_range &&
96 rotation_center == that.rotation_center &&
97 action_type == that.action_type &&
98 global == that.global && rotate == that.rotate &&
99 addtrackedframeoffset == that.addtrackedframeoffset &&
100 draw_vectors == that.draw_vectors &&
101 block_count == that.block_count &&
102 global_block_w == that.global_block_w &&
103 global_block_h == that.global_block_h &&
104 EQUIV(block_x, that.block_x) &&
105 EQUIV(block_y, that.block_y) &&
106 global_positions == that.global_positions &&
107 rotate_positions == that.rotate_positions &&
108 magnitude == that.magnitude &&
109 return_speed == that.return_speed &&
110 rotate_return_speed == that.rotate_return_speed &&
111 rotate_magnitude == that.rotate_magnitude &&
112 tracking_object == that.tracking_object &&
113 track_frame == that.track_frame &&
114 bottom_is_master == that.bottom_is_master &&
115 horizontal_only == that.horizontal_only &&
116 vertical_only == that.vertical_only;
119 void MotionConfig::copy_from(MotionConfig &that)
121 global_range_w = that.global_range_w;
122 global_range_h = that.global_range_h;
123 rotation_range = that.rotation_range;
124 rotation_center = that.rotation_center;
125 action_type = that.action_type;
126 global = that.global;
127 rotate = that.rotate;
128 addtrackedframeoffset = that.addtrackedframeoffset;
129 tracking_type = that.tracking_type;
130 draw_vectors = that.draw_vectors;
131 block_count = that.block_count;
132 block_x = that.block_x;
133 block_y = that.block_y;
134 global_positions = that.global_positions;
135 rotate_positions = that.rotate_positions;
136 global_block_w = that.global_block_w;
137 global_block_h = that.global_block_h;
138 magnitude = that.magnitude;
139 return_speed = that.return_speed;
140 rotate_magnitude = that.rotate_magnitude;
141 rotate_return_speed = that.rotate_return_speed;
142 tracking_object = that.tracking_object;
143 track_frame = that.track_frame;
144 bottom_is_master = that.bottom_is_master;
145 horizontal_only = that.horizontal_only;
146 vertical_only = that.vertical_only;
149 void MotionConfig::interpolate(MotionConfig &prev, MotionConfig &next,
150 int64_t prev_frame, int64_t next_frame, int64_t current_frame)
156 MotionMain::MotionMain(PluginServer *server)
157 : PluginVClient(server)
169 previous_frame_number = -1;
172 current_global_ref = 0;
173 global_target_src = 0;
174 global_target_dst = 0;
177 cache_fp = active_fp = 0;
179 cache_key = active_key = -1;
180 dx_offset = dy_offset = 0;
182 save_dx = load_dx = 0;
183 save_dy = load_dy = 0;
184 save_dt = load_dt = 0;
187 current_rotate_ref = 0;
188 rotate_target_src = 0;
189 rotate_target_dst = 0;
192 MotionMain::~MotionMain()
197 delete [] search_area;
199 delete rotate_engine;
200 delete motion_rotate;
202 delete prev_global_ref;
203 delete current_global_ref;
204 delete global_target_src;
205 delete global_target_dst;
209 delete prev_rotate_ref;
210 delete current_rotate_ref;
211 delete rotate_target_src;
212 delete rotate_target_dst;
215 const char* MotionMain::plugin_title() { return N_("Motion"); }
216 int MotionMain::is_realtime() { return 1; }
217 int MotionMain::is_multichannel() { return 1; }
220 NEW_WINDOW_MACRO(MotionMain, MotionWindow)
222 LOAD_CONFIGURATION_MACRO(MotionMain, MotionConfig)
226 void MotionMain::update_gui()
228 if( !thread ) return;
229 if( !load_configuration() ) return;
230 thread->window->lock_window("MotionMain::update_gui");
231 MotionWindow *window = (MotionWindow*)thread->window;
233 char string[BCTEXTLEN];
234 sprintf(string, "%d", config.global_positions);
235 window->global_search_positions->set_text(string);
236 sprintf(string, "%d", config.rotate_positions);
237 window->rotation_search_positions->set_text(string);
239 window->global_block_w->update(config.global_block_w);
240 window->global_block_h->update(config.global_block_h);
241 window->block_x->update(config.block_x);
242 window->block_y->update(config.block_y);
243 window->block_x_text->update((float)config.block_x);
244 window->block_y_text->update((float)config.block_y);
245 window->magnitude->update(config.magnitude);
246 window->return_speed->update(config.return_speed);
247 window->rotate_magnitude->update(config.rotate_magnitude);
248 window->rotate_return_speed->update(config.rotate_return_speed);
249 window->rotation_range->update(config.rotation_range);
250 window->rotation_center->update(config.rotation_center);
253 window->track_single->update(config.tracking_object == MotionScan::TRACK_SINGLE);
254 window->track_frame_number->update(config.track_frame);
255 window->track_previous->update(config.tracking_object == MotionScan::TRACK_PREVIOUS);
256 window->previous_same->update(config.tracking_object == MotionScan::PREVIOUS_SAME_BLOCK);
257 if( config.tracking_object != MotionScan::TRACK_SINGLE )
258 window->track_frame_number->disable();
260 window->track_frame_number->enable();
262 window->action_type->set_text(
263 ActionType::to_text(config.action_type));
264 window->tracking_type->set_text(
265 TrackingType::to_text(config.tracking_type));
266 window->track_direction->set_text(
267 TrackDirection::to_text(config.horizontal_only, config.vertical_only));
268 window->master_layer->set_text(
269 MasterLayer::to_text(config.bottom_is_master));
271 window->update_mode();
272 thread->window->unlock_window();
278 void MotionMain::save_data(KeyFrame *keyframe)
282 // cause data to be stored directly in text
283 output.set_shared_output(keyframe->xbuf);
284 output.tag.set_title("MOTION");
286 output.tag.set_property("BLOCK_COUNT", config.block_count);
287 output.tag.set_property("GLOBAL_POSITIONS", config.global_positions);
288 output.tag.set_property("ROTATE_POSITIONS", config.rotate_positions);
289 output.tag.set_property("GLOBAL_BLOCK_W", config.global_block_w);
290 output.tag.set_property("GLOBAL_BLOCK_H", config.global_block_h);
291 output.tag.set_property("BLOCK_X", config.block_x);
292 output.tag.set_property("BLOCK_Y", config.block_y);
293 output.tag.set_property("GLOBAL_RANGE_W", config.global_range_w);
294 output.tag.set_property("GLOBAL_RANGE_H", config.global_range_h);
295 output.tag.set_property("ROTATION_RANGE", config.rotation_range);
296 output.tag.set_property("ROTATION_CENTER", config.rotation_center);
297 output.tag.set_property("MAGNITUDE", config.magnitude);
298 output.tag.set_property("RETURN_SPEED", config.return_speed);
299 output.tag.set_property("ROTATE_MAGNITUDE", config.rotate_magnitude);
300 output.tag.set_property("ROTATE_RETURN_SPEED", config.rotate_return_speed);
301 output.tag.set_property("ACTION_TYPE", config.action_type);
302 output.tag.set_property("GLOBAL", config.global);
303 output.tag.set_property("ROTATE", config.rotate);
304 output.tag.set_property("ADDTRACKEDFRAMEOFFSET", config.addtrackedframeoffset);
305 output.tag.set_property("TRACKING_FILE", config.tracking_file);
306 output.tag.set_property("TRACKING_TYPE", config.tracking_type);
307 output.tag.set_property("DRAW_VECTORS", config.draw_vectors);
308 output.tag.set_property("TRACKING_OBJECT", config.tracking_object);
309 output.tag.set_property("TRACK_FRAME", config.track_frame);
310 output.tag.set_property("BOTTOM_IS_MASTER", config.bottom_is_master);
311 output.tag.set_property("HORIZONTAL_ONLY", config.horizontal_only);
312 output.tag.set_property("VERTICAL_ONLY", config.vertical_only);
314 output.tag.set_title("/MOTION");
316 output.terminate_string();
319 void MotionMain::read_data(KeyFrame *keyframe)
322 input.set_shared_input(keyframe->xbuf);
325 while( !(result = input.read_tag()) ) {
326 if( input.tag.title_is("MOTION") ) {
327 config.block_count = input.tag.get_property("BLOCK_COUNT", config.block_count);
328 config.global_positions = input.tag.get_property("GLOBAL_POSITIONS", config.global_positions);
329 config.rotate_positions = input.tag.get_property("ROTATE_POSITIONS", config.rotate_positions);
330 config.global_block_w = input.tag.get_property("GLOBAL_BLOCK_W", config.global_block_w);
331 config.global_block_h = input.tag.get_property("GLOBAL_BLOCK_H", config.global_block_h);
332 config.block_x = input.tag.get_property("BLOCK_X", config.block_x);
333 config.block_y = input.tag.get_property("BLOCK_Y", config.block_y);
334 config.global_range_w = input.tag.get_property("GLOBAL_RANGE_W", config.global_range_w);
335 config.global_range_h = input.tag.get_property("GLOBAL_RANGE_H", config.global_range_h);
336 config.rotation_range = input.tag.get_property("ROTATION_RANGE", config.rotation_range);
337 config.rotation_center = input.tag.get_property("ROTATION_CENTER", config.rotation_center);
338 config.magnitude = input.tag.get_property("MAGNITUDE", config.magnitude);
339 config.return_speed = input.tag.get_property("RETURN_SPEED", config.return_speed);
340 config.rotate_magnitude = input.tag.get_property("ROTATE_MAGNITUDE", config.rotate_magnitude);
341 config.rotate_return_speed = input.tag.get_property("ROTATE_RETURN_SPEED", config.rotate_return_speed);
342 config.action_type = input.tag.get_property("ACTION_TYPE", config.action_type);
343 config.global = input.tag.get_property("GLOBAL", config.global);
344 config.rotate = input.tag.get_property("ROTATE", config.rotate);
345 config.addtrackedframeoffset = input.tag.get_property("ADDTRACKEDFRAMEOFFSET", config.addtrackedframeoffset);
346 input.tag.get_property("TRACKING_FILE", config.tracking_file);
347 config.tracking_type = input.tag.get_property("TRACKING_TYPE", config.tracking_type);
348 config.draw_vectors = input.tag.get_property("DRAW_VECTORS", config.draw_vectors);
349 config.tracking_object = input.tag.get_property("TRACKING_OBJECT", config.tracking_object);
350 config.track_frame = input.tag.get_property("TRACK_FRAME", config.track_frame);
351 config.bottom_is_master = input.tag.get_property("BOTTOM_IS_MASTER", config.bottom_is_master);
352 config.horizontal_only = input.tag.get_property("HORIZONTAL_ONLY", config.horizontal_only);
353 config.vertical_only = input.tag.get_property("VERTICAL_ONLY", config.vertical_only);
359 void MotionMain::allocate_temp(int w, int h, int color_model)
362 ( temp_frame->get_w() != w || temp_frame->get_h() != h ) ) {
367 temp_frame = new VFrame(w, h, color_model, 0);
370 void MotionMain::process_global()
373 if( !engine ) engine = new MotionScan(PluginClient::get_project_smp() + 1,
374 PluginClient::get_project_smp() + 1);
376 // Determine if frames changed
377 engine->scan_frame(current_global_ref, prev_global_ref,
378 config.global_range_w, config.global_range_h,
379 config.global_block_w, config.global_block_h,
380 config.block_x, config.block_y,
381 config.tracking_object, config.tracking_type,
382 config.action_type, config.horizontal_only,
383 config.vertical_only, get_source_position(),
384 config.global_positions, total_dx, total_dy,
385 0, 0, load_ok, load_dx, load_dy);
386 current_dx = (engine->dx_result += dx_offset);
387 current_dy = (engine->dy_result += dy_offset);
390 if( config.tracking_type == MotionScan::SAVE ) {
391 save_dx = engine->dx_result;
392 save_dy = engine->dy_result;
395 // Add current motion vector to accumulation vector.
396 if( config.tracking_object != MotionScan::TRACK_SINGLE ) {
398 total_dx = (int64_t)total_dx * (100 - config.return_speed) / 100;
399 total_dy = (int64_t)total_dy * (100 - config.return_speed) / 100;
400 total_dx += engine->dx_result;
401 total_dy += engine->dy_result;
402 // printf("MotionMain::process_global total_dx=%d engine->dx_result=%d\n",
403 // total_dx, engine->dx_result);
406 // Make accumulation vector current
407 total_dx = engine->dx_result;
408 total_dy = engine->dy_result;
411 // Clamp accumulation vector
412 if( config.magnitude < 100 ) {
413 int block_x_orig = (int64_t)(config.block_x * current_global_ref->get_w() / 100);
414 int block_y_orig = (int64_t)(config.block_y * current_global_ref->get_h() / 100);
415 int max_block_x = (int64_t)(current_global_ref->get_w() - block_x_orig)
416 * OVERSAMPLE * config.magnitude / 100;
417 int max_block_y = (int64_t)(current_global_ref->get_h() - block_y_orig)
418 * OVERSAMPLE * config.magnitude / 100;
419 int min_block_x = (int64_t)-block_x_orig
420 * OVERSAMPLE * config.magnitude / 100;
421 int min_block_y = (int64_t)-block_y_orig
422 * OVERSAMPLE * config.magnitude / 100;
424 CLAMP(total_dx, min_block_x, max_block_x);
425 CLAMP(total_dy, min_block_y, max_block_y);
429 printf("MotionMain::process_global 2 total_dx=%.02f total_dy=%.02f\n",
430 (float)total_dx / OVERSAMPLE, (float)total_dy / OVERSAMPLE);
433 if( config.tracking_object != MotionScan::TRACK_SINGLE && !config.rotate ) {
434 // Transfer current reference frame to previous reference frame and update
435 // counter. Must wait for rotate to compare.
436 prev_global_ref->copy_from(current_global_ref);
437 previous_frame_number = get_source_position();
440 // Decide what to do with target based on requested operation
441 int interpolation = NEAREST_NEIGHBOR;
442 float dx = 0., dy = 0.;
443 switch(config.action_type) {
444 case MotionScan::NOTHING:
445 global_target_dst->copy_from(global_target_src);
447 case MotionScan::TRACK_PIXEL:
448 interpolation = NEAREST_NEIGHBOR;
449 dx = (int)(total_dx / OVERSAMPLE);
450 dy = (int)(total_dy / OVERSAMPLE);
452 case MotionScan::STABILIZE_PIXEL:
453 interpolation = NEAREST_NEIGHBOR;
454 dx = -(int)(total_dx / OVERSAMPLE);
455 dy = -(int)(total_dy / OVERSAMPLE);
457 case MotionScan::TRACK:
458 interpolation = CUBIC_LINEAR;
459 dx = (float)total_dx / OVERSAMPLE;
460 dy = (float)total_dy / OVERSAMPLE;
462 case MotionScan::STABILIZE:
463 interpolation = CUBIC_LINEAR;
464 dx = -(float)total_dx / OVERSAMPLE;
465 dy = -(float)total_dy / OVERSAMPLE;
470 if( config.action_type != MotionScan::NOTHING ) {
472 overlayer = new OverlayFrame(PluginClient::get_project_smp() + 1);
473 global_target_dst->clear_frame();
474 overlayer->overlay(global_target_dst, global_target_src,
475 0, 0, global_target_src->get_w(), global_target_src->get_h(),
477 (float)global_target_src->get_w() + dx,
478 (float)global_target_src->get_h() + dy,
479 1, TRANSFER_REPLACE, interpolation);
485 void MotionMain::process_rotation()
487 int block_x, block_y;
489 // Convert the previous global reference into the previous rotation reference.
490 // Convert global target destination into rotation target source.
491 if( config.global ) {
493 overlayer = new OverlayFrame(PluginClient::get_project_smp() + 1);
495 if( config.tracking_object == MotionScan::TRACK_SINGLE ) {
496 dx = (float)total_dx / OVERSAMPLE;
497 dy = (float)total_dy / OVERSAMPLE;
500 dx = (float)current_dx / OVERSAMPLE;
501 dy = (float)current_dy / OVERSAMPLE;
504 prev_rotate_ref->clear_frame();
505 overlayer->overlay(prev_rotate_ref, prev_global_ref,
506 0, 0, prev_global_ref->get_w(), prev_global_ref->get_h(),
508 (float)prev_global_ref->get_w() + dx,
509 (float)prev_global_ref->get_h() + dy,
510 1, TRANSFER_REPLACE, CUBIC_LINEAR);
511 // Pivot is destination global position
512 block_x = (int)(prev_rotate_ref->get_w() *
513 config.block_x / 100 + (float)total_dx / OVERSAMPLE);
514 block_y = (int)(prev_rotate_ref->get_h() *
515 config.block_y / 100 + (float)total_dy / OVERSAMPLE);
516 // Use the global target output as the rotation target input
517 rotate_target_src->copy_from(global_target_dst);
518 // Transfer current reference frame to previous reference frame for global.
519 if( config.tracking_object != MotionScan::TRACK_SINGLE ) {
520 prev_global_ref->copy_from(current_global_ref);
521 previous_frame_number = get_source_position();
526 block_x = (int)(prev_rotate_ref->get_w() * config.block_x / 100);
527 block_y = (int)(prev_rotate_ref->get_h() * config.block_y / 100);
532 motion_rotate = new RotateScan(this,
533 get_project_smp() + 1, get_project_smp() + 1);
535 current_angle = motion_rotate->
536 scan_frame(prev_rotate_ref, current_rotate_ref, block_x, block_y);
539 if( config.tracking_type == MotionScan::SAVE ) {
540 save_dt = current_angle;
543 // Add current rotation to accumulation
544 if( config.tracking_object != MotionScan::TRACK_SINGLE ) {
546 total_angle = total_angle * (100 - config.rotate_return_speed) / 100;
547 // Accumulate current rotation
548 total_angle += current_angle;
550 // Clamp rotation accumulation
551 if( config.rotate_magnitude < 90 ) {
552 CLAMP(total_angle, -config.rotate_magnitude, config.rotate_magnitude);
555 if( !config.global ) {
556 // Transfer current reference frame to previous reference frame and update counter.
557 prev_rotate_ref->copy_from(current_rotate_ref);
558 previous_frame_number = get_source_position();
562 total_angle = current_angle;
566 printf("MotionMain::process_rotation total_angle=%f\n", total_angle);
570 // Calculate rotation parameters based on requested operation
572 switch(config.action_type) {
573 case MotionScan::NOTHING:
574 rotate_target_dst->copy_from(rotate_target_src);
576 case MotionScan::TRACK:
577 case MotionScan::TRACK_PIXEL:
580 case MotionScan::STABILIZE:
581 case MotionScan::STABILIZE_PIXEL:
582 angle = -total_angle;
586 if( config.action_type != MotionScan::NOTHING ) {
588 rotate_engine = new AffineEngine(
589 PluginClient::get_project_smp() + 1,
590 PluginClient::get_project_smp() + 1);
592 rotate_target_dst->clear_frame();
594 // Determine pivot based on a number of factors.
595 switch(config.action_type) {
596 case MotionScan::TRACK:
597 case MotionScan::TRACK_PIXEL:
598 // Use destination of global tracking.
599 rotate_engine->set_in_pivot(block_x, block_y);
600 rotate_engine->set_out_pivot(block_x, block_y);
603 case MotionScan::STABILIZE:
604 case MotionScan::STABILIZE_PIXEL:
605 if( config.global ) {
606 // Use origin of global stabilize operation
607 rotate_engine->set_in_pivot(
608 (int)(rotate_target_dst->get_w() * config.block_x / 100),
609 (int)(rotate_target_dst->get_h() * config.block_y / 100));
610 rotate_engine->set_out_pivot(
611 (int)(rotate_target_dst->get_w() * config.block_x / 100),
612 (int)(rotate_target_dst->get_h() * config.block_y / 100));
616 rotate_engine->set_in_pivot(block_x, block_y);
617 rotate_engine->set_out_pivot(block_x, block_y);
622 rotate_engine->rotate(rotate_target_dst, rotate_target_src, angle);
623 // overlayer->overlay(rotate_target_dst, prev_rotate_ref,
624 // 0, 0, prev_rotate_ref->get_w(), prev_rotate_ref->get_h(),
625 // 0, 0, prev_rotate_ref->get_w(), prev_rotate_ref->get_h(),
626 // 1, TRANSFER_NORMAL, CUBIC_LINEAR);
627 // overlayer->overlay(rotate_target_dst, current_rotate_ref,
628 // 0, 0, prev_rotate_ref->get_w(), prev_rotate_ref->get_h(),
629 // 0, 0, prev_rotate_ref->get_w(), prev_rotate_ref->get_h(),
630 // 1, TRANSFER_NORMAL, // CUBIC_LINEAR);
635 int MotionMain::process_buffer(VFrame **frame, int64_t start_position, double frame_rate)
637 int prev_config_tracking_type = config.tracking_type;
638 int need_reconfigure = load_configuration();
639 int color_model = frame[0]->get_color_model();
640 w = frame[0]->get_w();
641 h = frame[0]->get_h();
644 printf("MotionMain::process_buffer %d start_position=%jd\n", __LINE__, start_position);
647 // Calculate the source and destination pointers for each of the operations.
648 // Get the layer to track motion in.
649 // Get the layer to apply motion in.
650 reference_layer = config.bottom_is_master ?
651 PluginClient::total_in_buffers - 1 : 0;
652 target_layer = config.bottom_is_master ?
653 0 : PluginClient::total_in_buffers - 1;
655 output_frame = frame[target_layer];
656 // Get the position of previous reference frame.
657 int64_t actual_previous_number;
658 // Skip if match frame not available
659 int skip_current = 0;
661 if( config.tracking_object == MotionScan::TRACK_SINGLE ) {
662 actual_previous_number = config.track_frame;
663 if( get_direction() == PLAY_REVERSE )
664 actual_previous_number++;
665 if( actual_previous_number == start_position )
669 actual_previous_number = start_position;
670 if( get_direction() == PLAY_FORWARD ) {
671 actual_previous_number--;
672 if( actual_previous_number < get_source_start() )
675 KeyFrame *keyframe = get_prev_keyframe(start_position, 1);
676 if( keyframe->position > 0 &&
677 actual_previous_number < keyframe->position )
682 actual_previous_number++;
683 if( actual_previous_number >= get_source_start() + get_total_len() )
686 KeyFrame *keyframe = get_next_keyframe(start_position, 1);
687 if( keyframe->position > 0 &&
688 actual_previous_number >= keyframe->position )
692 // Only count motion since last keyframe
695 if( !config.global && !config.rotate )
698 //printf("process_realtime: %jd %d %jd %jd\n", start_position,
699 // skip_current, previous_frame_number, actual_previous_number);
700 if( prev_config_tracking_type != MotionScan::SAVE &&
701 config.tracking_type == MotionScan::SAVE ) {
703 char save_file[BCTEXTLEN];
704 snprintf(save_file, sizeof(save_file), "%s.bak", config.tracking_file);
706 printf("MotionMain::process_buffer 2 rename tracking file: %s to %s\n",
707 config.tracking_file, save_file);
709 ::rename(config.tracking_file, save_file);
711 else if( !cache_file[0] || active_key > start_position )
714 // Load match frame and reset vectors
715 int need_reload = !skip_current &&
716 (previous_frame_number != actual_previous_number ||
719 total_dx = total_dy = 0; total_angle = 0;
720 previous_frame_number = actual_previous_number;
724 total_dx = total_dy = 0;
725 current_dx = current_dy = 0;
726 total_angle = current_angle = 0;
729 // Get the global pointers. Here we walk through the sequence of events.
730 if( config.global ) {
731 // Assume global only. Global reads previous frame and compares
732 // with current frame to get the current translation.
733 // The center of the search area is fixed in compensate mode or
734 // the user value + the accumulation vector in track mode.
735 if( !prev_global_ref )
736 prev_global_ref = new VFrame(w, h, color_model, 0);
737 if( !current_global_ref )
738 current_global_ref = new VFrame(w, h, color_model, 0);
740 // Global loads the current target frame into the src and
741 // writes it to the dst frame with desired translation.
742 if( !global_target_src )
743 global_target_src = new VFrame(w, h, color_model, 0);
744 if( !global_target_dst )
745 global_target_dst = new VFrame(w, h, color_model, 0);
747 // Load the global frames
749 read_frame(prev_global_ref, reference_layer,
750 previous_frame_number, frame_rate, 0);
753 read_frame(current_global_ref, reference_layer,
754 start_position, frame_rate, 0);
755 read_frame(global_target_src, target_layer,
756 start_position, frame_rate, 0);
758 // Global followed by rotate
759 if( config.rotate ) {
760 // Must translate the previous global reference by the current global
761 // accumulation vector to match the current global reference.
762 // The center of the search area is always the user value + the accumulation
764 if( !prev_rotate_ref )
765 prev_rotate_ref = new VFrame(w, h, color_model, 0);
766 // The current global reference is the current rotation reference.
767 if( !current_rotate_ref )
768 current_rotate_ref = new VFrame(w, h, color_model, 0);
769 current_rotate_ref->copy_from(current_global_ref);
771 // The global target destination is copied to the rotation target source
772 // then written to the rotation output with rotation.
773 // The pivot for the rotation is the center of the search area
774 // if we're tracking.
775 // The pivot is fixed to the user position if we're compensating.
776 if( !rotate_target_src )
777 rotate_target_src = new VFrame(w, h, color_model, 0);
778 if( !rotate_target_dst )
779 rotate_target_dst = new VFrame(w, h, color_model, 0);
783 else if( config.rotate ) {
784 // Rotation reads the previous reference frame and compares it with current
786 if( !prev_rotate_ref )
787 prev_rotate_ref = new VFrame(w, h, color_model, 0);
788 if( !current_rotate_ref )
789 current_rotate_ref = new VFrame(w, h, color_model, 0);
791 // Rotation loads target frame to temporary, rotates it, and writes it to the
792 // target frame. The pivot is always fixed.
793 if( !rotate_target_src )
794 rotate_target_src = new VFrame(w, h, color_model, 0);
795 if( !rotate_target_dst )
796 rotate_target_dst = new VFrame(w, h, color_model, 0);
799 // Load the rotate frames
801 read_frame(prev_rotate_ref, reference_layer,
802 previous_frame_number, frame_rate, 0);
804 read_frame(current_rotate_ref, reference_layer,
805 start_position, frame_rate, 0);
806 read_frame(rotate_target_src, target_layer,
807 start_position, frame_rate, 0);
810 dx_offset = 0; dy_offset = 0;
811 if( config.tracking_type == MotionScan::LOAD ) {
812 if( config.addtrackedframeoffset ) {
813 if( config.track_frame != tracking_frame ) {
814 tracking_frame = config.track_frame;
815 int64_t no; int dx, dy; float dt;
816 if( !get_cache_line(tracking_frame) &&
817 sscanf(cache_line, "%jd %d %d %f", &no, &dx, &dy, &dt) == 4 ) {
818 dx_offset = dx; dy_offset = dy;
821 eprintf("no offset data frame %jd\n", tracking_frame);
829 if( !skip_current ) {
831 if( config.tracking_type == MotionScan::LOAD ||
832 config.tracking_type == MotionScan::SAVE ) {
833 int64_t no; int dx, dy; float dt;
834 int64_t frame_no = get_source_position();
835 // Load result from disk
836 if( !get_cache_line(frame_no) &&
837 sscanf(cache_line, "%jd %d %d %f", &no, &dx, &dy, &dt) == 4 ) {
838 load_ok = 1; load_dx = dx; load_dy = dy; load_dt = dt;
842 printf("MotionMain::process_buffer: no tracking data frame %jd\n", frame_no);
847 // Get position change from previous frame to current frame
850 // Get rotation change from previous frame to current frame
853 //frame[target_layer]->copy_from(prev_rotate_ref);
854 //frame[target_layer]->copy_from(current_rotate_ref);
856 // write results to disk
857 if( config.tracking_type == MotionScan::SAVE ) {
859 int64_t frame_no = get_source_position();
860 snprintf(line, sizeof(line), "%jd %d %d %f\n",
861 frame_no, save_dx, save_dy, save_dt);
862 put_cache_line(line);
864 // Transfer the relevant target frame to the output
865 if( config.rotate ) {
866 frame[target_layer]->copy_from(rotate_target_dst);
869 frame[target_layer]->copy_from(global_target_dst);
872 // Read the target destination directly
874 read_frame(frame[target_layer],
875 target_layer, start_position, frame_rate, 0);
878 if( config.draw_vectors ) {
879 draw_vectors(frame[target_layer]);
883 printf("MotionMain::process_buffer %d\n", __LINE__);
890 void MotionMain::draw_vectors(VFrame *frame)
892 int w = frame->get_w(), h = frame->get_h();
893 int global_x1, global_y1, global_x2, global_y2;
894 int block_x, block_y, block_w, block_h;
895 int block_x1, block_y1, block_x2, block_y2;
896 int block_x3, block_y3, block_x4, block_y4;
897 int search_x1, search_y1, search_x2, search_y2;
898 int search_w, search_h;
901 if( config.global ) {
903 // Start of vector is center of previous block.
904 // End of vector is total accumulation.
905 if( config.tracking_object == MotionScan::TRACK_SINGLE ) {
906 global_x1 = (int64_t)(config.block_x * w / 100);
907 global_y1 = (int64_t)(config.block_y * h / 100);
908 global_x2 = global_x1 + total_dx / OVERSAMPLE;
909 global_y2 = global_y1 + total_dy / OVERSAMPLE;
910 //printf("MotionMain::draw_vectors %d %d %d %d %d %d\n", total_dx, total_dy, global_x1, global_y1, global_x2, global_y2);
912 // Start of vector is center of previous block.
913 // End of vector is current change.
914 else if( config.tracking_object == MotionScan::PREVIOUS_SAME_BLOCK ) {
915 global_x1 = (int64_t)(config.block_x * w / 100);
916 global_y1 = (int64_t)(config.block_y * h / 100);
917 global_x2 = global_x1 + current_dx / OVERSAMPLE;
918 global_y2 = global_y1 + current_dy / OVERSAMPLE;
921 global_x1 = (int64_t)(config.block_x * w / 100
922 + (total_dx - current_dx) / OVERSAMPLE);
923 global_y1 = (int64_t)(config.block_y * h / 100
924 + (total_dy - current_dy) / OVERSAMPLE);
925 global_x2 = (int64_t)(config.block_x * w / 100
926 + total_dx / OVERSAMPLE);
927 global_y2 = (int64_t)(config.block_y * h / 100
928 + total_dy / OVERSAMPLE);
933 block_w = config.global_block_w * w / 100;
934 block_h = config.global_block_h * h / 100;
935 block_x1 = block_x - block_w / 2;
936 block_y1 = block_y - block_h / 2;
937 block_x2 = block_x + block_w / 2;
938 block_y2 = block_y + block_h / 2;
939 search_w = config.global_range_w * w / 100;
940 search_h = config.global_range_h * h / 100;
941 search_x1 = block_x1 - search_w / 2;
942 search_y1 = block_y1 - search_h / 2;
943 search_x2 = block_x2 + search_w / 2;
944 search_y2 = block_y2 + search_h / 2;
946 //printf("MotionMain::draw_vectors %d %d %d %d %d %d %d %d %d %d %d %d\n",
947 // global_x1, global_y1, block_w, block_h, block_x1, block_y1,
948 // block_x2, block_y2, search_x1, search_y1, search_x2, search_y2);
950 MotionScan::clamp_scan(w, h,
951 &block_x1, &block_y1, &block_x2, &block_y2,
952 &search_x1, &search_y1, &search_x2, &search_y2, 1);
955 draw_arrow(frame, global_x1, global_y1, global_x2, global_y2);
958 draw_line(frame, block_x1, block_y1, block_x2, block_y1);
959 draw_line(frame, block_x2, block_y1, block_x2, block_y2);
960 draw_line(frame, block_x2, block_y2, block_x1, block_y2);
961 draw_line(frame, block_x1, block_y2, block_x1, block_y1);
964 draw_line(frame, search_x1, search_y1, search_x2, search_y1);
965 draw_line(frame, search_x2, search_y1, search_x2, search_y2);
966 draw_line(frame, search_x2, search_y2, search_x1, search_y2);
967 draw_line(frame, search_x1, search_y2, search_x1, search_y1);
969 // Block should be endpoint of motion
970 if( config.rotate ) {
976 block_x = (int64_t)(config.block_x * w / 100);
977 block_y = (int64_t)(config.block_y * h / 100);
980 block_w = config.global_block_w * w / 100;
981 block_h = config.global_block_h * h / 100;
982 if( config.rotate ) {
983 float angle = total_angle * 2 * M_PI / 360;
984 double base_angle1 = atan((float)block_h / block_w);
985 double base_angle2 = atan((float)block_w / block_h);
986 double target_angle1 = base_angle1 + angle;
987 double target_angle2 = base_angle2 + angle;
988 double radius = sqrt(block_w * block_w + block_h * block_h) / 2;
989 block_x1 = (int)(block_x - cos(target_angle1) * radius);
990 block_y1 = (int)(block_y - sin(target_angle1) * radius);
991 block_x2 = (int)(block_x + sin(target_angle2) * radius);
992 block_y2 = (int)(block_y - cos(target_angle2) * radius);
993 block_x3 = (int)(block_x - sin(target_angle2) * radius);
994 block_y3 = (int)(block_y + cos(target_angle2) * radius);
995 block_x4 = (int)(block_x + cos(target_angle1) * radius);
996 block_y4 = (int)(block_y + sin(target_angle1) * radius);
998 draw_line(frame, block_x1, block_y1, block_x2, block_y2);
999 draw_line(frame, block_x2, block_y2, block_x4, block_y4);
1000 draw_line(frame, block_x4, block_y4, block_x3, block_y3);
1001 draw_line(frame, block_x3, block_y3, block_x1, block_y1);
1005 if( !config.global ) {
1006 draw_line(frame, block_x, block_y - 5, block_x, block_y + 6);
1007 draw_line(frame, block_x - 5, block_y, block_x + 6, block_y);
1012 MotionVVFrame::MotionVVFrame(VFrame *vfrm, int n)
1013 : VFrame(vfrm->get_data(), -1, vfrm->get_y()-vfrm->get_data(),
1014 vfrm->get_u()-vfrm->get_data(), vfrm->get_v()-vfrm->get_data(),
1015 vfrm->get_w(), vfrm->get_h(), vfrm->get_color_model(),
1016 vfrm->get_bytes_per_line())
1021 int MotionVVFrame::draw_pixel(int x, int y)
1023 VFrame::draw_pixel(x+0, y+0);
1024 for( int i=1; i<n; ++i ) {
1025 VFrame::draw_pixel(x-i, y-i);
1026 VFrame::draw_pixel(x+i, y+i);
1031 void MotionMain::draw_line(VFrame *frame, int x1, int y1, int x2, int y2)
1033 int iw = frame->get_w(), ih = frame->get_h();
1034 int mx = iw > ih ? iw : ih;
1036 MotionVVFrame vfrm(frame, n);
1037 vfrm.set_pixel_color(WHITE);
1038 int m = 2; while( m < n ) m <<= 1;
1039 vfrm.set_stiple(2*m);
1040 vfrm.draw_line(x1,y1, x2,y2);
1043 #define ARROW_SIZE 10
1044 void MotionMain::draw_arrow(VFrame *frame, int x1, int y1, int x2, int y2)
1046 double angle = atan((float)(y2 - y1) / (float)(x2 - x1));
1047 double angle1 = angle + (float)145 / 360 * 2 * 3.14159265;
1048 double angle2 = angle - (float)145 / 360 * 2 * 3.14159265;
1051 x3 = x2 - (int)(ARROW_SIZE * cos(angle1));
1052 y3 = y2 - (int)(ARROW_SIZE * sin(angle1));
1053 x4 = x2 - (int)(ARROW_SIZE * cos(angle2));
1054 y4 = y2 - (int)(ARROW_SIZE * sin(angle2));
1057 x3 = x2 + (int)(ARROW_SIZE * cos(angle1));
1058 y3 = y2 + (int)(ARROW_SIZE * sin(angle1));
1059 x4 = x2 + (int)(ARROW_SIZE * cos(angle2));
1060 y4 = y2 + (int)(ARROW_SIZE * sin(angle2));
1064 draw_line(frame, x1, y1, x2, y2);
1065 // draw_line(frame, x1, y1 + 1, x2, y2 + 1);
1068 if( abs(y2 - y1) || abs(x2 - x1) ) draw_line(frame, x2, y2, x3, y3);
1069 // draw_line(frame, x2, y2 + 1, x3, y3 + 1);
1071 if( abs(y2 - y1) || abs(x2 - x1) ) draw_line(frame, x2, y2, x4, y4);
1072 // draw_line(frame, x2, y2 + 1, x4, y4 + 1);
1075 int MotionMain::open_cache_file()
1077 if( cache_fp ) return 0;
1078 if( !cache_file[0] ) return 1;
1079 if( !(cache_fp = fopen(cache_file, "r")) ) return 1;
1083 void MotionMain::close_cache_file()
1085 if( !cache_fp ) return;
1087 cache_fp = 0; cache_key = -1; tracking_frame = -1;
1090 int MotionMain::load_cache_line()
1093 if( open_cache_file() ) return 1;
1094 if( !fgets(cache_line, sizeof(cache_line), cache_fp) ) return 1;
1095 cache_key = strtol(cache_line, 0, 0);
1099 int MotionMain::get_cache_line(int64_t key)
1101 if( cache_key == key ) return 0;
1102 if( open_cache_file() ) return 1;
1103 if( cache_key >= 0 && key > cache_key ) {
1104 if( load_cache_line() ) return 1;
1105 if( cache_key == key ) return 0;
1106 if( cache_key > key ) return 1;
1108 // binary search file
1109 fseek(cache_fp, 0, SEEK_END);
1110 int64_t l = -1, r = ftell(cache_fp);
1111 while( (r - l) > 1 ) {
1112 int64_t m = (l + r) / 2;
1113 fseek(cache_fp, m, SEEK_SET);
1114 if( m > 0 && !fgets(cache_line, sizeof(cache_line), cache_fp) )
1116 if( !load_cache_line() ) {
1117 if( cache_key == key )
1119 if( cache_key < key ) { l = m; continue; }
1126 int MotionMain::locate_cache_line(int64_t key)
1129 if( key < 0 || !(ret=get_cache_line(key)) ||
1130 ( cache_key >= 0 && cache_key < key ) )
1131 ret = load_cache_line();
1135 int MotionMain::put_cache_line(const char *line)
1137 int64_t key = strtol(line, 0, 0);
1138 if( key == active_key ) return 1;
1141 snprintf(cache_file, sizeof(cache_file), "%s.bak", config.tracking_file);
1142 ::rename(config.tracking_file, cache_file);
1143 if( !(active_fp = fopen(config.tracking_file, "w")) ) {
1144 perror(config.tracking_file);
1145 fprintf(stderr, "err writing key %jd\n", key);
1151 if( active_key < key ) {
1152 locate_cache_line(active_key);
1153 while( cache_key >= 0 && key >= cache_key ) {
1154 if( key > cache_key )
1155 fputs(cache_line, active_fp);
1161 fputs(line, active_fp);
1166 void MotionMain::reset_cache_file()
1169 locate_cache_line(active_key);
1170 while( cache_key >= 0 ) {
1171 fputs(cache_line, active_fp);
1174 close_cache_file(); ::remove(cache_file);
1175 fclose(active_fp); active_fp = 0; active_key = -1;
1179 strcpy(cache_file, config.tracking_file);
1183 RotateScanPackage::RotateScanPackage()
1187 RotateScanUnit::RotateScanUnit(RotateScan *server, MotionMain *plugin)
1188 : LoadClient(server)
1190 this->server = server;
1191 this->plugin = plugin;
1196 RotateScanUnit::~RotateScanUnit()
1202 void RotateScanUnit::process_package(LoadPackage *package)
1204 if( server->skip ) return;
1205 RotateScanPackage *pkg = (RotateScanPackage*)package;
1207 if( (pkg->difference = server->get_cache(pkg->angle)) < 0 ) {
1208 //printf("RotateScanUnit::process_package %d\n", __LINE__);
1209 int color_model = server->previous_frame->get_color_model();
1210 int pixel_size = BC_CModels::calculate_pixelsize(color_model);
1211 int row_bytes = server->previous_frame->get_bytes_per_line();
1214 rotater = new AffineEngine(1, 1);
1217 server->previous_frame->get_w(),
1218 server->previous_frame->get_h(),
1220 //printf("RotateScanUnit::process_package %d\n", __LINE__);
1223 // Rotate original block size
1224 // rotater->set_viewport(server->block_x1, server->block_y1,
1225 // server->block_x2 - server->block_x1, server->block_y2 - server->block_y1);
1226 rotater->set_in_viewport(server->block_x1, server->block_y1,
1227 server->block_x2 - server->block_x1, server->block_y2 - server->block_y1);
1228 rotater->set_out_viewport(server->block_x1, server->block_y1,
1229 server->block_x2 - server->block_x1, server->block_y2 - server->block_y1);
1230 // rotater->set_pivot(server->block_x, server->block_y);
1231 rotater->set_in_pivot(server->block_x, server->block_y);
1232 rotater->set_out_pivot(server->block_x, server->block_y);
1233 //printf("RotateScanUnit::process_package %d\n", __LINE__);
1234 rotater->rotate(temp, server->previous_frame, pkg->angle);
1236 // Scan reduced block size
1237 //plugin->output_frame->copy_from(server->current_frame);
1238 //plugin->output_frame->copy_from(temp);
1239 //printf("RotateScanUnit::process_package %d %d %d %d %d\n",
1240 // __LINE__, server->scan_x, server->scan_y, server->scan_w, server->scan_h);
1241 // Clamp coordinates
1242 int x1 = server->scan_x;
1243 int y1 = server->scan_y;
1244 int x2 = x1 + server->scan_w;
1245 int y2 = y1 + server->scan_h;
1246 x2 = MIN(temp->get_w(), x2);
1247 y2 = MIN(temp->get_h(), y2);
1248 x2 = MIN(server->current_frame->get_w(), x2);
1249 y2 = MIN(server->current_frame->get_h(), y2);
1250 x1 = MAX(0, x1); y1 = MAX(0, y1);
1252 if( x2 > x1 && y2 > y1 ) {
1253 pkg->difference = MotionScan::abs_diff(
1254 temp->get_rows()[y1] + x1 * pixel_size,
1255 server->current_frame->get_rows()[y1] + x1 * pixel_size,
1256 row_bytes, x2 - x1, y2 - y1, color_model);
1257 //printf("RotateScanUnit::process_package %d\n", __LINE__);
1258 server->put_cache(pkg->angle, pkg->difference);
1261 VFrame png(x2-x1, y2-y1, BC_RGB888, -1);
1262 png.transfer_from(temp, 0, x1, y1, x2-x1, y2-y1);
1264 sprintf(fn,"%s%f.png","/tmp/temp",pkg->angle); png.write_png(fn);
1265 png.transfer_from(server->current_frame, 0, x1, y1, x2-x1, y2-y1);
1266 sprintf(fn,"%s%f.png","/tmp/curr",pkg->angle); png.write_png(fn);
1267 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=%jd\n",
1268 server->block_x1, server->block_y1, server->block_x2 - server->block_x1, server->block_y2 - server->block_y1,
1269 server->block_x, server->block_y, pkg->angle, server->scan_w, server->scan_h, pkg->difference);
1275 RotateScan::RotateScan(MotionMain *plugin,
1278 : LoadServer( //1, 1)
1279 total_clients, total_packages)
1281 this->plugin = plugin;
1282 cache_lock = new Mutex("RotateScan::cache_lock");
1286 RotateScan::~RotateScan()
1291 void RotateScan::init_packages()
1293 for( int i = 0; i < get_total_packages(); i++ ) {
1294 RotateScanPackage *pkg = (RotateScanPackage*)get_package(i);
1295 pkg->angle = scan_angle1 +
1296 i * (scan_angle2 - scan_angle1) / (total_steps - 1);
1300 LoadClient* RotateScan::new_client()
1302 return new RotateScanUnit(this, plugin);
1305 LoadPackage* RotateScan::new_package()
1307 return new RotateScanPackage;
1311 float RotateScan::scan_frame(VFrame *previous_frame, VFrame *current_frame,
1312 int block_x, int block_y)
1315 this->block_x = block_x;
1316 this->block_y = block_y;
1318 //printf("RotateScan::scan_frame %d\n", __LINE__);
1319 switch(plugin->config.tracking_type) {
1320 case MotionScan::NO_CALCULATE:
1321 result = plugin->config.rotation_center;
1325 case MotionScan::LOAD:
1326 case MotionScan::SAVE:
1327 if( plugin->load_ok ) {
1328 result = plugin->load_dt;
1334 this->previous_frame = previous_frame;
1335 this->current_frame = current_frame;
1336 int w = current_frame->get_w();
1337 int h = current_frame->get_h();
1338 int block_w = w * plugin->config.global_block_w / 100;
1339 int block_h = h * plugin->config.global_block_h / 100;
1341 if( this->block_x - block_w / 2 < 0 ) block_w = this->block_x * 2;
1342 if( this->block_y - block_h / 2 < 0 ) block_h = this->block_y * 2;
1343 if( this->block_x + block_w / 2 > w ) block_w = (w - this->block_x) * 2;
1344 if( this->block_y + block_h / 2 > h ) block_h = (h - this->block_y) * 2;
1346 block_x1 = this->block_x - block_w / 2;
1347 block_x2 = this->block_x + block_w / 2;
1348 block_y1 = this->block_y - block_h / 2;
1349 block_y2 = this->block_y + block_h / 2;
1351 // Calculate the maximum area available to scan after rotation.
1352 // Must be calculated from the starting range because of cache.
1353 // Get coords of rectangle after rotation.
1354 double center_x = this->block_x;
1355 double center_y = this->block_y;
1356 double max_angle = plugin->config.rotation_range;
1357 double base_angle1 = atan((float)block_h / block_w);
1358 double base_angle2 = atan((float)block_w / block_h);
1359 double target_angle1 = base_angle1 + max_angle * 2 * M_PI / 360;
1360 double target_angle2 = base_angle2 + max_angle * 2 * M_PI / 360;
1361 double radius = sqrt(block_w * block_w + block_h * block_h) / 2;
1362 double x1 = center_x - cos(target_angle1) * radius;
1363 double y1 = center_y - sin(target_angle1) * radius;
1364 double x2 = center_x + sin(target_angle2) * radius;
1365 double y2 = center_y - cos(target_angle2) * radius;
1366 double x3 = center_x - sin(target_angle2) * radius;
1367 double y3 = center_y + cos(target_angle2) * radius;
1369 // Track top edge to find greatest area.
1370 double max_area1 = 0;
1371 //double max_x1 = 0;
1373 for( double x = x1; x < x2; x++ ) {
1374 double y = y1 + (y2 - y1) * (x - x1) / (x2 - x1);
1375 if( x >= center_x && x < block_x2 && y >= block_y1 && y < center_y ) {
1376 double area = fabs(x - center_x) * fabs(y - center_y);
1377 if( area > max_area1 ) {
1385 // Track left edge to find greatest area.
1386 double max_area2 = 0;
1388 //double max_y2 = 0;
1389 for( double y = y1; y < y3; y++ ) {
1390 double x = x1 + (x3 - x1) * (y - y1) / (y3 - y1);
1391 if( x >= block_x1 && x < center_x && y >= block_y1 && y < center_y ) {
1392 double area = fabs(x - center_x) * fabs(y - center_y);
1393 if( area > max_area2 ) {
1401 double max_x, max_y;
1405 // Get reduced scan coords
1406 scan_w = (int)(fabs(max_x - center_x) * 2);
1407 scan_h = (int)(fabs(max_y - center_y) * 2);
1408 scan_x = (int)(center_x - scan_w / 2);
1409 scan_y = (int)(center_y - scan_h / 2);
1410 // printf("RotateScan::scan_frame center=%d,%d scan=%d,%d %dx%d\n",
1411 // this->block_x, this->block_y, scan_x, scan_y, scan_w, scan_h);
1412 // printf(" angle_range=%f block= %d,%d,%d,%d\n", max_angle, block_x1, block_y1, block_x2, block_y2);
1414 // Determine min angle from size of block
1415 double angle1 = atan((double)block_h / block_w);
1416 double angle2 = atan((double)(block_h - 1) / (block_w + 1));
1417 double min_angle = fabs(angle2 - angle1) / OVERSAMPLE;
1418 min_angle = MAX(min_angle, MIN_ANGLE);
1420 //printf("RotateScan::scan_frame %d min_angle=%f\n", __LINE__, min_angle * 360 / 2 / M_PI);
1422 cache.remove_all_objects();
1426 if( previous_frame->data_matches(current_frame) ) {
1427 //printf("RotateScan::scan_frame: frames match. Skipping.\n");
1428 result = plugin->config.rotation_center;
1434 // Initial search range
1435 float angle_range = max_angle;
1436 result = plugin->config.rotation_center;
1437 total_steps = plugin->config.rotate_positions;
1440 while( angle_range >= min_angle * total_steps ) {
1441 scan_angle1 = result - angle_range;
1442 scan_angle2 = result + angle_range;
1444 set_package_count(total_steps);
1445 //set_package_count(1);
1448 int64_t min_difference = -1;
1449 for( int i = 0; i < get_total_packages(); i++ ) {
1450 RotateScanPackage *pkg = (RotateScanPackage*)get_package(i);
1451 if( pkg->difference < min_difference || min_difference == -1 ) {
1452 min_difference = pkg->difference;
1453 result = pkg->angle;
1464 if( plugin->config.tracking_type == MotionScan::SAVE ) {
1465 plugin->save_dt = result;
1467 //printf("RotateScan::scan_frame %d angle=%f\n", __LINE__, result);
1471 int64_t RotateScan::get_cache(float angle)
1473 int64_t result = -1;
1474 cache_lock->lock("RotateScan::get_cache");
1475 for( int i = 0; i < cache.total; i++ ) {
1476 RotateScanCache *ptr = cache.values[i];
1477 if( fabs(ptr->angle - angle) <= MIN_ANGLE ) {
1478 result = ptr->difference;
1482 cache_lock->unlock();
1486 void RotateScan::put_cache(float angle, int64_t difference)
1488 RotateScanCache *ptr = new RotateScanCache(angle, difference);
1489 cache_lock->lock("RotateScan::put_cache");
1491 cache_lock->unlock();
1495 RotateScanCache::RotateScanCache(float angle, int64_t difference)
1497 this->angle = angle;
1498 this->difference = difference;