no longer need ffmpeg patch0 which was for Termux
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / motion-hv / motion-hv.h
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2016 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-hv.inc"
35 #include "motionwindow-hv.inc"
36 #include "overlayframe.inc"
37 #include "pluginvclient.h"
38 #include "vframe.inc"
39
40 class MotionHVMain;
41 class MotionHVWindow;
42
43
44
45
46 // Limits of global range in percent
47 #define MIN_RADIUS 1
48 #define MAX_RADIUS 100
49
50 // Limits of rotation range in degrees
51 #define MIN_ROTATION 1
52 #define MAX_ROTATION 25
53
54 // Limits of block size in percent.
55 #define MIN_BLOCK 1
56 #define MAX_BLOCK 100
57
58 // Limits of block count
59 #define MIN_BLOCKS 1
60 #define MAX_BLOCKS 200
61
62 // Precision of rotation
63 #define MIN_ANGLE 0.0001
64
65
66 class MotionHVConfig
67 {
68 public:
69         MotionHVConfig();
70
71         int equivalent(MotionHVConfig &that);
72         void copy_from(MotionHVConfig &that);
73         void interpolate(MotionHVConfig &prev,
74                 MotionHVConfig &next,
75                 int64_t prev_frame,
76                 int64_t next_frame,
77                 int64_t current_frame);
78         void boundaries();
79
80         int block_count;
81         int global_range_w;
82         int global_range_h;
83 // Range of angles above and below center rotation angle to search
84         int rotation_range;
85 // Center angle of rotation search
86         int rotation_center;
87         int magnitude;
88         int rotate_magnitude;
89         int return_speed;
90         int rotate_return_speed;
91         int draw_vectors;
92 // Percent of image size
93         int global_block_w;
94         int global_block_h;
95 //      int rotation_block_w;
96 //      int rotation_block_h;
97 // Number of search positions in each refinement of the log search
98 //      int global_positions;
99 //      int rotate_positions;
100 // Block position in percentage 0 - 100
101         double block_x;
102         double block_y;
103
104         int horizontal_only;
105         int vertical_only;
106         int global;
107         int rotate;
108 // Track or stabilize, single pixel, scan only, or nothing
109         int action_type;
110 // Recalculate, no calculate, save, or load coordinates from disk
111         int tracking_type;
112 // Track a single frame, previous frame, or previous frame same block
113         int tracking_object;
114
115 #if 0
116         enum
117         {
118 // action_type
119                 TRACK,
120                 STABILIZE,
121                 TRACK_PIXEL,
122                 STABILIZE_PIXEL,
123                 NOTHING,
124 // mode2
125                 RECALCULATE,
126                 SAVE,
127                 LOAD,
128                 NO_CALCULATE,
129 // tracking_object
130                 TRACK_SINGLE,
131                 TRACK_PREVIOUS,
132                 PREVIOUS_SAME_BLOCK
133         };
134 #endif
135
136
137 // Number of single frame to track relative to timeline start
138         int64_t track_frame;
139 // Master layer
140         int bottom_is_master;
141 };
142
143
144
145
146 class MotionHVMain : public PluginVClient
147 {
148 public:
149         MotionHVMain(PluginServer *server);
150         ~MotionHVMain();
151
152         int process_buffer(VFrame **frame,
153                 int64_t start_position,
154                 double frame_rate);
155         void process_global();
156         void process_rotation();
157         void draw_vectors(VFrame *frame);
158         int is_multichannel();
159         int is_realtime();
160         void save_data(KeyFrame *keyframe);
161         void read_data(KeyFrame *keyframe);
162         void update_gui();
163 // Calculate frame to copy from and frame to move
164         void calculate_pointers(VFrame **frame, VFrame **src, VFrame **dst);
165         void allocate_temp(int w, int h, int color_model);
166
167         PLUGIN_CLASS_MEMBERS2(MotionHVConfig)
168
169
170         static void draw_pixel(VFrame *frame, int x, int y);
171         static void draw_line(VFrame *frame, int x1, int y1, int x2, int y2);
172         void draw_arrow(VFrame *frame, int x1, int y1, int x2, int y2);
173
174 // Number of the previous reference frame on the timeline.
175         int64_t previous_frame_number;
176 // The frame compared with the previous frame to get the motion.
177 // It is moved to compensate for motion and copied to the previous_frame.
178         VFrame *temp_frame;
179         MotionHVScan *engine;
180 //      RotateScan *motion_rotate;
181         OverlayFrame *overlayer;
182         AffineEngine *rotate_engine;
183
184 // Accumulation of all global tracks since the plugin start.
185 // Multiplied by OVERSAMPLE.
186         int total_dx;
187         int total_dy;
188
189 // Rotation motion tracking
190         float total_angle;
191
192 // Current motion vector for drawing vectors
193         int current_dx;
194         int current_dy;
195         float current_angle;
196
197
198
199 // Oversampled current frame for motion estimation
200         int32_t *search_area;
201         int search_size;
202
203
204 // The layer to track motion in.
205         int reference_layer;
206 // The layer to apply motion in.
207         int target_layer;
208
209 // Pointer to the source and destination of each operation.
210 // These are fully allocated buffers.
211
212 // The previous reference frame for global motion tracking
213         VFrame *prev_global_ref;
214 // The current reference frame for global motion tracking
215         VFrame *current_global_ref;
216 // The input target frame for global motion tracking
217         VFrame *global_target_src;
218 // The output target frame for global motion tracking
219         VFrame *global_target_dst;
220
221 // The previous reference frame for rotation tracking
222         VFrame *prev_rotate_ref;
223 // The current reference frame for rotation tracking
224         VFrame *current_rotate_ref;
225 // The input target frame for rotation tracking.
226         VFrame *rotate_target_src;
227 // The output target frame for rotation tracking.
228         VFrame *rotate_target_dst;
229
230 // The output of process_buffer
231         VFrame *output_frame;
232         int w;
233         int h;
234 };
235
236
237 class MotionHvVVFrame : public VFrame
238 {
239 public:
240         MotionHvVVFrame(VFrame *vfrm, int n);
241         int draw_pixel(int x, int y);
242         int n;
243 };
244
245
246
247
248
249
250
251
252
253
254 #endif
255
256
257
258
259
260