1 /*
2 * Copyright (c) 2002 A'rpi
3 * This file is part of FFmpeg.
4 *
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.
9 *
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.
14 *
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.
18 */
19
20 /**
21 * @file
22 * border detection filter
23 * Ported from MPlayer libmpcodecs/vf_cropdetect.c.
24 */
25
29
34
44
46 {
54 };
55
57 return 0;
58 }
59
61 {
62 int total = 0;
64
65 switch (bpp) {
66 case 1:
67 while (--len >= 0) {
68 total += src[0];
70 }
71 break;
72 case 3:
73 case 4:
74 while (--len >= 0) {
75 total += src[0] + src[1] + src[2];
77 }
78 div *= 3;
79 break;
80 }
81 total /= div;
82
84 return total;
85 }
86
88 {
90
92
95
96 return 0;
97 }
98
100 {
103
106
107 s->
x1 = inlink->
w - 1;
108 s->
y1 = inlink->
h - 1;
111
112 return 0;
113 }
114
115 #define SET_META(key, value) \
116 snprintf(buf, sizeof(buf), "%d", value); \
117 av_dict_set(metadata, key, buf, 0)
118
120 {
124 int w, h, x,
y, shrink_by;
127
128 // ignore first 2 frames - they may be empty
131
132 // Reset the crop area every reset_count frames, if reset_count is > 0
139 }
140
141 for (y = 0; y < s->
y1; y++) {
144 break;
145 }
146 }
147
148 for (y = frame->
height - 1; y > s->
y2; y--) {
151 break;
152 }
153 }
154
155 for (y = 0; y < s->
x1; y++) {
158 break;
159 }
160 }
161
162 for (y = frame->
width - 1; y > s->
x2; y--) {
165 break;
166 }
167 }
168
169 // round x and y (up), important for yuv colorspaces
170 // make sure they stay rounded!
173
176
177 // w and h must be divisible by 2 as well because of yuv
178 // colorspace problems.
183
184 shrink_by = w % s->
round;
185 w -= shrink_by;
186 x += (shrink_by/2 + 1) & ~1;
187
188 shrink_by = h % s->
round;
189 h -= shrink_by;
190 y += (shrink_by/2 + 1) & ~1;
191
200
202 "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",
203 s->
x1, s->
x2, s->
y1, s->
y2, w, h, x, y, frame->
pts,
205 w, h, x, y);
206 }
207
209 }
210
211 #define OFFSET(x) offsetof(CropDetectContext, x)
212 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
213
215 {
"limit",
"Threshold below which the pixel is considered black",
OFFSET(limit),
AV_OPT_TYPE_INT, { .i64 = 24 }, 0, 255,
FLAGS },
217 {
"reset",
"Recalculate the crop area after this many frames",
OFFSET(reset_count),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX,
FLAGS },
218 {
"reset_count",
"Recalculate the crop area after this many frames",
OFFSET(reset_count),
AV_OPT_TYPE_INT,{ .i64 = 0 }, 0, INT_MAX,
FLAGS },
219 { NULL },
220 };
221
223
225 {
231 },
232 { NULL }
233 };
234
236 {
239 },
240 { NULL }
241 };
242
244 .
name =
"cropdetect",
246
248 .priv_class = &cropdetect_class,
251 .
inputs = avfilter_vf_cropdetect_inputs,
252 .
outputs = avfilter_vf_cropdetect_outputs,
254 };