2 * Copyright (c) 2014-2015 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of FFmpeg.
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.
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.
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.
22 * @todo switch to dualinput
25 #include "libavutil/imgutils.h"
26 #include "libavutil/opt.h"
29 #include "lavfutils.h"
33 typedef struct FOCContext {
37 int xmin, ymin, xmax, ymax;
41 AVFrame *needle_frame[MAX_MIPMAPS];
42 AVFrame *haystack_frame[MAX_MIPMAPS];
46 #define OFFSET(x) offsetof(FOCContext, x)
47 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
48 static const AVOption find_rect_options[] = {
49 { "object", "object bitmap filename", OFFSET(obj_filename), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = FLAGS },
50 { "threshold", "set threshold", OFFSET(threshold), AV_OPT_TYPE_FLOAT, {.dbl = 0.5}, 0, 1.0, FLAGS },
51 { "mipmaps", "set mipmaps", OFFSET(mipmaps), AV_OPT_TYPE_INT, {.i64 = 3}, 1, MAX_MIPMAPS, FLAGS },
52 { "xmin", "", OFFSET(xmin), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
53 { "ymin", "", OFFSET(ymin), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
54 { "xmax", "", OFFSET(xmax), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
55 { "ymax", "", OFFSET(ymax), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
56 { "discard", "", OFFSET(discard), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
60 AVFILTER_DEFINE_CLASS(find_rect);
62 static int query_formats(AVFilterContext *ctx)
64 static const enum AVPixelFormat pix_fmts[] = {
70 return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
73 static AVFrame *downscale(AVFrame *in)
76 AVFrame *frame = av_frame_alloc();
81 frame->format = in->format;
82 frame->width = (in->width + 1) / 2;
83 frame->height = (in->height+ 1) / 2;
85 if (av_frame_get_buffer(frame, 0) < 0) {
86 av_frame_free(&frame);
92 for(y = 0; y < frame->height; y++) {
93 for(x = 0; x < frame->width; x++) {
96 + src[2*x+0 + in->linesize[0]]
97 + src[2*x+1 + in->linesize[0]]
100 src += 2*in->linesize[0];
101 dst += frame->linesize[0];
106 static float compare(const AVFrame *haystack, const AVFrame *obj, int offx, int offy)
111 int64_t oo_sum_v = 0;
112 int64_t hh_sum_v = 0;
113 int64_t oh_sum_v = 0;
115 int n = obj->height * obj->width;
116 const uint8_t *odat = obj ->data[0];
117 const uint8_t *hdat = haystack->data[0] + offx + offy * haystack->linesize[0];
118 int64_t o_sigma, h_sigma;
120 for(y = 0; y < obj->height; y++) {
121 for(x = 0; x < obj->width; x++) {
126 oo_sum_v += o_v * o_v;
127 hh_sum_v += h_v * h_v;
128 oh_sum_v += o_v * h_v;
130 odat += obj->linesize[0];
131 hdat += haystack->linesize[0];
133 o_sigma = n*oo_sum_v - o_sum_v*(int64_t)o_sum_v;
134 h_sigma = n*hh_sum_v - h_sum_v*(int64_t)h_sum_v;
136 if (o_sigma == 0 || h_sigma == 0)
139 c = (n*oh_sum_v - o_sum_v*(int64_t)h_sum_v) / (sqrt(o_sigma)*sqrt(h_sigma));
144 static int config_input(AVFilterLink *inlink)
146 AVFilterContext *ctx = inlink->dst;
147 FOCContext *foc = ctx->priv;
150 foc->xmax = inlink->w - foc->obj_frame->width;
152 foc->ymax = inlink->h - foc->obj_frame->height;
157 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)
161 if (pass + 1 <= maxpass) {
163 search(foc, pass+1, maxpass, xmin>>1, (xmax+1)>>1, ymin>>1, (ymax+1)>>1, &sub_x, &sub_y, 2.0);
164 xmin = FFMAX(xmin, 2*sub_x - 4);
165 xmax = FFMIN(xmax, 2*sub_x + 4);
166 ymin = FFMAX(ymin, 2*sub_y - 4);
167 ymax = FFMIN(ymax, 2*sub_y + 4);
170 for (y = ymin; y <= ymax; y++) {
171 for (x = xmin; x <= xmax; x++) {
172 float score = compare(foc->haystack_frame[pass], foc->needle_frame[pass], x, y);
173 if (score < best_score) {
183 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
185 AVFilterContext *ctx = inlink->dst;
186 FOCContext *foc = ctx->priv;
192 foc->haystack_frame[0] = av_frame_clone(in);
193 for (i=1; i<foc->mipmaps; i++) {
194 foc->haystack_frame[i] = downscale(foc->haystack_frame[i-1]);
197 best_score = search(foc, 0, 0,
198 FFMAX(foc->xmin, foc->last_x - 8),
199 FFMIN(foc->xmax, foc->last_x + 8),
200 FFMAX(foc->ymin, foc->last_y - 8),
201 FFMIN(foc->ymax, foc->last_y + 8),
202 &best_x, &best_y, 2.0);
204 best_score = search(foc, 0, foc->mipmaps - 1, foc->xmin, foc->xmax, foc->ymin, foc->ymax,
205 &best_x, &best_y, best_score);
207 for (i=0; i<MAX_MIPMAPS; i++) {
208 av_frame_free(&foc->haystack_frame[i]);
211 if (best_score > foc->threshold) {
216 return ff_filter_frame(ctx->outputs[0], in);
220 av_log(ctx, AV_LOG_INFO, "Found at n=%"PRId64" pts_time=%f x=%d y=%d with score=%f\n",
221 inlink->frame_count_out, TS2D(in->pts) * av_q2d(inlink->time_base),
222 best_x, best_y, best_score);
223 foc->last_x = best_x;
224 foc->last_y = best_y;
226 snprintf(buf, sizeof(buf), "%f", best_score);
228 av_frame_make_writable(in);
230 av_dict_set_int(&in->metadata, "lavfi.rect.w", foc->obj_frame->width, 0);
231 av_dict_set_int(&in->metadata, "lavfi.rect.h", foc->obj_frame->height, 0);
232 av_dict_set_int(&in->metadata, "lavfi.rect.x", best_x, 0);
233 av_dict_set_int(&in->metadata, "lavfi.rect.y", best_y, 0);
234 av_dict_set(&in->metadata, "lavfi.rect.score", buf, 0);
236 return ff_filter_frame(ctx->outputs[0], in);
239 static av_cold void uninit(AVFilterContext *ctx)
241 FOCContext *foc = ctx->priv;
244 for (i = 0; i < MAX_MIPMAPS; i++) {
245 av_frame_free(&foc->needle_frame[i]);
246 av_frame_free(&foc->haystack_frame[i]);
250 av_freep(&foc->obj_frame->data[0]);
251 av_frame_free(&foc->obj_frame);
254 static av_cold int init(AVFilterContext *ctx)
256 FOCContext *foc = ctx->priv;
259 if (!foc->obj_filename) {
260 av_log(ctx, AV_LOG_ERROR, "object filename not set\n");
261 return AVERROR(EINVAL);
264 foc->obj_frame = av_frame_alloc();
266 return AVERROR(ENOMEM);
268 if ((ret = ff_load_image(foc->obj_frame->data, foc->obj_frame->linesize,
269 &foc->obj_frame->width, &foc->obj_frame->height,
270 &foc->obj_frame->format, foc->obj_filename, ctx)) < 0)
273 if (foc->obj_frame->format != AV_PIX_FMT_GRAY8) {
274 av_log(ctx, AV_LOG_ERROR, "object image is not a grayscale image\n");
275 return AVERROR(EINVAL);
278 foc->needle_frame[0] = av_frame_clone(foc->obj_frame);
279 for (i = 1; i < foc->mipmaps; i++) {
280 foc->needle_frame[i] = downscale(foc->needle_frame[i-1]);
281 if (!foc->needle_frame[i])
282 return AVERROR(ENOMEM);
288 static const AVFilterPad foc_inputs[] = {
291 .type = AVMEDIA_TYPE_VIDEO,
292 .config_props = config_input,
293 .filter_frame = filter_frame,
298 static const AVFilterPad foc_outputs[] = {
301 .type = AVMEDIA_TYPE_VIDEO,
306 const AVFilter ff_vf_find_rect = {
308 .description = NULL_IF_CONFIG_SMALL("Find a user specified object."),
309 .priv_size = sizeof(FOCContext),
312 .query_formats = query_formats,
313 .inputs = foc_inputs,
314 .outputs = foc_outputs,
315 .priv_class = &find_rect_class,