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 "overlayframe.inc"
35 #include "pluginvclient.h"
36 #include "rotateframe.inc"
48 // Limits of global range in percent
52 // Limits of rotation range in degrees
53 #define MIN_ROTATION 1
54 #define MAX_ROTATION 25
56 // Limits of block size in percent.
60 // Limits of block count
62 #define MAX_BLOCKS 200
64 // Precision of rotation
65 #define MIN_ANGLE 0.0001
67 #define TRACKING_FILE "/tmp/motion-cv"
74 int equivalent(MotionCVConfig &that);
75 void copy_from(MotionCVConfig &that);
76 void interpolate(MotionCVConfig &prev, MotionCVConfig &next,
77 int64_t prev_frame, int64_t next_frame, int64_t current_frame);
87 // Percent of image size
92 // Number of search positions in each refinement of the log search
95 // Block position in percentage 0 - 100
103 int addtrackedframeoffset;
104 char tracking_file[BCTEXTLEN];
105 // Track or stabilize, single pixel, scan only, or nothing
107 // Recalculate, no calculate, save, or load coordinates from disk
109 // Track a single frame, previous frame, or previous frame same block
129 // Number of single frame to track relative to timeline start
132 int bottom_is_master;
138 class MotionCVMain : public PluginVClient
141 MotionCVMain(PluginServer *server);
144 int process_buffer(VFrame **frame,
145 int64_t start_position,
147 void process_global();
148 void process_rotation();
149 void draw_vectors(VFrame *frame);
150 int is_multichannel();
152 void save_data(KeyFrame *keyframe);
153 void read_data(KeyFrame *keyframe);
155 // Calculate frame to copy from and frame to move
156 void calculate_pointers(VFrame **frame, VFrame **src, VFrame **dst);
157 void allocate_temp(int w, int h, int color_model);
159 PLUGIN_CLASS_MEMBERS2(MotionCVConfig)
162 int64_t abs_diff(unsigned char *prev_ptr,
163 unsigned char *current_ptr,
168 int64_t abs_diff_sub(unsigned char *prev_ptr,
169 unsigned char *current_ptr,
177 static void clamp_scan(int w,
188 static void draw_pixel(VFrame *frame, int x, int y);
189 static void draw_line(VFrame *frame, int x1, int y1, int x2, int y2);
190 void draw_arrow(VFrame *frame, int x1, int y1, int x2, int y2);
192 // Number of the previous reference frame on the timeline.
193 int64_t previous_frame_number;
194 // The frame compared with the previous frame to get the motion.
195 // It is moved to compensate for motion and copied to the previous_frame.
197 MotionCVScan *engine;
198 RotateCVScan *motion_rotate;
199 OverlayFrame *overlayer;
200 AffineEngine *rotate_engine;
202 // Accumulation of all global tracks since the plugin start.
203 // Multiplied by OVERSAMPLE.
207 // Rotation motion tracking
210 // Current motion vector for drawing vectors
215 char cache_file[BCTEXTLEN];
216 FILE *cache_fp, *active_fp;
217 void reset_cache_file();
218 int open_cache_file();
219 void close_cache_file();
220 int load_cache_line();
221 int locate_cache_line(int64_t key);
222 int get_cache_line(int64_t key);
223 int put_cache_line(const char *line);
224 char cache_line[BCSTRLEN];
225 int64_t cache_key, active_key;
226 // add constant frame offset values
227 int dx_offset, dy_offset;
228 int64_t tracking_frame;
229 // save/load result values
231 int save_dx, load_dx;
232 int save_dy, load_dy;
233 float save_dt, load_dt;
234 // Oversampled current frame for motion estimation
235 int32_t *search_area;
239 // The layer to track motion in.
241 // The layer to apply motion in.
244 // Pointer to the source and destination of each operation.
245 // These are fully allocated buffers.
247 // The previous reference frame for global motion tracking
248 VFrame *prev_global_ref;
249 // The current reference frame for global motion tracking
250 VFrame *current_global_ref;
251 // The input target frame for global motion tracking
252 VFrame *global_target_src;
253 // The output target frame for global motion tracking
254 VFrame *global_target_dst;
256 // The previous reference frame for rotation tracking
257 VFrame *prev_rotate_ref;
258 // The current reference frame for rotation tracking
259 VFrame *current_rotate_ref;
260 // The input target frame for rotation tracking.
261 VFrame *rotate_target_src;
262 // The output target frame for rotation tracking.
263 VFrame *rotate_target_dst;
265 // The output of process_buffer
266 VFrame *output_frame;
273 class MotionCvVVFrame : public VFrame
276 MotionCvVVFrame(VFrame *vfrm, int n);
277 int draw_pixel(int x, int y);
303 class MotionCVScanPackage : public LoadPackage
306 MotionCVScanPackage();
308 // For multiple blocks
309 int block_x1, block_y1, block_x2, block_y2;
310 int scan_x1, scan_y1, scan_x2, scan_y2;
313 int64_t max_difference;
314 int64_t min_difference;
324 class MotionCVScanCache
327 MotionCVScanCache(int x, int y, int64_t difference);
332 class MotionCVScanUnit : public LoadClient
335 MotionCVScanUnit(MotionCVScan *server, MotionCVMain *plugin);
338 void process_package(LoadPackage *package);
339 int64_t get_cache(int x, int y);
340 void put_cache(int x, int y, int64_t difference);
342 MotionCVScan *server;
343 MotionCVMain *plugin;
345 ArrayList<MotionCVScanCache*> cache;
349 class MotionCVScan : public LoadServer
352 MotionCVScan(MotionCVMain *plugin,
357 friend class MotionCVScanUnit;
359 void init_packages();
360 LoadClient* new_client();
361 LoadPackage* new_package();
363 // Invoke the motion engine for a search
364 // Frame before motion
365 void scan_frame(VFrame *previous_frame,
366 // Frame after motion
367 VFrame *current_frame);
368 int64_t get_cache(int x, int y);
369 void put_cache(int x, int y, int64_t difference);
371 // Change between previous frame and current frame multiplied by
377 VFrame *previous_frame;
378 // Frame after motion
379 VFrame *current_frame;
380 MotionCVMain *plugin;
397 ArrayList<MotionCVScanCache*> cache;
413 class RotateCVScanPackage : public LoadPackage
416 RotateCVScanPackage();
421 class RotateCVScanCache
424 RotateCVScanCache(float angle, int64_t difference);
429 class RotateCVScanUnit : public LoadClient
432 RotateCVScanUnit(RotateCVScan *server, MotionCVMain *plugin);
435 void process_package(LoadPackage *package);
437 RotateCVScan *server;
438 MotionCVMain *plugin;
439 AffineEngine *rotater;
443 class RotateCVScan : public LoadServer
446 RotateCVScan(MotionCVMain *plugin,
451 friend class RotateCVScanUnit;
453 void init_packages();
454 LoadClient* new_client();
455 LoadPackage* new_package();
457 // Invoke the motion engine for a search
458 // Frame before rotation
459 float scan_frame(VFrame *previous_frame,
460 // Frame after rotation
461 VFrame *current_frame,
465 int64_t get_cache(float angle);
466 void put_cache(float angle, int64_t difference);
473 VFrame *previous_frame;
474 // Frame after motion
475 VFrame *current_frame;
477 MotionCVMain *plugin;
493 // Range of angles to compare
494 float scan_angle1, scan_angle2;
497 ArrayList<RotateCVScanCache*> cache;