4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
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.
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.
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
23 //#include "../downsample/downsampleengine.h"
25 #include "motionscan.h"
31 // The module which does the actual scanning
33 MotionScanPackage::MotionScanPackage()
44 MotionScanUnit::MotionScanUnit(MotionScan *server)
47 this->server = server;
48 cache_lock = new Mutex("MotionScanUnit::cache_lock");
51 MotionScanUnit::~MotionScanUnit()
58 void MotionScanUnit::process_package(LoadPackage *package)
60 MotionScanPackage *pkg = (MotionScanPackage*)package;
61 int w = server->current_frame->get_w();
62 int h = server->current_frame->get_h();
63 int color_model = server->current_frame->get_color_model();
64 int pixel_size = BC_CModels::calculate_pixelsize(color_model);
65 int row_bytes = server->current_frame->get_bytes_per_line();
82 pkg->difference1 = server->get_cache(pkg->search_x, pkg->search_y);
83 if(pkg->difference1 < 0)
85 //printf("MotionScanUnit::process_package 1 search_x=%d search_y=%d scan_x1=%d scan_y1=%d scan_x2=%d scan_y2=%d x_steps=%d y_steps=%d\n",
86 //pkg->search_x, pkg->search_y, pkg->scan_x1, pkg->scan_y1, pkg->scan_x2, pkg->scan_y2, server->x_steps, server->y_steps);
87 // Pointers to first pixel in each block
88 unsigned char *prev_ptr = server->previous_frame->get_rows()[
90 pkg->search_x * pixel_size;
91 unsigned char *current_ptr = server->current_frame->get_rows()[
93 pkg->block_x1 * pixel_size;
96 pkg->difference1 = MotionScan::abs_diff(prev_ptr,
99 pkg->block_x2 - pkg->block_x1,
100 pkg->block_y2 - pkg->block_y1,
103 // printf("MotionScanUnit::process_package %d search_x=%d search_y=%d diff=%lld\n",
104 // __LINE__, server->block_x1 - pkg->search_x, server->block_y1 - pkg->search_y, pkg->difference1);
105 server->put_cache(pkg->search_x, pkg->search_y, pkg->difference1);
126 unsigned char *prev_ptr = server->previous_frame->get_rows()[
128 pkg->search_x * pixel_size;
129 unsigned char *current_ptr = server->current_frame->get_rows()[
131 pkg->block_x1 * pixel_size;
133 // With subpixel, there are two ways to compare each position, one by shifting
134 // the previous frame and two by shifting the current frame.
135 pkg->difference1 = MotionScan::abs_diff_sub(prev_ptr,
138 pkg->block_x2 - pkg->block_x1,
139 pkg->block_y2 - pkg->block_y1,
143 pkg->difference2 = MotionScan::abs_diff_sub(current_ptr,
146 pkg->block_x2 - pkg->block_x1,
147 pkg->block_y2 - pkg->block_y1,
151 // printf("MotionScanUnit::process_package sub_x=%d sub_y=%d search_x=%d search_y=%d diff1=%lld diff2=%lld\n",
157 // pkg->difference2);
174 int64_t MotionScanUnit::get_cache(int x, int y)
177 cache_lock->lock("MotionScanUnit::get_cache");
178 for(int i = 0; i < cache.total; i++)
180 MotionScanCache *ptr = cache.values[i];
181 if(ptr->x == x && ptr->y == y)
183 result = ptr->difference;
187 cache_lock->unlock();
191 void MotionScanUnit::put_cache(int x, int y, int64_t difference)
193 MotionScanCache *ptr = new MotionScanCache(x, y, difference);
194 cache_lock->lock("MotionScanUnit::put_cache");
196 cache_lock->unlock();
209 MotionScan::MotionScan(int total_clients,
213 total_clients, total_packages
217 cache_lock = new Mutex("MotionScan::cache_lock");
218 downsampled_previous = 0;
219 downsampled_current = 0;
223 MotionScan::~MotionScan()
226 delete downsampled_previous;
227 delete downsampled_current;
228 // delete downsample;
232 void MotionScan::init_packages()
234 // Set package coords
235 //printf("MotionScan::init_packages %d %d\n", __LINE__, get_total_packages());
236 for(int i = 0; i < get_total_packages(); i++)
238 MotionScanPackage *pkg = (MotionScanPackage*)get_package(i);
240 pkg->block_x1 = block_x1;
241 pkg->block_x2 = block_x2;
242 pkg->block_y1 = block_y1;
243 pkg->block_y2 = block_y2;
244 pkg->scan_x1 = scan_x1;
245 pkg->scan_x2 = scan_x2;
246 pkg->scan_y1 = scan_y1;
247 pkg->scan_y2 = scan_y2;
249 pkg->difference1 = 0;
250 pkg->difference2 = 0;
257 pkg->search_x = pkg->scan_x1 + (pkg->step % x_steps) *
258 (scan_x2 - scan_x1) / x_steps;
259 pkg->search_y = pkg->scan_y1 + (pkg->step / x_steps) *
260 (scan_y2 - scan_y1) / y_steps;
266 pkg->sub_x = pkg->step % (OVERSAMPLE * 2);
267 pkg->sub_y = pkg->step / (OVERSAMPLE * 2);
279 pkg->search_x = pkg->scan_x1 + pkg->sub_x / OVERSAMPLE + 1;
280 pkg->search_y = pkg->scan_y1 + pkg->sub_y / OVERSAMPLE + 1;
281 pkg->sub_x %= OVERSAMPLE;
282 pkg->sub_y %= OVERSAMPLE;
286 // printf("MotionScan::init_packages %d i=%d search_x=%d search_y=%d sub_x=%d sub_y=%d\n",
295 // printf("MotionScan::init_packages %d %d,%d %d,%d %d,%d\n",
306 LoadClient* MotionScan::new_client()
308 return new MotionScanUnit(this);
311 LoadPackage* MotionScan::new_package()
313 return new MotionScanPackage;
317 void MotionScan::set_test_match(int value)
319 this->test_match = value;
322 void MotionScan::scan_frame(VFrame *previous_frame,
323 VFrame *current_frame,
342 this->previous_frame_arg = previous_frame;
343 this->current_frame_arg = current_frame;
344 this->horizontal_only = horizontal_only;
345 this->vertical_only = vertical_only;
346 this->previous_frame = previous_frame_arg;
347 this->current_frame = current_frame_arg;
348 this->global_origin_x = global_origin_x;
349 this->global_origin_y = global_origin_y;
352 cache.remove_all_objects();
355 int w = current_frame->get_w();
356 int h = current_frame->get_h();
358 // Initial search parameters
359 int scan_w = w * global_range_w / 100;
360 int scan_h = h * global_range_h / 100;
361 int block_w = w * global_block_w / 100;
362 int block_h = h * global_block_h / 100;
364 // Location of block in previous frame
365 block_x1 = (int)(w * block_x / 100 - block_w / 2);
366 block_y1 = (int)(h * block_y / 100 - block_h / 2);
367 block_x2 = (int)(w * block_x / 100 + block_w / 2);
368 block_y2 = (int)(h * block_y / 100 + block_h / 2);
370 // Offset to location of previous block. This offset needn't be very accurate
371 // since it's the offset of the previous image and current image we want.
372 if(frame_type == MotionScan::TRACK_PREVIOUS)
374 block_x1 += total_dx / OVERSAMPLE;
375 block_y1 += total_dy / OVERSAMPLE;
376 block_x2 += total_dx / OVERSAMPLE;
377 block_y2 += total_dy / OVERSAMPLE;
382 switch(tracking_type)
385 case MotionScan::NO_CALCULATE:
391 case MotionScan::LOAD:
393 //printf("MotionScan::scan_frame %d\n", __LINE__);
394 // Load result from disk
395 char string[BCTEXTLEN];
396 sprintf(string, "%s%06d",
399 FILE *input = fopen(string, "r");
418 if(!skip && test_match)
420 if(previous_frame->data_matches(current_frame))
422 printf("MotionScan::scan_frame: data matches. skipping.\n");
432 //printf("MotionScan::scan_frame %d\n", __LINE__);
433 // Location of block in current frame
434 int origin_offset_x = this->global_origin_x * w / 100;
435 int origin_offset_y = this->global_origin_y * h / 100;
436 int x_result = block_x1 + origin_offset_x;
437 int y_result = block_y1 + origin_offset_y;
439 // printf("MotionScan::scan_frame 1 %d %d %d %d %d %d %d %d\n",
440 // block_x1 + block_w / 2,
441 // block_y1 + block_h / 2,
451 // Cache needs to be cleared if downsampling is used because the sums of
452 // different downsamplings can't be compared.
453 // Subpixel never uses the cache.
454 // cache.remove_all_objects();
455 scan_x1 = x_result - scan_w / 2;
456 scan_y1 = y_result - scan_h / 2;
457 scan_x2 = x_result + scan_w / 2;
458 scan_y2 = y_result + scan_h / 2;
462 // Zero out requested values
466 scan_y2 = block_y1 + 1;
471 scan_x2 = block_x1 + 1;
474 // printf("MotionScan::scan_frame 1 %d %d %d %d %d %d %d %d\n",
483 // Clamp the block coords before the scan so we get useful scan coords.
495 // printf("MotionScan::scan_frame 1 %d block_x1=%d block_y1=%d block_x2=%d block_y2=%d\n scan_x1=%d scan_y1=%d scan_x2=%d scan_y2=%d\n x_result=%d y_result=%d\n",
509 // Give up if invalid coords.
510 if(scan_y2 <= scan_y1 ||
511 scan_x2 <= scan_x1 ||
512 block_x2 <= block_x1 ||
513 block_y2 <= block_y1)
516 // For subpixel, the top row and left column are skipped
520 //printf("MotionScan::scan_frame %d %d %d\n", __LINE__, x_result, y_result);
521 // Scan every subpixel in a 2 pixel * 2 pixel square
522 total_pixels = (2 * OVERSAMPLE) * (2 * OVERSAMPLE);
524 this->total_steps = total_pixels;
525 // These aren't used in subpixel
526 this->x_steps = OVERSAMPLE * 2;
527 this->y_steps = OVERSAMPLE * 2;
529 set_package_count(this->total_steps);
532 // Get least difference
533 int64_t min_difference = -1;
534 for(int i = 0; i < get_total_packages(); i++)
536 MotionScanPackage *pkg = (MotionScanPackage*)get_package(i);
537 //printf("MotionScan::scan_frame %d search_x=%d search_y=%d sub_x=%d sub_y=%d diff1=%lld diff2=%lld\n",
538 //__LINE__, pkg->search_x, pkg->search_y, pkg->sub_x, pkg->sub_y, pkg->difference1, pkg->difference2);
539 if(pkg->difference1 < min_difference || min_difference == -1)
541 min_difference = pkg->difference1;
543 // The sub coords are 1 pixel up & left of the block coords
544 x_result = pkg->search_x * OVERSAMPLE + pkg->sub_x;
545 y_result = pkg->search_y * OVERSAMPLE + pkg->sub_y;
549 dx_result = block_x1 * OVERSAMPLE - x_result;
550 dy_result = block_y1 * OVERSAMPLE - y_result;
551 //printf("MotionScan::scan_frame %d dx_result=%d dy_result=%d diff=%lld\n",
552 //__LINE__, dx_result, dy_result, min_difference);
555 if(pkg->difference2 < min_difference)
557 min_difference = pkg->difference2;
559 x_result = pkg->search_x * OVERSAMPLE - pkg->sub_x;
560 y_result = pkg->search_y * OVERSAMPLE - pkg->sub_y;
562 dx_result = block_x1 * OVERSAMPLE - x_result;
563 dy_result = block_y1 * OVERSAMPLE - y_result;
564 //printf("MotionScan::scan_frame %d dx_result=%d dy_result=%d diff=%lld\n",
565 //__LINE__, dx_result, dy_result, min_difference);
574 total_pixels = (scan_x2 - scan_x1) * (scan_y2 - scan_y1);
575 this->total_steps = MIN(total_steps, total_pixels);
577 if(this->total_steps == total_pixels)
579 x_steps = scan_x2 - scan_x1;
580 y_steps = scan_y2 - scan_y1;
584 x_steps = (int)sqrt(this->total_steps);
585 y_steps = (int)sqrt(this->total_steps);
588 // Use downsampled images
589 // if(scan_x2 - scan_x1 > x_steps * 4 ||
590 // scan_y2 - scan_y1 > y_steps * 4)
592 // printf("MotionScan::scan_frame %d total_pixels=%d total_steps=%d x_steps=%d y_steps=%d x y steps=%d\n",
598 // x_steps * y_steps);
600 // if(!downsampled_previous ||
601 // !downsampled_previous->equivalent(previous_frame_arg))
603 // delete downsampled_previous;
604 // downsampled_previous = new VFrame(*previous_frame_arg);
607 // if(!downsampled_current ||
608 // !downsampled_current->equivalent(current_frame_arg))
610 // delete downsampled_current;
611 // downsampled_current = new VFrame(*current_frame_arg);
616 // downsample = new DownSampleServer(get_total_clients(),
617 // get_total_clients());
618 // downsample->process_frame(downsampled_previous,
619 // previous_frame_arg,
624 // (scan_y2 - scan_y1) / y_steps,
625 // (scan_x2 - scan_x1) / x_steps,
628 // downsample->process_frame(downsampled_current,
629 // current_frame_arg,
634 // (scan_y2 - scan_y1) / y_steps,
635 // (scan_x2 - scan_x1) / x_steps,
638 // this->previous_frame = downsampled_previous;
639 // this->current_frame = downsampled_current;
646 // printf("MotionScan::scan_frame %d this->total_steps=%d\n",
648 // this->total_steps);
651 set_package_count(this->total_steps);
654 // Get least difference
655 int64_t min_difference = -1;
656 for(int i = 0; i < get_total_packages(); i++)
658 MotionScanPackage *pkg = (MotionScanPackage*)get_package(i);
659 //printf("MotionScan::scan_frame %d search_x=%d search_y=%d sub_x=%d sub_y=%d diff1=%lld diff2=%lld\n",
660 //__LINE__, pkg->search_x, pkg->search_y, pkg->sub_x, pkg->sub_y, pkg->difference1, pkg->difference2);
661 if(pkg->difference1 < min_difference || min_difference == -1)
663 min_difference = pkg->difference1;
664 x_result = pkg->search_x;
665 y_result = pkg->search_y;
666 x_result *= OVERSAMPLE;
667 y_result *= OVERSAMPLE;
668 //printf("MotionScan::scan_frame %d x_result=%d y_result=%d diff=%lld\n",
669 //__LINE__, block_x1 * OVERSAMPLE - x_result, block_y1 * OVERSAMPLE - y_result, pkg->difference1);
674 // If a new search is required, rescale results back to pixels.
675 if(this->total_steps >= total_pixels)
677 // Single pixel accuracy reached. Now do exhaustive subpixel search.
678 if(action_type == MotionScan::STABILIZE ||
679 action_type == MotionScan::TRACK ||
680 action_type == MotionScan::NOTHING)
682 //printf("MotionScan::scan_frame %d %d %d\n", __LINE__, x_result, y_result);
683 x_result /= OVERSAMPLE;
684 y_result /= OVERSAMPLE;
691 // Fill in results and quit
692 dx_result = block_x1 * OVERSAMPLE - x_result;
693 dy_result = block_y1 * OVERSAMPLE - y_result;
694 //printf("MotionScan::scan_frame %d %d %d\n", __LINE__, dx_result, dy_result);
699 // Reduce scan area and try again
701 scan_w = (scan_x2 - scan_x1) / 2;
702 scan_h = (scan_y2 - scan_y1) / 2;
703 x_result /= OVERSAMPLE;
704 y_result /= OVERSAMPLE;
712 //printf("MotionScan::scan_frame %d\n", __LINE__);
715 if(vertical_only) dx_result = 0;
716 if(horizontal_only) dy_result = 0;
721 if(tracking_type == MotionScan::SAVE)
723 char string[BCTEXTLEN];
728 FILE *output = fopen(string, "w");
739 printf("MotionScan::scan_frame %d: save coordinate failed", __LINE__);
743 // printf("MotionScan::scan_frame %d dx=%.2f dy=%.2f\n",
745 // (float)this->dx_result / OVERSAMPLE,
746 // (float)this->dy_result / OVERSAMPLE);
765 int64_t MotionScan::get_cache(int x, int y)
768 cache_lock->lock("MotionScan::get_cache");
769 for(int i = 0; i < cache.total; i++)
771 MotionScanCache *ptr = cache.values[i];
772 if(ptr->x == x && ptr->y == y)
774 result = ptr->difference;
778 cache_lock->unlock();
782 void MotionScan::put_cache(int x, int y, int64_t difference)
784 MotionScanCache *ptr = new MotionScanCache(x, y, difference);
785 cache_lock->lock("MotionScan::put_cache");
787 cache_lock->unlock();
792 #define ABS_DIFF(type, temp_type, multiplier, components) \
794 temp_type result_temp = 0; \
795 for(int i = 0; i < h; i++) \
797 type *prev_row = (type*)prev_ptr; \
798 type *current_row = (type*)current_ptr; \
799 for(int j = 0; j < w; j++) \
801 for(int k = 0; k < 3; k++) \
803 temp_type difference; \
804 difference = *prev_row++ - *current_row++; \
806 result_temp -= difference; \
808 result_temp += difference; \
810 if(components == 4) \
816 prev_ptr += row_bytes; \
817 current_ptr += row_bytes; \
819 result = (int64_t)(result_temp * multiplier); \
822 int64_t MotionScan::abs_diff(unsigned char *prev_ptr,
823 unsigned char *current_ptr,
833 ABS_DIFF(unsigned char, int64_t, 1, 3)
836 ABS_DIFF(unsigned char, int64_t, 1, 4)
839 ABS_DIFF(float, double, 0x10000, 3)
842 ABS_DIFF(float, double, 0x10000, 4)
845 ABS_DIFF(unsigned char, int64_t, 1, 3)
848 ABS_DIFF(unsigned char, int64_t, 1, 4)
851 ABS_DIFF(uint16_t, int64_t, 1, 3)
853 case BC_YUVA16161616:
854 ABS_DIFF(uint16_t, int64_t, 1, 4)
862 #define ABS_DIFF_SUB(type, temp_type, multiplier, components) \
864 temp_type result_temp = 0; \
865 temp_type y2_fraction = sub_y * 0x100 / OVERSAMPLE; \
866 temp_type y1_fraction = 0x100 - y2_fraction; \
867 temp_type x2_fraction = sub_x * 0x100 / OVERSAMPLE; \
868 temp_type x1_fraction = 0x100 - x2_fraction; \
869 for(int i = 0; i < h_sub; i++) \
871 type *prev_row1 = (type*)prev_ptr; \
872 type *prev_row2 = (type*)prev_ptr + components; \
873 type *prev_row3 = (type*)(prev_ptr + row_bytes); \
874 type *prev_row4 = (type*)(prev_ptr + row_bytes) + components; \
875 type *current_row = (type*)current_ptr; \
876 for(int j = 0; j < w_sub; j++) \
878 /* Scan each component */ \
879 for(int k = 0; k < 3; k++) \
881 temp_type difference; \
882 temp_type prev_value = \
883 (*prev_row1++ * x1_fraction * y1_fraction + \
884 *prev_row2++ * x2_fraction * y1_fraction + \
885 *prev_row3++ * x1_fraction * y2_fraction + \
886 *prev_row4++ * x2_fraction * y2_fraction) / \
888 temp_type current_value = *current_row++; \
889 difference = prev_value - current_value; \
891 result_temp -= difference; \
893 result_temp += difference; \
897 if(components == 4) \
906 prev_ptr += row_bytes; \
907 current_ptr += row_bytes; \
909 result = (int64_t)(result_temp * multiplier); \
915 int64_t MotionScan::abs_diff_sub(unsigned char *prev_ptr,
916 unsigned char *current_ptr,
931 ABS_DIFF_SUB(unsigned char, int64_t, 1, 3)
934 ABS_DIFF_SUB(unsigned char, int64_t, 1, 4)
937 ABS_DIFF_SUB(float, double, 0x10000, 3)
940 ABS_DIFF_SUB(float, double, 0x10000, 4)
943 ABS_DIFF_SUB(unsigned char, int64_t, 1, 3)
946 ABS_DIFF_SUB(unsigned char, int64_t, 1, 4)
949 ABS_DIFF_SUB(uint16_t, int64_t, 1, 3)
951 case BC_YUVA16161616:
952 ABS_DIFF_SUB(uint16_t, int64_t, 1, 4)
962 MotionScanCache::MotionScanCache(int x, int y, int64_t difference)
966 this->difference = difference;
971 void MotionScan::clamp_scan(int w,
983 // printf("MotionMain::clamp_scan 1 w=%d h=%d block=%d %d %d %d scan=%d %d %d %d absolute=%d\n",
998 // scan is always out of range before block.
1001 int difference = -*scan_x1;
1002 *block_x1 += difference;
1008 int difference = -*scan_y1;
1009 *block_y1 += difference;
1015 int difference = *scan_x2 - w;
1016 *block_x2 -= difference;
1017 *scan_x2 -= difference;
1022 int difference = *scan_y2 - h;
1023 *block_y2 -= difference;
1024 *scan_y2 -= difference;
1027 CLAMP(*scan_x1, 0, w);
1028 CLAMP(*scan_y1, 0, h);
1029 CLAMP(*scan_x2, 0, w);
1030 CLAMP(*scan_y2, 0, h);
1036 int difference = -*scan_x1;
1037 *block_x1 += difference;
1038 *scan_x2 += difference;
1044 int difference = -*scan_y1;
1045 *block_y1 += difference;
1046 *scan_y2 += difference;
1050 if(*scan_x2 - *block_x1 + *block_x2 > w)
1052 int difference = *scan_x2 - *block_x1 + *block_x2 - w;
1053 *block_x2 -= difference;
1056 if(*scan_y2 - *block_y1 + *block_y2 > h)
1058 int difference = *scan_y2 - *block_y1 + *block_y2 - h;
1059 *block_y2 -= difference;
1062 // CLAMP(*scan_x1, 0, w - (*block_x2 - *block_x1));
1063 // CLAMP(*scan_y1, 0, h - (*block_y2 - *block_y1));
1064 // CLAMP(*scan_x2, 0, w - (*block_x2 - *block_x1));
1065 // CLAMP(*scan_y2, 0, h - (*block_y2 - *block_y1));
1068 // Sanity checks which break the calculation but should never happen if the
1069 // center of the block is inside the frame.
1070 CLAMP(*block_x1, 0, w);
1071 CLAMP(*block_x2, 0, w);
1072 CLAMP(*block_y1, 0, h);
1073 CLAMP(*block_y2, 0, h);
1075 // printf("MotionMain::clamp_scan 2 w=%d h=%d block=%d %d %d %d scan=%d %d %d %d absolute=%d\n",