2 * Copyright (c) 2002 A'rpi
3 * This file is part of FFmpeg.
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.
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.
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.
22 * border detection filter
23 * Ported from MPlayer libmpcodecs/vf_cropdetect.c.
26 #include "libavutil/imgutils.h"
27 #include "libavutil/internal.h"
28 #include "libavutil/opt.h"
35 typedef struct CropDetectContext {
46 static int query_formats(AVFilterContext *ctx)
48 static const enum AVPixelFormat pix_fmts[] = {
49 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
50 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P,
51 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
52 AV_PIX_FMT_YUV411P, AV_PIX_FMT_GRAY8,
53 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV410P,
54 AV_PIX_FMT_YUV420P9 , AV_PIX_FMT_YUV422P9 , AV_PIX_FMT_YUV444P9,
55 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
56 AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
57 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
58 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
59 AV_PIX_FMT_NV12, AV_PIX_FMT_NV21,
60 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
61 AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
65 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
69 static int checkline(void *ctx, const unsigned char *src, int stride, int len, int bpp)
73 const uint16_t *src16 = (const uint16_t *)src;
78 total += src[ 0] + src[ stride] + src[2*stride] + src[3*stride]
79 + src[4*stride] + src[5*stride] + src[6*stride] + src[7*stride];
91 total += src16[ 0] + src16[ stride] + src16[2*stride] + src16[3*stride]
92 + src16[4*stride] + src16[5*stride] + src16[6*stride] + src16[7*stride];
104 total += src[0] + src[1 ] + src[2 ]
105 + src[ stride] + src[1+ stride] + src[2+ stride];
106 + src[2*stride] + src[1+2*stride] + src[2+2*stride];
107 + src[3*stride] + src[1+3*stride] + src[2+3*stride];
112 total += src[0] + src[1] + src[2];
120 av_log(ctx, AV_LOG_DEBUG, "total:%d\n", total);
124 static av_cold int init(AVFilterContext *ctx)
126 CropDetectContext *s = ctx->priv;
130 av_log(ctx, AV_LOG_VERBOSE, "limit:%d round:%d reset_count:%d\n",
131 s->limit, s->round, s->reset_count);
136 static int config_input(AVFilterLink *inlink)
138 AVFilterContext *ctx = inlink->dst;
139 CropDetectContext *s = ctx->priv;
141 av_image_fill_max_pixsteps(s->max_pixsteps, NULL,
142 av_pix_fmt_desc_get(inlink->format));
144 s->x1 = inlink->w - 1;
145 s->y1 = inlink->h - 1;
152 #define SET_META(key, value) \
153 av_dict_set_int(metadata, key, value, 0)
155 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
157 AVFilterContext *ctx = inlink->dst;
158 CropDetectContext *s = ctx->priv;
159 int bpp = s->max_pixsteps[0];
160 int w, h, x, y, shrink_by;
161 AVDictionary **metadata;
162 int outliers, last_y;
164 // ignore first 2 frames - they may be empty
165 if (++s->frame_nb > 0) {
166 metadata = avpriv_frame_get_metadatap(frame);
168 // Reset the crop area every reset_count frames, if reset_count is > 0
169 if (s->reset_count > 0 && s->frame_nb > s->reset_count) {
170 s->x1 = frame->width - 1;
171 s->y1 = frame->height - 1;
177 #define FIND(DST, FROM, NOEND, INC, STEP0, STEP1, LEN) \
179 for (last_y = y = FROM; NOEND; y = y INC) {\
180 if (checkline(ctx, frame->data[0] + STEP0 * y, STEP1, LEN, bpp) > s->limit) {\
181 if (++outliers > s->max_outliers) { \
189 FIND(s->y1, 0, y < s->y1, +1, frame->linesize[0], bpp, frame->width);
190 FIND(s->y2, frame->height - 1, y > FFMAX(s->y2, s->y1), -1, frame->linesize[0], bpp, frame->width);
191 FIND(s->x1, 0, y < s->x1, +1, bpp, frame->linesize[0], frame->height);
192 FIND(s->x2, frame->width - 1, y > FFMAX(s->x2, s->x1), -1, bpp, frame->linesize[0], frame->height);
195 // round x and y (up), important for yuv colorspaces
196 // make sure they stay rounded!
203 // w and h must be divisible by 2 as well because of yuv
204 // colorspace problems.
210 shrink_by = w % s->round;
212 x += (shrink_by/2 + 1) & ~1;
214 shrink_by = h % s->round;
216 y += (shrink_by/2 + 1) & ~1;
218 SET_META("lavfi.cropdetect.x1", s->x1);
219 SET_META("lavfi.cropdetect.x2", s->x2);
220 SET_META("lavfi.cropdetect.y1", s->y1);
221 SET_META("lavfi.cropdetect.y2", s->y2);
222 SET_META("lavfi.cropdetect.w", w);
223 SET_META("lavfi.cropdetect.h", h);
224 SET_META("lavfi.cropdetect.x", x);
225 SET_META("lavfi.cropdetect.y", y);
227 av_log(ctx, AV_LOG_INFO,
228 "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",
229 s->x1, s->x2, s->y1, s->y2, w, h, x, y, frame->pts,
230 frame->pts == AV_NOPTS_VALUE ? -1 : frame->pts * av_q2d(inlink->time_base),
234 return ff_filter_frame(inlink->dst->outputs[0], frame);
237 #define OFFSET(x) offsetof(CropDetectContext, x)
238 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
240 static const AVOption cropdetect_options[] = {
241 { "limit", "Threshold below which the pixel is considered black", OFFSET(limit), AV_OPT_TYPE_INT, { .i64 = 24 }, 0, 255, FLAGS },
242 { "round", "Value by which the width/height should be divisible", OFFSET(round), AV_OPT_TYPE_INT, { .i64 = 16 }, 0, INT_MAX, FLAGS },
243 { "reset", "Recalculate the crop area after this many frames", OFFSET(reset_count), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
244 { "reset_count", "Recalculate the crop area after this many frames",OFFSET(reset_count),AV_OPT_TYPE_INT,{ .i64 = 0 }, 0, INT_MAX, FLAGS },
245 { "max_outliers", "Threshold count of outliers", OFFSET(max_outliers),AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
249 AVFILTER_DEFINE_CLASS(cropdetect);
251 static const AVFilterPad avfilter_vf_cropdetect_inputs[] = {
254 .type = AVMEDIA_TYPE_VIDEO,
255 .config_props = config_input,
256 .filter_frame = filter_frame,
261 static const AVFilterPad avfilter_vf_cropdetect_outputs[] = {
264 .type = AVMEDIA_TYPE_VIDEO
269 AVFilter ff_vf_cropdetect = {
270 .name = "cropdetect",
271 .description = NULL_IF_CONFIG_SMALL("Auto-detect crop size."),
272 .priv_size = sizeof(CropDetectContext),
273 .priv_class = &cropdetect_class,
275 .query_formats = query_formats,
276 .inputs = avfilter_vf_cropdetect_inputs,
277 .outputs = avfilter_vf_cropdetect_outputs,
278 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,