4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "../motion/motionscan.h"
24 #include "bcdisplayinfo.h"
25 #include "bcsignals.h"
33 #include "motionwindow.h"
35 #include "overlayframe.h"
36 #include "rotateframe.h"
37 #include "transportque.h"
43 REGISTER_PLUGIN(MotionMain2)
52 MotionConfig::MotionConfig()
54 for(int i = 0; i < TOTAL_POINTS; i++)
57 global_range_w[i] = 5;
58 global_range_h[i] = 5;
59 global_origin_x[i] = 0;
60 global_origin_y[i] = 0;
61 global_block_w[i] = MIN_BLOCK;
62 global_block_h[i] = MIN_BLOCK;
68 global_positions = 256;
71 action = MotionScan::STABILIZE;
72 calculation = MotionScan::NO_CALCULATE;
73 tracking_object = MotionScan::TRACK_SINGLE;
80 void MotionConfig::boundaries()
82 for(int i = 0; i < TOTAL_POINTS; i++)
84 CLAMP(global_range_w[i], MIN_RADIUS, MAX_RADIUS);
85 CLAMP(global_range_h[i], MIN_RADIUS, MAX_RADIUS);
86 CLAMP(global_origin_x[i], MIN_ORIGIN, MAX_ORIGIN);
87 CLAMP(global_origin_y[i], MIN_ORIGIN, MAX_ORIGIN);
88 CLAMP(global_block_w[i], MIN_BLOCK, MAX_BLOCK);
89 CLAMP(global_block_h[i], MIN_BLOCK, MAX_BLOCK);
94 int MotionConfig::equivalent(MotionConfig &that)
97 for(int i = 0; i < TOTAL_POINTS; i++)
99 if(global[i] != that.global[i] ||
100 global_range_w[i] != that.global_range_w[i] ||
101 global_range_h[i] != that.global_range_h[i] ||
102 draw_vectors[i] != that.draw_vectors[i] ||
103 global_block_w[i] != that.global_block_w[i] ||
104 global_block_h[i] != that.global_block_h[i] ||
105 global_origin_x[i] != that.global_origin_x[i] ||
106 global_origin_y[i] != that.global_origin_y[i] ||
107 !EQUIV(block_x[i], that.block_x[i]) ||
108 !EQUIV(block_y[i], that.block_y[i]))
112 if(magnitude != that.magnitude ||
113 return_speed != that.return_speed ||
114 action != that.action ||
115 calculation != that.calculation ||
116 tracking_object != that.tracking_object ||
117 track_frame != that.track_frame ||
118 bottom_is_master != that.bottom_is_master ||
119 horizontal_only != that.horizontal_only ||
120 vertical_only != that.vertical_only ||
121 global_positions != that.global_positions) result = 0;
126 void MotionConfig::copy_from(MotionConfig &that)
128 for(int i = 0; i < TOTAL_POINTS; i++)
130 global[i] = that.global[i];
131 global_range_w[i] = that.global_range_w[i];
132 global_range_h[i] = that.global_range_h[i];
133 global_origin_x[i] = that.global_origin_x[i];
134 global_origin_y[i] = that.global_origin_y[i];
135 draw_vectors[i] = that.draw_vectors[i];
136 block_x[i] = that.block_x[i];
137 block_y[i] = that.block_y[i];
138 global_block_w[i] = that.global_block_w[i];
139 global_block_h[i] = that.global_block_h[i];
142 global_positions = that.global_positions;
143 magnitude = that.magnitude;
144 return_speed = that.return_speed;
145 action = that.action;
146 calculation = that.calculation;
147 tracking_object = that.tracking_object;
148 track_frame = that.track_frame;
149 bottom_is_master = that.bottom_is_master;
150 horizontal_only = that.horizontal_only;
151 vertical_only = that.vertical_only;
154 void MotionConfig::interpolate(MotionConfig &prev,
158 int64_t current_frame)
160 //double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
161 //double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
163 for(int i = 0; i < TOTAL_POINTS; i++)
165 this->global[i] = prev.global[i];
166 this->block_x[i] = prev.block_x[i];
167 this->block_y[i] = prev.block_y[i];
168 this->global_range_w[i] = prev.global_range_w[i];
169 this->global_range_h[i] = prev.global_range_h[i];
170 this->global_origin_x[i] = prev.global_origin_x[i];
171 this->global_origin_y[i] = prev.global_origin_y[i];
172 this->draw_vectors[i] = prev.draw_vectors[i];
173 this->global_block_w[i] = prev.global_block_w[i];
174 this->global_block_h[i] = prev.global_block_h[i];
177 this->global_positions = prev.global_positions;
178 magnitude = prev.magnitude;
179 return_speed = prev.return_speed;
180 action = prev.action;
181 calculation = prev.calculation;
182 tracking_object = prev.tracking_object;
183 track_frame = prev.track_frame;
184 bottom_is_master = prev.bottom_is_master;
185 horizontal_only = prev.horizontal_only;
186 vertical_only = prev.vertical_only;
207 MotionMain2::MotionMain2(PluginServer *server)
208 : PluginVClient(server)
213 for(int i = 0; i < TOTAL_POINTS; i++)
222 previous_frame_number = -1;
225 current_global_ref = 0;
226 global_target_src = 0;
227 global_target_dst = 0;
230 MotionMain2::~MotionMain2()
236 delete [] search_area;
240 delete prev_global_ref;
241 delete current_global_ref;
242 delete global_target_src;
243 delete global_target_dst;
246 const char* MotionMain2::plugin_title() { return _("Motion 2 Point"); }
247 int MotionMain2::is_realtime() { return 1; }
248 int MotionMain2::is_multichannel() { return 1; }
251 NEW_WINDOW_MACRO(MotionMain2, MotionWindow)
253 LOAD_CONFIGURATION_MACRO(MotionMain2, MotionConfig)
257 void MotionMain2::update_gui()
261 if(load_configuration())
263 thread->window->lock_window("MotionMain2::update_gui");
265 char string[BCTEXTLEN];
267 for(int i = 0; i < TOTAL_POINTS; i++)
269 ((MotionWindow*)thread->window)->global[i]->update(config.global[i]);
271 ((MotionWindow*)thread->window)->global_block_w[i]->update(config.global_block_w[i]);
272 ((MotionWindow*)thread->window)->global_block_h[i]->update(config.global_block_h[i]);
273 ((MotionWindow*)thread->window)->global_origin_x[i]->update(config.global_origin_x[i]);
274 ((MotionWindow*)thread->window)->global_origin_y[i]->update(config.global_origin_y[i]);
275 ((MotionWindow*)thread->window)->block_x[i]->update(config.block_x[i]);
276 ((MotionWindow*)thread->window)->block_y[i]->update(config.block_y[i]);
277 ((MotionWindow*)thread->window)->block_x_text[i]->update((float)config.block_x[i]);
278 ((MotionWindow*)thread->window)->block_y_text[i]->update((float)config.block_y[i]);
279 ((MotionWindow*)thread->window)->vectors[i]->update(config.draw_vectors[i]);
282 sprintf(string, "%d", config.global_positions);
283 ((MotionWindow*)thread->window)->global_search_positions->set_text(string);
284 ((MotionWindow*)thread->window)->magnitude->update(config.magnitude);
285 ((MotionWindow*)thread->window)->return_speed->update(config.return_speed);
288 ((MotionWindow*)thread->window)->track_single->update(config.tracking_object == MotionScan::TRACK_SINGLE);
289 ((MotionWindow*)thread->window)->track_frame_number->update(config.track_frame);
290 ((MotionWindow*)thread->window)->track_previous->update(config.tracking_object == MotionScan::TRACK_PREVIOUS);
291 ((MotionWindow*)thread->window)->previous_same->update(config.tracking_object == MotionScan::PREVIOUS_SAME_BLOCK);
292 if(config.tracking_object != MotionScan::TRACK_SINGLE)
293 ((MotionWindow*)thread->window)->track_frame_number->disable();
295 ((MotionWindow*)thread->window)->track_frame_number->enable();
297 ((MotionWindow*)thread->window)->action->set_text(
298 Action::to_text(config.action));
299 ((MotionWindow*)thread->window)->calculation->set_text(
300 Calculation::to_text(config.calculation));
301 ((MotionWindow*)thread->window)->tracking_direction->set_text(
302 TrackingDirection::to_text(config.horizontal_only, config.vertical_only));
303 ((MotionWindow*)thread->window)->master_layer->set_text(
304 MasterLayer::to_text(config.bottom_is_master));
307 ((MotionWindow*)thread->window)->update_mode();
308 thread->window->unlock_window();
317 void MotionMain2::save_data(KeyFrame *keyframe)
321 // cause data to be stored directly in text
322 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
323 output.tag.set_title("MOTION2");
325 char string[BCTEXTLEN];
326 for(int i = 0; i < TOTAL_POINTS; i++)
328 sprintf(string, "GLOBAL%d", i);
329 output.tag.set_property(string, config.global[i]);
330 sprintf(string, "GLOBAL_BLOCK_W%d", i);
331 output.tag.set_property(string, config.global_block_w[i]);
332 sprintf(string, "GLOBAL_BLOCK_H%d", i);
333 output.tag.set_property(string, config.global_block_h[i]);
334 sprintf(string, "BLOCK_X%d", i);
335 output.tag.set_property(string, config.block_x[i]);
336 sprintf(string, "BLOCK_Y%d", i);
337 output.tag.set_property(string, config.block_y[i]);
338 sprintf(string, "GLOBAL_RANGE_W%d", i);
339 output.tag.set_property(string, config.global_range_w[i]);
340 sprintf(string, "GLOBAL_RANGE_H%d", i);
341 output.tag.set_property(string, config.global_range_h[i]);
342 sprintf(string, "GLOBAL_ORIGIN_X%d", i);
343 output.tag.set_property(string, config.global_origin_x[i]);
344 sprintf(string, "GLOBAL_ORIGIN_Y%d", i);
345 output.tag.set_property(string, config.global_origin_y[i]);
346 sprintf(string, "DRAW_VECTORS%d", i);
347 output.tag.set_property(string, config.draw_vectors[i]);
350 output.tag.set_property("GLOBAL_POSITIONS", config.global_positions);
351 output.tag.set_property("MAGNITUDE", config.magnitude);
352 output.tag.set_property("RETURN_SPEED", config.return_speed);
353 output.tag.set_property("ACTION_TYPE", config.action);
354 output.tag.set_property("TRACKING_TYPE", config.calculation);
355 output.tag.set_property("TRACKING_OBJECT", config.tracking_object);
356 output.tag.set_property("TRACK_FRAME", config.track_frame);
357 output.tag.set_property("BOTTOM_IS_MASTER", config.bottom_is_master);
358 output.tag.set_property("HORIZONTAL_ONLY", config.horizontal_only);
359 output.tag.set_property("VERTICAL_ONLY", config.vertical_only);
361 output.terminate_string();
364 void MotionMain2::read_data(KeyFrame *keyframe)
368 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
374 result = input.read_tag();
378 if(input.tag.title_is("MOTION2"))
380 char string[BCTEXTLEN];
381 for(int i = 0; i < TOTAL_POINTS; i++)
383 sprintf(string, "GLOBAL%d", i);
384 config.global[i] = input.tag.get_property(string, config.global[i]);
385 sprintf(string, "GLOBAL_BLOCK_W%d", i);
386 config.global_block_w[i] = input.tag.get_property(string, config.global_block_w[i]);
387 sprintf(string, "GLOBAL_BLOCK_H%d", i);
388 config.global_block_h[i] = input.tag.get_property(string, config.global_block_h[i]);
389 sprintf(string, "BLOCK_X%d", i);
390 config.block_x[i] = input.tag.get_property(string, config.block_x[i]);
391 sprintf(string, "BLOCK_Y%d", i);
392 config.block_y[i] = input.tag.get_property(string, config.block_y[i]);
393 sprintf(string, "GLOBAL_RANGE_W%d", i);
394 config.global_range_w[i] = input.tag.get_property(string, config.global_range_w[i]);
395 sprintf(string, "GLOBAL_RANGE_H%d", i);
396 config.global_range_h[i] = input.tag.get_property(string, config.global_range_h[i]);
397 sprintf(string, "GLOBAL_ORIGIN_X%d", i);
398 config.global_origin_x[i] = input.tag.get_property(string, config.global_origin_x[i]);
399 sprintf(string, "GLOBAL_ORIGIN_Y%d", i);
400 config.global_origin_y[i] = input.tag.get_property(string, config.global_origin_y[i]);
401 sprintf(string, "DRAW_VECTORS%d", i);
402 config.draw_vectors[i] = input.tag.get_property(string, config.draw_vectors[i]);
405 config.global_positions = input.tag.get_property("GLOBAL_POSITIONS", config.global_positions);
406 config.magnitude = input.tag.get_property("MAGNITUDE", config.magnitude);
407 config.return_speed = input.tag.get_property("RETURN_SPEED", config.return_speed);
408 config.action = input.tag.get_property("ACTION_TYPE", config.action);
409 config.calculation = input.tag.get_property("TRACKING_TYPE", config.calculation);
410 config.tracking_object = input.tag.get_property("TRACKING_OBJECT", config.tracking_object);
411 config.track_frame = input.tag.get_property("TRACK_FRAME", config.track_frame);
412 config.bottom_is_master = input.tag.get_property("BOTTOM_IS_MASTER", config.bottom_is_master);
413 config.horizontal_only = input.tag.get_property("HORIZONTAL_ONLY", config.horizontal_only);
414 config.vertical_only = input.tag.get_property("VERTICAL_ONLY", config.vertical_only);
429 void MotionMain2::allocate_temp(int w, int h, int color_model)
432 (temp_frame->get_w() != w ||
433 temp_frame->get_h() != h))
439 temp_frame = new VFrame(w, h, color_model);
444 void MotionMain2::scan_motion(int point)
446 if(!engine) engine = new MotionScan(PluginClient::get_project_smp() + 1,
447 PluginClient::get_project_smp() + 1);
449 // Get the current motion vector between the previous and current frame
450 engine->scan_frame(current_global_ref,
452 config.global_range_w[point],
453 config.global_range_h[point],
454 config.global_block_w[point],
455 config.global_block_h[point],
456 config.block_x[point],
457 config.block_y[point],
458 config.tracking_object,
461 config.horizontal_only,
462 config.vertical_only,
463 get_source_position(),
464 config.global_positions,
467 config.global_origin_x[point],
468 config.global_origin_y[point]);
474 current_dx[point] = engine->dx_result;
475 current_dy[point] = engine->dy_result;
477 // Add current motion vector to accumulation vector.
478 if(config.tracking_object != MotionScan::TRACK_SINGLE)
481 total_dx[point] = (int64_t)total_dx[point] * (100 - config.return_speed) / 100;
482 total_dy[point] = (int64_t)total_dy[point] * (100 - config.return_speed) / 100;
483 total_dx[point] += engine->dx_result;
484 total_dy[point] += engine->dy_result;
487 // Make accumulation vector current
489 total_dx[point] = engine->dx_result;
490 total_dy[point] = engine->dy_result;
491 // printf("MotionMain2::scan_motion %d %d %d %d\n",
498 // Clamp accumulation vector
499 if(config.magnitude < 100)
501 //int block_w = (int64_t)config.global_block_w[point] *
502 // current_global_ref->get_w() / 100;
503 //int block_h = (int64_t)config.global_block_h[point] *
504 // current_global_ref->get_h() / 100;
505 int block_x_orig = (int64_t)(config.block_x[point] *
506 current_global_ref->get_w() /
508 int block_y_orig = (int64_t)(config.block_y[point] *
509 current_global_ref->get_h() /
512 int max_block_x = (int64_t)(current_global_ref->get_w() - block_x_orig) *
516 int max_block_y = (int64_t)(current_global_ref->get_h() - block_y_orig) *
520 int min_block_x = (int64_t)-block_x_orig *
524 int min_block_y = (int64_t)-block_y_orig *
529 CLAMP(total_dx[point], min_block_x, max_block_x);
530 CLAMP(total_dy[point], min_block_y, max_block_y);
533 // printf("MotionMain2::scan_motion %d %d %d %d\n",
545 void MotionMain2::apply_motion()
547 if(config.tracking_object != MotionScan::TRACK_SINGLE)
549 // Transfer current reference frame to previous reference frame and update
551 prev_global_ref->copy_from(current_global_ref);
552 previous_frame_number = get_source_position();
555 // Decide what to do with target based on requested operation
556 //int interpolation; // variable set but not used
557 float origin_x[TOTAL_POINTS];
558 float origin_y[TOTAL_POINTS];
559 float end_x[TOTAL_POINTS];
560 float end_y[TOTAL_POINTS];
561 for(int i = 0; i < TOTAL_POINTS; i++)
563 get_current_vector(&origin_x[i],
572 // Calculate rotation if 2 points
576 if(config.global[ROTATION_POINT])
578 if(origin_x[1] - origin_x[0])
579 angle0 = atan((double)(origin_y[0] - origin_y[1]) /
580 (double)(origin_x[0] - origin_x[1]));
581 if(end_x[1] - end_x[0])
582 angle = atan((double)(end_y[0] - end_y[1]) /
583 (double)(end_x[0] - end_x[1]));
584 // printf("MotionMain2::apply_motion %d angle0=%f angle=%f\n",
586 // angle0 * 360 / 2 / M_PI,
587 // angle * 360 / 2 / M_PI);
592 // zoom = DISTANCE(origin_x[1], origin_y[1], origin_x[0], origin_y[0]) /
593 // DISTANCE(end_x[1], end_y[1], end_x[0], end_y[0]);
597 printf("MotionMain2::apply_motion %d total_dx=%.02f total_dy=%.02f angle=%f zoom=%f\n",
599 (float)total_dx[TRANSLATION_POINT] / OVERSAMPLE,
600 (float)total_dy[TRANSLATION_POINT] / OVERSAMPLE,
601 angle * 360 / 2 / M_PI,
604 // Calculate translation
607 switch(config.action)
609 case MotionScan::NOTHING:
610 global_target_dst->copy_from(global_target_src);
612 case MotionScan::TRACK:
613 //interpolation = CUBIC_LINEAR;
614 dx = (float)total_dx[0] / OVERSAMPLE;
615 dy = (float)total_dy[0] / OVERSAMPLE;
617 case MotionScan::TRACK_PIXEL:
618 //interpolation = NEAREST_NEIGHBOR;
619 dx = (int)(total_dx[0] / OVERSAMPLE);
620 dy = (int)(total_dy[0] / OVERSAMPLE);
622 //interpolation = NEAREST_NEIGHBOR;
623 dx = -(int)(total_dx[0] / OVERSAMPLE);
624 dy = -(int)(total_dy[0] / OVERSAMPLE);
627 case MotionScan::STABILIZE:
628 //interpolation = CUBIC_LINEAR;
629 dx = -(float)total_dx[0] / OVERSAMPLE;
630 dy = -(float)total_dy[0] / OVERSAMPLE;
639 if(config.action != MotionScan::NOTHING)
641 double w = get_output()->get_w();
642 double h = get_output()->get_h();
643 double pivot_x = end_x[0];
644 double pivot_y = end_y[0];
645 double angle1 = atan((double)pivot_y / (double)pivot_x) + angle;
646 double angle2 = atan((double)(w - pivot_x) / (double)pivot_y) + angle;
647 double angle3 = atan((double)(h - pivot_y) / (double)(w - pivot_x)) + angle;
648 double angle4 = atan((double)pivot_x / (double)(h - pivot_y)) + angle;
649 double radius1 = DISTANCE(0, 0, pivot_x, pivot_y) * zoom;
650 double radius2 = DISTANCE(w, 0, pivot_x, pivot_y) * zoom;
651 double radius3 = DISTANCE(w, h, pivot_x, pivot_y) * zoom;
652 double radius4 = DISTANCE(0, h, pivot_x, pivot_y) * zoom;
655 float x1 = (dx + pivot_x - cos(angle1) * radius1) * 100 / w;
656 float y1 = (dy + pivot_y - sin(angle1) * radius1) * 100 / h;
657 float x2 = (dx + pivot_x + sin(angle2) * radius2) * 100 / w;
658 float y2 = (dy + pivot_y - cos(angle2) * radius2) * 100 / h;
659 float x3 = (dx + pivot_x + cos(angle3) * radius3) * 100 / w;
660 float y3 = (dy + pivot_y + sin(angle3) * radius3) * 100 / h;
661 float x4 = (dx + pivot_x - sin(angle4) * radius4) * 100 / w;
662 float y4 = (dy + pivot_y + cos(angle4) * radius4) * 100 / h;
666 affine = new AffineEngine(PluginClient::get_project_smp() + 1,
667 PluginClient::get_project_smp() + 1);
668 global_target_dst->clear_frame();
671 // printf("MotionMain2::apply_motion %d %.02f %.02f %.02f %.02f %.02f %.02f %.02f %.02f\n",
682 affine->process(global_target_dst,
685 AffineEngine::PERSPECTIVE,
703 int MotionMain2::process_buffer(VFrame **frame,
704 int64_t start_position,
707 int need_reconfigure = load_configuration();
708 int color_model = frame[0]->get_color_model();
709 w = frame[0]->get_w();
710 h = frame[0]->get_h();
714 printf("MotionMain2::process_buffer 1 start_position=" _LD "\n", start_position);
718 // Calculate the source and destination pointers for each of the operations.
719 // Get the layer to track motion in.
720 reference_layer = config.bottom_is_master ?
721 PluginClient::total_in_buffers - 1 :
723 // Get the layer to apply motion in.
724 target_layer = config.bottom_is_master ?
726 PluginClient::total_in_buffers - 1;
729 output_frame = frame[target_layer];
732 // Get the position of previous reference frame.
733 int64_t actual_previous_number;
734 // Skip if match frame not available
735 int skip_current = 0;
738 if(config.tracking_object == MotionScan::TRACK_SINGLE)
740 actual_previous_number = config.track_frame;
741 if(get_direction() == PLAY_REVERSE)
742 actual_previous_number++;
743 if(actual_previous_number == start_position)
748 actual_previous_number = start_position;
749 if(get_direction() == PLAY_FORWARD)
751 actual_previous_number--;
752 if(actual_previous_number < get_source_start())
756 KeyFrame *keyframe = get_prev_keyframe(start_position, 1);
757 if(keyframe->position > 0 &&
758 actual_previous_number < keyframe->position)
764 actual_previous_number++;
765 if(actual_previous_number >= get_source_start() + get_total_len())
769 KeyFrame *keyframe = get_next_keyframe(start_position, 1);
770 if(keyframe->position > 0 &&
771 actual_previous_number >= keyframe->position)
776 // Only count motion since last keyframe
782 // Point 0 must be tracked for any other points to be tracked
783 // Action and Calculation must be something
784 if( !config.global[0] || ( config.action == MotionScan::NOTHING &&
785 config.calculation == MotionScan::NO_CALCULATE) )
789 // printf("process_buffer %d %lld %lld %d\n",
791 // previous_frame_number,
792 // actual_previous_number,
793 // need_reconfigure);
794 // Load match frame and reset vectors
795 int need_reload = !skip_current &&
796 (previous_frame_number != actual_previous_number ||
800 for(int i = 0; i < TOTAL_POINTS; i++)
805 previous_frame_number = actual_previous_number;
811 for(int i = 0; i < TOTAL_POINTS; i++)
823 // Get the global pointers. Here we walk through the sequence of events.
826 // Global reads previous frame and compares
827 // with current frame to get the current translation.
828 // The center of the search area is fixed in compensate mode or
829 // the user value + the accumulation vector in track mode.
831 prev_global_ref = new VFrame(w, h, color_model);
832 if(!current_global_ref)
833 current_global_ref = new VFrame(w, h, color_model);
835 // Global loads the current target frame into the src and
836 // writes it to the dst frame with desired translation.
837 if(!global_target_src)
838 global_target_src = new VFrame(w, h, color_model);
839 if(!global_target_dst)
840 global_target_dst = new VFrame(w, h, color_model);
843 // Load the global frames
846 read_frame(prev_global_ref,
848 previous_frame_number,
852 read_frame(current_global_ref,
856 read_frame(global_target_src,
871 // Get position change from previous frame to current frame
874 for(int i = 0; i < TOTAL_POINTS; i++)
875 if(config.global[i]) scan_motion(i);
884 //printf("MotionMain2::process_buffer 90 %d\n", skip_current);
886 // Transfer the relevant target frame to the output
889 frame[target_layer]->copy_from(global_target_dst);
892 // Read the target destination directly
894 read_frame(frame[target_layer],
900 if(config.draw_vectors)
902 for(int i = 0; i < TOTAL_POINTS; i++)
903 draw_vectors(frame[target_layer], i);
907 printf("MotionMain2::process_buffer 100 skip_current=%d\n", skip_current);
916 void MotionMain2::clamp_scan(int w,
928 // printf("MotionMain2::clamp_scan 1 w=%d h=%d block=%d %d %d %d scan=%d %d %d %d absolute=%d\n",
943 // scan is always out of range before block.
946 int difference = -*scan_x1;
947 *block_x1 += difference;
953 int difference = -*scan_y1;
954 *block_y1 += difference;
960 int difference = *scan_x2 - w;
961 *block_x2 -= difference;
962 *scan_x2 -= difference;
967 int difference = *scan_y2 - h;
968 *block_y2 -= difference;
969 *scan_y2 -= difference;
972 CLAMP(*scan_x1, 0, w);
973 CLAMP(*scan_y1, 0, h);
974 CLAMP(*scan_x2, 0, w);
975 CLAMP(*scan_y2, 0, h);
981 int difference = -*scan_x1;
982 *block_x1 += difference;
983 *scan_x2 += difference;
989 int difference = -*scan_y1;
990 *block_y1 += difference;
991 *scan_y2 += difference;
995 if(*scan_x2 - *block_x1 + *block_x2 > w)
997 int difference = *scan_x2 - *block_x1 + *block_x2 - w;
998 *block_x2 -= difference;
1001 if(*scan_y2 - *block_y1 + *block_y2 > h)
1003 int difference = *scan_y2 - *block_y1 + *block_y2 - h;
1004 *block_y2 -= difference;
1007 // CLAMP(*scan_x1, 0, w - (*block_x2 - *block_x1));
1008 // CLAMP(*scan_y1, 0, h - (*block_y2 - *block_y1));
1009 // CLAMP(*scan_x2, 0, w - (*block_x2 - *block_x1));
1010 // CLAMP(*scan_y2, 0, h - (*block_y2 - *block_y1));
1013 // Sanity checks which break the calculation but should never happen if the
1014 // center of the block is inside the frame.
1015 CLAMP(*block_x1, 0, w);
1016 CLAMP(*block_x2, 0, w);
1017 CLAMP(*block_y1, 0, h);
1018 CLAMP(*block_y2, 0, h);
1020 // printf("MotionMain2::clamp_scan 2 w=%d h=%d block=%d %d %d %d scan=%d %d %d %d absolute=%d\n",
1037 void MotionMain2::get_current_vector(float *origin_x,
1045 int w = get_output()->get_w();
1046 int h = get_output()->get_h();
1053 if(!origin_x) origin_x = &temp1;
1054 if(!origin_y) origin_y = &temp2;
1055 if(!current_x1) current_x1 = &temp3;
1056 if(!current_y1) current_y1 = &temp4;
1057 if(!current_x2) current_x2 = &temp5;
1058 if(!current_y2) current_y2 = &temp6;
1068 if(config.global[point])
1071 // Start of vector is center of previous block.
1072 // End of vector is total accumulation.
1073 if(config.tracking_object == MotionScan::TRACK_SINGLE)
1075 (*origin_x) = (*current_x1) = ((float)config.block_x[point] *
1078 (*origin_y) = (*current_y1) = ((float)config.block_y[point] *
1081 (*current_x2) = (*current_x1) + (float)total_dx[point] / OVERSAMPLE;
1082 (*current_y2) = (*current_y1) + (float)total_dy[point] / OVERSAMPLE;
1085 // Start of vector is center of previous block.
1086 // End of vector is current change.
1087 if(config.tracking_object == MotionScan::PREVIOUS_SAME_BLOCK)
1089 (*origin_x) = (*current_x1) = ((float)config.block_x[point] *
1092 (*origin_y) = (*current_y1) = ((float)config.block_y[point] *
1095 (*current_x2) = (*origin_x) + (float)current_dx[point] / OVERSAMPLE;
1096 (*current_y2) = (*origin_y) + (float)current_dy[point] / OVERSAMPLE;
1100 (*origin_x) = (float)config.block_x[point] *
1103 (*origin_y) = (float)config.block_y[point] *
1106 (*current_x1) = ((*origin_x) +
1107 (float)(total_dx[point] - current_dx[point]) /
1109 (*current_y1) = ((*origin_y) +
1110 (float)(total_dy[point] - current_dy[point]) /
1112 (*current_x2) = ((*origin_x) +
1113 (float)total_dx[point] /
1115 (*current_y2) = ((*origin_y) +
1116 (float)total_dy[point] /
1123 void MotionMain2::draw_vectors(VFrame *frame, int point)
1125 int w = frame->get_w();
1126 int h = frame->get_h();
1127 float global_x1, global_y1;
1128 float global_x2, global_y2;
1129 int block_x, block_y;
1130 int block_w, block_h;
1131 int block_x1, block_y1;
1132 int block_x2, block_y2;
1133 int search_w, search_h;
1134 int search_x1, search_y1;
1135 int search_x2, search_y2;
1136 int origin_offset_x;
1137 int origin_offset_y;
1139 if(!config.draw_vectors[point]) return;
1142 if(config.global[point])
1145 get_current_vector(0,
1154 // Draw destination rectangle
1155 if(config.action == MotionScan::NOTHING ||
1156 config.action == MotionScan::TRACK)
1158 block_x = (int)global_x2;
1159 block_y = (int)global_y2;
1162 // Draw source rectangle
1164 block_x = (int)global_x1;
1165 block_y = (int)global_y1;
1167 block_w = config.global_block_w[point] * w / 100;
1168 block_h = config.global_block_h[point] * h / 100;
1169 origin_offset_x = config.global_origin_x[point] * w / 100;
1170 origin_offset_y = config.global_origin_y[point] * h / 100;
1171 block_x1 = block_x - block_w / 2;
1172 block_y1 = block_y - block_h / 2;
1173 block_x2 = block_x + block_w / 2;
1174 block_y2 = block_y + block_h / 2;
1175 search_w = config.global_range_w[point] * w / 100;
1176 search_h = config.global_range_h[point] * h / 100;
1177 search_x1 = block_x1 + origin_offset_x - search_w / 2;
1178 search_y1 = block_y1 + origin_offset_y - search_h / 2;
1179 search_x2 = block_x2 + origin_offset_x + search_w / 2;
1180 search_y2 = block_y2 + origin_offset_y + search_h / 2;
1182 // printf("MotionMain2::draw_vectors %d %d %d %d %d %d %d %d %d %d %d %d\n",
1196 MotionScan::clamp_scan(w,
1209 draw_arrow(frame, (int)global_x1, (int)global_y1, (int)global_x2, (int)global_y2);
1212 draw_line(frame, block_x1, block_y1, block_x2, block_y1);
1213 draw_line(frame, block_x2, block_y1, block_x2, block_y2);
1214 draw_line(frame, block_x2, block_y2, block_x1, block_y2);
1215 draw_line(frame, block_x1, block_y2, block_x1, block_y1);
1219 draw_line(frame, search_x1, search_y1, search_x2, search_y1);
1220 draw_line(frame, search_x2, search_y1, search_x2, search_y2);
1221 draw_line(frame, search_x2, search_y2, search_x1, search_y2);
1222 draw_line(frame, search_x1, search_y2, search_x1, search_y1);
1228 void MotionMain2::draw_pixel(VFrame *frame, int x, int y)
1230 if(!(x >= 0 && y >= 0 && x < frame->get_w() && y < frame->get_h())) return;
1232 #define DRAW_PIXEL(x, y, components, do_yuv, max, type) \
1234 type **rows = (type**)frame->get_rows(); \
1235 rows[y][x * components] = max - rows[y][x * components]; \
1238 rows[y][x * components + 1] = max - rows[y][x * components + 1]; \
1239 rows[y][x * components + 2] = max - rows[y][x * components + 2]; \
1243 rows[y][x * components + 1] = (max / 2 + 1) - rows[y][x * components + 1]; \
1244 rows[y][x * components + 2] = (max / 2 + 1) - rows[y][x * components + 2]; \
1246 if(components == 4) \
1247 rows[y][x * components + 3] = max; \
1251 switch(frame->get_color_model())
1254 DRAW_PIXEL(x, y, 3, 0, 0xff, unsigned char);
1257 DRAW_PIXEL(x, y, 4, 0, 0xff, unsigned char);
1260 DRAW_PIXEL(x, y, 3, 0, 1.0, float);
1263 DRAW_PIXEL(x, y, 4, 0, 1.0, float);
1266 DRAW_PIXEL(x, y, 3, 1, 0xff, unsigned char);
1269 DRAW_PIXEL(x, y, 4, 1, 0xff, unsigned char);
1272 DRAW_PIXEL(x, y, 3, 0, 0xffff, uint16_t);
1275 DRAW_PIXEL(x, y, 3, 1, 0xffff, uint16_t);
1277 case BC_RGBA16161616:
1278 DRAW_PIXEL(x, y, 4, 0, 0xffff, uint16_t);
1280 case BC_YUVA16161616:
1281 DRAW_PIXEL(x, y, 4, 1, 0xffff, uint16_t);
1287 void MotionMain2::draw_line(VFrame *frame, int x1, int y1, int x2, int y2)
1289 int w = labs(x2 - x1);
1290 int h = labs(y2 - y1);
1291 //printf("MotionMain2::draw_line 1 %d %d %d %d\n", x1, y1, x2, y2);
1295 draw_pixel(frame, x1, y1);
1300 // Flip coordinates so x1 < x2
1310 int numerator = y2 - y1;
1311 int denominator = x2 - x1;
1312 for(int i = x1; i < x2; i++)
1314 int y = y1 + (int64_t)(i - x1) * (int64_t)numerator / (int64_t)denominator;
1315 draw_pixel(frame, i, y);
1320 // Flip coordinates so y1 < y2
1330 int numerator = x2 - x1;
1331 int denominator = y2 - y1;
1332 for(int i = y1; i < y2; i++)
1334 int x = x1 + (int64_t)(i - y1) * (int64_t)numerator / (int64_t)denominator;
1335 draw_pixel(frame, x, i);
1338 //printf("MotionMain2::draw_line 2\n");
1341 #define ARROW_SIZE 10
1342 void MotionMain2::draw_arrow(VFrame *frame, int x1, int y1, int x2, int y2)
1344 double angle = atan((float)(y2 - y1) / (float)(x2 - x1));
1345 double angle1 = angle + (float)145 / 360 * 2 * 3.14159265;
1346 double angle2 = angle - (float)145 / 360 * 2 * 3.14159265;
1353 x3 = x2 - (int)(ARROW_SIZE * cos(angle1));
1354 y3 = y2 - (int)(ARROW_SIZE * sin(angle1));
1355 x4 = x2 - (int)(ARROW_SIZE * cos(angle2));
1356 y4 = y2 - (int)(ARROW_SIZE * sin(angle2));
1360 x3 = x2 + (int)(ARROW_SIZE * cos(angle1));
1361 y3 = y2 + (int)(ARROW_SIZE * sin(angle1));
1362 x4 = x2 + (int)(ARROW_SIZE * cos(angle2));
1363 y4 = y2 + (int)(ARROW_SIZE * sin(angle2));
1367 draw_line(frame, x1, y1, x2, y2);
1368 // draw_line(frame, x1, y1 + 1, x2, y2 + 1);
1371 if(abs(y2 - y1) || abs(x2 - x1)) draw_line(frame, x2, y2, x3, y3);
1372 // draw_line(frame, x2, y2 + 1, x3, y3 + 1);
1374 if(abs(y2 - y1) || abs(x2 - x1)) draw_line(frame, x2, y2, x4, y4);
1375 // draw_line(frame, x2, y2 + 1, x4, y4 + 1);
1391 #define ABS_DIFF(type, temp_type, multiplier, components) \
1393 temp_type result_temp = 0; \
1394 for(int i = 0; i < h; i++) \
1396 type *prev_row = (type*)prev_ptr; \
1397 type *current_row = (type*)current_ptr; \
1398 for(int j = 0; j < w; j++) \
1400 for(int k = 0; k < 3; k++) \
1402 temp_type difference; \
1403 difference = *prev_row++ - *current_row++; \
1404 if(difference < 0) \
1405 result_temp -= difference; \
1407 result_temp += difference; \
1409 if(components == 4) \
1415 prev_ptr += row_bytes; \
1416 current_ptr += row_bytes; \
1418 result = (int64_t)(result_temp * multiplier); \
1421 int64_t MotionMain2::abs_diff(unsigned char *prev_ptr,
1422 unsigned char *current_ptr,
1432 ABS_DIFF(unsigned char, int64_t, 1, 3)
1435 ABS_DIFF(unsigned char, int64_t, 1, 4)
1438 ABS_DIFF(float, double, 0x10000, 3)
1441 ABS_DIFF(float, double, 0x10000, 4)
1444 ABS_DIFF(unsigned char, int64_t, 1, 3)
1447 ABS_DIFF(unsigned char, int64_t, 1, 4)
1450 ABS_DIFF(uint16_t, int64_t, 1, 3)
1452 case BC_YUVA16161616:
1453 ABS_DIFF(uint16_t, int64_t, 1, 4)
1461 #define ABS_DIFF_SUB(type, temp_type, multiplier, components) \
1463 temp_type result_temp = 0; \
1464 temp_type y2_fraction = sub_y * 0x100 / OVERSAMPLE; \
1465 temp_type y1_fraction = 0x100 - y2_fraction; \
1466 temp_type x2_fraction = sub_x * 0x100 / OVERSAMPLE; \
1467 temp_type x1_fraction = 0x100 - x2_fraction; \
1468 for(int i = 0; i < h_sub; i++) \
1470 type *prev_row1 = (type*)prev_ptr; \
1471 type *prev_row2 = (type*)prev_ptr + components; \
1472 type *prev_row3 = (type*)(prev_ptr + row_bytes); \
1473 type *prev_row4 = (type*)(prev_ptr + row_bytes) + components; \
1474 type *current_row = (type*)current_ptr; \
1475 for(int j = 0; j < w_sub; j++) \
1477 for(int k = 0; k < 3; k++) \
1479 temp_type difference; \
1480 temp_type prev_value = \
1481 (*prev_row1++ * x1_fraction * y1_fraction + \
1482 *prev_row2++ * x2_fraction * y1_fraction + \
1483 *prev_row3++ * x1_fraction * y2_fraction + \
1484 *prev_row4++ * x2_fraction * y2_fraction) / \
1486 temp_type current_value = *current_row++; \
1487 difference = prev_value - current_value; \
1488 if(difference < 0) \
1489 result_temp -= difference; \
1491 result_temp += difference; \
1494 if(components == 4) \
1503 prev_ptr += row_bytes; \
1504 current_ptr += row_bytes; \
1506 result = (int64_t)(result_temp * multiplier); \
1512 int64_t MotionMain2::abs_diff_sub(unsigned char *prev_ptr,
1513 unsigned char *current_ptr,
1528 ABS_DIFF_SUB(unsigned char, int64_t, 1, 3)
1531 ABS_DIFF_SUB(unsigned char, int64_t, 1, 4)
1534 ABS_DIFF_SUB(float, double, 0x10000, 3)
1537 ABS_DIFF_SUB(float, double, 0x10000, 4)
1540 ABS_DIFF_SUB(unsigned char, int64_t, 1, 3)
1543 ABS_DIFF_SUB(unsigned char, int64_t, 1, 4)
1546 ABS_DIFF_SUB(uint16_t, int64_t, 1, 3)
1548 case BC_YUVA16161616:
1549 ABS_DIFF_SUB(uint16_t, int64_t, 1, 4)
1559 MotionScanPackage::MotionScanPackage()
1570 MotionScanUnit::MotionScanUnit(MotionScan *server,
1571 MotionMain2 *plugin)
1572 : LoadClient(server)
1574 this->plugin = plugin;
1575 this->server = server;
1576 cache_lock = new Mutex("MotionScanUnit::cache_lock");
1579 MotionScanUnit::~MotionScanUnit()
1586 void MotionScanUnit::process_package(LoadPackage *package)
1588 MotionScanPackage *pkg = (MotionScanPackage*)package;
1589 int w = server->current_frame->get_w();
1590 int h = server->current_frame->get_h();
1591 int color_model = server->current_frame->get_color_model();
1592 int pixel_size = cmodel_calculate_pixelsize(color_model);
1593 int row_bytes = server->current_frame->get_bytes_per_line();
1608 if(!server->subpixel)
1610 int search_x = pkg->scan_x1 + (pkg->pixel % (pkg->scan_x2 - pkg->scan_x1));
1611 int search_y = pkg->scan_y1 + (pkg->pixel / (pkg->scan_x2 - pkg->scan_x1));
1614 pkg->difference1 = server->get_cache(search_x, search_y);
1615 if(pkg->difference1 < 0)
1617 //printf("MotionScanUnit::process_package 1 %d %d\n",
1618 //search_x, search_y, pkg->block_x2 - pkg->block_x1, pkg->block_y2 - pkg->block_y1);
1619 // Pointers to first pixel in each block
1620 unsigned char *prev_ptr = server->previous_frame->get_rows()[
1622 search_x * pixel_size;
1623 unsigned char *current_ptr = server->current_frame->get_rows()[
1625 pkg->block_x1 * pixel_size;
1627 pkg->difference1 = plugin->abs_diff(prev_ptr,
1630 pkg->block_x2 - pkg->block_x1,
1631 pkg->block_y2 - pkg->block_y1,
1633 //printf("MotionScanUnit::process_package 2\n");
1634 server->put_cache(search_x, search_y, pkg->difference1);
1655 int sub_x = pkg->pixel % (OVERSAMPLE * 2 - 1) + 1;
1656 int sub_y = pkg->pixel / (OVERSAMPLE * 2 - 1) + 1;
1658 if(plugin->config.horizontal_only)
1663 if(plugin->config.vertical_only)
1668 int search_x = pkg->scan_x1 + sub_x / OVERSAMPLE;
1669 int search_y = pkg->scan_y1 + sub_y / OVERSAMPLE;
1670 sub_x %= OVERSAMPLE;
1671 sub_y %= OVERSAMPLE;
1674 unsigned char *prev_ptr = server->previous_frame->get_rows()[
1676 search_x * pixel_size;
1677 unsigned char *current_ptr = server->current_frame->get_rows()[
1679 pkg->block_x1 * pixel_size;
1681 // With subpixel, there are two ways to compare each position, one by shifting
1682 // the previous frame and two by shifting the current frame.
1683 pkg->difference1 = plugin->abs_diff_sub(prev_ptr,
1686 pkg->block_x2 - pkg->block_x1,
1687 pkg->block_y2 - pkg->block_y1,
1691 pkg->difference2 = plugin->abs_diff_sub(current_ptr,
1694 pkg->block_x2 - pkg->block_x1,
1695 pkg->block_y2 - pkg->block_y1,
1699 // printf("MotionScanUnit::process_package sub_x=%d sub_y=%d search_x=%d search_y=%d diff1=%lld diff2=%lld\n",
1704 // pkg->difference1,
1705 // pkg->difference2);
1723 int64_t MotionScanUnit::get_cache(int x, int y)
1725 int64_t result = -1;
1726 cache_lock->lock("MotionScanUnit::get_cache");
1727 for(int i = 0; i < cache.total; i++)
1729 MotionScanCache *ptr = cache.values[i];
1730 if(ptr->x == x && ptr->y == y)
1732 result = ptr->difference;
1736 cache_lock->unlock();
1740 void MotionScanUnit::put_cache(int x, int y, int64_t difference)
1742 MotionScanCache *ptr = new MotionScanCache(x, y, difference);
1743 cache_lock->lock("MotionScanUnit::put_cache");
1745 cache_lock->unlock();
1758 MotionScan::MotionScan(MotionMain2 *plugin,
1763 total_clients, total_packages
1766 this->plugin = plugin;
1767 cache_lock = new Mutex("MotionScan::cache_lock");
1770 MotionScan::~MotionScan()
1776 void MotionScan::init_packages()
1778 // Set package coords
1779 for(int i = 0; i < get_total_packages(); i++)
1781 MotionScanPackage *pkg = (MotionScanPackage*)get_package(i);
1783 pkg->block_x1 = block_x1;
1784 pkg->block_x2 = block_x2;
1785 pkg->block_y1 = block_y1;
1786 pkg->block_y2 = block_y2;
1787 pkg->scan_x1 = scan_x1;
1788 pkg->scan_x2 = scan_x2;
1789 pkg->scan_y1 = scan_y1;
1790 pkg->scan_y2 = scan_y2;
1791 pkg->pixel = (int64_t)i * (int64_t)total_pixels / (int64_t)total_steps;
1792 pkg->difference1 = 0;
1793 pkg->difference2 = 0;
1800 LoadClient* MotionScan::new_client()
1802 return new MotionScanUnit(this, plugin);
1805 LoadPackage* MotionScan::new_package()
1807 return new MotionScanPackage;
1811 void MotionScan::scan_frame(VFrame *previous_frame,
1812 VFrame *current_frame,
1815 this->previous_frame = previous_frame;
1816 this->current_frame = current_frame;
1817 this->point = point;
1820 cache.remove_all_objects();
1823 // Single macroblock
1824 int w = current_frame->get_w();
1825 int h = current_frame->get_h();
1827 // Initial search parameters
1828 int scan_w = w * plugin->config.global_range_w[point] / 100;
1829 int scan_h = h * plugin->config.global_range_h[point] / 100;
1830 int block_w = w * plugin->config.global_block_w[point] / 100;
1831 int block_h = h * plugin->config.global_block_h[point] / 100;
1833 // Location of block in previous frame
1834 block_x1 = (int)(w * plugin->config.block_x[point] / 100 - block_w / 2);
1835 block_y1 = (int)(h * plugin->config.block_y[point] / 100 - block_h / 2);
1836 block_x2 = (int)(w * plugin->config.block_x[point] / 100 + block_w / 2);
1837 block_y2 = (int)(h * plugin->config.block_y[point] / 100 + block_h / 2);
1839 // Offset to location of previous block. This offset needn't be very accurate
1840 // since it's the offset of the previous image and current image we want.
1841 if(plugin->config.tracking_object == MotionScan::TRACK_PREVIOUS)
1843 block_x1 += plugin->total_dx[point] / OVERSAMPLE;
1844 block_y1 += plugin->total_dy[point] / OVERSAMPLE;
1845 block_x2 += plugin->total_dx[point] / OVERSAMPLE;
1846 block_y2 += plugin->total_dy[point] / OVERSAMPLE;
1851 switch(plugin->config.calculation)
1854 case MotionScan::NO_CALCULATE:
1860 case MotionScan::LOAD:
1862 printf("MotionScan::scan_frame %d\n", __LINE__);
1863 // Load result from disk
1864 char string[BCTEXTLEN];
1865 sprintf(string, "%s%06d",
1867 plugin->get_source_position());
1869 input = fopen(string, "r");
1872 for(int i = 0; i <= point; i++)
1885 // Scan from scratch
1894 printf("MotionScan::scan_frame %d\n", __LINE__);
1895 // Calculate center of search area in current frame
1896 int origin_offset_x = plugin->config.global_origin_x[point] * w / 100;
1897 int origin_offset_y = plugin->config.global_origin_y[point] * h / 100;
1898 int x_result = block_x1 + origin_offset_x;
1899 int y_result = block_y1 + origin_offset_y;
1901 // printf("MotionScan::scan_frame 1 %d %d %d %d %d %d %d %d\n",
1902 // block_x1 + block_w / 2,
1903 // block_y1 + block_h / 2,
1913 scan_x1 = x_result - scan_w / 2;
1914 scan_y1 = y_result - scan_h / 2;
1915 scan_x2 = x_result + scan_w / 2;
1916 scan_y2 = y_result + scan_h / 2;
1920 // Zero out requested values
1921 if(plugin->config.horizontal_only)
1924 scan_y2 = block_y1 + 1;
1926 if(plugin->config.vertical_only)
1929 scan_x2 = block_x1 + 1;
1932 // printf("MotionScan::scan_frame 1 %d %d %d %d %d %d %d %d\n",
1941 // Clamp the block coords before the scan so we get useful scan coords.
1942 MotionScan::clamp_scan(w,
1953 // printf("MotionScan::scan_frame 1\n block_x1=%d block_y1=%d block_x2=%d block_y2=%d\n scan_x1=%d scan_y1=%d scan_x2=%d scan_y2=%d\n x_result=%d y_result=%d\n",
1966 // Give up if invalid coords.
1967 if(scan_y2 <= scan_y1 ||
1968 scan_x2 <= scan_x1 ||
1969 block_x2 <= block_x1 ||
1970 block_y2 <= block_y1)
1974 // For subpixel, the top row and left column are skipped
1978 if(plugin->config.horizontal_only ||
1979 plugin->config.vertical_only)
1981 total_pixels = 4 * OVERSAMPLE * OVERSAMPLE - 4 * OVERSAMPLE;
1985 total_pixels = 4 * OVERSAMPLE;
1988 total_steps = total_pixels;
1990 set_package_count(total_steps);
1993 // Get least difference
1994 int64_t min_difference = -1;
1995 for(int i = 0; i < get_total_packages(); i++)
1997 MotionScanPackage *pkg = (MotionScanPackage*)get_package(i);
1998 if(pkg->difference1 < min_difference || min_difference == -1)
2000 min_difference = pkg->difference1;
2002 if(plugin->config.vertical_only)
2003 x_result = scan_x1 * OVERSAMPLE;
2005 x_result = scan_x1 * OVERSAMPLE +
2006 (pkg->pixel % (OVERSAMPLE * 2 - 1)) + 1;
2008 if(plugin->config.horizontal_only)
2009 y_result = scan_y1 * OVERSAMPLE;
2011 y_result = scan_y1 * OVERSAMPLE +
2012 (pkg->pixel / (OVERSAMPLE * 2 - 1)) + 1;
2016 dx_result = block_x1 * OVERSAMPLE - x_result;
2017 dy_result = block_y1 * OVERSAMPLE - y_result;
2020 if(pkg->difference2 < min_difference)
2022 min_difference = pkg->difference2;
2024 if(plugin->config.vertical_only)
2025 x_result = scan_x1 * OVERSAMPLE;
2027 x_result = scan_x2 * OVERSAMPLE -
2028 ((pkg->pixel % (OVERSAMPLE * 2 - 1)) + 1);
2030 if(plugin->config.horizontal_only)
2031 y_result = scan_y1 * OVERSAMPLE;
2033 y_result = scan_y2 * OVERSAMPLE -
2034 ((pkg->pixel / (OVERSAMPLE * 2 - 1)) + 1);
2036 dx_result = block_x1 * OVERSAMPLE - x_result;
2037 dy_result = block_y1 * OVERSAMPLE - y_result;
2042 //printf("MotionScan::scan_frame 1 %d %d %d %d\n", block_x1, block_y1, x_result, y_result);
2048 total_pixels = (scan_x2 - scan_x1) * (scan_y2 - scan_y1);
2049 total_steps = MIN(plugin->config.global_positions, total_pixels);
2051 set_package_count(total_steps);
2054 // Get least difference
2055 int64_t min_difference = -1;
2056 for(int i = 0; i < get_total_packages(); i++)
2058 MotionScanPackage *pkg = (MotionScanPackage*)get_package(i);
2059 if(pkg->difference1 < min_difference || min_difference == -1)
2061 min_difference = pkg->difference1;
2062 x_result = scan_x1 + (pkg->pixel % (scan_x2 - scan_x1));
2063 y_result = scan_y1 + (pkg->pixel / (scan_x2 - scan_x1));
2064 x_result *= OVERSAMPLE;
2065 y_result *= OVERSAMPLE;
2071 // printf("MotionScan::scan_frame 10 total_steps=%d total_pixels=%d subpixel=%d\n",
2076 // printf(" scan w=%d h=%d scan x1=%d y1=%d x2=%d y2=%d\n",
2084 // printf("MotionScan::scan_frame 2 block x1=%d y1=%d x2=%d y2=%d result x=%.2f y=%.2f\n",
2089 // (float)x_result / 4,
2090 // (float)y_result / 4);
2093 // If a new search is required, rescale results back to pixels.
2094 if(total_steps >= total_pixels)
2096 // Single pixel accuracy reached. Now do exhaustive subpixel search.
2097 if(plugin->config.action == MotionScan::STABILIZE ||
2098 plugin->config.action == MotionScan::TRACK ||
2099 plugin->config.action == MotionScan::NOTHING)
2101 x_result /= OVERSAMPLE;
2102 y_result /= OVERSAMPLE;
2109 // Fill in results and quit
2110 dx_result = block_x1 * OVERSAMPLE - x_result;
2111 dy_result = block_y1 * OVERSAMPLE - y_result;
2116 // Reduce scan area and try again
2118 scan_w = (scan_x2 - scan_x1) / 2;
2119 scan_h = (scan_y2 - scan_y1) / 2;
2120 x_result /= OVERSAMPLE;
2121 y_result /= OVERSAMPLE;
2136 if(plugin->config.calculation == MotionScan::SAVE)
2138 char string[BCTEXTLEN];
2142 plugin->get_source_position());
2145 output = fopen(string, "w");
2147 output = fopen(string, "a");
2158 perror("MotionScan::scan_frame SAVE 1");
2163 printf("MotionScan::scan_frame 10 point=%d dx=%.2f dy=%.2f\n",
2165 (float)this->dx_result / OVERSAMPLE,
2166 (float)this->dy_result / OVERSAMPLE);
2186 int64_t MotionScan::get_cache(int x, int y)
2188 int64_t result = -1;
2189 cache_lock->lock("MotionScan::get_cache");
2190 for(int i = 0; i < cache.total; i++)
2192 MotionScanCache *ptr = cache.values[i];
2193 if(ptr->x == x && ptr->y == y)
2195 result = ptr->difference;
2199 cache_lock->unlock();
2203 void MotionScan::put_cache(int x, int y, int64_t difference)
2205 MotionScanCache *ptr = new MotionScanCache(x, y, difference);
2206 cache_lock->lock("MotionScan::put_cache");
2208 cache_lock->unlock();
2215 MotionScanCache::MotionScanCache(int x, int y, int64_t difference)
2219 this->difference = difference;