../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
172 / 217
79.3%
Branches:
264 / 320
82.5%
Functions:
7 / 8
87.5%

libavfilter/vf_cropdetect.c
Line Branch Exec Source
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
26 #include "libavutil/imgutils.h"
27 #include "libavutil/internal.h"
28 #include "libavutil/mem.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/motion_vector.h"
31 #include "libavutil/qsort.h"
32
33 #include "avfilter.h"
34 #include "filters.h"
35 #include "video.h"
36 #include "edge_common.h"
37
38 typedef struct CropDetectContext {
39 const AVClass *class;
40 int x1, y1, x2, y2;
41 float limit;
42 float limit_upscaled;
43 int round;
44 int skip;
45 int reset_count;
46 int frame_nb;
47 int max_pixsteps[4];
48 int max_outliers;
49 int mode;
50 int window_size;
51 int mv_threshold;
52 int bitdepth;
53 float low, high;
54 uint8_t low_u8, high_u8;
55 uint8_t *filterbuf;
56 uint8_t *tmpbuf;
57 uint16_t *gradients;
58 char *directions;
59 int *bboxes[4];
60 } CropDetectContext;
61
62 static const enum AVPixelFormat pix_fmts[] = {
63 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
64 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P,
65 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
66 AV_PIX_FMT_YUV411P, AV_PIX_FMT_GRAY8,
67 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV410P,
68 AV_PIX_FMT_YUV420P9 , AV_PIX_FMT_YUV422P9 , AV_PIX_FMT_YUV444P9,
69 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
70 AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
71 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
72 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
73 AV_PIX_FMT_NV12, AV_PIX_FMT_NV21,
74 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
75 AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
76 AV_PIX_FMT_NONE
77 };
78
79 enum CropMode {
80 MODE_BLACK,
81 MODE_MV_EDGES,
82 MODE_NB
83 };
84
85 517 static int comp(const int *a,const int *b)
86 {
87 517 return FFDIFFSIGN(*a, *b);
88 }
89
90 3676 static int checkline(void *ctx, const unsigned char *src, int stride, int len, int bpp)
91 {
92 3676 int total = 0;
93 3676 int div = len;
94 3676 const uint16_t *src16 = (const uint16_t *)src;
95
96
1/4
✓ Branch 2 → 3 taken 3676 times.
✗ Branch 2 → 10 not taken.
✗ Branch 2 → 17 not taken.
✗ Branch 2 → 24 not taken.
3676 switch (bpp) {
97 3676 case 1:
98
2/2
✓ Branch 5 → 4 taken 330600 times.
✓ Branch 5 → 6 taken 3676 times.
334276 while (len >= 8) {
99 330600 total += src[ 0] + src[ stride] + src[2*stride] + src[3*stride]
100 330600 + src[4*stride] + src[5*stride] + src[6*stride] + src[7*stride];
101 330600 src += 8*stride;
102 330600 len -= 8;
103 }
104
2/2
✓ Branch 8 → 7 taken 48 times.
✓ Branch 8 → 9 taken 3676 times.
3724 while (--len >= 0) {
105 48 total += src[0];
106 48 src += stride;
107 }
108 3676 break;
109 ✗ case 2:
110 ✗ stride >>= 1;
111
0/2
✗ Branch 12 → 11 not taken.
✗ Branch 12 → 13 not taken.
✗ while (len >= 8) {
112 ✗ total += src16[ 0] + src16[ stride] + src16[2*stride] + src16[3*stride]
113 ✗ + src16[4*stride] + src16[5*stride] + src16[6*stride] + src16[7*stride];
114 ✗ src16 += 8*stride;
115 ✗ len -= 8;
116 }
117
0/2
✗ Branch 15 → 14 not taken.
✗ Branch 15 → 16 not taken.
✗ while (--len >= 0) {
118 ✗ total += src16[0];
119 ✗ src16 += stride;
120 }
121 ✗ break;
122 ✗ case 3:
123 case 4:
124
0/2
✗ Branch 19 → 18 not taken.
✗ Branch 19 → 20 not taken.
✗ while (len >= 4) {
125 ✗ total += src[0] + src[1 ] + src[2 ]
126 ✗ + src[ stride] + src[1+ stride] + src[2+ stride]
127 ✗ + src[2*stride] + src[1+2*stride] + src[2+2*stride]
128 ✗ + src[3*stride] + src[1+3*stride] + src[2+3*stride];
129 ✗ src += 4*stride;
130 ✗ len -= 4;
131 }
132
0/2
✗ Branch 22 → 21 not taken.
✗ Branch 22 → 23 not taken.
✗ while (--len >= 0) {
133 ✗ total += src[0] + src[1] + src[2];
134 ✗ src += stride;
135 }
136 ✗ div *= 3;
137 ✗ break;
138 }
139 3676 total /= div;
140
141 3676 av_log(ctx, AV_LOG_DEBUG, "total:%d\n", total);
142 3676 return total;
143 }
144
145 2787 static int checkline_edge(void *ctx, const unsigned char *src, int stride, int len)
146 {
147
2/2
✓ Branch 6 → 3 taken 416449 times.
✓ Branch 6 → 7 taken 113 times.
416562 while (--len >= 0) {
148
2/2
✓ Branch 3 → 4 taken 2674 times.
✓ Branch 3 → 5 taken 413775 times.
416449 if (src[0]) return 0;
149 413775 src += stride;
150 }
151
152 113 return 1;
153 }
154
155 3 static av_cold int init(AVFilterContext *ctx)
156 {
157 3 CropDetectContext *s = ctx->priv;
158
159 3 s->frame_nb = -1 * s->skip;
160 3 s->low_u8 = s->low * 255. + .5;
161 3 s->high_u8 = s->high * 255. + .5;
162
163 3 av_log(ctx, AV_LOG_VERBOSE, "limit:%f round:%d skip:%d reset_count:%d\n",
164 3 s->limit, s->round, s->skip, s->reset_count);
165
166 3 return 0;
167 }
168
169 3 static av_cold void uninit(AVFilterContext *ctx)
170 {
171 3 CropDetectContext *s = ctx->priv;
172
173 3 av_freep(&s->tmpbuf);
174 3 av_freep(&s->filterbuf);
175 3 av_freep(&s->gradients);
176 3 av_freep(&s->directions);
177 3 av_freep(&s->bboxes[0]);
178 3 av_freep(&s->bboxes[1]);
179 3 av_freep(&s->bboxes[2]);
180 3 av_freep(&s->bboxes[3]);
181 3 }
182
183 3 static int config_input(AVFilterLink *inlink)
184 {
185 3 AVFilterContext *ctx = inlink->dst;
186 3 CropDetectContext *s = ctx->priv;
187 3 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
188 3 const int bufsize = inlink->w * inlink->h;
189
190 3 av_image_fill_max_pixsteps(s->max_pixsteps, NULL, desc);
191
192 3 s->bitdepth = desc->comp[0].depth;
193
194
1/2
✓ Branch 4 → 5 taken 3 times.
✗ Branch 4 → 6 not taken.
3 if (s->limit < 1.0)
195 3 s->limit_upscaled = s->limit * ((1 << s->bitdepth) - 1);
196 else
197 ✗ s->limit_upscaled = s->limit;
198
199 3 s->x1 = inlink->w - 1;
200 3 s->y1 = inlink->h - 1;
201 3 s->x2 = 0;
202 3 s->y2 = 0;
203
204 3 s->window_size = FFMAX(s->reset_count, 15);
205 3 s->tmpbuf = av_malloc(bufsize);
206 3 s->filterbuf = av_malloc(bufsize * s->max_pixsteps[0]);
207 3 s->gradients = av_calloc(bufsize, sizeof(*s->gradients));
208 3 s->directions = av_malloc(bufsize);
209 3 s->bboxes[0] = av_malloc(s->window_size * sizeof(*s->bboxes[0]));
210 3 s->bboxes[1] = av_malloc(s->window_size * sizeof(*s->bboxes[1]));
211 3 s->bboxes[2] = av_malloc(s->window_size * sizeof(*s->bboxes[2]));
212 3 s->bboxes[3] = av_malloc(s->window_size * sizeof(*s->bboxes[3]));
213
214
4/8
✓ Branch 15 → 16 taken 3 times.
✗ Branch 15 → 23 not taken.
✓ Branch 16 → 17 taken 3 times.
✗ Branch 16 → 23 not taken.
✓ Branch 17 → 18 taken 3 times.
✗ Branch 17 → 23 not taken.
✓ Branch 18 → 19 taken 3 times.
✗ Branch 18 → 23 not taken.
3 if (!s->tmpbuf || !s->filterbuf || !s->gradients || !s->directions ||
215
4/8
✓ Branch 19 → 20 taken 3 times.
✗ Branch 19 → 23 not taken.
✓ Branch 20 → 21 taken 3 times.
✗ Branch 20 → 23 not taken.
✓ Branch 21 → 22 taken 3 times.
✗ Branch 21 → 23 not taken.
✗ Branch 22 → 23 not taken.
✓ Branch 22 → 24 taken 3 times.
3 !s->bboxes[0] || !s->bboxes[1] || !s->bboxes[2] || !s->bboxes[3])
216 ✗ return AVERROR(ENOMEM);
217
218 3 return 0;
219 }
220
221 #define SET_META(key, value) \
222 av_dict_set_int(metadata, key, value, 0)
223
224 52 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
225 {
226 52 AVFilterContext *ctx = inlink->dst;
227 52 CropDetectContext *s = ctx->priv;
228 52 int bpp = s->max_pixsteps[0];
229 int w, h, x, y, shrink_by, i;
230 AVDictionary **metadata;
231 int outliers, last_y;
232 52 int limit_upscaled = lrint(s->limit_upscaled);
233 char limit_str[22];
234
235 52 const int inw = inlink->w;
236 52 const int inh = inlink->h;
237 52 uint8_t *tmpbuf = s->tmpbuf;
238 52 uint8_t *filterbuf = s->filterbuf;
239 52 uint16_t *gradients = s->gradients;
240 52 int8_t *directions = s->directions;
241 52 const AVFrameSideData *sd = NULL;
242 int scan_w, scan_h, bboff;
243
244 52 void (*sobel)(int w, int h, uint16_t *dst, int dst_linesize,
245 int8_t *dir, int dir_linesize,
246
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 52 times.
52 const uint8_t *src, int src_linesize, int src_stride) = (bpp == 2) ? &ff_sobel_16 : &ff_sobel_8;
247 52 void (*gaussian_blur)(int w, int h,
248 uint8_t *dst, int dst_linesize,
249
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 52 times.
52 const uint8_t *src, int src_linesize, int src_stride) = (bpp == 2) ? &ff_gaussian_blur_16 : &ff_gaussian_blur_8;
250
251
252 // ignore first s->skip frames
253
2/2
✓ Branch 8 → 9 taken 46 times.
✓ Branch 8 → 345 taken 6 times.
52 if (++s->frame_nb > 0) {
254 46 metadata = &frame->metadata;
255
256 // Reset the crop area every reset_count frames, if reset_count is > 0
257
1/4
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 12 taken 46 times.
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 12 not taken.
46 if (s->reset_count > 0 && s->frame_nb > s->reset_count) {
258 ✗ s->x1 = frame->width - 1;
259 ✗ s->y1 = frame->height - 1;
260 ✗ s->x2 = 0;
261 ✗ s->y2 = 0;
262 ✗ s->frame_nb = 1;
263 }
264
265 #define FIND(DST, FROM, NOEND, INC, STEP0, STEP1, LEN) \
266 outliers = 0;\
267 for (last_y = y = FROM; NOEND; y = y INC) {\
268 if (checkline(ctx, frame->data[0] + STEP0 * y, STEP1, LEN, bpp) > limit_upscaled) {\
269 if (++outliers > s->max_outliers) { \
270 DST = last_y;\
271 break;\
272 }\
273 } else\
274 last_y = y INC;\
275 }
276
277
2/2
✓ Branch 12 → 13 taken 30 times.
✓ Branch 12 → 46 taken 16 times.
46 if (s->mode == MODE_BLACK) {
278
6/6
✓ Branch 15 → 16 taken 62 times.
✓ Branch 15 → 18 taken 1772 times.
✓ Branch 16 → 17 taken 1 time.
✓ Branch 16 → 19 taken 61 times.
✓ Branch 20 → 14 taken 1834 times.
✓ Branch 20 → 21 taken 29 times.
1863 FIND(s->y1, 0, y < s->y1, +1, frame->linesize[0], bpp, frame->width);
279
6/6
✓ Branch 23 → 24 taken 4 times.
✓ Branch 23 → 26 taken 1830 times.
✓ Branch 24 → 25 taken 1 time.
✓ Branch 24 → 27 taken 3 times.
✓ Branch 28 → 22 taken 1834 times.
✓ Branch 28 → 29 taken 29 times.
1863 FIND(s->y2, frame->height - 1, y > FFMAX(s->y2, s->y1), -1, frame->linesize[0], bpp, frame->width);
280
5/6
✓ Branch 31 → 32 taken 4 times.
✗ Branch 31 → 34 not taken.
✓ Branch 32 → 33 taken 1 time.
✓ Branch 32 → 35 taken 3 times.
✓ Branch 36 → 30 taken 4 times.
✓ Branch 36 → 37 taken 29 times.
33 FIND(s->x1, 0, y < s->x1, +1, bpp, frame->linesize[0], frame->height);
281
5/6
✓ Branch 39 → 40 taken 4 times.
✗ Branch 39 → 42 not taken.
✓ Branch 40 → 41 taken 1 time.
✓ Branch 40 → 43 taken 3 times.
✓ Branch 44 → 38 taken 4 times.
✓ Branch 44 → 45 taken 29 times.
33 FIND(s->x2, frame->width - 1, y > FFMAX(s->x2, s->x1), -1, bpp, frame->linesize[0], frame->height);
282 } else { // MODE_MV_EDGES
283 16 sd = av_frame_get_side_data(frame, AV_FRAME_DATA_MOTION_VECTORS);
284 16 s->x1 = 0;
285 16 s->y1 = 0;
286 16 s->x2 = inw - 1;
287 16 s->y2 = inh - 1;
288
289
1/2
✗ Branch 47 → 48 not taken.
✓ Branch 47 → 49 taken 16 times.
16 if (!sd) {
290 ✗ av_log(ctx, AV_LOG_WARNING, "Cannot detect: no motion vectors available");
291 } else {
292 // gaussian filter to reduce noise
293 16 gaussian_blur(inw, inh,
294 filterbuf, inw*bpp,
295 16 frame->data[0], frame->linesize[0], bpp);
296
297 // compute the 16-bits gradients and directions for the next step
298 16 sobel(inw, inh, gradients, inw, directions, inw, filterbuf, inw*bpp, bpp);
299
300 // non_maximum_suppression() will actually keep & clip what's necessary and
301 // ignore the rest, so we need a clean output buffer
302 16 memset(tmpbuf, 0, inw * inh);
303 16 ff_non_maximum_suppression(inw, inh, tmpbuf, inw, directions, inw, gradients, inw);
304
305
306 // keep high values, or low values surrounded by high values
307 16 ff_double_threshold(s->low_u8, s->high_u8, inw, inh,
308 tmpbuf, inw, tmpbuf, inw);
309
310 // scan all MVs and store bounding box
311 16 s->x1 = inw - 1;
312 16 s->y1 = inh - 1;
313 16 s->x2 = 0;
314 16 s->y2 = 0;
315
2/2
✓ Branch 65 → 54 taken 191400 times.
✓ Branch 65 → 66 taken 16 times.
191416 for (i = 0; i < sd->size / sizeof(AVMotionVector); i++) {
316 191400 const AVMotionVector *mv = (const AVMotionVector*)sd->data + i;
317 191400 const int mx = mv->dst_x - mv->src_x;
318 191400 const int my = mv->dst_y - mv->src_y;
319
320
2/4
✓ Branch 54 → 55 taken 191400 times.
✗ Branch 54 → 64 not taken.
✓ Branch 55 → 56 taken 191400 times.
✗ Branch 55 → 64 not taken.
191400 if (mv->dst_x >= 0 && mv->dst_x < inw &&
321
2/4
✓ Branch 56 → 57 taken 191400 times.
✗ Branch 56 → 64 not taken.
✓ Branch 57 → 58 taken 191400 times.
✗ Branch 57 → 64 not taken.
191400 mv->dst_y >= 0 && mv->dst_y < inh &&
322
2/4
✓ Branch 58 → 59 taken 191400 times.
✗ Branch 58 → 64 not taken.
✓ Branch 59 → 60 taken 191400 times.
✗ Branch 59 → 64 not taken.
191400 mv->src_x >= 0 && mv->src_x < inw &&
323
2/4
✓ Branch 60 → 61 taken 191400 times.
✗ Branch 60 → 64 not taken.
✓ Branch 61 → 62 taken 191400 times.
✗ Branch 61 → 64 not taken.
191400 mv->src_y >= 0 && mv->src_y < inh &&
324
2/2
✓ Branch 62 → 63 taken 263 times.
✓ Branch 62 → 64 taken 191137 times.
191400 mx * mx + my * my >= s->mv_threshold * s->mv_threshold) {
325 263 s->x1 = mv->dst_x < s->x1 ? mv->dst_x : s->x1;
326 263 s->y1 = mv->dst_y < s->y1 ? mv->dst_y : s->y1;
327 263 s->x2 = mv->dst_x > s->x2 ? mv->dst_x : s->x2;
328 263 s->y2 = mv->dst_y > s->y2 ? mv->dst_y : s->y2;
329 }
330 }
331
332 // assert x1<x2, y1<y2
333
2/2
✓ Branch 66 → 67 taken 2 times.
✓ Branch 66 → 68 taken 14 times.
16 if (s->x1 > s->x2) FFSWAP(int, s->x1, s->x2);
334
2/2
✓ Branch 68 → 69 taken 2 times.
✓ Branch 68 → 70 taken 14 times.
16 if (s->y1 > s->y2) FFSWAP(int, s->y1, s->y2);
335
336 // scan outward looking for 0-edge-lines in edge image
337 16 scan_w = s->x2 - s->x1;
338 16 scan_h = s->y2 - s->y1;
339
340 #define FIND_EDGE(DST, FROM, NOEND, INC, BASE, STEP0, STEP1, LEN) \
341 for (last_y = y = FROM; NOEND; y = y INC) { \
342 if (checkline_edge(ctx, BASE + STEP0 * y, STEP1, LEN)) { \
343 if (last_y INC == y) { \
344 DST = y; \
345 break; \
346 } else \
347 last_y = y; \
348 } \
349 } \
350 if (!(NOEND)) { \
351 DST = y -(INC); \
352 }
353
354 16 const uint8_t *rows = tmpbuf + s->x1;
355 16 const uint8_t *cols = tmpbuf + s->y1 * inw;
356
8/8
✓ Branch 72 → 73 taken 31 times.
✓ Branch 72 → 76 taken 438 times.
✓ Branch 73 → 74 taken 14 times.
✓ Branch 73 → 75 taken 17 times.
✓ Branch 77 → 71 taken 469 times.
✓ Branch 77 → 78 taken 2 times.
✓ Branch 78 → 79 taken 2 times.
✓ Branch 78 → 80 taken 14 times.
471 FIND_EDGE(s->y1, s->y1, y >= 0, -1, rows, inw, 1, scan_w);
357
8/8
✓ Branch 82 → 83 taken 30 times.
✓ Branch 82 → 86 taken 433 times.
✓ Branch 83 → 84 taken 14 times.
✓ Branch 83 → 85 taken 16 times.
✓ Branch 87 → 81 taken 463 times.
✓ Branch 87 → 88 taken 2 times.
✓ Branch 88 → 89 taken 2 times.
✓ Branch 88 → 90 taken 14 times.
465 FIND_EDGE(s->y2, s->y2, y < inh, +1, rows, inw, 1, scan_w);
358
8/8
✓ Branch 92 → 93 taken 26 times.
✓ Branch 92 → 96 taken 1029 times.
✓ Branch 93 → 94 taken 9 times.
✓ Branch 93 → 95 taken 17 times.
✓ Branch 97 → 91 taken 1055 times.
✓ Branch 97 → 98 taken 7 times.
✓ Branch 98 → 99 taken 7 times.
✓ Branch 98 → 100 taken 9 times.
1062 FIND_EDGE(s->x1, s->x1, y >= 0, -1, cols, 1, inw, scan_h);
359
8/8
✓ Branch 102 → 103 taken 26 times.
✓ Branch 102 → 106 taken 774 times.
✓ Branch 103 → 104 taken 10 times.
✓ Branch 103 → 105 taken 16 times.
✓ Branch 107 → 101 taken 800 times.
✓ Branch 107 → 108 taken 6 times.
✓ Branch 108 → 109 taken 6 times.
✓ Branch 108 → 110 taken 10 times.
806 FIND_EDGE(s->x2, s->x2, y < inw, +1, cols, 1, inw, scan_h);
360
361 // queue bboxes
362 16 bboff = (s->frame_nb - 1) % s->window_size;
363 16 s->bboxes[0][bboff] = s->x1;
364 16 s->bboxes[1][bboff] = s->x2;
365 16 s->bboxes[2][bboff] = s->y1;
366 16 s->bboxes[3][bboff] = s->y2;
367
368 // sort queue
369 16 bboff = FFMIN(s->frame_nb, s->window_size);
370
39/44
✓ Branch 112 → 113 taken 16 times.
✓ Branch 112 → 156 taken 6 times.
✓ Branch 114 → 115 taken 1 time.
✓ Branch 114 → 119 taken 15 times.
✗ Branch 116 → 117 not taken.
✓ Branch 116 → 118 taken 1 time.
✗ Branch 120 → 121 not taken.
✓ Branch 120 → 122 taken 15 times.
✓ Branch 124 → 125 taken 2 times.
✓ Branch 124 → 126 taken 14 times.
✓ Branch 126 → 127 taken 5 times.
✓ Branch 126 → 128 taken 11 times.
✓ Branch 131 → 132 taken 18 times.
✗ Branch 131 → 134 not taken.
✗ Branch 133 → 130 not taken.
✓ Branch 133 → 134 taken 18 times.
✓ Branch 136 → 137 taken 22 times.
✓ Branch 136 → 139 taken 2 times.
✓ Branch 138 → 135 taken 6 times.
✓ Branch 138 → 139 taken 16 times.
✓ Branch 139 → 140 taken 16 times.
✓ Branch 139 → 142 taken 2 times.
✓ Branch 142 → 129 taken 18 times.
✓ Branch 142 → 143 taken 11 times.
✓ Branch 143 → 144 taken 9 times.
✓ Branch 143 → 153 taken 2 times.
✓ Branch 144 → 145 taken 6 times.
✓ Branch 144 → 146 taken 3 times.
✓ Branch 145 → 146 taken 6 times.
✗ Branch 145 → 153 not taken.
✓ Branch 148 → 149 taken 43 times.
✓ Branch 148 → 151 taken 6 times.
✓ Branch 150 → 147 taken 40 times.
✓ Branch 150 → 151 taken 3 times.
✓ Branch 151 → 152 taken 6 times.
✓ Branch 151 → 153 taken 3 times.
✓ Branch 153 → 154 taken 1 time.
✓ Branch 153 → 155 taken 4 times.
✓ Branch 157 → 158 taken 2 times.
✓ Branch 157 → 159 taken 4 times.
✓ Branch 161 → 112 taken 22 times.
✓ Branch 161 → 162 taken 4 times.
✓ Branch 163 → 111 taken 21 times.
✓ Branch 163 → 164 taken 16 times.
117 AV_QSORT(s->bboxes[0], bboff, int, comp);
371
44/44
✓ Branch 166 → 167 taken 18 times.
✓ Branch 166 → 210 taken 10 times.
✓ Branch 168 → 169 taken 5 times.
✓ Branch 168 → 173 taken 13 times.
✓ Branch 170 → 171 taken 1 time.
✓ Branch 170 → 172 taken 4 times.
✓ Branch 174 → 175 taken 1 time.
✓ Branch 174 → 176 taken 12 times.
✓ Branch 178 → 179 taken 7 times.
✓ Branch 178 → 180 taken 11 times.
✓ Branch 180 → 181 taken 4 times.
✓ Branch 180 → 182 taken 14 times.
✓ Branch 185 → 186 taken 21 times.
✓ Branch 185 → 188 taken 2 times.
✓ Branch 187 → 184 taken 8 times.
✓ Branch 187 → 188 taken 13 times.
✓ Branch 190 → 191 taken 23 times.
✓ Branch 190 → 193 taken 6 times.
✓ Branch 192 → 189 taken 14 times.
✓ Branch 192 → 193 taken 9 times.
✓ Branch 193 → 194 taken 9 times.
✓ Branch 193 → 196 taken 6 times.
✓ Branch 196 → 183 taken 15 times.
✓ Branch 196 → 197 taken 14 times.
✓ Branch 197 → 198 taken 8 times.
✓ Branch 197 → 207 taken 6 times.
✓ Branch 198 → 199 taken 5 times.
✓ Branch 198 → 200 taken 3 times.
✓ Branch 199 → 200 taken 2 times.
✓ Branch 199 → 207 taken 3 times.
✓ Branch 202 → 203 taken 20 times.
✓ Branch 202 → 205 taken 4 times.
✓ Branch 204 → 201 taken 19 times.
✓ Branch 204 → 205 taken 1 time.
✓ Branch 205 → 206 taken 4 times.
✓ Branch 205 → 207 taken 1 time.
✓ Branch 207 → 208 taken 3 times.
✓ Branch 207 → 209 taken 7 times.
✓ Branch 211 → 212 taken 3 times.
✓ Branch 211 → 213 taken 7 times.
✓ Branch 215 → 166 taken 28 times.
✓ Branch 215 → 216 taken 8 times.
✓ Branch 217 → 165 taken 26 times.
✓ Branch 217 → 218 taken 16 times.
122 AV_QSORT(s->bboxes[1], bboff, int, comp);
372
43/44
✓ Branch 220 → 221 taken 18 times.
✓ Branch 220 → 264 taken 10 times.
✓ Branch 222 → 223 taken 5 times.
✓ Branch 222 → 227 taken 13 times.
✗ Branch 224 → 225 not taken.
✓ Branch 224 → 226 taken 5 times.
✓ Branch 228 → 229 taken 1 time.
✓ Branch 228 → 230 taken 12 times.
✓ Branch 232 → 233 taken 6 times.
✓ Branch 232 → 234 taken 12 times.
✓ Branch 234 → 235 taken 4 times.
✓ Branch 234 → 236 taken 14 times.
✓ Branch 239 → 240 taken 17 times.
✓ Branch 239 → 242 taken 3 times.
✓ Branch 241 → 238 taken 6 times.
✓ Branch 241 → 242 taken 11 times.
✓ Branch 244 → 245 taken 26 times.
✓ Branch 244 → 247 taken 7 times.
✓ Branch 246 → 243 taken 19 times.
✓ Branch 246 → 247 taken 7 times.
✓ Branch 247 → 248 taken 7 times.
✓ Branch 247 → 250 taken 7 times.
✓ Branch 250 → 237 taken 14 times.
✓ Branch 250 → 251 taken 14 times.
✓ Branch 251 → 252 taken 7 times.
✓ Branch 251 → 261 taken 7 times.
✓ Branch 252 → 253 taken 6 times.
✓ Branch 252 → 254 taken 1 time.
✓ Branch 253 → 254 taken 5 times.
✓ Branch 253 → 261 taken 1 time.
✓ Branch 256 → 257 taken 21 times.
✓ Branch 256 → 259 taken 4 times.
✓ Branch 258 → 255 taken 19 times.
✓ Branch 258 → 259 taken 2 times.
✓ Branch 259 → 260 taken 4 times.
✓ Branch 259 → 261 taken 2 times.
✓ Branch 261 → 262 taken 3 times.
✓ Branch 261 → 263 taken 7 times.
✓ Branch 265 → 266 taken 5 times.
✓ Branch 265 → 267 taken 5 times.
✓ Branch 269 → 220 taken 28 times.
✓ Branch 269 → 270 taken 8 times.
✓ Branch 271 → 219 taken 26 times.
✓ Branch 271 → 272 taken 16 times.
124 AV_QSORT(s->bboxes[2], bboff, int, comp);
373
39/44
✓ Branch 274 → 275 taken 16 times.
✓ Branch 274 → 318 taken 5 times.
✓ Branch 276 → 277 taken 3 times.
✓ Branch 276 → 281 taken 13 times.
✗ Branch 278 → 279 not taken.
✓ Branch 278 → 280 taken 3 times.
✓ Branch 282 → 283 taken 1 time.
✓ Branch 282 → 284 taken 12 times.
✓ Branch 286 → 287 taken 5 times.
✓ Branch 286 → 288 taken 11 times.
✓ Branch 288 → 289 taken 4 times.
✓ Branch 288 → 290 taken 12 times.
✓ Branch 293 → 294 taken 21 times.
✗ Branch 293 → 296 not taken.
✓ Branch 295 → 292 taken 8 times.
✓ Branch 295 → 296 taken 13 times.
✓ Branch 298 → 299 taken 22 times.
✓ Branch 298 → 301 taken 5 times.
✓ Branch 300 → 297 taken 14 times.
✓ Branch 300 → 301 taken 8 times.
✓ Branch 301 → 302 taken 8 times.
✓ Branch 301 → 304 taken 5 times.
✓ Branch 304 → 291 taken 13 times.
✓ Branch 304 → 305 taken 12 times.
✓ Branch 305 → 306 taken 7 times.
✓ Branch 305 → 315 taken 5 times.
✓ Branch 306 → 307 taken 3 times.
✓ Branch 306 → 308 taken 4 times.
✓ Branch 307 → 308 taken 2 times.
✓ Branch 307 → 315 taken 1 time.
✓ Branch 310 → 311 taken 28 times.
✓ Branch 310 → 313 taken 6 times.
✓ Branch 312 → 309 taken 28 times.
✗ Branch 312 → 313 not taken.
✓ Branch 313 → 314 taken 6 times.
✗ Branch 313 → 315 not taken.
✓ Branch 315 → 316 taken 1 time.
✓ Branch 315 → 317 taken 5 times.
✗ Branch 319 → 320 not taken.
✓ Branch 319 → 321 taken 5 times.
✓ Branch 323 → 274 taken 21 times.
✓ Branch 323 → 324 taken 7 times.
✓ Branch 325 → 273 taken 22 times.
✓ Branch 325 → 326 taken 16 times.
119 AV_QSORT(s->bboxes[3], bboff, int, comp);
374
375 // return median of window_size elems
376 16 s->x1 = s->bboxes[0][bboff/2];
377 16 s->x2 = s->bboxes[1][bboff/2];
378 16 s->y1 = s->bboxes[2][bboff/2];
379 16 s->y2 = s->bboxes[3][bboff/2];
380 }
381 }
382
383 // round x and y (up), important for yuv colorspaces
384 // make sure they stay rounded!
385 46 x = (s->x1+1) & ~1;
386 46 y = (s->y1+1) & ~1;
387
388 46 w = s->x2 - x + 1;
389 46 h = s->y2 - y + 1;
390
391 // w and h must be divisible by 2 as well because of yuv
392 // colorspace problems.
393
1/2
✗ Branch 327 → 328 not taken.
✓ Branch 327 → 329 taken 46 times.
46 if (s->round <= 1)
394 ✗ s->round = 16;
395
1/2
✗ Branch 329 → 330 not taken.
✓ Branch 329 → 331 taken 46 times.
46 if (s->round % 2)
396 ✗ s->round *= 2;
397
398 46 shrink_by = w % s->round;
399 46 w -= shrink_by;
400 46 x += (shrink_by/2 + 1) & ~1;
401
402 46 shrink_by = h % s->round;
403 46 h -= shrink_by;
404 46 y += (shrink_by/2 + 1) & ~1;
405
406 46 SET_META("lavfi.cropdetect.x1", s->x1);
407 46 SET_META("lavfi.cropdetect.x2", s->x2);
408 46 SET_META("lavfi.cropdetect.y1", s->y1);
409 46 SET_META("lavfi.cropdetect.y2", s->y2);
410 46 SET_META("lavfi.cropdetect.w", w);
411 46 SET_META("lavfi.cropdetect.h", h);
412 46 SET_META("lavfi.cropdetect.x", x);
413 46 SET_META("lavfi.cropdetect.y", y);
414
415 46 snprintf(limit_str, sizeof(limit_str), "%f", s->limit);
416 46 av_dict_set(metadata, "lavfi.cropdetect.limit", limit_str, 0);
417
418 46 av_log(ctx, AV_LOG_INFO,
419 "x1:%d x2:%d y1:%d y2:%d w:%d h:%d x:%d y:%d pts:%"PRId64" t:%f limit:%f crop=%d:%d:%d:%d\n",
420 s->x1, s->x2, s->y1, s->y2, w, h, x, y, frame->pts,
421 46 frame->pts == AV_NOPTS_VALUE ? -1 : frame->pts * av_q2d(inlink->time_base),
422
1/2
✓ Branch 340 → 341 taken 46 times.
✗ Branch 340 → 343 not taken.
46 s->limit, w, h, x, y);
423 }
424
425 52 return ff_filter_frame(inlink->dst->outputs[0], frame);
426 }
427
428 ✗ static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
429 char *res, int res_len, int flags)
430 {
431 ✗ CropDetectContext *s = ctx->priv;
432 ✗ float old_limit = s->limit;
433 int ret;
434
435
0/2
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 5 not taken.
✗ if ((ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags)) < 0)
436 ✗ return ret;
437
438
0/2
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 10 not taken.
✗ if (old_limit != s->limit) {
439
0/2
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
✗ if (s->limit < 1.0)
440 ✗ s->limit_upscaled = s->limit * ((1 << s->bitdepth) - 1);
441 else
442 ✗ s->limit_upscaled = s->limit;
443 ✗ s->frame_nb = s->reset_count;
444 }
445
446 ✗ return 0;
447 }
448
449 #define OFFSET(x) offsetof(CropDetectContext, x)
450 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
451 #define TFLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_RUNTIME_PARAM
452
453 static const AVOption cropdetect_options[] = {
454 { "limit", "Threshold below which the pixel is considered black", OFFSET(limit), AV_OPT_TYPE_FLOAT, { .dbl = 24.0/255 }, 0, 65535, TFLAGS },
455 { "round", "Value by which the width/height should be divisible", OFFSET(round), AV_OPT_TYPE_INT, { .i64 = 16 }, 0, INT_MAX, FLAGS },
456 { "reset", "Recalculate the crop area after this many frames", OFFSET(reset_count), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
457 { "skip", "Number of initial frames to skip", OFFSET(skip), AV_OPT_TYPE_INT, { .i64 = 2 }, 0, INT_MAX, FLAGS },
458 { "reset_count", "Recalculate the crop area after this many frames",OFFSET(reset_count),AV_OPT_TYPE_INT,{ .i64 = 0 }, 0, INT_MAX, FLAGS },
459 { "max_outliers", "Threshold count of outliers", OFFSET(max_outliers),AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
460 { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_BLACK}, 0, MODE_NB-1, FLAGS, .unit = "mode" },
461 { "black", "detect black pixels surrounding the video", 0, AV_OPT_TYPE_CONST, {.i64=MODE_BLACK}, INT_MIN, INT_MAX, FLAGS, .unit = "mode" },
462 { "mvedges", "detect motion and edged surrounding the video", 0, AV_OPT_TYPE_CONST, {.i64=MODE_MV_EDGES}, INT_MIN, INT_MAX, FLAGS, .unit = "mode" },
463 { "high", "Set high threshold for edge detection", OFFSET(high), AV_OPT_TYPE_FLOAT, {.dbl=25/255.}, 0, 1, FLAGS },
464 { "low", "Set low threshold for edge detection", OFFSET(low), AV_OPT_TYPE_FLOAT, {.dbl=15/255.}, 0, 1, FLAGS },
465 { "mv_threshold", "motion vector threshold when estimating video window size", OFFSET(mv_threshold), AV_OPT_TYPE_INT, {.i64=8}, 0, 100, FLAGS},
466 { NULL }
467 };
468
469 AVFILTER_DEFINE_CLASS(cropdetect);
470
471 static const AVFilterPad avfilter_vf_cropdetect_inputs[] = {
472 {
473 .name = "default",
474 .type = AVMEDIA_TYPE_VIDEO,
475 .config_props = config_input,
476 .filter_frame = filter_frame,
477 },
478 };
479
480 const FFFilter ff_vf_cropdetect = {
481 .p.name = "cropdetect",
482 .p.description = NULL_IF_CONFIG_SMALL("Auto-detect crop size."),
483 .p.priv_class = &cropdetect_class,
484 .p.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_METADATA_ONLY,
485 .priv_size = sizeof(CropDetectContext),
486 .init = init,
487 .uninit = uninit,
488 FILTER_INPUTS(avfilter_vf_cropdetect_inputs),
489 FILTER_OUTPUTS(ff_video_default_filterpad),
490 FILTER_PIXFMTS_ARRAY(pix_fmts),
491 .process_command = process_command,
492 };
493