GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavfilter/drawutils.c Lines: 302 362 83.4 %
Date: 2021-04-14 23:45:22 Branches: 194 238 81.5 %

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/colorspace.h"
27
#include "libavutil/intreadwrite.h"
28
#include "libavutil/mem.h"
29
#include "libavutil/pixdesc.h"
30
#include "drawutils.h"
31
#include "formats.h"
32
33
enum { RED = 0, GREEN, BLUE, ALPHA };
34
35
47733
int ff_fill_rgba_map(uint8_t *rgba_map, enum AVPixelFormat pix_fmt)
36
{
37

47733
    switch (pix_fmt) {
38
7
    case AV_PIX_FMT_0RGB:
39
7
    case AV_PIX_FMT_ARGB:  rgba_map[ALPHA] = 0; rgba_map[RED  ] = 1; rgba_map[GREEN] = 2; rgba_map[BLUE ] = 3; break;
40
5
    case AV_PIX_FMT_0BGR:
41
5
    case AV_PIX_FMT_ABGR:  rgba_map[ALPHA] = 0; rgba_map[BLUE ] = 1; rgba_map[GREEN] = 2; rgba_map[RED  ] = 3; break;
42
47638
    case AV_PIX_FMT_RGB48LE:
43
    case AV_PIX_FMT_RGB48BE:
44
    case AV_PIX_FMT_RGBA64BE:
45
    case AV_PIX_FMT_RGBA64LE:
46
    case AV_PIX_FMT_RGB0:
47
    case AV_PIX_FMT_RGBA:
48
47638
    case AV_PIX_FMT_RGB24: rgba_map[RED  ] = 0; rgba_map[GREEN] = 1; rgba_map[BLUE ] = 2; rgba_map[ALPHA] = 3; break;
49
11
    case AV_PIX_FMT_BGR48LE:
50
    case AV_PIX_FMT_BGR48BE:
51
    case AV_PIX_FMT_BGRA64BE:
52
    case AV_PIX_FMT_BGRA64LE:
53
    case AV_PIX_FMT_BGRA:
54
    case AV_PIX_FMT_BGR0:
55
11
    case AV_PIX_FMT_BGR24: rgba_map[BLUE ] = 0; rgba_map[GREEN] = 1; rgba_map[RED  ] = 2; rgba_map[ALPHA] = 3; break;
56
31
    case AV_PIX_FMT_GBRP9LE:
57
    case AV_PIX_FMT_GBRP9BE:
58
    case AV_PIX_FMT_GBRP10LE:
59
    case AV_PIX_FMT_GBRP10BE:
60
    case AV_PIX_FMT_GBRP12LE:
61
    case AV_PIX_FMT_GBRP12BE:
62
    case AV_PIX_FMT_GBRP14LE:
63
    case AV_PIX_FMT_GBRP14BE:
64
    case AV_PIX_FMT_GBRP16LE:
65
    case AV_PIX_FMT_GBRP16BE:
66
    case AV_PIX_FMT_GBRAP:
67
    case AV_PIX_FMT_GBRAP10LE:
68
    case AV_PIX_FMT_GBRAP10BE:
69
    case AV_PIX_FMT_GBRAP12LE:
70
    case AV_PIX_FMT_GBRAP12BE:
71
    case AV_PIX_FMT_GBRAP16LE:
72
    case AV_PIX_FMT_GBRAP16BE:
73
    case AV_PIX_FMT_GBRPF32LE:
74
    case AV_PIX_FMT_GBRPF32BE:
75
    case AV_PIX_FMT_GBRAPF32LE:
76
    case AV_PIX_FMT_GBRAPF32BE:
77
31
    case AV_PIX_FMT_GBRP:  rgba_map[GREEN] = 0; rgba_map[BLUE ] = 1; rgba_map[RED  ] = 2; rgba_map[ALPHA] = 3; break;
78
41
    default:                    /* unsupported */
79
41
        return AVERROR(EINVAL);
80
    }
81
47692
    return 0;
82
}
83
84
26123
int ff_draw_init(FFDrawContext *draw, enum AVPixelFormat format, unsigned flags)
85
{
86
26123
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);
87
    const AVComponentDescriptor *c;
88
26123
    unsigned i, nb_planes = 0;
89
26123
    int pixelstep[MAX_PLANES] = { 0 };
90
26123
    int full_range = 0;
91
92

26123
    if (!desc || !desc->name)
93
        return AVERROR(EINVAL);
94
26123
    if (desc->flags & ~(AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB | FF_PSEUDOPAL | AV_PIX_FMT_FLAG_ALPHA))
95
12838
        return AVERROR(ENOSYS);
96


13285
    if (format == AV_PIX_FMT_P010LE || format == AV_PIX_FMT_P010BE || format == AV_PIX_FMT_P016LE || format == AV_PIX_FMT_P016BE)
97
262
        return AVERROR(ENOSYS);
98


13023
    if (format == AV_PIX_FMT_YUVJ420P || format == AV_PIX_FMT_YUVJ422P || format == AV_PIX_FMT_YUVJ444P ||
99
12490
        format == AV_PIX_FMT_YUVJ411P || format == AV_PIX_FMT_YUVJ440P)
100
666
        full_range = 1;
101
46232
    for (i = 0; i < desc->nb_components; i++) {
102
35829
        c = &desc->comp[i];
103
        /* for now, only 8-16 bits formats */
104

35829
        if (c->depth < 8 || c->depth > 16)
105
1310
            return AVERROR(ENOSYS);
106
34519
        if (desc->flags & AV_PIX_FMT_FLAG_BE)
107
            return AVERROR(ENOSYS);
108
34519
        if (c->plane >= MAX_PLANES)
109
            return AVERROR(ENOSYS);
110
        /* strange interleaving */
111
34519
        if (pixelstep[c->plane] != 0 &&
112
5704
            pixelstep[c->plane] != c->step)
113
655
            return AVERROR(ENOSYS);
114
33864
        if (pixelstep[c->plane] == 6 &&
115
526
            c->depth == 16)
116
262
            return AVERROR(ENOSYS);
117
33602
        pixelstep[c->plane] = c->step;
118
33602
        if (pixelstep[c->plane] >= 8)
119
393
            return AVERROR(ENOSYS);
120
33209
        nb_planes = FFMAX(nb_planes, c->plane + 1);
121
    }
122
10403
    memset(draw, 0, sizeof(*draw));
123
10403
    draw->desc      = desc;
124
10403
    draw->format    = format;
125
10403
    draw->nb_planes = nb_planes;
126
10403
    draw->flags     = flags;
127
10403
    draw->full_range = full_range;
128
10403
    memcpy(draw->pixelstep, pixelstep, sizeof(draw->pixelstep));
129
10403
    draw->hsub[1] = draw->hsub[2] = draw->hsub_max = desc->log2_chroma_w;
130
10403
    draw->vsub[1] = draw->vsub[2] = draw->vsub_max = desc->log2_chroma_h;
131

39496
    for (i = 0; i < (desc->nb_components - !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA && !(flags & FF_DRAW_PROCESS_ALPHA))); i++)
132
29093
        draw->comp_mask[desc->comp[i].plane] |=
133
29093
            1 << desc->comp[i].offset;
134
10403
    return 0;
135
}
136
137
143811
void ff_draw_color(FFDrawContext *draw, FFDrawColor *color, const uint8_t rgba[4])
138
{
139
    unsigned i;
140
    uint8_t rgba_map[4];
141
142
143811
    if (rgba != color->rgba)
143
143811
        memcpy(color->rgba, rgba, sizeof(color->rgba));
144

191449
    if ((draw->desc->flags & AV_PIX_FMT_FLAG_RGB) &&
145
47638
        ff_fill_rgba_map(rgba_map, draw->format) >= 0) {
146
47637
        if (draw->nb_planes == 1) {
147
238115
            for (i = 0; i < 4; i++) {
148
190492
                color->comp[0].u8[rgba_map[i]] = rgba[i];
149
190492
                if (draw->desc->comp[rgba_map[i]].depth > 8) {
150
                    color->comp[0].u16[rgba_map[i]] = color->comp[0].u8[rgba_map[i]] << 8;
151
                }
152
            }
153
        } else {
154
70
            for (i = 0; i < 4; i++) {
155
56
                color->comp[rgba_map[i]].u8[0] = rgba[i];
156
56
                if (draw->desc->comp[rgba_map[i]].depth > 8)
157
27
                    color->comp[rgba_map[i]].u16[0] = color->comp[rgba_map[i]].u8[0] << (draw->desc->comp[rgba_map[i]].depth - 8);
158
            }
159
        }
160
96174
    } else if (draw->nb_planes >= 2) {
161
        /* assume YUV */
162
96162
        const AVPixFmtDescriptor *desc = draw->desc;
163
96162
        color->comp[desc->comp[0].plane].u8[desc->comp[0].offset] = draw->full_range ? RGB_TO_Y_JPEG(rgba[0], rgba[1], rgba[2]) : RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
164
96162
        color->comp[desc->comp[1].plane].u8[desc->comp[1].offset] = draw->full_range ? RGB_TO_U_JPEG(rgba[0], rgba[1], rgba[2]) : RGB_TO_U_CCIR(rgba[0], rgba[1], rgba[2], 0);
165
96162
        color->comp[desc->comp[2].plane].u8[desc->comp[2].offset] = draw->full_range ? RGB_TO_V_JPEG(rgba[0], rgba[1], rgba[2]) : RGB_TO_V_CCIR(rgba[0], rgba[1], rgba[2], 0);
166
96162
        color->comp[3].u8[0] = rgba[3];
167
#define EXPAND(compn) \
168
        if (desc->comp[compn].depth > 8) \
169
            color->comp[desc->comp[compn].plane].u16[desc->comp[compn].offset] = \
170
            color->comp[desc->comp[compn].plane].u8[desc->comp[compn].offset] << \
171
                (draw->desc->comp[compn].depth + draw->desc->comp[compn].shift - 8)
172
96162
        EXPAND(3);
173
96162
        EXPAND(2);
174
96162
        EXPAND(1);
175
96162
        EXPAND(0);
176

12
    } else if (draw->format == AV_PIX_FMT_GRAY8 || draw->format == AV_PIX_FMT_GRAY8A ||
177

8
               draw->format == AV_PIX_FMT_GRAY16LE || draw->format == AV_PIX_FMT_YA16LE ||
178
6
               draw->format == AV_PIX_FMT_GRAY9LE  ||
179
5
               draw->format == AV_PIX_FMT_GRAY10LE ||
180
4
               draw->format == AV_PIX_FMT_GRAY12LE ||
181
13
               draw->format == AV_PIX_FMT_GRAY14LE) {
182
10
        const AVPixFmtDescriptor *desc = draw->desc;
183
10
        color->comp[0].u8[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
184
10
        EXPAND(0);
185
10
        color->comp[1].u8[0] = rgba[3];
186
10
        EXPAND(1);
187
    } else {
188
2
        av_log(NULL, AV_LOG_WARNING,
189
2
               "Color conversion not implemented for %s\n", draw->desc->name);
190
2
        memset(color, 128, sizeof(*color));
191
    }
192
143811
}
193
194
391682
static uint8_t *pointer_at(FFDrawContext *draw, uint8_t *data[], int linesize[],
195
                           int plane, int x, int y)
196
{
197
391682
    return data[plane] +
198
783364
           (y >> draw->vsub[plane]) * linesize[plane] +
199
391682
           (x >> draw->hsub[plane]) * draw->pixelstep[plane];
200
}
201
202
37
void ff_copy_rectangle2(FFDrawContext *draw,
203
                        uint8_t *dst[], int dst_linesize[],
204
                        uint8_t *src[], int src_linesize[],
205
                        int dst_x, int dst_y, int src_x, int src_y,
206
                        int w, int h)
207
{
208
    int plane, y, wp, hp;
209
    uint8_t *p, *q;
210
211
148
    for (plane = 0; plane < draw->nb_planes; plane++) {
212
111
        p = pointer_at(draw, src, src_linesize, plane, src_x, src_y);
213
111
        q = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
214
111
        wp = AV_CEIL_RSHIFT(w, draw->hsub[plane]) * draw->pixelstep[plane];
215
111
        hp = AV_CEIL_RSHIFT(h, draw->vsub[plane]);
216
21423
        for (y = 0; y < hp; y++) {
217
21312
            memcpy(q, p, wp);
218
21312
            p += src_linesize[plane];
219
21312
            q += dst_linesize[plane];
220
        }
221
    }
222
37
}
223
224
145489
void ff_fill_rectangle(FFDrawContext *draw, FFDrawColor *color,
225
                       uint8_t *dst[], int dst_linesize[],
226
                       int dst_x, int dst_y, int w, int h)
227
{
228
    int plane, x, y, wp, hp;
229
    uint8_t *p0, *p;
230
145489
    FFDrawColor color_tmp = *color;
231
232
487187
    for (plane = 0; plane < draw->nb_planes; plane++) {
233
341698
        p0 = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
234
341698
        wp = AV_CEIL_RSHIFT(w, draw->hsub[plane]);
235
341698
        hp = AV_CEIL_RSHIFT(h, draw->vsub[plane]);
236
341698
        if (!hp)
237
            return;
238
341698
        p = p0;
239
240
        if (HAVE_BIGENDIAN && draw->desc->comp[0].depth > 8) {
241
            for (x = 0; 2*x < draw->pixelstep[plane]; x++)
242
                color_tmp.comp[plane].u16[x] = av_bswap16(color_tmp.comp[plane].u16[x]);
243
        }
244
245
        /* copy first line from color */
246
1762936
        for (x = 0; x < wp; x++) {
247
1421238
            memcpy(p, color_tmp.comp[plane].u8, draw->pixelstep[plane]);
248
1421238
            p += draw->pixelstep[plane];
249
        }
250
341698
        wp *= draw->pixelstep[plane];
251
        /* copy next lines from first line */
252
341698
        p = p0 + dst_linesize[plane];
253
6614788
        for (y = 1; y < hp; y++) {
254
6273090
            memcpy(p, p0, wp);
255
6273090
            p += dst_linesize[plane];
256
        }
257
    }
258
}
259
260
/**
261
 * Clip interval [x; x+w[ within [0; wmax[.
262
 * The resulting w may be negative if the final interval is empty.
263
 * dx, if not null, return the difference between in and out value of x.
264
 */
265
39148
static void clip_interval(int wmax, int *x, int *w, int *dx)
266
{
267
39148
    if (dx)
268
37924
        *dx = 0;
269
39148
    if (*x < 0) {
270
        if (dx)
271
            *dx = -*x;
272
        *w += *x;
273
        *x = 0;
274
    }
275
39148
    if (*x + *w > wmax)
276
        *w = wmax - *x;
277
39148
}
278
279
/**
280
 * Decompose w pixels starting at x
281
 * into start + (w starting at x) + end
282
 * with x and w aligned on multiples of 1<<sub.
283
 */
284
99524
static void subsampling_bounds(int sub, int *x, int *w, int *start, int *end)
285
{
286
99524
    int mask = (1 << sub) - 1;
287
288
99524
    *start = (-*x) & mask;
289
99524
    *x += *start;
290
99524
    *start = FFMIN(*start, *w);
291
99524
    *w -= *start;
292
99524
    *end = *w & mask;
293
99524
    *w >>= sub;
294
99524
}
295
296
71522
static int component_used(FFDrawContext *draw, int plane, int comp)
297
{
298
71522
    return (draw->comp_mask[plane] >> comp) & 1;
299
}
300
301
/* If alpha is in the [ 0 ; 0x1010101 ] range,
302
   then alpha * value is in the [ 0 ; 0xFFFFFFFF ] range,
303
   and >> 24 gives a correct rounding. */
304
44064
static void blend_line(uint8_t *dst, unsigned src, unsigned alpha,
305
                       int dx, int w, unsigned hsub, int left, int right)
306
{
307
44064
    unsigned asrc = alpha * src;
308
44064
    unsigned tau = 0x1010101 - alpha;
309
    int x;
310
311
44064
    if (left) {
312
        unsigned suba = (left * alpha) >> hsub;
313
        *dst = (*dst * (0x1010101 - suba) + src * suba) >> 24;
314
        dst += dx;
315
    }
316
3906864
    for (x = 0; x < w; x++) {
317
3862800
        *dst = (*dst * tau + asrc) >> 24;
318
3862800
        dst += dx;
319
    }
320
44064
    if (right) {
321
        unsigned suba = (right * alpha) >> hsub;
322
        *dst = (*dst * (0x1010101 - suba) + src * suba) >> 24;
323
    }
324
44064
}
325
326
11880
static void blend_line16(uint8_t *dst, unsigned src, unsigned alpha,
327
                         int dx, int w, unsigned hsub, int left, int right)
328
{
329
11880
    unsigned asrc = alpha * src;
330
11880
    unsigned tau = 0x10001 - alpha;
331
    int x;
332
333
11880
    if (left) {
334
        unsigned suba = (left * alpha) >> hsub;
335
        uint16_t value = AV_RL16(dst);
336
        AV_WL16(dst, (value * (0x10001 - suba) + src * suba) >> 16);
337
        dst += dx;
338
    }
339
803880
    for (x = 0; x < w; x++) {
340
792000
        uint16_t value = AV_RL16(dst);
341
792000
        AV_WL16(dst, (value * tau + asrc) >> 16);
342
792000
        dst += dx;
343
    }
344
11880
    if (right) {
345
        unsigned suba = (right * alpha) >> hsub;
346
        uint16_t value = AV_RL16(dst);
347
        AV_WL16(dst, (value * (0x10001 - suba) + src * suba) >> 16);
348
    }
349
11880
}
350
351
612
void ff_blend_rectangle(FFDrawContext *draw, FFDrawColor *color,
352
                        uint8_t *dst[], int dst_linesize[],
353
                        int dst_w, int dst_h,
354
                        int x0, int y0, int w, int h)
355
{
356
    unsigned alpha, nb_planes, nb_comp, plane, comp;
357
    int w_sub, h_sub, x_sub, y_sub, left, right, top, bottom, y;
358
    uint8_t *p0, *p;
359
360
    /* TODO optimize if alpha = 0xFF */
361
612
    clip_interval(dst_w, &x0, &w, NULL);
362
612
    clip_interval(dst_h, &y0, &h, NULL);
363

612
    if (w <= 0 || h <= 0 || !color->rgba[3])
364
        return;
365
612
    if (draw->desc->comp[0].depth <= 8) {
366
        /* 0x10203 * alpha + 2 is in the [ 2 ; 0x1010101 - 2 ] range */
367
502
        alpha = 0x10203 * color->rgba[3] + 0x2;
368
    } else {
369
        /* 0x101 * alpha is in the [ 2 ; 0x1001] range */
370
110
        alpha = 0x101 * color->rgba[3] + 0x2;
371
    }
372

612
    nb_planes = draw->nb_planes - !!(draw->desc->flags & AV_PIX_FMT_FLAG_ALPHA && !(draw->flags & FF_DRAW_PROCESS_ALPHA));
373
612
    nb_planes += !nb_planes;
374
2168
    for (plane = 0; plane < nb_planes; plane++) {
375
1556
        nb_comp = draw->pixelstep[plane];
376
1556
        p0 = pointer_at(draw, dst, dst_linesize, plane, x0, y0);
377
1556
        w_sub = w;
378
1556
        h_sub = h;
379
1556
        x_sub = x0;
380
1556
        y_sub = y0;
381
1556
        subsampling_bounds(draw->hsub[plane], &x_sub, &w_sub, &left, &right);
382
1556
        subsampling_bounds(draw->vsub[plane], &y_sub, &h_sub, &top, &bottom);
383
3792
        for (comp = 0; comp < nb_comp; comp++) {
384
2236
            const int depth = draw->desc->comp[comp].depth;
385
386
2236
            if (!component_used(draw, plane, comp))
387
400
                continue;
388
1836
            p = p0 + comp;
389
1836
            if (top) {
390
                if (depth <= 8) {
391
                    blend_line(p, color->comp[plane].u8[comp], alpha >> 1,
392
                               draw->pixelstep[plane], w_sub,
393
                               draw->hsub[plane], left, right);
394
                } else {
395
                    blend_line16(p, color->comp[plane].u16[comp], alpha >> 1,
396
                                 draw->pixelstep[plane], w_sub,
397
                                 draw->hsub[plane], left, right);
398
                }
399
                p += dst_linesize[plane];
400
            }
401
1836
            if (depth <= 8) {
402
45570
                for (y = 0; y < h_sub; y++) {
403
44064
                    blend_line(p, color->comp[plane].u8[comp], alpha,
404
                               draw->pixelstep[plane], w_sub,
405
44064
                               draw->hsub[plane], left, right);
406
44064
                    p += dst_linesize[plane];
407
                }
408
            } else {
409
12210
                for (y = 0; y < h_sub; y++) {
410
11880
                    blend_line16(p, color->comp[plane].u16[comp], alpha,
411
                                 draw->pixelstep[plane], w_sub,
412
11880
                                 draw->hsub[plane], left, right);
413
11880
                    p += dst_linesize[plane];
414
                }
415
            }
416
1836
            if (bottom) {
417
                if (depth <= 8) {
418
                    blend_line(p, color->comp[plane].u8[comp], alpha >> 1,
419
                               draw->pixelstep[plane], w_sub,
420
                               draw->hsub[plane], left, right);
421
                } else {
422
                    blend_line16(p, color->comp[plane].u16[comp], alpha >> 1,
423
                                 draw->pixelstep[plane], w_sub,
424
                                 draw->hsub[plane], left, right);
425
                }
426
            }
427
        }
428
    }
429
}
430
431
1070080
static void blend_pixel16(uint8_t *dst, unsigned src, unsigned alpha,
432
                          const uint8_t *mask, int mask_linesize, int l2depth,
433
                          unsigned w, unsigned h, unsigned shift, unsigned xm0)
434
{
435
1070080
    unsigned xm, x, y, t = 0;
436
1070080
    unsigned xmshf = 3 - l2depth;
437
1070080
    unsigned xmmod = 7 >> l2depth;
438
1070080
    unsigned mbits = (1 << (1 << l2depth)) - 1;
439
1070080
    unsigned mmult = 255 / mbits;
440
1070080
    uint16_t value = AV_RL16(dst);
441
442
2140160
    for (y = 0; y < h; y++) {
443
1070080
        xm = xm0;
444
2675200
        for (x = 0; x < w; x++) {
445
1605120
            t += ((mask[xm >> xmshf] >> ((~xm & xmmod) << l2depth)) & mbits)
446
1605120
                 * mmult;
447
1605120
            xm++;
448
        }
449
1070080
        mask += mask_linesize;
450
    }
451
1070080
    alpha = (t >> shift) * alpha;
452
1070080
    AV_WL16(dst, ((0x10001 - alpha) * value + alpha * src) >> 16);
453
1070080
}
454
455
5213952
static void blend_pixel(uint8_t *dst, unsigned src, unsigned alpha,
456
                        const uint8_t *mask, int mask_linesize, int l2depth,
457
                        unsigned w, unsigned h, unsigned shift, unsigned xm0)
458
{
459
5213952
    unsigned xm, x, y, t = 0;
460
5213952
    unsigned xmshf = 3 - l2depth;
461
5213952
    unsigned xmmod = 7 >> l2depth;
462
5213952
    unsigned mbits = (1 << (1 << l2depth)) - 1;
463
5213952
    unsigned mmult = 255 / mbits;
464
465
11113728
    for (y = 0; y < h; y++) {
466
5899776
        xm = xm0;
467
13217280
        for (x = 0; x < w; x++) {
468
7317504
            t += ((mask[xm >> xmshf] >> ((~xm & xmmod) << l2depth)) & mbits)
469
7317504
                 * mmult;
470
7317504
            xm++;
471
        }
472
5899776
        mask += mask_linesize;
473
    }
474
5213952
    alpha = (t >> shift) * alpha;
475
5213952
    *dst = ((0x1010101 - alpha) * *dst + alpha * src) >> 24;
476
5213952
}
477
478
163680
static void blend_line_hv16(uint8_t *dst, int dst_delta,
479
                            unsigned src, unsigned alpha,
480
                            const uint8_t *mask, int mask_linesize, int l2depth, int w,
481
                            unsigned hsub, unsigned vsub,
482
                            int xm, int left, int right, int hband)
483
{
484
    int x;
485
486
163680
    if (left) {
487
        blend_pixel16(dst, src, alpha, mask, mask_linesize, l2depth,
488
                      left, hband, hsub + vsub, xm);
489
        dst += dst_delta;
490
        xm += left;
491
    }
492
1233760
    for (x = 0; x < w; x++) {
493
1070080
        blend_pixel16(dst, src, alpha, mask, mask_linesize, l2depth,
494
1070080
                      1 << hsub, hband, hsub + vsub, xm);
495
1070080
        dst += dst_delta;
496
1070080
        xm += 1 << hsub;
497
    }
498
163680
    if (right)
499
        blend_pixel16(dst, src, alpha, mask, mask_linesize, l2depth,
500
                      right, hband, hsub + vsub, xm);
501
163680
}
502
503
606624
static void blend_line_hv(uint8_t *dst, int dst_delta,
504
                          unsigned src, unsigned alpha,
505
                          const uint8_t *mask, int mask_linesize, int l2depth, int w,
506
                          unsigned hsub, unsigned vsub,
507
                          int xm, int left, int right, int hband)
508
{
509
    int x;
510
511
606624
    if (left) {
512
        blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
513
                    left, hband, hsub + vsub, xm);
514
        dst += dst_delta;
515
        xm += left;
516
    }
517
5820576
    for (x = 0; x < w; x++) {
518
5213952
        blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
519
5213952
                    1 << hsub, hband, hsub + vsub, xm);
520
5213952
        dst += dst_delta;
521
5213952
        xm += 1 << hsub;
522
    }
523
606624
    if (right)
524
        blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
525
                    right, hband, hsub + vsub, xm);
526
606624
}
527
528
18962
void ff_blend_mask(FFDrawContext *draw, FFDrawColor *color,
529
                   uint8_t *dst[], int dst_linesize[], int dst_w, int dst_h,
530
                   const uint8_t *mask,  int mask_linesize, int mask_w, int mask_h,
531
                   int l2depth, unsigned endianness, int x0, int y0)
532
{
533
    unsigned alpha, nb_planes, nb_comp, plane, comp;
534
    int xm0, ym0, w_sub, h_sub, x_sub, y_sub, left, right, top, bottom, y;
535
    uint8_t *p0, *p;
536
    const uint8_t *m;
537
538
18962
    clip_interval(dst_w, &x0, &mask_w, &xm0);
539
18962
    clip_interval(dst_h, &y0, &mask_h, &ym0);
540
18962
    mask += ym0 * mask_linesize;
541

18962
    if (mask_w <= 0 || mask_h <= 0 || !color->rgba[3])
542
        return;
543
18962
    if (draw->desc->comp[0].depth <= 8) {
544
        /* alpha is in the [ 0 ; 0x10203 ] range,
545
           alpha * mask is in the [ 0 ; 0x1010101 - 4 ] range */
546
15552
        alpha = (0x10307 * color->rgba[3] + 0x3) >> 8;
547
    } else {
548
3410
        alpha = (0x101 * color->rgba[3] + 0x2) >> 8;
549
    }
550

18962
    nb_planes = draw->nb_planes - !!(draw->desc->flags & AV_PIX_FMT_FLAG_ALPHA && !(draw->flags & FF_DRAW_PROCESS_ALPHA));
551
18962
    nb_planes += !nb_planes;
552
67168
    for (plane = 0; plane < nb_planes; plane++) {
553
48206
        nb_comp = draw->pixelstep[plane];
554
48206
        p0 = pointer_at(draw, dst, dst_linesize, plane, x0, y0);
555
48206
        w_sub = mask_w;
556
48206
        h_sub = mask_h;
557
48206
        x_sub = x0;
558
48206
        y_sub = y0;
559
48206
        subsampling_bounds(draw->hsub[plane], &x_sub, &w_sub, &left, &right);
560
48206
        subsampling_bounds(draw->vsub[plane], &y_sub, &h_sub, &top, &bottom);
561
117492
        for (comp = 0; comp < nb_comp; comp++) {
562
69286
            const int depth = draw->desc->comp[comp].depth;
563
564
69286
            if (!component_used(draw, plane, comp))
565
12400
                continue;
566
56886
            p = p0 + comp;
567
56886
            m = mask;
568
56886
            if (top) {
569
                if (depth <= 8) {
570
                    blend_line_hv(p, draw->pixelstep[plane],
571
                                  color->comp[plane].u8[comp], alpha,
572
                                  m, mask_linesize, l2depth, w_sub,
573
                                  draw->hsub[plane], draw->vsub[plane],
574
                                  xm0, left, right, top);
575
                } else {
576
                    blend_line_hv16(p, draw->pixelstep[plane],
577
                                    color->comp[plane].u16[comp], alpha,
578
                                    m, mask_linesize, l2depth, w_sub,
579
                                    draw->hsub[plane], draw->vsub[plane],
580
                                    xm0, left, right, top);
581
                }
582
                p += dst_linesize[plane];
583
                m += top * mask_linesize;
584
            }
585
56886
            if (depth <= 8) {
586
653280
                for (y = 0; y < h_sub; y++) {
587
606624
                    blend_line_hv(p, draw->pixelstep[plane],
588
606624
                                  color->comp[plane].u8[comp], alpha,
589
                                  m, mask_linesize, l2depth, w_sub,
590
606624
                                  draw->hsub[plane], draw->vsub[plane],
591
606624
                                  xm0, left, right, 1 << draw->vsub[plane]);
592
606624
                    p += dst_linesize[plane];
593
606624
                    m += mask_linesize << draw->vsub[plane];
594
                }
595
            } else {
596
173910
                for (y = 0; y < h_sub; y++) {
597
163680
                    blend_line_hv16(p, draw->pixelstep[plane],
598
163680
                                    color->comp[plane].u16[comp], alpha,
599
                                    m, mask_linesize, l2depth, w_sub,
600
163680
                                    draw->hsub[plane], draw->vsub[plane],
601
163680
                                    xm0, left, right, 1 << draw->vsub[plane]);
602
163680
                    p += dst_linesize[plane];
603
163680
                    m += mask_linesize << draw->vsub[plane];
604
                }
605
            }
606
56886
            if (bottom) {
607
                if (depth <= 8) {
608
                    blend_line_hv(p, draw->pixelstep[plane],
609
                                  color->comp[plane].u8[comp], alpha,
610
                                  m, mask_linesize, l2depth, w_sub,
611
                                  draw->hsub[plane], draw->vsub[plane],
612
                                  xm0, left, right, bottom);
613
                } else {
614
                    blend_line_hv16(p, draw->pixelstep[plane],
615
                                    color->comp[plane].u16[comp], alpha,
616
                                    m, mask_linesize, l2depth, w_sub,
617
                                    draw->hsub[plane], draw->vsub[plane],
618
                                    xm0, left, right, bottom);
619
                }
620
            }
621
        }
622
    }
623
}
624
625
157110
int ff_draw_round_to_sub(FFDrawContext *draw, int sub_dir, int round_dir,
626
                         int value)
627
{
628
157110
    unsigned shift = sub_dir ? draw->vsub_max : draw->hsub_max;
629
630
157110
    if (!shift)
631
97310
        return value;
632
59800
    if (round_dir >= 0)
633
49302
        value += round_dir ? (1 << shift) - 1 : 1 << (shift - 1);
634
59800
    return (value >> shift) << shift;
635
}
636
637
131
AVFilterFormats *ff_draw_supported_pixel_formats(unsigned flags)
638
{
639
    enum AVPixelFormat i;
640
    FFDrawContext draw;
641
131
    AVFilterFormats *fmts = NULL;
642
    int ret;
643
644
26069
    for (i = 0; av_pix_fmt_desc_get(i); i++)
645

36156
        if (ff_draw_init(&draw, i, flags) >= 0 &&
646
10218
            (ret = ff_add_format(&fmts, i)) < 0)
647
            return NULL;
648
131
    return fmts;
649
}