]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_cropdetect.c
lavfi: use const for AVFilterPad declarations in all filters.
[ffmpeg] / libavfilter / vf_cropdetect.c
1 /*
2  * Copyright (c) 2002 A'rpi
3  * This file is part of Libav.
4  *
5  * Libav is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * Libav is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with Libav; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19
20 /**
21  * @file
22  * border detection filter
23  * Ported from MPlayer libmpcodecs/vf_cropdetect.c.
24  */
25
26 #include "libavutil/imgutils.h"
27 #include "avfilter.h"
28 #include "formats.h"
29 #include "internal.h"
30 #include "video.h"
31
32 typedef struct {
33     int x1, y1, x2, y2;
34     int limit;
35     int round;
36     int reset_count;
37     int frame_nb;
38     int max_pixsteps[4];
39 } CropDetectContext;
40
41 static int query_formats(AVFilterContext *ctx)
42 {
43     static const enum PixelFormat pix_fmts[] = {
44         PIX_FMT_YUV420P, PIX_FMT_YUVJ420P,
45         PIX_FMT_YUV422P, PIX_FMT_YUVJ422P,
46         PIX_FMT_YUV444P, PIX_FMT_YUVJ444P,
47         PIX_FMT_YUV411P, PIX_FMT_GRAY8,
48         PIX_FMT_NV12,    PIX_FMT_NV21,
49         PIX_FMT_NONE
50     };
51
52     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
53     return 0;
54 }
55
56 static int checkline(void *ctx, const unsigned char *src, int stride, int len, int bpp)
57 {
58     int total = 0;
59     int div = len;
60
61     switch (bpp) {
62     case 1:
63         while (--len >= 0) {
64             total += src[0];
65             src += stride;
66         }
67         break;
68     case 3:
69     case 4:
70         while (--len >= 0) {
71             total += src[0] + src[1] + src[2];
72             src += stride;
73         }
74         div *= 3;
75         break;
76     }
77     total /= div;
78
79     av_log(ctx, AV_LOG_DEBUG, "total:%d\n", total);
80     return total;
81 }
82
83 static av_cold int init(AVFilterContext *ctx, const char *args)
84 {
85     CropDetectContext *cd = ctx->priv;
86
87     cd->limit = 24;
88     cd->round = 0;
89     cd->reset_count = 0;
90     cd->frame_nb = -2;
91
92     if (args)
93         sscanf(args, "%d:%d:%d", &cd->limit, &cd->round, &cd->reset_count);
94
95     av_log(ctx, AV_LOG_VERBOSE, "limit:%d round:%d reset_count:%d\n",
96            cd->limit, cd->round, cd->reset_count);
97
98     return 0;
99 }
100
101 static int config_input(AVFilterLink *inlink)
102 {
103     AVFilterContext *ctx = inlink->dst;
104     CropDetectContext *cd = ctx->priv;
105
106     av_image_fill_max_pixsteps(cd->max_pixsteps, NULL,
107                                &av_pix_fmt_descriptors[inlink->format]);
108
109     cd->x1 = inlink->w - 1;
110     cd->y1 = inlink->h - 1;
111     cd->x2 = 0;
112     cd->y2 = 0;
113
114     return 0;
115 }
116
117 static void end_frame(AVFilterLink *inlink)
118 {
119     AVFilterContext *ctx = inlink->dst;
120     CropDetectContext *cd = ctx->priv;
121     AVFilterBufferRef *picref = inlink->cur_buf;
122     int bpp = cd->max_pixsteps[0];
123     int w, h, x, y, shrink_by;
124
125     // ignore first 2 frames - they may be empty
126     if (++cd->frame_nb > 0) {
127         // Reset the crop area every reset_count frames, if reset_count is > 0
128         if (cd->reset_count > 0 && cd->frame_nb > cd->reset_count) {
129             cd->x1 = picref->video->w-1;
130             cd->y1 = picref->video->h-1;
131             cd->x2 = 0;
132             cd->y2 = 0;
133             cd->frame_nb = 1;
134         }
135
136         for (y = 0; y < cd->y1; y++) {
137             if (checkline(ctx, picref->data[0] + picref->linesize[0] * y, bpp, picref->video->w, bpp) > cd->limit) {
138                 cd->y1 = y;
139                 break;
140             }
141         }
142
143         for (y = picref->video->h-1; y > cd->y2; y--) {
144             if (checkline(ctx, picref->data[0] + picref->linesize[0] * y, bpp, picref->video->w, bpp) > cd->limit) {
145                 cd->y2 = y;
146                 break;
147             }
148         }
149
150         for (y = 0; y < cd->x1; y++) {
151             if (checkline(ctx, picref->data[0] + bpp*y, picref->linesize[0], picref->video->h, bpp) > cd->limit) {
152                 cd->x1 = y;
153                 break;
154             }
155         }
156
157         for (y = picref->video->w-1; y > cd->x2; y--) {
158             if (checkline(ctx, picref->data[0] + bpp*y, picref->linesize[0], picref->video->h, bpp) > cd->limit) {
159                 cd->x2 = y;
160                 break;
161             }
162         }
163
164         // round x and y (up), important for yuv colorspaces
165         // make sure they stay rounded!
166         x = (cd->x1+1) & ~1;
167         y = (cd->y1+1) & ~1;
168
169         w = cd->x2 - x + 1;
170         h = cd->y2 - y + 1;
171
172         // w and h must be divisible by 2 as well because of yuv
173         // colorspace problems.
174         if (cd->round <= 1)
175             cd->round = 16;
176         if (cd->round % 2)
177             cd->round *= 2;
178
179         shrink_by = w % cd->round;
180         w -= shrink_by;
181         x += (shrink_by/2 + 1) & ~1;
182
183         shrink_by = h % cd->round;
184         h -= shrink_by;
185         y += (shrink_by/2 + 1) & ~1;
186
187         av_log(ctx, AV_LOG_INFO,
188                "x1:%d x2:%d y1:%d y2:%d w:%d h:%d x:%d y:%d pos:%"PRId64" pts:%"PRId64" t:%f crop=%d:%d:%d:%d\n",
189                cd->x1, cd->x2, cd->y1, cd->y2, w, h, x, y, picref->pos, picref->pts,
190                picref->pts == AV_NOPTS_VALUE ? -1 : picref->pts * av_q2d(inlink->time_base),
191                w, h, x, y);
192     }
193
194     ff_end_frame(inlink->dst->outputs[0]);
195 }
196
197 AVFilter avfilter_vf_cropdetect = {
198     .name        = "cropdetect",
199     .description = NULL_IF_CONFIG_SMALL("Auto-detect crop size."),
200
201     .priv_size = sizeof(CropDetectContext),
202     .init      = init,
203
204     .query_formats = query_formats,
205
206     .inputs    = (const AVFilterPad[]) {{ .name = "default",
207                                           .type             = AVMEDIA_TYPE_VIDEO,
208                                           .config_props     = config_input,
209                                           .get_video_buffer = ff_null_get_video_buffer,
210                                           .start_frame      = ff_null_start_frame,
211                                           .end_frame        = end_frame, },
212                                         { .name = NULL}},
213
214     .outputs   = (const AVFilterPad[]) {{ .name             = "default",
215                                           .type             = AVMEDIA_TYPE_VIDEO },
216                                         { .name = NULL}},
217 };