Replace PIX_FMT_* -> AV_PIX_FMT_*, PixelFormat -> AVPixelFormat
[libav.git] / libavfilter / vf_cropdetect.c
1 /*
2 * Copyright (c) 2002 A'rpi
3 * This file is part of Libav.
4 *
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.
9 *
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.
14 *
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.
18 */
19
20 /**
21 * @file
22 * border detection filter
23 * Ported from MPlayer libmpcodecs/vf_cropdetect.c.
24 */
25
26 #include <stdio.h>
27
28 #include "libavutil/imgutils.h"
29 #include "libavutil/internal.h"
30 #include "avfilter.h"
31 #include "formats.h"
32 #include "internal.h"
33 #include "video.h"
34
35 typedef struct {
36 int x1, y1, x2, y2;
37 int limit;
38 int round;
39 int reset_count;
40 int frame_nb;
41 int max_pixsteps[4];
42 } CropDetectContext;
43
44 static int query_formats(AVFilterContext *ctx)
45 {
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,
52 AV_PIX_FMT_NONE
53 };
54
55 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
56 return 0;
57 }
58
59 static int checkline(void *ctx, const unsigned char *src, int stride, int len, int bpp)
60 {
61 int total = 0;
62 int div = len;
63
64 switch (bpp) {
65 case 1:
66 while (--len >= 0) {
67 total += src[0];
68 src += stride;
69 }
70 break;
71 case 3:
72 case 4:
73 while (--len >= 0) {
74 total += src[0] + src[1] + src[2];
75 src += stride;
76 }
77 div *= 3;
78 break;
79 }
80 total /= div;
81
82 av_log(ctx, AV_LOG_DEBUG, "total:%d\n", total);
83 return total;
84 }
85
86 static av_cold int init(AVFilterContext *ctx, const char *args)
87 {
88 CropDetectContext *cd = ctx->priv;
89
90 cd->limit = 24;
91 cd->round = 0;
92 cd->reset_count = 0;
93 cd->frame_nb = -2;
94
95 if (args)
96 sscanf(args, "%d:%d:%d", &cd->limit, &cd->round, &cd->reset_count);
97
98 av_log(ctx, AV_LOG_VERBOSE, "limit:%d round:%d reset_count:%d\n",
99 cd->limit, cd->round, cd->reset_count);
100
101 return 0;
102 }
103
104 static int config_input(AVFilterLink *inlink)
105 {
106 AVFilterContext *ctx = inlink->dst;
107 CropDetectContext *cd = ctx->priv;
108
109 av_image_fill_max_pixsteps(cd->max_pixsteps, NULL,
110 &av_pix_fmt_descriptors[inlink->format]);
111
112 cd->x1 = inlink->w - 1;
113 cd->y1 = inlink->h - 1;
114 cd->x2 = 0;
115 cd->y2 = 0;
116
117 return 0;
118 }
119
120 static int end_frame(AVFilterLink *inlink)
121 {
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;
127
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;
134 cd->x2 = 0;
135 cd->y2 = 0;
136 cd->frame_nb = 1;
137 }
138
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) {
141 cd->y1 = y;
142 break;
143 }
144 }
145
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) {
148 cd->y2 = y;
149 break;
150 }
151 }
152
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) {
155 cd->x1 = y;
156 break;
157 }
158 }
159
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) {
162 cd->x2 = y;
163 break;
164 }
165 }
166
167 // round x and y (up), important for yuv colorspaces
168 // make sure they stay rounded!
169 x = (cd->x1+1) & ~1;
170 y = (cd->y1+1) & ~1;
171
172 w = cd->x2 - x + 1;
173 h = cd->y2 - y + 1;
174
175 // w and h must be divisible by 2 as well because of yuv
176 // colorspace problems.
177 if (cd->round <= 1)
178 cd->round = 16;
179 if (cd->round % 2)
180 cd->round *= 2;
181
182 shrink_by = w % cd->round;
183 w -= shrink_by;
184 x += (shrink_by/2 + 1) & ~1;
185
186 shrink_by = h % cd->round;
187 h -= shrink_by;
188 y += (shrink_by/2 + 1) & ~1;
189
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),
194 w, h, x, y);
195 }
196
197 return ff_end_frame(inlink->dst->outputs[0]);
198 }
199
200 AVFilter avfilter_vf_cropdetect = {
201 .name = "cropdetect",
202 .description = NULL_IF_CONFIG_SMALL("Auto-detect crop size."),
203
204 .priv_size = sizeof(CropDetectContext),
205 .init = init,
206
207 .query_formats = query_formats,
208
209 .inputs = (const AVFilterPad[]) {{ .name = "default",
210 .type = AVMEDIA_TYPE_VIDEO,
211 .config_props = config_input,
212 .get_video_buffer = ff_null_get_video_buffer,
213 .start_frame = ff_null_start_frame,
214 .end_frame = end_frame, },
215 { .name = NULL}},
216
217 .outputs = (const AVFilterPad[]) {{ .name = "default",
218 .type = AVMEDIA_TYPE_VIDEO },
219 { .name = NULL}},
220 };