add xvid/flv formats, add motion plugin varients + save file, fix build bug
[goodguy/history.git] / cinelerra-5.1 / plugins / motion-hv / motion-hv.C
similarity index 60%
rename from cinelerra-5.1/plugins/motion.new/motion.C
rename to cinelerra-5.1/plugins/motion-hv/motion-hv.C
index 81d9705b93ff4c1fb36c5d9bb245090167c00fd7..3b0b128ab7e500e31baf8eab232b93f6ea6f4480 100644 (file)
@@ -1,7 +1,7 @@
 
 /*
  * CINELERRA
- * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
+ * Copyright (C) 2016 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
@@ -27,9 +27,9 @@
 #include "filexml.h"
 #include "keyframe.h"
 #include "language.h"
-#include "motion.h"
-#include "motionscan.h"
-#include "motionwindow.h"
+#include "motion-hv.h"
+#include "motionscan-hv.h"
+#include "motionwindow-hv.h"
 #include "mutex.h"
 #include "overlayframe.h"
 #include "rotateframe.h"
@@ -39,7 +39,7 @@
 #include <errno.h>
 #include <unistd.h>
 
-REGISTER_PLUGIN(MotionMain)
+REGISTER_PLUGIN(MotionHVMain)
 
 #undef DEBUG
 
@@ -49,7 +49,7 @@ REGISTER_PLUGIN(MotionMain)
 
 
 
-MotionConfig::MotionConfig()
+MotionHVConfig::MotionHVConfig()
 {
        global_range_w = 5;
        global_range_h = 5;
@@ -58,29 +58,29 @@ MotionConfig::MotionConfig()
        block_count = 1;
        global_block_w = MIN_BLOCK;
        global_block_h = MIN_BLOCK;
-       rotation_block_w = MIN_BLOCK;
-       rotation_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;
+//     global_positions = 256;
+//     rotate_positions = 4;
        magnitude = 100;
        rotate_magnitude = 90;
        return_speed = 0;
        rotate_return_speed = 0;
-       action_type = MotionScan::STABILIZE;
-       global = 1;
+       action_type = MotionHVScan::STABILIZE;
+//     global = 1;
        rotate = 1;
-       tracking_type = MotionScan::NO_CALCULATE;
+       tracking_type = MotionHVScan::NO_CALCULATE;
        draw_vectors = 1;
-       tracking_object = MotionScan::TRACK_SINGLE;
+       tracking_object = MotionHVScan::TRACK_SINGLE;
        track_frame = 0;
        bottom_is_master = 1;
        horizontal_only = 0;
        vertical_only = 0;
 }
 
-void MotionConfig::boundaries()
+void MotionHVConfig::boundaries()
 {
        CLAMP(global_range_w, MIN_RADIUS, MAX_RADIUS);
        CLAMP(global_range_h, MIN_RADIUS, MAX_RADIUS);
@@ -89,29 +89,29 @@ 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);
+//     CLAMP(rotation_block_w, MIN_BLOCK, MAX_BLOCK);
+//     CLAMP(rotation_block_h, MIN_BLOCK, MAX_BLOCK);
 }
 
-int MotionConfig::equivalent(MotionConfig &that)
+int MotionHVConfig::equivalent(MotionHVConfig &that)
 {
        return global_range_w == that.global_range_w &&
                global_range_h == that.global_range_h &&
                rotation_range == that.rotation_range &&
                rotation_center == that.rotation_center &&
                action_type == that.action_type &&
-               global == that.global &&
+//             global == that.global &&
                rotate == that.rotate &&
                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 &&
+//             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 &&
+//             global_positions == that.global_positions &&
+//             rotate_positions == that.rotate_positions &&
                magnitude == that.magnitude &&
                return_speed == that.return_speed &&
                rotate_return_speed == that.rotate_return_speed &&
@@ -123,26 +123,26 @@ int MotionConfig::equivalent(MotionConfig &that)
                vertical_only == that.vertical_only;
 }
 
-void MotionConfig::copy_from(MotionConfig &that)
+void MotionHVConfig::copy_from(MotionHVConfig &that)
 {
        global_range_w = that.global_range_w;
        global_range_h = that.global_range_h;
        rotation_range = that.rotation_range;
        rotation_center = that.rotation_center;
        action_type = that.action_type;
-       global = that.global;
+//     global = that.global;
        rotate = that.rotate;
        tracking_type = that.tracking_type;
        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_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;
+//     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;
@@ -154,14 +154,14 @@ void MotionConfig::copy_from(MotionConfig &that)
        vertical_only = that.vertical_only;
 }
 
-void MotionConfig::interpolate(MotionConfig &prev,
-       MotionConfig &next,
+void MotionHVConfig::interpolate(MotionHVConfig &prev,
+       MotionHVConfig &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);
+       //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;
@@ -169,17 +169,17 @@ void MotionConfig::interpolate(MotionConfig &prev,
        rotation_range = prev.rotation_range;
        rotation_center = prev.rotation_center;
        action_type = prev.action_type;
-       global = prev.global;
+//     global = prev.global;
        rotate = prev.rotate;
        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_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;
+//     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;
@@ -209,12 +209,12 @@ void MotionConfig::interpolate(MotionConfig &prev,
 
 
 
-MotionMain::MotionMain(PluginServer *server)
+MotionHVMain::MotionHVMain(PluginServer *server)
  : PluginVClient(server)
 {
        engine = 0;
        rotate_engine = 0;
-       motion_rotate = 0;
+//     motion_rotate = 0;
        total_dx = 0;
        total_dy = 0;
        total_angle = 0;
@@ -235,7 +235,7 @@ MotionMain::MotionMain(PluginServer *server)
        rotate_target_dst = 0;
 }
 
-MotionMain::~MotionMain()
+MotionHVMain::~MotionHVMain()
 {
 
        delete engine;
@@ -243,7 +243,7 @@ MotionMain::~MotionMain()
        delete [] search_area;
        delete temp_frame;
        delete rotate_engine;
-       delete motion_rotate;
+//     delete motion_rotate;
 
 
        delete prev_global_ref;
@@ -257,67 +257,67 @@ MotionMain::~MotionMain()
        delete rotate_target_dst;
 }
 
-const char* MotionMain::plugin_title() { return _("Motion"); }
-int MotionMain::is_realtime() { return 1; }
-int MotionMain::is_multichannel() { return 1; }
+const char* MotionHVMain::plugin_title() { return _("MotionHV"); }
+int MotionHVMain::is_realtime() { return 1; }
+int MotionHVMain::is_multichannel() { return 1; }
 
 
-NEW_WINDOW_MACRO(MotionMain, MotionWindow)
+NEW_WINDOW_MACRO(MotionHVMain, MotionHVWindow)
 
-LOAD_CONFIGURATION_MACRO(MotionMain, MotionConfig)
+LOAD_CONFIGURATION_MACRO(MotionHVMain, MotionHVConfig)
 
 
 
-void MotionMain::update_gui()
+void MotionHVMain::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();
+                       thread->window->lock_window("MotionHVMain::update_gui");
+
+//                     char string[BCTEXTLEN];
+//                     sprintf(string, "%d", config.global_positions);
+//                     ((MotionHVWindow*)thread->window)->global_search_positions->set_text(string);
+//                     sprintf(string, "%d", config.rotate_positions);
+//                     ((MotionHVWindow*)thread->window)->rotation_search_positions->set_text(string);
+
+                       ((MotionHVWindow*)thread->window)->global_block_w->update(config.global_block_w);
+                       ((MotionHVWindow*)thread->window)->global_block_h->update(config.global_block_h);
+//                     ((MotionHVWindow*)thread->window)->rotation_block_w->update(config.rotation_block_w);
+//                     ((MotionHVWindow*)thread->window)->rotation_block_h->update(config.rotation_block_h);
+                       ((MotionHVWindow*)thread->window)->block_x->update(config.block_x);
+                       ((MotionHVWindow*)thread->window)->block_y->update(config.block_y);
+                       ((MotionHVWindow*)thread->window)->block_x_text->update((float)config.block_x);
+                       ((MotionHVWindow*)thread->window)->block_y_text->update((float)config.block_y);
+                       ((MotionHVWindow*)thread->window)->magnitude->update(config.magnitude);
+                       ((MotionHVWindow*)thread->window)->return_speed->update(config.return_speed);
+                       ((MotionHVWindow*)thread->window)->rotate_magnitude->update(config.rotate_magnitude);
+                       ((MotionHVWindow*)thread->window)->rotate_return_speed->update(config.rotate_return_speed);
+                       ((MotionHVWindow*)thread->window)->rotation_range->update(config.rotation_range);
+                       ((MotionHVWindow*)thread->window)->rotation_center->update(config.rotation_center);
+
+
+                       ((MotionHVWindow*)thread->window)->track_single->update(config.tracking_object == MotionHVScan::TRACK_SINGLE);
+                       ((MotionHVWindow*)thread->window)->track_frame_number->update(config.track_frame);
+                       ((MotionHVWindow*)thread->window)->track_previous->update(config.tracking_object == MotionHVScan::TRACK_PREVIOUS);
+                       ((MotionHVWindow*)thread->window)->previous_same->update(config.tracking_object == MotionHVScan::PREVIOUS_SAME_BLOCK);
+                       if(config.tracking_object != MotionHVScan::TRACK_SINGLE)
+                               ((MotionHVWindow*)thread->window)->track_frame_number->disable();
                        else
-                               ((MotionWindow*)thread->window)->track_frame_number->enable();
+                               ((MotionHVWindow*)thread->window)->track_frame_number->enable();
 
-                       ((MotionWindow*)thread->window)->action_type->set_text(
+                       ((MotionHVWindow*)thread->window)->action_type->set_text(
                                ActionType::to_text(config.action_type));
-                       ((MotionWindow*)thread->window)->tracking_type->set_text(
+                       ((MotionHVWindow*)thread->window)->tracking_type->set_text(
                                TrackingType::to_text(config.tracking_type));
-                       ((MotionWindow*)thread->window)->track_direction->set_text(
+                       ((MotionHVWindow*)thread->window)->track_direction->set_text(
                                TrackDirection::to_text(config.horizontal_only, config.vertical_only));
-                       ((MotionWindow*)thread->window)->master_layer->set_text(
+                       ((MotionHVWindow*)thread->window)->master_layer->set_text(
                                MasterLayer::to_text(config.bottom_is_master));
 
 
-                       ((MotionWindow*)thread->window)->update_mode();
+                       ((MotionHVWindow*)thread->window)->update_mode();
                        thread->window->unlock_window();
                }
        }
@@ -326,7 +326,7 @@ void MotionMain::update_gui()
 
 
 
-void MotionMain::save_data(KeyFrame *keyframe)
+void MotionHVMain::save_data(KeyFrame *keyframe)
 {
        FileXML output;
 
@@ -335,12 +335,12 @@ void MotionMain::save_data(KeyFrame *keyframe)
        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_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("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);
@@ -352,7 +352,7 @@ void MotionMain::save_data(KeyFrame *keyframe)
        output.tag.set_property("ROTATE_MAGNITUDE", config.rotate_magnitude);
        output.tag.set_property("ROTATE_RETURN_SPEED", config.rotate_return_speed);
        output.tag.set_property("ACTION_TYPE", config.action_type);
-       output.tag.set_property("GLOBAL", config.global);
+//     output.tag.set_property("GLOBAL", config.global);
        output.tag.set_property("ROTATE", config.rotate);
        output.tag.set_property("TRACKING_TYPE", config.tracking_type);
        output.tag.set_property("DRAW_VECTORS", config.draw_vectors);
@@ -364,11 +364,10 @@ void MotionMain::save_data(KeyFrame *keyframe)
        output.append_tag();
        output.tag.set_title("/MOTION");
        output.append_tag();
-       output.append_newline();
        output.terminate_string();
 }
 
-void MotionMain::read_data(KeyFrame *keyframe)
+void MotionHVMain::read_data(KeyFrame *keyframe)
 {
        FileXML input;
 
@@ -385,12 +384,12 @@ void MotionMain::read_data(KeyFrame *keyframe)
                        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_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.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);
@@ -402,7 +401,7 @@ void MotionMain::read_data(KeyFrame *keyframe)
                                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.global = input.tag.get_property("GLOBAL", config.global);
                                config.rotate = input.tag.get_property("ROTATE", config.rotate);
                                config.tracking_type = input.tag.get_property("TRACKING_TYPE", config.tracking_type);
                                config.draw_vectors = input.tag.get_property("DRAW_VECTORS", config.draw_vectors);
@@ -425,7 +424,7 @@ void MotionMain::read_data(KeyFrame *keyframe)
 
 
 
-void MotionMain::allocate_temp(int w, int h, int color_model)
+void MotionHVMain::allocate_temp(int w, int h, int color_model)
 {
        if(temp_frame &&
                (temp_frame->get_w() != w ||
@@ -439,46 +438,54 @@ void MotionMain::allocate_temp(int w, int h, int color_model)
 }
 
 
-void MotionMain::process_global()
+void MotionHVMain::process_global()
 {
+       int w = current_global_ref->get_w();
+       int h = current_global_ref->get_h();
 
-       if(!engine) engine = new MotionScan(PluginClient::get_project_smp() + 1,
+
+       if(!engine) engine = new MotionHVScan(PluginClient::get_project_smp() + 1,
                PluginClient::get_project_smp() + 1);
 
 // Determine if frames changed
+// printf("MotionHVMain::process_global %d block_y=%f total_dy=%d\n",
+//  __LINE__, config.block_y * h / 100, total_dy);
        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.global_range_w * w / 100,
+               config.global_range_h * h / 100,
+               config.global_block_w * w / 100,
+               config.global_block_h * h / 100,
+               config.block_x * w / 100,
+               config.block_y * h / 100,
                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);
+               0,
+               1, // do_motion
+               config.rotate, // do_rotate
+               config.rotation_center,
+               config.rotation_range);
+
        current_dx = engine->dx_result;
        current_dy = engine->dy_result;
 
 // Add current motion vector to accumulation vector.
-       if(config.tracking_object != MotionScan::TRACK_SINGLE)
+       if(config.tracking_object != MotionHVScan::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);
+// printf("MotionHVMain::process_global %d total_dy=%d engine->dy_result=%d\n",
+//  __LINE__, total_dy, engine->dy_result);
        }
        else
 // Make accumulation vector current
@@ -490,45 +497,29 @@ void MotionMain::process_global()
 // 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_w = (int64_t)config.global_block_w * w / 100;
+               //int block_h = (int64_t)config.global_block_h * h / 100;
+               int block_x_orig = (int64_t)(config.block_x * w / 100);
                int block_y_orig = (int64_t)(config.block_y *
-                       current_global_ref->get_h() /
-                       100);
+                       current_global_ref->get_h() / 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 max_block_x = (int64_t)(w - block_x_orig) *
+                       OVERSAMPLE * config.magnitude / 100;
+               int max_block_y = (int64_t)(h - block_y_orig) *
+                       OVERSAMPLE * config.magnitude / 100;
                int min_block_x = (int64_t)-block_x_orig *
-                       OVERSAMPLE *
-                       config.magnitude /
-                       100;
+                       OVERSAMPLE * config.magnitude / 100;
                int min_block_y = (int64_t)-block_y_orig *
-                       OVERSAMPLE *
-                       config.magnitude /
-                       100;
+                       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("MotionMain::process_global 2 total_dx=%.02f total_dy=%.02f\n",
-(float)total_dx / OVERSAMPLE,
-(float)total_dy / OVERSAMPLE);
-#endif
+// printf("MotionHVMain::process_global %d total_dx=%d total_dy=%d\n",
+//  __LINE__, total_dx, total_dy);
 
-       if(config.tracking_object != MotionScan::TRACK_SINGLE && !config.rotate)
+       if(config.tracking_object != MotionHVScan::TRACK_SINGLE && !config.rotate)
        {
 // Transfer current reference frame to previous reference frame and update
 // counter.  Must wait for rotate to compare.
@@ -537,31 +528,30 @@ printf("MotionMain::process_global 2 total_dx=%.02f total_dy=%.02f\n",
        }
 
 // Decide what to do with target based on requested operation
-       int interpolation;
-       float dx;
-       float dy;
+       int interpolation = NEAREST_NEIGHBOR;
+       float dx = 0.;
+       float dy = 0.;
        switch(config.action_type)
        {
-               case MotionScan::NOTHING:
+               case MotionHVScan::NOTHING:
                        global_target_dst->copy_from(global_target_src);
                        break;
-               case MotionScan::TRACK_PIXEL:
+               case MotionHVScan::TRACK_PIXEL:
                        interpolation = NEAREST_NEIGHBOR;
                        dx = (int)(total_dx / OVERSAMPLE);
                        dy = (int)(total_dy / OVERSAMPLE);
                        break;
-               case MotionScan::STABILIZE_PIXEL:
+               case MotionHVScan::STABILIZE_PIXEL:
                        interpolation = NEAREST_NEIGHBOR;
                        dx = -(int)(total_dx / OVERSAMPLE);
                        dy = -(int)(total_dy / OVERSAMPLE);
                        break;
-                       break;
-               case MotionScan::TRACK:
+               case MotionHVScan::TRACK:
                        interpolation = CUBIC_LINEAR;
                        dx = (float)total_dx / OVERSAMPLE;
                        dy = (float)total_dy / OVERSAMPLE;
                        break;
-               case MotionScan::STABILIZE:
+               case MotionHVScan::STABILIZE:
                        interpolation = CUBIC_LINEAR;
                        dx = -(float)total_dx / OVERSAMPLE;
                        dy = -(float)total_dy / OVERSAMPLE;
@@ -569,7 +559,7 @@ printf("MotionMain::process_global 2 total_dx=%.02f total_dy=%.02f\n",
        }
 
 
-       if(config.action_type != MotionScan::NOTHING)
+       if(config.action_type != MotionHVScan::NOTHING)
        {
                if(!overlayer)
                        overlayer = new OverlayFrame(PluginClient::get_project_smp() + 1);
@@ -592,20 +582,22 @@ printf("MotionMain::process_global 2 total_dx=%.02f total_dy=%.02f\n",
 
 
 
-void MotionMain::process_rotation()
+void MotionHVMain::process_rotation()
 {
        int block_x;
        int block_y;
 
+// Always require global
 // Convert the previous global reference into the previous rotation reference.
 // Convert global target destination into rotation target source.
-       if(config.global)
+//     if(config.global)
+       if(1)
        {
                if(!overlayer)
                        overlayer = new OverlayFrame(PluginClient::get_project_smp() + 1);
                float dx;
                float dy;
-               if(config.tracking_object == MotionScan::TRACK_SINGLE)
+               if(config.tracking_object == MotionHVScan::TRACK_SINGLE)
                {
                        dx = (float)total_dx / OVERSAMPLE;
                        dy = (float)total_dy / OVERSAMPLE;
@@ -644,7 +636,7 @@ void MotionMain::process_rotation()
 // 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 != MotionHVScan::TRACK_SINGLE)
                {
                        prev_global_ref->copy_from(current_global_ref);
                        previous_frame_number = get_source_position();
@@ -664,20 +656,20 @@ void MotionMain::process_rotation()
 
 
 // Get rotation
-       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);
-
-
+//     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);
+
+       current_angle = engine->dr_result;
 
 // Add current rotation to accumulation
-       if(config.tracking_object != MotionScan::TRACK_SINGLE)
+       if(config.tracking_object != MotionHVScan::TRACK_SINGLE)
        {
 // Retract over time
                total_angle = total_angle * (100 - config.rotate_return_speed) / 100;
@@ -690,13 +682,13 @@ void MotionMain::process_rotation()
                        CLAMP(total_angle, -config.rotate_magnitude, config.rotate_magnitude);
                }
 
-               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();
-               }
+//                     prev_rotate_ref->copy_from(current_rotate_ref);
+//                     previous_frame_number = get_source_position();
+//             }
        }
        else
        {
@@ -704,30 +696,30 @@ void MotionMain::process_rotation()
        }
 
 #ifdef DEBUG
-printf("MotionMain::process_rotation total_angle=%f\n", total_angle);
+printf("MotionHVMain::process_rotation total_angle=%f\n", total_angle);
 #endif
 
 
 // Calculate rotation parameters based on requested operation
-       float angle;
+       float angle = 0.;
        switch(config.action_type)
        {
-               case MotionScan::NOTHING:
+               case MotionHVScan::NOTHING:
                        rotate_target_dst->copy_from(rotate_target_src);
                        break;
-               case MotionScan::TRACK:
-               case MotionScan::TRACK_PIXEL:
+               case MotionHVScan::TRACK:
+               case MotionHVScan::TRACK_PIXEL:
                        angle = total_angle;
                        break;
-               case MotionScan::STABILIZE:
-               case MotionScan::STABILIZE_PIXEL:
+               case MotionHVScan::STABILIZE:
+               case MotionHVScan::STABILIZE_PIXEL:
                        angle = -total_angle;
                        break;
        }
 
 
 
-       if(config.action_type != MotionScan::NOTHING)
+       if(config.action_type != MotionHVScan::NOTHING)
        {
                if(!rotate_engine)
                        rotate_engine = new AffineEngine(PluginClient::get_project_smp() + 1,
@@ -738,17 +730,18 @@ printf("MotionMain::process_rotation total_angle=%f\n", total_angle);
 // Determine pivot based on a number of factors.
                switch(config.action_type)
                {
-                       case MotionScan::TRACK:
-                       case MotionScan::TRACK_PIXEL:
+                       case MotionHVScan::TRACK:
+                       case MotionHVScan::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;
 
-                       case MotionScan::STABILIZE:
-                       case MotionScan::STABILIZE_PIXEL:
-                               if(config.global)
+                       case MotionHVScan::STABILIZE:
+                       case MotionHVScan::STABILIZE_PIXEL:
+//                             if(config.global)
+                               if(1)
                                {
 // Use origin of global stabilize operation
 //                                     rotate_engine->set_pivot((int)(rotate_target_dst->get_w() *
@@ -782,6 +775,7 @@ printf("MotionMain::process_rotation total_angle=%f\n", total_angle);
                }
 
 
+printf("MotionHVMain::process_rotation angle=%f\n", angle);
                rotate_engine->rotate(rotate_target_dst, rotate_target_src, angle);
 // overlayer->overlay(rotate_target_dst,
 //     prev_rotate_ref,
@@ -824,7 +818,7 @@ printf("MotionMain::process_rotation total_angle=%f\n", total_angle);
 
 
 
-int MotionMain::process_buffer(VFrame **frame,
+int MotionHVMain::process_buffer(VFrame **frame,
        int64_t start_position,
        double frame_rate)
 {
@@ -835,7 +829,7 @@ int MotionMain::process_buffer(VFrame **frame,
 
 
 #ifdef DEBUG
-printf("MotionMain::process_buffer %d start_position=%lld\n", __LINE__, start_position);
+printf("MotionHVMain::process_buffer %d start_position=%lld\n", __LINE__, start_position);
 #endif
 
 
@@ -859,7 +853,7 @@ printf("MotionMain::process_buffer %d start_position=%lld\n", __LINE__, start_po
        int skip_current = 0;
 
 
-       if(config.tracking_object == MotionScan::TRACK_SINGLE)
+       if(config.tracking_object == MotionHVScan::TRACK_SINGLE)
        {
                actual_previous_number = config.track_frame;
                if(get_direction() == PLAY_REVERSE)
@@ -903,7 +897,7 @@ printf("MotionMain::process_buffer %d start_position=%lld\n", __LINE__, start_po
        }
 
 
-       if(!config.global && !config.rotate) skip_current = 1;
+//     if(!config.global && !config.rotate) skip_current = 1;
 
 
 
@@ -939,7 +933,8 @@ printf("MotionMain::process_buffer %d start_position=%lld\n", __LINE__, start_po
 
 
 // Get the global pointers.  Here we walk through the sequence of events.
-       if(config.global)
+//     if(config.global)
+       if(1)
        {
 // Assume global only.  Global reads previous frame and compares
 // with current frame to get the current translation.
@@ -964,17 +959,20 @@ printf("MotionMain::process_buffer %d start_position=%lld\n", __LINE__, start_po
                        read_frame(prev_global_ref,
                                reference_layer,
                                previous_frame_number,
-                               frame_rate);
+                               frame_rate,
+                               0);
                }
 
                read_frame(current_global_ref,
                        reference_layer,
                        start_position,
-                       frame_rate);
+                       frame_rate,
+                       0);
                read_frame(global_target_src,
                        target_layer,
                        start_position,
-                       frame_rate);
+                       frame_rate,
+                       0);
 
 
 
@@ -1028,16 +1026,19 @@ printf("MotionMain::process_buffer %d start_position=%lld\n", __LINE__, start_po
                        read_frame(prev_rotate_ref,
                                reference_layer,
                                previous_frame_number,
-                               frame_rate);
+                               frame_rate,
+                               0);
                }
                read_frame(current_rotate_ref,
                        reference_layer,
                        start_position,
-                       frame_rate);
+                       frame_rate,
+                       0);
                read_frame(rotate_target_src,
                        target_layer,
                        start_position,
-                       frame_rate);
+                       frame_rate,
+                       0);
        }
 
 
@@ -1046,13 +1047,14 @@ printf("MotionMain::process_buffer %d start_position=%lld\n", __LINE__, start_po
 
 
 
-
+//PRINT_TRACE
+//printf("skip_current=%d config.global=%d\n", skip_current, config.global);
 
 
        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();
 //frame[target_layer]->copy_from(prev_rotate_ref);
@@ -1082,7 +1084,8 @@ printf("MotionMain::process_buffer %d start_position=%lld\n", __LINE__, start_po
                read_frame(frame[target_layer],
                        target_layer,
                        start_position,
-                       frame_rate);
+                       frame_rate,
+                       0);
        }
 
        if(config.draw_vectors)
@@ -1091,14 +1094,14 @@ printf("MotionMain::process_buffer %d start_position=%lld\n", __LINE__, start_po
        }
 
 #ifdef DEBUG
-printf("MotionMain::process_buffer %d\n", __LINE__);
+printf("MotionHVMain::process_buffer %d\n", __LINE__);
 #endif
        return 0;
 }
 
 
 
-void MotionMain::draw_vectors(VFrame *frame)
+void MotionHVMain::draw_vectors(VFrame *frame)
 {
        int w = frame->get_w();
        int h = frame->get_h();
@@ -1113,16 +1116,16 @@ void MotionMain::draw_vectors(VFrame *frame)
        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)
+// always processing global
+//     if(config.global)
+       if(1)
        {
 // Get vector
 // Start of vector is center of previous block.
 // End of vector is total accumulation.
-               if(config.tracking_object == MotionScan::TRACK_SINGLE)
+               if(config.tracking_object == MotionHVScan::TRACK_SINGLE)
                {
                        global_x1 = (int64_t)(config.block_x *
                                w /
@@ -1132,12 +1135,12 @@ void MotionMain::draw_vectors(VFrame *frame)
                                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);
+//printf("MotionHVMain::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)
+               if(config.tracking_object == MotionHVScan::PREVIOUS_SAME_BLOCK)
                {
                        global_x1 = (int64_t)(config.block_x *
                                w /
@@ -1187,7 +1190,7 @@ 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",
+// printf("MotionHVMain::draw_vectors %d %d %d %d %d %d %d %d %d %d %d %d\n",
 // global_x1,
 // global_y1,
 // block_w,
@@ -1201,7 +1204,7 @@ void MotionMain::draw_vectors(VFrame *frame)
 // search_x2,
 // search_y2);
 
-               MotionScan::clamp_scan(w,
+               MotionHVScan::clamp_scan(w,
                        h,
                        &block_x1,
                        &block_y1,
@@ -1242,8 +1245,8 @@ void MotionMain::draw_vectors(VFrame *frame)
                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;
+       block_w = config.global_block_w * w / 100;
+       block_h = config.global_block_h * h / 100;
        if(config.rotate)
        {
                float angle = total_angle * 2 * M_PI / 360;
@@ -1278,7 +1281,7 @@ void MotionMain::draw_vectors(VFrame *frame)
 
 
 
-void MotionMain::draw_pixel(VFrame *frame, int x, int y)
+void MotionHVMain::draw_pixel(VFrame *frame, int x, int y)
 {
        if(!(x >= 0 && y >= 0 && x < frame->get_w() && y < frame->get_h())) return;
 
@@ -1337,11 +1340,11 @@ void MotionMain::draw_pixel(VFrame *frame, int x, int y)
 }
 
 
-void MotionMain::draw_line(VFrame *frame, int x1, int y1, int x2, int y2)
+void MotionHVMain::draw_line(VFrame *frame, int x1, int y1, int x2, int y2)
 {
        int w = labs(x2 - x1);
        int h = labs(y2 - y1);
-//printf("MotionMain::draw_line 1 %d %d %d %d\n", x1, y1, x2, y2);
+//printf("MotionHVMain::draw_line 1 %d %d %d %d\n", x1, y1, x2, y2);
 
        if(!w && !h)
        {
@@ -1388,11 +1391,11 @@ void MotionMain::draw_line(VFrame *frame, int x1, int y1, int x2, int y2)
                        draw_pixel(frame, x, i);
                }
        }
-//printf("MotionMain::draw_line 2\n");
+//printf("MotionHVMain::draw_line 2\n");
 }
 
 #define ARROW_SIZE 10
-void MotionMain::draw_arrow(VFrame *frame, int x1, int y1, int x2, int y2)
+void MotionHVMain::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;
@@ -1434,442 +1437,3 @@ void MotionMain::draw_arrow(VFrame *frame, int x1, int y1, int x2, int y2)
 
 
 
-
-
-
-
-
-
-
-
-
-RotateScanPackage::RotateScanPackage()
-{
-}
-
-
-RotateScanUnit::RotateScanUnit(RotateScan *server, MotionMain *plugin)
- : LoadClient(server)
-{
-       this->server = server;
-       this->plugin = plugin;
-       rotater = 0;
-       temp = 0;
-}
-
-RotateScanUnit::~RotateScanUnit()
-{
-       delete rotater;
-       delete temp;
-}
-
-void RotateScanUnit::process_package(LoadPackage *package)
-{
-       if(server->skip) return;
-       RotateScanPackage *pkg = (RotateScanPackage*)package;
-
-       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)
-                       rotater = new AffineEngine(1, 1);
-               if(!temp) temp = new VFrame(0,
-                       -1,
-                       server->previous_frame->get_w(),
-                       server->previous_frame->get_h(),
-                       color_model,
-                       -1);
-//printf("RotateScanUnit::process_package %d\n", __LINE__);
-
-
-// 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_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);
-
-// 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);
-// 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 = 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);
-//printf("RotateScanUnit::process_package %d\n", __LINE__);
-                       server->put_cache(pkg->angle, pkg->difference);
-               }
-
-// printf("RotateScanUnit::process_package 10 x=%d y=%d w=%d h=%d block_x=%d block_y=%d angle=%f scan_w=%d scan_h=%d diff=%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);
-       }
-}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-RotateScan::RotateScan(MotionMain *plugin,
-       int total_clients,
-       int total_packages)
- : LoadServer(
-//1, 1
-total_clients, total_packages
-)
-{
-       this->plugin = plugin;
-       cache_lock = new Mutex("RotateScan::cache_lock");
-}
-
-
-RotateScan::~RotateScan()
-{
-       delete cache_lock;
-}
-
-void RotateScan::init_packages()
-{
-       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;
-       }
-}
-
-LoadClient* RotateScan::new_client()
-{
-       return new RotateScanUnit(this, plugin);
-}
-
-LoadPackage* RotateScan::new_package()
-{
-       return new RotateScanPackage;
-}
-
-
-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;
-
-               case MotionScan::LOAD:
-               {
-                       char string[BCTEXTLEN];
-                       sprintf(string, "%s%06d", 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;
-               }
-       }
-
-
-
-
-
-
-
-
-       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("RotateScan::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);
-
-//printf("RotateScan::scan_frame %d min_angle=%f\n", __LINE__, min_angle * 360 / 2 / M_PI);
-
-       cache.remove_all_objects();
-
-
-       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)
-       {
-// 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)
-               {
-                       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++)
-                       {
-                               RotateScanPackage *pkg = (RotateScanPackage*)get_package(i);
-                               if(pkg->difference < min_difference || min_difference == -1)
-                               {
-                                       min_difference = pkg->difference;
-                                       result = pkg->angle;
-                               }
-//break;
-                       }
-
-                       angle_range /= 2;
-
-//break;
-               }
-       }
-
-//printf("RotateScan::scan_frame %d\n", __LINE__);
-
-       if(!skip && plugin->config.tracking_type == MotionScan::SAVE)
-       {
-               char string[BCTEXTLEN];
-               sprintf(string,
-                       "%s%06d",
-                       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");
-               }
-       }
-
-//printf("RotateScan::scan_frame %d angle=%f\n", __LINE__, result);
-
-
-
-       return result;
-}
-
-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++)
-       {
-               RotateScanCache *ptr = cache.values[i];
-               if(fabs(ptr->angle - angle) <= MIN_ANGLE)
-               {
-                       result = ptr->difference;
-                       break;
-               }
-       }
-       cache_lock->unlock();
-       return result;
-}
-
-void RotateScan::put_cache(float angle, int64_t difference)
-{
-       RotateScanCache *ptr = new RotateScanCache(angle, difference);
-       cache_lock->lock("RotateScan::put_cache");
-       cache.append(ptr);
-       cache_lock->unlock();
-}
-
-
-
-
-
-
-
-
-
-RotateScanCache::RotateScanCache(float angle, int64_t difference)
-{
-       this->angle = angle;
-       this->difference = difference;
-}
-
-
-