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 } BBoxContext;
37 
38 #define OFFSET(x) offsetof(BBoxContext, x)
39 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
40 
41 static const AVOption bbox_options[] = {
42  { "min_val", "set minimum luminance value for bounding box", OFFSET(min_val), AV_OPT_TYPE_INT, { .i64 = 16 }, 0, 254, FLAGS },
43  { NULL }
44 };
45 
47 
49 {
50  static const enum AVPixelFormat pix_fmts[] = {
57  };
58 
60  if (!fmts_list)
61  return AVERROR(ENOMEM);
62  return ff_set_common_formats(ctx, fmts_list);
63 }
64 
65 #define SET_META(key, value) \
66  av_dict_set_int(metadata, key, value, 0);
67 
69 {
70  AVFilterContext *ctx = inlink->dst;
71  BBoxContext *bbox = ctx->priv;
72  FFBoundingBox box;
73  int has_bbox, w, h;
74 
75  has_bbox =
77  frame->data[0], frame->linesize[0],
78  inlink->w, inlink->h, bbox->min_val);
79  w = box.x2 - box.x1 + 1;
80  h = box.y2 - box.y1 + 1;
81 
83  "n:%"PRId64" pts:%s pts_time:%s", inlink->frame_count_out,
84  av_ts2str(frame->pts), av_ts2timestr(frame->pts, &inlink->time_base));
85 
86  if (has_bbox) {
87  AVDictionary **metadata = &frame->metadata;
88 
89  SET_META("lavfi.bbox.x1", box.x1)
90  SET_META("lavfi.bbox.x2", box.x2)
91  SET_META("lavfi.bbox.y1", box.y1)
92  SET_META("lavfi.bbox.y2", box.y2)
93  SET_META("lavfi.bbox.w", w)
94  SET_META("lavfi.bbox.h", h)
95 
97  " x1:%d x2:%d y1:%d y2:%d w:%d h:%d"
98  " crop=%d:%d:%d:%d drawbox=%d:%d:%d:%d",
99  box.x1, box.x2, box.y1, box.y2, w, h,
100  w, h, box.x1, box.y1, /* crop params */
101  box.x1, box.y1, w, h); /* drawbox params */
102  }
103  av_log(ctx, AV_LOG_INFO, "\n");
104 
105  return ff_filter_frame(inlink->dst->outputs[0], frame);
106 }
107 
108 static const AVFilterPad bbox_inputs[] = {
109  {
110  .name = "default",
111  .type = AVMEDIA_TYPE_VIDEO,
112  .filter_frame = filter_frame,
113  },
114  { NULL }
115 };
116 
117 static const AVFilterPad bbox_outputs[] = {
118  {
119  .name = "default",
120  .type = AVMEDIA_TYPE_VIDEO,
121  },
122  { NULL }
123 };
124 
126  .name = "bbox",
127  .description = NULL_IF_CONFIG_SMALL("Compute bounding box for each frame."),
128  .priv_size = sizeof(BBoxContext),
129  .priv_class = &bbox_class,
131  .inputs = bbox_inputs,
134 };
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
AVERROR
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
opt.h
ff_make_format_list
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
bbox.h
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: vf_bbox.c:68
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1080
inlink
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
Definition: filter_design.txt:212
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
pixdesc.h
w
uint8_t w
Definition: llviddspenc.c:38
AVOption
AVOption.
Definition: opt.h:246
ff_vf_bbox
AVFilter ff_vf_bbox
Definition: vf_bbox.c:125
AV_PIX_FMT_YUV440P
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:99
BBoxContext
Definition: vf_bbox.c:33
AVDictionary
Definition: dict.c:30
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:148
AVFilterFormats
A list of supported formats for one end of a filter link.
Definition: formats.h:64
FLAGS
#define FLAGS
Definition: vf_bbox.c:39
FFBoundingBox::x2
int x2
Definition: bbox.h:27
SET_META
#define SET_META(key, value)
Definition: vf_bbox.c:65
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:54
ff_set_common_formats
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:568
outputs
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:275
ctx
AVFormatContext * ctx
Definition: movenc.c:48
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
OFFSET
#define OFFSET(x)
Definition: vf_bbox.c:38
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: vf_bbox.c:48
NULL
#define NULL
Definition: coverity.c:32
inputs
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
Definition: filter_design.txt:243
FFBoundingBox::x1
int x1
Definition: bbox.h:27
av_ts2timestr
#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
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:188
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(bbox)
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
internal.h
AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
#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:125
FFBoundingBox::y1
int y1
Definition: bbox.h:27
bbox_options
static const AVOption bbox_options[]
Definition: vf_bbox.c:41
BBoxContext::min_val
int min_val
Definition: vf_bbox.c:35
bbox_outputs
static const AVFilterPad bbox_outputs[]
Definition: vf_bbox.c:117
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:60
AVFilter
Filter definition.
Definition: avfilter.h:144
frame
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
Definition: filter_design.txt:264
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:223
avfilter.h
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
AVFilterContext
An instance of a filter.
Definition: avfilter.h:338
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AV_PIX_FMT_YUV422P
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
FFBoundingBox
Definition: bbox.h:26
AV_PIX_FMT_YUV411P
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:73
timestamp.h
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:565
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
av_ts2str
#define av_ts2str(ts)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: timestamp.h:54
h
h
Definition: vp9dsp_template.c:2038
bbox_inputs
static const AVFilterPad bbox_inputs[]
Definition: vf_bbox.c:108
ff_calculate_bounding_box
int ff_calculate_bounding_box(FFBoundingBox *bbox, const uint8_t *data, int linesize, int w, int h, int min_val)
Calculate the smallest rectangle that will encompass the region with values > min_val.
Definition: bbox.c:23
FFBoundingBox::y2
int y2
Definition: bbox.h:27