FFmpeg
Loading...
Searching...
No Matches
vf_find_rect.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2014-2015 Michael Niedermayer <michaelni@gmx.at>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg 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 * FFmpeg 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 along
17 * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21/**
22 * @todo switch to dualinput
23 */
24
25#include "libavutil/opt.h"
26
27#include "filters.h"
28#include "video.h"
29
30#include "lavfutils.h"
31
32#define MAX_MIPMAPS 5
33
46
47#define OFFSET(x) offsetof(FOCContext, x)
48#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
49static const AVOption find_rect_options[] = {
50 { "object", "object bitmap filename", OFFSET(obj_filename), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = FLAGS },
51 { "threshold", "set threshold", OFFSET(threshold), AV_OPT_TYPE_FLOAT, {.dbl = 0.5}, 0, 1.0, FLAGS },
52 { "mipmaps", "set mipmaps", OFFSET(mipmaps), AV_OPT_TYPE_INT, {.i64 = 3}, 1, MAX_MIPMAPS, FLAGS },
53 { "xmin", "", OFFSET(xmin), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
54 { "ymin", "", OFFSET(ymin), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
55 { "xmax", "", OFFSET(xmax), AV_OPT_TYPE_INT, {.i64 = INT_MAX}, 0, INT_MAX, FLAGS },
56 { "ymax", "", OFFSET(ymax), AV_OPT_TYPE_INT, {.i64 = INT_MAX}, 0, INT_MAX, FLAGS },
57 { "discard", "", OFFSET(discard), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
58 { NULL }
59};
60
62
64{
65 int x, y;
67 uint8_t *src, *dst;
68 if (!frame)
69 return NULL;
70
71 frame->format = in->format;
72 frame->width = (in->width + 1) / 2;
73 frame->height = (in->height+ 1) / 2;
74
75 if (av_frame_get_buffer(frame, 0) < 0) {
77 return NULL;
78 }
79 src = in ->data[0];
80 dst = frame->data[0];
81
82 int w2 = in->width/2;
83 int h2 = in->height/2;
84 for(y = 0; y < h2; y++) {
85 for(x = 0; x < w2; x++) {
86 dst[x] = ( src[2*x+0]
87 + src[2*x+1]
88 + src[2*x+0 + in->linesize[0]]
89 + src[2*x+1 + in->linesize[0]]
90 + 2) >> 2;
91 }
92 src += 2*in->linesize[0];
93 dst += frame->linesize[0];
94 }
95 src = in ->data[0];
96 dst = frame->data[0];
97 for(y = 0; y < frame->height; y++) {
98 int yd = y < h2 ? in->linesize[0] : 0;
99 x = yd ? w2 : 0;
100 for(; x < frame->width; x++) {
101 dst[x] = ( src[2*x+0]
102 + src[FFMIN(2*x+1, w2)]
103 + src[2*x+0 + yd]
104 + src[FFMIN(2*x+1, w2) + yd]
105 + 2) >> 2;
106 }
107 src += 2*in->linesize[0];
108 dst += frame->linesize[0];
109 }
110
111 return frame;
112}
113
114static float compare(const AVFrame *haystack, const AVFrame *obj, int offx, int offy)
115{
116 int x,y;
117 int o_sum_v = 0;
118 int h_sum_v = 0;
119 int64_t oo_sum_v = 0;
120 int64_t hh_sum_v = 0;
121 int64_t oh_sum_v = 0;
122 float c;
123 int n = obj->height * obj->width;
124 const uint8_t *odat = obj ->data[0];
125 const uint8_t *hdat = haystack->data[0] + offx + offy * haystack->linesize[0];
126 int64_t o_sigma, h_sigma;
127
128 for(y = 0; y < obj->height; y++) {
129 for(x = 0; x < obj->width; x++) {
130 int o_v = odat[x];
131 int h_v = hdat[x];
132 o_sum_v += o_v;
133 h_sum_v += h_v;
134 oo_sum_v += o_v * o_v;
135 hh_sum_v += h_v * h_v;
136 oh_sum_v += o_v * h_v;
137 }
138 odat += obj->linesize[0];
139 hdat += haystack->linesize[0];
140 }
141 o_sigma = n*oo_sum_v - o_sum_v*(int64_t)o_sum_v;
142 h_sigma = n*hh_sum_v - h_sum_v*(int64_t)h_sum_v;
143
144 if (o_sigma == 0 || h_sigma == 0)
145 return 1.0;
146
147 c = (n*oh_sum_v - o_sum_v*(int64_t)h_sum_v) / (sqrt(o_sigma)*sqrt(h_sigma));
148
149 return 1 - fabs(c);
150}
151
152static float search(FOCContext *foc, int pass, int maxpass, int xmin, int xmax, int ymin, int ymax, int *best_x, int *best_y, float best_score)
153{
154 int x, y;
155
156 if (pass + 1 <= maxpass) {
157 int sub_x, sub_y;
158 search(foc, pass+1, maxpass, xmin>>1, (xmax+1)>>1, ymin>>1, (ymax+1)>>1, &sub_x, &sub_y, 2.0);
159 xmin = FFMAX(xmin, 2*sub_x - 4);
160 xmax = FFMIN(xmax, 2*sub_x + 4);
161 ymin = FFMAX(ymin, 2*sub_y - 4);
162 ymax = FFMIN(ymax, 2*sub_y + 4);
163 }
164
165 for (y = ymin; y <= ymax; y++) {
166 for (x = xmin; x <= xmax; x++) {
167 float score = compare(foc->haystack_frame[pass], foc->needle_frame[pass], x, y);
168 if (score < best_score) {
169 best_score = score;
170 *best_x = x;
171 *best_y = y;
172 }
173 }
174 }
175 return best_score;
176}
177
178static int filter_frame(AVFilterLink *inlink, AVFrame *in)
179{
180 FilterLink *inl = ff_filter_link(inlink);
181 AVFilterContext *ctx = inlink->dst;
182 FOCContext *foc = ctx->priv;
183 float best_score;
184 int best_x, best_y;
185 int i;
186 char buf[32];
187
188 int xmin = FFMAX(foc->xmin, 0);
189 int ymin = FFMAX(foc->ymin, 0);
190 int xmax = FFMIN(foc->xmax, inlink->w - foc->obj_frame->width );
191 int ymax = FFMIN(foc->ymax, inlink->h - foc->obj_frame->height);
192
193 if (xmin > xmax || ymin > ymax)
194 av_log(ctx, AV_LOG_WARNING, "x/y min/max are invalid for the current frame\n");
195
196
197 foc->haystack_frame[0] = av_frame_clone(in);
198 for (i=1; i<foc->mipmaps; i++) {
199 foc->haystack_frame[i] = downscale(foc->haystack_frame[i-1]);
200 }
201
202 best_score = search(foc, 0, 0,
203 FFMAX(xmin, foc->last_x - 8),
204 FFMIN(xmax, foc->last_x + 8),
205 FFMAX(ymin, foc->last_y - 8),
206 FFMIN(ymax, foc->last_y + 8),
207 &best_x, &best_y, 2.0);
208
209 best_score = search(foc, 0, foc->mipmaps - 1, xmin, xmax, ymin, ymax,
210 &best_x, &best_y, best_score);
211
212 for (i=0; i<MAX_MIPMAPS; i++) {
214 }
215
216 if (best_score > foc->threshold) {
217 if (foc->discard) {
218 av_frame_free(&in);
219 return 0;
220 } else {
221 return ff_filter_frame(ctx->outputs[0], in);
222 }
223 }
224
225 av_log(ctx, AV_LOG_INFO, "Found at n=%"PRId64" pts_time=%f x=%d y=%d with score=%f\n",
226 inl->frame_count_out, TS2D(in->pts) * av_q2d(inlink->time_base),
227 best_x, best_y, best_score);
228 foc->last_x = best_x;
229 foc->last_y = best_y;
230
231 snprintf(buf, sizeof(buf), "%f", best_score);
232
233 av_dict_set_int(&in->metadata, "lavfi.rect.w", foc->obj_frame->width, 0);
234 av_dict_set_int(&in->metadata, "lavfi.rect.h", foc->obj_frame->height, 0);
235 av_dict_set_int(&in->metadata, "lavfi.rect.x", best_x, 0);
236 av_dict_set_int(&in->metadata, "lavfi.rect.y", best_y, 0);
237 av_dict_set(&in->metadata, "lavfi.rect.score", buf, 0);
238
239 return ff_filter_frame(ctx->outputs[0], in);
240}
241
243{
244 FOCContext *foc = ctx->priv;
245 int i;
246
247 for (i = 0; i < MAX_MIPMAPS; i++) {
250 }
251
253}
254
256{
257 FOCContext *foc = ctx->priv;
258 int ret, i;
259
260 if (!foc->obj_filename) {
261 av_log(ctx, AV_LOG_ERROR, "object filename not set\n");
262 return AVERROR(EINVAL);
263 }
264
265 ret = ff_load_image(&foc->obj_frame, foc->obj_filename, ctx);
266 if (ret < 0)
267 return ret;
268
269 if (foc->obj_frame->format != AV_PIX_FMT_GRAY8) {
270 av_log(ctx, AV_LOG_ERROR, "object image is not a grayscale image\n");
271 return AVERROR(EINVAL);
272 }
273
274 foc->needle_frame[0] = av_frame_clone(foc->obj_frame);
275 for (i = 1; i < foc->mipmaps; i++) {
276 foc->needle_frame[i] = downscale(foc->needle_frame[i-1]);
277 if (!foc->needle_frame[i])
278 return AVERROR(ENOMEM);
279 }
280
281 return 0;
282}
283
284static const AVFilterPad foc_inputs[] = {
285 {
286 .name = "default",
287 .type = AVMEDIA_TYPE_VIDEO,
288 .filter_frame = filter_frame,
289 },
290};
291
293 .p.name = "find_rect",
294 .p.description = NULL_IF_CONFIG_SMALL("Find a user specified object."),
296 .p.priv_class = &find_rect_class,
297 .priv_size = sizeof(FOCContext),
298 .init = init,
299 .uninit = uninit,
303};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
const FFFilter ff_vf_find_rect
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define FLAGS
Definition cmdutils.c:598
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static __device__ float fabs(float a)
static AVFrame * frame
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
Definition opt.h:270
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition opt.h:275
#define AVFILTER_FLAG_METADATA_ONLY
The filter is a "metadata" filter - it does not modify the frame data in any way.
Definition avfilter.h:182
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition dict.c:86
int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value, int flags)
Convenience wrapper for av_dict_set() that converts the value to a string and stores it.
Definition dict.c:177
#define AVERROR(e)
Definition error.h:45
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition frame.c:206
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:52
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition frame.c:483
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition rational.h:104
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
int ff_load_image(struct AVFrame **outframe, const char *filename, void *log_ctx)
Load image from filename and put the resulting image in an AVFrame.
Definition lavfutils.c:32
Miscellaneous utilities which make use of the libavformat library.
static av_cold void uninit(AVBitStreamFilterContext *ctx)
#define TS2D(ts)
Definition filters.h:482
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define FILTER_PIXFMTS(...)
Definition filters.h:250
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition filters.h:199
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
AVOptions.
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition pixfmt.h:85
#define snprintf
Definition snprintf.h:34
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition frame.h:574
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
int width
Definition frame.h:544
int height
Definition frame.h:544
AVDictionary * metadata
metadata.
Definition frame.h:750
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition frame.h:517
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition frame.h:559
AVOption.
Definition opt.h:428
float threshold
AVFrame * obj_frame
char * obj_filename
AVFrame * needle_frame[MAX_MIPMAPS]
AVFrame * haystack_frame[MAX_MIPMAPS]
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
static AVFormatContext * ctx
Definition movenc.c:49
static float compare(const AVFrame *haystack, const AVFrame *obj, int offx, int offy)
static AVFrame * downscale(AVFrame *in)
#define MAX_MIPMAPS
static const AVFilterPad foc_inputs[]
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
static av_cold void uninit(AVFilterContext *ctx)
#define OFFSET(x)
static const AVOption find_rect_options[]
static float search(FOCContext *foc, int pass, int maxpass, int xmin, int xmax, int ymin, int ymax, int *best_x, int *best_y, float best_score)
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
Definition video.c:37
static double c[64]