]> git.sesse.net Git - ffmpeg/blob - libavfilter/drawutils.c
7edaa4ac6c6d481e6beed69a323a11eb526dbbed
[ffmpeg] / libavfilter / drawutils.c
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 int ff_fill_rgba_map(uint8_t *rgba_map, enum AVPixelFormat pix_fmt)
36 {
37     switch (pix_fmt) {
38     case AV_PIX_FMT_0RGB:
39     case AV_PIX_FMT_ARGB:  rgba_map[ALPHA] = 0; rgba_map[RED  ] = 1; rgba_map[GREEN] = 2; rgba_map[BLUE ] = 3; break;
40     case AV_PIX_FMT_0BGR:
41     case AV_PIX_FMT_ABGR:  rgba_map[ALPHA] = 0; rgba_map[BLUE ] = 1; rgba_map[GREEN] = 2; rgba_map[RED  ] = 3; break;
42     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     case AV_PIX_FMT_RGB24: rgba_map[RED  ] = 0; rgba_map[GREEN] = 1; rgba_map[BLUE ] = 2; rgba_map[ALPHA] = 3; break;
49     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     case AV_PIX_FMT_BGR24: rgba_map[BLUE ] = 0; rgba_map[GREEN] = 1; rgba_map[RED  ] = 2; rgba_map[ALPHA] = 3; break;
56     case AV_PIX_FMT_GBRP9:
57     case AV_PIX_FMT_GBRP10:
58     case AV_PIX_FMT_GBRP12:
59     case AV_PIX_FMT_GBRP14:
60     case AV_PIX_FMT_GBRAP:
61     case AV_PIX_FMT_GBRP:  rgba_map[GREEN] = 0; rgba_map[BLUE ] = 1; rgba_map[RED  ] = 2; rgba_map[ALPHA] = 3; break;
62     default:                    /* unsupported */
63         return AVERROR(EINVAL);
64     }
65     return 0;
66 }
67
68 int ff_fill_line_with_color(uint8_t *line[4], int pixel_step[4], int w, uint8_t dst_color[4],
69                             enum AVPixelFormat pix_fmt, uint8_t rgba_color[4],
70                             int *is_packed_rgba, uint8_t rgba_map_ptr[4])
71 {
72     uint8_t rgba_map[4] = {0};
73     int i;
74     const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(pix_fmt);
75     int hsub;
76
77     av_assert0(pix_desc);
78
79     hsub = pix_desc->log2_chroma_w;
80
81     *is_packed_rgba = ff_fill_rgba_map(rgba_map, pix_fmt) >= 0;
82
83     if (*is_packed_rgba) {
84         pixel_step[0] = (av_get_bits_per_pixel(pix_desc))>>3;
85         for (i = 0; i < 4; i++)
86             dst_color[rgba_map[i]] = rgba_color[i];
87
88         line[0] = av_malloc_array(w, pixel_step[0]);
89         if (!line[0])
90             return AVERROR(ENOMEM);
91         for (i = 0; i < w; i++)
92             memcpy(line[0] + i * pixel_step[0], dst_color, pixel_step[0]);
93         if (rgba_map_ptr)
94             memcpy(rgba_map_ptr, rgba_map, sizeof(rgba_map[0]) * 4);
95     } else {
96         int plane;
97
98         dst_color[0] = RGB_TO_Y_CCIR(rgba_color[0], rgba_color[1], rgba_color[2]);
99         dst_color[1] = RGB_TO_U_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
100         dst_color[2] = RGB_TO_V_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
101         dst_color[3] = rgba_color[3];
102
103         for (plane = 0; plane < 4; plane++) {
104             int line_size;
105             int hsub1 = (plane == 1 || plane == 2) ? hsub : 0;
106
107             pixel_step[plane] = 1;
108             line_size = AV_CEIL_RSHIFT(w, hsub1) * pixel_step[plane];
109             line[plane] = av_malloc(line_size);
110             if (!line[plane]) {
111                 while(plane && line[plane-1])
112                     av_freep(&line[--plane]);
113                 return AVERROR(ENOMEM);
114             }
115             memset(line[plane], dst_color[plane], line_size);
116         }
117     }
118
119     return 0;
120 }
121
122 void ff_draw_rectangle(uint8_t *dst[4], int dst_linesize[4],
123                        uint8_t *src[4], int pixelstep[4],
124                        int hsub, int vsub, int x, int y, int w, int h)
125 {
126     int i, plane;
127     uint8_t *p;
128
129     for (plane = 0; plane < 4 && dst[plane]; plane++) {
130         int hsub1 = plane == 1 || plane == 2 ? hsub : 0;
131         int vsub1 = plane == 1 || plane == 2 ? vsub : 0;
132         int width  = AV_CEIL_RSHIFT(w, hsub1);
133         int height = AV_CEIL_RSHIFT(h, vsub1);
134
135         p = dst[plane] + (y >> vsub1) * dst_linesize[plane];
136         for (i = 0; i < height; i++) {
137             memcpy(p + (x >> hsub1) * pixelstep[plane],
138                    src[plane], width * pixelstep[plane]);
139             p += dst_linesize[plane];
140         }
141     }
142 }
143
144 void ff_copy_rectangle(uint8_t *dst[4], int dst_linesize[4],
145                        uint8_t *src[4], int src_linesize[4], int pixelstep[4],
146                        int hsub, int vsub, int x, int y, int y2, int w, int h)
147 {
148     int i, plane;
149     uint8_t *p;
150
151     for (plane = 0; plane < 4 && dst[plane]; plane++) {
152         int hsub1 = plane == 1 || plane == 2 ? hsub : 0;
153         int vsub1 = plane == 1 || plane == 2 ? vsub : 0;
154         int width  = AV_CEIL_RSHIFT(w, hsub1);
155         int height = AV_CEIL_RSHIFT(h, vsub1);
156
157         p = dst[plane] + (y >> vsub1) * dst_linesize[plane];
158         for (i = 0; i < height; i++) {
159             memcpy(p + (x >> hsub1) * pixelstep[plane],
160                    src[plane] + src_linesize[plane]*(i+(y2>>vsub1)), width * pixelstep[plane]);
161             p += dst_linesize[plane];
162         }
163     }
164 }
165
166 int ff_draw_init(FFDrawContext *draw, enum AVPixelFormat format, unsigned flags)
167 {
168     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);
169     const AVComponentDescriptor *c;
170     unsigned i, nb_planes = 0;
171     int pixelstep[MAX_PLANES] = { 0 };
172
173     if (!desc || !desc->name)
174         return AVERROR(EINVAL);
175     if (desc->flags & ~(AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB | AV_PIX_FMT_FLAG_PSEUDOPAL | AV_PIX_FMT_FLAG_ALPHA))
176         return AVERROR(ENOSYS);
177     for (i = 0; i < desc->nb_components; i++) {
178         c = &desc->comp[i];
179         /* for now, only 8-16 bits formats */
180         if (c->depth < 8 || c->depth > 16)
181             return AVERROR(ENOSYS);
182         if (desc->flags & AV_PIX_FMT_FLAG_BE)
183             return AVERROR(ENOSYS);
184         if (c->plane >= MAX_PLANES)
185             return AVERROR(ENOSYS);
186         /* strange interleaving */
187         if (pixelstep[c->plane] != 0 &&
188             pixelstep[c->plane] != c->step)
189             return AVERROR(ENOSYS);
190         if (pixelstep[c->plane] == 6 &&
191             c->depth == 16)
192             return AVERROR(ENOSYS);
193         pixelstep[c->plane] = c->step;
194         if (pixelstep[c->plane] >= 8)
195             return AVERROR(ENOSYS);
196         nb_planes = FFMAX(nb_planes, c->plane + 1);
197     }
198     if ((desc->log2_chroma_w || desc->log2_chroma_h) && nb_planes < 3)
199         return AVERROR(ENOSYS); /* exclude NV12 and NV21 */
200     memset(draw, 0, sizeof(*draw));
201     draw->desc      = desc;
202     draw->format    = format;
203     draw->nb_planes = nb_planes;
204     memcpy(draw->pixelstep, pixelstep, sizeof(draw->pixelstep));
205     draw->hsub[1] = draw->hsub[2] = draw->hsub_max = desc->log2_chroma_w;
206     draw->vsub[1] = draw->vsub[2] = draw->vsub_max = desc->log2_chroma_h;
207     for (i = 0; i < ((desc->nb_components - 1) | 1); i++)
208         draw->comp_mask[desc->comp[i].plane] |=
209             1 << desc->comp[i].offset;
210     return 0;
211 }
212
213 void ff_draw_color(FFDrawContext *draw, FFDrawColor *color, const uint8_t rgba[4])
214 {
215     unsigned i;
216     uint8_t rgba_map[4];
217
218     if (rgba != color->rgba)
219         memcpy(color->rgba, rgba, sizeof(color->rgba));
220     if ((draw->desc->flags & AV_PIX_FMT_FLAG_RGB) &&
221         ff_fill_rgba_map(rgba_map, draw->format) >= 0) {
222         if (draw->nb_planes == 1) {
223             for (i = 0; i < 4; i++) {
224                 color->comp[0].u8[rgba_map[i]] = rgba[i];
225                 if (draw->desc->comp[rgba_map[i]].depth > 8) {
226                     color->comp[0].u16[rgba_map[i]] = color->comp[0].u8[rgba_map[i]] << 8;
227                 }
228             }
229         } else {
230             for (i = 0; i < 4; i++) {
231                 color->comp[rgba_map[i]].u8[0] = rgba[i];
232                 if (draw->desc->comp[rgba_map[i]].depth > 8)
233                     color->comp[rgba_map[i]].u16[0] = color->comp[rgba_map[i]].u8[0] << (draw->desc->comp[rgba_map[i]].depth - 8);
234             }
235         }
236     } else if (draw->nb_planes == 3 || draw->nb_planes == 4) {
237         /* assume YUV */
238         color->comp[0].u8[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
239         color->comp[1].u8[0] = RGB_TO_U_CCIR(rgba[0], rgba[1], rgba[2], 0);
240         color->comp[2].u8[0] = RGB_TO_V_CCIR(rgba[0], rgba[1], rgba[2], 0);
241         color->comp[3].u8[0] = rgba[3];
242         if (draw->desc->comp[0].depth > 8)
243             color->comp[0].u16[0] = color->comp[0].u8[0] << (draw->desc->comp[0].depth - 8);
244         if (draw->desc->comp[1].depth > 8)
245             color->comp[1].u16[0] = color->comp[1].u8[0] << (draw->desc->comp[1].depth - 8);
246         if (draw->desc->comp[2].depth > 8)
247             color->comp[2].u16[0] = color->comp[2].u8[0] << (draw->desc->comp[2].depth - 8);
248         if (draw->desc->comp[3].depth > 8)
249             color->comp[3].u16[0] = color->comp[3].u8[0] << (draw->desc->comp[3].depth - 8);
250     } else if (draw->format == AV_PIX_FMT_GRAY8 || draw->format == AV_PIX_FMT_GRAY8A) {
251         color->comp[0].u8[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
252         color->comp[1].u8[0] = rgba[3];
253     } else if (draw->format == AV_PIX_FMT_GRAY16 || draw->format == AV_PIX_FMT_YA16) {
254         color->comp[0].u8[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
255         color->comp[0].u16[0] = color->comp[0].u8[0] << 8;
256         color->comp[1].u8[0] = rgba[3];
257         color->comp[1].u16[0] = color->comp[1].u8[0] << 8;
258     } else {
259         av_log(NULL, AV_LOG_WARNING,
260                "Color conversion not implemented for %s\n", draw->desc->name);
261         memset(color, 128, sizeof(*color));
262     }
263 }
264
265 static uint8_t *pointer_at(FFDrawContext *draw, uint8_t *data[], int linesize[],
266                            int plane, int x, int y)
267 {
268     return data[plane] +
269            (y >> draw->vsub[plane]) * linesize[plane] +
270            (x >> draw->hsub[plane]) * draw->pixelstep[plane];
271 }
272
273 void ff_copy_rectangle2(FFDrawContext *draw,
274                         uint8_t *dst[], int dst_linesize[],
275                         uint8_t *src[], int src_linesize[],
276                         int dst_x, int dst_y, int src_x, int src_y,
277                         int w, int h)
278 {
279     int plane, y, wp, hp;
280     uint8_t *p, *q;
281
282     for (plane = 0; plane < draw->nb_planes; plane++) {
283         p = pointer_at(draw, src, src_linesize, plane, src_x, src_y);
284         q = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
285         wp = AV_CEIL_RSHIFT(w, draw->hsub[plane]) * draw->pixelstep[plane];
286         hp = AV_CEIL_RSHIFT(h, draw->vsub[plane]);
287         for (y = 0; y < hp; y++) {
288             memcpy(q, p, wp);
289             p += src_linesize[plane];
290             q += dst_linesize[plane];
291         }
292     }
293 }
294
295 void ff_fill_rectangle(FFDrawContext *draw, FFDrawColor *color,
296                        uint8_t *dst[], int dst_linesize[],
297                        int dst_x, int dst_y, int w, int h)
298 {
299     int plane, x, y, wp, hp;
300     uint8_t *p0, *p;
301
302     for (plane = 0; plane < draw->nb_planes; plane++) {
303         p0 = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
304         wp = AV_CEIL_RSHIFT(w, draw->hsub[plane]);
305         hp = AV_CEIL_RSHIFT(h, draw->vsub[plane]);
306         if (!hp)
307             return;
308         p = p0;
309         /* copy first line from color */
310         for (x = 0; x < wp; x++) {
311             memcpy(p, color->comp[plane].u8, draw->pixelstep[plane]);
312             p += draw->pixelstep[plane];
313         }
314         wp *= draw->pixelstep[plane];
315         /* copy next lines from first line */
316         p = p0 + dst_linesize[plane];
317         for (y = 1; y < hp; y++) {
318             memcpy(p, p0, wp);
319             p += dst_linesize[plane];
320         }
321     }
322 }
323
324 /**
325  * Clip interval [x; x+w[ within [0; wmax[.
326  * The resulting w may be negative if the final interval is empty.
327  * dx, if not null, return the difference between in and out value of x.
328  */
329 static void clip_interval(int wmax, int *x, int *w, int *dx)
330 {
331     if (dx)
332         *dx = 0;
333     if (*x < 0) {
334         if (dx)
335             *dx = -*x;
336         *w += *x;
337         *x = 0;
338     }
339     if (*x + *w > wmax)
340         *w = wmax - *x;
341 }
342
343 /**
344  * Decompose w pixels starting at x
345  * into start + (w starting at x) + end
346  * with x and w aligned on multiples of 1<<sub.
347  */
348 static void subsampling_bounds(int sub, int *x, int *w, int *start, int *end)
349 {
350     int mask = (1 << sub) - 1;
351
352     *start = (-*x) & mask;
353     *x += *start;
354     *start = FFMIN(*start, *w);
355     *w -= *start;
356     *end = *w & mask;
357     *w >>= sub;
358 }
359
360 static int component_used(FFDrawContext *draw, int plane, int comp)
361 {
362     return (draw->comp_mask[plane] >> comp) & 1;
363 }
364
365 /* If alpha is in the [ 0 ; 0x1010101 ] range,
366    then alpha * value is in the [ 0 ; 0xFFFFFFFF ] range,
367    and >> 24 gives a correct rounding. */
368 static void blend_line(uint8_t *dst, unsigned src, unsigned alpha,
369                        int dx, int w, unsigned hsub, int left, int right)
370 {
371     unsigned asrc = alpha * src;
372     unsigned tau = 0x1010101 - alpha;
373     int x;
374
375     if (left) {
376         unsigned suba = (left * alpha) >> hsub;
377         *dst = (*dst * (0x1010101 - suba) + src * suba) >> 24;
378         dst += dx;
379     }
380     for (x = 0; x < w; x++) {
381         *dst = (*dst * tau + asrc) >> 24;
382         dst += dx;
383     }
384     if (right) {
385         unsigned suba = (right * alpha) >> hsub;
386         *dst = (*dst * (0x1010101 - suba) + src * suba) >> 24;
387     }
388 }
389
390 static void blend_line16(uint8_t *dst, unsigned src, unsigned alpha,
391                          int dx, int w, unsigned hsub, int left, int right)
392 {
393     unsigned asrc = alpha * src;
394     unsigned tau = 0x10001 - alpha;
395     int x;
396
397     if (left) {
398         unsigned suba = (left * alpha) >> hsub;
399         uint16_t value = AV_RL16(dst);
400         AV_WL16(dst, (value * (0x10001 - suba) + src * suba) >> 16);
401         dst += dx;
402     }
403     for (x = 0; x < w; x++) {
404         uint16_t value = AV_RL16(dst);
405         AV_WL16(dst, (value * tau + asrc) >> 16);
406         dst += dx;
407     }
408     if (right) {
409         unsigned suba = (right * alpha) >> hsub;
410         uint16_t value = AV_RL16(dst);
411         AV_WL16(dst, (value * (0x10001 - suba) + src * suba) >> 16);
412     }
413 }
414
415 void ff_blend_rectangle(FFDrawContext *draw, FFDrawColor *color,
416                         uint8_t *dst[], int dst_linesize[],
417                         int dst_w, int dst_h,
418                         int x0, int y0, int w, int h)
419 {
420     unsigned alpha, nb_planes, nb_comp, plane, comp;
421     int w_sub, h_sub, x_sub, y_sub, left, right, top, bottom, y;
422     uint8_t *p0, *p;
423
424     /* TODO optimize if alpha = 0xFF */
425     clip_interval(dst_w, &x0, &w, NULL);
426     clip_interval(dst_h, &y0, &h, NULL);
427     if (w <= 0 || h <= 0 || !color->rgba[3])
428         return;
429     if (draw->desc->comp[0].depth <= 8) {
430         /* 0x10203 * alpha + 2 is in the [ 2 ; 0x1010101 - 2 ] range */
431         alpha = 0x10203 * color->rgba[3] + 0x2;
432     } else {
433         /* 0x101 * alpha is in the [ 2 ; 0x1001] range */
434         alpha = 0x101 * color->rgba[3] + 0x2;
435     }
436     nb_planes = (draw->nb_planes - 1) | 1; /* eliminate alpha */
437     for (plane = 0; plane < nb_planes; plane++) {
438         nb_comp = draw->pixelstep[plane];
439         p0 = pointer_at(draw, dst, dst_linesize, plane, x0, y0);
440         w_sub = w;
441         h_sub = h;
442         x_sub = x0;
443         y_sub = y0;
444         subsampling_bounds(draw->hsub[plane], &x_sub, &w_sub, &left, &right);
445         subsampling_bounds(draw->vsub[plane], &y_sub, &h_sub, &top, &bottom);
446         for (comp = 0; comp < nb_comp; comp++) {
447             const int depth = draw->desc->comp[comp].depth;
448
449             if (!component_used(draw, plane, comp))
450                 continue;
451             p = p0 + comp;
452             if (top) {
453                 if (depth <= 8) {
454                     blend_line(p, color->comp[plane].u8[comp], alpha >> 1,
455                                draw->pixelstep[plane], w_sub,
456                                draw->hsub[plane], left, right);
457                 } else {
458                     blend_line16(p, color->comp[plane].u16[comp], alpha >> 1,
459                                  draw->pixelstep[plane], w_sub,
460                                  draw->hsub[plane], left, right);
461                 }
462                 p += dst_linesize[plane];
463             }
464             if (depth <= 8) {
465                 for (y = 0; y < h_sub; y++) {
466                     blend_line(p, color->comp[plane].u8[comp], alpha,
467                                draw->pixelstep[plane], w_sub,
468                                draw->hsub[plane], left, right);
469                     p += dst_linesize[plane];
470                 }
471             } else {
472                 for (y = 0; y < h_sub; y++) {
473                     blend_line16(p, color->comp[plane].u16[comp], alpha,
474                                  draw->pixelstep[plane], w_sub,
475                                  draw->hsub[plane], left, right);
476                     p += dst_linesize[plane];
477                 }
478             }
479             if (bottom) {
480                 if (depth <= 8) {
481                     blend_line(p, color->comp[plane].u8[comp], alpha >> 1,
482                                draw->pixelstep[plane], w_sub,
483                                draw->hsub[plane], left, right);
484                 } else {
485                     blend_line16(p, color->comp[plane].u16[comp], alpha >> 1,
486                                  draw->pixelstep[plane], w_sub,
487                                  draw->hsub[plane], left, right);
488                 }
489             }
490         }
491     }
492 }
493
494 static void blend_pixel16(uint8_t *dst, unsigned src, unsigned alpha,
495                           const uint8_t *mask, int mask_linesize, int l2depth,
496                           unsigned w, unsigned h, unsigned shift, unsigned xm0)
497 {
498     unsigned xm, x, y, t = 0;
499     unsigned xmshf = 3 - l2depth;
500     unsigned xmmod = 7 >> l2depth;
501     unsigned mbits = (1 << (1 << l2depth)) - 1;
502     unsigned mmult = 255 / mbits;
503     uint16_t value = AV_RL16(dst);
504
505     for (y = 0; y < h; y++) {
506         xm = xm0;
507         for (x = 0; x < w; x++) {
508             t += ((mask[xm >> xmshf] >> ((~xm & xmmod) << l2depth)) & mbits)
509                  * mmult;
510             xm++;
511         }
512         mask += mask_linesize;
513     }
514     alpha = (t >> shift) * alpha;
515     AV_WL16(dst, ((0x10001 - alpha) * value + alpha * src) >> 16);
516 }
517
518 static void blend_pixel(uint8_t *dst, unsigned src, unsigned alpha,
519                         const uint8_t *mask, int mask_linesize, int l2depth,
520                         unsigned w, unsigned h, unsigned shift, unsigned xm0)
521 {
522     unsigned xm, x, y, t = 0;
523     unsigned xmshf = 3 - l2depth;
524     unsigned xmmod = 7 >> l2depth;
525     unsigned mbits = (1 << (1 << l2depth)) - 1;
526     unsigned mmult = 255 / mbits;
527
528     for (y = 0; y < h; y++) {
529         xm = xm0;
530         for (x = 0; x < w; x++) {
531             t += ((mask[xm >> xmshf] >> ((~xm & xmmod) << l2depth)) & mbits)
532                  * mmult;
533             xm++;
534         }
535         mask += mask_linesize;
536     }
537     alpha = (t >> shift) * alpha;
538     *dst = ((0x1010101 - alpha) * *dst + alpha * src) >> 24;
539 }
540
541 static void blend_line_hv16(uint8_t *dst, int dst_delta,
542                             unsigned src, unsigned alpha,
543                             const uint8_t *mask, int mask_linesize, int l2depth, int w,
544                             unsigned hsub, unsigned vsub,
545                             int xm, int left, int right, int hband)
546 {
547     int x;
548
549     if (left) {
550         blend_pixel16(dst, src, alpha, mask, mask_linesize, l2depth,
551                       left, hband, hsub + vsub, xm);
552         dst += dst_delta;
553         xm += left;
554     }
555     for (x = 0; x < w; x++) {
556         blend_pixel16(dst, src, alpha, mask, mask_linesize, l2depth,
557                       1 << hsub, hband, hsub + vsub, xm);
558         dst += dst_delta;
559         xm += 1 << hsub;
560     }
561     if (right)
562         blend_pixel16(dst, src, alpha, mask, mask_linesize, l2depth,
563                       right, hband, hsub + vsub, xm);
564 }
565
566 static void blend_line_hv(uint8_t *dst, int dst_delta,
567                           unsigned src, unsigned alpha,
568                           const uint8_t *mask, int mask_linesize, int l2depth, int w,
569                           unsigned hsub, unsigned vsub,
570                           int xm, int left, int right, int hband)
571 {
572     int x;
573
574     if (left) {
575         blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
576                     left, hband, hsub + vsub, xm);
577         dst += dst_delta;
578         xm += left;
579     }
580     for (x = 0; x < w; x++) {
581         blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
582                     1 << hsub, hband, hsub + vsub, xm);
583         dst += dst_delta;
584         xm += 1 << hsub;
585     }
586     if (right)
587         blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
588                     right, hband, hsub + vsub, xm);
589 }
590
591 void ff_blend_mask(FFDrawContext *draw, FFDrawColor *color,
592                    uint8_t *dst[], int dst_linesize[], int dst_w, int dst_h,
593                    const uint8_t *mask,  int mask_linesize, int mask_w, int mask_h,
594                    int l2depth, unsigned endianness, int x0, int y0)
595 {
596     unsigned alpha, nb_planes, nb_comp, plane, comp;
597     int xm0, ym0, w_sub, h_sub, x_sub, y_sub, left, right, top, bottom, y;
598     uint8_t *p0, *p;
599     const uint8_t *m;
600
601     clip_interval(dst_w, &x0, &mask_w, &xm0);
602     clip_interval(dst_h, &y0, &mask_h, &ym0);
603     mask += ym0 * mask_linesize;
604     if (mask_w <= 0 || mask_h <= 0 || !color->rgba[3])
605         return;
606     if (draw->desc->comp[0].depth <= 8) {
607         /* alpha is in the [ 0 ; 0x10203 ] range,
608            alpha * mask is in the [ 0 ; 0x1010101 - 4 ] range */
609         alpha = (0x10307 * color->rgba[3] + 0x3) >> 8;
610     } else {
611         alpha = (0x101 * color->rgba[3] + 0x2) >> 8;
612     }
613     nb_planes = (draw->nb_planes - 1) | 1; /* eliminate alpha */
614     for (plane = 0; plane < nb_planes; plane++) {
615         nb_comp = draw->pixelstep[plane];
616         p0 = pointer_at(draw, dst, dst_linesize, plane, x0, y0);
617         w_sub = mask_w;
618         h_sub = mask_h;
619         x_sub = x0;
620         y_sub = y0;
621         subsampling_bounds(draw->hsub[plane], &x_sub, &w_sub, &left, &right);
622         subsampling_bounds(draw->vsub[plane], &y_sub, &h_sub, &top, &bottom);
623         for (comp = 0; comp < nb_comp; comp++) {
624             const int depth = draw->desc->comp[comp].depth;
625
626             if (!component_used(draw, plane, comp))
627                 continue;
628             p = p0 + comp;
629             m = mask;
630             if (top) {
631                 if (depth <= 8) {
632                     blend_line_hv(p, draw->pixelstep[plane],
633                                   color->comp[plane].u8[comp], alpha,
634                                   m, mask_linesize, l2depth, w_sub,
635                                   draw->hsub[plane], draw->vsub[plane],
636                                   xm0, left, right, top);
637                 } else {
638                     blend_line_hv16(p, draw->pixelstep[plane],
639                                     color->comp[plane].u16[comp], alpha,
640                                     m, mask_linesize, l2depth, w_sub,
641                                     draw->hsub[plane], draw->vsub[plane],
642                                     xm0, left, right, top);
643                 }
644                 p += dst_linesize[plane];
645                 m += top * mask_linesize;
646             }
647             if (depth <= 8) {
648                 for (y = 0; y < h_sub; y++) {
649                     blend_line_hv(p, draw->pixelstep[plane],
650                                   color->comp[plane].u8[comp], alpha,
651                                   m, mask_linesize, l2depth, w_sub,
652                                   draw->hsub[plane], draw->vsub[plane],
653                                   xm0, left, right, 1 << draw->vsub[plane]);
654                     p += dst_linesize[plane];
655                     m += mask_linesize << draw->vsub[plane];
656                 }
657             } else {
658                 for (y = 0; y < h_sub; y++) {
659                     blend_line_hv16(p, draw->pixelstep[plane],
660                                     color->comp[plane].u16[comp], alpha,
661                                     m, mask_linesize, l2depth, w_sub,
662                                     draw->hsub[plane], draw->vsub[plane],
663                                     xm0, left, right, 1 << draw->vsub[plane]);
664                     p += dst_linesize[plane];
665                     m += mask_linesize << draw->vsub[plane];
666                 }
667             }
668             if (bottom) {
669                 if (depth <= 8) {
670                     blend_line_hv(p, draw->pixelstep[plane],
671                                   color->comp[plane].u8[comp], alpha,
672                                   m, mask_linesize, l2depth, w_sub,
673                                   draw->hsub[plane], draw->vsub[plane],
674                                   xm0, left, right, bottom);
675                 } else {
676                     blend_line_hv16(p, draw->pixelstep[plane],
677                                     color->comp[plane].u16[comp], alpha,
678                                     m, mask_linesize, l2depth, w_sub,
679                                     draw->hsub[plane], draw->vsub[plane],
680                                     xm0, left, right, bottom);
681                 }
682             }
683         }
684     }
685 }
686
687 int ff_draw_round_to_sub(FFDrawContext *draw, int sub_dir, int round_dir,
688                          int value)
689 {
690     unsigned shift = sub_dir ? draw->vsub_max : draw->hsub_max;
691
692     if (!shift)
693         return value;
694     if (round_dir >= 0)
695         value += round_dir ? (1 << shift) - 1 : 1 << (shift - 1);
696     return (value >> shift) << shift;
697 }
698
699 AVFilterFormats *ff_draw_supported_pixel_formats(unsigned flags)
700 {
701     enum AVPixelFormat i;
702     FFDrawContext draw;
703     AVFilterFormats *fmts = NULL;
704     int ret;
705
706     for (i = 0; av_pix_fmt_desc_get(i); i++)
707         if (ff_draw_init(&draw, i, flags) >= 0 &&
708             (ret = ff_add_format(&fmts, i)) < 0)
709             return NULL;
710     return fmts;
711 }
712
713 #ifdef TEST
714
715 #undef printf
716
717 int main(void)
718 {
719     enum AVPixelFormat f;
720     const AVPixFmtDescriptor *desc;
721     FFDrawContext draw;
722     FFDrawColor color;
723     int r, i;
724
725     for (f = 0; av_pix_fmt_desc_get(f); f++) {
726         desc = av_pix_fmt_desc_get(f);
727         if (!desc->name)
728             continue;
729         printf("Testing %s...%*s", desc->name,
730                (int)(16 - strlen(desc->name)), "");
731         r = ff_draw_init(&draw, f, 0);
732         if (r < 0) {
733             char buf[128];
734             av_strerror(r, buf, sizeof(buf));
735             printf("no: %s\n", buf);
736             continue;
737         }
738         ff_draw_color(&draw, &color, (uint8_t[]) { 1, 0, 0, 1 });
739         for (i = 0; i < sizeof(color); i++)
740             if (((uint8_t *)&color)[i] != 128)
741                 break;
742         if (i == sizeof(color)) {
743             printf("fallback color\n");
744             continue;
745         }
746         printf("ok\n");
747     }
748     return 0;
749 }
750
751 #endif