remove whitespace at eol
[goodguy/history.git] / cinelerra-5.1 / plugins / motion.new / 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 "bchash.inc"
31 #include "filexml.inc"
32 #include "keyframe.inc"
33 #include "loadbalance.h"
34 #include "motionscan.inc"
35 #include "motionwindow.inc"
36 #include "overlayframe.inc"
37 #include "pluginvclient.h"
38 #include "rotateframe.inc"
39 #include "vframe.inc"
40
41 class MotionMain;
42 class MotionWindow;
43 class RotateScan;
44
45
46
47
48 // Limits of global range in percent
49 #define MIN_RADIUS 1
50 #define MAX_RADIUS 100
51
52 // Limits of rotation range in degrees
53 #define MIN_ROTATION 1
54 #define MAX_ROTATION 25
55
56 // Limits of block size in percent.
57 #define MIN_BLOCK 1
58 #define MAX_BLOCK 100
59
60 // Limits of block count
61 #define MIN_BLOCKS 1
62 #define MAX_BLOCKS 200
63
64 // Precision of rotation
65 #define MIN_ANGLE 0.0001
66
67 #define ROTATION_FILE "/tmp/rotate"
68
69 class MotionConfig
70 {
71 public:
72         MotionConfig();
73
74         int equivalent(MotionConfig &that);
75         void copy_from(MotionConfig &that);
76         void interpolate(MotionConfig &prev,
77                 MotionConfig &next,
78                 int64_t prev_frame,
79                 int64_t next_frame,
80                 int64_t current_frame);
81         void boundaries();
82
83         int block_count;
84         int global_range_w;
85         int global_range_h;
86 // Range of angles above and below center rotation angle to search
87         int rotation_range;
88 // Center angle of rotation search
89         int rotation_center;
90         int magnitude;
91         int rotate_magnitude;
92         int return_speed;
93         int rotate_return_speed;
94         int draw_vectors;
95 // Percent of image size
96         int global_block_w;
97         int global_block_h;
98         int rotation_block_w;
99         int rotation_block_h;
100 // Number of search positions in each refinement of the log search
101         int global_positions;
102         int rotate_positions;
103 // Block position in percentage 0 - 100
104         double block_x;
105         double block_y;
106
107         int horizontal_only;
108         int vertical_only;
109         int global;
110         int rotate;
111 // Track or stabilize, single pixel, scan only, or nothing
112         int action_type;
113 // Recalculate, no calculate, save, or load coordinates from disk
114         int tracking_type;
115 // Track a single frame, previous frame, or previous frame same block
116         int tracking_object;
117
118 #if 0
119         enum
120         {
121 // action_type
122                 TRACK,
123                 STABILIZE,
124                 TRACK_PIXEL,
125                 STABILIZE_PIXEL,
126                 NOTHING,
127 // mode2
128                 RECALCULATE,
129                 SAVE,
130                 LOAD,
131                 NO_CALCULATE,
132 // tracking_object
133                 TRACK_SINGLE,
134                 TRACK_PREVIOUS,
135                 PREVIOUS_SAME_BLOCK
136         };
137 #endif
138
139
140 // Number of single frame to track relative to timeline start
141         int64_t track_frame;
142 // Master layer
143         int bottom_is_master;
144 };
145
146
147
148
149 class MotionMain : public PluginVClient
150 {
151 public:
152         MotionMain(PluginServer *server);
153         ~MotionMain();
154
155         int process_buffer(VFrame **frame,
156                 int64_t start_position,
157                 double frame_rate);
158         void process_global();
159         void process_rotation();
160         void draw_vectors(VFrame *frame);
161         int is_multichannel();
162         int is_realtime();
163         void save_data(KeyFrame *keyframe);
164         void read_data(KeyFrame *keyframe);
165         void update_gui();
166 // Calculate frame to copy from and frame to move
167         void calculate_pointers(VFrame **frame, VFrame **src, VFrame **dst);
168         void allocate_temp(int w, int h, int color_model);
169
170         PLUGIN_CLASS_MEMBERS2(MotionConfig)
171
172
173         static void draw_pixel(VFrame *frame, int x, int y);
174         static void draw_line(VFrame *frame, int x1, int y1, int x2, int y2);
175         void draw_arrow(VFrame *frame, int x1, int y1, int x2, int y2);
176
177 // Number of the previous reference frame on the timeline.
178         int64_t previous_frame_number;
179 // The frame compared with the previous frame to get the motion.
180 // It is moved to compensate for motion and copied to the previous_frame.
181         VFrame *temp_frame;
182         MotionScan *engine;
183         RotateScan *motion_rotate;
184         OverlayFrame *overlayer;
185         AffineEngine *rotate_engine;
186
187 // Accumulation of all global tracks since the plugin start.
188 // Multiplied by OVERSAMPLE.
189         int total_dx;
190         int total_dy;
191
192 // Rotation motion tracking
193         float total_angle;
194
195 // Current motion vector for drawing vectors
196         int current_dx;
197         int current_dy;
198         float current_angle;
199
200
201
202 // Oversampled current frame for motion estimation
203         int32_t *search_area;
204         int search_size;
205
206
207 // The layer to track motion in.
208         int reference_layer;
209 // The layer to apply motion in.
210         int target_layer;
211
212 // Pointer to the source and destination of each operation.
213 // These are fully allocated buffers.
214
215 // The previous reference frame for global motion tracking
216         VFrame *prev_global_ref;
217 // The current reference frame for global motion tracking
218         VFrame *current_global_ref;
219 // The input target frame for global motion tracking
220         VFrame *global_target_src;
221 // The output target frame for global motion tracking
222         VFrame *global_target_dst;
223
224 // The previous reference frame for rotation tracking
225         VFrame *prev_rotate_ref;
226 // The current reference frame for rotation tracking
227         VFrame *current_rotate_ref;
228 // The input target frame for rotation tracking.
229         VFrame *rotate_target_src;
230 // The output target frame for rotation tracking.
231         VFrame *rotate_target_dst;
232
233 // The output of process_buffer
234         VFrame *output_frame;
235         int w;
236         int h;
237 };
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262 class RotateScanPackage : public LoadPackage
263 {
264 public:
265         RotateScanPackage();
266         float angle;
267         int64_t difference;
268 };
269
270 class RotateScanCache
271 {
272 public:
273         RotateScanCache(float angle, int64_t difference);
274         float angle;
275         int64_t difference;
276 };
277
278 class RotateScanUnit : public LoadClient
279 {
280 public:
281         RotateScanUnit(RotateScan *server, MotionMain *plugin);
282         ~RotateScanUnit();
283
284         void process_package(LoadPackage *package);
285
286         RotateScan *server;
287         MotionMain *plugin;
288         AffineEngine *rotater;
289         VFrame *temp;
290 };
291
292 class RotateScan : public LoadServer
293 {
294 public:
295         RotateScan(MotionMain *plugin,
296                 int total_clients,
297                 int total_packages);
298         ~RotateScan();
299
300         friend class RotateScanUnit;
301
302         void init_packages();
303         LoadClient* new_client();
304         LoadPackage* new_package();
305
306 // Invoke the motion engine for a search
307 // Frame before rotation
308         float scan_frame(VFrame *previous_frame,
309 // Frame after rotation
310                 VFrame *current_frame,
311 // Pivot
312                 int block_x,
313                 int block_y);
314         int64_t get_cache(float angle);
315         void put_cache(float angle, int64_t difference);
316
317
318 // Angle result
319         float result;
320
321 private:
322         VFrame *previous_frame;
323 // Frame after motion
324         VFrame *current_frame;
325
326         MotionMain *plugin;
327         int skip;
328
329 // Pivot
330         int block_x;
331         int block_y;
332 // Block to rotate
333         int block_x1;
334         int block_x2;
335         int block_y1;
336         int block_y2;
337 // Area to compare
338         int scan_x;
339         int scan_y;
340         int scan_w;
341         int scan_h;
342 // Range of angles to compare
343         float scan_angle1, scan_angle2;
344         int total_steps;
345
346         ArrayList<RotateScanCache*> cache;
347         Mutex *cache_lock;
348 };
349
350
351
352
353 #endif
354
355
356
357
358
359