FFmpeg  2.6.9
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 {
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  return 0;
61 }
62 
63 #define SET_META(key, value) \
64  av_dict_set_int(metadata, key, value, 0);
65 
66 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
67 {
68  AVFilterContext *ctx = inlink->dst;
69  BBoxContext *bbox = ctx->priv;
70  FFBoundingBox box;
71  int has_bbox, w, h;
72 
73  has_bbox =
75  frame->data[0], frame->linesize[0],
76  inlink->w, inlink->h, bbox->min_val);
77  w = box.x2 - box.x1 + 1;
78  h = box.y2 - box.y1 + 1;
79 
80  av_log(ctx, AV_LOG_INFO,
81  "n:%"PRId64" pts:%s pts_time:%s", inlink->frame_count,
82  av_ts2str(frame->pts), av_ts2timestr(frame->pts, &inlink->time_base));
83 
84  if (has_bbox) {
85  AVDictionary **metadata = avpriv_frame_get_metadatap(frame);
86 
87  SET_META("lavfi.bbox.x1", box.x1)
88  SET_META("lavfi.bbox.x2", box.x2)
89  SET_META("lavfi.bbox.y1", box.y1)
90  SET_META("lavfi.bbox.y2", box.y2)
91  SET_META("lavfi.bbox.w", w)
92  SET_META("lavfi.bbox.h", h)
93 
94  av_log(ctx, AV_LOG_INFO,
95  " x1:%d x2:%d y1:%d y2:%d w:%d h:%d"
96  " crop=%d:%d:%d:%d drawbox=%d:%d:%d:%d",
97  box.x1, box.x2, box.y1, box.y2, w, h,
98  w, h, box.x1, box.y1, /* crop params */
99  box.x1, box.y1, w, h); /* drawbox params */
100  }
101  av_log(ctx, AV_LOG_INFO, "\n");
102 
103  return ff_filter_frame(inlink->dst->outputs[0], frame);
104 }
105 
106 static const AVFilterPad bbox_inputs[] = {
107  {
108  .name = "default",
109  .type = AVMEDIA_TYPE_VIDEO,
110  .filter_frame = filter_frame,
111  },
112  { NULL }
113 };
114 
115 static const AVFilterPad bbox_outputs[] = {
116  {
117  .name = "default",
118  .type = AVMEDIA_TYPE_VIDEO,
119  },
120  { NULL }
121 };
122 
124  .name = "bbox",
125  .description = NULL_IF_CONFIG_SMALL("Compute bounding box for each frame."),
126  .priv_size = sizeof(BBoxContext),
127  .priv_class = &bbox_class,
129  .inputs = bbox_inputs,
130  .outputs = bbox_outputs,
132 };
#define NULL
Definition: coverity.c:32
static const AVFilterPad bbox_inputs[]
Definition: vf_bbox.c:106
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: vf_bbox.c:66
This structure describes decoded (raw) audio or video data.
Definition: frame.h:163
AVOption.
Definition: opt.h:255
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:73
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:246
Main libavfilter public API header.
AVFilter ff_vf_bbox
Definition: vf_bbox.c:123
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:294
#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:451
const char * name
Pad name.
Definition: internal.h:67
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1145
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:249
static AVFrame * frame
void 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:539
#define av_log(a,...)
#define SET_META(key, value)
Definition: vf_bbox.c:63
A filter pad used for either input or output.
Definition: internal.h:61
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:180
void * priv
private data for use by the filter
Definition: avfilter.h:654
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:72
#define FLAGS
Definition: vf_bbox.c:39
static const AVOption bbox_options[]
Definition: vf_bbox.c:41
int min_val
Definition: vf_bbox.c:35
int y1
Definition: bbox.h:27
#define AV_LOG_INFO
Standard information.
Definition: log.h:186
AVDictionary ** avpriv_frame_get_metadatap(AVFrame *frame)
Definition: frame.c:47
#define OFFSET(x)
Definition: vf_bbox.c:38
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:191
static int query_formats(AVFilterContext *ctx)
Definition: vf_bbox.c:48
Describe the class of an AVClass context structure.
Definition: log.h:66
Filter definition.
Definition: avfilter.h:470
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:237
static const AVFilterPad bbox_outputs[]
Definition: vf_bbox.c:115
const char * name
Filter name.
Definition: avfilter.h:474
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:648
static int flags
Definition: cpu.c:47
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:174
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:68
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
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:75
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
int x1
Definition: bbox.h:27
An instance of a filter.
Definition: avfilter.h:633
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:106
internal API functions
AVPixelFormat
Pixel format.
Definition: pixfmt.h:66