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"
45 static int query_formats(AVFilterContext *ctx)
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_NV12, AV_PIX_FMT_NV21,
56 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
60 static int checkline(void *ctx, const unsigned char *src, int stride, int len, int bpp)
75 total += src[0] + src[1] + src[2];
83 av_log(ctx, AV_LOG_DEBUG, "total:%d\n", total);
87 static av_cold int init(AVFilterContext *ctx, const char *args)
89 CropDetectContext *cd = ctx->priv;
93 av_log(ctx, AV_LOG_VERBOSE, "limit:%d round:%d reset_count:%d\n",
94 cd->limit, cd->round, cd->reset_count);
99 static int config_input(AVFilterLink *inlink)
101 AVFilterContext *ctx = inlink->dst;
102 CropDetectContext *cd = ctx->priv;
104 av_image_fill_max_pixsteps(cd->max_pixsteps, NULL,
105 av_pix_fmt_desc_get(inlink->format));
107 cd->x1 = inlink->w - 1;
108 cd->y1 = inlink->h - 1;
115 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
117 AVFilterContext *ctx = inlink->dst;
118 CropDetectContext *cd = ctx->priv;
119 int bpp = cd->max_pixsteps[0];
120 int w, h, x, y, shrink_by;
122 // ignore first 2 frames - they may be empty
123 if (++cd->frame_nb > 0) {
124 // Reset the crop area every reset_count frames, if reset_count is > 0
125 if (cd->reset_count > 0 && cd->frame_nb > cd->reset_count) {
126 cd->x1 = frame->width - 1;
127 cd->y1 = frame->height - 1;
133 for (y = 0; y < cd->y1; y++) {
134 if (checkline(ctx, frame->data[0] + frame->linesize[0] * y, bpp, frame->width, bpp) > cd->limit) {
140 for (y = frame->height - 1; y > cd->y2; y--) {
141 if (checkline(ctx, frame->data[0] + frame->linesize[0] * y, bpp, frame->width, bpp) > cd->limit) {
147 for (y = 0; y < cd->x1; y++) {
148 if (checkline(ctx, frame->data[0] + bpp*y, frame->linesize[0], frame->height, bpp) > cd->limit) {
154 for (y = frame->width - 1; y > cd->x2; y--) {
155 if (checkline(ctx, frame->data[0] + bpp*y, frame->linesize[0], frame->height, bpp) > cd->limit) {
161 // round x and y (up), important for yuv colorspaces
162 // make sure they stay rounded!
169 // w and h must be divisible by 2 as well because of yuv
170 // colorspace problems.
176 shrink_by = w % cd->round;
178 x += (shrink_by/2 + 1) & ~1;
180 shrink_by = h % cd->round;
182 y += (shrink_by/2 + 1) & ~1;
184 av_log(ctx, AV_LOG_INFO,
185 "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",
186 cd->x1, cd->x2, cd->y1, cd->y2, w, h, x, y, frame->pts,
187 frame->pts == AV_NOPTS_VALUE ? -1 : frame->pts * av_q2d(inlink->time_base),
191 return ff_filter_frame(inlink->dst->outputs[0], frame);
194 #define OFFSET(x) offsetof(CropDetectContext, x)
195 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
197 static const AVOption cropdetect_options[] = {
198 { "limit", "Threshold below which the pixel is considered black", OFFSET(limit), AV_OPT_TYPE_INT, { .i64 = 24 }, 0, 255, FLAGS },
199 { "round", "Value by which the width/height should be divisible", OFFSET(round), AV_OPT_TYPE_INT, { .i64 = 16 }, 0, INT_MAX, FLAGS },
200 { "reset", "Recalculate the crop area after this many frames", OFFSET(reset_count), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
201 { "reset_count", "Recalculate the crop area after this many frames",OFFSET(reset_count),AV_OPT_TYPE_INT,{ .i64 = 0 }, 0, INT_MAX, FLAGS },
205 AVFILTER_DEFINE_CLASS(cropdetect);
207 static const AVFilterPad avfilter_vf_cropdetect_inputs[] = {
210 .type = AVMEDIA_TYPE_VIDEO,
211 .config_props = config_input,
212 .get_video_buffer = ff_null_get_video_buffer,
213 .filter_frame = filter_frame,
218 static const AVFilterPad avfilter_vf_cropdetect_outputs[] = {
221 .type = AVMEDIA_TYPE_VIDEO
226 AVFilter avfilter_vf_cropdetect = {
227 .name = "cropdetect",
228 .description = NULL_IF_CONFIG_SMALL("Auto-detect crop size."),
230 .priv_size = sizeof(CropDetectContext),
231 .priv_class = &cropdetect_class,
233 .query_formats = query_formats,
234 .inputs = avfilter_vf_cropdetect_inputs,
235 .outputs = avfilter_vf_cropdetect_outputs,