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/pixdesc.h"
27 #include "libavutil/timestamp.h"
28 #include "avfilter.h"
29 #include "bbox.h"
30 #include "internal.h"
31 
32 typedef struct {
33  unsigned int frame;
34 } BBoxContext;
35 
37 {
38  static const enum AVPixelFormat pix_fmts[] = {
45  };
46 
48  return 0;
49 }
50 
51 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
52 {
53  AVFilterContext *ctx = inlink->dst;
54  BBoxContext *bbox = ctx->priv;
55  FFBoundingBox box;
56  int has_bbox, w, h;
57 
58  has_bbox =
60  frame->data[0], frame->linesize[0],
61  inlink->w, inlink->h, 16);
62  w = box.x2 - box.x1 + 1;
63  h = box.y2 - box.y1 + 1;
64 
65  av_log(ctx, AV_LOG_INFO,
66  "n:%d pts:%s pts_time:%s", bbox->frame,
67  av_ts2str(frame->pts), av_ts2timestr(frame->pts, &inlink->time_base));
68 
69  if (has_bbox) {
70  av_log(ctx, AV_LOG_INFO,
71  " x1:%d x2:%d y1:%d y2:%d w:%d h:%d"
72  " crop=%d:%d:%d:%d drawbox=%d:%d:%d:%d",
73  box.x1, box.x2, box.y1, box.y2, w, h,
74  w, h, box.x1, box.y1, /* crop params */
75  box.x1, box.y1, w, h); /* drawbox params */
76  }
77  av_log(ctx, AV_LOG_INFO, "\n");
78 
79  bbox->frame++;
80  return ff_filter_frame(inlink->dst->outputs[0], frame);
81 }
82 
83 static const AVFilterPad bbox_inputs[] = {
84  {
85  .name = "default",
86  .type = AVMEDIA_TYPE_VIDEO,
87  .filter_frame = filter_frame,
88  },
89  { NULL }
90 };
91 
92 static const AVFilterPad bbox_outputs[] = {
93  {
94  .name = "default",
95  .type = AVMEDIA_TYPE_VIDEO,
96  },
97  { NULL }
98 };
99 
101  .name = "bbox",
102  .description = NULL_IF_CONFIG_SMALL("Compute bounding box for each frame."),
103  .priv_size = sizeof(BBoxContext),
105  .inputs = bbox_inputs,
106  .outputs = bbox_outputs,
107 };
static const AVFilterPad bbox_inputs[]
Definition: vf_bbox.c:83
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: vf_bbox.c:51
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
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:117
external API header
output residual component w
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:308
const char * name
Pad name.
it can be given away to ff_start_frame *A reference passed to ff_filter_frame(or the deprecated ff_start_frame) is given away and must no longer be used.*A reference created with avfilter_ref_buffer belongs to the code that created it.*A reference obtained with ff_get_video_buffer or ff_get_audio_buffer belongs to the code that requested it.*A reference given as return value by the get_video_buffer or get_audio_buffer method is given away and must no longer be used.Link reference fields---------------------The AVFilterLink structure has a few AVFilterBufferRef fields.The cur_buf and out_buf were used with the deprecated start_frame/draw_slice/end_frame API and should no longer be used.src_buf
unsigned int frame
Definition: vf_bbox.c:33
timestamp utils, mostly useful for debugging/logging purposes
AVFilter avfilter_vf_bbox
Definition: vf_bbox.c:100
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:159
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:545
frame
Definition: stft.m:14
A filter pad used for either input or output.
#define av_ts2timestr(ts, tb)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: timestamp.h:72
int x2
Definition: bbox.h:27
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void * priv
private data for use by the filter
Definition: avfilter.h:545
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:72
int y1
Definition: bbox.h:27
NULL
Definition: eval.c:55
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:101
static int query_formats(AVFilterContext *ctx)
Definition: vf_bbox.c:36
Filter definition.
Definition: avfilter.h:436
static const AVFilterPad bbox_outputs[]
Definition: vf_bbox.c:92
const char * name
filter name
Definition: avfilter.h:437
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:539
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
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:50
int x1
Definition: bbox.h:27
An instance of a filter.
Definition: avfilter.h:524
#define AV_LOG_INFO
Definition: log.h:156
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:103
internal API functions
AVPixelFormat
Pixel format.
Definition: pixfmt.h:66
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