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