]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_cropdetect.c
Merge commit 'cf53704c55378cc0dcfc16637cdac7d58f0b3107'
[ffmpeg] / libavfilter / vf_cropdetect.c
1 /*
2  * Copyright (c) 2002 A'rpi
3  * This file is part of FFmpeg.
4  *
5  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 "libavutil/internal.h"
28 #include "libavutil/opt.h"
29 #include "avfilter.h"
30 #include "formats.h"
31 #include "internal.h"
32 #include "video.h"
33
34 typedef struct {
35     const AVClass *class;
36     int x1, y1, x2, y2;
37     int limit;
38     int round;
39     int reset_count;
40     int frame_nb;
41     int max_pixsteps[4];
42 } CropDetectContext;
43
44 #define OFFSET(x) offsetof(CropDetectContext, x)
45 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
46
47 static const AVOption cropdetect_options[] = {
48     { "limit", "set black threshold", OFFSET(limit), AV_OPT_TYPE_INT, {.i64=24}, 0, 255, FLAGS },
49     { "round", "set width/height round value", OFFSET(round), AV_OPT_TYPE_INT, {.i64=16}, 0, INT_MAX, FLAGS },
50     { "reset_count", "set after how many frames to reset detected info",  OFFSET(reset_count), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
51     { NULL }
52 };
53
54 AVFILTER_DEFINE_CLASS(cropdetect);
55
56 static int query_formats(AVFilterContext *ctx)
57 {
58     static const enum AVPixelFormat pix_fmts[] = {
59         AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
60         AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P,
61         AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
62         AV_PIX_FMT_YUV411P, AV_PIX_FMT_GRAY8,
63         AV_PIX_FMT_NV12,    AV_PIX_FMT_NV21,
64         AV_PIX_FMT_NONE
65     };
66
67     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
68     return 0;
69 }
70
71 static int checkline(void *ctx, const unsigned char *src, int stride, int len, int bpp)
72 {
73     int total = 0;
74     int div = len;
75
76     switch (bpp) {
77     case 1:
78         while (--len >= 0) {
79             total += src[0];
80             src += stride;
81         }
82         break;
83     case 3:
84     case 4:
85         while (--len >= 0) {
86             total += src[0] + src[1] + src[2];
87             src += stride;
88         }
89         div *= 3;
90         break;
91     }
92     total /= div;
93
94     av_log(ctx, AV_LOG_DEBUG, "total:%d\n", total);
95     return total;
96 }
97
98 static av_cold int init(AVFilterContext *ctx, const char *args)
99 {
100     CropDetectContext *cd = ctx->priv;
101
102     cd->frame_nb = -2;
103     av_log(ctx, AV_LOG_VERBOSE, "limit:%d round:%d reset_count:%d\n",
104            cd->limit, cd->round, cd->reset_count);
105
106     return 0;
107 }
108
109 static int config_input(AVFilterLink *inlink)
110 {
111     AVFilterContext *ctx = inlink->dst;
112     CropDetectContext *cd = ctx->priv;
113
114     av_image_fill_max_pixsteps(cd->max_pixsteps, NULL,
115                                av_pix_fmt_desc_get(inlink->format));
116
117     cd->x1 = inlink->w - 1;
118     cd->y1 = inlink->h - 1;
119     cd->x2 = 0;
120     cd->y2 = 0;
121
122     return 0;
123 }
124
125 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
126 {
127     AVFilterContext *ctx = inlink->dst;
128     CropDetectContext *cd = ctx->priv;
129     int bpp = cd->max_pixsteps[0];
130     int w, h, x, y, shrink_by;
131
132     // ignore first 2 frames - they may be empty
133     if (++cd->frame_nb > 0) {
134         // Reset the crop area every reset_count frames, if reset_count is > 0
135         if (cd->reset_count > 0 && cd->frame_nb > cd->reset_count) {
136             cd->x1 = frame->width  - 1;
137             cd->y1 = frame->height - 1;
138             cd->x2 = 0;
139             cd->y2 = 0;
140             cd->frame_nb = 1;
141         }
142
143         for (y = 0; y < cd->y1; y++) {
144             if (checkline(ctx, frame->data[0] + frame->linesize[0] * y, bpp, frame->width, bpp) > cd->limit) {
145                 cd->y1 = y;
146                 break;
147             }
148         }
149
150         for (y = frame->height - 1; y > cd->y2; y--) {
151             if (checkline(ctx, frame->data[0] + frame->linesize[0] * y, bpp, frame->width, bpp) > cd->limit) {
152                 cd->y2 = y;
153                 break;
154             }
155         }
156
157         for (y = 0; y < cd->x1; y++) {
158             if (checkline(ctx, frame->data[0] + bpp*y, frame->linesize[0], frame->height, bpp) > cd->limit) {
159                 cd->x1 = y;
160                 break;
161             }
162         }
163
164         for (y = frame->width - 1; y > cd->x2; y--) {
165             if (checkline(ctx, frame->data[0] + bpp*y, frame->linesize[0], frame->height, bpp) > cd->limit) {
166                 cd->x2 = y;
167                 break;
168             }
169         }
170
171         // round x and y (up), important for yuv colorspaces
172         // make sure they stay rounded!
173         x = (cd->x1+1) & ~1;
174         y = (cd->y1+1) & ~1;
175
176         w = cd->x2 - x + 1;
177         h = cd->y2 - y + 1;
178
179         // w and h must be divisible by 2 as well because of yuv
180         // colorspace problems.
181         if (cd->round <= 1)
182             cd->round = 16;
183         if (cd->round % 2)
184             cd->round *= 2;
185
186         shrink_by = w % cd->round;
187         w -= shrink_by;
188         x += (shrink_by/2 + 1) & ~1;
189
190         shrink_by = h % cd->round;
191         h -= shrink_by;
192         y += (shrink_by/2 + 1) & ~1;
193
194         av_log(ctx, AV_LOG_INFO,
195                "x1:%d x2:%d y1:%d y2:%d w:%d h:%d x:%d y:%d pts:%"PRId64" t:%f crop=%d:%d:%d:%d\n",
196                cd->x1, cd->x2, cd->y1, cd->y2, w, h, x, y, frame->pts,
197                frame->pts == AV_NOPTS_VALUE ? -1 : frame->pts * av_q2d(inlink->time_base),
198                w, h, x, y);
199     }
200
201     return ff_filter_frame(inlink->dst->outputs[0], frame);
202 }
203
204 static const AVFilterPad avfilter_vf_cropdetect_inputs[] = {
205     {
206         .name             = "default",
207         .type             = AVMEDIA_TYPE_VIDEO,
208         .config_props     = config_input,
209         .get_video_buffer = ff_null_get_video_buffer,
210         .filter_frame     = filter_frame,
211     },
212     { NULL }
213 };
214
215 static const AVFilterPad avfilter_vf_cropdetect_outputs[] = {
216     {
217         .name = "default",
218         .type = AVMEDIA_TYPE_VIDEO
219     },
220     { NULL }
221 };
222
223 static const char *const shorthand[] = { "limit", "round", "reset_count", NULL };
224
225 AVFilter avfilter_vf_cropdetect = {
226     .name        = "cropdetect",
227     .description = NULL_IF_CONFIG_SMALL("Auto-detect crop size."),
228
229     .priv_size = sizeof(CropDetectContext),
230     .init      = init,
231     .query_formats = query_formats,
232     .inputs    = avfilter_vf_cropdetect_inputs,
233     .outputs   = avfilter_vf_cropdetect_outputs,
234     .priv_class = &cropdetect_class,
235     .shorthand  = shorthand,
236 };