8e756ee38e2297f6839943453651284e4d15afbf
[goodguy/history.git] / cinelerra-5.1 / plugins / motion / motionscan.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 MOTIONSCAN_H
23 #define MOTIONSCAN_H
24
25
26 #include "arraylist.h"
27 //#include "../downsample/downsampleengine.inc"
28 #include "loadbalance.h"
29 #include "vframe.inc"
30 #include <stdint.h>
31
32 class MotionScan;
33
34 #define OVERSAMPLE 4
35 #define MOTION_FILE "/tmp/m"
36
37 class MotionScanPackage : public LoadPackage
38 {
39 public:
40         MotionScanPackage();
41
42 // For multiple blocks
43 // Position of stationary block
44         int block_x1, block_y1, block_x2, block_y2;
45 // Range of positions to scan
46         int scan_x1, scan_y1, scan_x2, scan_y2;
47         int dx;
48         int dy;
49         int64_t max_difference;
50         int64_t min_difference;
51         int64_t min_pixel;
52         int is_border;
53         int valid;
54 // For single block
55         int step;
56         int64_t difference1;
57         int64_t difference2;
58 // Search position to nearest pixel
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 MotionScanCache
67 {
68 public:
69         MotionScanCache(int x, int y, int64_t difference);
70         int x, y;
71         int64_t difference;
72 };
73
74 class MotionScanUnit : public LoadClient
75 {
76 public:
77         MotionScanUnit(MotionScan *server);
78         ~MotionScanUnit();
79
80         void process_package(LoadPackage *package);
81         int64_t get_cache(int x, int y);
82         void put_cache(int x, int y, int64_t difference);
83
84         MotionScan *server;
85
86         ArrayList<MotionScanCache*> cache;
87         Mutex *cache_lock;
88 };
89
90 class MotionScan : public LoadServer
91 {
92 public:
93         MotionScan(int total_clients, 
94                 int total_packages);
95         ~MotionScan();
96
97         friend class MotionScanUnit;
98
99         void init_packages();
100         LoadClient* new_client();
101         LoadPackage* new_package();
102 // Test for identical frames before scanning
103         void set_test_match(int value);
104
105 // Invoke the motion engine for a search
106 // Frame before motion
107         void scan_frame(VFrame *previous_frame,
108 // Frame after motion
109                 VFrame *current_frame,
110                 int global_range_w,
111                 int global_range_h,
112                 int global_block_w,
113                 int global_block_h,
114                 double block_x,
115                 double block_y,
116                 int frame_type,
117                 int tracking_type,
118                 int action_type,
119                 int horizontal_only,
120                 int vertical_only,
121                 int source_position,
122                 int total_steps,
123                 int total_dx,
124                 int total_dy,
125                 int global_origin_x,
126                 int global_origin_y);
127         int64_t get_cache(int x, int y);
128         void put_cache(int x, int y, int64_t difference);
129
130         static int64_t abs_diff(unsigned char *prev_ptr,
131                 unsigned char *current_ptr,
132                 int row_bytes,
133                 int w,
134                 int h,
135                 int color_model);
136         static int64_t abs_diff_sub(unsigned char *prev_ptr,
137                 unsigned char *current_ptr,
138                 int row_bytes,
139                 int w,
140                 int h,
141                 int color_model,
142                 int sub_x,
143                 int sub_y);
144
145
146         static void clamp_scan(int w, 
147                 int h, 
148                 int *block_x1,
149                 int *block_y1,
150                 int *block_x2,
151                 int *block_y2,
152                 int *scan_x1,
153                 int *scan_y1,
154                 int *scan_x2,
155                 int *scan_y2,
156                 int use_absolute);
157
158 // Change between previous frame and current frame multiplied by 
159 // OVERSAMPLE
160         int dx_result;
161         int dy_result;
162
163         enum
164         {
165 // action_type
166                 TRACK,
167                 STABILIZE,
168                 TRACK_PIXEL,
169                 STABILIZE_PIXEL,
170                 NOTHING
171         };
172         
173         enum
174         {
175 // tracking_type
176                 CALCULATE,
177                 SAVE,
178                 LOAD,
179                 NO_CALCULATE
180         };
181         
182         enum
183         {
184 // frame_type
185                 TRACK_SINGLE,
186                 TRACK_PREVIOUS,
187                 PREVIOUS_SAME_BLOCK
188         };
189
190 private:
191 // Pointer to downsampled frame before motion
192         VFrame *previous_frame;
193 // Pointer to downsampled frame after motion
194         VFrame *current_frame;
195 // Frames passed from user
196         VFrame *previous_frame_arg;
197         VFrame *current_frame_arg;
198 // Downsampled frames
199         VFrame *downsampled_previous;
200         VFrame *downsampled_current;
201 // Test for identical frames before processing
202 // Faster to skip it if the frames are usually different
203         int test_match;
204         int skip;
205 // For single block
206         int block_x1;
207         int block_x2;
208         int block_y1;
209         int block_y2;
210         int scan_x1;
211         int scan_y1;
212         int scan_x2;
213         int scan_y2;
214         int total_pixels;
215         int total_steps;
216         int edge_steps;
217         int y_steps;
218         int x_steps;
219         int subpixel;
220         int horizontal_only;
221         int vertical_only;
222         int global_origin_x;
223         int global_origin_y;
224
225         ArrayList<MotionScanCache*> cache;
226         Mutex *cache_lock;
227 //      DownSampleServer *downsample;
228 };
229
230
231
232 #endif