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"
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
216 char active_file[BCTEXTLEN];
217 int get_line_key(const char *filename, int64_t key, char *line, int len);
218 // add constant frame offset values
219 int dx_offset, dy_offset;
220 int64_t tracking_frame;
221 // save/load result values
223 int save_dx, load_dx;
224 int save_dy, load_dy;
225 float save_dt, load_dt;
226 // Oversampled current frame for motion estimation
227 int32_t *search_area;
231 // The layer to track motion in.
233 // The layer to apply motion in.
236 // Pointer to the source and destination of each operation.
237 // These are fully allocated buffers.
239 // The previous reference frame for global motion tracking
240 VFrame *prev_global_ref;
241 // The current reference frame for global motion tracking
242 VFrame *current_global_ref;
243 // The input target frame for global motion tracking
244 VFrame *global_target_src;
245 // The output target frame for global motion tracking
246 VFrame *global_target_dst;
248 // The previous reference frame for rotation tracking
249 VFrame *prev_rotate_ref;
250 // The current reference frame for rotation tracking
251 VFrame *current_rotate_ref;
252 // The input target frame for rotation tracking.
253 VFrame *rotate_target_src;
254 // The output target frame for rotation tracking.
255 VFrame *rotate_target_dst;
257 // The output of process_buffer
258 VFrame *output_frame;
287 class MotionCVScanPackage : public LoadPackage
290 MotionCVScanPackage();
292 // For multiple blocks
293 int block_x1, block_y1, block_x2, block_y2;
294 int scan_x1, scan_y1, scan_x2, scan_y2;
297 int64_t max_difference;
298 int64_t min_difference;
308 class MotionCVScanCache
311 MotionCVScanCache(int x, int y, int64_t difference);
316 class MotionCVScanUnit : public LoadClient
319 MotionCVScanUnit(MotionCVScan *server, MotionCVMain *plugin);
322 void process_package(LoadPackage *package);
323 int64_t get_cache(int x, int y);
324 void put_cache(int x, int y, int64_t difference);
326 MotionCVScan *server;
327 MotionCVMain *plugin;
329 ArrayList<MotionCVScanCache*> cache;
333 class MotionCVScan : public LoadServer
336 MotionCVScan(MotionCVMain *plugin,
341 friend class MotionCVScanUnit;
343 void init_packages();
344 LoadClient* new_client();
345 LoadPackage* new_package();
347 // Invoke the motion engine for a search
348 // Frame before motion
349 void scan_frame(VFrame *previous_frame,
350 // Frame after motion
351 VFrame *current_frame);
352 int64_t get_cache(int x, int y);
353 void put_cache(int x, int y, int64_t difference);
355 // Change between previous frame and current frame multiplied by
361 VFrame *previous_frame;
362 // Frame after motion
363 VFrame *current_frame;
364 MotionCVMain *plugin;
381 ArrayList<MotionCVScanCache*> cache;
397 class RotateCVScanPackage : public LoadPackage
400 RotateCVScanPackage();
405 class RotateCVScanCache
408 RotateCVScanCache(float angle, int64_t difference);
413 class RotateCVScanUnit : public LoadClient
416 RotateCVScanUnit(RotateCVScan *server, MotionCVMain *plugin);
419 void process_package(LoadPackage *package);
421 RotateCVScan *server;
422 MotionCVMain *plugin;
423 AffineEngine *rotater;
427 class RotateCVScan : public LoadServer
430 RotateCVScan(MotionCVMain *plugin,
435 friend class RotateCVScanUnit;
437 void init_packages();
438 LoadClient* new_client();
439 LoadPackage* new_package();
441 // Invoke the motion engine for a search
442 // Frame before rotation
443 float scan_frame(VFrame *previous_frame,
444 // Frame after rotation
445 VFrame *current_frame,
449 int64_t get_cache(float angle);
450 void put_cache(float angle, int64_t difference);
457 VFrame *previous_frame;
458 // Frame after motion
459 VFrame *current_frame;
461 MotionCVMain *plugin;
477 // Range of angles to compare
478 float scan_angle1, scan_angle2;
481 ArrayList<RotateCVScanCache*> cache;