2 * Copyright (c) 2002 A'rpi
3 * This file is part of Libav.
5 * Libav 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 * Libav 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 Libav; 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"
30 #include "libavutil/opt.h"
37 typedef struct CropDetectContext {
47 static int query_formats(AVFilterContext *ctx)
49 static const enum AVPixelFormat pix_fmts[] = {
50 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
51 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P,
52 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
53 AV_PIX_FMT_YUV411P, AV_PIX_FMT_GRAY8,
54 AV_PIX_FMT_NV12, AV_PIX_FMT_NV21,
58 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
62 static int checkline(void *ctx, const unsigned char *src, int stride, int len, int bpp)
77 total += src[0] + src[1] + src[2];
85 av_log(ctx, AV_LOG_DEBUG, "total:%d\n", total);
89 static av_cold int init(AVFilterContext *ctx)
91 CropDetectContext *s = ctx->priv;
95 av_log(ctx, AV_LOG_VERBOSE, "limit:%d round:%d reset_count:%d\n",
96 s->limit, s->round, s->reset_count);
101 static int config_input(AVFilterLink *inlink)
103 AVFilterContext *ctx = inlink->dst;
104 CropDetectContext *s = ctx->priv;
106 av_image_fill_max_pixsteps(s->max_pixsteps, NULL,
107 av_pix_fmt_desc_get(inlink->format));
109 s->x1 = inlink->w - 1;
110 s->y1 = inlink->h - 1;
117 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
119 AVFilterContext *ctx = inlink->dst;
120 CropDetectContext *s = ctx->priv;
121 int bpp = s->max_pixsteps[0];
122 int w, h, x, y, shrink_by;
124 // ignore first 2 frames - they may be empty
125 if (++s->frame_nb > 0) {
126 // Reset the crop area every reset_count frames, if reset_count is > 0
127 if (s->reset_count > 0 && s->frame_nb > s->reset_count) {
128 s->x1 = frame->width - 1;
129 s->y1 = frame->height - 1;
135 for (y = 0; y < s->y1; y++) {
136 if (checkline(ctx, frame->data[0] + frame->linesize[0] * y, bpp, frame->width, bpp) > s->limit) {
142 for (y = frame->height - 1; y > s->y2; y--) {
143 if (checkline(ctx, frame->data[0] + frame->linesize[0] * y, bpp, frame->width, bpp) > s->limit) {
149 for (y = 0; y < s->x1; y++) {
150 if (checkline(ctx, frame->data[0] + bpp*y, frame->linesize[0], frame->height, bpp) > s->limit) {
156 for (y = frame->width - 1; y > s->x2; y--) {
157 if (checkline(ctx, frame->data[0] + bpp*y, frame->linesize[0], frame->height, bpp) > s->limit) {
163 // round x and y (up), important for yuv colorspaces
164 // make sure they stay rounded!
171 // w and h must be divisible by 2 as well because of yuv
172 // colorspace problems.
178 shrink_by = w % s->round;
180 x += (shrink_by/2 + 1) & ~1;
182 shrink_by = h % s->round;
184 y += (shrink_by/2 + 1) & ~1;
186 av_log(ctx, AV_LOG_INFO,
187 "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",
188 s->x1, s->x2, s->y1, s->y2, w, h, x, y, frame->pts,
189 frame->pts == AV_NOPTS_VALUE ? -1 : frame->pts * av_q2d(inlink->time_base),
193 return ff_filter_frame(inlink->dst->outputs[0], frame);
196 #define OFFSET(x) offsetof(CropDetectContext, x)
197 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
198 static const AVOption options[] = {
199 { "limit", "Threshold below which the pixel is considered black", OFFSET(limit), AV_OPT_TYPE_INT, { .i64 = 24 }, 0, INT_MAX, FLAGS },
200 { "round", "Value by which the width/height should be divisible", OFFSET(round), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
201 { "reset", "Recalculate the crop area after this many frames", OFFSET(reset_count), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
205 static const AVClass cropdetect_class = {
206 .class_name = "cropdetect",
207 .item_name = av_default_item_name,
209 .version = LIBAVUTIL_VERSION_INT,
212 static const AVFilterPad avfilter_vf_cropdetect_inputs[] = {
215 .type = AVMEDIA_TYPE_VIDEO,
216 .config_props = config_input,
217 .get_video_buffer = ff_null_get_video_buffer,
218 .filter_frame = filter_frame,
223 static const AVFilterPad avfilter_vf_cropdetect_outputs[] = {
226 .type = AVMEDIA_TYPE_VIDEO
231 AVFilter ff_vf_cropdetect = {
232 .name = "cropdetect",
233 .description = NULL_IF_CONFIG_SMALL("Auto-detect crop size."),
235 .priv_size = sizeof(CropDetectContext),
236 .priv_class = &cropdetect_class,
239 .query_formats = query_formats,
241 .inputs = avfilter_vf_cropdetect_inputs,
243 .outputs = avfilter_vf_cropdetect_outputs,