2 * Copyright 2011 Stefano Sabatini <stefano.sabatini-lala poste it>
3 * Copyright 2012 Nicolas George <nicolas.george normalesup org>
5 * This file is part of FFmpeg.
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.
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.
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
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"
33 enum { RED = 0, GREEN, BLUE, ALPHA };
35 int ff_fill_rgba_map(uint8_t *rgba_map, enum AVPixelFormat pix_fmt)
39 case AV_PIX_FMT_ARGB: rgba_map[ALPHA] = 0; rgba_map[RED ] = 1; rgba_map[GREEN] = 2; rgba_map[BLUE ] = 3; break;
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:
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:
55 case AV_PIX_FMT_BGR24: rgba_map[BLUE ] = 0; rgba_map[GREEN] = 1; rgba_map[RED ] = 2; rgba_map[ALPHA] = 3; break;
56 case AV_PIX_FMT_GBRP9:
57 case AV_PIX_FMT_GBRP10:
58 case AV_PIX_FMT_GBRP12:
59 case AV_PIX_FMT_GBRP14:
60 case AV_PIX_FMT_GBRAP:
61 case AV_PIX_FMT_GBRP: rgba_map[GREEN] = 0; rgba_map[BLUE ] = 1; rgba_map[RED ] = 2; rgba_map[ALPHA] = 3; break;
62 default: /* unsupported */
63 return AVERROR(EINVAL);
68 int ff_fill_line_with_color(uint8_t *line[4], int pixel_step[4], int w, uint8_t dst_color[4],
69 enum AVPixelFormat pix_fmt, uint8_t rgba_color[4],
70 int *is_packed_rgba, uint8_t rgba_map_ptr[4])
72 uint8_t rgba_map[4] = {0};
74 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(pix_fmt);
79 hsub = pix_desc->log2_chroma_w;
81 *is_packed_rgba = ff_fill_rgba_map(rgba_map, pix_fmt) >= 0;
83 if (*is_packed_rgba) {
84 pixel_step[0] = (av_get_bits_per_pixel(pix_desc))>>3;
85 for (i = 0; i < 4; i++)
86 dst_color[rgba_map[i]] = rgba_color[i];
88 line[0] = av_malloc_array(w, pixel_step[0]);
90 return AVERROR(ENOMEM);
91 for (i = 0; i < w; i++)
92 memcpy(line[0] + i * pixel_step[0], dst_color, pixel_step[0]);
94 memcpy(rgba_map_ptr, rgba_map, sizeof(rgba_map[0]) * 4);
98 dst_color[0] = RGB_TO_Y_CCIR(rgba_color[0], rgba_color[1], rgba_color[2]);
99 dst_color[1] = RGB_TO_U_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
100 dst_color[2] = RGB_TO_V_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
101 dst_color[3] = rgba_color[3];
103 for (plane = 0; plane < 4; plane++) {
105 int hsub1 = (plane == 1 || plane == 2) ? hsub : 0;
107 pixel_step[plane] = 1;
108 line_size = AV_CEIL_RSHIFT(w, hsub1) * pixel_step[plane];
109 line[plane] = av_malloc(line_size);
111 while(plane && line[plane-1])
112 av_freep(&line[--plane]);
113 return AVERROR(ENOMEM);
115 memset(line[plane], dst_color[plane], line_size);
122 void ff_draw_rectangle(uint8_t *dst[4], int dst_linesize[4],
123 uint8_t *src[4], int pixelstep[4],
124 int hsub, int vsub, int x, int y, int w, int h)
129 for (plane = 0; plane < 4 && dst[plane]; plane++) {
130 int hsub1 = plane == 1 || plane == 2 ? hsub : 0;
131 int vsub1 = plane == 1 || plane == 2 ? vsub : 0;
132 int width = AV_CEIL_RSHIFT(w, hsub1);
133 int height = AV_CEIL_RSHIFT(h, vsub1);
135 p = dst[plane] + (y >> vsub1) * dst_linesize[plane];
136 for (i = 0; i < height; i++) {
137 memcpy(p + (x >> hsub1) * pixelstep[plane],
138 src[plane], width * pixelstep[plane]);
139 p += dst_linesize[plane];
144 void ff_copy_rectangle(uint8_t *dst[4], int dst_linesize[4],
145 uint8_t *src[4], int src_linesize[4], int pixelstep[4],
146 int hsub, int vsub, int x, int y, int y2, int w, int h)
151 for (plane = 0; plane < 4 && dst[plane]; plane++) {
152 int hsub1 = plane == 1 || plane == 2 ? hsub : 0;
153 int vsub1 = plane == 1 || plane == 2 ? vsub : 0;
154 int width = AV_CEIL_RSHIFT(w, hsub1);
155 int height = AV_CEIL_RSHIFT(h, vsub1);
157 p = dst[plane] + (y >> vsub1) * dst_linesize[plane];
158 for (i = 0; i < height; i++) {
159 memcpy(p + (x >> hsub1) * pixelstep[plane],
160 src[plane] + src_linesize[plane]*(i+(y2>>vsub1)), width * pixelstep[plane]);
161 p += dst_linesize[plane];
166 int ff_draw_init(FFDrawContext *draw, enum AVPixelFormat format, unsigned flags)
168 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);
169 const AVComponentDescriptor *c;
170 unsigned i, nb_planes = 0;
171 int pixelstep[MAX_PLANES] = { 0 };
173 if (!desc || !desc->name)
174 return AVERROR(EINVAL);
175 if (desc->flags & ~(AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB | AV_PIX_FMT_FLAG_PSEUDOPAL | AV_PIX_FMT_FLAG_ALPHA))
176 return AVERROR(ENOSYS);
177 for (i = 0; i < desc->nb_components; i++) {
179 /* for now, only 8-16 bits formats */
180 if (c->depth < 8 || c->depth > 16)
181 return AVERROR(ENOSYS);
182 if (desc->flags & AV_PIX_FMT_FLAG_BE)
183 return AVERROR(ENOSYS);
184 if (c->plane >= MAX_PLANES)
185 return AVERROR(ENOSYS);
186 /* strange interleaving */
187 if (pixelstep[c->plane] != 0 &&
188 pixelstep[c->plane] != c->step)
189 return AVERROR(ENOSYS);
190 if (pixelstep[c->plane] == 6 &&
192 return AVERROR(ENOSYS);
193 pixelstep[c->plane] = c->step;
194 if (pixelstep[c->plane] >= 8)
195 return AVERROR(ENOSYS);
196 nb_planes = FFMAX(nb_planes, c->plane + 1);
198 if ((desc->log2_chroma_w || desc->log2_chroma_h) && nb_planes < 3)
199 return AVERROR(ENOSYS); /* exclude NV12 and NV21 */
200 memset(draw, 0, sizeof(*draw));
202 draw->format = format;
203 draw->nb_planes = nb_planes;
204 memcpy(draw->pixelstep, pixelstep, sizeof(draw->pixelstep));
205 draw->hsub[1] = draw->hsub[2] = draw->hsub_max = desc->log2_chroma_w;
206 draw->vsub[1] = draw->vsub[2] = draw->vsub_max = desc->log2_chroma_h;
207 for (i = 0; i < ((desc->nb_components - 1) | 1); i++)
208 draw->comp_mask[desc->comp[i].plane] |=
209 1 << desc->comp[i].offset;
213 void ff_draw_color(FFDrawContext *draw, FFDrawColor *color, const uint8_t rgba[4])
218 if (rgba != color->rgba)
219 memcpy(color->rgba, rgba, sizeof(color->rgba));
220 if ((draw->desc->flags & AV_PIX_FMT_FLAG_RGB) &&
221 ff_fill_rgba_map(rgba_map, draw->format) >= 0) {
222 if (draw->nb_planes == 1) {
223 for (i = 0; i < 4; i++) {
224 color->comp[0].u8[rgba_map[i]] = rgba[i];
225 if (draw->desc->comp[rgba_map[i]].depth > 8) {
226 color->comp[0].u16[rgba_map[i]] = color->comp[0].u8[rgba_map[i]] << 8;
230 for (i = 0; i < 4; i++) {
231 color->comp[rgba_map[i]].u8[0] = rgba[i];
232 if (draw->desc->comp[rgba_map[i]].depth > 8)
233 color->comp[rgba_map[i]].u16[0] = color->comp[rgba_map[i]].u8[0] << (draw->desc->comp[rgba_map[i]].depth - 8);
236 } else if (draw->nb_planes == 3 || draw->nb_planes == 4) {
238 color->comp[0].u8[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
239 color->comp[1].u8[0] = RGB_TO_U_CCIR(rgba[0], rgba[1], rgba[2], 0);
240 color->comp[2].u8[0] = RGB_TO_V_CCIR(rgba[0], rgba[1], rgba[2], 0);
241 color->comp[3].u8[0] = rgba[3];
242 if (draw->desc->comp[0].depth > 8)
243 color->comp[0].u16[0] = color->comp[0].u8[0] << (draw->desc->comp[0].depth - 8);
244 if (draw->desc->comp[1].depth > 8)
245 color->comp[1].u16[0] = color->comp[1].u8[0] << (draw->desc->comp[1].depth - 8);
246 if (draw->desc->comp[2].depth > 8)
247 color->comp[2].u16[0] = color->comp[2].u8[0] << (draw->desc->comp[2].depth - 8);
248 if (draw->desc->comp[3].depth > 8)
249 color->comp[3].u16[0] = color->comp[3].u8[0] << (draw->desc->comp[3].depth - 8);
250 } else if (draw->format == AV_PIX_FMT_GRAY8 || draw->format == AV_PIX_FMT_GRAY8A) {
251 color->comp[0].u8[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
252 color->comp[1].u8[0] = rgba[3];
253 } else if (draw->format == AV_PIX_FMT_GRAY16 || draw->format == AV_PIX_FMT_YA16) {
254 color->comp[0].u8[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
255 color->comp[0].u16[0] = color->comp[0].u8[0] << 8;
256 color->comp[1].u8[0] = rgba[3];
257 color->comp[1].u16[0] = color->comp[1].u8[0] << 8;
259 av_log(NULL, AV_LOG_WARNING,
260 "Color conversion not implemented for %s\n", draw->desc->name);
261 memset(color, 128, sizeof(*color));
265 static uint8_t *pointer_at(FFDrawContext *draw, uint8_t *data[], int linesize[],
266 int plane, int x, int y)
269 (y >> draw->vsub[plane]) * linesize[plane] +
270 (x >> draw->hsub[plane]) * draw->pixelstep[plane];
273 void ff_copy_rectangle2(FFDrawContext *draw,
274 uint8_t *dst[], int dst_linesize[],
275 uint8_t *src[], int src_linesize[],
276 int dst_x, int dst_y, int src_x, int src_y,
279 int plane, y, wp, hp;
282 for (plane = 0; plane < draw->nb_planes; plane++) {
283 p = pointer_at(draw, src, src_linesize, plane, src_x, src_y);
284 q = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
285 wp = AV_CEIL_RSHIFT(w, draw->hsub[plane]) * draw->pixelstep[plane];
286 hp = AV_CEIL_RSHIFT(h, draw->vsub[plane]);
287 for (y = 0; y < hp; y++) {
289 p += src_linesize[plane];
290 q += dst_linesize[plane];
295 void ff_fill_rectangle(FFDrawContext *draw, FFDrawColor *color,
296 uint8_t *dst[], int dst_linesize[],
297 int dst_x, int dst_y, int w, int h)
299 int plane, x, y, wp, hp;
301 FFDrawColor color_tmp = *color;
303 for (plane = 0; plane < draw->nb_planes; plane++) {
304 p0 = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
305 wp = AV_CEIL_RSHIFT(w, draw->hsub[plane]);
306 hp = AV_CEIL_RSHIFT(h, draw->vsub[plane]);
311 if (HAVE_BIGENDIAN && draw->desc->comp[0].depth > 8) {
312 for (x = 0; 2*x < draw->pixelstep[plane]; x++)
313 color_tmp.comp[plane].u16[x] = av_bswap16(color_tmp.comp[plane].u16[x]);
316 /* copy first line from color */
317 for (x = 0; x < wp; x++) {
318 memcpy(p, color_tmp.comp[plane].u8, draw->pixelstep[plane]);
319 p += draw->pixelstep[plane];
321 wp *= draw->pixelstep[plane];
322 /* copy next lines from first line */
323 p = p0 + dst_linesize[plane];
324 for (y = 1; y < hp; y++) {
326 p += dst_linesize[plane];
332 * Clip interval [x; x+w[ within [0; wmax[.
333 * The resulting w may be negative if the final interval is empty.
334 * dx, if not null, return the difference between in and out value of x.
336 static void clip_interval(int wmax, int *x, int *w, int *dx)
351 * Decompose w pixels starting at x
352 * into start + (w starting at x) + end
353 * with x and w aligned on multiples of 1<<sub.
355 static void subsampling_bounds(int sub, int *x, int *w, int *start, int *end)
357 int mask = (1 << sub) - 1;
359 *start = (-*x) & mask;
361 *start = FFMIN(*start, *w);
367 static int component_used(FFDrawContext *draw, int plane, int comp)
369 return (draw->comp_mask[plane] >> comp) & 1;
372 /* If alpha is in the [ 0 ; 0x1010101 ] range,
373 then alpha * value is in the [ 0 ; 0xFFFFFFFF ] range,
374 and >> 24 gives a correct rounding. */
375 static void blend_line(uint8_t *dst, unsigned src, unsigned alpha,
376 int dx, int w, unsigned hsub, int left, int right)
378 unsigned asrc = alpha * src;
379 unsigned tau = 0x1010101 - alpha;
383 unsigned suba = (left * alpha) >> hsub;
384 *dst = (*dst * (0x1010101 - suba) + src * suba) >> 24;
387 for (x = 0; x < w; x++) {
388 *dst = (*dst * tau + asrc) >> 24;
392 unsigned suba = (right * alpha) >> hsub;
393 *dst = (*dst * (0x1010101 - suba) + src * suba) >> 24;
397 static void blend_line16(uint8_t *dst, unsigned src, unsigned alpha,
398 int dx, int w, unsigned hsub, int left, int right)
400 unsigned asrc = alpha * src;
401 unsigned tau = 0x10001 - alpha;
405 unsigned suba = (left * alpha) >> hsub;
406 uint16_t value = AV_RL16(dst);
407 AV_WL16(dst, (value * (0x10001 - suba) + src * suba) >> 16);
410 for (x = 0; x < w; x++) {
411 uint16_t value = AV_RL16(dst);
412 AV_WL16(dst, (value * tau + asrc) >> 16);
416 unsigned suba = (right * alpha) >> hsub;
417 uint16_t value = AV_RL16(dst);
418 AV_WL16(dst, (value * (0x10001 - suba) + src * suba) >> 16);
422 void ff_blend_rectangle(FFDrawContext *draw, FFDrawColor *color,
423 uint8_t *dst[], int dst_linesize[],
424 int dst_w, int dst_h,
425 int x0, int y0, int w, int h)
427 unsigned alpha, nb_planes, nb_comp, plane, comp;
428 int w_sub, h_sub, x_sub, y_sub, left, right, top, bottom, y;
431 /* TODO optimize if alpha = 0xFF */
432 clip_interval(dst_w, &x0, &w, NULL);
433 clip_interval(dst_h, &y0, &h, NULL);
434 if (w <= 0 || h <= 0 || !color->rgba[3])
436 if (draw->desc->comp[0].depth <= 8) {
437 /* 0x10203 * alpha + 2 is in the [ 2 ; 0x1010101 - 2 ] range */
438 alpha = 0x10203 * color->rgba[3] + 0x2;
440 /* 0x101 * alpha is in the [ 2 ; 0x1001] range */
441 alpha = 0x101 * color->rgba[3] + 0x2;
443 nb_planes = (draw->nb_planes - 1) | 1; /* eliminate alpha */
444 for (plane = 0; plane < nb_planes; plane++) {
445 nb_comp = draw->pixelstep[plane];
446 p0 = pointer_at(draw, dst, dst_linesize, plane, x0, y0);
451 subsampling_bounds(draw->hsub[plane], &x_sub, &w_sub, &left, &right);
452 subsampling_bounds(draw->vsub[plane], &y_sub, &h_sub, &top, &bottom);
453 for (comp = 0; comp < nb_comp; comp++) {
454 const int depth = draw->desc->comp[comp].depth;
456 if (!component_used(draw, plane, comp))
461 blend_line(p, color->comp[plane].u8[comp], alpha >> 1,
462 draw->pixelstep[plane], w_sub,
463 draw->hsub[plane], left, right);
465 blend_line16(p, color->comp[plane].u16[comp], alpha >> 1,
466 draw->pixelstep[plane], w_sub,
467 draw->hsub[plane], left, right);
469 p += dst_linesize[plane];
472 for (y = 0; y < h_sub; y++) {
473 blend_line(p, color->comp[plane].u8[comp], alpha,
474 draw->pixelstep[plane], w_sub,
475 draw->hsub[plane], left, right);
476 p += dst_linesize[plane];
479 for (y = 0; y < h_sub; y++) {
480 blend_line16(p, color->comp[plane].u16[comp], alpha,
481 draw->pixelstep[plane], w_sub,
482 draw->hsub[plane], left, right);
483 p += dst_linesize[plane];
488 blend_line(p, color->comp[plane].u8[comp], alpha >> 1,
489 draw->pixelstep[plane], w_sub,
490 draw->hsub[plane], left, right);
492 blend_line16(p, color->comp[plane].u16[comp], alpha >> 1,
493 draw->pixelstep[plane], w_sub,
494 draw->hsub[plane], left, right);
501 static void blend_pixel16(uint8_t *dst, unsigned src, unsigned alpha,
502 const uint8_t *mask, int mask_linesize, int l2depth,
503 unsigned w, unsigned h, unsigned shift, unsigned xm0)
505 unsigned xm, x, y, t = 0;
506 unsigned xmshf = 3 - l2depth;
507 unsigned xmmod = 7 >> l2depth;
508 unsigned mbits = (1 << (1 << l2depth)) - 1;
509 unsigned mmult = 255 / mbits;
510 uint16_t value = AV_RL16(dst);
512 for (y = 0; y < h; y++) {
514 for (x = 0; x < w; x++) {
515 t += ((mask[xm >> xmshf] >> ((~xm & xmmod) << l2depth)) & mbits)
519 mask += mask_linesize;
521 alpha = (t >> shift) * alpha;
522 AV_WL16(dst, ((0x10001 - alpha) * value + alpha * src) >> 16);
525 static void blend_pixel(uint8_t *dst, unsigned src, unsigned alpha,
526 const uint8_t *mask, int mask_linesize, int l2depth,
527 unsigned w, unsigned h, unsigned shift, unsigned xm0)
529 unsigned xm, x, y, t = 0;
530 unsigned xmshf = 3 - l2depth;
531 unsigned xmmod = 7 >> l2depth;
532 unsigned mbits = (1 << (1 << l2depth)) - 1;
533 unsigned mmult = 255 / mbits;
535 for (y = 0; y < h; y++) {
537 for (x = 0; x < w; x++) {
538 t += ((mask[xm >> xmshf] >> ((~xm & xmmod) << l2depth)) & mbits)
542 mask += mask_linesize;
544 alpha = (t >> shift) * alpha;
545 *dst = ((0x1010101 - alpha) * *dst + alpha * src) >> 24;
548 static void blend_line_hv16(uint8_t *dst, int dst_delta,
549 unsigned src, unsigned alpha,
550 const uint8_t *mask, int mask_linesize, int l2depth, int w,
551 unsigned hsub, unsigned vsub,
552 int xm, int left, int right, int hband)
557 blend_pixel16(dst, src, alpha, mask, mask_linesize, l2depth,
558 left, hband, hsub + vsub, xm);
562 for (x = 0; x < w; x++) {
563 blend_pixel16(dst, src, alpha, mask, mask_linesize, l2depth,
564 1 << hsub, hband, hsub + vsub, xm);
569 blend_pixel16(dst, src, alpha, mask, mask_linesize, l2depth,
570 right, hband, hsub + vsub, xm);
573 static void blend_line_hv(uint8_t *dst, int dst_delta,
574 unsigned src, unsigned alpha,
575 const uint8_t *mask, int mask_linesize, int l2depth, int w,
576 unsigned hsub, unsigned vsub,
577 int xm, int left, int right, int hband)
582 blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
583 left, hband, hsub + vsub, xm);
587 for (x = 0; x < w; x++) {
588 blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
589 1 << hsub, hband, hsub + vsub, xm);
594 blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
595 right, hband, hsub + vsub, xm);
598 void ff_blend_mask(FFDrawContext *draw, FFDrawColor *color,
599 uint8_t *dst[], int dst_linesize[], int dst_w, int dst_h,
600 const uint8_t *mask, int mask_linesize, int mask_w, int mask_h,
601 int l2depth, unsigned endianness, int x0, int y0)
603 unsigned alpha, nb_planes, nb_comp, plane, comp;
604 int xm0, ym0, w_sub, h_sub, x_sub, y_sub, left, right, top, bottom, y;
608 clip_interval(dst_w, &x0, &mask_w, &xm0);
609 clip_interval(dst_h, &y0, &mask_h, &ym0);
610 mask += ym0 * mask_linesize;
611 if (mask_w <= 0 || mask_h <= 0 || !color->rgba[3])
613 if (draw->desc->comp[0].depth <= 8) {
614 /* alpha is in the [ 0 ; 0x10203 ] range,
615 alpha * mask is in the [ 0 ; 0x1010101 - 4 ] range */
616 alpha = (0x10307 * color->rgba[3] + 0x3) >> 8;
618 alpha = (0x101 * color->rgba[3] + 0x2) >> 8;
620 nb_planes = (draw->nb_planes - 1) | 1; /* eliminate alpha */
621 for (plane = 0; plane < nb_planes; plane++) {
622 nb_comp = draw->pixelstep[plane];
623 p0 = pointer_at(draw, dst, dst_linesize, plane, x0, y0);
628 subsampling_bounds(draw->hsub[plane], &x_sub, &w_sub, &left, &right);
629 subsampling_bounds(draw->vsub[plane], &y_sub, &h_sub, &top, &bottom);
630 for (comp = 0; comp < nb_comp; comp++) {
631 const int depth = draw->desc->comp[comp].depth;
633 if (!component_used(draw, plane, comp))
639 blend_line_hv(p, draw->pixelstep[plane],
640 color->comp[plane].u8[comp], alpha,
641 m, mask_linesize, l2depth, w_sub,
642 draw->hsub[plane], draw->vsub[plane],
643 xm0, left, right, top);
645 blend_line_hv16(p, draw->pixelstep[plane],
646 color->comp[plane].u16[comp], alpha,
647 m, mask_linesize, l2depth, w_sub,
648 draw->hsub[plane], draw->vsub[plane],
649 xm0, left, right, top);
651 p += dst_linesize[plane];
652 m += top * mask_linesize;
655 for (y = 0; y < h_sub; y++) {
656 blend_line_hv(p, draw->pixelstep[plane],
657 color->comp[plane].u8[comp], alpha,
658 m, mask_linesize, l2depth, w_sub,
659 draw->hsub[plane], draw->vsub[plane],
660 xm0, left, right, 1 << draw->vsub[plane]);
661 p += dst_linesize[plane];
662 m += mask_linesize << draw->vsub[plane];
665 for (y = 0; y < h_sub; y++) {
666 blend_line_hv16(p, draw->pixelstep[plane],
667 color->comp[plane].u16[comp], alpha,
668 m, mask_linesize, l2depth, w_sub,
669 draw->hsub[plane], draw->vsub[plane],
670 xm0, left, right, 1 << draw->vsub[plane]);
671 p += dst_linesize[plane];
672 m += mask_linesize << draw->vsub[plane];
677 blend_line_hv(p, draw->pixelstep[plane],
678 color->comp[plane].u8[comp], alpha,
679 m, mask_linesize, l2depth, w_sub,
680 draw->hsub[plane], draw->vsub[plane],
681 xm0, left, right, bottom);
683 blend_line_hv16(p, draw->pixelstep[plane],
684 color->comp[plane].u16[comp], alpha,
685 m, mask_linesize, l2depth, w_sub,
686 draw->hsub[plane], draw->vsub[plane],
687 xm0, left, right, bottom);
694 int ff_draw_round_to_sub(FFDrawContext *draw, int sub_dir, int round_dir,
697 unsigned shift = sub_dir ? draw->vsub_max : draw->hsub_max;
702 value += round_dir ? (1 << shift) - 1 : 1 << (shift - 1);
703 return (value >> shift) << shift;
706 AVFilterFormats *ff_draw_supported_pixel_formats(unsigned flags)
708 enum AVPixelFormat i;
710 AVFilterFormats *fmts = NULL;
713 for (i = 0; av_pix_fmt_desc_get(i); i++)
714 if (ff_draw_init(&draw, i, flags) >= 0 &&
715 (ret = ff_add_format(&fmts, i)) < 0)
726 enum AVPixelFormat f;
727 const AVPixFmtDescriptor *desc;
732 for (f = 0; av_pix_fmt_desc_get(f); f++) {
733 desc = av_pix_fmt_desc_get(f);
736 printf("Testing %s...%*s", desc->name,
737 (int)(16 - strlen(desc->name)), "");
738 r = ff_draw_init(&draw, f, 0);
741 av_strerror(r, buf, sizeof(buf));
742 printf("no: %s\n", buf);
745 ff_draw_color(&draw, &color, (uint8_t[]) { 1, 0, 0, 1 });
746 for (i = 0; i < sizeof(color); i++)
747 if (((uint8_t *)&color)[i] != 128)
749 if (i == sizeof(color)) {
750 printf("fallback color\n");