]> git.sesse.net Git - ffmpeg/blob - libavfilter/drawutils.c
avformat/segment: give a warning message for remove initial_offset option
[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     if (format == AV_PIX_FMT_P010LE || format == AV_PIX_FMT_P010BE)
188         return AVERROR(ENOSYS);
189     for (i = 0; i < desc->nb_components; i++) {
190         c = &desc->comp[i];
191         /* for now, only 8-16 bits formats */
192         if (c->depth < 8 || c->depth > 16)
193             return AVERROR(ENOSYS);
194         if (desc->flags & AV_PIX_FMT_FLAG_BE)
195             return AVERROR(ENOSYS);
196         if (c->plane >= MAX_PLANES)
197             return AVERROR(ENOSYS);
198         /* strange interleaving */
199         if (pixelstep[c->plane] != 0 &&
200             pixelstep[c->plane] != c->step)
201             return AVERROR(ENOSYS);
202         if (pixelstep[c->plane] == 6 &&
203             c->depth == 16)
204             return AVERROR(ENOSYS);
205         pixelstep[c->plane] = c->step;
206         if (pixelstep[c->plane] >= 8)
207             return AVERROR(ENOSYS);
208         nb_planes = FFMAX(nb_planes, c->plane + 1);
209     }
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 - !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA)); 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 >= 2) {
247         /* assume YUV */
248         const AVPixFmtDescriptor *desc = draw->desc;
249         color->comp[desc->comp[0].plane].u8[desc->comp[0].offset] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
250         color->comp[desc->comp[1].plane].u8[desc->comp[1].offset] = RGB_TO_U_CCIR(rgba[0], rgba[1], rgba[2], 0);
251         color->comp[desc->comp[2].plane].u8[desc->comp[2].offset] = RGB_TO_V_CCIR(rgba[0], rgba[1], rgba[2], 0);
252         color->comp[3].u8[0] = rgba[3];
253 #define EXPAND(compn) \
254         if (desc->comp[compn].depth > 8) \
255             color->comp[desc->comp[compn].plane].u16[desc->comp[compn].offset] = \
256             color->comp[desc->comp[compn].plane].u8[desc->comp[compn].offset] << \
257                 (draw->desc->comp[compn].depth + draw->desc->comp[compn].shift - 8)
258         EXPAND(3);
259         EXPAND(2);
260         EXPAND(1);
261         EXPAND(0);
262     } else if (draw->format == AV_PIX_FMT_GRAY8 || draw->format == AV_PIX_FMT_GRAY8A) {
263         color->comp[0].u8[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
264         color->comp[1].u8[0] = rgba[3];
265     } else if (draw->format == AV_PIX_FMT_GRAY16LE || draw->format == AV_PIX_FMT_YA16LE) {
266         color->comp[0].u8[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
267         color->comp[0].u16[0] = color->comp[0].u8[0] << 8;
268         color->comp[1].u8[0] = rgba[3];
269         color->comp[1].u16[0] = color->comp[1].u8[0] << 8;
270     } else {
271         av_log(NULL, AV_LOG_WARNING,
272                "Color conversion not implemented for %s\n", draw->desc->name);
273         memset(color, 128, sizeof(*color));
274     }
275 }
276
277 static uint8_t *pointer_at(FFDrawContext *draw, uint8_t *data[], int linesize[],
278                            int plane, int x, int y)
279 {
280     return data[plane] +
281            (y >> draw->vsub[plane]) * linesize[plane] +
282            (x >> draw->hsub[plane]) * draw->pixelstep[plane];
283 }
284
285 void ff_copy_rectangle2(FFDrawContext *draw,
286                         uint8_t *dst[], int dst_linesize[],
287                         uint8_t *src[], int src_linesize[],
288                         int dst_x, int dst_y, int src_x, int src_y,
289                         int w, int h)
290 {
291     int plane, y, wp, hp;
292     uint8_t *p, *q;
293
294     for (plane = 0; plane < draw->nb_planes; plane++) {
295         p = pointer_at(draw, src, src_linesize, plane, src_x, src_y);
296         q = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
297         wp = AV_CEIL_RSHIFT(w, draw->hsub[plane]) * draw->pixelstep[plane];
298         hp = AV_CEIL_RSHIFT(h, draw->vsub[plane]);
299         for (y = 0; y < hp; y++) {
300             memcpy(q, p, wp);
301             p += src_linesize[plane];
302             q += dst_linesize[plane];
303         }
304     }
305 }
306
307 void ff_fill_rectangle(FFDrawContext *draw, FFDrawColor *color,
308                        uint8_t *dst[], int dst_linesize[],
309                        int dst_x, int dst_y, int w, int h)
310 {
311     int plane, x, y, wp, hp;
312     uint8_t *p0, *p;
313     FFDrawColor color_tmp = *color;
314
315     for (plane = 0; plane < draw->nb_planes; plane++) {
316         p0 = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
317         wp = AV_CEIL_RSHIFT(w, draw->hsub[plane]);
318         hp = AV_CEIL_RSHIFT(h, draw->vsub[plane]);
319         if (!hp)
320             return;
321         p = p0;
322
323         if (HAVE_BIGENDIAN && draw->desc->comp[0].depth > 8) {
324             for (x = 0; 2*x < draw->pixelstep[plane]; x++)
325                 color_tmp.comp[plane].u16[x] = av_bswap16(color_tmp.comp[plane].u16[x]);
326         }
327
328         /* copy first line from color */
329         for (x = 0; x < wp; x++) {
330             memcpy(p, color_tmp.comp[plane].u8, draw->pixelstep[plane]);
331             p += draw->pixelstep[plane];
332         }
333         wp *= draw->pixelstep[plane];
334         /* copy next lines from first line */
335         p = p0 + dst_linesize[plane];
336         for (y = 1; y < hp; y++) {
337             memcpy(p, p0, wp);
338             p += dst_linesize[plane];
339         }
340     }
341 }
342
343 /**
344  * Clip interval [x; x+w[ within [0; wmax[.
345  * The resulting w may be negative if the final interval is empty.
346  * dx, if not null, return the difference between in and out value of x.
347  */
348 static void clip_interval(int wmax, int *x, int *w, int *dx)
349 {
350     if (dx)
351         *dx = 0;
352     if (*x < 0) {
353         if (dx)
354             *dx = -*x;
355         *w += *x;
356         *x = 0;
357     }
358     if (*x + *w > wmax)
359         *w = wmax - *x;
360 }
361
362 /**
363  * Decompose w pixels starting at x
364  * into start + (w starting at x) + end
365  * with x and w aligned on multiples of 1<<sub.
366  */
367 static void subsampling_bounds(int sub, int *x, int *w, int *start, int *end)
368 {
369     int mask = (1 << sub) - 1;
370
371     *start = (-*x) & mask;
372     *x += *start;
373     *start = FFMIN(*start, *w);
374     *w -= *start;
375     *end = *w & mask;
376     *w >>= sub;
377 }
378
379 static int component_used(FFDrawContext *draw, int plane, int comp)
380 {
381     return (draw->comp_mask[plane] >> comp) & 1;
382 }
383
384 /* If alpha is in the [ 0 ; 0x1010101 ] range,
385    then alpha * value is in the [ 0 ; 0xFFFFFFFF ] range,
386    and >> 24 gives a correct rounding. */
387 static void blend_line(uint8_t *dst, unsigned src, unsigned alpha,
388                        int dx, int w, unsigned hsub, int left, int right)
389 {
390     unsigned asrc = alpha * src;
391     unsigned tau = 0x1010101 - alpha;
392     int x;
393
394     if (left) {
395         unsigned suba = (left * alpha) >> hsub;
396         *dst = (*dst * (0x1010101 - suba) + src * suba) >> 24;
397         dst += dx;
398     }
399     for (x = 0; x < w; x++) {
400         *dst = (*dst * tau + asrc) >> 24;
401         dst += dx;
402     }
403     if (right) {
404         unsigned suba = (right * alpha) >> hsub;
405         *dst = (*dst * (0x1010101 - suba) + src * suba) >> 24;
406     }
407 }
408
409 static void blend_line16(uint8_t *dst, unsigned src, unsigned alpha,
410                          int dx, int w, unsigned hsub, int left, int right)
411 {
412     unsigned asrc = alpha * src;
413     unsigned tau = 0x10001 - alpha;
414     int x;
415
416     if (left) {
417         unsigned suba = (left * alpha) >> hsub;
418         uint16_t value = AV_RL16(dst);
419         AV_WL16(dst, (value * (0x10001 - suba) + src * suba) >> 16);
420         dst += dx;
421     }
422     for (x = 0; x < w; x++) {
423         uint16_t value = AV_RL16(dst);
424         AV_WL16(dst, (value * tau + asrc) >> 16);
425         dst += dx;
426     }
427     if (right) {
428         unsigned suba = (right * alpha) >> hsub;
429         uint16_t value = AV_RL16(dst);
430         AV_WL16(dst, (value * (0x10001 - suba) + src * suba) >> 16);
431     }
432 }
433
434 void ff_blend_rectangle(FFDrawContext *draw, FFDrawColor *color,
435                         uint8_t *dst[], int dst_linesize[],
436                         int dst_w, int dst_h,
437                         int x0, int y0, int w, int h)
438 {
439     unsigned alpha, nb_planes, nb_comp, plane, comp;
440     int w_sub, h_sub, x_sub, y_sub, left, right, top, bottom, y;
441     uint8_t *p0, *p;
442
443     /* TODO optimize if alpha = 0xFF */
444     clip_interval(dst_w, &x0, &w, NULL);
445     clip_interval(dst_h, &y0, &h, NULL);
446     if (w <= 0 || h <= 0 || !color->rgba[3])
447         return;
448     if (draw->desc->comp[0].depth <= 8) {
449         /* 0x10203 * alpha + 2 is in the [ 2 ; 0x1010101 - 2 ] range */
450         alpha = 0x10203 * color->rgba[3] + 0x2;
451     } else {
452         /* 0x101 * alpha is in the [ 2 ; 0x1001] range */
453         alpha = 0x101 * color->rgba[3] + 0x2;
454     }
455     nb_planes = draw->nb_planes - !!(draw->desc->flags & AV_PIX_FMT_FLAG_ALPHA);
456     nb_planes += !nb_planes;
457     for (plane = 0; plane < nb_planes; plane++) {
458         nb_comp = draw->pixelstep[plane];
459         p0 = pointer_at(draw, dst, dst_linesize, plane, x0, y0);
460         w_sub = w;
461         h_sub = h;
462         x_sub = x0;
463         y_sub = y0;
464         subsampling_bounds(draw->hsub[plane], &x_sub, &w_sub, &left, &right);
465         subsampling_bounds(draw->vsub[plane], &y_sub, &h_sub, &top, &bottom);
466         for (comp = 0; comp < nb_comp; comp++) {
467             const int depth = draw->desc->comp[comp].depth;
468
469             if (!component_used(draw, plane, comp))
470                 continue;
471             p = p0 + comp;
472             if (top) {
473                 if (depth <= 8) {
474                     blend_line(p, color->comp[plane].u8[comp], alpha >> 1,
475                                draw->pixelstep[plane], w_sub,
476                                draw->hsub[plane], left, right);
477                 } else {
478                     blend_line16(p, color->comp[plane].u16[comp], alpha >> 1,
479                                  draw->pixelstep[plane], w_sub,
480                                  draw->hsub[plane], left, right);
481                 }
482                 p += dst_linesize[plane];
483             }
484             if (depth <= 8) {
485                 for (y = 0; y < h_sub; y++) {
486                     blend_line(p, color->comp[plane].u8[comp], alpha,
487                                draw->pixelstep[plane], w_sub,
488                                draw->hsub[plane], left, right);
489                     p += dst_linesize[plane];
490                 }
491             } else {
492                 for (y = 0; y < h_sub; y++) {
493                     blend_line16(p, color->comp[plane].u16[comp], alpha,
494                                  draw->pixelstep[plane], w_sub,
495                                  draw->hsub[plane], left, right);
496                     p += dst_linesize[plane];
497                 }
498             }
499             if (bottom) {
500                 if (depth <= 8) {
501                     blend_line(p, color->comp[plane].u8[comp], alpha >> 1,
502                                draw->pixelstep[plane], w_sub,
503                                draw->hsub[plane], left, right);
504                 } else {
505                     blend_line16(p, color->comp[plane].u16[comp], alpha >> 1,
506                                  draw->pixelstep[plane], w_sub,
507                                  draw->hsub[plane], left, right);
508                 }
509             }
510         }
511     }
512 }
513
514 static void blend_pixel16(uint8_t *dst, unsigned src, unsigned alpha,
515                           const uint8_t *mask, int mask_linesize, int l2depth,
516                           unsigned w, unsigned h, unsigned shift, unsigned xm0)
517 {
518     unsigned xm, x, y, t = 0;
519     unsigned xmshf = 3 - l2depth;
520     unsigned xmmod = 7 >> l2depth;
521     unsigned mbits = (1 << (1 << l2depth)) - 1;
522     unsigned mmult = 255 / mbits;
523     uint16_t value = AV_RL16(dst);
524
525     for (y = 0; y < h; y++) {
526         xm = xm0;
527         for (x = 0; x < w; x++) {
528             t += ((mask[xm >> xmshf] >> ((~xm & xmmod) << l2depth)) & mbits)
529                  * mmult;
530             xm++;
531         }
532         mask += mask_linesize;
533     }
534     alpha = (t >> shift) * alpha;
535     AV_WL16(dst, ((0x10001 - alpha) * value + alpha * src) >> 16);
536 }
537
538 static void blend_pixel(uint8_t *dst, unsigned src, unsigned alpha,
539                         const uint8_t *mask, int mask_linesize, int l2depth,
540                         unsigned w, unsigned h, unsigned shift, unsigned xm0)
541 {
542     unsigned xm, x, y, t = 0;
543     unsigned xmshf = 3 - l2depth;
544     unsigned xmmod = 7 >> l2depth;
545     unsigned mbits = (1 << (1 << l2depth)) - 1;
546     unsigned mmult = 255 / mbits;
547
548     for (y = 0; y < h; y++) {
549         xm = xm0;
550         for (x = 0; x < w; x++) {
551             t += ((mask[xm >> xmshf] >> ((~xm & xmmod) << l2depth)) & mbits)
552                  * mmult;
553             xm++;
554         }
555         mask += mask_linesize;
556     }
557     alpha = (t >> shift) * alpha;
558     *dst = ((0x1010101 - alpha) * *dst + alpha * src) >> 24;
559 }
560
561 static void blend_line_hv16(uint8_t *dst, int dst_delta,
562                             unsigned src, unsigned alpha,
563                             const uint8_t *mask, int mask_linesize, int l2depth, int w,
564                             unsigned hsub, unsigned vsub,
565                             int xm, int left, int right, int hband)
566 {
567     int x;
568
569     if (left) {
570         blend_pixel16(dst, src, alpha, mask, mask_linesize, l2depth,
571                       left, hband, hsub + vsub, xm);
572         dst += dst_delta;
573         xm += left;
574     }
575     for (x = 0; x < w; x++) {
576         blend_pixel16(dst, src, alpha, mask, mask_linesize, l2depth,
577                       1 << hsub, hband, hsub + vsub, xm);
578         dst += dst_delta;
579         xm += 1 << hsub;
580     }
581     if (right)
582         blend_pixel16(dst, src, alpha, mask, mask_linesize, l2depth,
583                       right, hband, hsub + vsub, xm);
584 }
585
586 static void blend_line_hv(uint8_t *dst, int dst_delta,
587                           unsigned src, unsigned alpha,
588                           const uint8_t *mask, int mask_linesize, int l2depth, int w,
589                           unsigned hsub, unsigned vsub,
590                           int xm, int left, int right, int hband)
591 {
592     int x;
593
594     if (left) {
595         blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
596                     left, hband, hsub + vsub, xm);
597         dst += dst_delta;
598         xm += left;
599     }
600     for (x = 0; x < w; x++) {
601         blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
602                     1 << hsub, hband, hsub + vsub, xm);
603         dst += dst_delta;
604         xm += 1 << hsub;
605     }
606     if (right)
607         blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
608                     right, hband, hsub + vsub, xm);
609 }
610
611 void ff_blend_mask(FFDrawContext *draw, FFDrawColor *color,
612                    uint8_t *dst[], int dst_linesize[], int dst_w, int dst_h,
613                    const uint8_t *mask,  int mask_linesize, int mask_w, int mask_h,
614                    int l2depth, unsigned endianness, int x0, int y0)
615 {
616     unsigned alpha, nb_planes, nb_comp, plane, comp;
617     int xm0, ym0, w_sub, h_sub, x_sub, y_sub, left, right, top, bottom, y;
618     uint8_t *p0, *p;
619     const uint8_t *m;
620
621     clip_interval(dst_w, &x0, &mask_w, &xm0);
622     clip_interval(dst_h, &y0, &mask_h, &ym0);
623     mask += ym0 * mask_linesize;
624     if (mask_w <= 0 || mask_h <= 0 || !color->rgba[3])
625         return;
626     if (draw->desc->comp[0].depth <= 8) {
627         /* alpha is in the [ 0 ; 0x10203 ] range,
628            alpha * mask is in the [ 0 ; 0x1010101 - 4 ] range */
629         alpha = (0x10307 * color->rgba[3] + 0x3) >> 8;
630     } else {
631         alpha = (0x101 * color->rgba[3] + 0x2) >> 8;
632     }
633     nb_planes = draw->nb_planes - !!(draw->desc->flags & AV_PIX_FMT_FLAG_ALPHA);
634     nb_planes += !nb_planes;
635     for (plane = 0; plane < nb_planes; plane++) {
636         nb_comp = draw->pixelstep[plane];
637         p0 = pointer_at(draw, dst, dst_linesize, plane, x0, y0);
638         w_sub = mask_w;
639         h_sub = mask_h;
640         x_sub = x0;
641         y_sub = y0;
642         subsampling_bounds(draw->hsub[plane], &x_sub, &w_sub, &left, &right);
643         subsampling_bounds(draw->vsub[plane], &y_sub, &h_sub, &top, &bottom);
644         for (comp = 0; comp < nb_comp; comp++) {
645             const int depth = draw->desc->comp[comp].depth;
646
647             if (!component_used(draw, plane, comp))
648                 continue;
649             p = p0 + comp;
650             m = mask;
651             if (top) {
652                 if (depth <= 8) {
653                     blend_line_hv(p, draw->pixelstep[plane],
654                                   color->comp[plane].u8[comp], alpha,
655                                   m, mask_linesize, l2depth, w_sub,
656                                   draw->hsub[plane], draw->vsub[plane],
657                                   xm0, left, right, top);
658                 } else {
659                     blend_line_hv16(p, draw->pixelstep[plane],
660                                     color->comp[plane].u16[comp], alpha,
661                                     m, mask_linesize, l2depth, w_sub,
662                                     draw->hsub[plane], draw->vsub[plane],
663                                     xm0, left, right, top);
664                 }
665                 p += dst_linesize[plane];
666                 m += top * mask_linesize;
667             }
668             if (depth <= 8) {
669                 for (y = 0; y < h_sub; y++) {
670                     blend_line_hv(p, draw->pixelstep[plane],
671                                   color->comp[plane].u8[comp], alpha,
672                                   m, mask_linesize, l2depth, w_sub,
673                                   draw->hsub[plane], draw->vsub[plane],
674                                   xm0, left, right, 1 << draw->vsub[plane]);
675                     p += dst_linesize[plane];
676                     m += mask_linesize << draw->vsub[plane];
677                 }
678             } else {
679                 for (y = 0; y < h_sub; y++) {
680                     blend_line_hv16(p, draw->pixelstep[plane],
681                                     color->comp[plane].u16[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             }
689             if (bottom) {
690                 if (depth <= 8) {
691                     blend_line_hv(p, draw->pixelstep[plane],
692                                   color->comp[plane].u8[comp], alpha,
693                                   m, mask_linesize, l2depth, w_sub,
694                                   draw->hsub[plane], draw->vsub[plane],
695                                   xm0, left, right, bottom);
696                 } else {
697                     blend_line_hv16(p, draw->pixelstep[plane],
698                                     color->comp[plane].u16[comp], alpha,
699                                     m, mask_linesize, l2depth, w_sub,
700                                     draw->hsub[plane], draw->vsub[plane],
701                                     xm0, left, right, bottom);
702                 }
703             }
704         }
705     }
706 }
707
708 int ff_draw_round_to_sub(FFDrawContext *draw, int sub_dir, int round_dir,
709                          int value)
710 {
711     unsigned shift = sub_dir ? draw->vsub_max : draw->hsub_max;
712
713     if (!shift)
714         return value;
715     if (round_dir >= 0)
716         value += round_dir ? (1 << shift) - 1 : 1 << (shift - 1);
717     return (value >> shift) << shift;
718 }
719
720 AVFilterFormats *ff_draw_supported_pixel_formats(unsigned flags)
721 {
722     enum AVPixelFormat i;
723     FFDrawContext draw;
724     AVFilterFormats *fmts = NULL;
725     int ret;
726
727     for (i = 0; av_pix_fmt_desc_get(i); i++)
728         if (ff_draw_init(&draw, i, flags) >= 0 &&
729             (ret = ff_add_format(&fmts, i)) < 0)
730             return NULL;
731     return fmts;
732 }