Fixed initialisation of tf in file_open(). Without setting the memory to 0,
[cinelerra_cv/mob.git] / plugins / motion / motion.h
blob2c1a762369767f53444210459797b2fa9533c012
1 #ifndef MOTION_H
2 #define MOTION_H
4 #include <math.h>
5 #include <stdint.h>
6 #include <string.h>
8 #include "affine.inc"
9 #include "bchash.inc"
10 #include "filexml.inc"
11 #include "keyframe.inc"
12 #include "loadbalance.h"
13 #include "motionwindow.inc"
14 #include "overlayframe.inc"
15 #include "pluginvclient.h"
16 #include "rotateframe.inc"
17 #include "vframe.inc"
19 class MotionMain;
20 class MotionWindow;
21 class MotionScan;
22 class RotateScan;
25 #define OVERSAMPLE 4
28 // Limits of global range in percent
29 #define MIN_RADIUS 1
30 #define MAX_RADIUS 50
32 // Limits of rotation range in degrees
33 #define MIN_ROTATION 1
34 #define MAX_ROTATION 25
36 // Limits of block size in percent.
37 #define MIN_BLOCK 1
38 #define MAX_BLOCK 100
40 // Limits of block count
41 #define MIN_BLOCKS 1
42 #define MAX_BLOCKS 200
44 // Precision of rotation
45 #define MIN_ANGLE 0.0001
47 #define MOTION_FILE "/tmp/motion"
48 #define ROTATION_FILE "/tmp/rotate"
50 class MotionConfig
52 public:
53 MotionConfig();
55 int equivalent(MotionConfig &that);
56 void copy_from(MotionConfig &that);
57 void interpolate(MotionConfig &prev,
58 MotionConfig &next,
59 int64_t prev_frame,
60 int64_t next_frame,
61 int64_t current_frame);
62 void boundaries();
64 int block_count;
65 int global_range_w;
66 int global_range_h;
67 int rotation_range;
68 int magnitude;
69 int return_speed;
70 int draw_vectors;
71 // Percent of image size
72 int global_block_w;
73 int global_block_h;
74 int rotation_block_w;
75 int rotation_block_h;
76 // Number of search positions in each refinement of the log search
77 int global_positions;
78 int rotate_positions;
79 // Block position in percentage 0 - 100
80 double block_x;
81 double block_y;
83 int horizontal_only;
84 int vertical_only;
85 int global;
86 int rotate;
87 // Track or stabilize, single pixel, scan only, or nothing
88 int mode1;
89 // Recalculate, no calculate, save, or load coordinates from disk
90 int mode2;
91 // Track a single frame, previous frame, or previous frame same block
92 int mode3;
93 enum
95 // mode1
96 TRACK,
97 STABILIZE,
98 TRACK_PIXEL,
99 STABILIZE_PIXEL,
100 NOTHING,
101 // mode2
102 RECALCULATE,
103 SAVE,
104 LOAD,
105 NO_CALCULATE,
106 // mode3
107 TRACK_SINGLE,
108 TRACK_PREVIOUS,
109 PREVIOUS_SAME_BLOCK
111 // Number of single frame to track relative to timeline start
112 int64_t track_frame;
113 // Master layer
114 int bottom_is_master;
120 class MotionMain : public PluginVClient
122 public:
123 MotionMain(PluginServer *server);
124 ~MotionMain();
126 int process_buffer(VFrame **frame,
127 int64_t start_position,
128 double frame_rate);
129 void process_global();
130 void process_rotation();
131 void draw_vectors(VFrame *frame);
132 int is_multichannel();
133 int is_realtime();
134 int load_defaults();
135 int save_defaults();
136 void save_data(KeyFrame *keyframe);
137 void read_data(KeyFrame *keyframe);
138 void update_gui();
139 // Calculate frame to copy from and frame to move
140 void calculate_pointers(VFrame **frame, VFrame **src, VFrame **dst);
141 void allocate_temp(int w, int h, int color_model);
143 PLUGIN_CLASS_MEMBERS(MotionConfig, MotionThread)
145 int64_t abs_diff(unsigned char *prev_ptr,
146 unsigned char *current_ptr,
147 int row_bytes,
148 int w,
149 int h,
150 int color_model);
151 int64_t abs_diff_sub(unsigned char *prev_ptr,
152 unsigned char *current_ptr,
153 int row_bytes,
154 int w,
155 int h,
156 int color_model,
157 int sub_x,
158 int sub_y);
160 static void clamp_scan(int w,
161 int h,
162 int *block_x1,
163 int *block_y1,
164 int *block_x2,
165 int *block_y2,
166 int *scan_x1,
167 int *scan_y1,
168 int *scan_x2,
169 int *scan_y2,
170 int use_absolute);
171 static void draw_pixel(VFrame *frame, int x, int y);
172 static void draw_line(VFrame *frame, int x1, int y1, int x2, int y2);
173 void draw_arrow(VFrame *frame, int x1, int y1, int x2, int y2);
175 // Number of the previous reference frame on the timeline.
176 int64_t previous_frame_number;
177 // The frame compared with the previous frame to get the motion.
178 // It is moved to compensate for motion and copied to the previous_frame.
179 VFrame *temp_frame;
180 MotionScan *engine;
181 RotateScan *motion_rotate;
182 OverlayFrame *overlayer;
183 AffineEngine *rotate_engine;
185 // Accumulation of all global tracks since the plugin start.
186 // Multiplied by OVERSAMPLE.
187 int total_dx;
188 int total_dy;
190 // Rotation motion tracking
191 float total_angle;
193 // Current motion vector for drawing vectors
194 int current_dx;
195 int current_dy;
196 float current_angle;
200 // Oversampled current frame for motion estimation
201 int32_t *search_area;
202 int search_size;
205 // The layer to track motion in.
206 int reference_layer;
207 // The layer to apply motion in.
208 int target_layer;
210 // Pointer to the source and destination of each operation.
211 // These are fully allocated buffers.
213 // The previous reference frame for global motion tracking
214 VFrame *prev_global_ref;
215 // The current reference frame for global motion tracking
216 VFrame *current_global_ref;
217 // The input target frame for global motion tracking
218 VFrame *global_target_src;
219 // The output target frame for global motion tracking
220 VFrame *global_target_dst;
222 // The previous reference frame for rotation tracking
223 VFrame *prev_rotate_ref;
224 // The current reference frame for rotation tracking
225 VFrame *current_rotate_ref;
226 // The input target frame for rotation tracking.
227 VFrame *rotate_target_src;
228 // The output target frame for rotation tracking.
229 VFrame *rotate_target_dst;
231 // The output of process_buffer
232 VFrame *output_frame;
233 int w;
234 int h;
261 class MotionScanPackage : public LoadPackage
263 public:
264 MotionScanPackage();
266 // For multiple blocks
267 int block_x1, block_y1, block_x2, block_y2;
268 int scan_x1, scan_y1, scan_x2, scan_y2;
269 int dx;
270 int dy;
271 int64_t max_difference;
272 int64_t min_difference;
273 int64_t min_pixel;
274 int is_border;
275 int valid;
276 // For single block
277 int pixel;
278 int64_t difference1;
279 int64_t difference2;
282 class MotionScanCache
284 public:
285 MotionScanCache(int x, int y, int64_t difference);
286 int x, y;
287 int64_t difference;
290 class MotionScanUnit : public LoadClient
292 public:
293 MotionScanUnit(MotionScan *server, MotionMain *plugin);
294 ~MotionScanUnit();
296 void process_package(LoadPackage *package);
297 int64_t get_cache(int x, int y);
298 void put_cache(int x, int y, int64_t difference);
300 MotionScan *server;
301 MotionMain *plugin;
303 ArrayList<MotionScanCache*> cache;
304 Mutex *cache_lock;
307 class MotionScan : public LoadServer
309 public:
310 MotionScan(MotionMain *plugin,
311 int total_clients,
312 int total_packages);
313 ~MotionScan();
315 friend class MotionScanUnit;
317 void init_packages();
318 LoadClient* new_client();
319 LoadPackage* new_package();
321 // Invoke the motion engine for a search
322 // Frame before motion
323 void scan_frame(VFrame *previous_frame,
324 // Frame after motion
325 VFrame *current_frame);
326 int64_t get_cache(int x, int y);
327 void put_cache(int x, int y, int64_t difference);
329 // Change between previous frame and current frame multiplied by
330 // OVERSAMPLE
331 int dx_result;
332 int dy_result;
334 private:
335 VFrame *previous_frame;
336 // Frame after motion
337 VFrame *current_frame;
338 MotionMain *plugin;
339 int skip;
340 // For single block
341 int block_x1;
342 int block_x2;
343 int block_y1;
344 int block_y2;
345 int scan_x1;
346 int scan_y1;
347 int scan_x2;
348 int scan_y2;
349 int total_pixels;
350 int total_steps;
351 int subpixel;
354 ArrayList<MotionScanCache*> cache;
355 Mutex *cache_lock;
370 class RotateScanPackage : public LoadPackage
372 public:
373 RotateScanPackage();
374 float angle;
375 int64_t difference;
378 class RotateScanCache
380 public:
381 RotateScanCache(float angle, int64_t difference);
382 float angle;
383 int64_t difference;
386 class RotateScanUnit : public LoadClient
388 public:
389 RotateScanUnit(RotateScan *server, MotionMain *plugin);
390 ~RotateScanUnit();
392 void process_package(LoadPackage *package);
394 RotateScan *server;
395 MotionMain *plugin;
396 AffineEngine *rotater;
397 VFrame *temp;
400 class RotateScan : public LoadServer
402 public:
403 RotateScan(MotionMain *plugin,
404 int total_clients,
405 int total_packages);
406 ~RotateScan();
408 friend class RotateScanUnit;
410 void init_packages();
411 LoadClient* new_client();
412 LoadPackage* new_package();
414 // Invoke the motion engine for a search
415 // Frame before rotation
416 float scan_frame(VFrame *previous_frame,
417 // Frame after rotation
418 VFrame *current_frame,
419 // Pivot
420 int block_x,
421 int block_y);
422 int64_t get_cache(float angle);
423 void put_cache(float angle, int64_t difference);
426 // Angle result
427 float result;
429 private:
430 VFrame *previous_frame;
431 // Frame after motion
432 VFrame *current_frame;
434 MotionMain *plugin;
435 int skip;
437 // Pivot
438 int block_x;
439 int block_y;
440 // Block to rotate
441 int block_x1;
442 int block_x2;
443 int block_y1;
444 int block_y2;
445 // Area to compare
446 int scan_x;
447 int scan_y;
448 int scan_w;
449 int scan_h;
450 // Range of angles to compare
451 float scan_angle1, scan_angle2;
452 int total_steps;
454 ArrayList<RotateScanCache*> cache;
455 Mutex *cache_lock;
461 #endif