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