FFmpeg
vf_bbox.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 Stefano Sabatini
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /**
22  * @file
23  * bounding box detection filter
24  */
25 
26 #include "libavutil/opt.h"
27 #include "libavutil/pixdesc.h"
28 #include "libavutil/timestamp.h"
29 #include "avfilter.h"
30 #include "bbox.h"
31 #include "internal.h"
32 
33 typedef struct BBoxContext {
34  const AVClass *class;
35  int min_val;
36  int depth;
37 } BBoxContext;
38 
39 #define OFFSET(x) offsetof(BBoxContext, x)
40 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
41 
42 static const AVOption bbox_options[] = {
43  { "min_val", "set minimum luminance value for bounding box", OFFSET(min_val), AV_OPT_TYPE_INT, { .i64 = 16 }, 0, UINT16_MAX, FLAGS },
44  { NULL }
45 };
46 
48 
50 {
51  static const enum AVPixelFormat pix_fmts[] = {
73  };
74 
75  AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
76  if (!fmts_list)
77  return AVERROR(ENOMEM);
78  return ff_set_common_formats(ctx, fmts_list);
79 }
80 
81 #define SET_META(key, value) \
82  av_dict_set_int(metadata, key, value, 0);
83 
85 {
86  AVFilterContext *ctx = inlink->dst;
87  BBoxContext *bbox = ctx->priv;
88  FFBoundingBox box;
89  int has_bbox, w, h;
90 
91  has_bbox =
93  frame->data[0], frame->linesize[0],
94  inlink->w, inlink->h, bbox->min_val, bbox->depth);
95  w = box.x2 - box.x1 + 1;
96  h = box.y2 - box.y1 + 1;
97 
98  av_log(ctx, AV_LOG_INFO,
99  "n:%"PRId64" pts:%s pts_time:%s", inlink->frame_count_out,
100  av_ts2str(frame->pts), av_ts2timestr(frame->pts, &inlink->time_base));
101 
102  if (has_bbox) {
103  AVDictionary **metadata = &frame->metadata;
104 
105  SET_META("lavfi.bbox.x1", box.x1)
106  SET_META("lavfi.bbox.x2", box.x2)
107  SET_META("lavfi.bbox.y1", box.y1)
108  SET_META("lavfi.bbox.y2", box.y2)
109  SET_META("lavfi.bbox.w", w)
110  SET_META("lavfi.bbox.h", h)
111 
112  av_log(ctx, AV_LOG_INFO,
113  " x1:%d x2:%d y1:%d y2:%d w:%d h:%d"
114  " crop=%d:%d:%d:%d drawbox=%d:%d:%d:%d",
115  box.x1, box.x2, box.y1, box.y2, w, h,
116  w, h, box.x1, box.y1, /* crop params */
117  box.x1, box.y1, w, h); /* drawbox params */
118  }
119  av_log(ctx, AV_LOG_INFO, "\n");
120 
121  return ff_filter_frame(inlink->dst->outputs[0], frame);
122 }
123 
124 static int config_output(AVFilterLink *outlink)
125 {
126  AVFilterContext *ctx = outlink->src;
127  BBoxContext *s = ctx->priv;
128  const AVPixFmtDescriptor *desc;
129 
130  desc = av_pix_fmt_desc_get(outlink->format);
131  if (!desc)
132  return AVERROR_BUG;
133  s->depth = desc->comp[0].depth;
134 
135  return 0;
136 }
137 
138 static const AVFilterPad bbox_inputs[] = {
139  {
140  .name = "default",
141  .type = AVMEDIA_TYPE_VIDEO,
142  .filter_frame = filter_frame,
143  },
144  { NULL }
145 };
146 
147 static const AVFilterPad bbox_outputs[] = {
148  {
149  .name = "default",
150  .type = AVMEDIA_TYPE_VIDEO,
151  .config_props = config_output,
152  },
153  { NULL }
154 };
155 
157  .name = "bbox",
158  .description = NULL_IF_CONFIG_SMALL("Compute bounding box for each frame."),
159  .priv_size = sizeof(BBoxContext),
160  .priv_class = &bbox_class,
162  .inputs = bbox_inputs,
163  .outputs = bbox_outputs,
166 };
#define NULL
Definition: coverity.c:32
static const AVFilterPad bbox_inputs[]
Definition: vf_bbox.c:138
#define AV_PIX_FMT_YUVA422P16
Definition: pixfmt.h:442
#define AV_PIX_FMT_YUV440P10
Definition: pixfmt.h:401
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: vf_bbox.c:84
#define AV_PIX_FMT_YUVA422P9
Definition: pixfmt.h:434
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2573
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
AVOption.
Definition: opt.h:248
#define AV_PIX_FMT_YUVA420P10
Definition: pixfmt.h:436
#define AV_PIX_FMT_YUV444P14
Definition: pixfmt.h:409
#define AV_PIX_FMT_YUVA422P10
Definition: pixfmt.h:437
const char * desc
Definition: libsvtav1.c:79
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
Main libavfilter public API header.
#define AV_PIX_FMT_GRAY9
Definition: pixfmt.h:379
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:403
AVFilter ff_vf_bbox
Definition: vf_bbox.c:156
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:287
#define AV_PIX_FMT_GRAY10
Definition: pixfmt.h:380
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition: avfilter.h:126
const char * name
Pad name.
Definition: internal.h:60
#define AV_PIX_FMT_GRAY12
Definition: pixfmt.h:381
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1094
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:101
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:117
AVOptions.
timestamp utils, mostly useful for debugging/logging purposes
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:411
#define AV_PIX_FMT_YUVA420P9
Definition: pixfmt.h:433
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range...
Definition: pixfmt.h:100
static int config_output(AVFilterLink *outlink)
Definition: vf_bbox.c:124
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition: pixfmt.h:79
AVDictionary * metadata
metadata.
Definition: frame.h:604
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:412
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:404
#define AV_PIX_FMT_YUVA420P16
Definition: pixfmt.h:441
#define av_log(a,...)
#define SET_META(key, value)
Definition: vf_bbox.c:81
A filter pad used for either input or output.
Definition: internal.h:54
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition: pixfmt.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:588
AVFILTER_DEFINE_CLASS(bbox)
#define av_ts2timestr(ts, tb)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: timestamp.h:76
int x2
Definition: bbox.h:27
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:117
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options...
Definition: avfilter.c:882
void * priv
private data for use by the filter
Definition: avfilter.h:356
#define AV_PIX_FMT_YUVA444P16
Definition: pixfmt.h:443
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:402
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
#define FLAGS
Definition: vf_bbox.c:40
#define AV_PIX_FMT_YUV422P9
Definition: pixfmt.h:397
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:383
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:78
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition: af_acrusher.c:336
#define AV_PIX_FMT_YUVA444P12
Definition: pixfmt.h:440
uint8_t w
Definition: llviddspenc.c:39
static const AVOption bbox_options[]
Definition: vf_bbox.c:42
AVFormatContext * ctx
Definition: movenc.c:48
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
#define s(width, name)
Definition: cbs_vp9.c:257
#define AV_PIX_FMT_YUVA444P10
Definition: pixfmt.h:438
int min_val
Definition: vf_bbox.c:35
int y1
Definition: bbox.h:27
#define AV_PIX_FMT_YUV444P9
Definition: pixfmt.h:398
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
#define AV_PIX_FMT_YUV420P16
Definition: pixfmt.h:410
#define AV_LOG_INFO
Standard information.
Definition: log.h:205
#define AV_PIX_FMT_YUV420P14
Definition: pixfmt.h:407
#define OFFSET(x)
Definition: vf_bbox.c:39
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:349
static int query_formats(AVFilterContext *ctx)
Definition: vf_bbox.c:49
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:177
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
#define AV_PIX_FMT_GRAY14
Definition: pixfmt.h:382
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:399
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:72
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:145
static const AVFilterPad bbox_outputs[]
Definition: vf_bbox.c:147
const char * name
Filter name.
Definition: avfilter.h:149
#define AV_PIX_FMT_YUV440P12
Definition: pixfmt.h:405
int ff_calculate_bounding_box(FFBoundingBox *bbox, const uint8_t *data, int linesize, int w, int h, int min_val, int depth)
Calculate the smallest rectangle that will encompass the region with values > min_val.
Definition: bbox.c:81
#define AV_PIX_FMT_YUV420P9
Definition: pixfmt.h:396
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:353
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:303
#define AV_PIX_FMT_YUV422P14
Definition: pixfmt.h:408
#define flags(name, subs,...)
Definition: cbs_av1.c:561
int depth
Definition: vf_bbox.c:36
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:400
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:406
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
Y , 8bpp.
Definition: pixfmt.h:74
#define AV_PIX_FMT_YUVA444P9
Definition: pixfmt.h:435
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition: pixfmt.h:80
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:73
int y2
Definition: bbox.h:27
#define av_ts2str(ts)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: timestamp.h:54
A list of supported formats for one end of a filter link.
Definition: formats.h:65
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) full scale (JPEG), deprecated in favor ...
Definition: pixfmt.h:258
int x1
Definition: bbox.h:27
An instance of a filter.
Definition: avfilter.h:341
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:99
internal API functions
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
int depth
Number of bits in the component.
Definition: pixdesc.h:58
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
#define AV_PIX_FMT_YUV422P16
Definition: pixfmt.h:411
#define AV_PIX_FMT_YUVA422P12
Definition: pixfmt.h:439