FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_find_rect.c
Date: 2024-04-25 05:10:44
Exec Total Coverage
Lines: 0 131 0.0%
Functions: 0 7 0.0%
Branches: 0 58 0.0%

Line Branch Exec Source
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/mem.h"
26 #include "libavutil/opt.h"
27 #include "internal.h"
28 #include "video.h"
29
30 #include "lavfutils.h"
31
32 #define MAX_MIPMAPS 5
33
34 typedef struct FOCContext {
35 AVClass *class;
36 float threshold;
37 int mipmaps;
38 int xmin, ymin, xmax, ymax;
39 char *obj_filename;
40 int last_x, last_y;
41 AVFrame *obj_frame;
42 AVFrame *needle_frame[MAX_MIPMAPS];
43 AVFrame *haystack_frame[MAX_MIPMAPS];
44 int discard;
45 } FOCContext;
46
47 #define OFFSET(x) offsetof(FOCContext, x)
48 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
49 static 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 = 0}, 0, INT_MAX, FLAGS },
56 { "ymax", "", OFFSET(ymax), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
57 { "discard", "", OFFSET(discard), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
58 { NULL }
59 };
60
61 AVFILTER_DEFINE_CLASS(find_rect);
62
63 static AVFrame *downscale(AVFrame *in)
64 {
65 int x, y;
66 AVFrame *frame = av_frame_alloc();
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) {
76 av_frame_free(&frame);
77 return NULL;
78 }
79 src = in ->data[0];
80 dst = frame->data[0];
81
82 for(y = 0; y < frame->height; y++) {
83 for(x = 0; x < frame->width; x++) {
84 dst[x] = ( src[2*x+0]
85 + src[2*x+1]
86 + src[2*x+0 + in->linesize[0]]
87 + src[2*x+1 + in->linesize[0]]
88 + 2) >> 2;
89 }
90 src += 2*in->linesize[0];
91 dst += frame->linesize[0];
92 }
93 return frame;
94 }
95
96 static float compare(const AVFrame *haystack, const AVFrame *obj, int offx, int offy)
97 {
98 int x,y;
99 int o_sum_v = 0;
100 int h_sum_v = 0;
101 int64_t oo_sum_v = 0;
102 int64_t hh_sum_v = 0;
103 int64_t oh_sum_v = 0;
104 float c;
105 int n = obj->height * obj->width;
106 const uint8_t *odat = obj ->data[0];
107 const uint8_t *hdat = haystack->data[0] + offx + offy * haystack->linesize[0];
108 int64_t o_sigma, h_sigma;
109
110 for(y = 0; y < obj->height; y++) {
111 for(x = 0; x < obj->width; x++) {
112 int o_v = odat[x];
113 int h_v = hdat[x];
114 o_sum_v += o_v;
115 h_sum_v += h_v;
116 oo_sum_v += o_v * o_v;
117 hh_sum_v += h_v * h_v;
118 oh_sum_v += o_v * h_v;
119 }
120 odat += obj->linesize[0];
121 hdat += haystack->linesize[0];
122 }
123 o_sigma = n*oo_sum_v - o_sum_v*(int64_t)o_sum_v;
124 h_sigma = n*hh_sum_v - h_sum_v*(int64_t)h_sum_v;
125
126 if (o_sigma == 0 || h_sigma == 0)
127 return 1.0;
128
129 c = (n*oh_sum_v - o_sum_v*(int64_t)h_sum_v) / (sqrt(o_sigma)*sqrt(h_sigma));
130
131 return 1 - fabs(c);
132 }
133
134 static int config_input(AVFilterLink *inlink)
135 {
136 AVFilterContext *ctx = inlink->dst;
137 FOCContext *foc = ctx->priv;
138
139 if (foc->xmax <= 0)
140 foc->xmax = inlink->w - foc->obj_frame->width;
141 if (foc->ymax <= 0)
142 foc->ymax = inlink->h - foc->obj_frame->height;
143
144 return 0;
145 }
146
147 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)
148 {
149 int x, y;
150
151 if (pass + 1 <= maxpass) {
152 int sub_x, sub_y;
153 search(foc, pass+1, maxpass, xmin>>1, (xmax+1)>>1, ymin>>1, (ymax+1)>>1, &sub_x, &sub_y, 2.0);
154 xmin = FFMAX(xmin, 2*sub_x - 4);
155 xmax = FFMIN(xmax, 2*sub_x + 4);
156 ymin = FFMAX(ymin, 2*sub_y - 4);
157 ymax = FFMIN(ymax, 2*sub_y + 4);
158 }
159
160 for (y = ymin; y <= ymax; y++) {
161 for (x = xmin; x <= xmax; x++) {
162 float score = compare(foc->haystack_frame[pass], foc->needle_frame[pass], x, y);
163 if (score < best_score) {
164 best_score = score;
165 *best_x = x;
166 *best_y = y;
167 }
168 }
169 }
170 return best_score;
171 }
172
173 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
174 {
175 AVFilterContext *ctx = inlink->dst;
176 FOCContext *foc = ctx->priv;
177 float best_score;
178 int best_x, best_y;
179 int i;
180 char buf[32];
181
182 foc->haystack_frame[0] = av_frame_clone(in);
183 for (i=1; i<foc->mipmaps; i++) {
184 foc->haystack_frame[i] = downscale(foc->haystack_frame[i-1]);
185 }
186
187 best_score = search(foc, 0, 0,
188 FFMAX(foc->xmin, foc->last_x - 8),
189 FFMIN(foc->xmax, foc->last_x + 8),
190 FFMAX(foc->ymin, foc->last_y - 8),
191 FFMIN(foc->ymax, foc->last_y + 8),
192 &best_x, &best_y, 2.0);
193
194 best_score = search(foc, 0, foc->mipmaps - 1, foc->xmin, foc->xmax, foc->ymin, foc->ymax,
195 &best_x, &best_y, best_score);
196
197 for (i=0; i<MAX_MIPMAPS; i++) {
198 av_frame_free(&foc->haystack_frame[i]);
199 }
200
201 if (best_score > foc->threshold) {
202 if (foc->discard) {
203 av_frame_free(&in);
204 return 0;
205 } else {
206 return ff_filter_frame(ctx->outputs[0], in);
207 }
208 }
209
210 av_log(ctx, AV_LOG_INFO, "Found at n=%"PRId64" pts_time=%f x=%d y=%d with score=%f\n",
211 inlink->frame_count_out, TS2D(in->pts) * av_q2d(inlink->time_base),
212 best_x, best_y, best_score);
213 foc->last_x = best_x;
214 foc->last_y = best_y;
215
216 snprintf(buf, sizeof(buf), "%f", best_score);
217
218 av_dict_set_int(&in->metadata, "lavfi.rect.w", foc->obj_frame->width, 0);
219 av_dict_set_int(&in->metadata, "lavfi.rect.h", foc->obj_frame->height, 0);
220 av_dict_set_int(&in->metadata, "lavfi.rect.x", best_x, 0);
221 av_dict_set_int(&in->metadata, "lavfi.rect.y", best_y, 0);
222 av_dict_set(&in->metadata, "lavfi.rect.score", buf, 0);
223
224 return ff_filter_frame(ctx->outputs[0], in);
225 }
226
227 static av_cold void uninit(AVFilterContext *ctx)
228 {
229 FOCContext *foc = ctx->priv;
230 int i;
231
232 for (i = 0; i < MAX_MIPMAPS; i++) {
233 av_frame_free(&foc->needle_frame[i]);
234 av_frame_free(&foc->haystack_frame[i]);
235 }
236
237 if (foc->obj_frame)
238 av_freep(&foc->obj_frame->data[0]);
239 av_frame_free(&foc->obj_frame);
240 }
241
242 static av_cold int init(AVFilterContext *ctx)
243 {
244 FOCContext *foc = ctx->priv;
245 int ret, i;
246
247 if (!foc->obj_filename) {
248 av_log(ctx, AV_LOG_ERROR, "object filename not set\n");
249 return AVERROR(EINVAL);
250 }
251
252 foc->obj_frame = av_frame_alloc();
253 if (!foc->obj_frame)
254 return AVERROR(ENOMEM);
255
256 if ((ret = ff_load_image(foc->obj_frame->data, foc->obj_frame->linesize,
257 &foc->obj_frame->width, &foc->obj_frame->height,
258 &foc->obj_frame->format, foc->obj_filename, ctx)) < 0)
259 return ret;
260
261 if (foc->obj_frame->format != AV_PIX_FMT_GRAY8) {
262 av_log(ctx, AV_LOG_ERROR, "object image is not a grayscale image\n");
263 return AVERROR(EINVAL);
264 }
265
266 foc->needle_frame[0] = av_frame_clone(foc->obj_frame);
267 for (i = 1; i < foc->mipmaps; i++) {
268 foc->needle_frame[i] = downscale(foc->needle_frame[i-1]);
269 if (!foc->needle_frame[i])
270 return AVERROR(ENOMEM);
271 }
272
273 return 0;
274 }
275
276 static const AVFilterPad foc_inputs[] = {
277 {
278 .name = "default",
279 .type = AVMEDIA_TYPE_VIDEO,
280 .config_props = config_input,
281 .filter_frame = filter_frame,
282 },
283 };
284
285 const AVFilter ff_vf_find_rect = {
286 .name = "find_rect",
287 .description = NULL_IF_CONFIG_SMALL("Find a user specified object."),
288 .priv_size = sizeof(FOCContext),
289 .init = init,
290 .uninit = uninit,
291 .flags = AVFILTER_FLAG_METADATA_ONLY,
292 FILTER_INPUTS(foc_inputs),
293 FILTER_OUTPUTS(ff_video_default_filterpad),
294 FILTER_PIXFMTS(AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P),
295 .priv_class = &find_rect_class,
296 };
297