2 * This file is part of FFmpeg.
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #include "libavutil/avutil.h"
20 #include "libavutil/colorspace.h"
21 #include "libavutil/pixdesc.h"
22 #include "drawutils.h"
24 enum { RED = 0, GREEN, BLUE, ALPHA };
26 int ff_fill_rgba_map(uint8_t *rgba_map, enum PixelFormat pix_fmt)
30 case PIX_FMT_ARGB: rgba_map[ALPHA] = 0; rgba_map[RED ] = 1; rgba_map[GREEN] = 2; rgba_map[BLUE ] = 3; break;
32 case PIX_FMT_ABGR: rgba_map[ALPHA] = 0; rgba_map[BLUE ] = 1; rgba_map[GREEN] = 2; rgba_map[RED ] = 3; break;
35 case PIX_FMT_RGB24: rgba_map[RED ] = 0; rgba_map[GREEN] = 1; rgba_map[BLUE ] = 2; rgba_map[ALPHA] = 3; break;
38 case PIX_FMT_BGR24: rgba_map[BLUE ] = 0; rgba_map[GREEN] = 1; rgba_map[RED ] = 2; rgba_map[ALPHA] = 3; break;
39 default: /* unsupported */
40 return AVERROR(EINVAL);
45 int ff_fill_line_with_color(uint8_t *line[4], int pixel_step[4], int w, uint8_t dst_color[4],
46 enum PixelFormat pix_fmt, uint8_t rgba_color[4],
47 int *is_packed_rgba, uint8_t rgba_map_ptr[4])
49 uint8_t rgba_map[4] = {0};
51 const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[pix_fmt];
52 int hsub = pix_desc->log2_chroma_w;
54 *is_packed_rgba = ff_fill_rgba_map(rgba_map, pix_fmt) >= 0;
56 if (*is_packed_rgba) {
57 pixel_step[0] = (av_get_bits_per_pixel(pix_desc))>>3;
58 for (i = 0; i < 4; i++)
59 dst_color[rgba_map[i]] = rgba_color[i];
61 line[0] = av_malloc(w * pixel_step[0]);
62 for (i = 0; i < w; i++)
63 memcpy(line[0] + i * pixel_step[0], dst_color, pixel_step[0]);
65 memcpy(rgba_map_ptr, rgba_map, sizeof(rgba_map[0]) * 4);
69 dst_color[0] = RGB_TO_Y_CCIR(rgba_color[0], rgba_color[1], rgba_color[2]);
70 dst_color[1] = RGB_TO_U_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
71 dst_color[2] = RGB_TO_V_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
72 dst_color[3] = rgba_color[3];
74 for (plane = 0; plane < 4; plane++) {
76 int hsub1 = (plane == 1 || plane == 2) ? hsub : 0;
78 pixel_step[plane] = 1;
79 line_size = (w >> hsub1) * pixel_step[plane];
80 line[plane] = av_malloc(line_size);
81 memset(line[plane], dst_color[plane], line_size);
88 void ff_draw_rectangle(uint8_t *dst[4], int dst_linesize[4],
89 uint8_t *src[4], int pixelstep[4],
90 int hsub, int vsub, int x, int y, int w, int h)
95 for (plane = 0; plane < 4 && dst[plane]; plane++) {
96 int hsub1 = plane == 1 || plane == 2 ? hsub : 0;
97 int vsub1 = plane == 1 || plane == 2 ? vsub : 0;
99 p = dst[plane] + (y >> vsub1) * dst_linesize[plane];
100 for (i = 0; i < (h >> vsub1); i++) {
101 memcpy(p + (x >> hsub1) * pixelstep[plane],
102 src[plane], (w >> hsub1) * pixelstep[plane]);
103 p += dst_linesize[plane];
108 void ff_copy_rectangle(uint8_t *dst[4], int dst_linesize[4],
109 uint8_t *src[4], int src_linesize[4], int pixelstep[4],
110 int hsub, int vsub, int x, int y, int y2, int w, int h)
115 for (plane = 0; plane < 4 && dst[plane]; plane++) {
116 int hsub1 = plane == 1 || plane == 2 ? hsub : 0;
117 int vsub1 = plane == 1 || plane == 2 ? vsub : 0;
119 p = dst[plane] + (y >> vsub1) * dst_linesize[plane];
120 for (i = 0; i < (h >> vsub1); i++) {
121 memcpy(p + (x >> hsub1) * pixelstep[plane],
122 src[plane] + src_linesize[plane]*(i+(y2>>vsub1)), (w >> hsub1) * pixelstep[plane]);
123 p += dst_linesize[plane];
128 int ff_draw_init(FFDrawContext *draw, enum PixelFormat format, unsigned flags)
130 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[format];
131 const AVComponentDescriptor *c;
132 unsigned i, nb_planes = 0;
133 int pixelstep[MAX_PLANES] = { 0 };
136 return AVERROR(EINVAL);
137 if (desc->flags & ~(PIX_FMT_PLANAR | PIX_FMT_RGB))
138 return AVERROR(ENOSYS);
139 for (i = 0; i < desc->nb_components; i++) {
141 /* for now, only 8-bits formats */
142 if (c->depth_minus1 != 8 - 1)
143 return AVERROR(ENOSYS);
144 if (c->plane >= MAX_PLANES)
145 return AVERROR(ENOSYS);
146 /* strange interleaving */
147 if (pixelstep[c->plane] != 0 &&
148 pixelstep[c->plane] != c->step_minus1 + 1)
149 return AVERROR(ENOSYS);
150 pixelstep[c->plane] = c->step_minus1 + 1;
151 if (pixelstep[c->plane] >= 8)
152 return AVERROR(ENOSYS);
153 nb_planes = FFMAX(nb_planes, c->plane + 1);
155 if ((desc->log2_chroma_w || desc->log2_chroma_h) && nb_planes < 3)
156 return AVERROR(ENOSYS); /* exclude NV12 and NV21 */
157 memset(draw, 0, sizeof(*draw));
159 draw->format = format;
160 draw->nb_planes = nb_planes;
161 memcpy(draw->pixelstep, pixelstep, sizeof(draw->pixelstep));
162 if (nb_planes >= 3 && !(desc->flags & PIX_FMT_RGB)) {
163 draw->hsub[1] = draw->hsub[2] = draw->hsub_max = desc->log2_chroma_w;
164 draw->vsub[1] = draw->vsub[2] = draw->vsub_max = desc->log2_chroma_h;
166 for (i = 0; i < ((desc->nb_components - 1) | 1); i++)
167 draw->comp_mask[desc->comp[i].plane] |=
168 1 << (desc->comp[i].offset_plus1 - 1);
172 void ff_draw_color(FFDrawContext *draw, FFDrawColor *color, uint8_t rgba[4])
177 if (rgba != color->rgba)
178 memcpy(color->rgba, rgba, sizeof(color->rgba));
179 if ((draw->desc->flags & PIX_FMT_RGB) && draw->nb_planes == 1 &&
180 ff_fill_rgba_map(rgba_map, draw->format) >= 0) {
181 for (i = 0; i < 4; i++)
182 color->comp[0].u8[rgba_map[i]] = rgba[i];
183 } else if (draw->nb_planes == 3 || draw->nb_planes == 4) {
185 color->comp[0].u8[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
186 color->comp[1].u8[0] = RGB_TO_U_CCIR(rgba[0], rgba[1], rgba[2], 0);
187 color->comp[2].u8[0] = RGB_TO_V_CCIR(rgba[0], rgba[1], rgba[2], 0);
188 color->comp[3].u8[0] = rgba[3];
189 } else if (draw->format == PIX_FMT_GRAY8 || draw->format == PIX_FMT_GRAY8A) {
190 color->comp[0].u8[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
191 color->comp[1].u8[0] = rgba[3];
193 av_log(NULL, AV_LOG_WARNING,
194 "Color conversion not implemented for %s\n", draw->desc->name);
195 memset(color, 128, sizeof(*color));
199 static uint8_t *pointer_at(FFDrawContext *draw, uint8_t *data[], int linesize[],
200 int plane, int x, int y)
203 (y >> draw->vsub[plane]) * linesize[plane] +
204 (x >> draw->hsub[plane]) * draw->pixelstep[plane];
207 void ff_copy_rectangle2(FFDrawContext *draw,
208 uint8_t *dst[], int dst_linesize[],
209 uint8_t *src[], int src_linesize[],
210 int dst_x, int dst_y, int src_x, int src_y,
213 int plane, y, wp, hp;
216 for (plane = 0; plane < draw->nb_planes; plane++) {
217 p = pointer_at(draw, src, src_linesize, plane, src_x, src_y);
218 q = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
219 wp = (w >> draw->hsub[plane]) * draw->pixelstep[plane];
220 hp = (h >> draw->vsub[plane]);
221 for (y = 0; y < hp; y++) {
223 p += src_linesize[plane];
224 q += dst_linesize[plane];
229 void ff_fill_rectangle(FFDrawContext *draw, FFDrawColor *color,
230 uint8_t *dst[], int dst_linesize[],
231 int dst_x, int dst_y, int w, int h)
233 int plane, x, y, wp, hp;
236 for (plane = 0; plane < draw->nb_planes; plane++) {
237 p0 = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
238 wp = (w >> draw->hsub[plane]);
239 hp = (h >> draw->vsub[plane]);
243 /* copy first line from color */
244 for (x = 0; x < wp; x++) {
245 memcpy(p, color->comp[plane].u8, draw->pixelstep[plane]);
246 p += draw->pixelstep[plane];
248 wp *= draw->pixelstep[plane];
249 /* copy next lines from first line */
250 p = p0 + dst_linesize[plane];
251 for (y = 1; y < hp; y++) {
253 p += dst_linesize[plane];
259 * Clip interval [x; x+w[ within [0; wmax[.
260 * The resulting w may be negative if the final interval is empty.
261 * dx, if not null, return the difference between in and out value of x.
263 static void clip_interval(int wmax, int *x, int *w, int *dx)
278 * Decompose w pixels starting at x
279 * into start + (w starting at x) + end
280 * with x and w aligned on multiples of 1<<sub.
282 static void subsampling_bounds(int sub, int *x, int *w, int *start, int *end)
284 int mask = (1 << sub) - 1;
286 *start = (-*x) & mask;
288 *start = FFMIN(*start, *w);
294 static int component_used(FFDrawContext *draw, int plane, int comp)
296 return (draw->comp_mask[plane] >> comp) & 1;
299 /* If alpha is in the [ 0 ; 0x1010101 ] range,
300 then alpha * value is in the [ 0 ; 0xFFFFFFFF ] range,
301 and >> 24 gives a correct rounding. */
302 static void blend_line(uint8_t *dst, unsigned src, unsigned alpha,
303 int dx, int w, unsigned hsub, int left, int right)
305 unsigned asrc = alpha * src;
306 unsigned tau = 0x1010101 - alpha;
311 unsigned suba = (left * alpha) >> hsub;
312 *dst = (*dst * (0x1010101 - suba) + src * suba) >> 24;
315 for (x = 0; x < w; x++) {
316 *dst = (*dst * tau + asrc) >> 24;
320 unsigned suba = (right * alpha) >> hsub;
321 *dst = (*dst * (0x1010101 - suba) + src * suba) >> 24;
325 void ff_blend_rectangle(FFDrawContext *draw, FFDrawColor *color,
326 uint8_t *dst[], int dst_linesize[],
327 int dst_w, int dst_h,
328 int x0, int y0, int w, int h)
330 unsigned alpha, nb_planes, nb_comp, plane, comp;
331 int w_sub, h_sub, x_sub, y_sub, left, right, top, bottom, y;
334 /* TODO optimize if alpha = 0xFF */
335 clip_interval(dst_w, &x0, &w, NULL);
336 clip_interval(dst_h, &y0, &h, NULL);
337 if (w <= 0 || h <= 0 || !color->rgba[3])
339 /* 0x10203 * alpha + 2 is in the [ 2 ; 0x1010101 - 2 ] range */
340 alpha = 0x10203 * color->rgba[3] + 0x2;
341 nb_planes = (draw->nb_planes - 1) | 1; /* eliminate alpha */
342 for (plane = 0; plane < nb_planes; plane++) {
343 nb_comp = draw->pixelstep[plane];
344 p0 = pointer_at(draw, dst, dst_linesize, plane, x0, y0);
349 subsampling_bounds(draw->hsub[plane], &x_sub, &w_sub, &left, &right);
350 subsampling_bounds(draw->vsub[plane], &y_sub, &h_sub, &top, &bottom);
351 for (comp = 0; comp < nb_comp; comp++) {
352 if (!component_used(draw, plane, comp))
356 blend_line(p, color->comp[plane].u8[comp], alpha >> 1,
357 draw->pixelstep[plane], w_sub,
358 draw->hsub[plane], left, right);
359 p += dst_linesize[plane];
361 for (y = 0; y < h_sub; y++) {
362 blend_line(p, color->comp[plane].u8[comp], alpha,
363 draw->pixelstep[plane], w_sub,
364 draw->hsub[plane], left, right);
365 p += dst_linesize[plane];
368 blend_line(p, color->comp[plane].u8[comp], alpha >> 1,
369 draw->pixelstep[plane], w_sub,
370 draw->hsub[plane], left, right);
375 static void blend_pixel(uint8_t *dst, unsigned src, unsigned alpha,
376 uint8_t *mask, int mask_linesize, int l2depth,
377 unsigned w, unsigned h, unsigned shift, unsigned xm0)
379 unsigned xm, x, y, t = 0;
380 unsigned xmshf = 3 - l2depth;
381 unsigned xmmod = 7 >> l2depth;
382 unsigned mbits = (1 << (1 << l2depth)) - 1;
383 unsigned mmult = 255 / mbits;
385 for (y = 0; y < h; y++) {
387 for (x = 0; x < w; x++) {
388 t += ((mask[xm >> xmshf] >> ((~xm & xmmod) << l2depth)) & mbits)
392 mask += mask_linesize;
394 alpha = (t >> shift) * alpha;
395 *dst = ((0x1010101 - alpha) * *dst + alpha * src) >> 24;
398 static void blend_line_hv(uint8_t *dst, int dst_delta,
399 unsigned src, unsigned alpha,
400 uint8_t *mask, int mask_linesize, int l2depth, int w,
401 unsigned hsub, unsigned vsub,
402 int xm, int left, int right, int hband)
407 blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
408 left, hband, hsub + vsub, xm);
412 for (x = 0; x < w; x++) {
413 blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
414 1 << hsub, hband, hsub + vsub, xm);
419 blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
420 right, hband, hsub + vsub, xm);
423 void ff_blend_mask(FFDrawContext *draw, FFDrawColor *color,
424 uint8_t *dst[], int dst_linesize[], int dst_w, int dst_h,
425 uint8_t *mask, int mask_linesize, int mask_w, int mask_h,
426 int l2depth, unsigned endianness, int x0, int y0)
428 unsigned alpha, nb_planes, nb_comp, plane, comp;
429 int xm0, ym0, w_sub, h_sub, x_sub, y_sub, left, right, top, bottom, y;
432 clip_interval(dst_w, &x0, &mask_w, &xm0);
433 clip_interval(dst_h, &y0, &mask_h, &ym0);
434 mask += ym0 * mask_linesize;
435 if (mask_w <= 0 || mask_h <= 0 || !color->rgba[3])
437 /* alpha is in the [ 0 ; 0x10203 ] range,
438 alpha * mask is in the [ 0 ; 0x1010101 - 4 ] range */
439 alpha = (0x10307 * color->rgba[3] + 0x3) >> 8;
440 nb_planes = (draw->nb_planes - 1) | 1; /* eliminate alpha */
441 for (plane = 0; plane < nb_planes; plane++) {
442 nb_comp = draw->pixelstep[plane];
443 p0 = pointer_at(draw, dst, dst_linesize, plane, x0, y0);
448 subsampling_bounds(draw->hsub[plane], &x_sub, &w_sub, &left, &right);
449 subsampling_bounds(draw->vsub[plane], &y_sub, &h_sub, &top, &bottom);
450 for (comp = 0; comp < nb_comp; comp++) {
451 if (!component_used(draw, plane, comp))
456 blend_line_hv(p, draw->pixelstep[plane],
457 color->comp[plane].u8[comp], alpha,
458 m, mask_linesize, l2depth, w_sub,
459 draw->hsub[plane], draw->vsub[plane],
460 xm0, left, right, top);
461 p += dst_linesize[plane];
462 m += top * mask_linesize;
464 for (y = 0; y < h_sub; y++) {
465 blend_line_hv(p, draw->pixelstep[plane],
466 color->comp[plane].u8[comp], alpha,
467 m, mask_linesize, l2depth, w_sub,
468 draw->hsub[plane], draw->vsub[plane],
469 xm0, left, right, 1 << draw->vsub[plane]);
470 p += dst_linesize[plane];
471 m += mask_linesize << draw->vsub[plane];
474 blend_line_hv(p, draw->pixelstep[plane],
475 color->comp[plane].u8[comp], alpha,
476 m, mask_linesize, l2depth, w_sub,
477 draw->hsub[plane], draw->vsub[plane],
478 xm0, left, right, bottom);
483 int ff_draw_round_to_sub(FFDrawContext *draw, int sub_dir, int round_dir,
486 unsigned shift = sub_dir ? draw->vsub_max : draw->hsub_max;
491 value += round_dir ? (1 << shift) - 1 : 1 << (shift - 1);
492 return (value >> shift) << shift;
495 AVFilterFormats *ff_draw_supported_pixel_formats(unsigned flags)
497 enum PixelFormat i, pix_fmts[PIX_FMT_NB + 1];
501 for (i = 0; i < PIX_FMT_NB; i++)
502 if (ff_draw_init(&draw, i, flags) >= 0)
504 pix_fmts[n++] = PIX_FMT_NONE;
505 return avfilter_make_format_list(pix_fmts);
515 const AVPixFmtDescriptor *desc;
520 for (f = 0; f < PIX_FMT_NB; f++) {
521 desc = &av_pix_fmt_descriptors[f];
524 printf("Testing %s...%*s", desc->name,
525 (int)(16 - strlen(desc->name)), "");
526 r = ff_draw_init(&draw, f, 0);
529 av_strerror(r, buf, sizeof(buf));
530 printf("no: %s\n", buf);
533 ff_draw_color(&draw, &color, (uint8_t[]) { 1, 0, 0, 1 });
534 for (i = 0; i < sizeof(color); i++)
535 if (((uint8_t *)&color)[i] != 128)
537 if (i == sizeof(color)) {
538 printf("fallback color\n");