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;
302 for (plane = 0; plane < draw->nb_planes; plane++) {
303 p0 = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
304 wp = AV_CEIL_RSHIFT(w, draw->hsub[plane]);
305 hp = AV_CEIL_RSHIFT(h, draw->vsub[plane]);
309 /* copy first line from color */
310 for (x = 0; x < wp; x++) {
311 memcpy(p, color->comp[plane].u8, draw->pixelstep[plane]);
312 p += draw->pixelstep[plane];
314 wp *= draw->pixelstep[plane];
315 /* copy next lines from first line */
316 p = p0 + dst_linesize[plane];
317 for (y = 1; y < hp; y++) {
319 p += dst_linesize[plane];
325 * Clip interval [x; x+w[ within [0; wmax[.
326 * The resulting w may be negative if the final interval is empty.
327 * dx, if not null, return the difference between in and out value of x.
329 static void clip_interval(int wmax, int *x, int *w, int *dx)
344 * Decompose w pixels starting at x
345 * into start + (w starting at x) + end
346 * with x and w aligned on multiples of 1<<sub.
348 static void subsampling_bounds(int sub, int *x, int *w, int *start, int *end)
350 int mask = (1 << sub) - 1;
352 *start = (-*x) & mask;
354 *start = FFMIN(*start, *w);
360 static int component_used(FFDrawContext *draw, int plane, int comp)
362 return (draw->comp_mask[plane] >> comp) & 1;
365 /* If alpha is in the [ 0 ; 0x1010101 ] range,
366 then alpha * value is in the [ 0 ; 0xFFFFFFFF ] range,
367 and >> 24 gives a correct rounding. */
368 static void blend_line(uint8_t *dst, unsigned src, unsigned alpha,
369 int dx, int w, unsigned hsub, int left, int right)
371 unsigned asrc = alpha * src;
372 unsigned tau = 0x1010101 - alpha;
376 unsigned suba = (left * alpha) >> hsub;
377 *dst = (*dst * (0x1010101 - suba) + src * suba) >> 24;
380 for (x = 0; x < w; x++) {
381 *dst = (*dst * tau + asrc) >> 24;
385 unsigned suba = (right * alpha) >> hsub;
386 *dst = (*dst * (0x1010101 - suba) + src * suba) >> 24;
390 static void blend_line16(uint8_t *dst, unsigned src, unsigned alpha,
391 int dx, int w, unsigned hsub, int left, int right)
393 unsigned asrc = alpha * src;
394 unsigned tau = 0x10001 - alpha;
398 unsigned suba = (left * alpha) >> hsub;
399 uint16_t value = AV_RL16(dst);
400 AV_WL16(dst, (value * (0x10001 - suba) + src * suba) >> 16);
403 for (x = 0; x < w; x++) {
404 uint16_t value = AV_RL16(dst);
405 AV_WL16(dst, (value * tau + asrc) >> 16);
409 unsigned suba = (right * alpha) >> hsub;
410 uint16_t value = AV_RL16(dst);
411 AV_WL16(dst, (value * (0x10001 - suba) + src * suba) >> 16);
415 void ff_blend_rectangle(FFDrawContext *draw, FFDrawColor *color,
416 uint8_t *dst[], int dst_linesize[],
417 int dst_w, int dst_h,
418 int x0, int y0, int w, int h)
420 unsigned alpha, nb_planes, nb_comp, plane, comp;
421 int w_sub, h_sub, x_sub, y_sub, left, right, top, bottom, y;
424 /* TODO optimize if alpha = 0xFF */
425 clip_interval(dst_w, &x0, &w, NULL);
426 clip_interval(dst_h, &y0, &h, NULL);
427 if (w <= 0 || h <= 0 || !color->rgba[3])
429 if (draw->desc->comp[0].depth <= 8) {
430 /* 0x10203 * alpha + 2 is in the [ 2 ; 0x1010101 - 2 ] range */
431 alpha = 0x10203 * color->rgba[3] + 0x2;
433 /* 0x101 * alpha is in the [ 2 ; 0x1001] range */
434 alpha = 0x101 * color->rgba[3] + 0x2;
436 nb_planes = (draw->nb_planes - 1) | 1; /* eliminate alpha */
437 for (plane = 0; plane < nb_planes; plane++) {
438 nb_comp = draw->pixelstep[plane];
439 p0 = pointer_at(draw, dst, dst_linesize, plane, x0, y0);
444 subsampling_bounds(draw->hsub[plane], &x_sub, &w_sub, &left, &right);
445 subsampling_bounds(draw->vsub[plane], &y_sub, &h_sub, &top, &bottom);
446 for (comp = 0; comp < nb_comp; comp++) {
447 const int depth = draw->desc->comp[comp].depth;
449 if (!component_used(draw, plane, comp))
454 blend_line(p, color->comp[plane].u8[comp], alpha >> 1,
455 draw->pixelstep[plane], w_sub,
456 draw->hsub[plane], left, right);
458 blend_line16(p, color->comp[plane].u16[comp], alpha >> 1,
459 draw->pixelstep[plane], w_sub,
460 draw->hsub[plane], left, right);
462 p += dst_linesize[plane];
465 for (y = 0; y < h_sub; y++) {
466 blend_line(p, color->comp[plane].u8[comp], alpha,
467 draw->pixelstep[plane], w_sub,
468 draw->hsub[plane], left, right);
469 p += dst_linesize[plane];
472 for (y = 0; y < h_sub; y++) {
473 blend_line16(p, color->comp[plane].u16[comp], alpha,
474 draw->pixelstep[plane], w_sub,
475 draw->hsub[plane], left, right);
476 p += dst_linesize[plane];
481 blend_line(p, color->comp[plane].u8[comp], alpha >> 1,
482 draw->pixelstep[plane], w_sub,
483 draw->hsub[plane], left, right);
485 blend_line16(p, color->comp[plane].u16[comp], alpha >> 1,
486 draw->pixelstep[plane], w_sub,
487 draw->hsub[plane], left, right);
494 static void blend_pixel16(uint8_t *dst, unsigned src, unsigned alpha,
495 const uint8_t *mask, int mask_linesize, int l2depth,
496 unsigned w, unsigned h, unsigned shift, unsigned xm0)
498 unsigned xm, x, y, t = 0;
499 unsigned xmshf = 3 - l2depth;
500 unsigned xmmod = 7 >> l2depth;
501 unsigned mbits = (1 << (1 << l2depth)) - 1;
502 unsigned mmult = 255 / mbits;
503 uint16_t value = AV_RL16(dst);
505 for (y = 0; y < h; y++) {
507 for (x = 0; x < w; x++) {
508 t += ((mask[xm >> xmshf] >> ((~xm & xmmod) << l2depth)) & mbits)
512 mask += mask_linesize;
514 alpha = (t >> shift) * alpha;
515 AV_WL16(dst, ((0x10001 - alpha) * value + alpha * src) >> 16);
518 static void blend_pixel(uint8_t *dst, unsigned src, unsigned alpha,
519 const uint8_t *mask, int mask_linesize, int l2depth,
520 unsigned w, unsigned h, unsigned shift, unsigned xm0)
522 unsigned xm, x, y, t = 0;
523 unsigned xmshf = 3 - l2depth;
524 unsigned xmmod = 7 >> l2depth;
525 unsigned mbits = (1 << (1 << l2depth)) - 1;
526 unsigned mmult = 255 / mbits;
528 for (y = 0; y < h; y++) {
530 for (x = 0; x < w; x++) {
531 t += ((mask[xm >> xmshf] >> ((~xm & xmmod) << l2depth)) & mbits)
535 mask += mask_linesize;
537 alpha = (t >> shift) * alpha;
538 *dst = ((0x1010101 - alpha) * *dst + alpha * src) >> 24;
541 static void blend_line_hv16(uint8_t *dst, int dst_delta,
542 unsigned src, unsigned alpha,
543 const uint8_t *mask, int mask_linesize, int l2depth, int w,
544 unsigned hsub, unsigned vsub,
545 int xm, int left, int right, int hband)
550 blend_pixel16(dst, src, alpha, mask, mask_linesize, l2depth,
551 left, hband, hsub + vsub, xm);
555 for (x = 0; x < w; x++) {
556 blend_pixel16(dst, src, alpha, mask, mask_linesize, l2depth,
557 1 << hsub, hband, hsub + vsub, xm);
562 blend_pixel16(dst, src, alpha, mask, mask_linesize, l2depth,
563 right, hband, hsub + vsub, xm);
566 static void blend_line_hv(uint8_t *dst, int dst_delta,
567 unsigned src, unsigned alpha,
568 const uint8_t *mask, int mask_linesize, int l2depth, int w,
569 unsigned hsub, unsigned vsub,
570 int xm, int left, int right, int hband)
575 blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
576 left, hband, hsub + vsub, xm);
580 for (x = 0; x < w; x++) {
581 blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
582 1 << hsub, hband, hsub + vsub, xm);
587 blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
588 right, hband, hsub + vsub, xm);
591 void ff_blend_mask(FFDrawContext *draw, FFDrawColor *color,
592 uint8_t *dst[], int dst_linesize[], int dst_w, int dst_h,
593 const uint8_t *mask, int mask_linesize, int mask_w, int mask_h,
594 int l2depth, unsigned endianness, int x0, int y0)
596 unsigned alpha, nb_planes, nb_comp, plane, comp;
597 int xm0, ym0, w_sub, h_sub, x_sub, y_sub, left, right, top, bottom, y;
601 clip_interval(dst_w, &x0, &mask_w, &xm0);
602 clip_interval(dst_h, &y0, &mask_h, &ym0);
603 mask += ym0 * mask_linesize;
604 if (mask_w <= 0 || mask_h <= 0 || !color->rgba[3])
606 if (draw->desc->comp[0].depth <= 8) {
607 /* alpha is in the [ 0 ; 0x10203 ] range,
608 alpha * mask is in the [ 0 ; 0x1010101 - 4 ] range */
609 alpha = (0x10307 * color->rgba[3] + 0x3) >> 8;
611 alpha = (0x101 * color->rgba[3] + 0x2) >> 8;
613 nb_planes = (draw->nb_planes - 1) | 1; /* eliminate alpha */
614 for (plane = 0; plane < nb_planes; plane++) {
615 nb_comp = draw->pixelstep[plane];
616 p0 = pointer_at(draw, dst, dst_linesize, plane, x0, y0);
621 subsampling_bounds(draw->hsub[plane], &x_sub, &w_sub, &left, &right);
622 subsampling_bounds(draw->vsub[plane], &y_sub, &h_sub, &top, &bottom);
623 for (comp = 0; comp < nb_comp; comp++) {
624 const int depth = draw->desc->comp[comp].depth;
626 if (!component_used(draw, plane, comp))
632 blend_line_hv(p, draw->pixelstep[plane],
633 color->comp[plane].u8[comp], alpha,
634 m, mask_linesize, l2depth, w_sub,
635 draw->hsub[plane], draw->vsub[plane],
636 xm0, left, right, top);
638 blend_line_hv16(p, draw->pixelstep[plane],
639 color->comp[plane].u16[comp], alpha,
640 m, mask_linesize, l2depth, w_sub,
641 draw->hsub[plane], draw->vsub[plane],
642 xm0, left, right, top);
644 p += dst_linesize[plane];
645 m += top * mask_linesize;
648 for (y = 0; y < h_sub; y++) {
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, 1 << draw->vsub[plane]);
654 p += dst_linesize[plane];
655 m += mask_linesize << draw->vsub[plane];
658 for (y = 0; y < h_sub; y++) {
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, 1 << draw->vsub[plane]);
664 p += dst_linesize[plane];
665 m += mask_linesize << draw->vsub[plane];
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, bottom);
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, bottom);
687 int ff_draw_round_to_sub(FFDrawContext *draw, int sub_dir, int round_dir,
690 unsigned shift = sub_dir ? draw->vsub_max : draw->hsub_max;
695 value += round_dir ? (1 << shift) - 1 : 1 << (shift - 1);
696 return (value >> shift) << shift;
699 AVFilterFormats *ff_draw_supported_pixel_formats(unsigned flags)
701 enum AVPixelFormat i;
703 AVFilterFormats *fmts = NULL;
706 for (i = 0; av_pix_fmt_desc_get(i); i++)
707 if (ff_draw_init(&draw, i, flags) >= 0 &&
708 (ret = ff_add_format(&fmts, i)) < 0)
719 enum AVPixelFormat f;
720 const AVPixFmtDescriptor *desc;
725 for (f = 0; av_pix_fmt_desc_get(f); f++) {
726 desc = av_pix_fmt_desc_get(f);
729 printf("Testing %s...%*s", desc->name,
730 (int)(16 - strlen(desc->name)), "");
731 r = ff_draw_init(&draw, f, 0);
734 av_strerror(r, buf, sizeof(buf));
735 printf("no: %s\n", buf);
738 ff_draw_color(&draw, &color, (uint8_t[]) { 1, 0, 0, 1 });
739 for (i = 0; i < sizeof(color); i++)
740 if (((uint8_t *)&color)[i] != 128)
742 if (i == sizeof(color)) {
743 printf("fallback color\n");