FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_colordetectdsp.h
Date: 2025-10-11 06:18:21
Exec Total Coverage
Lines: 86 86 100.0%
Functions: 9 9 100.0%
Branches: 58 68 85.3%

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 #ifndef AVFILTER_COLORDETECTDSP_H
20 #define AVFILTER_COLORDETECTDSP_H
21
22 #include <stddef.h>
23 #include <stdint.h>
24
25 #include "config.h"
26
27 #include "libavutil/attributes.h"
28 #include "libavutil/avassert.h"
29 #include "libavutil/pixfmt.h"
30
31 enum FFAlphaDetect {
32 FF_ALPHA_NONE = -1,
33 FF_ALPHA_UNDETERMINED = 0,
34 FF_ALPHA_TRANSPARENT = 1 << 0, ///< alpha < alpha_max
35 FF_ALPHA_STRAIGHT = (1 << 1) | FF_ALPHA_TRANSPARENT, ///< alpha < pixel
36 /* No way to positively identify premultiplied alpha */
37 };
38
39 typedef struct FFColorDetectDSPContext {
40 /* Returns 1 if an out-of-range value was detected, 0 otherwise */
41 int (*detect_range)(const uint8_t *data, ptrdiff_t stride,
42 ptrdiff_t width, ptrdiff_t height,
43 int mpeg_min, int mpeg_max);
44
45 /* Returns an FFAlphaDetect enum value */
46 int (*detect_alpha)(const uint8_t *color, ptrdiff_t color_stride,
47 const uint8_t *alpha, ptrdiff_t alpha_stride,
48 ptrdiff_t width, ptrdiff_t height,
49 int alpha_max, int mpeg_range, int offset);
50 } FFColorDetectDSPContext;
51
52 void ff_color_detect_dsp_init_aarch64(FFColorDetectDSPContext *dsp, int depth,
53 enum AVColorRange color_range);
54 void ff_color_detect_dsp_init_x86(FFColorDetectDSPContext *dsp, int depth,
55 enum AVColorRange color_range);
56
57 64 static inline int ff_detect_range_impl_c(const uint8_t *data, ptrdiff_t stride,
58 ptrdiff_t width, ptrdiff_t height,
59 uint8_t mpeg_min, uint8_t mpeg_max)
60 {
61
2/2
✓ Branch 0 taken 482 times.
✓ Branch 1 taken 44 times.
526 while (height--) {
62 482 uint8_t cond = 0;
63
2/2
✓ Branch 0 taken 190648 times.
✓ Branch 1 taken 482 times.
191130 for (int x = 0; x < width; x++) {
64 190648 const uint8_t val = data[x];
65
3/4
✓ Branch 0 taken 190628 times.
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 190628 times.
190648 cond |= val < mpeg_min || val > mpeg_max;
66 }
67
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 462 times.
482 if (cond)
68 20 return 1;
69 462 data += stride;
70 }
71
72 44 return 0;
73 }
74
75 64 static inline int ff_detect_range_c(const uint8_t *data, ptrdiff_t stride,
76 ptrdiff_t width, ptrdiff_t height,
77 int mpeg_min, int mpeg_max)
78 {
79
2/4
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 64 times.
64 av_assume(mpeg_min >= 0 && mpeg_min <= UINT8_MAX);
80
2/4
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 64 times.
64 av_assume(mpeg_max >= 0 && mpeg_max <= UINT8_MAX);
81 64 return ff_detect_range_impl_c(data, stride, width, height, mpeg_min, mpeg_max);
82 }
83
84 64 static inline int ff_detect_range16_impl_c(const uint8_t *data, ptrdiff_t stride,
85 ptrdiff_t width, ptrdiff_t height,
86 uint16_t mpeg_min, uint16_t mpeg_max)
87 {
88
2/2
✓ Branch 0 taken 508 times.
✓ Branch 1 taken 48 times.
556 while (height--) {
89 508 const uint16_t *data16 = (const uint16_t *) data;
90 508 uint8_t cond = 0;
91
2/2
✓ Branch 0 taken 102344 times.
✓ Branch 1 taken 508 times.
102852 for (int x = 0; x < width; x++) {
92 102344 const uint16_t val = data16[x];
93
3/4
✓ Branch 0 taken 102328 times.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 102328 times.
102344 cond |= val < mpeg_min || val > mpeg_max;
94 }
95
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 492 times.
508 if (cond)
96 16 return 1;
97 492 data += stride;
98 }
99
100 48 return 0;
101 }
102
103 64 static inline int ff_detect_range16_c(const uint8_t *data, ptrdiff_t stride,
104 ptrdiff_t width, ptrdiff_t height,
105 int mpeg_min, int mpeg_max)
106 {
107
2/4
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 64 times.
64 av_assume(mpeg_min >= 0 && mpeg_min <= UINT16_MAX);
108
2/4
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 64 times.
64 av_assume(mpeg_max >= 0 && mpeg_max <= UINT16_MAX);
109 64 return ff_detect_range16_impl_c(data, stride, width, height, mpeg_min, mpeg_max);
110 }
111
112 static inline int
113 65 ff_detect_alpha_full_c(const uint8_t *color, ptrdiff_t color_stride,
114 const uint8_t *alpha, ptrdiff_t alpha_stride,
115 ptrdiff_t width, ptrdiff_t height,
116 int alpha_max, int mpeg_range, int offset)
117 {
118 65 uint8_t transparent = 0;
119
2/2
✓ Branch 0 taken 558 times.
✓ Branch 1 taken 58 times.
616 while (height--) {
120 558 uint8_t straight = 0;
121
2/2
✓ Branch 0 taken 246536 times.
✓ Branch 1 taken 558 times.
247094 for (int x = 0; x < width; x++) {
122 246536 straight |= color[x] > alpha[x];
123 246536 transparent |= alpha[x] != alpha_max;
124 }
125
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 551 times.
558 if (straight)
126 7 return FF_ALPHA_STRAIGHT;
127 551 color += color_stride;
128 551 alpha += alpha_stride;
129 }
130 58 return transparent ? FF_ALPHA_TRANSPARENT : 0;
131 }
132
133 static inline int
134 52 ff_detect_alpha_limited_c(const uint8_t *color, ptrdiff_t color_stride,
135 const uint8_t *alpha, ptrdiff_t alpha_stride,
136 ptrdiff_t width, ptrdiff_t height,
137 int alpha_max, int mpeg_range, int offset)
138 {
139 52 uint8_t transparent = 0;
140
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 4 times.
116 while (height--) {
141 112 uint8_t straight = 0;
142
2/2
✓ Branch 0 taken 52288 times.
✓ Branch 1 taken 112 times.
52400 for (int x = 0; x < width; x++) {
143 52288 straight |= alpha_max * color[x] - offset > mpeg_range * alpha[x];
144 52288 transparent |= alpha[x] != alpha_max;
145 }
146
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 64 times.
112 if (straight)
147 48 return FF_ALPHA_STRAIGHT;
148 64 color += color_stride;
149 64 alpha += alpha_stride;
150 }
151 4 return transparent ? FF_ALPHA_TRANSPARENT : 0;
152 }
153
154 static inline int
155 62 ff_detect_alpha16_full_c(const uint8_t *color, ptrdiff_t color_stride,
156 const uint8_t *alpha, ptrdiff_t alpha_stride,
157 ptrdiff_t width, ptrdiff_t height,
158 int alpha_max, int mpeg_range, int offset)
159 {
160 62 uint8_t transparent = 0;
161
2/2
✓ Branch 0 taken 500 times.
✓ Branch 1 taken 48 times.
548 while (height--) {
162 500 const uint16_t *color16 = (const uint16_t *) color;
163 500 const uint16_t *alpha16 = (const uint16_t *) alpha;
164 500 uint8_t straight = 0;
165
2/2
✓ Branch 0 taken 116824 times.
✓ Branch 1 taken 500 times.
117324 for (int x = 0; x < width; x++) {
166 116824 straight |= color16[x] > alpha16[x];
167 116824 transparent |= alpha16[x] != alpha_max;
168 }
169
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 486 times.
500 if (straight)
170 14 return FF_ALPHA_STRAIGHT;
171 486 color += color_stride;
172 486 alpha += alpha_stride;
173 }
174 48 return transparent ? FF_ALPHA_TRANSPARENT : 0;
175 }
176
177 static inline int
178 52 ff_detect_alpha16_limited_c(const uint8_t *color, ptrdiff_t color_stride,
179 const uint8_t *alpha, ptrdiff_t alpha_stride,
180 ptrdiff_t width, ptrdiff_t height,
181 int alpha_max, int mpeg_range, int offset)
182 {
183 52 uint8_t transparent = 0;
184
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 4 times.
116 while (height--) {
185 112 const uint16_t *color16 = (const uint16_t *) color;
186 112 const uint16_t *alpha16 = (const uint16_t *) alpha;
187
2/2
✓ Branch 0 taken 13232 times.
✓ Branch 1 taken 64 times.
13296 for (int x = 0; x < width; x++) {
188
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 13184 times.
13232 if ((int64_t) alpha_max * color16[x] - offset > (int64_t) mpeg_range * alpha16[x])
189 48 return FF_ALPHA_STRAIGHT;
190 13184 transparent |= alpha16[x] != alpha_max;
191 }
192 64 color += color_stride;
193 64 alpha += alpha_stride;
194 }
195 4 return transparent ? FF_ALPHA_TRANSPARENT : 0;
196 }
197
198 static av_cold inline void
199 78 ff_color_detect_dsp_init(FFColorDetectDSPContext *dsp, int depth,
200 enum AVColorRange color_range)
201 {
202
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 39 times.
78 dsp->detect_range = depth > 8 ? ff_detect_range16_c : ff_detect_range_c;
203
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 52 times.
78 if (color_range == AVCOL_RANGE_JPEG) {
204
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 13 times.
26 dsp->detect_alpha = depth > 8 ? ff_detect_alpha16_full_c : ff_detect_alpha_full_c;
205 } else {
206
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 26 times.
52 dsp->detect_alpha = depth > 8 ? ff_detect_alpha16_limited_c : ff_detect_alpha_limited_c;
207 }
208
209 #if ARCH_AARCH64
210 ff_color_detect_dsp_init_aarch64(dsp, depth, color_range);
211 #elif ARCH_X86
212 78 ff_color_detect_dsp_init_x86(dsp, depth, color_range);
213 #endif
214 78 }
215
216 #endif /* AVFILTER_COLORDETECTDSP_H */
217