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 int query_formats(AVFilterContext *ctx)
38 static const enum AVPixelFormat pix_fmts[] = {
47 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
51 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
53 AVFilterContext *ctx = inlink->dst;
54 BBoxContext *bbox = ctx->priv;
59 ff_calculate_bounding_box(&box,
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;
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));
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 */
77 av_log(ctx, AV_LOG_INFO, "\n");
80 return ff_filter_frame(inlink->dst->outputs[0], frame);
83 static const AVFilterPad bbox_inputs[] = {
86 .type = AVMEDIA_TYPE_VIDEO,
87 .filter_frame = filter_frame,
92 static const AVFilterPad bbox_outputs[] = {
95 .type = AVMEDIA_TYPE_VIDEO,
100 AVFilter avfilter_vf_bbox = {
102 .description = NULL_IF_CONFIG_SMALL("Compute bounding box for each frame."),
103 .priv_size = sizeof(BBoxContext),
104 .query_formats = query_formats,
105 .inputs = bbox_inputs,
106 .outputs = bbox_outputs,