FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/edge_common.c
Date: 2025-07-28 20:30:09
Exec Total Coverage
Lines: 44 44 100.0%
Functions: 3 3 100.0%
Branches: 72 73 98.6%

Line Branch Exec Source
1 /*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "edge_common.h"
20
21 // Internal helper for ff_sobel()
22 31647152 static int get_rounded_direction(int gx, int gy)
23 {
24 /* reference angles:
25 * tan( pi/8) = sqrt(2)-1
26 * tan(3pi/8) = sqrt(2)+1
27 * Gy/Gx is the tangent of the angle (theta), so Gy/Gx is compared against
28 * <ref-angle>, or more simply Gy against <ref-angle>*Gx
29 *
30 * Gx and Gy bounds = [-1020;1020], using 16-bit arithmetic:
31 * round((sqrt(2)-1) * (1<<16)) = 27146
32 * round((sqrt(2)+1) * (1<<16)) = 158218
33 */
34
2/2
✓ Branch 0 taken 18117222 times.
✓ Branch 1 taken 13529930 times.
31647152 if (gx) {
35 int tanpi8gx, tan3pi8gx;
36
37
2/2
✓ Branch 0 taken 7481451 times.
✓ Branch 1 taken 10635771 times.
18117222 if (gx < 0)
38 7481451 gx = -gx, gy = -gy;
39 18117222 gy *= (1 << 16);
40 18117222 tanpi8gx = 27146 * gx;
41 18117222 tan3pi8gx = 158218 * gx;
42
4/4
✓ Branch 0 taken 15609674 times.
✓ Branch 1 taken 2507548 times.
✓ Branch 2 taken 3048340 times.
✓ Branch 3 taken 12561334 times.
18117222 if (gy > -tan3pi8gx && gy < -tanpi8gx) return DIRECTION_45UP;
43
4/4
✓ Branch 0 taken 12561334 times.
✓ Branch 1 taken 2507548 times.
✓ Branch 2 taken 4476287 times.
✓ Branch 3 taken 8085047 times.
15068882 if (gy > -tanpi8gx && gy < tanpi8gx) return DIRECTION_HORIZONTAL;
44
4/4
✓ Branch 0 taken 8085047 times.
✓ Branch 1 taken 2507548 times.
✓ Branch 2 taken 5392120 times.
✓ Branch 3 taken 2692927 times.
10592595 if (gy > tanpi8gx && gy < tan3pi8gx) return DIRECTION_45DOWN;
45 }
46 18730405 return DIRECTION_VERTICAL;
47 }
48
49 #undef DEPTH
50 #define DEPTH 8
51 #include "edge_template.c"
52
53 #undef DEPTH
54 #define DEPTH 16
55 #include "edge_template.c"
56
57 // Filters rounded gradients to drop all non-maxima
58 // Expects gradients generated by ff_sobel()
59 // Expects zero's destination buffer
60 103 void ff_non_maximum_suppression(int w, int h,
61 uint8_t *dst, int dst_linesize,
62 const int8_t *dir, int dir_linesize,
63 const uint16_t *src, int src_linesize)
64 {
65 int i, j;
66
67 #define COPY_MAXIMA(ay, ax, by, bx) do { \
68 if (src[i] > src[(ay)*src_linesize + i+(ax)] && \
69 src[i] > src[(by)*src_linesize + i+(bx)]) \
70 dst[i] = av_clip_uint8(src[i]); \
71 } while (0)
72
73
2/2
✓ Branch 0 taken 47106 times.
✓ Branch 1 taken 103 times.
47209 for (j = 1; j < h - 1; j++) {
74 47106 dst += dst_linesize;
75 47106 dir += dir_linesize;
76 47106 src += src_linesize;
77
2/2
✓ Branch 0 taken 31647152 times.
✓ Branch 1 taken 47106 times.
31694258 for (i = 1; i < w - 1; i++) {
78
4/5
✓ Branch 0 taken 3048340 times.
✓ Branch 1 taken 5392120 times.
✓ Branch 2 taken 4476287 times.
✓ Branch 3 taken 18730405 times.
✗ Branch 4 not taken.
31647152 switch (dir[i]) {
79
4/4
✓ Branch 0 taken 1577295 times.
✓ Branch 1 taken 1471045 times.
✓ Branch 2 taken 756449 times.
✓ Branch 3 taken 820846 times.
3048340 case DIRECTION_45UP: COPY_MAXIMA( 1, -1, -1, 1); break;
80
4/4
✓ Branch 0 taken 2668696 times.
✓ Branch 1 taken 2723424 times.
✓ Branch 2 taken 1341905 times.
✓ Branch 3 taken 1326791 times.
5392120 case DIRECTION_45DOWN: COPY_MAXIMA(-1, -1, 1, 1); break;
81
4/4
✓ Branch 0 taken 2054874 times.
✓ Branch 1 taken 2421413 times.
✓ Branch 2 taken 653023 times.
✓ Branch 3 taken 1401851 times.
4476287 case DIRECTION_HORIZONTAL: COPY_MAXIMA( 0, -1, 0, 1); break;
82
4/4
✓ Branch 0 taken 3666214 times.
✓ Branch 1 taken 15064191 times.
✓ Branch 2 taken 1078151 times.
✓ Branch 3 taken 2588063 times.
18730405 case DIRECTION_VERTICAL: COPY_MAXIMA(-1, 0, 1, 0); break;
83 }
84 }
85 }
86 103 }
87
88 // Filter to keep all pixels > high, and keep all pixels > low where all surrounding pixels > high
89 103 void ff_double_threshold(int low, int high, int w, int h,
90 uint8_t *dst, int dst_linesize,
91 const uint8_t *src, int src_linesize)
92 {
93 int i, j;
94
95
2/2
✓ Branch 0 taken 47312 times.
✓ Branch 1 taken 103 times.
47415 for (j = 0; j < h; j++) {
96
2/2
✓ Branch 0 taken 31833824 times.
✓ Branch 1 taken 47312 times.
31881136 for (i = 0; i < w; i++) {
97
2/2
✓ Branch 0 taken 2672241 times.
✓ Branch 1 taken 29161583 times.
31833824 if (src[i] > high) {
98 2672241 dst[i] = src[i];
99 2672241 continue;
100 }
101
102
8/8
✓ Branch 0 taken 29114271 times.
✓ Branch 1 taken 47312 times.
✓ Branch 2 taken 29066959 times.
✓ Branch 3 taken 47312 times.
✓ Branch 4 taken 29020935 times.
✓ Branch 5 taken 46024 times.
✓ Branch 6 taken 28974911 times.
✓ Branch 7 taken 46024 times.
29161583 if (!(!i || i == w - 1 || !j || j == h - 1) &&
103
2/2
✓ Branch 0 taken 469479 times.
✓ Branch 1 taken 28505432 times.
28974911 src[i] > low &&
104
2/2
✓ Branch 0 taken 459738 times.
✓ Branch 1 taken 9741 times.
469479 (src[-src_linesize + i-1] > high ||
105
2/2
✓ Branch 0 taken 450240 times.
✓ Branch 1 taken 9498 times.
459738 src[-src_linesize + i ] > high ||
106
2/2
✓ Branch 0 taken 443967 times.
✓ Branch 1 taken 6273 times.
450240 src[-src_linesize + i+1] > high ||
107
2/2
✓ Branch 0 taken 435341 times.
✓ Branch 1 taken 8626 times.
443967 src[ i-1] > high ||
108
2/2
✓ Branch 0 taken 428195 times.
✓ Branch 1 taken 7146 times.
435341 src[ i+1] > high ||
109
2/2
✓ Branch 0 taken 422774 times.
✓ Branch 1 taken 5421 times.
428195 src[ src_linesize + i-1] > high ||
110
2/2
✓ Branch 0 taken 415726 times.
✓ Branch 1 taken 7048 times.
422774 src[ src_linesize + i ] > high ||
111
2/2
✓ Branch 0 taken 5968 times.
✓ Branch 1 taken 409758 times.
415726 src[ src_linesize + i+1] > high))
112 59721 dst[i] = src[i];
113 else
114 29101862 dst[i] = 0;
115 }
116 47312 dst += dst_linesize;
117 47312 src += src_linesize;
118 }
119 103 }
120