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"
31 #include "motionscan.h"
32 #include "motionwindow.h"
34 #include "overlayframe.h"
35 #include "rotateframe.h"
36 #include "transportque.h"
42 REGISTER_PLUGIN(MotionMain)
53 MotionConfig::MotionConfig()
60 global_block_w = 50; // MIN_BLOCK;
61 global_block_h = 50; // MIN_BLOCK;
62 // rotation_block_w = MIN_BLOCK;
63 // rotation_block_h = MIN_BLOCK;
66 global_positions = 256;
69 rotate_magnitude = 30;
71 rotate_return_speed = 8;
72 action_type = MotionScan::STABILIZE_PIXEL;
75 addtrackedframeoffset = 0;
76 tracking_type = MotionScan::CALCULATE;
78 tracking_object = MotionScan::TRACK_PREVIOUS;
85 void MotionConfig::set_cpus(int cpus)
87 int gpos = 64, gpos_limit = 16 * cpus;
88 if( gpos_limit > 131072 ) gpos_limit = 131072;
89 while( gpos < gpos_limit ) gpos *= 2;
90 global_positions = gpos;
91 int rpos = 4, rpos_limit = cpus / 4;
92 if( rpos_limit > 32 ) gpos_limit = 32;
93 while( rpos < rpos_limit ) rpos *= 2;
94 rotate_positions = rpos;
97 void MotionConfig::boundaries()
99 CLAMP(global_range_w, MIN_RADIUS, MAX_RADIUS);
100 CLAMP(global_range_h, MIN_RADIUS, MAX_RADIUS);
101 CLAMP(rotation_range, MIN_ROTATION, MAX_ROTATION);
102 CLAMP(rotation_center, -MAX_ROTATION, MAX_ROTATION);
103 CLAMP(block_count, MIN_BLOCKS, MAX_BLOCKS);
104 CLAMP(global_block_w, MIN_BLOCK, MAX_BLOCK);
105 CLAMP(global_block_h, MIN_BLOCK, MAX_BLOCK);
106 // CLAMP(rotation_block_w, MIN_BLOCK, MAX_BLOCK);
107 // CLAMP(rotation_block_h, MIN_BLOCK, MAX_BLOCK);
110 int MotionConfig::equivalent(MotionConfig &that)
112 return global_range_w == that.global_range_w &&
113 global_range_h == that.global_range_h &&
114 rotation_range == that.rotation_range &&
115 rotation_center == that.rotation_center &&
116 action_type == that.action_type &&
117 global == that.global &&
118 rotate == that.rotate &&
119 addtrackedframeoffset == that.addtrackedframeoffset &&
120 draw_vectors == that.draw_vectors &&
121 block_count == that.block_count &&
122 global_block_w == that.global_block_w &&
123 global_block_h == that.global_block_h &&
124 // rotation_block_w == that.rotation_block_w &&
125 // rotation_block_h == that.rotation_block_h &&
126 EQUIV(block_x, that.block_x) &&
127 EQUIV(block_y, that.block_y) &&
128 global_positions == that.global_positions &&
129 rotate_positions == that.rotate_positions &&
130 magnitude == that.magnitude &&
131 return_speed == that.return_speed &&
132 rotate_return_speed == that.rotate_return_speed &&
133 rotate_magnitude == that.rotate_magnitude &&
134 tracking_object == that.tracking_object &&
135 track_frame == that.track_frame &&
136 bottom_is_master == that.bottom_is_master &&
137 horizontal_only == that.horizontal_only &&
138 vertical_only == that.vertical_only;
141 void MotionConfig::copy_from(MotionConfig &that)
143 global_range_w = that.global_range_w;
144 global_range_h = that.global_range_h;
145 rotation_range = that.rotation_range;
146 rotation_center = that.rotation_center;
147 action_type = that.action_type;
148 global = that.global;
149 rotate = that.rotate;
150 addtrackedframeoffset = that.addtrackedframeoffset;
151 tracking_type = that.tracking_type;
152 draw_vectors = that.draw_vectors;
153 block_count = that.block_count;
154 block_x = that.block_x;
155 block_y = that.block_y;
156 global_positions = that.global_positions;
157 rotate_positions = that.rotate_positions;
158 global_block_w = that.global_block_w;
159 global_block_h = that.global_block_h;
160 // rotation_block_w = that.rotation_block_w;
161 // rotation_block_h = that.rotation_block_h;
162 magnitude = that.magnitude;
163 return_speed = that.return_speed;
164 rotate_magnitude = that.rotate_magnitude;
165 rotate_return_speed = that.rotate_return_speed;
166 tracking_object = that.tracking_object;
167 track_frame = that.track_frame;
168 bottom_is_master = that.bottom_is_master;
169 horizontal_only = that.horizontal_only;
170 vertical_only = that.vertical_only;
173 void MotionConfig::interpolate(MotionConfig &prev,
177 int64_t current_frame)
179 //double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
180 //double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
181 this->block_x = prev.block_x;
182 this->block_y = prev.block_y;
183 global_range_w = prev.global_range_w;
184 global_range_h = prev.global_range_h;
185 rotation_range = prev.rotation_range;
186 rotation_center = prev.rotation_center;
187 action_type = prev.action_type;
188 global = prev.global;
189 rotate = prev.rotate;
190 addtrackedframeoffset = prev.addtrackedframeoffset;
191 tracking_type = prev.tracking_type;
192 draw_vectors = prev.draw_vectors;
193 block_count = prev.block_count;
194 global_positions = prev.global_positions;
195 rotate_positions = prev.rotate_positions;
196 global_block_w = prev.global_block_w;
197 global_block_h = prev.global_block_h;
198 // rotation_block_w = prev.rotation_block_w;
199 // rotation_block_h = prev.rotation_block_h;
200 magnitude = prev.magnitude;
201 return_speed = prev.return_speed;
202 rotate_magnitude = prev.rotate_magnitude;
203 rotate_return_speed = prev.rotate_return_speed;
204 tracking_object = prev.tracking_object;
205 track_frame = prev.track_frame;
206 bottom_is_master = prev.bottom_is_master;
207 horizontal_only = prev.horizontal_only;
208 vertical_only = prev.vertical_only;
229 MotionMain::MotionMain(PluginServer *server)
230 : PluginVClient(server)
242 previous_frame_number = -1;
245 current_global_ref = 0;
246 global_target_src = 0;
247 global_target_dst = 0;
250 current_rotate_ref = 0;
251 rotate_target_src = 0;
252 rotate_target_dst = 0;
254 config.set_cpus(get_project_smp() + 1);
257 MotionMain::~MotionMain()
262 delete [] search_area;
264 delete rotate_engine;
265 delete motion_rotate;
268 delete prev_global_ref;
269 delete current_global_ref;
270 delete global_target_src;
271 delete global_target_dst;
273 delete prev_rotate_ref;
274 delete current_rotate_ref;
275 delete rotate_target_src;
276 delete rotate_target_dst;
279 const char* MotionMain::plugin_title() { return _("Motion"); }
280 int MotionMain::is_realtime() { return 1; }
281 int MotionMain::is_multichannel() { return 1; }
284 NEW_WINDOW_MACRO(MotionMain, MotionWindow)
286 LOAD_CONFIGURATION_MACRO(MotionMain, MotionConfig)
290 void MotionMain::update_gui()
294 if(load_configuration())
296 thread->window->lock_window("MotionMain::update_gui");
298 char string[BCTEXTLEN];
299 sprintf(string, "%d", config.global_positions);
300 ((MotionWindow*)thread->window)->global_search_positions->set_text(string);
301 sprintf(string, "%d", config.rotate_positions);
302 ((MotionWindow*)thread->window)->rotation_search_positions->set_text(string);
304 ((MotionWindow*)thread->window)->global_block_w->update(config.global_block_w);
305 ((MotionWindow*)thread->window)->global_block_h->update(config.global_block_h);
306 // ((MotionWindow*)thread->window)->rotation_block_w->update(config.rotation_block_w);
307 // ((MotionWindow*)thread->window)->rotation_block_h->update(config.rotation_block_h);
308 ((MotionWindow*)thread->window)->block_x->update(config.block_x);
309 ((MotionWindow*)thread->window)->block_y->update(config.block_y);
310 ((MotionWindow*)thread->window)->block_x_text->update((float)config.block_x);
311 ((MotionWindow*)thread->window)->block_y_text->update((float)config.block_y);
312 ((MotionWindow*)thread->window)->magnitude->update(config.magnitude);
313 ((MotionWindow*)thread->window)->return_speed->update(config.return_speed);
314 ((MotionWindow*)thread->window)->rotate_magnitude->update(config.rotate_magnitude);
315 ((MotionWindow*)thread->window)->rotate_return_speed->update(config.rotate_return_speed);
316 ((MotionWindow*)thread->window)->rotation_range->update(config.rotation_range);
317 ((MotionWindow*)thread->window)->rotation_center->update(config.rotation_center);
320 ((MotionWindow*)thread->window)->track_single->update(config.tracking_object == MotionScan::TRACK_SINGLE);
321 ((MotionWindow*)thread->window)->track_frame_number->update(config.track_frame);
322 ((MotionWindow*)thread->window)->track_previous->update(config.tracking_object == MotionScan::TRACK_PREVIOUS);
323 ((MotionWindow*)thread->window)->previous_same->update(config.tracking_object == MotionScan::PREVIOUS_SAME_BLOCK);
324 if(config.tracking_object != MotionScan::TRACK_SINGLE)
325 ((MotionWindow*)thread->window)->track_frame_number->disable();
327 ((MotionWindow*)thread->window)->track_frame_number->enable();
329 ((MotionWindow*)thread->window)->action_type->set_text(
330 ActionType::to_text(config.action_type));
331 ((MotionWindow*)thread->window)->tracking_type->set_text(
332 TrackingType::to_text(config.tracking_type));
333 ((MotionWindow*)thread->window)->track_direction->set_text(
334 TrackDirection::to_text(config.horizontal_only, config.vertical_only));
335 ((MotionWindow*)thread->window)->master_layer->set_text(
336 MasterLayer::to_text(config.bottom_is_master));
339 ((MotionWindow*)thread->window)->update_mode();
340 thread->window->unlock_window();
348 void MotionMain::save_data(KeyFrame *keyframe)
352 // cause data to be stored directly in text
353 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
354 output.tag.set_title("MOTION");
356 output.tag.set_property("BLOCK_COUNT", config.block_count);
357 output.tag.set_property("GLOBAL_POSITIONS", config.global_positions);
358 output.tag.set_property("ROTATE_POSITIONS", config.rotate_positions);
359 output.tag.set_property("GLOBAL_BLOCK_W", config.global_block_w);
360 output.tag.set_property("GLOBAL_BLOCK_H", config.global_block_h);
361 // output.tag.set_property("ROTATION_BLOCK_W", config.rotation_block_w);
362 // output.tag.set_property("ROTATION_BLOCK_H", config.rotation_block_h);
363 output.tag.set_property("BLOCK_X", config.block_x);
364 output.tag.set_property("BLOCK_Y", config.block_y);
365 output.tag.set_property("GLOBAL_RANGE_W", config.global_range_w);
366 output.tag.set_property("GLOBAL_RANGE_H", config.global_range_h);
367 output.tag.set_property("ROTATION_RANGE", config.rotation_range);
368 output.tag.set_property("ROTATION_CENTER", config.rotation_center);
369 output.tag.set_property("MAGNITUDE", config.magnitude);
370 output.tag.set_property("RETURN_SPEED", config.return_speed);
371 output.tag.set_property("ROTATE_MAGNITUDE", config.rotate_magnitude);
372 output.tag.set_property("ROTATE_RETURN_SPEED", config.rotate_return_speed);
373 output.tag.set_property("ACTION_TYPE", config.action_type);
374 output.tag.set_property("GLOBAL", config.global);
375 output.tag.set_property("ROTATE", config.rotate);
376 output.tag.set_property("ADDTRACKEDFRAMEOFFSET", config.addtrackedframeoffset);
377 output.tag.set_property("TRACKING_TYPE", config.tracking_type);
378 output.tag.set_property("DRAW_VECTORS", config.draw_vectors);
379 output.tag.set_property("TRACKING_OBJECT", config.tracking_object);
380 output.tag.set_property("TRACK_FRAME", config.track_frame);
381 output.tag.set_property("BOTTOM_IS_MASTER", config.bottom_is_master);
382 output.tag.set_property("HORIZONTAL_ONLY", config.horizontal_only);
383 output.tag.set_property("VERTICAL_ONLY", config.vertical_only);
385 output.tag.set_title("/MOTION");
387 output.terminate_string();
390 void MotionMain::read_data(KeyFrame *keyframe)
394 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
400 result = input.read_tag();
404 if(input.tag.title_is("MOTION"))
406 config.block_count = input.tag.get_property("BLOCK_COUNT", config.block_count);
407 config.global_positions = input.tag.get_property("GLOBAL_POSITIONS", config.global_positions);
408 config.rotate_positions = input.tag.get_property("ROTATE_POSITIONS", config.rotate_positions);
409 config.global_block_w = input.tag.get_property("GLOBAL_BLOCK_W", config.global_block_w);
410 config.global_block_h = input.tag.get_property("GLOBAL_BLOCK_H", config.global_block_h);
411 // config.rotation_block_w = input.tag.get_property("ROTATION_BLOCK_W", config.rotation_block_w);
412 // config.rotation_block_h = input.tag.get_property("ROTATION_BLOCK_H", config.rotation_block_h);
413 config.block_x = input.tag.get_property("BLOCK_X", config.block_x);
414 config.block_y = input.tag.get_property("BLOCK_Y", config.block_y);
415 config.global_range_w = input.tag.get_property("GLOBAL_RANGE_W", config.global_range_w);
416 config.global_range_h = input.tag.get_property("GLOBAL_RANGE_H", config.global_range_h);
417 config.rotation_range = input.tag.get_property("ROTATION_RANGE", config.rotation_range);
418 config.rotation_center = input.tag.get_property("ROTATION_CENTER", config.rotation_center);
419 config.magnitude = input.tag.get_property("MAGNITUDE", config.magnitude);
420 config.return_speed = input.tag.get_property("RETURN_SPEED", config.return_speed);
421 config.rotate_magnitude = input.tag.get_property("ROTATE_MAGNITUDE", config.rotate_magnitude);
422 config.rotate_return_speed = input.tag.get_property("ROTATE_RETURN_SPEED", config.rotate_return_speed);
423 config.action_type = input.tag.get_property("ACTION_TYPE", config.action_type);
424 config.global = input.tag.get_property("GLOBAL", config.global);
425 config.rotate = input.tag.get_property("ROTATE", config.rotate);
426 config.addtrackedframeoffset = input.tag.get_property("ADDTRACKEDFRAMEOFFSET", config.addtrackedframeoffset);
427 config.tracking_type = input.tag.get_property("TRACKING_TYPE", config.tracking_type);
428 config.draw_vectors = input.tag.get_property("DRAW_VECTORS", config.draw_vectors);
429 config.tracking_object = input.tag.get_property("TRACKING_OBJECT", config.tracking_object);
430 config.track_frame = input.tag.get_property("TRACK_FRAME", config.track_frame);
431 config.bottom_is_master = input.tag.get_property("BOTTOM_IS_MASTER", config.bottom_is_master);
432 config.horizontal_only = input.tag.get_property("HORIZONTAL_ONLY", config.horizontal_only);
433 config.vertical_only = input.tag.get_property("VERTICAL_ONLY", config.vertical_only);
448 void MotionMain::allocate_temp(int w, int h, int color_model)
451 (temp_frame->get_w() != w ||
452 temp_frame->get_h() != h))
458 temp_frame = new VFrame(w, h, color_model);
462 void MotionMain::process_global()
465 if(!engine) engine = new MotionScan(PluginClient::get_project_smp() + 1,
466 PluginClient::get_project_smp() + 1);
468 // Determine if frames changed
469 engine->scan_frame(current_global_ref,
471 config.global_range_w,
472 config.global_range_h,
473 config.global_block_w,
474 config.global_block_h,
477 config.tracking_object,
478 config.tracking_type,
480 config.horizontal_only,
481 config.vertical_only,
482 get_source_position(),
483 config.global_positions,
488 current_dx = engine->dx_result;
489 current_dy = engine->dy_result;
491 // Add current motion vector to accumulation vector.
492 if(config.tracking_object != MotionScan::TRACK_SINGLE)
495 total_dx = (int64_t)total_dx * (100 - config.return_speed) / 100;
496 total_dy = (int64_t)total_dy * (100 - config.return_speed) / 100;
497 total_dx += engine->dx_result;
498 total_dy += engine->dy_result;
499 // printf("MotionMain::process_global total_dx=%d engine->dx_result=%d\n",
501 // engine->dx_result);
504 // Make accumulation vector current
506 total_dx = engine->dx_result;
507 total_dy = engine->dy_result;
510 // Clamp accumulation vector
511 if(config.magnitude < 100)
513 //int block_w = (int64_t)config.global_block_w *
514 // current_global_ref->get_w() / 100;
515 //int block_h = (int64_t)config.global_block_h *
516 // current_global_ref->get_h() / 100;
517 int block_x_orig = (int64_t)(config.block_x *
518 current_global_ref->get_w() /
520 int block_y_orig = (int64_t)(config.block_y *
521 current_global_ref->get_h() /
524 int max_block_x = (int64_t)(current_global_ref->get_w() - block_x_orig) *
528 int max_block_y = (int64_t)(current_global_ref->get_h() - block_y_orig) *
532 int min_block_x = (int64_t)-block_x_orig *
536 int min_block_y = (int64_t)-block_y_orig *
541 CLAMP(total_dx, min_block_x, max_block_x);
542 CLAMP(total_dy, min_block_y, max_block_y);
546 printf("MotionMain::process_global 2 total_dx=%.02f total_dy=%.02f\n",
547 (float)total_dx / OVERSAMPLE,
548 (float)total_dy / OVERSAMPLE);
551 if(config.tracking_object != MotionScan::TRACK_SINGLE && !config.rotate)
553 // Transfer current reference frame to previous reference frame and update
554 // counter. Must wait for rotate to compare.
555 prev_global_ref->copy_from(current_global_ref);
556 previous_frame_number = get_source_position();
559 // Decide what to do with target based on requested operation
560 int interpolation = NEAREST_NEIGHBOR;
563 switch(config.action_type)
565 case MotionScan::NOTHING:
566 global_target_dst->copy_from(global_target_src);
568 case MotionScan::TRACK_PIXEL:
569 interpolation = NEAREST_NEIGHBOR;
570 dx = (int)(total_dx / OVERSAMPLE);
571 dy = (int)(total_dy / OVERSAMPLE);
573 case MotionScan::STABILIZE_PIXEL:
574 interpolation = NEAREST_NEIGHBOR;
575 dx = -(int)(total_dx / OVERSAMPLE);
576 dy = -(int)(total_dy / OVERSAMPLE);
579 case MotionScan::TRACK:
580 interpolation = CUBIC_LINEAR;
581 dx = (float)total_dx / OVERSAMPLE;
582 dy = (float)total_dy / OVERSAMPLE;
584 case MotionScan::STABILIZE:
585 interpolation = CUBIC_LINEAR;
586 dx = -(float)total_dx / OVERSAMPLE;
587 dy = -(float)total_dy / OVERSAMPLE;
592 if(config.action_type != MotionScan::NOTHING)
595 overlayer = new OverlayFrame(PluginClient::get_project_smp() + 1);
596 global_target_dst->clear_frame();
597 overlayer->overlay(global_target_dst,
601 global_target_src->get_w(),
602 global_target_src->get_h(),
605 (float)global_target_src->get_w() + dx,
606 (float)global_target_src->get_h() + dy,
615 void MotionMain::process_rotation()
620 // Convert the previous global reference into the previous rotation reference.
621 // Convert global target destination into rotation target source.
625 overlayer = new OverlayFrame(PluginClient::get_project_smp() + 1);
628 if(config.tracking_object == MotionScan::TRACK_SINGLE)
630 dx = (float)total_dx / OVERSAMPLE;
631 dy = (float)total_dy / OVERSAMPLE;
635 dx = (float)current_dx / OVERSAMPLE;
636 dy = (float)current_dy / OVERSAMPLE;
639 prev_rotate_ref->clear_frame();
640 overlayer->overlay(prev_rotate_ref,
644 prev_global_ref->get_w(),
645 prev_global_ref->get_h(),
648 (float)prev_global_ref->get_w() + dx,
649 (float)prev_global_ref->get_h() + dy,
653 // Pivot is destination global position
654 block_x = (int)(prev_rotate_ref->get_w() *
659 block_y = (int)(prev_rotate_ref->get_h() *
664 // Use the global target output as the rotation target input
665 rotate_target_src->copy_from(global_target_dst);
666 // Transfer current reference frame to previous reference frame for global.
667 if(config.tracking_object != MotionScan::TRACK_SINGLE)
669 prev_global_ref->copy_from(current_global_ref);
670 previous_frame_number = get_source_position();
676 block_x = (int)(prev_rotate_ref->get_w() *
679 block_y = (int)(prev_rotate_ref->get_h() *
688 motion_rotate = new RotateScan(this,
689 get_project_smp() + 1,
690 get_project_smp() + 1);
692 current_angle = motion_rotate->scan_frame(prev_rotate_ref,
699 // Add current rotation to accumulation
700 if(config.tracking_object != MotionScan::TRACK_SINGLE)
703 total_angle = total_angle * (100 - config.rotate_return_speed) / 100;
704 // Accumulate current rotation
705 total_angle += current_angle;
707 // Clamp rotation accumulation
708 if(config.rotate_magnitude < 90)
710 CLAMP(total_angle, -config.rotate_magnitude, config.rotate_magnitude);
715 // Transfer current reference frame to previous reference frame and update
717 prev_rotate_ref->copy_from(current_rotate_ref);
718 previous_frame_number = get_source_position();
723 total_angle = current_angle;
727 printf("MotionMain::process_rotation total_angle=%f\n", total_angle);
731 // Calculate rotation parameters based on requested operation
733 switch(config.action_type)
735 case MotionScan::NOTHING:
736 rotate_target_dst->copy_from(rotate_target_src);
738 case MotionScan::TRACK:
739 case MotionScan::TRACK_PIXEL:
742 case MotionScan::STABILIZE:
743 case MotionScan::STABILIZE_PIXEL:
744 angle = -total_angle;
750 if(config.action_type != MotionScan::NOTHING)
753 rotate_engine = new AffineEngine(PluginClient::get_project_smp() + 1,
754 PluginClient::get_project_smp() + 1);
756 rotate_target_dst->clear_frame();
758 // Determine pivot based on a number of factors.
759 switch(config.action_type)
761 case MotionScan::TRACK:
762 case MotionScan::TRACK_PIXEL:
763 // Use destination of global tracking.
764 // rotate_engine->set_pivot(block_x, block_y);
765 rotate_engine->set_in_pivot(block_x, block_y);
766 rotate_engine->set_out_pivot(block_x, block_y);
769 case MotionScan::STABILIZE:
770 case MotionScan::STABILIZE_PIXEL:
773 // Use origin of global stabilize operation
774 // rotate_engine->set_pivot((int)(rotate_target_dst->get_w() *
777 // (int)(rotate_target_dst->get_h() *
780 rotate_engine->set_in_pivot((int)(rotate_target_dst->get_w() *
783 (int)(rotate_target_dst->get_h() *
786 rotate_engine->set_out_pivot((int)(rotate_target_dst->get_w() *
789 (int)(rotate_target_dst->get_h() *
797 // rotate_engine->set_pivot(block_x, block_y);
798 rotate_engine->set_in_pivot(block_x, block_y);
799 rotate_engine->set_out_pivot(block_x, block_y);
805 rotate_engine->rotate(rotate_target_dst, rotate_target_src, angle);
806 // overlayer->overlay(rotate_target_dst,
810 // prev_rotate_ref->get_w(),
811 // prev_rotate_ref->get_h(),
814 // prev_rotate_ref->get_w(),
815 // prev_rotate_ref->get_h(),
819 // overlayer->overlay(rotate_target_dst,
820 // current_rotate_ref,
823 // prev_rotate_ref->get_w(),
824 // prev_rotate_ref->get_h(),
827 // prev_rotate_ref->get_w(),
828 // prev_rotate_ref->get_h(),
847 int MotionMain::process_buffer(VFrame **frame,
848 int64_t start_position,
851 int need_reconfigure = load_configuration();
852 int color_model = frame[0]->get_color_model();
853 w = frame[0]->get_w();
854 h = frame[0]->get_h();
858 printf("MotionMain::process_buffer %d start_position=%lld\n", __LINE__, start_position);
862 // Calculate the source and destination pointers for each of the operations.
863 // Get the layer to track motion in.
864 reference_layer = config.bottom_is_master ?
865 PluginClient::total_in_buffers - 1 :
867 // Get the layer to apply motion in.
868 target_layer = config.bottom_is_master ?
870 PluginClient::total_in_buffers - 1;
873 output_frame = frame[target_layer];
876 // Get the position of previous reference frame.
877 int64_t actual_previous_number;
878 // Skip if match frame not available
879 int skip_current = 0;
882 if(config.tracking_object == MotionScan::TRACK_SINGLE)
884 actual_previous_number = config.track_frame;
885 if(get_direction() == PLAY_REVERSE)
886 actual_previous_number++;
887 if(actual_previous_number == start_position)
892 actual_previous_number = start_position;
893 if(get_direction() == PLAY_FORWARD)
895 actual_previous_number--;
896 if(actual_previous_number < get_source_start())
900 KeyFrame *keyframe = get_prev_keyframe(start_position, 1);
901 if(keyframe->position > 0 &&
902 actual_previous_number < keyframe->position)
908 actual_previous_number++;
909 if(actual_previous_number >= get_source_start() + get_total_len())
913 KeyFrame *keyframe = get_next_keyframe(start_position, 1);
914 if(keyframe->position > 0 &&
915 actual_previous_number >= keyframe->position)
920 // Only count motion since last keyframe
926 if(!config.global && !config.rotate) skip_current = 1;
931 // printf("process_realtime %d %lld %lld\n",
933 // previous_frame_number,
934 // actual_previous_number);
935 // Load match frame and reset vectors
936 int need_reload = !skip_current &&
937 (previous_frame_number != actual_previous_number ||
944 previous_frame_number = actual_previous_number;
961 // Get the global pointers. Here we walk through the sequence of events.
964 // Assume global only. Global reads previous frame and compares
965 // with current frame to get the current translation.
966 // The center of the search area is fixed in compensate mode or
967 // the user value + the accumulation vector in track mode.
969 prev_global_ref = new VFrame(w, h, color_model);
970 if(!current_global_ref)
971 current_global_ref = new VFrame(w, h, color_model);
973 // Global loads the current target frame into the src and
974 // writes it to the dst frame with desired translation.
975 if(!global_target_src)
976 global_target_src = new VFrame(w, h, color_model);
977 if(!global_target_dst)
978 global_target_dst = new VFrame(w, h, color_model);
981 // Load the global frames
984 read_frame(prev_global_ref,
986 previous_frame_number,
991 read_frame(current_global_ref,
996 read_frame(global_target_src,
1004 // Global followed by rotate
1007 // Must translate the previous global reference by the current global
1008 // accumulation vector to match the current global reference.
1009 // The center of the search area is always the user value + the accumulation
1011 if(!prev_rotate_ref)
1012 prev_rotate_ref = new VFrame(w, h, color_model);
1013 // The current global reference is the current rotation reference.
1014 if(!current_rotate_ref)
1015 current_rotate_ref = new VFrame(w, h, color_model);
1016 current_rotate_ref->copy_from(current_global_ref);
1018 // The global target destination is copied to the rotation target source
1019 // then written to the rotation output with rotation.
1020 // The pivot for the rotation is the center of the search area
1021 // if we're tracking.
1022 // The pivot is fixed to the user position if we're compensating.
1023 if(!rotate_target_src)
1024 rotate_target_src = new VFrame(w, h, color_model);
1025 if(!rotate_target_dst)
1026 rotate_target_dst = new VFrame(w, h, color_model);
1033 // Rotation reads the previous reference frame and compares it with current
1035 if(!prev_rotate_ref)
1036 prev_rotate_ref = new VFrame(w, h, color_model);
1037 if(!current_rotate_ref)
1038 current_rotate_ref = new VFrame(w, h, color_model);
1040 // Rotation loads target frame to temporary, rotates it, and writes it to the
1041 // target frame. The pivot is always fixed.
1042 if(!rotate_target_src)
1043 rotate_target_src = new VFrame(w, h, color_model);
1044 if(!rotate_target_dst)
1045 rotate_target_dst = new VFrame(w, h, color_model);
1048 // Load the rotate frames
1051 read_frame(prev_rotate_ref,
1053 previous_frame_number,
1057 read_frame(current_rotate_ref,
1062 read_frame(rotate_target_src,
1080 // Get position change from previous frame to current frame
1081 if(config.global) process_global();
1082 // Get rotation change from previous frame to current frame
1083 if(config.rotate) process_rotation();
1084 //frame[target_layer]->copy_from(prev_rotate_ref);
1085 //frame[target_layer]->copy_from(current_rotate_ref);
1093 // Transfer the relevant target frame to the output
1098 frame[target_layer]->copy_from(rotate_target_dst);
1102 frame[target_layer]->copy_from(global_target_dst);
1106 // Read the target destination directly
1108 read_frame(frame[target_layer],
1115 if(config.draw_vectors)
1117 draw_vectors(frame[target_layer]);
1121 printf("MotionMain::process_buffer %d\n", __LINE__);
1128 void MotionMain::draw_vectors(VFrame *frame)
1130 int w = frame->get_w();
1131 int h = frame->get_h();
1132 int global_x1, global_y1;
1133 int global_x2, global_y2;
1134 int block_x, block_y;
1135 int block_w, block_h;
1136 int block_x1, block_y1;
1137 int block_x2, block_y2;
1138 int block_x3, block_y3;
1139 int block_x4, block_y4;
1140 int search_w, search_h;
1141 int search_x1, search_y1;
1142 int search_x2, search_y2;
1148 // Start of vector is center of previous block.
1149 // End of vector is total accumulation.
1150 if(config.tracking_object == MotionScan::TRACK_SINGLE)
1152 global_x1 = (int64_t)(config.block_x *
1155 global_y1 = (int64_t)(config.block_y *
1158 global_x2 = global_x1 + total_dx / OVERSAMPLE;
1159 global_y2 = global_y1 + total_dy / OVERSAMPLE;
1160 //printf("MotionMain::draw_vectors %d %d %d %d %d %d\n", total_dx, total_dy, global_x1, global_y1, global_x2, global_y2);
1163 // Start of vector is center of previous block.
1164 // End of vector is current change.
1165 if(config.tracking_object == MotionScan::PREVIOUS_SAME_BLOCK)
1167 global_x1 = (int64_t)(config.block_x *
1170 global_y1 = (int64_t)(config.block_y *
1173 global_x2 = global_x1 + current_dx / OVERSAMPLE;
1174 global_y2 = global_y1 + current_dy / OVERSAMPLE;
1178 global_x1 = (int64_t)(config.block_x *
1181 (total_dx - current_dx) /
1183 global_y1 = (int64_t)(config.block_y *
1186 (total_dy - current_dy) /
1188 global_x2 = (int64_t)(config.block_x *
1193 global_y2 = (int64_t)(config.block_y *
1200 block_x = global_x1;
1201 block_y = global_y1;
1202 block_w = config.global_block_w * w / 100;
1203 block_h = config.global_block_h * h / 100;
1204 block_x1 = block_x - block_w / 2;
1205 block_y1 = block_y - block_h / 2;
1206 block_x2 = block_x + block_w / 2;
1207 block_y2 = block_y + block_h / 2;
1208 search_w = config.global_range_w * w / 100;
1209 search_h = config.global_range_h * h / 100;
1210 search_x1 = block_x1 - search_w / 2;
1211 search_y1 = block_y1 - search_h / 2;
1212 search_x2 = block_x2 + search_w / 2;
1213 search_y2 = block_y2 + search_h / 2;
1215 // printf("MotionMain::draw_vectors %d %d %d %d %d %d %d %d %d %d %d %d\n",
1229 MotionScan::clamp_scan(w,
1242 draw_arrow(frame, global_x1, global_y1, global_x2, global_y2);
1245 draw_line(frame, block_x1, block_y1, block_x2, block_y1);
1246 draw_line(frame, block_x2, block_y1, block_x2, block_y2);
1247 draw_line(frame, block_x2, block_y2, block_x1, block_y2);
1248 draw_line(frame, block_x1, block_y2, block_x1, block_y1);
1252 draw_line(frame, search_x1, search_y1, search_x2, search_y1);
1253 draw_line(frame, search_x2, search_y1, search_x2, search_y2);
1254 draw_line(frame, search_x2, search_y2, search_x1, search_y2);
1255 draw_line(frame, search_x1, search_y2, search_x1, search_y1);
1257 // Block should be endpoint of motion
1260 block_x = global_x2;
1261 block_y = global_y2;
1266 block_x = (int64_t)(config.block_x * w / 100);
1267 block_y = (int64_t)(config.block_y * h / 100);
1270 block_w = config.global_block_w * w / 100;
1271 block_h = config.global_block_h * h / 100;
1274 float angle = total_angle * 2 * M_PI / 360;
1275 double base_angle1 = atan((float)block_h / block_w);
1276 double base_angle2 = atan((float)block_w / block_h);
1277 double target_angle1 = base_angle1 + angle;
1278 double target_angle2 = base_angle2 + angle;
1279 double radius = sqrt(block_w * block_w + block_h * block_h) / 2;
1280 block_x1 = (int)(block_x - cos(target_angle1) * radius);
1281 block_y1 = (int)(block_y - sin(target_angle1) * radius);
1282 block_x2 = (int)(block_x + sin(target_angle2) * radius);
1283 block_y2 = (int)(block_y - cos(target_angle2) * radius);
1284 block_x3 = (int)(block_x - sin(target_angle2) * radius);
1285 block_y3 = (int)(block_y + cos(target_angle2) * radius);
1286 block_x4 = (int)(block_x + cos(target_angle1) * radius);
1287 block_y4 = (int)(block_y + sin(target_angle1) * radius);
1289 draw_line(frame, block_x1, block_y1, block_x2, block_y2);
1290 draw_line(frame, block_x2, block_y2, block_x4, block_y4);
1291 draw_line(frame, block_x4, block_y4, block_x3, block_y3);
1292 draw_line(frame, block_x3, block_y3, block_x1, block_y1);
1298 draw_line(frame, block_x, block_y - 5, block_x, block_y + 6);
1299 draw_line(frame, block_x - 5, block_y, block_x + 6, block_y);
1306 void MotionMain::draw_pixel(VFrame *frame, int x, int y)
1308 if(!(x >= 0 && y >= 0 && x < frame->get_w() && y < frame->get_h())) return;
1310 #define DRAW_PIXEL(x, y, components, do_yuv, max, type) \
1312 type **rows = (type**)frame->get_rows(); \
1313 rows[y][x * components] = max - rows[y][x * components]; \
1316 rows[y][x * components + 1] = max - rows[y][x * components + 1]; \
1317 rows[y][x * components + 2] = max - rows[y][x * components + 2]; \
1321 rows[y][x * components + 1] = (max / 2 + 1) - rows[y][x * components + 1]; \
1322 rows[y][x * components + 2] = (max / 2 + 1) - rows[y][x * components + 2]; \
1324 if(components == 4) \
1325 rows[y][x * components + 3] = max; \
1329 switch(frame->get_color_model())
1332 DRAW_PIXEL(x, y, 3, 0, 0xff, unsigned char);
1335 DRAW_PIXEL(x, y, 4, 0, 0xff, unsigned char);
1338 DRAW_PIXEL(x, y, 3, 0, 1.0, float);
1341 DRAW_PIXEL(x, y, 4, 0, 1.0, float);
1344 DRAW_PIXEL(x, y, 3, 1, 0xff, unsigned char);
1347 DRAW_PIXEL(x, y, 4, 1, 0xff, unsigned char);
1350 DRAW_PIXEL(x, y, 3, 0, 0xffff, uint16_t);
1353 DRAW_PIXEL(x, y, 3, 1, 0xffff, uint16_t);
1355 case BC_RGBA16161616:
1356 DRAW_PIXEL(x, y, 4, 0, 0xffff, uint16_t);
1358 case BC_YUVA16161616:
1359 DRAW_PIXEL(x, y, 4, 1, 0xffff, uint16_t);
1365 void MotionMain::draw_line(VFrame *frame, int x1, int y1, int x2, int y2)
1367 int w = labs(x2 - x1);
1368 int h = labs(y2 - y1);
1369 //printf("MotionMain::draw_line 1 %d %d %d %d\n", x1, y1, x2, y2);
1373 draw_pixel(frame, x1, y1);
1378 // Flip coordinates so x1 < x2
1388 int numerator = y2 - y1;
1389 int denominator = x2 - x1;
1390 for(int i = x1; i < x2; i++)
1392 int y = y1 + (int64_t)(i - x1) * (int64_t)numerator / (int64_t)denominator;
1393 draw_pixel(frame, i, y);
1398 // Flip coordinates so y1 < y2
1408 int numerator = x2 - x1;
1409 int denominator = y2 - y1;
1410 for(int i = y1; i < y2; i++)
1412 int x = x1 + (int64_t)(i - y1) * (int64_t)numerator / (int64_t)denominator;
1413 draw_pixel(frame, x, i);
1416 //printf("MotionMain::draw_line 2\n");
1419 #define ARROW_SIZE 10
1420 void MotionMain::draw_arrow(VFrame *frame, int x1, int y1, int x2, int y2)
1422 double angle = atan((float)(y2 - y1) / (float)(x2 - x1));
1423 double angle1 = angle + (float)145 / 360 * 2 * 3.14159265;
1424 double angle2 = angle - (float)145 / 360 * 2 * 3.14159265;
1431 x3 = x2 - (int)(ARROW_SIZE * cos(angle1));
1432 y3 = y2 - (int)(ARROW_SIZE * sin(angle1));
1433 x4 = x2 - (int)(ARROW_SIZE * cos(angle2));
1434 y4 = y2 - (int)(ARROW_SIZE * sin(angle2));
1438 x3 = x2 + (int)(ARROW_SIZE * cos(angle1));
1439 y3 = y2 + (int)(ARROW_SIZE * sin(angle1));
1440 x4 = x2 + (int)(ARROW_SIZE * cos(angle2));
1441 y4 = y2 + (int)(ARROW_SIZE * sin(angle2));
1445 draw_line(frame, x1, y1, x2, y2);
1446 // draw_line(frame, x1, y1 + 1, x2, y2 + 1);
1449 if(abs(y2 - y1) || abs(x2 - x1)) draw_line(frame, x2, y2, x3, y3);
1450 // draw_line(frame, x2, y2 + 1, x3, y3 + 1);
1452 if(abs(y2 - y1) || abs(x2 - x1)) draw_line(frame, x2, y2, x4, y4);
1453 // draw_line(frame, x2, y2 + 1, x4, y4 + 1);
1471 RotateScanPackage::RotateScanPackage()
1476 RotateScanUnit::RotateScanUnit(RotateScan *server, MotionMain *plugin)
1477 : LoadClient(server)
1479 this->server = server;
1480 this->plugin = plugin;
1485 RotateScanUnit::~RotateScanUnit()
1491 void RotateScanUnit::process_package(LoadPackage *package)
1493 if(server->skip) return;
1494 RotateScanPackage *pkg = (RotateScanPackage*)package;
1496 if((pkg->difference = server->get_cache(pkg->angle)) < 0)
1498 //printf("RotateScanUnit::process_package %d\n", __LINE__);
1499 int color_model = server->previous_frame->get_color_model();
1500 int pixel_size = BC_CModels::calculate_pixelsize(color_model);
1501 int row_bytes = server->previous_frame->get_bytes_per_line();
1504 rotater = new AffineEngine(1, 1);
1505 if(!temp) temp = new VFrame(0,
1507 server->previous_frame->get_w(),
1508 server->previous_frame->get_h(),
1511 //printf("RotateScanUnit::process_package %d\n", __LINE__);
1514 // Rotate original block size
1515 // rotater->set_viewport(server->block_x1,
1516 // server->block_y1,
1517 // server->block_x2 - server->block_x1,
1518 // server->block_y2 - server->block_y1);
1519 rotater->set_in_viewport(server->block_x1,
1521 server->block_x2 - server->block_x1,
1522 server->block_y2 - server->block_y1);
1523 rotater->set_out_viewport(server->block_x1,
1525 server->block_x2 - server->block_x1,
1526 server->block_y2 - server->block_y1);
1527 // rotater->set_pivot(server->block_x, server->block_y);
1528 rotater->set_in_pivot(server->block_x, server->block_y);
1529 rotater->set_out_pivot(server->block_x, server->block_y);
1530 //printf("RotateScanUnit::process_package %d\n", __LINE__);
1531 rotater->rotate(temp,
1532 server->previous_frame,
1535 // Scan reduced block size
1536 //plugin->output_frame->copy_from(server->current_frame);
1537 //plugin->output_frame->copy_from(temp);
1538 // printf("RotateScanUnit::process_package %d %d %d %d %d\n",
1544 // Clamp coordinates
1545 int x1 = server->scan_x;
1546 int y1 = server->scan_y;
1547 int x2 = x1 + server->scan_w;
1548 int y2 = y1 + server->scan_h;
1549 x2 = MIN(temp->get_w(), x2);
1550 y2 = MIN(temp->get_h(), y2);
1551 x2 = MIN(server->current_frame->get_w(), x2);
1552 y2 = MIN(server->current_frame->get_h(), y2);
1556 if(x2 > x1 && y2 > y1)
1558 pkg->difference = MotionScan::abs_diff(
1559 temp->get_rows()[y1] + x1 * pixel_size,
1560 server->current_frame->get_rows()[y1] + x1 * pixel_size,
1565 //printf("RotateScanUnit::process_package %d\n", __LINE__);
1566 server->put_cache(pkg->angle, pkg->difference);
1569 VFrame png(x2-x1, y2-y1, BC_RGB888, -1);
1570 png.transfer_from(temp, 0, x1, y1, x2-x1, y2-y1);
1572 sprintf(fn,"%s%f.png","/tmp/temp",pkg->angle); png.write_png(fn);
1573 png.transfer_from(server->current_frame, 0, x1, y1, x2-x1, y2-y1);
1574 sprintf(fn,"%s%f.png","/tmp/curr",pkg->angle); png.write_png(fn);
1575 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",
1576 server->block_x1, server->block_y1, server->block_x2 - server->block_x1, server->block_y2 - server->block_y1,
1577 server->block_x, server->block_y, pkg->angle, server->scan_w, server->scan_h, pkg->difference);
1603 RotateScan::RotateScan(MotionMain *plugin,
1608 total_clients, total_packages
1611 this->plugin = plugin;
1612 cache_lock = new Mutex("RotateScan::cache_lock");
1616 RotateScan::~RotateScan()
1621 void RotateScan::init_packages()
1623 for(int i = 0; i < get_total_packages(); i++)
1625 RotateScanPackage *pkg = (RotateScanPackage*)get_package(i);
1627 (scan_angle2 - scan_angle1) /
1633 LoadClient* RotateScan::new_client()
1635 return new RotateScanUnit(this, plugin);
1638 LoadPackage* RotateScan::new_package()
1640 return new RotateScanPackage;
1644 float RotateScan::scan_frame(VFrame *previous_frame,
1645 VFrame *current_frame,
1650 this->block_x = block_x;
1651 this->block_y = block_y;
1653 //printf("RotateScan::scan_frame %d\n", __LINE__);
1654 switch(plugin->config.tracking_type)
1656 case MotionScan::NO_CALCULATE:
1657 result = plugin->config.rotation_center;
1661 case MotionScan::LOAD:
1663 char string[BCTEXTLEN];
1664 sprintf(string, "%s%06jd",
1665 ROTATION_FILE, plugin->get_source_position());
1666 FILE *input = fopen(string, "r");
1669 fscanf(input, "%f", &result);
1675 perror("RotateScan::scan_frame LOAD");
1688 this->previous_frame = previous_frame;
1689 this->current_frame = current_frame;
1690 int w = current_frame->get_w();
1691 int h = current_frame->get_h();
1692 int block_w = w * plugin->config.global_block_w / 100;
1693 int block_h = h * plugin->config.global_block_h / 100;
1695 if(this->block_x - block_w / 2 < 0) block_w = this->block_x * 2;
1696 if(this->block_y - block_h / 2 < 0) block_h = this->block_y * 2;
1697 if(this->block_x + block_w / 2 > w) block_w = (w - this->block_x) * 2;
1698 if(this->block_y + block_h / 2 > h) block_h = (h - this->block_y) * 2;
1700 block_x1 = this->block_x - block_w / 2;
1701 block_x2 = this->block_x + block_w / 2;
1702 block_y1 = this->block_y - block_h / 2;
1703 block_y2 = this->block_y + block_h / 2;
1705 // Calculate the maximum area available to scan after rotation.
1706 // Must be calculated from the starting range because of cache.
1707 // Get coords of rectangle after rotation.
1708 double center_x = this->block_x;
1709 double center_y = this->block_y;
1710 double max_angle = plugin->config.rotation_range;
1711 double base_angle1 = atan((float)block_h / block_w);
1712 double base_angle2 = atan((float)block_w / block_h);
1713 double target_angle1 = base_angle1 + max_angle * 2 * M_PI / 360;
1714 double target_angle2 = base_angle2 + max_angle * 2 * M_PI / 360;
1715 double radius = sqrt(block_w * block_w + block_h * block_h) / 2;
1716 double x1 = center_x - cos(target_angle1) * radius;
1717 double y1 = center_y - sin(target_angle1) * radius;
1718 double x2 = center_x + sin(target_angle2) * radius;
1719 double y2 = center_y - cos(target_angle2) * radius;
1720 double x3 = center_x - sin(target_angle2) * radius;
1721 double y3 = center_y + cos(target_angle2) * radius;
1723 // Track top edge to find greatest area.
1724 double max_area1 = 0;
1725 //double max_x1 = 0;
1727 for(double x = x1; x < x2; x++)
1729 double y = y1 + (y2 - y1) * (x - x1) / (x2 - x1);
1730 if(x >= center_x && x < block_x2 && y >= block_y1 && y < center_y)
1732 double area = fabs(x - center_x) * fabs(y - center_y);
1733 if(area > max_area1)
1742 // Track left edge to find greatest area.
1743 double max_area2 = 0;
1745 //double max_y2 = 0;
1746 for(double y = y1; y < y3; y++)
1748 double x = x1 + (x3 - x1) * (y - y1) / (y3 - y1);
1749 if(x >= block_x1 && x < center_x && y >= block_y1 && y < center_y)
1751 double area = fabs(x - center_x) * fabs(y - center_y);
1752 if(area > max_area2)
1761 double max_x, max_y;
1765 // Get reduced scan coords
1766 scan_w = (int)(fabs(max_x - center_x) * 2);
1767 scan_h = (int)(fabs(max_y - center_y) * 2);
1768 scan_x = (int)(center_x - scan_w / 2);
1769 scan_y = (int)(center_y - scan_h / 2);
1770 // printf("RotateScan::scan_frame center=%d,%d scan=%d,%d %dx%d\n",
1771 // this->block_x, this->block_y, scan_x, scan_y, scan_w, scan_h);
1772 // printf(" angle_range=%f block= %d,%d,%d,%d\n", max_angle, block_x1, block_y1, block_x2, block_y2);
1774 // Determine min angle from size of block
1775 double angle1 = atan((double)block_h / block_w);
1776 double angle2 = atan((double)(block_h - 1) / (block_w + 1));
1777 double min_angle = fabs(angle2 - angle1) / OVERSAMPLE;
1778 min_angle = MAX(min_angle, MIN_ANGLE);
1780 //printf("RotateScan::scan_frame %d min_angle=%f\n", __LINE__, min_angle * 360 / 2 / M_PI);
1782 cache.remove_all_objects();
1787 if(previous_frame->data_matches(current_frame))
1789 //printf("RotateScan::scan_frame: frames match. Skipping.\n");
1790 result = plugin->config.rotation_center;
1797 // Initial search range
1798 float angle_range = max_angle;
1799 result = plugin->config.rotation_center;
1800 total_steps = plugin->config.rotate_positions;
1803 while(angle_range >= min_angle * total_steps)
1805 scan_angle1 = result - angle_range;
1806 scan_angle2 = result + angle_range;
1809 set_package_count(total_steps);
1810 //set_package_count(1);
1813 int64_t min_difference = -1;
1814 for(int i = 0; i < get_total_packages(); i++)
1816 RotateScanPackage *pkg = (RotateScanPackage*)get_package(i);
1817 if(pkg->difference < min_difference || min_difference == -1)
1819 min_difference = pkg->difference;
1820 result = pkg->angle;
1831 //printf("RotateScan::scan_frame %d\n", __LINE__);
1833 if(!skip && plugin->config.tracking_type == MotionScan::SAVE)
1835 char string[BCTEXTLEN];
1836 sprintf(string, "%s%06jd",
1837 ROTATION_FILE, plugin->get_source_position());
1838 FILE *output = fopen(string, "w");
1841 fprintf(output, "%f\n", result);
1846 perror("RotateScan::scan_frame SAVE");
1850 //printf("RotateScan::scan_frame %d angle=%f\n", __LINE__, result);
1857 int64_t RotateScan::get_cache(float angle)
1859 int64_t result = -1;
1860 cache_lock->lock("RotateScan::get_cache");
1861 for(int i = 0; i < cache.total; i++)
1863 RotateScanCache *ptr = cache.values[i];
1864 if(fabs(ptr->angle - angle) <= MIN_ANGLE)
1866 result = ptr->difference;
1870 cache_lock->unlock();
1874 void RotateScan::put_cache(float angle, int64_t difference)
1876 RotateScanCache *ptr = new RotateScanCache(angle, difference);
1877 cache_lock->lock("RotateScan::put_cache");
1879 cache_lock->unlock();
1890 RotateScanCache::RotateScanCache(float angle, int64_t difference)
1892 this->angle = angle;
1893 this->difference = difference;