Credit Andrew - fix vorbis audio which was scratchy and ensure aging plugin does...
[goodguy/cinelerra.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
36 class MotionScanPackage : public LoadPackage
37 {
38 public:
39         MotionScanPackage();
40
41 // For multiple blocks
42 // Position of stationary block
43         int block_x1, block_y1, block_x2, block_y2;
44 // Range of positions to scan
45         int scan_x1, scan_y1, scan_x2, scan_y2;
46         int dx;
47         int dy;
48         int64_t max_difference;
49         int64_t min_difference;
50         int64_t min_pixel;
51         int is_border;
52         int valid;
53 // For single block
54         int step;
55         int64_t difference1;
56         int64_t difference2;
57 // Search position to nearest pixel
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 MotionScanCache
66 {
67 public:
68         MotionScanCache(int x, int y, int64_t difference);
69         int x, y;
70         int64_t difference;
71 };
72
73 class MotionScanUnit : public LoadClient
74 {
75 public:
76         MotionScanUnit(MotionScan *server);
77         ~MotionScanUnit();
78
79         void process_package(LoadPackage *package);
80         int64_t get_cache(int x, int y);
81         void put_cache(int x, int y, int64_t difference);
82
83         MotionScan *server;
84
85         ArrayList<MotionScanCache*> cache;
86         Mutex *cache_lock;
87 };
88
89 class MotionScan : public LoadServer
90 {
91 public:
92         MotionScan(MotionMain *plugin,
93                 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         void scan_frame(VFrame *previous_frame, // Frame before motion
107                 VFrame *current_frame, // Frame after motion
108                 int global_range_w, int global_range_h, int global_block_w, int global_block_h,
109                 double block_x, double block_y, int frame_type, int tracking_type, int action_type,
110                 int horizontal_only, int vertical_only, int source_position, int total_steps,
111                 int total_dx, int total_dy, int global_origin_x, int global_origin_y, int passno,
112                 int load_ok=0, int load_dx=0, int load_dy=0);
113         int64_t get_cache(int x, int y);
114         void put_cache(int x, int y, int64_t difference);
115
116         static int64_t abs_diff(unsigned char *prev_ptr, unsigned char *current_ptr,
117                 int row_bytes, int w, int h, int color_model);
118         static int64_t abs_diff_sub(unsigned char *prev_ptr, unsigned char *current_ptr,
119                 int row_bytes, int w, int h, int color_model, int sub_x, int sub_y);
120
121         static void clamp_scan(int w, int h,
122                 int *block_x1, int *block_y1, int *block_x2, int *block_y2,
123                 int *scan_x1, int *scan_y1, int *scan_x2, int *scan_y2,
124                 int use_absolute);
125
126 // Change between previous frame and current frame multiplied by
127 // OVERSAMPLE
128         int dx_result, dy_result;
129
130 // Currently best expected location of scan block
131         int x_result, y_result;
132
133         enum { // action_type
134                 TRACK,
135                 STABILIZE,
136                 TRACK_PIXEL,
137                 STABILIZE_PIXEL,
138                 NOTHING
139         };
140
141         enum { // tracking_type
142                 CALCULATE,
143                 SAVE,
144                 LOAD,
145                 NO_CALCULATE
146         };
147
148         enum { // frame_type
149                 TRACK_SINGLE,
150                 TRACK_PREVIOUS,
151                 PREVIOUS_SAME_BLOCK
152         };
153
154 private:
155 // Pointer to downsampled frame before motion
156         VFrame *previous_frame;
157 // Pointer to downsampled frame after motion
158         VFrame *current_frame;
159 // Frames passed from user
160         VFrame *previous_frame_arg;
161         VFrame *current_frame_arg;
162 // Downsampled frames
163         VFrame *downsampled_previous;
164         VFrame *downsampled_current;
165         MotionMain *plugin;
166 // Test for identical frames before processing
167 // Faster to skip it if the frames are usually different
168         int test_match;
169         int skip;
170 // For single block
171         int block_x1;
172         int block_x2;
173         int block_y1;
174         int block_y2;
175         int scan_x1;
176         int scan_y1;
177         int scan_x2;
178         int scan_y2;
179         int total_pixels;
180         int total_steps;
181         int edge_steps;
182         int y_steps;
183         int x_steps;
184         int subpixel;
185         int horizontal_only;
186         int vertical_only;
187         int global_origin_x;
188         int global_origin_y;
189
190         ArrayList<MotionScanCache*> cache;
191         Mutex *cache_lock;
192 //      DownSampleServer *downsample;
193 };
194
195
196
197 #endif