4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include "filexml.inc"
32 #include "keyframe.inc"
33 #include "loadbalance.h"
34 #include "motionscan.inc"
35 #include "motionwindow.inc"
36 #include "overlayframe.inc"
37 #include "pluginvclient.h"
38 #include "rotateframe.inc"
46 // Limits of global range in percent
48 #define MAX_RADIUS 100
50 // Limits of rotation range in degrees
51 #define MIN_ROTATION 1
52 #define MAX_ROTATION 25
54 // Limits of block size in percent.
58 // Limits of block count
60 #define MAX_BLOCKS 200
62 // Precision of rotation
63 #define MIN_ANGLE 0.0001
65 #define TRACKING_FILE "/tmp/motion"
72 int equivalent(MotionConfig &that);
73 void copy_from(MotionConfig &that);
74 void interpolate(MotionConfig &prev, MotionConfig &next,
75 int64_t prev_frame, int64_t next_frame, int64_t current_frame);
81 // Range of angles above and below center rotation angle to search
83 // Center angle of rotation search
88 int rotate_return_speed;
90 // Percent of image size
93 // int rotation_block_w;
94 // int rotation_block_h;
95 // Number of search positions in each refinement of the log search
98 // Block position in percentage 0 - 100
106 int addtrackedframeoffset;
107 char tracking_file[BCTEXTLEN];
108 // Track or stabilize, single pixel, scan only, or nothing
110 // Recalculate, no calculate, save, or load coordinates from disk
112 // Track a single frame, previous frame, or previous frame same block
115 // Number of single frame to track relative to timeline start
118 int bottom_is_master;
122 class MotionMain : public PluginVClient
125 MotionMain(PluginServer *server);
128 int process_buffer(VFrame **frame, int64_t start_position, double frame_rate);
129 void process_global();
130 void process_rotation();
131 void draw_vectors(VFrame *frame);
132 int is_multichannel();
134 void save_data(KeyFrame *keyframe);
135 void read_data(KeyFrame *keyframe);
137 // Calculate frame to copy from and frame to move
138 void calculate_pointers(VFrame **frame, VFrame **src, VFrame **dst);
139 void allocate_temp(int w, int h, int color_model);
141 PLUGIN_CLASS_MEMBERS2(MotionConfig)
143 static void draw_pixel(VFrame *frame, int x, int y);
144 static void draw_line(VFrame *frame, int x1, int y1, int x2, int y2);
145 void draw_arrow(VFrame *frame, int x1, int y1, int x2, int y2);
147 // Number of the previous reference frame on the timeline.
148 int64_t previous_frame_number;
149 // The frame compared with the previous frame to get the motion.
150 // It is moved to compensate for motion and copied to the previous_frame.
153 RotateScan *motion_rotate;
154 OverlayFrame *overlayer;
155 AffineEngine *rotate_engine;
157 // Accumulation of all global tracks since the plugin start.
158 // Multiplied by OVERSAMPLE.
162 // Rotation motion tracking
165 // Current motion vector for drawing vectors
170 char cache_file[BCTEXTLEN];
171 FILE *cache_fp, *active_fp;
172 void reset_cache_file();
173 int open_cache_file();
174 void close_cache_file();
175 int load_cache_line();
176 int locate_cache_line(int64_t key);
177 int get_cache_line(int64_t key);
178 int put_cache_line(const char *line);
179 char cache_line[BCSTRLEN];
180 int64_t cache_key, active_key;
181 // add constant frame offset values
182 int dx_offset, dy_offset;
183 int64_t tracking_frame;
184 // save/load result values
186 int save_dx, load_dx;
187 int save_dy, load_dy;
188 float save_dt, load_dt;
190 // Oversampled current frame for motion estimation
191 int32_t *search_area;
195 // The layer to track motion in.
197 // The layer to apply motion in.
200 // Pointer to the source and destination of each operation.
201 // These are fully allocated buffers.
203 // The previous reference frame for global motion tracking
204 VFrame *prev_global_ref;
205 // The current reference frame for global motion tracking
206 VFrame *current_global_ref;
207 // The input target frame for global motion tracking
208 VFrame *global_target_src;
209 // The output target frame for global motion tracking
210 VFrame *global_target_dst;
212 // The previous reference frame for rotation tracking
213 VFrame *prev_rotate_ref;
214 // The current reference frame for rotation tracking
215 VFrame *current_rotate_ref;
216 // The input target frame for rotation tracking.
217 VFrame *rotate_target_src;
218 // The output target frame for rotation tracking.
219 VFrame *rotate_target_dst;
221 // The output of process_buffer
222 VFrame *output_frame;
228 class RotateScanPackage : public LoadPackage
236 class RotateScanCache
239 RotateScanCache(float angle, int64_t difference);
244 class RotateScanUnit : public LoadClient
247 RotateScanUnit(RotateScan *server, MotionMain *plugin);
250 void process_package(LoadPackage *package);
254 AffineEngine *rotater;
258 class RotateScan : public LoadServer
261 RotateScan(MotionMain *plugin,
266 friend class RotateScanUnit;
268 void init_packages();
269 LoadClient* new_client();
270 LoadPackage* new_package();
272 // Invoke the motion engine for a search
273 // Frame before rotation
274 float scan_frame(VFrame *previous_frame,
275 // Frame after rotation
276 VFrame *current_frame,
280 int64_t get_cache(float angle);
281 void put_cache(float angle, int64_t difference);
288 VFrame *previous_frame;
289 // Frame after motion
290 VFrame *current_frame;
308 // Range of angles to compare
309 float scan_angle1, scan_angle2;
312 ArrayList<RotateScanCache*> cache;