FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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/avassert.h"
26 #include "libavutil/imgutils.h"
27 #include "libavutil/opt.h"
28 #include "internal.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;
44 } FOCContext;
45 
46 #define OFFSET(x) offsetof(FOCContext, x)
47 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
48 static const AVOption foc_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  { NULL }
57 };
58 
59 static const AVClass foc_class = {
60  .class_name = "find_rect",
61  .item_name = av_default_item_name,
62  .option = foc_options,
63  .version = LIBAVUTIL_VERSION_INT,
64  .category = AV_CLASS_CATEGORY_FILTER,
65 };
66 
68 {
69  static const enum AVPixelFormat pix_fmts[] = {
73  };
74 
75  return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
76 }
77 
79 {
80  int x, y;
82  uint8_t *src, *dst;
83  if (!frame)
84  return NULL;
85 
86  frame->format = in->format;
87  frame->width = (in->width + 1) / 2;
88  frame->height = (in->height+ 1) / 2;
89 
90  if (av_frame_get_buffer(frame, 32) < 0) {
91  av_frame_free(&frame);
92  return NULL;
93  }
94  src = in ->data[0];
95  dst = frame->data[0];
96 
97  for(y = 0; y < frame->height; y++) {
98  for(x = 0; x < frame->width; x++) {
99  dst[x] = ( src[2*x+0]
100  + src[2*x+1]
101  + src[2*x+0 + in->linesize[0]]
102  + src[2*x+1 + in->linesize[0]]
103  + 2) >> 2;
104  }
105  src += 2*in->linesize[0];
106  dst += frame->linesize[0];
107  }
108  return frame;
109 }
110 
111 static float compare(const AVFrame *haystack, const AVFrame *obj, int offx, int offy)
112 {
113  int x,y;
114  int o_sum_v = 0;
115  int h_sum_v = 0;
116  int64_t oo_sum_v = 0;
117  int64_t hh_sum_v = 0;
118  int64_t oh_sum_v = 0;
119  float c;
120  int n = obj->height * obj->width;
121  const uint8_t *odat = obj ->data[0];
122  const uint8_t *hdat = haystack->data[0] + offx + offy * haystack->linesize[0];
123  int64_t o_sigma, h_sigma;
124 
125  for(y = 0; y < obj->height; y++) {
126  for(x = 0; x < obj->width; x++) {
127  int o_v = odat[x];
128  int h_v = hdat[x];
129  o_sum_v += o_v;
130  h_sum_v += h_v;
131  oo_sum_v += o_v * o_v;
132  hh_sum_v += h_v * h_v;
133  oh_sum_v += o_v * h_v;
134  }
135  odat += obj->linesize[0];
136  hdat += haystack->linesize[0];
137  }
138  o_sigma = n*oo_sum_v - o_sum_v*(int64_t)o_sum_v;
139  h_sigma = n*hh_sum_v - h_sum_v*(int64_t)h_sum_v;
140 
141  if (o_sigma == 0 || h_sigma == 0)
142  return 1.0;
143 
144  c = (n*oh_sum_v - o_sum_v*(int64_t)h_sum_v) / (sqrt(o_sigma)*sqrt(h_sigma));
145 
146  return 1 - fabs(c);
147 }
148 
149 static int config_input(AVFilterLink *inlink)
150 {
151  AVFilterContext *ctx = inlink->dst;
152  FOCContext *foc = ctx->priv;
153 
154  if (foc->xmax <= 0)
155  foc->xmax = inlink->w - foc->obj_frame->width;
156  if (foc->ymax <= 0)
157  foc->ymax = inlink->h - foc->obj_frame->height;
158 
159  return 0;
160 }
161 
162 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)
163 {
164  int x, y;
165 
166  if (pass + 1 <= maxpass) {
167  int sub_x, sub_y;
168  search(foc, pass+1, maxpass, xmin>>1, (xmax+1)>>1, ymin>>1, (ymax+1)>>1, &sub_x, &sub_y, 1.0);
169  xmin = FFMAX(xmin, 2*sub_x - 4);
170  xmax = FFMIN(xmax, 2*sub_x + 4);
171  ymin = FFMAX(ymin, 2*sub_y - 4);
172  ymax = FFMIN(ymax, 2*sub_y + 4);
173  }
174 
175  for (y = ymin; y <= ymax; y++) {
176  for (x = xmin; x <= xmax; x++) {
177  float score = compare(foc->haystack_frame[pass], foc->needle_frame[pass], x, y);
178  av_assert0(score != 0);
179  if (score < best_score) {
180  best_score = score;
181  *best_x = x;
182  *best_y = y;
183  }
184  }
185  }
186  return best_score;
187 }
188 
189 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
190 {
191  AVFilterContext *ctx = inlink->dst;
192  FOCContext *foc = ctx->priv;
193  float best_score;
194  int best_x, best_y;
195  int i;
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(foc->xmin, foc->last_x - 8),
204  FFMIN(foc->xmax, foc->last_x + 8),
205  FFMAX(foc->ymin, foc->last_y - 8),
206  FFMIN(foc->ymax, foc->last_y + 8),
207  &best_x, &best_y, 1.0);
208 
209  best_score = search(foc, 0, foc->mipmaps - 1, foc->xmin, foc->xmax, foc->ymin, foc->ymax,
210  &best_x, &best_y, best_score);
211 
212  for (i=0; i<MAX_MIPMAPS; i++) {
213  av_frame_free(&foc->haystack_frame[i]);
214  }
215 
216  if (best_score > foc->threshold) {
217  return ff_filter_frame(ctx->outputs[0], in);
218  }
219 
220  av_log(ctx, AV_LOG_DEBUG, "Found at %d %d score %f\n", best_x, best_y, best_score);
221  foc->last_x = best_x;
222  foc->last_y = best_y;
223 
225 
226  av_dict_set_int(&in->metadata, "lavfi.rect.w", foc->obj_frame->width, 0);
227  av_dict_set_int(&in->metadata, "lavfi.rect.h", foc->obj_frame->height, 0);
228  av_dict_set_int(&in->metadata, "lavfi.rect.x", best_x, 0);
229  av_dict_set_int(&in->metadata, "lavfi.rect.y", best_y, 0);
230 
231  return ff_filter_frame(ctx->outputs[0], in);
232 }
233 
234 static av_cold void uninit(AVFilterContext *ctx)
235 {
236  FOCContext *foc = ctx->priv;
237  int i;
238 
239  for (i = 0; i < MAX_MIPMAPS; i++) {
240  av_frame_free(&foc->needle_frame[i]);
241  av_frame_free(&foc->haystack_frame[i]);
242  }
243 
244  if (foc->obj_frame)
245  av_freep(&foc->obj_frame->data[0]);
246  av_frame_free(&foc->obj_frame);
247 }
248 
249 static av_cold int init(AVFilterContext *ctx)
250 {
251  FOCContext *foc = ctx->priv;
252  int ret, i;
253 
254  if (!foc->obj_filename) {
255  av_log(ctx, AV_LOG_ERROR, "object filename not set\n");
256  return AVERROR(EINVAL);
257  }
258 
259  foc->obj_frame = av_frame_alloc();
260  if (!foc->obj_frame)
261  return AVERROR(ENOMEM);
262 
263  if ((ret = ff_load_image(foc->obj_frame->data, foc->obj_frame->linesize,
264  &foc->obj_frame->width, &foc->obj_frame->height,
265  &foc->obj_frame->format, foc->obj_filename, ctx)) < 0)
266  return ret;
267 
268  if (foc->obj_frame->format != AV_PIX_FMT_GRAY8) {
269  av_log(ctx, AV_LOG_ERROR, "object image is not a grayscale image\n");
270  return AVERROR(EINVAL);
271  }
272 
273  foc->needle_frame[0] = av_frame_clone(foc->obj_frame);
274  for (i = 1; i < foc->mipmaps; i++) {
275  foc->needle_frame[i] = downscale(foc->needle_frame[i-1]);
276  if (!foc->needle_frame[i])
277  return AVERROR(ENOMEM);
278  }
279 
280  return 0;
281 }
282 
283 static const AVFilterPad foc_inputs[] = {
284  {
285  .name = "default",
286  .type = AVMEDIA_TYPE_VIDEO,
287  .config_props = config_input,
288  .filter_frame = filter_frame,
289  },
290  { NULL }
291 };
292 
293 static const AVFilterPad foc_outputs[] = {
294  {
295  .name = "default",
296  .type = AVMEDIA_TYPE_VIDEO,
297  },
298  { NULL }
299 };
300 
302  .name = "find_rect",
303  .description = NULL_IF_CONFIG_SMALL("Find a user specified object"),
304  .priv_size = sizeof(FOCContext),
305  .init = init,
306  .uninit = uninit,
308  .inputs = foc_inputs,
309  .outputs = foc_outputs,
310  .priv_class = &foc_class,
311 };
static float compare(const AVFrame *haystack, const AVFrame *obj, int offx, int offy)
Definition: vf_find_rect.c:111
#define NULL
Definition: coverity.c:32
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
AVOption.
Definition: opt.h:255
misc image utilities
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:248
#define LIBAVUTIL_VERSION_INT
Definition: version.h:62
AVFilter ff_vf_find_rect
Definition: vf_find_rect.c:301
#define OFFSET(x)
Definition: vf_find_rect.c:46
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_find_rect.c:189
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
const char * name
Pad name.
Definition: internal.h:67
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1145
uint8_t
#define av_cold
Definition: attributes.h:74
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:135
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:162
AVOptions.
#define FLAGS
Definition: vf_find_rect.c:47
static AVFrame * frame
AVFrame * obj_frame
Definition: vf_find_rect.c:41
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_find_rect.c:234
AVDictionary * metadata
metadata.
Definition: frame.h:543
static const AVFilterPad foc_outputs[]
Definition: vf_find_rect.c:293
static AVFrame * downscale(AVFrame *in)
Definition: vf_find_rect.c:78
#define MAX_MIPMAPS
Definition: vf_find_rect.c:32
#define av_log(a,...)
A filter pad used for either input or output.
Definition: internal.h:61
static const AVFilterPad foc_inputs[]
Definition: vf_find_rect.c:283
int width
width and height of the video frame
Definition: frame.h:220
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
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:542
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:148
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
void * priv
private data for use by the filter
Definition: avfilter.h:654
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
simple assert() macros that are a bit more flexible than ISO C assert().
#define FFMAX(a, b)
Definition: common.h:64
static const AVClass foc_class
Definition: vf_find_rect.c:59
#define pass
Definition: fft_template.c:509
#define FFMIN(a, b)
Definition: common.h:66
float y
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:75
ret
Definition: avfilter.c:974
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:24
int n
Definition: avisynth_c.h:547
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:449
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:232
static const AVOption foc_options[]
Definition: vf_find_rect.c:48
AVS_Value src
Definition: avisynth_c.h:482
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:199
AVFrame * haystack_frame[MAX_MIPMAPS]
Definition: vf_find_rect.c:43
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;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);returnNULL;}returnac;}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;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->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);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
static av_cold int init(AVFilterContext *ctx)
Definition: vf_find_rect.c:249
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:470
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:239
const char * name
Filter name.
Definition: avfilter.h:474
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:648
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition: frame.c:265
int av_frame_make_writable(AVFrame *frame)
Ensure that the frame data is writable, avoiding data copy if possible.
Definition: frame.c:505
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:182
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:63
Y , 8bpp.
Definition: pixfmt.h:71
float threshold
Definition: vf_find_rect.c:36
static double c[64]
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:143
static int config_input(AVFilterLink *inlink)
Definition: vf_find_rect.c:149
static int query_formats(AVFilterContext *ctx)
Definition: vf_find_rect.c:67
An instance of a filter.
Definition: avfilter.h:633
int height
Definition: frame.h:220
#define av_freep(p)
char * obj_filename
Definition: vf_find_rect.c:39
internal API functions
AVPixelFormat
Pixel format.
Definition: pixfmt.h:61
Miscellaneous utilities which make use of the libavformat library.
AVFrame * needle_frame[MAX_MIPMAPS]
Definition: vf_find_rect.c:42