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