FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_cropdetect.c
Date: 2022-11-26 13:19:19
Exec Total Coverage
Lines: 170 214 79.4%
Branches: 260 330 78.8%

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