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