add auto zoombar/status color, fix 3 batchrender boobies, rotate plugin tweaks, add...
[goodguy/cinelerra.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 "motioncache-hv.inc"
29 #include "vframe.inc"
30 #include <stdint.h>
31
32 class MotionHVScan;
33
34 #define OVERSAMPLE 4
35 #define MOTION_FILE "/tmp/m"
36 #define ROTATION_FILE "/tmp/r"
37
38 class MotionHVScanPackage : public LoadPackage
39 {
40 public:
41         MotionHVScanPackage();
42
43 // For multiple blocks
44 // Position of stationary block after downsampling
45         int block_x1, block_y1, block_x2, block_y2;
46 // index of rotated frame
47         int angle_step;
48
49         int dx;
50         int dy;
51         int64_t max_difference;
52         int64_t min_difference;
53         int64_t min_pixel;
54         int is_border;
55         int valid;
56         int64_t difference1;
57         int64_t difference2;
58 // Search position of current package to nearest pixel with downsampling
59         int search_x;
60         int search_y;
61 // Subpixel of search position
62         int sub_x;
63         int sub_y;
64 };
65
66 class MotionHVScanUnit : public LoadClient
67 {
68 public:
69         MotionHVScanUnit(MotionHVScan *server);
70         ~MotionHVScanUnit();
71
72         void process_package(LoadPackage *package);
73         void subpixel(MotionHVScanPackage *pkg);
74         void single_pixel(MotionHVScanPackage *pkg);
75
76         MotionHVScan *server;
77 };
78
79 class MotionHVScan : public LoadServer
80 {
81 public:
82         MotionHVScan(int total_clients,
83                 int total_packages);
84         ~MotionHVScan();
85
86         friend class MotionHVScanUnit;
87
88         void init_packages();
89         LoadClient* new_client();
90         LoadPackage* new_package();
91 // Test for identical frames before scanning
92         void set_test_match(int value);
93         void set_cache(MotionHVCache *cache);
94
95 // Invoke the motion engine for a search
96 // Frame before motion
97         void scan_frame(VFrame *previous_frame,
98                 VFrame *current_frame,
99                 int global_range_w, // in pixels
100                 int global_range_h,
101                 int global_block_w, // in pixels
102                 int global_block_h,
103                 int block_x, // in pixels
104                 int block_y,
105                 int frame_type,
106                 int tracking_type,
107                 int action_type,
108                 int horizontal_only,
109                 int vertical_only,
110                 int source_position,
111                 int total_dx, // in pixels * OVERSAMPLE
112                 int total_dy,
113                 int global_origin_x, // in pixels
114                 int global_origin_y,
115                 int do_motion,
116                 int do_rotate,
117                 double rotation_center, // in deg
118                 double rotation_range);
119
120         static int64_t abs_diff(unsigned char *prev_ptr,
121                 unsigned char *current_ptr,
122                 int row_bytes,
123                 int w,
124                 int h,
125                 int color_model);
126         static int64_t abs_diff_sub(unsigned char *prev_ptr,
127                 unsigned char *current_ptr,
128                 int row_bytes,
129                 int w,
130                 int h,
131                 int color_model,
132                 int sub_x,
133                 int sub_y);
134
135
136         static void clamp_scan(int w,
137                 int h,
138                 int *block_x1,
139                 int *block_y1,
140                 int *block_x2,
141                 int *block_y2,
142                 int *scan_x1,
143                 int *scan_y1,
144                 int *scan_x2,
145                 int *scan_y2,
146                 int use_absolute);
147
148 // Change between previous frame and current frame multiplied by
149 // OVERSAMPLE
150         int dx_result;
151         int dy_result;
152         float dr_result;
153
154         enum
155         {
156 // action_type
157                 TRACK,
158                 STABILIZE,
159                 TRACK_PIXEL,
160                 STABILIZE_PIXEL,
161                 NOTHING
162         };
163
164         enum
165         {
166 // tracking_type
167                 CALCULATE,
168                 SAVE,
169                 LOAD,
170                 NO_CALCULATE
171         };
172
173         enum
174         {
175 // frame_type
176                 TRACK_SINGLE,
177                 TRACK_PREVIOUS,
178                 PREVIOUS_SAME_BLOCK
179         };
180
181 private:
182         void downsample_frame(VFrame *dst,
183                 VFrame *src,
184                 int downsample);
185         void pixel_search(int &x_result, int &y_result, double &r_result);
186         void subpixel_search(int &x_result, int &y_result);
187         double step_to_angle(int step, double center);
188
189 //      double calculate_variance(unsigned char *current_ptr,
190 //              int row_bytes,
191 //              int w,
192 //              int h,
193 //              int color_model);
194         double calculate_range(unsigned char *current_ptr,
195                 int row_bytes,
196                 int w,
197                 int h,
198                 int color_model);
199
200
201         MotionHVCache *downsample_cache;
202         int shared_downsample;
203         AffineEngine *rotater;
204 // Pointer to downsampled frame before motion
205         VFrame *previous_frame;
206 // Pointer to downsampled frame after motion
207         VFrame *current_frame;
208 // Frames passed from user
209         VFrame *previous_frame_arg;
210         VFrame *current_frame_arg;
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