FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_colordetectdsp.h
Date: 2026-05-01 13:04:54
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 493 times.
✓ Branch 1 taken 45 times.
538 while (height--) {
62 493 uint8_t cond = 0;
63
2/2
✓ Branch 0 taken 196588 times.
✓ Branch 1 taken 493 times.
197081 for (int x = 0; x < width; x++) {
64 196588 const uint8_t val = data[x];
65
3/4
✓ Branch 0 taken 196569 times.
✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 196569 times.
196588 cond |= val < mpeg_min || val > mpeg_max;
66 }
67
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 474 times.
493 if (cond)
68 19 return 1;
69 474 data += stride;
70 }
71
72 45 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 543 times.
✓ Branch 1 taken 60 times.
603 while (height--) {
89 543 const uint16_t *data16 = (const uint16_t *) data;
90 543 uint8_t cond = 0;
91
2/2
✓ Branch 0 taken 111794 times.
✓ Branch 1 taken 543 times.
112337 for (int x = 0; x < width; x++) {
92 111794 const uint16_t val = data16[x];
93
3/4
✓ Branch 0 taken 111790 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 111790 times.
111794 cond |= val < mpeg_min || val > mpeg_max;
94 }
95
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 539 times.
543 if (cond)
96 4 return 1;
97 539 data += stride;
98 }
99
100 60 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 66 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 66 uint8_t transparent = 0;
119
2/2
✓ Branch 0 taken 576 times.
✓ Branch 1 taken 62 times.
638 while (height--) {
120 576 uint8_t straight = 0;
121
2/2
✓ Branch 0 taken 249088 times.
✓ Branch 1 taken 576 times.
249664 for (int x = 0; x < width; x++) {
122 249088 straight |= color[x] > alpha[x];
123 249088 transparent |= alpha[x] != alpha_max;
124 }
125
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 572 times.
576 if (straight)
126 4 return FF_ALPHA_STRAIGHT;
127 572 color += color_stride;
128 572 alpha += alpha_stride;
129 }
130 62 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 61 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 61 uint8_t transparent = 0;
161
2/2
✓ Branch 0 taken 496 times.
✓ Branch 1 taken 52 times.
548 while (height--) {
162 496 const uint16_t *color16 = (const uint16_t *) color;
163 496 const uint16_t *alpha16 = (const uint16_t *) alpha;
164 496 uint8_t straight = 0;
165
2/2
✓ Branch 0 taken 118304 times.
✓ Branch 1 taken 496 times.
118800 for (int x = 0; x < width; x++) {
166 118304 straight |= color16[x] > alpha16[x];
167 118304 transparent |= alpha16[x] != alpha_max;
168 }
169
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 487 times.
496 if (straight)
170 9 return FF_ALPHA_STRAIGHT;
171 487 color += color_stride;
172 487 alpha += alpha_stride;
173 }
174 52 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 84 ff_color_detect_dsp_init(FFColorDetectDSPContext *dsp, int depth,
200 enum AVColorRange color_range)
201 {
202
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 42 times.
84 dsp->detect_range = depth > 8 ? ff_detect_range16_c : ff_detect_range_c;
203
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 56 times.
84 if (color_range == AVCOL_RANGE_JPEG) {
204
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 14 times.
28 dsp->detect_alpha = depth > 8 ? ff_detect_alpha16_full_c : ff_detect_alpha_full_c;
205 } else {
206
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 28 times.
56 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 && HAVE_X86ASM
212 84 ff_color_detect_dsp_init_x86(dsp, depth, color_range);
213 #endif
214 84 }
215
216 #endif /* AVFILTER_COLORDETECTDSP_H */
217