FFmpeg  4.4.7
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/imgutils.h"
26 #include "libavutil/opt.h"
27 #include "internal.h"
28 
29 #include "lavfutils.h"
30 
31 #define MAX_MIPMAPS 5
32 
33 typedef struct FOCContext {
34  AVClass *class;
35  float threshold;
36  int mipmaps;
37  int xmin, ymin, xmax, ymax;
38  char *obj_filename;
39  int last_x, last_y;
43 } FOCContext;
44 
45 #define OFFSET(x) offsetof(FOCContext, x)
46 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
47 static const AVOption find_rect_options[] = {
48  { "object", "object bitmap filename", OFFSET(obj_filename), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = FLAGS },
49  { "threshold", "set threshold", OFFSET(threshold), AV_OPT_TYPE_FLOAT, {.dbl = 0.5}, 0, 1.0, FLAGS },
50  { "mipmaps", "set mipmaps", OFFSET(mipmaps), AV_OPT_TYPE_INT, {.i64 = 3}, 1, MAX_MIPMAPS, FLAGS },
51  { "xmin", "", OFFSET(xmin), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
52  { "ymin", "", OFFSET(ymin), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
53  { "xmax", "", OFFSET(xmax), AV_OPT_TYPE_INT, {.i64 = INT_MAX}, 0, INT_MAX, FLAGS },
54  { "ymax", "", OFFSET(ymax), AV_OPT_TYPE_INT, {.i64 = INT_MAX}, 0, INT_MAX, FLAGS },
55  { NULL }
56 };
57 
59 
61 {
62  static const enum AVPixelFormat pix_fmts[] = {
66  };
67 
69 }
70 
72 {
73  int x, y;
75  uint8_t *src, *dst;
76  if (!frame)
77  return NULL;
78 
79  frame->format = in->format;
80  frame->width = (in->width + 1) / 2;
81  frame->height = (in->height+ 1) / 2;
82 
83  if (av_frame_get_buffer(frame, 0) < 0) {
85  return NULL;
86  }
87  src = in ->data[0];
88  dst = frame->data[0];
89 
90  int w2 = in->width/2;
91  int h2 = in->height/2;
92  for(y = 0; y < h2; y++) {
93  for(x = 0; x < w2; x++) {
94  dst[x] = ( src[2*x+0]
95  + src[2*x+1]
96  + src[2*x+0 + in->linesize[0]]
97  + src[2*x+1 + in->linesize[0]]
98  + 2) >> 2;
99  }
100  src += 2*in->linesize[0];
101  dst += frame->linesize[0];
102  }
103  src = in ->data[0];
104  dst = frame->data[0];
105  for(y = 0; y < frame->height; y++) {
106  int yd = y < h2 ? in->linesize[0] : 0;
107  x = yd ? w2 : 0;
108  for(; x < frame->width; x++) {
109  dst[x] = ( src[2*x+0]
110  + src[FFMIN(2*x+1, w2)]
111  + src[2*x+0 + yd]
112  + src[FFMIN(2*x+1, w2) + yd]
113  + 2) >> 2;
114  }
115  src += 2*in->linesize[0];
116  dst += frame->linesize[0];
117  }
118 
119  return frame;
120 }
121 
122 static float compare(const AVFrame *haystack, const AVFrame *obj, int offx, int offy)
123 {
124  int x,y;
125  int o_sum_v = 0;
126  int h_sum_v = 0;
127  int64_t oo_sum_v = 0;
128  int64_t hh_sum_v = 0;
129  int64_t oh_sum_v = 0;
130  float c;
131  int n = obj->height * obj->width;
132  const uint8_t *odat = obj ->data[0];
133  const uint8_t *hdat = haystack->data[0] + offx + offy * haystack->linesize[0];
134  int64_t o_sigma, h_sigma;
135 
136  for(y = 0; y < obj->height; y++) {
137  for(x = 0; x < obj->width; x++) {
138  int o_v = odat[x];
139  int h_v = hdat[x];
140  o_sum_v += o_v;
141  h_sum_v += h_v;
142  oo_sum_v += o_v * o_v;
143  hh_sum_v += h_v * h_v;
144  oh_sum_v += o_v * h_v;
145  }
146  odat += obj->linesize[0];
147  hdat += haystack->linesize[0];
148  }
149  o_sigma = n*oo_sum_v - o_sum_v*(int64_t)o_sum_v;
150  h_sigma = n*hh_sum_v - h_sum_v*(int64_t)h_sum_v;
151 
152  if (o_sigma == 0 || h_sigma == 0)
153  return 1.0;
154 
155  c = (n*oh_sum_v - o_sum_v*(int64_t)h_sum_v) / (sqrt(o_sigma)*sqrt(h_sigma));
156 
157  return 1 - fabs(c);
158 }
159 
160 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 {
162  int x, y;
163 
164  if (pass + 1 <= maxpass) {
165  int sub_x, sub_y;
166  search(foc, pass+1, maxpass, xmin>>1, (xmax+1)>>1, ymin>>1, (ymax+1)>>1, &sub_x, &sub_y, 2.0);
167  xmin = FFMAX(xmin, 2*sub_x - 4);
168  xmax = FFMIN(xmax, 2*sub_x + 4);
169  ymin = FFMAX(ymin, 2*sub_y - 4);
170  ymax = FFMIN(ymax, 2*sub_y + 4);
171  }
172 
173  for (y = ymin; y <= ymax; y++) {
174  for (x = xmin; x <= xmax; x++) {
175  float score = compare(foc->haystack_frame[pass], foc->needle_frame[pass], x, y);
176  if (score < best_score) {
177  best_score = score;
178  *best_x = x;
179  *best_y = y;
180  }
181  }
182  }
183  return best_score;
184 }
185 
186 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
187 {
188  AVFilterContext *ctx = inlink->dst;
189  FOCContext *foc = ctx->priv;
190  float best_score;
191  int best_x, best_y;
192  int i;
193 
194  int xmin = FFMAX(foc->xmin, 0);
195  int ymin = FFMAX(foc->ymin, 0);
196  int xmax = FFMIN(foc->xmax, inlink->w - foc->obj_frame->width );
197  int ymax = FFMIN(foc->ymax, inlink->h - foc->obj_frame->height);
198 
199  foc->haystack_frame[0] = av_frame_clone(in);
200  for (i=1; i<foc->mipmaps; i++) {
201  foc->haystack_frame[i] = downscale(foc->haystack_frame[i-1]);
202  }
203 
204  best_score = search(foc, 0, 0,
205  FFMAX(xmin, foc->last_x - 8),
206  FFMIN(xmax, foc->last_x + 8),
207  FFMAX(ymin, foc->last_y - 8),
208  FFMIN(ymax, foc->last_y + 8),
209  &best_x, &best_y, 2.0);
210 
211  best_score = search(foc, 0, foc->mipmaps - 1, xmin, xmax, ymin, ymax,
212  &best_x, &best_y, best_score);
213 
214  for (i=0; i<MAX_MIPMAPS; i++) {
216  }
217 
218  if (best_score > foc->threshold) {
219  return ff_filter_frame(ctx->outputs[0], in);
220  }
221 
222  av_log(ctx, AV_LOG_DEBUG, "Found at %d %d score %f\n", best_x, best_y, best_score);
223  foc->last_x = best_x;
224  foc->last_y = best_y;
225 
227 
228  av_dict_set_int(&in->metadata, "lavfi.rect.w", foc->obj_frame->width, 0);
229  av_dict_set_int(&in->metadata, "lavfi.rect.h", foc->obj_frame->height, 0);
230  av_dict_set_int(&in->metadata, "lavfi.rect.x", best_x, 0);
231  av_dict_set_int(&in->metadata, "lavfi.rect.y", best_y, 0);
232 
233  return ff_filter_frame(ctx->outputs[0], in);
234 }
235 
237 {
238  FOCContext *foc = ctx->priv;
239  int i;
240 
241  for (i = 0; i < MAX_MIPMAPS; i++) {
242  av_frame_free(&foc->needle_frame[i]);
244  }
245 
246  if (foc->obj_frame)
247  av_freep(&foc->obj_frame->data[0]);
248  av_frame_free(&foc->obj_frame);
249 }
250 
252 {
253  FOCContext *foc = ctx->priv;
254  int ret, i;
255 
256  if (!foc->obj_filename) {
257  av_log(ctx, AV_LOG_ERROR, "object filename not set\n");
258  return AVERROR(EINVAL);
259  }
260 
261  foc->obj_frame = av_frame_alloc();
262  if (!foc->obj_frame)
263  return AVERROR(ENOMEM);
264 
265  if ((ret = ff_load_image(foc->obj_frame->data, foc->obj_frame->linesize,
266  &foc->obj_frame->width, &foc->obj_frame->height,
267  &foc->obj_frame->format, foc->obj_filename, ctx)) < 0)
268  return ret;
269 
270  if (foc->obj_frame->format != AV_PIX_FMT_GRAY8) {
271  av_log(ctx, AV_LOG_ERROR, "object image is not a grayscale image\n");
272  return AVERROR(EINVAL);
273  }
274 
275  foc->needle_frame[0] = av_frame_clone(foc->obj_frame);
276  for (i = 1; i < foc->mipmaps; i++) {
277  foc->needle_frame[i] = downscale(foc->needle_frame[i-1]);
278  if (!foc->needle_frame[i])
279  return AVERROR(ENOMEM);
280  }
281 
282  return 0;
283 }
284 
285 static const AVFilterPad foc_inputs[] = {
286  {
287  .name = "default",
288  .type = AVMEDIA_TYPE_VIDEO,
289  .filter_frame = filter_frame,
290  },
291  { NULL }
292 };
293 
294 static const AVFilterPad foc_outputs[] = {
295  {
296  .name = "default",
297  .type = AVMEDIA_TYPE_VIDEO,
298  },
299  { NULL }
300 };
301 
303  .name = "find_rect",
304  .description = NULL_IF_CONFIG_SMALL("Find a user specified object."),
305  .priv_size = sizeof(FOCContext),
306  .init = init,
307  .uninit = uninit,
309  .inputs = foc_inputs,
310  .outputs = foc_outputs,
311  .priv_class = &find_rect_class,
312 };
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
#define av_cold
Definition: attributes.h:88
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
uint8_t
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1096
#define FFMIN(a, b)
Definition: common.h:105
#define FFMAX(a, b)
Definition: common.h:103
#define NULL
Definition: coverity.c:32
long long int64_t
Definition: coverity.c:34
static __device__ float fabs(float a)
Definition: cuda_runtime.h:182
static AVFrame * frame
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:587
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:286
@ AV_OPT_TYPE_INT
Definition: opt.h:225
@ AV_OPT_TYPE_FLOAT
Definition: opt.h:228
@ AV_OPT_TYPE_STRING
Definition: opt.h:229
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:147
#define AVERROR(e)
Definition: error.h:43
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:540
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition: frame.c:337
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:190
int av_frame_make_writable(AVFrame *frame)
Ensure that the frame data is writable, avoiding data copy if possible.
Definition: frame.c:611
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
misc image utilities
int i
Definition: input.c:407
int ff_load_image(uint8_t *data[4], int linesize[4], int *w, int *h, enum AVPixelFormat *pix_fmt, const char *filename, void *log_ctx)
Load image from filename and put the resulting image in data.
Definition: lavfutils.c:25
Miscellaneous utilities which make use of the libavformat library.
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:309
AVOptions.
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
@ 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:78
Describe the class of an AVClass context structure.
Definition: log.h:67
An instance of a filter.
Definition: avfilter.h:341
A filter pad used for either input or output.
Definition: internal.h:54
const char * name
Pad name.
Definition: internal.h:60
Filter definition.
Definition: avfilter.h:145
const char * name
Filter name.
Definition: avfilter.h:149
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
int width
Definition: frame.h:376
int height
Definition: frame.h:376
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:349
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:391
AVOption.
Definition: opt.h:248
float threshold
Definition: vf_find_rect.c:35
AVFrame * obj_frame
Definition: vf_find_rect.c:40
char * obj_filename
Definition: vf_find_rect.c:38
AVFrame * needle_frame[MAX_MIPMAPS]
Definition: vf_find_rect.c:41
AVFrame * haystack_frame[MAX_MIPMAPS]
Definition: vf_find_rect.c:42
#define av_freep(p)
#define av_log(a,...)
#define src
Definition: vp8dsp.c:255
AVFormatContext * ctx
Definition: movenc.c:48
#define pass
Definition: tx_template.c:347
static float compare(const AVFrame *haystack, const AVFrame *obj, int offx, int offy)
Definition: vf_find_rect.c:122
static const AVFilterPad foc_outputs[]
Definition: vf_find_rect.c:294
#define MAX_MIPMAPS
Definition: vf_find_rect.c:31
static const AVFilterPad foc_inputs[]
Definition: vf_find_rect.c:285
static int query_formats(AVFilterContext *ctx)
Definition: vf_find_rect.c:60
AVFILTER_DEFINE_CLASS(find_rect)
#define FLAGS
Definition: vf_find_rect.c:46
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_find_rect.c:186
static av_cold int init(AVFilterContext *ctx)
Definition: vf_find_rect.c:251
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_find_rect.c:236
#define OFFSET(x)
Definition: vf_find_rect.c:45
AVFilter ff_vf_find_rect
Definition: vf_find_rect.c:302
static const AVOption find_rect_options[]
Definition: vf_find_rect.c:47
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)
Definition: vf_find_rect.c:160
static AVFrame * downscale(AVFrame *in)
Definition: vf_find_rect.c:71
static double c[64]