add auto zoombar/status color, fix 3 batchrender boobies, rotate plugin tweaks, add...
[goodguy/cinelerra.git] / cinelerra-5.1 / 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-hv.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 class Motion2VVFrame : public VFrame
232 {
233 public:
234         Motion2VVFrame(VFrame *vfrm, int n);
235         int draw_pixel(int x, int y);
236         int n;
237 };
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259 #if 0
260
261
262 class MotionScanPackage : public LoadPackage
263 {
264 public:
265         MotionScanPackage();
266
267 // For multiple blocks
268         int block_x1, block_y1, block_x2, block_y2;
269         int scan_x1, scan_y1, scan_x2, scan_y2;
270         int dx;
271         int dy;
272         int64_t max_difference;
273         int64_t min_difference;
274         int64_t min_pixel;
275         int is_border;
276         int valid;
277 // For single block
278         int pixel;
279         int64_t difference1;
280         int64_t difference2;
281 };
282
283 class MotionScanCache
284 {
285 public:
286         MotionScanCache(int x, int y, int64_t difference);
287         int x, y;
288         int64_t difference;
289 };
290
291 class MotionScanUnit : public LoadClient
292 {
293 public:
294         MotionScanUnit(MotionScan *server, MotionMain2 *plugin);
295         ~MotionScanUnit();
296
297         void process_package(LoadPackage *package);
298         int64_t get_cache(int x, int y);
299         void put_cache(int x, int y, int64_t difference);
300
301         MotionScan *server;
302         MotionMain2 *plugin;
303
304         ArrayList<MotionScanCache*> cache;
305         Mutex *cache_lock;
306 };
307
308 class MotionScan : public LoadServer
309 {
310 public:
311         MotionScan(MotionMain2 *plugin,
312                 int total_clients,
313                 int total_packages);
314         ~MotionScan();
315
316         friend class MotionScanUnit;
317
318         void init_packages();
319         LoadClient* new_client();
320         LoadPackage* new_package();
321
322 // Invoke the motion engine for a search
323 // Frame before motion
324         void scan_frame(VFrame *previous_frame,
325 // Frame after motion
326                 VFrame *current_frame,
327 // Motion point
328                 int point);
329         int64_t get_cache(int x, int y);
330         void put_cache(int x, int y, int64_t difference);
331
332 // Change between previous frame and current frame multiplied by
333 // OVERSAMPLE
334         int dx_result;
335         int dy_result;
336
337 private:
338         VFrame *previous_frame;
339 // Frame after motion
340         VFrame *current_frame;
341         MotionMain2 *plugin;
342         int skip;
343 // For single block
344         int block_x1;
345         int block_x2;
346         int block_y1;
347         int block_y2;
348         int scan_x1;
349         int scan_y1;
350         int scan_x2;
351         int scan_y2;
352         int total_pixels;
353         int total_steps;
354         int point;
355         int subpixel;
356
357
358         ArrayList<MotionScanCache*> cache;
359         Mutex *cache_lock;
360 };
361
362 #endif // 0
363
364
365
366
367
368
369
370
371
372
373
374
375 #endif
376
377
378
379
380
381