Fourth set of 50 GPL attribution for CV-Contributors added +
[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  * Copyright (C) 2003-2016 Cinelerra CV contributors
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  */
22
23 #include "affine.h"
24 #include "bcdisplayinfo.h"
25 #include "clip.h"
26 #include "bchash.h"
27 #include "bcsignals.h"
28 #include "filexml.h"
29 #include "keyframe.h"
30 #include "language.h"
31 #include "mainerror.h"
32 #include "motion.h"
33 #include "motionscan.h"
34 #include "motionwindow.h"
35 #include "mutex.h"
36 #include "overlayframe.h"
37 #include "rotateframe.h"
38 #include "transportque.h"
39
40
41 #include <errno.h>
42 #include <unistd.h>
43
44 REGISTER_PLUGIN(MotionMain)
45
46
47 //#define DEBUG
48
49 MotionConfig::MotionConfig()
50 {
51         global_range_w = 25; //5;
52         global_range_h = 25; //5;
53         rotation_range = 8; //5;
54         rotation_center = 0;
55         block_count = 1;
56         global_block_w = 33; //MIN_BLOCK;
57         global_block_h = 33; //MIN_BLOCK;
58         block_x = 50;
59         block_y = 50;
60         noise_level = 0;
61         noise_rotation = 0;
62         global_positions = 256;
63         rotate_positions = 8; // 4;
64         magnitude = 100;
65         rotate_magnitude = 30;
66         return_speed = 5; //0;
67         rotate_return_speed = 5; //0;
68         action_type = MotionScan::STABILIZE;
69         global = 1;
70         rotate = 1;
71         twopass = 0;
72         addtrackedframeoffset = 0;
73         strcpy(tracking_file, TRACKING_FILE);
74         tracking_type = MotionScan::SAVE; //MotionScan::NO_CALCULATE;
75         tracking_object = MotionScan::TRACK_PREVIOUS; //TRACK_SINGLE;
76         draw_vectors = 1; //0;
77         track_frame = 0;
78         bottom_is_master = 1;
79         horizontal_only = 0;
80         vertical_only = 0;
81 }
82
83
84 void MotionConfig::boundaries()
85 {
86         CLAMP(global_range_w, MIN_RADIUS, MAX_RADIUS);
87         CLAMP(global_range_h, MIN_RADIUS, MAX_RADIUS);
88         CLAMP(rotation_range, MIN_ROTATION, MAX_ROTATION);
89         CLAMP(rotation_center, -MAX_ROTATION, MAX_ROTATION);
90         CLAMP(block_count, MIN_BLOCKS, MAX_BLOCKS);
91         CLAMP(global_block_w, MIN_BLOCK, MAX_BLOCK);
92         CLAMP(global_block_h, MIN_BLOCK, MAX_BLOCK);
93 }
94
95 int MotionConfig::equivalent(MotionConfig &that)
96 {
97         return global_range_w == that.global_range_w &&
98                 global_range_h == that.global_range_h &&
99                 rotation_range == that.rotation_range &&
100                 rotation_center == that.rotation_center &&
101                 action_type == that.action_type &&
102                 global == that.global && rotate == that.rotate &&
103                 twopass == that.twopass &&
104                 addtrackedframeoffset == that.addtrackedframeoffset &&
105                 draw_vectors == that.draw_vectors &&
106                 block_count == that.block_count &&
107                 global_block_w == that.global_block_w &&
108                 global_block_h == that.global_block_h &&
109                 EQUIV(block_x, that.block_x) &&
110                 EQUIV(block_y, that.block_y) &&
111                 noise_level == that.noise_level &&
112                 noise_rotation == that.noise_rotation &&
113                 global_positions == that.global_positions &&
114                 rotate_positions == that.rotate_positions &&
115                 magnitude == that.magnitude &&
116                 return_speed == that.return_speed &&
117                 rotate_return_speed == that.rotate_return_speed &&
118                 rotate_magnitude == that.rotate_magnitude &&
119                 tracking_object == that.tracking_object &&
120                 track_frame == that.track_frame &&
121                 bottom_is_master == that.bottom_is_master &&
122                 horizontal_only == that.horizontal_only &&
123                 vertical_only == that.vertical_only;
124 }
125
126 void MotionConfig::copy_from(MotionConfig &that)
127 {
128         global_range_w = that.global_range_w;
129         global_range_h = that.global_range_h;
130         rotation_range = that.rotation_range;
131         rotation_center = that.rotation_center;
132         action_type = that.action_type;
133         global = that.global;
134         rotate = that.rotate;
135         twopass = that.twopass;
136         addtrackedframeoffset = that.addtrackedframeoffset;
137         tracking_type = that.tracking_type;
138         draw_vectors = that.draw_vectors;
139         block_count = that.block_count;
140         block_x = that.block_x;
141         block_y = that.block_y;
142         noise_level = that.noise_level;
143         noise_rotation = that.noise_rotation;
144         global_positions = that.global_positions;
145         rotate_positions = that.rotate_positions;
146         global_block_w = that.global_block_w;
147         global_block_h = that.global_block_h;
148         magnitude = that.magnitude;
149         return_speed = that.return_speed;
150         rotate_magnitude = that.rotate_magnitude;
151         rotate_return_speed = that.rotate_return_speed;
152         tracking_object = that.tracking_object;
153         track_frame = that.track_frame;
154         bottom_is_master = that.bottom_is_master;
155         horizontal_only = that.horizontal_only;
156         vertical_only = that.vertical_only;
157 }
158
159 void MotionConfig::interpolate(MotionConfig &prev, MotionConfig &next,
160         int64_t prev_frame, int64_t next_frame, int64_t current_frame)
161 {
162         copy_from(prev);
163 }
164
165
166 MotionMain::MotionMain(PluginServer *server)
167  : PluginVClient(server)
168 {
169         engine = 0;
170         rotate_engine = 0;
171         motion_rotate = 0;
172         total_dx = 0;
173         total_dy = 0;
174         total_angle = 0;
175         overlayer = 0;
176         search_area = 0;
177         search_size = 0;
178         temp_frame = 0;
179         previous_frame_number = -1;
180
181         prev_global_ref = 0;
182         current_global_ref = 0;
183         global_target_src = 0;
184         global_target_dst = 0;
185
186         cache_file[0] = 0;
187         cache_fp = active_fp = 0;
188         cache_line[0] = 0;
189         cache_key = active_key = -1;
190         dx_offset = dy_offset = 0;
191         dt_offset = 0;
192         load_ok = 0;
193         save_dx = load_dx = 0;
194         save_dy = load_dy = 0;
195         save_dt = load_dt = 0;
196         tracking_frame = -1;
197         prev_rotate_ref = 0;
198         current_rotate_ref = 0;
199         rotate_target_src = 0;
200         rotate_target_dst = 0;
201 }
202
203 MotionMain::~MotionMain()
204 {
205
206         delete engine;
207         delete overlayer;
208         delete [] search_area;
209         delete temp_frame;
210         delete rotate_engine;
211         delete motion_rotate;
212
213         delete prev_global_ref;
214         delete current_global_ref;
215         delete global_target_src;
216         delete global_target_dst;
217
218         reset_cache_file();
219
220         delete prev_rotate_ref;
221         delete current_rotate_ref;
222         delete rotate_target_src;
223         delete rotate_target_dst;
224 }
225
226 const char* MotionMain::plugin_title() { return N_("Motion"); }
227 int MotionMain::is_realtime() { return 1; }
228 int MotionMain::is_multichannel() { return 1; }
229
230
231 NEW_WINDOW_MACRO(MotionMain, MotionWindow)
232
233 LOAD_CONFIGURATION_MACRO(MotionMain, MotionConfig)
234
235
236
237 void MotionMain::update_gui()
238 {
239         if( !thread ) return;
240         if( !load_configuration() ) return;
241         thread->window->lock_window("MotionMain::update_gui");
242         MotionWindow *window = (MotionWindow*)thread->window;
243
244         char string[BCTEXTLEN];
245         sprintf(string, "%d", config.global_positions);
246         window->global_search_positions->set_text(string);
247         sprintf(string, "%d", config.rotate_positions);
248         window->rotation_search_positions->set_text(string);
249
250         window->global_block_w->update(config.global_block_w);
251         window->global_block_h->update(config.global_block_h);
252         window->block_x->update(config.block_x);
253         window->block_y->update(config.block_y);
254         window->block_x_text->update((float)config.block_x);
255         window->block_y_text->update((float)config.block_y);
256         window->noise_level->update(config.noise_level);
257         window->noise_level_text->update((float)config.noise_level);
258         window->noise_rotation->update(config.noise_rotation);
259         window->noise_rotation_text->update((float)config.noise_rotation);
260         window->magnitude->update(config.magnitude);
261         window->return_speed->update(config.return_speed);
262         window->rotate_magnitude->update(config.rotate_magnitude);
263         window->rotate_return_speed->update(config.rotate_return_speed);
264         window->rotation_range->update(config.rotation_range);
265         window->rotation_center->update(config.rotation_center);
266
267
268         window->track_single->update(config.tracking_object == MotionScan::TRACK_SINGLE);
269         window->track_frame_number->update(config.track_frame);
270         window->track_previous->update(config.tracking_object == MotionScan::TRACK_PREVIOUS);
271         window->previous_same->update(config.tracking_object == MotionScan::PREVIOUS_SAME_BLOCK);
272         if( config.tracking_object != MotionScan::TRACK_SINGLE )
273         {
274                 window->track_frame_number->disable();
275                 window->frame_current->disable();
276         }
277         else
278         {
279                 window->track_frame_number->enable();
280                 window->frame_current->enable();
281         }
282
283         window->action_type->set_text(
284                 ActionType::to_text(config.action_type));
285         window->tracking_type->set_text(
286                 TrackingType::to_text(config.tracking_type));
287         window->track_direction->set_text(
288                 TrackDirection::to_text(config.horizontal_only, config.vertical_only));
289         window->master_layer->set_text(
290                 MasterLayer::to_text(config.bottom_is_master));
291
292         window->update_mode();
293         thread->window->unlock_window();
294 }
295
296
297
298
299 void MotionMain::save_data(KeyFrame *keyframe)
300 {
301         FileXML output;
302
303 // cause data to be stored directly in text
304         output.set_shared_output(keyframe->xbuf);
305         output.tag.set_title("MOTION");
306
307         output.tag.set_property("BLOCK_COUNT", config.block_count);
308         output.tag.set_property("GLOBAL_POSITIONS", config.global_positions);
309         output.tag.set_property("ROTATE_POSITIONS", config.rotate_positions);
310         output.tag.set_property("GLOBAL_BLOCK_W", config.global_block_w);
311         output.tag.set_property("GLOBAL_BLOCK_H", config.global_block_h);
312         output.tag.set_property("BLOCK_X", config.block_x);
313         output.tag.set_property("BLOCK_Y", config.block_y);
314         output.tag.set_property("GLOBAL_RANGE_W", config.global_range_w);
315         output.tag.set_property("GLOBAL_RANGE_H", config.global_range_h);
316         output.tag.set_property("ROTATION_RANGE", config.rotation_range);
317         output.tag.set_property("ROTATION_CENTER", config.rotation_center);
318         output.tag.set_property("MAGNITUDE", config.magnitude);
319         output.tag.set_property("RETURN_SPEED", config.return_speed);
320         output.tag.set_property("ROTATE_MAGNITUDE", config.rotate_magnitude);
321         output.tag.set_property("ROTATE_RETURN_SPEED", config.rotate_return_speed);
322         output.tag.set_property("NOISE_LEVEL", config.noise_level);
323         output.tag.set_property("NOISE_ROTATION", config.noise_rotation);
324         output.tag.set_property("ACTION_TYPE", config.action_type);
325         output.tag.set_property("GLOBAL", config.global);
326         output.tag.set_property("ROTATE", config.rotate);
327         output.tag.set_property("TWOPASS", config.twopass);
328         output.tag.set_property("ADDTRACKEDFRAMEOFFSET", config.addtrackedframeoffset);
329         output.tag.set_property("TRACKING_FILE", config.tracking_file);
330         output.tag.set_property("TRACKING_TYPE", config.tracking_type);
331         output.tag.set_property("DRAW_VECTORS", config.draw_vectors);
332         output.tag.set_property("TRACKING_OBJECT", config.tracking_object);
333         output.tag.set_property("TRACK_FRAME", config.track_frame);
334         output.tag.set_property("BOTTOM_IS_MASTER", config.bottom_is_master);
335         output.tag.set_property("HORIZONTAL_ONLY", config.horizontal_only);
336         output.tag.set_property("VERTICAL_ONLY", config.vertical_only);
337         output.append_tag();
338         output.tag.set_title("/MOTION");
339         output.append_tag();
340         output.terminate_string();
341 }
342
343 void MotionMain::read_data(KeyFrame *keyframe)
344 {
345         FileXML input;
346         input.set_shared_input(keyframe->xbuf);
347         int result = 0;
348
349         while( !(result = input.read_tag()) ) {
350                 if( input.tag.title_is("MOTION") ) {
351                         config.block_count = input.tag.get_property("BLOCK_COUNT", config.block_count);
352                         config.global_positions = input.tag.get_property("GLOBAL_POSITIONS", config.global_positions);
353                         config.rotate_positions = input.tag.get_property("ROTATE_POSITIONS", config.rotate_positions);
354                         config.global_block_w = input.tag.get_property("GLOBAL_BLOCK_W", config.global_block_w);
355                         config.global_block_h = input.tag.get_property("GLOBAL_BLOCK_H", config.global_block_h);
356                         config.block_x = input.tag.get_property("BLOCK_X", config.block_x);
357                         config.block_y = input.tag.get_property("BLOCK_Y", config.block_y);
358                         config.global_range_w = input.tag.get_property("GLOBAL_RANGE_W", config.global_range_w);
359                         config.global_range_h = input.tag.get_property("GLOBAL_RANGE_H", config.global_range_h);
360                         config.rotation_range = input.tag.get_property("ROTATION_RANGE", config.rotation_range);
361                         config.rotation_center = input.tag.get_property("ROTATION_CENTER", config.rotation_center);
362                         config.magnitude = input.tag.get_property("MAGNITUDE", config.magnitude);
363                         config.return_speed = input.tag.get_property("RETURN_SPEED", config.return_speed);
364                         config.rotate_magnitude = input.tag.get_property("ROTATE_MAGNITUDE", config.rotate_magnitude);
365                         config.rotate_return_speed = input.tag.get_property("ROTATE_RETURN_SPEED", config.rotate_return_speed);
366                         config.noise_level = input.tag.get_property("NOISE_LEVEL", config.noise_level);
367                         config.noise_rotation = input.tag.get_property("NOISE_ROTATION", config.noise_rotation);
368                         config.action_type = input.tag.get_property("ACTION_TYPE", config.action_type);
369                         config.global = input.tag.get_property("GLOBAL", config.global);
370                         config.rotate = input.tag.get_property("ROTATE", config.rotate);
371                         config.twopass = input.tag.get_property("TWOPASS", config.twopass);
372                         config.addtrackedframeoffset = input.tag.get_property("ADDTRACKEDFRAMEOFFSET", config.addtrackedframeoffset);
373                         input.tag.get_property("TRACKING_FILE", config.tracking_file);
374                         config.tracking_type = input.tag.get_property("TRACKING_TYPE", config.tracking_type);
375                         config.draw_vectors = input.tag.get_property("DRAW_VECTORS", config.draw_vectors);
376                         config.tracking_object = input.tag.get_property("TRACKING_OBJECT", config.tracking_object);
377                         config.track_frame = input.tag.get_property("TRACK_FRAME", config.track_frame);
378                         config.bottom_is_master = input.tag.get_property("BOTTOM_IS_MASTER", config.bottom_is_master);
379                         config.horizontal_only = input.tag.get_property("HORIZONTAL_ONLY", config.horizontal_only);
380                         config.vertical_only = input.tag.get_property("VERTICAL_ONLY", config.vertical_only);
381                 }
382         }
383         config.boundaries();
384 }
385
386 void MotionMain::allocate_temp(int w, int h, int color_model)
387 {
388         if( temp_frame &&
389             ( temp_frame->get_w() != w || temp_frame->get_h() != h ) ) {
390                 delete temp_frame;
391                 temp_frame = 0;
392         }
393         if( !temp_frame )
394                 temp_frame = new VFrame(w, h, color_model, 0);
395 }
396
397 void MotionMain::process_global()
398 {
399
400         if( !engine ) engine = new MotionScan(this,
401                 PluginClient::get_project_smp() + 1,
402                 PluginClient::get_project_smp() + 1);
403
404 // Determine if frames changed, either single pass or pass 1
405 // Attention, prev_global_ref and current_global_ref are interchanged
406         engine->scan_frame(current_global_ref, prev_global_ref,
407                 config.global_range_w, config.global_range_h,
408                 config.global_block_w, config.global_block_h,
409                 config.block_x, config.block_y,
410                 config.tracking_object, config.tracking_type,
411                 config.action_type, config.horizontal_only,
412                 config.vertical_only, get_source_position(),
413                 config.global_positions, total_dx, total_dy,
414                 0, 0, config.twopass, load_ok, load_dx, load_dy);
415         current_dx = (engine->dx_result += dx_offset);
416         current_dy = (engine->dy_result += dy_offset);
417
418 // Save results
419         if( config.tracking_type == MotionScan::SAVE ) {
420                 save_dx = current_dx;
421                 save_dy = current_dy;
422         }
423
424 // Add current motion vector to accumulation vector.
425         if( config.tracking_object != MotionScan::TRACK_SINGLE ) {
426 // Retract over time
427                 total_dx = (int64_t)total_dx * (100 - config.return_speed) / 100;
428                 total_dy = (int64_t)total_dy * (100 - config.return_speed) / 100;
429                 total_dx += engine->dx_result;
430                 total_dy += engine->dy_result;
431 // printf("MotionMain::process_global total_dx=%d engine->dx_result=%d\n",
432 // total_dx, engine->dx_result);
433         }
434         else {
435 // Make accumulation vector current
436                 total_dx = engine->dx_result;
437                 total_dy = engine->dy_result;
438         }
439
440 // Clamp accumulation vector
441         if( config.magnitude < 100 ) {
442                 int block_x_orig = lrint(config.block_x * prev_global_ref->get_w() / 100);
443                 int block_y_orig = lrint(config.block_y * prev_global_ref->get_h() / 100);
444                 int max_block_x = (int64_t)(prev_global_ref->get_w() - block_x_orig)
445                         * OVERSAMPLE * config.magnitude / 100;
446                 int max_block_y = (int64_t)(prev_global_ref->get_h() - block_y_orig)
447                         * OVERSAMPLE * config.magnitude / 100;
448                 int min_block_x = (int64_t)-block_x_orig
449                         * OVERSAMPLE * config.magnitude / 100;
450                 int min_block_y = (int64_t)-block_y_orig
451                         * OVERSAMPLE * config.magnitude / 100;
452
453                 CLAMP(total_dx, min_block_x, max_block_x);
454                 CLAMP(total_dy, min_block_y, max_block_y);
455         }
456
457 #ifdef DEBUG
458 printf("MotionMain::process_global 2 total_dx=%.02f total_dy=%.02f\n",
459   (float)total_dx / OVERSAMPLE, (float)total_dy / OVERSAMPLE);
460 #endif
461
462 // If there will be 2nd pass, target will be transformed then
463         if(!config.twopass || load_ok)
464         {
465                 if( config.tracking_object != MotionScan::TRACK_SINGLE && !config.rotate ) {
466 // Transfer current reference frame to previous reference frame and update
467 // counter.  Must wait for rotate to compare.
468                         prev_global_ref->copy_from(current_global_ref);
469                         previous_frame_number = get_source_position();
470                 }
471
472 // No 2nd pass, decide here what to do with target based on requested operation
473                 int interpolation = NEAREST_NEIGHBOR;
474                 float dx = 0., dy = 0.;
475                 switch(config.action_type) {
476                 case MotionScan::NOTHING:
477                         global_target_dst->copy_from(global_target_src);
478                         break;
479                 case MotionScan::TRACK_PIXEL:
480                         interpolation = NEAREST_NEIGHBOR;
481                         dx = rint((float)total_dx / OVERSAMPLE);
482                         dy = rint((float)total_dy / OVERSAMPLE);
483                         break;
484                 case MotionScan::STABILIZE_PIXEL:
485                         interpolation = NEAREST_NEIGHBOR;
486                         dx = -rint((float)total_dx / OVERSAMPLE);
487                         dy = -rint((float)total_dy / OVERSAMPLE);
488                         break;
489                 case MotionScan::TRACK:
490                         interpolation = CUBIC_LINEAR;
491                         dx = (float)total_dx / OVERSAMPLE;
492                         dy = (float)total_dy / OVERSAMPLE;
493                         break;
494                 case MotionScan::STABILIZE:
495                         interpolation = CUBIC_LINEAR;
496                         dx = -(float)total_dx / OVERSAMPLE;
497                         dy = -(float)total_dy / OVERSAMPLE;
498                         break;
499                 }
500
501
502                 if( config.action_type != MotionScan::NOTHING ) {
503                         if( !overlayer )
504                                 overlayer = new OverlayFrame(PluginClient::get_project_smp() + 1);
505                         global_target_dst->clear_frame();
506                         overlayer->overlay(global_target_dst, global_target_src,
507                                 0, 0, global_target_src->get_w(), global_target_src->get_h(),
508                                 dx, dy,
509                                 (float)global_target_src->get_w() + dx,
510                                 (float)global_target_src->get_h() + dy,
511                                 1, TRANSFER_REPLACE, interpolation);
512                 }
513         }
514 }
515
516 void MotionMain::refine_global()
517 {
518         int block_x, block_y;
519         double tmp_x, tmp_y;
520         float dx = 0., dy = 0.;
521
522 // Use temp_frame instead of current_global_ref for refined translation search
523         allocate_temp(w, h, current_global_ref->get_color_model());
524         temp_frame->clear_frame();
525
526         if(config.rotate)
527         {
528 // Here we have to rotate current_rotate_ref into temp_frame
529 // backwards because prev_global_ref and current_global_ref are interchanged
530                 if(!rotate_engine)
531                         rotate_engine = new AffineEngine(
532                                 PluginClient::get_project_smp() + 1,
533                                 PluginClient::get_project_smp() + 1);
534
535                 float angle;
536                 if(config.tracking_object == MotionScan::TRACK_SINGLE)
537                 {
538                         angle = total_angle;
539                 }
540                 else
541                 {
542                         angle = current_angle;
543                 }
544
545 // Pivot need not be very accurate, it is attached to the previous frame
546 // while current frame is moved and rotated
547 // Nevertheless compute it in floating point and round to int afterwards
548                 tmp_x = current_rotate_ref->get_w() * config.block_x / 100;
549                 tmp_y = current_rotate_ref->get_h() * config.block_y / 100;
550                 if(config.tracking_object == MotionScan::TRACK_PREVIOUS)
551                 {
552 // Pivot is moved along the previous frame
553                         tmp_x += (double)(total_dx-current_dx) / OVERSAMPLE;
554                         tmp_y += (double)(total_dy-current_dy) / OVERSAMPLE;
555                 }
556                 block_x = lrint (tmp_x);
557                 block_y = lrint (tmp_y);
558                 rotate_engine->set_in_pivot(block_x, block_y);
559                 rotate_engine->set_out_pivot(block_x, block_y);
560
561                 rotate_engine->rotate(temp_frame, current_rotate_ref, -angle);
562         }
563         else
564         {
565 // Here we have to translate current_global_ref into temp_frame
566 // backwards because prev_global_ref and current_global_ref are interchanged
567                 if(!overlayer) 
568                         overlayer = new OverlayFrame(PluginClient::get_project_smp() + 1);
569                 if(config.tracking_object == MotionScan::TRACK_SINGLE)
570                 {
571                         dx = (float)total_dx / OVERSAMPLE;
572                         dy = (float)total_dy / OVERSAMPLE;
573                 }
574                 else
575                 {
576                         dx = (float)current_dx / OVERSAMPLE;
577                         dy = (float)current_dy / OVERSAMPLE;
578                 }
579
580                 overlayer->overlay(temp_frame,
581                         current_global_ref,
582                         0,
583                         0,
584                         current_global_ref->get_w(),
585                         current_global_ref->get_h(),
586                         -dx,
587                         -dy,
588                         (float)current_global_ref->get_w() - dx,
589                         (float)current_global_ref->get_h() - dy,
590                         1,
591                         TRANSFER_REPLACE,
592                         CUBIC_LINEAR);
593         }
594
595 // Determine additional translation, pass 2
596 // Attention, prev_global_ref and current_global_ref are interchanged
597 // Engine must have been created already by process_global()
598         engine->scan_frame(temp_frame, prev_global_ref,
599                 config.global_range_w, config.global_range_h,
600                 config.global_block_w, config.global_block_h,
601                 config.block_x, config.block_y,
602                 config.tracking_object, config.tracking_type,
603                 config.action_type, config.horizontal_only,
604                 config.vertical_only, get_source_position(),
605                 config.global_positions, total_dx, total_dy,
606                 0, 0, 2, load_ok, load_dx, load_dy);
607
608 // Translation correction is to be added to motion vector
609         current_dx += engine->dx_result;
610         current_dy += engine->dy_result;
611         total_dx   += engine->dx_result;
612         total_dy   += engine->dy_result;
613
614 // Refine saved results
615         if( config.tracking_type == MotionScan::SAVE ) {
616                 save_dx = current_dx;
617                 save_dy = current_dy;
618         }
619
620 // Clamp accumulation vector
621         if( config.magnitude < 100 ) {
622                 int block_x_orig = lrint(config.block_x * prev_global_ref->get_w() / 100);
623                 int block_y_orig = lrint(config.block_y * prev_global_ref->get_h() / 100);
624                 int max_block_x = (int64_t)(prev_global_ref->get_w() - block_x_orig)
625                         * OVERSAMPLE * config.magnitude / 100;
626                 int max_block_y = (int64_t)(prev_global_ref->get_h() - block_y_orig)
627                         * OVERSAMPLE * config.magnitude / 100;
628                 int min_block_x = (int64_t)-block_x_orig
629                         * OVERSAMPLE * config.magnitude / 100;
630                 int min_block_y = (int64_t)-block_y_orig
631                         * OVERSAMPLE * config.magnitude / 100;
632
633                 CLAMP(total_dx, min_block_x, max_block_x);
634                 CLAMP(total_dy, min_block_y, max_block_y);
635         }
636
637 #ifdef DEBUG
638 printf("MotionMain::refine_global 2 total_dx=%.02f total_dy=%.02f\n",
639   (float)total_dx / OVERSAMPLE, (float)total_dy / OVERSAMPLE);
640 #endif
641
642         if( config.tracking_object != MotionScan::TRACK_SINGLE && !config.rotate ) {
643 // Transfer current reference frame to previous reference frame and update
644 // counter.  Must wait for rotate to compare.
645                 prev_global_ref->copy_from(current_global_ref);
646                 previous_frame_number = get_source_position();
647         }
648
649 // Decide what to do with target based on requested operation
650         int interpolation = NEAREST_NEIGHBOR;
651         dx = dy = 0.;
652         switch(config.action_type) {
653         case MotionScan::NOTHING:
654                 global_target_dst->copy_from(global_target_src);
655                 break;
656         case MotionScan::TRACK_PIXEL:
657                 interpolation = NEAREST_NEIGHBOR;
658                 dx = rint((float)total_dx / OVERSAMPLE);
659                 dy = rint((float)total_dy / OVERSAMPLE);
660                 break;
661         case MotionScan::STABILIZE_PIXEL:
662                 interpolation = NEAREST_NEIGHBOR;
663                 dx = -rint((float)total_dx / OVERSAMPLE);
664                 dy = -rint((float)total_dy / OVERSAMPLE);
665                 break;
666         case MotionScan::TRACK:
667                 interpolation = CUBIC_LINEAR;
668                 dx = (float)total_dx / OVERSAMPLE;
669                 dy = (float)total_dy / OVERSAMPLE;
670                 break;
671         case MotionScan::STABILIZE:
672                 interpolation = CUBIC_LINEAR;
673                 dx = -(float)total_dx / OVERSAMPLE;
674                 dy = -(float)total_dy / OVERSAMPLE;
675                 break;
676         }
677
678
679         if( config.action_type != MotionScan::NOTHING ) {
680 // Should be already created elsewhere but try it here just for safety
681                 if( !overlayer )
682                         overlayer = new OverlayFrame(PluginClient::get_project_smp() + 1);
683                 global_target_dst->clear_frame();
684                 overlayer->overlay(global_target_dst, global_target_src,
685                         0, 0, global_target_src->get_w(), global_target_src->get_h(),
686                         dx, dy,
687                         (float)global_target_src->get_w() + dx,
688                         (float)global_target_src->get_h() + dy,
689                         1, TRANSFER_REPLACE, interpolation);
690         }
691 }
692
693
694
695 void MotionMain::process_rotation()
696 {
697         int block_x, block_y;
698         double tmp_x, tmp_y;
699
700 // Here we have to translate current_global_ref into current_rotate_ref
701 // backwards because prev_rotate_ref and current_rotate_ref are interchanged.
702 // Also copy prev_global_ref into prev_rotate_ref for comparing.
703 // Convert global target destination into rotation target source.
704         if( config.global ) {
705                 if( !overlayer )
706                         overlayer = new OverlayFrame(PluginClient::get_project_smp() + 1);
707                 float dx, dy;
708                 if( config.tracking_object == MotionScan::TRACK_SINGLE ) {
709                         dx = (float)total_dx / OVERSAMPLE;
710                         dy = (float)total_dy / OVERSAMPLE;
711                 }
712                 else {
713                         dx = (float)current_dx / OVERSAMPLE;
714                         dy = (float)current_dy / OVERSAMPLE;
715                 }
716
717                 prev_rotate_ref->copy_from(prev_global_ref);
718                 current_rotate_ref->clear_frame();
719                 overlayer->overlay(current_rotate_ref, current_global_ref,
720                         0, 0, current_global_ref->get_w(), current_global_ref->get_h(),
721                         -dx, -dy,
722                         (float)current_global_ref->get_w() - dx,
723                         (float)current_global_ref->get_h() - dy,
724                         1, TRANSFER_REPLACE, CUBIC_LINEAR);
725 // Pivot need not be very accurate, it is attached to the previous frame
726 // while current frame is moved and rotated
727 // Nevertheless compute it in floating point and round to int afterwards
728                 tmp_x = prev_rotate_ref->get_w() * config.block_x / 100;
729                 tmp_y = prev_rotate_ref->get_h() * config.block_y / 100;
730                 if(config.tracking_object == MotionScan::TRACK_PREVIOUS)
731                 {
732 // Pivot is moved along the previous frame
733                         tmp_x += (double)(total_dx-current_dx) / OVERSAMPLE;
734                         tmp_y += (double)(total_dy-current_dy) / OVERSAMPLE;
735                 }
736 // Use the global target output as the rotation target input
737                 rotate_target_src->copy_from(global_target_dst);
738 // Transfer current reference frame to previous reference frame for global.
739                 if(config.tracking_object != MotionScan::TRACK_SINGLE &&
740                         (!config.twopass || load_ok))
741                 {
742                         prev_global_ref->copy_from(current_global_ref);
743                         previous_frame_number = get_source_position();
744                 }
745         }
746         else {
747 // Pivot is fixed as translation switched off
748                 tmp_x = prev_rotate_ref->get_w() * config.block_x / 100;
749                 tmp_y = prev_rotate_ref->get_h() * config.block_y / 100;
750         }
751         block_x = lrint (tmp_x);
752         block_y = lrint (tmp_y);
753
754 // Get rotation, either single pass or pass 1
755         if( !motion_rotate )
756                 motion_rotate = new RotateScan(this,
757                         get_project_smp() + 1, get_project_smp() + 1);
758
759 // Attention, prev_rotate_ref and current_rotate_ref are interchanged
760         current_angle = motion_rotate->
761                 scan_frame(current_rotate_ref, prev_rotate_ref, block_x, block_y, config.twopass);
762         current_angle += dt_offset;
763
764 // Save result
765         if( config.tracking_type == MotionScan::SAVE ) {
766                 save_dt = current_angle;
767         }
768
769 // Add current rotation to accumulation
770         if( config.tracking_object != MotionScan::TRACK_SINGLE ) {
771 // Retract over time
772                 total_angle = total_angle * (100 - config.rotate_return_speed) / 100;
773 // Accumulate current rotation
774                 total_angle += current_angle;
775
776 // Clamp rotation accumulation
777                 if( config.rotate_magnitude < 90 ) {
778                         CLAMP(total_angle, -config.rotate_magnitude, config.rotate_magnitude);
779                 }
780         }
781         else {
782                 total_angle = current_angle;
783         }
784
785 #ifdef DEBUG
786 printf("MotionMain::process_rotation total_angle=%f\n", total_angle);
787 #endif
788
789 // If there will be 2nd pass, target will be transformed then
790         if(!config.twopass || load_ok)
791         {
792                 if( config.tracking_object != MotionScan::TRACK_SINGLE && !config.global ) {
793 // Transfer current reference frame to previous reference frame and update counter.
794                         prev_rotate_ref->copy_from(current_rotate_ref);
795                         previous_frame_number = get_source_position();
796                 }
797
798 // No 2nd pass, calculate rotation parameters based on requested operation
799 // Use origin of global stabilize operation for pivot by default
800 // Compute it in floating point for accuracy and round to int afterwards
801                 tmp_x = rotate_target_src->get_w() * config.block_x / 100;
802                 tmp_y = rotate_target_src->get_h() * config.block_y / 100;
803
804                 float angle = 0.;
805                 switch(config.action_type) {
806                 case MotionScan::NOTHING:
807                         rotate_target_dst->copy_from(rotate_target_src);
808                         break;
809                 case MotionScan::TRACK:
810                 case MotionScan::TRACK_PIXEL:
811                         if(config.global)
812                         {
813 // Use destination of global tracking for pivot.
814                                 tmp_x += (double)total_dx / OVERSAMPLE;
815                                 tmp_y += (double)total_dy / OVERSAMPLE;
816                         }
817                         angle = total_angle;
818                         break;
819                 case MotionScan::STABILIZE:
820                 case MotionScan::STABILIZE_PIXEL:
821                         angle = -total_angle;
822                         break;
823                 }
824                 block_x = lrint (tmp_x);
825                 block_y = lrint (tmp_y);
826
827                 if( config.action_type != MotionScan::NOTHING ) {
828                         if( !rotate_engine )
829                                 rotate_engine = new AffineEngine(
830                                         PluginClient::get_project_smp() + 1,
831                                         PluginClient::get_project_smp() + 1);
832
833                         rotate_target_dst->clear_frame();
834
835                         rotate_engine->set_in_pivot(block_x, block_y);
836                         rotate_engine->set_out_pivot(block_x, block_y);
837
838                         rotate_engine->rotate(rotate_target_dst, rotate_target_src, angle);
839                 }
840         }
841 }
842
843 void MotionMain::refine_rotation()
844 {
845         int block_x, block_y;
846         double tmp_x, tmp_y;
847         float angle;
848
849 // Use temp_frame instead of current_rotate_ref for refined rotation search
850         allocate_temp(w, h, current_rotate_ref->get_color_model());
851         temp_frame->clear_frame();
852
853         if( config.global ) {
854 // Here we have to translate current_global_ref into current_rotate_ref
855 // backwards because prev_rotate_ref and current_rotate_ref are interchanged
856 // prev_rotate_ref must have been copied already by process_rotation()
857                 if( !overlayer )
858                         overlayer = new OverlayFrame(PluginClient::get_project_smp() + 1);
859                 float dx, dy;
860                 if( config.tracking_object == MotionScan::TRACK_SINGLE ) {
861                         dx = (float)total_dx / OVERSAMPLE;
862                         dy = (float)total_dy / OVERSAMPLE;
863                 }
864                 else {
865                         dx = (float)current_dx / OVERSAMPLE;
866                         dy = (float)current_dy / OVERSAMPLE;
867                 }
868
869                 current_rotate_ref->clear_frame();
870                 overlayer->overlay(current_rotate_ref, current_global_ref,
871                         0, 0, current_global_ref->get_w(), current_global_ref->get_h(),
872                         -dx, -dy,
873                         (float)current_global_ref->get_w() - dx,
874                         (float)current_global_ref->get_h() - dy,
875                         1, TRANSFER_REPLACE, CUBIC_LINEAR);
876
877 // Pivot need not be very accurate, it is attached to the previous frame
878 // while current frame is moved and rotated
879 // Nevertheless compute it in floating point and round to int afterwards
880                 tmp_x = current_rotate_ref->get_w() * config.block_x / 100;
881                 tmp_y = current_rotate_ref->get_h() * config.block_y / 100;
882                 if(config.tracking_object == MotionScan::TRACK_PREVIOUS)
883                 {
884 // Pivot is moved along the previous frame
885                         tmp_x += (double)(total_dx-current_dx) / OVERSAMPLE;
886                         tmp_y += (double)(total_dy-current_dy) / OVERSAMPLE;
887                 }
888         }
889         else {
890 // Pivot is fixed as translation switched off
891                 tmp_x = current_rotate_ref->get_w() * config.block_x / 100;
892                 tmp_y = current_rotate_ref->get_h() * config.block_y / 100;
893         }
894         block_x = lrint (tmp_x);
895         block_y = lrint (tmp_y);
896
897 // Now rotate current_rotate_ref into temp_frame
898 // backwards because prev_global_ref and current_global_ref are interchanged
899         if(!rotate_engine)
900                 rotate_engine = new AffineEngine(
901                         PluginClient::get_project_smp() + 1,
902                         PluginClient::get_project_smp() + 1);
903
904         if(config.tracking_object == MotionScan::TRACK_SINGLE)
905         {
906                 angle = total_angle;
907         }
908         else
909         {
910                 angle = current_angle;
911         }
912
913         rotate_engine->set_in_pivot(block_x, block_y);
914         rotate_engine->set_out_pivot(block_x, block_y);
915
916         rotate_engine->rotate(temp_frame, current_rotate_ref, -angle);
917
918 // Determine additional rotation, pass 2
919 // Attention, prev_rotate_ref and current_rotate_ref are interchanged
920 // Engine must have been created already by process_rotation()
921         angle = motion_rotate->
922                 scan_frame(temp_frame, prev_rotate_ref, block_x, block_y, 2);
923
924 // Rotation correction is to be added to accumulated angle
925         current_angle += angle;
926         total_angle   += angle;
927
928 // Refine saved result
929         if( config.tracking_type == MotionScan::SAVE ) {
930                 save_dt = current_angle;
931         }
932
933 // Clamp rotation accumulation
934         if( config.rotate_magnitude < 90 ) {
935                 CLAMP(total_angle, -config.rotate_magnitude, config.rotate_magnitude);
936         }
937
938 #ifdef DEBUG
939 printf("MotionMain::process_rotation total_angle=%f\n", total_angle);
940 #endif
941
942         if(config.tracking_object != MotionScan::TRACK_SINGLE)
943         {
944 // Transfer current reference frame to previous reference frame and update
945 // counter.
946                 if(config.global)
947                 {
948                         prev_global_ref->copy_from(current_global_ref);
949                 }
950                 else
951                 {
952                         prev_rotate_ref->copy_from(current_rotate_ref);
953                 }
954                 previous_frame_number = get_source_position();
955         }
956
957 // Calculate rotation parameters based on requested operation
958 // Use origin of global stabilize operation for pivot by default
959 // Compute it in floating point for accuracy and round to int afterwards
960         tmp_x = rotate_target_src->get_w() * config.block_x / 100;
961         tmp_y = rotate_target_src->get_h() * config.block_y / 100;
962
963         switch(config.action_type) {
964         case MotionScan::NOTHING:
965                 rotate_target_dst->copy_from(rotate_target_src);
966                 break;
967         case MotionScan::TRACK:
968         case MotionScan::TRACK_PIXEL:
969                 if(config.global)
970                 {
971 // Use destination of global tracking for pivot.
972                         tmp_x += (double)total_dx / OVERSAMPLE;
973                         tmp_y += (double)total_dy / OVERSAMPLE;
974                 }
975                 angle = total_angle;
976                 break;
977         case MotionScan::STABILIZE:
978         case MotionScan::STABILIZE_PIXEL:
979                 angle = -total_angle;
980                 break;
981         }
982         block_x = lrint (tmp_x);
983         block_y = lrint (tmp_y);
984
985         if( config.action_type != MotionScan::NOTHING ) {
986 // Should be already created elsewhere but try it here just for safety
987                 if( !rotate_engine )
988                         rotate_engine = new AffineEngine(
989                                 PluginClient::get_project_smp() + 1,
990                                 PluginClient::get_project_smp() + 1);
991
992                 rotate_target_dst->clear_frame();
993
994                 rotate_engine->set_in_pivot(block_x, block_y);
995                 rotate_engine->set_out_pivot(block_x, block_y);
996
997                 rotate_engine->rotate(rotate_target_dst, rotate_target_src, angle);
998         }
999 }
1000
1001
1002 int MotionMain::process_buffer(VFrame **frame, int64_t start_position, double frame_rate)
1003 {
1004         int need_reconfigure = load_configuration();
1005         int color_model = frame[0]->get_color_model();
1006         w = frame[0]->get_w();
1007         h = frame[0]->get_h();
1008
1009 #ifdef DEBUG
1010 printf("MotionMain::process_buffer %d start_position=%jd\n", __LINE__, start_position);
1011 #endif
1012
1013 // Calculate the source and destination pointers for each of the operations.
1014 // Get the layer to track motion in.
1015 // Get the layer to apply motion in.
1016         reference_layer = config.bottom_is_master ?
1017                 PluginClient::total_in_buffers - 1 : 0;
1018         target_layer = config.bottom_is_master ?
1019                 0 : PluginClient::total_in_buffers - 1;
1020
1021         output_frame = frame[target_layer];
1022 // Get the position of previous reference frame.
1023         int64_t actual_previous_number;
1024 // Skip if match frame not available
1025         int skip_current = 0;
1026
1027         if( config.tracking_object == MotionScan::TRACK_SINGLE ) {
1028                 actual_previous_number = config.track_frame;
1029                 if( get_direction() == PLAY_REVERSE )
1030                         actual_previous_number++;
1031                 if( actual_previous_number == start_position )
1032                         skip_current = 1;
1033         }
1034         else {
1035                 actual_previous_number = start_position;
1036                 if( get_direction() == PLAY_FORWARD ) {
1037                         actual_previous_number--;
1038                         if( actual_previous_number < get_source_start() )
1039                                 skip_current = 1;
1040                         else {
1041                                 KeyFrame *keyframe = get_prev_keyframe(start_position, 1);
1042                                 if( keyframe->position > 0 &&
1043                                     actual_previous_number < keyframe->position )
1044                                         skip_current = 1;
1045                         }
1046                 }
1047                 else {
1048                         actual_previous_number++;
1049                         if( actual_previous_number >= get_source_start() + get_total_len() )
1050                                 skip_current = 1;
1051                         else {
1052                                 KeyFrame *keyframe = get_next_keyframe(start_position, 1);
1053                                 if( keyframe->position > 0 &&
1054                                     actual_previous_number >= keyframe->position )
1055                                         skip_current = 1;
1056                         }
1057                 }
1058 // Only count motion since last keyframe
1059         }
1060
1061         if( !config.global && !config.rotate )
1062                 skip_current = 1;
1063
1064 //printf("process_realtime: %jd %d %jd %jd\n", start_position,
1065 // skip_current, previous_frame_number, actual_previous_number);
1066         if( ((config.tracking_type != MotionScan::LOAD &&
1067               config.tracking_type != MotionScan::SAVE) && cache_fp) ||
1068             ((config.tracking_type == MotionScan::LOAD ||
1069               config.tracking_type == MotionScan::SAVE) && !cache_fp) ||
1070             !cache_file[0] || (active_fp && active_key > start_position) )
1071                 reset_cache_file();
1072
1073 // Load match frame and reset vectors
1074         int need_reload = !skip_current &&
1075                 (previous_frame_number != actual_previous_number ||
1076                 need_reconfigure);
1077         if( need_reload ) {
1078                 total_dx = total_dy = 0; total_angle = 0;
1079                 previous_frame_number = actual_previous_number;
1080         }
1081
1082         if( skip_current ) {
1083                 total_dx = total_dy = 0;
1084                 current_dx = current_dy = 0;
1085                 total_angle = current_angle = 0;
1086         }
1087
1088 // Get the global pointers.  Here we walk through the sequence of events.
1089         if( config.global ) {
1090 // Assume global only.  Global reads previous frame and compares
1091 // with current frame to get the current translation.
1092 // The center of the search area is fixed in compensate mode or
1093 // the user value + the accumulation vector in track mode.
1094                 if( !prev_global_ref )
1095                         prev_global_ref = new VFrame(w, h, color_model, 0);
1096                 if( !current_global_ref )
1097                         current_global_ref = new VFrame(w, h, color_model, 0);
1098
1099 // Global loads the current target frame into the src and
1100 // writes it to the dst frame with desired translation.
1101                 if( !global_target_src )
1102                         global_target_src = new VFrame(w, h, color_model, 0);
1103                 if( !global_target_dst )
1104                         global_target_dst = new VFrame(w, h, color_model, 0);
1105
1106 // Load the global frames
1107                 if( need_reload ) {
1108                         read_frame(prev_global_ref, reference_layer,
1109                                 previous_frame_number, frame_rate, 0);
1110                 }
1111
1112                 read_frame(current_global_ref, reference_layer,
1113                         start_position, frame_rate, 0);
1114                 read_frame(global_target_src, target_layer,
1115                         start_position, frame_rate, 0);
1116
1117 // Global followed by rotate
1118                 if( config.rotate ) {
1119 // Must translate the previous global reference by the current global
1120 // accumulation vector to match the current global reference.
1121 // The center of the search area is always the user value + the accumulation
1122 // vector.
1123                         if( !prev_rotate_ref )
1124                                 prev_rotate_ref = new VFrame(w, h, color_model, 0);
1125 // The current global reference is the current rotation reference.
1126                         if( !current_rotate_ref )
1127                                 current_rotate_ref = new VFrame(w, h, color_model, 0);
1128                         current_rotate_ref->copy_from(current_global_ref);
1129
1130 // The global target destination is copied to the rotation target source
1131 // then written to the rotation output with rotation.
1132 // The pivot for the rotation is the center of the search area
1133 // if we're tracking.
1134 // The pivot is fixed to the user position if we're compensating.
1135                         if( !rotate_target_src )
1136                                 rotate_target_src = new VFrame(w, h, color_model, 0);
1137                         if( !rotate_target_dst )
1138                                 rotate_target_dst = new VFrame(w, h, color_model, 0);
1139                 }
1140         }
1141 // Rotation only
1142         else if( config.rotate ) {
1143 // Rotation reads the previous reference frame and compares it with current
1144 // reference frame.
1145                 if( !prev_rotate_ref )
1146                         prev_rotate_ref = new VFrame(w, h, color_model, 0);
1147                 if( !current_rotate_ref )
1148                         current_rotate_ref = new VFrame(w, h, color_model, 0);
1149
1150 // Rotation loads target frame to temporary, rotates it, and writes it to the
1151 // target frame.  The pivot is always fixed.
1152                 if( !rotate_target_src )
1153                         rotate_target_src = new VFrame(w, h, color_model, 0);
1154                 if( !rotate_target_dst )
1155                         rotate_target_dst = new VFrame(w, h, color_model, 0);
1156
1157
1158 // Load the rotate frames
1159                 if( need_reload ) {
1160                         read_frame(prev_rotate_ref, reference_layer,
1161                                 previous_frame_number, frame_rate, 0);
1162                 }
1163                 read_frame(current_rotate_ref, reference_layer,
1164                         start_position, frame_rate, 0);
1165                 read_frame(rotate_target_src, target_layer,
1166                         start_position, frame_rate, 0);
1167         }
1168
1169         if( config.tracking_type == MotionScan::LOAD ) {
1170                 if( config.addtrackedframeoffset ) {
1171                         if( config.track_frame != tracking_frame ) {
1172                                 tracking_frame = config.track_frame;
1173                                 int64_t no;  int dx, dy;  float dt;
1174                                 if( !get_cache_line(tracking_frame) &&
1175                                     sscanf(cache_line, "%jd %d %d %f", &no, &dx, &dy, &dt) == 4 ) {
1176                                         dx_offset += dx; dy_offset += dy;
1177                                         dt_offset += dt;
1178                                 }
1179                                 else {
1180                                         eprintf("no offset data frame %jd\n", tracking_frame);
1181                                 }
1182                         }
1183                 }
1184                 else
1185                 {
1186                         dx_offset = 0;
1187                         dy_offset = 0;
1188                         dt_offset = 0;
1189                         tracking_frame = -1;
1190                 }
1191         }
1192         else
1193         {
1194                 dx_offset = 0;
1195                 dy_offset = 0;
1196                 dt_offset = 0;
1197                 tracking_frame = -1;
1198         }
1199
1200         if( !skip_current ) {
1201                 load_ok = 0;
1202                 if( config.tracking_type == MotionScan::LOAD ||
1203                     config.tracking_type == MotionScan::SAVE ) {
1204                         int64_t no;  int dx, dy;  float dt;
1205                         int64_t frame_no = get_source_position();
1206 // Load result from disk
1207                         if( !get_cache_line(frame_no) &&
1208                             sscanf(cache_line, "%jd %d %d %f", &no, &dx, &dy, &dt) == 4 ) {
1209                                 load_ok = 1;  load_dx = dx;  load_dy = dy;  load_dt = dt;
1210                         }
1211                         else {
1212 #ifdef DEBUG
1213 printf("MotionMain::process_buffer: no tracking data frame %jd\n", frame_no);
1214 #endif
1215                         }
1216                 }
1217
1218 // Get position change from previous frame to current frame
1219                 if( config.global )
1220                         process_global();
1221 // Get rotation change from previous frame to current frame
1222                 if( config.rotate )
1223                         process_rotation();
1224 //frame[target_layer]->copy_from(prev_rotate_ref);
1225 //frame[target_layer]->copy_from(current_rotate_ref);
1226                 if(config.twopass && !load_ok)
1227 // Second pass not needed if coords loaded from cache
1228                 {
1229                         if(config.global) refine_global();
1230                         if(config.rotate) refine_rotation();
1231                 }
1232
1233 // write results to disk
1234                 if( config.tracking_type == MotionScan::SAVE ) {
1235                         char line[BCSTRLEN];
1236                         int64_t frame_no = get_source_position();
1237                         snprintf(line, sizeof(line), "%jd %d %d %f\n",
1238                                 frame_no, save_dx, save_dy, save_dt);
1239                         put_cache_line(line);
1240                 }
1241 // Transfer the relevant target frame to the output
1242                 if( config.rotate ) {
1243                         frame[target_layer]->copy_from(rotate_target_dst);
1244                 }
1245                 else {
1246                         frame[target_layer]->copy_from(global_target_dst);
1247                 }
1248         }
1249 // Read the target destination directly
1250         else {
1251                 read_frame(frame[target_layer],
1252                         target_layer, start_position, frame_rate, 0);
1253         }
1254
1255         if( config.draw_vectors ) {
1256                 draw_vectors(frame[target_layer]);
1257         }
1258
1259 #ifdef DEBUG
1260 printf("MotionMain::process_buffer %d\n", __LINE__);
1261 #endif
1262         return 0;
1263 }
1264
1265
1266
1267 void MotionMain::draw_vectors(VFrame *frame)
1268 {
1269         int w = frame->get_w(), h = frame->get_h();
1270         int global_x1, global_y1, global_x2, global_y2;
1271         int block_x, block_y, block_w, block_h;
1272         int block_x1, block_y1, block_x2, block_y2;
1273         int block_x3, block_y3, block_x4, block_y4;
1274         int search_x1, search_y1, search_x2, search_y2;
1275         int search_w, search_h;
1276         double tmp_x1, tmp_y1;
1277         double tmp_x2, tmp_y2;
1278
1279
1280         if( config.global ) {
1281 // Get vector as double and round the result for more regular behavior
1282                 if( config.tracking_object == MotionScan::TRACK_SINGLE ) {
1283 // Start of vector is center of original block.
1284 // Length of vector is total accumulation.
1285                         tmp_x1 = config.block_x * w / 100;
1286                         tmp_y1 = config.block_y * h / 100;
1287                         tmp_x2 = tmp_x1 + (double)total_dx / OVERSAMPLE;
1288                         tmp_y2 = tmp_y1 + (double)total_dy / OVERSAMPLE;
1289                 }
1290                 else if( config.tracking_object == MotionScan::PREVIOUS_SAME_BLOCK ) {
1291 // Start of vector is center of original block.
1292 // Length of vector is current change.
1293                         tmp_x1 = config.block_x * w / 100;
1294                         tmp_y1 = config.block_y * h / 100;
1295                         tmp_x2 = tmp_x1 + (double)current_dx / OVERSAMPLE;
1296                         tmp_y2 = tmp_y1 + (double)current_dy / OVERSAMPLE;
1297                 }
1298                 else {
1299 // Start of vector is center of current block.
1300 // Length of vector is current change.
1301                         tmp_x1 = config.block_x * w / 100 + 
1302                                 (double)(total_dx - current_dx) / OVERSAMPLE;
1303                         tmp_y1 = config.block_y * h / 100 +
1304                                 (double)(total_dy - current_dy) / OVERSAMPLE;
1305                         tmp_x2 = config.block_x * w / 100 + 
1306                                 (double)total_dx / OVERSAMPLE;
1307                         tmp_y2 = config.block_y * h / 100 +
1308                                 (double)total_dy / OVERSAMPLE;
1309                 }
1310                 global_x1 = lrint (tmp_x1);
1311                 global_y1 = lrint (tmp_y1);
1312                 global_x2 = lrint (tmp_x2);
1313                 global_y2 = lrint (tmp_y2);
1314 //printf("MotionMain::draw_vectors %d %d %d %d %d %d\n", total_dx, total_dy, global_x1, global_y1, global_x2, global_y2);
1315
1316                 block_x = global_x1;
1317                 block_y = global_y1;
1318                 if((config.tracking_object == MotionScan::TRACK_SINGLE ||
1319                     config.tracking_object == MotionScan::TRACK_PREVIOUS) &&
1320                    !config.rotate)
1321                 {
1322 // Show block at endpoint of motion if no rotation shown
1323                         block_x = global_x2;
1324                         block_y = global_y2;
1325                 }
1326                 block_w = config.global_block_w * w / 100;
1327                 block_h = config.global_block_h * h / 100;
1328                 block_x1 = block_x - block_w / 2;
1329                 block_y1 = block_y - block_h / 2;
1330                 block_x2 = block_x + block_w / 2;
1331                 block_y2 = block_y + block_h / 2;
1332                 search_w = config.global_range_w * w / 100;
1333                 search_h = config.global_range_h * h / 100;
1334                 search_x1 = block_x1 - search_w / 2;
1335                 search_y1 = block_y1 - search_h / 2;
1336                 search_x2 = block_x2 + search_w / 2;
1337                 search_y2 = block_y2 + search_h / 2;
1338
1339 //printf("MotionMain::draw_vectors %d %d %d %d %d %d %d %d %d %d %d %d\n",
1340 // global_x1, global_y1, block_w, block_h, block_x1, block_y1,
1341 // block_x2, block_y2, search_x1, search_y1, search_x2, search_y2);
1342
1343                 MotionScan::clamp_scan(w, h,
1344                         &block_x1, &block_y1, &block_x2, &block_y2,
1345                         &search_x1, &search_y1, &search_x2, &search_y2, 1);
1346
1347 // Vector
1348                 draw_arrow(frame, global_x1, global_y1, global_x2, global_y2);
1349
1350 // Macroblock
1351                 draw_line(frame, block_x1, block_y1, block_x2, block_y1);
1352                 draw_line(frame, block_x2, block_y1, block_x2, block_y2);
1353                 draw_line(frame, block_x2, block_y2, block_x1, block_y2);
1354                 draw_line(frame, block_x1, block_y2, block_x1, block_y1);
1355
1356 // Search area
1357                 draw_line(frame, search_x1, search_y1, search_x2, search_y1);
1358                 draw_line(frame, search_x2, search_y1, search_x2, search_y2);
1359                 draw_line(frame, search_x2, search_y2, search_x1, search_y2);
1360                 draw_line(frame, search_x1, search_y2, search_x1, search_y1);
1361
1362 // Block should be endpoint of motion
1363                 if( config.rotate ) {
1364                         block_x = global_x2;
1365                         block_y = global_y2;
1366                 }
1367         }
1368         else {
1369                 block_x = lrint (config.block_x * w / 100);
1370                 block_y = lrint (config.block_y * h / 100);
1371         }
1372
1373         block_w = config.global_block_w * w / 100;
1374         block_h = config.global_block_h * h / 100;
1375         if( config.rotate ) {
1376                 float angle = total_angle * 2 * M_PI / 360;
1377                 double base_angle1 = atan((float)block_h / block_w);
1378                 double base_angle2 = atan((float)block_w / block_h);
1379                 double target_angle1 = base_angle1 + angle;
1380                 double target_angle2 = base_angle2 + angle;
1381                 double radius = sqrt(block_w * block_w + block_h * block_h) / 2;
1382                 block_x1 = lrint(block_x - cos(target_angle1) * radius);
1383                 block_y1 = lrint(block_y - sin(target_angle1) * radius);
1384                 block_x2 = lrint(block_x + sin(target_angle2) * radius);
1385                 block_y2 = lrint(block_y - cos(target_angle2) * radius);
1386                 block_x3 = lrint(block_x - sin(target_angle2) * radius);
1387                 block_y3 = lrint(block_y + cos(target_angle2) * radius);
1388                 block_x4 = lrint(block_x + cos(target_angle1) * radius);
1389                 block_y4 = lrint(block_y + sin(target_angle1) * radius);
1390
1391                 draw_line(frame, block_x1, block_y1, block_x2, block_y2);
1392                 draw_line(frame, block_x2, block_y2, block_x4, block_y4);
1393                 draw_line(frame, block_x4, block_y4, block_x3, block_y3);
1394                 draw_line(frame, block_x3, block_y3, block_x1, block_y1);
1395
1396
1397 // Center
1398                 if( !config.global ) {
1399                         draw_line(frame, block_x, block_y - 5, block_x, block_y + 6);
1400                         draw_line(frame, block_x - 5, block_y, block_x + 6, block_y);
1401                 }
1402         }
1403 }
1404
1405 MotionVVFrame::MotionVVFrame(VFrame *vfrm, int n)
1406  : VFrame(vfrm->get_data(), -1, vfrm->get_y()-vfrm->get_data(),
1407         vfrm->get_u()-vfrm->get_data(), vfrm->get_v()-vfrm->get_data(),
1408         vfrm->get_w(), vfrm->get_h(), vfrm->get_color_model(),
1409         vfrm->get_bytes_per_line())
1410 {
1411         this->n = n;
1412 }
1413
1414 int MotionVVFrame::draw_pixel(int x, int y)
1415 {
1416         VFrame::draw_pixel(x+0, y+0);
1417         for( int i=1; i<n; ++i ) {
1418                 VFrame::draw_pixel(x-i, y-i);
1419                 VFrame::draw_pixel(x+i, y+i);
1420         }
1421         return 0;
1422 }
1423
1424 void MotionMain::draw_line(VFrame *frame, int x1, int y1, int x2, int y2)
1425 {
1426         int iw = frame->get_w(), ih = frame->get_h();
1427         int mx = iw > ih ? iw : ih;
1428         int n = mx/800 + 1;
1429         MotionVVFrame vfrm(frame, n);
1430         vfrm.set_pixel_color(WHITE);
1431         int m = 2;  while( m < n ) m <<= 1;
1432         vfrm.set_stiple(2*m);
1433         vfrm.draw_line(x1,y1, x2,y2);
1434 }
1435
1436 #define ARROW_SIZE 10
1437 void MotionMain::draw_arrow(VFrame *frame, int x1, int y1, int x2, int y2)
1438 {
1439         double angle = atan2((double)(y2 - y1), (double)(x2 - x1));
1440         double angle1 = angle + (float)145 / 360 * 2 * M_PI;
1441         double angle2 = angle - (float)145 / 360 * 2 * M_PI;
1442         int x3 = x2 + (int)(ARROW_SIZE * cos(angle1));
1443         int y3 = y2 + (int)(ARROW_SIZE * sin(angle1));
1444         int x4 = x2 + (int)(ARROW_SIZE * cos(angle2));
1445         int y4 = y2 + (int)(ARROW_SIZE * sin(angle2));
1446
1447 // Main vector
1448         draw_line(frame, x1, y1, x2, y2);
1449 //      draw_line(frame, x1, y1 + 1, x2, y2 + 1);
1450
1451 // Arrow line
1452         if( abs(y2 - y1) || abs(x2 - x1) ) draw_line(frame, x2, y2, x3, y3);
1453 //      draw_line(frame, x2, y2 + 1, x3, y3 + 1);
1454 // Arrow line
1455         if( abs(y2 - y1) || abs(x2 - x1) ) draw_line(frame, x2, y2, x4, y4);
1456 //      draw_line(frame, x2, y2 + 1, x4, y4 + 1);
1457 }
1458
1459 int MotionMain::open_cache_file()
1460 {
1461         if( cache_fp ) return 0;
1462         if( !cache_file[0] ) return 1;
1463         if( !(cache_fp = fopen(cache_file, "r")) ) return 1;
1464         return 0;
1465 }
1466
1467 void MotionMain::close_cache_file()
1468 {
1469         if( !cache_fp ) return;
1470         fclose(cache_fp);
1471         cache_fp = 0; cache_key = -1; tracking_frame = -1;
1472 }
1473
1474 int MotionMain::load_cache_line()
1475 {
1476         cache_key = -1;
1477         if( open_cache_file() ) return 1;
1478         if( !fgets(cache_line, sizeof(cache_line), cache_fp) ) return 1;
1479         cache_key = strtol(cache_line, 0, 0);
1480         return 0;
1481 }
1482
1483 int MotionMain::get_cache_line(int64_t key)
1484 {
1485         if( cache_key == key ) return 0;
1486         if( open_cache_file() ) return 1;
1487         if( cache_key >= 0 && key > cache_key ) {
1488                 if( load_cache_line() ) return 1;
1489                 if( cache_key == key ) return 0;
1490                 if( cache_key > key ) return 1;
1491         }
1492 // binary search file
1493         fseek(cache_fp, 0, SEEK_END);
1494         int64_t l = -1, r = ftell(cache_fp);
1495         while( (r - l) > 1 ) {
1496                 int64_t m = (l + r) / 2;
1497                 fseek(cache_fp, m, SEEK_SET);
1498                 if( m > 0 && !fgets(cache_line, sizeof(cache_line), cache_fp) )
1499                         return -1;
1500                 if( !load_cache_line() ) {
1501                         if( cache_key == key )
1502                                 return 0;
1503                         if( cache_key < key ) { l = m; continue; }
1504                 }
1505                 r = m;
1506         }
1507         return 1;
1508 }
1509
1510 int MotionMain::locate_cache_line(int64_t key)
1511 {
1512         int ret = 1;
1513         if( key < 0 || !(ret=get_cache_line(key)) ||
1514             ( cache_key >= 0 && cache_key < key ) )
1515                 ret = load_cache_line();
1516         return ret;
1517 }
1518
1519 int MotionMain::put_cache_line(const char *line)
1520 {
1521         int64_t key = strtol(line, 0, 0);
1522         if( key == active_key ) return 1;
1523         if( !active_fp ) {
1524                 close_cache_file();
1525                 snprintf(cache_file, sizeof(cache_file), "%s.bak", config.tracking_file);
1526                 ::remove(cache_file);
1527                 ::rename(config.tracking_file, cache_file);
1528                 if( !(active_fp = fopen(config.tracking_file, "w")) ) {
1529                         perror(config.tracking_file);
1530                         fprintf(stderr, "err writing key %jd\n", key);
1531                         return -1;
1532                 }
1533                 active_key = -1;
1534         }
1535
1536         if( active_key < key ) {
1537                 locate_cache_line(active_key);
1538                 while( cache_key >= 0 && key >= cache_key ) {
1539                         if( key > cache_key )
1540                                 fputs(cache_line, active_fp);
1541                         load_cache_line();
1542                 }
1543         }
1544
1545         active_key = key;
1546         fputs(line, active_fp);
1547         fflush(active_fp);
1548         return 0;
1549 }
1550
1551 void MotionMain::reset_cache_file()
1552 {
1553         if( active_fp ) {
1554                 locate_cache_line(active_key);
1555                 while( cache_key >= 0 ) {
1556                         fputs(cache_line, active_fp);
1557                         load_cache_line();
1558                 }
1559                 close_cache_file();  ::remove(cache_file);
1560                 fclose(active_fp); active_fp = 0; active_key = -1;
1561         }
1562         else
1563                 close_cache_file();
1564         strcpy(cache_file, config.tracking_file);
1565         if (!cache_file[0]) strcpy(cache_file, TRACKING_FILE);
1566 }
1567
1568
1569 RotateScanPackage::RotateScanPackage()
1570 {
1571 }
1572
1573 RotateScanUnit::RotateScanUnit(RotateScan *server, MotionMain *plugin)
1574  : LoadClient(server)
1575 {
1576         this->server = server;
1577         this->plugin = plugin;
1578         rotater = 0;
1579         temp = 0;
1580 }
1581
1582 RotateScanUnit::~RotateScanUnit()
1583 {
1584         delete rotater;
1585         delete temp;
1586 }
1587
1588 void RotateScanUnit::process_package(LoadPackage *package)
1589 {
1590         if( server->skip ) return;
1591         RotateScanPackage *pkg = (RotateScanPackage*)package;
1592
1593         if( (pkg->difference = server->get_cache(pkg->angle)) < 0 ) {
1594 //printf("RotateScanUnit::process_package %d\n", __LINE__);
1595                 int color_model = server->previous_frame->get_color_model();
1596                 int pixel_size = BC_CModels::calculate_pixelsize(color_model);
1597                 int row_bytes = server->previous_frame->get_bytes_per_line();
1598                 float angle = pkg->angle;
1599 // Ensure a tiny displacement if angle is nearly exact zero
1600 // As angle is in degree and MIN_ANGLE is in radian,
1601 // displacement of 1/57th of the smallest possible angle can be discarded
1602 // This trick is needed to trigger interpolation
1603                 if (fabs (angle) < MIN_ANGLE) angle = MIN_ANGLE;
1604
1605                 if( !rotater )
1606                         rotater = new AffineEngine(1, 1);
1607                 if( !temp )
1608                         temp = new VFrame(
1609                                 server->previous_frame->get_w(),
1610                                 server->previous_frame->get_h(),
1611                                 color_model, 0);
1612 //printf("RotateScanUnit::process_package %d\n", __LINE__);
1613
1614
1615 // Rotate original block size
1616 //              rotater->set_viewport(server->block_x1, server->block_y1,
1617 //                      server->block_x2 - server->block_x1, server->block_y2 - server->block_y1);
1618                 rotater->set_in_viewport(server->block_x1, server->block_y1,
1619                         server->block_x2 - server->block_x1, server->block_y2 - server->block_y1);
1620                 rotater->set_out_viewport(server->block_x1, server->block_y1,
1621                         server->block_x2 - server->block_x1, server->block_y2 - server->block_y1);
1622 //              rotater->set_pivot(server->block_x, server->block_y);
1623                 rotater->set_in_pivot(server->block_x, server->block_y);
1624                 rotater->set_out_pivot(server->block_x, server->block_y);
1625 //printf("RotateScanUnit::process_package %d\n", __LINE__);
1626                 rotater->rotate(temp, server->previous_frame, angle);
1627
1628 // Scan reduced block size
1629 //plugin->output_frame->copy_from(server->current_frame);
1630 //plugin->output_frame->copy_from(temp);
1631 //printf("RotateScanUnit::process_package %d %d %d %d %d\n",
1632 // __LINE__, server->scan_x, server->scan_y, server->scan_w, server->scan_h);
1633 // Clamp coordinates
1634                 int x1 = server->scan_x;
1635                 int y1 = server->scan_y;
1636                 int x2 = x1 + server->scan_w;
1637                 int y2 = y1 + server->scan_h;
1638                 x2 = MIN(temp->get_w(), x2);
1639                 y2 = MIN(temp->get_h(), y2);
1640                 x2 = MIN(server->current_frame->get_w(), x2);
1641                 y2 = MIN(server->current_frame->get_h(), y2);
1642                 x1 = MAX(0, x1);  y1 = MAX(0, y1);
1643
1644                 if( x2 > x1 && y2 > y1 ) {
1645                         pkg->difference = MotionScan::abs_diff(
1646                                 temp->get_rows()[y1] + x1 * pixel_size,
1647                                 server->current_frame->get_rows()[y1] + x1 * pixel_size,
1648                                 row_bytes, x2 - x1, y2 - y1, color_model);
1649 //printf("RotateScanUnit::process_package %d\n", __LINE__);
1650                         server->put_cache(pkg->angle, pkg->difference);
1651 // Dumping rotated frame for debugging
1652 //                      temp->write_ppm(temp, "/tmp/a%06ld-a%f.ppm",
1653 //                                      plugin->get_source_position(),
1654 //                                      pkg->angle);
1655 //                      if (pkg->angle == 0)
1656 //                        temp->write_ppm(server->previous_frame,
1657 //                                        "/tmp/a%06ld-t.ppm",
1658 //                                        plugin->get_source_position());
1659                 }
1660 #if 0
1661         VFrame png(x2-x1, y2-y1, BC_RGB888, -1);
1662         png.transfer_from(temp, 0, x1, y1, x2-x1, y2-y1);
1663         char fn[64];
1664         sprintf(fn,"%s%f.png","/tmp/temp",pkg->angle); png.write_png(fn);
1665         png.transfer_from(server->current_frame, 0, x1, y1, x2-x1, y2-y1);
1666         sprintf(fn,"%s%f.png","/tmp/curr",pkg->angle); png.write_png(fn);
1667 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",
1668  server->block_x1, server->block_y1, server->block_x2 - server->block_x1, server->block_y2 - server->block_y1,
1669  server->block_x,  server->block_y,  pkg->angle,  server->scan_w, server->scan_h, pkg->difference);
1670 #endif
1671         }
1672 }
1673
1674
1675 RotateScan::RotateScan(MotionMain *plugin,
1676         int total_clients,
1677         int total_packages)
1678  : LoadServer( //1, 1)
1679                 total_clients, total_packages)
1680 {
1681         this->plugin = plugin;
1682         cache_lock = new Mutex("RotateScan::cache_lock");
1683 }
1684
1685
1686 RotateScan::~RotateScan()
1687 {
1688         delete cache_lock;
1689 }
1690
1691 void RotateScan::init_packages()
1692 {
1693         for( int i = 0; i < get_total_packages(); i++ ) {
1694                 RotateScanPackage *pkg = (RotateScanPackage*)get_package(i);
1695                 pkg->angle = scan_angle1 +
1696                         i * (scan_angle2 - scan_angle1) / total_steps;
1697         }
1698 }
1699
1700 LoadClient* RotateScan::new_client()
1701 {
1702         return new RotateScanUnit(this, plugin);
1703 }
1704
1705 LoadPackage* RotateScan::new_package()
1706 {
1707         return new RotateScanPackage;
1708 }
1709
1710
1711 float RotateScan::scan_frame(VFrame *previous_frame, VFrame *current_frame,
1712         int block_x, int block_y, int passno)
1713 {
1714 // Attention, process_buffer feeds previous_frame and current_frame interchanged
1715 // Preinitialize sane rotation results
1716         skip = 0;
1717         this->block_x = block_x;
1718         this->block_y = block_y;
1719
1720 // passno == 0: single pass tracking
1721 // passno == 1: 1st pass of two-pass tracking (reduce accuracy)
1722 // passno == 2: 2nd pass of two-pass tracking (reduce angle range)
1723 // Save may be needed for 2nd pass
1724 //      float result_saved = 0;
1725         if (passno == 2)
1726         {
1727 // result_saved needed for some debug printing only
1728 //              result_saved = result;
1729                 result = 0;
1730         }
1731         else
1732         {
1733                 result = plugin->config.rotation_center;
1734         }
1735
1736 //printf("RotateScan::scan_frame %d frame=%ld passno=%d\n", __LINE__, plugin->get_source_position(), passno);
1737         switch(plugin->config.tracking_type) {
1738         case MotionScan::NO_CALCULATE:
1739                 result = plugin->config.rotation_center;
1740                 if (passno == 2) result = 0;
1741                 skip = 1;
1742                 break;
1743
1744         case MotionScan::LOAD:
1745         case MotionScan::SAVE:
1746                 if( plugin->load_ok ) {
1747                         result = plugin->load_dt;
1748                         if (passno == 2) result = 0;
1749                         skip = 1;
1750                 }
1751                 break;
1752
1753 // Scan from scratch with sane rotation results
1754         default:
1755                 result = plugin->config.rotation_center;
1756                 if (passno == 2) result = 0;
1757                 skip = 0;
1758                 break;
1759         }
1760
1761         this->previous_frame = previous_frame;
1762         this->current_frame = current_frame;
1763         int w = current_frame->get_w();
1764         int h = current_frame->get_h();
1765         int block_w = w * plugin->config.global_block_w / 100;
1766         int block_h = h * plugin->config.global_block_h / 100;
1767
1768         if( this->block_x - block_w / 2 < 0 ) block_w = this->block_x * 2;
1769         if( this->block_y - block_h / 2 < 0 ) block_h = this->block_y * 2;
1770         if( this->block_x + block_w / 2 > w ) block_w = (w - this->block_x) * 2;
1771         if( this->block_y + block_h / 2 > h ) block_h = (h - this->block_y) * 2;
1772
1773         block_x1 = this->block_x - block_w / 2;
1774         block_x2 = this->block_x + block_w / 2;
1775         block_y1 = this->block_y - block_h / 2;
1776         block_y2 = this->block_y + block_h / 2;
1777
1778 // Calculate the maximum area available to scan after rotation.
1779 // Must be calculated from the starting range because of cache.
1780 // Get coords of rectangle after rotation.
1781         double center_x = this->block_x;
1782         double center_y = this->block_y;
1783         double max_angle = plugin->config.rotation_range;
1784         double base_angle1 = atan((float)block_h / block_w);
1785         double base_angle2 = atan((float)block_w / block_h);
1786         double target_angle1 = base_angle1 + max_angle * 2 * M_PI / 360;
1787         double target_angle2 = base_angle2 + max_angle * 2 * M_PI / 360;
1788         double radius = sqrt(block_w * block_w + block_h * block_h) / 2;
1789         double x1 = center_x - cos(target_angle1) * radius;
1790         double y1 = center_y - sin(target_angle1) * radius;
1791         double x2 = center_x + sin(target_angle2) * radius;
1792         double y2 = center_y - cos(target_angle2) * radius;
1793         double x3 = center_x - sin(target_angle2) * radius;
1794         double y3 = center_y + cos(target_angle2) * radius;
1795
1796 // Track top edge to find greatest area.
1797         double max_area1 = 0;
1798         //double max_x1 = 0;
1799         double max_y1 = 0;
1800         for( double x = x1; x < x2; x++ ) {
1801                 double y = y1 + (y2 - y1) * (x - x1) / (x2 - x1);
1802                 if( x >= center_x && x < block_x2 && y >= block_y1 && y < center_y ) {
1803                         double area = fabs(x - center_x) * fabs(y - center_y);
1804                         if( area > max_area1 ) {
1805                                 max_area1 = area;
1806                                 //max_x1 = x;
1807                                 max_y1 = y;
1808                         }
1809                 }
1810         }
1811
1812 // Track left edge to find greatest area.
1813         double max_area2 = 0;
1814         double max_x2 = 0;
1815         //double max_y2 = 0;
1816         for( double y = y1; y < y3; y++ ) {
1817                 double x = x1 + (x3 - x1) * (y - y1) / (y3 - y1);
1818                 if( x >= block_x1 && x < center_x && y >= block_y1 && y < center_y ) {
1819                         double area = fabs(x - center_x) * fabs(y - center_y);
1820                         if( area > max_area2 ) {
1821                                 max_area2 = area;
1822                                 max_x2 = x;
1823                                 //max_y2 = y;
1824                         }
1825                 }
1826         }
1827
1828         double max_x, max_y;
1829         max_x = max_x2;
1830         max_y = max_y1;
1831
1832 // Get reduced scan coords
1833         scan_w = (int)(fabs(max_x - center_x) * 2);
1834         scan_h = (int)(fabs(max_y - center_y) * 2);
1835         scan_x = (int)(center_x - scan_w / 2);
1836         scan_y = (int)(center_y - scan_h / 2);
1837 // printf("RotateScan::scan_frame center=%d,%d scan=%d,%d %dx%d\n",
1838 // this->block_x, this->block_y, scan_x, scan_y, scan_w, scan_h);
1839 // printf("    angle_range=%f block= %d,%d,%d,%d\n", max_angle, block_x1, block_y1, block_x2, block_y2);
1840
1841 // Determine min angle from size of block
1842         double angle1 = atan((double)block_h / block_w);
1843         double angle2 = atan((double)(block_h - 1) / (block_w + 1));
1844 // Attention we get min_angle and MIN_ANGLE in radian, but elsewhere use degree
1845         double min_angle = fabs(angle2 - angle1) / OVERSAMPLE;
1846         min_angle = MAX(min_angle, MIN_ANGLE);
1847 // Convert min_angle to degree for convenience
1848         min_angle *= 180 / M_PI;
1849
1850 //printf("RotateScan::scan_frame %d min_angle=%f\n", __LINE__, min_angle);
1851
1852         cache.remove_all_objects();
1853
1854
1855         if( !skip ) {
1856                 if( previous_frame->data_matches(current_frame) ) {
1857 //printf("RotateScan::scan_frame: frames match.  Skipping.\n");
1858                         result = plugin->config.rotation_center;
1859                         if (passno == 2) result = 0;
1860                         skip = 1;
1861                 }
1862         }
1863
1864         if( !skip ) {
1865 // Initial search range
1866                 float angle_range = max_angle;
1867                 result = plugin->config.rotation_center;
1868                 if (passno == 2) result = 0;
1869
1870                 if (passno == 1)
1871                 {
1872 // Evtl stop search earlier for 1st pass to gain speed
1873                         if (angle_range > 16 && min_angle < 1) min_angle = 1;
1874                         else if (angle_range > 4 && min_angle < 0.25) min_angle = angle_range/16;
1875                         else if (angle_range > min_angle*4) min_angle *= 4;
1876                 }
1877
1878                 if (passno == 2)
1879                 {
1880 // Evtl reduce angle_range for refinement pass to gain speed
1881                         if (angle_range > 16) angle_range /= 4;
1882                         else if (angle_range > 4) angle_range = 4;
1883                         if (angle_range < min_angle*4) angle_range = min_angle*4;
1884                         if (angle_range > max_angle) angle_range = max_angle;
1885                 }
1886
1887 // Pre-negate result as previous_frame and current_frame have been interchanged
1888                 result = -result;
1889
1890                 while( angle_range >= min_angle ) {
1891                         scan_angle1 = result - angle_range;
1892                         scan_angle2 = result + angle_range;
1893 // Find number of required steps, even and no more than configured at once
1894                         total_steps = (int)ceil(angle_range*2/min_angle);
1895                         if (total_steps & 1) total_steps ++;
1896                         if (total_steps > plugin->config.rotate_positions) total_steps = plugin->config.rotate_positions;
1897
1898 //printf("RotateScan::scan_frame angle_range=%f from=%f to=%f steps=%d\n", angle_range, scan_angle1, scan_angle2, total_steps);
1899
1900 // Use odd number of samples to ensure that rotation center be always included
1901                         set_package_count(total_steps+1);
1902 //set_package_count(1);
1903                         process_packages();
1904
1905                         int64_t min_difference = -1, max_difference = -1, noiselev = -1;
1906                         for( int i = 0; i < get_total_packages(); i++ ) {
1907                                 RotateScanPackage *pkg = (RotateScanPackage*)get_package(i);
1908                                 if( pkg->difference < min_difference || min_difference == -1 ) {
1909                                         min_difference = pkg->difference;
1910                                         result = pkg->angle;
1911                                 }
1912                                 if( pkg->difference > max_difference || max_difference == -1 ) {
1913                                         max_difference = pkg->difference;
1914                                 }
1915 //printf("RotateScan::scan_frame pkg=%d angle=%f diff=%ld min_diff=%ld max_diff=%ld\n", i, pkg->angle, pkg->difference, min_difference, max_difference);
1916 //break;
1917                         }
1918 // Determine noise level (not active on pass 2)
1919                         noiselev = min_difference+(max_difference-min_difference)*plugin->config.noise_rotation/100;
1920                         if (passno == 2) noiselev = min_difference;
1921 //printf("RotateScan::scan_frame min_diff=%ld max_diff=%ld noiselev=%ld\n", min_difference, max_difference, noiselev);
1922                         for(int i = 0; i < get_total_packages(); i++)
1923                         {
1924                                 RotateScanPackage *pkg = (RotateScanPackage*)get_package(i);
1925 // Already found as the best sample, not necessary to memorize
1926                                 if(result == pkg->angle) continue;
1927 // Above noise level - a definitely bad sample, skip
1928                                 if(pkg->difference > noiselev) continue;
1929 // Below noise level but farther from rotation center, skip
1930                                 if(fabs(pkg->angle-plugin->config.rotation_center) > fabs(result-plugin->config.rotation_center)) continue;
1931 // Below noise level and nearer to rotation center, memorize
1932                                 if(fabs(pkg->angle-plugin->config.rotation_center) < fabs(result-plugin->config.rotation_center))
1933                                 {
1934                                         min_difference = pkg->difference;
1935                                         result = pkg->angle;
1936 //printf("RotateScan::scan_frame angle override=%d angle=%f diff=%ld min_diff=%ld\n", i, pkg->angle, pkg->difference, min_difference);
1937                                         continue;
1938                                 }
1939 // Equal distances to rotation center, memorize sample with min difference
1940                                 if(pkg->difference < min_difference)
1941                                 {
1942                                         min_difference = pkg->difference;
1943                                         result = pkg->angle;
1944 //printf("RotateScan::scan_frame difference override=%d angle=%f diff=%ld min_diff=%ld\n", i, pkg->angle, pkg->difference, min_difference);
1945                                         continue;
1946                                 }
1947                         }
1948                         float new_range = 0, angle_diff = 0;
1949                         for(int i = 0; i < get_total_packages(); i++)
1950                         {
1951                                 RotateScanPackage *pkg = (RotateScanPackage*)get_package(i);
1952 // Above noise level - skip this angle
1953                                 if(pkg->difference > noiselev) continue;
1954 // Below noise level - measure max difference from the best angle
1955                                 if(angle_diff < fabs(result-pkg->angle))
1956                                 {
1957                                         angle_diff = fabs(result-pkg->angle);
1958 //printf("RotateScan::scan_frame angle diff override=%d angle=%f diff=%f\n", i, pkg->angle, angle_diff);
1959                                 }
1960                         }
1961 // Optimum new angle range might be +/- one search step from the best angle
1962                         new_range = angle_range * 2 / total_steps;
1963 // Evtl expand angle range to +/- two search steps if some samples below noise
1964                         if (angle_diff > 0) new_range = angle_range * 4 / total_steps;
1965 // Evtl expand angle range to include samples below noise level
1966                         if (new_range < angle_diff) new_range = angle_diff;
1967 // But always reduce angle range at least twice
1968                         if (new_range > angle_range / 2) new_range = angle_range / 2;
1969                         angle_range = new_range;
1970 //break;
1971                 }
1972 // Negate result as previous_frame and current_frame have been interchanged
1973 // and get rid of negative zeros in coord files
1974                 result = -result;
1975                 if (fabs (result) < MIN_ANGLE) result = 0;
1976         }
1977
1978 // Dumping compared frames for debugging
1979 //              current_frame->write_ppm(previous_frame, "/tmp/a%06ld-p.ppm",
1980 //                                       plugin->get_source_position());
1981 //              current_frame->write_ppm(current_frame, "/tmp/a%06ld-c.ppm",
1982 //                                       plugin->get_source_position());
1983
1984 //printf("RotateScan::scan_frame %d passno=%d saved angle=%f measured angle=%f twopass angle=%f\n", __LINE__, passno, result_saved, result, result_saved+result);
1985         return result;
1986 }
1987
1988 int64_t RotateScan::get_cache(float angle)
1989 {
1990         int64_t result = -1;
1991         cache_lock->lock("RotateScan::get_cache");
1992         for( int i = 0; i < cache.total; i++ ) {
1993                 RotateScanCache *ptr = cache.values[i];
1994 // Attention, MIN_ANGLE in radian, while angle and ptr->angle in degree !
1995                 if( fabs(ptr->angle - angle) <= MIN_ANGLE * 90 / M_PI ) {
1996                         result = ptr->difference;
1997                         break;
1998                 }
1999         }
2000         cache_lock->unlock();
2001         return result;
2002 }
2003
2004 void RotateScan::put_cache(float angle, int64_t difference)
2005 {
2006         RotateScanCache *ptr = new RotateScanCache(angle, difference);
2007         cache_lock->lock("RotateScan::put_cache");
2008         cache.append(ptr);
2009         cache_lock->unlock();
2010 }
2011
2012
2013 RotateScanCache::RotateScanCache(float angle, int64_t difference)
2014 {
2015         this->angle = angle;
2016         this->difference = difference;
2017 }
2018
2019
2020