4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5 * Copyright (C) 2003-2016 Cinelerra CV contributors
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include "filexml.inc"
33 #include "keyframe.inc"
34 #include "loadbalance.h"
35 #include "overlayframe.inc"
36 #include "pluginvclient.h"
37 #include "rotateframe.inc"
49 // Limits of global range in percent
53 // Limits of rotation range in degrees
54 #define MIN_ROTATION 1
55 #define MAX_ROTATION 25
57 // Limits of block size in percent.
61 // Limits of block count
63 #define MAX_BLOCKS 200
65 // Precision of rotation
66 #define MIN_ANGLE 0.0001
68 #define TRACKING_FILE "/tmp/motion-cv"
75 int equivalent(MotionCVConfig &that);
76 void copy_from(MotionCVConfig &that);
77 void interpolate(MotionCVConfig &prev, MotionCVConfig &next,
78 int64_t prev_frame, int64_t next_frame, int64_t current_frame);
88 // Percent of image size
93 // Number of search positions in each refinement of the log search
96 // Block position in percentage 0 - 100
104 int addtrackedframeoffset;
105 char tracking_file[BCTEXTLEN];
106 // Track or stabilize, single pixel, scan only, or nothing
108 // Recalculate, no calculate, save, or load coordinates from disk
110 // Track a single frame, previous frame, or previous frame same block
130 // Number of single frame to track relative to timeline start
133 int bottom_is_master;
139 class MotionCVMain : public PluginVClient
142 MotionCVMain(PluginServer *server);
145 int process_buffer(VFrame **frame,
146 int64_t start_position,
148 void process_global();
149 void process_rotation();
150 void draw_vectors(VFrame *frame);
151 int is_multichannel();
153 void save_data(KeyFrame *keyframe);
154 void read_data(KeyFrame *keyframe);
156 // Calculate frame to copy from and frame to move
157 void calculate_pointers(VFrame **frame, VFrame **src, VFrame **dst);
158 void allocate_temp(int w, int h, int color_model);
160 PLUGIN_CLASS_MEMBERS2(MotionCVConfig)
163 int64_t abs_diff(unsigned char *prev_ptr,
164 unsigned char *current_ptr,
169 int64_t abs_diff_sub(unsigned char *prev_ptr,
170 unsigned char *current_ptr,
178 static void clamp_scan(int w,
189 static void draw_pixel(VFrame *frame, int x, int y);
190 static void draw_line(VFrame *frame, int x1, int y1, int x2, int y2);
191 void draw_arrow(VFrame *frame, int x1, int y1, int x2, int y2);
193 // Number of the previous reference frame on the timeline.
194 int64_t previous_frame_number;
195 // The frame compared with the previous frame to get the motion.
196 // It is moved to compensate for motion and copied to the previous_frame.
198 MotionCVScan *engine;
199 RotateCVScan *motion_rotate;
200 OverlayFrame *overlayer;
201 AffineEngine *rotate_engine;
203 // Accumulation of all global tracks since the plugin start.
204 // Multiplied by OVERSAMPLE.
208 // Rotation motion tracking
211 // Current motion vector for drawing vectors
216 char cache_file[BCTEXTLEN];
217 FILE *cache_fp, *active_fp;
218 void reset_cache_file();
219 int open_cache_file();
220 void close_cache_file();
221 int load_cache_line();
222 int locate_cache_line(int64_t key);
223 int get_cache_line(int64_t key);
224 int put_cache_line(const char *line);
225 char cache_line[BCSTRLEN];
226 int64_t cache_key, active_key;
227 // add constant frame offset values
228 int dx_offset, dy_offset;
229 int64_t tracking_frame;
230 // save/load result values
232 int save_dx, load_dx;
233 int save_dy, load_dy;
234 float save_dt, load_dt;
235 // Oversampled current frame for motion estimation
236 int32_t *search_area;
240 // The layer to track motion in.
242 // The layer to apply motion in.
245 // Pointer to the source and destination of each operation.
246 // These are fully allocated buffers.
248 // The previous reference frame for global motion tracking
249 VFrame *prev_global_ref;
250 // The current reference frame for global motion tracking
251 VFrame *current_global_ref;
252 // The input target frame for global motion tracking
253 VFrame *global_target_src;
254 // The output target frame for global motion tracking
255 VFrame *global_target_dst;
257 // The previous reference frame for rotation tracking
258 VFrame *prev_rotate_ref;
259 // The current reference frame for rotation tracking
260 VFrame *current_rotate_ref;
261 // The input target frame for rotation tracking.
262 VFrame *rotate_target_src;
263 // The output target frame for rotation tracking.
264 VFrame *rotate_target_dst;
266 // The output of process_buffer
267 VFrame *output_frame;
274 class MotionCvVVFrame : public VFrame
277 MotionCvVVFrame(VFrame *vfrm, int n);
278 int draw_pixel(int x, int y);
304 class MotionCVScanPackage : public LoadPackage
307 MotionCVScanPackage();
309 // For multiple blocks
310 int block_x1, block_y1, block_x2, block_y2;
311 int scan_x1, scan_y1, scan_x2, scan_y2;
314 int64_t max_difference;
315 int64_t min_difference;
325 class MotionCVScanCache
328 MotionCVScanCache(int x, int y, int64_t difference);
333 class MotionCVScanUnit : public LoadClient
336 MotionCVScanUnit(MotionCVScan *server, MotionCVMain *plugin);
339 void process_package(LoadPackage *package);
340 int64_t get_cache(int x, int y);
341 void put_cache(int x, int y, int64_t difference);
343 MotionCVScan *server;
344 MotionCVMain *plugin;
346 ArrayList<MotionCVScanCache*> cache;
350 class MotionCVScan : public LoadServer
353 MotionCVScan(MotionCVMain *plugin,
358 friend class MotionCVScanUnit;
360 void init_packages();
361 LoadClient* new_client();
362 LoadPackage* new_package();
364 // Invoke the motion engine for a search
365 // Frame before motion
366 void scan_frame(VFrame *previous_frame,
367 // Frame after motion
368 VFrame *current_frame);
369 int64_t get_cache(int x, int y);
370 void put_cache(int x, int y, int64_t difference);
372 // Change between previous frame and current frame multiplied by
378 VFrame *previous_frame;
379 // Frame after motion
380 VFrame *current_frame;
381 MotionCVMain *plugin;
398 ArrayList<MotionCVScanCache*> cache;
414 class RotateCVScanPackage : public LoadPackage
417 RotateCVScanPackage();
422 class RotateCVScanCache
425 RotateCVScanCache(float angle, int64_t difference);
430 class RotateCVScanUnit : public LoadClient
433 RotateCVScanUnit(RotateCVScan *server, MotionCVMain *plugin);
436 void process_package(LoadPackage *package);
438 RotateCVScan *server;
439 MotionCVMain *plugin;
440 AffineEngine *rotater;
444 class RotateCVScan : public LoadServer
447 RotateCVScan(MotionCVMain *plugin,
452 friend class RotateCVScanUnit;
454 void init_packages();
455 LoadClient* new_client();
456 LoadPackage* new_package();
458 // Invoke the motion engine for a search
459 // Frame before rotation
460 float scan_frame(VFrame *previous_frame,
461 // Frame after rotation
462 VFrame *current_frame,
466 int64_t get_cache(float angle);
467 void put_cache(float angle, int64_t difference);
474 VFrame *previous_frame;
475 // Frame after motion
476 VFrame *current_frame;
478 MotionCVMain *plugin;
494 // Range of angles to compare
495 float scan_angle1, scan_angle2;
498 ArrayList<RotateCVScanCache*> cache;