3 * Copyright (C) 2016 Adam Williams <broadcast at earthling dot net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 // store previously scaled images here for operations
24 // with multiple motion scans on the same frame.
27 #include "bcsignals.h"
29 #include "motioncache-hv.h"
35 MotionHVCacheItem::MotionHVCacheItem()
39 MotionHVCacheItem::~MotionHVCacheItem()
41 //printf("MotionHVCacheItem::~MotionHVCacheItem %d image=%p\n", __LINE__, image);
50 MotionHVCache::MotionHVCache()
52 lock = new Mutex("MotionHVCache::lock");
55 MotionHVCache::~MotionHVCache()
57 //printf("MotionHVCache::~MotionHVCache %d this=%p\n", __LINE__, this);
63 void MotionHVCache::clear()
65 images.remove_all_objects();
70 #define DOWNSAMPLE(type, temp_type, components, max) \
76 type **in_rows = (type**)src->get_rows(); \
77 type **out_rows = (type**)dst->get_rows(); \
79 for(int i = 0; i < h; i += downsample) \
82 int y2 = MIN(i + downsample, h); \
90 int x2 = MIN(j + downsample, w); \
92 temp_type scale = (x2 - x1) * (y2 - y1); \
93 if(x2 > x1 && y2 > y1) \
96 /* Read in values */ \
100 if(components == 4) a = 0; \
102 for(int k = y1; k < y2; k++) \
104 type *row = in_rows[k] + x1 * components; \
105 for(int l = x1; l < x2; l++) \
110 if(components == 4) a += *row++; \
114 /* Write average */ \
118 if(components == 4) a /= scale; \
120 type *row = out_rows[y1 / downsample] + \
121 x1 / downsample * components; \
125 if(components == 4) *row++ = a; \
128 /*printf("DOWNSAMPLE 3 %d\n", i);*/ \
135 void MotionHVCache::downsample_frame(VFrame *dst,
139 int h = src->get_h();
140 int w = src->get_w();
143 //printf("downsample=%d w=%d h=%d dst=%d %d\n", downsample, w, h, dst->get_w(), dst->get_h());
144 switch(src->get_color_model())
147 DOWNSAMPLE(uint8_t, int64_t, 3, 0xff)
150 DOWNSAMPLE(float, float, 3, 1.0)
153 DOWNSAMPLE(uint8_t, int64_t, 4, 0xff)
156 DOWNSAMPLE(float, float, 4, 1.0)
159 DOWNSAMPLE(uint8_t, int64_t, 3, 0xff)
162 DOWNSAMPLE(uint8_t, int64_t, 4, 0xff)
168 VFrame* MotionHVCache::get_image(int ratio,
174 lock->lock("MotionHVCache::get_image 1");
176 for(int i = 0; i < images.size(); i++)
178 if(images.get(i)->ratio == ratio &&
179 images.get(i)->is_previous == is_previous)
181 VFrame *result = images.get(i)->image;
190 new VFrame(downsampled_w+1, downsampled_h+1, src->get_color_model(), 0);
191 downsample_frame(result, src, ratio);
193 MotionHVCacheItem *item = new MotionHVCacheItem();
194 item->image = result;
195 item->is_previous = is_previous;