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"
32 #include "motionwindow.h"
34 #include "overlayframe.h"
35 #include "rotateframe.h"
36 #include "transportque.h"
42 REGISTER_PLUGIN(MotionMain2)
51 MotionConfig::MotionConfig()
53 for(int i = 0; i < TOTAL_POINTS; i++)
56 global_range_w[i] = 5;
57 global_range_h[i] = 5;
58 global_origin_x[i] = 0;
59 global_origin_y[i] = 0;
60 global_block_w[i] = MIN_BLOCK;
61 global_block_h[i] = MIN_BLOCK;
67 global_positions = 256;
70 action = MotionScan::STABILIZE;
71 calculation = MotionScan::NO_CALCULATE;
72 tracking_object = MotionScan::TRACK_SINGLE;
79 void MotionConfig::boundaries()
81 for(int i = 0; i < TOTAL_POINTS; i++)
83 CLAMP(global_range_w[i], MIN_RADIUS, MAX_RADIUS);
84 CLAMP(global_range_h[i], MIN_RADIUS, MAX_RADIUS);
85 CLAMP(global_origin_x[i], MIN_ORIGIN, MAX_ORIGIN);
86 CLAMP(global_origin_y[i], MIN_ORIGIN, MAX_ORIGIN);
87 CLAMP(global_block_w[i], MIN_BLOCK, MAX_BLOCK);
88 CLAMP(global_block_h[i], MIN_BLOCK, MAX_BLOCK);
93 int MotionConfig::equivalent(MotionConfig &that)
96 for(int i = 0; i < TOTAL_POINTS; i++)
98 if(global[i] != that.global[i] ||
99 global_range_w[i] != that.global_range_w[i] ||
100 global_range_h[i] != that.global_range_h[i] ||
101 draw_vectors[i] != that.draw_vectors[i] ||
102 global_block_w[i] != that.global_block_w[i] ||
103 global_block_h[i] != that.global_block_h[i] ||
104 global_origin_x[i] != that.global_origin_x[i] ||
105 global_origin_y[i] != that.global_origin_y[i] ||
106 !EQUIV(block_x[i], that.block_x[i]) ||
107 !EQUIV(block_y[i], that.block_y[i]))
111 if(magnitude != that.magnitude ||
112 return_speed != that.return_speed ||
113 action != that.action ||
114 calculation != that.calculation ||
115 tracking_object != that.tracking_object ||
116 track_frame != that.track_frame ||
117 bottom_is_master != that.bottom_is_master ||
118 horizontal_only != that.horizontal_only ||
119 vertical_only != that.vertical_only ||
120 global_positions != that.global_positions) result = 0;
125 void MotionConfig::copy_from(MotionConfig &that)
127 for(int i = 0; i < TOTAL_POINTS; i++)
129 global[i] = that.global[i];
130 global_range_w[i] = that.global_range_w[i];
131 global_range_h[i] = that.global_range_h[i];
132 global_origin_x[i] = that.global_origin_x[i];
133 global_origin_y[i] = that.global_origin_y[i];
134 draw_vectors[i] = that.draw_vectors[i];
135 block_x[i] = that.block_x[i];
136 block_y[i] = that.block_y[i];
137 global_block_w[i] = that.global_block_w[i];
138 global_block_h[i] = that.global_block_h[i];
141 global_positions = that.global_positions;
142 magnitude = that.magnitude;
143 return_speed = that.return_speed;
144 action = that.action;
145 calculation = that.calculation;
146 tracking_object = that.tracking_object;
147 track_frame = that.track_frame;
148 bottom_is_master = that.bottom_is_master;
149 horizontal_only = that.horizontal_only;
150 vertical_only = that.vertical_only;
153 void MotionConfig::interpolate(MotionConfig &prev,
157 int64_t current_frame)
159 //double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
160 //double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
162 for(int i = 0; i < TOTAL_POINTS; i++)
164 this->global[i] = prev.global[i];
165 this->block_x[i] = prev.block_x[i];
166 this->block_y[i] = prev.block_y[i];
167 this->global_range_w[i] = prev.global_range_w[i];
168 this->global_range_h[i] = prev.global_range_h[i];
169 this->global_origin_x[i] = prev.global_origin_x[i];
170 this->global_origin_y[i] = prev.global_origin_y[i];
171 this->draw_vectors[i] = prev.draw_vectors[i];
172 this->global_block_w[i] = prev.global_block_w[i];
173 this->global_block_h[i] = prev.global_block_h[i];
176 this->global_positions = prev.global_positions;
177 magnitude = prev.magnitude;
178 return_speed = prev.return_speed;
179 action = prev.action;
180 calculation = prev.calculation;
181 tracking_object = prev.tracking_object;
182 track_frame = prev.track_frame;
183 bottom_is_master = prev.bottom_is_master;
184 horizontal_only = prev.horizontal_only;
185 vertical_only = prev.vertical_only;
206 MotionMain2::MotionMain2(PluginServer *server)
207 : PluginVClient(server)
212 for(int i = 0; i < TOTAL_POINTS; i++)
221 previous_frame_number = -1;
224 current_global_ref = 0;
225 global_target_src = 0;
226 global_target_dst = 0;
229 MotionMain2::~MotionMain2()
235 delete [] search_area;
239 delete prev_global_ref;
240 delete current_global_ref;
241 delete global_target_src;
242 delete global_target_dst;
245 const char* MotionMain2::plugin_title() { return _("Motion 2 Point"); }
246 int MotionMain2::is_realtime() { return 1; }
247 int MotionMain2::is_multichannel() { return 1; }
250 NEW_WINDOW_MACRO(MotionMain2, MotionWindow)
252 LOAD_CONFIGURATION_MACRO(MotionMain2, MotionConfig)
256 void MotionMain2::update_gui()
260 if(load_configuration())
262 thread->window->lock_window("MotionMain2::update_gui");
264 char string[BCTEXTLEN];
266 for(int i = 0; i < TOTAL_POINTS; i++)
268 ((MotionWindow*)thread->window)->global[i]->update(config.global[i]);
270 ((MotionWindow*)thread->window)->global_block_w[i]->update(config.global_block_w[i]);
271 ((MotionWindow*)thread->window)->global_block_h[i]->update(config.global_block_h[i]);
272 ((MotionWindow*)thread->window)->global_origin_x[i]->update(config.global_origin_x[i]);
273 ((MotionWindow*)thread->window)->global_origin_y[i]->update(config.global_origin_y[i]);
274 ((MotionWindow*)thread->window)->block_x[i]->update(config.block_x[i]);
275 ((MotionWindow*)thread->window)->block_y[i]->update(config.block_y[i]);
276 ((MotionWindow*)thread->window)->block_x_text[i]->update((float)config.block_x[i]);
277 ((MotionWindow*)thread->window)->block_y_text[i]->update((float)config.block_y[i]);
278 ((MotionWindow*)thread->window)->vectors[i]->update(config.draw_vectors[i]);
281 sprintf(string, "%d", config.global_positions);
282 ((MotionWindow*)thread->window)->global_search_positions->set_text(string);
283 ((MotionWindow*)thread->window)->magnitude->update(config.magnitude);
284 ((MotionWindow*)thread->window)->return_speed->update(config.return_speed);
287 ((MotionWindow*)thread->window)->track_single->update(config.tracking_object == MotionScan::TRACK_SINGLE);
288 ((MotionWindow*)thread->window)->track_frame_number->update(config.track_frame);
289 ((MotionWindow*)thread->window)->track_previous->update(config.tracking_object == MotionScan::TRACK_PREVIOUS);
290 ((MotionWindow*)thread->window)->previous_same->update(config.tracking_object == MotionScan::PREVIOUS_SAME_BLOCK);
291 if(config.tracking_object != MotionScan::TRACK_SINGLE)
292 ((MotionWindow*)thread->window)->track_frame_number->disable();
294 ((MotionWindow*)thread->window)->track_frame_number->enable();
296 ((MotionWindow*)thread->window)->action->set_text(
297 Action::to_text(config.action));
298 ((MotionWindow*)thread->window)->calculation->set_text(
299 Calculation::to_text(config.calculation));
300 ((MotionWindow*)thread->window)->tracking_direction->set_text(
301 TrackingDirection::to_text(config.horizontal_only, config.vertical_only));
302 ((MotionWindow*)thread->window)->master_layer->set_text(
303 MasterLayer::to_text(config.bottom_is_master));
306 ((MotionWindow*)thread->window)->update_mode();
307 thread->window->unlock_window();
316 void MotionMain2::save_data(KeyFrame *keyframe)
320 // cause data to be stored directly in text
321 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
322 output.tag.set_title("MOTION2");
324 char string[BCTEXTLEN];
325 for(int i = 0; i < TOTAL_POINTS; i++)
327 sprintf(string, "GLOBAL%d", i);
328 output.tag.set_property(string, config.global[i]);
329 sprintf(string, "GLOBAL_BLOCK_W%d", i);
330 output.tag.set_property(string, config.global_block_w[i]);
331 sprintf(string, "GLOBAL_BLOCK_H%d", i);
332 output.tag.set_property(string, config.global_block_h[i]);
333 sprintf(string, "BLOCK_X%d", i);
334 output.tag.set_property(string, config.block_x[i]);
335 sprintf(string, "BLOCK_Y%d", i);
336 output.tag.set_property(string, config.block_y[i]);
337 sprintf(string, "GLOBAL_RANGE_W%d", i);
338 output.tag.set_property(string, config.global_range_w[i]);
339 sprintf(string, "GLOBAL_RANGE_H%d", i);
340 output.tag.set_property(string, config.global_range_h[i]);
341 sprintf(string, "GLOBAL_ORIGIN_X%d", i);
342 output.tag.set_property(string, config.global_origin_x[i]);
343 sprintf(string, "GLOBAL_ORIGIN_Y%d", i);
344 output.tag.set_property(string, config.global_origin_y[i]);
345 sprintf(string, "DRAW_VECTORS%d", i);
346 output.tag.set_property(string, config.draw_vectors[i]);
349 output.tag.set_property("GLOBAL_POSITIONS", config.global_positions);
350 output.tag.set_property("MAGNITUDE", config.magnitude);
351 output.tag.set_property("RETURN_SPEED", config.return_speed);
352 output.tag.set_property("ACTION_TYPE", config.action);
353 output.tag.set_property("TRACKING_TYPE", config.calculation);
354 output.tag.set_property("TRACKING_OBJECT", config.tracking_object);
355 output.tag.set_property("TRACK_FRAME", config.track_frame);
356 output.tag.set_property("BOTTOM_IS_MASTER", config.bottom_is_master);
357 output.tag.set_property("HORIZONTAL_ONLY", config.horizontal_only);
358 output.tag.set_property("VERTICAL_ONLY", config.vertical_only);
360 output.tag.set_title("/MOTION2");
362 output.append_newline();
363 output.terminate_string();
366 void MotionMain2::read_data(KeyFrame *keyframe)
370 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
376 result = input.read_tag();
380 if(input.tag.title_is("MOTION2"))
382 char string[BCTEXTLEN];
383 for(int i = 0; i < TOTAL_POINTS; i++)
385 sprintf(string, "GLOBAL%d", i);
386 config.global[i] = input.tag.get_property(string, config.global[i]);
387 sprintf(string, "GLOBAL_BLOCK_W%d", i);
388 config.global_block_w[i] = input.tag.get_property(string, config.global_block_w[i]);
389 sprintf(string, "GLOBAL_BLOCK_H%d", i);
390 config.global_block_h[i] = input.tag.get_property(string, config.global_block_h[i]);
391 sprintf(string, "BLOCK_X%d", i);
392 config.block_x[i] = input.tag.get_property(string, config.block_x[i]);
393 sprintf(string, "BLOCK_Y%d", i);
394 config.block_y[i] = input.tag.get_property(string, config.block_y[i]);
395 sprintf(string, "GLOBAL_RANGE_W%d", i);
396 config.global_range_w[i] = input.tag.get_property(string, config.global_range_w[i]);
397 sprintf(string, "GLOBAL_RANGE_H%d", i);
398 config.global_range_h[i] = input.tag.get_property(string, config.global_range_h[i]);
399 sprintf(string, "GLOBAL_ORIGIN_X%d", i);
400 config.global_origin_x[i] = input.tag.get_property(string, config.global_origin_x[i]);
401 sprintf(string, "GLOBAL_ORIGIN_Y%d", i);
402 config.global_origin_y[i] = input.tag.get_property(string, config.global_origin_y[i]);
403 sprintf(string, "DRAW_VECTORS%d", i);
404 config.draw_vectors[i] = input.tag.get_property(string, config.draw_vectors[i]);
407 config.global_positions = input.tag.get_property("GLOBAL_POSITIONS", config.global_positions);
408 config.magnitude = input.tag.get_property("MAGNITUDE", config.magnitude);
409 config.return_speed = input.tag.get_property("RETURN_SPEED", config.return_speed);
410 config.action = input.tag.get_property("ACTION_TYPE", config.action);
411 config.calculation = input.tag.get_property("TRACKING_TYPE", config.calculation);
412 config.tracking_object = input.tag.get_property("TRACKING_OBJECT", config.tracking_object);
413 config.track_frame = input.tag.get_property("TRACK_FRAME", config.track_frame);
414 config.bottom_is_master = input.tag.get_property("BOTTOM_IS_MASTER", config.bottom_is_master);
415 config.horizontal_only = input.tag.get_property("HORIZONTAL_ONLY", config.horizontal_only);
416 config.vertical_only = input.tag.get_property("VERTICAL_ONLY", config.vertical_only);
431 void MotionMain2::allocate_temp(int w, int h, int color_model)
434 (temp_frame->get_w() != w ||
435 temp_frame->get_h() != h))
441 temp_frame = new VFrame(w, h, color_model);
446 void MotionMain2::scan_motion(int point)
448 if(!engine) engine = new MotionScan(PluginClient::get_project_smp() + 1,
449 PluginClient::get_project_smp() + 1);
451 // Get the current motion vector between the previous and current frame
452 engine->scan_frame(current_global_ref,
454 config.global_range_w[point],
455 config.global_range_h[point],
456 config.global_block_w[point],
457 config.global_block_h[point],
458 config.block_x[point],
459 config.block_y[point],
460 config.tracking_object,
463 config.horizontal_only,
464 config.vertical_only,
465 get_source_position(),
466 config.global_positions,
469 config.global_origin_x[point],
470 config.global_origin_y[point]);
476 current_dx[point] = engine->dx_result;
477 current_dy[point] = engine->dy_result;
479 // Add current motion vector to accumulation vector.
480 if(config.tracking_object != MotionScan::TRACK_SINGLE)
483 total_dx[point] = (int64_t)total_dx[point] * (100 - config.return_speed) / 100;
484 total_dy[point] = (int64_t)total_dy[point] * (100 - config.return_speed) / 100;
485 total_dx[point] += engine->dx_result;
486 total_dy[point] += engine->dy_result;
489 // Make accumulation vector current
491 total_dx[point] = engine->dx_result;
492 total_dy[point] = engine->dy_result;
493 // printf("MotionMain2::scan_motion %d %d %d %d\n",
500 // Clamp accumulation vector
501 if(config.magnitude < 100)
503 //int block_w = (int64_t)config.global_block_w[point] *
504 // current_global_ref->get_w() / 100;
505 //int block_h = (int64_t)config.global_block_h[point] *
506 // current_global_ref->get_h() / 100;
507 int block_x_orig = (int64_t)(config.block_x[point] *
508 current_global_ref->get_w() /
510 int block_y_orig = (int64_t)(config.block_y[point] *
511 current_global_ref->get_h() /
514 int max_block_x = (int64_t)(current_global_ref->get_w() - block_x_orig) *
518 int max_block_y = (int64_t)(current_global_ref->get_h() - block_y_orig) *
522 int min_block_x = (int64_t)-block_x_orig *
526 int min_block_y = (int64_t)-block_y_orig *
531 CLAMP(total_dx[point], min_block_x, max_block_x);
532 CLAMP(total_dy[point], min_block_y, max_block_y);
535 // printf("MotionMain2::scan_motion %d %d %d %d\n",
547 void MotionMain2::apply_motion()
549 if(config.tracking_object != MotionScan::TRACK_SINGLE)
551 // Transfer current reference frame to previous reference frame and update
553 prev_global_ref->copy_from(current_global_ref);
554 previous_frame_number = get_source_position();
557 // Decide what to do with target based on requested operation
558 //int interpolation; // variable set but not used
559 float origin_x[TOTAL_POINTS];
560 float origin_y[TOTAL_POINTS];
561 float end_x[TOTAL_POINTS];
562 float end_y[TOTAL_POINTS];
563 for(int i = 0; i < TOTAL_POINTS; i++)
565 get_current_vector(&origin_x[i],
574 // Calculate rotation if 2 points
578 if(config.global[ROTATION_POINT])
580 if(origin_x[1] - origin_x[0])
581 angle0 = atan((double)(origin_y[0] - origin_y[1]) /
582 (double)(origin_x[0] - origin_x[1]));
583 if(end_x[1] - end_x[0])
584 angle = atan((double)(end_y[0] - end_y[1]) /
585 (double)(end_x[0] - end_x[1]));
586 // printf("MotionMain2::apply_motion %d angle0=%f angle=%f\n",
588 // angle0 * 360 / 2 / M_PI,
589 // angle * 360 / 2 / M_PI);
594 // zoom = DISTANCE(origin_x[1], origin_y[1], origin_x[0], origin_y[0]) /
595 // DISTANCE(end_x[1], end_y[1], end_x[0], end_y[0]);
599 printf("MotionMain2::apply_motion %d total_dx=%.02f total_dy=%.02f angle=%f zoom=%f\n",
601 (float)total_dx[TRANSLATION_POINT] / OVERSAMPLE,
602 (float)total_dy[TRANSLATION_POINT] / OVERSAMPLE,
603 angle * 360 / 2 / M_PI,
606 // Calculate translation
609 switch(config.action)
611 case MotionScan::NOTHING:
612 global_target_dst->copy_from(global_target_src);
614 case MotionScan::TRACK:
615 //interpolation = CUBIC_LINEAR;
616 dx = (float)total_dx[0] / OVERSAMPLE;
617 dy = (float)total_dy[0] / OVERSAMPLE;
619 case MotionScan::TRACK_PIXEL:
620 //interpolation = NEAREST_NEIGHBOR;
621 dx = (int)(total_dx[0] / OVERSAMPLE);
622 dy = (int)(total_dy[0] / OVERSAMPLE);
624 //interpolation = NEAREST_NEIGHBOR;
625 dx = -(int)(total_dx[0] / OVERSAMPLE);
626 dy = -(int)(total_dy[0] / OVERSAMPLE);
629 case MotionScan::STABILIZE:
630 //interpolation = CUBIC_LINEAR;
631 dx = -(float)total_dx[0] / OVERSAMPLE;
632 dy = -(float)total_dy[0] / OVERSAMPLE;
641 if(config.action != MotionScan::NOTHING)
643 double w = get_output()->get_w();
644 double h = get_output()->get_h();
645 double pivot_x = end_x[0];
646 double pivot_y = end_y[0];
647 double angle1 = atan((double)pivot_y / (double)pivot_x) + angle;
648 double angle2 = atan((double)(w - pivot_x) / (double)pivot_y) + angle;
649 double angle3 = atan((double)(h - pivot_y) / (double)(w - pivot_x)) + angle;
650 double angle4 = atan((double)pivot_x / (double)(h - pivot_y)) + angle;
651 double radius1 = DISTANCE(0, 0, pivot_x, pivot_y) * zoom;
652 double radius2 = DISTANCE(w, 0, pivot_x, pivot_y) * zoom;
653 double radius3 = DISTANCE(w, h, pivot_x, pivot_y) * zoom;
654 double radius4 = DISTANCE(0, h, pivot_x, pivot_y) * zoom;
657 float x1 = (dx + pivot_x - cos(angle1) * radius1) * 100 / w;
658 float y1 = (dy + pivot_y - sin(angle1) * radius1) * 100 / h;
659 float x2 = (dx + pivot_x + sin(angle2) * radius2) * 100 / w;
660 float y2 = (dy + pivot_y - cos(angle2) * radius2) * 100 / h;
661 float x3 = (dx + pivot_x + cos(angle3) * radius3) * 100 / w;
662 float y3 = (dy + pivot_y + sin(angle3) * radius3) * 100 / h;
663 float x4 = (dx + pivot_x - sin(angle4) * radius4) * 100 / w;
664 float y4 = (dy + pivot_y + cos(angle4) * radius4) * 100 / h;
668 affine = new AffineEngine(PluginClient::get_project_smp() + 1,
669 PluginClient::get_project_smp() + 1);
670 global_target_dst->clear_frame();
673 // printf("MotionMain2::apply_motion %d %.02f %.02f %.02f %.02f %.02f %.02f %.02f %.02f\n",
684 affine->process(global_target_dst,
687 AffineEngine::PERSPECTIVE,
705 int MotionMain2::process_buffer(VFrame **frame,
706 int64_t start_position,
709 int need_reconfigure = load_configuration();
710 int color_model = frame[0]->get_color_model();
711 w = frame[0]->get_w();
712 h = frame[0]->get_h();
716 printf("MotionMain2::process_buffer 1 start_position=%jd\n", start_position);
720 // Calculate the source and destination pointers for each of the operations.
721 // Get the layer to track motion in.
722 reference_layer = config.bottom_is_master ?
723 PluginClient::total_in_buffers - 1 :
725 // Get the layer to apply motion in.
726 target_layer = config.bottom_is_master ?
728 PluginClient::total_in_buffers - 1;
731 output_frame = frame[target_layer];
734 // Get the position of previous reference frame.
735 int64_t actual_previous_number;
736 // Skip if match frame not available
737 int skip_current = 0;
740 if(config.tracking_object == MotionScan::TRACK_SINGLE)
742 actual_previous_number = config.track_frame;
743 if(get_direction() == PLAY_REVERSE)
744 actual_previous_number++;
745 if(actual_previous_number == start_position)
750 actual_previous_number = start_position;
751 if(get_direction() == PLAY_FORWARD)
753 actual_previous_number--;
754 if(actual_previous_number < get_source_start())
758 KeyFrame *keyframe = get_prev_keyframe(start_position, 1);
759 if(keyframe->position > 0 &&
760 actual_previous_number < keyframe->position)
766 actual_previous_number++;
767 if(actual_previous_number >= get_source_start() + get_total_len())
771 KeyFrame *keyframe = get_next_keyframe(start_position, 1);
772 if(keyframe->position > 0 &&
773 actual_previous_number >= keyframe->position)
778 // Only count motion since last keyframe
784 // Point 0 must be tracked for any other points to be tracked
785 // Action and Calculation must be something
786 if( !config.global[0] || ( config.action == MotionScan::NOTHING &&
787 config.calculation == MotionScan::NO_CALCULATE) )
791 // printf("process_buffer %d %lld %lld %d\n",
793 // previous_frame_number,
794 // actual_previous_number,
795 // need_reconfigure);
796 // Load match frame and reset vectors
797 int need_reload = !skip_current &&
798 (previous_frame_number != actual_previous_number ||
802 for(int i = 0; i < TOTAL_POINTS; i++)
807 previous_frame_number = actual_previous_number;
813 for(int i = 0; i < TOTAL_POINTS; i++)
825 // Get the global pointers. Here we walk through the sequence of events.
828 // Global reads previous frame and compares
829 // with current frame to get the current translation.
830 // The center of the search area is fixed in compensate mode or
831 // the user value + the accumulation vector in track mode.
833 prev_global_ref = new VFrame(w, h, color_model);
834 if(!current_global_ref)
835 current_global_ref = new VFrame(w, h, color_model);
837 // Global loads the current target frame into the src and
838 // writes it to the dst frame with desired translation.
839 if(!global_target_src)
840 global_target_src = new VFrame(w, h, color_model);
841 if(!global_target_dst)
842 global_target_dst = new VFrame(w, h, color_model);
845 // Load the global frames
848 read_frame(prev_global_ref,
850 previous_frame_number,
855 read_frame(current_global_ref,
860 read_frame(global_target_src,
876 // Get position change from previous frame to current frame
879 for(int i = 0; i < TOTAL_POINTS; i++)
880 if(config.global[i]) scan_motion(i);
889 //printf("MotionMain2::process_buffer 90 %d\n", skip_current);
891 // Transfer the relevant target frame to the output
894 frame[target_layer]->copy_from(global_target_dst);
897 // Read the target destination directly
899 read_frame(frame[target_layer],
906 if(config.draw_vectors)
908 for(int i = 0; i < TOTAL_POINTS; i++)
909 draw_vectors(frame[target_layer], i);
913 printf("MotionMain2::process_buffer 100 skip_current=%d\n", skip_current);
922 void MotionMain2::clamp_scan(int w,
934 // printf("MotionMain2::clamp_scan 1 w=%d h=%d block=%d %d %d %d scan=%d %d %d %d absolute=%d\n",
949 // scan is always out of range before block.
952 int difference = -*scan_x1;
953 *block_x1 += difference;
959 int difference = -*scan_y1;
960 *block_y1 += difference;
966 int difference = *scan_x2 - w;
967 *block_x2 -= difference;
968 *scan_x2 -= difference;
973 int difference = *scan_y2 - h;
974 *block_y2 -= difference;
975 *scan_y2 -= difference;
978 CLAMP(*scan_x1, 0, w);
979 CLAMP(*scan_y1, 0, h);
980 CLAMP(*scan_x2, 0, w);
981 CLAMP(*scan_y2, 0, h);
987 int difference = -*scan_x1;
988 *block_x1 += difference;
989 *scan_x2 += difference;
995 int difference = -*scan_y1;
996 *block_y1 += difference;
997 *scan_y2 += difference;
1001 if(*scan_x2 - *block_x1 + *block_x2 > w)
1003 int difference = *scan_x2 - *block_x1 + *block_x2 - w;
1004 *block_x2 -= difference;
1007 if(*scan_y2 - *block_y1 + *block_y2 > h)
1009 int difference = *scan_y2 - *block_y1 + *block_y2 - h;
1010 *block_y2 -= difference;
1013 // CLAMP(*scan_x1, 0, w - (*block_x2 - *block_x1));
1014 // CLAMP(*scan_y1, 0, h - (*block_y2 - *block_y1));
1015 // CLAMP(*scan_x2, 0, w - (*block_x2 - *block_x1));
1016 // CLAMP(*scan_y2, 0, h - (*block_y2 - *block_y1));
1019 // Sanity checks which break the calculation but should never happen if the
1020 // center of the block is inside the frame.
1021 CLAMP(*block_x1, 0, w);
1022 CLAMP(*block_x2, 0, w);
1023 CLAMP(*block_y1, 0, h);
1024 CLAMP(*block_y2, 0, h);
1026 // printf("MotionMain2::clamp_scan 2 w=%d h=%d block=%d %d %d %d scan=%d %d %d %d absolute=%d\n",
1043 void MotionMain2::get_current_vector(float *origin_x,
1051 int w = get_output()->get_w();
1052 int h = get_output()->get_h();
1059 if(!origin_x) origin_x = &temp1;
1060 if(!origin_y) origin_y = &temp2;
1061 if(!current_x1) current_x1 = &temp3;
1062 if(!current_y1) current_y1 = &temp4;
1063 if(!current_x2) current_x2 = &temp5;
1064 if(!current_y2) current_y2 = &temp6;
1074 if(config.global[point])
1077 // Start of vector is center of previous block.
1078 // End of vector is total accumulation.
1079 if(config.tracking_object == MotionScan::TRACK_SINGLE)
1081 (*origin_x) = (*current_x1) = ((float)config.block_x[point] *
1084 (*origin_y) = (*current_y1) = ((float)config.block_y[point] *
1087 (*current_x2) = (*current_x1) + (float)total_dx[point] / OVERSAMPLE;
1088 (*current_y2) = (*current_y1) + (float)total_dy[point] / OVERSAMPLE;
1091 // Start of vector is center of previous block.
1092 // End of vector is current change.
1093 if(config.tracking_object == MotionScan::PREVIOUS_SAME_BLOCK)
1095 (*origin_x) = (*current_x1) = ((float)config.block_x[point] *
1098 (*origin_y) = (*current_y1) = ((float)config.block_y[point] *
1101 (*current_x2) = (*origin_x) + (float)current_dx[point] / OVERSAMPLE;
1102 (*current_y2) = (*origin_y) + (float)current_dy[point] / OVERSAMPLE;
1106 (*origin_x) = (float)config.block_x[point] *
1109 (*origin_y) = (float)config.block_y[point] *
1112 (*current_x1) = ((*origin_x) +
1113 (float)(total_dx[point] - current_dx[point]) /
1115 (*current_y1) = ((*origin_y) +
1116 (float)(total_dy[point] - current_dy[point]) /
1118 (*current_x2) = ((*origin_x) +
1119 (float)total_dx[point] /
1121 (*current_y2) = ((*origin_y) +
1122 (float)total_dy[point] /
1129 void MotionMain2::draw_vectors(VFrame *frame, int point)
1131 int w = frame->get_w();
1132 int h = frame->get_h();
1133 float global_x1, global_y1;
1134 float global_x2, global_y2;
1135 int block_x, block_y;
1136 int block_w, block_h;
1137 int block_x1, block_y1;
1138 int block_x2, block_y2;
1139 int search_w, search_h;
1140 int search_x1, search_y1;
1141 int search_x2, search_y2;
1142 int origin_offset_x;
1143 int origin_offset_y;
1145 if(!config.draw_vectors[point]) return;
1148 if(config.global[point])
1151 get_current_vector(0,
1160 // Draw destination rectangle
1161 if(config.action == MotionScan::NOTHING ||
1162 config.action == MotionScan::TRACK)
1164 block_x = (int)global_x2;
1165 block_y = (int)global_y2;
1168 // Draw source rectangle
1170 block_x = (int)global_x1;
1171 block_y = (int)global_y1;
1173 block_w = config.global_block_w[point] * w / 100;
1174 block_h = config.global_block_h[point] * h / 100;
1175 origin_offset_x = config.global_origin_x[point] * w / 100;
1176 origin_offset_y = config.global_origin_y[point] * h / 100;
1177 block_x1 = block_x - block_w / 2;
1178 block_y1 = block_y - block_h / 2;
1179 block_x2 = block_x + block_w / 2;
1180 block_y2 = block_y + block_h / 2;
1181 search_w = config.global_range_w[point] * w / 100;
1182 search_h = config.global_range_h[point] * h / 100;
1183 search_x1 = block_x1 + origin_offset_x - search_w / 2;
1184 search_y1 = block_y1 + origin_offset_y - search_h / 2;
1185 search_x2 = block_x2 + origin_offset_x + search_w / 2;
1186 search_y2 = block_y2 + origin_offset_y + search_h / 2;
1188 // printf("MotionMain2::draw_vectors %d %d %d %d %d %d %d %d %d %d %d %d\n",
1202 MotionScan::clamp_scan(w,
1215 draw_arrow(frame, (int)global_x1, (int)global_y1, (int)global_x2, (int)global_y2);
1218 draw_line(frame, block_x1, block_y1, block_x2, block_y1);
1219 draw_line(frame, block_x2, block_y1, block_x2, block_y2);
1220 draw_line(frame, block_x2, block_y2, block_x1, block_y2);
1221 draw_line(frame, block_x1, block_y2, block_x1, block_y1);
1225 draw_line(frame, search_x1, search_y1, search_x2, search_y1);
1226 draw_line(frame, search_x2, search_y1, search_x2, search_y2);
1227 draw_line(frame, search_x2, search_y2, search_x1, search_y2);
1228 draw_line(frame, search_x1, search_y2, search_x1, search_y1);
1234 void MotionMain2::draw_pixel(VFrame *frame, int x, int y)
1236 if(!(x >= 0 && y >= 0 && x < frame->get_w() && y < frame->get_h())) return;
1238 #define DRAW_PIXEL(x, y, components, do_yuv, max, type) \
1240 type **rows = (type**)frame->get_rows(); \
1241 rows[y][x * components] = max - rows[y][x * components]; \
1244 rows[y][x * components + 1] = max - rows[y][x * components + 1]; \
1245 rows[y][x * components + 2] = max - rows[y][x * components + 2]; \
1249 rows[y][x * components + 1] = (max / 2 + 1) - rows[y][x * components + 1]; \
1250 rows[y][x * components + 2] = (max / 2 + 1) - rows[y][x * components + 2]; \
1252 if(components == 4) \
1253 rows[y][x * components + 3] = max; \
1257 switch(frame->get_color_model())
1260 DRAW_PIXEL(x, y, 3, 0, 0xff, unsigned char);
1263 DRAW_PIXEL(x, y, 4, 0, 0xff, unsigned char);
1266 DRAW_PIXEL(x, y, 3, 0, 1.0, float);
1269 DRAW_PIXEL(x, y, 4, 0, 1.0, float);
1272 DRAW_PIXEL(x, y, 3, 1, 0xff, unsigned char);
1275 DRAW_PIXEL(x, y, 4, 1, 0xff, unsigned char);
1278 DRAW_PIXEL(x, y, 3, 0, 0xffff, uint16_t);
1281 DRAW_PIXEL(x, y, 3, 1, 0xffff, uint16_t);
1283 case BC_RGBA16161616:
1284 DRAW_PIXEL(x, y, 4, 0, 0xffff, uint16_t);
1286 case BC_YUVA16161616:
1287 DRAW_PIXEL(x, y, 4, 1, 0xffff, uint16_t);
1293 void MotionMain2::draw_line(VFrame *frame, int x1, int y1, int x2, int y2)
1295 int w = labs(x2 - x1);
1296 int h = labs(y2 - y1);
1297 //printf("MotionMain2::draw_line 1 %d %d %d %d\n", x1, y1, x2, y2);
1301 draw_pixel(frame, x1, y1);
1306 // Flip coordinates so x1 < x2
1316 int numerator = y2 - y1;
1317 int denominator = x2 - x1;
1318 for(int i = x1; i < x2; i++)
1320 int y = y1 + (int64_t)(i - x1) * (int64_t)numerator / (int64_t)denominator;
1321 draw_pixel(frame, i, y);
1326 // Flip coordinates so y1 < y2
1336 int numerator = x2 - x1;
1337 int denominator = y2 - y1;
1338 for(int i = y1; i < y2; i++)
1340 int x = x1 + (int64_t)(i - y1) * (int64_t)numerator / (int64_t)denominator;
1341 draw_pixel(frame, x, i);
1344 //printf("MotionMain2::draw_line 2\n");
1347 #define ARROW_SIZE 10
1348 void MotionMain2::draw_arrow(VFrame *frame, int x1, int y1, int x2, int y2)
1350 double angle = atan((float)(y2 - y1) / (float)(x2 - x1));
1351 double angle1 = angle + (float)145 / 360 * 2 * 3.14159265;
1352 double angle2 = angle - (float)145 / 360 * 2 * 3.14159265;
1359 x3 = x2 - (int)(ARROW_SIZE * cos(angle1));
1360 y3 = y2 - (int)(ARROW_SIZE * sin(angle1));
1361 x4 = x2 - (int)(ARROW_SIZE * cos(angle2));
1362 y4 = y2 - (int)(ARROW_SIZE * sin(angle2));
1366 x3 = x2 + (int)(ARROW_SIZE * cos(angle1));
1367 y3 = y2 + (int)(ARROW_SIZE * sin(angle1));
1368 x4 = x2 + (int)(ARROW_SIZE * cos(angle2));
1369 y4 = y2 + (int)(ARROW_SIZE * sin(angle2));
1373 draw_line(frame, x1, y1, x2, y2);
1374 // draw_line(frame, x1, y1 + 1, x2, y2 + 1);
1377 if(abs(y2 - y1) || abs(x2 - x1)) draw_line(frame, x2, y2, x3, y3);
1378 // draw_line(frame, x2, y2 + 1, x3, y3 + 1);
1380 if(abs(y2 - y1) || abs(x2 - x1)) draw_line(frame, x2, y2, x4, y4);
1381 // draw_line(frame, x2, y2 + 1, x4, y4 + 1);
1397 #define ABS_DIFF(type, temp_type, multiplier, components) \
1399 temp_type result_temp = 0; \
1400 for(int i = 0; i < h; i++) \
1402 type *prev_row = (type*)prev_ptr; \
1403 type *current_row = (type*)current_ptr; \
1404 for(int j = 0; j < w; j++) \
1406 for(int k = 0; k < 3; k++) \
1408 temp_type difference; \
1409 difference = *prev_row++ - *current_row++; \
1410 if(difference < 0) \
1411 result_temp -= difference; \
1413 result_temp += difference; \
1415 if(components == 4) \
1421 prev_ptr += row_bytes; \
1422 current_ptr += row_bytes; \
1424 result = (int64_t)(result_temp * multiplier); \
1427 int64_t MotionMain2::abs_diff(unsigned char *prev_ptr,
1428 unsigned char *current_ptr,
1438 ABS_DIFF(unsigned char, int64_t, 1, 3)
1441 ABS_DIFF(unsigned char, int64_t, 1, 4)
1444 ABS_DIFF(float, double, 0x10000, 3)
1447 ABS_DIFF(float, double, 0x10000, 4)
1450 ABS_DIFF(unsigned char, int64_t, 1, 3)
1453 ABS_DIFF(unsigned char, int64_t, 1, 4)
1456 ABS_DIFF(uint16_t, int64_t, 1, 3)
1458 case BC_YUVA16161616:
1459 ABS_DIFF(uint16_t, int64_t, 1, 4)
1467 #define ABS_DIFF_SUB(type, temp_type, multiplier, components) \
1469 temp_type result_temp = 0; \
1470 temp_type y2_fraction = sub_y * 0x100 / OVERSAMPLE; \
1471 temp_type y1_fraction = 0x100 - y2_fraction; \
1472 temp_type x2_fraction = sub_x * 0x100 / OVERSAMPLE; \
1473 temp_type x1_fraction = 0x100 - x2_fraction; \
1474 for(int i = 0; i < h_sub; i++) \
1476 type *prev_row1 = (type*)prev_ptr; \
1477 type *prev_row2 = (type*)prev_ptr + components; \
1478 type *prev_row3 = (type*)(prev_ptr + row_bytes); \
1479 type *prev_row4 = (type*)(prev_ptr + row_bytes) + components; \
1480 type *current_row = (type*)current_ptr; \
1481 for(int j = 0; j < w_sub; j++) \
1483 for(int k = 0; k < 3; k++) \
1485 temp_type difference; \
1486 temp_type prev_value = \
1487 (*prev_row1++ * x1_fraction * y1_fraction + \
1488 *prev_row2++ * x2_fraction * y1_fraction + \
1489 *prev_row3++ * x1_fraction * y2_fraction + \
1490 *prev_row4++ * x2_fraction * y2_fraction) / \
1492 temp_type current_value = *current_row++; \
1493 difference = prev_value - current_value; \
1494 if(difference < 0) \
1495 result_temp -= difference; \
1497 result_temp += difference; \
1500 if(components == 4) \
1509 prev_ptr += row_bytes; \
1510 current_ptr += row_bytes; \
1512 result = (int64_t)(result_temp * multiplier); \
1518 int64_t MotionMain2::abs_diff_sub(unsigned char *prev_ptr,
1519 unsigned char *current_ptr,
1534 ABS_DIFF_SUB(unsigned char, int64_t, 1, 3)
1537 ABS_DIFF_SUB(unsigned char, int64_t, 1, 4)
1540 ABS_DIFF_SUB(float, double, 0x10000, 3)
1543 ABS_DIFF_SUB(float, double, 0x10000, 4)
1546 ABS_DIFF_SUB(unsigned char, int64_t, 1, 3)
1549 ABS_DIFF_SUB(unsigned char, int64_t, 1, 4)
1552 ABS_DIFF_SUB(uint16_t, int64_t, 1, 3)
1554 case BC_YUVA16161616:
1555 ABS_DIFF_SUB(uint16_t, int64_t, 1, 4)
1565 MotionScanPackage::MotionScanPackage()
1576 MotionScanUnit::MotionScanUnit(MotionScan *server,
1577 MotionMain2 *plugin)
1578 : LoadClient(server)
1580 this->plugin = plugin;
1581 this->server = server;
1582 cache_lock = new Mutex("MotionScanUnit::cache_lock");
1585 MotionScanUnit::~MotionScanUnit()
1592 void MotionScanUnit::process_package(LoadPackage *package)
1594 MotionScanPackage *pkg = (MotionScanPackage*)package;
1595 int w = server->current_frame->get_w();
1596 int h = server->current_frame->get_h();
1597 int color_model = server->current_frame->get_color_model();
1598 int pixel_size = cmodel_calculate_pixelsize(color_model);
1599 int row_bytes = server->current_frame->get_bytes_per_line();
1614 if(!server->subpixel)
1616 int search_x = pkg->scan_x1 + (pkg->pixel % (pkg->scan_x2 - pkg->scan_x1));
1617 int search_y = pkg->scan_y1 + (pkg->pixel / (pkg->scan_x2 - pkg->scan_x1));
1620 pkg->difference1 = server->get_cache(search_x, search_y);
1621 if(pkg->difference1 < 0)
1623 //printf("MotionScanUnit::process_package 1 %d %d\n",
1624 //search_x, search_y, pkg->block_x2 - pkg->block_x1, pkg->block_y2 - pkg->block_y1);
1625 // Pointers to first pixel in each block
1626 unsigned char *prev_ptr = server->previous_frame->get_rows()[
1628 search_x * pixel_size;
1629 unsigned char *current_ptr = server->current_frame->get_rows()[
1631 pkg->block_x1 * pixel_size;
1633 pkg->difference1 = plugin->abs_diff(prev_ptr,
1636 pkg->block_x2 - pkg->block_x1,
1637 pkg->block_y2 - pkg->block_y1,
1639 //printf("MotionScanUnit::process_package 2\n");
1640 server->put_cache(search_x, search_y, pkg->difference1);
1661 int sub_x = pkg->pixel % (OVERSAMPLE * 2 - 1) + 1;
1662 int sub_y = pkg->pixel / (OVERSAMPLE * 2 - 1) + 1;
1664 if(plugin->config.horizontal_only)
1669 if(plugin->config.vertical_only)
1674 int search_x = pkg->scan_x1 + sub_x / OVERSAMPLE;
1675 int search_y = pkg->scan_y1 + sub_y / OVERSAMPLE;
1676 sub_x %= OVERSAMPLE;
1677 sub_y %= OVERSAMPLE;
1680 unsigned char *prev_ptr = server->previous_frame->get_rows()[
1682 search_x * pixel_size;
1683 unsigned char *current_ptr = server->current_frame->get_rows()[
1685 pkg->block_x1 * pixel_size;
1687 // With subpixel, there are two ways to compare each position, one by shifting
1688 // the previous frame and two by shifting the current frame.
1689 pkg->difference1 = plugin->abs_diff_sub(prev_ptr,
1692 pkg->block_x2 - pkg->block_x1,
1693 pkg->block_y2 - pkg->block_y1,
1697 pkg->difference2 = plugin->abs_diff_sub(current_ptr,
1700 pkg->block_x2 - pkg->block_x1,
1701 pkg->block_y2 - pkg->block_y1,
1705 // printf("MotionScanUnit::process_package sub_x=%d sub_y=%d search_x=%d search_y=%d diff1=%lld diff2=%lld\n",
1710 // pkg->difference1,
1711 // pkg->difference2);
1729 int64_t MotionScanUnit::get_cache(int x, int y)
1731 int64_t result = -1;
1732 cache_lock->lock("MotionScanUnit::get_cache");
1733 for(int i = 0; i < cache.total; i++)
1735 MotionScanCache *ptr = cache.values[i];
1736 if(ptr->x == x && ptr->y == y)
1738 result = ptr->difference;
1742 cache_lock->unlock();
1746 void MotionScanUnit::put_cache(int x, int y, int64_t difference)
1748 MotionScanCache *ptr = new MotionScanCache(x, y, difference);
1749 cache_lock->lock("MotionScanUnit::put_cache");
1751 cache_lock->unlock();
1764 MotionScan::MotionScan(MotionMain2 *plugin,
1769 total_clients, total_packages
1772 this->plugin = plugin;
1773 cache_lock = new Mutex("MotionScan::cache_lock");
1776 MotionScan::~MotionScan()
1782 void MotionScan::init_packages()
1784 // Set package coords
1785 for(int i = 0; i < get_total_packages(); i++)
1787 MotionScanPackage *pkg = (MotionScanPackage*)get_package(i);
1789 pkg->block_x1 = block_x1;
1790 pkg->block_x2 = block_x2;
1791 pkg->block_y1 = block_y1;
1792 pkg->block_y2 = block_y2;
1793 pkg->scan_x1 = scan_x1;
1794 pkg->scan_x2 = scan_x2;
1795 pkg->scan_y1 = scan_y1;
1796 pkg->scan_y2 = scan_y2;
1797 pkg->pixel = (int64_t)i * (int64_t)total_pixels / (int64_t)total_steps;
1798 pkg->difference1 = 0;
1799 pkg->difference2 = 0;
1806 LoadClient* MotionScan::new_client()
1808 return new MotionScanUnit(this, plugin);
1811 LoadPackage* MotionScan::new_package()
1813 return new MotionScanPackage;
1817 void MotionScan::scan_frame(VFrame *previous_frame,
1818 VFrame *current_frame,
1821 this->previous_frame = previous_frame;
1822 this->current_frame = current_frame;
1823 this->point = point;
1826 cache.remove_all_objects();
1829 // Single macroblock
1830 int w = current_frame->get_w();
1831 int h = current_frame->get_h();
1833 // Initial search parameters
1834 int scan_w = w * plugin->config.global_range_w[point] / 100;
1835 int scan_h = h * plugin->config.global_range_h[point] / 100;
1836 int block_w = w * plugin->config.global_block_w[point] / 100;
1837 int block_h = h * plugin->config.global_block_h[point] / 100;
1839 // Location of block in previous frame
1840 block_x1 = (int)(w * plugin->config.block_x[point] / 100 - block_w / 2);
1841 block_y1 = (int)(h * plugin->config.block_y[point] / 100 - block_h / 2);
1842 block_x2 = (int)(w * plugin->config.block_x[point] / 100 + block_w / 2);
1843 block_y2 = (int)(h * plugin->config.block_y[point] / 100 + block_h / 2);
1845 // Offset to location of previous block. This offset needn't be very accurate
1846 // since it's the offset of the previous image and current image we want.
1847 if(plugin->config.tracking_object == MotionScan::TRACK_PREVIOUS)
1849 block_x1 += plugin->total_dx[point] / OVERSAMPLE;
1850 block_y1 += plugin->total_dy[point] / OVERSAMPLE;
1851 block_x2 += plugin->total_dx[point] / OVERSAMPLE;
1852 block_y2 += plugin->total_dy[point] / OVERSAMPLE;
1857 switch(plugin->config.calculation)
1860 case MotionScan::NO_CALCULATE:
1866 case MotionScan::LOAD:
1868 printf("MotionScan::scan_frame %d\n", __LINE__);
1869 // Load result from disk
1870 char string[BCTEXTLEN];
1871 sprintf(string, "%s%06d",
1873 plugin->get_source_position());
1875 input = fopen(string, "r");
1878 for(int i = 0; i <= point; i++)
1891 // Scan from scratch
1900 printf("MotionScan::scan_frame %d\n", __LINE__);
1901 // Calculate center of search area in current frame
1902 int origin_offset_x = plugin->config.global_origin_x[point] * w / 100;
1903 int origin_offset_y = plugin->config.global_origin_y[point] * h / 100;
1904 int x_result = block_x1 + origin_offset_x;
1905 int y_result = block_y1 + origin_offset_y;
1907 // printf("MotionScan::scan_frame 1 %d %d %d %d %d %d %d %d\n",
1908 // block_x1 + block_w / 2,
1909 // block_y1 + block_h / 2,
1919 scan_x1 = x_result - scan_w / 2;
1920 scan_y1 = y_result - scan_h / 2;
1921 scan_x2 = x_result + scan_w / 2;
1922 scan_y2 = y_result + scan_h / 2;
1926 // Zero out requested values
1927 if(plugin->config.horizontal_only)
1930 scan_y2 = block_y1 + 1;
1932 if(plugin->config.vertical_only)
1935 scan_x2 = block_x1 + 1;
1938 // printf("MotionScan::scan_frame 1 %d %d %d %d %d %d %d %d\n",
1947 // Clamp the block coords before the scan so we get useful scan coords.
1948 MotionScan::clamp_scan(w,
1959 // 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",
1972 // Give up if invalid coords.
1973 if(scan_y2 <= scan_y1 ||
1974 scan_x2 <= scan_x1 ||
1975 block_x2 <= block_x1 ||
1976 block_y2 <= block_y1)
1980 // For subpixel, the top row and left column are skipped
1984 if(plugin->config.horizontal_only ||
1985 plugin->config.vertical_only)
1987 total_pixels = 4 * OVERSAMPLE * OVERSAMPLE - 4 * OVERSAMPLE;
1991 total_pixels = 4 * OVERSAMPLE;
1994 total_steps = total_pixels;
1996 set_package_count(total_steps);
1999 // Get least difference
2000 int64_t min_difference = -1;
2001 for(int i = 0; i < get_total_packages(); i++)
2003 MotionScanPackage *pkg = (MotionScanPackage*)get_package(i);
2004 if(pkg->difference1 < min_difference || min_difference == -1)
2006 min_difference = pkg->difference1;
2008 if(plugin->config.vertical_only)
2009 x_result = scan_x1 * OVERSAMPLE;
2011 x_result = scan_x1 * OVERSAMPLE +
2012 (pkg->pixel % (OVERSAMPLE * 2 - 1)) + 1;
2014 if(plugin->config.horizontal_only)
2015 y_result = scan_y1 * OVERSAMPLE;
2017 y_result = scan_y1 * OVERSAMPLE +
2018 (pkg->pixel / (OVERSAMPLE * 2 - 1)) + 1;
2022 dx_result = block_x1 * OVERSAMPLE - x_result;
2023 dy_result = block_y1 * OVERSAMPLE - y_result;
2026 if(pkg->difference2 < min_difference)
2028 min_difference = pkg->difference2;
2030 if(plugin->config.vertical_only)
2031 x_result = scan_x1 * OVERSAMPLE;
2033 x_result = scan_x2 * OVERSAMPLE -
2034 ((pkg->pixel % (OVERSAMPLE * 2 - 1)) + 1);
2036 if(plugin->config.horizontal_only)
2037 y_result = scan_y1 * OVERSAMPLE;
2039 y_result = scan_y2 * OVERSAMPLE -
2040 ((pkg->pixel / (OVERSAMPLE * 2 - 1)) + 1);
2042 dx_result = block_x1 * OVERSAMPLE - x_result;
2043 dy_result = block_y1 * OVERSAMPLE - y_result;
2048 //printf("MotionScan::scan_frame 1 %d %d %d %d\n", block_x1, block_y1, x_result, y_result);
2054 total_pixels = (scan_x2 - scan_x1) * (scan_y2 - scan_y1);
2055 total_steps = MIN(plugin->config.global_positions, total_pixels);
2057 set_package_count(total_steps);
2060 // Get least difference
2061 int64_t min_difference = -1;
2062 for(int i = 0; i < get_total_packages(); i++)
2064 MotionScanPackage *pkg = (MotionScanPackage*)get_package(i);
2065 if(pkg->difference1 < min_difference || min_difference == -1)
2067 min_difference = pkg->difference1;
2068 x_result = scan_x1 + (pkg->pixel % (scan_x2 - scan_x1));
2069 y_result = scan_y1 + (pkg->pixel / (scan_x2 - scan_x1));
2070 x_result *= OVERSAMPLE;
2071 y_result *= OVERSAMPLE;
2077 // printf("MotionScan::scan_frame 10 total_steps=%d total_pixels=%d subpixel=%d\n",
2082 // printf(" scan w=%d h=%d scan x1=%d y1=%d x2=%d y2=%d\n",
2090 // printf("MotionScan::scan_frame 2 block x1=%d y1=%d x2=%d y2=%d result x=%.2f y=%.2f\n",
2095 // (float)x_result / 4,
2096 // (float)y_result / 4);
2099 // If a new search is required, rescale results back to pixels.
2100 if(total_steps >= total_pixels)
2102 // Single pixel accuracy reached. Now do exhaustive subpixel search.
2103 if(plugin->config.action == MotionScan::STABILIZE ||
2104 plugin->config.action == MotionScan::TRACK ||
2105 plugin->config.action == MotionScan::NOTHING)
2107 x_result /= OVERSAMPLE;
2108 y_result /= OVERSAMPLE;
2115 // Fill in results and quit
2116 dx_result = block_x1 * OVERSAMPLE - x_result;
2117 dy_result = block_y1 * OVERSAMPLE - y_result;
2122 // Reduce scan area and try again
2124 scan_w = (scan_x2 - scan_x1) / 2;
2125 scan_h = (scan_y2 - scan_y1) / 2;
2126 x_result /= OVERSAMPLE;
2127 y_result /= OVERSAMPLE;
2142 if(plugin->config.calculation == MotionScan::SAVE)
2144 char string[BCTEXTLEN];
2148 plugin->get_source_position());
2151 output = fopen(string, "w");
2153 output = fopen(string, "a");
2164 perror("MotionScan::scan_frame SAVE 1");
2169 printf("MotionScan::scan_frame 10 point=%d dx=%.2f dy=%.2f\n",
2171 (float)this->dx_result / OVERSAMPLE,
2172 (float)this->dy_result / OVERSAMPLE);
2192 int64_t MotionScan::get_cache(int x, int y)
2194 int64_t result = -1;
2195 cache_lock->lock("MotionScan::get_cache");
2196 for(int i = 0; i < cache.total; i++)
2198 MotionScanCache *ptr = cache.values[i];
2199 if(ptr->x == x && ptr->y == y)
2201 result = ptr->difference;
2205 cache_lock->unlock();
2209 void MotionScan::put_cache(int x, int y, int64_t difference)
2211 MotionScanCache *ptr = new MotionScanCache(x, y, difference);
2212 cache_lock->lock("MotionScan::put_cache");
2214 cache_lock->unlock();
2221 MotionScanCache::MotionScanCache(int x, int y, int64_t difference)
2225 this->difference = difference;