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