FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/drawutils.c
Date: 2022-12-09 07:38:14
Exec Total Coverage
Lines: 333 389 85.6%
Functions: 19 19 100.0%
Branches: 209 266 78.6%

Line Branch Exec Source
1 /*
2 * Copyright 2011 Stefano Sabatini <stefano.sabatini-lala poste it>
3 * Copyright 2012 Nicolas George <nicolas.george normalesup org>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <string.h>
23
24 #include "libavutil/avassert.h"
25 #include "libavutil/avutil.h"
26 #include "libavutil/csp.h"
27 #include "libavutil/intreadwrite.h"
28 #include "libavutil/pixdesc.h"
29 #include "colorspace.h"
30 #include "drawutils.h"
31 #include "formats.h"
32
33 enum { RED = 0, GREEN, BLUE, ALPHA };
34
35 103 int ff_fill_rgba_map(uint8_t *rgba_map, enum AVPixelFormat pix_fmt)
36 {
37 103 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
38
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 62 times.
103 if (!(desc->flags & AV_PIX_FMT_FLAG_RGB))
39 41 return AVERROR(EINVAL);
40
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (desc->flags & AV_PIX_FMT_FLAG_BITSTREAM)
41 return AVERROR(EINVAL);
42
3/4
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 62 times.
62 av_assert0(desc->nb_components == 3 + !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA));
43
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 39 times.
62 if (desc->flags & AV_PIX_FMT_FLAG_PLANAR) {
44 23 rgba_map[RED] = desc->comp[0].plane;
45 23 rgba_map[GREEN] = desc->comp[1].plane;
46 23 rgba_map[BLUE] = desc->comp[2].plane;
47
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 11 times.
23 rgba_map[ALPHA] = (desc->flags & AV_PIX_FMT_FLAG_ALPHA) ? desc->comp[3].plane : 3;
48 } else {
49 39 int had0 = 0;
50 39 unsigned depthb = 0;
51 unsigned i;
52
2/2
✓ Branch 0 taken 131 times.
✓ Branch 1 taken 39 times.
170 for (i = 0; i < desc->nb_components; i++) {
53 /* all components must have same depth in bytes */
54 131 unsigned db = (desc->comp[i].depth + 7) / 8;
55 131 unsigned pos = desc->comp[i].offset / db;
56
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 131 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
131 if (depthb && (depthb != db))
57 return AVERROR(ENOSYS);
58
59
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 131 times.
131 if (desc->comp[i].offset % db)
60 return AVERROR(ENOSYS);
61
62 131 had0 |= pos == 0;
63 131 rgba_map[i] = pos;
64 }
65
66
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 14 times.
39 if (desc->nb_components == 3)
67
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 rgba_map[ALPHA] = had0 ? 3 : 0;
68 }
69
70
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 av_assert0(rgba_map[RED] != rgba_map[GREEN]);
71
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 av_assert0(rgba_map[GREEN] != rgba_map[BLUE]);
72
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 av_assert0(rgba_map[BLUE] != rgba_map[RED]);
73
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 av_assert0(rgba_map[RED] != rgba_map[ALPHA]);
74
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 av_assert0(rgba_map[GREEN] != rgba_map[ALPHA]);
75
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 av_assert0(rgba_map[BLUE] != rgba_map[ALPHA]);
76
77 62 return 0;
78 }
79
80 33285 int ff_draw_init2(FFDrawContext *draw, enum AVPixelFormat format, enum AVColorSpace csp,
81 enum AVColorRange range, unsigned flags)
82 {
83 33285 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);
84 33285 const AVLumaCoefficients *luma = NULL;
85 const AVComponentDescriptor *c;
86 33285 unsigned i, nb_planes = 0;
87 33285 int pixelstep[MAX_PLANES] = { 0 };
88 33285 int depthb = 0;
89
90
2/4
✓ Branch 0 taken 33285 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 33285 times.
33285 if (!desc || !desc->name)
91 return AVERROR(EINVAL);
92
2/2
✓ Branch 0 taken 11622 times.
✓ Branch 1 taken 21663 times.
33285 if (desc->flags & AV_PIX_FMT_FLAG_BE)
93 11622 return AVERROR(ENOSYS);
94
2/2
✓ Branch 0 taken 4917 times.
✓ Branch 1 taken 16746 times.
21663 if (desc->flags & ~(AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB | AV_PIX_FMT_FLAG_ALPHA))
95 4917 return AVERROR(ENOSYS);
96
1/2
✓ Branch 0 taken 16746 times.
✗ Branch 1 not taken.
16746 if (csp == AVCOL_SPC_UNSPECIFIED)
97
2/2
✓ Branch 0 taken 5406 times.
✓ Branch 1 taken 11340 times.
16746 csp = (desc->flags & AV_PIX_FMT_FLAG_RGB) ? AVCOL_SPC_RGB : AVCOL_SPC_SMPTE170M;
98
3/4
✓ Branch 0 taken 11340 times.
✓ Branch 1 taken 5406 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11340 times.
16746 if (!(desc->flags & AV_PIX_FMT_FLAG_RGB) && !(luma = av_csp_luma_coeffs_from_avcsp(csp)))
99 return AVERROR(EINVAL);
100
1/2
✓ Branch 0 taken 16746 times.
✗ Branch 1 not taken.
16746 if (range == AVCOL_RANGE_UNSPECIFIED)
101
4/4
✓ Branch 0 taken 16443 times.
✓ Branch 1 taken 151 times.
✓ Branch 2 taken 16291 times.
✓ Branch 3 taken 152 times.
33340 range = (format == AV_PIX_FMT_YUVJ420P || format == AV_PIX_FMT_YUVJ422P ||
102
4/4
✓ Branch 0 taken 16141 times.
✓ Branch 1 taken 150 times.
✓ Branch 2 taken 15990 times.
✓ Branch 3 taken 151 times.
16291 format == AV_PIX_FMT_YUVJ444P || format == AV_PIX_FMT_YUVJ411P ||
103
2/2
✓ Branch 0 taken 5406 times.
✓ Branch 1 taken 10584 times.
15990 format == AV_PIX_FMT_YUVJ440P || csp == AVCOL_SPC_RGB)
104
2/2
✓ Branch 0 taken 16594 times.
✓ Branch 1 taken 152 times.
33340 ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
105
3/4
✓ Branch 0 taken 10584 times.
✓ Branch 1 taken 6162 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10584 times.
16746 if (range != AVCOL_RANGE_JPEG && range != AVCOL_RANGE_MPEG)
106 return AVERROR(EINVAL);
107
2/2
✓ Branch 0 taken 45506 times.
✓ Branch 1 taken 13021 times.
58527 for (i = 0; i < desc->nb_components; i++) {
108 int db;
109 45506 c = &desc->comp[i];
110 /* for now, only 8-16 bits formats */
111
3/4
✓ Branch 0 taken 44016 times.
✓ Branch 1 taken 1490 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 44016 times.
45506 if (c->depth < 8 || c->depth > 16)
112 1490 return AVERROR(ENOSYS);
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44016 times.
44016 if (c->plane >= MAX_PLANES)
114 return AVERROR(ENOSYS);
115 /* data must either be in the high or low bits, never middle */
116
4/4
✓ Branch 0 taken 3442 times.
✓ Branch 1 taken 40574 times.
✓ Branch 2 taken 447 times.
✓ Branch 3 taken 2995 times.
44016 if (c->shift && ((c->shift + c->depth) & 0x7))
117 447 return AVERROR(ENOSYS);
118 /* mixed >8 and <=8 depth */
119 43569 db = (c->depth + 7) / 8;
120
3/4
✓ Branch 0 taken 28611 times.
✓ Branch 1 taken 14958 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 28611 times.
43569 if (depthb && (depthb != db))
121 return AVERROR(ENOSYS);
122 43569 depthb = db;
123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43569 times.
43569 if (db * (c->offset + 1) > 16)
124 return AVERROR(ENOSYS);
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43569 times.
43569 if (c->offset % db)
126 return AVERROR(ENOSYS);
127 /* strange interleaving */
128
2/2
✓ Branch 0 taken 8134 times.
✓ Branch 1 taken 35435 times.
43569 if (pixelstep[c->plane] != 0 &&
129
2/2
✓ Branch 0 taken 894 times.
✓ Branch 1 taken 7240 times.
8134 pixelstep[c->plane] != c->step)
130 894 return AVERROR(ENOSYS);
131
2/2
✓ Branch 0 taken 598 times.
✓ Branch 1 taken 42077 times.
42675 if (pixelstep[c->plane] == 6 &&
132
2/2
✓ Branch 0 taken 298 times.
✓ Branch 1 taken 300 times.
598 c->depth == 16)
133 298 return AVERROR(ENOSYS);
134 42377 pixelstep[c->plane] = c->step;
135
2/2
✓ Branch 0 taken 596 times.
✓ Branch 1 taken 41781 times.
42377 if (pixelstep[c->plane] >= 8)
136 596 return AVERROR(ENOSYS);
137 41781 nb_planes = FFMAX(nb_planes, c->plane + 1);
138 }
139 13021 memset(draw, 0, sizeof(*draw));
140 13021 draw->desc = desc;
141 13021 draw->format = format;
142 13021 draw->nb_planes = nb_planes;
143 13021 draw->range = range;
144 13021 draw->csp = csp;
145 13021 draw->flags = flags;
146
2/2
✓ Branch 0 taken 9999 times.
✓ Branch 1 taken 3022 times.
13021 if (luma)
147 9999 ff_fill_rgb2yuv_table(luma, draw->rgb2yuv);
148 13021 memcpy(draw->pixelstep, pixelstep, sizeof(draw->pixelstep));
149 13021 draw->hsub[1] = draw->hsub[2] = draw->hsub_max = desc->log2_chroma_w;
150 13021 draw->vsub[1] = draw->vsub[2] = draw->vsub_max = desc->log2_chroma_h;
151 13021 return 0;
152 }
153
154 33285 int ff_draw_init(FFDrawContext *draw, enum AVPixelFormat format, unsigned flags)
155 {
156 33285 return ff_draw_init2(draw, format, AVCOL_SPC_UNSPECIFIED, AVCOL_RANGE_UNSPECIFIED, flags);
157 }
158
159 195268 void ff_draw_color(FFDrawContext *draw, FFDrawColor *color, const uint8_t rgba[4])
160 {
161 unsigned i;
162 double yuvad[4];
163 double rgbad[4];
164 195268 const AVPixFmtDescriptor *desc = draw->desc;
165
166
1/2
✓ Branch 0 taken 195268 times.
✗ Branch 1 not taken.
195268 if (rgba != color->rgba)
167 195268 memcpy(color->rgba, rgba, sizeof(color->rgba));
168
169 195268 memset(color->comp, 0, sizeof(color->comp));
170
171
2/2
✓ Branch 0 taken 781072 times.
✓ Branch 1 taken 195268 times.
976340 for (int i = 0; i < 4; i++)
172 781072 rgbad[i] = color->rgba[i] / 255.;
173
174
2/2
✓ Branch 0 taken 50828 times.
✓ Branch 1 taken 144440 times.
195268 if (draw->desc->flags & AV_PIX_FMT_FLAG_RGB)
175 50828 memcpy(yuvad, rgbad, sizeof(double) * 3);
176 else
177 144440 ff_matrix_mul_3x3_vec(yuvad, rgbad, draw->rgb2yuv);
178
179 195268 yuvad[3] = rgbad[3];
180
181
2/2
✓ Branch 0 taken 585804 times.
✓ Branch 1 taken 195268 times.
781072 for (int i = 0; i < 3; i++) {
182
4/4
✓ Branch 0 taken 433320 times.
✓ Branch 1 taken 152484 times.
✓ Branch 2 taken 288880 times.
✓ Branch 3 taken 144440 times.
585804 int chroma = (!(draw->desc->flags & AV_PIX_FMT_FLAG_RGB) && i > 0);
183
2/2
✓ Branch 0 taken 433287 times.
✓ Branch 1 taken 152517 times.
585804 if (draw->range == AVCOL_RANGE_MPEG) {
184
2/2
✓ Branch 0 taken 288858 times.
✓ Branch 1 taken 144429 times.
433287 yuvad[i] *= (chroma ? 224. : 219.) / 255.;
185
2/2
✓ Branch 0 taken 288858 times.
✓ Branch 1 taken 144429 times.
433287 yuvad[i] += (chroma ? 128. : 16.) / 255.;
186
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 152495 times.
152517 } else if (chroma) {
187 22 yuvad[i] += 0.5;
188 }
189 }
190
191 // Ensure we place the alpha appropriately for gray formats
192
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 195258 times.
195268 if (desc->nb_components <= 2)
193 10 yuvad[1] = yuvad[3];
194
195
2/2
✓ Branch 0 taken 609639 times.
✓ Branch 1 taken 195268 times.
804907 for (i = 0; i < desc->nb_components; i++) {
196 609639 unsigned val = yuvad[i] * ((1 << (draw->desc->comp[i].depth + draw->desc->comp[i].shift)) - 1) + 0.5;
197
2/2
✓ Branch 0 taken 59646 times.
✓ Branch 1 taken 549993 times.
609639 if (desc->comp[i].depth > 8)
198 59646 color->comp[desc->comp[i].plane].u16[desc->comp[i].offset / 2] = val;
199 else
200 549993 color->comp[desc->comp[i].plane].u8[desc->comp[i].offset] = val;
201 }
202 195268 }
203
204 566020 static uint8_t *pointer_at(FFDrawContext *draw, uint8_t *data[], int linesize[],
205 int plane, int x, int y)
206 {
207 566020 return data[plane] +
208 1132040 (y >> draw->vsub[plane]) * linesize[plane] +
209 566020 (x >> draw->hsub[plane]) * draw->pixelstep[plane];
210 }
211
212 37 void ff_copy_rectangle2(FFDrawContext *draw,
213 uint8_t *dst[], int dst_linesize[],
214 uint8_t *src[], int src_linesize[],
215 int dst_x, int dst_y, int src_x, int src_y,
216 int w, int h)
217 {
218 int plane, y, wp, hp;
219 uint8_t *p, *q;
220
221
2/2
✓ Branch 0 taken 111 times.
✓ Branch 1 taken 37 times.
148 for (plane = 0; plane < draw->nb_planes; plane++) {
222 111 p = pointer_at(draw, src, src_linesize, plane, src_x, src_y);
223 111 q = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
224 111 wp = AV_CEIL_RSHIFT(w, draw->hsub[plane]) * draw->pixelstep[plane];
225 111 hp = AV_CEIL_RSHIFT(h, draw->vsub[plane]);
226
2/2
✓ Branch 0 taken 21312 times.
✓ Branch 1 taken 111 times.
21423 for (y = 0; y < hp; y++) {
227 21312 memcpy(q, p, wp);
228 21312 p += src_linesize[plane];
229 21312 q += dst_linesize[plane];
230 }
231 }
232 37 }
233
234 197054 void ff_fill_rectangle(FFDrawContext *draw, FFDrawColor *color,
235 uint8_t *dst[], int dst_linesize[],
236 int dst_x, int dst_y, int w, int h)
237 {
238 int plane, x, y, wp, hp;
239 uint8_t *p0, *p;
240 197054 FFDrawColor color_tmp = *color;
241
242
2/2
✓ Branch 0 taken 489947 times.
✓ Branch 1 taken 197054 times.
687001 for (plane = 0; plane < draw->nb_planes; plane++) {
243 489947 p0 = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
244 489947 wp = AV_CEIL_RSHIFT(w, draw->hsub[plane]);
245 489947 hp = AV_CEIL_RSHIFT(h, draw->vsub[plane]);
246
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 489947 times.
489947 if (!hp)
247 return;
248 489947 p = p0;
249
250 if (HAVE_BIGENDIAN && draw->desc->comp[0].depth > 8) {
251 for (x = 0; 2*x < draw->pixelstep[plane]; x++)
252 color_tmp.comp[plane].u16[x] = av_bswap16(color_tmp.comp[plane].u16[x]);
253 }
254
255 /* copy first line from color */
256
2/2
✓ Branch 0 taken 1874759 times.
✓ Branch 1 taken 489947 times.
2364706 for (x = 0; x < wp; x++) {
257 1874759 memcpy(p, color_tmp.comp[plane].u8, draw->pixelstep[plane]);
258 1874759 p += draw->pixelstep[plane];
259 }
260 489947 wp *= draw->pixelstep[plane];
261 /* copy next lines from first line */
262 489947 p = p0 + dst_linesize[plane];
263
2/2
✓ Branch 0 taken 8421714 times.
✓ Branch 1 taken 489947 times.
8911661 for (y = 1; y < hp; y++) {
264 8421714 memcpy(p, p0, wp);
265 8421714 p += dst_linesize[plane];
266 }
267 }
268 }
269
270 /**
271 * Clip interval [x; x+w[ within [0; wmax[.
272 * The resulting w may be negative if the final interval is empty.
273 * dx, if not null, return the difference between in and out value of x.
274 */
275 56954 static void clip_interval(int wmax, int *x, int *w, int *dx)
276 {
277
2/2
✓ Branch 0 taken 55172 times.
✓ Branch 1 taken 1782 times.
56954 if (dx)
278 55172 *dx = 0;
279
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56954 times.
56954 if (*x < 0) {
280 if (dx)
281 *dx = -*x;
282 *w += *x;
283 *x = 0;
284 }
285
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56954 times.
56954 if (*x + *w > wmax)
286 *w = wmax - *x;
287 56954 }
288
289 /**
290 * Decompose w pixels starting at x
291 * into start + (w starting at x) + end
292 * with x and w aligned on multiples of 1<<sub.
293 */
294 151702 static void subsampling_bounds(int sub, int *x, int *w, int *start, int *end)
295 {
296 151702 int mask = (1 << sub) - 1;
297
298 151702 *start = (-*x) & mask;
299 151702 *x += *start;
300 151702 *start = FFMIN(*start, *w);
301 151702 *w -= *start;
302 151702 *end = *w & mask;
303 151702 *w >>= sub;
304 151702 }
305
306 /* If alpha is in the [ 0 ; 0x1010101 ] range,
307 then alpha * value is in the [ 0 ; 0xFFFFFFFF ] range,
308 and >> 24 gives a correct rounding. */
309 64512 static void blend_line(uint8_t *dst, unsigned src, unsigned alpha,
310 int dx, int w, unsigned hsub, int left, int right)
311 {
312 64512 unsigned asrc = alpha * src;
313 64512 unsigned tau = 0x1010101 - alpha;
314 int x;
315
316
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64512 times.
64512 if (left) {
317 unsigned suba = (left * alpha) >> hsub;
318 *dst = (*dst * (0x1010101 - suba) + src * suba) >> 24;
319 dst += dx;
320 }
321
2/2
✓ Branch 0 taken 5423400 times.
✓ Branch 1 taken 64512 times.
5487912 for (x = 0; x < w; x++) {
322 5423400 *dst = (*dst * tau + asrc) >> 24;
323 5423400 dst += dx;
324 }
325
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64512 times.
64512 if (right) {
326 unsigned suba = (right * alpha) >> hsub;
327 *dst = (*dst * (0x1010101 - suba) + src * suba) >> 24;
328 }
329 64512 }
330
331 11880 static void blend_line16(uint8_t *dst, unsigned src, unsigned alpha,
332 int dx, int w, unsigned hsub, int left, int right)
333 {
334 11880 unsigned asrc = alpha * src;
335 11880 unsigned tau = 0x10001 - alpha;
336 int x;
337
338
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11880 times.
11880 if (left) {
339 unsigned suba = (left * alpha) >> hsub;
340 uint16_t value = AV_RL16(dst);
341 AV_WL16(dst, (value * (0x10001 - suba) + src * suba) >> 16);
342 dst += dx;
343 }
344
2/2
✓ Branch 0 taken 792000 times.
✓ Branch 1 taken 11880 times.
803880 for (x = 0; x < w; x++) {
345 792000 uint16_t value = AV_RL16(dst);
346 792000 AV_WL16(dst, (value * tau + asrc) >> 16);
347 792000 dst += dx;
348 }
349
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11880 times.
11880 if (right) {
350 unsigned suba = (right * alpha) >> hsub;
351 uint16_t value = AV_RL16(dst);
352 AV_WL16(dst, (value * (0x10001 - suba) + src * suba) >> 16);
353 }
354 11880 }
355
356 891 void ff_blend_rectangle(FFDrawContext *draw, FFDrawColor *color,
357 uint8_t *dst[], int dst_linesize[],
358 int dst_w, int dst_h,
359 int x0, int y0, int w, int h)
360 {
361 unsigned alpha, nb_planes, nb_comp, plane, comp;
362 int w_sub, h_sub, x_sub, y_sub, left, right, top, bottom, y;
363 uint8_t *p0, *p;
364
365 1782 nb_comp = draw->desc->nb_components -
366
3/4
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 821 times.
✓ Branch 2 taken 70 times.
✗ Branch 3 not taken.
891 !!(draw->desc->flags & AV_PIX_FMT_FLAG_ALPHA && !(draw->flags & FF_DRAW_PROCESS_ALPHA));
367
368 /* TODO optimize if alpha = 0xFF */
369 891 clip_interval(dst_w, &x0, &w, NULL);
370 891 clip_interval(dst_h, &y0, &h, NULL);
371
3/6
✓ Branch 0 taken 891 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 891 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 891 times.
891 if (w <= 0 || h <= 0 || !color->rgba[3])
372 return;
373
2/2
✓ Branch 0 taken 781 times.
✓ Branch 1 taken 110 times.
891 if (draw->desc->comp[0].depth <= 8) {
374 /* 0x10203 * alpha + 2 is in the [ 2 ; 0x1010101 - 2 ] range */
375 781 alpha = 0x10203 * color->rgba[3] + 0x2;
376 } else {
377 /* 0x101 * alpha is in the [ 2 ; 0x1001] range */
378 110 alpha = 0x101 * color->rgba[3] + 0x2;
379 }
380
3/4
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 821 times.
✓ Branch 2 taken 70 times.
✗ Branch 3 not taken.
891 nb_planes = draw->nb_planes - !!(draw->desc->flags & AV_PIX_FMT_FLAG_ALPHA && !(draw->flags & FF_DRAW_PROCESS_ALPHA));
381 891 nb_planes += !nb_planes;
382
2/2
✓ Branch 0 taken 2373 times.
✓ Branch 1 taken 891 times.
3264 for (plane = 0; plane < nb_planes; plane++) {
383 2373 p0 = pointer_at(draw, dst, dst_linesize, plane, x0, y0);
384 2373 w_sub = w;
385 2373 h_sub = h;
386 2373 x_sub = x0;
387 2373 y_sub = y0;
388 2373 subsampling_bounds(draw->hsub[plane], &x_sub, &w_sub, &left, &right);
389 2373 subsampling_bounds(draw->vsub[plane], &y_sub, &h_sub, &top, &bottom);
390
2/2
✓ Branch 0 taken 7119 times.
✓ Branch 1 taken 2373 times.
9492 for (comp = 0; comp < nb_comp; comp++) {
391 7119 const int depth = draw->desc->comp[comp].depth;
392 7119 const int offset = draw->desc->comp[comp].offset;
393 7119 const int index = offset / ((depth + 7) / 8);
394
395
2/2
✓ Branch 0 taken 4446 times.
✓ Branch 1 taken 2673 times.
7119 if (draw->desc->comp[comp].plane != plane)
396 4446 continue;
397 2673 p = p0 + offset;
398
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2673 times.
2673 if (top) {
399 if (depth <= 8) {
400 blend_line(p, color->comp[plane].u8[index], alpha >> 1,
401 draw->pixelstep[plane], w_sub,
402 draw->hsub[plane], left, right);
403 } else {
404 blend_line16(p, color->comp[plane].u16[index], alpha >> 1,
405 draw->pixelstep[plane], w_sub,
406 draw->hsub[plane], left, right);
407 }
408 p += dst_linesize[plane];
409 }
410
2/2
✓ Branch 0 taken 2343 times.
✓ Branch 1 taken 330 times.
2673 if (depth <= 8) {
411
2/2
✓ Branch 0 taken 64512 times.
✓ Branch 1 taken 2343 times.
66855 for (y = 0; y < h_sub; y++) {
412 64512 blend_line(p, color->comp[plane].u8[index], alpha,
413 draw->pixelstep[plane], w_sub,
414 64512 draw->hsub[plane], left, right);
415 64512 p += dst_linesize[plane];
416 }
417 } else {
418
2/2
✓ Branch 0 taken 11880 times.
✓ Branch 1 taken 330 times.
12210 for (y = 0; y < h_sub; y++) {
419 11880 blend_line16(p, color->comp[plane].u16[index], alpha,
420 draw->pixelstep[plane], w_sub,
421 11880 draw->hsub[plane], left, right);
422 11880 p += dst_linesize[plane];
423 }
424 }
425
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2673 times.
2673 if (bottom) {
426 if (depth <= 8) {
427 blend_line(p, color->comp[plane].u8[index], alpha >> 1,
428 draw->pixelstep[plane], w_sub,
429 draw->hsub[plane], left, right);
430 } else {
431 blend_line16(p, color->comp[plane].u16[index], alpha >> 1,
432 draw->pixelstep[plane], w_sub,
433 draw->hsub[plane], left, right);
434 }
435 }
436 }
437 }
438 }
439
440 1070080 static void blend_pixel16(uint8_t *dst, unsigned src, unsigned alpha,
441 const uint8_t *mask, int mask_linesize, int l2depth,
442 unsigned w, unsigned h, unsigned shift, unsigned xm0)
443 {
444 1070080 unsigned xm, x, y, t = 0;
445 1070080 unsigned xmshf = 3 - l2depth;
446 1070080 unsigned xmmod = 7 >> l2depth;
447 1070080 unsigned mbits = (1 << (1 << l2depth)) - 1;
448 1070080 unsigned mmult = 255 / mbits;
449 1070080 uint16_t value = AV_RL16(dst);
450
451
2/2
✓ Branch 0 taken 1070080 times.
✓ Branch 1 taken 1070080 times.
2140160 for (y = 0; y < h; y++) {
452 1070080 xm = xm0;
453
2/2
✓ Branch 0 taken 1605120 times.
✓ Branch 1 taken 1070080 times.
2675200 for (x = 0; x < w; x++) {
454 1605120 t += ((mask[xm >> xmshf] >> ((~xm & xmmod) << l2depth)) & mbits)
455 1605120 * mmult;
456 1605120 xm++;
457 }
458 1070080 mask += mask_linesize;
459 }
460 1070080 alpha = (t >> shift) * alpha;
461 1070080 AV_WL16(dst, ((0x10001 - alpha) * value + alpha * src) >> 16);
462 1070080 }
463
464 7310496 static void blend_pixel(uint8_t *dst, unsigned src, unsigned alpha,
465 const uint8_t *mask, int mask_linesize, int l2depth,
466 unsigned w, unsigned h, unsigned shift, unsigned xm0)
467 {
468 7310496 unsigned xm, x, y, t = 0;
469 7310496 unsigned xmshf = 3 - l2depth;
470 7310496 unsigned xmmod = 7 >> l2depth;
471 7310496 unsigned mbits = (1 << (1 << l2depth)) - 1;
472 7310496 unsigned mmult = 255 / mbits;
473
474
2/2
✓ Branch 0 taken 8647168 times.
✓ Branch 1 taken 7310496 times.
15957664 for (y = 0; y < h; y++) {
475 8647168 xm = xm0;
476
2/2
✓ Branch 0 taken 11369472 times.
✓ Branch 1 taken 8647168 times.
20016640 for (x = 0; x < w; x++) {
477 11369472 t += ((mask[xm >> xmshf] >> ((~xm & xmmod) << l2depth)) & mbits)
478 11369472 * mmult;
479 11369472 xm++;
480 }
481 8647168 mask += mask_linesize;
482 }
483 7310496 alpha = (t >> shift) * alpha;
484 7310496 *dst = ((0x1010101 - alpha) * *dst + alpha * src) >> 24;
485 7310496 }
486
487 163680 static void blend_line_hv16(uint8_t *dst, int dst_delta,
488 unsigned src, unsigned alpha,
489 const uint8_t *mask, int mask_linesize, int l2depth, int w,
490 unsigned hsub, unsigned vsub,
491 int xm, int left, int right, int hband)
492 {
493 int x;
494
495
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 163680 times.
163680 if (left) {
496 blend_pixel16(dst, src, alpha, mask, mask_linesize, l2depth,
497 left, hband, hsub + vsub, xm);
498 dst += dst_delta;
499 xm += left;
500 }
501
2/2
✓ Branch 0 taken 1070080 times.
✓ Branch 1 taken 163680 times.
1233760 for (x = 0; x < w; x++) {
502 1070080 blend_pixel16(dst, src, alpha, mask, mask_linesize, l2depth,
503 1070080 1 << hsub, hband, hsub + vsub, xm);
504 1070080 dst += dst_delta;
505 1070080 xm += 1 << hsub;
506 }
507
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 163680 times.
163680 if (right)
508 blend_pixel16(dst, src, alpha, mask, mask_linesize, l2depth,
509 right, hband, hsub + vsub, xm);
510 163680 }
511
512 887572 static void blend_line_hv(uint8_t *dst, int dst_delta,
513 unsigned src, unsigned alpha,
514 const uint8_t *mask, int mask_linesize, int l2depth, int w,
515 unsigned hsub, unsigned vsub,
516 int xm, int left, int right, int hband)
517 {
518 int x;
519
520
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 887572 times.
887572 if (left) {
521 blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
522 left, hband, hsub + vsub, xm);
523 dst += dst_delta;
524 xm += left;
525 }
526
2/2
✓ Branch 0 taken 7310496 times.
✓ Branch 1 taken 887572 times.
8198068 for (x = 0; x < w; x++) {
527 7310496 blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
528 7310496 1 << hsub, hband, hsub + vsub, xm);
529 7310496 dst += dst_delta;
530 7310496 xm += 1 << hsub;
531 }
532
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 887572 times.
887572 if (right)
533 blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
534 right, hband, hsub + vsub, xm);
535 887572 }
536
537 27586 void ff_blend_mask(FFDrawContext *draw, FFDrawColor *color,
538 uint8_t *dst[], int dst_linesize[], int dst_w, int dst_h,
539 const uint8_t *mask, int mask_linesize, int mask_w, int mask_h,
540 int l2depth, unsigned endianness, int x0, int y0)
541 {
542 unsigned alpha, nb_planes, nb_comp, plane, comp;
543 int xm0, ym0, w_sub, h_sub, x_sub, y_sub, left, right, top, bottom, y;
544 uint8_t *p0, *p;
545 const uint8_t *m;
546
547 55172 nb_comp = draw->desc->nb_components -
548
3/4
✓ Branch 0 taken 2170 times.
✓ Branch 1 taken 25416 times.
✓ Branch 2 taken 2170 times.
✗ Branch 3 not taken.
27586 !!(draw->desc->flags & AV_PIX_FMT_FLAG_ALPHA && !(draw->flags & FF_DRAW_PROCESS_ALPHA));
549
550 27586 clip_interval(dst_w, &x0, &mask_w, &xm0);
551 27586 clip_interval(dst_h, &y0, &mask_h, &ym0);
552 27586 mask += ym0 * mask_linesize;
553
3/6
✓ Branch 0 taken 27586 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27586 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 27586 times.
27586 if (mask_w <= 0 || mask_h <= 0 || !color->rgba[3])
554 return;
555
2/2
✓ Branch 0 taken 24176 times.
✓ Branch 1 taken 3410 times.
27586 if (draw->desc->comp[0].depth <= 8) {
556 /* alpha is in the [ 0 ; 0x10203 ] range,
557 alpha * mask is in the [ 0 ; 0x1010101 - 4 ] range */
558 24176 alpha = (0x10307 * color->rgba[3] + 0x3) >> 8;
559 } else {
560 3410 alpha = (0x101 * color->rgba[3] + 0x2) >> 8;
561 }
562
3/4
✓ Branch 0 taken 2170 times.
✓ Branch 1 taken 25416 times.
✓ Branch 2 taken 2170 times.
✗ Branch 3 not taken.
27586 nb_planes = draw->nb_planes - !!(draw->desc->flags & AV_PIX_FMT_FLAG_ALPHA && !(draw->flags & FF_DRAW_PROCESS_ALPHA));
563 27586 nb_planes += !nb_planes;
564
2/2
✓ Branch 0 taken 73478 times.
✓ Branch 1 taken 27586 times.
101064 for (plane = 0; plane < nb_planes; plane++) {
565 73478 p0 = pointer_at(draw, dst, dst_linesize, plane, x0, y0);
566 73478 w_sub = mask_w;
567 73478 h_sub = mask_h;
568 73478 x_sub = x0;
569 73478 y_sub = y0;
570 73478 subsampling_bounds(draw->hsub[plane], &x_sub, &w_sub, &left, &right);
571 73478 subsampling_bounds(draw->vsub[plane], &y_sub, &h_sub, &top, &bottom);
572
2/2
✓ Branch 0 taken 220434 times.
✓ Branch 1 taken 73478 times.
293912 for (comp = 0; comp < nb_comp; comp++) {
573 220434 const int depth = draw->desc->comp[comp].depth;
574 220434 const int offset = draw->desc->comp[comp].offset;
575 220434 const int index = offset / ((depth + 7) / 8);
576
577
2/2
✓ Branch 0 taken 137676 times.
✓ Branch 1 taken 82758 times.
220434 if (draw->desc->comp[comp].plane != plane)
578 137676 continue;
579 82758 p = p0 + offset;
580 82758 m = mask;
581
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 82578 times.
82758 if (top) {
582
1/2
✓ Branch 0 taken 180 times.
✗ Branch 1 not taken.
180 if (depth <= 8) {
583 180 blend_line_hv(p, draw->pixelstep[plane],
584 180 color->comp[plane].u8[index], alpha,
585 m, mask_linesize, l2depth, w_sub,
586 180 draw->hsub[plane], draw->vsub[plane],
587 xm0, left, right, top);
588 } else {
589 blend_line_hv16(p, draw->pixelstep[plane],
590 color->comp[plane].u16[index], alpha,
591 m, mask_linesize, l2depth, w_sub,
592 draw->hsub[plane], draw->vsub[plane],
593 xm0, left, right, top);
594 }
595 180 p += dst_linesize[plane];
596 180 m += top * mask_linesize;
597 }
598
2/2
✓ Branch 0 taken 72528 times.
✓ Branch 1 taken 10230 times.
82758 if (depth <= 8) {
599
2/2
✓ Branch 0 taken 887212 times.
✓ Branch 1 taken 72528 times.
959740 for (y = 0; y < h_sub; y++) {
600 887212 blend_line_hv(p, draw->pixelstep[plane],
601 887212 color->comp[plane].u8[index], alpha,
602 m, mask_linesize, l2depth, w_sub,
603 887212 draw->hsub[plane], draw->vsub[plane],
604 887212 xm0, left, right, 1 << draw->vsub[plane]);
605 887212 p += dst_linesize[plane];
606 887212 m += mask_linesize << draw->vsub[plane];
607 }
608 } else {
609
2/2
✓ Branch 0 taken 163680 times.
✓ Branch 1 taken 10230 times.
173910 for (y = 0; y < h_sub; y++) {
610 163680 blend_line_hv16(p, draw->pixelstep[plane],
611 163680 color->comp[plane].u16[index], alpha,
612 m, mask_linesize, l2depth, w_sub,
613 163680 draw->hsub[plane], draw->vsub[plane],
614 163680 xm0, left, right, 1 << draw->vsub[plane]);
615 163680 p += dst_linesize[plane];
616 163680 m += mask_linesize << draw->vsub[plane];
617 }
618 }
619
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 82578 times.
82758 if (bottom) {
620
1/2
✓ Branch 0 taken 180 times.
✗ Branch 1 not taken.
180 if (depth <= 8) {
621 180 blend_line_hv(p, draw->pixelstep[plane],
622 180 color->comp[plane].u8[index], alpha,
623 m, mask_linesize, l2depth, w_sub,
624 180 draw->hsub[plane], draw->vsub[plane],
625 xm0, left, right, bottom);
626 } else {
627 blend_line_hv16(p, draw->pixelstep[plane],
628 color->comp[plane].u16[index], alpha,
629 m, mask_linesize, l2depth, w_sub,
630 draw->hsub[plane], draw->vsub[plane],
631 xm0, left, right, bottom);
632 }
633 }
634 }
635 }
636 }
637
638 214515 int ff_draw_round_to_sub(FFDrawContext *draw, int sub_dir, int round_dir,
639 int value)
640 {
641
2/2
✓ Branch 0 taken 192794 times.
✓ Branch 1 taken 21721 times.
214515 unsigned shift = sub_dir ? draw->vsub_max : draw->hsub_max;
642
643
2/2
✓ Branch 0 taken 100753 times.
✓ Branch 1 taken 113762 times.
214515 if (!shift)
644 100753 return value;
645
2/2
✓ Branch 0 taken 95151 times.
✓ Branch 1 taken 18611 times.
113762 if (round_dir >= 0)
646
2/2
✓ Branch 0 taken 3115 times.
✓ Branch 1 taken 92036 times.
95151 value += round_dir ? (1 << shift) - 1 : 1 << (shift - 1);
647 113762 return (value >> shift) << shift;
648 }
649
650 149 AVFilterFormats *ff_draw_supported_pixel_formats(unsigned flags)
651 {
652 enum AVPixelFormat i;
653 FFDrawContext draw;
654 149 AVFilterFormats *fmts = NULL;
655 int ret;
656
657
2/2
✓ Branch 1 taken 33078 times.
✓ Branch 2 taken 149 times.
33227 for (i = 0; av_pix_fmt_desc_get(i); i++)
658
3/4
✓ Branch 1 taken 12814 times.
✓ Branch 2 taken 20264 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 12814 times.
45892 if (ff_draw_init(&draw, i, flags) >= 0 &&
659 12814 (ret = ff_add_format(&fmts, i)) < 0)
660 return NULL;
661 149 return fmts;
662 }
663