]> git.sesse.net Git - ffmpeg/blob - libavfilter/drawutils.c
avfilter/drawutils: Fix ff_fill_rectangle() on big endian
[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     FFDrawColor color_tmp = *color;
302
303     for (plane = 0; plane < draw->nb_planes; plane++) {
304         p0 = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
305         wp = AV_CEIL_RSHIFT(w, draw->hsub[plane]);
306         hp = AV_CEIL_RSHIFT(h, draw->vsub[plane]);
307         if (!hp)
308             return;
309         p = p0;
310
311         if (HAVE_BIGENDIAN && draw->desc->comp[0].depth > 8) {
312             for (x = 0; 2*x < draw->pixelstep[plane]; x++)
313                 color_tmp.comp[plane].u16[x] = av_bswap16(color_tmp.comp[plane].u16[x]);
314         }
315
316         /* copy first line from color */
317         for (x = 0; x < wp; x++) {
318             memcpy(p, color_tmp.comp[plane].u8, draw->pixelstep[plane]);
319             p += draw->pixelstep[plane];
320         }
321         wp *= draw->pixelstep[plane];
322         /* copy next lines from first line */
323         p = p0 + dst_linesize[plane];
324         for (y = 1; y < hp; y++) {
325             memcpy(p, p0, wp);
326             p += dst_linesize[plane];
327         }
328     }
329 }
330
331 /**
332  * Clip interval [x; x+w[ within [0; wmax[.
333  * The resulting w may be negative if the final interval is empty.
334  * dx, if not null, return the difference between in and out value of x.
335  */
336 static void clip_interval(int wmax, int *x, int *w, int *dx)
337 {
338     if (dx)
339         *dx = 0;
340     if (*x < 0) {
341         if (dx)
342             *dx = -*x;
343         *w += *x;
344         *x = 0;
345     }
346     if (*x + *w > wmax)
347         *w = wmax - *x;
348 }
349
350 /**
351  * Decompose w pixels starting at x
352  * into start + (w starting at x) + end
353  * with x and w aligned on multiples of 1<<sub.
354  */
355 static void subsampling_bounds(int sub, int *x, int *w, int *start, int *end)
356 {
357     int mask = (1 << sub) - 1;
358
359     *start = (-*x) & mask;
360     *x += *start;
361     *start = FFMIN(*start, *w);
362     *w -= *start;
363     *end = *w & mask;
364     *w >>= sub;
365 }
366
367 static int component_used(FFDrawContext *draw, int plane, int comp)
368 {
369     return (draw->comp_mask[plane] >> comp) & 1;
370 }
371
372 /* If alpha is in the [ 0 ; 0x1010101 ] range,
373    then alpha * value is in the [ 0 ; 0xFFFFFFFF ] range,
374    and >> 24 gives a correct rounding. */
375 static void blend_line(uint8_t *dst, unsigned src, unsigned alpha,
376                        int dx, int w, unsigned hsub, int left, int right)
377 {
378     unsigned asrc = alpha * src;
379     unsigned tau = 0x1010101 - alpha;
380     int x;
381
382     if (left) {
383         unsigned suba = (left * alpha) >> hsub;
384         *dst = (*dst * (0x1010101 - suba) + src * suba) >> 24;
385         dst += dx;
386     }
387     for (x = 0; x < w; x++) {
388         *dst = (*dst * tau + asrc) >> 24;
389         dst += dx;
390     }
391     if (right) {
392         unsigned suba = (right * alpha) >> hsub;
393         *dst = (*dst * (0x1010101 - suba) + src * suba) >> 24;
394     }
395 }
396
397 static void blend_line16(uint8_t *dst, unsigned src, unsigned alpha,
398                          int dx, int w, unsigned hsub, int left, int right)
399 {
400     unsigned asrc = alpha * src;
401     unsigned tau = 0x10001 - alpha;
402     int x;
403
404     if (left) {
405         unsigned suba = (left * alpha) >> hsub;
406         uint16_t value = AV_RL16(dst);
407         AV_WL16(dst, (value * (0x10001 - suba) + src * suba) >> 16);
408         dst += dx;
409     }
410     for (x = 0; x < w; x++) {
411         uint16_t value = AV_RL16(dst);
412         AV_WL16(dst, (value * tau + asrc) >> 16);
413         dst += dx;
414     }
415     if (right) {
416         unsigned suba = (right * alpha) >> hsub;
417         uint16_t value = AV_RL16(dst);
418         AV_WL16(dst, (value * (0x10001 - suba) + src * suba) >> 16);
419     }
420 }
421
422 void ff_blend_rectangle(FFDrawContext *draw, FFDrawColor *color,
423                         uint8_t *dst[], int dst_linesize[],
424                         int dst_w, int dst_h,
425                         int x0, int y0, int w, int h)
426 {
427     unsigned alpha, nb_planes, nb_comp, plane, comp;
428     int w_sub, h_sub, x_sub, y_sub, left, right, top, bottom, y;
429     uint8_t *p0, *p;
430
431     /* TODO optimize if alpha = 0xFF */
432     clip_interval(dst_w, &x0, &w, NULL);
433     clip_interval(dst_h, &y0, &h, NULL);
434     if (w <= 0 || h <= 0 || !color->rgba[3])
435         return;
436     if (draw->desc->comp[0].depth <= 8) {
437         /* 0x10203 * alpha + 2 is in the [ 2 ; 0x1010101 - 2 ] range */
438         alpha = 0x10203 * color->rgba[3] + 0x2;
439     } else {
440         /* 0x101 * alpha is in the [ 2 ; 0x1001] range */
441         alpha = 0x101 * color->rgba[3] + 0x2;
442     }
443     nb_planes = (draw->nb_planes - 1) | 1; /* eliminate alpha */
444     for (plane = 0; plane < nb_planes; plane++) {
445         nb_comp = draw->pixelstep[plane];
446         p0 = pointer_at(draw, dst, dst_linesize, plane, x0, y0);
447         w_sub = w;
448         h_sub = h;
449         x_sub = x0;
450         y_sub = y0;
451         subsampling_bounds(draw->hsub[plane], &x_sub, &w_sub, &left, &right);
452         subsampling_bounds(draw->vsub[plane], &y_sub, &h_sub, &top, &bottom);
453         for (comp = 0; comp < nb_comp; comp++) {
454             const int depth = draw->desc->comp[comp].depth;
455
456             if (!component_used(draw, plane, comp))
457                 continue;
458             p = p0 + comp;
459             if (top) {
460                 if (depth <= 8) {
461                     blend_line(p, color->comp[plane].u8[comp], alpha >> 1,
462                                draw->pixelstep[plane], w_sub,
463                                draw->hsub[plane], left, right);
464                 } else {
465                     blend_line16(p, color->comp[plane].u16[comp], alpha >> 1,
466                                  draw->pixelstep[plane], w_sub,
467                                  draw->hsub[plane], left, right);
468                 }
469                 p += dst_linesize[plane];
470             }
471             if (depth <= 8) {
472                 for (y = 0; y < h_sub; y++) {
473                     blend_line(p, color->comp[plane].u8[comp], alpha,
474                                draw->pixelstep[plane], w_sub,
475                                draw->hsub[plane], left, right);
476                     p += dst_linesize[plane];
477                 }
478             } else {
479                 for (y = 0; y < h_sub; y++) {
480                     blend_line16(p, color->comp[plane].u16[comp], alpha,
481                                  draw->pixelstep[plane], w_sub,
482                                  draw->hsub[plane], left, right);
483                     p += dst_linesize[plane];
484                 }
485             }
486             if (bottom) {
487                 if (depth <= 8) {
488                     blend_line(p, color->comp[plane].u8[comp], alpha >> 1,
489                                draw->pixelstep[plane], w_sub,
490                                draw->hsub[plane], left, right);
491                 } else {
492                     blend_line16(p, color->comp[plane].u16[comp], alpha >> 1,
493                                  draw->pixelstep[plane], w_sub,
494                                  draw->hsub[plane], left, right);
495                 }
496             }
497         }
498     }
499 }
500
501 static void blend_pixel16(uint8_t *dst, unsigned src, unsigned alpha,
502                           const uint8_t *mask, int mask_linesize, int l2depth,
503                           unsigned w, unsigned h, unsigned shift, unsigned xm0)
504 {
505     unsigned xm, x, y, t = 0;
506     unsigned xmshf = 3 - l2depth;
507     unsigned xmmod = 7 >> l2depth;
508     unsigned mbits = (1 << (1 << l2depth)) - 1;
509     unsigned mmult = 255 / mbits;
510     uint16_t value = AV_RL16(dst);
511
512     for (y = 0; y < h; y++) {
513         xm = xm0;
514         for (x = 0; x < w; x++) {
515             t += ((mask[xm >> xmshf] >> ((~xm & xmmod) << l2depth)) & mbits)
516                  * mmult;
517             xm++;
518         }
519         mask += mask_linesize;
520     }
521     alpha = (t >> shift) * alpha;
522     AV_WL16(dst, ((0x10001 - alpha) * value + alpha * src) >> 16);
523 }
524
525 static void blend_pixel(uint8_t *dst, unsigned src, unsigned alpha,
526                         const uint8_t *mask, int mask_linesize, int l2depth,
527                         unsigned w, unsigned h, unsigned shift, unsigned xm0)
528 {
529     unsigned xm, x, y, t = 0;
530     unsigned xmshf = 3 - l2depth;
531     unsigned xmmod = 7 >> l2depth;
532     unsigned mbits = (1 << (1 << l2depth)) - 1;
533     unsigned mmult = 255 / mbits;
534
535     for (y = 0; y < h; y++) {
536         xm = xm0;
537         for (x = 0; x < w; x++) {
538             t += ((mask[xm >> xmshf] >> ((~xm & xmmod) << l2depth)) & mbits)
539                  * mmult;
540             xm++;
541         }
542         mask += mask_linesize;
543     }
544     alpha = (t >> shift) * alpha;
545     *dst = ((0x1010101 - alpha) * *dst + alpha * src) >> 24;
546 }
547
548 static void blend_line_hv16(uint8_t *dst, int dst_delta,
549                             unsigned src, unsigned alpha,
550                             const uint8_t *mask, int mask_linesize, int l2depth, int w,
551                             unsigned hsub, unsigned vsub,
552                             int xm, int left, int right, int hband)
553 {
554     int x;
555
556     if (left) {
557         blend_pixel16(dst, src, alpha, mask, mask_linesize, l2depth,
558                       left, hband, hsub + vsub, xm);
559         dst += dst_delta;
560         xm += left;
561     }
562     for (x = 0; x < w; x++) {
563         blend_pixel16(dst, src, alpha, mask, mask_linesize, l2depth,
564                       1 << hsub, hband, hsub + vsub, xm);
565         dst += dst_delta;
566         xm += 1 << hsub;
567     }
568     if (right)
569         blend_pixel16(dst, src, alpha, mask, mask_linesize, l2depth,
570                       right, hband, hsub + vsub, xm);
571 }
572
573 static void blend_line_hv(uint8_t *dst, int dst_delta,
574                           unsigned src, unsigned alpha,
575                           const uint8_t *mask, int mask_linesize, int l2depth, int w,
576                           unsigned hsub, unsigned vsub,
577                           int xm, int left, int right, int hband)
578 {
579     int x;
580
581     if (left) {
582         blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
583                     left, hband, hsub + vsub, xm);
584         dst += dst_delta;
585         xm += left;
586     }
587     for (x = 0; x < w; x++) {
588         blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
589                     1 << hsub, hband, hsub + vsub, xm);
590         dst += dst_delta;
591         xm += 1 << hsub;
592     }
593     if (right)
594         blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
595                     right, hband, hsub + vsub, xm);
596 }
597
598 void ff_blend_mask(FFDrawContext *draw, FFDrawColor *color,
599                    uint8_t *dst[], int dst_linesize[], int dst_w, int dst_h,
600                    const uint8_t *mask,  int mask_linesize, int mask_w, int mask_h,
601                    int l2depth, unsigned endianness, int x0, int y0)
602 {
603     unsigned alpha, nb_planes, nb_comp, plane, comp;
604     int xm0, ym0, w_sub, h_sub, x_sub, y_sub, left, right, top, bottom, y;
605     uint8_t *p0, *p;
606     const uint8_t *m;
607
608     clip_interval(dst_w, &x0, &mask_w, &xm0);
609     clip_interval(dst_h, &y0, &mask_h, &ym0);
610     mask += ym0 * mask_linesize;
611     if (mask_w <= 0 || mask_h <= 0 || !color->rgba[3])
612         return;
613     if (draw->desc->comp[0].depth <= 8) {
614         /* alpha is in the [ 0 ; 0x10203 ] range,
615            alpha * mask is in the [ 0 ; 0x1010101 - 4 ] range */
616         alpha = (0x10307 * color->rgba[3] + 0x3) >> 8;
617     } else {
618         alpha = (0x101 * color->rgba[3] + 0x2) >> 8;
619     }
620     nb_planes = (draw->nb_planes - 1) | 1; /* eliminate alpha */
621     for (plane = 0; plane < nb_planes; plane++) {
622         nb_comp = draw->pixelstep[plane];
623         p0 = pointer_at(draw, dst, dst_linesize, plane, x0, y0);
624         w_sub = mask_w;
625         h_sub = mask_h;
626         x_sub = x0;
627         y_sub = y0;
628         subsampling_bounds(draw->hsub[plane], &x_sub, &w_sub, &left, &right);
629         subsampling_bounds(draw->vsub[plane], &y_sub, &h_sub, &top, &bottom);
630         for (comp = 0; comp < nb_comp; comp++) {
631             const int depth = draw->desc->comp[comp].depth;
632
633             if (!component_used(draw, plane, comp))
634                 continue;
635             p = p0 + comp;
636             m = mask;
637             if (top) {
638                 if (depth <= 8) {
639                     blend_line_hv(p, draw->pixelstep[plane],
640                                   color->comp[plane].u8[comp], alpha,
641                                   m, mask_linesize, l2depth, w_sub,
642                                   draw->hsub[plane], draw->vsub[plane],
643                                   xm0, left, right, top);
644                 } else {
645                     blend_line_hv16(p, draw->pixelstep[plane],
646                                     color->comp[plane].u16[comp], alpha,
647                                     m, mask_linesize, l2depth, w_sub,
648                                     draw->hsub[plane], draw->vsub[plane],
649                                     xm0, left, right, top);
650                 }
651                 p += dst_linesize[plane];
652                 m += top * mask_linesize;
653             }
654             if (depth <= 8) {
655                 for (y = 0; y < h_sub; y++) {
656                     blend_line_hv(p, draw->pixelstep[plane],
657                                   color->comp[plane].u8[comp], alpha,
658                                   m, mask_linesize, l2depth, w_sub,
659                                   draw->hsub[plane], draw->vsub[plane],
660                                   xm0, left, right, 1 << draw->vsub[plane]);
661                     p += dst_linesize[plane];
662                     m += mask_linesize << draw->vsub[plane];
663                 }
664             } else {
665                 for (y = 0; y < h_sub; y++) {
666                     blend_line_hv16(p, draw->pixelstep[plane],
667                                     color->comp[plane].u16[comp], alpha,
668                                     m, mask_linesize, l2depth, w_sub,
669                                     draw->hsub[plane], draw->vsub[plane],
670                                     xm0, left, right, 1 << draw->vsub[plane]);
671                     p += dst_linesize[plane];
672                     m += mask_linesize << draw->vsub[plane];
673                 }
674             }
675             if (bottom) {
676                 if (depth <= 8) {
677                     blend_line_hv(p, draw->pixelstep[plane],
678                                   color->comp[plane].u8[comp], alpha,
679                                   m, mask_linesize, l2depth, w_sub,
680                                   draw->hsub[plane], draw->vsub[plane],
681                                   xm0, left, right, bottom);
682                 } else {
683                     blend_line_hv16(p, draw->pixelstep[plane],
684                                     color->comp[plane].u16[comp], alpha,
685                                     m, mask_linesize, l2depth, w_sub,
686                                     draw->hsub[plane], draw->vsub[plane],
687                                     xm0, left, right, bottom);
688                 }
689             }
690         }
691     }
692 }
693
694 int ff_draw_round_to_sub(FFDrawContext *draw, int sub_dir, int round_dir,
695                          int value)
696 {
697     unsigned shift = sub_dir ? draw->vsub_max : draw->hsub_max;
698
699     if (!shift)
700         return value;
701     if (round_dir >= 0)
702         value += round_dir ? (1 << shift) - 1 : 1 << (shift - 1);
703     return (value >> shift) << shift;
704 }
705
706 AVFilterFormats *ff_draw_supported_pixel_formats(unsigned flags)
707 {
708     enum AVPixelFormat i;
709     FFDrawContext draw;
710     AVFilterFormats *fmts = NULL;
711     int ret;
712
713     for (i = 0; av_pix_fmt_desc_get(i); i++)
714         if (ff_draw_init(&draw, i, flags) >= 0 &&
715             (ret = ff_add_format(&fmts, i)) < 0)
716             return NULL;
717     return fmts;
718 }
719
720 #ifdef TEST
721
722 #undef printf
723
724 int main(void)
725 {
726     enum AVPixelFormat f;
727     const AVPixFmtDescriptor *desc;
728     FFDrawContext draw;
729     FFDrawColor color;
730     int r, i;
731
732     for (f = 0; av_pix_fmt_desc_get(f); f++) {
733         desc = av_pix_fmt_desc_get(f);
734         if (!desc->name)
735             continue;
736         printf("Testing %s...%*s", desc->name,
737                (int)(16 - strlen(desc->name)), "");
738         r = ff_draw_init(&draw, f, 0);
739         if (r < 0) {
740             char buf[128];
741             av_strerror(r, buf, sizeof(buf));
742             printf("no: %s\n", buf);
743             continue;
744         }
745         ff_draw_color(&draw, &color, (uint8_t[]) { 1, 0, 0, 1 });
746         for (i = 0; i < sizeof(color); i++)
747             if (((uint8_t *)&color)[i] != 128)
748                 break;
749         if (i == sizeof(color)) {
750             printf("fallback color\n");
751             continue;
752         }
753         printf("ok\n");
754     }
755     return 0;
756 }
757
758 #endif