]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_bbox.c
Merge commit '218aefce4472dc02ee3f12830a9a894bf7916da9'
[ffmpeg] / libavfilter / vf_bbox.c
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     int vsub, hsub;
35 } BBoxContext;
36
37 static av_cold int init(AVFilterContext *ctx, const char *args)
38 {
39     BBoxContext *bbox = ctx->priv;
40     bbox->frame = 0;
41     return 0;
42 }
43
44 static int query_formats(AVFilterContext *ctx)
45 {
46     static const enum AVPixelFormat pix_fmts[] = {
47         AV_PIX_FMT_YUV420P,
48         AV_PIX_FMT_YUV444P,
49         AV_PIX_FMT_YUV440P,
50         AV_PIX_FMT_YUV422P,
51         AV_PIX_FMT_YUV411P,
52         AV_PIX_FMT_NONE,
53     };
54
55     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
56     return 0;
57 }
58
59 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
60 {
61     AVFilterContext *ctx = inlink->dst;
62     BBoxContext *bbox = ctx->priv;
63     FFBoundingBox box;
64     int has_bbox, w, h;
65
66     has_bbox =
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;
72
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));
76
77     if (has_bbox) {
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 */
84     }
85     av_log(ctx, AV_LOG_INFO, "\n");
86
87     bbox->frame++;
88     return ff_filter_frame(inlink->dst->outputs[0], picref);
89 }
90
91 static const AVFilterPad bbox_inputs[] = {
92     {
93         .name             = "default",
94         .type             = AVMEDIA_TYPE_VIDEO,
95         .get_video_buffer = ff_null_get_video_buffer,
96         .filter_frame     = filter_frame,
97         .min_perms        = AV_PERM_READ,
98     },
99     { NULL }
100 };
101
102 static const AVFilterPad bbox_outputs[] = {
103     {
104         .name = "default",
105         .type = AVMEDIA_TYPE_VIDEO,
106     },
107     { NULL }
108 };
109
110 AVFilter avfilter_vf_bbox = {
111     .name          = "bbox",
112     .description   = NULL_IF_CONFIG_SMALL("Compute bounding box for each frame."),
113     .priv_size     = sizeof(BBoxContext),
114     .query_formats = query_formats,
115     .init          = init,
116     .inputs        = bbox_inputs,
117     .outputs       = bbox_outputs,
118 };