plugin fixes and upgrades
[goodguy/history.git] / cinelerra-5.0 / plugins / motion2point / motion.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  * 
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.
10  * 
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.
15  * 
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
19  * 
20  */
21
22 #include "affine.h"
23 #include "../motion/motionscan.h"
24 #include "bcdisplayinfo.h"
25 #include "bcsignals.h"
26 #include "clip.h"
27 #include "bchash.h"
28 #include "filexml.h"
29 #include "format.inc"
30 #include "keyframe.h"
31 #include "language.h"
32 #include "motion.h"
33 #include "motionwindow.h"
34 #include "mutex.h"
35 #include "overlayframe.h"
36 #include "rotateframe.h"
37 #include "transportque.h"
38
39
40 #include <errno.h>
41 #include <unistd.h>
42
43 REGISTER_PLUGIN(MotionMain2)
44
45 //#undef DEBUG
46
47 #ifndef DEBUG
48 #define DEBUG
49 #endif
50
51
52 MotionConfig::MotionConfig()
53 {
54         for(int i = 0; i < TOTAL_POINTS; i++)
55         {
56                 global[i] = 1;
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;
63                 block_x[i] = 50;
64                 block_y[i] = 50;
65                 draw_vectors[i] = 1;
66         }
67
68         global_positions = 256;
69         magnitude = 100;
70         return_speed = 0;
71         action = MotionScan::STABILIZE;
72         calculation = MotionScan::NO_CALCULATE;
73         tracking_object = MotionScan::TRACK_SINGLE;
74         track_frame = 0;
75         bottom_is_master = 1;
76         horizontal_only = 0;
77         vertical_only = 0;
78 }
79
80 void MotionConfig::boundaries()
81 {
82         for(int i = 0; i < TOTAL_POINTS; i++)
83         {
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);
90         }
91
92 }
93
94 int MotionConfig::equivalent(MotionConfig &that)
95 {
96         int result = 1;
97         for(int i = 0; i < TOTAL_POINTS; i++)
98         {
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]))
109                    result = 0;
110         }
111
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;
122
123         return result;  
124 }
125
126 void MotionConfig::copy_from(MotionConfig &that)
127 {
128         for(int i = 0; i < TOTAL_POINTS; i++)
129         {
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];
140         }
141
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;
152 }
153
154 void MotionConfig::interpolate(MotionConfig &prev, 
155         MotionConfig &next, 
156         int64_t prev_frame, 
157         int64_t next_frame, 
158         int64_t current_frame)
159 {
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);
162
163         for(int i = 0; i < TOTAL_POINTS; i++)
164         {
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];
175         }
176
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;
187 }
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207 MotionMain2::MotionMain2(PluginServer *server)
208  : PluginVClient(server)
209 {
210         
211         engine = 0;
212         affine = 0;
213         for(int i = 0; i < TOTAL_POINTS; i++)
214         {
215                 total_dx[i] = 0;
216                 total_dy[i] = 0;
217         }
218         overlayer = 0;
219         search_area = 0;
220         search_size = 0;
221         temp_frame = 0;
222         previous_frame_number = -1;
223
224         prev_global_ref = 0;
225         current_global_ref = 0;
226         global_target_src = 0;
227         global_target_dst = 0;
228 }
229
230 MotionMain2::~MotionMain2()
231 {
232         
233         delete engine;
234         delete affine;
235         delete overlayer;
236         delete [] search_area;
237         delete temp_frame;
238
239
240         delete prev_global_ref;
241         delete current_global_ref;
242         delete global_target_src;
243         delete global_target_dst;
244 }
245
246 const char* MotionMain2::plugin_title() { return _("Motion 2 Point"); }
247 int MotionMain2::is_realtime() { return 1; }
248 int MotionMain2::is_multichannel() { return 1; }
249
250
251 NEW_WINDOW_MACRO(MotionMain2, MotionWindow)
252
253 LOAD_CONFIGURATION_MACRO(MotionMain2, MotionConfig)
254
255
256
257 void MotionMain2::update_gui()
258 {
259         if(thread)
260         {
261                 if(load_configuration())
262                 {
263                         thread->window->lock_window("MotionMain2::update_gui");
264                         
265                         char string[BCTEXTLEN];
266                         
267                         for(int i = 0; i < TOTAL_POINTS; i++)
268                         {
269                                 ((MotionWindow*)thread->window)->global[i]->update(config.global[i]);
270
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]);
280                         }
281
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);
286
287
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();
294                         else
295                                 ((MotionWindow*)thread->window)->track_frame_number->enable();
296
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));
305
306
307                         ((MotionWindow*)thread->window)->update_mode();
308                         thread->window->unlock_window();
309                 }
310         }
311 }
312
313
314
315
316
317 void MotionMain2::save_data(KeyFrame *keyframe)
318 {
319         FileXML output;
320
321 // cause data to be stored directly in text
322         output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
323         output.tag.set_title("MOTION2");
324
325         char string[BCTEXTLEN];
326         for(int i = 0; i < TOTAL_POINTS; i++)
327         {
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]);
348         }
349
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);
360         output.append_tag();
361         output.tag.set_title("/MOTION2");
362         output.append_tag();
363         output.append_newline();
364         output.terminate_string();
365 }
366
367 void MotionMain2::read_data(KeyFrame *keyframe)
368 {
369         FileXML input;
370
371         input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
372
373         int result = 0;
374
375         while(!result)
376         {
377                 result = input.read_tag();
378
379                 if(!result)
380                 {
381                         if(input.tag.title_is("MOTION2"))
382                         {
383                                 char string[BCTEXTLEN];
384                                 for(int i = 0; i < TOTAL_POINTS; i++)
385                                 {
386                                         sprintf(string, "GLOBAL%d", i);
387                                         config.global[i] = input.tag.get_property(string, config.global[i]);
388                                         sprintf(string, "GLOBAL_BLOCK_W%d", i);
389                                         config.global_block_w[i] = input.tag.get_property(string, config.global_block_w[i]);
390                                         sprintf(string, "GLOBAL_BLOCK_H%d", i);
391                                         config.global_block_h[i] = input.tag.get_property(string, config.global_block_h[i]);
392                                         sprintf(string, "BLOCK_X%d", i);
393                                         config.block_x[i] = input.tag.get_property(string, config.block_x[i]);
394                                         sprintf(string, "BLOCK_Y%d", i);
395                                         config.block_y[i] = input.tag.get_property(string, config.block_y[i]);
396                                         sprintf(string, "GLOBAL_RANGE_W%d", i);
397                                         config.global_range_w[i] = input.tag.get_property(string, config.global_range_w[i]);
398                                         sprintf(string, "GLOBAL_RANGE_H%d", i);
399                                         config.global_range_h[i] = input.tag.get_property(string, config.global_range_h[i]);
400                                         sprintf(string, "GLOBAL_ORIGIN_X%d", i);
401                                         config.global_origin_x[i] = input.tag.get_property(string, config.global_origin_x[i]);
402                                         sprintf(string, "GLOBAL_ORIGIN_Y%d", i);
403                                         config.global_origin_y[i] = input.tag.get_property(string, config.global_origin_y[i]);
404                                         sprintf(string, "DRAW_VECTORS%d", i);
405                                         config.draw_vectors[i] = input.tag.get_property(string, config.draw_vectors[i]);
406                                 }
407
408                                 config.global_positions = input.tag.get_property("GLOBAL_POSITIONS", config.global_positions);
409                                 config.magnitude = input.tag.get_property("MAGNITUDE", config.magnitude);
410                                 config.return_speed = input.tag.get_property("RETURN_SPEED", config.return_speed);
411                                 config.action = input.tag.get_property("ACTION_TYPE", config.action);
412                                 config.calculation = input.tag.get_property("TRACKING_TYPE", config.calculation);
413                                 config.tracking_object = input.tag.get_property("TRACKING_OBJECT", config.tracking_object);
414                                 config.track_frame = input.tag.get_property("TRACK_FRAME", config.track_frame);
415                                 config.bottom_is_master = input.tag.get_property("BOTTOM_IS_MASTER", config.bottom_is_master);
416                                 config.horizontal_only = input.tag.get_property("HORIZONTAL_ONLY", config.horizontal_only);
417                                 config.vertical_only = input.tag.get_property("VERTICAL_ONLY", config.vertical_only);
418                         }
419                 }
420         }
421         config.boundaries();
422 }
423
424
425
426
427
428
429
430
431
432 void MotionMain2::allocate_temp(int w, int h, int color_model)
433 {
434         if(temp_frame && 
435                 (temp_frame->get_w() != w ||
436                 temp_frame->get_h() != h))
437         {
438                 delete temp_frame;
439                 temp_frame = 0;
440         }
441         if(!temp_frame)
442                 temp_frame = new VFrame(w, h, color_model);
443 }
444
445
446
447 void MotionMain2::scan_motion(int point)
448 {
449         if(!engine) engine = new MotionScan(PluginClient::get_project_smp() + 1,
450                 PluginClient::get_project_smp() + 1);
451
452 // Get the current motion vector between the previous and current frame
453         engine->scan_frame(current_global_ref, 
454                 prev_global_ref, 
455                 config.global_range_w[point],
456                 config.global_range_h[point],
457                 config.global_block_w[point],
458                 config.global_block_h[point],
459                 config.block_x[point],
460                 config.block_y[point],
461                 config.tracking_object,
462                 config.calculation,
463                 config.action,
464                 config.horizontal_only,
465                 config.vertical_only,
466                 get_source_position(),
467                 config.global_positions,
468                 total_dx[point],
469                 total_dy[point],
470                 config.global_origin_x[point],
471                 config.global_origin_y[point]);
472
473 //              0,
474 //              0,
475 //              0,
476 //              0);
477         current_dx[point] = engine->dx_result;
478         current_dy[point] = engine->dy_result;
479
480 // Add current motion vector to accumulation vector.
481         if(config.tracking_object != MotionScan::TRACK_SINGLE)
482         {
483 // Retract over time
484                 total_dx[point] = (int64_t)total_dx[point] * (100 - config.return_speed) / 100;
485                 total_dy[point] = (int64_t)total_dy[point] * (100 - config.return_speed) / 100;
486                 total_dx[point] += engine->dx_result;
487                 total_dy[point] += engine->dy_result;
488         }
489         else
490 // Make accumulation vector current
491         {
492                 total_dx[point] = engine->dx_result;
493                 total_dy[point] = engine->dy_result;
494 // printf("MotionMain2::scan_motion %d %d %d %d\n", 
495 // __LINE__, 
496 // point,
497 // total_dx[point], 
498 // total_dy[point]);
499         }
500
501 // Clamp accumulation vector
502         if(config.magnitude < 100)
503         {
504                 //int block_w = (int64_t)config.global_block_w[point] * 
505                 //              current_global_ref->get_w() / 100;
506                 //int block_h = (int64_t)config.global_block_h[point] * 
507                 //              current_global_ref->get_h() / 100;
508                 int block_x_orig = (int64_t)(config.block_x[point] * 
509                         current_global_ref->get_w() / 
510                         100);
511                 int block_y_orig = (int64_t)(config.block_y[point] *
512                         current_global_ref->get_h() / 
513                         100);
514
515                 int max_block_x = (int64_t)(current_global_ref->get_w() - block_x_orig) *
516                         OVERSAMPLE * 
517                         config.magnitude / 
518                         100;
519                 int max_block_y = (int64_t)(current_global_ref->get_h() - block_y_orig) *
520                         OVERSAMPLE *
521                         config.magnitude / 
522                         100;
523                 int min_block_x = (int64_t)-block_x_orig * 
524                         OVERSAMPLE * 
525                         config.magnitude / 
526                         100;
527                 int min_block_y = (int64_t)-block_y_orig * 
528                         OVERSAMPLE * 
529                         config.magnitude / 
530                         100;
531
532                 CLAMP(total_dx[point], min_block_x, max_block_x);
533                 CLAMP(total_dy[point], min_block_y, max_block_y);
534         }
535
536 // printf("MotionMain2::scan_motion %d %d %d %d\n", 
537 // __LINE__, 
538 // point,
539 // total_dx[point], 
540 // total_dy[point]);
541
542
543 }
544
545
546
547
548 void MotionMain2::apply_motion()
549 {
550         if(config.tracking_object != MotionScan::TRACK_SINGLE)
551         {
552 // Transfer current reference frame to previous reference frame and update
553 // counter.
554                 prev_global_ref->copy_from(current_global_ref);
555                 previous_frame_number = get_source_position();
556         }
557
558 // Decide what to do with target based on requested operation
559         //int interpolation; //   variable set but not used
560         float origin_x[TOTAL_POINTS];
561         float origin_y[TOTAL_POINTS];
562         float end_x[TOTAL_POINTS];
563         float end_y[TOTAL_POINTS];
564         for(int i = 0; i < TOTAL_POINTS; i++)
565         {
566                 get_current_vector(&origin_x[i], 
567                         &origin_y[i], 
568                         0,
569                         0,
570                         &end_x[i], 
571                         &end_y[i], 
572                         i);
573         }
574
575 // Calculate rotation if 2 points
576         double angle = 0.0;
577         double angle0 = 0.0;
578         double zoom = 1.0;
579         if(config.global[ROTATION_POINT])
580         {
581                         if(origin_x[1] - origin_x[0])
582                                 angle0 = atan((double)(origin_y[0] - origin_y[1]) /
583                                         (double)(origin_x[0] - origin_x[1]));
584                         if(end_x[1] - end_x[0])
585                                 angle = atan((double)(end_y[0] - end_y[1]) /
586                                         (double)(end_x[0] - end_x[1]));
587 // printf("MotionMain2::apply_motion %d angle0=%f angle=%f\n", 
588 // __LINE__,
589 // angle0 * 360 / 2 / M_PI, 
590 // angle * 360 / 2 / M_PI);
591
592                         angle -= angle0;
593
594 // Calculate zoom
595 //                      zoom = DISTANCE(origin_x[1], origin_y[1], origin_x[0], origin_y[0]) /
596 //                              DISTANCE(end_x[1], end_y[1], end_x[0], end_y[0]);
597
598         }
599
600 printf("MotionMain2::apply_motion %d total_dx=%.02f total_dy=%.02f angle=%f zoom=%f\n", 
601 __LINE__,
602 (float)total_dx[TRANSLATION_POINT] / OVERSAMPLE,
603 (float)total_dy[TRANSLATION_POINT] / OVERSAMPLE, 
604 angle * 360 / 2 / M_PI,
605 zoom);
606
607 // Calculate translation
608         float dx = 0.0;
609         float dy = 0.0;
610         switch(config.action)
611         {
612                 case MotionScan::NOTHING:
613                         global_target_dst->copy_from(global_target_src);
614                         break;
615                 case MotionScan::TRACK:
616                         //interpolation = CUBIC_LINEAR;
617                         dx = (float)total_dx[0] / OVERSAMPLE;
618                         dy = (float)total_dy[0] / OVERSAMPLE;
619                         break;
620                 case MotionScan::TRACK_PIXEL:
621                         //interpolation = NEAREST_NEIGHBOR;
622                         dx = (int)(total_dx[0] / OVERSAMPLE);
623                         dy = (int)(total_dy[0] / OVERSAMPLE);
624                         break;
625                         //interpolation = NEAREST_NEIGHBOR;
626                         dx = -(int)(total_dx[0] / OVERSAMPLE);
627                         dy = -(int)(total_dy[0] / OVERSAMPLE);
628                         angle *= -1;
629                         break;
630                 case MotionScan::STABILIZE:
631                         //interpolation = CUBIC_LINEAR;
632                         dx = -(float)total_dx[0] / OVERSAMPLE;
633                         dy = -(float)total_dy[0] / OVERSAMPLE;
634                         angle *= -1;
635                         break;
636         }
637
638
639
640
641
642         if(config.action != MotionScan::NOTHING)
643         {
644                 double w = get_output()->get_w();
645                 double h = get_output()->get_h();
646                 double pivot_x = end_x[0];
647                 double pivot_y = end_y[0];
648                 double angle1 = atan((double)pivot_y / (double)pivot_x) + angle;
649                 double angle2 = atan((double)(w - pivot_x) / (double)pivot_y) + angle;
650                 double angle3 = atan((double)(h - pivot_y) / (double)(w - pivot_x)) + angle;
651                 double angle4 = atan((double)pivot_x / (double)(h - pivot_y)) + angle;
652                 double radius1 = DISTANCE(0, 0, pivot_x, pivot_y) * zoom;
653                 double radius2 = DISTANCE(w, 0, pivot_x, pivot_y) * zoom;
654                 double radius3 = DISTANCE(w, h, pivot_x, pivot_y) * zoom;
655                 double radius4 = DISTANCE(0, h, pivot_x, pivot_y) * zoom;
656
657
658                 float x1 = (dx + pivot_x - cos(angle1) * radius1) * 100 / w;
659                 float y1 = (dy + pivot_y - sin(angle1) * radius1) * 100 / h;
660                 float x2 = (dx + pivot_x + sin(angle2) * radius2) * 100 / w;
661                 float y2 = (dy + pivot_y - cos(angle2) * radius2) * 100 / h;
662                 float x3 = (dx + pivot_x + cos(angle3) * radius3) * 100 / w;
663                 float y3 = (dy + pivot_y + sin(angle3) * radius3) * 100 / h;
664                 float x4 = (dx + pivot_x - sin(angle4) * radius4) * 100 / w;
665                 float y4 = (dy + pivot_y + cos(angle4) * radius4) * 100 / h;
666
667
668                 if(!affine)
669                         affine = new AffineEngine(PluginClient::get_project_smp() + 1,
670                                 PluginClient::get_project_smp() + 1);
671                 global_target_dst->clear_frame();
672
673
674 // printf("MotionMain2::apply_motion %d %.02f %.02f %.02f %.02f %.02f %.02f %.02f %.02f\n",
675 // __LINE__,
676 // x1, 
677 // y1, 
678 // x2, 
679 // y2, 
680 // x3, 
681 // y3, 
682 // x4, 
683 // y4);
684
685                 affine->process(global_target_dst,
686                         global_target_src, 
687                         0,
688                         AffineEngine::PERSPECTIVE,
689                         x1, 
690                         y1, 
691                         x2, 
692                         y2, 
693                         x3, 
694                         y3, 
695                         x4, 
696                         y4,
697                         1);
698         }
699 }
700
701
702
703
704
705
706 int MotionMain2::process_buffer(VFrame **frame,
707         int64_t start_position,
708         double frame_rate)
709 {
710         int need_reconfigure = load_configuration();
711         int color_model = frame[0]->get_color_model();
712         w = frame[0]->get_w();
713         h = frame[0]->get_h();
714         
715
716 #ifdef DEBUG
717 printf("MotionMain2::process_buffer 1 start_position=%jd\n", start_position);
718 #endif
719
720
721 // Calculate the source and destination pointers for each of the operations.
722 // Get the layer to track motion in.
723         reference_layer = config.bottom_is_master ?
724                 PluginClient::total_in_buffers - 1 :
725                 0;
726 // Get the layer to apply motion in.
727         target_layer = config.bottom_is_master ?
728                 0 :
729                 PluginClient::total_in_buffers - 1;
730
731
732         output_frame = frame[target_layer];
733
734
735 // Get the position of previous reference frame.
736         int64_t actual_previous_number;
737 // Skip if match frame not available
738         int skip_current = 0;
739
740
741         if(config.tracking_object == MotionScan::TRACK_SINGLE)
742         {
743                 actual_previous_number = config.track_frame;
744                 if(get_direction() == PLAY_REVERSE)
745                         actual_previous_number++;
746                 if(actual_previous_number == start_position)
747                         skip_current = 1;
748         }
749         else
750         {
751                 actual_previous_number = start_position;
752                 if(get_direction() == PLAY_FORWARD)
753                 {
754                         actual_previous_number--;
755                         if(actual_previous_number < get_source_start())
756                                 skip_current = 1;
757                         else
758                         {
759                                 KeyFrame *keyframe = get_prev_keyframe(start_position, 1);
760                                 if(keyframe->position > 0 &&
761                                         actual_previous_number < keyframe->position)
762                                         skip_current = 1;
763                         }
764                 }
765                 else
766                 {
767                         actual_previous_number++;
768                         if(actual_previous_number >= get_source_start() + get_total_len())
769                                 skip_current = 1;
770                         else
771                         {
772                                 KeyFrame *keyframe = get_next_keyframe(start_position, 1);
773                                 if(keyframe->position > 0 &&
774                                         actual_previous_number >= keyframe->position)
775                                         skip_current = 1;
776                         }
777                 }
778
779 // Only count motion since last keyframe
780                 
781
782         }
783
784
785 // Point 0 must be tracked for any other points to be tracked
786 // Action and Calculation must be something
787         if( !config.global[0] || ( config.action == MotionScan::NOTHING &&
788                         config.calculation == MotionScan::NO_CALCULATE) )
789                 skip_current = 1;
790
791
792 // printf("process_buffer %d %lld %lld %d\n", 
793 // skip_current, 
794 // previous_frame_number, 
795 // actual_previous_number,
796 // need_reconfigure);
797 // Load match frame and reset vectors
798         int need_reload = !skip_current && 
799                 (previous_frame_number != actual_previous_number ||
800                 need_reconfigure);
801         if(need_reload)
802         {
803                 for(int i = 0; i < TOTAL_POINTS; i++)
804                 {
805                         total_dx[i] = 0;
806                         total_dy[i] = 0;
807                 }
808                 previous_frame_number = actual_previous_number;
809         }
810
811
812         if(skip_current)
813         {
814                 for(int i = 0; i < TOTAL_POINTS; i++)
815                 {
816                         total_dx[i] = 0;
817                         total_dy[i] = 0;
818                         current_dx[i] = 0;
819                         current_dy[i] = 0;
820                 }
821         }
822
823
824
825
826 // Get the global pointers.  Here we walk through the sequence of events.
827         if(config.global[0])
828         {
829 // Global reads previous frame and compares
830 // with current frame to get the current translation.
831 // The center of the search area is fixed in compensate mode or
832 // the user value + the accumulation vector in track mode.
833                 if(!prev_global_ref)
834                         prev_global_ref = new VFrame(w, h, color_model);
835                 if(!current_global_ref)
836                         current_global_ref = new VFrame(w, h, color_model);
837
838 // Global loads the current target frame into the src and 
839 // writes it to the dst frame with desired translation.
840                 if(!global_target_src)
841                         global_target_src = new VFrame(w, h, color_model);
842                 if(!global_target_dst)
843                         global_target_dst = new VFrame(w, h, color_model);
844
845
846 // Load the global frames
847                 if(need_reload)
848                 {
849                         read_frame(prev_global_ref, 
850                                 reference_layer, 
851                                 previous_frame_number, 
852                                 frame_rate,
853                                 0);
854                 }
855
856                 read_frame(current_global_ref, 
857                         reference_layer, 
858                         start_position, 
859                         frame_rate,
860                         0);
861                 read_frame(global_target_src,
862                         target_layer,
863                         start_position,
864                         frame_rate,
865                         0);
866         }
867
868
869
870
871
872
873
874
875         if(!skip_current)
876         {
877 // Get position change from previous frame to current frame
878                 if(config.global[0]) 
879                 {
880                         for(int i = 0; i < TOTAL_POINTS; i++)
881                                 if(config.global[i]) scan_motion(i);
882                 }
883                 
884                 apply_motion();
885         }
886
887
888
889
890 //printf("MotionMain2::process_buffer 90 %d\n", skip_current);
891
892 // Transfer the relevant target frame to the output
893         if(!skip_current)
894         {
895                 frame[target_layer]->copy_from(global_target_dst);
896         }
897         else
898 // Read the target destination directly
899         {
900                 read_frame(frame[target_layer],
901                         target_layer,
902                         start_position,
903                         frame_rate,
904                         0);
905         }
906
907         if(config.draw_vectors)
908         {
909                 for(int i = 0; i < TOTAL_POINTS; i++)
910                         draw_vectors(frame[target_layer], i);
911         }
912
913 #ifdef DEBUG
914 printf("MotionMain2::process_buffer 100 skip_current=%d\n", skip_current);
915 #endif
916         return 0;
917 }
918
919
920
921 #if 0
922
923 void MotionMain2::clamp_scan(int w, 
924         int h, 
925         int *block_x1,
926         int *block_y1,
927         int *block_x2,
928         int *block_y2,
929         int *scan_x1,
930         int *scan_y1,
931         int *scan_x2,
932         int *scan_y2,
933         int use_absolute)
934 {
935 // printf("MotionMain2::clamp_scan 1 w=%d h=%d block=%d %d %d %d scan=%d %d %d %d absolute=%d\n",
936 // w,
937 // h,
938 // *block_x1,
939 // *block_y1,
940 // *block_x2,
941 // *block_y2,
942 // *scan_x1,
943 // *scan_y1,
944 // *scan_x2,
945 // *scan_y2,
946 // use_absolute);
947
948         if(use_absolute)
949         {
950 // scan is always out of range before block.
951                 if(*scan_x1 < 0)
952                 {
953                         int difference = -*scan_x1;
954                         *block_x1 += difference;
955                         *scan_x1 = 0;
956                 }
957
958                 if(*scan_y1 < 0)
959                 {
960                         int difference = -*scan_y1;
961                         *block_y1 += difference;
962                         *scan_y1 = 0;
963                 }
964
965                 if(*scan_x2 > w)
966                 {
967                         int difference = *scan_x2 - w;
968                         *block_x2 -= difference;
969                         *scan_x2 -= difference;
970                 }
971
972                 if(*scan_y2 > h)
973                 {
974                         int difference = *scan_y2 - h;
975                         *block_y2 -= difference;
976                         *scan_y2 -= difference;
977                 }
978
979                 CLAMP(*scan_x1, 0, w);
980                 CLAMP(*scan_y1, 0, h);
981                 CLAMP(*scan_x2, 0, w);
982                 CLAMP(*scan_y2, 0, h);
983         }
984         else
985         {
986                 if(*scan_x1 < 0)
987                 {
988                         int difference = -*scan_x1;
989                         *block_x1 += difference;
990                         *scan_x2 += difference;
991                         *scan_x1 = 0;
992                 }
993
994                 if(*scan_y1 < 0)
995                 {
996                         int difference = -*scan_y1;
997                         *block_y1 += difference;
998                         *scan_y2 += difference;
999                         *scan_y1 = 0;
1000                 }
1001
1002                 if(*scan_x2 - *block_x1 + *block_x2 > w)
1003                 {
1004                         int difference = *scan_x2 - *block_x1 + *block_x2 - w;
1005                         *block_x2 -= difference;
1006                 }
1007
1008                 if(*scan_y2 - *block_y1 + *block_y2 > h)
1009                 {
1010                         int difference = *scan_y2 - *block_y1 + *block_y2 - h;
1011                         *block_y2 -= difference;
1012                 }
1013
1014 //              CLAMP(*scan_x1, 0, w - (*block_x2 - *block_x1));
1015 //              CLAMP(*scan_y1, 0, h - (*block_y2 - *block_y1));
1016 //              CLAMP(*scan_x2, 0, w - (*block_x2 - *block_x1));
1017 //              CLAMP(*scan_y2, 0, h - (*block_y2 - *block_y1));
1018         }
1019
1020 // Sanity checks which break the calculation but should never happen if the
1021 // center of the block is inside the frame.
1022         CLAMP(*block_x1, 0, w);
1023         CLAMP(*block_x2, 0, w);
1024         CLAMP(*block_y1, 0, h);
1025         CLAMP(*block_y2, 0, h);
1026
1027 // printf("MotionMain2::clamp_scan 2 w=%d h=%d block=%d %d %d %d scan=%d %d %d %d absolute=%d\n",
1028 // w,
1029 // h,
1030 // *block_x1,
1031 // *block_y1,
1032 // *block_x2,
1033 // *block_y2,
1034 // *scan_x1,
1035 // *scan_y1,
1036 // *scan_x2,
1037 // *scan_y2,
1038 // use_absolute);
1039 }
1040
1041
1042 #endif // 0
1043
1044 void MotionMain2::get_current_vector(float *origin_x,
1045         float *origin_y,
1046         float *current_x1, 
1047         float *current_y1, 
1048         float *current_x2, 
1049         float *current_y2, 
1050         int point)
1051 {
1052         int w = get_output()->get_w();
1053         int h = get_output()->get_h();
1054         float temp1;
1055         float temp2;
1056         float temp3;
1057         float temp4;
1058         float temp5;
1059         float temp6;
1060         if(!origin_x) origin_x = &temp1;
1061         if(!origin_y) origin_y = &temp2;
1062         if(!current_x1) current_x1 = &temp3;
1063         if(!current_y1) current_y1 = &temp4;
1064         if(!current_x2) current_x2 = &temp5;
1065         if(!current_y2) current_y2 = &temp6;
1066
1067         *origin_x = 0;
1068         *origin_y = 0;
1069         *current_x1 = 0.0;
1070         *current_y1 = 0.0;
1071         *current_x2 = 0.0;
1072         *current_y2 = 0.0;
1073         
1074         
1075         if(config.global[point])
1076         {
1077 // Get vector
1078 // Start of vector is center of previous block.
1079 // End of vector is total accumulation.
1080                 if(config.tracking_object == MotionScan::TRACK_SINGLE)
1081                 {
1082                         (*origin_x) = (*current_x1) = ((float)config.block_x[point] * 
1083                                 w / 
1084                                 100);
1085                         (*origin_y) = (*current_y1) = ((float)config.block_y[point] *
1086                                 h / 
1087                                 100);
1088                         (*current_x2) = (*current_x1) + (float)total_dx[point] / OVERSAMPLE;
1089                         (*current_y2) = (*current_y1) + (float)total_dy[point] / OVERSAMPLE;
1090                 }
1091                 else
1092 // Start of vector is center of previous block.
1093 // End of vector is current change.
1094                 if(config.tracking_object == MotionScan::PREVIOUS_SAME_BLOCK)
1095                 {
1096                         (*origin_x) = (*current_x1) = ((float)config.block_x[point] * 
1097                                 w / 
1098                                 100);
1099                         (*origin_y) = (*current_y1) = ((float)config.block_y[point] *
1100                                 h / 
1101                                 100);
1102                         (*current_x2) = (*origin_x) + (float)current_dx[point] / OVERSAMPLE;
1103                         (*current_y2) = (*origin_y) + (float)current_dy[point] / OVERSAMPLE;
1104                 }
1105                 else
1106                 {
1107                         (*origin_x) = (float)config.block_x[point] * 
1108                                 w / 
1109                                 100;
1110                         (*origin_y) = (float)config.block_y[point] *
1111                                 h / 
1112                                 100;
1113                         (*current_x1) = ((*origin_x) + 
1114                                 (float)(total_dx[point] - current_dx[point]) / 
1115                                 OVERSAMPLE);
1116                         (*current_y1) = ((*origin_y) +
1117                                 (float)(total_dy[point] - current_dy[point]) /
1118                                 OVERSAMPLE);
1119                         (*current_x2) = ((*origin_x) + 
1120                                 (float)total_dx[point] / 
1121                                 OVERSAMPLE);
1122                         (*current_y2) = ((*origin_y) +
1123                                 (float)total_dy[point] /
1124                                 OVERSAMPLE);
1125                 }
1126         }
1127 }
1128
1129
1130 void MotionMain2::draw_vectors(VFrame *frame, int point)
1131 {
1132         int w = frame->get_w();
1133         int h = frame->get_h();
1134         float global_x1, global_y1;
1135         float global_x2, global_y2;
1136         int block_x, block_y;
1137         int block_w, block_h;
1138         int block_x1, block_y1;
1139         int block_x2, block_y2;
1140         int search_w, search_h;
1141         int search_x1, search_y1;
1142         int search_x2, search_y2;
1143         int origin_offset_x;
1144         int origin_offset_y;
1145
1146         if(!config.draw_vectors[point]) return;
1147
1148
1149         if(config.global[point])
1150         {
1151 // Get vector
1152                 get_current_vector(0,
1153                         0,
1154                         &global_x1, 
1155                         &global_y1, 
1156                         &global_x2, 
1157                         &global_y2, 
1158                         point);
1159
1160
1161 // Draw destination rectangle
1162                 if(config.action == MotionScan::NOTHING || 
1163                         config.action == MotionScan::TRACK)
1164                 {
1165                         block_x = (int)global_x2;
1166                         block_y = (int)global_y2;
1167                 }
1168                 else
1169 // Draw source rectangle
1170                 {
1171                         block_x = (int)global_x1;
1172                         block_y = (int)global_y1;
1173                 }
1174                 block_w = config.global_block_w[point] * w / 100;
1175                 block_h = config.global_block_h[point] * h / 100;
1176                 origin_offset_x = config.global_origin_x[point] * w / 100;
1177                 origin_offset_y = config.global_origin_y[point] * h / 100;
1178                 block_x1 = block_x - block_w / 2;
1179                 block_y1 = block_y - block_h / 2;
1180                 block_x2 = block_x + block_w / 2;
1181                 block_y2 = block_y + block_h / 2;
1182                 search_w = config.global_range_w[point] * w / 100;
1183                 search_h = config.global_range_h[point] * h / 100;
1184                 search_x1 = block_x1 + origin_offset_x - search_w / 2;
1185                 search_y1 = block_y1 + origin_offset_y - search_h / 2;
1186                 search_x2 = block_x2 + origin_offset_x + search_w / 2;
1187                 search_y2 = block_y2 + origin_offset_y + search_h / 2;
1188
1189 // printf("MotionMain2::draw_vectors %d %d %d %d %d %d %d %d %d %d %d %d\n",
1190 // global_x1,
1191 // global_y1,
1192 // block_w,
1193 // block_h,
1194 // block_x1,
1195 // block_y1,
1196 // block_x2,
1197 // block_y2,
1198 // search_x1,
1199 // search_y1,
1200 // search_x2,
1201 // search_y2);
1202
1203                 MotionScan::clamp_scan(w, 
1204                         h, 
1205                         &block_x1,
1206                         &block_y1,
1207                         &block_x2,
1208                         &block_y2,
1209                         &search_x1,
1210                         &search_y1,
1211                         &search_x2,
1212                         &search_y2,
1213                         1);
1214
1215 // Vector
1216                 draw_arrow(frame, (int)global_x1, (int)global_y1, (int)global_x2, (int)global_y2);
1217
1218 // Macroblock
1219                 draw_line(frame, block_x1, block_y1, block_x2, block_y1);
1220                 draw_line(frame, block_x2, block_y1, block_x2, block_y2);
1221                 draw_line(frame, block_x2, block_y2, block_x1, block_y2);
1222                 draw_line(frame, block_x1, block_y2, block_x1, block_y1);
1223
1224
1225 // Search area
1226                 draw_line(frame, search_x1, search_y1, search_x2, search_y1);
1227                 draw_line(frame, search_x2, search_y1, search_x2, search_y2);
1228                 draw_line(frame, search_x2, search_y2, search_x1, search_y2);
1229                 draw_line(frame, search_x1, search_y2, search_x1, search_y1);
1230         }
1231 }
1232
1233
1234
1235 void MotionMain2::draw_pixel(VFrame *frame, int x, int y)
1236 {
1237         if(!(x >= 0 && y >= 0 && x < frame->get_w() && y < frame->get_h())) return;
1238
1239 #define DRAW_PIXEL(x, y, components, do_yuv, max, type) \
1240 { \
1241         type **rows = (type**)frame->get_rows(); \
1242         rows[y][x * components] = max - rows[y][x * components]; \
1243         if(!do_yuv) \
1244         { \
1245                 rows[y][x * components + 1] = max - rows[y][x * components + 1]; \
1246                 rows[y][x * components + 2] = max - rows[y][x * components + 2]; \
1247         } \
1248         else \
1249         { \
1250                 rows[y][x * components + 1] = (max / 2 + 1) - rows[y][x * components + 1]; \
1251                 rows[y][x * components + 2] = (max / 2 + 1) - rows[y][x * components + 2]; \
1252         } \
1253         if(components == 4) \
1254                 rows[y][x * components + 3] = max; \
1255 }
1256
1257
1258         switch(frame->get_color_model())
1259         {
1260                 case BC_RGB888:
1261                         DRAW_PIXEL(x, y, 3, 0, 0xff, unsigned char);
1262                         break;
1263                 case BC_RGBA8888:
1264                         DRAW_PIXEL(x, y, 4, 0, 0xff, unsigned char);
1265                         break;
1266                 case BC_RGB_FLOAT:
1267                         DRAW_PIXEL(x, y, 3, 0, 1.0, float);
1268                         break;
1269                 case BC_RGBA_FLOAT:
1270                         DRAW_PIXEL(x, y, 4, 0, 1.0, float);
1271                         break;
1272                 case BC_YUV888:
1273                         DRAW_PIXEL(x, y, 3, 1, 0xff, unsigned char);
1274                         break;
1275                 case BC_YUVA8888:
1276                         DRAW_PIXEL(x, y, 4, 1, 0xff, unsigned char);
1277                         break;
1278                 case BC_RGB161616:
1279                         DRAW_PIXEL(x, y, 3, 0, 0xffff, uint16_t);
1280                         break;
1281                 case BC_YUV161616:
1282                         DRAW_PIXEL(x, y, 3, 1, 0xffff, uint16_t);
1283                         break;
1284                 case BC_RGBA16161616:
1285                         DRAW_PIXEL(x, y, 4, 0, 0xffff, uint16_t);
1286                         break;
1287                 case BC_YUVA16161616:
1288                         DRAW_PIXEL(x, y, 4, 1, 0xffff, uint16_t);
1289                         break;
1290         }
1291 }
1292
1293
1294 void MotionMain2::draw_line(VFrame *frame, int x1, int y1, int x2, int y2)
1295 {
1296         int w = labs(x2 - x1);
1297         int h = labs(y2 - y1);
1298 //printf("MotionMain2::draw_line 1 %d %d %d %d\n", x1, y1, x2, y2);
1299
1300         if(!w && !h)
1301         {
1302                 draw_pixel(frame, x1, y1);
1303         }
1304         else
1305         if(w > h)
1306         {
1307 // Flip coordinates so x1 < x2
1308                 if(x2 < x1)
1309                 {
1310                         y2 ^= y1;
1311                         y1 ^= y2;
1312                         y2 ^= y1;
1313                         x1 ^= x2;
1314                         x2 ^= x1;
1315                         x1 ^= x2;
1316                 }
1317                 int numerator = y2 - y1;
1318                 int denominator = x2 - x1;
1319                 for(int i = x1; i < x2; i++)
1320                 {
1321                         int y = y1 + (int64_t)(i - x1) * (int64_t)numerator / (int64_t)denominator;
1322                         draw_pixel(frame, i, y);
1323                 }
1324         }
1325         else
1326         {
1327 // Flip coordinates so y1 < y2
1328                 if(y2 < y1)
1329                 {
1330                         y2 ^= y1;
1331                         y1 ^= y2;
1332                         y2 ^= y1;
1333                         x1 ^= x2;
1334                         x2 ^= x1;
1335                         x1 ^= x2;
1336                 }
1337                 int numerator = x2 - x1;
1338                 int denominator = y2 - y1;
1339                 for(int i = y1; i < y2; i++)
1340                 {
1341                         int x = x1 + (int64_t)(i - y1) * (int64_t)numerator / (int64_t)denominator;
1342                         draw_pixel(frame, x, i);
1343                 }
1344         }
1345 //printf("MotionMain2::draw_line 2\n");
1346 }
1347
1348 #define ARROW_SIZE 10
1349 void MotionMain2::draw_arrow(VFrame *frame, int x1, int y1, int x2, int y2)
1350 {
1351         double angle = atan((float)(y2 - y1) / (float)(x2 - x1));
1352         double angle1 = angle + (float)145 / 360 * 2 * 3.14159265;
1353         double angle2 = angle - (float)145 / 360 * 2 * 3.14159265;
1354         int x3;
1355         int y3;
1356         int x4;
1357         int y4;
1358         if(x2 < x1)
1359         {
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));
1364         }
1365         else
1366         {
1367                 x3 = x2 + (int)(ARROW_SIZE * cos(angle1));
1368                 y3 = y2 + (int)(ARROW_SIZE * sin(angle1));
1369                 x4 = x2 + (int)(ARROW_SIZE * cos(angle2));
1370                 y4 = y2 + (int)(ARROW_SIZE * sin(angle2));
1371         }
1372
1373 // Main vector
1374         draw_line(frame, x1, y1, x2, y2);
1375 //      draw_line(frame, x1, y1 + 1, x2, y2 + 1);
1376
1377 // Arrow line
1378         if(abs(y2 - y1) || abs(x2 - x1)) draw_line(frame, x2, y2, x3, y3);
1379 //      draw_line(frame, x2, y2 + 1, x3, y3 + 1);
1380 // Arrow line
1381         if(abs(y2 - y1) || abs(x2 - x1)) draw_line(frame, x2, y2, x4, y4);
1382 //      draw_line(frame, x2, y2 + 1, x4, y4 + 1);
1383 }
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394 #if 0
1395
1396
1397
1398 #define ABS_DIFF(type, temp_type, multiplier, components) \
1399 { \
1400         temp_type result_temp = 0; \
1401         for(int i = 0; i < h; i++) \
1402         { \
1403                 type *prev_row = (type*)prev_ptr; \
1404                 type *current_row = (type*)current_ptr; \
1405                 for(int j = 0; j < w; j++) \
1406                 { \
1407                         for(int k = 0; k < 3; k++) \
1408                         { \
1409                                 temp_type difference; \
1410                                 difference = *prev_row++ - *current_row++; \
1411                                 if(difference < 0) \
1412                                         result_temp -= difference; \
1413                                 else \
1414                                         result_temp += difference; \
1415                         } \
1416                         if(components == 4) \
1417                         { \
1418                                 prev_row++; \
1419                                 current_row++; \
1420                         } \
1421                 } \
1422                 prev_ptr += row_bytes; \
1423                 current_ptr += row_bytes; \
1424         } \
1425         result = (int64_t)(result_temp * multiplier); \
1426 }
1427
1428 int64_t MotionMain2::abs_diff(unsigned char *prev_ptr,
1429         unsigned char *current_ptr,
1430         int row_bytes,
1431         int w,
1432         int h,
1433         int color_model)
1434 {
1435         int64_t result = 0;
1436         switch(color_model)
1437         {
1438                 case BC_RGB888:
1439                         ABS_DIFF(unsigned char, int64_t, 1, 3)
1440                         break;
1441                 case BC_RGBA8888:
1442                         ABS_DIFF(unsigned char, int64_t, 1, 4)
1443                         break;
1444                 case BC_RGB_FLOAT:
1445                         ABS_DIFF(float, double, 0x10000, 3)
1446                         break;
1447                 case BC_RGBA_FLOAT:
1448                         ABS_DIFF(float, double, 0x10000, 4)
1449                         break;
1450                 case BC_YUV888:
1451                         ABS_DIFF(unsigned char, int64_t, 1, 3)
1452                         break;
1453                 case BC_YUVA8888:
1454                         ABS_DIFF(unsigned char, int64_t, 1, 4)
1455                         break;
1456                 case BC_YUV161616:
1457                         ABS_DIFF(uint16_t, int64_t, 1, 3)
1458                         break;
1459                 case BC_YUVA16161616:
1460                         ABS_DIFF(uint16_t, int64_t, 1, 4)
1461                         break;
1462         }
1463         return result;
1464 }
1465
1466
1467
1468 #define ABS_DIFF_SUB(type, temp_type, multiplier, components) \
1469 { \
1470         temp_type result_temp = 0; \
1471         temp_type y2_fraction = sub_y * 0x100 / OVERSAMPLE; \
1472         temp_type y1_fraction = 0x100 - y2_fraction; \
1473         temp_type x2_fraction = sub_x * 0x100 / OVERSAMPLE; \
1474         temp_type x1_fraction = 0x100 - x2_fraction; \
1475         for(int i = 0; i < h_sub; i++) \
1476         { \
1477                 type *prev_row1 = (type*)prev_ptr; \
1478                 type *prev_row2 = (type*)prev_ptr + components; \
1479                 type *prev_row3 = (type*)(prev_ptr + row_bytes); \
1480                 type *prev_row4 = (type*)(prev_ptr + row_bytes) + components; \
1481                 type *current_row = (type*)current_ptr; \
1482                 for(int j = 0; j < w_sub; j++) \
1483                 { \
1484                         for(int k = 0; k < 3; k++) \
1485                         { \
1486                                 temp_type difference; \
1487                                 temp_type prev_value = \
1488                                         (*prev_row1++ * x1_fraction * y1_fraction + \
1489                                         *prev_row2++ * x2_fraction * y1_fraction + \
1490                                         *prev_row3++ * x1_fraction * y2_fraction + \
1491                                         *prev_row4++ * x2_fraction * y2_fraction) / \
1492                                         0x100 / 0x100; \
1493                                 temp_type current_value = *current_row++; \
1494                                 difference = prev_value - current_value; \
1495                                 if(difference < 0) \
1496                                         result_temp -= difference; \
1497                                 else \
1498                                         result_temp += difference; \
1499                         } \
1500  \
1501                         if(components == 4) \
1502                         { \
1503                                 prev_row1++; \
1504                                 prev_row2++; \
1505                                 prev_row3++; \
1506                                 prev_row4++; \
1507                                 current_row++; \
1508                         } \
1509                 } \
1510                 prev_ptr += row_bytes; \
1511                 current_ptr += row_bytes; \
1512         } \
1513         result = (int64_t)(result_temp * multiplier); \
1514 }
1515
1516
1517
1518
1519 int64_t MotionMain2::abs_diff_sub(unsigned char *prev_ptr,
1520         unsigned char *current_ptr,
1521         int row_bytes,
1522         int w,
1523         int h,
1524         int color_model,
1525         int sub_x,
1526         int sub_y)
1527 {
1528         int h_sub = h - 1;
1529         int w_sub = w - 1;
1530         int64_t result = 0;
1531
1532         switch(color_model)
1533         {
1534                 case BC_RGB888:
1535                         ABS_DIFF_SUB(unsigned char, int64_t, 1, 3)
1536                         break;
1537                 case BC_RGBA8888:
1538                         ABS_DIFF_SUB(unsigned char, int64_t, 1, 4)
1539                         break;
1540                 case BC_RGB_FLOAT:
1541                         ABS_DIFF_SUB(float, double, 0x10000, 3)
1542                         break;
1543                 case BC_RGBA_FLOAT:
1544                         ABS_DIFF_SUB(float, double, 0x10000, 4)
1545                         break;
1546                 case BC_YUV888:
1547                         ABS_DIFF_SUB(unsigned char, int64_t, 1, 3)
1548                         break;
1549                 case BC_YUVA8888:
1550                         ABS_DIFF_SUB(unsigned char, int64_t, 1, 4)
1551                         break;
1552                 case BC_YUV161616:
1553                         ABS_DIFF_SUB(uint16_t, int64_t, 1, 3)
1554                         break;
1555                 case BC_YUVA16161616:
1556                         ABS_DIFF_SUB(uint16_t, int64_t, 1, 4)
1557                         break;
1558         }
1559         return result;
1560 }
1561
1562
1563
1564
1565
1566 MotionScanPackage::MotionScanPackage()
1567  : LoadPackage()
1568 {
1569         valid = 1;
1570 }
1571
1572
1573
1574
1575
1576
1577 MotionScanUnit::MotionScanUnit(MotionScan *server, 
1578         MotionMain2 *plugin)
1579  : LoadClient(server)
1580 {
1581         this->plugin = plugin;
1582         this->server = server;
1583         cache_lock = new Mutex("MotionScanUnit::cache_lock");
1584 }
1585
1586 MotionScanUnit::~MotionScanUnit()
1587 {
1588         delete cache_lock;
1589 }
1590
1591
1592
1593 void MotionScanUnit::process_package(LoadPackage *package)
1594 {
1595         MotionScanPackage *pkg = (MotionScanPackage*)package;
1596         int w = server->current_frame->get_w();
1597         int h = server->current_frame->get_h();
1598         int color_model = server->current_frame->get_color_model();
1599         int pixel_size = cmodel_calculate_pixelsize(color_model);
1600         int row_bytes = server->current_frame->get_bytes_per_line();
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614 // Single pixel
1615         if(!server->subpixel)
1616         {
1617                 int search_x = pkg->scan_x1 + (pkg->pixel % (pkg->scan_x2 - pkg->scan_x1));
1618                 int search_y = pkg->scan_y1 + (pkg->pixel / (pkg->scan_x2 - pkg->scan_x1));
1619
1620 // Try cache
1621                 pkg->difference1 = server->get_cache(search_x, search_y);
1622                 if(pkg->difference1 < 0)
1623                 {
1624 //printf("MotionScanUnit::process_package 1 %d %d\n", 
1625 //search_x, search_y, pkg->block_x2 - pkg->block_x1, pkg->block_y2 - pkg->block_y1);
1626 // Pointers to first pixel in each block
1627                         unsigned char *prev_ptr = server->previous_frame->get_rows()[
1628                                 search_y] +     
1629                                 search_x * pixel_size;
1630                         unsigned char *current_ptr = server->current_frame->get_rows()[
1631                                 pkg->block_y1] +
1632                                 pkg->block_x1 * pixel_size;
1633 // Scan block
1634                         pkg->difference1 = plugin->abs_diff(prev_ptr,
1635                                 current_ptr,
1636                                 row_bytes,
1637                                 pkg->block_x2 - pkg->block_x1,
1638                                 pkg->block_y2 - pkg->block_y1,
1639                                 color_model);
1640 //printf("MotionScanUnit::process_package 2\n");
1641                         server->put_cache(search_x, search_y, pkg->difference1);
1642                 }
1643         }
1644
1645
1646
1647
1648
1649
1650
1651         else
1652
1653
1654
1655
1656
1657
1658
1659
1660 // Sub pixel
1661         {
1662                 int sub_x = pkg->pixel % (OVERSAMPLE * 2 - 1) + 1;
1663                 int sub_y = pkg->pixel / (OVERSAMPLE * 2 - 1) + 1;
1664
1665                 if(plugin->config.horizontal_only)
1666                 {
1667                         sub_y = 0;
1668                 }
1669
1670                 if(plugin->config.vertical_only)
1671                 {
1672                         sub_x = 0;
1673                 }
1674
1675                 int search_x = pkg->scan_x1 + sub_x / OVERSAMPLE;
1676                 int search_y = pkg->scan_y1 + sub_y / OVERSAMPLE;
1677                 sub_x %= OVERSAMPLE;
1678                 sub_y %= OVERSAMPLE;
1679
1680
1681                 unsigned char *prev_ptr = server->previous_frame->get_rows()[
1682                         search_y] +
1683                         search_x * pixel_size;
1684                 unsigned char *current_ptr = server->current_frame->get_rows()[
1685                         pkg->block_y1] +
1686                         pkg->block_x1 * pixel_size;
1687
1688 // With subpixel, there are two ways to compare each position, one by shifting
1689 // the previous frame and two by shifting the current frame.
1690                 pkg->difference1 = plugin->abs_diff_sub(prev_ptr,
1691                         current_ptr,
1692                         row_bytes,
1693                         pkg->block_x2 - pkg->block_x1,
1694                         pkg->block_y2 - pkg->block_y1,
1695                         color_model,
1696                         sub_x,
1697                         sub_y);
1698                 pkg->difference2 = plugin->abs_diff_sub(current_ptr,
1699                         prev_ptr,
1700                         row_bytes,
1701                         pkg->block_x2 - pkg->block_x1,
1702                         pkg->block_y2 - pkg->block_y1,
1703                         color_model,
1704                         sub_x,
1705                         sub_y);
1706 // printf("MotionScanUnit::process_package sub_x=%d sub_y=%d search_x=%d search_y=%d diff1=%lld diff2=%lld\n",
1707 // sub_x,
1708 // sub_y,
1709 // search_x,
1710 // search_y,
1711 // pkg->difference1,
1712 // pkg->difference2);
1713         }
1714
1715
1716
1717
1718
1719 }
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730 int64_t MotionScanUnit::get_cache(int x, int y)
1731 {
1732         int64_t result = -1;
1733         cache_lock->lock("MotionScanUnit::get_cache");
1734         for(int i = 0; i < cache.total; i++)
1735         {
1736                 MotionScanCache *ptr = cache.values[i];
1737                 if(ptr->x == x && ptr->y == y)
1738                 {
1739                         result = ptr->difference;
1740                         break;
1741                 }
1742         }
1743         cache_lock->unlock();
1744         return result;
1745 }
1746
1747 void MotionScanUnit::put_cache(int x, int y, int64_t difference)
1748 {
1749         MotionScanCache *ptr = new MotionScanCache(x, y, difference);
1750         cache_lock->lock("MotionScanUnit::put_cache");
1751         cache.append(ptr);
1752         cache_lock->unlock();
1753 }
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765 MotionScan::MotionScan(MotionMain2 *plugin, 
1766         int total_clients,
1767         int total_packages)
1768  : LoadServer(
1769 //1, 1 
1770 total_clients, total_packages 
1771 )
1772 {
1773         this->plugin = plugin;
1774         cache_lock = new Mutex("MotionScan::cache_lock");
1775 }
1776
1777 MotionScan::~MotionScan()
1778 {
1779         delete cache_lock;
1780 }
1781
1782
1783 void MotionScan::init_packages()
1784 {
1785 // Set package coords
1786         for(int i = 0; i < get_total_packages(); i++)
1787         {
1788                 MotionScanPackage *pkg = (MotionScanPackage*)get_package(i);
1789
1790                 pkg->block_x1 = block_x1;
1791                 pkg->block_x2 = block_x2;
1792                 pkg->block_y1 = block_y1;
1793                 pkg->block_y2 = block_y2;
1794                 pkg->scan_x1 = scan_x1;
1795                 pkg->scan_x2 = scan_x2;
1796                 pkg->scan_y1 = scan_y1;
1797                 pkg->scan_y2 = scan_y2;
1798                 pkg->pixel = (int64_t)i * (int64_t)total_pixels / (int64_t)total_steps;
1799                 pkg->difference1 = 0;
1800                 pkg->difference2 = 0;
1801                 pkg->dx = 0;
1802                 pkg->dy = 0;
1803                 pkg->valid = 1;
1804         }
1805 }
1806
1807 LoadClient* MotionScan::new_client()
1808 {
1809         return new MotionScanUnit(this, plugin);
1810 }
1811
1812 LoadPackage* MotionScan::new_package()
1813 {
1814         return new MotionScanPackage;
1815 }
1816
1817
1818 void MotionScan::scan_frame(VFrame *previous_frame,
1819         VFrame *current_frame,
1820         int point)
1821 {
1822         this->previous_frame = previous_frame;
1823         this->current_frame = current_frame;
1824         this->point = point;
1825         subpixel = 0;
1826
1827         cache.remove_all_objects();
1828
1829
1830 // Single macroblock
1831         int w = current_frame->get_w();
1832         int h = current_frame->get_h();
1833
1834 // Initial search parameters
1835         int scan_w = w * plugin->config.global_range_w[point] / 100;
1836         int scan_h = h * plugin->config.global_range_h[point] / 100;
1837         int block_w = w * plugin->config.global_block_w[point] / 100;
1838         int block_h = h * plugin->config.global_block_h[point] / 100;
1839
1840 // Location of block in previous frame
1841         block_x1 = (int)(w * plugin->config.block_x[point] / 100 - block_w / 2);
1842         block_y1 = (int)(h * plugin->config.block_y[point] / 100 - block_h / 2);
1843         block_x2 = (int)(w * plugin->config.block_x[point] / 100 + block_w / 2);
1844         block_y2 = (int)(h * plugin->config.block_y[point] / 100 + block_h / 2);
1845
1846 // Offset to location of previous block.  This offset needn't be very accurate
1847 // since it's the offset of the previous image and current image we want.
1848         if(plugin->config.tracking_object == MotionScan::TRACK_PREVIOUS)
1849         {
1850                 block_x1 += plugin->total_dx[point] / OVERSAMPLE;
1851                 block_y1 += plugin->total_dy[point] / OVERSAMPLE;
1852                 block_x2 += plugin->total_dx[point] / OVERSAMPLE;
1853                 block_y2 += plugin->total_dy[point] / OVERSAMPLE;
1854         }
1855
1856         skip = 0;
1857
1858         switch(plugin->config.calculation)
1859         {
1860 // Don't calculate
1861                 case MotionScan::NO_CALCULATE:
1862                         dx_result = 0;
1863                         dy_result = 0;
1864                         skip = 1;
1865                         break;
1866
1867                 case MotionScan::LOAD:
1868                 {
1869 printf("MotionScan::scan_frame %d\n", __LINE__);
1870 // Load result from disk
1871                         char string[BCTEXTLEN];
1872                         sprintf(string, "%s%06d", 
1873                                 MOTION_FILE, 
1874                                 plugin->get_source_position());
1875                         FILE *input;
1876                         input = fopen(string, "r");
1877                         if(input)
1878                         {
1879                                 for(int i = 0; i <= point; i++)
1880                                 {
1881                                         fscanf(input, 
1882                                                 "%d %d", 
1883                                                 &dx_result,
1884                                                 &dy_result);
1885                                 }
1886                                 fclose(input);
1887                                 skip = 1;
1888                         }
1889                         break;
1890                 }
1891
1892 // Scan from scratch
1893                 default:
1894                         skip = 0;
1895                         break;
1896         }
1897
1898 // Perform scan
1899         if(!skip)
1900         {
1901 printf("MotionScan::scan_frame %d\n", __LINE__);
1902 // Calculate center of search area in current frame
1903                 int origin_offset_x = plugin->config.global_origin_x[point] * w / 100;
1904                 int origin_offset_y = plugin->config.global_origin_y[point] * h / 100;
1905                 int x_result = block_x1 + origin_offset_x;
1906                 int y_result = block_y1 + origin_offset_y;
1907
1908 // printf("MotionScan::scan_frame 1 %d %d %d %d %d %d %d %d\n",
1909 // block_x1 + block_w / 2,
1910 // block_y1 + block_h / 2,
1911 // block_w,
1912 // block_h,
1913 // block_x1,
1914 // block_y1,
1915 // block_x2,
1916 // block_y2);
1917
1918                 while(1)
1919                 {
1920                         scan_x1 = x_result - scan_w / 2;
1921                         scan_y1 = y_result - scan_h / 2;
1922                         scan_x2 = x_result + scan_w / 2;
1923                         scan_y2 = y_result + scan_h / 2;
1924
1925
1926
1927 // Zero out requested values
1928                         if(plugin->config.horizontal_only)
1929                         {
1930                                 scan_y1 = block_y1;
1931                                 scan_y2 = block_y1 + 1;
1932                         }
1933                         if(plugin->config.vertical_only)
1934                         {
1935                                 scan_x1 = block_x1;
1936                                 scan_x2 = block_x1 + 1;
1937                         }
1938
1939 // printf("MotionScan::scan_frame 1 %d %d %d %d %d %d %d %d\n",
1940 // block_x1,
1941 // block_y1,
1942 // block_x2,
1943 // block_y2,
1944 // scan_x1,
1945 // scan_y1,
1946 // scan_x2,
1947 // scan_y2);
1948 // Clamp the block coords before the scan so we get useful scan coords.
1949                         MotionScan::clamp_scan(w, 
1950                                 h, 
1951                                 &block_x1,
1952                                 &block_y1,
1953                                 &block_x2,
1954                                 &block_y2,
1955                                 &scan_x1,
1956                                 &scan_y1,
1957                                 &scan_x2,
1958                                 &scan_y2,
1959                                 0);
1960 // 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", 
1961 // block_x1,
1962 // block_y1,
1963 // block_x2,
1964 // block_y2,
1965 // scan_x1, 
1966 // scan_y1, 
1967 // scan_x2, 
1968 // scan_y2, 
1969 // x_result, 
1970 // y_result);
1971
1972
1973 // Give up if invalid coords.
1974                         if(scan_y2 <= scan_y1 ||
1975                                 scan_x2 <= scan_x1 ||
1976                                 block_x2 <= block_x1 ||
1977                                 block_y2 <= block_y1)
1978                                 break;
1979
1980
1981 // For subpixel, the top row and left column are skipped
1982                         if(subpixel)
1983                         {
1984
1985                                 if(plugin->config.horizontal_only ||
1986                                         plugin->config.vertical_only)
1987                                 {
1988                                         total_pixels = 4 * OVERSAMPLE * OVERSAMPLE - 4 * OVERSAMPLE;
1989                                 }
1990                                 else
1991                                 {
1992                                         total_pixels = 4 * OVERSAMPLE;
1993                                 }
1994
1995                                 total_steps = total_pixels;
1996
1997                                 set_package_count(total_steps);
1998                                 process_packages();
1999
2000 // Get least difference
2001                                 int64_t min_difference = -1;
2002                                 for(int i = 0; i < get_total_packages(); i++)
2003                                 {
2004                                         MotionScanPackage *pkg = (MotionScanPackage*)get_package(i);
2005                                         if(pkg->difference1 < min_difference || min_difference == -1)
2006                                         {
2007                                                 min_difference = pkg->difference1;
2008
2009                                                 if(plugin->config.vertical_only)
2010                                                         x_result = scan_x1 * OVERSAMPLE;
2011                                                 else
2012                                                         x_result = scan_x1 * OVERSAMPLE + 
2013                                                                 (pkg->pixel % (OVERSAMPLE * 2 - 1)) + 1;
2014                                                 
2015                                                 if(plugin->config.horizontal_only)
2016                                                         y_result = scan_y1 * OVERSAMPLE;
2017                                                 else
2018                                                         y_result = scan_y1 * OVERSAMPLE + 
2019                                                                 (pkg->pixel / (OVERSAMPLE * 2 - 1)) + 1;
2020
2021
2022 // Fill in results
2023                                                 dx_result = block_x1 * OVERSAMPLE - x_result;
2024                                                 dy_result = block_y1 * OVERSAMPLE - y_result;
2025                                         }
2026
2027                                         if(pkg->difference2 < min_difference)
2028                                         {
2029                                                 min_difference = pkg->difference2;
2030
2031                                                 if(plugin->config.vertical_only)
2032                                                         x_result = scan_x1 * OVERSAMPLE;
2033                                                 else
2034                                                         x_result = scan_x2 * OVERSAMPLE -
2035                                                                 ((pkg->pixel % (OVERSAMPLE * 2 - 1)) + 1);
2036
2037                                                 if(plugin->config.horizontal_only)
2038                                                         y_result = scan_y1 * OVERSAMPLE;
2039                                                 else
2040                                                         y_result = scan_y2 * OVERSAMPLE -
2041                                                                 ((pkg->pixel / (OVERSAMPLE * 2 - 1)) + 1);
2042
2043                                                 dx_result = block_x1 * OVERSAMPLE - x_result;
2044                                                 dy_result = block_y1 * OVERSAMPLE - y_result;
2045                                         }
2046                                 }
2047
2048
2049 //printf("MotionScan::scan_frame 1 %d %d %d %d\n", block_x1, block_y1, x_result, y_result);
2050                                 break;
2051                         }
2052                         else
2053                         {
2054
2055                                 total_pixels = (scan_x2 - scan_x1) * (scan_y2 - scan_y1);
2056                                 total_steps = MIN(plugin->config.global_positions, total_pixels);
2057
2058                                 set_package_count(total_steps);
2059                                 process_packages();
2060
2061 // Get least difference
2062                                 int64_t min_difference = -1;
2063                                 for(int i = 0; i < get_total_packages(); i++)
2064                                 {
2065                                         MotionScanPackage *pkg = (MotionScanPackage*)get_package(i);
2066                                         if(pkg->difference1 < min_difference || min_difference == -1)
2067                                         {
2068                                                 min_difference = pkg->difference1;
2069                                                 x_result = scan_x1 + (pkg->pixel % (scan_x2 - scan_x1));
2070                                                 y_result = scan_y1 + (pkg->pixel / (scan_x2 - scan_x1));
2071                                                 x_result *= OVERSAMPLE;
2072                                                 y_result *= OVERSAMPLE;
2073                                         }
2074                                 }
2075
2076
2077
2078 // printf("MotionScan::scan_frame 10 total_steps=%d total_pixels=%d subpixel=%d\n",
2079 // total_steps, 
2080 // total_pixels,
2081 // subpixel);
2082 // 
2083 // printf("     scan w=%d h=%d scan x1=%d y1=%d x2=%d y2=%d\n",
2084 // scan_w,
2085 // scan_h, 
2086 // scan_x1,
2087 // scan_y1,
2088 // scan_x2,
2089 // scan_y2);
2090 // 
2091 // printf("MotionScan::scan_frame 2 block x1=%d y1=%d x2=%d y2=%d result x=%.2f y=%.2f\n", 
2092 // block_x1, 
2093 // block_y1, 
2094 // block_x2,
2095 // block_y2,
2096 // (float)x_result / 4, 
2097 // (float)y_result / 4);
2098
2099
2100 // If a new search is required, rescale results back to pixels.
2101                                 if(total_steps >= total_pixels)
2102                                 {
2103 // Single pixel accuracy reached.  Now do exhaustive subpixel search.
2104                                         if(plugin->config.action == MotionScan::STABILIZE ||
2105                                                 plugin->config.action == MotionScan::TRACK ||
2106                                                 plugin->config.action == MotionScan::NOTHING)
2107                                         {
2108                                                 x_result /= OVERSAMPLE;
2109                                                 y_result /= OVERSAMPLE;
2110                                                 scan_w = 2;
2111                                                 scan_h = 2;
2112                                                 subpixel = 1;
2113                                         }
2114                                         else
2115                                         {
2116 // Fill in results and quit
2117                                                 dx_result = block_x1 * OVERSAMPLE - x_result;
2118                                                 dy_result = block_y1 * OVERSAMPLE - y_result;
2119                                                 break;
2120                                         }
2121                                 }
2122                                 else
2123 // Reduce scan area and try again
2124                                 {
2125                                         scan_w = (scan_x2 - scan_x1) / 2;
2126                                         scan_h = (scan_y2 - scan_y1) / 2;
2127                                         x_result /= OVERSAMPLE;
2128                                         y_result /= OVERSAMPLE;
2129                                 }
2130                         }
2131                 }
2132
2133                 dx_result *= -1;
2134                 dy_result *= -1;
2135         }
2136
2137
2138
2139
2140
2141
2142 // Write results
2143         if(plugin->config.calculation == MotionScan::SAVE)
2144         {
2145                 char string[BCTEXTLEN];
2146                 sprintf(string, 
2147                         "%s%06d", 
2148                         MOTION_FILE, 
2149                         plugin->get_source_position());
2150                 FILE *output;
2151                 if(point == 0)
2152                         output = fopen(string, "w");
2153                 else
2154                         output = fopen(string, "a");
2155                 if(output)
2156                 {
2157                         fprintf(output, 
2158                                 "%d %d\n",
2159                                 dx_result,
2160                                 dy_result);
2161                         fclose(output);
2162                 }
2163                 else
2164                 {
2165                         perror("MotionScan::scan_frame SAVE 1");
2166                 }
2167         }
2168
2169 #ifdef DEBUG
2170 printf("MotionScan::scan_frame 10 point=%d dx=%.2f dy=%.2f\n", 
2171 point,
2172 (float)this->dx_result / OVERSAMPLE,
2173 (float)this->dy_result / OVERSAMPLE);
2174 #endif
2175 }
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193 int64_t MotionScan::get_cache(int x, int y)
2194 {
2195         int64_t result = -1;
2196         cache_lock->lock("MotionScan::get_cache");
2197         for(int i = 0; i < cache.total; i++)
2198         {
2199                 MotionScanCache *ptr = cache.values[i];
2200                 if(ptr->x == x && ptr->y == y)
2201                 {
2202                         result = ptr->difference;
2203                         break;
2204                 }
2205         }
2206         cache_lock->unlock();
2207         return result;
2208 }
2209
2210 void MotionScan::put_cache(int x, int y, int64_t difference)
2211 {
2212         MotionScanCache *ptr = new MotionScanCache(x, y, difference);
2213         cache_lock->lock("MotionScan::put_cache");
2214         cache.append(ptr);
2215         cache_lock->unlock();
2216 }
2217
2218
2219
2220
2221
2222 MotionScanCache::MotionScanCache(int x, int y, int64_t difference)
2223 {
2224         this->x = x;
2225         this->y = y;
2226         this->difference = difference;
2227 }
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238 #endif
2239
2240
2241