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.
26 #include "libavutil/imgutils.h"
40 static int query_formats(AVFilterContext *ctx)
42 static const enum PixelFormat pix_fmts[] = {
43 PIX_FMT_YUV420P, PIX_FMT_YUVJ420P,
44 PIX_FMT_YUV422P, PIX_FMT_YUVJ422P,
45 PIX_FMT_YUV444P, PIX_FMT_YUVJ444P,
46 PIX_FMT_YUV411P, PIX_FMT_GRAY8,
47 PIX_FMT_NV12, PIX_FMT_NV21,
51 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
55 static int checkline(void *ctx, const unsigned char *src, int stride, int len, int bpp)
70 total += src[0] + src[1] + src[2];
78 av_log(ctx, AV_LOG_DEBUG, "total:%d\n", total);
82 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
84 CropDetectContext *cd = ctx->priv;
92 sscanf(args, "%d:%d:%d", &cd->limit, &cd->round, &cd->reset_count);
94 av_log(ctx, AV_LOG_INFO, "limit:%d round:%d reset_count:%d\n",
95 cd->limit, cd->round, cd->reset_count);
100 static int config_input(AVFilterLink *inlink)
102 AVFilterContext *ctx = inlink->dst;
103 CropDetectContext *cd = ctx->priv;
105 av_image_fill_max_pixsteps(cd->max_pixsteps, NULL,
106 &av_pix_fmt_descriptors[inlink->format]);
108 cd->x1 = inlink->w - 1;
109 cd->y1 = inlink->h - 1;
116 static void end_frame(AVFilterLink *inlink)
118 AVFilterContext *ctx = inlink->dst;
119 CropDetectContext *cd = ctx->priv;
120 AVFilterBufferRef *picref = inlink->cur_buf;
121 int bpp = cd->max_pixsteps[0];
122 int w, h, x, y, shrink_by;
124 // ignore first 2 frames - they may be empty
125 if (++cd->frame_nb > 0) {
126 // Reset the crop area every reset_count frames, if reset_count is > 0
127 if (cd->reset_count > 0 && cd->frame_nb > cd->reset_count) {
128 cd->x1 = picref->video->w-1;
129 cd->y1 = picref->video->h-1;
135 for (y = 0; y < cd->y1; y++) {
136 if (checkline(ctx, picref->data[0] + picref->linesize[0] * y, bpp, picref->video->w, bpp) > cd->limit) {
142 for (y = picref->video->h-1; y > cd->y2; y--) {
143 if (checkline(ctx, picref->data[0] + picref->linesize[0] * y, bpp, picref->video->w, bpp) > cd->limit) {
149 for (y = 0; y < cd->x1; y++) {
150 if (checkline(ctx, picref->data[0] + bpp*y, picref->linesize[0], picref->video->h, bpp) > cd->limit) {
156 for (y = picref->video->w-1; y > cd->x2; y--) {
157 if (checkline(ctx, picref->data[0] + bpp*y, picref->linesize[0], picref->video->h, bpp) > cd->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 % cd->round;
180 x += (shrink_by/2 + 1) & ~1;
182 shrink_by = h % cd->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 pos:%"PRId64" pts:%"PRId64" t:%f crop=%d:%d:%d:%d\n",
188 cd->x1, cd->x2, cd->y1, cd->y2, w, h, x, y, picref->pos, picref->pts,
189 picref->pts == AV_NOPTS_VALUE ? -1 : picref->pts * av_q2d(inlink->time_base),
193 avfilter_end_frame(inlink->dst->outputs[0]);
196 AVFilter avfilter_vf_cropdetect = {
197 .name = "cropdetect",
198 .description = NULL_IF_CONFIG_SMALL("Auto-detect crop size."),
200 .priv_size = sizeof(CropDetectContext),
203 .query_formats = query_formats,
205 .inputs = (AVFilterPad[]) {{ .name = "default",
206 .type = AVMEDIA_TYPE_VIDEO,
207 .config_props = config_input,
208 .get_video_buffer = ff_null_get_video_buffer,
209 .start_frame = ff_null_start_frame,
210 .end_frame = end_frame, },
213 .outputs = (AVFilterPad[]) {{ .name = "default",
214 .type = AVMEDIA_TYPE_VIDEO },