fixes for colorpicker loops/deadlocks, add mask smooth_buttons, rm orig mask_smooth
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / motion / motion.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2012 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 "bcdisplayinfo.h"
24 #include "clip.h"
25 #include "bchash.h"
26 #include "bcsignals.h"
27 #include "filexml.h"
28 #include "keyframe.h"
29 #include "language.h"
30 #include "mainerror.h"
31 #include "motion.h"
32 #include "motionscan.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(MotionMain)
44
45
46 //#define DEBUG
47
48 MotionConfig::MotionConfig()
49 {
50         global_range_w = 25; //5;
51         global_range_h = 25; //5;
52         rotation_range = 8; //5;
53         rotation_center = 0;
54         block_count = 1;
55         global_block_w = 33; //MIN_BLOCK;
56         global_block_h = 33; //MIN_BLOCK;
57         block_x = 50;
58         block_y = 50;
59         global_positions = 256;
60         rotate_positions = 8; // 4;
61         magnitude = 100;
62         rotate_magnitude = 30;
63         return_speed = 5; //0;
64         rotate_return_speed = 5; //0;
65         action_type = MotionScan::STABILIZE;
66         global = 1;
67         rotate = 1;
68         addtrackedframeoffset = 0;
69         strcpy(tracking_file, TRACKING_FILE);
70         tracking_type = MotionScan::SAVE; //MotionScan::NO_CALCULATE;
71         tracking_object = MotionScan::TRACK_PREVIOUS; //TRACK_SINGLE;
72         draw_vectors = 1; //0;
73         track_frame = 0;
74         bottom_is_master = 1;
75         horizontal_only = 0;
76         vertical_only = 0;
77 }
78
79
80 void MotionConfig::boundaries()
81 {
82         CLAMP(global_range_w, MIN_RADIUS, MAX_RADIUS);
83         CLAMP(global_range_h, MIN_RADIUS, MAX_RADIUS);
84         CLAMP(rotation_range, MIN_ROTATION, MAX_ROTATION);
85         CLAMP(rotation_center, -MAX_ROTATION, MAX_ROTATION);
86         CLAMP(block_count, MIN_BLOCKS, MAX_BLOCKS);
87         CLAMP(global_block_w, MIN_BLOCK, MAX_BLOCK);
88         CLAMP(global_block_h, MIN_BLOCK, MAX_BLOCK);
89 }
90
91 int MotionConfig::equivalent(MotionConfig &that)
92 {
93         return global_range_w == that.global_range_w &&
94                 global_range_h == that.global_range_h &&
95                 rotation_range == that.rotation_range &&
96                 rotation_center == that.rotation_center &&
97                 action_type == that.action_type &&
98                 global == that.global && rotate == that.rotate &&
99                 addtrackedframeoffset == that.addtrackedframeoffset &&
100                 draw_vectors == that.draw_vectors &&
101                 block_count == that.block_count &&
102                 global_block_w == that.global_block_w &&
103                 global_block_h == that.global_block_h &&
104                 EQUIV(block_x, that.block_x) &&
105                 EQUIV(block_y, that.block_y) &&
106                 global_positions == that.global_positions &&
107                 rotate_positions == that.rotate_positions &&
108                 magnitude == that.magnitude &&
109                 return_speed == that.return_speed &&
110                 rotate_return_speed == that.rotate_return_speed &&
111                 rotate_magnitude == that.rotate_magnitude &&
112                 tracking_object == that.tracking_object &&
113                 track_frame == that.track_frame &&
114                 bottom_is_master == that.bottom_is_master &&
115                 horizontal_only == that.horizontal_only &&
116                 vertical_only == that.vertical_only;
117 }
118
119 void MotionConfig::copy_from(MotionConfig &that)
120 {
121         global_range_w = that.global_range_w;
122         global_range_h = that.global_range_h;
123         rotation_range = that.rotation_range;
124         rotation_center = that.rotation_center;
125         action_type = that.action_type;
126         global = that.global;
127         rotate = that.rotate;
128         addtrackedframeoffset = that.addtrackedframeoffset;
129         tracking_type = that.tracking_type;
130         draw_vectors = that.draw_vectors;
131         block_count = that.block_count;
132         block_x = that.block_x;
133         block_y = that.block_y;
134         global_positions = that.global_positions;
135         rotate_positions = that.rotate_positions;
136         global_block_w = that.global_block_w;
137         global_block_h = that.global_block_h;
138         magnitude = that.magnitude;
139         return_speed = that.return_speed;
140         rotate_magnitude = that.rotate_magnitude;
141         rotate_return_speed = that.rotate_return_speed;
142         tracking_object = that.tracking_object;
143         track_frame = that.track_frame;
144         bottom_is_master = that.bottom_is_master;
145         horizontal_only = that.horizontal_only;
146         vertical_only = that.vertical_only;
147 }
148
149 void MotionConfig::interpolate(MotionConfig &prev, MotionConfig &next,
150         int64_t prev_frame, int64_t next_frame, int64_t current_frame)
151 {
152         copy_from(prev);
153 }
154
155
156 MotionMain::MotionMain(PluginServer *server)
157  : PluginVClient(server)
158 {
159         engine = 0;
160         rotate_engine = 0;
161         motion_rotate = 0;
162         total_dx = 0;
163         total_dy = 0;
164         total_angle = 0;
165         overlayer = 0;
166         search_area = 0;
167         search_size = 0;
168         temp_frame = 0;
169         previous_frame_number = -1;
170
171         prev_global_ref = 0;
172         current_global_ref = 0;
173         global_target_src = 0;
174         global_target_dst = 0;
175
176         cache_file[0] = 0;
177         cache_fp = active_fp = 0;
178         cache_line[0] = 0;
179         cache_key = active_key = -1;
180         dx_offset = dy_offset = 0;
181         load_ok = 0;
182         save_dx = load_dx = 0;
183         save_dy = load_dy = 0;
184         save_dt = load_dt = 0;
185         tracking_frame = -1;
186         prev_rotate_ref = 0;
187         current_rotate_ref = 0;
188         rotate_target_src = 0;
189         rotate_target_dst = 0;
190 }
191
192 MotionMain::~MotionMain()
193 {
194
195         delete engine;
196         delete overlayer;
197         delete [] search_area;
198         delete temp_frame;
199         delete rotate_engine;
200         delete motion_rotate;
201
202         delete prev_global_ref;
203         delete current_global_ref;
204         delete global_target_src;
205         delete global_target_dst;
206
207         reset_cache_file();
208
209         delete prev_rotate_ref;
210         delete current_rotate_ref;
211         delete rotate_target_src;
212         delete rotate_target_dst;
213 }
214
215 const char* MotionMain::plugin_title() { return N_("Motion"); }
216 int MotionMain::is_realtime() { return 1; }
217 int MotionMain::is_multichannel() { return 1; }
218
219
220 NEW_WINDOW_MACRO(MotionMain, MotionWindow)
221
222 LOAD_CONFIGURATION_MACRO(MotionMain, MotionConfig)
223
224
225
226 void MotionMain::update_gui()
227 {
228         if( !thread ) return;
229         if( !load_configuration() ) return;
230         thread->window->lock_window("MotionMain::update_gui");
231         MotionWindow *window = (MotionWindow*)thread->window;
232
233         char string[BCTEXTLEN];
234         sprintf(string, "%d", config.global_positions);
235         window->global_search_positions->set_text(string);
236         sprintf(string, "%d", config.rotate_positions);
237         window->rotation_search_positions->set_text(string);
238
239         window->global_block_w->update(config.global_block_w);
240         window->global_block_h->update(config.global_block_h);
241         window->block_x->update(config.block_x);
242         window->block_y->update(config.block_y);
243         window->block_x_text->update((float)config.block_x);
244         window->block_y_text->update((float)config.block_y);
245         window->magnitude->update(config.magnitude);
246         window->return_speed->update(config.return_speed);
247         window->rotate_magnitude->update(config.rotate_magnitude);
248         window->rotate_return_speed->update(config.rotate_return_speed);
249         window->rotation_range->update(config.rotation_range);
250         window->rotation_center->update(config.rotation_center);
251
252
253         window->track_single->update(config.tracking_object == MotionScan::TRACK_SINGLE);
254         window->track_frame_number->update(config.track_frame);
255         window->track_previous->update(config.tracking_object == MotionScan::TRACK_PREVIOUS);
256         window->previous_same->update(config.tracking_object == MotionScan::PREVIOUS_SAME_BLOCK);
257         if( config.tracking_object != MotionScan::TRACK_SINGLE )
258                 window->track_frame_number->disable();
259         else
260                 window->track_frame_number->enable();
261
262         window->action_type->set_text(
263                 ActionType::to_text(config.action_type));
264         window->tracking_type->set_text(
265                 TrackingType::to_text(config.tracking_type));
266         window->track_direction->set_text(
267                 TrackDirection::to_text(config.horizontal_only, config.vertical_only));
268         window->master_layer->set_text(
269                 MasterLayer::to_text(config.bottom_is_master));
270
271         window->update_mode();
272         thread->window->unlock_window();
273 }
274
275
276
277
278 void MotionMain::save_data(KeyFrame *keyframe)
279 {
280         FileXML output;
281
282 // cause data to be stored directly in text
283         output.set_shared_output(keyframe->xbuf);
284         output.tag.set_title("MOTION");
285
286         output.tag.set_property("BLOCK_COUNT", config.block_count);
287         output.tag.set_property("GLOBAL_POSITIONS", config.global_positions);
288         output.tag.set_property("ROTATE_POSITIONS", config.rotate_positions);
289         output.tag.set_property("GLOBAL_BLOCK_W", config.global_block_w);
290         output.tag.set_property("GLOBAL_BLOCK_H", config.global_block_h);
291         output.tag.set_property("BLOCK_X", config.block_x);
292         output.tag.set_property("BLOCK_Y", config.block_y);
293         output.tag.set_property("GLOBAL_RANGE_W", config.global_range_w);
294         output.tag.set_property("GLOBAL_RANGE_H", config.global_range_h);
295         output.tag.set_property("ROTATION_RANGE", config.rotation_range);
296         output.tag.set_property("ROTATION_CENTER", config.rotation_center);
297         output.tag.set_property("MAGNITUDE", config.magnitude);
298         output.tag.set_property("RETURN_SPEED", config.return_speed);
299         output.tag.set_property("ROTATE_MAGNITUDE", config.rotate_magnitude);
300         output.tag.set_property("ROTATE_RETURN_SPEED", config.rotate_return_speed);
301         output.tag.set_property("ACTION_TYPE", config.action_type);
302         output.tag.set_property("GLOBAL", config.global);
303         output.tag.set_property("ROTATE", config.rotate);
304         output.tag.set_property("ADDTRACKEDFRAMEOFFSET", config.addtrackedframeoffset);
305         output.tag.set_property("TRACKING_FILE", config.tracking_file);
306         output.tag.set_property("TRACKING_TYPE", config.tracking_type);
307         output.tag.set_property("DRAW_VECTORS", config.draw_vectors);
308         output.tag.set_property("TRACKING_OBJECT", config.tracking_object);
309         output.tag.set_property("TRACK_FRAME", config.track_frame);
310         output.tag.set_property("BOTTOM_IS_MASTER", config.bottom_is_master);
311         output.tag.set_property("HORIZONTAL_ONLY", config.horizontal_only);
312         output.tag.set_property("VERTICAL_ONLY", config.vertical_only);
313         output.append_tag();
314         output.tag.set_title("/MOTION");
315         output.append_tag();
316         output.terminate_string();
317 }
318
319 void MotionMain::read_data(KeyFrame *keyframe)
320 {
321         FileXML input;
322         input.set_shared_input(keyframe->xbuf);
323         int result = 0;
324
325         while( !(result = input.read_tag()) ) {
326                 if( input.tag.title_is("MOTION") ) {
327                         config.block_count = input.tag.get_property("BLOCK_COUNT", config.block_count);
328                         config.global_positions = input.tag.get_property("GLOBAL_POSITIONS", config.global_positions);
329                         config.rotate_positions = input.tag.get_property("ROTATE_POSITIONS", config.rotate_positions);
330                         config.global_block_w = input.tag.get_property("GLOBAL_BLOCK_W", config.global_block_w);
331                         config.global_block_h = input.tag.get_property("GLOBAL_BLOCK_H", config.global_block_h);
332                         config.block_x = input.tag.get_property("BLOCK_X", config.block_x);
333                         config.block_y = input.tag.get_property("BLOCK_Y", config.block_y);
334                         config.global_range_w = input.tag.get_property("GLOBAL_RANGE_W", config.global_range_w);
335                         config.global_range_h = input.tag.get_property("GLOBAL_RANGE_H", config.global_range_h);
336                         config.rotation_range = input.tag.get_property("ROTATION_RANGE", config.rotation_range);
337                         config.rotation_center = input.tag.get_property("ROTATION_CENTER", config.rotation_center);
338                         config.magnitude = input.tag.get_property("MAGNITUDE", config.magnitude);
339                         config.return_speed = input.tag.get_property("RETURN_SPEED", config.return_speed);
340                         config.rotate_magnitude = input.tag.get_property("ROTATE_MAGNITUDE", config.rotate_magnitude);
341                         config.rotate_return_speed = input.tag.get_property("ROTATE_RETURN_SPEED", config.rotate_return_speed);
342                         config.action_type = input.tag.get_property("ACTION_TYPE", config.action_type);
343                         config.global = input.tag.get_property("GLOBAL", config.global);
344                         config.rotate = input.tag.get_property("ROTATE", config.rotate);
345                         config.addtrackedframeoffset = input.tag.get_property("ADDTRACKEDFRAMEOFFSET", config.addtrackedframeoffset);
346                         input.tag.get_property("TRACKING_FILE", config.tracking_file);
347                         config.tracking_type = input.tag.get_property("TRACKING_TYPE", config.tracking_type);
348                         config.draw_vectors = input.tag.get_property("DRAW_VECTORS", config.draw_vectors);
349                         config.tracking_object = input.tag.get_property("TRACKING_OBJECT", config.tracking_object);
350                         config.track_frame = input.tag.get_property("TRACK_FRAME", config.track_frame);
351                         config.bottom_is_master = input.tag.get_property("BOTTOM_IS_MASTER", config.bottom_is_master);
352                         config.horizontal_only = input.tag.get_property("HORIZONTAL_ONLY", config.horizontal_only);
353                         config.vertical_only = input.tag.get_property("VERTICAL_ONLY", config.vertical_only);
354                 }
355         }
356         config.boundaries();
357 }
358
359 void MotionMain::allocate_temp(int w, int h, int color_model)
360 {
361         if( temp_frame &&
362             ( temp_frame->get_w() != w || temp_frame->get_h() != h ) ) {
363                 delete temp_frame;
364                 temp_frame = 0;
365         }
366         if( !temp_frame )
367                 temp_frame = new VFrame(w, h, color_model, 0);
368 }
369
370 void MotionMain::process_global()
371 {
372
373         if( !engine ) engine = new MotionScan(PluginClient::get_project_smp() + 1,
374                 PluginClient::get_project_smp() + 1);
375
376 // Determine if frames changed
377         engine->scan_frame(current_global_ref, prev_global_ref,
378                 config.global_range_w, config.global_range_h,
379                 config.global_block_w, config.global_block_h,
380                 config.block_x, config.block_y,
381                 config.tracking_object, config.tracking_type,
382                 config.action_type, config.horizontal_only,
383                 config.vertical_only, get_source_position(),
384                 config.global_positions, total_dx, total_dy,
385                 0, 0, load_ok, load_dx, load_dy);
386         current_dx = (engine->dx_result += dx_offset);
387         current_dy = (engine->dy_result += dy_offset);
388
389 // Write results
390         if( config.tracking_type == MotionScan::SAVE ) {
391                 save_dx = engine->dx_result;
392                 save_dy = engine->dy_result;
393         }
394
395 // Add current motion vector to accumulation vector.
396         if( config.tracking_object != MotionScan::TRACK_SINGLE ) {
397 // Retract over time
398                 total_dx = (int64_t)total_dx * (100 - config.return_speed) / 100;
399                 total_dy = (int64_t)total_dy * (100 - config.return_speed) / 100;
400                 total_dx += engine->dx_result;
401                 total_dy += engine->dy_result;
402 // printf("MotionMain::process_global total_dx=%d engine->dx_result=%d\n",
403 // total_dx, engine->dx_result);
404         }
405         else {
406 // Make accumulation vector current
407                 total_dx = engine->dx_result;
408                 total_dy = engine->dy_result;
409         }
410
411 // Clamp accumulation vector
412         if( config.magnitude < 100 ) {
413                 int block_x_orig = (int64_t)(config.block_x * current_global_ref->get_w() / 100);
414                 int block_y_orig = (int64_t)(config.block_y * current_global_ref->get_h() / 100);
415                 int max_block_x = (int64_t)(current_global_ref->get_w() - block_x_orig)
416                         * OVERSAMPLE * config.magnitude / 100;
417                 int max_block_y = (int64_t)(current_global_ref->get_h() - block_y_orig)
418                         * OVERSAMPLE * config.magnitude / 100;
419                 int min_block_x = (int64_t)-block_x_orig
420                         * OVERSAMPLE * config.magnitude / 100;
421                 int min_block_y = (int64_t)-block_y_orig
422                         * OVERSAMPLE * config.magnitude / 100;
423
424                 CLAMP(total_dx, min_block_x, max_block_x);
425                 CLAMP(total_dy, min_block_y, max_block_y);
426         }
427
428 #ifdef DEBUG
429 printf("MotionMain::process_global 2 total_dx=%.02f total_dy=%.02f\n",
430   (float)total_dx / OVERSAMPLE, (float)total_dy / OVERSAMPLE);
431 #endif
432
433         if( config.tracking_object != MotionScan::TRACK_SINGLE && !config.rotate ) {
434 // Transfer current reference frame to previous reference frame and update
435 // counter.  Must wait for rotate to compare.
436                 prev_global_ref->copy_from(current_global_ref);
437                 previous_frame_number = get_source_position();
438         }
439
440 // Decide what to do with target based on requested operation
441         int interpolation = NEAREST_NEIGHBOR;
442         float dx = 0., dy = 0.;
443         switch(config.action_type) {
444         case MotionScan::NOTHING:
445                 global_target_dst->copy_from(global_target_src);
446                 break;
447         case MotionScan::TRACK_PIXEL:
448                 interpolation = NEAREST_NEIGHBOR;
449                 dx = (int)(total_dx / OVERSAMPLE);
450                 dy = (int)(total_dy / OVERSAMPLE);
451                 break;
452         case MotionScan::STABILIZE_PIXEL:
453                 interpolation = NEAREST_NEIGHBOR;
454                 dx = -(int)(total_dx / OVERSAMPLE);
455                 dy = -(int)(total_dy / OVERSAMPLE);
456                 break;
457         case MotionScan::TRACK:
458                 interpolation = CUBIC_LINEAR;
459                 dx = (float)total_dx / OVERSAMPLE;
460                 dy = (float)total_dy / OVERSAMPLE;
461                 break;
462         case MotionScan::STABILIZE:
463                 interpolation = CUBIC_LINEAR;
464                 dx = -(float)total_dx / OVERSAMPLE;
465                 dy = -(float)total_dy / OVERSAMPLE;
466                 break;
467         }
468
469
470         if( config.action_type != MotionScan::NOTHING ) {
471                 if( !overlayer )
472                         overlayer = new OverlayFrame(PluginClient::get_project_smp() + 1);
473                 global_target_dst->clear_frame();
474                 overlayer->overlay(global_target_dst, global_target_src,
475                         0, 0, global_target_src->get_w(), global_target_src->get_h(),
476                         dx, dy,
477                         (float)global_target_src->get_w() + dx,
478                         (float)global_target_src->get_h() + dy,
479                         1, TRANSFER_REPLACE, interpolation);
480         }
481 }
482
483
484
485 void MotionMain::process_rotation()
486 {
487         int block_x, block_y;
488
489 // Convert the previous global reference into the previous rotation reference.
490 // Convert global target destination into rotation target source.
491         if( config.global ) {
492                 if( !overlayer )
493                         overlayer = new OverlayFrame(PluginClient::get_project_smp() + 1);
494                 float dx, dy;
495                 if( config.tracking_object == MotionScan::TRACK_SINGLE ) {
496                         dx = (float)total_dx / OVERSAMPLE;
497                         dy = (float)total_dy / OVERSAMPLE;
498                 }
499                 else {
500                         dx = (float)current_dx / OVERSAMPLE;
501                         dy = (float)current_dy / OVERSAMPLE;
502                 }
503
504                 prev_rotate_ref->clear_frame();
505                 overlayer->overlay(prev_rotate_ref, prev_global_ref,
506                         0, 0, prev_global_ref->get_w(), prev_global_ref->get_h(),
507                         dx, dy,
508                         (float)prev_global_ref->get_w() + dx,
509                         (float)prev_global_ref->get_h() + dy,
510                         1, TRANSFER_REPLACE, CUBIC_LINEAR);
511 // Pivot is destination global position
512                 block_x = (int)(prev_rotate_ref->get_w() *
513                         config.block_x / 100 + (float)total_dx / OVERSAMPLE);
514                 block_y = (int)(prev_rotate_ref->get_h() *
515                         config.block_y / 100 + (float)total_dy / OVERSAMPLE);
516 // Use the global target output as the rotation target input
517                 rotate_target_src->copy_from(global_target_dst);
518 // Transfer current reference frame to previous reference frame for global.
519                 if( config.tracking_object != MotionScan::TRACK_SINGLE ) {
520                         prev_global_ref->copy_from(current_global_ref);
521                         previous_frame_number = get_source_position();
522                 }
523         }
524         else {
525 // Pivot is fixed
526                 block_x = (int)(prev_rotate_ref->get_w() * config.block_x / 100);
527                 block_y = (int)(prev_rotate_ref->get_h() * config.block_y / 100);
528         }
529
530 // Get rotation
531         if( !motion_rotate )
532                 motion_rotate = new RotateScan(this,
533                         get_project_smp() + 1, get_project_smp() + 1);
534
535         current_angle = motion_rotate->
536                 scan_frame(prev_rotate_ref, current_rotate_ref, block_x, block_y);
537
538 // Write results
539         if( config.tracking_type == MotionScan::SAVE ) {
540                 save_dt = current_angle;
541         }
542
543 // Add current rotation to accumulation
544         if( config.tracking_object != MotionScan::TRACK_SINGLE ) {
545 // Retract over time
546                 total_angle = total_angle * (100 - config.rotate_return_speed) / 100;
547 // Accumulate current rotation
548                 total_angle += current_angle;
549
550 // Clamp rotation accumulation
551                 if( config.rotate_magnitude < 90 ) {
552                         CLAMP(total_angle, -config.rotate_magnitude, config.rotate_magnitude);
553                 }
554
555                 if( !config.global ) {
556 // Transfer current reference frame to previous reference frame and update counter.
557                         prev_rotate_ref->copy_from(current_rotate_ref);
558                         previous_frame_number = get_source_position();
559                 }
560         }
561         else {
562                 total_angle = current_angle;
563         }
564
565 #ifdef DEBUG
566 printf("MotionMain::process_rotation total_angle=%f\n", total_angle);
567 #endif
568
569
570 // Calculate rotation parameters based on requested operation
571         float angle = 0.;
572         switch(config.action_type) {
573         case MotionScan::NOTHING:
574                 rotate_target_dst->copy_from(rotate_target_src);
575                 break;
576         case MotionScan::TRACK:
577         case MotionScan::TRACK_PIXEL:
578                 angle = total_angle;
579                 break;
580         case MotionScan::STABILIZE:
581         case MotionScan::STABILIZE_PIXEL:
582                 angle = -total_angle;
583                 break;
584         }
585
586         if( config.action_type != MotionScan::NOTHING ) {
587                 if( !rotate_engine )
588                         rotate_engine = new AffineEngine(
589                                 PluginClient::get_project_smp() + 1,
590                                 PluginClient::get_project_smp() + 1);
591
592                 rotate_target_dst->clear_frame();
593
594 // Determine pivot based on a number of factors.
595                 switch(config.action_type) {
596                 case MotionScan::TRACK:
597                 case MotionScan::TRACK_PIXEL:
598 // Use destination of global tracking.
599                         rotate_engine->set_in_pivot(block_x, block_y);
600                         rotate_engine->set_out_pivot(block_x, block_y);
601                         break;
602
603                 case MotionScan::STABILIZE:
604                 case MotionScan::STABILIZE_PIXEL:
605                         if( config.global ) {
606 // Use origin of global stabilize operation
607                                 rotate_engine->set_in_pivot(
608                                         (int)(rotate_target_dst->get_w() * config.block_x / 100),
609                                         (int)(rotate_target_dst->get_h() * config.block_y / 100));
610                                 rotate_engine->set_out_pivot(
611                                         (int)(rotate_target_dst->get_w() * config.block_x / 100),
612                                         (int)(rotate_target_dst->get_h() * config.block_y / 100));
613                                 }
614                                 else {
615 // Use origin
616                                         rotate_engine->set_in_pivot(block_x, block_y);
617                                         rotate_engine->set_out_pivot(block_x, block_y);
618                                 }
619                                 break;
620                 }
621
622                 rotate_engine->rotate(rotate_target_dst, rotate_target_src, angle);
623 // overlayer->overlay(rotate_target_dst, prev_rotate_ref,
624 //      0, 0, prev_rotate_ref->get_w(), prev_rotate_ref->get_h(),
625 //      0, 0, prev_rotate_ref->get_w(), prev_rotate_ref->get_h(),
626 //      1, TRANSFER_NORMAL, CUBIC_LINEAR);
627 // overlayer->overlay(rotate_target_dst, current_rotate_ref,
628 //      0, 0, prev_rotate_ref->get_w(), prev_rotate_ref->get_h(),
629 //      0, 0, prev_rotate_ref->get_w(), prev_rotate_ref->get_h(),
630 //      1, TRANSFER_NORMAL, //  CUBIC_LINEAR);
631         }
632 }
633
634
635 int MotionMain::process_buffer(VFrame **frame, int64_t start_position, double frame_rate)
636 {
637         int prev_config_tracking_type = config.tracking_type;
638         int need_reconfigure = load_configuration();
639         int color_model = frame[0]->get_color_model();
640         w = frame[0]->get_w();
641         h = frame[0]->get_h();
642
643 #ifdef DEBUG
644 printf("MotionMain::process_buffer %d start_position=%jd\n", __LINE__, start_position);
645 #endif
646
647 // Calculate the source and destination pointers for each of the operations.
648 // Get the layer to track motion in.
649 // Get the layer to apply motion in.
650         reference_layer = config.bottom_is_master ?
651                 PluginClient::total_in_buffers - 1 : 0;
652         target_layer = config.bottom_is_master ?
653                 0 : PluginClient::total_in_buffers - 1;
654
655         output_frame = frame[target_layer];
656 // Get the position of previous reference frame.
657         int64_t actual_previous_number;
658 // Skip if match frame not available
659         int skip_current = 0;
660
661         if( config.tracking_object == MotionScan::TRACK_SINGLE ) {
662                 actual_previous_number = config.track_frame;
663                 if( get_direction() == PLAY_REVERSE )
664                         actual_previous_number++;
665                 if( actual_previous_number == start_position )
666                         skip_current = 1;
667         }
668         else {
669                 actual_previous_number = start_position;
670                 if( get_direction() == PLAY_FORWARD ) {
671                         actual_previous_number--;
672                         if( actual_previous_number < get_source_start() )
673                                 skip_current = 1;
674                         else {
675                                 KeyFrame *keyframe = get_prev_keyframe(start_position, 1);
676                                 if( keyframe->position > 0 &&
677                                     actual_previous_number < keyframe->position )
678                                         skip_current = 1;
679                         }
680                 }
681                 else {
682                         actual_previous_number++;
683                         if( actual_previous_number >= get_source_start() + get_total_len() )
684                                 skip_current = 1;
685                         else {
686                                 KeyFrame *keyframe = get_next_keyframe(start_position, 1);
687                                 if( keyframe->position > 0 &&
688                                     actual_previous_number >= keyframe->position )
689                                         skip_current = 1;
690                         }
691                 }
692 // Only count motion since last keyframe
693         }
694
695         if( !config.global && !config.rotate )
696                 skip_current = 1;
697
698 //printf("process_realtime: %jd %d %jd %jd\n", start_position,
699 // skip_current, previous_frame_number, actual_previous_number);
700         if( prev_config_tracking_type != MotionScan::SAVE &&
701             config.tracking_type == MotionScan::SAVE ) {
702                 reset_cache_file();
703                 char save_file[BCTEXTLEN];
704                 snprintf(save_file, sizeof(save_file), "%s.bak", config.tracking_file);
705 #ifdef DEBUG
706 printf("MotionMain::process_buffer 2 rename tracking file: %s to %s\n",
707  config.tracking_file, save_file);
708 #endif
709                 ::rename(config.tracking_file, save_file);
710         }
711         else if( !cache_file[0] || active_key > start_position )
712                 reset_cache_file();
713
714 // Load match frame and reset vectors
715         int need_reload = !skip_current &&
716                 (previous_frame_number != actual_previous_number ||
717                 need_reconfigure);
718         if( need_reload ) {
719                 total_dx = total_dy = 0; total_angle = 0;
720                 previous_frame_number = actual_previous_number;
721         }
722
723         if( skip_current ) {
724                 total_dx = total_dy = 0;
725                 current_dx = current_dy = 0;
726                 total_angle = current_angle = 0;
727         }
728
729 // Get the global pointers.  Here we walk through the sequence of events.
730         if( config.global ) {
731 // Assume global only.  Global reads previous frame and compares
732 // with current frame to get the current translation.
733 // The center of the search area is fixed in compensate mode or
734 // the user value + the accumulation vector in track mode.
735                 if( !prev_global_ref )
736                         prev_global_ref = new VFrame(w, h, color_model, 0);
737                 if( !current_global_ref )
738                         current_global_ref = new VFrame(w, h, color_model, 0);
739
740 // Global loads the current target frame into the src and
741 // writes it to the dst frame with desired translation.
742                 if( !global_target_src )
743                         global_target_src = new VFrame(w, h, color_model, 0);
744                 if( !global_target_dst )
745                         global_target_dst = new VFrame(w, h, color_model, 0);
746
747 // Load the global frames
748                 if( need_reload ) {
749                         read_frame(prev_global_ref, reference_layer,
750                                 previous_frame_number, frame_rate, 0);
751                 }
752
753                 read_frame(current_global_ref, reference_layer,
754                         start_position, frame_rate, 0);
755                 read_frame(global_target_src, target_layer,
756                         start_position, frame_rate, 0);
757
758 // Global followed by rotate
759                 if( config.rotate ) {
760 // Must translate the previous global reference by the current global
761 // accumulation vector to match the current global reference.
762 // The center of the search area is always the user value + the accumulation
763 // vector.
764                         if( !prev_rotate_ref )
765                                 prev_rotate_ref = new VFrame(w, h, color_model, 0);
766 // The current global reference is the current rotation reference.
767                         if( !current_rotate_ref )
768                                 current_rotate_ref = new VFrame(w, h, color_model, 0);
769                         current_rotate_ref->copy_from(current_global_ref);
770
771 // The global target destination is copied to the rotation target source
772 // then written to the rotation output with rotation.
773 // The pivot for the rotation is the center of the search area
774 // if we're tracking.
775 // The pivot is fixed to the user position if we're compensating.
776                         if( !rotate_target_src )
777                                 rotate_target_src = new VFrame(w, h, color_model, 0);
778                         if( !rotate_target_dst )
779                                 rotate_target_dst = new VFrame(w, h, color_model, 0);
780                 }
781         }
782 // Rotation only
783         else if( config.rotate ) {
784 // Rotation reads the previous reference frame and compares it with current
785 // reference frame.
786                 if( !prev_rotate_ref )
787                         prev_rotate_ref = new VFrame(w, h, color_model, 0);
788                 if( !current_rotate_ref )
789                         current_rotate_ref = new VFrame(w, h, color_model, 0);
790
791 // Rotation loads target frame to temporary, rotates it, and writes it to the
792 // target frame.  The pivot is always fixed.
793                 if( !rotate_target_src )
794                         rotate_target_src = new VFrame(w, h, color_model, 0);
795                 if( !rotate_target_dst )
796                         rotate_target_dst = new VFrame(w, h, color_model, 0);
797
798
799 // Load the rotate frames
800                 if( need_reload ) {
801                         read_frame(prev_rotate_ref, reference_layer,
802                                 previous_frame_number, frame_rate, 0);
803                 }
804                 read_frame(current_rotate_ref, reference_layer,
805                         start_position, frame_rate, 0);
806                 read_frame(rotate_target_src, target_layer,
807                         start_position, frame_rate, 0);
808         }
809
810         dx_offset = 0; dy_offset = 0;
811         if( config.tracking_type == MotionScan::LOAD ) {
812                 if( config.addtrackedframeoffset ) {
813                         if( config.track_frame != tracking_frame ) {
814                                 tracking_frame = config.track_frame;
815                                 int64_t no;  int dx, dy;  float dt;
816                                 if( !get_cache_line(tracking_frame) &&
817                                     sscanf(cache_line, "%jd %d %d %f", &no, &dx, &dy, &dt) == 4 ) {
818                                         dx_offset = dx; dy_offset = dy;
819                                 }
820                                 else {
821                                         eprintf("no offset data frame %jd\n", tracking_frame);
822                                 }
823                         }
824                 }
825                 else
826                         tracking_frame = -1;
827         }
828
829         if( !skip_current ) {
830                 load_ok = 0;
831                 if( config.tracking_type == MotionScan::LOAD ||
832                     config.tracking_type == MotionScan::SAVE ) {
833                         int64_t no;  int dx, dy;  float dt;
834                         int64_t frame_no = get_source_position();
835 // Load result from disk
836                         if( !get_cache_line(frame_no) &&
837                             sscanf(cache_line, "%jd %d %d %f", &no, &dx, &dy, &dt) == 4 ) {
838                                 load_ok = 1;  load_dx = dx;  load_dy = dy;  load_dt = dt;
839                         }
840                         else {
841 #ifdef DEBUG
842 printf("MotionMain::process_buffer: no tracking data frame %jd\n", frame_no);
843 #endif
844                         }
845                 }
846
847 // Get position change from previous frame to current frame
848                 if( config.global )
849                         process_global();
850 // Get rotation change from previous frame to current frame
851                 if( config.rotate )
852                         process_rotation();
853 //frame[target_layer]->copy_from(prev_rotate_ref);
854 //frame[target_layer]->copy_from(current_rotate_ref);
855
856 // write results to disk
857                 if( config.tracking_type == MotionScan::SAVE ) {
858                         char line[BCSTRLEN];
859                         int64_t frame_no = get_source_position();
860                         snprintf(line, sizeof(line), "%jd %d %d %f\n",
861                                 frame_no, save_dx, save_dy, save_dt);
862                         put_cache_line(line);
863                 }
864 // Transfer the relevant target frame to the output
865                 if( config.rotate ) {
866                         frame[target_layer]->copy_from(rotate_target_dst);
867                 }
868                 else {
869                         frame[target_layer]->copy_from(global_target_dst);
870                 }
871         }
872 // Read the target destination directly
873         else {
874                 read_frame(frame[target_layer],
875                         target_layer, start_position, frame_rate, 0);
876         }
877
878         if( config.draw_vectors ) {
879                 draw_vectors(frame[target_layer]);
880         }
881
882 #ifdef DEBUG
883 printf("MotionMain::process_buffer %d\n", __LINE__);
884 #endif
885         return 0;
886 }
887
888
889
890 void MotionMain::draw_vectors(VFrame *frame)
891 {
892         int w = frame->get_w(), h = frame->get_h();
893         int global_x1, global_y1, global_x2, global_y2;
894         int block_x, block_y, block_w, block_h;
895         int block_x1, block_y1, block_x2, block_y2;
896         int block_x3, block_y3, block_x4, block_y4;
897         int search_x1, search_y1, search_x2, search_y2;
898         int search_w, search_h;
899
900
901         if( config.global ) {
902 // Get vector
903 // Start of vector is center of previous block.
904 // End of vector is total accumulation.
905                 if( config.tracking_object == MotionScan::TRACK_SINGLE ) {
906                         global_x1 = (int64_t)(config.block_x * w / 100);
907                         global_y1 = (int64_t)(config.block_y * h / 100);
908                         global_x2 = global_x1 + total_dx / OVERSAMPLE;
909                         global_y2 = global_y1 + total_dy / OVERSAMPLE;
910 //printf("MotionMain::draw_vectors %d %d %d %d %d %d\n", total_dx, total_dy, global_x1, global_y1, global_x2, global_y2);
911                 }
912 // Start of vector is center of previous block.
913 // End of vector is current change.
914                 else if( config.tracking_object == MotionScan::PREVIOUS_SAME_BLOCK ) {
915                         global_x1 = (int64_t)(config.block_x * w / 100);
916                         global_y1 = (int64_t)(config.block_y * h / 100);
917                         global_x2 = global_x1 + current_dx / OVERSAMPLE;
918                         global_y2 = global_y1 + current_dy / OVERSAMPLE;
919                 }
920                 else {
921                         global_x1 = (int64_t)(config.block_x * w / 100
922                                 + (total_dx - current_dx) / OVERSAMPLE);
923                         global_y1 = (int64_t)(config.block_y * h / 100
924                                 + (total_dy - current_dy) / OVERSAMPLE);
925                         global_x2 = (int64_t)(config.block_x * w / 100
926                                 + total_dx / OVERSAMPLE);
927                         global_y2 = (int64_t)(config.block_y * h / 100
928                                 + total_dy / OVERSAMPLE);
929                 }
930
931                 block_x = global_x1;
932                 block_y = global_y1;
933                 block_w = config.global_block_w * w / 100;
934                 block_h = config.global_block_h * h / 100;
935                 block_x1 = block_x - block_w / 2;
936                 block_y1 = block_y - block_h / 2;
937                 block_x2 = block_x + block_w / 2;
938                 block_y2 = block_y + block_h / 2;
939                 search_w = config.global_range_w * w / 100;
940                 search_h = config.global_range_h * h / 100;
941                 search_x1 = block_x1 - search_w / 2;
942                 search_y1 = block_y1 - search_h / 2;
943                 search_x2 = block_x2 + search_w / 2;
944                 search_y2 = block_y2 + search_h / 2;
945
946 //printf("MotionMain::draw_vectors %d %d %d %d %d %d %d %d %d %d %d %d\n",
947 // global_x1, global_y1, block_w, block_h, block_x1, block_y1,
948 // block_x2, block_y2, search_x1, search_y1, search_x2, search_y2);
949
950                 MotionScan::clamp_scan(w, h,
951                         &block_x1, &block_y1, &block_x2, &block_y2,
952                         &search_x1, &search_y1, &search_x2, &search_y2, 1);
953
954 // Vector
955                 draw_arrow(frame, global_x1, global_y1, global_x2, global_y2);
956
957 // Macroblock
958                 draw_line(frame, block_x1, block_y1, block_x2, block_y1);
959                 draw_line(frame, block_x2, block_y1, block_x2, block_y2);
960                 draw_line(frame, block_x2, block_y2, block_x1, block_y2);
961                 draw_line(frame, block_x1, block_y2, block_x1, block_y1);
962
963 // Search area
964                 draw_line(frame, search_x1, search_y1, search_x2, search_y1);
965                 draw_line(frame, search_x2, search_y1, search_x2, search_y2);
966                 draw_line(frame, search_x2, search_y2, search_x1, search_y2);
967                 draw_line(frame, search_x1, search_y2, search_x1, search_y1);
968
969 // Block should be endpoint of motion
970                 if( config.rotate ) {
971                         block_x = global_x2;
972                         block_y = global_y2;
973                 }
974         }
975         else {
976                 block_x = (int64_t)(config.block_x * w / 100);
977                 block_y = (int64_t)(config.block_y * h / 100);
978         }
979
980         block_w = config.global_block_w * w / 100;
981         block_h = config.global_block_h * h / 100;
982         if( config.rotate ) {
983                 float angle = total_angle * 2 * M_PI / 360;
984                 double base_angle1 = atan((float)block_h / block_w);
985                 double base_angle2 = atan((float)block_w / block_h);
986                 double target_angle1 = base_angle1 + angle;
987                 double target_angle2 = base_angle2 + angle;
988                 double radius = sqrt(block_w * block_w + block_h * block_h) / 2;
989                 block_x1 = (int)(block_x - cos(target_angle1) * radius);
990                 block_y1 = (int)(block_y - sin(target_angle1) * radius);
991                 block_x2 = (int)(block_x + sin(target_angle2) * radius);
992                 block_y2 = (int)(block_y - cos(target_angle2) * radius);
993                 block_x3 = (int)(block_x - sin(target_angle2) * radius);
994                 block_y3 = (int)(block_y + cos(target_angle2) * radius);
995                 block_x4 = (int)(block_x + cos(target_angle1) * radius);
996                 block_y4 = (int)(block_y + sin(target_angle1) * radius);
997
998                 draw_line(frame, block_x1, block_y1, block_x2, block_y2);
999                 draw_line(frame, block_x2, block_y2, block_x4, block_y4);
1000                 draw_line(frame, block_x4, block_y4, block_x3, block_y3);
1001                 draw_line(frame, block_x3, block_y3, block_x1, block_y1);
1002
1003
1004 // Center
1005                 if( !config.global ) {
1006                         draw_line(frame, block_x, block_y - 5, block_x, block_y + 6);
1007                         draw_line(frame, block_x - 5, block_y, block_x + 6, block_y);
1008                 }
1009         }
1010 }
1011
1012 MotionVVFrame::MotionVVFrame(VFrame *vfrm, int n)
1013  : VFrame(vfrm->get_data(), -1, vfrm->get_y()-vfrm->get_data(),
1014         vfrm->get_u()-vfrm->get_data(), vfrm->get_v()-vfrm->get_data(),
1015         vfrm->get_w(), vfrm->get_h(), vfrm->get_color_model(),
1016         vfrm->get_bytes_per_line())
1017 {
1018         this->n = n;
1019 }
1020
1021 int MotionVVFrame::draw_pixel(int x, int y)
1022 {
1023         VFrame::draw_pixel(x+0, y+0);
1024         for( int i=1; i<n; ++i ) {
1025                 VFrame::draw_pixel(x-i, y-i);
1026                 VFrame::draw_pixel(x+i, y+i);
1027         }
1028         return 0;
1029 }
1030
1031 void MotionMain::draw_line(VFrame *frame, int x1, int y1, int x2, int y2)
1032 {
1033         int iw = frame->get_w(), ih = frame->get_h();
1034         int mx = iw > ih ? iw : ih;
1035         int n = mx/800 + 1;
1036         MotionVVFrame vfrm(frame, n);
1037         vfrm.set_pixel_color(WHITE);
1038         int m = 2;  while( m < n ) m <<= 1;
1039         vfrm.set_stiple(2*m);
1040         vfrm.draw_line(x1,y1, x2,y2);
1041 }
1042
1043 #define ARROW_SIZE 10
1044 void MotionMain::draw_arrow(VFrame *frame, int x1, int y1, int x2, int y2)
1045 {
1046         double angle = atan((float)(y2 - y1) / (float)(x2 - x1));
1047         double angle1 = angle + (float)145 / 360 * 2 * 3.14159265;
1048         double angle2 = angle - (float)145 / 360 * 2 * 3.14159265;
1049         int x3, y3, x4, y4;
1050         if( x2 < x1 ) {
1051                 x3 = x2 - (int)(ARROW_SIZE * cos(angle1));
1052                 y3 = y2 - (int)(ARROW_SIZE * sin(angle1));
1053                 x4 = x2 - (int)(ARROW_SIZE * cos(angle2));
1054                 y4 = y2 - (int)(ARROW_SIZE * sin(angle2));
1055         }
1056         else {
1057                 x3 = x2 + (int)(ARROW_SIZE * cos(angle1));
1058                 y3 = y2 + (int)(ARROW_SIZE * sin(angle1));
1059                 x4 = x2 + (int)(ARROW_SIZE * cos(angle2));
1060                 y4 = y2 + (int)(ARROW_SIZE * sin(angle2));
1061         }
1062
1063 // Main vector
1064         draw_line(frame, x1, y1, x2, y2);
1065 //      draw_line(frame, x1, y1 + 1, x2, y2 + 1);
1066
1067 // Arrow line
1068         if( abs(y2 - y1) || abs(x2 - x1) ) draw_line(frame, x2, y2, x3, y3);
1069 //      draw_line(frame, x2, y2 + 1, x3, y3 + 1);
1070 // Arrow line
1071         if( abs(y2 - y1) || abs(x2 - x1) ) draw_line(frame, x2, y2, x4, y4);
1072 //      draw_line(frame, x2, y2 + 1, x4, y4 + 1);
1073 }
1074
1075 int MotionMain::open_cache_file()
1076 {
1077         if( cache_fp ) return 0;
1078         if( !cache_file[0] ) return 1;
1079         if( !(cache_fp = fopen(cache_file, "r")) ) return 1;
1080         return 0;
1081 }
1082
1083 void MotionMain::close_cache_file()
1084 {
1085         if( !cache_fp ) return;
1086         fclose(cache_fp);
1087         cache_fp = 0; cache_key = -1; tracking_frame = -1;
1088 }
1089
1090 int MotionMain::load_cache_line()
1091 {
1092         cache_key = -1;
1093         if( open_cache_file() ) return 1;
1094         if( !fgets(cache_line, sizeof(cache_line), cache_fp) ) return 1;
1095         cache_key = strtol(cache_line, 0, 0);
1096         return 0;
1097 }
1098
1099 int MotionMain::get_cache_line(int64_t key)
1100 {
1101         if( cache_key == key ) return 0;
1102         if( open_cache_file() ) return 1;
1103         if( cache_key >= 0 && key > cache_key ) {
1104                 if( load_cache_line() ) return 1;
1105                 if( cache_key == key ) return 0;
1106                 if( cache_key > key ) return 1;
1107         }
1108 // binary search file
1109         fseek(cache_fp, 0, SEEK_END);
1110         int64_t l = -1, r = ftell(cache_fp);
1111         while( (r - l) > 1 ) {
1112                 int64_t m = (l + r) / 2;
1113                 fseek(cache_fp, m, SEEK_SET);
1114                 if( m > 0 && !fgets(cache_line, sizeof(cache_line), cache_fp) )
1115                         return -1;
1116                 if( !load_cache_line() ) {
1117                         if( cache_key == key )
1118                                 return 0;
1119                         if( cache_key < key ) { l = m; continue; }
1120                 }
1121                 r = m;
1122         }
1123         return 1;
1124 }
1125
1126 int MotionMain::locate_cache_line(int64_t key)
1127 {
1128         int ret = 1;
1129         if( key < 0 || !(ret=get_cache_line(key)) ||
1130             ( cache_key >= 0 && cache_key < key ) )
1131                 ret = load_cache_line();
1132         return ret;
1133 }
1134
1135 int MotionMain::put_cache_line(const char *line)
1136 {
1137         int64_t key = strtol(line, 0, 0);
1138         if( key == active_key ) return 1;
1139         if( !active_fp ) {
1140                 close_cache_file();
1141                 snprintf(cache_file, sizeof(cache_file), "%s.bak", config.tracking_file);
1142                 ::rename(config.tracking_file, cache_file);
1143                 if( !(active_fp = fopen(config.tracking_file, "w")) ) {
1144                         perror(config.tracking_file);
1145                         fprintf(stderr, "err writing key %jd\n", key);
1146                         return -1;
1147                 }
1148                 active_key = -1;
1149         }
1150
1151         if( active_key < key ) {
1152                 locate_cache_line(active_key);
1153                 while( cache_key >= 0 && key >= cache_key ) {
1154                         if( key > cache_key )
1155                                 fputs(cache_line, active_fp);
1156                         load_cache_line();
1157                 }
1158         }
1159
1160         active_key = key;
1161         fputs(line, active_fp);
1162         fflush(active_fp);
1163         return 0;
1164 }
1165
1166 void MotionMain::reset_cache_file()
1167 {
1168         if( active_fp ) {
1169                 locate_cache_line(active_key);
1170                 while( cache_key >= 0 ) {
1171                         fputs(cache_line, active_fp);
1172                         load_cache_line();
1173                 }
1174                 close_cache_file();  ::remove(cache_file);
1175                 fclose(active_fp); active_fp = 0; active_key = -1;
1176         }
1177         else
1178                 close_cache_file();
1179         strcpy(cache_file, config.tracking_file);
1180 }
1181
1182
1183 RotateScanPackage::RotateScanPackage()
1184 {
1185 }
1186
1187 RotateScanUnit::RotateScanUnit(RotateScan *server, MotionMain *plugin)
1188  : LoadClient(server)
1189 {
1190         this->server = server;
1191         this->plugin = plugin;
1192         rotater = 0;
1193         temp = 0;
1194 }
1195
1196 RotateScanUnit::~RotateScanUnit()
1197 {
1198         delete rotater;
1199         delete temp;
1200 }
1201
1202 void RotateScanUnit::process_package(LoadPackage *package)
1203 {
1204         if( server->skip ) return;
1205         RotateScanPackage *pkg = (RotateScanPackage*)package;
1206
1207         if( (pkg->difference = server->get_cache(pkg->angle)) < 0 ) {
1208 //printf("RotateScanUnit::process_package %d\n", __LINE__);
1209                 int color_model = server->previous_frame->get_color_model();
1210                 int pixel_size = BC_CModels::calculate_pixelsize(color_model);
1211                 int row_bytes = server->previous_frame->get_bytes_per_line();
1212
1213                 if( !rotater )
1214                         rotater = new AffineEngine(1, 1);
1215                 if( !temp )
1216                         temp = new VFrame(
1217                                 server->previous_frame->get_w(),
1218                                 server->previous_frame->get_h(),
1219                                 color_model, 0);
1220 //printf("RotateScanUnit::process_package %d\n", __LINE__);
1221
1222
1223 // Rotate original block size
1224 //              rotater->set_viewport(server->block_x1, server->block_y1,
1225 //                      server->block_x2 - server->block_x1, server->block_y2 - server->block_y1);
1226                 rotater->set_in_viewport(server->block_x1, server->block_y1,
1227                         server->block_x2 - server->block_x1, server->block_y2 - server->block_y1);
1228                 rotater->set_out_viewport(server->block_x1, server->block_y1,
1229                         server->block_x2 - server->block_x1, server->block_y2 - server->block_y1);
1230 //              rotater->set_pivot(server->block_x, server->block_y);
1231                 rotater->set_in_pivot(server->block_x, server->block_y);
1232                 rotater->set_out_pivot(server->block_x, server->block_y);
1233 //printf("RotateScanUnit::process_package %d\n", __LINE__);
1234                 rotater->rotate(temp, server->previous_frame, pkg->angle);
1235
1236 // Scan reduced block size
1237 //plugin->output_frame->copy_from(server->current_frame);
1238 //plugin->output_frame->copy_from(temp);
1239 //printf("RotateScanUnit::process_package %d %d %d %d %d\n",
1240 // __LINE__, server->scan_x, server->scan_y, server->scan_w, server->scan_h);
1241 // Clamp coordinates
1242                 int x1 = server->scan_x;
1243                 int y1 = server->scan_y;
1244                 int x2 = x1 + server->scan_w;
1245                 int y2 = y1 + server->scan_h;
1246                 x2 = MIN(temp->get_w(), x2);
1247                 y2 = MIN(temp->get_h(), y2);
1248                 x2 = MIN(server->current_frame->get_w(), x2);
1249                 y2 = MIN(server->current_frame->get_h(), y2);
1250                 x1 = MAX(0, x1);  y1 = MAX(0, y1);
1251
1252                 if( x2 > x1 && y2 > y1 ) {
1253                         pkg->difference = MotionScan::abs_diff(
1254                                 temp->get_rows()[y1] + x1 * pixel_size,
1255                                 server->current_frame->get_rows()[y1] + x1 * pixel_size,
1256                                 row_bytes, x2 - x1, y2 - y1, color_model);
1257 //printf("RotateScanUnit::process_package %d\n", __LINE__);
1258                         server->put_cache(pkg->angle, pkg->difference);
1259                 }
1260 #if 0
1261         VFrame png(x2-x1, y2-y1, BC_RGB888, -1);
1262         png.transfer_from(temp, 0, x1, y1, x2-x1, y2-y1);
1263         char fn[64];
1264         sprintf(fn,"%s%f.png","/tmp/temp",pkg->angle); png.write_png(fn);
1265         png.transfer_from(server->current_frame, 0, x1, y1, x2-x1, y2-y1);
1266         sprintf(fn,"%s%f.png","/tmp/curr",pkg->angle); png.write_png(fn);
1267 printf("RotateScanUnit::process_package 10 x=%d y=%d w=%d h=%d block_x=%d block_y=%d angle=%f scan_w=%d scan_h=%d diff=%jd\n",
1268  server->block_x1, server->block_y1, server->block_x2 - server->block_x1, server->block_y2 - server->block_y1,
1269  server->block_x,  server->block_y,  pkg->angle,  server->scan_w, server->scan_h, pkg->difference);
1270 #endif
1271         }
1272 }
1273
1274
1275 RotateScan::RotateScan(MotionMain *plugin,
1276         int total_clients,
1277         int total_packages)
1278  : LoadServer( //1, 1)
1279                 total_clients, total_packages)
1280 {
1281         this->plugin = plugin;
1282         cache_lock = new Mutex("RotateScan::cache_lock");
1283 }
1284
1285
1286 RotateScan::~RotateScan()
1287 {
1288         delete cache_lock;
1289 }
1290
1291 void RotateScan::init_packages()
1292 {
1293         for( int i = 0; i < get_total_packages(); i++ ) {
1294                 RotateScanPackage *pkg = (RotateScanPackage*)get_package(i);
1295                 pkg->angle = scan_angle1 +
1296                         i * (scan_angle2 - scan_angle1) / (total_steps - 1);
1297         }
1298 }
1299
1300 LoadClient* RotateScan::new_client()
1301 {
1302         return new RotateScanUnit(this, plugin);
1303 }
1304
1305 LoadPackage* RotateScan::new_package()
1306 {
1307         return new RotateScanPackage;
1308 }
1309
1310
1311 float RotateScan::scan_frame(VFrame *previous_frame, VFrame *current_frame,
1312         int block_x, int block_y)
1313 {
1314         skip = 0;
1315         this->block_x = block_x;
1316         this->block_y = block_y;
1317
1318 //printf("RotateScan::scan_frame %d\n", __LINE__);
1319         switch(plugin->config.tracking_type) {
1320         case MotionScan::NO_CALCULATE:
1321                 result = plugin->config.rotation_center;
1322                 skip = 1;
1323                 break;
1324
1325         case MotionScan::LOAD:
1326         case MotionScan::SAVE:
1327                 if( plugin->load_ok ) {
1328                         result = plugin->load_dt;
1329                         skip = 1;
1330                 }
1331                 break;
1332         }
1333
1334         this->previous_frame = previous_frame;
1335         this->current_frame = current_frame;
1336         int w = current_frame->get_w();
1337         int h = current_frame->get_h();
1338         int block_w = w * plugin->config.global_block_w / 100;
1339         int block_h = h * plugin->config.global_block_h / 100;
1340
1341         if( this->block_x - block_w / 2 < 0 ) block_w = this->block_x * 2;
1342         if( this->block_y - block_h / 2 < 0 ) block_h = this->block_y * 2;
1343         if( this->block_x + block_w / 2 > w ) block_w = (w - this->block_x) * 2;
1344         if( this->block_y + block_h / 2 > h ) block_h = (h - this->block_y) * 2;
1345
1346         block_x1 = this->block_x - block_w / 2;
1347         block_x2 = this->block_x + block_w / 2;
1348         block_y1 = this->block_y - block_h / 2;
1349         block_y2 = this->block_y + block_h / 2;
1350
1351 // Calculate the maximum area available to scan after rotation.
1352 // Must be calculated from the starting range because of cache.
1353 // Get coords of rectangle after rotation.
1354         double center_x = this->block_x;
1355         double center_y = this->block_y;
1356         double max_angle = plugin->config.rotation_range;
1357         double base_angle1 = atan((float)block_h / block_w);
1358         double base_angle2 = atan((float)block_w / block_h);
1359         double target_angle1 = base_angle1 + max_angle * 2 * M_PI / 360;
1360         double target_angle2 = base_angle2 + max_angle * 2 * M_PI / 360;
1361         double radius = sqrt(block_w * block_w + block_h * block_h) / 2;
1362         double x1 = center_x - cos(target_angle1) * radius;
1363         double y1 = center_y - sin(target_angle1) * radius;
1364         double x2 = center_x + sin(target_angle2) * radius;
1365         double y2 = center_y - cos(target_angle2) * radius;
1366         double x3 = center_x - sin(target_angle2) * radius;
1367         double y3 = center_y + cos(target_angle2) * radius;
1368
1369 // Track top edge to find greatest area.
1370         double max_area1 = 0;
1371         //double max_x1 = 0;
1372         double max_y1 = 0;
1373         for( double x = x1; x < x2; x++ ) {
1374                 double y = y1 + (y2 - y1) * (x - x1) / (x2 - x1);
1375                 if( x >= center_x && x < block_x2 && y >= block_y1 && y < center_y ) {
1376                         double area = fabs(x - center_x) * fabs(y - center_y);
1377                         if( area > max_area1 ) {
1378                                 max_area1 = area;
1379                                 //max_x1 = x;
1380                                 max_y1 = y;
1381                         }
1382                 }
1383         }
1384
1385 // Track left edge to find greatest area.
1386         double max_area2 = 0;
1387         double max_x2 = 0;
1388         //double max_y2 = 0;
1389         for( double y = y1; y < y3; y++ ) {
1390                 double x = x1 + (x3 - x1) * (y - y1) / (y3 - y1);
1391                 if( x >= block_x1 && x < center_x && y >= block_y1 && y < center_y ) {
1392                         double area = fabs(x - center_x) * fabs(y - center_y);
1393                         if( area > max_area2 ) {
1394                                 max_area2 = area;
1395                                 max_x2 = x;
1396                                 //max_y2 = y;
1397                         }
1398                 }
1399         }
1400
1401         double max_x, max_y;
1402         max_x = max_x2;
1403         max_y = max_y1;
1404
1405 // Get reduced scan coords
1406         scan_w = (int)(fabs(max_x - center_x) * 2);
1407         scan_h = (int)(fabs(max_y - center_y) * 2);
1408         scan_x = (int)(center_x - scan_w / 2);
1409         scan_y = (int)(center_y - scan_h / 2);
1410 // printf("RotateScan::scan_frame center=%d,%d scan=%d,%d %dx%d\n",
1411 // this->block_x, this->block_y, scan_x, scan_y, scan_w, scan_h);
1412 // printf("    angle_range=%f block= %d,%d,%d,%d\n", max_angle, block_x1, block_y1, block_x2, block_y2);
1413
1414 // Determine min angle from size of block
1415         double angle1 = atan((double)block_h / block_w);
1416         double angle2 = atan((double)(block_h - 1) / (block_w + 1));
1417         double min_angle = fabs(angle2 - angle1) / OVERSAMPLE;
1418         min_angle = MAX(min_angle, MIN_ANGLE);
1419
1420 //printf("RotateScan::scan_frame %d min_angle=%f\n", __LINE__, min_angle * 360 / 2 / M_PI);
1421
1422         cache.remove_all_objects();
1423
1424
1425         if( !skip ) {
1426                 if( previous_frame->data_matches(current_frame) ) {
1427 //printf("RotateScan::scan_frame: frames match.  Skipping.\n");
1428                         result = plugin->config.rotation_center;
1429                         skip = 1;
1430                 }
1431         }
1432
1433         if( !skip ) {
1434 // Initial search range
1435                 float angle_range = max_angle;
1436                 result = plugin->config.rotation_center;
1437                 total_steps = plugin->config.rotate_positions;
1438
1439
1440                 while( angle_range >= min_angle * total_steps ) {
1441                         scan_angle1 = result - angle_range;
1442                         scan_angle2 = result + angle_range;
1443
1444                         set_package_count(total_steps);
1445 //set_package_count(1);
1446                         process_packages();
1447
1448                         int64_t min_difference = -1;
1449                         for( int i = 0; i < get_total_packages(); i++ ) {
1450                                 RotateScanPackage *pkg = (RotateScanPackage*)get_package(i);
1451                                 if( pkg->difference < min_difference || min_difference == -1 ) {
1452                                         min_difference = pkg->difference;
1453                                         result = pkg->angle;
1454                                 }
1455 //break;
1456                         }
1457
1458                         angle_range /= 2;
1459
1460 //break;
1461                 }
1462         }
1463
1464         if( plugin->config.tracking_type == MotionScan::SAVE ) {
1465                 plugin->save_dt = result;
1466         }
1467 //printf("RotateScan::scan_frame %d angle=%f\n", __LINE__, result);
1468         return result;
1469 }
1470
1471 int64_t RotateScan::get_cache(float angle)
1472 {
1473         int64_t result = -1;
1474         cache_lock->lock("RotateScan::get_cache");
1475         for( int i = 0; i < cache.total; i++ ) {
1476                 RotateScanCache *ptr = cache.values[i];
1477                 if( fabs(ptr->angle - angle) <= MIN_ANGLE ) {
1478                         result = ptr->difference;
1479                         break;
1480                 }
1481         }
1482         cache_lock->unlock();
1483         return result;
1484 }
1485
1486 void RotateScan::put_cache(float angle, int64_t difference)
1487 {
1488         RotateScanCache *ptr = new RotateScanCache(angle, difference);
1489         cache_lock->lock("RotateScan::put_cache");
1490         cache.append(ptr);
1491         cache_lock->unlock();
1492 }
1493
1494
1495 RotateScanCache::RotateScanCache(float angle, int64_t difference)
1496 {
1497         this->angle = angle;
1498         this->difference = difference;
1499 }
1500
1501
1502