4661b144ebd9e7dcae0c89fb8dc2b08d9257756d
[goodguy/history.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 _("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->get_data(), MESSAGESIZE);
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->get_data(), strlen(keyframe->get_data()));
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
1013 void MotionMain::draw_pixel(VFrame *frame, int x, int y)
1014 {
1015         if( !(x >= 0 && y >= 0 && x < frame->get_w() && y < frame->get_h()) ) return;
1016
1017 #define DRAW_PIXEL(model, x, y, components, do_yuv, max, type) \
1018  case model: { \
1019         type **rows = (type**)frame->get_rows(); \
1020         rows[y][x * components] = max - rows[y][x * components]; \
1021         if( !do_yuv ) { \
1022                 rows[y][x * components + 1] = max - rows[y][x * components + 1]; \
1023                 rows[y][x * components + 2] = max - rows[y][x * components + 2]; \
1024         } \
1025         else { \
1026                 rows[y][x * components + 1] = (max / 2 + 1) - rows[y][x * components + 1]; \
1027                 rows[y][x * components + 2] = (max / 2 + 1) - rows[y][x * components + 2]; \
1028         } \
1029         if( components == 4 ) \
1030                 rows[y][x * components + 3] = max; \
1031 } break
1032
1033         switch(frame->get_color_model()) {
1034         DRAW_PIXEL(BC_RGB888, x, y, 3, 0, 0xff, unsigned char);
1035         DRAW_PIXEL(BC_RGBA8888, x, y, 4, 0, 0xff, unsigned char);
1036         DRAW_PIXEL(BC_RGB_FLOAT, x, y, 3, 0, 1.0, float);
1037         DRAW_PIXEL(BC_RGBA_FLOAT, x, y, 4, 0, 1.0, float);
1038         DRAW_PIXEL(BC_YUV888, x, y, 3, 1, 0xff, unsigned char);
1039         DRAW_PIXEL(BC_YUVA8888, x, y, 4, 1, 0xff, unsigned char);
1040         DRAW_PIXEL(BC_RGB161616, x, y, 3, 0, 0xffff, uint16_t);
1041         DRAW_PIXEL(BC_YUV161616, x, y, 3, 1, 0xffff, uint16_t);
1042         DRAW_PIXEL(BC_RGBA16161616, x, y, 4, 0, 0xffff, uint16_t);
1043         DRAW_PIXEL(BC_YUVA16161616, x, y, 4, 1, 0xffff, uint16_t);
1044         }
1045 }
1046
1047
1048 void MotionMain::draw_line(VFrame *frame, int x1, int y1, int x2, int y2)
1049 {
1050         int w = labs(x2 - x1);
1051         int h = labs(y2 - y1);
1052 //printf("MotionMain::draw_line 1 %d %d %d %d\n", x1, y1, x2, y2);
1053
1054         if( !w && !h ) {
1055                 draw_pixel(frame, x1, y1);
1056         }
1057         else if( w > h ) {
1058 // Flip coordinates so x1 < x2
1059                 if( x2 < x1 ) {
1060                         y2 ^= y1; y1 ^= y2; y2 ^= y1;
1061                         x1 ^= x2; x2 ^= x1; x1 ^= x2;
1062                 }
1063                 int numerator = y2 - y1;
1064                 int denominator = x2 - x1;
1065                 for( int i = x1; i < x2; i++ ) {
1066                         int y = y1 + (int64_t)(i - x1) * (int64_t)numerator / (int64_t)denominator;
1067                         draw_pixel(frame, i, y);
1068                 }
1069         }
1070         else {
1071 // Flip coordinates so y1 < y2
1072                 if( y2 < y1 ) {
1073                         y2 ^= y1; y1 ^= y2; y2 ^= y1;
1074                         x1 ^= x2; x2 ^= x1; x1 ^= x2;
1075                 }
1076                 int numerator = x2 - x1;
1077                 int denominator = y2 - y1;
1078                 for( int i = y1; i < y2; i++ ) {
1079                         int x = x1 + (int64_t)(i - y1) * (int64_t)numerator / (int64_t)denominator;
1080                         draw_pixel(frame, x, i);
1081                 }
1082         }
1083 //printf("MotionMain::draw_line 2\n");
1084 }
1085
1086 #define ARROW_SIZE 10
1087 void MotionMain::draw_arrow(VFrame *frame, int x1, int y1, int x2, int y2)
1088 {
1089         double angle = atan((float)(y2 - y1) / (float)(x2 - x1));
1090         double angle1 = angle + (float)145 / 360 * 2 * 3.14159265;
1091         double angle2 = angle - (float)145 / 360 * 2 * 3.14159265;
1092         int x3, y3, x4, y4;
1093         if( x2 < x1 ) {
1094                 x3 = x2 - (int)(ARROW_SIZE * cos(angle1));
1095                 y3 = y2 - (int)(ARROW_SIZE * sin(angle1));
1096                 x4 = x2 - (int)(ARROW_SIZE * cos(angle2));
1097                 y4 = y2 - (int)(ARROW_SIZE * sin(angle2));
1098         }
1099         else {
1100                 x3 = x2 + (int)(ARROW_SIZE * cos(angle1));
1101                 y3 = y2 + (int)(ARROW_SIZE * sin(angle1));
1102                 x4 = x2 + (int)(ARROW_SIZE * cos(angle2));
1103                 y4 = y2 + (int)(ARROW_SIZE * sin(angle2));
1104         }
1105
1106 // Main vector
1107         draw_line(frame, x1, y1, x2, y2);
1108 //      draw_line(frame, x1, y1 + 1, x2, y2 + 1);
1109
1110 // Arrow line
1111         if( abs(y2 - y1) || abs(x2 - x1) ) draw_line(frame, x2, y2, x3, y3);
1112 //      draw_line(frame, x2, y2 + 1, x3, y3 + 1);
1113 // Arrow line
1114         if( abs(y2 - y1) || abs(x2 - x1) ) draw_line(frame, x2, y2, x4, y4);
1115 //      draw_line(frame, x2, y2 + 1, x4, y4 + 1);
1116 }
1117
1118 int MotionMain::open_cache_file()
1119 {
1120         if( cache_fp ) return 0;
1121         if( !cache_file[0] ) return 1;
1122         if( !(cache_fp = fopen(cache_file, "r")) ) return 1;
1123         return 0;
1124 }
1125
1126 void MotionMain::close_cache_file()
1127 {
1128         if( !cache_fp ) return;
1129         fclose(cache_fp);
1130         cache_fp = 0; cache_key = -1; tracking_frame = -1;
1131 }
1132
1133 int MotionMain::load_cache_line()
1134 {
1135         cache_key = -1;
1136         if( open_cache_file() ) return 1;
1137         if( !fgets(cache_line, sizeof(cache_line), cache_fp) ) return 1;
1138         cache_key = strtol(cache_line, 0, 0);
1139         return 0;
1140 }
1141
1142 int MotionMain::get_cache_line(int64_t key)
1143 {
1144         if( cache_key == key ) return 0;
1145         if( open_cache_file() ) return 1;
1146         if( cache_key >= 0 && key > cache_key ) {
1147                 if( load_cache_line() ) return 1;
1148                 if( cache_key == key ) return 0;
1149                 if( cache_key > key ) return 1;
1150         }
1151 // binary search file
1152         fseek(cache_fp, 0, SEEK_END);
1153         int64_t l = -1, r = ftell(cache_fp);
1154         while( (r - l) > 1 ) {
1155                 int64_t m = (l + r) / 2;
1156                 fseek(cache_fp, m, SEEK_SET);
1157                 if( m > 0 && !fgets(cache_line, sizeof(cache_line), cache_fp) )
1158                         return -1;
1159                 if( !load_cache_line() ) {
1160                         if( cache_key == key )
1161                                 return 0;
1162                         if( cache_key < key ) { l = m; continue; }
1163                 }
1164                 r = m;
1165         }
1166         return 1;
1167 }
1168
1169 int MotionMain::locate_cache_line(int64_t key)
1170 {
1171         int ret = 1;
1172         if( key < 0 || !(ret=get_cache_line(key)) ||
1173             ( cache_key >= 0 && cache_key < key ) )
1174                 ret = load_cache_line();
1175         return ret;
1176 }
1177
1178 int MotionMain::put_cache_line(const char *line)
1179 {
1180         int64_t key = strtol(line, 0, 0);
1181         if( key == active_key ) return 1;
1182         if( !active_fp ) {
1183                 close_cache_file();
1184                 snprintf(cache_file, sizeof(cache_file), "%s.bak", config.tracking_file);
1185                 ::rename(config.tracking_file, cache_file);
1186                 if( !(active_fp = fopen(config.tracking_file, "w")) ) {
1187                         perror(config.tracking_file);
1188                         fprintf(stderr, "err writing key %jd\n", key);
1189                         return -1;
1190                 }
1191                 active_key = -1;
1192         }
1193
1194         if( active_key < key ) {
1195                 locate_cache_line(active_key);
1196                 while( cache_key >= 0 && key >= cache_key ) {
1197                         if( key > cache_key )
1198                                 fputs(cache_line, active_fp);
1199                         load_cache_line();
1200                 }
1201         }
1202
1203         active_key = key;
1204         fputs(line, active_fp);
1205         fflush(active_fp);
1206         return 0;
1207 }
1208
1209 void MotionMain::reset_cache_file()
1210 {
1211         if( active_fp ) {
1212                 locate_cache_line(active_key);
1213                 while( cache_key >= 0 ) {
1214                         fputs(cache_line, active_fp);
1215                         load_cache_line();
1216                 }
1217                 close_cache_file();  ::remove(cache_file);
1218                 fclose(active_fp); active_fp = 0; active_key = -1;
1219         }
1220         else
1221                 close_cache_file();
1222         strcpy(cache_file, config.tracking_file);
1223 }
1224
1225
1226 RotateScanPackage::RotateScanPackage()
1227 {
1228 }
1229
1230 RotateScanUnit::RotateScanUnit(RotateScan *server, MotionMain *plugin)
1231  : LoadClient(server)
1232 {
1233         this->server = server;
1234         this->plugin = plugin;
1235         rotater = 0;
1236         temp = 0;
1237 }
1238
1239 RotateScanUnit::~RotateScanUnit()
1240 {
1241         delete rotater;
1242         delete temp;
1243 }
1244
1245 void RotateScanUnit::process_package(LoadPackage *package)
1246 {
1247         if( server->skip ) return;
1248         RotateScanPackage *pkg = (RotateScanPackage*)package;
1249
1250         if( (pkg->difference = server->get_cache(pkg->angle)) < 0 ) {
1251 //printf("RotateScanUnit::process_package %d\n", __LINE__);
1252                 int color_model = server->previous_frame->get_color_model();
1253                 int pixel_size = BC_CModels::calculate_pixelsize(color_model);
1254                 int row_bytes = server->previous_frame->get_bytes_per_line();
1255
1256                 if( !rotater )
1257                         rotater = new AffineEngine(1, 1);
1258                 if( !temp )
1259                         temp = new VFrame(
1260                                 server->previous_frame->get_w(),
1261                                 server->previous_frame->get_h(),
1262                                 color_model, 0);
1263 //printf("RotateScanUnit::process_package %d\n", __LINE__);
1264
1265
1266 // Rotate original block size
1267 //              rotater->set_viewport(server->block_x1, server->block_y1,
1268 //                      server->block_x2 - server->block_x1, server->block_y2 - server->block_y1);
1269                 rotater->set_in_viewport(server->block_x1, server->block_y1,
1270                         server->block_x2 - server->block_x1, server->block_y2 - server->block_y1);
1271                 rotater->set_out_viewport(server->block_x1, server->block_y1,
1272                         server->block_x2 - server->block_x1, server->block_y2 - server->block_y1);
1273 //              rotater->set_pivot(server->block_x, server->block_y);
1274                 rotater->set_in_pivot(server->block_x, server->block_y);
1275                 rotater->set_out_pivot(server->block_x, server->block_y);
1276 //printf("RotateScanUnit::process_package %d\n", __LINE__);
1277                 rotater->rotate(temp, server->previous_frame, pkg->angle);
1278
1279 // Scan reduced block size
1280 //plugin->output_frame->copy_from(server->current_frame);
1281 //plugin->output_frame->copy_from(temp);
1282 //printf("RotateScanUnit::process_package %d %d %d %d %d\n",
1283 // __LINE__, server->scan_x, server->scan_y, server->scan_w, server->scan_h);
1284 // Clamp coordinates
1285                 int x1 = server->scan_x;
1286                 int y1 = server->scan_y;
1287                 int x2 = x1 + server->scan_w;
1288                 int y2 = y1 + server->scan_h;
1289                 x2 = MIN(temp->get_w(), x2);
1290                 y2 = MIN(temp->get_h(), y2);
1291                 x2 = MIN(server->current_frame->get_w(), x2);
1292                 y2 = MIN(server->current_frame->get_h(), y2);
1293                 x1 = MAX(0, x1);  y1 = MAX(0, y1);
1294
1295                 if( x2 > x1 && y2 > y1 ) {
1296                         pkg->difference = MotionScan::abs_diff(
1297                                 temp->get_rows()[y1] + x1 * pixel_size,
1298                                 server->current_frame->get_rows()[y1] + x1 * pixel_size,
1299                                 row_bytes, x2 - x1, y2 - y1, color_model);
1300 //printf("RotateScanUnit::process_package %d\n", __LINE__);
1301                         server->put_cache(pkg->angle, pkg->difference);
1302                 }
1303 #if 0
1304         VFrame png(x2-x1, y2-y1, BC_RGB888, -1);
1305         png.transfer_from(temp, 0, x1, y1, x2-x1, y2-y1);
1306         char fn[64];
1307         sprintf(fn,"%s%f.png","/tmp/temp",pkg->angle); png.write_png(fn);
1308         png.transfer_from(server->current_frame, 0, x1, y1, x2-x1, y2-y1);
1309         sprintf(fn,"%s%f.png","/tmp/curr",pkg->angle); png.write_png(fn);
1310 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",
1311  server->block_x1, server->block_y1, server->block_x2 - server->block_x1, server->block_y2 - server->block_y1,
1312  server->block_x,  server->block_y,  pkg->angle,  server->scan_w, server->scan_h, pkg->difference);
1313 #endif
1314         }
1315 }
1316
1317
1318 RotateScan::RotateScan(MotionMain *plugin,
1319         int total_clients,
1320         int total_packages)
1321  : LoadServer( //1, 1)
1322                 total_clients, total_packages)
1323 {
1324         this->plugin = plugin;
1325         cache_lock = new Mutex("RotateScan::cache_lock");
1326 }
1327
1328
1329 RotateScan::~RotateScan()
1330 {
1331         delete cache_lock;
1332 }
1333
1334 void RotateScan::init_packages()
1335 {
1336         for( int i = 0; i < get_total_packages(); i++ ) {
1337                 RotateScanPackage *pkg = (RotateScanPackage*)get_package(i);
1338                 pkg->angle = scan_angle1 +
1339                         i * (scan_angle2 - scan_angle1) / (total_steps - 1);
1340         }
1341 }
1342
1343 LoadClient* RotateScan::new_client()
1344 {
1345         return new RotateScanUnit(this, plugin);
1346 }
1347
1348 LoadPackage* RotateScan::new_package()
1349 {
1350         return new RotateScanPackage;
1351 }
1352
1353
1354 float RotateScan::scan_frame(VFrame *previous_frame, VFrame *current_frame,
1355         int block_x, int block_y)
1356 {
1357         skip = 0;
1358         this->block_x = block_x;
1359         this->block_y = block_y;
1360
1361 //printf("RotateScan::scan_frame %d\n", __LINE__);
1362         switch(plugin->config.tracking_type) {
1363         case MotionScan::NO_CALCULATE:
1364                 result = plugin->config.rotation_center;
1365                 skip = 1;
1366                 break;
1367
1368         case MotionScan::LOAD:
1369         case MotionScan::SAVE:
1370                 if( plugin->load_ok ) {
1371                         result = plugin->load_dt;
1372                         skip = 1;
1373                 }
1374                 break;
1375         }
1376
1377         this->previous_frame = previous_frame;
1378         this->current_frame = current_frame;
1379         int w = current_frame->get_w();
1380         int h = current_frame->get_h();
1381         int block_w = w * plugin->config.global_block_w / 100;
1382         int block_h = h * plugin->config.global_block_h / 100;
1383
1384         if( this->block_x - block_w / 2 < 0 ) block_w = this->block_x * 2;
1385         if( this->block_y - block_h / 2 < 0 ) block_h = this->block_y * 2;
1386         if( this->block_x + block_w / 2 > w ) block_w = (w - this->block_x) * 2;
1387         if( this->block_y + block_h / 2 > h ) block_h = (h - this->block_y) * 2;
1388
1389         block_x1 = this->block_x - block_w / 2;
1390         block_x2 = this->block_x + block_w / 2;
1391         block_y1 = this->block_y - block_h / 2;
1392         block_y2 = this->block_y + block_h / 2;
1393
1394 // Calculate the maximum area available to scan after rotation.
1395 // Must be calculated from the starting range because of cache.
1396 // Get coords of rectangle after rotation.
1397         double center_x = this->block_x;
1398         double center_y = this->block_y;
1399         double max_angle = plugin->config.rotation_range;
1400         double base_angle1 = atan((float)block_h / block_w);
1401         double base_angle2 = atan((float)block_w / block_h);
1402         double target_angle1 = base_angle1 + max_angle * 2 * M_PI / 360;
1403         double target_angle2 = base_angle2 + max_angle * 2 * M_PI / 360;
1404         double radius = sqrt(block_w * block_w + block_h * block_h) / 2;
1405         double x1 = center_x - cos(target_angle1) * radius;
1406         double y1 = center_y - sin(target_angle1) * radius;
1407         double x2 = center_x + sin(target_angle2) * radius;
1408         double y2 = center_y - cos(target_angle2) * radius;
1409         double x3 = center_x - sin(target_angle2) * radius;
1410         double y3 = center_y + cos(target_angle2) * radius;
1411
1412 // Track top edge to find greatest area.
1413         double max_area1 = 0;
1414         //double max_x1 = 0;
1415         double max_y1 = 0;
1416         for( double x = x1; x < x2; x++ ) {
1417                 double y = y1 + (y2 - y1) * (x - x1) / (x2 - x1);
1418                 if( x >= center_x && x < block_x2 && y >= block_y1 && y < center_y ) {
1419                         double area = fabs(x - center_x) * fabs(y - center_y);
1420                         if( area > max_area1 ) {
1421                                 max_area1 = area;
1422                                 //max_x1 = x;
1423                                 max_y1 = y;
1424                         }
1425                 }
1426         }
1427
1428 // Track left edge to find greatest area.
1429         double max_area2 = 0;
1430         double max_x2 = 0;
1431         //double max_y2 = 0;
1432         for( double y = y1; y < y3; y++ ) {
1433                 double x = x1 + (x3 - x1) * (y - y1) / (y3 - y1);
1434                 if( x >= block_x1 && x < center_x && y >= block_y1 && y < center_y ) {
1435                         double area = fabs(x - center_x) * fabs(y - center_y);
1436                         if( area > max_area2 ) {
1437                                 max_area2 = area;
1438                                 max_x2 = x;
1439                                 //max_y2 = y;
1440                         }
1441                 }
1442         }
1443
1444         double max_x, max_y;
1445         max_x = max_x2;
1446         max_y = max_y1;
1447
1448 // Get reduced scan coords
1449         scan_w = (int)(fabs(max_x - center_x) * 2);
1450         scan_h = (int)(fabs(max_y - center_y) * 2);
1451         scan_x = (int)(center_x - scan_w / 2);
1452         scan_y = (int)(center_y - scan_h / 2);
1453 // printf("RotateScan::scan_frame center=%d,%d scan=%d,%d %dx%d\n",
1454 // this->block_x, this->block_y, scan_x, scan_y, scan_w, scan_h);
1455 // printf("    angle_range=%f block= %d,%d,%d,%d\n", max_angle, block_x1, block_y1, block_x2, block_y2);
1456
1457 // Determine min angle from size of block
1458         double angle1 = atan((double)block_h / block_w);
1459         double angle2 = atan((double)(block_h - 1) / (block_w + 1));
1460         double min_angle = fabs(angle2 - angle1) / OVERSAMPLE;
1461         min_angle = MAX(min_angle, MIN_ANGLE);
1462
1463 //printf("RotateScan::scan_frame %d min_angle=%f\n", __LINE__, min_angle * 360 / 2 / M_PI);
1464
1465         cache.remove_all_objects();
1466
1467
1468         if( !skip ) {
1469                 if( previous_frame->data_matches(current_frame) ) {
1470 //printf("RotateScan::scan_frame: frames match.  Skipping.\n");
1471                         result = plugin->config.rotation_center;
1472                         skip = 1;
1473                 }
1474         }
1475
1476         if( !skip ) {
1477 // Initial search range
1478                 float angle_range = max_angle;
1479                 result = plugin->config.rotation_center;
1480                 total_steps = plugin->config.rotate_positions;
1481
1482
1483                 while( angle_range >= min_angle * total_steps ) {
1484                         scan_angle1 = result - angle_range;
1485                         scan_angle2 = result + angle_range;
1486
1487                         set_package_count(total_steps);
1488 //set_package_count(1);
1489                         process_packages();
1490
1491                         int64_t min_difference = -1;
1492                         for( int i = 0; i < get_total_packages(); i++ ) {
1493                                 RotateScanPackage *pkg = (RotateScanPackage*)get_package(i);
1494                                 if( pkg->difference < min_difference || min_difference == -1 ) {
1495                                         min_difference = pkg->difference;
1496                                         result = pkg->angle;
1497                                 }
1498 //break;
1499                         }
1500
1501                         angle_range /= 2;
1502
1503 //break;
1504                 }
1505         }
1506
1507         if( plugin->config.tracking_type == MotionScan::SAVE ) {
1508                 plugin->save_dt = result;
1509         }
1510 //printf("RotateScan::scan_frame %d angle=%f\n", __LINE__, result);
1511         return result;
1512 }
1513
1514 int64_t RotateScan::get_cache(float angle)
1515 {
1516         int64_t result = -1;
1517         cache_lock->lock("RotateScan::get_cache");
1518         for( int i = 0; i < cache.total; i++ ) {
1519                 RotateScanCache *ptr = cache.values[i];
1520                 if( fabs(ptr->angle - angle) <= MIN_ANGLE ) {
1521                         result = ptr->difference;
1522                         break;
1523                 }
1524         }
1525         cache_lock->unlock();
1526         return result;
1527 }
1528
1529 void RotateScan::put_cache(float angle, int64_t difference)
1530 {
1531         RotateScanCache *ptr = new RotateScanCache(angle, difference);
1532         cache_lock->lock("RotateScan::put_cache");
1533         cache.append(ptr);
1534         cache_lock->unlock();
1535 }
1536
1537
1538 RotateScanCache::RotateScanCache(float angle, int64_t difference)
1539 {
1540         this->angle = angle;
1541         this->difference = difference;
1542 }
1543
1544
1545