initial commit
[goodguy/history.git] / cinelerra-5.0 / plugins / motion2point / motion.h
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  * 
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.
10  * 
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.
15  * 
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
19  * 
20  */
21
22 #ifndef MOTION_H
23 #define MOTION_H
24
25 #include <math.h>
26 #include <stdint.h>
27 #include <string.h>
28
29 #include "affine.inc"
30 #include "motionscan.inc"
31 #include "bchash.inc"
32 #include "filexml.inc"
33 #include "keyframe.inc"
34 #include "loadbalance.h"
35 #include "motionwindow.inc"
36 #include "overlayframe.inc"
37 #include "pluginvclient.h"
38 #include "rotateframe.inc"
39 #include "vframe.inc"
40
41 class MotionMain2;
42 class MotionWindow;
43 class RotateScan;
44
45
46 #define OVERSAMPLE 4
47 #define TOTAL_POINTS 2
48 // Point 0 is used for determining translation
49 #define TRANSLATION_POINT 0
50 // Point 1 is used for determining rotation and scaling
51 #define ROTATION_POINT 1
52
53 // Limits of global range in percent
54 #define MIN_RADIUS 1
55 #define MAX_RADIUS 50
56
57 // Limits of global origin in percent
58 #define MIN_ORIGIN -50
59 #define MAX_ORIGIN 50
60
61 // Limits of rotation range in degrees
62 #define MIN_ROTATION 1
63 #define MAX_ROTATION 25
64
65 // Limits of block size in percent.
66 #define MIN_BLOCK 1
67 #define MAX_BLOCK 100
68
69 // Limits of block count
70 #define MIN_BLOCKS 1
71 #define MAX_BLOCKS 200
72
73 #define MOTION_FILE "/tmp/m"
74 #define ROTATION_FILE "/tmp/r"
75
76 class MotionConfig
77 {
78 public:
79         MotionConfig();
80
81         int equivalent(MotionConfig &that);
82         void copy_from(MotionConfig &that);
83         void interpolate(MotionConfig &prev, 
84                 MotionConfig &next, 
85                 int64_t prev_frame, 
86                 int64_t next_frame, 
87                 int64_t current_frame);
88         void boundaries();
89
90 // Enable each point
91         int global[TOTAL_POINTS];
92 // Search range 1-100
93         int global_range_w[TOTAL_POINTS];
94         int global_range_h[TOTAL_POINTS];
95 // Search origin relative to block position -50-50
96         int global_origin_x[TOTAL_POINTS];
97         int global_origin_y[TOTAL_POINTS];
98         int draw_vectors[TOTAL_POINTS];
99 // Size of comparison block.  Percent of image size
100         int global_block_w[TOTAL_POINTS];
101         int global_block_h[TOTAL_POINTS];
102 // Block position in percentage 0 - 100
103         double block_x[TOTAL_POINTS];
104         double block_y[TOTAL_POINTS];
105
106 // Number of search positions in each refinement of the log search
107         int global_positions;
108 // Maximum position offset
109         int magnitude;
110         int return_speed;
111 // Track single direction only
112         int horizontal_only;
113         int vertical_only;
114 // Number of single frame to track relative to timeline start
115         int64_t track_frame;
116 // Master layer
117         int bottom_is_master;
118 // Track or stabilize, single pixel, scan only, or nothing
119         int action;
120 // Recalculate, no calculate, save, or load coordinates from disk
121         int calculation;
122 // Track a single frame, previous frame, or previous frame same block
123         int tracking_object;
124 };
125
126
127
128
129 class MotionMain2 : public PluginVClient
130 {
131 public:
132         MotionMain2(PluginServer *server);
133         ~MotionMain2();
134
135         int process_buffer(VFrame **frame,
136                 int64_t start_position,
137                 double frame_rate);
138         void scan_motion(int point);
139         void apply_motion();
140         void draw_vectors(VFrame *frame, int point);
141         int is_multichannel();
142         int is_realtime();
143         void save_data(KeyFrame *keyframe);
144         void read_data(KeyFrame *keyframe);
145         void update_gui();
146 // Calculate frame to copy from and frame to move
147         void calculate_pointers(VFrame **frame, VFrame **src, VFrame **dst);
148         void allocate_temp(int w, int h, int color_model);
149
150         PLUGIN_CLASS_MEMBERS(MotionConfig)
151
152         int64_t abs_diff(unsigned char *prev_ptr,
153                 unsigned char *current_ptr,
154                 int row_bytes,
155                 int w,
156                 int h,
157                 int color_model);
158         int64_t abs_diff_sub(unsigned char *prev_ptr,
159                 unsigned char *current_ptr,
160                 int row_bytes,
161                 int w,
162                 int h,
163                 int color_model,
164                 int sub_x,
165                 int sub_y);
166
167         static void draw_pixel(VFrame *frame, int x, int y);
168         static void draw_line(VFrame *frame, int x1, int y1, int x2, int y2);
169         void draw_arrow(VFrame *frame, int x1, int y1, int x2, int y2);
170 // Get start and end of current motion vector in pixels
171         void get_current_vector(float *origin_x,
172                 float *origin_y,
173                 float *current_x1, 
174                 float *current_y1, 
175                 float *current_x2, 
176                 float *current_y2, 
177                 int point);
178
179 // Number of the previous reference frame on the timeline.
180         int64_t previous_frame_number;
181 // The frame compared with the previous frame to get the motion.
182 // It is moved to compensate for motion and copied to the previous_frame.
183         VFrame *temp_frame;
184         MotionScan *engine;
185         OverlayFrame *overlayer;
186         AffineEngine *affine;
187
188 // Accumulation of all global tracks since the plugin start.
189 // Multiplied by OVERSAMPLE.
190         int total_dx[TOTAL_POINTS];
191         int total_dy[TOTAL_POINTS];
192
193
194
195 // Current motion vector for drawing vectors
196         int current_dx[TOTAL_POINTS];
197         int current_dy[TOTAL_POINTS];
198
199
200
201 // Oversampled current frame for motion estimation
202         int32_t *search_area;
203         int search_size;
204
205
206 // The layer to track motion in.
207         int reference_layer;
208 // The layer to apply motion in.
209         int target_layer;
210
211 // Pointer to the source and destination of each operation.
212 // These are fully allocated buffers.
213
214 // The previous reference frame for global motion tracking
215         VFrame *prev_global_ref;
216 // The current reference frame for global motion tracking
217         VFrame *current_global_ref;
218 // The input target frame for global motion tracking
219         VFrame *global_target_src;
220 // The output target frame for global motion tracking
221         VFrame *global_target_dst;
222
223 // The output of process_buffer
224         VFrame *output_frame;
225         int w;
226         int h;
227 };
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250 #if 0
251
252
253 class MotionScanPackage : public LoadPackage
254 {
255 public:
256         MotionScanPackage();
257
258 // For multiple blocks
259         int block_x1, block_y1, block_x2, block_y2;
260         int scan_x1, scan_y1, scan_x2, scan_y2;
261         int dx;
262         int dy;
263         int64_t max_difference;
264         int64_t min_difference;
265         int64_t min_pixel;
266         int is_border;
267         int valid;
268 // For single block
269         int pixel;
270         int64_t difference1;
271         int64_t difference2;
272 };
273
274 class MotionScanCache
275 {
276 public:
277         MotionScanCache(int x, int y, int64_t difference);
278         int x, y;
279         int64_t difference;
280 };
281
282 class MotionScanUnit : public LoadClient
283 {
284 public:
285         MotionScanUnit(MotionScan *server, MotionMain2 *plugin);
286         ~MotionScanUnit();
287
288         void process_package(LoadPackage *package);
289         int64_t get_cache(int x, int y);
290         void put_cache(int x, int y, int64_t difference);
291
292         MotionScan *server;
293         MotionMain2 *plugin;
294
295         ArrayList<MotionScanCache*> cache;
296         Mutex *cache_lock;
297 };
298
299 class MotionScan : public LoadServer
300 {
301 public:
302         MotionScan(MotionMain2 *plugin, 
303                 int total_clients, 
304                 int total_packages);
305         ~MotionScan();
306
307         friend class MotionScanUnit;
308
309         void init_packages();
310         LoadClient* new_client();
311         LoadPackage* new_package();
312
313 // Invoke the motion engine for a search
314 // Frame before motion
315         void scan_frame(VFrame *previous_frame,
316 // Frame after motion
317                 VFrame *current_frame,
318 // Motion point
319                 int point);
320         int64_t get_cache(int x, int y);
321         void put_cache(int x, int y, int64_t difference);
322
323 // Change between previous frame and current frame multiplied by 
324 // OVERSAMPLE
325         int dx_result;
326         int dy_result;
327
328 private:
329         VFrame *previous_frame;
330 // Frame after motion
331         VFrame *current_frame;
332         MotionMain2 *plugin;
333         int skip;
334 // For single block
335         int block_x1;
336         int block_x2;
337         int block_y1;
338         int block_y2;
339         int scan_x1;
340         int scan_y1;
341         int scan_x2;
342         int scan_y2;
343         int total_pixels;
344         int total_steps;
345         int point;
346         int subpixel;
347
348
349         ArrayList<MotionScanCache*> cache;
350         Mutex *cache_lock;
351 };
352
353 #endif // 0
354
355
356
357
358
359
360
361
362
363
364
365
366 #endif
367
368
369
370
371
372