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.
28 #include "libavutil/imgutils.h"
29 #include "libavutil/internal.h"
44 static int query_formats(AVFilterContext *ctx)
46 static const enum AVPixelFormat pix_fmts[] = {
47 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
48 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P,
49 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
50 AV_PIX_FMT_YUV411P, AV_PIX_FMT_GRAY8,
51 AV_PIX_FMT_NV12, AV_PIX_FMT_NV21,
55 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
59 static int checkline(void *ctx, const unsigned char *src, int stride, int len, int bpp)
74 total += src[0] + src[1] + src[2];
82 av_log(ctx, AV_LOG_DEBUG, "total:%d\n", total);
86 static av_cold int init(AVFilterContext *ctx, const char *args)
88 CropDetectContext *cd = ctx->priv;
96 sscanf(args, "%d:%d:%d", &cd->limit, &cd->round, &cd->reset_count);
98 av_log(ctx, AV_LOG_VERBOSE, "limit:%d round:%d reset_count:%d\n",
99 cd->limit, cd->round, cd->reset_count);
104 static int config_input(AVFilterLink *inlink)
106 AVFilterContext *ctx = inlink->dst;
107 CropDetectContext *cd = ctx->priv;
109 av_image_fill_max_pixsteps(cd->max_pixsteps, NULL,
110 av_pix_fmt_desc_get(inlink->format));
112 cd->x1 = inlink->w - 1;
113 cd->y1 = inlink->h - 1;
120 static int end_frame(AVFilterLink *inlink)
122 AVFilterContext *ctx = inlink->dst;
123 CropDetectContext *cd = ctx->priv;
124 AVFilterBufferRef *picref = inlink->cur_buf;
125 int bpp = cd->max_pixsteps[0];
126 int w, h, x, y, shrink_by;
128 // ignore first 2 frames - they may be empty
129 if (++cd->frame_nb > 0) {
130 // Reset the crop area every reset_count frames, if reset_count is > 0
131 if (cd->reset_count > 0 && cd->frame_nb > cd->reset_count) {
132 cd->x1 = picref->video->w-1;
133 cd->y1 = picref->video->h-1;
139 for (y = 0; y < cd->y1; y++) {
140 if (checkline(ctx, picref->data[0] + picref->linesize[0] * y, bpp, picref->video->w, bpp) > cd->limit) {
146 for (y = picref->video->h-1; y > cd->y2; y--) {
147 if (checkline(ctx, picref->data[0] + picref->linesize[0] * y, bpp, picref->video->w, bpp) > cd->limit) {
153 for (y = 0; y < cd->x1; y++) {
154 if (checkline(ctx, picref->data[0] + bpp*y, picref->linesize[0], picref->video->h, bpp) > cd->limit) {
160 for (y = picref->video->w-1; y > cd->x2; y--) {
161 if (checkline(ctx, picref->data[0] + bpp*y, picref->linesize[0], picref->video->h, bpp) > cd->limit) {
167 // round x and y (up), important for yuv colorspaces
168 // make sure they stay rounded!
175 // w and h must be divisible by 2 as well because of yuv
176 // colorspace problems.
182 shrink_by = w % cd->round;
184 x += (shrink_by/2 + 1) & ~1;
186 shrink_by = h % cd->round;
188 y += (shrink_by/2 + 1) & ~1;
190 av_log(ctx, AV_LOG_INFO,
191 "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",
192 cd->x1, cd->x2, cd->y1, cd->y2, w, h, x, y, picref->pos, picref->pts,
193 picref->pts == AV_NOPTS_VALUE ? -1 : picref->pts * av_q2d(inlink->time_base),
197 return ff_end_frame(inlink->dst->outputs[0]);
200 static const AVFilterPad avfilter_vf_cropdetect_inputs[] = {
203 .type = AVMEDIA_TYPE_VIDEO,
204 .config_props = config_input,
205 .get_video_buffer = ff_null_get_video_buffer,
206 .start_frame = ff_null_start_frame,
207 .end_frame = end_frame,
212 static const AVFilterPad avfilter_vf_cropdetect_outputs[] = {
215 .type = AVMEDIA_TYPE_VIDEO
220 AVFilter avfilter_vf_cropdetect = {
221 .name = "cropdetect",
222 .description = NULL_IF_CONFIG_SMALL("Auto-detect crop size."),
224 .priv_size = sizeof(CropDetectContext),
227 .query_formats = query_formats,
229 .inputs = avfilter_vf_cropdetect_inputs,
231 .outputs = avfilter_vf_cropdetect_outputs,