]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_cropdetect.c
lavfi: make formats API private on next bump.
[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 "video.h"
30
31 typedef struct {
32     int x1, y1, x2, y2;
33     int limit;
34     int round;
35     int reset_count;
36     int frame_nb;
37     int max_pixsteps[4];
38 } CropDetectContext;
39
40 static int query_formats(AVFilterContext *ctx)
41 {
42     static const enum PixelFormat pix_fmts[] = {
43         PIX_FMT_YUV420P, PIX_FMT_YUVJ420P,
44         PIX_FMT_YUV422P, PIX_FMT_YUVJ422P,
45         PIX_FMT_YUV444P, PIX_FMT_YUVJ444P,
46         PIX_FMT_YUV411P, PIX_FMT_GRAY8,
47         PIX_FMT_NV12,    PIX_FMT_NV21,
48         PIX_FMT_NONE
49     };
50
51     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
52     return 0;
53 }
54
55 static int checkline(void *ctx, const unsigned char *src, int stride, int len, int bpp)
56 {
57     int total = 0;
58     int div = len;
59
60     switch (bpp) {
61     case 1:
62         while (--len >= 0) {
63             total += src[0];
64             src += stride;
65         }
66         break;
67     case 3:
68     case 4:
69         while (--len >= 0) {
70             total += src[0] + src[1] + src[2];
71             src += stride;
72         }
73         div *= 3;
74         break;
75     }
76     total /= div;
77
78     av_log(ctx, AV_LOG_DEBUG, "total:%d\n", total);
79     return total;
80 }
81
82 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
83 {
84     CropDetectContext *cd = ctx->priv;
85
86     cd->limit = 24;
87     cd->round = 0;
88     cd->reset_count = 0;
89     cd->frame_nb = -2;
90
91     if (args)
92         sscanf(args, "%d:%d:%d", &cd->limit, &cd->round, &cd->reset_count);
93
94     av_log(ctx, AV_LOG_INFO, "limit:%d round:%d reset_count:%d\n",
95            cd->limit, cd->round, cd->reset_count);
96
97     return 0;
98 }
99
100 static int config_input(AVFilterLink *inlink)
101 {
102     AVFilterContext *ctx = inlink->dst;
103     CropDetectContext *cd = ctx->priv;
104
105     av_image_fill_max_pixsteps(cd->max_pixsteps, NULL,
106                                &av_pix_fmt_descriptors[inlink->format]);
107
108     cd->x1 = inlink->w - 1;
109     cd->y1 = inlink->h - 1;
110     cd->x2 = 0;
111     cd->y2 = 0;
112
113     return 0;
114 }
115
116 static void end_frame(AVFilterLink *inlink)
117 {
118     AVFilterContext *ctx = inlink->dst;
119     CropDetectContext *cd = ctx->priv;
120     AVFilterBufferRef *picref = inlink->cur_buf;
121     int bpp = cd->max_pixsteps[0];
122     int w, h, x, y, shrink_by;
123
124     // ignore first 2 frames - they may be empty
125     if (++cd->frame_nb > 0) {
126         // Reset the crop area every reset_count frames, if reset_count is > 0
127         if (cd->reset_count > 0 && cd->frame_nb > cd->reset_count) {
128             cd->x1 = picref->video->w-1;
129             cd->y1 = picref->video->h-1;
130             cd->x2 = 0;
131             cd->y2 = 0;
132             cd->frame_nb = 1;
133         }
134
135         for (y = 0; y < cd->y1; y++) {
136             if (checkline(ctx, picref->data[0] + picref->linesize[0] * y, bpp, picref->video->w, bpp) > cd->limit) {
137                 cd->y1 = y;
138                 break;
139             }
140         }
141
142         for (y = picref->video->h-1; y > cd->y2; y--) {
143             if (checkline(ctx, picref->data[0] + picref->linesize[0] * y, bpp, picref->video->w, bpp) > cd->limit) {
144                 cd->y2 = y;
145                 break;
146             }
147         }
148
149         for (y = 0; y < cd->x1; y++) {
150             if (checkline(ctx, picref->data[0] + bpp*y, picref->linesize[0], picref->video->h, bpp) > cd->limit) {
151                 cd->x1 = y;
152                 break;
153             }
154         }
155
156         for (y = picref->video->w-1; y > cd->x2; y--) {
157             if (checkline(ctx, picref->data[0] + bpp*y, picref->linesize[0], picref->video->h, bpp) > cd->limit) {
158                 cd->x2 = y;
159                 break;
160             }
161         }
162
163         // round x and y (up), important for yuv colorspaces
164         // make sure they stay rounded!
165         x = (cd->x1+1) & ~1;
166         y = (cd->y1+1) & ~1;
167
168         w = cd->x2 - x + 1;
169         h = cd->y2 - y + 1;
170
171         // w and h must be divisible by 2 as well because of yuv
172         // colorspace problems.
173         if (cd->round <= 1)
174             cd->round = 16;
175         if (cd->round % 2)
176             cd->round *= 2;
177
178         shrink_by = w % cd->round;
179         w -= shrink_by;
180         x += (shrink_by/2 + 1) & ~1;
181
182         shrink_by = h % cd->round;
183         h -= shrink_by;
184         y += (shrink_by/2 + 1) & ~1;
185
186         av_log(ctx, AV_LOG_INFO,
187                "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",
188                cd->x1, cd->x2, cd->y1, cd->y2, w, h, x, y, picref->pos, picref->pts,
189                picref->pts == AV_NOPTS_VALUE ? -1 : picref->pts * av_q2d(inlink->time_base),
190                w, h, x, y);
191     }
192
193     avfilter_end_frame(inlink->dst->outputs[0]);
194 }
195
196 AVFilter avfilter_vf_cropdetect = {
197     .name        = "cropdetect",
198     .description = NULL_IF_CONFIG_SMALL("Auto-detect crop size."),
199
200     .priv_size = sizeof(CropDetectContext),
201     .init      = init,
202
203     .query_formats = query_formats,
204
205     .inputs    = (AVFilterPad[]) {{ .name = "default",
206                                     .type             = AVMEDIA_TYPE_VIDEO,
207                                     .config_props     = config_input,
208                                     .get_video_buffer = ff_null_get_video_buffer,
209                                     .start_frame      = ff_null_start_frame,
210                                     .end_frame        = end_frame, },
211                                   { .name = NULL}},
212
213     .outputs   = (AVFilterPad[]) {{ .name             = "default",
214                                     .type             = AVMEDIA_TYPE_VIDEO },
215                                   { .name = NULL}},
216 };