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;
295 class MotionCVScanPackage : public LoadPackage
298 MotionCVScanPackage();
300 // For multiple blocks
301 int block_x1, block_y1, block_x2, block_y2;
302 int scan_x1, scan_y1, scan_x2, scan_y2;
305 int64_t max_difference;
306 int64_t min_difference;
316 class MotionCVScanCache
319 MotionCVScanCache(int x, int y, int64_t difference);
324 class MotionCVScanUnit : public LoadClient
327 MotionCVScanUnit(MotionCVScan *server, MotionCVMain *plugin);
330 void process_package(LoadPackage *package);
331 int64_t get_cache(int x, int y);
332 void put_cache(int x, int y, int64_t difference);
334 MotionCVScan *server;
335 MotionCVMain *plugin;
337 ArrayList<MotionCVScanCache*> cache;
341 class MotionCVScan : public LoadServer
344 MotionCVScan(MotionCVMain *plugin,
349 friend class MotionCVScanUnit;
351 void init_packages();
352 LoadClient* new_client();
353 LoadPackage* new_package();
355 // Invoke the motion engine for a search
356 // Frame before motion
357 void scan_frame(VFrame *previous_frame,
358 // Frame after motion
359 VFrame *current_frame);
360 int64_t get_cache(int x, int y);
361 void put_cache(int x, int y, int64_t difference);
363 // Change between previous frame and current frame multiplied by
369 VFrame *previous_frame;
370 // Frame after motion
371 VFrame *current_frame;
372 MotionCVMain *plugin;
389 ArrayList<MotionCVScanCache*> cache;
405 class RotateCVScanPackage : public LoadPackage
408 RotateCVScanPackage();
413 class RotateCVScanCache
416 RotateCVScanCache(float angle, int64_t difference);
421 class RotateCVScanUnit : public LoadClient
424 RotateCVScanUnit(RotateCVScan *server, MotionCVMain *plugin);
427 void process_package(LoadPackage *package);
429 RotateCVScan *server;
430 MotionCVMain *plugin;
431 AffineEngine *rotater;
435 class RotateCVScan : public LoadServer
438 RotateCVScan(MotionCVMain *plugin,
443 friend class RotateCVScanUnit;
445 void init_packages();
446 LoadClient* new_client();
447 LoadPackage* new_package();
449 // Invoke the motion engine for a search
450 // Frame before rotation
451 float scan_frame(VFrame *previous_frame,
452 // Frame after rotation
453 VFrame *current_frame,
457 int64_t get_cache(float angle);
458 void put_cache(float angle, int64_t difference);
465 VFrame *previous_frame;
466 // Frame after motion
467 VFrame *current_frame;
469 MotionCVMain *plugin;
485 // Range of angles to compare
486 float scan_angle1, scan_angle2;
489 ArrayList<RotateCVScanCache*> cache;