ffmpeg frame reset retry to push design limits
[goodguy/history.git] / cinelerra-5.1 / plugins / motion-hv / motionscan-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 MOTIONSCAN_H
23 #define MOTIONSCAN_H
24
25
26 #include "affine.inc"
27 #include "loadbalance.h"
28 #include "vframe.inc"
29 #include <stdint.h>
30
31 class MotionHVScan;
32
33 #define OVERSAMPLE 4
34 #define MOTION_FILE "/tmp/m"
35 #define ROTATION_FILE "/tmp/r"
36
37 class MotionHVScanPackage : public LoadPackage
38 {
39 public:
40         MotionHVScanPackage();
41
42 // For multiple blocks
43 // Position of stationary block after downsampling
44         int block_x1, block_y1, block_x2, block_y2;
45 // index of rotated frame
46         int angle_step;
47
48         int dx;
49         int dy;
50         int64_t max_difference;
51         int64_t min_difference;
52         int64_t min_pixel;
53         int is_border;
54         int valid;
55         int64_t difference1;
56         int64_t difference2;
57 // Search position of current package to nearest pixel with downsampling
58         int search_x;
59         int search_y;
60 // Subpixel of search position
61         int sub_x;
62         int sub_y;
63 };
64
65 class MotionHVScanUnit : public LoadClient
66 {
67 public:
68         MotionHVScanUnit(MotionHVScan *server);
69         ~MotionHVScanUnit();
70
71         void process_package(LoadPackage *package);
72         void subpixel(MotionHVScanPackage *pkg);
73         void single_pixel(MotionHVScanPackage *pkg);
74
75         MotionHVScan *server;
76 };
77
78 class MotionHVScan : public LoadServer
79 {
80 public:
81         MotionHVScan(int total_clients,
82                 int total_packages);
83         ~MotionHVScan();
84
85         friend class MotionHVScanUnit;
86
87         void init_packages();
88         LoadClient* new_client();
89         LoadPackage* new_package();
90 // Test for identical frames before scanning
91         void set_test_match(int value);
92
93 // Invoke the motion engine for a search
94 // Frame before motion
95         void scan_frame(VFrame *previous_frame,
96                 VFrame *current_frame,
97                 int global_range_w, // in pixels
98                 int global_range_h,
99                 int global_block_w, // in pixels
100                 int global_block_h,
101                 int block_x, // in pixels
102                 int block_y,
103                 int frame_type,
104                 int tracking_type,
105                 int action_type,
106                 int horizontal_only,
107                 int vertical_only,
108                 int source_position,
109                 int total_dx, // in pixels * OVERSAMPLE
110                 int total_dy,
111                 int global_origin_x, // in pixels
112                 int global_origin_y,
113                 int do_motion,
114                 int do_rotate,
115                 double rotation_center, // in deg
116                 double rotation_range);
117
118         static int64_t abs_diff(unsigned char *prev_ptr,
119                 unsigned char *current_ptr,
120                 int row_bytes,
121                 int w,
122                 int h,
123                 int color_model);
124         static int64_t abs_diff_sub(unsigned char *prev_ptr,
125                 unsigned char *current_ptr,
126                 int row_bytes,
127                 int w,
128                 int h,
129                 int color_model,
130                 int sub_x,
131                 int sub_y);
132
133
134         static void clamp_scan(int w,
135                 int h,
136                 int *block_x1,
137                 int *block_y1,
138                 int *block_x2,
139                 int *block_y2,
140                 int *scan_x1,
141                 int *scan_y1,
142                 int *scan_x2,
143                 int *scan_y2,
144                 int use_absolute);
145
146 // Change between previous frame and current frame multiplied by
147 // OVERSAMPLE
148         int dx_result;
149         int dy_result;
150         float dr_result;
151
152         enum
153         {
154 // action_type
155                 TRACK,
156                 STABILIZE,
157                 TRACK_PIXEL,
158                 STABILIZE_PIXEL,
159                 NOTHING
160         };
161
162         enum
163         {
164 // tracking_type
165                 CALCULATE,
166                 SAVE,
167                 LOAD,
168                 NO_CALCULATE
169         };
170
171         enum
172         {
173 // frame_type
174                 TRACK_SINGLE,
175                 TRACK_PREVIOUS,
176                 PREVIOUS_SAME_BLOCK
177         };
178
179 private:
180         void downsample_frame(VFrame *dst,
181                 VFrame *src,
182                 int downsample);
183         void pixel_search(int &x_result, int &y_result, double &r_result);
184         void subpixel_search(int &x_result, int &y_result);
185         double step_to_angle(int step, double center);
186
187 //      double calculate_variance(unsigned char *current_ptr,
188 //              int row_bytes,
189 //              int w,
190 //              int h,
191 //              int color_model);
192         double calculate_range(unsigned char *current_ptr,
193                 int row_bytes,
194                 int w,
195                 int h,
196                 int color_model);
197
198
199
200         AffineEngine *rotater;
201 // Pointer to downsampled frame before motion
202         VFrame *previous_frame;
203 // Pointer to downsampled frame after motion
204         VFrame *current_frame;
205 // Frames passed from user
206         VFrame *previous_frame_arg;
207         VFrame *current_frame_arg;
208 // Downsampled frames
209         VFrame *downsampled_previous;
210         VFrame *downsampled_current;
211 // rotated versions of current_frame
212         VFrame **rotated_current;
213 // allocation of rotated_current array, a copy of angle_steps
214         int total_rotated;
215 // Test for identical frames before processing
216 // Faster to skip it if the frames are usually different
217         int test_match;
218         int skip;
219 // macroblocks didn't have enough data
220         int failed;
221 // For single block
222         int block_x1;
223         int block_x2;
224         int block_y1;
225         int block_y2;
226         int scan_w;
227         int scan_h;
228         int scan_x1;
229         int scan_y1;
230         int scan_x2;
231         int scan_y2;
232         double scan_angle1, scan_angle2;
233         int y_steps;
234         int x_steps;
235         int angle_steps;
236 // in deg
237         double angle_step;
238         int subpixel;
239         int horizontal_only;
240         int vertical_only;
241         int global_origin_x;
242         int global_origin_y;
243         int action_type;
244         int current_downsample;
245         int downsampled_w;
246         int downsampled_h;
247         int total_steps;
248         int do_motion;
249         int do_rotate;
250         int rotation_pass;
251 // in deg
252         double rotation_center;
253         double rotation_range;
254 };
255
256
257
258 #endif