2 * Copyright (c) 2012 Stefano Sabatini
4 * This file is part of FFmpeg.
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.
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.
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
23 * bounding box detection filter
26 #include "libavutil/pixdesc.h"
27 #include "libavutil/timestamp.h"
36 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
38 BBoxContext *bbox = ctx->priv;
43 static int query_formats(AVFilterContext *ctx)
45 static const enum PixelFormat pix_fmts[] = {
54 avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
58 static void end_frame(AVFilterLink *inlink)
60 AVFilterContext *ctx = inlink->dst;
61 BBoxContext *bbox = ctx->priv;
62 AVFilterBufferRef *picref = inlink->cur_buf;
67 ff_calculate_bounding_box(&box,
68 picref->data[0], picref->linesize[0],
69 inlink->w, inlink->h, 16);
70 w = box.x2 - box.x1 + 1;
71 h = box.y2 - box.y1 + 1;
73 av_log(ctx, AV_LOG_INFO,
74 "n:%d pts:%s pts_time:%s", bbox->frame,
75 av_ts2str(picref->pts), av_ts2timestr(picref->pts, &inlink->time_base));
78 av_log(ctx, AV_LOG_INFO,
79 "x1:%d x2:%d y1:%d y2:%d w:%d h:%d"
80 " crop=%d:%d:%d:%d drawbox=%d:%d:%d:%d",
81 box.x1, box.x2, box.y1, box.y2, w, h,
82 w, h, box.x1, box.y1, /* crop params */
83 box.x1, box.y1, w, h); /* drawbox params */
85 av_log(ctx, AV_LOG_INFO, "\n");
88 avfilter_end_frame(inlink->dst->outputs[0]);
91 AVFilter avfilter_vf_bbox = {
93 .description = NULL_IF_CONFIG_SMALL("Compute bounding box for each frame."),
94 .priv_size = sizeof(BBoxContext),
95 .query_formats = query_formats,
98 .inputs = (const AVFilterPad[]) {
100 .type = AVMEDIA_TYPE_VIDEO,
101 .get_video_buffer = avfilter_null_get_video_buffer,
102 .start_frame = avfilter_null_start_frame,
103 .end_frame = end_frame,
104 .min_perms = AV_PERM_READ, },
108 .outputs = (const AVFilterPad[]) {
110 .type = AVMEDIA_TYPE_VIDEO },