merge hv v6, rework trace methods
[goodguy/history.git] / cinelerra-5.1 / plugins / motion / rotatescan.C
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 #include "affine.h"
23 #include "bcsignals.h"
24 #include "clip.h"
25 #include "motionscan.h"
26 #include "rotatescan.h"
27 #include "motion.h"
28 #include "mutex.h"
29 #include "vframe.h"
30
31
32
33
34
35
36
37
38
39 RotateScanPackage::RotateScanPackage()
40 {
41 }
42
43
44 RotateScanUnit::RotateScanUnit(RotateScan *server, MotionMain *plugin)
45  : LoadClient(server)
46 {
47         this->server = server;
48         this->plugin = plugin;
49         rotater = 0;
50         temp = 0;
51 }
52
53 RotateScanUnit::~RotateScanUnit()
54 {
55         delete rotater;
56         delete temp;
57 }
58
59 void RotateScanUnit::process_package(LoadPackage *package)
60 {
61         if(server->skip) return;
62         RotateScanPackage *pkg = (RotateScanPackage*)package;
63
64         if((pkg->difference = server->get_cache(pkg->angle)) < 0)
65         {
66 //printf("RotateScanUnit::process_package %d\n", __LINE__);
67                 int color_model = server->previous_frame->get_color_model();
68                 int pixel_size = BC_CModels::calculate_pixelsize(color_model);
69                 int row_bytes = server->previous_frame->get_bytes_per_line();
70
71                 if(!rotater)
72                         rotater = new AffineEngine(1, 1);
73                 if(!temp) temp = new VFrame(0,
74                         -1,
75                         server->previous_frame->get_w(),
76                         server->previous_frame->get_h(),
77                         color_model,
78                         -1);
79 //printf("RotateScanUnit::process_package %d\n", __LINE__);
80
81
82 // Rotate original block size
83 //              rotater->set_viewport(server->block_x1,
84 //                      server->block_y1,
85 //                      server->block_x2 - server->block_x1,
86 //                      server->block_y2 - server->block_y1);
87                 rotater->set_in_viewport(server->block_x1,
88                         server->block_y1,
89                         server->block_x2 - server->block_x1,
90                         server->block_y2 - server->block_y1);
91                 rotater->set_out_viewport(server->block_x1,
92                         server->block_y1,
93                         server->block_x2 - server->block_x1,
94                         server->block_y2 - server->block_y1);
95 //              rotater->set_pivot(server->block_x, server->block_y);
96                 rotater->set_in_pivot(server->block_x, server->block_y);
97                 rotater->set_out_pivot(server->block_x, server->block_y);
98 //printf("RotateScanUnit::process_package %d\n", __LINE__);
99                 rotater->rotate(temp,
100                         server->previous_frame,
101                         pkg->angle);
102
103 // Scan reduced block size
104 //plugin->output_frame->copy_from(server->current_frame);
105 //plugin->output_frame->copy_from(temp);
106 // printf("RotateScanUnit::process_package %d %d %d %d %d\n",
107 // __LINE__,
108 // server->scan_x,
109 // server->scan_y,
110 // server->scan_w,
111 // server->scan_h);
112 // Clamp coordinates
113                 int x1 = server->scan_x;
114                 int y1 = server->scan_y;
115                 int x2 = x1 + server->scan_w;
116                 int y2 = y1 + server->scan_h;
117                 x2 = MIN(temp->get_w(), x2);
118                 y2 = MIN(temp->get_h(), y2);
119                 x2 = MIN(server->current_frame->get_w(), x2);
120                 y2 = MIN(server->current_frame->get_h(), y2);
121                 x1 = MAX(0, x1);
122                 y1 = MAX(0, y1);
123
124                 if(x2 > x1 && y2 > y1)
125                 {
126                         pkg->difference = MotionScan::abs_diff(
127                                 temp->get_rows()[y1] + x1 * pixel_size,
128                                 server->current_frame->get_rows()[y1] + x1 * pixel_size,
129                                 row_bytes,
130                                 x2 - x1,
131                                 y2 - y1,
132                                 color_model);
133 //printf("RotateScanUnit::process_package %d\n", __LINE__);
134                         server->put_cache(pkg->angle, pkg->difference);
135                 }
136
137 // printf("RotateScanUnit::process_package 10 x=%d y=%d w=%d h=%d block_x=%d block_y=%d angle=%f scan_w=%d scan_h=%d diff=%lld\n",
138 // server->block_x1,
139 // server->block_y1,
140 // server->block_x2 - server->block_x1,
141 // server->block_y2 - server->block_y1,
142 // server->block_x,
143 // server->block_y,
144 // pkg->angle,
145 // server->scan_w,
146 // server->scan_h,
147 // pkg->difference);
148         }
149 }
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172 RotateScan::RotateScan(MotionMain *plugin,
173         int total_clients,
174         int total_packages)
175  : LoadServer(
176 //1, 1
177 total_clients, total_packages
178 )
179 {
180         this->plugin = plugin;
181         cache_lock = new Mutex("RotateScan::cache_lock");
182 }
183
184
185 RotateScan::~RotateScan()
186 {
187         delete cache_lock;
188 }
189
190 void RotateScan::init_packages()
191 {
192         for(int i = 0; i < get_total_packages(); i++)
193         {
194                 RotateScanPackage *pkg = (RotateScanPackage*)get_package(i);
195                 pkg->angle = i *
196                         (scan_angle2 - scan_angle1) /
197                         (total_steps - 1) +
198                         scan_angle1;
199         }
200 }
201
202 LoadClient* RotateScan::new_client()
203 {
204         return new RotateScanUnit(this, plugin);
205 }
206
207 LoadPackage* RotateScan::new_package()
208 {
209         return new RotateScanPackage;
210 }
211
212
213 float RotateScan::scan_frame(VFrame *previous_frame,
214         VFrame *current_frame,
215         int block_x,
216         int block_y)
217 {
218         skip = 0;
219         this->block_x = block_x;
220         this->block_y = block_y;
221
222 //printf("RotateScan::scan_frame %d\n", __LINE__);
223         switch(plugin->config.tracking_type)
224         {
225                 case MotionScan::NO_CALCULATE:
226                         result = plugin->config.rotation_center;
227                         skip = 1;
228                         break;
229
230                 case MotionScan::LOAD:
231                 {
232                         char string[BCTEXTLEN];
233                         sprintf(string,
234                                 "%s%06ld",
235                                 ROTATION_FILE,
236                                 plugin->get_source_position());
237                         FILE *input = fopen(string, "r");
238                         if(input)
239                         {
240                                 int temp = fscanf(input, "%f", &result);
241                                 fclose(input);
242                                 skip = 1;
243                         }
244                         else
245                         {
246                                 perror("RotateScan::scan_frame LOAD");
247                         }
248                         break;
249                 }
250         }
251
252
253
254
255
256
257
258
259         this->previous_frame = previous_frame;
260         this->current_frame = current_frame;
261         int w = current_frame->get_w();
262         int h = current_frame->get_h();
263         int block_w = w * plugin->config.global_block_w / 100;
264         int block_h = h * plugin->config.global_block_h / 100;
265
266         if(this->block_x - block_w / 2 < 0) block_w = this->block_x * 2;
267         if(this->block_y - block_h / 2 < 0) block_h = this->block_y * 2;
268         if(this->block_x + block_w / 2 > w) block_w = (w - this->block_x) * 2;
269         if(this->block_y + block_h / 2 > h) block_h = (h - this->block_y) * 2;
270
271         block_x1 = this->block_x - block_w / 2;
272         block_x2 = this->block_x + block_w / 2;
273         block_y1 = this->block_y - block_h / 2;
274         block_y2 = this->block_y + block_h / 2;
275
276
277 // Calculate the maximum area available to scan after rotation.
278 // Must be calculated from the starting range because of cache.
279 // Get coords of rectangle after rotation.
280         double center_x = this->block_x;
281         double center_y = this->block_y;
282         double max_angle = plugin->config.rotation_range;
283         double base_angle1 = atan((float)block_h / block_w);
284         double base_angle2 = atan((float)block_w / block_h);
285         double target_angle1 = base_angle1 + max_angle * 2 * M_PI / 360;
286         double target_angle2 = base_angle2 + max_angle * 2 * M_PI / 360;
287         double radius = sqrt(block_w * block_w + block_h * block_h) / 2;
288         double x1 = center_x - cos(target_angle1) * radius;
289         double y1 = center_y - sin(target_angle1) * radius;
290         double x2 = center_x + sin(target_angle2) * radius;
291         double y2 = center_y - cos(target_angle2) * radius;
292         double x3 = center_x - sin(target_angle2) * radius;
293         double y3 = center_y + cos(target_angle2) * radius;
294
295 // Track top edge to find greatest area.
296         double max_area1 = 0;
297         double max_x1 = 0;
298         double max_y1 = 0;
299         for(double x = x1; x < x2; x++)
300         {
301                 double y = y1 + (y2 - y1) * (x - x1) / (x2 - x1);
302                 if(x >= center_x && x < block_x2 && y >= block_y1 && y < center_y)
303                 {
304                         double area = fabs(x - center_x) * fabs(y - center_y);
305                         if(area > max_area1)
306                         {
307                                 max_area1 = area;
308                                 max_x1 = x;
309                                 max_y1 = y;
310                         }
311                 }
312         }
313
314 // Track left edge to find greatest area.
315         double max_area2 = 0;
316         double max_x2 = 0;
317         double max_y2 = 0;
318         for(double y = y1; y < y3; y++)
319         {
320                 double x = x1 + (x3 - x1) * (y - y1) / (y3 - y1);
321                 if(x >= block_x1 && x < center_x && y >= block_y1 && y < center_y)
322                 {
323                         double area = fabs(x - center_x) * fabs(y - center_y);
324                         if(area > max_area2)
325                         {
326                                 max_area2 = area;
327                                 max_x2 = x;
328                                 max_y2 = y;
329                         }
330                 }
331         }
332
333         double max_x, max_y;
334         max_x = max_x2;
335         max_y = max_y1;
336
337 // Get reduced scan coords
338         scan_w = (int)(fabs(max_x - center_x) * 2);
339         scan_h = (int)(fabs(max_y - center_y) * 2);
340         scan_x = (int)(center_x - scan_w / 2);
341         scan_y = (int)(center_y - scan_h / 2);
342 // printf("RotateScan::scan_frame center=%d,%d scan=%d,%d %dx%d\n",
343 // this->block_x, this->block_y, scan_x, scan_y, scan_w, scan_h);
344 // printf("    angle_range=%f block= %d,%d,%d,%d\n", max_angle, block_x1, block_y1, block_x2, block_y2);
345
346 // Determine min angle from size of block
347         double angle1 = atan((double)block_h / block_w);
348         double angle2 = atan((double)(block_h - 1) / (block_w + 1));
349         double min_angle = fabs(angle2 - angle1) / OVERSAMPLE;
350         min_angle = MAX(min_angle, MIN_ANGLE);
351
352 //printf("RotateScan::scan_frame %d min_angle=%f\n", __LINE__, min_angle * 360 / 2 / M_PI);
353
354         cache.remove_all_objects();
355
356
357         if(!skip)
358         {
359                 if(previous_frame->data_matches(current_frame))
360                 {
361 //printf("RotateScan::scan_frame: frames match.  Skipping.\n");
362                         result = plugin->config.rotation_center;
363                         skip = 1;
364                 }
365         }
366
367         if(!skip)
368         {
369 // Initial search range
370                 float angle_range = max_angle;
371                 result = plugin->config.rotation_center;
372                 total_steps = plugin->config.rotate_positions;
373
374
375                 while(angle_range >= min_angle * total_steps)
376                 {
377                         scan_angle1 = result - angle_range;
378                         scan_angle2 = result + angle_range;
379
380
381                         set_package_count(total_steps);
382 //set_package_count(1);
383                         process_packages();
384
385                         int64_t min_difference = -1;
386                         for(int i = 0; i < get_total_packages(); i++)
387                         {
388                                 RotateScanPackage *pkg = (RotateScanPackage*)get_package(i);
389                                 if(pkg->difference < min_difference || min_difference == -1)
390                                 {
391                                         min_difference = pkg->difference;
392                                         result = pkg->angle;
393                                 }
394 //break;
395                         }
396
397                         angle_range /= 2;
398
399 //break;
400                 }
401         }
402
403 //printf("RotateScan::scan_frame %d\n", __LINE__);
404
405         if(!skip && plugin->config.tracking_type == MotionScan::SAVE)
406         {
407                 char string[BCTEXTLEN];
408                 sprintf(string,
409                         "%s%06ld",
410                         ROTATION_FILE,
411                         plugin->get_source_position());
412                 FILE *output = fopen(string, "w");
413                 if(output)
414                 {
415                         fprintf(output, "%f\n", result);
416                         fclose(output);
417                 }
418                 else
419                 {
420                         perror("RotateScan::scan_frame SAVE");
421                 }
422         }
423
424 //printf("RotateScan::scan_frame %d angle=%f\n", __LINE__, result);
425
426
427
428         return result;
429 }
430
431 int64_t RotateScan::get_cache(float angle)
432 {
433         int64_t result = -1;
434         cache_lock->lock("RotateScan::get_cache");
435         for(int i = 0; i < cache.total; i++)
436         {
437                 RotateScanCache *ptr = cache.values[i];
438                 if(fabs(ptr->angle - angle) <= MIN_ANGLE)
439                 {
440                         result = ptr->difference;
441                         break;
442                 }
443         }
444         cache_lock->unlock();
445         return result;
446 }
447
448 void RotateScan::put_cache(float angle, int64_t difference)
449 {
450         RotateScanCache *ptr = new RotateScanCache(angle, difference);
451         cache_lock->lock("RotateScan::put_cache");
452         cache.append(ptr);
453         cache_lock->unlock();
454 }
455
456
457
458
459
460
461
462
463
464 RotateScanCache::RotateScanCache(float angle, int64_t difference)
465 {
466         this->angle = angle;
467         this->difference = difference;
468 }
469
470
471