mod motion tracking file in motion/motion-cv
authorGood Guy <[email protected]>
Fri, 25 Nov 2016 19:39:41 +0000 (12:39 -0700)
committerGood Guy <[email protected]>
Fri, 25 Nov 2016 19:39:41 +0000 (12:39 -0700)
15 files changed:
cinelerra-5.1/cinelerra/transportque.C
cinelerra-5.1/plugins/motion-cv/motion-cv.C
cinelerra-5.1/plugins/motion-cv/motion-cv.C.orig [deleted file]
cinelerra-5.1/plugins/motion-cv/motion-cv.h
cinelerra-5.1/plugins/motion-cv/motionwindow-cv.C
cinelerra-5.1/plugins/motion-cv/motionwindow-cv.h
cinelerra-5.1/plugins/motion/motion.C
cinelerra-5.1/plugins/motion/motion.h
cinelerra-5.1/plugins/motion/motion.inc
cinelerra-5.1/plugins/motion/motionscan.C
cinelerra-5.1/plugins/motion/motionscan.h
cinelerra-5.1/plugins/motion/motionscan.inc
cinelerra-5.1/plugins/motion/motionwindow.C
cinelerra-5.1/plugins/motion/motionwindow.h
cinelerra-5.1/plugins/motion/motionwindow.inc

index 5b7fba9d4266ebce17592041b7f1eae35e83f99e..4a880ee0a2476221065a49f0a19d80cc18fa1cf2 100644 (file)
@@ -111,19 +111,17 @@ int TransportCommand::get_direction()
                case SLOW_FWD:
                case CURRENT_FRAME:
                        return PLAY_FORWARD;
-                       break;
 
                case SINGLE_FRAME_REWIND:
                case NORMAL_REWIND:
                case FAST_REWIND:
                case SLOW_REWIND:
                        return PLAY_REVERSE;
-                       break;
 
                default:
-                       return PLAY_FORWARD;
                        break;
        }
+       return PLAY_FORWARD;
 }
 
 float TransportCommand::get_speed()
index 13f67e460cccd52fd8c91ac5e97ad0f934385492..5e4a1c19cd2196d4438467ee9aa4ee8df7fb8edb 100644 (file)
@@ -2,21 +2,21 @@
 /*
  * CINELERRA
  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
- * 
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
- * 
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- * 
+ *
  */
 
 #include "affine.h"
@@ -27,6 +27,7 @@
 #include "filexml.h"
 #include "keyframe.h"
 #include "language.h"
+#include "mainerror.h"
 #include "motion-cv.h"
 #include "motionwindow-cv.h"
 #include "mutex.h"
 #include "rotateframe.h"
 #include "transportque.h"
 
-
 #include <errno.h>
 #include <unistd.h>
 
 REGISTER_PLUGIN(MotionCVMain)
-
 //#undef DEBUG
 
-#ifndef DEBUG
-#define DEBUG
-#endif
-
-
 MotionCVConfig::MotionCVConfig()
 {
-       global_range_w = 5;
-       global_range_h = 5;
-       rotation_range = 5;
+       global_range_w = 25; //5;
+       global_range_h = 25; //5;
+       rotation_range = 8; //5;
        block_count = 1;
-       global_block_w = MIN_BLOCK;
-       global_block_h = MIN_BLOCK;
-       rotation_block_w = MIN_BLOCK;
-       rotation_block_h = MIN_BLOCK;
+       global_block_w = 33; //MIN_BLOCK;
+       global_block_h = 33; //MIN_BLOCK;
+       rotation_block_w = 16; //MIN_BLOCK;
+       rotation_block_h = 16; //MIN_BLOCK;
        block_x = 50;
        block_y = 50;
        global_positions = 256;
-       rotate_positions = 4;
+       rotate_positions = 8; // 4;
        magnitude = 100;
-       return_speed = 0;
+       return_speed = 5; //0;
        mode1 = STABILIZE;
        global = 1;
        rotate = 1;
        addtrackedframeoffset = 0;
        strcpy(tracking_file, TRACKING_FILE);
-       mode2 = RECALCULATE;
+       mode2 = SAVE; //NO_CALCULATE;
+       mode3 = TRACK_PREVIOUS; //TRACK_SINGLE;
        draw_vectors = 1;
-       mode3 = MotionCVConfig::TRACK_SINGLE;
        track_frame = 0;
        bottom_is_master = 1;
        horizontal_only = 0;
@@ -95,8 +89,7 @@ int MotionCVConfig::equivalent(MotionCVConfig &that)
                global_range_h == that.global_range_h &&
                rotation_range == that.rotation_range &&
                mode1 == that.mode1 &&
-               global == that.global &&
-               rotate == that.rotate &&
+               global == that.global && rotate == that.rotate &&
                addtrackedframeoffset == that.addtrackedframeoffset &&
                !strcmp(tracking_file, that.tracking_file) &&
                draw_vectors == that.draw_vectors &&
@@ -148,8 +141,8 @@ void MotionCVConfig::copy_from(MotionCVConfig &that)
        vertical_only = that.vertical_only;
 }
 
-void MotionCVConfig::interpolate(MotionCVConfig &prev, MotionCVConfig &next, 
-               int64_t prev_frame, int64_t next_frame, int64_t current_frame)
+void MotionCVConfig::interpolate(MotionCVConfig &prev, MotionCVConfig &next,
+       int64_t prev_frame, int64_t next_frame, int64_t current_frame)
 {
        copy_from(prev);
 }
@@ -174,14 +167,16 @@ MotionCVMain::MotionCVMain(PluginServer *server)
        global_target_src = 0;
        global_target_dst = 0;
 
-       active_fp = 0;
-       active_file[0] = 0;
-       tracking_frame = -1;
+       cache_file[0] = 0;
+       cache_fp = active_fp = 0;
+       cache_line[0] = 0;
+       cache_key = active_key = -1;
        dx_offset = dy_offset = 0;
        load_ok = 0;
        save_dx = load_dx = 0;
        save_dy = load_dy = 0;
        save_dt = load_dt = 0;
+       tracking_frame = -1;
 
        prev_rotate_ref = 0;
        current_rotate_ref = 0;
@@ -191,10 +186,9 @@ MotionCVMain::MotionCVMain(PluginServer *server)
 
 MotionCVMain::~MotionCVMain()
 {
-
        delete engine;
        delete overlayer;
-       delete [] search_area;
+       delete[]search_area;
        delete temp_frame;
        delete rotate_engine;
        delete motion_rotate;
@@ -204,7 +198,7 @@ MotionCVMain::~MotionCVMain()
        delete global_target_src;
        delete global_target_dst;
 
-       if( active_fp ) fclose(active_fp); 
+       reset_cache_file();
 
        delete prev_rotate_ref;
        delete current_rotate_ref;
@@ -212,72 +206,55 @@ MotionCVMain::~MotionCVMain()
        delete rotate_target_dst;
 }
 
-const charMotionCVMain::plugin_title() { return _("MotionCV"); }
+const char *MotionCVMain::plugin_title() { return _("MotionCV"); }
 int MotionCVMain::is_realtime() { return 1; }
 int MotionCVMain::is_multichannel() { return 1; }
 
-
 NEW_WINDOW_MACRO(MotionCVMain, MotionCVWindow)
 
 LOAD_CONFIGURATION_MACRO(MotionCVMain, MotionCVConfig)
 
-
-
 void MotionCVMain::update_gui()
 {
-       if(thread)
-       {
-               if(load_configuration())
-               {
-                       thread->window->lock_window("MotionCVMain::update_gui");
-                       MotionCVWindow *window = (MotionCVWindow *)thread->window;
-                       
-                       char string[BCTEXTLEN];
-                       sprintf(string, "%d", config.global_positions);
-                       window->global_search_positions->set_text(string);
-                       sprintf(string, "%d", config.rotate_positions);
-                       window->rotation_search_positions->set_text(string);
-
-                       window->global_block_w->update(config.global_block_w);
-                       window->global_block_h->update(config.global_block_h);
-                       window->rotation_block_w->update(config.rotation_block_w);
-                       window->rotation_block_h->update(config.rotation_block_h);
-                       window->block_x->update(config.block_x);
-                       window->block_y->update(config.block_y);
-                       window->block_x_text->update((float)config.block_x);
-                       window->block_y_text->update((float)config.block_y);
-                       window->magnitude->update(config.magnitude);
-                       window->return_speed->update(config.return_speed);
-
-
-                       window->track_single->update(config.mode3 == MotionCVConfig::TRACK_SINGLE);
-                       window->track_frame_number->update(config.track_frame);
-                       window->track_previous->update(config.mode3 == MotionCVConfig::TRACK_PREVIOUS);
-                       window->previous_same->update(config.mode3 == MotionCVConfig::PREVIOUS_SAME_BLOCK);
-                       if(config.mode3 != MotionCVConfig::TRACK_SINGLE)
-                               window->track_frame_number->disable();
-                       else
-                               window->track_frame_number->enable();
-
-                       window->mode1->set_text(
-                               Mode1::to_text(config.mode1));
-                       window->mode2->set_text(
-                               Mode2::to_text(config.mode2));
-                       window->mode3->set_text(
-                               Mode3::to_text(config.horizontal_only, config.vertical_only));
-                       window->master_layer->set_text(
-                               MasterLayer::to_text(config.bottom_is_master));
-
-
-                       window->update_mode();
-                       window->unlock_window();
-               }
-       }
+       if( thread ) return;
+       if( !load_configuration() ) return;
+       thread->window->lock_window("MotionCVMain::update_gui");
+       MotionCVWindow *window = (MotionCVWindow *) thread->window;
+
+       char string[BCTEXTLEN];
+       sprintf(string, "%d", config.global_positions);
+       window->global_search_positions->set_text(string);
+       sprintf(string, "%d", config.rotate_positions);
+       window->rotation_search_positions->set_text(string);
+
+       window->global_block_w->update(config.global_block_w);
+       window->global_block_h->update(config.global_block_h);
+       window->rotation_block_w->update(config.rotation_block_w);
+       window->rotation_block_h->update(config.rotation_block_h);
+       window->block_x->update(config.block_x);
+       window->block_y->update(config.block_y);
+       window->block_x_text->update((float)config.block_x);
+       window->block_y_text->update((float)config.block_y);
+       window->magnitude->update(config.magnitude);
+       window->return_speed->update(config.return_speed);
+
+       window->track_single->update(config.mode3 == MotionCVConfig::TRACK_SINGLE);
+       window->track_frame_number->update(config.track_frame);
+       window->track_previous->update(config.mode3 == MotionCVConfig::TRACK_PREVIOUS);
+       window->previous_same->update(config.mode3 == MotionCVConfig::PREVIOUS_SAME_BLOCK);
+       if( config.mode3 != MotionCVConfig::TRACK_SINGLE )
+               window->track_frame_number->disable();
+       else
+               window->track_frame_number->enable();
+
+       window->mode1->set_text(Mode1::to_text(config.mode1));
+       window->mode2->set_text(Mode2::to_text(config.mode2));
+       window->mode3->set_text(Mode3::to_text(config.horizontal_only, config.vertical_only));
+       window->master_layer->set_text(MasterLayer::to_text(config.bottom_is_master));
+       window->update_mode();
+       window->unlock_window();
 }
 
-
-
-
 void MotionCVMain::save_data(KeyFrame *keyframe)
 {
        FileXML output;
@@ -321,307 +298,223 @@ void MotionCVMain::save_data(KeyFrame *keyframe)
 void MotionCVMain::read_data(KeyFrame *keyframe)
 {
        FileXML input;
-
        input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
-
        int result = 0;
 
-       while(!result)
-       {
-               result = input.read_tag();
-
-               if(!result)
-               {
-                       if(input.tag.title_is("MOTION"))
-                       {
-                               config.block_count = input.tag.get_property("BLOCK_COUNT", config.block_count);
-                               config.global_positions = input.tag.get_property("GLOBAL_POSITIONS", config.global_positions);
-                               config.rotate_positions = input.tag.get_property("ROTATE_POSITIONS", config.rotate_positions);
-                               config.global_block_w = input.tag.get_property("GLOBAL_BLOCK_W", config.global_block_w);
-                               config.global_block_h = input.tag.get_property("GLOBAL_BLOCK_H", config.global_block_h);
-                               config.rotation_block_w = input.tag.get_property("ROTATION_BLOCK_W", config.rotation_block_w);
-                               config.rotation_block_h = input.tag.get_property("ROTATION_BLOCK_H", config.rotation_block_h);
-                               config.block_x = input.tag.get_property("BLOCK_X", config.block_x);
-                               config.block_y = input.tag.get_property("BLOCK_Y", config.block_y);
-                               config.global_range_w = input.tag.get_property("GLOBAL_RANGE_W", config.global_range_w);
-                               config.global_range_h = input.tag.get_property("GLOBAL_RANGE_H", config.global_range_h);
-                               config.rotation_range = input.tag.get_property("ROTATION_RANGE", config.rotation_range);
-                               config.magnitude = input.tag.get_property("MAGNITUDE", config.magnitude);
-                               config.return_speed = input.tag.get_property("RETURN_SPEED", config.return_speed);
-                               config.mode1 = input.tag.get_property("MODE1", config.mode1);
-                               config.global = input.tag.get_property("GLOBAL", config.global);
-                               config.rotate = input.tag.get_property("ROTATE", config.rotate);
-                               config.addtrackedframeoffset = input.tag.get_property("ADDTRACKEDFRAMEOFFSET", config.addtrackedframeoffset);
-                               input.tag.get_property("TRACKING_FILE", config.tracking_file);
-                               config.mode2 = input.tag.get_property("MODE2", config.mode2);
-                               config.draw_vectors = input.tag.get_property("DRAW_VECTORS", config.draw_vectors);
-                               config.mode3 = input.tag.get_property("MODE3", config.mode3);
-                               config.track_frame = input.tag.get_property("TRACK_FRAME", config.track_frame);
-                               config.bottom_is_master = input.tag.get_property("BOTTOM_IS_MASTER", config.bottom_is_master);
-                               config.horizontal_only = input.tag.get_property("HORIZONTAL_ONLY", config.horizontal_only);
-                               config.vertical_only = input.tag.get_property("VERTICAL_ONLY", config.vertical_only);
-                       }
+       while( !(result = input.read_tag()) ) {
+               if( input.tag.title_is("MOTION") ) {
+                       config.block_count = input.tag.get_property("BLOCK_COUNT", config.block_count);
+                       config.global_positions = input.tag.get_property("GLOBAL_POSITIONS", config.global_positions);
+                       config.rotate_positions = input.tag.get_property("ROTATE_POSITIONS", config.rotate_positions);
+                       config.global_block_w = input.tag.get_property("GLOBAL_BLOCK_W", config.global_block_w);
+                       config.global_block_h = input.tag.get_property("GLOBAL_BLOCK_H", config.global_block_h);
+                       config.rotation_block_w = input.tag.get_property("ROTATION_BLOCK_W", config.rotation_block_w);
+                       config.rotation_block_h = input.tag.get_property("ROTATION_BLOCK_H", config.rotation_block_h);
+                       config.block_x = input.tag.get_property("BLOCK_X", config.block_x);
+                       config.block_y = input.tag.get_property("BLOCK_Y", config.block_y);
+                       config.global_range_w = input.tag.get_property("GLOBAL_RANGE_W", config.global_range_w);
+                       config.global_range_h = input.tag.get_property("GLOBAL_RANGE_H", config.global_range_h);
+                       config.rotation_range = input.tag.get_property("ROTATION_RANGE", config.rotation_range);
+                       config.magnitude = input.tag.get_property("MAGNITUDE", config.magnitude);
+                       config.return_speed = input.tag.get_property("RETURN_SPEED", config.return_speed);
+                       config.mode1 = input.tag.get_property("MODE1", config.mode1);
+                       config.global = input.tag.get_property("GLOBAL", config.global);
+                       config.rotate = input.tag.get_property("ROTATE", config.rotate);
+                       config.addtrackedframeoffset = input.tag.get_property("ADDTRACKEDFRAMEOFFSET", config.addtrackedframeoffset);
+                       input.tag.get_property("TRACKING_FILE", config.tracking_file);
+                       config.mode2 = input.tag.get_property("MODE2", config.mode2);
+                       config.draw_vectors = input.tag.get_property("DRAW_VECTORS", config.draw_vectors);
+                       config.mode3 = input.tag.get_property("MODE3", config.mode3);
+                       config.track_frame = input.tag.get_property("TRACK_FRAME", config.track_frame);
+                       config.bottom_is_master = input.tag.get_property("BOTTOM_IS_MASTER", config.bottom_is_master);
+                       config.horizontal_only = input.tag.get_property("HORIZONTAL_ONLY", config.horizontal_only);
+                       config.vertical_only = input.tag.get_property("VERTICAL_ONLY", config.vertical_only);
                }
        }
        config.boundaries();
 }
 
-
-
-
-
-
-
-
-
 void MotionCVMain::allocate_temp(int w, int h, int color_model)
 {
-       if(temp_frame && 
-               (temp_frame->get_w() != w ||
-               temp_frame->get_h() != h))
-       {
+       if( temp_frame &&
+           ( temp_frame->get_w() != w || temp_frame->get_h() != h ) ) {
                delete temp_frame;
                temp_frame = 0;
        }
-       if(!temp_frame)
+       if( !temp_frame )
                temp_frame = new VFrame(w, h, color_model);
 }
 
-
-
 void MotionCVMain::process_global()
 {
-       if(!engine) engine = new MotionCVScan(this,
-               PluginClient::get_project_smp() + 1,
-               PluginClient::get_project_smp() + 1);
+       if( !engine )
+               engine = new MotionCVScan(this,
+                                 PluginClient::get_project_smp() + 1,
+                                 PluginClient::get_project_smp() + 1);
 
 // Get the current motion vector between the previous and current frame
        engine->scan_frame(current_global_ref, prev_global_ref);
-       current_dx = engine->dx_result;
-       current_dy = engine->dy_result;
+       current_dx = (engine->dx_result += dx_offset);
+       current_dy = (engine->dy_result += dy_offset);
+
+// Write results
+       if( config.mode2 == MotionCVConfig::SAVE ) {
+               save_dx = engine->dx_result;
+               save_dy = engine->dy_result;
+       }
 
 // Add current motion vector to accumulation vector.
-       if(config.mode3 != MotionCVConfig::TRACK_SINGLE)
-       {
+       if( config.mode3 != MotionCVConfig::TRACK_SINGLE ) {
 // Retract over time
-               total_dx = (int64_t)total_dx * (100 - config.return_speed) / 100;
-               total_dy = (int64_t)total_dy * (100 - config.return_speed) / 100;
+               total_dx = (int64_t) total_dx *(100 - config.return_speed) / 100;
+               total_dy = (int64_t) total_dy *(100 - config.return_speed) / 100;
                total_dx += engine->dx_result;
                total_dy += engine->dy_result;
        }
-       else
 // Make accumulation vector current
-       {
+       else {
                total_dx = engine->dx_result;
                total_dy = engine->dy_result;
        }
 
 // Clamp accumulation vector
-       if(config.magnitude < 100)
-       {
-               //int block_w = (int64_t)config.global_block_w * 
-               //              current_global_ref->get_w() / 100;
-               //int block_h = (int64_t)config.global_block_h * 
-               //              current_global_ref->get_h() / 100;
-               int block_x_orig = (int64_t)(config.block_x * 
-                       current_global_ref->get_w() / 
-                       100);
-               int block_y_orig = (int64_t)(config.block_y *
-                       current_global_ref->get_h() / 
-                       100);
-
-               int max_block_x = (int64_t)(current_global_ref->get_w() - block_x_orig) *
-                       OVERSAMPLE * 
-                       config.magnitude / 
-                       100;
-               int max_block_y = (int64_t)(current_global_ref->get_h() - block_y_orig) *
-                       OVERSAMPLE *
-                       config.magnitude / 
-                       100;
-               int min_block_x = (int64_t)-block_x_orig * 
-                       OVERSAMPLE * 
-                       config.magnitude / 
-                       100;
-               int min_block_y = (int64_t)-block_y_orig * 
-                       OVERSAMPLE * 
-                       config.magnitude / 
-                       100;
+       if( config.magnitude < 100 ) {
+               int block_x_orig = (int64_t)(config.block_x * current_global_ref->get_w() / 100);
+               int block_y_orig = (int64_t)(config.block_y * current_global_ref->get_h() / 100);
+
+               int max_block_x = (int64_t) (current_global_ref->get_w() - block_x_orig)
+                       * OVERSAMPLE * config.magnitude / 100;
+               int max_block_y = (int64_t) (current_global_ref->get_h() - block_y_orig)
+                       * OVERSAMPLE * config.magnitude / 100;
+               int min_block_x = (int64_t)
+                       -block_x_orig * OVERSAMPLE * config.magnitude / 100;
+               int min_block_y = (int64_t)
+                       -block_y_orig * OVERSAMPLE * config.magnitude / 100;
 
                CLAMP(total_dx, min_block_x, max_block_x);
                CLAMP(total_dy, min_block_y, max_block_y);
        }
-
 #ifdef DEBUG
-printf("MotionCVMain::process_global 2 total_dx=%.02f total_dy=%.02f\n", 
-(float)total_dx / OVERSAMPLE,
-(float)total_dy / OVERSAMPLE);
+printf("MotionCVMain::process_global 2 total_dx=%.02f total_dy=%.02f\n",
+ (float)total_dx / OVERSAMPLE, (float)total_dy / OVERSAMPLE);
 #endif
 
-       if(config.mode3 != MotionCVConfig::TRACK_SINGLE && !config.rotate)
-       {
+       if( config.mode3 != MotionCVConfig::TRACK_SINGLE && !config.rotate ) {
 // Transfer current reference frame to previous reference frame and update
 // counter.  Must wait for rotate to compare.
                prev_global_ref->copy_from(current_global_ref);
                previous_frame_number = get_source_position();
        }
-
 // Decide what to do with target based on requested operation
        int interpolation = NEAREST_NEIGHBOR;
-       float dx = 0;
-       float dy = 0;
-       switch(config.mode1)
-       {
-               case MotionCVConfig::NOTHING:
-                       global_target_dst->copy_from(global_target_src);
-                       break;
-               case MotionCVConfig::TRACK_PIXEL:
-                       interpolation = NEAREST_NEIGHBOR;
-                       dx = (int)(total_dx / OVERSAMPLE);
-                       dy = (int)(total_dy / OVERSAMPLE);
-                       break;
-               case MotionCVConfig::STABILIZE_PIXEL:
-                       interpolation = NEAREST_NEIGHBOR;
-                       dx = -(int)(total_dx / OVERSAMPLE);
-                       dy = -(int)(total_dy / OVERSAMPLE);
-                       break;
-                       break;
-               case MotionCVConfig::TRACK:
-                       interpolation = CUBIC_LINEAR;
-                       dx = (float)total_dx / OVERSAMPLE;
-                       dy = (float)total_dy / OVERSAMPLE;
-                       break;
-               case MotionCVConfig::STABILIZE:
-                       interpolation = CUBIC_LINEAR;
-                       dx = -(float)total_dx / OVERSAMPLE;
-                       dy = -(float)total_dy / OVERSAMPLE;
-                       break;
+       float dx = 0, dy = 0;
+       switch( config.mode1 ) {
+       case MotionCVConfig::NOTHING:
+               global_target_dst->copy_from(global_target_src);
+               break;
+       case MotionCVConfig::TRACK_PIXEL:
+               interpolation = NEAREST_NEIGHBOR;
+               dx = (int)(total_dx / OVERSAMPLE);
+               dy = (int)(total_dy / OVERSAMPLE);
+               break;
+       case MotionCVConfig::STABILIZE_PIXEL:
+               interpolation = NEAREST_NEIGHBOR;
+               dx = -(int)(total_dx / OVERSAMPLE);
+               dy = -(int)(total_dy / OVERSAMPLE);
+               break;
+               break;
+       case MotionCVConfig::TRACK:
+               interpolation = CUBIC_LINEAR;
+               dx = (float)total_dx / OVERSAMPLE;
+               dy = (float)total_dy / OVERSAMPLE;
+               break;
+       case MotionCVConfig::STABILIZE:
+               interpolation = CUBIC_LINEAR;
+               dx = -(float)total_dx / OVERSAMPLE;
+               dy = -(float)total_dy / OVERSAMPLE;
+               break;
        }
 
-
-       if(config.mode1 != MotionCVConfig::NOTHING)
-       {
-               if(!overlayer) 
+       if( config.mode1 != MotionCVConfig::NOTHING ) {
+               if( !overlayer )
                        overlayer = new OverlayFrame(PluginClient::get_project_smp() + 1);
                global_target_dst->clear_frame();
-               overlayer->overlay(global_target_dst,
-                       global_target_src,
-                       0,
-                       0,
-                       global_target_src->get_w(),
-                       global_target_src->get_h(),
-                       dx,
-                       dy,
-                       (float)global_target_src->get_w() + dx,
-                       (float)global_target_src->get_h() + dy,
-                       1,
-                       TRANSFER_REPLACE,
-                       interpolation);
+               overlayer->overlay(global_target_dst, global_target_src, 0, 0,
+                                  global_target_src->get_w(), global_target_src->get_h(),
+                                  dx, dy,
+                                  (float)global_target_src->get_w() + dx,
+                                  (float)global_target_src->get_h() + dy,
+                                  1, TRANSFER_REPLACE, interpolation);
        }
 }
 
-
-
 void MotionCVMain::process_rotation()
 {
-       int block_x;
-       int block_y;
+       int block_x, block_y;
 
 // Convert the previous global reference into the previous rotation reference.
 // Convert global target destination into rotation target source.
-       if(config.global)
-       {
-               if(!overlayer) 
+       if( config.global ) {
+               if( !overlayer )
                        overlayer = new OverlayFrame(PluginClient::get_project_smp() + 1);
-               float dx;
-               float dy;
-               if(config.mode3 == MotionCVConfig::TRACK_SINGLE)
-               {
+               float dx, dy;
+               if( config.mode3 == MotionCVConfig::TRACK_SINGLE ) {
                        dx = (float)total_dx / OVERSAMPLE;
                        dy = (float)total_dy / OVERSAMPLE;
                }
-               else
-               {
+               else {
                        dx = (float)current_dx / OVERSAMPLE;
                        dy = (float)current_dy / OVERSAMPLE;
                }
 
                prev_rotate_ref->clear_frame();
-               overlayer->overlay(prev_rotate_ref,
-                       prev_global_ref,
-                       0,
-                       0,
-                       prev_global_ref->get_w(),
-                       prev_global_ref->get_h(),
-                       dx,
-                       dy,
+               overlayer->overlay(prev_rotate_ref, prev_global_ref,
+                       0, 0, prev_global_ref->get_w(), prev_global_ref->get_h(),
+                       dx, dy,
                        (float)prev_global_ref->get_w() + dx,
                        (float)prev_global_ref->get_h() + dy,
-                       1,
-                       TRANSFER_REPLACE,
-                       CUBIC_LINEAR);
+                       1, TRANSFER_REPLACE, CUBIC_LINEAR);
 // Pivot is destination global position
-               block_x = (int)(prev_rotate_ref->get_w() * 
-                       config.block_x / 
-                       100 +
-                       (float)total_dx / 
-                       OVERSAMPLE);
-               block_y = (int)(prev_rotate_ref->get_h() * 
-                       config.block_y / 
-                       100 +
-                       (float)total_dy / 
-                       OVERSAMPLE);
+               block_x = (int)(prev_rotate_ref->get_w() *
+                               config.block_x / 100 + (float)total_dx / OVERSAMPLE);
+               block_y = (int)(prev_rotate_ref->get_h() *
+                               config.block_y / 100 + (float)total_dy / OVERSAMPLE);
 // Use the global target output as the rotation target input
                rotate_target_src->copy_from(global_target_dst);
 // Transfer current reference frame to previous reference frame for global.
-               if(config.mode3 != MotionCVConfig::TRACK_SINGLE)
-               {
+               if( config.mode3 != MotionCVConfig::TRACK_SINGLE ) {
                        prev_global_ref->copy_from(current_global_ref);
                        previous_frame_number = get_source_position();
                }
        }
-       else
-       {
 // Pivot is fixed
-               block_x = (int)(prev_rotate_ref->get_w() * 
-                       config.block_x / 
-                       100);
-               block_y = (int)(prev_rotate_ref->get_h() * 
-                       config.block_y / 
-                       100);
+       else {
+               block_x = (int)(prev_rotate_ref->get_w() * config.block_x / 100);
+               block_y = (int)(prev_rotate_ref->get_h() * config.block_y / 100);
        }
 
-
-
 // Get rotation
-       if(!motion_rotate)
-               motion_rotate = new RotateCVScan(this, 
-                       get_project_smp() + 1, 
-                       get_project_smp() + 1);
-
-       current_angle = motion_rotate->scan_frame(prev_rotate_ref, 
-               current_rotate_ref,
-               block_x,
-               block_y);
-
+       if( !motion_rotate )
+               motion_rotate = new RotateCVScan(this,
+                        get_project_smp() + 1, get_project_smp() + 1);
 
+       current_angle = motion_rotate->scan_frame(prev_rotate_ref, current_rotate_ref,
+                 block_x, block_y);
 
 // Add current rotation to accumulation
-       if(config.mode3 != MotionCVConfig::TRACK_SINGLE)
-       {
+       if( config.mode3 != MotionCVConfig::TRACK_SINGLE ) {
 // Retract over time
                total_angle = total_angle * (100 - config.return_speed) / 100;
                total_angle += current_angle;
 
-               if(!config.global)
-               {
+               if( !config.global ) {
 // Transfer current reference frame to previous reference frame and update
 // counter.
                        prev_rotate_ref->copy_from(current_rotate_ref);
                        previous_frame_number = get_source_position();
                }
        }
-       else
-       {
+       else {
                total_angle = current_angle;
        }
 
@@ -629,477 +522,350 @@ void MotionCVMain::process_rotation()
 printf("MotionCVMain::process_rotation total_angle=%f\n", total_angle);
 #endif
 
-
 // Calculate rotation parameters based on requested operation
        float angle = 0;
-       switch(config.mode1)
-       {
-               case MotionCVConfig::NOTHING:
-                       rotate_target_dst->copy_from(rotate_target_src);
-                       break;
-               case MotionCVConfig::TRACK:
-               case MotionCVConfig::TRACK_PIXEL:
-                       angle = total_angle;
-                       break;
-               case MotionCVConfig::STABILIZE:
-               case MotionCVConfig::STABILIZE_PIXEL:
-                       angle = -total_angle;
-                       break;
+       switch( config.mode1 ) {
+       case MotionCVConfig::NOTHING:
+               rotate_target_dst->copy_from(rotate_target_src);
+               break;
+       case MotionCVConfig::TRACK:
+       case MotionCVConfig::TRACK_PIXEL:
+               angle = total_angle;
+               break;
+       case MotionCVConfig::STABILIZE:
+       case MotionCVConfig::STABILIZE_PIXEL:
+               angle = -total_angle;
+               break;
        }
 
-
-
-       if(config.mode1 != MotionCVConfig::NOTHING)
-       {
-               if(!rotate_engine)
-                       rotate_engine = new AffineEngine(PluginClient::get_project_smp() + 1,
+       if( config.mode1 != MotionCVConfig::NOTHING ) {
+               if( !rotate_engine )
+                       rotate_engine = new AffineEngine(
+                               PluginClient::get_project_smp() + 1,
                                PluginClient::get_project_smp() + 1);
 
                rotate_target_dst->clear_frame();
 
 // Determine pivot based on a number of factors.
-               switch(config.mode1)
-               {
-                       case MotionCVConfig::TRACK:
-                       case MotionCVConfig::TRACK_PIXEL:
+               switch( config.mode1 ) {
+               case MotionCVConfig::TRACK:
+               case MotionCVConfig::TRACK_PIXEL:
 // Use destination of global tracking.
-                               rotate_engine->set_pivot(block_x, block_y);
-                               break;
+                       rotate_engine->set_pivot(block_x, block_y);
+                       break;
 
-                       case MotionCVConfig::STABILIZE:
-                       case MotionCVConfig::STABILIZE_PIXEL:
-                               if(config.global)
-                               {
+               case MotionCVConfig::STABILIZE:
+               case MotionCVConfig::STABILIZE_PIXEL:
+                       if( config.global ) {
 // Use origin of global stabilize operation
-                                       rotate_engine->set_pivot((int)(rotate_target_dst->get_w() * 
-                                                       config.block_x / 
-                                                       100),
-                                               (int)(rotate_target_dst->get_h() * 
-                                                       config.block_y / 
-                                                       100));
-                               
-                               }
-                               else
-                               {
+                               rotate_engine->set_pivot(
+                                       (int)(rotate_target_dst->get_w() * config.block_x / 100),
+                                       (int)(rotate_target_dst->get_h() * config.block_y / 100));
+                       }
 // Use origin
-                                       rotate_engine->set_pivot(block_x, block_y);
-                               }
-                               break;
+                       else {
+                               rotate_engine->set_pivot(block_x, block_y);
+                       }
+                       break;
                }
 
-
                rotate_engine->rotate(rotate_target_dst, rotate_target_src, angle);
-// overlayer->overlay(rotate_target_dst,
-//     prev_rotate_ref,
-//     0,
-//     0,
-//     prev_rotate_ref->get_w(),
-//     prev_rotate_ref->get_h(),
-//     0,
-//     0,
-//     prev_rotate_ref->get_w(),
-//     prev_rotate_ref->get_h(),
-//     1,
-//     TRANSFER_NORMAL,
-//     CUBIC_LINEAR);
-// overlayer->overlay(rotate_target_dst,
-//     current_rotate_ref,
-//     0,
-//     0,
-//     prev_rotate_ref->get_w(),
-//     prev_rotate_ref->get_h(),
-//     0,
-//     0,
-//     prev_rotate_ref->get_w(),
-//     prev_rotate_ref->get_h(),
-//     1,
-//     TRANSFER_NORMAL,
-//     CUBIC_LINEAR);
-
-
+// overlayer->overlay(rotate_target_dst, prev_rotate_ref,
+//  0, 0, prev_rotate_ref->get_w(), prev_rotate_ref->get_h(),
+//  0, 0, prev_rotate_ref->get_w(), prev_rotate_ref->get_h(),
+//  1, TRANSFER_NORMAL, CUBIC_LINEAR);
+// overlayer->overlay(rotate_target_dst, current_rotate_ref,
+//  0, 0, prev_rotate_ref->get_w(), prev_rotate_ref->get_h(),
+//  0, 0, prev_rotate_ref->get_w(), prev_rotate_ref->get_h(),
+//  1, TRANSFER_NORMAL, CUBIC_LINEAR);
        }
-
-
 }
 
-
-
-
-
-
-
-
-
-int MotionCVMain::process_buffer(VFrame **frame,
-       int64_t start_position,
-       double frame_rate)
+int MotionCVMain::process_buffer(VFrame ** frame,
+                                int64_t start_position, double frame_rate)
 {
        int prev_config_mode2 = config.mode2;
        int need_reconfigure = load_configuration();
        int color_model = frame[0]->get_color_model();
        w = frame[0]->get_w();
        h = frame[0]->get_h();
-       
 
 #ifdef DEBUG
 printf("MotionCVMain::process_buffer 1 start_position=%jd\n", start_position);
 #endif
 
-
 // Calculate the source and destination pointers for each of the operations.
 // Get the layer to track motion in.
        reference_layer = config.bottom_is_master ?
-               PluginClient::total_in_buffers - 1 :
-               0;
+           PluginClient::total_in_buffers - 1 : 0;
 // Get the layer to apply motion in.
        target_layer = config.bottom_is_master ?
-               0 :
-               PluginClient::total_in_buffers - 1;
-
+           0 : PluginClient::total_in_buffers - 1;
 
        output_frame = frame[target_layer];
 
-
 // Get the position of previous reference frame.
        int64_t actual_previous_number;
 // Skip if match frame not available
        int skip_current = 0;
 
-
-       if(config.mode3 == MotionCVConfig::TRACK_SINGLE)
-       {
+       if( config.mode3 == MotionCVConfig::TRACK_SINGLE ) {
                actual_previous_number = config.track_frame;
-               if(get_direction() == PLAY_REVERSE)
+               if( get_direction() == PLAY_REVERSE )
                        actual_previous_number++;
-               if(actual_previous_number == start_position)
+               if( actual_previous_number == start_position )
                        skip_current = 1;
        }
-       else
-       {
+       else {
                actual_previous_number = start_position;
-               if(get_direction() == PLAY_FORWARD)
-               {
+               if( get_direction() == PLAY_FORWARD ) {
                        actual_previous_number--;
-                       if(actual_previous_number < get_source_start())
+                       if( actual_previous_number < get_source_start() )
                                skip_current = 1;
-                       else
-                       {
+                       else {
                                KeyFrame *keyframe = get_prev_keyframe(start_position, 1);
-                               if(keyframe->position > 0 &&
-                                       actual_previous_number < keyframe->position)
+                               if( keyframe->position > 0 &&
+                                   actual_previous_number < keyframe->position )
                                        skip_current = 1;
                        }
                }
-               else
-               {
+               else {
                        actual_previous_number++;
-                       if(actual_previous_number >= get_source_start() + get_total_len())
+                       if( actual_previous_number >= get_source_start() + get_total_len() )
                                skip_current = 1;
-                       else
-                       {
+                       else {
                                KeyFrame *keyframe = get_next_keyframe(start_position, 1);
-                               if(keyframe->position > 0 &&
-                                       actual_previous_number >= keyframe->position)
+                               if( keyframe->position > 0 &&
+                                   actual_previous_number >= keyframe->position )
                                        skip_current = 1;
                        }
                }
-
 // Only count motion since last keyframe
-               
-
        }
 
+       if( !config.global &&!config.rotate )
+               skip_current = 1;
 
-       if(!config.global && !config.rotate) skip_current = 1;
-
-
-
-
-// printf("process_realtime %d %lld %lld\n", 
-// skip_current, 
-// previous_frame_number, 
-// actual_previous_number);
-// Load match frame and reset vectors
-       int need_reload = !skip_current && 
-               (previous_frame_number != actual_previous_number ||
-               need_reconfigure);
-       if(need_reload)
-       {
-               total_dx = 0;
-               total_dy = 0;
-               total_angle = 0;
-               previous_frame_number = actual_previous_number;
-       }
+//printf("process_realtime: %jd %d %jd %jd\n", start_position,
+// skip_current, previous_frame_number, actual_previous_number);
        if( prev_config_mode2 != MotionCVConfig::SAVE &&
            config.mode2 == MotionCVConfig::SAVE ) {
+               reset_cache_file();
+               char save_file[BCTEXTLEN];
+               sprintf(save_file, "%s.sav", config.tracking_file);
 #ifdef DEBUG
-printf("MotionCVMain::process_buffer 2 remove tracking file: %s\n", config.tracking_file);
+printf("MotionCVMain::process_buffer 2 rename tracking file: %s to %s\n",
+ config.tracking_file, save_file);
 #endif
-               ::remove(config.tracking_file);
+               ::rename(config.tracking_file, save_file);
        }
+       else if( !cache_file[0] || active_key > start_position )
+               reset_cache_file();
 
-
-       if(skip_current)
-       {
-               total_dx = 0;
-               total_dy = 0;
-               current_dx = 0;
-               current_dy = 0;
-               total_angle = 0;
-               current_angle = 0;
+// Load match frame and reset vectors
+       int need_reload = !skip_current &&
+           (previous_frame_number != actual_previous_number ||
+            need_reconfigure);
+       if( need_reload ) {
+               total_dx = total_dy = 0;  total_angle = 0;
+               previous_frame_number = actual_previous_number;
        }
 
-
-
+       if( skip_current ) {
+               total_dx = total_dy = 0;
+               current_dx = current_dy = 0;
+               total_angle = current_angle = 0;
+       }
 
 // Get the global pointers.  Here we walk through the sequence of events.
-       if(config.global)
-       {
+       if( config.global ) {
 // Assume global only.  Global reads previous frame and compares
 // with current frame to get the current translation.
 // The center of the search area is fixed in compensate mode or
 // the user value + the accumulation vector in track mode.
-               if(!prev_global_ref)
+               if( !prev_global_ref )
                        prev_global_ref = new VFrame(w, h, color_model);
-               if(!current_global_ref)
+               if( !current_global_ref )
                        current_global_ref = new VFrame(w, h, color_model);
 
-// Global loads the current target frame into the src and 
+// Global loads the current target frame into the src and
 // writes it to the dst frame with desired translation.
-               if(!global_target_src)
+               if( !global_target_src )
                        global_target_src = new VFrame(w, h, color_model);
-               if(!global_target_dst)
+               if( !global_target_dst )
                        global_target_dst = new VFrame(w, h, color_model);
 
-
 // Load the global frames
-               if(need_reload)
-               {
-                       read_frame(prev_global_ref, 
-                               reference_layer, 
-                               previous_frame_number, 
-                               frame_rate,
-                               0);
+               if( need_reload ) {
+                       read_frame(prev_global_ref, reference_layer,
+                               previous_frame_number, frame_rate, 0);
                }
 
-               read_frame(current_global_ref, 
-                       reference_layer, 
-                       start_position, 
-                       frame_rate,
-                       0);
-               read_frame(global_target_src,
-                       target_layer,
-                       start_position,
-                       frame_rate,
-                       0);
-
-
+               read_frame(current_global_ref, reference_layer,
+                               start_position, frame_rate, 0);
+               read_frame(global_target_src, target_layer,
+                               start_position, frame_rate, 0);
 
 // Global followed by rotate
-               if(config.rotate)
-               {
+               if( config.rotate ) {
 // Must translate the previous global reference by the current global
 // accumulation vector to match the current global reference.
 // The center of the search area is always the user value + the accumulation
 // vector.
-                       if(!prev_rotate_ref)
+                       if( !prev_rotate_ref )
                                prev_rotate_ref = new VFrame(w, h, color_model);
 // The current global reference is the current rotation reference.
-                       if(!current_rotate_ref)
+                       if( !current_rotate_ref )
                                current_rotate_ref = new VFrame(w, h, color_model);
                        current_rotate_ref->copy_from(current_global_ref);
 
 // The global target destination is copied to the rotation target source
 // then written to the rotation output with rotation.
-// The pivot for the rotation is the center of the search area 
+// The pivot for the rotation is the center of the search area
 // if we're tracking.
 // The pivot is fixed to the user position if we're compensating.
-                       if(!rotate_target_src)
+                       if( !rotate_target_src )
                                rotate_target_src = new VFrame(w, h, color_model);
-                       if(!rotate_target_dst)
-                               rotate_target_dst = new VFrame(w,, color_model);
+                       if( !rotate_target_dst )
+                               rotate_target_dst = new VFrame(w, h, color_model);
                }
        }
-       else
 // Rotation only
-       if(config.rotate)
-       {
-// Rotation reads the previous reference frame and compares it with current 
+       else if( config.rotate ) {
+// Rotation reads the previous reference frame and compares it with current
 // reference frame.
-               if(!prev_rotate_ref)
+               if( !prev_rotate_ref )
                        prev_rotate_ref = new VFrame(w, h, color_model);
-               if(!current_rotate_ref)
+               if( !current_rotate_ref )
                        current_rotate_ref = new VFrame(w, h, color_model);
 
 // Rotation loads target frame to temporary, rotates it, and writes it to the
 // target frame.  The pivot is always fixed.
-               if(!rotate_target_src)
+               if( !rotate_target_src )
                        rotate_target_src = new VFrame(w, h, color_model);
-               if(!rotate_target_dst)
-                       rotate_target_dst = new VFrame(w,h , color_model);
-
+               if( !rotate_target_dst )
+                       rotate_target_dst = new VFrame(w, h, color_model);
 
 // Load the rotate frames
-               if(need_reload)
-               {
-                       read_frame(prev_rotate_ref, 
-                               reference_layer, 
-                               previous_frame_number, 
-                               frame_rate,
-                               0);
+               if( need_reload ) {
+                       read_frame(prev_rotate_ref,
+                                  reference_layer,
+                                  previous_frame_number, frame_rate, 0);
                }
-               read_frame(current_rotate_ref, 
-                       reference_layer, 
-                       start_position, 
-                       frame_rate,
-                       0);
+               read_frame(current_rotate_ref,
+                          reference_layer, start_position, frame_rate, 0);
                read_frame(rotate_target_src,
-                       target_layer,
-                       start_position,
-                       frame_rate,
-                       0);
+                          target_layer, start_position, frame_rate, 0);
        }
 
-       if(!skip_current)
-       {
-
-               if( config.mode2 == MotionCVConfig::LOAD ) {
-                       char line[BCTEXTLEN];
-                       int64_t frame_no, no;  int dx, dy;  float dt;
-                       if( config.addtrackedframeoffset && config.track_frame != tracking_frame ) {
-                               tracking_frame = frame_no = config.track_frame;
-                               if( !get_line_key(config.tracking_file, frame_no, line, sizeof(line)) &&
-                                   sscanf(line, "%jd %d %d %f", &no, &dx, &dy, &dt) == 4 ) {
-                                       dx_offset = dx;  dy_offset = dy;
+       dx_offset = 0; dy_offset = 0;
+       if( config.mode2 == MotionCVConfig::LOAD ) {
+               if( config.addtrackedframeoffset ) {
+                       if( config.track_frame != tracking_frame ) {
+                               tracking_frame = config.track_frame;
+                               int64_t no;  int dx, dy;  float dt;
+                               if( !get_cache_line(tracking_frame) &&
+                                   sscanf(cache_line, "%jd %d %d %f", &no, &dx, &dy, &dt) == 4 ) {
+                                       dx_offset = dx; dy_offset = dy;
                                }
                                else {
-#ifdef DEBUG
-                                       printf("MotionCVMain::process_buffer: no offset data frame %jd\n", frame_no);
-#endif
-                                       dx_offset = 0;  dy_offset = 0;
+                                       eprintf("no offset data frame %jd\n", tracking_frame);
                                }
                        }
+               }
+               else
+                       tracking_frame = -1;
+       }
+
+       if( !skip_current ) {
+               load_ok = 0;
+               if( config.mode2 == MotionCVConfig::LOAD ||
+                   config.mode2 == MotionCVConfig::SAVE ) {
+                       int64_t no;  int dx, dy;  float dt;
+                       int64_t frame_no = get_source_position();
 // Load result from disk
-                       load_ok= 0;
-                       frame_no = get_source_position();
-                       if( !get_line_key(config.tracking_file, frame_no, line, sizeof(line)) &&
-                           sscanf(line, "%jd %d %d %f", &frame_no, &dx, &dy, &dt) == 4 ) {
-                               load_ok= 1;  load_dx = dx;  load_dy = dy;  load_dt = dt;
+                       if( !get_cache_line(frame_no) &&
+                           sscanf(cache_line, "%jd %d %d %f", &no, &dx, &dy, &dt) == 4 ) {
+                               load_ok = 1; load_dx = dx; load_dy = dy; load_dt = dt;
                        }
                        else {
 #ifdef DEBUG
-                               printf("MotionCVMain::process_buffer: no tracking data frame %jd\n", frame_no);
+printf("MotionCVMain::process_buffer: no tracking data frame %jd\n", frame_no);
 #endif
                        }
                }
-
 // Get position change from previous frame to current frame
-               if(config.global) process_global();
+               if( config.global )
+                       process_global();
 // Get rotation change from previous frame to current frame
-               if(config.rotate) process_rotation();
+               if( config.rotate )
+                       process_rotation();
 //frame[target_layer]->copy_from(prev_rotate_ref);
 //frame[target_layer]->copy_from(current_rotate_ref);
 
 // write results to disk
                if( config.mode2 == MotionCVConfig::SAVE ) {
-                       FILE *output = fopen(config.tracking_file, "aw");
-                       if( output ) {
-                               int64_t frame_no = get_source_position();
-                               fprintf(output, "%jd %d %d %f\n", frame_no, save_dx, save_dy, save_dt);
-                               fclose(output);
-                       }
-                       else {
-                               perror("MotionCVMain::process buffer save");
-                       }
+                       char line[BCSTRLEN];
+                       int64_t frame_no = get_source_position();
+                       snprintf(line, sizeof(line), "%jd %d %d %f\n",
+                                frame_no, save_dx, save_dy, save_dt);
+                       put_cache_line(line);
                }
-       }
-
 
 // Transfer the relevant target frame to the output
-       if(!skip_current)
-       {
-               if(config.rotate)
-               {
+               if( config.rotate ) {
                        frame[target_layer]->copy_from(rotate_target_dst);
                }
-               else
-               {
+               else {
                        frame[target_layer]->copy_from(global_target_dst);
                }
        }
-       else
 // Read the target destination directly
-       {
+       else {
                read_frame(frame[target_layer],
-                       target_layer,
-                       start_position,
-                       frame_rate,
-                       0);
+                          target_layer, start_position, frame_rate, 0);
        }
 
-       if(config.draw_vectors)
-       {
+       if( config.draw_vectors ) {
                draw_vectors(frame[target_layer]);
        }
-
 #ifdef DEBUG
-printf("MotionCVMain::process_buffer 100\n");
+       printf("MotionCVMain::process_buffer 100\n");
 #endif
        return 0;
 }
 
-
-void MotionCVMain::clamp_scan(int w, 
-       int h, 
-       int *block_x1,
-       int *block_y1,
-       int *block_x2,
-       int *block_y2,
-       int *scan_x1,
-       int *scan_y1,
-       int *scan_x2,
-       int *scan_y2,
-       int use_absolute)
+void MotionCVMain::clamp_scan(int w, int h,
+               int *block_x1, int *block_y1, int *block_x2, int *block_y2,
+               int *scan_x1, int *scan_y1, int *scan_x2, int *scan_y2,
+               int use_absolute)
 {
-// printf("MotionCVMain::clamp_scan 1 w=%d h=%d block=%d %d %d %d scan=%d %d %d %d absolute=%d\n",
-// w,
-// h,
-// *block_x1,
-// *block_y1,
-// *block_x2,
-// *block_y2,
-// *scan_x1,
-// *scan_y1,
-// *scan_x2,
-// *scan_y2,
-// use_absolute);
-
-       if(use_absolute)
-       {
+// printf("MotionCVMain::clamp_scan 1 w=%d h=%d block=%d %d %d"
+// " %d scan=%d %d %d %d absolute=%d\n",
+// w, h, *block_x1, *block_y1, *block_x2, *block_y2,
+// *scan_x1, *scan_y1, *scan_x2, *scan_y2, use_absolute);
+
+       if( use_absolute ) {
 // scan is always out of range before block.
-               if(*scan_x1 < 0)
-               {
+               if( *scan_x1 < 0 ) {
                        int difference = -*scan_x1;
                        *block_x1 += difference;
                        *scan_x1 = 0;
                }
 
-               if(*scan_y1 < 0)
-               {
+               if( *scan_y1 < 0 ) {
                        int difference = -*scan_y1;
                        *block_y1 += difference;
                        *scan_y1 = 0;
                }
 
-               if(*scan_x2 > w)
-               {
+               if( *scan_x2 > w ) {
                        int difference = *scan_x2 - w;
                        *block_x2 -= difference;
                        *scan_x2 -= difference;
                }
 
-               if(*scan_y2 > h)
-               {
+               if( *scan_y2 > h ) {
                        int difference = *scan_y2 - h;
                        *block_y2 -= difference;
                        *scan_y2 -= difference;
@@ -1110,40 +876,34 @@ void MotionCVMain::clamp_scan(int w,
                CLAMP(*scan_x2, 0, w);
                CLAMP(*scan_y2, 0, h);
        }
-       else
-       {
-               if(*scan_x1 < 0)
-               {
+       else {
+               if( *scan_x1 < 0 ) {
                        int difference = -*scan_x1;
                        *block_x1 += difference;
                        *scan_x2 += difference;
                        *scan_x1 = 0;
                }
 
-               if(*scan_y1 < 0)
-               {
+               if( *scan_y1 < 0 ) {
                        int difference = -*scan_y1;
                        *block_y1 += difference;
                        *scan_y2 += difference;
                        *scan_y1 = 0;
                }
 
-               if(*scan_x2 - *block_x1 + *block_x2 > w)
-               {
+               if( *scan_x2 - *block_x1 + *block_x2 > w ) {
                        int difference = *scan_x2 - *block_x1 + *block_x2 - w;
                        *block_x2 -= difference;
                }
 
-               if(*scan_y2 - *block_y1 + *block_y2 > h)
-               {
+               if( *scan_y2 - *block_y1 + *block_y2 > h ) {
                        int difference = *scan_y2 - *block_y1 + *block_y2 - h;
                        *block_y2 -= difference;
                }
-
-//             CLAMP(*scan_x1, 0, w - (*block_x2 - *block_x1));
-//             CLAMP(*scan_y1, 0, h - (*block_y2 - *block_y1));
-//             CLAMP(*scan_x2, 0, w - (*block_x2 - *block_x1));
-//             CLAMP(*scan_y2, 0, h - (*block_y2 - *block_y1));
+//              CLAMP(*scan_x1, 0, w - (*block_x2 - *block_x1));
+//              CLAMP(*scan_y1, 0, h - (*block_y2 - *block_y1));
+//              CLAMP(*scan_x2, 0, w - (*block_x2 - *block_x1));
+//              CLAMP(*scan_y2, 0, h - (*block_y2 - *block_y1));
        }
 
 // Sanity checks which break the calculation but should never happen if the
@@ -1153,93 +913,51 @@ void MotionCVMain::clamp_scan(int w,
        CLAMP(*block_y1, 0, h);
        CLAMP(*block_y2, 0, h);
 
-// printf("MotionCVMain::clamp_scan 2 w=%d h=%d block=%d %d %d %d scan=%d %d %d %d absolute=%d\n",
-// w,
-// h,
-// *block_x1,
-// *block_y1,
-// *block_x2,
-// *block_y2,
-// *scan_x1,
-// *scan_y1,
-// *scan_x2,
-// *scan_y2,
-// use_absolute);
+// printf("MotionCVMain::clamp_scan 2 w=%d h=%d"
+// " block=%d %d %d %d scan=%d %d %d %d absolute=%d\n",
+// w, h, *block_x1, *block_y1, *block_x2, *block_y2,
+// *scan_x1, *scan_y1, *scan_x2, *scan_y2, use_absolute);
 }
 
-
-
 void MotionCVMain::draw_vectors(VFrame *frame)
 {
-       int w = frame->get_w();
-       int h = frame->get_h();
-       int global_x1, global_y1;
-       int global_x2, global_y2;
-       int block_x, block_y;
-       int block_w, block_h;
-       int block_x1, block_y1;
-       int block_x2, block_y2;
-       int block_x3, block_y3;
-       int block_x4, block_y4;
+       int w = frame->get_w(), h = frame->get_h();
+       int global_x1, global_y1, global_x2, global_y2;
+       int block_x, block_y, block_w, block_h;
+       int block_x1, block_y1, block_x2, block_y2;
+       int block_x3, block_y3, block_x4, block_y4;
+       int search_x1, search_y1, search_x2, search_y2;
        int search_w, search_h;
-       int search_x1, search_y1;
-       int search_x2, search_y2;
-       //int search_x3, search_y3;
-       //int search_x4, search_y4;
 
-       if(config.global)
-       {
+       if( config.global ) {
 // Get vector
 // Start of vector is center of previous block.
 // End of vector is total accumulation.
-               if(config.mode3 == MotionCVConfig::TRACK_SINGLE)
-               {
-                       global_x1 = (int64_t)(config.block_x * 
-                               w / 
-                               100);
-                       global_y1 = (int64_t)(config.block_y *
-                               h / 
-                               100);
+               if( config.mode3 == MotionCVConfig::TRACK_SINGLE ) {
+                       global_x1 = (int64_t) (config.block_x * w / 100);
+                       global_y1 = (int64_t) (config.block_y * h / 100);
                        global_x2 = global_x1 + total_dx / OVERSAMPLE;
                        global_y2 = global_y1 + total_dy / OVERSAMPLE;
-//printf("MotionCVMain::draw_vectors %d %d %d %d %d %d\n", total_dx, total_dy, global_x1, global_y1, global_x2, global_y2);
+//printf("MotionCVMain::draw_vectors %d %d %d %d %d %d\n",
+// total_dx, total_dy, global_x1, global_y1, global_x2, global_y2);
                }
-               else
 // Start of vector is center of previous block.
 // End of vector is current change.
-               if(config.mode3 == MotionCVConfig::PREVIOUS_SAME_BLOCK)
-               {
-                       global_x1 = (int64_t)(config.block_x * 
-                               w / 
-                               100);
-                       global_y1 = (int64_t)(config.block_y *
-                               h / 
-                               100);
+               else if( config.mode3 == MotionCVConfig::PREVIOUS_SAME_BLOCK ) {
+                       global_x1 = (int64_t) (config.block_x * w / 100);
+                       global_y1 = (int64_t) (config.block_y * h / 100);
                        global_x2 = global_x1 + current_dx / OVERSAMPLE;
                        global_y2 = global_y1 + current_dy / OVERSAMPLE;
                }
-               else
-               {
-                       global_x1 = (int64_t)(config.block_x * 
-                               w / 
-                               100 + 
-                               (total_dx - current_dx) / 
-                               OVERSAMPLE);
-                       global_y1 = (int64_t)(config.block_y *
-                               h / 
-                               100 +
-                               (total_dy - current_dy) /
-                               OVERSAMPLE);
-                       global_x2 = (int64_t)(config.block_x * 
-                               w / 
-                               100 + 
-                               total_dx / 
-                               OVERSAMPLE);
-                       global_y2 = (int64_t)(config.block_y *
-                               h / 
-                               100 +
-                               total_dy /
-                               OVERSAMPLE);
+               else {
+                       global_x1 = (int64_t) (config.block_x * w / 100
+                                       + (total_dx - current_dx) / OVERSAMPLE);
+                       global_y1 = (int64_t) (config.block_y * h / 100
+                                       + (total_dy - current_dy) / OVERSAMPLE);
+                       global_x2 = (int64_t) (config.block_x * w / 100
+                                       + total_dx / OVERSAMPLE);
+                       global_y2 = (int64_t) (config.block_y * h / 100
+                                       + total_dy / OVERSAMPLE);
                }
 
                block_x = global_x1;
@@ -1258,30 +976,11 @@ void MotionCVMain::draw_vectors(VFrame *frame)
                search_y2 = block_y2 + search_h / 2;
 
 // printf("MotionCVMain::draw_vectors %d %d %d %d %d %d %d %d %d %d %d %d\n",
-// global_x1,
-// global_y1,
-// block_w,
-// block_h,
-// block_x1,
-// block_y1,
-// block_x2,
-// block_y2,
-// search_x1,
-// search_y1,
-// search_x2,
-// search_y2);
-
-               clamp_scan(w, 
-                       h, 
-                       &block_x1,
-                       &block_y1,
-                       &block_x2,
-                       &block_y2,
-                       &search_x1,
-                       &search_y1,
-                       &search_x2,
-                       &search_y2,
-                       1);
+// global_x1, global_y1, block_w, block_h, block_x1, block_y1, block_x2, block_y2,
+// search_x1, search_y1, search_x2, search_y2);
+               clamp_scan(w, h,
+                          &block_x1, &block_y1, &block_x2, &block_y2,
+                          &search_x1, &search_y1, &search_x2, &search_y2, 1);
 
 // Vector
                draw_arrow(frame, global_x1, global_y1, global_x2, global_y2);
@@ -1292,7 +991,6 @@ void MotionCVMain::draw_vectors(VFrame *frame)
                draw_line(frame, block_x2, block_y2, block_x1, block_y2);
                draw_line(frame, block_x1, block_y2, block_x1, block_y1);
 
-
 // Search area
                draw_line(frame, search_x1, search_y1, search_x2, search_y1);
                draw_line(frame, search_x2, search_y1, search_x2, search_y2);
@@ -1300,22 +998,19 @@ void MotionCVMain::draw_vectors(VFrame *frame)
                draw_line(frame, search_x1, search_y2, search_x1, search_y1);
 
 // Block should be endpoint of motion
-               if(config.rotate)
-               {
+               if( config.rotate ) {
                        block_x = global_x2;
                        block_y = global_y2;
                }
        }
-       else
-       {
-               block_x = (int64_t)(config.block_x * w / 100);
-               block_y = (int64_t)(config.block_y * h / 100);
+       else {
+               block_x = (int64_t) (config.block_x * w / 100);
+               block_y = (int64_t) (config.block_y * h / 100);
        }
 
        block_w = config.rotation_block_w * w / 100;
        block_h = config.rotation_block_h * h / 100;
-       if(config.rotate)
-       {
+       if( config.rotate ) {
                float angle = total_angle * 2 * M_PI / 360;
                double base_angle1 = atan((float)block_h / block_w);
                double base_angle2 = atan((float)block_w / block_h);
@@ -1336,125 +1031,83 @@ void MotionCVMain::draw_vectors(VFrame *frame)
                draw_line(frame, block_x4, block_y4, block_x3, block_y3);
                draw_line(frame, block_x3, block_y3, block_x1, block_y1);
 
-
 // Center
-               if(!config.global)
-               {
+               if( !config.global ) {
                        draw_line(frame, block_x, block_y - 5, block_x, block_y + 6);
                        draw_line(frame, block_x - 5, block_y, block_x + 6, block_y);
                }
        }
 }
 
-
-
 void MotionCVMain::draw_pixel(VFrame *frame, int x, int y)
 {
-       if(!(x >= 0 && y >= 0 && x < frame->get_w() && y < frame->get_h())) return;
+       if( !(x >= 0 && y >= 0 && x < frame->get_w() && y < frame->get_h()) )
+               return;
 
-#define DRAW_PIXEL(x, y, components, do_yuv, max, type) \
-{ \
+#define DRAW_PIXEL(model, x, y, components, do_yuv, max, type) \
+ case model: { \
        type **rows = (type**)frame->get_rows(); \
        rows[y][x * components] = max - rows[y][x * components]; \
-       if(!do_yuv) \
-       { \
+       if( !do_yuv ) { \
                rows[y][x * components + 1] = max - rows[y][x * components + 1]; \
                rows[y][x * components + 2] = max - rows[y][x * components + 2]; \
        } \
-       else \
-       { \
+       else { \
                rows[y][x * components + 1] = (max / 2 + 1) - rows[y][x * components + 1]; \
                rows[y][x * components + 2] = (max / 2 + 1) - rows[y][x * components + 2]; \
        } \
-       if(components == 4) \
+       if( components == 4 ) \
                rows[y][x * components + 3] = max; \
-}
-
-
-       switch(frame->get_color_model())
-       {
-               case BC_RGB888:
-                       DRAW_PIXEL(x, y, 3, 0, 0xff, unsigned char);
-                       break;
-               case BC_RGBA8888:
-                       DRAW_PIXEL(x, y, 4, 0, 0xff, unsigned char);
-                       break;
-               case BC_RGB_FLOAT:
-                       DRAW_PIXEL(x, y, 3, 0, 1.0, float);
-                       break;
-               case BC_RGBA_FLOAT:
-                       DRAW_PIXEL(x, y, 4, 0, 1.0, float);
-                       break;
-               case BC_YUV888:
-                       DRAW_PIXEL(x, y, 3, 1, 0xff, unsigned char);
-                       break;
-               case BC_YUVA8888:
-                       DRAW_PIXEL(x, y, 4, 1, 0xff, unsigned char);
-                       break;
-               case BC_RGB161616:
-                       DRAW_PIXEL(x, y, 3, 0, 0xffff, uint16_t);
-                       break;
-               case BC_YUV161616:
-                       DRAW_PIXEL(x, y, 3, 1, 0xffff, uint16_t);
-                       break;
-               case BC_RGBA16161616:
-                       DRAW_PIXEL(x, y, 4, 0, 0xffff, uint16_t);
-                       break;
-               case BC_YUVA16161616:
-                       DRAW_PIXEL(x, y, 4, 1, 0xffff, uint16_t);
-                       break;
+} break
+
+       switch( frame->get_color_model() ) {
+       DRAW_PIXEL(BC_RGB888,x, y, 3, 0, 0xff, unsigned char);
+       DRAW_PIXEL(BC_RGBA8888,x, y, 4, 0, 0xff, unsigned char);
+       DRAW_PIXEL(BC_RGB_FLOAT,x, y, 3, 0, 1.0, float);
+       DRAW_PIXEL(BC_RGBA_FLOAT,x, y, 4, 0, 1.0, float);
+       DRAW_PIXEL(BC_YUV888,x, y, 3, 1, 0xff, unsigned char);
+       DRAW_PIXEL(BC_YUVA8888,x, y, 4, 1, 0xff, unsigned char);
+       DRAW_PIXEL(BC_RGB161616,x, y, 3, 0, 0xffff, uint16_t);
+       DRAW_PIXEL(BC_YUV161616,x, y, 3, 1, 0xffff, uint16_t);
+       DRAW_PIXEL(BC_RGBA16161616,x, y, 4, 0, 0xffff, uint16_t);
+       DRAW_PIXEL(BC_YUVA16161616,x, y, 4, 1, 0xffff, uint16_t);
        }
 }
 
-
 void MotionCVMain::draw_line(VFrame *frame, int x1, int y1, int x2, int y2)
 {
        int w = labs(x2 - x1);
        int h = labs(y2 - y1);
 //printf("MotionCVMain::draw_line 1 %d %d %d %d\n", x1, y1, x2, y2);
 
-       if(!w && !h)
-       {
+       if( !w && !h ) {
                draw_pixel(frame, x1, y1);
        }
-       else
-       if(w > h)
-       {
+       else if( w > h ) {
 // Flip coordinates so x1 < x2
-               if(x2 < x1)
-               {
-                       y2 ^= y1;
-                       y1 ^= y2;
-                       y2 ^= y1;
-                       x1 ^= x2;
-                       x2 ^= x1;
-                       x1 ^= x2;
+               if( x2 < x1 ) {
+                       y2 ^= y1;  y1 ^= y2;  y2 ^= y1;
+                       x1 ^= x2;  x2 ^= x1;  x1 ^= x2;
                }
                int numerator = y2 - y1;
                int denominator = x2 - x1;
-               for(int i = x1; i < x2; i++)
-               {
-                       int y = y1 + (int64_t)(i - x1) * (int64_t)numerator / (int64_t)denominator;
+               for( int i = x1; i < x2; i++ ) {
+                       int y = y1 + (int64_t) (i - x1)
+                           * (int64_t) numerator / (int64_t) denominator;
                        draw_pixel(frame, i, y);
                }
        }
-       else
-       {
+       else {
 // Flip coordinates so y1 < y2
-               if(y2 < y1)
-               {
-                       y2 ^= y1;
-                       y1 ^= y2;
-                       y2 ^= y1;
-                       x1 ^= x2;
-                       x2 ^= x1;
-                       x1 ^= x2;
+               if( y2 < y1 ) {
+                       y2 ^= y1;  y1 ^= y2;  y2 ^= y1;
+                       x1 ^= x2;  x2 ^= x1;  x1 ^= x2;
                }
                int numerator = x2 - x1;
                int denominator = y2 - y1;
-               for(int i = y1; i < y2; i++)
-               {
-                       int x = x1 + (int64_t)(i - y1) * (int64_t)numerator / (int64_t)denominator;
+               for( int i = y1; i < y2; i++ ) {
+                       int x = x1 + (int64_t) (i - y1)
+                           * (int64_t) numerator / (int64_t) denominator;
                        draw_pixel(frame, x, i);
                }
        }
@@ -1467,19 +1120,14 @@ void MotionCVMain::draw_arrow(VFrame *frame, int x1, int y1, int x2, int y2)
        double angle = atan((float)(y2 - y1) / (float)(x2 - x1));
        double angle1 = angle + (float)145 / 360 * 2 * 3.14159265;
        double angle2 = angle - (float)145 / 360 * 2 * 3.14159265;
-       int x3;
-       int y3;
-       int x4;
-       int y4;
-       if(x2 < x1)
-       {
+       int x3, y3, x4, y4;
+       if( x2 < x1 ) {
                x3 = x2 - (int)(ARROW_SIZE * cos(angle1));
                y3 = y2 - (int)(ARROW_SIZE * sin(angle1));
                x4 = x2 - (int)(ARROW_SIZE * cos(angle2));
                y4 = y2 - (int)(ARROW_SIZE * sin(angle2));
        }
-       else
-       {
+       else {
                x3 = x2 + (int)(ARROW_SIZE * cos(angle1));
                y3 = y2 + (int)(ARROW_SIZE * sin(angle1));
                x4 = x2 + (int)(ARROW_SIZE * cos(angle2));
@@ -1488,39 +1136,34 @@ void MotionCVMain::draw_arrow(VFrame *frame, int x1, int y1, int x2, int y2)
 
 // Main vector
        draw_line(frame, x1, y1, x2, y2);
-//     draw_line(frame, x1, y1 + 1, x2, y2 + 1);
+//      draw_line(frame, x1, y1 + 1, x2, y2 + 1);
 
 // Arrow line
-       if(abs(y2 - y1) || abs(x2 - x1)) draw_line(frame, x2, y2, x3, y3);
-//     draw_line(frame, x2, y2 + 1, x3, y3 + 1);
+       if( abs(y2 - y1) || abs(x2 - x1) )
+               draw_line(frame, x2, y2, x3, y3);
+//      draw_line(frame, x2, y2 + 1, x3, y3 + 1);
 // Arrow line
-       if(abs(y2 - y1) || abs(x2 - x1)) draw_line(frame, x2, y2, x4, y4);
-//     draw_line(frame, x2, y2 + 1, x4, y4 + 1);
+       if( abs(y2 - y1) || abs(x2 - x1) )
+               draw_line(frame, x2, y2, x4, y4);
+//      draw_line(frame, x2, y2 + 1, x4, y4 + 1);
 }
 
-
-
-
-#define ABS_DIFF(type, temp_type, multiplier, components) \
-{ \
+#define ABS_DIFF(model, type, temp_type, multiplier, components) \
+case model: { \
        temp_type result_temp = 0; \
-       for(int i = 0; i < h; i++) \
-       { \
+       for( int i = 0; i < h; i++ ) { \
                type *prev_row = (type*)prev_ptr; \
                type *current_row = (type*)current_ptr; \
-               for(int j = 0; j < w; j++) \
-               { \
-                       for(int k = 0; k < 3; k++) \
-                       { \
+               for( int j = 0; j < w; j++ ) { \
+                       for( int k = 0; k < 3; k++ ) { \
                                temp_type difference; \
                                difference = *prev_row++ - *current_row++; \
-                               if(difference < 0) \
+                               if( difference < 0 ) \
                                        result_temp -= difference; \
                                else \
                                        result_temp += difference; \
                        } \
-                       if(components == 4) \
-                       { \
+                       if( components == 4 ) { \
                                prev_row++; \
                                current_row++; \
                        } \
@@ -1529,66 +1172,40 @@ void MotionCVMain::draw_arrow(VFrame *frame, int x1, int y1, int x2, int y2)
                current_ptr += row_bytes; \
        } \
        result = (int64_t)(result_temp * multiplier); \
-}
+} break
 
-int64_t MotionCVMain::abs_diff(unsigned char *prev_ptr,
-       unsigned char *current_ptr,
-       int row_bytes,
-       int w,
-       int h,
-       int color_model)
+int64_t MotionCVMain::abs_diff(unsigned char *prev_ptr, unsigned char *current_ptr,
+              int row_bytes, int w, int h, int color_model)
 {
        int64_t result = 0;
-       switch(color_model)
-       {
-               case BC_RGB888:
-                       ABS_DIFF(unsigned char, int64_t, 1, 3)
-                       break;
-               case BC_RGBA8888:
-                       ABS_DIFF(unsigned char, int64_t, 1, 4)
-                       break;
-               case BC_RGB_FLOAT:
-                       ABS_DIFF(float, double, 0x10000, 3)
-                       break;
-               case BC_RGBA_FLOAT:
-                       ABS_DIFF(float, double, 0x10000, 4)
-                       break;
-               case BC_YUV888:
-                       ABS_DIFF(unsigned char, int64_t, 1, 3)
-                       break;
-               case BC_YUVA8888:
-                       ABS_DIFF(unsigned char, int64_t, 1, 4)
-                       break;
-               case BC_YUV161616:
-                       ABS_DIFF(uint16_t, int64_t, 1, 3)
-                       break;
-               case BC_YUVA16161616:
-                       ABS_DIFF(uint16_t, int64_t, 1, 4)
-                       break;
+       switch( color_model ) {
+       ABS_DIFF(BC_RGB888,unsigned char, int64_t, 1, 3);
+       ABS_DIFF(BC_RGBA8888,unsigned char, int64_t, 1, 4);
+       ABS_DIFF(BC_RGB_FLOAT,float, double, 0x10000, 3);
+       ABS_DIFF(BC_RGBA_FLOAT,float, double, 0x10000, 4);
+       ABS_DIFF(BC_YUV888,unsigned char, int64_t, 1, 3);
+       ABS_DIFF(BC_YUVA8888,unsigned char, int64_t, 1, 4);
+       ABS_DIFF(BC_YUV161616,uint16_t, int64_t, 1, 3);
+       ABS_DIFF(BC_YUVA16161616,uint16_t, int64_t, 1, 4);
        }
        return result;
 }
 
-
-
-#define ABS_DIFF_SUB(type, temp_type, multiplier, components) \
-{ \
+#define ABS_DIFF_SUB(model, type, temp_type, multiplier, components) \
+case model: { \
        temp_type result_temp = 0; \
        temp_type y2_fraction = sub_y * 0x100 / OVERSAMPLE; \
        temp_type y1_fraction = 0x100 - y2_fraction; \
        temp_type x2_fraction = sub_x * 0x100 / OVERSAMPLE; \
        temp_type x1_fraction = 0x100 - x2_fraction; \
-       for(int i = 0; i < h_sub; i++) \
-       { \
+       for( int i = 0; i < h_sub; i++ ) { \
                type *prev_row1 = (type*)prev_ptr; \
                type *prev_row2 = (type*)prev_ptr + components; \
                type *prev_row3 = (type*)(prev_ptr + row_bytes); \
                type *prev_row4 = (type*)(prev_ptr + row_bytes) + components; \
                type *current_row = (type*)current_ptr; \
-               for(int j = 0; j < w_sub; j++) \
-               { \
-                       for(int k = 0; k < 3; k++) \
-                       { \
+               for( int j = 0; j < w_sub; j++ ) { \
+                       for( int k = 0; k < 3; k++ ) { \
                                temp_type difference; \
                                temp_type prev_value = \
                                        (*prev_row1++ * x1_fraction * y1_fraction + \
@@ -1598,18 +1215,15 @@ int64_t MotionCVMain::abs_diff(unsigned char *prev_ptr,
                                        0x100 / 0x100; \
                                temp_type current_value = *current_row++; \
                                difference = prev_value - current_value; \
-                               if(difference < 0) \
+                               if( difference < 0 ) \
                                        result_temp -= difference; \
                                else \
                                        result_temp += difference; \
                        } \
  \
-                       if(components == 4) \
-                       { \
-                               prev_row1++; \
-                               prev_row2++; \
-                               prev_row3++; \
-                               prev_row4++; \
+                       if( components == 4 ) { \
+                               prev_row1++; prev_row2++; \
+                               prev_row3++; prev_row4++; \
                                current_row++; \
                        } \
                } \
@@ -1617,91 +1231,135 @@ int64_t MotionCVMain::abs_diff(unsigned char *prev_ptr,
                current_ptr += row_bytes; \
        } \
        result = (int64_t)(result_temp * multiplier); \
-}
-
+} break
 
-
-
-int64_t MotionCVMain::abs_diff_sub(unsigned char *prev_ptr,
-       unsigned char *current_ptr,
-       int row_bytes,
-       int w,
-       int h,
-       int color_model,
-       int sub_x,
-       int sub_y)
+int64_t MotionCVMain::abs_diff_sub(unsigned char *prev_ptr, unsigned char *current_ptr,
+          int row_bytes, int w, int h, int color_model, int sub_x, int sub_y)
 {
        int h_sub = h - 1;
        int w_sub = w - 1;
        int64_t result = 0;
 
-       switch(color_model)
-       {
-               case BC_RGB888:
-                       ABS_DIFF_SUB(unsigned char, int64_t, 1, 3)
-                       break;
-               case BC_RGBA8888:
-                       ABS_DIFF_SUB(unsigned char, int64_t, 1, 4)
-                       break;
-               case BC_RGB_FLOAT:
-                       ABS_DIFF_SUB(float, double, 0x10000, 3)
-                       break;
-               case BC_RGBA_FLOAT:
-                       ABS_DIFF_SUB(float, double, 0x10000, 4)
-                       break;
-               case BC_YUV888:
-                       ABS_DIFF_SUB(unsigned char, int64_t, 1, 3)
-                       break;
-               case BC_YUVA8888:
-                       ABS_DIFF_SUB(unsigned char, int64_t, 1, 4)
-                       break;
-               case BC_YUV161616:
-                       ABS_DIFF_SUB(uint16_t, int64_t, 1, 3)
-                       break;
-               case BC_YUVA16161616:
-                       ABS_DIFF_SUB(uint16_t, int64_t, 1, 4)
-                       break;
+       switch( color_model ) {
+       ABS_DIFF_SUB(BC_RGB888,unsigned char, int64_t, 1, 3);
+       ABS_DIFF_SUB(BC_RGBA8888,unsigned char, int64_t, 1, 4);
+       ABS_DIFF_SUB(BC_RGB_FLOAT,float, double, 0x10000, 3);
+       ABS_DIFF_SUB(BC_RGBA_FLOAT,float, double, 0x10000, 4);
+       ABS_DIFF_SUB(BC_YUV888,unsigned char, int64_t, 1, 3);
+       ABS_DIFF_SUB(BC_YUVA8888,unsigned char, int64_t, 1, 4);
+       ABS_DIFF_SUB(BC_YUV161616,uint16_t, int64_t, 1, 3);
+       ABS_DIFF_SUB(BC_YUVA16161616,uint16_t, int64_t, 1, 4);
        }
        return result;
 }
 
-int MotionCVMain::get_line_key(const char *filename, int64_t key, char *line, int len)
+
+int MotionCVMain::open_cache_file()
+{
+       if( cache_fp ) return 0;
+       if( !cache_file[0] ) return 1;
+       if( !(cache_fp = fopen(cache_file, "r")) ) return 1;
+       return 0;
+}
+
+void MotionCVMain::close_cache_file()
+{
+       if( !cache_fp ) return;
+       fclose(cache_fp);
+       cache_fp = 0; cache_key = -1; tracking_frame = -1;
+}
+
+int MotionCVMain::load_cache_line()
+{
+       cache_key = -1;
+       if( open_cache_file() ) return 1;
+       if( !fgets(cache_line, sizeof(cache_line), cache_fp) ) return 1;
+       cache_key = strtol(cache_line, 0, 0);
+       return 0;
+}
+
+int MotionCVMain::get_cache_line(int64_t key)
 {
-       if( active_fp && strcmp(active_file, filename) ) {
-               fclose(active_fp);  active_fp = 0;  active_file[0] = 0;
+       if( cache_key == key ) return 0;
+       if( open_cache_file() ) return 1;
+       if( cache_key >= 0 && key > cache_key ) {
+               if( load_cache_line() ) return 1;
+               if( cache_key == key ) return 0;
+               if( cache_key > key ) return 1;
        }
-       if( !active_fp ) {
-               if( !(active_fp = fopen(filename, "r")) ) {
-                       perror("open motion file");
-                       fprintf(stderr,"err reading key %jd\n", key);
+// binary search file
+       fseek(cache_fp, 0, SEEK_END);
+       int64_t l = -1, r = ftell(cache_fp);
+       while( (r - l) > 1 ) {
+               int64_t m = (l + r) / 2;
+               fseek(cache_fp, m, SEEK_SET);
+               if( m > 0 && !fgets(cache_line, sizeof(cache_line), cache_fp) )
                        return -1;
+               if( !load_cache_line() ) {
+                       if( cache_key == key )
+                               return 0;
+                       if( cache_key < key ) { l = m; continue; }
                }
-               strcpy(active_file, filename);
-               tracking_frame = -1;
+               r = m;
        }
-       int64_t recd = 0;
-       if( fgets(line, len, active_fp) && (recd=strtol(line,0,0)) == key )
-               return 0;
-// binary search file
-       fseek(active_fp, 0, SEEK_END);
-       int64_t l = -1, r = ftell(active_fp);
-       while( (r-l) > 1 ) {
-               int64_t m = (l+r) / 2;
-               fseek(active_fp, m, SEEK_SET);
-               if( m > 0 && !fgets(line, len, active_fp) ) {
-                       fprintf(stderr,"err reading key %jd\n", key);
+       return 1;
+}
+
+int MotionCVMain::locate_cache_line(int64_t key)
+{
+       int ret = 1;
+       if( key < 0 || !(ret=get_cache_line(key)) ||
+           ( cache_key >= 0 && cache_key < key ) )
+               ret = load_cache_line();
+       return ret;
+}
+
+int MotionCVMain::put_cache_line(const char *line)
+{
+       int64_t key = strtol(line, 0, 0);
+       if( key == active_key ) return 1;
+       if( !active_fp ) {
+               close_cache_file();
+               sprintf(cache_file, "%s.bak", config.tracking_file);
+               ::rename(config.tracking_file, cache_file);
+               if( !(active_fp = fopen(config.tracking_file, "w")) ) {
+                       perror(config.tracking_file);
+                       fprintf(stderr, "err writing key %jd\n", key);
                        return -1;
                }
-               if( fgets(line, len, active_fp) ) {
-                       recd = strtol(line,0,0);
-                       if( recd == key ) return 0;
-                       if( recd < key ) { l = m; continue; }
+               active_key = -1;
+       }
+
+       if( active_key < key ) {
+               locate_cache_line(active_key);
+               while( cache_key >= 0 && key >= cache_key ) {
+                       if( key > cache_key )
+                               fputs(cache_line, active_fp);
+                       load_cache_line();
                }
-               r = m;
        }
-       return 1;
+
+       active_key = key;
+       fputs(line, active_fp);
+       fflush(active_fp);
+       return 0;
 }
 
+void MotionCVMain::reset_cache_file()
+{
+       if( active_fp ) {
+               locate_cache_line(active_key);
+               while( cache_key >= 0 ) {
+                       fputs(cache_line, active_fp);
+                       load_cache_line();
+               }
+               close_cache_file();  ::remove(cache_file);
+               fclose(active_fp); active_fp = 0; active_key = -1;
+       }
+       else
+               close_cache_file();
+       strcpy(cache_file, config.tracking_file);
+}
 
 
 MotionCVScanPackage::MotionCVScanPackage()
@@ -1710,9 +1368,7 @@ MotionCVScanPackage::MotionCVScanPackage()
        valid = 1;
 }
 
-
-MotionCVScanUnit::MotionCVScanUnit(MotionCVScan *server, 
-       MotionCVMain *plugin)
+MotionCVScanUnit::MotionCVScanUnit(MotionCVScan *server, MotionCVMain *plugin)
  : LoadClient(server)
 {
        this->plugin = plugin;
@@ -1725,11 +1381,9 @@ MotionCVScanUnit::~MotionCVScanUnit()
        delete cache_lock;
 }
 
-
-
 void MotionCVScanUnit::process_package(LoadPackage *package)
 {
-       MotionCVScanPackage *pkg = (MotionCVScanPackage*)package;
+       MotionCVScanPackage *pkg = (MotionCVScanPackage *) package;
        //int w = server->current_frame->get_w();
        //int h = server->current_frame->get_h();
        int color_model = server->current_frame->get_color_model();
@@ -1737,114 +1391,73 @@ void MotionCVScanUnit::process_package(LoadPackage *package)
        int row_bytes = server->current_frame->get_bytes_per_line();
 
 // Single pixel
-       if(!server->subpixel)
-       {
+       if( !server->subpixel ) {
                int search_x = pkg->scan_x1 + (pkg->pixel % (pkg->scan_x2 - pkg->scan_x1));
                int search_y = pkg->scan_y1 + (pkg->pixel / (pkg->scan_x2 - pkg->scan_x1));
 
 // Try cache
                pkg->difference1 = server->get_cache(search_x, search_y);
-               if(pkg->difference1 < 0)
-               {
-//printf("MotionCVScanUnit::process_package 1 %d %d\n", 
-//search_x, search_y, pkg->block_x2 - pkg->block_x1, pkg->block_y2 - pkg->block_y1);
+               if( pkg->difference1 < 0 ) {
+//printf("MotionCVScanUnit::process_package 1 %d %d\n",
+// search_x, search_y, pkg->block_x2 - pkg->block_x1, pkg->block_y2 - pkg->block_y1);
 // Pointers to first pixel in each block
-                       unsigned char *prev_ptr = server->previous_frame->get_rows()[
-                               search_y] +     
-                               search_x * pixel_size;
-                       unsigned char *current_ptr = server->current_frame->get_rows()[
-                               pkg->block_y1] +
-                               pkg->block_x1 * pixel_size;
+                       unsigned char *prev_ptr =
+                           server->previous_frame->get_rows()[search_y]
+                               search_x * pixel_size;
+                       unsigned char *current_ptr =
+                           server->current_frame->get_rows()[pkg->block_y1]
+                               pkg->block_x1 * pixel_size;
 // Scan block
-                       pkg->difference1 = plugin->abs_diff(prev_ptr,
-                               current_ptr,
-                               row_bytes,
-                               pkg->block_x2 - pkg->block_x1,
-                               pkg->block_y2 - pkg->block_y1,
-                               color_model);
+                       pkg->difference1 = plugin->abs_diff(prev_ptr, current_ptr, row_bytes,
+                                   pkg->block_x2 - pkg->block_x1, pkg->block_y2 - pkg->block_y1,
+                                   color_model);
 //printf("MotionCVScanUnit::process_package 2\n");
                        server->put_cache(search_x, search_y, pkg->difference1);
                }
        }
-       else
 // Sub pixel
-       {
+       else {
                int sub_x = pkg->pixel % (OVERSAMPLE * 2 - 1) + 1;
                int sub_y = pkg->pixel / (OVERSAMPLE * 2 - 1) + 1;
 
-               if(plugin->config.horizontal_only)
-               {
-                       sub_y = 0;
-               }
-
-               if(plugin->config.vertical_only)
-               {
-                       sub_x = 0;
-               }
+               if( plugin->config.horizontal_only ) sub_y = 0;
+               if( plugin->config.vertical_only ) sub_x = 0;
 
                int search_x = pkg->scan_x1 + sub_x / OVERSAMPLE;
                int search_y = pkg->scan_y1 + sub_y / OVERSAMPLE;
                sub_x %= OVERSAMPLE;
                sub_y %= OVERSAMPLE;
 
-
-               unsigned char *prev_ptr = server->previous_frame->get_rows()[
-                       search_y] +
-                       search_x * pixel_size;
-               unsigned char *current_ptr = server->current_frame->get_rows()[
-                       pkg->block_y1] +
-                       pkg->block_x1 * pixel_size;
+               unsigned char *prev_ptr =
+                   server->previous_frame->get_rows()[search_y]
+                       + search_x * pixel_size;
+               unsigned char *current_ptr =
+                   server->current_frame->get_rows()[pkg->block_y1]
+                       + pkg->block_x1 * pixel_size;
 
 // With subpixel, there are two ways to compare each position, one by shifting
 // the previous frame and two by shifting the current frame.
-               pkg->difference1 = plugin->abs_diff_sub(prev_ptr,
-                       current_ptr,
-                       row_bytes,
-                       pkg->block_x2 - pkg->block_x1,
-                       pkg->block_y2 - pkg->block_y1,
-                       color_model,
-                       sub_x,
-                       sub_y);
-               pkg->difference2 = plugin->abs_diff_sub(current_ptr,
-                       prev_ptr,
-                       row_bytes,
-                       pkg->block_x2 - pkg->block_x1,
-                       pkg->block_y2 - pkg->block_y1,
-                       color_model,
-                       sub_x,
-                       sub_y);
-// printf("MotionCVScanUnit::process_package sub_x=%d sub_y=%d search_x=%d search_y=%d diff1=%lld diff2=%lld\n",
-// sub_x,
-// sub_y,
-// search_x,
-// search_y,
-// pkg->difference1,
-// pkg->difference2);
+               pkg->difference1 = plugin->abs_diff_sub(prev_ptr, current_ptr, row_bytes,
+                       pkg->block_x2 - pkg->block_x1, pkg->block_y2 - pkg->block_y1,
+                       color_model, sub_x, sub_y);
+               pkg->difference2 =
+                   plugin->abs_diff_sub(current_ptr, prev_ptr, row_bytes,
+                        pkg->block_x2 - pkg->block_x1, pkg->block_y2 - pkg->block_y1,
+                        color_model, sub_x, sub_y);
+//printf("MotionCVScanUnit::process_package sub_x=%d sub_y=%d"
+// " search_x=%d search_y=%d diff1=%jd diff2=%jd\n",
+// sub_x, sub_y, search_x, search_y, pkg->difference1, pkg->difference2);
        }
 
-
-
-
 }
 
-
-
-
-
-
-
-
-
-
 int64_t MotionCVScanUnit::get_cache(int x, int y)
 {
        int64_t result = -1;
        cache_lock->lock("MotionCVScanUnit::get_cache");
-       for(int i = 0; i < cache.total; i++)
-       {
+       for( int i = 0; i < cache.total; i++ ) {
                MotionCVScanCache *ptr = cache.values[i];
-               if(ptr->x == x && ptr->y == y)
-               {
+               if( ptr->x == x && ptr->y == y ) {
                        result = ptr->difference;
                        break;
                }
@@ -1861,23 +1474,10 @@ void MotionCVScanUnit::put_cache(int x, int y, int64_t difference)
        cache_lock->unlock();
 }
 
-
-
-
-
-
-
-
-
-
-
-MotionCVScan::MotionCVScan(MotionCVMain *plugin, 
-       int total_clients,
-       int total_packages)
- : LoadServer(
-//1, 1 
-total_clients, total_packages 
-)
+MotionCVScan::MotionCVScan(MotionCVMain *plugin,
+                  int total_clients, int total_packages)
+:LoadServer( //1, 1
+          total_clients, total_packages)
 {
        this->plugin = plugin;
        cache_lock = new Mutex("MotionCVScan::cache_lock");
@@ -1888,44 +1488,34 @@ MotionCVScan::~MotionCVScan()
        delete cache_lock;
 }
 
-
 void MotionCVScan::init_packages()
 {
 // Set package coords
-       for(int i = 0; i < get_total_packages(); i++)
-       {
-               MotionCVScanPackage *pkg = (MotionCVScanPackage*)get_package(i);
-
-               pkg->block_x1 = block_x1;
-               pkg->block_x2 = block_x2;
-               pkg->block_y1 = block_y1;
-               pkg->block_y2 = block_y2;
-               pkg->scan_x1 = scan_x1;
-               pkg->scan_x2 = scan_x2;
-               pkg->scan_y1 = scan_y1;
-               pkg->scan_y2 = scan_y2;
-               pkg->pixel = (int64_t)i * (int64_t)total_pixels / (int64_t)total_steps;
-               pkg->difference1 = 0;
-               pkg->difference2 = 0;
-               pkg->dx = 0;
-               pkg->dy = 0;
+       for( int i = 0; i < get_total_packages(); i++ ) {
+               MotionCVScanPackage *pkg = (MotionCVScanPackage *) get_package(i);
+
+               pkg->block_x1 = block_x1; pkg->block_x2 = block_x2;
+               pkg->block_y1 = block_y1; pkg->block_y2 = block_y2;
+               pkg->scan_x1 = scan_x1;   pkg->scan_x2 = scan_x2;
+               pkg->scan_y1 = scan_y1;   pkg->scan_y2 = scan_y2;
+               pkg->pixel = (int64_t) i *(int64_t) total_pixels / (int64_t) total_steps;
+               pkg->difference1 = 0;     pkg->difference2 = 0;
+               pkg->dx = pkg->dy = 0;
                pkg->valid = 1;
        }
 }
 
-LoadClientMotionCVScan::new_client()
+LoadClient *MotionCVScan::new_client()
 {
        return new MotionCVScanUnit(this, plugin);
 }
 
-LoadPackageMotionCVScan::new_package()
+LoadPackage *MotionCVScan::new_package()
 {
        return new MotionCVScanPackage;
 }
 
-
-void MotionCVScan::scan_frame(VFrame *previous_frame,
-       VFrame *current_frame)
+void MotionCVScan::scan_frame(VFrame *previous_frame, VFrame *current_frame)
 {
        this->previous_frame = previous_frame;
        this->current_frame = current_frame;
@@ -1933,7 +1523,6 @@ void MotionCVScan::scan_frame(VFrame *previous_frame,
 
        cache.remove_all_objects();
 
-
 // Single macroblock
        int w = current_frame->get_w();
        int h = current_frame->get_h();
@@ -1952,8 +1541,7 @@ void MotionCVScan::scan_frame(VFrame *previous_frame,
 
 // Offset to location of previous block.  This offset needn't be very accurate
 // since it's the offset of the previous image and current image we want.
-       if(plugin->config.mode3 == MotionCVConfig::TRACK_PREVIOUS)
-       {
+       if( plugin->config.mode3 == MotionCVConfig::TRACK_PREVIOUS ) {
                block_x1 += plugin->total_dx / OVERSAMPLE;
                block_y1 += plugin->total_dy / OVERSAMPLE;
                block_x2 += plugin->total_dx / OVERSAMPLE;
@@ -1962,170 +1550,124 @@ void MotionCVScan::scan_frame(VFrame *previous_frame,
 
        skip = 0;
 
-       switch(plugin->config.mode2)
-       {
+       switch( plugin->config.mode2 ) {
 // Don't calculate
-               case MotionCVConfig::NO_CALCULATE:
-                       dx_result = 0;
-                       dy_result = 0;
+       case MotionCVConfig::NO_CALCULATE:
+               dx_result = dy_result = 0;
+               skip = 1;
+               break;
+
+       case MotionCVConfig::LOAD:
+       case MotionCVConfig::SAVE:
+               if( plugin->load_ok ) {
+                       dx_result = plugin->load_dx;
+                       dy_result = plugin->load_dy;
                        skip = 1;
-                       break;
-
-               case MotionCVConfig::LOAD:
-               {
-                       if( plugin->load_ok ) {
-                               dx_result = plugin->load_dx;
-                               dy_result = plugin->load_dy;
-                               skip = 1;
-                       }
-                       break;
                }
+               break;
 
 // Scan from scratch
-               default:
-                       skip = 0;
-                       break;
+       default:
+               skip = 0;
+               break;
        }
 
 // Perform scan
-       if(!skip)
-       {
+       if( !skip ) {
 // Location of block in current frame
                int x_result = block_x1;
                int y_result = block_y1;
 
-// printf("MotionCVScan::scan_frame 1 %d %d %d %d %d %d %d %d\n",
-// block_x1 + block_w / 2,
-// block_y1 + block_h / 2,
-// block_w,
-// block_h,
-// block_x1,
-// block_y1,
-// block_x2,
-// block_y2);
-
-               while(1)
-               {
+//printf("MotionCVScan::scan_frame 1 %d %d %d %d %d %d %d %d\n",
+// block_x1 + block_w / 2, block_y1 + block_h / 2,
+// block_w, block_h, block_x1, block_y1, block_x2, block_y2);
+
+               while( 1 ) {
                        scan_x1 = x_result - scan_w / 2;
                        scan_y1 = y_result - scan_h / 2;
                        scan_x2 = x_result + scan_w / 2;
                        scan_y2 = y_result + scan_h / 2;
 
-
-
 // Zero out requested values
-                       if(plugin->config.horizontal_only)
-                       {
+                       if( plugin->config.horizontal_only ) {
                                scan_y1 = block_y1;
                                scan_y2 = block_y1 + 1;
                        }
-                       if(plugin->config.vertical_only)
-                       {
+                       if( plugin->config.vertical_only ) {
                                scan_x1 = block_x1;
                                scan_x2 = block_x1 + 1;
                        }
-
 // printf("MotionCVScan::scan_frame 1 %d %d %d %d %d %d %d %d\n",
-// block_x1,
-// block_y1,
-// block_x2,
-// block_y2,
-// scan_x1,
-// scan_y1,
-// scan_x2,
-// scan_y2);
+// block_x1, block_y1, block_x2, block_y2, scan_x1, scan_y1, scan_x2, scan_y2);
 // Clamp the block coords before the scan so we get useful scan coords.
-                       MotionCVMain::clamp_scan(w, 
-                               h, 
-                               &block_x1,
-                               &block_y1,
-                               &block_x2,
-                               &block_y2,
-                               &scan_x1,
-                               &scan_y1,
-                               &scan_x2,
-                               &scan_y2,
-                               0);
-// printf("MotionCVScan::scan_frame 1\n    block_x1=%d block_y1=%d block_x2=%d block_y2=%d\n    scan_x1=%d scan_y1=%d scan_x2=%d scan_y2=%d\n    x_result=%d y_result=%d\n", 
-// block_x1,
-// block_y1,
-// block_x2,
-// block_y2,
-// scan_x1, 
-// scan_y1, 
-// scan_x2, 
-// scan_y2, 
-// x_result, 
-// y_result);
-
+                       MotionCVMain::clamp_scan(w, h,
+                               &block_x1, &block_y1, &block_x2, &block_y2,
+                               &scan_x1, &scan_y1, &scan_x2, &scan_y2, 0);
+//printf("MotionCVScan::scan_frame 1\n"
+// "    block_x1=%d block_y1=%d block_x2=%d block_y2=%d\n"
+// "    scan_x1=%d scan_y1=%d scan_x2=%d scan_y2=%d\n"
+// "    x_result=%d y_result=%d\n",
+// block_x1, block_y1, block_x2, block_y2,
+// scan_x1, scan_y1, scan_x2, scan_y2, x_result, y_result);
 
 // Give up if invalid coords.
-                       if(scan_y2 <= scan_y1 ||
-                               scan_x2 <= scan_x1 ||
-                               block_x2 <= block_x1 ||
-                               block_y2 <= block_y1)
+                       if( scan_y2 <= scan_y1 || scan_x2 <= scan_x1 ||
+                           block_x2 <= block_x1 || block_y2 <= block_y1 )
                                break;
 
 // For subpixel, the top row and left column are skipped
-                       if(subpixel)
-                       {
-                               if(plugin->config.horizontal_only ||
-                                       plugin->config.vertical_only)
-                               {
-                                       total_pixels = 4 * OVERSAMPLE * OVERSAMPLE - 4 * OVERSAMPLE;
+                       if( subpixel ) {
+                               if( plugin->config.horizontal_only ||
+                                   plugin->config.vertical_only ) {
+                                       total_pixels = 4 * OVERSAMPLE * OVERSAMPLE
+                                               - 4 * OVERSAMPLE;
                                }
-                               else
-                               {
+                               else {
                                        total_pixels = 4 * OVERSAMPLE;
                                }
 
                                total_steps = total_pixels;
-
                                set_package_count(total_steps);
                                process_packages();
 
 // Get least difference
                                int64_t min_difference = -1;
-                               for(int i = 0; i < get_total_packages(); i++)
-                               {
-                                       MotionCVScanPackage *pkg = (MotionCVScanPackage*)get_package(i);
-                                       if(pkg->difference1 < min_difference || min_difference == -1)
-                                       {
+                               for(int i = 0; i < get_total_packages(); i++ ) {
+                                       MotionCVScanPackage *pkg = (MotionCVScanPackage *) get_package(i);
+                                       if( pkg->difference1 < min_difference || min_difference == -1 ) {
                                                min_difference = pkg->difference1;
 
-                                               if(plugin->config.vertical_only)
+                                               if( plugin->config.  vertical_only )
                                                        x_result = scan_x1 * OVERSAMPLE;
                                                else
-                                                       x_result = scan_x1 * OVERSAMPLE + 
-                                                               (pkg->pixel % (OVERSAMPLE * 2 - 1)) + 1;
-                                               
-                                               if(plugin->config.horizontal_only)
+                                                       x_result = scan_x1 * OVERSAMPLE
+                                                           + (pkg->pixel % (OVERSAMPLE * 2 - 1)) + 1;
+
+                                               if( plugin->config.  horizontal_only )
                                                        y_result = scan_y1 * OVERSAMPLE;
                                                else
-                                                       y_result = scan_y1 * OVERSAMPLE + 
-                                                               (pkg->pixel / (OVERSAMPLE * 2 - 1)) + 1;
-
+                                                       y_result = scan_y1 * OVERSAMPLE
+                                                           + (pkg->pixel / (OVERSAMPLE * 2 - 1)) + 1;
 
 // Fill in results
                                                dx_result = block_x1 * OVERSAMPLE - x_result;
                                                dy_result = block_y1 * OVERSAMPLE - y_result;
                                        }
 
-                                       if(pkg->difference2 < min_difference)
-                                       {
+                                       if( pkg->difference2 < min_difference ) {
                                                min_difference = pkg->difference2;
 
-                                               if(plugin->config.vertical_only)
+                                               if( plugin->config.  vertical_only )
                                                        x_result = scan_x1 * OVERSAMPLE;
                                                else
-                                                       x_result = scan_x2 * OVERSAMPLE -
-                                                               ((pkg->pixel % (OVERSAMPLE * 2 - 1)) + 1);
+                                                       x_result = scan_x2 * OVERSAMPLE
+                                                           - ((pkg->pixel % (OVERSAMPLE * 2 - 1)) + 1);
 
-                                               if(plugin->config.horizontal_only)
+                                               if( plugin->config.  horizontal_only )
                                                        y_result = scan_y1 * OVERSAMPLE;
                                                else
-                                                       y_result = scan_y2 * OVERSAMPLE -
-                                                               ((pkg->pixel / (OVERSAMPLE * 2 - 1)) + 1);
+                                                       y_result = scan_y2 * OVERSAMPLE
+                                                           - ((pkg->pixel / (OVERSAMPLE * 2 - 1)) + 1);
 
                                                dx_result = block_x1 * OVERSAMPLE - x_result;
                                                dy_result = block_y1 * OVERSAMPLE - y_result;
@@ -2135,8 +1677,7 @@ void MotionCVScan::scan_frame(VFrame *previous_frame,
 //printf("MotionCVScan::scan_frame 1 %d %d %d %d\n", block_x1, block_y1, x_result, y_result);
                                break;
                        }
-                       else
-                       {
+                       else {
                                total_pixels = (scan_x2 - scan_x1) * (scan_y2 - scan_y1);
                                total_steps = MIN(plugin->config.global_positions, total_pixels);
 
@@ -2145,11 +1686,9 @@ void MotionCVScan::scan_frame(VFrame *previous_frame,
 
 // Get least difference
                                int64_t min_difference = -1;
-                               for(int i = 0; i < get_total_packages(); i++)
-                               {
-                                       MotionCVScanPackage *pkg = (MotionCVScanPackage*)get_package(i);
-                                       if(pkg->difference1 < min_difference || min_difference == -1)
-                                       {
+                               for( int i = 0; i < get_total_packages(); i++ ) {
+                                       MotionCVScanPackage *pkg = (MotionCVScanPackage *) get_package(i);
+                                       if( pkg->difference1 < min_difference || min_difference == -1 ) {
                                                min_difference = pkg->difference1;
                                                x_result = scan_x1 + (pkg->pixel % (scan_x2 - scan_x1));
                                                y_result = scan_y1 + (pkg->pixel / (scan_x2 - scan_x1));
@@ -2158,53 +1697,35 @@ void MotionCVScan::scan_frame(VFrame *previous_frame,
                                        }
                                }
 
-// printf("MotionCVScan::scan_frame 10 total_steps=%d total_pixels=%d subpixel=%d\n",
-// total_steps, 
-// total_pixels,
-// subpixel);
-// 
-// printf("    scan w=%d h=%d scan x1=%d y1=%d x2=%d y2=%d\n",
-// scan_w,
-// scan_h, 
-// scan_x1,
-// scan_y1,
-// scan_x2,
-// scan_y2);
-// 
-// printf("MotionCVScan::scan_frame 2 block x1=%d y1=%d x2=%d y2=%d result x=%.2f y=%.2f\n", 
-// block_x1, 
-// block_y1, 
-// block_x2,
-// block_y2,
-// (float)x_result / 4, 
-// (float)y_result / 4);
-
+//printf("MotionCVScan::scan_frame 10 total_steps=%d total_pixels=%d subpixel=%d\n",
+// total_steps, total_pixels, subpixel);
+//
+//printf("     scan w=%d h=%d scan x1=%d y1=%d x2=%d y2=%d\n",
+// scan_w, scan_h, scan_x1, scan_y1, scan_x2, scan_y2);
+//
+// printf("MotionCVScan::scan_frame 2 block x1=%d y1=%d x2=%d y2=%d result x=%.2f y=%.2f\n",
+// block_x1, block_y1, block_x2, block_y2, (float)x_result / 4, (float)y_result / 4);
 
 // If a new search is required, rescale results back to pixels.
-                               if(total_steps >= total_pixels)
-                               {
+                               if( total_steps >= total_pixels ) {
 // Single pixel accuracy reached.  Now do exhaustive subpixel search.
-                                       if(plugin->config.mode1 == MotionCVConfig::STABILIZE ||
-                                               plugin->config.mode1 == MotionCVConfig::TRACK ||
-                                               plugin->config.mode1 == MotionCVConfig::NOTHING)
-                                       {
+                                       if( plugin->config.mode1 == MotionCVConfig::STABILIZE ||
+                                           plugin->config.mode1 == MotionCVConfig::TRACK ||
+                                           plugin->config.mode1 == MotionCVConfig::NOTHING ) {
                                                x_result /= OVERSAMPLE;
                                                y_result /= OVERSAMPLE;
-                                               scan_w = 2;
-                                               scan_h = 2;
+                                               scan_w = scan_h = 2;
                                                subpixel = 1;
                                        }
-                                       else
-                                       {
 // Fill in results and quit
+                                       else {
                                                dx_result = block_x1 * OVERSAMPLE - x_result;
                                                dy_result = block_y1 * OVERSAMPLE - y_result;
                                                break;
                                        }
                                }
-                               else
 // Reduce scan area and try again
-                               {
+                               else {
                                        scan_w = (scan_x2 - scan_x1) / 2;
                                        scan_h = (scan_y2 - scan_y1) / 2;
                                        x_result /= OVERSAMPLE;
@@ -2214,48 +1735,22 @@ void MotionCVScan::scan_frame(VFrame *previous_frame,
                }
 
                // Add offsets from the "tracked single frame"
-               dx_result = plugin->dx_offset - dx_result;
-               dy_result = plugin->dy_offset - dy_result;
-
-               if(plugin->config.mode2 == MotionCVConfig::SAVE)
-               {
-                       plugin->save_dx = dx_result;
-                       plugin->save_dy = dy_result;
-               }
+               dx_result = -dx_result;
+               dy_result = -dy_result;
        }
-
 #ifdef DEBUG
-printf("MotionCVScan::scan_frame 10 dx=%.2f dy=%.2f\n", 
-(float)this->dx_result / OVERSAMPLE,
-(float)this->dy_result / OVERSAMPLE);
+printf("MotionCVScan::scan_frame 10 dx=%.2f dy=%.2f\n",
+ (float)this->dx_result / OVERSAMPLE, (float)this->dy_result / OVERSAMPLE);
 #endif
 }
 
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
 int64_t MotionCVScan::get_cache(int x, int y)
 {
        int64_t result = -1;
        cache_lock->lock("MotionCVScan::get_cache");
-       for(int i = 0; i < cache.total; i++)
-       {
+       for( int i = 0; i < cache.total; i++ ) {
                MotionCVScanCache *ptr = cache.values[i];
-               if(ptr->x == x && ptr->y == y)
-               {
+               if( ptr->x == x && ptr->y == y ) {
                        result = ptr->difference;
                        break;
                }
@@ -2272,35 +1767,16 @@ void MotionCVScan::put_cache(int x, int y, int64_t difference)
        cache_lock->unlock();
 }
 
-
-
-
-
 MotionCVScanCache::MotionCVScanCache(int x, int y, int64_t difference)
 {
-       this->x = x;
-       this->y = y;
+       this->x = x;  this->y = y;
        this->difference = difference;
 }
 
-
-
-
-
-
-
-
-
-
-
-
-
-
 RotateCVScanPackage::RotateCVScanPackage()
 {
 }
 
-
 RotateCVScanUnit::RotateCVScanUnit(RotateCVScan *server, MotionCVMain *plugin)
  : LoadClient(server)
 {
@@ -2318,34 +1794,30 @@ RotateCVScanUnit::~RotateCVScanUnit()
 
 void RotateCVScanUnit::process_package(LoadPackage *package)
 {
-       if(server->skip) return;
-       RotateCVScanPackage *pkg = (RotateCVScanPackage*)package;
+       if( server->skip )
+               return;
+       RotateCVScanPackage *pkg = (RotateCVScanPackage *) package;
 
-       if((pkg->difference = server->get_cache(pkg->angle)) < 0)
-       {
+       if( (pkg->difference = server->get_cache(pkg->angle)) < 0 ) {
 //printf("RotateCVScanUnit::process_package 1\n");
                int color_model = server->previous_frame->get_color_model();
                int pixel_size = BC_CModels::calculate_pixelsize(color_model);
                int row_bytes = server->previous_frame->get_bytes_per_line();
 
-               if(!rotater)
+               if( !rotater )
                        rotater = new AffineEngine(1, 1);
-               if(!temp) temp = new VFrame(
-                       server->previous_frame->get_w(),
-                       server->previous_frame->get_h(),
-                       color_model);
-
+               if( !temp )
+                       temp = new VFrame(server->previous_frame->get_w(),
+                                       server->previous_frame->get_h(),
+                                       color_model);
 
 // RotateCV original block size
-               rotater->set_viewport(server->block_x1, 
-                       server->block_y1,
+               rotater->set_viewport(server->block_x1, server->block_y1,
                        server->block_x2 - server->block_x1,
                        server->block_y2 - server->block_y1);
                rotater->set_pivot(server->block_x, server->block_y);
 //pkg->angle = 2;
-               rotater->rotate(temp,
-                       server->previous_frame,
-                       pkg->angle);
+               rotater->rotate(temp, server->previous_frame, pkg->angle);
 // Clamp coordinates
                int x1 = server->scan_x;
                int y1 = server->scan_y;
@@ -2355,70 +1827,34 @@ void RotateCVScanUnit::process_package(LoadPackage *package)
                y2 = MIN(temp->get_h(), y2);
                x2 = MIN(server->current_frame->get_w(), x2);
                y2 = MIN(server->current_frame->get_h(), y2);
-               x1 = MAX(0, x1);
-               y1 = MAX(0, y1);
+               x1 = MAX(0, x1);  y1 = MAX(0, y1);
 
-               if(x2 > x1 && y2 > y1)
-               {
+               if( x2 > x1 && y2 > y1 ) {
                        pkg->difference = plugin->abs_diff(
                                temp->get_rows()[y1] + x1 * pixel_size,
-                               server->current_frame->get_rows()[y1] + x1 * pixel_size,
-                               row_bytes,
-                               x2 - x1,
-                               y2 - y1,
-                               color_model);
+                               server->current_frame-> get_rows()[y1] + x1 * pixel_size,
+                               row_bytes, x2 - x1, y2 - y1, color_model);
 //printf("RotateCVScanUnit::process_package %d\n", __LINE__);
                        server->put_cache(pkg->angle, pkg->difference);
-                }
-
-// printf("RotateCVScanUnit::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=%lld\n", 
-// server->block_x1, 
-// server->block_y1,
-// server->block_x2 - server->block_x1,
-// server->block_y2 - server->block_y1,
-// server->block_x,
-// server->block_y,
-// pkg->angle, 
-// server->scan_w,
-// server->scan_h,
+               }
+//printf("RotateCVScanUnit::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=%lld\n",
+// server->block_x1, server->block_y1,
+// server->block_x2 - server->block_x1, server->block_y2 - server->block_y1,
+// server->block_x, server->block_y, pkg->angle, server->scan_w, server->scan_h,
 // pkg->difference);
        }
 }
 
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-RotateCVScan::RotateCVScan(MotionCVMain *plugin, 
-       int total_clients, 
-       int total_packages)
- : LoadServer(
-//1, 1 
-total_clients, total_packages 
-)
+RotateCVScan::RotateCVScan(MotionCVMain *plugin,
+               int total_clients, int total_packages)
+:LoadServer( //1, 1
+               total_clients, total_packages)
 {
        this->plugin = plugin;
        cache_lock = new Mutex("RotateCVScan::cache_lock");
 }
 
-
 RotateCVScan::~RotateCVScan()
 {
        delete cache_lock;
@@ -2426,54 +1862,45 @@ RotateCVScan::~RotateCVScan()
 
 void RotateCVScan::init_packages()
 {
-       for(int i = 0; i < get_total_packages(); i++)
-       {
-               RotateCVScanPackage *pkg = (RotateCVScanPackage*)get_package(i);
-               pkg->angle = i * 
-                       (scan_angle2 - scan_angle1) / 
-                       (total_steps - 1) + 
-                       scan_angle1;
+       for( int i = 0; i < get_total_packages(); i++ ) {
+               RotateCVScanPackage *pkg = (RotateCVScanPackage *) get_package(i);
+               pkg->angle = i * (scan_angle2 - scan_angle1) / (total_steps - 1)
+                       + scan_angle1;
        }
 }
 
-LoadClientRotateCVScan::new_client()
+LoadClient *RotateCVScan::new_client()
 {
        return new RotateCVScanUnit(this, plugin);
 }
 
-LoadPackageRotateCVScan::new_package()
+LoadPackage *RotateCVScan::new_package()
 {
        return new RotateCVScanPackage;
 }
 
-
 float RotateCVScan::scan_frame(VFrame *previous_frame,
-       VFrame *current_frame,
-       int block_x,
-       int block_y)
+               VFrame *current_frame, int block_x, int block_y)
 {
        skip = 0;
        this->block_x = block_x;
        this->block_y = block_y;
 
-       switch(plugin->config.mode2)
-       {
-               case MotionCVConfig::NO_CALCULATE:
-                       result = 0;
-                       skip = 1;
-                       break;
+       switch( plugin->config.mode2 ) {
+       case MotionCVConfig::NO_CALCULATE:
+               result = 0;
+               skip = 1;
+               break;
 
-               case MotionCVConfig::LOAD:
-               {
-                       if( plugin->load_ok ) {
-                               result = plugin->load_dt;
-                               skip = 1;
-                       }
-                       break;
+       case MotionCVConfig::LOAD:
+       case MotionCVConfig::SAVE:
+               if( plugin->load_ok ) {
+                       result = plugin->load_dt;
+                       skip = 1;
                }
+               break;
        }
 
-
        this->previous_frame = previous_frame;
        this->current_frame = current_frame;
        int w = current_frame->get_w();
@@ -2481,17 +1908,16 @@ float RotateCVScan::scan_frame(VFrame *previous_frame,
        int block_w = w * plugin->config.rotation_block_w / 100;
        int block_h = h * plugin->config.rotation_block_h / 100;
 
-       if(this->block_x - block_w / 2 < 0) block_w = this->block_x * 2;
-       if(this->block_y - block_h / 2 < 0) block_h = this->block_y * 2;
-       if(this->block_x + block_w / 2 > w) block_w = (w - this->block_x) * 2;
-       if(this->block_y + block_h / 2 > h) block_h = (h - this->block_y) * 2;
+       if( this->block_x - block_w / 2 < 0 ) block_w = this->block_x * 2;
+       if( this->block_y - block_h / 2 < 0 ) block_h = this->block_y * 2;
+       if( this->block_x + block_w / 2 > w ) block_w = (w - this->block_x) * 2;
+       if( this->block_y + block_h / 2 > h ) block_h = (h - this->block_y) * 2;
 
        block_x1 = this->block_x - block_w / 2;
        block_x2 = this->block_x + block_w / 2;
        block_y1 = this->block_y - block_h / 2;
        block_y2 = this->block_y + block_h / 2;
 
-
 // Calculate the maximum area available to scan after rotation.
 // Must be calculated from the starting range because of cache.
 // Get coords of rectangle after rotation.
@@ -2514,14 +1940,11 @@ float RotateCVScan::scan_frame(VFrame *previous_frame,
        double max_area1 = 0;
        //double max_x1 = 0;
        double max_y1 = 0;
-       for(double x = x1; x < x2; x++)
-       {
+       for( double x = x1; x < x2; x++ ) {
                double y = y1 + (y2 - y1) * (x - x1) / (x2 - x1);
-               if(x >= center_x && x < block_x2 && y >= block_y1 && y < center_y)
-               {
+               if( x >= center_x && x < block_x2 && y >= block_y1 && y < center_y ) {
                        double area = fabs(x - center_x) * fabs(y - center_y);
-                       if(area > max_area1)
-                       {
+                       if( area > max_area1 ) {
                                max_area1 = area;
                                //max_x1 = x;
                                max_y1 = y;
@@ -2533,14 +1956,11 @@ float RotateCVScan::scan_frame(VFrame *previous_frame,
        double max_area2 = 0;
        double max_x2 = 0;
        //double max_y2 = 0;
-       for(double y = y1; y < y3; y++)
-       {
+       for( double y = y1; y < y3; y++ ) {
                double x = x1 + (x3 - x1) * (y - y1) / (y3 - y1);
-               if(x >= block_x1 && x < center_x && y >= block_y1 && y < center_y)
-               {
+               if( x >= block_x1 && x < center_x && y >= block_y1 && y < center_y ) {
                        double area = fabs(x - center_x) * fabs(y - center_y);
-                       if(area > max_area2)
-                       {
+                       if( area > max_area2 ) {
                                max_area2 = area;
                                max_x2 = x;
                                //max_y2 = y;
@@ -2557,7 +1977,7 @@ float RotateCVScan::scan_frame(VFrame *previous_frame,
        scan_h = (int)(fabs(max_y - center_y) * 2);
        scan_x = (int)(center_x - scan_w / 2);
        scan_y = (int)(center_y - scan_h / 2);
-// printf("RotateCVScan::scan_frame center=%d,%d scan=%d,%d %dx%d\n", 
+// printf("RotateCVScan::scan_frame center=%d,%d scan=%d,%d %dx%d\n",
 // this->block_x, this->block_y, scan_x, scan_y, scan_w, scan_h);
 // printf("    angle_range=%f block= %d,%d,%d,%d\n", max_angle, block_x1, block_y1, block_x2, block_y2);
 
@@ -2572,30 +1992,24 @@ printf("RotateCVScan::scan_frame min_angle=%f\n", min_angle * 360 / 2 / M_PI);
 #endif
 
        cache.remove_all_objects();
-       if(!skip)
-       {
+       if( !skip ) {
 // Initial search range
                float angle_range = (float)plugin->config.rotation_range;
                result = 0;
                total_steps = plugin->config.rotate_positions;
 
-
-               while(angle_range >= min_angle * total_steps)
-               {
+               while( angle_range >= min_angle * total_steps ) {
                        scan_angle1 = result - angle_range;
                        scan_angle2 = result + angle_range;
 
-
                        set_package_count(total_steps);
 //set_package_count(1);
                        process_packages();
 
                        int64_t min_difference = -1;
-                       for(int i = 0; i < get_total_packages(); i++)
-                       {
-                               RotateCVScanPackage *pkg = (RotateCVScanPackage*)get_package(i);
-                               if(pkg->difference < min_difference || min_difference == -1)
-                               {
+                       for( int i = 0; i < get_total_packages(); i++ ) {
+                               RotateCVScanPackage *pkg = (RotateCVScanPackage *) get_package(i);
+                               if( pkg->difference < min_difference || min_difference == -1 ) {
                                        min_difference = pkg->difference;
                                        result = pkg->angle;
                                }
@@ -2606,16 +2020,15 @@ printf("RotateCVScan::scan_frame min_angle=%f\n", min_angle * 360 / 2 / M_PI);
 
 //break;
                }
+       }
 
-               if(plugin->config.mode2 == MotionCVConfig::SAVE) {
-                       plugin->save_dt = result;
-               }
+       if( plugin->config.mode2 == MotionCVConfig::SAVE ) {
+               plugin->save_dt = result;
        }
 
 #ifdef DEBUG
 printf("RotateCVScan::scan_frame 10 angle=%f\n", result);
 #endif
-
        return result;
 }
 
@@ -2623,11 +2036,9 @@ int64_t RotateCVScan::get_cache(float angle)
 {
        int64_t result = -1;
        cache_lock->lock("RotateCVScan::get_cache");
-       for(int i = 0; i < cache.total; i++)
-       {
+       for( int i = 0; i < cache.total; i++ ) {
                RotateCVScanCache *ptr = cache.values[i];
-               if(fabs(ptr->angle - angle) <= MIN_ANGLE)
-               {
+               if( fabs(ptr->angle - angle) <= MIN_ANGLE ) {
                        result = ptr->difference;
                        break;
                }
@@ -2644,14 +2055,6 @@ void RotateCVScan::put_cache(float angle, int64_t difference)
        cache_lock->unlock();
 }
 
-
-
-
-
-
-
-
-
 RotateCVScanCache::RotateCVScanCache(float angle, int64_t difference)
 {
        this->angle = angle;
@@ -2659,4 +2062,3 @@ RotateCVScanCache::RotateCVScanCache(float angle, int64_t difference)
 }
 
 
-
diff --git a/cinelerra-5.1/plugins/motion-cv/motion-cv.C.orig b/cinelerra-5.1/plugins/motion-cv/motion-cv.C.orig
deleted file mode 100644 (file)
index a5eb191..0000000
+++ /dev/null
@@ -1,2731 +0,0 @@
-
-/*
- * CINELERRA
- * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
- * 
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- * 
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- * 
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- * 
- */
-
-#include "affine.h"
-#include "bcdisplayinfo.h"
-#include "clip.h"
-#include "bchash.h"
-#include "bcsignals.h"
-#include "filexml.h"
-#include "keyframe.h"
-#include "language.h"
-#include "motion-cv.h"
-#include "motionwindow-cv.h"
-#include "mutex.h"
-#include "overlayframe.h"
-#include "rotateframe.h"
-#include "transportque.h"
-
-
-#include <errno.h>
-#include <unistd.h>
-
-REGISTER_PLUGIN(MotionCVMain)
-
-//#undef DEBUG
-
-#ifndef DEBUG
-#define DEBUG
-#endif
-
-
-MotionCVConfig::MotionCVConfig()
-{
-       global_range_w = 5;
-       global_range_h = 5;
-       rotation_range = 5;
-       block_count = 1;
-       global_block_w = MIN_BLOCK;
-       global_block_h = MIN_BLOCK;
-       rotation_block_w = MIN_BLOCK;
-       rotation_block_h = MIN_BLOCK;
-       block_x = 50;
-       block_y = 50;
-       global_positions = 256;
-       rotate_positions = 4;
-       magnitude = 100;
-       return_speed = 0;
-       mode1 = STABILIZE;
-       global = 1;
-       rotate = 1;
-       addtrackedframeoffset = 0;
-       mode2 = RECALCULATE;
-       draw_vectors = 1;
-       mode3 = MotionCVConfig::TRACK_SINGLE;
-       track_frame = 0;
-       bottom_is_master = 1;
-       horizontal_only = 0;
-       vertical_only = 0;
-}
-
-void MotionCVConfig::boundaries()
-{
-       CLAMP(global_range_w, MIN_RADIUS, MAX_RADIUS);
-       CLAMP(global_range_h, MIN_RADIUS, MAX_RADIUS);
-       CLAMP(rotation_range, MIN_ROTATION, MAX_ROTATION);
-       CLAMP(block_count, MIN_BLOCKS, MAX_BLOCKS);
-       CLAMP(global_block_w, MIN_BLOCK, MAX_BLOCK);
-       CLAMP(global_block_h, MIN_BLOCK, MAX_BLOCK);
-       CLAMP(rotation_block_w, MIN_BLOCK, MAX_BLOCK);
-       CLAMP(rotation_block_h, MIN_BLOCK, MAX_BLOCK);
-}
-
-int MotionCVConfig::equivalent(MotionCVConfig &that)
-{
-       return global_range_w == that.global_range_w &&
-               global_range_h == that.global_range_h &&
-               rotation_range == that.rotation_range &&
-               mode1 == that.mode1 &&
-               global == that.global &&
-               rotate == that.rotate &&
-               addtrackedframeoffset == that.addtrackedframeoffset &&
-               draw_vectors == that.draw_vectors &&
-               block_count == that.block_count &&
-               global_block_w == that.global_block_w &&
-               global_block_h == that.global_block_h &&
-               rotation_block_w == that.rotation_block_w &&
-               rotation_block_h == that.rotation_block_h &&
-               EQUIV(block_x, that.block_x) &&
-               EQUIV(block_y, that.block_y) &&
-               global_positions == that.global_positions &&
-               rotate_positions == that.rotate_positions &&
-               magnitude == that.magnitude &&
-               return_speed == that.return_speed &&
-               mode3 == that.mode3 &&
-               track_frame == that.track_frame &&
-               bottom_is_master == that.bottom_is_master &&
-               horizontal_only == that.horizontal_only &&
-               vertical_only == that.vertical_only;
-}
-
-void MotionCVConfig::copy_from(MotionCVConfig &that)
-{
-       global_range_w = that.global_range_w;
-       global_range_h = that.global_range_h;
-       rotation_range = that.rotation_range;
-       mode1 = that.mode1;
-       global = that.global;
-       rotate = that.rotate;
-       addtrackedframeoffset = that.addtrackedframeoffset;
-       mode2 = that.mode2;
-       draw_vectors = that.draw_vectors;
-       block_count = that.block_count;
-       block_x = that.block_x;
-       block_y = that.block_y;
-       global_positions = that.global_positions;
-       rotate_positions = that.rotate_positions;
-       global_block_w = that.global_block_w;
-       global_block_h = that.global_block_h;
-       rotation_block_w = that.rotation_block_w;
-       rotation_block_h = that.rotation_block_h;
-       magnitude = that.magnitude;
-       return_speed = that.return_speed;
-       mode3 = that.mode3;
-       track_frame = that.track_frame;
-       bottom_is_master = that.bottom_is_master;
-       horizontal_only = that.horizontal_only;
-       vertical_only = that.vertical_only;
-}
-
-void MotionCVConfig::interpolate(MotionCVConfig &prev, 
-       MotionCVConfig &next, 
-       int64_t prev_frame, 
-       int64_t next_frame, 
-       int64_t current_frame)
-{
-       //double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
-       //double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
-       this->block_x = prev.block_x;
-       this->block_y = prev.block_y;
-       global_range_w = prev.global_range_w;
-       global_range_h = prev.global_range_h;
-       rotation_range = prev.rotation_range;
-       mode1 = prev.mode1;
-       global = prev.global;
-       rotate = prev.rotate;
-       addtrackedframeoffset = prev.addtrackedframeoffset;
-       mode2 = prev.mode2;
-       draw_vectors = prev.draw_vectors;
-       block_count = prev.block_count;
-       global_positions = prev.global_positions;
-       rotate_positions = prev.rotate_positions;
-       global_block_w = prev.global_block_w;
-       global_block_h = prev.global_block_h;
-       rotation_block_w = prev.rotation_block_w;
-       rotation_block_h = prev.rotation_block_h;
-       magnitude = prev.magnitude;
-       return_speed = prev.return_speed;
-       mode3 = prev.mode3;
-       track_frame = prev.track_frame;
-       bottom_is_master = prev.bottom_is_master;
-       horizontal_only = prev.horizontal_only;
-       vertical_only = prev.vertical_only;
-}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-MotionCVMain::MotionCVMain(PluginServer *server)
- : PluginVClient(server)
-{
-       engine = 0;
-       rotate_engine = 0;
-       motion_rotate = 0;
-       total_dx = 0;
-       total_dy = 0;
-       total_angle = 0;
-       overlayer = 0;
-       search_area = 0;
-       search_size = 0;
-       temp_frame = 0;
-       previous_frame_number = -1;
-
-       prev_global_ref = 0;
-       current_global_ref = 0;
-       global_target_src = 0;
-       global_target_dst = 0;
-
-       prev_rotate_ref = 0;
-       current_rotate_ref = 0;
-       rotate_target_src = 0;
-       rotate_target_dst = 0;
-}
-
-MotionCVMain::~MotionCVMain()
-{
-
-       delete engine;
-       delete overlayer;
-       delete [] search_area;
-       delete temp_frame;
-       delete rotate_engine;
-       delete motion_rotate;
-
-
-       delete prev_global_ref;
-       delete current_global_ref;
-       delete global_target_src;
-       delete global_target_dst;
-
-       delete prev_rotate_ref;
-       delete current_rotate_ref;
-       delete rotate_target_src;
-       delete rotate_target_dst;
-}
-
-const char* MotionCVMain::plugin_title() { return _("MotionCV"); }
-int MotionCVMain::is_realtime() { return 1; }
-int MotionCVMain::is_multichannel() { return 1; }
-
-
-NEW_WINDOW_MACRO(MotionCVMain, MotionCVWindow)
-
-LOAD_CONFIGURATION_MACRO(MotionCVMain, MotionCVConfig)
-
-
-
-void MotionCVMain::update_gui()
-{
-       if(thread)
-       {
-               if(load_configuration())
-               {
-                       thread->window->lock_window("MotionCVMain::update_gui");
-                       MotionCVWindow *window = (MotionCVWindow *)thread->window;
-                       
-                       char string[BCTEXTLEN];
-                       sprintf(string, "%d", config.global_positions);
-                       window->global_search_positions->set_text(string);
-                       sprintf(string, "%d", config.rotate_positions);
-                       window->rotation_search_positions->set_text(string);
-
-                       window->global_block_w->update(config.global_block_w);
-                       window->global_block_h->update(config.global_block_h);
-                       window->rotation_block_w->update(config.rotation_block_w);
-                       window->rotation_block_h->update(config.rotation_block_h);
-                       window->block_x->update(config.block_x);
-                       window->block_y->update(config.block_y);
-                       window->block_x_text->update((float)config.block_x);
-                       window->block_y_text->update((float)config.block_y);
-                       window->magnitude->update(config.magnitude);
-                       window->return_speed->update(config.return_speed);
-
-
-                       window->track_single->update(config.mode3 == MotionCVConfig::TRACK_SINGLE);
-                       window->track_frame_number->update(config.track_frame);
-                       window->track_previous->update(config.mode3 == MotionCVConfig::TRACK_PREVIOUS);
-                       window->previous_same->update(config.mode3 == MotionCVConfig::PREVIOUS_SAME_BLOCK);
-                       if(config.mode3 != MotionCVConfig::TRACK_SINGLE)
-                               window->track_frame_number->disable();
-                       else
-                               window->track_frame_number->enable();
-
-                       window->mode1->set_text(
-                               Mode1::to_text(config.mode1));
-                       window->mode2->set_text(
-                               Mode2::to_text(config.mode2));
-                       window->mode3->set_text(
-                               Mode3::to_text(config.horizontal_only, config.vertical_only));
-                       window->master_layer->set_text(
-                               MasterLayer::to_text(config.bottom_is_master));
-
-
-                       window->update_mode();
-                       window->unlock_window();
-               }
-       }
-}
-
-
-
-
-void MotionCVMain::save_data(KeyFrame *keyframe)
-{
-       FileXML output;
-
-// cause data to be stored directly in text
-       output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
-       output.tag.set_title("MOTION");
-
-       output.tag.set_property("BLOCK_COUNT", config.block_count);
-       output.tag.set_property("GLOBAL_POSITIONS", config.global_positions);
-       output.tag.set_property("ROTATE_POSITIONS", config.rotate_positions);
-       output.tag.set_property("GLOBAL_BLOCK_W", config.global_block_w);
-       output.tag.set_property("GLOBAL_BLOCK_H", config.global_block_h);
-       output.tag.set_property("ROTATION_BLOCK_W", config.rotation_block_w);
-       output.tag.set_property("ROTATION_BLOCK_H", config.rotation_block_h);
-       output.tag.set_property("BLOCK_X", config.block_x);
-       output.tag.set_property("BLOCK_Y", config.block_y);
-       output.tag.set_property("GLOBAL_RANGE_W", config.global_range_w);
-       output.tag.set_property("GLOBAL_RANGE_H", config.global_range_h);
-       output.tag.set_property("ROTATION_RANGE", config.rotation_range);
-       output.tag.set_property("MAGNITUDE", config.magnitude);
-       output.tag.set_property("RETURN_SPEED", config.return_speed);
-       output.tag.set_property("MODE1", config.mode1);
-       output.tag.set_property("GLOBAL", config.global);
-       output.tag.set_property("ROTATE", config.rotate);
-       output.tag.set_property("ADDTRACKEDFRAMEOFFSET", config.addtrackedframeoffset);
-       output.tag.set_property("MODE2", config.mode2);
-       output.tag.set_property("DRAW_VECTORS", config.draw_vectors);
-       output.tag.set_property("MODE3", config.mode3);
-       output.tag.set_property("TRACK_FRAME", config.track_frame);
-       output.tag.set_property("BOTTOM_IS_MASTER", config.bottom_is_master);
-       output.tag.set_property("HORIZONTAL_ONLY", config.horizontal_only);
-       output.tag.set_property("VERTICAL_ONLY", config.vertical_only);
-       output.append_tag();
-       output.tag.set_title("/MOTION");
-       output.append_tag();
-       output.terminate_string();
-}
-
-void MotionCVMain::read_data(KeyFrame *keyframe)
-{
-       FileXML input;
-
-       input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
-
-       int result = 0;
-
-       while(!result)
-       {
-               result = input.read_tag();
-
-               if(!result)
-               {
-                       if(input.tag.title_is("MOTION"))
-                       {
-                               config.block_count = input.tag.get_property("BLOCK_COUNT", config.block_count);
-                               config.global_positions = input.tag.get_property("GLOBAL_POSITIONS", config.global_positions);
-                               config.rotate_positions = input.tag.get_property("ROTATE_POSITIONS", config.rotate_positions);
-                               config.global_block_w = input.tag.get_property("GLOBAL_BLOCK_W", config.global_block_w);
-                               config.global_block_h = input.tag.get_property("GLOBAL_BLOCK_H", config.global_block_h);
-                               config.rotation_block_w = input.tag.get_property("ROTATION_BLOCK_W", config.rotation_block_w);
-                               config.rotation_block_h = input.tag.get_property("ROTATION_BLOCK_H", config.rotation_block_h);
-                               config.block_x = input.tag.get_property("BLOCK_X", config.block_x);
-                               config.block_y = input.tag.get_property("BLOCK_Y", config.block_y);
-                               config.global_range_w = input.tag.get_property("GLOBAL_RANGE_W", config.global_range_w);
-                               config.global_range_h = input.tag.get_property("GLOBAL_RANGE_H", config.global_range_h);
-                               config.rotation_range = input.tag.get_property("ROTATION_RANGE", config.rotation_range);
-                               config.magnitude = input.tag.get_property("MAGNITUDE", config.magnitude);
-                               config.return_speed = input.tag.get_property("RETURN_SPEED", config.return_speed);
-                               config.mode1 = input.tag.get_property("MODE1", config.mode1);
-                               config.global = input.tag.get_property("GLOBAL", config.global);
-                               config.rotate = input.tag.get_property("ROTATE", config.rotate);
-                               config.addtrackedframeoffset = input.tag.get_property("ADDTRACKEDFRAMEOFFSET", config.addtrackedframeoffset);
-                               config.mode2 = input.tag.get_property("MODE2", config.mode2);
-                               config.draw_vectors = input.tag.get_property("DRAW_VECTORS", config.draw_vectors);
-                               config.mode3 = input.tag.get_property("MODE3", config.mode3);
-                               config.track_frame = input.tag.get_property("TRACK_FRAME", config.track_frame);
-                               config.bottom_is_master = input.tag.get_property("BOTTOM_IS_MASTER", config.bottom_is_master);
-                               config.horizontal_only = input.tag.get_property("HORIZONTAL_ONLY", config.horizontal_only);
-                               config.vertical_only = input.tag.get_property("VERTICAL_ONLY", config.vertical_only);
-                       }
-               }
-       }
-       config.boundaries();
-}
-
-
-
-
-
-
-
-
-
-void MotionCVMain::allocate_temp(int w, int h, int color_model)
-{
-       if(temp_frame && 
-               (temp_frame->get_w() != w ||
-               temp_frame->get_h() != h))
-       {
-               delete temp_frame;
-               temp_frame = 0;
-       }
-       if(!temp_frame)
-               temp_frame = new VFrame(w, h, color_model);
-}
-
-
-
-void MotionCVMain::process_global()
-{
-       if(!engine) engine = new MotionCVScan(this,
-               PluginClient::get_project_smp() + 1,
-               PluginClient::get_project_smp() + 1);
-
-// Get the current motion vector between the previous and current frame
-       engine->scan_frame(current_global_ref, prev_global_ref);
-       current_dx = engine->dx_result;
-       current_dy = engine->dy_result;
-
-// Add current motion vector to accumulation vector.
-       if(config.mode3 != MotionCVConfig::TRACK_SINGLE)
-       {
-// Retract over time
-               total_dx = (int64_t)total_dx * (100 - config.return_speed) / 100;
-               total_dy = (int64_t)total_dy * (100 - config.return_speed) / 100;
-               total_dx += engine->dx_result;
-               total_dy += engine->dy_result;
-       }
-       else
-// Make accumulation vector current
-       {
-               total_dx = engine->dx_result;
-               total_dy = engine->dy_result;
-       }
-
-// Clamp accumulation vector
-       if(config.magnitude < 100)
-       {
-               //int block_w = (int64_t)config.global_block_w * 
-               //              current_global_ref->get_w() / 100;
-               //int block_h = (int64_t)config.global_block_h * 
-               //              current_global_ref->get_h() / 100;
-               int block_x_orig = (int64_t)(config.block_x * 
-                       current_global_ref->get_w() / 
-                       100);
-               int block_y_orig = (int64_t)(config.block_y *
-                       current_global_ref->get_h() / 
-                       100);
-
-               int max_block_x = (int64_t)(current_global_ref->get_w() - block_x_orig) *
-                       OVERSAMPLE * 
-                       config.magnitude / 
-                       100;
-               int max_block_y = (int64_t)(current_global_ref->get_h() - block_y_orig) *
-                       OVERSAMPLE *
-                       config.magnitude / 
-                       100;
-               int min_block_x = (int64_t)-block_x_orig * 
-                       OVERSAMPLE * 
-                       config.magnitude / 
-                       100;
-               int min_block_y = (int64_t)-block_y_orig * 
-                       OVERSAMPLE * 
-                       config.magnitude / 
-                       100;
-
-               CLAMP(total_dx, min_block_x, max_block_x);
-               CLAMP(total_dy, min_block_y, max_block_y);
-       }
-
-#ifdef DEBUG
-printf("MotionCVMain::process_global 2 total_dx=%.02f total_dy=%.02f\n", 
-(float)total_dx / OVERSAMPLE,
-(float)total_dy / OVERSAMPLE);
-#endif
-
-       if(config.mode3 != MotionCVConfig::TRACK_SINGLE && !config.rotate)
-       {
-// Transfer current reference frame to previous reference frame and update
-// counter.  Must wait for rotate to compare.
-               prev_global_ref->copy_from(current_global_ref);
-               previous_frame_number = get_source_position();
-       }
-
-// Decide what to do with target based on requested operation
-       int interpolation = NEAREST_NEIGHBOR;
-       float dx = 0;
-       float dy = 0;
-       switch(config.mode1)
-       {
-               case MotionCVConfig::NOTHING:
-                       global_target_dst->copy_from(global_target_src);
-                       break;
-               case MotionCVConfig::TRACK_PIXEL:
-                       interpolation = NEAREST_NEIGHBOR;
-                       dx = (int)(total_dx / OVERSAMPLE);
-                       dy = (int)(total_dy / OVERSAMPLE);
-                       break;
-               case MotionCVConfig::STABILIZE_PIXEL:
-                       interpolation = NEAREST_NEIGHBOR;
-                       dx = -(int)(total_dx / OVERSAMPLE);
-                       dy = -(int)(total_dy / OVERSAMPLE);
-                       break;
-                       break;
-               case MotionCVConfig::TRACK:
-                       interpolation = CUBIC_LINEAR;
-                       dx = (float)total_dx / OVERSAMPLE;
-                       dy = (float)total_dy / OVERSAMPLE;
-                       break;
-               case MotionCVConfig::STABILIZE:
-                       interpolation = CUBIC_LINEAR;
-                       dx = -(float)total_dx / OVERSAMPLE;
-                       dy = -(float)total_dy / OVERSAMPLE;
-                       break;
-       }
-
-
-       if(config.mode1 != MotionCVConfig::NOTHING)
-       {
-               if(!overlayer) 
-                       overlayer = new OverlayFrame(PluginClient::get_project_smp() + 1);
-               global_target_dst->clear_frame();
-               overlayer->overlay(global_target_dst,
-                       global_target_src,
-                       0,
-                       0,
-                       global_target_src->get_w(),
-                       global_target_src->get_h(),
-                       dx,
-                       dy,
-                       (float)global_target_src->get_w() + dx,
-                       (float)global_target_src->get_h() + dy,
-                       1,
-                       TRANSFER_REPLACE,
-                       interpolation);
-       }
-}
-
-
-
-void MotionCVMain::process_rotation()
-{
-       int block_x;
-       int block_y;
-
-// Convert the previous global reference into the previous rotation reference.
-// Convert global target destination into rotation target source.
-       if(config.global)
-       {
-               if(!overlayer) 
-                       overlayer = new OverlayFrame(PluginClient::get_project_smp() + 1);
-               float dx;
-               float dy;
-               if(config.mode3 == MotionCVConfig::TRACK_SINGLE)
-               {
-                       dx = (float)total_dx / OVERSAMPLE;
-                       dy = (float)total_dy / OVERSAMPLE;
-               }
-               else
-               {
-                       dx = (float)current_dx / OVERSAMPLE;
-                       dy = (float)current_dy / OVERSAMPLE;
-               }
-
-               prev_rotate_ref->clear_frame();
-               overlayer->overlay(prev_rotate_ref,
-                       prev_global_ref,
-                       0,
-                       0,
-                       prev_global_ref->get_w(),
-                       prev_global_ref->get_h(),
-                       dx,
-                       dy,
-                       (float)prev_global_ref->get_w() + dx,
-                       (float)prev_global_ref->get_h() + dy,
-                       1,
-                       TRANSFER_REPLACE,
-                       CUBIC_LINEAR);
-// Pivot is destination global position
-               block_x = (int)(prev_rotate_ref->get_w() * 
-                       config.block_x / 
-                       100 +
-                       (float)total_dx / 
-                       OVERSAMPLE);
-               block_y = (int)(prev_rotate_ref->get_h() * 
-                       config.block_y / 
-                       100 +
-                       (float)total_dy / 
-                       OVERSAMPLE);
-// Use the global target output as the rotation target input
-               rotate_target_src->copy_from(global_target_dst);
-// Transfer current reference frame to previous reference frame for global.
-               if(config.mode3 != MotionCVConfig::TRACK_SINGLE)
-               {
-                       prev_global_ref->copy_from(current_global_ref);
-                       previous_frame_number = get_source_position();
-               }
-       }
-       else
-       {
-// Pivot is fixed
-               block_x = (int)(prev_rotate_ref->get_w() * 
-                       config.block_x / 
-                       100);
-               block_y = (int)(prev_rotate_ref->get_h() * 
-                       config.block_y / 
-                       100);
-       }
-
-
-
-// Get rotation
-       if(!motion_rotate)
-               motion_rotate = new RotateCVScan(this, 
-                       get_project_smp() + 1, 
-                       get_project_smp() + 1);
-
-       current_angle = motion_rotate->scan_frame(prev_rotate_ref, 
-               current_rotate_ref,
-               block_x,
-               block_y);
-
-
-
-// Add current rotation to accumulation
-       if(config.mode3 != MotionCVConfig::TRACK_SINGLE)
-       {
-// Retract over time
-               total_angle = total_angle * (100 - config.return_speed) / 100;
-               total_angle += current_angle;
-
-               if(!config.global)
-               {
-// Transfer current reference frame to previous reference frame and update
-// counter.
-                       prev_rotate_ref->copy_from(current_rotate_ref);
-                       previous_frame_number = get_source_position();
-               }
-       }
-       else
-       {
-               total_angle = current_angle;
-       }
-
-#ifdef DEBUG
-printf("MotionCVMain::process_rotation total_angle=%f\n", total_angle);
-#endif
-
-
-// Calculate rotation parameters based on requested operation
-       float angle = 0;
-       switch(config.mode1)
-       {
-               case MotionCVConfig::NOTHING:
-                       rotate_target_dst->copy_from(rotate_target_src);
-                       break;
-               case MotionCVConfig::TRACK:
-               case MotionCVConfig::TRACK_PIXEL:
-                       angle = total_angle;
-                       break;
-               case MotionCVConfig::STABILIZE:
-               case MotionCVConfig::STABILIZE_PIXEL:
-                       angle = -total_angle;
-                       break;
-       }
-
-
-
-       if(config.mode1 != MotionCVConfig::NOTHING)
-       {
-               if(!rotate_engine)
-                       rotate_engine = new AffineEngine(PluginClient::get_project_smp() + 1,
-                               PluginClient::get_project_smp() + 1);
-
-               rotate_target_dst->clear_frame();
-
-// Determine pivot based on a number of factors.
-               switch(config.mode1)
-               {
-                       case MotionCVConfig::TRACK:
-                       case MotionCVConfig::TRACK_PIXEL:
-// Use destination of global tracking.
-                               rotate_engine->set_pivot(block_x, block_y);
-                               break;
-
-                       case MotionCVConfig::STABILIZE:
-                       case MotionCVConfig::STABILIZE_PIXEL:
-                               if(config.global)
-                               {
-// Use origin of global stabilize operation
-                                       rotate_engine->set_pivot((int)(rotate_target_dst->get_w() * 
-                                                       config.block_x / 
-                                                       100),
-                                               (int)(rotate_target_dst->get_h() * 
-                                                       config.block_y / 
-                                                       100));
-                               
-                               }
-                               else
-                               {
-// Use origin
-                                       rotate_engine->set_pivot(block_x, block_y);
-                               }
-                               break;
-               }
-
-
-               rotate_engine->rotate(rotate_target_dst, rotate_target_src, angle);
-// overlayer->overlay(rotate_target_dst,
-//     prev_rotate_ref,
-//     0,
-//     0,
-//     prev_rotate_ref->get_w(),
-//     prev_rotate_ref->get_h(),
-//     0,
-//     0,
-//     prev_rotate_ref->get_w(),
-//     prev_rotate_ref->get_h(),
-//     1,
-//     TRANSFER_NORMAL,
-//     CUBIC_LINEAR);
-// overlayer->overlay(rotate_target_dst,
-//     current_rotate_ref,
-//     0,
-//     0,
-//     prev_rotate_ref->get_w(),
-//     prev_rotate_ref->get_h(),
-//     0,
-//     0,
-//     prev_rotate_ref->get_w(),
-//     prev_rotate_ref->get_h(),
-//     1,
-//     TRANSFER_NORMAL,
-//     CUBIC_LINEAR);
-
-
-       }
-
-
-}
-
-
-
-
-
-
-
-
-
-int MotionCVMain::process_buffer(VFrame **frame,
-       int64_t start_position,
-       double frame_rate)
-{
-       int need_reconfigure = load_configuration();
-       int color_model = frame[0]->get_color_model();
-       w = frame[0]->get_w();
-       h = frame[0]->get_h();
-       
-
-#ifdef DEBUG
-printf("MotionCVMain::process_buffer 1 start_position=%jd\n", start_position);
-#endif
-
-
-// Calculate the source and destination pointers for each of the operations.
-// Get the layer to track motion in.
-       reference_layer = config.bottom_is_master ?
-               PluginClient::total_in_buffers - 1 :
-               0;
-// Get the layer to apply motion in.
-       target_layer = config.bottom_is_master ?
-               0 :
-               PluginClient::total_in_buffers - 1;
-
-
-       output_frame = frame[target_layer];
-
-
-// Get the position of previous reference frame.
-       int64_t actual_previous_number;
-// Skip if match frame not available
-       int skip_current = 0;
-
-
-       if(config.mode3 == MotionCVConfig::TRACK_SINGLE)
-       {
-               actual_previous_number = config.track_frame;
-               if(get_direction() == PLAY_REVERSE)
-                       actual_previous_number++;
-               if(actual_previous_number == start_position)
-                       skip_current = 1;
-       }
-       else
-       {
-               actual_previous_number = start_position;
-               if(get_direction() == PLAY_FORWARD)
-               {
-                       actual_previous_number--;
-                       if(actual_previous_number < get_source_start())
-                               skip_current = 1;
-                       else
-                       {
-                               KeyFrame *keyframe = get_prev_keyframe(start_position, 1);
-                               if(keyframe->position > 0 &&
-                                       actual_previous_number < keyframe->position)
-                                       skip_current = 1;
-                       }
-               }
-               else
-               {
-                       actual_previous_number++;
-                       if(actual_previous_number >= get_source_start() + get_total_len())
-                               skip_current = 1;
-                       else
-                       {
-                               KeyFrame *keyframe = get_next_keyframe(start_position, 1);
-                               if(keyframe->position > 0 &&
-                                       actual_previous_number >= keyframe->position)
-                                       skip_current = 1;
-                       }
-               }
-
-// Only count motion since last keyframe
-               
-
-       }
-
-
-       if(!config.global && !config.rotate) skip_current = 1;
-
-
-
-
-// printf("process_realtime %d %lld %lld\n", 
-// skip_current, 
-// previous_frame_number, 
-// actual_previous_number);
-// Load match frame and reset vectors
-       int need_reload = !skip_current && 
-               (previous_frame_number != actual_previous_number ||
-               need_reconfigure);
-       if(need_reload)
-       {
-               total_dx = 0;
-               total_dy = 0;
-               total_angle = 0;
-               previous_frame_number = actual_previous_number;
-       }
-
-
-       if(skip_current)
-       {
-               total_dx = 0;
-               total_dy = 0;
-               current_dx = 0;
-               current_dy = 0;
-               total_angle = 0;
-               current_angle = 0;
-       }
-
-
-
-
-// Get the global pointers.  Here we walk through the sequence of events.
-       if(config.global)
-       {
-// Assume global only.  Global reads previous frame and compares
-// with current frame to get the current translation.
-// The center of the search area is fixed in compensate mode or
-// the user value + the accumulation vector in track mode.
-               if(!prev_global_ref)
-                       prev_global_ref = new VFrame(w, h, color_model);
-               if(!current_global_ref)
-                       current_global_ref = new VFrame(w, h, color_model);
-
-// Global loads the current target frame into the src and 
-// writes it to the dst frame with desired translation.
-               if(!global_target_src)
-                       global_target_src = new VFrame(w, h, color_model);
-               if(!global_target_dst)
-                       global_target_dst = new VFrame(w, h, color_model);
-
-
-// Load the global frames
-               if(need_reload)
-               {
-                       read_frame(prev_global_ref, 
-                               reference_layer, 
-                               previous_frame_number, 
-                               frame_rate,
-                               0);
-               }
-
-               read_frame(current_global_ref, 
-                       reference_layer, 
-                       start_position, 
-                       frame_rate,
-                       0);
-               read_frame(global_target_src,
-                       target_layer,
-                       start_position,
-                       frame_rate,
-                       0);
-
-
-
-// Global followed by rotate
-               if(config.rotate)
-               {
-// Must translate the previous global reference by the current global
-// accumulation vector to match the current global reference.
-// The center of the search area is always the user value + the accumulation
-// vector.
-                       if(!prev_rotate_ref)
-                               prev_rotate_ref = new VFrame(w, h, color_model);
-// The current global reference is the current rotation reference.
-                       if(!current_rotate_ref)
-                               current_rotate_ref = new VFrame(w, h, color_model);
-                       current_rotate_ref->copy_from(current_global_ref);
-
-// The global target destination is copied to the rotation target source
-// then written to the rotation output with rotation.
-// The pivot for the rotation is the center of the search area 
-// if we're tracking.
-// The pivot is fixed to the user position if we're compensating.
-                       if(!rotate_target_src)
-                               rotate_target_src = new VFrame(w, h, color_model);
-                       if(!rotate_target_dst)
-                               rotate_target_dst = new VFrame(w,h , color_model);
-               }
-       }
-       else
-// Rotation only
-       if(config.rotate)
-       {
-// Rotation reads the previous reference frame and compares it with current 
-// reference frame.
-               if(!prev_rotate_ref)
-                       prev_rotate_ref = new VFrame(w, h, color_model);
-               if(!current_rotate_ref)
-                       current_rotate_ref = new VFrame(w, h, color_model);
-
-// Rotation loads target frame to temporary, rotates it, and writes it to the
-// target frame.  The pivot is always fixed.
-               if(!rotate_target_src)
-                       rotate_target_src = new VFrame(w, h, color_model);
-               if(!rotate_target_dst)
-                       rotate_target_dst = new VFrame(w,h , color_model);
-
-
-// Load the rotate frames
-               if(need_reload)
-               {
-                       read_frame(prev_rotate_ref, 
-                               reference_layer, 
-                               previous_frame_number, 
-                               frame_rate,
-                               0);
-               }
-               read_frame(current_rotate_ref, 
-                       reference_layer, 
-                       start_position, 
-                       frame_rate,
-                       0);
-               read_frame(rotate_target_src,
-                       target_layer,
-                       start_position,
-                       frame_rate,
-                       0);
-       }
-
-
-
-
-
-
-
-
-
-
-       if(!skip_current)
-       {
-// Get position change from previous frame to current frame
-               if(config.global) process_global();
-// Get rotation change from previous frame to current frame
-               if(config.rotate) process_rotation();
-//frame[target_layer]->copy_from(prev_rotate_ref);
-//frame[target_layer]->copy_from(current_rotate_ref);
-       }
-
-
-
-
-
-
-// Transfer the relevant target frame to the output
-       if(!skip_current)
-       {
-               if(config.rotate)
-               {
-                       frame[target_layer]->copy_from(rotate_target_dst);
-               }
-               else
-               {
-                       frame[target_layer]->copy_from(global_target_dst);
-               }
-       }
-       else
-// Read the target destination directly
-       {
-               read_frame(frame[target_layer],
-                       target_layer,
-                       start_position,
-                       frame_rate,
-                       0);
-       }
-
-       if(config.draw_vectors)
-       {
-               draw_vectors(frame[target_layer]);
-       }
-
-#ifdef DEBUG
-printf("MotionCVMain::process_buffer 100\n");
-#endif
-       return 0;
-}
-
-
-void MotionCVMain::clamp_scan(int w, 
-       int h, 
-       int *block_x1,
-       int *block_y1,
-       int *block_x2,
-       int *block_y2,
-       int *scan_x1,
-       int *scan_y1,
-       int *scan_x2,
-       int *scan_y2,
-       int use_absolute)
-{
-// printf("MotionCVMain::clamp_scan 1 w=%d h=%d block=%d %d %d %d scan=%d %d %d %d absolute=%d\n",
-// w,
-// h,
-// *block_x1,
-// *block_y1,
-// *block_x2,
-// *block_y2,
-// *scan_x1,
-// *scan_y1,
-// *scan_x2,
-// *scan_y2,
-// use_absolute);
-
-       if(use_absolute)
-       {
-// scan is always out of range before block.
-               if(*scan_x1 < 0)
-               {
-                       int difference = -*scan_x1;
-                       *block_x1 += difference;
-                       *scan_x1 = 0;
-               }
-
-               if(*scan_y1 < 0)
-               {
-                       int difference = -*scan_y1;
-                       *block_y1 += difference;
-                       *scan_y1 = 0;
-               }
-
-               if(*scan_x2 > w)
-               {
-                       int difference = *scan_x2 - w;
-                       *block_x2 -= difference;
-                       *scan_x2 -= difference;
-               }
-
-               if(*scan_y2 > h)
-               {
-                       int difference = *scan_y2 - h;
-                       *block_y2 -= difference;
-                       *scan_y2 -= difference;
-               }
-
-               CLAMP(*scan_x1, 0, w);
-               CLAMP(*scan_y1, 0, h);
-               CLAMP(*scan_x2, 0, w);
-               CLAMP(*scan_y2, 0, h);
-       }
-       else
-       {
-               if(*scan_x1 < 0)
-               {
-                       int difference = -*scan_x1;
-                       *block_x1 += difference;
-                       *scan_x2 += difference;
-                       *scan_x1 = 0;
-               }
-
-               if(*scan_y1 < 0)
-               {
-                       int difference = -*scan_y1;
-                       *block_y1 += difference;
-                       *scan_y2 += difference;
-                       *scan_y1 = 0;
-               }
-
-               if(*scan_x2 - *block_x1 + *block_x2 > w)
-               {
-                       int difference = *scan_x2 - *block_x1 + *block_x2 - w;
-                       *block_x2 -= difference;
-               }
-
-               if(*scan_y2 - *block_y1 + *block_y2 > h)
-               {
-                       int difference = *scan_y2 - *block_y1 + *block_y2 - h;
-                       *block_y2 -= difference;
-               }
-
-//             CLAMP(*scan_x1, 0, w - (*block_x2 - *block_x1));
-//             CLAMP(*scan_y1, 0, h - (*block_y2 - *block_y1));
-//             CLAMP(*scan_x2, 0, w - (*block_x2 - *block_x1));
-//             CLAMP(*scan_y2, 0, h - (*block_y2 - *block_y1));
-       }
-
-// Sanity checks which break the calculation but should never happen if the
-// center of the block is inside the frame.
-       CLAMP(*block_x1, 0, w);
-       CLAMP(*block_x2, 0, w);
-       CLAMP(*block_y1, 0, h);
-       CLAMP(*block_y2, 0, h);
-
-// printf("MotionCVMain::clamp_scan 2 w=%d h=%d block=%d %d %d %d scan=%d %d %d %d absolute=%d\n",
-// w,
-// h,
-// *block_x1,
-// *block_y1,
-// *block_x2,
-// *block_y2,
-// *scan_x1,
-// *scan_y1,
-// *scan_x2,
-// *scan_y2,
-// use_absolute);
-}
-
-
-
-void MotionCVMain::draw_vectors(VFrame *frame)
-{
-       int w = frame->get_w();
-       int h = frame->get_h();
-       int global_x1, global_y1;
-       int global_x2, global_y2;
-       int block_x, block_y;
-       int block_w, block_h;
-       int block_x1, block_y1;
-       int block_x2, block_y2;
-       int block_x3, block_y3;
-       int block_x4, block_y4;
-       int search_w, search_h;
-       int search_x1, search_y1;
-       int search_x2, search_y2;
-       //int search_x3, search_y3;
-       //int search_x4, search_y4;
-
-       if(config.global)
-       {
-// Get vector
-// Start of vector is center of previous block.
-// End of vector is total accumulation.
-               if(config.mode3 == MotionCVConfig::TRACK_SINGLE)
-               {
-                       global_x1 = (int64_t)(config.block_x * 
-                               w / 
-                               100);
-                       global_y1 = (int64_t)(config.block_y *
-                               h / 
-                               100);
-                       global_x2 = global_x1 + total_dx / OVERSAMPLE;
-                       global_y2 = global_y1 + total_dy / OVERSAMPLE;
-//printf("MotionCVMain::draw_vectors %d %d %d %d %d %d\n", total_dx, total_dy, global_x1, global_y1, global_x2, global_y2);
-               }
-               else
-// Start of vector is center of previous block.
-// End of vector is current change.
-               if(config.mode3 == MotionCVConfig::PREVIOUS_SAME_BLOCK)
-               {
-                       global_x1 = (int64_t)(config.block_x * 
-                               w / 
-                               100);
-                       global_y1 = (int64_t)(config.block_y *
-                               h / 
-                               100);
-                       global_x2 = global_x1 + current_dx / OVERSAMPLE;
-                       global_y2 = global_y1 + current_dy / OVERSAMPLE;
-               }
-               else
-               {
-                       global_x1 = (int64_t)(config.block_x * 
-                               w / 
-                               100 + 
-                               (total_dx - current_dx) / 
-                               OVERSAMPLE);
-                       global_y1 = (int64_t)(config.block_y *
-                               h / 
-                               100 +
-                               (total_dy - current_dy) /
-                               OVERSAMPLE);
-                       global_x2 = (int64_t)(config.block_x * 
-                               w / 
-                               100 + 
-                               total_dx / 
-                               OVERSAMPLE);
-                       global_y2 = (int64_t)(config.block_y *
-                               h / 
-                               100 +
-                               total_dy /
-                               OVERSAMPLE);
-               }
-
-               block_x = global_x1;
-               block_y = global_y1;
-               block_w = config.global_block_w * w / 100;
-               block_h = config.global_block_h * h / 100;
-               block_x1 = block_x - block_w / 2;
-               block_y1 = block_y - block_h / 2;
-               block_x2 = block_x + block_w / 2;
-               block_y2 = block_y + block_h / 2;
-               search_w = config.global_range_w * w / 100;
-               search_h = config.global_range_h * h / 100;
-               search_x1 = block_x1 - search_w / 2;
-               search_y1 = block_y1 - search_h / 2;
-               search_x2 = block_x2 + search_w / 2;
-               search_y2 = block_y2 + search_h / 2;
-
-// printf("MotionCVMain::draw_vectors %d %d %d %d %d %d %d %d %d %d %d %d\n",
-// global_x1,
-// global_y1,
-// block_w,
-// block_h,
-// block_x1,
-// block_y1,
-// block_x2,
-// block_y2,
-// search_x1,
-// search_y1,
-// search_x2,
-// search_y2);
-
-               clamp_scan(w, 
-                       h, 
-                       &block_x1,
-                       &block_y1,
-                       &block_x2,
-                       &block_y2,
-                       &search_x1,
-                       &search_y1,
-                       &search_x2,
-                       &search_y2,
-                       1);
-
-// Vector
-               draw_arrow(frame, global_x1, global_y1, global_x2, global_y2);
-
-// Macroblock
-               draw_line(frame, block_x1, block_y1, block_x2, block_y1);
-               draw_line(frame, block_x2, block_y1, block_x2, block_y2);
-               draw_line(frame, block_x2, block_y2, block_x1, block_y2);
-               draw_line(frame, block_x1, block_y2, block_x1, block_y1);
-
-
-// Search area
-               draw_line(frame, search_x1, search_y1, search_x2, search_y1);
-               draw_line(frame, search_x2, search_y1, search_x2, search_y2);
-               draw_line(frame, search_x2, search_y2, search_x1, search_y2);
-               draw_line(frame, search_x1, search_y2, search_x1, search_y1);
-
-// Block should be endpoint of motion
-               if(config.rotate)
-               {
-                       block_x = global_x2;
-                       block_y = global_y2;
-               }
-       }
-       else
-       {
-               block_x = (int64_t)(config.block_x * w / 100);
-               block_y = (int64_t)(config.block_y * h / 100);
-       }
-
-       block_w = config.rotation_block_w * w / 100;
-       block_h = config.rotation_block_h * h / 100;
-       if(config.rotate)
-       {
-               float angle = total_angle * 2 * M_PI / 360;
-               double base_angle1 = atan((float)block_h / block_w);
-               double base_angle2 = atan((float)block_w / block_h);
-               double target_angle1 = base_angle1 + angle;
-               double target_angle2 = base_angle2 + angle;
-               double radius = sqrt(block_w * block_w + block_h * block_h) / 2;
-               block_x1 = (int)(block_x - cos(target_angle1) * radius);
-               block_y1 = (int)(block_y - sin(target_angle1) * radius);
-               block_x2 = (int)(block_x + sin(target_angle2) * radius);
-               block_y2 = (int)(block_y - cos(target_angle2) * radius);
-               block_x3 = (int)(block_x - sin(target_angle2) * radius);
-               block_y3 = (int)(block_y + cos(target_angle2) * radius);
-               block_x4 = (int)(block_x + cos(target_angle1) * radius);
-               block_y4 = (int)(block_y + sin(target_angle1) * radius);
-
-               draw_line(frame, block_x1, block_y1, block_x2, block_y2);
-               draw_line(frame, block_x2, block_y2, block_x4, block_y4);
-               draw_line(frame, block_x4, block_y4, block_x3, block_y3);
-               draw_line(frame, block_x3, block_y3, block_x1, block_y1);
-
-
-// Center
-               if(!config.global)
-               {
-                       draw_line(frame, block_x, block_y - 5, block_x, block_y + 6);
-                       draw_line(frame, block_x - 5, block_y, block_x + 6, block_y);
-               }
-       }
-}
-
-
-
-void MotionCVMain::draw_pixel(VFrame *frame, int x, int y)
-{
-       if(!(x >= 0 && y >= 0 && x < frame->get_w() && y < frame->get_h())) return;
-
-#define DRAW_PIXEL(x, y, components, do_yuv, max, type) \
-{ \
-       type **rows = (type**)frame->get_rows(); \
-       rows[y][x * components] = max - rows[y][x * components]; \
-       if(!do_yuv) \
-       { \
-               rows[y][x * components + 1] = max - rows[y][x * components + 1]; \
-               rows[y][x * components + 2] = max - rows[y][x * components + 2]; \
-       } \
-       else \
-       { \
-               rows[y][x * components + 1] = (max / 2 + 1) - rows[y][x * components + 1]; \
-               rows[y][x * components + 2] = (max / 2 + 1) - rows[y][x * components + 2]; \
-       } \
-       if(components == 4) \
-               rows[y][x * components + 3] = max; \
-}
-
-
-       switch(frame->get_color_model())
-       {
-               case BC_RGB888:
-                       DRAW_PIXEL(x, y, 3, 0, 0xff, unsigned char);
-                       break;
-               case BC_RGBA8888:
-                       DRAW_PIXEL(x, y, 4, 0, 0xff, unsigned char);
-                       break;
-               case BC_RGB_FLOAT:
-                       DRAW_PIXEL(x, y, 3, 0, 1.0, float);
-                       break;
-               case BC_RGBA_FLOAT:
-                       DRAW_PIXEL(x, y, 4, 0, 1.0, float);
-                       break;
-               case BC_YUV888:
-                       DRAW_PIXEL(x, y, 3, 1, 0xff, unsigned char);
-                       break;
-               case BC_YUVA8888:
-                       DRAW_PIXEL(x, y, 4, 1, 0xff, unsigned char);
-                       break;
-               case BC_RGB161616:
-                       DRAW_PIXEL(x, y, 3, 0, 0xffff, uint16_t);
-                       break;
-               case BC_YUV161616:
-                       DRAW_PIXEL(x, y, 3, 1, 0xffff, uint16_t);
-                       break;
-               case BC_RGBA16161616:
-                       DRAW_PIXEL(x, y, 4, 0, 0xffff, uint16_t);
-                       break;
-               case BC_YUVA16161616:
-                       DRAW_PIXEL(x, y, 4, 1, 0xffff, uint16_t);
-                       break;
-       }
-}
-
-
-void MotionCVMain::draw_line(VFrame *frame, int x1, int y1, int x2, int y2)
-{
-       int w = labs(x2 - x1);
-       int h = labs(y2 - y1);
-//printf("MotionCVMain::draw_line 1 %d %d %d %d\n", x1, y1, x2, y2);
-
-       if(!w && !h)
-       {
-               draw_pixel(frame, x1, y1);
-       }
-       else
-       if(w > h)
-       {
-// Flip coordinates so x1 < x2
-               if(x2 < x1)
-               {
-                       y2 ^= y1;
-                       y1 ^= y2;
-                       y2 ^= y1;
-                       x1 ^= x2;
-                       x2 ^= x1;
-                       x1 ^= x2;
-               }
-               int numerator = y2 - y1;
-               int denominator = x2 - x1;
-               for(int i = x1; i < x2; i++)
-               {
-                       int y = y1 + (int64_t)(i - x1) * (int64_t)numerator / (int64_t)denominator;
-                       draw_pixel(frame, i, y);
-               }
-       }
-       else
-       {
-// Flip coordinates so y1 < y2
-               if(y2 < y1)
-               {
-                       y2 ^= y1;
-                       y1 ^= y2;
-                       y2 ^= y1;
-                       x1 ^= x2;
-                       x2 ^= x1;
-                       x1 ^= x2;
-               }
-               int numerator = x2 - x1;
-               int denominator = y2 - y1;
-               for(int i = y1; i < y2; i++)
-               {
-                       int x = x1 + (int64_t)(i - y1) * (int64_t)numerator / (int64_t)denominator;
-                       draw_pixel(frame, x, i);
-               }
-       }
-//printf("MotionCVMain::draw_line 2\n");
-}
-
-#define ARROW_SIZE 10
-void MotionCVMain::draw_arrow(VFrame *frame, int x1, int y1, int x2, int y2)
-{
-       double angle = atan((float)(y2 - y1) / (float)(x2 - x1));
-       double angle1 = angle + (float)145 / 360 * 2 * 3.14159265;
-       double angle2 = angle - (float)145 / 360 * 2 * 3.14159265;
-       int x3;
-       int y3;
-       int x4;
-       int y4;
-       if(x2 < x1)
-       {
-               x3 = x2 - (int)(ARROW_SIZE * cos(angle1));
-               y3 = y2 - (int)(ARROW_SIZE * sin(angle1));
-               x4 = x2 - (int)(ARROW_SIZE * cos(angle2));
-               y4 = y2 - (int)(ARROW_SIZE * sin(angle2));
-       }
-       else
-       {
-               x3 = x2 + (int)(ARROW_SIZE * cos(angle1));
-               y3 = y2 + (int)(ARROW_SIZE * sin(angle1));
-               x4 = x2 + (int)(ARROW_SIZE * cos(angle2));
-               y4 = y2 + (int)(ARROW_SIZE * sin(angle2));
-       }
-
-// Main vector
-       draw_line(frame, x1, y1, x2, y2);
-//     draw_line(frame, x1, y1 + 1, x2, y2 + 1);
-
-// Arrow line
-       if(abs(y2 - y1) || abs(x2 - x1)) draw_line(frame, x2, y2, x3, y3);
-//     draw_line(frame, x2, y2 + 1, x3, y3 + 1);
-// Arrow line
-       if(abs(y2 - y1) || abs(x2 - x1)) draw_line(frame, x2, y2, x4, y4);
-//     draw_line(frame, x2, y2 + 1, x4, y4 + 1);
-}
-
-
-
-
-#define ABS_DIFF(type, temp_type, multiplier, components) \
-{ \
-       temp_type result_temp = 0; \
-       for(int i = 0; i < h; i++) \
-       { \
-               type *prev_row = (type*)prev_ptr; \
-               type *current_row = (type*)current_ptr; \
-               for(int j = 0; j < w; j++) \
-               { \
-                       for(int k = 0; k < 3; k++) \
-                       { \
-                               temp_type difference; \
-                               difference = *prev_row++ - *current_row++; \
-                               if(difference < 0) \
-                                       result_temp -= difference; \
-                               else \
-                                       result_temp += difference; \
-                       } \
-                       if(components == 4) \
-                       { \
-                               prev_row++; \
-                               current_row++; \
-                       } \
-               } \
-               prev_ptr += row_bytes; \
-               current_ptr += row_bytes; \
-       } \
-       result = (int64_t)(result_temp * multiplier); \
-}
-
-int64_t MotionCVMain::abs_diff(unsigned char *prev_ptr,
-       unsigned char *current_ptr,
-       int row_bytes,
-       int w,
-       int h,
-       int color_model)
-{
-       int64_t result = 0;
-       switch(color_model)
-       {
-               case BC_RGB888:
-                       ABS_DIFF(unsigned char, int64_t, 1, 3)
-                       break;
-               case BC_RGBA8888:
-                       ABS_DIFF(unsigned char, int64_t, 1, 4)
-                       break;
-               case BC_RGB_FLOAT:
-                       ABS_DIFF(float, double, 0x10000, 3)
-                       break;
-               case BC_RGBA_FLOAT:
-                       ABS_DIFF(float, double, 0x10000, 4)
-                       break;
-               case BC_YUV888:
-                       ABS_DIFF(unsigned char, int64_t, 1, 3)
-                       break;
-               case BC_YUVA8888:
-                       ABS_DIFF(unsigned char, int64_t, 1, 4)
-                       break;
-               case BC_YUV161616:
-                       ABS_DIFF(uint16_t, int64_t, 1, 3)
-                       break;
-               case BC_YUVA16161616:
-                       ABS_DIFF(uint16_t, int64_t, 1, 4)
-                       break;
-       }
-       return result;
-}
-
-
-
-#define ABS_DIFF_SUB(type, temp_type, multiplier, components) \
-{ \
-       temp_type result_temp = 0; \
-       temp_type y2_fraction = sub_y * 0x100 / OVERSAMPLE; \
-       temp_type y1_fraction = 0x100 - y2_fraction; \
-       temp_type x2_fraction = sub_x * 0x100 / OVERSAMPLE; \
-       temp_type x1_fraction = 0x100 - x2_fraction; \
-       for(int i = 0; i < h_sub; i++) \
-       { \
-               type *prev_row1 = (type*)prev_ptr; \
-               type *prev_row2 = (type*)prev_ptr + components; \
-               type *prev_row3 = (type*)(prev_ptr + row_bytes); \
-               type *prev_row4 = (type*)(prev_ptr + row_bytes) + components; \
-               type *current_row = (type*)current_ptr; \
-               for(int j = 0; j < w_sub; j++) \
-               { \
-                       for(int k = 0; k < 3; k++) \
-                       { \
-                               temp_type difference; \
-                               temp_type prev_value = \
-                                       (*prev_row1++ * x1_fraction * y1_fraction + \
-                                       *prev_row2++ * x2_fraction * y1_fraction + \
-                                       *prev_row3++ * x1_fraction * y2_fraction + \
-                                       *prev_row4++ * x2_fraction * y2_fraction) / \
-                                       0x100 / 0x100; \
-                               temp_type current_value = *current_row++; \
-                               difference = prev_value - current_value; \
-                               if(difference < 0) \
-                                       result_temp -= difference; \
-                               else \
-                                       result_temp += difference; \
-                       } \
- \
-                       if(components == 4) \
-                       { \
-                               prev_row1++; \
-                               prev_row2++; \
-                               prev_row3++; \
-                               prev_row4++; \
-                               current_row++; \
-                       } \
-               } \
-               prev_ptr += row_bytes; \
-               current_ptr += row_bytes; \
-       } \
-       result = (int64_t)(result_temp * multiplier); \
-}
-
-
-
-
-int64_t MotionCVMain::abs_diff_sub(unsigned char *prev_ptr,
-       unsigned char *current_ptr,
-       int row_bytes,
-       int w,
-       int h,
-       int color_model,
-       int sub_x,
-       int sub_y)
-{
-       int h_sub = h - 1;
-       int w_sub = w - 1;
-       int64_t result = 0;
-
-       switch(color_model)
-       {
-               case BC_RGB888:
-                       ABS_DIFF_SUB(unsigned char, int64_t, 1, 3)
-                       break;
-               case BC_RGBA8888:
-                       ABS_DIFF_SUB(unsigned char, int64_t, 1, 4)
-                       break;
-               case BC_RGB_FLOAT:
-                       ABS_DIFF_SUB(float, double, 0x10000, 3)
-                       break;
-               case BC_RGBA_FLOAT:
-                       ABS_DIFF_SUB(float, double, 0x10000, 4)
-                       break;
-               case BC_YUV888:
-                       ABS_DIFF_SUB(unsigned char, int64_t, 1, 3)
-                       break;
-               case BC_YUVA8888:
-                       ABS_DIFF_SUB(unsigned char, int64_t, 1, 4)
-                       break;
-               case BC_YUV161616:
-                       ABS_DIFF_SUB(uint16_t, int64_t, 1, 3)
-                       break;
-               case BC_YUVA16161616:
-                       ABS_DIFF_SUB(uint16_t, int64_t, 1, 4)
-                       break;
-       }
-       return result;
-}
-
-
-
-
-
-MotionCVScanPackage::MotionCVScanPackage()
- : LoadPackage()
-{
-       valid = 1;
-}
-
-
-
-
-
-
-MotionCVScanUnit::MotionCVScanUnit(MotionCVScan *server, 
-       MotionCVMain *plugin)
- : LoadClient(server)
-{
-       this->plugin = plugin;
-       this->server = server;
-       cache_lock = new Mutex("MotionCVScanUnit::cache_lock");
-}
-
-MotionCVScanUnit::~MotionCVScanUnit()
-{
-       delete cache_lock;
-}
-
-
-
-void MotionCVScanUnit::process_package(LoadPackage *package)
-{
-       MotionCVScanPackage *pkg = (MotionCVScanPackage*)package;
-       //int w = server->current_frame->get_w();
-       //int h = server->current_frame->get_h();
-       int color_model = server->current_frame->get_color_model();
-       int pixel_size = BC_CModels::calculate_pixelsize(color_model);
-       int row_bytes = server->current_frame->get_bytes_per_line();
-
-
-
-
-
-
-
-
-
-
-
-
-// Single pixel
-       if(!server->subpixel)
-       {
-               int search_x = pkg->scan_x1 + (pkg->pixel % (pkg->scan_x2 - pkg->scan_x1));
-               int search_y = pkg->scan_y1 + (pkg->pixel / (pkg->scan_x2 - pkg->scan_x1));
-
-// Try cache
-               pkg->difference1 = server->get_cache(search_x, search_y);
-               if(pkg->difference1 < 0)
-               {
-//printf("MotionCVScanUnit::process_package 1 %d %d\n", 
-//search_x, search_y, pkg->block_x2 - pkg->block_x1, pkg->block_y2 - pkg->block_y1);
-// Pointers to first pixel in each block
-                       unsigned char *prev_ptr = server->previous_frame->get_rows()[
-                               search_y] +     
-                               search_x * pixel_size;
-                       unsigned char *current_ptr = server->current_frame->get_rows()[
-                               pkg->block_y1] +
-                               pkg->block_x1 * pixel_size;
-// Scan block
-                       pkg->difference1 = plugin->abs_diff(prev_ptr,
-                               current_ptr,
-                               row_bytes,
-                               pkg->block_x2 - pkg->block_x1,
-                               pkg->block_y2 - pkg->block_y1,
-                               color_model);
-//printf("MotionCVScanUnit::process_package 2\n");
-                       server->put_cache(search_x, search_y, pkg->difference1);
-               }
-       }
-
-
-
-
-
-
-
-       else
-
-
-
-
-
-
-
-
-// Sub pixel
-       {
-               int sub_x = pkg->pixel % (OVERSAMPLE * 2 - 1) + 1;
-               int sub_y = pkg->pixel / (OVERSAMPLE * 2 - 1) + 1;
-
-               if(plugin->config.horizontal_only)
-               {
-                       sub_y = 0;
-               }
-
-               if(plugin->config.vertical_only)
-               {
-                       sub_x = 0;
-               }
-
-               int search_x = pkg->scan_x1 + sub_x / OVERSAMPLE;
-               int search_y = pkg->scan_y1 + sub_y / OVERSAMPLE;
-               sub_x %= OVERSAMPLE;
-               sub_y %= OVERSAMPLE;
-
-
-               unsigned char *prev_ptr = server->previous_frame->get_rows()[
-                       search_y] +
-                       search_x * pixel_size;
-               unsigned char *current_ptr = server->current_frame->get_rows()[
-                       pkg->block_y1] +
-                       pkg->block_x1 * pixel_size;
-
-// With subpixel, there are two ways to compare each position, one by shifting
-// the previous frame and two by shifting the current frame.
-               pkg->difference1 = plugin->abs_diff_sub(prev_ptr,
-                       current_ptr,
-                       row_bytes,
-                       pkg->block_x2 - pkg->block_x1,
-                       pkg->block_y2 - pkg->block_y1,
-                       color_model,
-                       sub_x,
-                       sub_y);
-               pkg->difference2 = plugin->abs_diff_sub(current_ptr,
-                       prev_ptr,
-                       row_bytes,
-                       pkg->block_x2 - pkg->block_x1,
-                       pkg->block_y2 - pkg->block_y1,
-                       color_model,
-                       sub_x,
-                       sub_y);
-// printf("MotionCVScanUnit::process_package sub_x=%d sub_y=%d search_x=%d search_y=%d diff1=%lld diff2=%lld\n",
-// sub_x,
-// sub_y,
-// search_x,
-// search_y,
-// pkg->difference1,
-// pkg->difference2);
-       }
-
-
-
-
-}
-
-
-
-
-
-
-
-
-
-
-int64_t MotionCVScanUnit::get_cache(int x, int y)
-{
-       int64_t result = -1;
-       cache_lock->lock("MotionCVScanUnit::get_cache");
-       for(int i = 0; i < cache.total; i++)
-       {
-               MotionCVScanCache *ptr = cache.values[i];
-               if(ptr->x == x && ptr->y == y)
-               {
-                       result = ptr->difference;
-                       break;
-               }
-       }
-       cache_lock->unlock();
-       return result;
-}
-
-void MotionCVScanUnit::put_cache(int x, int y, int64_t difference)
-{
-       MotionCVScanCache *ptr = new MotionCVScanCache(x, y, difference);
-       cache_lock->lock("MotionCVScanUnit::put_cache");
-       cache.append(ptr);
-       cache_lock->unlock();
-}
-
-
-
-
-
-
-
-
-
-
-
-MotionCVScan::MotionCVScan(MotionCVMain *plugin, 
-       int total_clients,
-       int total_packages)
- : LoadServer(
-//1, 1 
-total_clients, total_packages 
-)
-{
-       this->plugin = plugin;
-       cache_lock = new Mutex("MotionCVScan::cache_lock");
-}
-
-MotionCVScan::~MotionCVScan()
-{
-       delete cache_lock;
-}
-
-
-void MotionCVScan::init_packages()
-{
-// Set package coords
-       for(int i = 0; i < get_total_packages(); i++)
-       {
-               MotionCVScanPackage *pkg = (MotionCVScanPackage*)get_package(i);
-
-               pkg->block_x1 = block_x1;
-               pkg->block_x2 = block_x2;
-               pkg->block_y1 = block_y1;
-               pkg->block_y2 = block_y2;
-               pkg->scan_x1 = scan_x1;
-               pkg->scan_x2 = scan_x2;
-               pkg->scan_y1 = scan_y1;
-               pkg->scan_y2 = scan_y2;
-               pkg->pixel = (int64_t)i * (int64_t)total_pixels / (int64_t)total_steps;
-               pkg->difference1 = 0;
-               pkg->difference2 = 0;
-               pkg->dx = 0;
-               pkg->dy = 0;
-               pkg->valid = 1;
-       }
-}
-
-LoadClient* MotionCVScan::new_client()
-{
-       return new MotionCVScanUnit(this, plugin);
-}
-
-LoadPackage* MotionCVScan::new_package()
-{
-       return new MotionCVScanPackage;
-}
-
-
-void MotionCVScan::scan_frame(VFrame *previous_frame,
-       VFrame *current_frame)
-{
-       this->previous_frame = previous_frame;
-       this->current_frame = current_frame;
-       subpixel = 0;
-
-       cache.remove_all_objects();
-
-
-// Single macroblock
-       int w = current_frame->get_w();
-       int h = current_frame->get_h();
-
-// Initial search parameters
-       int scan_w = w * plugin->config.global_range_w / 100;
-       int scan_h = h * plugin->config.global_range_h / 100;
-       int block_w = w * plugin->config.global_block_w / 100;
-       int block_h = h * plugin->config.global_block_h / 100;
-
-// Location of block in previous frame
-       block_x1 = (int)(w * plugin->config.block_x / 100 - block_w / 2);
-       block_y1 = (int)(h * plugin->config.block_y / 100 - block_h / 2);
-       block_x2 = (int)(w * plugin->config.block_x / 100 + block_w / 2);
-       block_y2 = (int)(h * plugin->config.block_y / 100 + block_h / 2);
-
-// Offset to location of previous block.  This offset needn't be very accurate
-// since it's the offset of the previous image and current image we want.
-       if(plugin->config.mode3 == MotionCVConfig::TRACK_PREVIOUS)
-       {
-               block_x1 += plugin->total_dx / OVERSAMPLE;
-               block_y1 += plugin->total_dy / OVERSAMPLE;
-               block_x2 += plugin->total_dx / OVERSAMPLE;
-               block_y2 += plugin->total_dy / OVERSAMPLE;
-       }
-
-       skip = 0;
-
-       switch(plugin->config.mode2)
-       {
-// Don't calculate
-               case MotionCVConfig::NO_CALCULATE:
-                       dx_result = 0;
-                       dy_result = 0;
-                       skip = 1;
-                       break;
-
-               case MotionCVConfig::LOAD:
-               {
-// Load result from disk
-                       char string[BCTEXTLEN];
-                       sprintf(string, "%s%06jd", MOTION_FILE, plugin->get_source_position());
-                       FILE *input = fopen(string, "r");
-                       if(input)
-                       {
-                               fscanf(input, 
-                                       "%d %d", 
-                                       &dx_result,
-                                       &dy_result);
-                               fclose(input);
-                               skip = 1;
-                       }
-                       break;
-               }
-
-// Scan from scratch
-               default:
-                       skip = 0;
-                       break;
-       }
-
-// Perform scan
-       if(!skip)
-       {
-// Location of block in current frame
-               int x_result = block_x1;
-               int y_result = block_y1;
-
-// printf("MotionCVScan::scan_frame 1 %d %d %d %d %d %d %d %d\n",
-// block_x1 + block_w / 2,
-// block_y1 + block_h / 2,
-// block_w,
-// block_h,
-// block_x1,
-// block_y1,
-// block_x2,
-// block_y2);
-
-               while(1)
-               {
-                       scan_x1 = x_result - scan_w / 2;
-                       scan_y1 = y_result - scan_h / 2;
-                       scan_x2 = x_result + scan_w / 2;
-                       scan_y2 = y_result + scan_h / 2;
-
-
-
-// Zero out requested values
-                       if(plugin->config.horizontal_only)
-                       {
-                               scan_y1 = block_y1;
-                               scan_y2 = block_y1 + 1;
-                       }
-                       if(plugin->config.vertical_only)
-                       {
-                               scan_x1 = block_x1;
-                               scan_x2 = block_x1 + 1;
-                       }
-
-// printf("MotionCVScan::scan_frame 1 %d %d %d %d %d %d %d %d\n",
-// block_x1,
-// block_y1,
-// block_x2,
-// block_y2,
-// scan_x1,
-// scan_y1,
-// scan_x2,
-// scan_y2);
-// Clamp the block coords before the scan so we get useful scan coords.
-                       MotionCVMain::clamp_scan(w, 
-                               h, 
-                               &block_x1,
-                               &block_y1,
-                               &block_x2,
-                               &block_y2,
-                               &scan_x1,
-                               &scan_y1,
-                               &scan_x2,
-                               &scan_y2,
-                               0);
-// printf("MotionCVScan::scan_frame 1\n    block_x1=%d block_y1=%d block_x2=%d block_y2=%d\n    scan_x1=%d scan_y1=%d scan_x2=%d scan_y2=%d\n    x_result=%d y_result=%d\n", 
-// block_x1,
-// block_y1,
-// block_x2,
-// block_y2,
-// scan_x1, 
-// scan_y1, 
-// scan_x2, 
-// scan_y2, 
-// x_result, 
-// y_result);
-
-
-// Give up if invalid coords.
-                       if(scan_y2 <= scan_y1 ||
-                               scan_x2 <= scan_x1 ||
-                               block_x2 <= block_x1 ||
-                               block_y2 <= block_y1)
-                               break;
-
-// For subpixel, the top row and left column are skipped
-                       if(subpixel)
-                       {
-                               if(plugin->config.horizontal_only ||
-                                       plugin->config.vertical_only)
-                               {
-                                       total_pixels = 4 * OVERSAMPLE * OVERSAMPLE - 4 * OVERSAMPLE;
-                               }
-                               else
-                               {
-                                       total_pixels = 4 * OVERSAMPLE;
-                               }
-
-                               total_steps = total_pixels;
-
-                               set_package_count(total_steps);
-                               process_packages();
-
-// Get least difference
-                               int64_t min_difference = -1;
-                               for(int i = 0; i < get_total_packages(); i++)
-                               {
-                                       MotionCVScanPackage *pkg = (MotionCVScanPackage*)get_package(i);
-                                       if(pkg->difference1 < min_difference || min_difference == -1)
-                                       {
-                                               min_difference = pkg->difference1;
-
-                                               if(plugin->config.vertical_only)
-                                                       x_result = scan_x1 * OVERSAMPLE;
-                                               else
-                                                       x_result = scan_x1 * OVERSAMPLE + 
-                                                               (pkg->pixel % (OVERSAMPLE * 2 - 1)) + 1;
-                                               
-                                               if(plugin->config.horizontal_only)
-                                                       y_result = scan_y1 * OVERSAMPLE;
-                                               else
-                                                       y_result = scan_y1 * OVERSAMPLE + 
-                                                               (pkg->pixel / (OVERSAMPLE * 2 - 1)) + 1;
-
-
-// Fill in results
-                                               dx_result = block_x1 * OVERSAMPLE - x_result;
-                                               dy_result = block_y1 * OVERSAMPLE - y_result;
-                                       }
-
-                                       if(pkg->difference2 < min_difference)
-                                       {
-                                               min_difference = pkg->difference2;
-
-                                               if(plugin->config.vertical_only)
-                                                       x_result = scan_x1 * OVERSAMPLE;
-                                               else
-                                                       x_result = scan_x2 * OVERSAMPLE -
-                                                               ((pkg->pixel % (OVERSAMPLE * 2 - 1)) + 1);
-
-                                               if(plugin->config.horizontal_only)
-                                                       y_result = scan_y1 * OVERSAMPLE;
-                                               else
-                                                       y_result = scan_y2 * OVERSAMPLE -
-                                                               ((pkg->pixel / (OVERSAMPLE * 2 - 1)) + 1);
-
-                                               dx_result = block_x1 * OVERSAMPLE - x_result;
-                                               dy_result = block_y1 * OVERSAMPLE - y_result;
-                                       }
-                               }
-
-//printf("MotionCVScan::scan_frame 1 %d %d %d %d\n", block_x1, block_y1, x_result, y_result);
-                               break;
-                       }
-                       else
-                       {
-                               total_pixels = (scan_x2 - scan_x1) * (scan_y2 - scan_y1);
-                               total_steps = MIN(plugin->config.global_positions, total_pixels);
-
-                               set_package_count(total_steps);
-                               process_packages();
-
-// Get least difference
-                               int64_t min_difference = -1;
-                               for(int i = 0; i < get_total_packages(); i++)
-                               {
-                                       MotionCVScanPackage *pkg = (MotionCVScanPackage*)get_package(i);
-                                       if(pkg->difference1 < min_difference || min_difference == -1)
-                                       {
-                                               min_difference = pkg->difference1;
-                                               x_result = scan_x1 + (pkg->pixel % (scan_x2 - scan_x1));
-                                               y_result = scan_y1 + (pkg->pixel / (scan_x2 - scan_x1));
-                                               x_result *= OVERSAMPLE;
-                                               y_result *= OVERSAMPLE;
-                                       }
-                               }
-
-// printf("MotionCVScan::scan_frame 10 total_steps=%d total_pixels=%d subpixel=%d\n",
-// total_steps, 
-// total_pixels,
-// subpixel);
-// 
-// printf("    scan w=%d h=%d scan x1=%d y1=%d x2=%d y2=%d\n",
-// scan_w,
-// scan_h, 
-// scan_x1,
-// scan_y1,
-// scan_x2,
-// scan_y2);
-// 
-// printf("MotionCVScan::scan_frame 2 block x1=%d y1=%d x2=%d y2=%d result x=%.2f y=%.2f\n", 
-// block_x1, 
-// block_y1, 
-// block_x2,
-// block_y2,
-// (float)x_result / 4, 
-// (float)y_result / 4);
-
-
-// If a new search is required, rescale results back to pixels.
-                               if(total_steps >= total_pixels)
-                               {
-// Single pixel accuracy reached.  Now do exhaustive subpixel search.
-                                       if(plugin->config.mode1 == MotionCVConfig::STABILIZE ||
-                                               plugin->config.mode1 == MotionCVConfig::TRACK ||
-                                               plugin->config.mode1 == MotionCVConfig::NOTHING)
-                                       {
-                                               x_result /= OVERSAMPLE;
-                                               y_result /= OVERSAMPLE;
-                                               scan_w = 2;
-                                               scan_h = 2;
-                                               subpixel = 1;
-                                       }
-                                       else
-                                       {
-// Fill in results and quit
-                                               dx_result = block_x1 * OVERSAMPLE - x_result;
-                                               dy_result = block_y1 * OVERSAMPLE - y_result;
-                                               break;
-                                       }
-                               }
-                               else
-// Reduce scan area and try again
-                               {
-                                       scan_w = (scan_x2 - scan_x1) / 2;
-                                       scan_h = (scan_y2 - scan_y1) / 2;
-                                       x_result /= OVERSAMPLE;
-                                       y_result /= OVERSAMPLE;
-                               }
-                       }
-               }
-
-               dx_result *= -1;
-               dy_result *= -1;
-
-               // Add offsets from the "tracked single frame"
-               if (plugin->config.addtrackedframeoffset) {
-                 int tf_dx_result, tf_dy_result;
-                 char string[BCTEXTLEN];
-                 sprintf(string, "%s%06jd", MOTION_FILE, plugin->config.track_frame);
-                 FILE *input = fopen(string, "r");
-                 if(input)
-                   {
-                     fscanf(input, 
-                            "%d %d", 
-                            &tf_dx_result,
-                            &tf_dy_result);
-                     dx_result += tf_dx_result;
-                     dy_result += tf_dy_result;
-                     fclose(input);
-                   }
-               }
-
-       }
-
-
-
-
-
-
-// Write results
-       if(plugin->config.mode2 == MotionCVConfig::SAVE)
-       {
-               char string[BCTEXTLEN];
-               sprintf(string, 
-                       "%s%06jd", 
-                       MOTION_FILE, 
-                       plugin->get_source_position());
-               FILE *output = fopen(string, "w");
-               if(output)
-               {
-                       fprintf(output, 
-                               "%d %d\n",
-                               dx_result,
-                               dy_result);
-                       fclose(output);
-               }
-               else
-               {
-                       perror("MotionCVScan::scan_frame SAVE 1");
-               }
-       }
-
-#ifdef DEBUG
-printf("MotionCVScan::scan_frame 10 dx=%.2f dy=%.2f\n", 
-(float)this->dx_result / OVERSAMPLE,
-(float)this->dy_result / OVERSAMPLE);
-#endif
-}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-int64_t MotionCVScan::get_cache(int x, int y)
-{
-       int64_t result = -1;
-       cache_lock->lock("MotionCVScan::get_cache");
-       for(int i = 0; i < cache.total; i++)
-       {
-               MotionCVScanCache *ptr = cache.values[i];
-               if(ptr->x == x && ptr->y == y)
-               {
-                       result = ptr->difference;
-                       break;
-               }
-       }
-       cache_lock->unlock();
-       return result;
-}
-
-void MotionCVScan::put_cache(int x, int y, int64_t difference)
-{
-       MotionCVScanCache *ptr = new MotionCVScanCache(x, y, difference);
-       cache_lock->lock("MotionCVScan::put_cache");
-       cache.append(ptr);
-       cache_lock->unlock();
-}
-
-
-
-
-
-MotionCVScanCache::MotionCVScanCache(int x, int y, int64_t difference)
-{
-       this->x = x;
-       this->y = y;
-       this->difference = difference;
-}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-RotateCVScanPackage::RotateCVScanPackage()
-{
-}
-
-
-RotateCVScanUnit::RotateCVScanUnit(RotateCVScan *server, MotionCVMain *plugin)
- : LoadClient(server)
-{
-       this->server = server;
-       this->plugin = plugin;
-       rotater = 0;
-       temp = 0;
-}
-
-RotateCVScanUnit::~RotateCVScanUnit()
-{
-       delete rotater;
-       delete temp;
-}
-
-void RotateCVScanUnit::process_package(LoadPackage *package)
-{
-       if(server->skip) return;
-       RotateCVScanPackage *pkg = (RotateCVScanPackage*)package;
-
-       if((pkg->difference = server->get_cache(pkg->angle)) < 0)
-       {
-//printf("RotateCVScanUnit::process_package 1\n");
-               int color_model = server->previous_frame->get_color_model();
-               int pixel_size = BC_CModels::calculate_pixelsize(color_model);
-               int row_bytes = server->previous_frame->get_bytes_per_line();
-
-               if(!rotater)
-                       rotater = new AffineEngine(1, 1);
-               if(!temp) temp = new VFrame(
-                       server->previous_frame->get_w(),
-                       server->previous_frame->get_h(),
-                       color_model);
-
-
-// RotateCV original block size
-               rotater->set_viewport(server->block_x1, 
-                       server->block_y1,
-                       server->block_x2 - server->block_x1,
-                       server->block_y2 - server->block_y1);
-               rotater->set_pivot(server->block_x, server->block_y);
-//pkg->angle = 2;
-               rotater->rotate(temp,
-                       server->previous_frame,
-                       pkg->angle);
-// Clamp coordinates
-               int x1 = server->scan_x;
-               int y1 = server->scan_y;
-               int x2 = x1 + server->scan_w;
-               int y2 = y1 + server->scan_h;
-               x2 = MIN(temp->get_w(), x2);
-               y2 = MIN(temp->get_h(), y2);
-               x2 = MIN(server->current_frame->get_w(), x2);
-               y2 = MIN(server->current_frame->get_h(), y2);
-               x1 = MAX(0, x1);
-               y1 = MAX(0, y1);
-
-               if(x2 > x1 && y2 > y1)
-               {
-                       pkg->difference = plugin->abs_diff(
-                               temp->get_rows()[y1] + x1 * pixel_size,
-                               server->current_frame->get_rows()[y1] + x1 * pixel_size,
-                               row_bytes,
-                               x2 - x1,
-                               y2 - y1,
-                               color_model);
-//printf("RotateCVScanUnit::process_package %d\n", __LINE__);
-                       server->put_cache(pkg->angle, pkg->difference);
-                }
-
-// printf("RotateCVScanUnit::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=%lld\n", 
-// server->block_x1, 
-// server->block_y1,
-// server->block_x2 - server->block_x1,
-// server->block_y2 - server->block_y1,
-// server->block_x,
-// server->block_y,
-// pkg->angle, 
-// server->scan_w,
-// server->scan_h,
-// pkg->difference);
-       }
-}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-RotateCVScan::RotateCVScan(MotionCVMain *plugin, 
-       int total_clients, 
-       int total_packages)
- : LoadServer(
-//1, 1 
-total_clients, total_packages 
-)
-{
-       this->plugin = plugin;
-       cache_lock = new Mutex("RotateCVScan::cache_lock");
-}
-
-
-RotateCVScan::~RotateCVScan()
-{
-       delete cache_lock;
-}
-
-void RotateCVScan::init_packages()
-{
-       for(int i = 0; i < get_total_packages(); i++)
-       {
-               RotateCVScanPackage *pkg = (RotateCVScanPackage*)get_package(i);
-               pkg->angle = i * 
-                       (scan_angle2 - scan_angle1) / 
-                       (total_steps - 1) + 
-                       scan_angle1;
-       }
-}
-
-LoadClient* RotateCVScan::new_client()
-{
-       return new RotateCVScanUnit(this, plugin);
-}
-
-LoadPackage* RotateCVScan::new_package()
-{
-       return new RotateCVScanPackage;
-}
-
-
-float RotateCVScan::scan_frame(VFrame *previous_frame,
-       VFrame *current_frame,
-       int block_x,
-       int block_y)
-{
-       skip = 0;
-       this->block_x = block_x;
-       this->block_y = block_y;
-
-       switch(plugin->config.mode2)
-       {
-               case MotionCVConfig::NO_CALCULATE:
-                       result = 0;
-                       skip = 1;
-                       break;
-
-               case MotionCVConfig::LOAD:
-               {
-                       char string[BCTEXTLEN];
-                       sprintf(string, "%s%06jd", ROTATION_FILE, plugin->get_source_position());
-                       FILE *input = fopen(string, "r");
-                       if(input)
-                       {
-                               fscanf(input, "%f", &result);
-                               fclose(input);
-                               skip = 1;
-                       }
-                       else
-                       {
-                               perror("RotateCVScan::scan_frame LOAD");
-                       }
-                       break;
-               }
-       }
-
-
-
-
-
-
-
-
-       this->previous_frame = previous_frame;
-       this->current_frame = current_frame;
-       int w = current_frame->get_w();
-       int h = current_frame->get_h();
-       int block_w = w * plugin->config.rotation_block_w / 100;
-       int block_h = h * plugin->config.rotation_block_h / 100;
-
-       if(this->block_x - block_w / 2 < 0) block_w = this->block_x * 2;
-       if(this->block_y - block_h / 2 < 0) block_h = this->block_y * 2;
-       if(this->block_x + block_w / 2 > w) block_w = (w - this->block_x) * 2;
-       if(this->block_y + block_h / 2 > h) block_h = (h - this->block_y) * 2;
-
-       block_x1 = this->block_x - block_w / 2;
-       block_x2 = this->block_x + block_w / 2;
-       block_y1 = this->block_y - block_h / 2;
-       block_y2 = this->block_y + block_h / 2;
-
-
-// Calculate the maximum area available to scan after rotation.
-// Must be calculated from the starting range because of cache.
-// Get coords of rectangle after rotation.
-       double center_x = this->block_x;
-       double center_y = this->block_y;
-       double max_angle = plugin->config.rotation_range;
-       double base_angle1 = atan((float)block_h / block_w);
-       double base_angle2 = atan((float)block_w / block_h);
-       double target_angle1 = base_angle1 + max_angle * 2 * M_PI / 360;
-       double target_angle2 = base_angle2 + max_angle * 2 * M_PI / 360;
-       double radius = sqrt(block_w * block_w + block_h * block_h) / 2;
-       double x1 = center_x - cos(target_angle1) * radius;
-       double y1 = center_y - sin(target_angle1) * radius;
-       double x2 = center_x + sin(target_angle2) * radius;
-       double y2 = center_y - cos(target_angle2) * radius;
-       double x3 = center_x - sin(target_angle2) * radius;
-       double y3 = center_y + cos(target_angle2) * radius;
-
-// Track top edge to find greatest area.
-       double max_area1 = 0;
-       //double max_x1 = 0;
-       double max_y1 = 0;
-       for(double x = x1; x < x2; x++)
-       {
-               double y = y1 + (y2 - y1) * (x - x1) / (x2 - x1);
-               if(x >= center_x && x < block_x2 && y >= block_y1 && y < center_y)
-               {
-                       double area = fabs(x - center_x) * fabs(y - center_y);
-                       if(area > max_area1)
-                       {
-                               max_area1 = area;
-                               //max_x1 = x;
-                               max_y1 = y;
-                       }
-               }
-       }
-
-// Track left edge to find greatest area.
-       double max_area2 = 0;
-       double max_x2 = 0;
-       //double max_y2 = 0;
-       for(double y = y1; y < y3; y++)
-       {
-               double x = x1 + (x3 - x1) * (y - y1) / (y3 - y1);
-               if(x >= block_x1 && x < center_x && y >= block_y1 && y < center_y)
-               {
-                       double area = fabs(x - center_x) * fabs(y - center_y);
-                       if(area > max_area2)
-                       {
-                               max_area2 = area;
-                               max_x2 = x;
-                               //max_y2 = y;
-                       }
-               }
-       }
-
-       double max_x, max_y;
-       max_x = max_x2;
-       max_y = max_y1;
-
-// Get reduced scan coords
-       scan_w = (int)(fabs(max_x - center_x) * 2);
-       scan_h = (int)(fabs(max_y - center_y) * 2);
-       scan_x = (int)(center_x - scan_w / 2);
-       scan_y = (int)(center_y - scan_h / 2);
-// printf("RotateCVScan::scan_frame center=%d,%d scan=%d,%d %dx%d\n", 
-// this->block_x, this->block_y, scan_x, scan_y, scan_w, scan_h);
-// printf("    angle_range=%f block= %d,%d,%d,%d\n", max_angle, block_x1, block_y1, block_x2, block_y2);
-
-// Determine min angle from size of block
-       double angle1 = atan((double)block_h / block_w);
-       double angle2 = atan((double)(block_h - 1) / (block_w + 1));
-       double min_angle = fabs(angle2 - angle1) / OVERSAMPLE;
-       min_angle = MAX(min_angle, MIN_ANGLE);
-
-#ifdef DEBUG
-printf("RotateCVScan::scan_frame min_angle=%f\n", min_angle * 360 / 2 / M_PI);
-#endif
-
-       cache.remove_all_objects();
-       if(!skip)
-       {
-// Initial search range
-               float angle_range = (float)plugin->config.rotation_range;
-               result = 0;
-               total_steps = plugin->config.rotate_positions;
-
-
-               while(angle_range >= min_angle * total_steps)
-               {
-                       scan_angle1 = result - angle_range;
-                       scan_angle2 = result + angle_range;
-
-
-                       set_package_count(total_steps);
-//set_package_count(1);
-                       process_packages();
-
-                       int64_t min_difference = -1;
-                       for(int i = 0; i < get_total_packages(); i++)
-                       {
-                               RotateCVScanPackage *pkg = (RotateCVScanPackage*)get_package(i);
-                               if(pkg->difference < min_difference || min_difference == -1)
-                               {
-                                       min_difference = pkg->difference;
-                                       result = pkg->angle;
-                               }
-//break;
-                       }
-
-                       angle_range /= 2;
-
-//break;
-               }
-       }
-
-
-       if(!skip && plugin->config.mode2 == MotionCVConfig::SAVE)
-       {
-               char string[BCTEXTLEN];
-               sprintf(string, 
-                       "%s%06jd", 
-                       ROTATION_FILE, 
-                       plugin->get_source_position());
-               FILE *output = fopen(string, "w");
-               if(output)
-               {
-                       fprintf(output, "%f\n", result);
-                       fclose(output);
-               }
-               else
-               {
-                       perror("RotateCVScan::scan_frame SAVE");
-               }
-       }
-
-#ifdef DEBUG
-printf("RotateCVScan::scan_frame 10 angle=%f\n", result);
-#endif
-       
-
-
-       return result;
-}
-
-int64_t RotateCVScan::get_cache(float angle)
-{
-       int64_t result = -1;
-       cache_lock->lock("RotateCVScan::get_cache");
-       for(int i = 0; i < cache.total; i++)
-       {
-               RotateCVScanCache *ptr = cache.values[i];
-               if(fabs(ptr->angle - angle) <= MIN_ANGLE)
-               {
-                       result = ptr->difference;
-                       break;
-               }
-       }
-       cache_lock->unlock();
-       return result;
-}
-
-void RotateCVScan::put_cache(float angle, int64_t difference)
-{
-       RotateCVScanCache *ptr = new RotateCVScanCache(angle, difference);
-       cache_lock->lock("RotateCVScan::put_cache");
-       cache.append(ptr);
-       cache_lock->unlock();
-}
-
-
-
-
-
-
-
-
-
-RotateCVScanCache::RotateCVScanCache(float angle, int64_t difference)
-{
-       this->angle = angle;
-       this->difference = difference;
-}
-
-
-
index b0c6ed47ed5292368c34c46224863bcf3fc3d514..823ce860077c7025e3b522af6b5b7896b6da99ce 100644 (file)
@@ -2,21 +2,21 @@
 /*
  * CINELERRA
  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
- * 
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
- * 
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- * 
+ *
  */
 
 #ifndef MOTION_H
@@ -64,7 +64,7 @@ class RotateCVScan;
 // Precision of rotation
 #define MIN_ANGLE 0.0001
 
-#define TRACKING_FILE "/tmp/motion"
+#define TRACKING_FILE "/tmp/motion-cv"
 
 class MotionCVConfig
 {
@@ -73,7 +73,7 @@ public:
 
        int equivalent(MotionCVConfig &that);
        void copy_from(MotionCVConfig &that);
-       void interpolate(MotionCVConfig &prev, MotionCVConfig &next, 
+       void interpolate(MotionCVConfig &prev, MotionCVConfig &next,
                int64_t prev_frame, int64_t next_frame, int64_t current_frame);
        void boundaries();
 
@@ -174,8 +174,8 @@ public:
                int sub_x,
                int sub_y);
 
-       static void clamp_scan(int w, 
-               int h, 
+       static void clamp_scan(int w,
+               int h,
                int *block_x1,
                int *block_y1,
                int *block_x2,
@@ -212,9 +212,17 @@ public:
        int current_dy;
        float current_angle;
 
-       FILE *active_fp;
-       char active_file[BCTEXTLEN];
-       int get_line_key(const char *filename, int64_t key, char *line, int len);
+       char cache_file[BCTEXTLEN];
+       FILE *cache_fp, *active_fp;
+       void reset_cache_file();
+       int open_cache_file();
+       void close_cache_file();
+       int load_cache_line();
+       int locate_cache_line(int64_t key);
+       int get_cache_line(int64_t key);
+       int put_cache_line(const char *line);
+       char cache_line[BCSTRLEN];
+       int64_t cache_key, active_key;
 // add constant frame offset values
        int dx_offset, dy_offset;
        int64_t tracking_frame;
@@ -333,8 +341,8 @@ public:
 class MotionCVScan : public LoadServer
 {
 public:
-       MotionCVScan(MotionCVMain *plugin, 
-               int total_clients, 
+       MotionCVScan(MotionCVMain *plugin,
+               int total_clients,
                int total_packages);
        ~MotionCVScan();
 
@@ -352,7 +360,7 @@ public:
        int64_t get_cache(int x, int y);
        void put_cache(int x, int y, int64_t difference);
 
-// Change between previous frame and current frame multiplied by 
+// Change between previous frame and current frame multiplied by
 // OVERSAMPLE
        int dx_result;
        int dy_result;
@@ -427,8 +435,8 @@ public:
 class RotateCVScan : public LoadServer
 {
 public:
-       RotateCVScan(MotionCVMain *plugin, 
-               int total_clients, 
+       RotateCVScan(MotionCVMain *plugin,
+               int total_clients,
                int total_packages);
        ~RotateCVScan();
 
index e0e2b109b3abf5ecd62d9fadd42b6798509f8258..2c4c00492d1afdcf8409683ef10a8a84feb9bde3 100644 (file)
@@ -2,41 +2,38 @@
 /*
  * CINELERRA
  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
- * 
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
- * 
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- * 
+ *
  */
 
 #include "bcdisplayinfo.h"
 #include "clip.h"
+#include "edl.h"
+#include "fonts.h"
+#include "edlsession.h"
 #include "language.h"
 #include "motion-cv.h"
 #include "motionwindow-cv.h"
-
-
-
-
-
-
-
-
+#include "mwindow.h"
+#include "pluginserver.h"
 
 MotionCVWindow::MotionCVWindow(MotionCVMain *plugin)
  : PluginClientWindow(plugin, 815, 650, 815, 650, 0)
 {
-       this->plugin = plugin; 
+       this->plugin = plugin;
 }
 
 MotionCVWindow::~MotionCVWindow()
@@ -49,207 +46,142 @@ void MotionCVWindow::create_objects()
        int x2 = 410;
        BC_Title *title;
 
-
-
-       add_subwindow(global = new MotionCVGlobal(plugin,
-               this,
-               x1,
-               y));
-
-       add_subwindow(rotate = new MotionCVRotate(plugin,
-               this,
-               x2,
-               y));
+       add_subwindow(global = new MotionCVGlobal(plugin, this, x1, y));
+       add_subwindow(rotate = new MotionCVRotate(plugin, this, x2, y));
        y += 50;
 
-       add_subwindow(title = new BC_Title(x1, 
-               y, 
+       add_subwindow(title = new BC_Title(x1, y,
                _("Translation search radius:\n(W/H Percent of image)")));
-       add_subwindow(global_range_w = new GlobalRange(plugin, 
-               x1 + title->get_w() + 10, 
-               y,
-               &plugin->config.global_range_w));
-       add_subwindow(global_range_h = new GlobalRange(plugin, 
-               x1 + title->get_w() + 30 + global_range_w->get_w(),
-               y,
-               &plugin->config.global_range_h));
-
-       add_subwindow(title = new BC_Title(x2, 
-               y, 
+       add_subwindow(global_range_w = new GlobalRange(plugin,
+                       x1 + title->get_w() + 10, y,
+                       &plugin->config.global_range_w));
+       add_subwindow(global_range_h = new GlobalRange(plugin,
+                       x1 + title->get_w() + 30 + global_range_w->get_w(), y,
+                       &plugin->config.global_range_h));
+
+       add_subwindow(title = new BC_Title(x2, y,
                _("Rotation search radius:\n(Degrees)")));
-       add_subwindow(rotation_range = new RotationRange(plugin, 
-               x2 + title->get_w() + 10, 
-               y));
+       add_subwindow(rotation_range =
+                     new RotationRange(plugin, x2 + title->get_w() + 10, y));
 
        y += 50;
-       add_subwindow(title = new BC_Title(x1, 
-               y, 
+       add_subwindow(title = new BC_Title(x1, y,
                _("Translation block size:\n(W/H Percent of image)")));
-       add_subwindow(global_block_w = new BlockSize(plugin, 
-               x1 + title->get_w() + 10, 
-               y,
-               &plugin->config.global_block_w));
-       add_subwindow(global_block_h = new BlockSize(plugin, 
-               x1 + title->get_w() + 30 + global_block_w->get_w(),
-               y,
-               &plugin->config.global_block_h));
-
-       add_subwindow(title = new BC_Title(x2, 
-               y, 
+       add_subwindow(global_block_w = new BlockSize(plugin,
+                       x1 + title->get_w() + 10, y,
+                       &plugin->config.global_block_w));
+       add_subwindow(global_block_h = new BlockSize(plugin,
+                       x1 + title->get_w() + 30 + global_block_w->get_w(), y,
+                       &plugin->config.global_block_h));
+
+       add_subwindow(title = new BC_Title(x2, y,
                _("Rotation block size:\n(W/H Percent of image)")));
-       add_subwindow(rotation_block_w = new BlockSize(plugin, 
-               x2 + title->get_w() + 10, 
-               y,
-               &plugin->config.rotation_block_w));
-       add_subwindow(rotation_block_h = new BlockSize(plugin, 
-               x2 + title->get_w() + 30 + rotation_block_w->get_w(),
-               y,
-               &plugin->config.rotation_block_h));
+       add_subwindow(rotation_block_w = new BlockSize(plugin,
+                       x2 + title->get_w() + 10, y,
+                       &plugin->config.rotation_block_w));
+       add_subwindow(rotation_block_h = new BlockSize(plugin,
+                       x2 + title->get_w() + 30 + rotation_block_w->get_w(), y,
+                       &plugin->config.rotation_block_h));
 
        y += 50;
        add_subwindow(title = new BC_Title(x1, y, _("Translation search steps:")));
-       add_subwindow(global_search_positions = new GlobalSearchPositions(plugin, 
-               x1 + title->get_w() + 10, 
-               y, 
-               80));
+       add_subwindow(global_search_positions = new GlobalSearchPositions(plugin,
+                       x1 + title->get_w() + 10, y, 80));
        global_search_positions->create_objects();
 
        add_subwindow(title = new BC_Title(x2, y, _("Rotation search steps:")));
-       add_subwindow(rotation_search_positions = new RotationSearchPositions(plugin, 
-               x2 + title->get_w() + 10, 
-               y, 
-               80));
+       add_subwindow(rotation_search_positions = new RotationSearchPositions(plugin,
+                       x2 + title->get_w() + 10, y, 80));
        rotation_search_positions->create_objects();
 
        y += 50;
        add_subwindow(title = new BC_Title(x, y, _("Translation direction:")));
-       add_subwindow(mode3 = new Mode3(plugin, 
-               this, 
-               x + title->get_w() + 10, 
-               y));
+       add_subwindow(mode3 = new Mode3(plugin,
+                       this, x + title->get_w() + 10, y));
        mode3->create_objects();
 
        y += 40;
        add_subwindow(title = new BC_Title(x, y + 10, _("Block X:")));
-       add_subwindow(block_x = new MotionCVBlockX(plugin, 
-               this, 
-               x + title->get_w() + 10, 
-               y));
-       add_subwindow(block_x_text = new MotionCVBlockXText(plugin, 
-               this, 
-               x + title->get_w() + 10 + block_x->get_w() + 10, 
-               y + 10));
+       add_subwindow(block_x = new MotionCVBlockX(plugin, this,
+                       x + title->get_w() + 10, y));
+       add_subwindow(block_x_text = new MotionCVBlockXText(plugin,
+                       this, x + title->get_w() + 10 + block_x->get_w() + 10, y + 10));
 
        y += 40;
        add_subwindow(title = new BC_Title(x, y + 10, _("Block Y:")));
-       add_subwindow(block_y = new MotionCVBlockY(plugin, 
-               this, 
-               x + title->get_w() + 10, 
-               y));
-       add_subwindow(block_y_text = new MotionCVBlockYText(plugin, 
-               this, 
-               x + title->get_w() + 10 + block_y->get_w() + 10, 
-               y + 10));
+       add_subwindow(block_y = new MotionCVBlockY(plugin, this,
+                       x + title->get_w() + 10, y));
+       add_subwindow(block_y_text = new MotionCVBlockYText(plugin, this,
+                       x + title->get_w() + 10 + block_y->get_w() + 10, y + 10));
 
        y += 50;
        add_subwindow(title = new BC_Title(x, y + 10, _("Maximum absolute offset:")));
-       add_subwindow(magnitude = new MotionCVMagnitude(plugin, 
-               x + title->get_w() + 10, 
-               y));
+       add_subwindow(magnitude = new MotionCVMagnitude(plugin,
+                       x + title->get_w() + 10, y));
 
        y += 40;
        add_subwindow(title = new BC_Title(x, y + 10, _("Settling speed:")));
        add_subwindow(return_speed = new MotionCVReturnSpeed(plugin,
-               x + title->get_w() + 10, 
-               y));
-
-
+                       x + title->get_w() + 10, y));
 
        y += 40;
-       add_subwindow(vectors = new MotionCVDrawVectors(plugin,
-               this,
-               x,
-               y));
+       add_subwindow(vectors = new MotionCVDrawVectors(plugin, this, x, y));
 
        add_subwindow(title = new BC_Title(x2, y, _("Tracking file:")));
        add_subwindow(tracking_file = new MotionCVTrackingFile(plugin,
-               plugin->config.tracking_file, this, x2+title->get_w() + 20, y));
+                       plugin->config.  tracking_file, this,
+                       x2 + title->get_w() + 20, y));
 
        y += 40;
-       add_subwindow(track_single = new TrackSingleFrame(plugin, 
-               this,
-               x, 
-               y));
-       add_subwindow(title = new BC_Title(x + track_single->get_w() + 20, 
-               y, 
-               _("Frame number:")));
-       add_subwindow(track_frame_number = new TrackFrameNumber(plugin, 
-               this,
-               x + track_single->get_w() + title->get_w() + 20, 
-               y));
-       add_subwindow(addtrackedframeoffset = new AddTrackedFrameOffset(plugin,
-               this,
-               x + track_single->get_w() + title->get_w() + 30,
-               y + track_single->get_h()));
-
+       x1 = x;  int y1 = y;
+       add_subwindow(track_single =
+               new TrackSingleFrame(plugin, this, x1, y1));
+       add_subwindow(title =
+               new BC_Title(x1 += track_single->get_w() + 20, y1, _("Frame number:")));
+       add_subwindow(track_frame_number =
+               new TrackFrameNumber(plugin, this, x1 += title->get_w(), y1));
+       add_subwindow(addtrackedframeoffset =
+               new AddTrackedFrameOffset(plugin, this, x1, y1+=track_frame_number->get_h()));
+       int pef = client->server->mwindow->edl->session->video_every_frame;
+       add_subwindow(pef_title = new BC_Title(x1, y1+=addtrackedframeoffset->get_h() + 5,
+               !pef ?  _("For best results\n"
+                               " Set: Play every frame\n"
+                               " Preferences-> Playback-> Video Out") :
+                       _("Currently using: Play every frame"), MEDIUMFONT,
+               !pef ? RED : GREEN));
 
        y += 20;
-       add_subwindow(track_previous = new TrackPreviousFrame(plugin, 
-               this,
-               x, 
-               y));
-
+       add_subwindow(track_previous = new TrackPreviousFrame(plugin, this, x, y));
        y += 20;
-       add_subwindow(previous_same = new PreviousFrameSameBlock(plugin, 
-               this,
-               x, 
-               y));
+       add_subwindow(previous_same = new PreviousFrameSameBlock(plugin, this, x, y));
 
        y += 40;
        //int y1 = y;
        add_subwindow(title = new BC_Title(x, y, _("Master layer:")));
-       add_subwindow(master_layer = new MasterLayer(plugin, 
-               this,
-               x + title->get_w() + 10, 
-               y));
+       add_subwindow(master_layer = new MasterLayer(plugin, this,
+                       x + title->get_w() + 10, y));
        master_layer->create_objects();
        y += 30;
 
-
        add_subwindow(title = new BC_Title(x, y, _("Action:")));
-       add_subwindow(mode1 = new Mode1(plugin, 
-               this,
-               x + title->get_w() + 10, 
-               y));
+       add_subwindow(mode1 = new Mode1(plugin, this,
+                       x + title->get_w() + 10, y));
        mode1->create_objects();
        y += 30;
 
-
-
-
        add_subwindow(title = new BC_Title(x, y, _("Calculation:")));
-       add_subwindow(mode2 = new Mode2(plugin, 
-               this, 
-               x + title->get_w() + 10, 
-               y));
+       add_subwindow(mode2 = new Mode2(plugin, this,
+                       x + title->get_w() + 10, y));
        mode2->create_objects();
 
-
-
        show_window(1);
 }
 
 void MotionCVWindow::update_mode()
 {
-       global_range_w->update(plugin->config.global_range_w,
-               MIN_RADIUS,
-               MAX_RADIUS);
-       global_range_h->update(plugin->config.global_range_h,
-               MIN_RADIUS,
-               MAX_RADIUS);
-       rotation_range->update(plugin->config.rotation_range,
-               MIN_ROTATION,
-               MAX_ROTATION);
+       global_range_w->update(plugin->config.global_range_w, MIN_RADIUS, MAX_RADIUS);
+       global_range_h->update(plugin->config.global_range_h, MIN_RADIUS, MAX_RADIUS);
+       rotation_range->update(plugin->config.rotation_range, MIN_ROTATION, MAX_ROTATION);
        vectors->update(plugin->config.draw_vectors);
        tracking_file->update(plugin->config.tracking_file);
        global->update(plugin->config.global);
@@ -257,32 +189,13 @@ void MotionCVWindow::update_mode()
        addtrackedframeoffset->update(plugin->config.addtrackedframeoffset);
 }
 
-
-
-
-
-
-
-
-
-
-
-
-GlobalRange::GlobalRange(MotionCVMain *plugin, 
-       int x, 
-       int y,
-       int *value)
- : BC_IPot(x, 
-               y, 
-               (int64_t)*value,
-               (int64_t)MIN_RADIUS,
-               (int64_t)MAX_RADIUS)
+GlobalRange::GlobalRange(MotionCVMain *plugin, int x, int y, int *value)
+:BC_IPot(x, y, (int64_t) * value, (int64_t) MIN_RADIUS, (int64_t) MAX_RADIUS)
 {
        this->plugin = plugin;
        this->value = value;
 }
 
-
 int GlobalRange::handle_event()
 {
        *value = (int)get_value();
@@ -290,22 +203,15 @@ int GlobalRange::handle_event()
        return 1;
 }
 
-
-
-
-RotationRange::RotationRange(MotionCVMain *plugin, 
-       int x, 
-       int y)
- : BC_IPot(x, 
-               y, 
-               (int64_t)plugin->config.rotation_range,
-               (int64_t)MIN_ROTATION,
-               (int64_t)MAX_ROTATION)
+RotationRange::RotationRange(MotionCVMain *plugin, int x, int y)
+ :
+BC_IPot(x,
+    y, (int64_t) plugin->config.rotation_range,
+    (int64_t) MIN_ROTATION, (int64_t) MAX_ROTATION)
 {
        this->plugin = plugin;
 }
 
-
 int RotationRange::handle_event()
 {
        plugin->config.rotation_range = (int)get_value();
@@ -313,28 +219,13 @@ int RotationRange::handle_event()
        return 1;
 }
 
-
-
-
-
-
-
-
-BlockSize::BlockSize(MotionCVMain *plugin, 
-       int x, 
-       int y,
-       int *value)
- : BC_IPot(x, 
-               y, 
-               (int64_t)*value,
-               (int64_t)MIN_BLOCK,
-               (int64_t)MAX_BLOCK)
+BlockSize::BlockSize(MotionCVMain *plugin, int x, int y, int *value)
+ : BC_IPot(x, y, (int64_t) * value, (int64_t) MIN_BLOCK, (int64_t) MAX_BLOCK)
 {
        this->plugin = plugin;
        this->value = value;
 }
 
-
 int BlockSize::handle_event()
 {
        *value = (int)get_value();
@@ -342,30 +233,13 @@ int BlockSize::handle_event()
        return 1;
 }
 
-
-
-
-
-
-
-
-
-
-
-
-
-GlobalSearchPositions::GlobalSearchPositions(MotionCVMain *plugin, 
-       int x, 
-       int y,
-       int w)
- : BC_PopupMenu(x,
-       y,
-       w,
-       "",
-       1)
+GlobalSearchPositions::GlobalSearchPositions(MotionCVMain *plugin,
+                                            int x, int y, int w)
+ : BC_PopupMenu(x, y, w, "", 1)
 {
        this->plugin = plugin;
 }
+
 void GlobalSearchPositions::create_objects()
 {
        add_item(new BC_MenuItem("64"));
@@ -392,24 +266,13 @@ int GlobalSearchPositions::handle_event()
        return 1;
 }
 
-
-
-
-
-
-
-RotationSearchPositions::RotationSearchPositions(MotionCVMain *plugin, 
-       int x, 
-       int y,
-       int w)
- : BC_PopupMenu(x,
-       y,
-       w,
-       "",
-       1)
+RotationSearchPositions::RotationSearchPositions(MotionCVMain *plugin,
+               int x, int y, int w)
+ : BC_PopupMenu(x, y, w, "", 1)
 {
        this->plugin = plugin;
 }
+
 void RotationSearchPositions::create_objects()
 {
        add_item(new BC_MenuItem("4"));
@@ -428,21 +291,8 @@ int RotationSearchPositions::handle_event()
        return 1;
 }
 
-
-
-
-
-
-
-
-MotionCVMagnitude::MotionCVMagnitude(MotionCVMain *plugin, 
-       int x, 
-       int y)
- : BC_IPot(x, 
-               y, 
-               (int64_t)plugin->config.magnitude,
-               (int64_t)0,
-               (int64_t)100)
+MotionCVMagnitude::MotionCVMagnitude(MotionCVMain *plugin, int x, int y)
+ : BC_IPot(x, y, (int64_t) plugin->config.magnitude, (int64_t) 0, (int64_t) 100)
 {
        this->plugin = plugin;
 }
@@ -454,15 +304,8 @@ int MotionCVMagnitude::handle_event()
        return 1;
 }
 
-
-MotionCVReturnSpeed::MotionCVReturnSpeed(MotionCVMain *plugin, 
-       int x, 
-       int y)
- : BC_IPot(x, 
-               y, 
-               (int64_t)plugin->config.return_speed,
-               (int64_t)0,
-               (int64_t)100)
+MotionCVReturnSpeed::MotionCVReturnSpeed(MotionCVMain *plugin, int x, int y)
+ : BC_IPot(x, y, (int64_t) plugin->config.return_speed, (int64_t) 0, (int64_t) 100)
 {
        this->plugin = plugin;
 }
@@ -474,15 +317,9 @@ int MotionCVReturnSpeed::handle_event()
        return 1;
 }
 
-
-
-AddTrackedFrameOffset::AddTrackedFrameOffset(MotionCVMain *plugin, 
-       MotionCVWindow *gui,
-       int x, 
-       int y)
- : BC_CheckBox(x, 
-       y, 
-       plugin->config.addtrackedframeoffset,
+AddTrackedFrameOffset::AddTrackedFrameOffset(MotionCVMain *plugin,
+               MotionCVWindow *gui, int x, int y)
+ : BC_CheckBox(x, y, plugin->config.addtrackedframeoffset,
        _("Add (loaded) offset from tracked frame"))
 {
        this->plugin = plugin;
@@ -497,7 +334,7 @@ int AddTrackedFrameOffset::handle_event()
 }
 
 MotionCVTrackingFile::MotionCVTrackingFile(MotionCVMain *plugin,
-       const char *filename, MotionCVWindow *gui, int x, int y)
+               const char *filename, MotionCVWindow *gui, int x, int y)
  : BC_TextBox(x, y, 250, 1, filename)
 {
        this->plugin = plugin;
@@ -511,14 +348,9 @@ int MotionCVTrackingFile::handle_event()
        return 1;
 }
 
-MotionCVGlobal::MotionCVGlobal(MotionCVMain *plugin, 
-       MotionCVWindow *gui,
-       int x, 
-       int y)
- : BC_CheckBox(x, 
-       y, 
-       plugin->config.global,
-       _("Track translation"))
+MotionCVGlobal::MotionCVGlobal(MotionCVMain *plugin,
+               MotionCVWindow *gui, int x, int y)
+ :BC_CheckBox(x, y, plugin->config.global, _("Track translation"))
 {
        this->plugin = plugin;
        this->gui = gui;
@@ -531,14 +363,9 @@ int MotionCVGlobal::handle_event()
        return 1;
 }
 
-MotionCVRotate::MotionCVRotate(MotionCVMain *plugin, 
-       MotionCVWindow *gui,
-       int x, 
-       int y)
- : BC_CheckBox(x, 
-       y, 
-       plugin->config.rotate,
-       _("Track rotation"))
+MotionCVRotate::MotionCVRotate(MotionCVMain *plugin,
+               MotionCVWindow *gui, int x, int y)
+ : BC_CheckBox(x, y, plugin->config.rotate, _("Track rotation"))
 {
        this->plugin = plugin;
        this->gui = gui;
@@ -551,19 +378,9 @@ int MotionCVRotate::handle_event()
        return 1;
 }
 
-
-
-
-
-MotionCVBlockX::MotionCVBlockX(MotionCVMain *plugin, 
-       MotionCVWindow *gui,
-       int x, 
-       int y)
- : BC_FPot(x,
-       y,
-       plugin->config.block_x,
-       (float)0, 
-       (float)100)
+MotionCVBlockX::MotionCVBlockX(MotionCVMain *plugin,
+               MotionCVWindow *gui, int x, int y)
+ : BC_FPot(x, y, plugin->config.block_x, (float)0, (float)100)
 {
        this->plugin = plugin;
        this->gui = gui;
@@ -577,18 +394,9 @@ int MotionCVBlockX::handle_event()
        return 1;
 }
 
-
-
-
-MotionCVBlockY::MotionCVBlockY(MotionCVMain *plugin, 
-       MotionCVWindow *gui,
-       int x, 
-       int y)
- : BC_FPot(x,
-       y,
-       (float)plugin->config.block_y,
-       (float)0, 
-       (float)100)
+MotionCVBlockY::MotionCVBlockY(MotionCVMain *plugin,
+               MotionCVWindow *gui, int x, int y)
+ : BC_FPot(x, y, (float)plugin->config.block_y, (float)0, (float)100)
 {
        this->plugin = plugin;
        this->gui = gui;
@@ -602,15 +410,9 @@ int MotionCVBlockY::handle_event()
        return 1;
 }
 
-MotionCVBlockXText::MotionCVBlockXText(MotionCVMain *plugin, 
-       MotionCVWindow *gui,
-       int x, 
-       int y)
- : BC_TextBox(x,
-       y,
-       75,
-       1,
-       (float)plugin->config.block_x)
+MotionCVBlockXText::MotionCVBlockXText(MotionCVMain *plugin,
+               MotionCVWindow *gui, int x, int y)
+ : BC_TextBox(x, y, 75, 1, (float)plugin->config.block_x)
 {
        this->plugin = plugin;
        this->gui = gui;
@@ -625,18 +427,9 @@ int MotionCVBlockXText::handle_event()
        return 1;
 }
 
-
-
-
-MotionCVBlockYText::MotionCVBlockYText(MotionCVMain *plugin, 
-       MotionCVWindow *gui,
-       int x, 
-       int y)
- : BC_TextBox(x,
-       y,
-       75,
-       1,
-       (float)plugin->config.block_y)
+MotionCVBlockYText::MotionCVBlockYText(MotionCVMain *plugin,
+               MotionCVWindow *gui, int x, int y)
+ : BC_TextBox(x, y, 75, 1, (float)plugin->config.block_y)
 {
        this->plugin = plugin;
        this->gui = gui;
@@ -651,29 +444,9 @@ int MotionCVBlockYText::handle_event()
        return 1;
 }
 
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-MotionCVDrawVectors::MotionCVDrawVectors(MotionCVMain *plugin, 
-       MotionCVWindow *gui,
-       int x, 
-       int y)
- : BC_CheckBox(x,
-       y, 
-       plugin->config.draw_vectors,
-       _("Draw vectors"))
+MotionCVDrawVectors::MotionCVDrawVectors(MotionCVMain *plugin,
+               MotionCVWindow *gui, int x, int y)
+ : BC_CheckBox(x, y, plugin->config.draw_vectors, _("Draw vectors"))
 {
        this->gui = gui;
        this->plugin = plugin;
@@ -686,21 +459,12 @@ int MotionCVDrawVectors::handle_event()
        return 1;
 }
 
-
-
-
-
-
-
-
-TrackSingleFrame::TrackSingleFrame(MotionCVMain *plugin, 
-       MotionCVWindow *gui,
-       int x, 
-       int y)
- : BC_Radial(x, 
-       y, 
-       plugin->config.mode3 == MotionCVConfig::TRACK_SINGLE, 
-       _("Track single frame"))
+TrackSingleFrame::TrackSingleFrame(MotionCVMain *plugin,
+               MotionCVWindow *gui, int x, int y)
+ :
+BC_Radial(x,
+      y, plugin->config.mode3 == MotionCVConfig::TRACK_SINGLE,
+      _("Track single frame"))
 {
        this->plugin = plugin;
        this->gui = gui;
@@ -716,22 +480,14 @@ int TrackSingleFrame::handle_event()
        return 1;
 }
 
-
-
-
-
-
-
-
-TrackFrameNumber::TrackFrameNumber(MotionCVMain *plugin, 
-       MotionCVWindow *gui,
-       int x, 
-       int y)
+TrackFrameNumber::TrackFrameNumber(MotionCVMain *plugin,
+               MotionCVWindow *gui, int x, int y)
  : BC_TextBox(x, y, 100, 1, plugin->config.track_frame)
 {
        this->plugin = plugin;
        this->gui = gui;
-       if(plugin->config.mode3 != MotionCVConfig::TRACK_SINGLE) disable();
+       if( plugin->config.mode3 != MotionCVConfig::TRACK_SINGLE )
+               disable();
 }
 
 int TrackFrameNumber::handle_event()
@@ -741,24 +497,17 @@ int TrackFrameNumber::handle_event()
        return 1;
 }
 
-
-
-
-
-
-
-TrackPreviousFrame::TrackPreviousFrame(MotionCVMain *plugin, 
-       MotionCVWindow *gui,
-       int x, 
-       int y)
- : BC_Radial(x, 
-       y, 
-       plugin->config.mode3 == MotionCVConfig::TRACK_PREVIOUS, 
-       _("Track previous frame"))
+TrackPreviousFrame::TrackPreviousFrame(MotionCVMain *plugin,
+               MotionCVWindow *gui, int x, int y)
+ :
+BC_Radial(x,
+      y, plugin->config.mode3 == MotionCVConfig::TRACK_PREVIOUS,
+      _("Track previous frame"))
 {
        this->plugin = plugin;
        this->gui = gui;
 }
+
 int TrackPreviousFrame::handle_event()
 {
        plugin->config.mode3 = MotionCVConfig::TRACK_PREVIOUS;
@@ -769,25 +518,17 @@ int TrackPreviousFrame::handle_event()
        return 1;
 }
 
-
-
-
-
-
-
-
-PreviousFrameSameBlock::PreviousFrameSameBlock(MotionCVMain *plugin, 
-       MotionCVWindow *gui,
-       int x, 
-       int y)
- : BC_Radial(x, 
-       y, 
-       plugin->config.mode3 == MotionCVConfig::PREVIOUS_SAME_BLOCK, 
-       _("Previous frame same block"))
+PreviousFrameSameBlock::PreviousFrameSameBlock(MotionCVMain *plugin,
+               MotionCVWindow *gui, int x, int y)
+ :
+BC_Radial(x,
+      y, plugin->config.mode3 == MotionCVConfig::PREVIOUS_SAME_BLOCK,
+      _("Previous frame same block"))
 {
        this->plugin = plugin;
        this->gui = gui;
 }
+
 int PreviousFrameSameBlock::handle_event()
 {
        plugin->config.mode3 = MotionCVConfig::PREVIOUS_SAME_BLOCK;
@@ -798,16 +539,10 @@ int PreviousFrameSameBlock::handle_event()
        return 1;
 }
 
-
-
-
-
-
-
-
-MasterLayer::MasterLayer(MotionCVMain *plugin, MotionCVWindow *gui, int x, int y)
+MasterLayer::MasterLayer(MotionCVMain *plugin, MotionCVWindow *gui, int x,
+                        int y)
  : BC_PopupMenu(x, y, calculate_w(gui),
-               to_text(plugin->config.bottom_is_master))
+            to_text(plugin->config.bottom_is_master))
 {
        this->plugin = plugin;
        this->gui = gui;
@@ -828,11 +563,11 @@ void MasterLayer::create_objects()
 
 int MasterLayer::from_text(char *text)
 {
-       if(!strcmp(text, _("Top"))) return 0;
+       if( !strcmp(text, _("Top")) ) return 0;
        return 1;
 }
 
-const charMasterLayer::to_text(int mode)
+const char *MasterLayer::to_text(int mode)
 {
        return mode ? _("Bottom") : _("Top");
 }
@@ -845,16 +580,8 @@ int MasterLayer::calculate_w(MotionCVWindow *gui)
        return result + 50;
 }
 
-
-
-
-
-
-
-
 Mode1::Mode1(MotionCVMain *plugin, MotionCVWindow *gui, int x, int y)
- : BC_PopupMenu(x, y, calculate_w(gui),
-               to_text(plugin->config.mode1))
+ : BC_PopupMenu(x, y, calculate_w(gui), to_text(plugin->config.mode1))
 {
        this->plugin = plugin;
        this->gui = gui;
@@ -878,22 +605,22 @@ void Mode1::create_objects()
 
 int Mode1::from_text(char *text)
 {
-       if(!strcmp(text, _("Track Subpixel"))) return MotionCVConfig::TRACK;
-       if(!strcmp(text, _("Track Pixel"))) return MotionCVConfig::TRACK_PIXEL;
-       if(!strcmp(text, _("Stabilize Subpixel"))) return MotionCVConfig::STABILIZE;
-       if(!strcmp(text, _("Stabilize Pixel"))) return MotionCVConfig::STABILIZE_PIXEL;
-       //if(!strcmp(text, _("Do Nothing"))) return MotionCVConfig::NOTHING;
+       if( !strcmp(text, _("Track Subpixel")) ) return MotionCVConfig::TRACK;
+       if( !strcmp(text, _("Track Pixel")) ) return MotionCVConfig::TRACK_PIXEL;
+       if( !strcmp(text, _("Stabilize Subpixel")) ) return MotionCVConfig::STABILIZE;
+       if( !strcmp(text, _("Stabilize Pixel")) ) return MotionCVConfig::STABILIZE_PIXEL;
+       //if( !strcmp(text, _("Do Nothing")) ) return MotionCVConfig::NOTHING;
        return MotionCVConfig::NOTHING;
 }
 
-const charMode1::to_text(int mode)
+const char *Mode1::to_text(int mode)
 {
-       switch(mode) {
-       case MotionCVConfig::TRACK:     return _("Track Subpixel");
-       case MotionCVConfig::TRACK_PIXEL:       return _("Track Pixel");
-       case MotionCVConfig::STABILIZE: return _("Stabilize Subpixel");
+       switch( mode ) {
+       case MotionCVConfig::TRACK: return _("Track Subpixel");
+       case MotionCVConfig::TRACK_PIXEL: return _("Track Pixel");
+       case MotionCVConfig::STABILIZE: return _("Stabilize Subpixel");
        case MotionCVConfig::STABILIZE_PIXEL: return _("Stabilize Pixel");
-       case MotionCVConfig::NOTHING:   return _("Do Nothing");
+       case MotionCVConfig::NOTHING: return _("Do Nothing");
        }
        return "";
 }
@@ -909,13 +636,8 @@ int Mode1::calculate_w(MotionCVWindow *gui)
        return result + 50;
 }
 
-
-
-
-
 Mode2::Mode2(MotionCVMain *plugin, MotionCVWindow *gui, int x, int y)
- : BC_PopupMenu(x, y, calculate_w(gui),
-               to_text(plugin->config.mode2))
+ : BC_PopupMenu(x, y, calculate_w(gui), to_text(plugin->config.mode2))
 {
        this->plugin = plugin;
        this->gui = gui;
@@ -930,24 +652,24 @@ int Mode2::handle_event()
 
 void Mode2::create_objects()
 {
-       add_item(new BC_MenuItem(to_text(MotionCVConfig::NO_CALCULATE)));
-       add_item(new BC_MenuItem(to_text(MotionCVConfig::RECALCULATE)));
        add_item(new BC_MenuItem(to_text(MotionCVConfig::SAVE)));
        add_item(new BC_MenuItem(to_text(MotionCVConfig::LOAD)));
+       add_item(new BC_MenuItem(to_text(MotionCVConfig::RECALCULATE)));
+       add_item(new BC_MenuItem(to_text(MotionCVConfig::NO_CALCULATE)));
 }
 
 int Mode2::from_text(char *text)
 {
-       if(!strcmp(text, _("Recalculate"))) return MotionCVConfig::RECALCULATE;
-       if(!strcmp(text, _("Save coords to tracking file"))) return MotionCVConfig::SAVE;
-       if(!strcmp(text, _("Load coords from tracking file"))) return MotionCVConfig::LOAD;
-       //if(!strcmp(text, _("Don't Calculate"))) return MotionCVConfig::NO_CALCULATE;
+       if( !strcmp(text, _("Recalculate")) ) return MotionCVConfig::RECALCULATE;
+       if( !strcmp(text, _("Save coords to tracking file")) ) return MotionCVConfig::SAVE;
+       if( !strcmp(text, _("Load coords from tracking file")) ) return MotionCVConfig::LOAD;
+       //if( !strcmp(text, _("Don't Calculate")) ) return MotionCVConfig::NO_CALCULATE;
        return MotionCVConfig::NO_CALCULATE;
 }
 
-const charMode2::to_text(int mode)
+const char *Mode2::to_text(int mode)
 {
-       switch(mode) {
+       switch( mode ) {
        case MotionCVConfig::NO_CALCULATE: return _("Don't Calculate");
        case MotionCVConfig::RECALCULATE: return _("Recalculate");
        case MotionCVConfig::SAVE: return _("Save coords to tracking file");
@@ -966,18 +688,10 @@ int Mode2::calculate_w(MotionCVWindow *gui)
        return result + 50;
 }
 
-
-
-
-
-
-
-
-
-
 Mode3::Mode3(MotionCVMain *plugin, MotionCVWindow *gui, int x, int y)
- : BC_PopupMenu(x, y, calculate_w(gui),
-               to_text(plugin->config.horizontal_only, plugin->config.vertical_only))
+ :
+BC_PopupMenu(x, y, calculate_w(gui),
+        to_text(plugin->config.horizontal_only, plugin->config.vertical_only))
 {
        this->plugin = plugin;
        this->gui = gui;
@@ -985,7 +699,8 @@ Mode3::Mode3(MotionCVMain *plugin, MotionCVWindow *gui, int x, int y)
 
 int Mode3::handle_event()
 {
-       from_text(&plugin->config.horizontal_only, &plugin->config.vertical_only, get_text());
+       from_text(&plugin->config.horizontal_only,
+                 &plugin->config.vertical_only, get_text());
        plugin->send_configure_change();
        return 1;
 }
@@ -1001,14 +716,14 @@ void Mode3::from_text(int *horizontal_only, int *vertical_only, char *text)
 {
        *horizontal_only = 0;
        *vertical_only = 0;
-       if(!strcmp(text, to_text(1, 0))) *horizontal_only = 1;
-       if(!strcmp(text, to_text(0, 1))) *vertical_only = 1;
+       if( !strcmp(text, to_text(1, 0)) ) *horizontal_only = 1;
+       if( !strcmp(text, to_text(0, 1)) ) *vertical_only = 1;
 }
 
-const charMode3::to_text(int horizontal_only, int vertical_only)
+const char *Mode3::to_text(int horizontal_only, int vertical_only)
 {
-       if(horizontal_only) return _("Horizontal only");
-       if(vertical_only) return _("Vertical only");
+       if( horizontal_only ) return _("Horizontal only");
+       if( vertical_only ) return _("Vertical only");
        return _("Both");
 }
 
@@ -1021,3 +736,4 @@ int Mode3::calculate_w(MotionCVWindow *gui)
        return result + 50;
 }
 
+
index c3cd5f8d11f91803189888b90e30f69cfa0366e2..53fdd0704c98ca9b4ecb7c3703b7a587bcce6582 100644 (file)
@@ -2,21 +2,21 @@
 /*
  * CINELERRA
  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
- * 
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
- * 
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- * 
+ *
  */
 
 #include "guicast.h"
@@ -82,9 +82,9 @@ public:
 class TrackSingleFrame : public BC_Radial
 {
 public:
-       TrackSingleFrame(MotionCVMain *plugin, 
+       TrackSingleFrame(MotionCVMain *plugin,
                MotionCVWindow *gui,
-               int x, 
+               int x,
                int y);
        int handle_event();
        MotionCVMain *plugin;
@@ -94,9 +94,9 @@ public:
 class TrackFrameNumber : public BC_TextBox
 {
 public:
-       TrackFrameNumber(MotionCVMain *plugin, 
+       TrackFrameNumber(MotionCVMain *plugin,
                MotionCVWindow *gui,
-               int x, 
+               int x,
                int y);
        int handle_event();
        MotionCVMain *plugin;
@@ -106,9 +106,9 @@ public:
 class TrackPreviousFrame : public BC_Radial
 {
 public:
-       TrackPreviousFrame(MotionCVMain *plugin, 
+       TrackPreviousFrame(MotionCVMain *plugin,
                MotionCVWindow *gui,
-               int x, 
+               int x,
                int y);
        int handle_event();
        MotionCVMain *plugin;
@@ -118,9 +118,9 @@ public:
 class PreviousFrameSameBlock : public BC_Radial
 {
 public:
-       PreviousFrameSameBlock(MotionCVMain *plugin, 
+       PreviousFrameSameBlock(MotionCVMain *plugin,
                MotionCVWindow *gui,
-               int x, 
+               int x,
                int y);
        int handle_event();
        MotionCVMain *plugin;
@@ -130,8 +130,8 @@ public:
 class GlobalRange : public BC_IPot
 {
 public:
-       GlobalRange(MotionCVMain *plugin, 
-               int x, 
+       GlobalRange(MotionCVMain *plugin,
+               int x,
                int y,
                int *value);
        int handle_event();
@@ -142,8 +142,8 @@ public:
 class RotationRange : public BC_IPot
 {
 public:
-       RotationRange(MotionCVMain *plugin, 
-               int x, 
+       RotationRange(MotionCVMain *plugin,
+               int x,
                int y);
        int handle_event();
        MotionCVMain *plugin;
@@ -152,8 +152,8 @@ public:
 class BlockSize : public BC_IPot
 {
 public:
-       BlockSize(MotionCVMain *plugin, 
-               int x, 
+       BlockSize(MotionCVMain *plugin,
+               int x,
                int y,
                int *value);
        int handle_event();
@@ -164,9 +164,9 @@ public:
 class MotionCVBlockX : public BC_FPot
 {
 public:
-       MotionCVBlockX(MotionCVMain *plugin, 
+       MotionCVBlockX(MotionCVMain *plugin,
                MotionCVWindow *gui,
-               int x, 
+               int x,
                int y);
        int handle_event();
        MotionCVWindow *gui;
@@ -176,9 +176,9 @@ public:
 class MotionCVBlockY : public BC_FPot
 {
 public:
-       MotionCVBlockY(MotionCVMain *plugin, 
+       MotionCVBlockY(MotionCVMain *plugin,
                MotionCVWindow *gui,
-               int x, 
+               int x,
                int y);
        int handle_event();
        MotionCVWindow *gui;
@@ -188,9 +188,9 @@ public:
 class MotionCVBlockXText : public BC_TextBox
 {
 public:
-       MotionCVBlockXText(MotionCVMain *plugin, 
+       MotionCVBlockXText(MotionCVMain *plugin,
                MotionCVWindow *gui,
-               int x, 
+               int x,
                int y);
        int handle_event();
        MotionCVWindow *gui;
@@ -200,9 +200,9 @@ public:
 class MotionCVBlockYText : public BC_TextBox
 {
 public:
-       MotionCVBlockYText(MotionCVMain *plugin, 
+       MotionCVBlockYText(MotionCVMain *plugin,
                MotionCVWindow *gui,
-               int x, 
+               int x,
                int y);
        int handle_event();
        MotionCVWindow *gui;
@@ -212,8 +212,8 @@ public:
 class GlobalSearchPositions : public BC_PopupMenu
 {
 public:
-       GlobalSearchPositions(MotionCVMain *plugin, 
-               int x, 
+       GlobalSearchPositions(MotionCVMain *plugin,
+               int x,
                int y,
                int w);
        void create_objects();
@@ -224,8 +224,8 @@ public:
 class RotationSearchPositions : public BC_PopupMenu
 {
 public:
-       RotationSearchPositions(MotionCVMain *plugin, 
-               int x, 
+       RotationSearchPositions(MotionCVMain *plugin,
+               int x,
                int y,
                int w);
        void create_objects();
@@ -236,8 +236,8 @@ public:
 class MotionCVMagnitude : public BC_IPot
 {
 public:
-       MotionCVMagnitude(MotionCVMain *plugin, 
-               int x, 
+       MotionCVMagnitude(MotionCVMain *plugin,
+               int x,
                int y);
        int handle_event();
        MotionCVMain *plugin;
@@ -246,8 +246,8 @@ public:
 class MotionCVReturnSpeed : public BC_IPot
 {
 public:
-       MotionCVReturnSpeed(MotionCVMain *plugin, 
-               int x, 
+       MotionCVReturnSpeed(MotionCVMain *plugin,
+               int x,
                int y);
        int handle_event();
        MotionCVMain *plugin;
@@ -258,9 +258,9 @@ public:
 class MotionCVDrawVectors : public BC_CheckBox
 {
 public:
-       MotionCVDrawVectors(MotionCVMain *plugin, 
+       MotionCVDrawVectors(MotionCVMain *plugin,
                MotionCVWindow *gui,
-               int x, 
+               int x,
                int y);
        int handle_event();
        MotionCVMain *plugin;
@@ -270,9 +270,9 @@ public:
 class AddTrackedFrameOffset : public BC_CheckBox
 {
 public:
-       AddTrackedFrameOffset(MotionCVMain *plugin, 
+       AddTrackedFrameOffset(MotionCVMain *plugin,
                MotionCVWindow *gui,
-               int x, 
+               int x,
                int y);
        int handle_event();
        MotionCVWindow *gui;
@@ -292,9 +292,9 @@ public:
 class MotionCVGlobal : public BC_CheckBox
 {
 public:
-       MotionCVGlobal(MotionCVMain *plugin, 
+       MotionCVGlobal(MotionCVMain *plugin,
                MotionCVWindow *gui,
-               int x, 
+               int x,
                int y);
        int handle_event();
        MotionCVWindow *gui;
@@ -304,9 +304,9 @@ public:
 class MotionCVRotate : public BC_CheckBox
 {
 public:
-       MotionCVRotate(MotionCVMain *plugin, 
+       MotionCVRotate(MotionCVMain *plugin,
                MotionCVWindow *gui,
-               int x, 
+               int x,
                int y);
        int handle_event();
        MotionCVWindow *gui;
@@ -353,6 +353,7 @@ public:
        MasterLayer *master_layer;
        Mode2 *mode2;
        Mode3 *mode3;
+       BC_Title *pef_title;
 
        MotionCVMain *plugin;
 };
index ed6768037e5deb70bd7d38a2bed497b226f3f76a..b1eb5b8e38b6e9550b8df0d364cbf0daa59b9d1e 100644 (file)
@@ -27,6 +27,7 @@
 #include "filexml.h"
 #include "keyframe.h"
 #include "language.h"
+#include "mainerror.h"
 #include "motion.h"
 #include "motionscan.h"
 #include "motionwindow.h"
 REGISTER_PLUGIN(MotionMain)
 
 
-#undef DEBUG
-
-// #ifndef DEBUG
-// #define DEBUG
-// #endif
-
-
+//#define DEBUG
 
 MotionConfig::MotionConfig()
 {
-       global_range_w = 10;
-       global_range_h = 10;
-       rotation_range = 5;
+       global_range_w = 25; //5;
+       global_range_h = 25; //5;
+       rotation_range = 8; //5;
        rotation_center = 0;
        block_count = 1;
-       global_block_w = 50; // MIN_BLOCK;
-       global_block_h = 50; // MIN_BLOCK;
-//     rotation_block_w = MIN_BLOCK;
-//     rotation_block_h = MIN_BLOCK;
+       global_block_w = 33; //MIN_BLOCK;
+       global_block_h = 33; //MIN_BLOCK;
        block_x = 50;
        block_y = 50;
        global_positions = 256;
-       rotate_positions = 4;
-       magnitude = 25;
+       rotate_positions = 8; // 4;
+       magnitude = 100;
        rotate_magnitude = 30;
-       return_speed = 8;
-       rotate_return_speed = 8;
-       action_type = MotionScan::STABILIZE_PIXEL;
+       return_speed = 5; //0;
+       rotate_return_speed = 5; //0;
+       action_type = MotionScan::STABILIZE;
        global = 1;
        rotate = 1;
        addtrackedframeoffset = 0;
-       tracking_type = MotionScan::CALCULATE;
-       draw_vectors = 0;
-       tracking_object = MotionScan::TRACK_PREVIOUS;
+       strcpy(tracking_file, TRACKING_FILE);
+       tracking_type = MotionScan::SAVE; //MotionScan::NO_CALCULATE;
+       tracking_object = MotionScan::TRACK_PREVIOUS; //TRACK_SINGLE;
+       draw_vectors = 1; //0;
        track_frame = 0;
        bottom_is_master = 1;
        horizontal_only = 0;
        vertical_only = 0;
 }
 
-void MotionConfig::set_cpus(int cpus)
-{
-       int gpos = 64, gpos_limit = 16 * cpus;
-       if( gpos_limit > 131072 ) gpos_limit = 131072;
-       while( gpos < gpos_limit ) gpos *= 2;
-       global_positions = gpos;
-       int rpos = 4, rpos_limit = cpus / 4;
-       if( rpos_limit > 32 ) gpos_limit = 32;
-       while( rpos < rpos_limit ) rpos *= 2;
-       rotate_positions = rpos;
-}
 
 void MotionConfig::boundaries()
 {
@@ -103,8 +86,6 @@ void MotionConfig::boundaries()
        CLAMP(block_count, MIN_BLOCKS, MAX_BLOCKS);
        CLAMP(global_block_w, MIN_BLOCK, MAX_BLOCK);
        CLAMP(global_block_h, MIN_BLOCK, MAX_BLOCK);
-//     CLAMP(rotation_block_w, MIN_BLOCK, MAX_BLOCK);
-//     CLAMP(rotation_block_h, MIN_BLOCK, MAX_BLOCK);
 }
 
 int MotionConfig::equivalent(MotionConfig &that)
@@ -114,15 +95,12 @@ int MotionConfig::equivalent(MotionConfig &that)
                rotation_range == that.rotation_range &&
                rotation_center == that.rotation_center &&
                action_type == that.action_type &&
-               global == that.global &&
-               rotate == that.rotate &&
+               global == that.global && rotate == that.rotate &&
                addtrackedframeoffset == that.addtrackedframeoffset &&
                draw_vectors == that.draw_vectors &&
                block_count == that.block_count &&
                global_block_w == that.global_block_w &&
                global_block_h == that.global_block_h &&
-//             rotation_block_w == that.rotation_block_w &&
-//             rotation_block_h == that.rotation_block_h &&
                EQUIV(block_x, that.block_x) &&
                EQUIV(block_y, that.block_y) &&
                global_positions == that.global_positions &&
@@ -157,8 +135,6 @@ void MotionConfig::copy_from(MotionConfig &that)
        rotate_positions = that.rotate_positions;
        global_block_w = that.global_block_w;
        global_block_h = that.global_block_h;
-//     rotation_block_w = that.rotation_block_w;
-//     rotation_block_h = that.rotation_block_h;
        magnitude = that.magnitude;
        return_speed = that.return_speed;
        rotate_magnitude = that.rotate_magnitude;
@@ -170,62 +146,13 @@ void MotionConfig::copy_from(MotionConfig &that)
        vertical_only = that.vertical_only;
 }
 
-void MotionConfig::interpolate(MotionConfig &prev,
-       MotionConfig &next,
-       int64_t prev_frame,
-       int64_t next_frame,
-       int64_t current_frame)
+void MotionConfig::interpolate(MotionConfig &prev, MotionConfig &next,
+       int64_t prev_frame, int64_t next_frame, int64_t current_frame)
 {
-       //double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
-       //double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
-       this->block_x = prev.block_x;
-       this->block_y = prev.block_y;
-       global_range_w = prev.global_range_w;
-       global_range_h = prev.global_range_h;
-       rotation_range = prev.rotation_range;
-       rotation_center = prev.rotation_center;
-       action_type = prev.action_type;
-       global = prev.global;
-       rotate = prev.rotate;
-       addtrackedframeoffset = prev.addtrackedframeoffset;
-       tracking_type = prev.tracking_type;
-       draw_vectors = prev.draw_vectors;
-       block_count = prev.block_count;
-       global_positions = prev.global_positions;
-       rotate_positions = prev.rotate_positions;
-       global_block_w = prev.global_block_w;
-       global_block_h = prev.global_block_h;
-//     rotation_block_w = prev.rotation_block_w;
-//     rotation_block_h = prev.rotation_block_h;
-       magnitude = prev.magnitude;
-       return_speed = prev.return_speed;
-       rotate_magnitude = prev.rotate_magnitude;
-       rotate_return_speed = prev.rotate_return_speed;
-       tracking_object = prev.tracking_object;
-       track_frame = prev.track_frame;
-       bottom_is_master = prev.bottom_is_master;
-       horizontal_only = prev.horizontal_only;
-       vertical_only = prev.vertical_only;
+       copy_from(prev);
 }
 
 
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
 MotionMain::MotionMain(PluginServer *server)
  : PluginVClient(server)
 {
@@ -246,12 +173,20 @@ MotionMain::MotionMain(PluginServer *server)
        global_target_src = 0;
        global_target_dst = 0;
 
+       cache_file[0] = 0;
+       cache_fp = active_fp = 0;
+       cache_line[0] = 0;
+       cache_key = active_key = -1;
+       dx_offset = dy_offset = 0;
+       load_ok = 0;
+       save_dx = load_dx = 0;
+       save_dy = load_dy = 0;
+       save_dt = load_dt = 0;
+       tracking_frame = -1;
        prev_rotate_ref = 0;
        current_rotate_ref = 0;
        rotate_target_src = 0;
        rotate_target_dst = 0;
-
-       config.set_cpus(get_project_smp() + 1);
 }
 
 MotionMain::~MotionMain()
@@ -264,12 +199,13 @@ MotionMain::~MotionMain()
        delete rotate_engine;
        delete motion_rotate;
 
-
        delete prev_global_ref;
        delete current_global_ref;
        delete global_target_src;
        delete global_target_dst;
 
+       reset_cache_file();
+
        delete prev_rotate_ref;
        delete current_rotate_ref;
        delete rotate_target_src;
@@ -289,57 +225,51 @@ LOAD_CONFIGURATION_MACRO(MotionMain, MotionConfig)
 
 void MotionMain::update_gui()
 {
-       if(thread)
-       {
-               if(load_configuration())
-               {
-                       thread->window->lock_window("MotionMain::update_gui");
-
-                       char string[BCTEXTLEN];
-                       sprintf(string, "%d", config.global_positions);
-                       ((MotionWindow*)thread->window)->global_search_positions->set_text(string);
-                       sprintf(string, "%d", config.rotate_positions);
-                       ((MotionWindow*)thread->window)->rotation_search_positions->set_text(string);
-
-                       ((MotionWindow*)thread->window)->global_block_w->update(config.global_block_w);
-                       ((MotionWindow*)thread->window)->global_block_h->update(config.global_block_h);
-//                     ((MotionWindow*)thread->window)->rotation_block_w->update(config.rotation_block_w);
-//                     ((MotionWindow*)thread->window)->rotation_block_h->update(config.rotation_block_h);
-                       ((MotionWindow*)thread->window)->block_x->update(config.block_x);
-                       ((MotionWindow*)thread->window)->block_y->update(config.block_y);
-                       ((MotionWindow*)thread->window)->block_x_text->update((float)config.block_x);
-                       ((MotionWindow*)thread->window)->block_y_text->update((float)config.block_y);
-                       ((MotionWindow*)thread->window)->magnitude->update(config.magnitude);
-                       ((MotionWindow*)thread->window)->return_speed->update(config.return_speed);
-                       ((MotionWindow*)thread->window)->rotate_magnitude->update(config.rotate_magnitude);
-                       ((MotionWindow*)thread->window)->rotate_return_speed->update(config.rotate_return_speed);
-                       ((MotionWindow*)thread->window)->rotation_range->update(config.rotation_range);
-                       ((MotionWindow*)thread->window)->rotation_center->update(config.rotation_center);
-
-
-                       ((MotionWindow*)thread->window)->track_single->update(config.tracking_object == MotionScan::TRACK_SINGLE);
-                       ((MotionWindow*)thread->window)->track_frame_number->update(config.track_frame);
-                       ((MotionWindow*)thread->window)->track_previous->update(config.tracking_object == MotionScan::TRACK_PREVIOUS);
-                       ((MotionWindow*)thread->window)->previous_same->update(config.tracking_object == MotionScan::PREVIOUS_SAME_BLOCK);
-                       if(config.tracking_object != MotionScan::TRACK_SINGLE)
-                               ((MotionWindow*)thread->window)->track_frame_number->disable();
-                       else
-                               ((MotionWindow*)thread->window)->track_frame_number->enable();
-
-                       ((MotionWindow*)thread->window)->action_type->set_text(
-                               ActionType::to_text(config.action_type));
-                       ((MotionWindow*)thread->window)->tracking_type->set_text(
-                               TrackingType::to_text(config.tracking_type));
-                       ((MotionWindow*)thread->window)->track_direction->set_text(
-                               TrackDirection::to_text(config.horizontal_only, config.vertical_only));
-                       ((MotionWindow*)thread->window)->master_layer->set_text(
-                               MasterLayer::to_text(config.bottom_is_master));
-
-
-                       ((MotionWindow*)thread->window)->update_mode();
-                       thread->window->unlock_window();
-               }
-       }
+       if( !thread ) return;
+       if( !load_configuration() ) return;
+       thread->window->lock_window("MotionMain::update_gui");
+       MotionWindow *window = (MotionWindow*)thread->window;
+
+       char string[BCTEXTLEN];
+       sprintf(string, "%d", config.global_positions);
+       window->global_search_positions->set_text(string);
+       sprintf(string, "%d", config.rotate_positions);
+       window->rotation_search_positions->set_text(string);
+
+       window->global_block_w->update(config.global_block_w);
+       window->global_block_h->update(config.global_block_h);
+       window->block_x->update(config.block_x);
+       window->block_y->update(config.block_y);
+       window->block_x_text->update((float)config.block_x);
+       window->block_y_text->update((float)config.block_y);
+       window->magnitude->update(config.magnitude);
+       window->return_speed->update(config.return_speed);
+       window->rotate_magnitude->update(config.rotate_magnitude);
+       window->rotate_return_speed->update(config.rotate_return_speed);
+       window->rotation_range->update(config.rotation_range);
+       window->rotation_center->update(config.rotation_center);
+
+
+       window->track_single->update(config.tracking_object == MotionScan::TRACK_SINGLE);
+       window->track_frame_number->update(config.track_frame);
+       window->track_previous->update(config.tracking_object == MotionScan::TRACK_PREVIOUS);
+       window->previous_same->update(config.tracking_object == MotionScan::PREVIOUS_SAME_BLOCK);
+       if( config.tracking_object != MotionScan::TRACK_SINGLE )
+               window->track_frame_number->disable();
+       else
+               window->track_frame_number->enable();
+
+       window->action_type->set_text(
+               ActionType::to_text(config.action_type));
+       window->tracking_type->set_text(
+               TrackingType::to_text(config.tracking_type));
+       window->track_direction->set_text(
+               TrackDirection::to_text(config.horizontal_only, config.vertical_only));
+       window->master_layer->set_text(
+               MasterLayer::to_text(config.bottom_is_master));
+
+       window->update_mode();
+       thread->window->unlock_window();
 }
 
 
@@ -358,8 +288,6 @@ void MotionMain::save_data(KeyFrame *keyframe)
        output.tag.set_property("ROTATE_POSITIONS", config.rotate_positions);
        output.tag.set_property("GLOBAL_BLOCK_W", config.global_block_w);
        output.tag.set_property("GLOBAL_BLOCK_H", config.global_block_h);
-//     output.tag.set_property("ROTATION_BLOCK_W", config.rotation_block_w);
-//     output.tag.set_property("ROTATION_BLOCK_H", config.rotation_block_h);
        output.tag.set_property("BLOCK_X", config.block_x);
        output.tag.set_property("BLOCK_Y", config.block_y);
        output.tag.set_property("GLOBAL_RANGE_W", config.global_range_w);
@@ -374,6 +302,7 @@ void MotionMain::save_data(KeyFrame *keyframe)
        output.tag.set_property("GLOBAL", config.global);
        output.tag.set_property("ROTATE", config.rotate);
        output.tag.set_property("ADDTRACKEDFRAMEOFFSET", config.addtrackedframeoffset);
+       output.tag.set_property("TRACKING_FILE", config.tracking_file);
        output.tag.set_property("TRACKING_TYPE", config.tracking_type);
        output.tag.set_property("DRAW_VECTORS", config.draw_vectors);
        output.tag.set_property("TRACKING_OBJECT", config.tracking_object);
@@ -390,153 +319,107 @@ void MotionMain::save_data(KeyFrame *keyframe)
 void MotionMain::read_data(KeyFrame *keyframe)
 {
        FileXML input;
-
        input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
-
        int result = 0;
 
-       while(!result)
-       {
-               result = input.read_tag();
-
-               if(!result)
-               {
-                       if(input.tag.title_is("MOTION"))
-                       {
-                               config.block_count = input.tag.get_property("BLOCK_COUNT", config.block_count);
-                               config.global_positions = input.tag.get_property("GLOBAL_POSITIONS", config.global_positions);
-                               config.rotate_positions = input.tag.get_property("ROTATE_POSITIONS", config.rotate_positions);
-                               config.global_block_w = input.tag.get_property("GLOBAL_BLOCK_W", config.global_block_w);
-                               config.global_block_h = input.tag.get_property("GLOBAL_BLOCK_H", config.global_block_h);
-//                             config.rotation_block_w = input.tag.get_property("ROTATION_BLOCK_W", config.rotation_block_w);
-//                             config.rotation_block_h = input.tag.get_property("ROTATION_BLOCK_H", config.rotation_block_h);
-                               config.block_x = input.tag.get_property("BLOCK_X", config.block_x);
-                               config.block_y = input.tag.get_property("BLOCK_Y", config.block_y);
-                               config.global_range_w = input.tag.get_property("GLOBAL_RANGE_W", config.global_range_w);
-                               config.global_range_h = input.tag.get_property("GLOBAL_RANGE_H", config.global_range_h);
-                               config.rotation_range = input.tag.get_property("ROTATION_RANGE", config.rotation_range);
-                               config.rotation_center = input.tag.get_property("ROTATION_CENTER", config.rotation_center);
-                               config.magnitude = input.tag.get_property("MAGNITUDE", config.magnitude);
-                               config.return_speed = input.tag.get_property("RETURN_SPEED", config.return_speed);
-                               config.rotate_magnitude = input.tag.get_property("ROTATE_MAGNITUDE", config.rotate_magnitude);
-                               config.rotate_return_speed = input.tag.get_property("ROTATE_RETURN_SPEED", config.rotate_return_speed);
-                               config.action_type = input.tag.get_property("ACTION_TYPE", config.action_type);
-                               config.global = input.tag.get_property("GLOBAL", config.global);
-                               config.rotate = input.tag.get_property("ROTATE", config.rotate);
-                               config.addtrackedframeoffset = input.tag.get_property("ADDTRACKEDFRAMEOFFSET", config.addtrackedframeoffset);
-                               config.tracking_type = input.tag.get_property("TRACKING_TYPE", config.tracking_type);
-                               config.draw_vectors = input.tag.get_property("DRAW_VECTORS", config.draw_vectors);
-                               config.tracking_object = input.tag.get_property("TRACKING_OBJECT", config.tracking_object);
-                               config.track_frame = input.tag.get_property("TRACK_FRAME", config.track_frame);
-                               config.bottom_is_master = input.tag.get_property("BOTTOM_IS_MASTER", config.bottom_is_master);
-                               config.horizontal_only = input.tag.get_property("HORIZONTAL_ONLY", config.horizontal_only);
-                               config.vertical_only = input.tag.get_property("VERTICAL_ONLY", config.vertical_only);
-                       }
+       while( !(result = input.read_tag()) ) {
+               if( input.tag.title_is("MOTION") ) {
+                       config.block_count = input.tag.get_property("BLOCK_COUNT", config.block_count);
+                       config.global_positions = input.tag.get_property("GLOBAL_POSITIONS", config.global_positions);
+                       config.rotate_positions = input.tag.get_property("ROTATE_POSITIONS", config.rotate_positions);
+                       config.global_block_w = input.tag.get_property("GLOBAL_BLOCK_W", config.global_block_w);
+                       config.global_block_h = input.tag.get_property("GLOBAL_BLOCK_H", config.global_block_h);
+                       config.block_x = input.tag.get_property("BLOCK_X", config.block_x);
+                       config.block_y = input.tag.get_property("BLOCK_Y", config.block_y);
+                       config.global_range_w = input.tag.get_property("GLOBAL_RANGE_W", config.global_range_w);
+                       config.global_range_h = input.tag.get_property("GLOBAL_RANGE_H", config.global_range_h);
+                       config.rotation_range = input.tag.get_property("ROTATION_RANGE", config.rotation_range);
+                       config.rotation_center = input.tag.get_property("ROTATION_CENTER", config.rotation_center);
+                       config.magnitude = input.tag.get_property("MAGNITUDE", config.magnitude);
+                       config.return_speed = input.tag.get_property("RETURN_SPEED", config.return_speed);
+                       config.rotate_magnitude = input.tag.get_property("ROTATE_MAGNITUDE", config.rotate_magnitude);
+                       config.rotate_return_speed = input.tag.get_property("ROTATE_RETURN_SPEED", config.rotate_return_speed);
+                       config.action_type = input.tag.get_property("ACTION_TYPE", config.action_type);
+                       config.global = input.tag.get_property("GLOBAL", config.global);
+                       config.rotate = input.tag.get_property("ROTATE", config.rotate);
+                       config.addtrackedframeoffset = input.tag.get_property("ADDTRACKEDFRAMEOFFSET", config.addtrackedframeoffset);
+                       input.tag.get_property("TRACKING_FILE", config.tracking_file);
+                       config.tracking_type = input.tag.get_property("TRACKING_TYPE", config.tracking_type);
+                       config.draw_vectors = input.tag.get_property("DRAW_VECTORS", config.draw_vectors);
+                       config.tracking_object = input.tag.get_property("TRACKING_OBJECT", config.tracking_object);
+                       config.track_frame = input.tag.get_property("TRACK_FRAME", config.track_frame);
+                       config.bottom_is_master = input.tag.get_property("BOTTOM_IS_MASTER", config.bottom_is_master);
+                       config.horizontal_only = input.tag.get_property("HORIZONTAL_ONLY", config.horizontal_only);
+                       config.vertical_only = input.tag.get_property("VERTICAL_ONLY", config.vertical_only);
                }
        }
        config.boundaries();
 }
 
-
-
-
-
-
-
-
-
 void MotionMain::allocate_temp(int w, int h, int color_model)
 {
-       if(temp_frame &&
-               (temp_frame->get_w() != w ||
-               temp_frame->get_h() != h))
-       {
+       if( temp_frame &&
+           ( temp_frame->get_w() != w || temp_frame->get_h() != h ) ) {
                delete temp_frame;
                temp_frame = 0;
        }
-       if(!temp_frame)
+       if( !temp_frame )
                temp_frame = new VFrame(w, h, color_model);
 }
 
-
 void MotionMain::process_global()
 {
 
-       if(!engine) engine = new MotionScan(PluginClient::get_project_smp() + 1,
+       if( !engine ) engine = new MotionScan(PluginClient::get_project_smp() + 1,
                PluginClient::get_project_smp() + 1);
 
 // Determine if frames changed
-       engine->scan_frame(current_global_ref,
-               prev_global_ref,
-               config.global_range_w,
-               config.global_range_h,
-               config.global_block_w,
-               config.global_block_h,
-               config.block_x,
-               config.block_y,
-               config.tracking_object,
-               config.tracking_type,
-               config.action_type,
-               config.horizontal_only,
-               config.vertical_only,
-               get_source_position(),
-               config.global_positions,
-               total_dx,
-               total_dy,
-               0,
-               0);
-       current_dx = engine->dx_result;
-       current_dy = engine->dy_result;
+       engine->scan_frame(current_global_ref, prev_global_ref,
+               config.global_range_w, config.global_range_h,
+               config.global_block_w, config.global_block_h,
+               config.block_x, config.block_y,
+               config.tracking_object, config.tracking_type,
+               config.action_type, config.horizontal_only,
+               config.vertical_only, get_source_position(),
+               config.global_positions, total_dx, total_dy,
+               0, 0, load_ok, load_dx, load_dy);
+       current_dx = (engine->dx_result += dx_offset);
+       current_dy = (engine->dy_result += dy_offset);
+
+// Write results
+       if( config.tracking_type == MotionScan::SAVE ) {
+               save_dx = engine->dx_result;
+               save_dy = engine->dy_result;
+       }
 
 // Add current motion vector to accumulation vector.
-       if(config.tracking_object != MotionScan::TRACK_SINGLE)
-       {
+       if( config.tracking_object != MotionScan::TRACK_SINGLE ) {
 // Retract over time
                total_dx = (int64_t)total_dx * (100 - config.return_speed) / 100;
                total_dy = (int64_t)total_dy * (100 - config.return_speed) / 100;
                total_dx += engine->dx_result;
                total_dy += engine->dy_result;
 // printf("MotionMain::process_global total_dx=%d engine->dx_result=%d\n",
-// total_dx,
-// engine->dx_result);
+// total_dx, engine->dx_result);
        }
-       else
+       else {
 // Make accumulation vector current
-       {
                total_dx = engine->dx_result;
                total_dy = engine->dy_result;
        }
 
 // Clamp accumulation vector
-       if(config.magnitude < 100)
-       {
-               //int block_w = (int64_t)config.global_block_w *
-               //              current_global_ref->get_w() / 100;
-               //int block_h = (int64_t)config.global_block_h *
-               //              current_global_ref->get_h() / 100;
-               int block_x_orig = (int64_t)(config.block_x *
-                       current_global_ref->get_w() /
-                       100);
-               int block_y_orig = (int64_t)(config.block_y *
-                       current_global_ref->get_h() /
-                       100);
-
-               int max_block_x = (int64_t)(current_global_ref->get_w() - block_x_orig) *
-                       OVERSAMPLE *
-                       config.magnitude /
-                       100;
-               int max_block_y = (int64_t)(current_global_ref->get_h() - block_y_orig) *
-                       OVERSAMPLE *
-                       config.magnitude /
-                       100;
-               int min_block_x = (int64_t)-block_x_orig *
-                       OVERSAMPLE *
-                       config.magnitude /
-                       100;
-               int min_block_y = (int64_t)-block_y_orig *
-                       OVERSAMPLE *
-                       config.magnitude /
-                       100;
+       if( config.magnitude < 100 ) {
+               int block_x_orig = (int64_t)(config.block_x * current_global_ref->get_w() / 100);
+               int block_y_orig = (int64_t)(config.block_y * current_global_ref->get_h() / 100);
+               int max_block_x = (int64_t)(current_global_ref->get_w() - block_x_orig)
+                       * OVERSAMPLE * config.magnitude / 100;
+               int max_block_y = (int64_t)(current_global_ref->get_h() - block_y_orig)
+                       * OVERSAMPLE * config.magnitude / 100;
+               int min_block_x = (int64_t)-block_x_orig
+                       * OVERSAMPLE * config.magnitude / 100;
+               int min_block_y = (int64_t)-block_y_orig
+                       * OVERSAMPLE * config.magnitude / 100;
 
                CLAMP(total_dx, min_block_x, max_block_x);
                CLAMP(total_dy, min_block_y, max_block_y);
@@ -544,12 +427,10 @@ void MotionMain::process_global()
 
 #ifdef DEBUG
 printf("MotionMain::process_global 2 total_dx=%.02f total_dy=%.02f\n",
-(float)total_dx / OVERSAMPLE,
-(float)total_dy / OVERSAMPLE);
+  (float)total_dx / OVERSAMPLE, (float)total_dy / OVERSAMPLE);
 #endif
 
-       if(config.tracking_object != MotionScan::TRACK_SINGLE && !config.rotate)
-       {
+       if( config.tracking_object != MotionScan::TRACK_SINGLE && !config.rotate ) {
 // Transfer current reference frame to previous reference frame and update
 // counter.  Must wait for rotate to compare.
                prev_global_ref->copy_from(current_global_ref);
@@ -558,55 +439,44 @@ printf("MotionMain::process_global 2 total_dx=%.02f total_dy=%.02f\n",
 
 // Decide what to do with target based on requested operation
        int interpolation = NEAREST_NEIGHBOR;
-       float dx = 0.;
-       float dy = 0.;
-       switch(config.action_type)
-       {
-               case MotionScan::NOTHING:
-                       global_target_dst->copy_from(global_target_src);
-                       break;
-               case MotionScan::TRACK_PIXEL:
-                       interpolation = NEAREST_NEIGHBOR;
-                       dx = (int)(total_dx / OVERSAMPLE);
-                       dy = (int)(total_dy / OVERSAMPLE);
-                       break;
-               case MotionScan::STABILIZE_PIXEL:
-                       interpolation = NEAREST_NEIGHBOR;
-                       dx = -(int)(total_dx / OVERSAMPLE);
-                       dy = -(int)(total_dy / OVERSAMPLE);
-                       break;
-                       break;
-               case MotionScan::TRACK:
-                       interpolation = CUBIC_LINEAR;
-                       dx = (float)total_dx / OVERSAMPLE;
-                       dy = (float)total_dy / OVERSAMPLE;
-                       break;
-               case MotionScan::STABILIZE:
-                       interpolation = CUBIC_LINEAR;
-                       dx = -(float)total_dx / OVERSAMPLE;
-                       dy = -(float)total_dy / OVERSAMPLE;
-                       break;
+       float dx = 0., dy = 0.;
+       switch(config.action_type) {
+       case MotionScan::NOTHING:
+               global_target_dst->copy_from(global_target_src);
+               break;
+       case MotionScan::TRACK_PIXEL:
+               interpolation = NEAREST_NEIGHBOR;
+               dx = (int)(total_dx / OVERSAMPLE);
+               dy = (int)(total_dy / OVERSAMPLE);
+               break;
+       case MotionScan::STABILIZE_PIXEL:
+               interpolation = NEAREST_NEIGHBOR;
+               dx = -(int)(total_dx / OVERSAMPLE);
+               dy = -(int)(total_dy / OVERSAMPLE);
+               break;
+       case MotionScan::TRACK:
+               interpolation = CUBIC_LINEAR;
+               dx = (float)total_dx / OVERSAMPLE;
+               dy = (float)total_dy / OVERSAMPLE;
+               break;
+       case MotionScan::STABILIZE:
+               interpolation = CUBIC_LINEAR;
+               dx = -(float)total_dx / OVERSAMPLE;
+               dy = -(float)total_dy / OVERSAMPLE;
+               break;
        }
 
 
-       if(config.action_type != MotionScan::NOTHING)
-       {
-               if(!overlayer)
+       if( config.action_type != MotionScan::NOTHING ) {
+               if( !overlayer )
                        overlayer = new OverlayFrame(PluginClient::get_project_smp() + 1);
                global_target_dst->clear_frame();
-               overlayer->overlay(global_target_dst,
-                       global_target_src,
-                       0,
-                       0,
-                       global_target_src->get_w(),
-                       global_target_src->get_h(),
-                       dx,
-                       dy,
+               overlayer->overlay(global_target_dst, global_target_src,
+                       0, 0, global_target_src->get_w(), global_target_src->get_h(),
+                       dx, dy,
                        (float)global_target_src->get_w() + dx,
                        (float)global_target_src->get_h() + dy,
-                       1,
-                       TRANSFER_REPLACE,
-                       interpolation);
+                       1, TRANSFER_REPLACE, interpolation);
        }
 }
 
@@ -614,112 +484,81 @@ printf("MotionMain::process_global 2 total_dx=%.02f total_dy=%.02f\n",
 
 void MotionMain::process_rotation()
 {
-       int block_x;
-       int block_y;
+       int block_x, block_y;
 
 // Convert the previous global reference into the previous rotation reference.
 // Convert global target destination into rotation target source.
-       if(config.global)
-       {
-               if(!overlayer)
+       if( config.global ) {
+               if( !overlayer )
                        overlayer = new OverlayFrame(PluginClient::get_project_smp() + 1);
-               float dx;
-               float dy;
-               if(config.tracking_object == MotionScan::TRACK_SINGLE)
-               {
+               float dx, dy;
+               if( config.tracking_object == MotionScan::TRACK_SINGLE ) {
                        dx = (float)total_dx / OVERSAMPLE;
                        dy = (float)total_dy / OVERSAMPLE;
                }
-               else
-               {
+               else {
                        dx = (float)current_dx / OVERSAMPLE;
                        dy = (float)current_dy / OVERSAMPLE;
                }
 
                prev_rotate_ref->clear_frame();
-               overlayer->overlay(prev_rotate_ref,
-                       prev_global_ref,
-                       0,
-                       0,
-                       prev_global_ref->get_w(),
-                       prev_global_ref->get_h(),
-                       dx,
-                       dy,
+               overlayer->overlay(prev_rotate_ref, prev_global_ref,
+                       0, 0, prev_global_ref->get_w(), prev_global_ref->get_h(),
+                       dx, dy,
                        (float)prev_global_ref->get_w() + dx,
                        (float)prev_global_ref->get_h() + dy,
-                       1,
-                       TRANSFER_REPLACE,
-                       CUBIC_LINEAR);
+                       1, TRANSFER_REPLACE, CUBIC_LINEAR);
 // Pivot is destination global position
                block_x = (int)(prev_rotate_ref->get_w() *
-                       config.block_x /
-                       100 +
-                       (float)total_dx /
-                       OVERSAMPLE);
+                       config.block_x / 100 + (float)total_dx / OVERSAMPLE);
                block_y = (int)(prev_rotate_ref->get_h() *
-                       config.block_y /
-                       100 +
-                       (float)total_dy /
-                       OVERSAMPLE);
+                       config.block_y / 100 + (float)total_dy / OVERSAMPLE);
 // Use the global target output as the rotation target input
                rotate_target_src->copy_from(global_target_dst);
 // Transfer current reference frame to previous reference frame for global.
-               if(config.tracking_object != MotionScan::TRACK_SINGLE)
-               {
+               if( config.tracking_object != MotionScan::TRACK_SINGLE ) {
                        prev_global_ref->copy_from(current_global_ref);
                        previous_frame_number = get_source_position();
                }
        }
-       else
-       {
+       else {
 // Pivot is fixed
-               block_x = (int)(prev_rotate_ref->get_w() *
-                       config.block_x /
-                       100);
-               block_y = (int)(prev_rotate_ref->get_h() *
-                       config.block_y /
-                       100);
+               block_x = (int)(prev_rotate_ref->get_w() * config.block_x / 100);
+               block_y = (int)(prev_rotate_ref->get_h() * config.block_y / 100);
        }
 
-
-
 // Get rotation
-       if(!motion_rotate)
+       if( !motion_rotate )
                motion_rotate = new RotateScan(this,
-                       get_project_smp() + 1,
-                       get_project_smp() + 1);
-
-       current_angle = motion_rotate->scan_frame(prev_rotate_ref,
-               current_rotate_ref,
-               block_x,
-               block_y);
+                       get_project_smp() + 1, get_project_smp() + 1);
 
+       current_angle = motion_rotate->
+               scan_frame(prev_rotate_ref, current_rotate_ref, block_x, block_y);
 
+// Write results
+       if( config.tracking_type == MotionScan::SAVE ) {
+               save_dt = current_angle;
+       }
 
 // Add current rotation to accumulation
-       if(config.tracking_object != MotionScan::TRACK_SINGLE)
-       {
+       if( config.tracking_object != MotionScan::TRACK_SINGLE ) {
 // Retract over time
                total_angle = total_angle * (100 - config.rotate_return_speed) / 100;
 // Accumulate current rotation
                total_angle += current_angle;
 
 // Clamp rotation accumulation
-               if(config.rotate_magnitude < 90)
-               {
+               if( config.rotate_magnitude < 90 ) {
                        CLAMP(total_angle, -config.rotate_magnitude, config.rotate_magnitude);
                }
 
-               if(!config.global)
-               {
-// Transfer current reference frame to previous reference frame and update
-// counter.
+               if( !config.global ) {
+// Transfer current reference frame to previous reference frame and update counter.
                        prev_rotate_ref->copy_from(current_rotate_ref);
                        previous_frame_number = get_source_position();
                }
        }
-       else
-       {
+       else {
                total_angle = current_angle;
        }
 
@@ -730,288 +569,202 @@ printf("MotionMain::process_rotation total_angle=%f\n", total_angle);
 
 // Calculate rotation parameters based on requested operation
        float angle = 0.;
-       switch(config.action_type)
-       {
-               case MotionScan::NOTHING:
-                       rotate_target_dst->copy_from(rotate_target_src);
-                       break;
-               case MotionScan::TRACK:
-               case MotionScan::TRACK_PIXEL:
-                       angle = total_angle;
-                       break;
-               case MotionScan::STABILIZE:
-               case MotionScan::STABILIZE_PIXEL:
-                       angle = -total_angle;
-                       break;
+       switch(config.action_type) {
+       case MotionScan::NOTHING:
+               rotate_target_dst->copy_from(rotate_target_src);
+               break;
+       case MotionScan::TRACK:
+       case MotionScan::TRACK_PIXEL:
+               angle = total_angle;
+               break;
+       case MotionScan::STABILIZE:
+       case MotionScan::STABILIZE_PIXEL:
+               angle = -total_angle;
+               break;
        }
 
-
-
-       if(config.action_type != MotionScan::NOTHING)
-       {
-               if(!rotate_engine)
-                       rotate_engine = new AffineEngine(PluginClient::get_project_smp() + 1,
+       if( config.action_type != MotionScan::NOTHING ) {
+               if( !rotate_engine )
+                       rotate_engine = new AffineEngine(
+                               PluginClient::get_project_smp() + 1,
                                PluginClient::get_project_smp() + 1);
 
                rotate_target_dst->clear_frame();
 
 // Determine pivot based on a number of factors.
-               switch(config.action_type)
-               {
-                       case MotionScan::TRACK:
-                       case MotionScan::TRACK_PIXEL:
+               switch(config.action_type) {
+               case MotionScan::TRACK:
+               case MotionScan::TRACK_PIXEL:
 // Use destination of global tracking.
-//                             rotate_engine->set_pivot(block_x, block_y);
-                               rotate_engine->set_in_pivot(block_x, block_y);
-                               rotate_engine->set_out_pivot(block_x, block_y);
-                               break;
+                       rotate_engine->set_in_pivot(block_x, block_y);
+                       rotate_engine->set_out_pivot(block_x, block_y);
+                       break;
 
-                       case MotionScan::STABILIZE:
-                       case MotionScan::STABILIZE_PIXEL:
-                               if(config.global)
-                               {
+               case MotionScan::STABILIZE:
+               case MotionScan::STABILIZE_PIXEL:
+                       if( config.global ) {
 // Use origin of global stabilize operation
-//                                     rotate_engine->set_pivot((int)(rotate_target_dst->get_w() *
-//                                                     config.block_x /
-//                                                     100),
-//                                             (int)(rotate_target_dst->get_h() *
-//                                                     config.block_y /
-//                                                     100));
-                                       rotate_engine->set_in_pivot((int)(rotate_target_dst->get_w() *
-                                                       config.block_x /
-                                                       100),
-                                               (int)(rotate_target_dst->get_h() *
-                                                       config.block_y /
-                                                       100));
-                                       rotate_engine->set_out_pivot((int)(rotate_target_dst->get_w() *
-                                                       config.block_x /
-                                                       100),
-                                               (int)(rotate_target_dst->get_h() *
-                                                       config.block_y /
-                                                       100));
-
+                               rotate_engine->set_in_pivot(
+                                       (int)(rotate_target_dst->get_w() * config.block_x / 100),
+                                       (int)(rotate_target_dst->get_h() * config.block_y / 100));
+                               rotate_engine->set_out_pivot(
+                                       (int)(rotate_target_dst->get_w() * config.block_x / 100),
+                                       (int)(rotate_target_dst->get_h() * config.block_y / 100));
                                }
-                               else
-                               {
+                               else {
 // Use origin
-//                                     rotate_engine->set_pivot(block_x, block_y);
                                        rotate_engine->set_in_pivot(block_x, block_y);
                                        rotate_engine->set_out_pivot(block_x, block_y);
                                }
                                break;
                }
 
-
                rotate_engine->rotate(rotate_target_dst, rotate_target_src, angle);
-// overlayer->overlay(rotate_target_dst,
-//     prev_rotate_ref,
-//     0,
-//     0,
-//     prev_rotate_ref->get_w(),
-//     prev_rotate_ref->get_h(),
-//     0,
-//     0,
-//     prev_rotate_ref->get_w(),
-//     prev_rotate_ref->get_h(),
-//     1,
-//     TRANSFER_NORMAL,
-//     CUBIC_LINEAR);
-// overlayer->overlay(rotate_target_dst,
-//     current_rotate_ref,
-//     0,
-//     0,
-//     prev_rotate_ref->get_w(),
-//     prev_rotate_ref->get_h(),
-//     0,
-//     0,
-//     prev_rotate_ref->get_w(),
-//     prev_rotate_ref->get_h(),
-//     1,
-//     TRANSFER_NORMAL,
-//     CUBIC_LINEAR);
-
-
+// overlayer->overlay(rotate_target_dst, prev_rotate_ref,
+//     0, 0, prev_rotate_ref->get_w(), prev_rotate_ref->get_h(),
+//     0, 0, prev_rotate_ref->get_w(), prev_rotate_ref->get_h(),
+//     1, TRANSFER_NORMAL, CUBIC_LINEAR);
+// overlayer->overlay(rotate_target_dst, current_rotate_ref,
+//     0, 0, prev_rotate_ref->get_w(), prev_rotate_ref->get_h(),
+//     0, 0, prev_rotate_ref->get_w(), prev_rotate_ref->get_h(),
+//     1, TRANSFER_NORMAL, //  CUBIC_LINEAR);
        }
-
-
 }
 
 
-
-
-
-
-
-
-
-int MotionMain::process_buffer(VFrame **frame,
-       int64_t start_position,
-       double frame_rate)
+int MotionMain::process_buffer(VFrame **frame, int64_t start_position, double frame_rate)
 {
+       int prev_config_tracking_type = config.tracking_type;
        int need_reconfigure = load_configuration();
        int color_model = frame[0]->get_color_model();
        w = frame[0]->get_w();
        h = frame[0]->get_h();
 
-
 #ifdef DEBUG
-printf("MotionMain::process_buffer %d start_position=%lld\n", __LINE__, start_position);
+printf("MotionMain::process_buffer %d start_position=%jd\n", __LINE__, start_position);
 #endif
 
-
 // Calculate the source and destination pointers for each of the operations.
 // Get the layer to track motion in.
-       reference_layer = config.bottom_is_master ?
-               PluginClient::total_in_buffers - 1 :
-               0;
 // Get the layer to apply motion in.
+       reference_layer = config.bottom_is_master ?
+               PluginClient::total_in_buffers - 1 : 0;
        target_layer = config.bottom_is_master ?
-               0 :
-               PluginClient::total_in_buffers - 1;
-
+               0 : PluginClient::total_in_buffers - 1;
 
        output_frame = frame[target_layer];
-
-
 // Get the position of previous reference frame.
        int64_t actual_previous_number;
 // Skip if match frame not available
        int skip_current = 0;
 
-
-       if(config.tracking_object == MotionScan::TRACK_SINGLE)
-       {
+       if( config.tracking_object == MotionScan::TRACK_SINGLE ) {
                actual_previous_number = config.track_frame;
-               if(get_direction() == PLAY_REVERSE)
+               if( get_direction() == PLAY_REVERSE )
                        actual_previous_number++;
-               if(actual_previous_number == start_position)
+               if( actual_previous_number == start_position )
                        skip_current = 1;
        }
-       else
-       {
+       else {
                actual_previous_number = start_position;
-               if(get_direction() == PLAY_FORWARD)
-               {
+               if( get_direction() == PLAY_FORWARD ) {
                        actual_previous_number--;
-                       if(actual_previous_number < get_source_start())
+                       if( actual_previous_number < get_source_start() )
                                skip_current = 1;
-                       else
-                       {
+                       else {
                                KeyFrame *keyframe = get_prev_keyframe(start_position, 1);
-                               if(keyframe->position > 0 &&
-                                       actual_previous_number < keyframe->position)
+                               if( keyframe->position > 0 &&
+                                   actual_previous_number < keyframe->position )
                                        skip_current = 1;
                        }
                }
-               else
-               {
+               else {
                        actual_previous_number++;
-                       if(actual_previous_number >= get_source_start() + get_total_len())
+                       if( actual_previous_number >= get_source_start() + get_total_len() )
                                skip_current = 1;
-                       else
-                       {
+                       else {
                                KeyFrame *keyframe = get_next_keyframe(start_position, 1);
-                               if(keyframe->position > 0 &&
-                                       actual_previous_number >= keyframe->position)
+                               if( keyframe->position > 0 &&
+                                   actual_previous_number >= keyframe->position )
                                        skip_current = 1;
                        }
                }
-
 // Only count motion since last keyframe
-
-
        }
 
+       if( !config.global && !config.rotate )
+               skip_current = 1;
 
-       if(!config.global && !config.rotate) skip_current = 1;
-
-
-
+//printf("process_realtime: %jd %d %jd %jd\n", start_position,
+// skip_current, previous_frame_number, actual_previous_number);
+       if( prev_config_tracking_type != MotionScan::SAVE &&
+           config.tracking_type == MotionScan::SAVE ) {
+               reset_cache_file();
+               char save_file[BCTEXTLEN];
+               sprintf(save_file,"%s.bak", config.tracking_file);
+#ifdef DEBUG
+printf("MotionMain::process_buffer 2 rename tracking file: %s to %s\n",
+ config.tracking_file, save_file);
+#endif
+               ::rename(config.tracking_file, save_file);
+       }
+       else if( !cache_file[0] || active_key > start_position )
+               reset_cache_file();
 
-// printf("process_realtime %d %lld %lld\n",
-// skip_current,
-// previous_frame_number,
-// actual_previous_number);
 // Load match frame and reset vectors
        int need_reload = !skip_current &&
                (previous_frame_number != actual_previous_number ||
                need_reconfigure);
-       if(need_reload)
-       {
-               total_dx = 0;
-               total_dy = 0;
-               total_angle = 0;
+       if( need_reload ) {
+               total_dx = total_dy = 0; total_angle = 0;
                previous_frame_number = actual_previous_number;
        }
 
-
-       if(skip_current)
-       {
-               total_dx = 0;
-               total_dy = 0;
-               current_dx = 0;
-               current_dy = 0;
-               total_angle = 0;
-               current_angle = 0;
+       if( skip_current ) {
+               total_dx = total_dy = 0;
+               current_dx = current_dy = 0;
+               total_angle = current_angle = 0;
        }
 
-
-
-
 // Get the global pointers.  Here we walk through the sequence of events.
-       if(config.global)
-       {
+       if( config.global ) {
 // Assume global only.  Global reads previous frame and compares
 // with current frame to get the current translation.
 // The center of the search area is fixed in compensate mode or
 // the user value + the accumulation vector in track mode.
-               if(!prev_global_ref)
+               if( !prev_global_ref )
                        prev_global_ref = new VFrame(w, h, color_model);
-               if(!current_global_ref)
+               if( !current_global_ref )
                        current_global_ref = new VFrame(w, h, color_model);
 
 // Global loads the current target frame into the src and
 // writes it to the dst frame with desired translation.
-               if(!global_target_src)
+               if( !global_target_src )
                        global_target_src = new VFrame(w, h, color_model);
-               if(!global_target_dst)
+               if( !global_target_dst )
                        global_target_dst = new VFrame(w, h, color_model);
 
-
 // Load the global frames
-               if(need_reload)
-               {
-                       read_frame(prev_global_ref,
-                               reference_layer,
-                               previous_frame_number,
-                               frame_rate,
-                               0);
+               if( need_reload ) {
+                       read_frame(prev_global_ref, reference_layer,
+                               previous_frame_number, frame_rate, 0);
                }
 
-               read_frame(current_global_ref,
-                       reference_layer,
-                       start_position,
-                       frame_rate,
-                       0);
-               read_frame(global_target_src,
-                       target_layer,
-                       start_position,
-                       frame_rate,
-                       0);
-
-
+               read_frame(current_global_ref, reference_layer,
+                       start_position, frame_rate, 0);
+               read_frame(global_target_src, target_layer,
+                       start_position, frame_rate, 0);
 
 // Global followed by rotate
-               if(config.rotate)
-               {
+               if( config.rotate ) {
 // Must translate the previous global reference by the current global
 // accumulation vector to match the current global reference.
 // The center of the search area is always the user value + the accumulation
 // vector.
-                       if(!prev_rotate_ref)
+                       if( !prev_rotate_ref )
                                prev_rotate_ref = new VFrame(w, h, color_model);
 // The current global reference is the current rotation reference.
-                       if(!current_rotate_ref)
+                       if( !current_rotate_ref )
                                current_rotate_ref = new VFrame(w, h, color_model);
                        current_rotate_ref->copy_from(current_global_ref);
 
@@ -1020,100 +773,109 @@ printf("MotionMain::process_buffer %d start_position=%lld\n", __LINE__, start_po
 // The pivot for the rotation is the center of the search area
 // if we're tracking.
 // The pivot is fixed to the user position if we're compensating.
-                       if(!rotate_target_src)
+                       if( !rotate_target_src )
                                rotate_target_src = new VFrame(w, h, color_model);
-                       if(!rotate_target_dst)
+                       if( !rotate_target_dst )
                                rotate_target_dst = new VFrame(w, h, color_model);
                }
        }
-       else
 // Rotation only
-       if(config.rotate)
-       {
+       else if( config.rotate ) {
 // Rotation reads the previous reference frame and compares it with current
 // reference frame.
-               if(!prev_rotate_ref)
+               if( !prev_rotate_ref )
                        prev_rotate_ref = new VFrame(w, h, color_model);
-               if(!current_rotate_ref)
+               if( !current_rotate_ref )
                        current_rotate_ref = new VFrame(w, h, color_model);
 
 // Rotation loads target frame to temporary, rotates it, and writes it to the
 // target frame.  The pivot is always fixed.
-               if(!rotate_target_src)
+               if( !rotate_target_src )
                        rotate_target_src = new VFrame(w, h, color_model);
-               if(!rotate_target_dst)
+               if( !rotate_target_dst )
                        rotate_target_dst = new VFrame(w, h, color_model);
 
 
 // Load the rotate frames
-               if(need_reload)
-               {
-                       read_frame(prev_rotate_ref,
-                               reference_layer,
-                               previous_frame_number,
-                               frame_rate,
-                               0);
+               if( need_reload ) {
+                       read_frame(prev_rotate_ref, reference_layer,
+                               previous_frame_number, frame_rate, 0);
                }
-               read_frame(current_rotate_ref,
-                       reference_layer,
-                       start_position,
-                       frame_rate,
-                       0);
-               read_frame(rotate_target_src,
-                       target_layer,
-                       start_position,
-                       frame_rate,
-                       0);
+               read_frame(current_rotate_ref, reference_layer,
+                       start_position, frame_rate, 0);
+               read_frame(rotate_target_src, target_layer,
+                       start_position, frame_rate, 0);
        }
 
+       dx_offset = 0; dy_offset = 0;
+       if( config.tracking_type == MotionScan::LOAD ) {
+               if( config.addtrackedframeoffset ) {
+                       if( config.track_frame != tracking_frame ) {
+                               tracking_frame = config.track_frame;
+                               int64_t no;  int dx, dy;  float dt;
+                               if( !get_cache_line(tracking_frame) &&
+                                   sscanf(cache_line, "%jd %d %d %f", &no, &dx, &dy, &dt) == 4 ) {
+                                       dx_offset = dx; dy_offset = dy;
+                               }
+                               else {
+                                       eprintf("no offset data frame %jd\n", tracking_frame);
+                               }
+                       }
+               }
+               else
+                       tracking_frame = -1;
+       }
 
+       if( !skip_current ) {
+               load_ok = 0;
+               if( config.tracking_type == MotionScan::LOAD ||
+                   config.tracking_type == MotionScan::SAVE ) {
+                       int64_t no;  int dx, dy;  float dt;
+                       int64_t frame_no = get_source_position();
+// Load result from disk
+                       if( !get_cache_line(frame_no) &&
+                           sscanf(cache_line, "%jd %d %d %f", &no, &dx, &dy, &dt) == 4 ) {
+                               load_ok = 1;  load_dx = dx;  load_dy = dy;  load_dt = dt;
+                       }
+                       else {
+#ifdef DEBUG
+printf("MotionMain::process_buffer: no tracking data frame %jd\n", frame_no);
+#endif
+                       }
+               }
 
-
-
-
-
-
-
-
-       if(!skip_current)
-       {
 // Get position change from previous frame to current frame
-               if(config.global) process_global();
+               if( config.global )
+                       process_global();
 // Get rotation change from previous frame to current frame
-               if(config.rotate) process_rotation();
+               if( config.rotate )
+                       process_rotation();
 //frame[target_layer]->copy_from(prev_rotate_ref);
 //frame[target_layer]->copy_from(current_rotate_ref);
-       }
-
-
-
-
-
 
+// write results to disk
+               if( config.tracking_type == MotionScan::SAVE ) {
+                       char line[BCSTRLEN];
+                       int64_t frame_no = get_source_position();
+                       snprintf(line, sizeof(line), "%jd %d %d %f\n",
+                               frame_no, save_dx, save_dy, save_dt);
+                       put_cache_line(line);
+               }
 // Transfer the relevant target frame to the output
-       if(!skip_current)
-       {
-               if(config.rotate)
-               {
+               if( config.rotate ) {
                        frame[target_layer]->copy_from(rotate_target_dst);
                }
-               else
-               {
+               else {
                        frame[target_layer]->copy_from(global_target_dst);
                }
        }
-       else
 // Read the target destination directly
-       {
+       else {
                read_frame(frame[target_layer],
-                       target_layer,
-                       start_position,
-                       frame_rate,
-                       0);
+                       target_layer, start_position, frame_rate, 0);
        }
 
-       if(config.draw_vectors)
-       {
+       if( config.draw_vectors ) {
                draw_vectors(frame[target_layer]);
        }
 
@@ -1127,74 +889,43 @@ printf("MotionMain::process_buffer %d\n", __LINE__);
 
 void MotionMain::draw_vectors(VFrame *frame)
 {
-       int w = frame->get_w();
-       int h = frame->get_h();
-       int global_x1, global_y1;
-       int global_x2, global_y2;
-       int block_x, block_y;
-       int block_w, block_h;
-       int block_x1, block_y1;
-       int block_x2, block_y2;
-       int block_x3, block_y3;
-       int block_x4, block_y4;
+       int w = frame->get_w(), h = frame->get_h();
+       int global_x1, global_y1, global_x2, global_y2;
+       int block_x, block_y, block_w, block_h;
+       int block_x1, block_y1, block_x2, block_y2;
+       int block_x3, block_y3, block_x4, block_y4;
+       int search_x1, search_y1, search_x2, search_y2;
        int search_w, search_h;
-       int search_x1, search_y1;
-       int search_x2, search_y2;
 
 
-       if(config.global)
-       {
+       if( config.global ) {
 // Get vector
 // Start of vector is center of previous block.
 // End of vector is total accumulation.
-               if(config.tracking_object == MotionScan::TRACK_SINGLE)
-               {
-                       global_x1 = (int64_t)(config.block_x *
-                               w /
-                               100);
-                       global_y1 = (int64_t)(config.block_y *
-                               h /
-                               100);
+               if( config.tracking_object == MotionScan::TRACK_SINGLE ) {
+                       global_x1 = (int64_t)(config.block_x * w / 100);
+                       global_y1 = (int64_t)(config.block_y * h / 100);
                        global_x2 = global_x1 + total_dx / OVERSAMPLE;
                        global_y2 = global_y1 + total_dy / OVERSAMPLE;
 //printf("MotionMain::draw_vectors %d %d %d %d %d %d\n", total_dx, total_dy, global_x1, global_y1, global_x2, global_y2);
                }
-               else
 // Start of vector is center of previous block.
 // End of vector is current change.
-               if(config.tracking_object == MotionScan::PREVIOUS_SAME_BLOCK)
-               {
-                       global_x1 = (int64_t)(config.block_x *
-                               w /
-                               100);
-                       global_y1 = (int64_t)(config.block_y *
-                               h /
-                               100);
+               else if( config.tracking_object == MotionScan::PREVIOUS_SAME_BLOCK ) {
+                       global_x1 = (int64_t)(config.block_x * w / 100);
+                       global_y1 = (int64_t)(config.block_y * h / 100);
                        global_x2 = global_x1 + current_dx / OVERSAMPLE;
                        global_y2 = global_y1 + current_dy / OVERSAMPLE;
                }
-               else
-               {
-                       global_x1 = (int64_t)(config.block_x *
-                               w /
-                               100 +
-                               (total_dx - current_dx) /
-                               OVERSAMPLE);
-                       global_y1 = (int64_t)(config.block_y *
-                               h /
-                               100 +
-                               (total_dy - current_dy) /
-                               OVERSAMPLE);
-                       global_x2 = (int64_t)(config.block_x *
-                               w /
-                               100 +
-                               total_dx /
-                               OVERSAMPLE);
-                       global_y2 = (int64_t)(config.block_y *
-                               h /
-                               100 +
-                               total_dy /
-                               OVERSAMPLE);
+               else {
+                       global_x1 = (int64_t)(config.block_x * w / 100
+                               + (total_dx - current_dx) / OVERSAMPLE);
+                       global_y1 = (int64_t)(config.block_y * h / 100
+                               + (total_dy - current_dy) / OVERSAMPLE);
+                       global_x2 = (int64_t)(config.block_x * w / 100
+                               + total_dx / OVERSAMPLE);
+                       global_y2 = (int64_t)(config.block_y * h / 100
+                               + total_dy / OVERSAMPLE);
                }
 
                block_x = global_x1;
@@ -1212,31 +943,13 @@ void MotionMain::draw_vectors(VFrame *frame)
                search_x2 = block_x2 + search_w / 2;
                search_y2 = block_y2 + search_h / 2;
 
-// printf("MotionMain::draw_vectors %d %d %d %d %d %d %d %d %d %d %d %d\n",
-// global_x1,
-// global_y1,
-// block_w,
-// block_h,
-// block_x1,
-// block_y1,
-// block_x2,
-// block_y2,
-// search_x1,
-// search_y1,
-// search_x2,
-// search_y2);
-
-               MotionScan::clamp_scan(w,
-                       h,
-                       &block_x1,
-                       &block_y1,
-                       &block_x2,
-                       &block_y2,
-                       &search_x1,
-                       &search_y1,
-                       &search_x2,
-                       &search_y2,
-                       1);
+//printf("MotionMain::draw_vectors %d %d %d %d %d %d %d %d %d %d %d %d\n",
+// global_x1, global_y1, block_w, block_h, block_x1, block_y1,
+// block_x2, block_y2, search_x1, search_y1, search_x2, search_y2);
+
+               MotionScan::clamp_scan(w, h,
+                       &block_x1, &block_y1, &block_x2, &block_y2,
+                       &search_x1, &search_y1, &search_x2, &search_y2, 1);
 
 // Vector
                draw_arrow(frame, global_x1, global_y1, global_x2, global_y2);
@@ -1247,7 +960,6 @@ void MotionMain::draw_vectors(VFrame *frame)
                draw_line(frame, block_x2, block_y2, block_x1, block_y2);
                draw_line(frame, block_x1, block_y2, block_x1, block_y1);
 
-
 // Search area
                draw_line(frame, search_x1, search_y1, search_x2, search_y1);
                draw_line(frame, search_x2, search_y1, search_x2, search_y2);
@@ -1255,22 +967,19 @@ void MotionMain::draw_vectors(VFrame *frame)
                draw_line(frame, search_x1, search_y2, search_x1, search_y1);
 
 // Block should be endpoint of motion
-               if(config.rotate)
-               {
+               if( config.rotate ) {
                        block_x = global_x2;
                        block_y = global_y2;
                }
        }
-       else
-       {
+       else {
                block_x = (int64_t)(config.block_x * w / 100);
                block_y = (int64_t)(config.block_y * h / 100);
        }
 
        block_w = config.global_block_w * w / 100;
        block_h = config.global_block_h * h / 100;
-       if(config.rotate)
-       {
+       if( config.rotate ) {
                float angle = total_angle * 2 * M_PI / 360;
                double base_angle1 = atan((float)block_h / block_w);
                double base_angle2 = atan((float)block_w / block_h);
@@ -1293,8 +1002,7 @@ void MotionMain::draw_vectors(VFrame *frame)
 
 
 // Center
-               if(!config.global)
-               {
+               if( !config.global ) {
                        draw_line(frame, block_x, block_y - 5, block_x, block_y + 6);
                        draw_line(frame, block_x - 5, block_y, block_x + 6, block_y);
                }
@@ -1302,62 +1010,37 @@ void MotionMain::draw_vectors(VFrame *frame)
 }
 
 
-
 void MotionMain::draw_pixel(VFrame *frame, int x, int y)
 {
-       if(!(x >= 0 && y >= 0 && x < frame->get_w() && y < frame->get_h())) return;
+       if( !(x >= 0 && y >= 0 && x < frame->get_w() && y < frame->get_h()) ) return;
 
-#define DRAW_PIXEL(x, y, components, do_yuv, max, type) \
-{ \
+#define DRAW_PIXEL(model, x, y, components, do_yuv, max, type) \
+ case model: { \
        type **rows = (type**)frame->get_rows(); \
        rows[y][x * components] = max - rows[y][x * components]; \
-       if(!do_yuv) \
-       { \
+       if( !do_yuv ) { \
                rows[y][x * components + 1] = max - rows[y][x * components + 1]; \
                rows[y][x * components + 2] = max - rows[y][x * components + 2]; \
        } \
-       else \
-       { \
+       else { \
                rows[y][x * components + 1] = (max / 2 + 1) - rows[y][x * components + 1]; \
                rows[y][x * components + 2] = (max / 2 + 1) - rows[y][x * components + 2]; \
        } \
-       if(components == 4) \
+       if( components == 4 ) \
                rows[y][x * components + 3] = max; \
-}
-
-
-       switch(frame->get_color_model())
-       {
-               case BC_RGB888:
-                       DRAW_PIXEL(x, y, 3, 0, 0xff, unsigned char);
-                       break;
-               case BC_RGBA8888:
-                       DRAW_PIXEL(x, y, 4, 0, 0xff, unsigned char);
-                       break;
-               case BC_RGB_FLOAT:
-                       DRAW_PIXEL(x, y, 3, 0, 1.0, float);
-                       break;
-               case BC_RGBA_FLOAT:
-                       DRAW_PIXEL(x, y, 4, 0, 1.0, float);
-                       break;
-               case BC_YUV888:
-                       DRAW_PIXEL(x, y, 3, 1, 0xff, unsigned char);
-                       break;
-               case BC_YUVA8888:
-                       DRAW_PIXEL(x, y, 4, 1, 0xff, unsigned char);
-                       break;
-               case BC_RGB161616:
-                       DRAW_PIXEL(x, y, 3, 0, 0xffff, uint16_t);
-                       break;
-               case BC_YUV161616:
-                       DRAW_PIXEL(x, y, 3, 1, 0xffff, uint16_t);
-                       break;
-               case BC_RGBA16161616:
-                       DRAW_PIXEL(x, y, 4, 0, 0xffff, uint16_t);
-                       break;
-               case BC_YUVA16161616:
-                       DRAW_PIXEL(x, y, 4, 1, 0xffff, uint16_t);
-                       break;
+} break
+
+       switch(frame->get_color_model()) {
+       DRAW_PIXEL(BC_RGB888, x, y, 3, 0, 0xff, unsigned char);
+       DRAW_PIXEL(BC_RGBA8888, x, y, 4, 0, 0xff, unsigned char);
+       DRAW_PIXEL(BC_RGB_FLOAT, x, y, 3, 0, 1.0, float);
+       DRAW_PIXEL(BC_RGBA_FLOAT, x, y, 4, 0, 1.0, float);
+       DRAW_PIXEL(BC_YUV888, x, y, 3, 1, 0xff, unsigned char);
+       DRAW_PIXEL(BC_YUVA8888, x, y, 4, 1, 0xff, unsigned char);
+       DRAW_PIXEL(BC_RGB161616, x, y, 3, 0, 0xffff, uint16_t);
+       DRAW_PIXEL(BC_YUV161616, x, y, 3, 1, 0xffff, uint16_t);
+       DRAW_PIXEL(BC_RGBA16161616, x, y, 4, 0, 0xffff, uint16_t);
+       DRAW_PIXEL(BC_YUVA16161616, x, y, 4, 1, 0xffff, uint16_t);
        }
 }
 
@@ -1368,47 +1051,31 @@ void MotionMain::draw_line(VFrame *frame, int x1, int y1, int x2, int y2)
        int h = labs(y2 - y1);
 //printf("MotionMain::draw_line 1 %d %d %d %d\n", x1, y1, x2, y2);
 
-       if(!w && !h)
-       {
+       if( !w && !h ) {
                draw_pixel(frame, x1, y1);
        }
-       else
-       if(w > h)
-       {
+       else if( w > h ) {
 // Flip coordinates so x1 < x2
-               if(x2 < x1)
-               {
-                       y2 ^= y1;
-                       y1 ^= y2;
-                       y2 ^= y1;
-                       x1 ^= x2;
-                       x2 ^= x1;
-                       x1 ^= x2;
+               if( x2 < x1 ) {
+                       y2 ^= y1; y1 ^= y2; y2 ^= y1;
+                       x1 ^= x2; x2 ^= x1; x1 ^= x2;
                }
                int numerator = y2 - y1;
                int denominator = x2 - x1;
-               for(int i = x1; i < x2; i++)
-               {
+               for( int i = x1; i < x2; i++ ) {
                        int y = y1 + (int64_t)(i - x1) * (int64_t)numerator / (int64_t)denominator;
                        draw_pixel(frame, i, y);
                }
        }
-       else
-       {
+       else {
 // Flip coordinates so y1 < y2
-               if(y2 < y1)
-               {
-                       y2 ^= y1;
-                       y1 ^= y2;
-                       y2 ^= y1;
-                       x1 ^= x2;
-                       x2 ^= x1;
-                       x1 ^= x2;
+               if( y2 < y1 ) {
+                       y2 ^= y1; y1 ^= y2; y2 ^= y1;
+                       x1 ^= x2; x2 ^= x1; x1 ^= x2;
                }
                int numerator = x2 - x1;
                int denominator = y2 - y1;
-               for(int i = y1; i < y2; i++)
-               {
+               for( int i = y1; i < y2; i++ ) {
                        int x = x1 + (int64_t)(i - y1) * (int64_t)numerator / (int64_t)denominator;
                        draw_pixel(frame, x, i);
                }
@@ -1422,19 +1089,14 @@ void MotionMain::draw_arrow(VFrame *frame, int x1, int y1, int x2, int y2)
        double angle = atan((float)(y2 - y1) / (float)(x2 - x1));
        double angle1 = angle + (float)145 / 360 * 2 * 3.14159265;
        double angle2 = angle - (float)145 / 360 * 2 * 3.14159265;
-       int x3;
-       int y3;
-       int x4;
-       int y4;
-       if(x2 < x1)
-       {
+       int x3, y3, x4, y4;
+       if( x2 < x1 ) {
                x3 = x2 - (int)(ARROW_SIZE * cos(angle1));
                y3 = y2 - (int)(ARROW_SIZE * sin(angle1));
                x4 = x2 - (int)(ARROW_SIZE * cos(angle2));
                y4 = y2 - (int)(ARROW_SIZE * sin(angle2));
        }
-       else
-       {
+       else {
                x3 = x2 + (int)(ARROW_SIZE * cos(angle1));
                y3 = y2 + (int)(ARROW_SIZE * sin(angle1));
                x4 = x2 + (int)(ARROW_SIZE * cos(angle2));
@@ -1446,33 +1108,125 @@ void MotionMain::draw_arrow(VFrame *frame, int x1, int y1, int x2, int y2)
 //     draw_line(frame, x1, y1 + 1, x2, y2 + 1);
 
 // Arrow line
-       if(abs(y2 - y1) || abs(x2 - x1)) draw_line(frame, x2, y2, x3, y3);
+       if( abs(y2 - y1) || abs(x2 - x1) ) draw_line(frame, x2, y2, x3, y3);
 //     draw_line(frame, x2, y2 + 1, x3, y3 + 1);
 // Arrow line
-       if(abs(y2 - y1) || abs(x2 - x1)) draw_line(frame, x2, y2, x4, y4);
+       if( abs(y2 - y1) || abs(x2 - x1) ) draw_line(frame, x2, y2, x4, y4);
 //     draw_line(frame, x2, y2 + 1, x4, y4 + 1);
 }
 
+int MotionMain::open_cache_file()
+{
+       if( cache_fp ) return 0;
+       if( !cache_file[0] ) return 1;
+       if( !(cache_fp = fopen(cache_file, "r")) ) return 1;
+       return 0;
+}
 
+void MotionMain::close_cache_file()
+{
+       if( !cache_fp ) return;
+       fclose(cache_fp);
+       cache_fp = 0; cache_key = -1; tracking_frame = -1;
+}
 
+int MotionMain::load_cache_line()
+{
+       cache_key = -1;
+       if( open_cache_file() ) return 1;
+       if( !fgets(cache_line, sizeof(cache_line), cache_fp) ) return 1;
+       cache_key = strtol(cache_line, 0, 0);
+       return 0;
+}
 
+int MotionMain::get_cache_line(int64_t key)
+{
+       if( cache_key == key ) return 0;
+       if( open_cache_file() ) return 1;
+       if( cache_key >= 0 && key > cache_key ) {
+               if( load_cache_line() ) return 1;
+               if( cache_key == key ) return 0;
+               if( cache_key > key ) return 1;
+       }
+// binary search file
+       fseek(cache_fp, 0, SEEK_END);
+       int64_t l = -1, r = ftell(cache_fp);
+       while( (r - l) > 1 ) {
+               int64_t m = (l + r) / 2;
+               fseek(cache_fp, m, SEEK_SET);
+               if( m > 0 && !fgets(cache_line, sizeof(cache_line), cache_fp) )
+                       return -1;
+               if( !load_cache_line() ) {
+                       if( cache_key == key )
+                               return 0;
+                       if( cache_key < key ) { l = m; continue; }
+               }
+               r = m;
+       }
+       return 1;
+}
 
+int MotionMain::locate_cache_line(int64_t key)
+{
+       int ret = 1;
+       if( key < 0 || !(ret=get_cache_line(key)) ||
+           ( cache_key >= 0 && cache_key < key ) )
+               ret = load_cache_line();
+       return ret;
+}
 
+int MotionMain::put_cache_line(const char *line)
+{
+       int64_t key = strtol(line, 0, 0);
+       if( key == active_key ) return 1;
+       if( !active_fp ) {
+               close_cache_file();
+               sprintf(cache_file, "%s.bak", config.tracking_file);
+               ::rename(config.tracking_file, cache_file);
+               if( !(active_fp = fopen(config.tracking_file, "w")) ) {
+                       perror(config.tracking_file);
+                       fprintf(stderr, "err writing key %jd\n", key);
+                       return -1;
+               }
+               active_key = -1;
+       }
 
+       if( active_key < key ) {
+               locate_cache_line(active_key);
+               while( cache_key >= 0 && key >= cache_key ) {
+                       if( key > cache_key )
+                               fputs(cache_line, active_fp);
+                       load_cache_line();
+               }
+       }
 
+       active_key = key;
+       fputs(line, active_fp);
+       fflush(active_fp);
+       return 0;
+}
 
-
-
-
-
-
+void MotionMain::reset_cache_file()
+{
+       if( active_fp ) {
+               locate_cache_line(active_key);
+               while( cache_key >= 0 ) {
+                       fputs(cache_line, active_fp);
+                       load_cache_line();
+               }
+               close_cache_file();  ::remove(cache_file);
+               fclose(active_fp); active_fp = 0; active_key = -1;
+       }
+       else
+               close_cache_file();
+       strcpy(cache_file, config.tracking_file);
+}
 
 
 RotateScanPackage::RotateScanPackage()
 {
 }
 
-
 RotateScanUnit::RotateScanUnit(RotateScan *server, MotionMain *plugin)
  : LoadClient(server)
 {
@@ -1490,19 +1244,18 @@ RotateScanUnit::~RotateScanUnit()
 
 void RotateScanUnit::process_package(LoadPackage *package)
 {
-       if(server->skip) return;
+       if( server->skip ) return;
        RotateScanPackage *pkg = (RotateScanPackage*)package;
 
-       if((pkg->difference = server->get_cache(pkg->angle)) < 0)
-       {
+       if( (pkg->difference = server->get_cache(pkg->angle)) < 0 ) {
 //printf("RotateScanUnit::process_package %d\n", __LINE__);
                int color_model = server->previous_frame->get_color_model();
                int pixel_size = BC_CModels::calculate_pixelsize(color_model);
                int row_bytes = server->previous_frame->get_bytes_per_line();
 
-               if(!rotater)
+               if( !rotater )
                        rotater = new AffineEngine(1, 1);
-               if(!temp) temp = new VFrame(0,
+               if( !temp ) temp = new VFrame(0,
                        -1,
                        server->previous_frame->get_w(),
                        server->previous_frame->get_h(),
@@ -1512,35 +1265,23 @@ void RotateScanUnit::process_package(LoadPackage *package)
 
 
 // Rotate original block size
-//             rotater->set_viewport(server->block_x1,
-//                     server->block_y1,
-//                     server->block_x2 - server->block_x1,
-//                     server->block_y2 - server->block_y1);
-               rotater->set_in_viewport(server->block_x1,
-                       server->block_y1,
-                       server->block_x2 - server->block_x1,
-                       server->block_y2 - server->block_y1);
-               rotater->set_out_viewport(server->block_x1,
-                       server->block_y1,
-                       server->block_x2 - server->block_x1,
-                       server->block_y2 - server->block_y1);
+//             rotater->set_viewport(server->block_x1, server->block_y1,
+//                     server->block_x2 - server->block_x1, server->block_y2 - server->block_y1);
+               rotater->set_in_viewport(server->block_x1, server->block_y1,
+                       server->block_x2 - server->block_x1, server->block_y2 - server->block_y1);
+               rotater->set_out_viewport(server->block_x1, server->block_y1,
+                       server->block_x2 - server->block_x1, server->block_y2 - server->block_y1);
 //             rotater->set_pivot(server->block_x, server->block_y);
                rotater->set_in_pivot(server->block_x, server->block_y);
                rotater->set_out_pivot(server->block_x, server->block_y);
 //printf("RotateScanUnit::process_package %d\n", __LINE__);
-               rotater->rotate(temp,
-                       server->previous_frame,
-                       pkg->angle);
+               rotater->rotate(temp, server->previous_frame, pkg->angle);
 
 // Scan reduced block size
 //plugin->output_frame->copy_from(server->current_frame);
 //plugin->output_frame->copy_from(temp);
-// printf("RotateScanUnit::process_package %d %d %d %d %d\n",
-// __LINE__,
-// server->scan_x,
-// server->scan_y,
-// server->scan_w,
-// server->scan_h);
+//printf("RotateScanUnit::process_package %d %d %d %d %d\n",
+// __LINE__, server->scan_x, server->scan_y, server->scan_w, server->scan_h);
 // Clamp coordinates
                int x1 = server->scan_x;
                int y1 = server->scan_y;
@@ -1550,18 +1291,13 @@ void RotateScanUnit::process_package(LoadPackage *package)
                y2 = MIN(temp->get_h(), y2);
                x2 = MIN(server->current_frame->get_w(), x2);
                y2 = MIN(server->current_frame->get_h(), y2);
-               x1 = MAX(0, x1);
-               y1 = MAX(0, y1);
+               x1 = MAX(0, x1);  y1 = MAX(0, y1);
 
-               if(x2 > x1 && y2 > y1)
-               {
+               if( x2 > x1 && y2 > y1 ) {
                        pkg->difference = MotionScan::abs_diff(
                                temp->get_rows()[y1] + x1 * pixel_size,
                                server->current_frame->get_rows()[y1] + x1 * pixel_size,
-                               row_bytes,
-                               x2 - x1,
-                               y2 - y1,
-                               color_model);
+                               row_bytes, x2 - x1, y2 - y1, color_model);
 //printf("RotateScanUnit::process_package %d\n", __LINE__);
                        server->put_cache(pkg->angle, pkg->difference);
                }
@@ -1579,34 +1315,12 @@ printf("RotateScanUnit::process_package 10 x=%d y=%d w=%d h=%d block_x=%d block_
        }
 }
 
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
 
 RotateScan::RotateScan(MotionMain *plugin,
        int total_clients,
        int total_packages)
- : LoadServer(
-//1, 1
-total_clients, total_packages
-)
+ : LoadServer( //1, 1)
+               total_clients, total_packages)
 {
        this->plugin = plugin;
        cache_lock = new Mutex("RotateScan::cache_lock");
@@ -1620,13 +1334,10 @@ RotateScan::~RotateScan()
 
 void RotateScan::init_packages()
 {
-       for(int i = 0; i < get_total_packages(); i++)
-       {
+       for( int i = 0; i < get_total_packages(); i++ ) {
                RotateScanPackage *pkg = (RotateScanPackage*)get_package(i);
-               pkg->angle = i *
-                       (scan_angle2 - scan_angle1) /
-                       (total_steps - 1) +
-                       scan_angle1;
+               pkg->angle = scan_angle1 +
+                       i * (scan_angle2 - scan_angle1) / (total_steps - 1);
        }
 }
 
@@ -1641,50 +1352,29 @@ LoadPackage* RotateScan::new_package()
 }
 
 
-float RotateScan::scan_frame(VFrame *previous_frame,
-       VFrame *current_frame,
-       int block_x,
-       int block_y)
+float RotateScan::scan_frame(VFrame *previous_frame, VFrame *current_frame,
+       int block_x, int block_y)
 {
        skip = 0;
        this->block_x = block_x;
        this->block_y = block_y;
 
 //printf("RotateScan::scan_frame %d\n", __LINE__);
-       switch(plugin->config.tracking_type)
-       {
-               case MotionScan::NO_CALCULATE:
-                       result = plugin->config.rotation_center;
-                       skip = 1;
-                       break;
+       switch(plugin->config.tracking_type) {
+       case MotionScan::NO_CALCULATE:
+               result = plugin->config.rotation_center;
+               skip = 1;
+               break;
 
-               case MotionScan::LOAD:
-               {
-                       char string[BCTEXTLEN];
-                       sprintf(string, "%s%06jd",
-                                ROTATION_FILE, plugin->get_source_position());
-                       FILE *input = fopen(string, "r");
-                       if(input)
-                       {
-                               fscanf(input, "%f", &result);
-                               fclose(input);
-                               skip = 1;
-                       }
-                       else
-                       {
-                               perror("RotateScan::scan_frame LOAD");
-                       }
-                       break;
+       case MotionScan::LOAD:
+       case MotionScan::SAVE:
+               if( plugin->load_ok ) {
+                       result = plugin->load_dt;
+                       skip = 1;
                }
+               break;
        }
 
-
-
-
-
-
-
-
        this->previous_frame = previous_frame;
        this->current_frame = current_frame;
        int w = current_frame->get_w();
@@ -1692,10 +1382,10 @@ float RotateScan::scan_frame(VFrame *previous_frame,
        int block_w = w * plugin->config.global_block_w / 100;
        int block_h = h * plugin->config.global_block_h / 100;
 
-       if(this->block_x - block_w / 2 < 0) block_w = this->block_x * 2;
-       if(this->block_y - block_h / 2 < 0) block_h = this->block_y * 2;
-       if(this->block_x + block_w / 2 > w) block_w = (w - this->block_x) * 2;
-       if(this->block_y + block_h / 2 > h) block_h = (h - this->block_y) * 2;
+       if( this->block_x - block_w / 2 < 0 ) block_w = this->block_x * 2;
+       if( this->block_y - block_h / 2 < 0 ) block_h = this->block_y * 2;
+       if( this->block_x + block_w / 2 > w ) block_w = (w - this->block_x) * 2;
+       if( this->block_y + block_h / 2 > h ) block_h = (h - this->block_y) * 2;
 
        block_x1 = this->block_x - block_w / 2;
        block_x2 = this->block_x + block_w / 2;
@@ -1724,14 +1414,11 @@ float RotateScan::scan_frame(VFrame *previous_frame,
        double max_area1 = 0;
        //double max_x1 = 0;
        double max_y1 = 0;
-       for(double x = x1; x < x2; x++)
-       {
+       for( double x = x1; x < x2; x++ ) {
                double y = y1 + (y2 - y1) * (x - x1) / (x2 - x1);
-               if(x >= center_x && x < block_x2 && y >= block_y1 && y < center_y)
-               {
+               if( x >= center_x && x < block_x2 && y >= block_y1 && y < center_y ) {
                        double area = fabs(x - center_x) * fabs(y - center_y);
-                       if(area > max_area1)
-                       {
+                       if( area > max_area1 ) {
                                max_area1 = area;
                                //max_x1 = x;
                                max_y1 = y;
@@ -1743,14 +1430,11 @@ float RotateScan::scan_frame(VFrame *previous_frame,
        double max_area2 = 0;
        double max_x2 = 0;
        //double max_y2 = 0;
-       for(double y = y1; y < y3; y++)
-       {
+       for( double y = y1; y < y3; y++ ) {
                double x = x1 + (x3 - x1) * (y - y1) / (y3 - y1);
-               if(x >= block_x1 && x < center_x && y >= block_y1 && y < center_y)
-               {
+               if( x >= block_x1 && x < center_x && y >= block_y1 && y < center_y ) {
                        double area = fabs(x - center_x) * fabs(y - center_y);
-                       if(area > max_area2)
-                       {
+                       if( area > max_area2 ) {
                                max_area2 = area;
                                max_x2 = x;
                                //max_y2 = y;
@@ -1782,40 +1466,33 @@ float RotateScan::scan_frame(VFrame *previous_frame,
        cache.remove_all_objects();
 
 
-       if(!skip)
-       {
-               if(previous_frame->data_matches(current_frame))
-               {
+       if( !skip ) {
+               if( previous_frame->data_matches(current_frame) ) {
 //printf("RotateScan::scan_frame: frames match.  Skipping.\n");
                        result = plugin->config.rotation_center;
                        skip = 1;
                }
        }
 
-       if(!skip)
-       {
+       if( !skip ) {
 // Initial search range
                float angle_range = max_angle;
                result = plugin->config.rotation_center;
                total_steps = plugin->config.rotate_positions;
 
 
-               while(angle_range >= min_angle * total_steps)
-               {
+               while( angle_range >= min_angle * total_steps ) {
                        scan_angle1 = result - angle_range;
                        scan_angle2 = result + angle_range;
 
-
                        set_package_count(total_steps);
 //set_package_count(1);
                        process_packages();
 
                        int64_t min_difference = -1;
-                       for(int i = 0; i < get_total_packages(); i++)
-                       {
+                       for( int i = 0; i < get_total_packages(); i++ ) {
                                RotateScanPackage *pkg = (RotateScanPackage*)get_package(i);
-                               if(pkg->difference < min_difference || min_difference == -1)
-                               {
+                               if( pkg->difference < min_difference || min_difference == -1 ) {
                                        min_difference = pkg->difference;
                                        result = pkg->angle;
                                }
@@ -1828,29 +1505,10 @@ float RotateScan::scan_frame(VFrame *previous_frame,
                }
        }
 
-//printf("RotateScan::scan_frame %d\n", __LINE__);
-
-       if(!skip && plugin->config.tracking_type == MotionScan::SAVE)
-       {
-               char string[BCTEXTLEN];
-               sprintf(string, "%s%06jd",
-                       ROTATION_FILE, plugin->get_source_position());
-               FILE *output = fopen(string, "w");
-               if(output)
-               {
-                       fprintf(output, "%f\n", result);
-                       fclose(output);
-               }
-               else
-               {
-                       perror("RotateScan::scan_frame SAVE");
-               }
+       if( plugin->config.tracking_type == MotionScan::SAVE ) {
+               plugin->save_dt = result;
        }
-
 //printf("RotateScan::scan_frame %d angle=%f\n", __LINE__, result);
-
-
-
        return result;
 }
 
@@ -1858,11 +1516,9 @@ int64_t RotateScan::get_cache(float angle)
 {
        int64_t result = -1;
        cache_lock->lock("RotateScan::get_cache");
-       for(int i = 0; i < cache.total; i++)
-       {
+       for( int i = 0; i < cache.total; i++ ) {
                RotateScanCache *ptr = cache.values[i];
-               if(fabs(ptr->angle - angle) <= MIN_ANGLE)
-               {
+               if( fabs(ptr->angle - angle) <= MIN_ANGLE ) {
                        result = ptr->difference;
                        break;
                }
@@ -1880,13 +1536,6 @@ void RotateScan::put_cache(float angle, int64_t difference)
 }
 
 
-
-
-
-
-
-
-
 RotateScanCache::RotateScanCache(float angle, int64_t difference)
 {
        this->angle = angle;
index e0011d751171bc8707ee32361650a65692c25a47..493aeeef0f735a1f913ab8404181bd7ffe033d51 100644 (file)
@@ -43,8 +43,6 @@ class MotionWindow;
 class RotateScan;
 
 
-
-
 // Limits of global range in percent
 #define MIN_RADIUS 1
 #define MAX_RADIUS 100
@@ -64,7 +62,7 @@ class RotateScan;
 // Precision of rotation
 #define MIN_ANGLE 0.0001
 
-#define ROTATION_FILE "/tmp/r"
+#define TRACKING_FILE "/tmp/motion"
 
 class MotionConfig
 {
@@ -73,13 +71,9 @@ public:
 
        int equivalent(MotionConfig &that);
        void copy_from(MotionConfig &that);
-       void interpolate(MotionConfig &prev,
-               MotionConfig &next,
-               int64_t prev_frame,
-               int64_t next_frame,
-               int64_t current_frame);
+       void interpolate(MotionConfig &prev, MotionConfig &next,
+               int64_t prev_frame, int64_t next_frame, int64_t current_frame);
        void boundaries();
-       void set_cpus(int cpus);
 
        int block_count;
        int global_range_w;
@@ -110,6 +104,7 @@ public:
        int global;
        int rotate;
        int addtrackedframeoffset;
+       char tracking_file[BCTEXTLEN];
 // Track or stabilize, single pixel, scan only, or nothing
        int action_type;
 // Recalculate, no calculate, save, or load coordinates from disk
@@ -117,28 +112,6 @@ public:
 // Track a single frame, previous frame, or previous frame same block
        int tracking_object;
 
-#if 0
-       enum
-       {
-// action_type
-               TRACK,
-               STABILIZE,
-               TRACK_PIXEL,
-               STABILIZE_PIXEL,
-               NOTHING,
-// mode2
-               RECALCULATE,
-               SAVE,
-               LOAD,
-               NO_CALCULATE,
-// tracking_object
-               TRACK_SINGLE,
-               TRACK_PREVIOUS,
-               PREVIOUS_SAME_BLOCK
-       };
-#endif
-
-
 // Number of single frame to track relative to timeline start
        int64_t track_frame;
 // Master layer
@@ -146,17 +119,13 @@ public:
 };
 
 
-
-
 class MotionMain : public PluginVClient
 {
 public:
        MotionMain(PluginServer *server);
        ~MotionMain();
 
-       int process_buffer(VFrame **frame,
-               int64_t start_position,
-               double frame_rate);
+       int process_buffer(VFrame **frame, int64_t start_position, double frame_rate);
        void process_global();
        void process_rotation();
        void draw_vectors(VFrame *frame);
@@ -171,7 +140,6 @@ public:
 
        PLUGIN_CLASS_MEMBERS2(MotionConfig)
 
-
        static void draw_pixel(VFrame *frame, int x, int y);
        static void draw_line(VFrame *frame, int x1, int y1, int x2, int y2);
        void draw_arrow(VFrame *frame, int x1, int y1, int x2, int y2);
@@ -199,7 +167,25 @@ public:
        int current_dy;
        float current_angle;
 
-
+       char cache_file[BCTEXTLEN];
+       FILE *cache_fp, *active_fp;
+       void reset_cache_file();
+       int open_cache_file();
+       void close_cache_file();
+       int load_cache_line();
+       int locate_cache_line(int64_t key);
+       int get_cache_line(int64_t key);
+       int put_cache_line(const char *line);
+       char cache_line[BCSTRLEN];
+       int64_t cache_key, active_key;
+// add constant frame offset values
+       int dx_offset, dy_offset;
+       int64_t tracking_frame;
+// save/load result values
+       int load_ok;
+       int save_dx, load_dx;
+       int save_dy, load_dy;
+       float save_dt, load_dt;
 
 // Oversampled current frame for motion estimation
        int32_t *search_area;
@@ -239,28 +225,6 @@ public:
 };
 
 
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
 class RotateScanPackage : public LoadPackage
 {
 public:
@@ -349,13 +313,4 @@ private:
        Mutex *cache_lock;
 };
 
-
-
-
 #endif
-
-
-
-
-
-
index 01c3594729637e0450a96ee327dd08c09088016b..5ba3c81a51f99f9212e6cde7f9c3ffc2a566fc92 100644 (file)
@@ -22,9 +22,7 @@
 #ifndef MOTION_INC
 #define MOTION_INC
 
-
 class MotionConfig;
 class MotionMain;
 
-
 #endif
index b1a533ef850477a9c7412d39e92c45d5d60c2a09..0685b6562a9f51ebc15b7efb1d9de503b04f6ef3 100644 (file)
@@ -2,21 +2,21 @@
 /*
  * CINELERRA
  * Copyright (C) 2012 Adam Williams <broadcast at earthling dot net>
- * 
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
- * 
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- * 
+ *
  */
 
 #include "clip.h"
 
 // The module which does the actual scanning
 
-
-
-
-
 MotionScanPackage::MotionScanPackage()
  : LoadPackage()
 {
        valid = 1;
 }
 
-
-
-
-
-
 MotionScanUnit::MotionScanUnit(MotionScan *server)
  : LoadClient(server)
 {
@@ -57,8 +48,6 @@ MotionScanUnit::~MotionScanUnit()
        delete cache_lock;
 }
 
-
-
 void MotionScanUnit::process_package(LoadPackage *package)
 {
        MotionScanPackage *pkg = (MotionScanPackage*)package;
@@ -68,40 +57,25 @@ void MotionScanUnit::process_package(LoadPackage *package)
        int pixel_size = BC_CModels::calculate_pixelsize(color_model);
        int row_bytes = server->current_frame->get_bytes_per_line();
 
-
-
-
-
-
-
-
-
-
-
-
 // Single pixel
-       if(!server->subpixel)
-       {
+       if( !server->subpixel ) {
 // Try cache
                pkg->difference1 = server->get_cache(pkg->search_x, pkg->search_y);
-               if(pkg->difference1 < 0)
-               {
-//printf("MotionScanUnit::process_package 1 search_x=%d search_y=%d scan_x1=%d scan_y1=%d scan_x2=%d scan_y2=%d x_steps=%d y_steps=%d\n", 
-//pkg->search_x, pkg->search_y, pkg->scan_x1, pkg->scan_y1, pkg->scan_x2, pkg->scan_y2, server->x_steps, server->y_steps);
+               if( pkg->difference1 < 0 ) {
+//printf("MotionScanUnit::process_package 1 search_x=%d search_y=%d"
+// " scan_x1=%d scan_y1=%d scan_x2=%d scan_y2=%d x_steps=%d y_steps=%d\n",
+// pkg->search_x, pkg->search_y, pkg->scan_x1, pkg->scan_y1, pkg->scan_x2, pkg->scan_y2,
+// server->x_steps, server->y_steps);
 // Pointers to first pixel in each block
-                       unsigned char *prev_ptr = server->previous_frame->get_rows()[
-                               pkg->search_y] +        
+                       unsigned char *prev_ptr =
+                           server->previous_frame->get_rows()[pkg->search_y] +
                                pkg->search_x * pixel_size;
-                       unsigned char *current_ptr = server->current_frame->get_rows()[
-                               pkg->block_y1] +
+                       unsigned char *current_ptr =
+                           server->current_frame->get_rows()[pkg->block_y1] +
                                pkg->block_x1 * pixel_size;
-
 // Scan block
-                       pkg->difference1 = MotionScan::abs_diff(prev_ptr,
-                               current_ptr,
-                               row_bytes,
-                               pkg->block_x2 - pkg->block_x1,
-                               pkg->block_y2 - pkg->block_y1,
+                       pkg->difference1 = MotionScan::abs_diff(prev_ptr, current_ptr, row_bytes,
+                               pkg->block_x2 - pkg->block_x1, pkg->block_y2 - pkg->block_y1,
                                color_model);
 
 // printf("MotionScanUnit::process_package %d search_x=%d search_y=%d diff=%lld\n",
@@ -109,81 +83,36 @@ void MotionScanUnit::process_package(LoadPackage *package)
                        server->put_cache(pkg->search_x, pkg->search_y, pkg->difference1);
                }
        }
-
-
-
-
-
-
-
-       else
-
-
-
-
-
-
-
-
 // Sub pixel
-       {
-               unsigned char *prev_ptr = server->previous_frame->get_rows()[
-                       pkg->search_y] +
+       else {
+               unsigned char *prev_ptr =
+                   server->previous_frame->get_rows()[pkg->search_y] +
                        pkg->search_x * pixel_size;
-               unsigned char *current_ptr = server->current_frame->get_rows()[
-                       pkg->block_y1] +
+               unsigned char *current_ptr =
+                   server->current_frame->get_rows()[pkg->block_y1] +
                        pkg->block_x1 * pixel_size;
 
 // With subpixel, there are two ways to compare each position, one by shifting
 // the previous frame and two by shifting the current frame.
-               pkg->difference1 = MotionScan::abs_diff_sub(prev_ptr,
-                       current_ptr,
-                       row_bytes,
-                       pkg->block_x2 - pkg->block_x1,
-                       pkg->block_y2 - pkg->block_y1,
-                       color_model,
-                       pkg->sub_x,
-                       pkg->sub_y);
-               pkg->difference2 = MotionScan::abs_diff_sub(current_ptr,
-                       prev_ptr,
-                       row_bytes,
-                       pkg->block_x2 - pkg->block_x1,
-                       pkg->block_y2 - pkg->block_y1,
-                       color_model,
-                       pkg->sub_x,
-                       pkg->sub_y);
-// printf("MotionScanUnit::process_package sub_x=%d sub_y=%d search_x=%d search_y=%d diff1=%lld diff2=%lld\n",
-// sub_x,
-// sub_y,
-// search_x,
-// search_y,
-// pkg->difference1,
-// pkg->difference2);
+               pkg->difference1 = MotionScan::abs_diff_sub(prev_ptr, current_ptr, row_bytes,
+                       pkg->block_x2 - pkg->block_x1, pkg->block_y2 - pkg->block_y1,
+                       color_model, pkg->sub_x, pkg->sub_y);
+               pkg->difference2 =
+                   MotionScan::abs_diff_sub(current_ptr, prev_ptr, row_bytes,
+                       pkg->block_x2 - pkg->block_x1, pkg->block_y2 - pkg->block_y1,
+                       color_model, pkg->sub_x, pkg->sub_y);
+//printf("MotionScanUnit::process_package sub_x=%d sub_y=%d search_x=%d search_y=%d diff1=%lld diff2=%lld\n",
+// sub_x, sub_y, search_x, search_y, pkg->difference1, pkg->difference2);
        }
-
-
-
-
 }
 
-
-
-
-
-
-
-
-
-
 int64_t MotionScanUnit::get_cache(int x, int y)
 {
        int64_t result = -1;
        cache_lock->lock("MotionScanUnit::get_cache");
-       for(int i = 0; i < cache.total; i++)
-       {
+       for( int i = 0; i < cache.total; i++ ) {
                MotionScanCache *ptr = cache.values[i];
-               if(ptr->x == x && ptr->y == y)
-               {
+               if( ptr->x == x && ptr->y == y ) {
                        result = ptr->difference;
                        break;
                }
@@ -200,22 +129,9 @@ void MotionScanUnit::put_cache(int x, int y, int64_t difference)
        cache_lock->unlock();
 }
 
-
-
-
-
-
-
-
-
-
-
-MotionScan::MotionScan(int total_clients,
-       int total_packages)
- : LoadServer(
-//1, 1 
-total_clients, total_packages 
-)
+MotionScan::MotionScan(int total_clients, int total_packages)
+ : LoadServer( //1, 1
+               total_clients, total_packages)
 {
        test_match = 1;
        cache_lock = new Mutex("MotionScan::cache_lock");
@@ -232,78 +148,46 @@ MotionScan::~MotionScan()
 //     delete downsample;
 }
 
-
 void MotionScan::init_packages()
 {
 // Set package coords
 //printf("MotionScan::init_packages %d %d\n", __LINE__, get_total_packages());
-       for(int i = 0; i < get_total_packages(); i++)
-       {
+       for( int i = 0; i < get_total_packages(); i++ ) {
                MotionScanPackage *pkg = (MotionScanPackage*)get_package(i);
 
-               pkg->block_x1 = block_x1;
-               pkg->block_x2 = block_x2;
-               pkg->block_y1 = block_y1;
-               pkg->block_y2 = block_y2;
-               pkg->scan_x1 = scan_x1;
-               pkg->scan_x2 = scan_x2;
-               pkg->scan_y1 = scan_y1;
-               pkg->scan_y2 = scan_y2;
-               pkg->step = i;
-               pkg->difference1 = 0;
-               pkg->difference2 = 0;
-               pkg->dx = 0;
-               pkg->dy = 0;
-               pkg->valid = 1;
-               
-               if(!subpixel)
-               {
-                       pkg->search_x = pkg->scan_x1 + (pkg->step % x_steps) *
-                               (scan_x2 - scan_x1) / x_steps;
-                       pkg->search_y = pkg->scan_y1 + (pkg->step / x_steps) *
-                               (scan_y2 - scan_y1) / y_steps;
-                       pkg->sub_x = 0;
-                       pkg->sub_y = 0;
+               pkg->block_x1 = block_x1; pkg->block_x2 = block_x2;
+               pkg->block_y1 = block_y1; pkg->block_y2 = block_y2;
+               pkg->scan_x1 = scan_x1;   pkg->scan_x2 = scan_x2;
+               pkg->scan_y1 = scan_y1;   pkg->scan_y2 = scan_y2;
+               pkg->difference1 = 0;     pkg->difference2 = 0;
+               pkg->step = i;            pkg->valid = 1;
+               pkg->dx = pkg->dy = 0;
+
+               if( !subpixel ) {
+                       pkg->search_x = pkg->scan_x1 +
+                               (pkg->step % x_steps) * (scan_x2 - scan_x1) / x_steps;
+                       pkg->search_y = pkg->scan_y1 +
+                               (pkg->step / x_steps) * (scan_y2 - scan_y1) / y_steps;
+                       pkg->sub_x = pkg->sub_y = 0;
                }
-               else
-               {
+               else {
                        pkg->sub_x = pkg->step % (OVERSAMPLE * 2);
                        pkg->sub_y = pkg->step / (OVERSAMPLE * 2);
 
-                       if(horizontal_only)
-                       {
-                               pkg->sub_y = 0;
-                       }
-
-                       if(vertical_only)
-                       {
-                               pkg->sub_x = 0;
-                       }
+                       if( horizontal_only ) pkg->sub_y = 0;
+                       if( vertical_only ) pkg->sub_x = 0;
 
                        pkg->search_x = pkg->scan_x1 + pkg->sub_x / OVERSAMPLE + 1;
                        pkg->search_y = pkg->scan_y1 + pkg->sub_y / OVERSAMPLE + 1;
                        pkg->sub_x %= OVERSAMPLE;
                        pkg->sub_y %= OVERSAMPLE;
 
-
-
-// printf("MotionScan::init_packages %d i=%d search_x=%d search_y=%d sub_x=%d sub_y=%d\n", 
-// __LINE__,
-// i,
-// pkg->search_x,
-// pkg->search_y,
-// pkg->sub_x,
-// pkg->sub_y);
+// printf("MotionScan::init_packages %d i=%d search_x=%d search_y=%d sub_x=%d sub_y=%d\n",
+// __LINE__, i, pkg->search_x, pkg->search_y, pkg->sub_x, pkg->sub_y);
                }
 
 // printf("MotionScan::init_packages %d %d,%d %d,%d %d,%d\n",
-// __LINE__,
-// scan_x1,
-// scan_x2,
-// scan_y1,
-// scan_y2,
-// pkg->search_x,
-// pkg->search_y);
+// __LINE__, scan_x1, scan_x2, scan_y1, scan_y2, pkg->search_x, pkg->search_y);
        }
 }
 
@@ -317,31 +201,20 @@ LoadPackage* MotionScan::new_package()
        return new MotionScanPackage;
 }
 
-
 void MotionScan::set_test_match(int value)
 {
        this->test_match = value;
 }
 
-void MotionScan::scan_frame(VFrame *previous_frame,
-       VFrame *current_frame,
-       int global_range_w,
-       int global_range_h,
-       int global_block_w,
-       int global_block_h,
-       double block_x,
-       double block_y,
-       int frame_type,
-       int tracking_type,
-       int action_type,
-       int horizontal_only,
-       int vertical_only,
-       int source_position,
-       int total_steps,
-       int total_dx,
-       int total_dy,
-       int global_origin_x,
-       int global_origin_y)
+void MotionScan::scan_frame(VFrame *previous_frame, VFrame *current_frame,
+               int global_range_w, int global_range_h,
+               int global_block_w, int global_block_h,
+               double block_x, double block_y, int frame_type,
+               int tracking_type, int action_type,
+               int horizontal_only, int vertical_only,
+               int source_position, int total_steps, int total_dx,
+               int total_dy, int global_origin_x, int global_origin_y,
+               int load_ok, int load_dx, int load_dy)
 {
        this->previous_frame_arg = previous_frame;
        this->current_frame_arg = current_frame;
@@ -373,8 +246,7 @@ void MotionScan::scan_frame(VFrame *previous_frame,
 
 // Offset to location of previous block.  This offset needn't be very accurate
 // since it's the offset of the previous image and current image we want.
-       if(frame_type == MotionScan::TRACK_PREVIOUS)
-       {
+       if( frame_type == MotionScan::TRACK_PREVIOUS ) {
                block_x1 += total_dx / OVERSAMPLE;
                block_y1 += total_dy / OVERSAMPLE;
                block_x2 += total_dx / OVERSAMPLE;
@@ -383,58 +255,37 @@ void MotionScan::scan_frame(VFrame *previous_frame,
 
        skip = 0;
 
-       switch(tracking_type)
-       {
+       switch( tracking_type ) {
 // Don't calculate
-               case MotionScan::NO_CALCULATE:
-                       dx_result = 0;
-                       dy_result = 0;
+       case MotionScan::NO_CALCULATE:
+               dx_result = dy_result = 0;
+               skip = 1;
+               break;
+
+       case MotionScan::LOAD:
+       case MotionScan::SAVE:
+               if( load_ok ) {
+                       dx_result = load_dx;
+                       dy_result = load_dy;
                        skip = 1;
-                       break;
-
-               case MotionScan::LOAD:
-               {
-// Load result from disk
-                       char string[BCTEXTLEN];
-                       sprintf(string, "%s%06d", 
-                               MOTION_FILE, 
-                               source_position);
-//printf("MotionScan::scan_frame %d %s\n", __LINE__, string);
-                       FILE *input = fopen(string, "r");
-                       if(input)
-                       {
-                               (void)fscanf(input, "%d %d", 
-                                       &dx_result, &dy_result);
-// HACK
-//dx_result *= 2;
-//dy_result *= 2;
-//printf("MotionScan::scan_frame %d %d %d\n", __LINE__, dx_result, dy_result);
-                               fclose(input);
-                               skip = 1;
-                       }
-                       break;
                }
+               break;
 
 // Scan from scratch
-               default:
-                       skip = 0;
-                       break;
+       default:
+               skip = 0;
+               break;
        }
 
-       if(!skip && test_match)
-       {
-               if(previous_frame->data_matches(current_frame))
-               {
-printf("MotionScan::scan_frame: data matches. skipping.\n");
-                       dx_result = 0;
-                       dy_result = 0;
+       if( !skip && test_match ) {
+               if( previous_frame->data_matches(current_frame) ) {
+                       printf("MotionScan::scan_frame: data matches. skipping.\n");
+                       dx_result = dy_result = 0;
                        skip = 1;
                }
        }
-
 // Perform scan
-       if(!skip)
-       {
+       if( !skip ) {
 //printf("MotionScan::scan_frame %d\n", __LINE__);
 // Location of block in current frame
                int origin_offset_x = this->global_origin_x * w / 100;
@@ -442,19 +293,12 @@ printf("MotionScan::scan_frame: data matches. skipping.\n");
                int x_result = block_x1 + origin_offset_x;
                int y_result = block_y1 + origin_offset_y;
 
-// printf("MotionScan::scan_frame 1 %d %d %d %d %d %d %d %d\n",
-// block_x1 + block_w / 2,
-// block_y1 + block_h / 2,
-// block_w,
-// block_h,
-// block_x1,
-// block_y1,
-// block_x2,
-// block_y2);
-
-               while(1)
-               {
-// Cache needs to be cleared if downsampling is used because the sums of 
+//printf("MotionScan::scan_frame 1 %d %d %d %d %d %d %d %d\n",
+// block_x1 + block_w / 2, block_y1 + block_h / 2,
+// block_w, block_h, block_x1, block_y1, block_x2, block_y2);
+
+               while(1) {
+// Cache needs to be cleared if downsampling is used because the sums of
 // different downsamplings can't be compared.
 // Subpixel never uses the cache.
 //                     cache.remove_all_objects();
@@ -463,65 +307,33 @@ printf("MotionScan::scan_frame: data matches. skipping.\n");
                        scan_x2 = x_result + scan_w / 2;
                        scan_y2 = y_result + scan_h / 2;
 
-
-
 // Zero out requested values
-                       if(horizontal_only)
-                       {
+                       if( horizontal_only ) {
                                scan_y1 = block_y1;
                                scan_y2 = block_y1 + 1;
                        }
-                       if(vertical_only)
-                       {
+                       if( vertical_only ) {
                                scan_x1 = block_x1;
                                scan_x2 = block_x1 + 1;
                        }
-
-// printf("MotionScan::scan_frame 1 %d %d %d %d %d %d %d %d\n",
-// block_x1,
-// block_y1,
-// block_x2,
-// block_y2,
-// scan_x1,
-// scan_y1,
-// scan_x2,
-// scan_y2);
+//printf("MotionScan::scan_frame 1 %d %d %d %d %d %d %d %d\n",
+// block_x1, block_y1, block_x2, block_y2, scan_x1, scan_y1, scan_x2, scan_y2);
 // Clamp the block coords before the scan so we get useful scan coords.
-                       clamp_scan(w, 
-                               h, 
-                               &block_x1,
-                               &block_y1,
-                               &block_x2,
-                               &block_y2,
-                               &scan_x1,
-                               &scan_y1,
-                               &scan_x2,
-                               &scan_y2,
-                               0);
-// printf("MotionScan::scan_frame 1 %d block_x1=%d block_y1=%d block_x2=%d block_y2=%d\n        scan_x1=%d scan_y1=%d scan_x2=%d scan_y2=%d\n    x_result=%d y_result=%d\n", 
-// __LINE__,
-// block_x1,
-// block_y1,
-// block_x2,
-// block_y2,
-// scan_x1, 
-// scan_y1, 
-// scan_x2, 
-// scan_y2, 
-// x_result, 
-// y_result);
-
+                       clamp_scan(w, h, &block_x1, &block_y1, &block_x2,
+                               &block_y2, &scan_x1, &scan_y1, &scan_x2,
+                               &scan_y2, 0);
+// printf("MotionScan::scan_frame 1 %d block_x1=%d block_y1=%d block_x2=%d block_y2=%d\n"
+//  "    scan_x1=%d scan_y1=%d scan_x2=%d scan_y2=%d\n"
+//  "    x_result=%d y_result=%d\n", __LINE__, block_x1, block_y1, block_x2, block_y2,
+//      scan_x1, scan_y1, scan_x2, scan_y2, x_result, y_result);
 
 // Give up if invalid coords.
-                       if(scan_y2 <= scan_y1 ||
-                               scan_x2 <= scan_x1 ||
-                               block_x2 <= block_x1 ||
-                               block_y2 <= block_y1)
+                       if (scan_y2 <= scan_y1 || scan_x2 <= scan_x1 ||
+                           block_x2 <= block_x1 || block_y2 <= block_y1 )
                                break;
 
 // For subpixel, the top row and left column are skipped
-                       if(subpixel)
-                       {
+                       if( subpixel ) {
 
 //printf("MotionScan::scan_frame %d %d %d\n", __LINE__, x_result, y_result);
 // Scan every subpixel in a 2 pixel * 2 pixel square
@@ -537,29 +349,25 @@ printf("MotionScan::scan_frame: data matches. skipping.\n");
 
 // Get least difference
                                int64_t min_difference = -1;
-                               for(int i = 0; i < get_total_packages(); i++)
-                               {
-                                       MotionScanPackage *pkg = (MotionScanPackage*)get_package(i);
-//printf("MotionScan::scan_frame %d search_x=%d search_y=%d sub_x=%d sub_y=%d diff1=%lld diff2=%lld\n", 
+                               for( int i = 0; i < get_total_packages(); i++ ) {
+                                       MotionScanPackage *pkg = (MotionScanPackage *)get_package(i);
+//printf("MotionScan::scan_frame %d search_x=%d search_y=%d sub_x=%d sub_y=%d diff1=%lld diff2=%lld\n",
 //__LINE__, pkg->search_x, pkg->search_y, pkg->sub_x, pkg->sub_y, pkg->difference1, pkg->difference2);
-                                       if(pkg->difference1 < min_difference || min_difference == -1)
-                                       {
+                                       if( pkg->difference1 < min_difference ||
+                                           min_difference == -1 ) {
                                                min_difference = pkg->difference1;
 
 // The sub coords are 1 pixel up & left of the block coords
                                                x_result = pkg->search_x * OVERSAMPLE + pkg->sub_x;
                                                y_result = pkg->search_y * OVERSAMPLE + pkg->sub_y;
-
-
 // Fill in results
                                                dx_result = block_x1 * OVERSAMPLE - x_result;
                                                dy_result = block_y1 * OVERSAMPLE - y_result;
-//printf("MotionScan::scan_frame %d dx_result=%d dy_result=%d diff=%lld\n", 
+//printf("MotionScan::scan_frame %d dx_result=%d dy_result=%d diff=%lld\n",
 //__LINE__, dx_result, dy_result, min_difference);
                                        }
 
-                                       if(pkg->difference2 < min_difference)
-                                       {
+                                       if( pkg->difference2 < min_difference ) {
                                                min_difference = pkg->difference2;
 
                                                x_result = pkg->search_x * OVERSAMPLE - pkg->sub_x;
@@ -567,34 +375,33 @@ printf("MotionScan::scan_frame: data matches. skipping.\n");
 
                                                dx_result = block_x1 * OVERSAMPLE - x_result;
                                                dy_result = block_y1 * OVERSAMPLE - y_result;
-//printf("MotionScan::scan_frame %d dx_result=%d dy_result=%d diff=%lld\n", 
+//printf("MotionScan::scan_frame %d dx_result=%d dy_result=%d diff=%lld\n",
 //__LINE__, dx_result, dy_result, min_difference);
                                        }
                                }
 
                                break;
                        }
-                       else
 // Single pixel
-                       {
-                               total_pixels = (scan_x2 - scan_x1) * (scan_y2 - scan_y1);
-                               this->total_steps = MIN(total_steps, total_pixels);
+                       else {
+                               total_pixels =
+                                   (scan_x2 - scan_x1) * (scan_y2 - scan_y1);
+                               this->total_steps =
+                                   MIN(total_steps, total_pixels);
 
-                               if(this->total_steps == total_pixels)
-                               {
+                               if( this->total_steps == total_pixels ) {
                                        x_steps = scan_x2 - scan_x1;
                                        y_steps = scan_y2 - scan_y1;
                                }
-                               else
-                               {
+                               else {
                                        x_steps = (int)sqrt(this->total_steps);
                                        y_steps = (int)sqrt(this->total_steps);
                                }
 
 // Use downsampled images
-//                             if(scan_x2 - scan_x1 > x_steps * 4 ||
-//                                     scan_y2 - scan_y1 > y_steps * 4)
-//                             {
+//                             if( scan_x2 - scan_x1 > x_steps * 4 ||
+//                                      scan_y2 - scan_y1 > y_steps * 4 )
+//                              {
 // printf("MotionScan::scan_frame %d total_pixels=%d total_steps=%d x_steps=%d y_steps=%d x y steps=%d\n",
 // __LINE__,
 // total_pixels,
@@ -602,108 +409,85 @@ printf("MotionScan::scan_frame: data matches. skipping.\n");
 // x_steps,
 // y_steps,
 // x_steps * y_steps);
-// 
-//                                     if(!downsampled_previous ||
-//                                             !downsampled_previous->equivalent(previous_frame_arg))
-//                                     {
+//
+//                                     if( !downsampled_previous ||
+//                                              !downsampled_previous->equivalent(previous_frame_arg) )
+//                                      {
 //                                             delete downsampled_previous;
 //                                             downsampled_previous = new VFrame(*previous_frame_arg);
 //                                     }
-// 
-//                                     if(!downsampled_current ||
-//                                             !downsampled_current->equivalent(current_frame_arg))
-//                                     {
+//
+//                                     if( !downsampled_current ||
+//                                              !downsampled_current->equivalent(current_frame_arg) )
+//                                      {
 //                                             delete downsampled_current;
 //                                             downsampled_current = new VFrame(*current_frame_arg);
 //                                     }
-// 
-// 
-//                                     if(!downsample)
-//                                             downsample = new DownSampleServer(get_total_clients(), 
+//
+//
+//                                     if( !downsample )
+//                                             downsample = new DownSampleServer(get_total_clients(),
 //                                                     get_total_clients());
-//                                     downsample->process_frame(downsampled_previous, 
-//                                             previous_frame_arg, 
-//                                             1, 
-//                                             1, 
-//                                             1, 
-//                                             1,
+//                                     downsample->process_frame(downsampled_previous,
+//                                             previous_frame_arg, 1, 1, 1, 1,
 //                                             (scan_y2 - scan_y1) / y_steps,
 //                                             (scan_x2 - scan_x1) / x_steps,
-//                                             0,
-//                                             0);
-//                                     downsample->process_frame(downsampled_current, 
-//                                             current_frame_arg, 
-//                                             1, 
-//                                             1, 
-//                                             1, 
-//                                             1,
+//                                             0, 0);
+//                                     downsample->process_frame(downsampled_current,
+//                                             current_frame_arg, 1, 1, 1, 1,
 //                                             (scan_y2 - scan_y1) / y_steps,
 //                                             (scan_x2 - scan_x1) / x_steps,
-//                                             0,
-//                                             0);
+//                                             0, 0);
 //                                     this->previous_frame = downsampled_previous;
 //                                     this->current_frame = downsampled_current;
 //                             }
 
-
-
-
-
-// printf("MotionScan::scan_frame %d this->total_steps=%d\n", 
-// __LINE__, 
-// this->total_steps);
-
+// printf("MotionScan::scan_frame %d this->total_steps=%d\n",
+// __LINE__, this->total_steps);
 
                                set_package_count(this->total_steps);
                                process_packages();
 
 // Get least difference
                                int64_t min_difference = -1;
-                               for(int i = 0; i < get_total_packages(); i++)
-                               {
-                                       MotionScanPackage *pkg = (MotionScanPackage*)get_package(i);
-//printf("MotionScan::scan_frame %d search_x=%d search_y=%d sub_x=%d sub_y=%d diff1=%lld diff2=%lld\n", 
-//__LINE__, pkg->search_x, pkg->search_y, pkg->sub_x, pkg->sub_y, pkg->difference1, pkg->difference2);
-                                       if(pkg->difference1 < min_difference || min_difference == -1)
-                                       {
+                               for( int i = 0; i < get_total_packages(); i++ ) {
+                                       MotionScanPackage *pkg = (MotionScanPackage *) get_package(i);
+//printf("MotionScan::scan_frame %d search_x=%d search_y=%d sub_x=%d sub_y=%d diff1=%lld diff2=%lld\n",
+// __LINE__, pkg->search_x, pkg->search_y, pkg->sub_x, pkg->sub_y, pkg->difference1, pkg->difference2);
+                                       if (pkg->difference1 < min_difference
+                                           || min_difference == -1) {
                                                min_difference = pkg->difference1;
                                                x_result = pkg->search_x;
                                                y_result = pkg->search_y;
                                                x_result *= OVERSAMPLE;
                                                y_result *= OVERSAMPLE;
-//printf("MotionScan::scan_frame %d x_result=%d y_result=%d diff=%lld\n", 
+//printf("MotionScan::scan_frame %d x_result=%d y_result=%d diff=%lld\n",
 //__LINE__, block_x1 * OVERSAMPLE - x_result, block_y1 * OVERSAMPLE - y_result, pkg->difference1);
                                        }
                                }
 
-
 // If a new search is required, rescale results back to pixels.
-                               if(this->total_steps >= total_pixels)
-                               {
+                               if( this->total_steps >= total_pixels ) {
 // Single pixel accuracy reached.  Now do exhaustive subpixel search.
-                                       if(action_type == MotionScan::STABILIZE ||
+                                       if( action_type == MotionScan::STABILIZE ||
                                                action_type == MotionScan::TRACK ||
-                                               action_type == MotionScan::NOTHING)
-                                       {
+                                               action_type == MotionScan::NOTHING ) {
 //printf("MotionScan::scan_frame %d %d %d\n", __LINE__, x_result, y_result);
                                                x_result /= OVERSAMPLE;
                                                y_result /= OVERSAMPLE;
-                                               scan_w = 2;
-                                               scan_h = 2;
+                                               scan_w = scan_h = 2;
                                                subpixel = 1;
                                        }
-                                       else
-                                       {
 // Fill in results and quit
+                                       else {
                                                dx_result = block_x1 * OVERSAMPLE - x_result;
                                                dy_result = block_y1 * OVERSAMPLE - y_result;
 //printf("MotionScan::scan_frame %d %d %d\n", __LINE__, dx_result, dy_result);
                                                break;
                                        }
                                }
-                               else
 // Reduce scan area and try again
-                               {
+                               else {
                                        scan_w = (scan_x2 - scan_x1) / 2;
                                        scan_h = (scan_y2 - scan_y1) / 2;
                                        x_result /= OVERSAMPLE;
@@ -711,72 +495,25 @@ printf("MotionScan::scan_frame: data matches. skipping.\n");
                                }
                        }
                }
-
-               dx_result *= -1;
-               dy_result *= -1;
+               dx_result = -dx_result;
+               dy_result = -dy_result;
        }
 //printf("MotionScan::scan_frame %d\n", __LINE__);
 
+       if( vertical_only ) dx_result = 0;
+       if( horizontal_only ) dy_result = 0;
 
-       if(vertical_only) dx_result = 0;
-       if(horizontal_only) dy_result = 0;
-
-
-
-// Write results
-       if(tracking_type == MotionScan::SAVE)
-       {
-               char string[BCTEXTLEN];
-               sprintf(string, 
-                       "%s%06d", 
-                       MOTION_FILE, 
-                       source_position);
-               FILE *output = fopen(string, "w");
-               if(output)
-               {
-                       fprintf(output, 
-                               "%d %d\n",
-                               dx_result,
-                               dy_result);
-                       fclose(output);
-               }
-               else
-               {
-                       printf("MotionScan::scan_frame %d: save coordinate failed", __LINE__);
-               }
-       }
-
-// printf("MotionScan::scan_frame %d dx=%.2f dy=%.2f\n", 
-// __LINE__,
-// (float)this->dx_result / OVERSAMPLE,
-// (float)this->dy_result / OVERSAMPLE);
+//printf("MotionScan::scan_frame %d dx=%.2f dy=%.2f\n",
+// __LINE__, (float)this->dx_result / OVERSAMPLE, (float)this->dy_result / OVERSAMPLE);
 }
 
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
 int64_t MotionScan::get_cache(int x, int y)
 {
        int64_t result = -1;
        cache_lock->lock("MotionScan::get_cache");
-       for(int i = 0; i < cache.total; i++)
-       {
+       for( int i = 0; i < cache.total; i++ ) {
                MotionScanCache *ptr = cache.values[i];
-               if(ptr->x == x && ptr->y == y)
-               {
+               if( ptr->x == x && ptr->y == y ) {
                        result = ptr->difference;
                        break;
                }
@@ -793,28 +530,21 @@ void MotionScan::put_cache(int x, int y, int64_t difference)
        cache_lock->unlock();
 }
 
-
-
-#define ABS_DIFF(type, temp_type, multiplier, components) \
-{ \
+#define ABS_DIFF(model, type, temp_type, multiplier, components) case model: { \
        temp_type result_temp = 0; \
-       for(int i = 0; i < h; i++) \
-       { \
+       for( int i = 0; i < h; i++ ) { \
                type *prev_row = (type*)prev_ptr; \
                type *current_row = (type*)current_ptr; \
-               for(int j = 0; j < w; j++) \
-               { \
-                       for(int k = 0; k < 3; k++) \
-                       { \
+               for( int j = 0; j < w; j++ ) { \
+                       for( int k = 0; k < 3; k++ ) { \
                                temp_type difference; \
                                difference = *prev_row++ - *current_row++; \
-                               if(difference < 0) \
+                               if( difference < 0 ) \
                                        result_temp -= difference; \
                                else \
                                        result_temp += difference; \
                        } \
-                       if(components == 4) \
-                       { \
+                       if( components == 4 ) { \
                                prev_row++; \
                                current_row++; \
                        } \
@@ -823,67 +553,41 @@ void MotionScan::put_cache(int x, int y, int64_t difference)
                current_ptr += row_bytes; \
        } \
        result = (int64_t)(result_temp * multiplier); \
-}
+} break
 
 int64_t MotionScan::abs_diff(unsigned char *prev_ptr,
-       unsigned char *current_ptr,
-       int row_bytes,
-       int w,
-       int h,
-       int color_model)
+               unsigned char *current_ptr, int row_bytes, int w,
+               int h, int color_model)
 {
        int64_t result = 0;
-       switch(color_model)
-       {
-               case BC_RGB888:
-                       ABS_DIFF(unsigned char, int64_t, 1, 3)
-                       break;
-               case BC_RGBA8888:
-                       ABS_DIFF(unsigned char, int64_t, 1, 4)
-                       break;
-               case BC_RGB_FLOAT:
-                       ABS_DIFF(float, double, 0x10000, 3)
-                       break;
-               case BC_RGBA_FLOAT:
-                       ABS_DIFF(float, double, 0x10000, 4)
-                       break;
-               case BC_YUV888:
-                       ABS_DIFF(unsigned char, int64_t, 1, 3)
-                       break;
-               case BC_YUVA8888:
-                       ABS_DIFF(unsigned char, int64_t, 1, 4)
-                       break;
-               case BC_YUV161616:
-                       ABS_DIFF(uint16_t, int64_t, 1, 3)
-                       break;
-               case BC_YUVA16161616:
-                       ABS_DIFF(uint16_t, int64_t, 1, 4)
-                       break;
+       switch( color_model ) {
+       ABS_DIFF(BC_RGB888, unsigned char, int64_t, 1, 3);
+       ABS_DIFF(BC_RGBA8888, unsigned char, int64_t, 1, 4);
+       ABS_DIFF(BC_RGB_FLOAT, float, double, 0x10000, 3);
+       ABS_DIFF(BC_RGBA_FLOAT, float, double, 0x10000, 4);
+       ABS_DIFF(BC_YUV888, unsigned char, int64_t, 1, 3);
+       ABS_DIFF(BC_YUVA8888, unsigned char, int64_t, 1, 4);
+       ABS_DIFF(BC_YUV161616, uint16_t, int64_t, 1, 3);
+       ABS_DIFF(BC_YUVA16161616, uint16_t, int64_t, 1, 4);
        }
        return result;
 }
 
-
-
-#define ABS_DIFF_SUB(type, temp_type, multiplier, components) \
-{ \
+#define ABS_DIFF_SUB(model, type, temp_type, multiplier, components) case model: { \
        temp_type result_temp = 0; \
        temp_type y2_fraction = sub_y * 0x100 / OVERSAMPLE; \
        temp_type y1_fraction = 0x100 - y2_fraction; \
        temp_type x2_fraction = sub_x * 0x100 / OVERSAMPLE; \
        temp_type x1_fraction = 0x100 - x2_fraction; \
-       for(int i = 0; i < h_sub; i++) \
-       { \
+       for( int i = 0; i < h_sub; i++ ) { \
                type *prev_row1 = (type*)prev_ptr; \
                type *prev_row2 = (type*)prev_ptr + components; \
                type *prev_row3 = (type*)(prev_ptr + row_bytes); \
                type *prev_row4 = (type*)(prev_ptr + row_bytes) + components; \
                type *current_row = (type*)current_ptr; \
-               for(int j = 0; j < w_sub; j++) \
-               { \
+               for( int j = 0; j < w_sub; j++ ) { \
 /* Scan each component */ \
-                       for(int k = 0; k < 3; k++) \
-                       { \
+                       for( int k = 0; k < 3; k++ ) { \
                                temp_type difference; \
                                temp_type prev_value = \
                                        (*prev_row1++ * x1_fraction * y1_fraction + \
@@ -893,15 +597,14 @@ int64_t MotionScan::abs_diff(unsigned char *prev_ptr,
                                        0x100 / 0x100; \
                                temp_type current_value = *current_row++; \
                                difference = prev_value - current_value; \
-                               if(difference < 0) \
+                               if( difference < 0 ) \
                                        result_temp -= difference; \
                                else \
                                        result_temp += difference; \
                        } \
  \
 /* skip alpha */ \
-                       if(components == 4) \
-                       { \
+                       if( components == 4 ) { \
                                prev_row1++; \
                                prev_row2++; \
                                prev_row3++; \
@@ -913,58 +616,30 @@ int64_t MotionScan::abs_diff(unsigned char *prev_ptr,
                current_ptr += row_bytes; \
        } \
        result = (int64_t)(result_temp * multiplier); \
-}
-
-
-
+} break
 
 int64_t MotionScan::abs_diff_sub(unsigned char *prev_ptr,
-       unsigned char *current_ptr,
-       int row_bytes,
-       int w,
-       int h,
-       int color_model,
-       int sub_x,
-       int sub_y)
+               unsigned char *current_ptr, int row_bytes,
+               int w, int h, int color_model, int sub_x,
+               int sub_y)
 {
        int h_sub = h - 1;
        int w_sub = w - 1;
        int64_t result = 0;
 
-       switch(color_model)
-       {
-               case BC_RGB888:
-                       ABS_DIFF_SUB(unsigned char, int64_t, 1, 3)
-                       break;
-               case BC_RGBA8888:
-                       ABS_DIFF_SUB(unsigned char, int64_t, 1, 4)
-                       break;
-               case BC_RGB_FLOAT:
-                       ABS_DIFF_SUB(float, double, 0x10000, 3)
-                       break;
-               case BC_RGBA_FLOAT:
-                       ABS_DIFF_SUB(float, double, 0x10000, 4)
-                       break;
-               case BC_YUV888:
-                       ABS_DIFF_SUB(unsigned char, int64_t, 1, 3)
-                       break;
-               case BC_YUVA8888:
-                       ABS_DIFF_SUB(unsigned char, int64_t, 1, 4)
-                       break;
-               case BC_YUV161616:
-                       ABS_DIFF_SUB(uint16_t, int64_t, 1, 3)
-                       break;
-               case BC_YUVA16161616:
-                       ABS_DIFF_SUB(uint16_t, int64_t, 1, 4)
-                       break;
+       switch( color_model ) {
+       ABS_DIFF_SUB(BC_RGB888, unsigned char, int64_t, 1, 3);
+       ABS_DIFF_SUB(BC_RGBA8888, unsigned char, int64_t, 1, 4);
+       ABS_DIFF_SUB(BC_RGB_FLOAT, float, double, 0x10000, 3);
+       ABS_DIFF_SUB(BC_RGBA_FLOAT, float, double, 0x10000, 4);
+       ABS_DIFF_SUB(BC_YUV888, unsigned char, int64_t, 1, 3);
+       ABS_DIFF_SUB(BC_YUVA8888, unsigned char, int64_t, 1, 4);
+       ABS_DIFF_SUB(BC_YUV161616, uint16_t, int64_t, 1, 3);
+       ABS_DIFF_SUB(BC_YUVA16161616, uint16_t, int64_t, 1, 4);
        }
        return result;
 }
 
-
-
-
-
 MotionScanCache::MotionScanCache(int x, int y, int64_t difference)
 {
        this->x = x;
@@ -972,61 +647,38 @@ MotionScanCache::MotionScanCache(int x, int y, int64_t difference)
        this->difference = difference;
 }
 
-
-
-void MotionScan::clamp_scan(int w, 
-       int h, 
-       int *block_x1,
-       int *block_y1,
-       int *block_x2,
-       int *block_y2,
-       int *scan_x1,
-       int *scan_y1,
-       int *scan_x2,
-       int *scan_y2,
-       int use_absolute)
+void MotionScan::clamp_scan(int w, int h,
+               int *block_x1, int *block_y1, int *block_x2,
+               int *block_y2, int *scan_x1, int *scan_y1,
+               int *scan_x2, int *scan_y2, int use_absolute)
 {
-// printf("MotionMain::clamp_scan 1 w=%d h=%d block=%d %d %d %d scan=%d %d %d %d absolute=%d\n",
-// w,
-// h,
-// *block_x1,
-// *block_y1,
-// *block_x2,
-// *block_y2,
-// *scan_x1,
-// *scan_y1,
-// *scan_x2,
-// *scan_y2,
+//printf("MotionMain::clamp_scan 1 w=%d h=%d block=%d %d %d %d scan=%d %d %d %d absolute=%d\n",
+// w, h, *block_x1, *block_y1, *block_x2, *block_y2, *scan_x1, *scan_y1, *scan_x2, *scan_y2,
 // use_absolute);
 
-       if(use_absolute)
-       {
+       if( use_absolute ) {
 // Limit size of scan area
 // Used for drawing vectors
 // scan is always out of range before block.
-               if(*scan_x1 < 0)
-               {
+               if( *scan_x1 < 0 ) {
 //                     int difference = -*scan_x1;
 //                     *block_x1 += difference;
                        *scan_x1 = 0;
                }
 
-               if(*scan_y1 < 0)
-               {
+               if( *scan_y1 < 0 ) {
 //                     int difference = -*scan_y1;
 //                     *block_y1 += difference;
                        *scan_y1 = 0;
                }
 
-               if(*scan_x2 > w)
-               {
+               if( *scan_x2 > w ) {
                        int difference = *scan_x2 - w;
 //                     *block_x2 -= difference;
                        *scan_x2 -= difference;
                }
 
-               if(*scan_y2 > h)
-               {
+               if( *scan_y2 > h ) {
                        int difference = *scan_y2 - h;
 //                     *block_y2 -= difference;
                        *scan_y2 -= difference;
@@ -1037,44 +689,38 @@ void MotionScan::clamp_scan(int w,
                CLAMP(*scan_x2, 0, w);
                CLAMP(*scan_y2, 0, h);
        }
-       else
-       {
+       else {
 // Limit range of upper left block coordinates
 // Used for motion tracking
-               if(*scan_x1 < 0)
-               {
+               if( *scan_x1 < 0 ) {
                        int difference = -*scan_x1;
 //                     *block_x1 += difference;
                        *scan_x2 += difference;
                        *scan_x1 = 0;
                }
 
-               if(*scan_y1 < 0)
-               {
+               if( *scan_y1 < 0 ) {
                        int difference = -*scan_y1;
 //                     *block_y1 += difference;
                        *scan_y2 += difference;
                        *scan_y1 = 0;
                }
 
-               if(*scan_x2 - *block_x1 + *block_x2 > w)
-               {
+               if( *scan_x2 - *block_x1 + *block_x2 > w ) {
                        int difference = *scan_x2 - *block_x1 + *block_x2 - w;
                        *scan_x2 -= difference;
 //                     *block_x2 -= difference;
                }
 
-               if(*scan_y2 - *block_y1 + *block_y2 > h)
-               {
+               if( *scan_y2 - *block_y1 + *block_y2 > h ) {
                        int difference = *scan_y2 - *block_y1 + *block_y2 - h;
                        *scan_y2 -= difference;
 //                     *block_y2 -= difference;
                }
-
-//             CLAMP(*scan_x1, 0, w - (*block_x2 - *block_x1));
-//             CLAMP(*scan_y1, 0, h - (*block_y2 - *block_y1));
-//             CLAMP(*scan_x2, 0, w - (*block_x2 - *block_x1));
-//             CLAMP(*scan_y2, 0, h - (*block_y2 - *block_y1));
+//              CLAMP(*scan_x1, 0, w - (*block_x2 - *block_x1));
+//              CLAMP(*scan_y1, 0, h - (*block_y2 - *block_y1));
+//              CLAMP(*scan_x2, 0, w - (*block_x2 - *block_x1));
+//              CLAMP(*scan_y2, 0, h - (*block_y2 - *block_y1));
        }
 
 // Sanity checks which break the calculation but should never happen if the
@@ -1084,19 +730,7 @@ void MotionScan::clamp_scan(int w,
        CLAMP(*block_y1, 0, h);
        CLAMP(*block_y2, 0, h);
 
-// printf("MotionMain::clamp_scan 2 w=%d h=%d block=%d %d %d %d scan=%d %d %d %d absolute=%d\n",
-// w,
-// h,
-// *block_x1,
-// *block_y1,
-// *block_x2,
-// *block_y2,
-// *scan_x1,
-// *scan_y1,
-// *scan_x2,
-// *scan_y2,
+//printf("MotionMain::clamp_scan 2 w=%d h=%d block=%d %d %d %d scan=%d %d %d %d absolute=%d\n",
+// w, h, *block_x1, *block_y1, *block_x2, *block_y2, *scan_x1, *scan_y1, *scan_x2, *scan_y2,
 // use_absolute);
 }
-
-
-
index 8e756ee38e2297f6839943453651284e4d15afbf..32aeb5aa765ab81c85b3d8ad84d55b72659a6a1b 100644 (file)
@@ -2,21 +2,21 @@
 /*
  * CINELERRA
  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
- * 
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
- * 
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- * 
+ *
  */
 
 #ifndef MOTIONSCAN_H
@@ -32,7 +32,6 @@
 class MotionScan;
 
 #define OVERSAMPLE 4
-#define MOTION_FILE "/tmp/m"
 
 class MotionScanPackage : public LoadPackage
 {
@@ -90,7 +89,7 @@ public:
 class MotionScan : public LoadServer
 {
 public:
-       MotionScan(int total_clients, 
+       MotionScan(int total_clients,
                int total_packages);
        ~MotionScan();
 
@@ -103,85 +102,46 @@ public:
        void set_test_match(int value);
 
 // Invoke the motion engine for a search
-// Frame before motion
-       void scan_frame(VFrame *previous_frame,
-// Frame after motion
-               VFrame *current_frame,
-               int global_range_w,
-               int global_range_h,
-               int global_block_w,
-               int global_block_h,
-               double block_x,
-               double block_y,
-               int frame_type,
-               int tracking_type,
-               int action_type,
-               int horizontal_only,
-               int vertical_only,
-               int source_position,
-               int total_steps,
-               int total_dx,
-               int total_dy,
-               int global_origin_x,
-               int global_origin_y);
+       void scan_frame(VFrame *previous_frame, // Frame before motion
+               VFrame *current_frame, // Frame after motion
+               int global_range_w, int global_range_h, int global_block_w, int global_block_h,
+               double block_x, double block_y, int frame_type, int tracking_type, int action_type,
+               int horizontal_only, int vertical_only, int source_position, int total_steps,
+               int total_dx, int total_dy, int global_origin_x, int global_origin_y,
+               int load_ok=0, int load_dx=0, int load_dy=0);
        int64_t get_cache(int x, int y);
        void put_cache(int x, int y, int64_t difference);
 
-       static int64_t abs_diff(unsigned char *prev_ptr,
-               unsigned char *current_ptr,
-               int row_bytes,
-               int w,
-               int h,
-               int color_model);
-       static int64_t abs_diff_sub(unsigned char *prev_ptr,
-               unsigned char *current_ptr,
-               int row_bytes,
-               int w,
-               int h,
-               int color_model,
-               int sub_x,
-               int sub_y);
-
-
-       static void clamp_scan(int w, 
-               int h, 
-               int *block_x1,
-               int *block_y1,
-               int *block_x2,
-               int *block_y2,
-               int *scan_x1,
-               int *scan_y1,
-               int *scan_x2,
-               int *scan_y2,
+       static int64_t abs_diff(unsigned char *prev_ptr, unsigned char *current_ptr,
+               int row_bytes, int w, int h, int color_model);
+       static int64_t abs_diff_sub(unsigned char *prev_ptr, unsigned char *current_ptr,
+               int row_bytes, int w, int h, int color_model, int sub_x, int sub_y);
+
+       static void clamp_scan(int w, int h,
+               int *block_x1, int *block_y1, int *block_x2, int *block_y2,
+               int *scan_x1, int *scan_y1, int *scan_x2, int *scan_y2,
                int use_absolute);
 
-// Change between previous frame and current frame multiplied by 
+// Change between previous frame and current frame multiplied by
 // OVERSAMPLE
-       int dx_result;
-       int dy_result;
+       int dx_result, dy_result;
 
-       enum
-       {
-// action_type
+       enum { // action_type
                TRACK,
                STABILIZE,
                TRACK_PIXEL,
                STABILIZE_PIXEL,
                NOTHING
        };
-       
-       enum
-       {
-// tracking_type
+
+       enum { // tracking_type
                CALCULATE,
                SAVE,
                LOAD,
                NO_CALCULATE
        };
-       
-       enum
-       {
-// frame_type
+
+       enum { // frame_type
                TRACK_SINGLE,
                TRACK_PREVIOUS,
                PREVIOUS_SAME_BLOCK
index 3c574345a85793601bd075bfee322055f57839ca..5d749d36503f99791811c1d4926913541ebc5bff 100644 (file)
 #ifndef MOTIONSCAN_INC
 #define MOTIONSCAN_INC
 
-
-
-
 class MotionScan;
 
-
-
 #endif
-
-
-
index 02972d15b1cbef590c20c6cb537b07387d1f1a96..7d6fa43839ec6adb371259ce497b6223d437139a 100644 (file)
 
 #include "bcdisplayinfo.h"
 #include "clip.h"
+#include "edl.h"
+#include "fonts.h"
+#include "edlsession.h"
 #include "language.h"
 #include "motion.h"
 #include "motionscan.h"
 #include "motionwindow.h"
-
-
-
-
-
-
-
-
-
+#include "mwindow.h"
+#include "pluginserver.h"
 
 MotionWindow::MotionWindow(MotionMain *plugin)
  : PluginClientWindow(plugin, 600, 650, 600, 650, 0)
@@ -51,50 +47,34 @@ void MotionWindow::create_objects()
        int x2 = 310;
        BC_Title *title;
 
-
-
-       add_subwindow(global = new MotionGlobal(plugin,
-               this,
-               x1,
-               y));
-
-       add_subwindow(rotate = new MotionRotate(plugin,
-               this,
-               x2,
-               y));
+       add_subwindow(global = new MotionGlobal(plugin, this, x1, y));
+       add_subwindow(rotate = new MotionRotate(plugin, this, x2, y));
        y += 50;
 
-       add_subwindow(title = new BC_Title(x1,
-               y,
+       add_subwindow(title = new BC_Title(x1, y,
                _("Translation search radius:\n(W/H Percent of image)")));
        add_subwindow(global_range_w = new GlobalRange(plugin,
-               x1 + title->get_w() + 10,
-               y,
+               x1 + title->get_w() + 10, y,
                &plugin->config.global_range_w));
        add_subwindow(global_range_h = new GlobalRange(plugin,
-               x1 + title->get_w() + 10 + global_range_w->get_w(),
-               y,
+               x1 + title->get_w() + 10 + global_range_w->get_w(), y,
                &plugin->config.global_range_h));
 
-       add_subwindow(title = new BC_Title(x2,
-               y,
+       add_subwindow(title = new BC_Title(x2, y,
                _("Rotation search radius:\n(Degrees)")));
        add_subwindow(rotation_range = new RotationRange(plugin,
-               x2 + title->get_w() + 10,
-               y));
+               x2 + title->get_w() + 10, y));
 
        y += 50;
-       add_subwindow(title = new BC_Title(x1,
-               y,
+       add_subwindow(title = new BC_Title(x1, y,
                _("Translation block size:\n(W/H Percent of image)")));
-       add_subwindow(global_block_w = new BlockSize(plugin,
-               x1 + title->get_w() + 10,
-               y,
+       add_subwindow(global_block_w =
+               new BlockSize(plugin, x1 + title->get_w() + 10, y,
                &plugin->config.global_block_w));
-       add_subwindow(global_block_h = new BlockSize(plugin,
-               x1 + title->get_w() + 10 + global_block_w->get_w(),
-               y,
-               &plugin->config.global_block_h));
+       add_subwindow(global_block_h =
+               new BlockSize(plugin, x1 + title->get_w() + 10 +
+                       global_block_w->get_w(), y,
+                       &plugin->config.global_block_h));
 
 //     add_subwindow(title = new BC_Title(x2,
 //             y,
@@ -110,17 +90,13 @@ void MotionWindow::create_objects()
 
        y += 50;
        add_subwindow(title = new BC_Title(x1, y, _("Translation search steps:")));
-       add_subwindow(global_search_positions = new GlobalSearchPositions(plugin,
-               x1 + title->get_w() + 10,
-               y,
-               80));
+       add_subwindow(global_search_positions =
+               new GlobalSearchPositions(plugin, x1 + title->get_w() + 10, y, 80));
        global_search_positions->create_objects();
 
        add_subwindow(title = new BC_Title(x2, y, _("Rotation search steps:")));
-       add_subwindow(rotation_search_positions = new RotationSearchPositions(plugin,
-               x2 + title->get_w() + 10,
-               y,
-               80));
+       add_subwindow(rotation_search_positions =
+               new RotationSearchPositions(plugin, x2 + title->get_w() + 10, y, 80));
        rotation_search_positions->create_objects();
 
        y += 50;
@@ -133,48 +109,35 @@ void MotionWindow::create_objects()
 
        y += 40;
        add_subwindow(title = new BC_Title(x, y + 10, _("Block X:")));
-       add_subwindow(block_x = new MotionBlockX(plugin,
-               this,
-               x + title->get_w() + 10,
-               y));
-       add_subwindow(block_x_text = new MotionBlockXText(plugin,
-               this,
-               x + title->get_w() + 10 + block_x->get_w() + 10,
-               y + 10));
+       add_subwindow(block_x =
+               new MotionBlockX(plugin, this, x + title->get_w() + 10, y));
+       add_subwindow(block_x_text =
+               new MotionBlockXText(plugin, this,
+                       x + title->get_w() + 10 + block_x->get_w() + 10, y + 10));
 
        add_subwindow(title = new BC_Title(x2, y, _("Rotation center:")));
-       add_subwindow(rotation_center = new RotationCenter(plugin,
-               x2 + title->get_w() + 10,
-               y));
-
-
+       add_subwindow(rotation_center =
+               new RotationCenter(plugin, x2 + title->get_w() + 10, y));
 
        int y1 = y;
        y += 50;
        add_subwindow(title = new BC_Title(x2, y + 10, _("Maximum angle offset:")));
-       add_subwindow(rotate_magnitude = new MotionRMagnitude(plugin,
-               x2 + title->get_w() + 10,
-               y));
+       add_subwindow(rotate_magnitude =
+               new MotionRMagnitude(plugin, x2 + title->get_w() + 10, y));
 
        y += 40;
        add_subwindow(title = new BC_Title(x2, y + 10, _("Rotation settling speed:")));
-       add_subwindow(rotate_return_speed = new MotionRReturnSpeed(plugin,
-               x2 + title->get_w() + 10,
-               y));
-
-
+       add_subwindow(rotate_return_speed =
+               new MotionRReturnSpeed(plugin, x2 + title->get_w() + 10, y));
 
        y = y1;
        y += 40;
        add_subwindow(title = new BC_Title(x, y + 10, _("Block Y:")));
-       add_subwindow(block_y = new MotionBlockY(plugin,
-               this,
-               x + title->get_w() + 10,
-               y));
-       add_subwindow(block_y_text = new MotionBlockYText(plugin,
-               this,
-               x + title->get_w() + 10 + block_y->get_w() + 10,
-               y + 10));
+       add_subwindow(block_y =
+               new MotionBlockY(plugin, this, x + title->get_w() + 10, y));
+       add_subwindow(block_y_text =
+               new MotionBlockYText(plugin, this,
+                       x + title->get_w() + 10 + block_y->get_w() + 10, y + 10));
 
        y += 50;
        add_subwindow(title = new BC_Title(x, y + 10, _("Maximum absolute offset:")));
@@ -184,121 +147,99 @@ void MotionWindow::create_objects()
 
        y += 40;
        add_subwindow(title = new BC_Title(x, y + 10, _("Motion settling speed:")));
-       add_subwindow(return_speed = new MotionReturnSpeed(plugin,
-               x + title->get_w() + 10,
-               y));
-
-
+       add_subwindow(return_speed =
+               new MotionReturnSpeed(plugin, x + title->get_w() + 10, y));
 
        y += 40;
-       add_subwindow(vectors = new MotionDrawVectors(plugin,
-               this,
-               x,
-               y));
+       add_subwindow(vectors = new MotionDrawVectors(plugin, this, x, y));
 
+       add_subwindow(title = new BC_Title(x2, y, _("Tracking file:")));
+       add_subwindow(tracking_file = new MotionTrackingFile(plugin,
+               plugin->config.tracking_file, this, x2+title->get_w() + 20, y));
 
        y += 40;
-       add_subwindow(track_single = new TrackSingleFrame(plugin,
-               this,
-               x,
-               y));
-       add_subwindow(title = new BC_Title(x + track_single->get_w() + 20,
-               y,
-               _("Frame number:")));
-       add_subwindow(track_frame_number = new TrackFrameNumber(plugin,
-               this,
-               x + track_single->get_w() + title->get_w() + 20,
-               y));
-       add_subwindow(addtrackedframeoffset = new AddTrackedFrameOffset(plugin,
-               this,
-               x + track_single->get_w() + title->get_w() + 20 + track_frame_number->get_w(),
-               y));
-
+       x1 = x;  y1 = y;
+       add_subwindow(track_single =
+               new TrackSingleFrame(plugin, this, x1, y1));
+       add_subwindow(title =
+               new BC_Title(x1 += track_single->get_w() + 20, y1, _("Frame number:")));
+       add_subwindow(track_frame_number =
+               new TrackFrameNumber(plugin, this, x1 += title->get_w(), y1));
+       add_subwindow(addtrackedframeoffset =
+               new AddTrackedFrameOffset(plugin, this, x1, y1+=track_frame_number->get_h()));
+       int pef = client->server->mwindow->edl->session->video_every_frame;
+       add_subwindow(pef_title = new BC_Title(x1, y1+=addtrackedframeoffset->get_h() + 5,
+               !pef ?  _("For best results\n"
+                               " Set: Play every frame\n"
+                               " Preferences-> Playback-> Video Out") :
+                       _("Currently using: Play every frame"), MEDIUMFONT,
+               !pef ? RED : GREEN));
 
        y += 20;
-       add_subwindow(track_previous = new TrackPreviousFrame(plugin,
-               this,
-               x,
-               y));
+       add_subwindow(track_previous =
+               new TrackPreviousFrame(plugin, this, x, y));
 
        y += 20;
-       add_subwindow(previous_same = new PreviousFrameSameBlock(plugin,
-               this,
-               x,
-               y));
+       add_subwindow(previous_same =
+               new PreviousFrameSameBlock(plugin, this, x, y));
 
        y += 40;
        y1 = y;
        add_subwindow(title = new BC_Title(x, y, _("Master layer:")));
        add_subwindow(master_layer = new MasterLayer(plugin,
-               this,
-               x + title->get_w() + 10,
-               y));
+               this, x + title->get_w() + 10, y));
        master_layer->create_objects();
        y += 30;
 
-
        add_subwindow(title = new BC_Title(x, y, _("Action:")));
        add_subwindow(action_type = new ActionType(plugin,
-               this,
-               x + title->get_w() + 10,
-               y));
+               this, x + title->get_w() + 10, y));
        action_type->create_objects();
        y += 30;
 
-
-
-
        add_subwindow(title = new BC_Title(x, y, _("Calculation:")));
        add_subwindow(tracking_type = new TrackingType(plugin,
-               this,
-               x + title->get_w() + 10,
-               y));
+               this, x + title->get_w() + 10, y));
        tracking_type->create_objects();
 
-
-
        show_window(1);
 }
 
 void MotionWindow::update_mode()
 {
        global_range_w->update(plugin->config.global_range_w,
-               MIN_RADIUS,
-               MAX_RADIUS);
+               MIN_RADIUS, MAX_RADIUS);
        global_range_h->update(plugin->config.global_range_h,
-               MIN_RADIUS,
-               MAX_RADIUS);
+               MIN_RADIUS, MAX_RADIUS);
        rotation_range->update(plugin->config.rotation_range,
-               MIN_ROTATION,
-               MAX_ROTATION);
+               MIN_ROTATION, MAX_ROTATION);
        vectors->update(plugin->config.draw_vectors);
+       tracking_file->update(plugin->config.tracking_file);
        global->update(plugin->config.global);
        rotate->update(plugin->config.rotate);
        addtrackedframeoffset->update(plugin->config.addtrackedframeoffset);
 }
 
+MotionTrackingFile::MotionTrackingFile(MotionMain *plugin,
+       const char *filename, MotionWindow *gui, int x, int y)
+ : BC_TextBox(x, y, 150, 1, filename)
+{
+       this->plugin = plugin;
+       this->gui = gui;
+};
 
-
-
-
-
-
-
-
-
-
+int MotionTrackingFile::handle_event()
+{
+       strcpy(plugin->config.tracking_file, get_text());
+       plugin->send_configure_change();
+       return 1;
+}
 
 
 GlobalRange::GlobalRange(MotionMain *plugin,
-       int x,
-       int y,
-       int *value)
- : BC_IPot(x,
-               y,
-               (int64_t)*value,
-               (int64_t)MIN_RADIUS,
-               (int64_t)MAX_RADIUS)
+       int x, int y, int *value)
+ : BC_IPot(x, y, (int64_t)*value,
+       (int64_t)MIN_RADIUS, (int64_t)MAX_RADIUS)
 {
        this->plugin = plugin;
        this->value = value;
@@ -315,14 +256,9 @@ int GlobalRange::handle_event()
 
 
 
-RotationRange::RotationRange(MotionMain *plugin,
-       int x,
-       int y)
- : BC_IPot(x,
-               y,
-               (int64_t)plugin->config.rotation_range,
-               (int64_t)MIN_ROTATION,
-               (int64_t)MAX_ROTATION)
+RotationRange::RotationRange(MotionMain *plugin, int x, int y)
+ : BC_IPot(x, y, (int64_t)plugin->config.rotation_range,
+       (int64_t)MIN_ROTATION, (int64_t)MAX_ROTATION)
 {
        this->plugin = plugin;
 }
@@ -336,16 +272,9 @@ int RotationRange::handle_event()
 }
 
 
-
-
-RotationCenter::RotationCenter(MotionMain *plugin,
-       int x,
-       int y)
- : BC_IPot(x,
-               y,
-               (int64_t)plugin->config.rotation_center,
-               (int64_t)-MAX_ROTATION,
-               (int64_t)MAX_ROTATION)
+RotationCenter::RotationCenter(MotionMain *plugin, int x, int y)
+ : BC_IPot(x, y, (int64_t)plugin->config.rotation_center,
+       (int64_t)-MAX_ROTATION, (int64_t)MAX_ROTATION)
 {
        this->plugin = plugin;
 }
@@ -359,10 +288,6 @@ int RotationCenter::handle_event()
 }
 
 
-
-
-
-
 BlockSize::BlockSize(MotionMain *plugin,
        int x,
        int y,
@@ -386,26 +311,9 @@ int BlockSize::handle_event()
 }
 
 
-
-
-
-
-
-
-
-
-
-
-
 GlobalSearchPositions::GlobalSearchPositions(MotionMain *plugin,
-       int x,
-       int y,
-       int w)
- : BC_PopupMenu(x,
-       y,
-       w,
-       "",
-       1)
+       int x, int y, int w)
+ : BC_PopupMenu(x, y, w, "", 1)
 {
        this->plugin = plugin;
 }
@@ -438,20 +346,9 @@ int GlobalSearchPositions::handle_event()
 }
 
 
-
-
-
-
-
 RotationSearchPositions::RotationSearchPositions(MotionMain *plugin,
-       int x,
-       int y,
-       int w)
- : BC_PopupMenu(x,
-       y,
-       w,
-       "",
-       1)
+       int x, int y, int w)
+ : BC_PopupMenu(x, y, w, "", 1)
 {
        this->plugin = plugin;
 }
@@ -474,20 +371,9 @@ int RotationSearchPositions::handle_event()
 }
 
 
-
-
-
-
-
-
-MotionMagnitude::MotionMagnitude(MotionMain *plugin,
-       int x,
-       int y)
- : BC_IPot(x,
-               y,
-               (int64_t)plugin->config.magnitude,
-               (int64_t)0,
-               (int64_t)100)
+MotionMagnitude::MotionMagnitude(MotionMain *plugin, int x, int y)
+ : BC_IPot(x, y,
+       (int64_t)plugin->config.magnitude, (int64_t)0, (int64_t)100)
 {
        this->plugin = plugin;
 }
@@ -500,14 +386,9 @@ int MotionMagnitude::handle_event()
 }
 
 
-MotionReturnSpeed::MotionReturnSpeed(MotionMain *plugin,
-       int x,
-       int y)
- : BC_IPot(x,
-               y,
-               (int64_t)plugin->config.return_speed,
-               (int64_t)0,
-               (int64_t)100)
+MotionReturnSpeed::MotionReturnSpeed(MotionMain *plugin, int x, int y)
+ : BC_IPot(x, y,
+       (int64_t)plugin->config.return_speed, (int64_t)0, (int64_t)100)
 {
        this->plugin = plugin;
 }
@@ -522,12 +403,8 @@ int MotionReturnSpeed::handle_event()
 
 
 AddTrackedFrameOffset::AddTrackedFrameOffset(MotionMain *plugin,
-       MotionWindow *gui,
-       int x,
-       int y)
- : BC_CheckBox(x,
-       y,
-       plugin->config.addtrackedframeoffset,
+       MotionWindow *gui, int x, int y)
+ : BC_CheckBox(x, y, plugin->config.addtrackedframeoffset,
        _("Add (loaded) offset from tracked frame"))
 {
        this->plugin = plugin;
@@ -542,14 +419,9 @@ int AddTrackedFrameOffset::handle_event()
 }
 
 
-MotionRMagnitude::MotionRMagnitude(MotionMain *plugin,
-       int x,
-       int y)
- : BC_IPot(x,
-               y,
-               (int64_t)plugin->config.rotate_magnitude,
-               (int64_t)0,
-               (int64_t)90)
+MotionRMagnitude::MotionRMagnitude(MotionMain *plugin, int x, int y)
+ : BC_IPot(x, y,
+       (int64_t)plugin->config.rotate_magnitude, (int64_t)0, (int64_t)90)
 {
        this->plugin = plugin;
 }
@@ -563,14 +435,9 @@ int MotionRMagnitude::handle_event()
 
 
 
-MotionRReturnSpeed::MotionRReturnSpeed(MotionMain *plugin,
-       int x,
-       int y)
- : BC_IPot(x,
-               y,
-               (int64_t)plugin->config.rotate_return_speed,
-               (int64_t)0,
-               (int64_t)100)
+MotionRReturnSpeed::MotionRReturnSpeed(MotionMain *plugin, int x, int y)
+ : BC_IPot(x, y,
+       (int64_t)plugin->config.rotate_return_speed, (int64_t)0, (int64_t)100)
 {
        this->plugin = plugin;
 }
@@ -583,17 +450,9 @@ int MotionRReturnSpeed::handle_event()
 }
 
 
-
-
-
 MotionGlobal::MotionGlobal(MotionMain *plugin,
-       MotionWindow *gui,
-       int x,
-       int y)
- : BC_CheckBox(x,
-       y,
-       plugin->config.global,
-       _("Track translation"))
+       MotionWindow *gui, int x, int y)
+ : BC_CheckBox(x, y, plugin->config.global, _("Track translation"))
 {
        this->plugin = plugin;
        this->gui = gui;
@@ -607,13 +466,8 @@ int MotionGlobal::handle_event()
 }
 
 MotionRotate::MotionRotate(MotionMain *plugin,
-       MotionWindow *gui,
-       int x,
-       int y)
- : BC_CheckBox(x,
-       y,
-       plugin->config.rotate,
-       _("Track rotation"))
+       MotionWindow *gui, int x, int y)
+ : BC_CheckBox(x, y, plugin->config.rotate, _("Track rotation"))
 {
        this->plugin = plugin;
        this->gui = gui;
@@ -627,18 +481,9 @@ int MotionRotate::handle_event()
 }
 
 
-
-
-
 MotionBlockX::MotionBlockX(MotionMain *plugin,
-       MotionWindow *gui,
-       int x,
-       int y)
- : BC_FPot(x,
-       y,
-       plugin->config.block_x,
-       (float)0,
-       (float)100)
+       MotionWindow *gui, int x, int y)
+ : BC_FPot(x, y, plugin->config.block_x, (float)0, (float)100)
 {
        this->plugin = plugin;
        this->gui = gui;
@@ -653,8 +498,6 @@ int MotionBlockX::handle_event()
 }
 
 
-
-
 MotionBlockY::MotionBlockY(MotionMain *plugin,
        MotionWindow *gui,
        int x,
@@ -678,14 +521,8 @@ int MotionBlockY::handle_event()
 }
 
 MotionBlockXText::MotionBlockXText(MotionMain *plugin,
-       MotionWindow *gui,
-       int x,
-       int y)
- : BC_TextBox(x,
-       y,
-       75,
-       1,
-       (float)plugin->config.block_x)
+       MotionWindow *gui, int x, int y)
+ : BC_TextBox(x, y, 75, 1, (float)plugin->config.block_x)
 {
        this->plugin = plugin;
        this->gui = gui;
@@ -704,14 +541,8 @@ int MotionBlockXText::handle_event()
 
 
 MotionBlockYText::MotionBlockYText(MotionMain *plugin,
-       MotionWindow *gui,
-       int x,
-       int y)
- : BC_TextBox(x,
-       y,
-       75,
-       1,
-       (float)plugin->config.block_y)
+       MotionWindow *gui, int x, int y)
+ : BC_TextBox(x, y, 75, 1, (float)plugin->config.block_y)
 {
        this->plugin = plugin;
        this->gui = gui;
@@ -727,24 +558,8 @@ int MotionBlockYText::handle_event()
 }
 
 
-
-
-
-
-
-
-
-
-
-
-
-
-
-
 MotionDrawVectors::MotionDrawVectors(MotionMain *plugin,
-       MotionWindow *gui,
-       int x,
-       int y)
+       MotionWindow *gui, int x, int y)
  : BC_CheckBox(x,
        y,
        plugin->config.draw_vectors,
@@ -762,12 +577,6 @@ int MotionDrawVectors::handle_event()
 }
 
 
-
-
-
-
-
-
 TrackSingleFrame::TrackSingleFrame(MotionMain *plugin,
        MotionWindow *gui,
        int x,
@@ -791,13 +600,6 @@ int TrackSingleFrame::handle_event()
        return 1;
 }
 
-
-
-
-
-
-
-
 TrackFrameNumber::TrackFrameNumber(MotionMain *plugin,
        MotionWindow *gui,
        int x,
@@ -817,11 +619,6 @@ int TrackFrameNumber::handle_event()
 }
 
 
-
-
-
-
-
 TrackPreviousFrame::TrackPreviousFrame(MotionMain *plugin,
        MotionWindow *gui,
        int x,
@@ -845,12 +642,6 @@ int TrackPreviousFrame::handle_event()
 }
 
 
-
-
-
-
-
-
 PreviousFrameSameBlock::PreviousFrameSameBlock(MotionMain *plugin,
        MotionWindow *gui,
        int x,
@@ -874,12 +665,6 @@ int PreviousFrameSameBlock::handle_event()
 }
 
 
-
-
-
-
-
-
 MasterLayer::MasterLayer(MotionMain *plugin, MotionWindow *gui, int x, int y)
  : BC_PopupMenu(x,
        y,
@@ -923,12 +708,6 @@ int MasterLayer::calculate_w(MotionWindow *gui)
 }
 
 
-
-
-
-
-
-
 ActionType::ActionType(MotionMain *plugin, MotionWindow *gui, int x, int y)
  : BC_PopupMenu(x,
        y,
@@ -995,9 +774,6 @@ int ActionType::calculate_w(MotionWindow *gui)
 }
 
 
-
-
-
 TrackingType::TrackingType(MotionMain *plugin, MotionWindow *gui, int x, int y)
  : BC_PopupMenu(x,
        y,
@@ -1017,16 +793,16 @@ int TrackingType::handle_event()
 
 void TrackingType::create_objects()
 {
-       add_item(new BC_MenuItem(to_text(MotionScan::NO_CALCULATE)));
-       add_item(new BC_MenuItem(to_text(MotionScan::CALCULATE)));
        add_item(new BC_MenuItem(to_text(MotionScan::SAVE)));
        add_item(new BC_MenuItem(to_text(MotionScan::LOAD)));
+       add_item(new BC_MenuItem(to_text(MotionScan::CALCULATE)));
+       add_item(new BC_MenuItem(to_text(MotionScan::NO_CALCULATE)));
 }
 
 int TrackingType::from_text(char *text)
 {
-       if(!strcmp(text, _("Save coords to /tmp"))) return MotionScan::SAVE;
-       if(!strcmp(text, _("Load coords from /tmp"))) return MotionScan::LOAD;
+       if(!strcmp(text, _("Save coords to tracking file"))) return MotionScan::SAVE;
+       if(!strcmp(text, _("Load coords from tracking file"))) return MotionScan::LOAD;
        if(!strcmp(text, _("Recalculate"))) return MotionScan::CALCULATE;
        //if(!strcmp(text, _("Don't Calculate"))) return MotionScan::NO_CALCULATE;
        return MotionScan::NO_CALCULATE;
@@ -1036,15 +812,11 @@ char* TrackingType::to_text(int mode)
 {
        switch(mode)
        {
-               case MotionScan::SAVE:
-                       return _("Save coords to /tmp");
-               case MotionScan::LOAD:
-                       return _("Load coords from /tmp");
-               case MotionScan::CALCULATE:
-                       return _("Recalculate");
+               case MotionScan::SAVE:          return _("Save coords to tracking file");
+               case MotionScan::LOAD:          return _("Load coords from tracking file");
+               case MotionScan::CALCULATE:     return _("Recalculate");
                default:
-               case MotionScan::NO_CALCULATE:
-                       return _("Don't Calculate");
+               case MotionScan::NO_CALCULATE:  return _("Don't Calculate");
        }
 }
 
@@ -1059,14 +831,6 @@ int TrackingType::calculate_w(MotionWindow *gui)
 }
 
 
-
-
-
-
-
-
-
-
 TrackDirection::TrackDirection(MotionMain *plugin, MotionWindow *gui, int x, int y)
  : BC_PopupMenu(x,
        y,
index 4a66a1a6cbdee42e364f1f0f1a1985070b619bce..c1f83bd042a69a0aa86c13a55ecf60b639bd4884 100644 (file)
@@ -2,21 +2,21 @@
 /*
  * CINELERRA
  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
- * 
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
- * 
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- * 
+ *
  */
 
 #include "guicast.h"
@@ -78,10 +78,7 @@ public:
 class TrackSingleFrame : public BC_Radial
 {
 public:
-       TrackSingleFrame(MotionMain *plugin, 
-               MotionWindow *gui,
-               int x, 
-               int y);
+       TrackSingleFrame(MotionMain *plugin, MotionWindow *gui, int x, int y);
        int handle_event();
        MotionMain *plugin;
        MotionWindow *gui;
@@ -90,10 +87,7 @@ public:
 class TrackFrameNumber : public BC_TextBox
 {
 public:
-       TrackFrameNumber(MotionMain *plugin, 
-               MotionWindow *gui,
-               int x, 
-               int y);
+       TrackFrameNumber(MotionMain *plugin, MotionWindow *gui, int x, int y);
        int handle_event();
        MotionMain *plugin;
        MotionWindow *gui;
@@ -102,10 +96,7 @@ public:
 class TrackPreviousFrame : public BC_Radial
 {
 public:
-       TrackPreviousFrame(MotionMain *plugin, 
-               MotionWindow *gui,
-               int x, 
-               int y);
+       TrackPreviousFrame(MotionMain *plugin, MotionWindow *gui, int x, int y);
        int handle_event();
        MotionMain *plugin;
        MotionWindow *gui;
@@ -114,10 +105,7 @@ public:
 class PreviousFrameSameBlock : public BC_Radial
 {
 public:
-       PreviousFrameSameBlock(MotionMain *plugin, 
-               MotionWindow *gui,
-               int x, 
-               int y);
+       PreviousFrameSameBlock(MotionMain *plugin, MotionWindow *gui, int x, int y);
        int handle_event();
        MotionMain *plugin;
        MotionWindow *gui;
@@ -126,10 +114,7 @@ public:
 class GlobalRange : public BC_IPot
 {
 public:
-       GlobalRange(MotionMain *plugin, 
-               int x, 
-               int y,
-               int *value);
+       GlobalRange(MotionMain *plugin, int x, int y, int *value);
        int handle_event();
        MotionMain *plugin;
        int *value;
@@ -138,9 +123,7 @@ public:
 class RotationRange : public BC_IPot
 {
 public:
-       RotationRange(MotionMain *plugin, 
-               int x, 
-               int y);
+       RotationRange(MotionMain *plugin, int x, int y);
        int handle_event();
        MotionMain *plugin;
 };
@@ -148,9 +131,7 @@ public:
 class RotationCenter : public BC_IPot
 {
 public:
-       RotationCenter(MotionMain *plugin, 
-               int x, 
-               int y);
+       RotationCenter(MotionMain *plugin, int x, int y);
        int handle_event();
        MotionMain *plugin;
 };
@@ -158,10 +139,7 @@ public:
 class BlockSize : public BC_IPot
 {
 public:
-       BlockSize(MotionMain *plugin, 
-               int x, 
-               int y,
-               int *value);
+       BlockSize(MotionMain *plugin, int x, int y, int *value);
        int handle_event();
        MotionMain *plugin;
        int *value;
@@ -170,10 +148,7 @@ public:
 class MotionBlockX : public BC_FPot
 {
 public:
-       MotionBlockX(MotionMain *plugin, 
-               MotionWindow *gui,
-               int x, 
-               int y);
+       MotionBlockX(MotionMain *plugin, MotionWindow *gui, int x, int y);
        int handle_event();
        MotionWindow *gui;
        MotionMain *plugin;
@@ -182,10 +157,7 @@ public:
 class MotionBlockY : public BC_FPot
 {
 public:
-       MotionBlockY(MotionMain *plugin, 
-               MotionWindow *gui,
-               int x, 
-               int y);
+       MotionBlockY(MotionMain *plugin, MotionWindow *gui, int x, int y);
        int handle_event();
        MotionWindow *gui;
        MotionMain *plugin;
@@ -194,10 +166,7 @@ public:
 class MotionBlockXText : public BC_TextBox
 {
 public:
-       MotionBlockXText(MotionMain *plugin, 
-               MotionWindow *gui,
-               int x, 
-               int y);
+       MotionBlockXText(MotionMain *plugin, MotionWindow *gui, int x, int y);
        int handle_event();
        MotionWindow *gui;
        MotionMain *plugin;
@@ -206,10 +175,7 @@ public:
 class MotionBlockYText : public BC_TextBox
 {
 public:
-       MotionBlockYText(MotionMain *plugin, 
-               MotionWindow *gui,
-               int x, 
-               int y);
+       MotionBlockYText(MotionMain *plugin, MotionWindow *gui, int x, int y);
        int handle_event();
        MotionWindow *gui;
        MotionMain *plugin;
@@ -218,10 +184,7 @@ public:
 class GlobalSearchPositions : public BC_PopupMenu
 {
 public:
-       GlobalSearchPositions(MotionMain *plugin, 
-               int x, 
-               int y,
-               int w);
+       GlobalSearchPositions(MotionMain *plugin, int x, int y, int w);
        void create_objects();
        int handle_event();
        MotionMain *plugin;
@@ -230,10 +193,7 @@ public:
 class RotationSearchPositions : public BC_PopupMenu
 {
 public:
-       RotationSearchPositions(MotionMain *plugin, 
-               int x, 
-               int y,
-               int w);
+       RotationSearchPositions(MotionMain *plugin, int x, int y, int w);
        void create_objects();
        int handle_event();
        MotionMain *plugin;
@@ -242,9 +202,7 @@ public:
 class MotionMagnitude : public BC_IPot
 {
 public:
-       MotionMagnitude(MotionMain *plugin, 
-               int x, 
-               int y);
+       MotionMagnitude(MotionMain *plugin, int x, int y);
        int handle_event();
        MotionMain *plugin;
 };
@@ -252,9 +210,7 @@ public:
 class MotionRMagnitude : public BC_IPot
 {
 public:
-       MotionRMagnitude(MotionMain *plugin, 
-               int x, 
-               int y);
+       MotionRMagnitude(MotionMain *plugin, int x, int y);
        int handle_event();
        MotionMain *plugin;
 };
@@ -262,9 +218,7 @@ public:
 class MotionReturnSpeed : public BC_IPot
 {
 public:
-       MotionReturnSpeed(MotionMain *plugin, 
-               int x, 
-               int y);
+       MotionReturnSpeed(MotionMain *plugin, int x, int y);
        int handle_event();
        MotionMain *plugin;
 };
@@ -273,9 +227,7 @@ public:
 class MotionRReturnSpeed : public BC_IPot
 {
 public:
-       MotionRReturnSpeed(MotionMain *plugin, 
-               int x, 
-               int y);
+       MotionRReturnSpeed(MotionMain *plugin, int x, int y);
        int handle_event();
        MotionMain *plugin;
 };
@@ -284,10 +236,7 @@ public:
 class MotionDrawVectors : public BC_CheckBox
 {
 public:
-       MotionDrawVectors(MotionMain *plugin, 
-               MotionWindow *gui,
-               int x, 
-               int y);
+       MotionDrawVectors(MotionMain *plugin, MotionWindow *gui, int x, int y);
        int handle_event();
        MotionMain *plugin;
        MotionWindow *gui;
@@ -296,22 +245,26 @@ public:
 class AddTrackedFrameOffset : public BC_CheckBox
 {
 public:
-       AddTrackedFrameOffset(MotionMain *plugin, 
-               MotionWindow *gui,
-               int x, 
-               int y);
+       AddTrackedFrameOffset(MotionMain *plugin, MotionWindow *gui, int x, int y);
        int handle_event();
        MotionWindow *gui;
        MotionMain *plugin;
 };
 
+class MotionTrackingFile : public BC_TextBox
+{
+public:
+       MotionTrackingFile(MotionMain *plugin, const char *filename,
+               MotionWindow *gui, int x, int y);
+       int handle_event();
+       MotionMain *plugin;
+       MotionWindow *gui;
+};
+
 class MotionGlobal : public BC_CheckBox
 {
 public:
-       MotionGlobal(MotionMain *plugin, 
-               MotionWindow *gui,
-               int x, 
-               int y);
+       MotionGlobal(MotionMain *plugin, MotionWindow *gui, int x, int y);
        int handle_event();
        MotionWindow *gui;
        MotionMain *plugin;
@@ -320,10 +273,7 @@ public:
 class MotionRotate : public BC_CheckBox
 {
 public:
-       MotionRotate(MotionMain *plugin, 
-               MotionWindow *gui,
-               int x, 
-               int y);
+       MotionRotate(MotionMain *plugin, MotionWindow *gui, int x, int y);
        int handle_event();
        MotionWindow *gui;
        MotionMain *plugin;
@@ -361,6 +311,7 @@ public:
        MotionRReturnSpeed *rotate_return_speed;
        ActionType *action_type;
        MotionDrawVectors *vectors;
+       MotionTrackingFile *tracking_file;
        MotionGlobal *global;
        MotionRotate *rotate;
        AddTrackedFrameOffset *addtrackedframeoffset;
@@ -371,15 +322,9 @@ public:
        MasterLayer *master_layer;
        TrackingType *tracking_type;
        TrackDirection *track_direction;
+       BC_Title *pef_title;
 
        MotionMain *plugin;
 };
 
 
-
-
-
-
-
-
-
index 50404d715a5af075e55a3c087ca11a256d5887c5..2f867c5fc74bcbd03a1a1d1feb94227e517e602f 100644 (file)
@@ -23,6 +23,5 @@
 #define MOTIONWINDOW_INC
 
 class MotionWindow;
-class MotionThread;
 
 #endif