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/avutil.h"
25 #include "libavutil/colorspace.h"
26 #include "libavutil/mem.h"
27 #include "libavutil/pixdesc.h"
28 #include "drawutils.h"
31 enum { RED = 0, GREEN, BLUE, ALPHA };
33 int ff_fill_rgba_map(uint8_t *rgba_map, enum PixelFormat pix_fmt)
37 case PIX_FMT_ARGB: rgba_map[ALPHA] = 0; rgba_map[RED ] = 1; rgba_map[GREEN] = 2; rgba_map[BLUE ] = 3; break;
39 case PIX_FMT_ABGR: rgba_map[ALPHA] = 0; rgba_map[BLUE ] = 1; rgba_map[GREEN] = 2; rgba_map[RED ] = 3; break;
42 case PIX_FMT_RGB24: rgba_map[RED ] = 0; rgba_map[GREEN] = 1; rgba_map[BLUE ] = 2; rgba_map[ALPHA] = 3; break;
45 case PIX_FMT_BGR24: rgba_map[BLUE ] = 0; rgba_map[GREEN] = 1; rgba_map[RED ] = 2; rgba_map[ALPHA] = 3; break;
46 default: /* unsupported */
47 return AVERROR(EINVAL);
52 int ff_fill_line_with_color(uint8_t *line[4], int pixel_step[4], int w, uint8_t dst_color[4],
53 enum PixelFormat pix_fmt, uint8_t rgba_color[4],
54 int *is_packed_rgba, uint8_t rgba_map_ptr[4])
56 uint8_t rgba_map[4] = {0};
58 const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[pix_fmt];
59 int hsub = pix_desc->log2_chroma_w;
61 *is_packed_rgba = ff_fill_rgba_map(rgba_map, pix_fmt) >= 0;
63 if (*is_packed_rgba) {
64 pixel_step[0] = (av_get_bits_per_pixel(pix_desc))>>3;
65 for (i = 0; i < 4; i++)
66 dst_color[rgba_map[i]] = rgba_color[i];
68 line[0] = av_malloc(w * pixel_step[0]);
69 for (i = 0; i < w; i++)
70 memcpy(line[0] + i * pixel_step[0], dst_color, pixel_step[0]);
72 memcpy(rgba_map_ptr, rgba_map, sizeof(rgba_map[0]) * 4);
76 dst_color[0] = RGB_TO_Y_CCIR(rgba_color[0], rgba_color[1], rgba_color[2]);
77 dst_color[1] = RGB_TO_U_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
78 dst_color[2] = RGB_TO_V_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
79 dst_color[3] = rgba_color[3];
81 for (plane = 0; plane < 4; plane++) {
83 int hsub1 = (plane == 1 || plane == 2) ? hsub : 0;
85 pixel_step[plane] = 1;
86 line_size = (w >> hsub1) * pixel_step[plane];
87 line[plane] = av_malloc(line_size);
88 memset(line[plane], dst_color[plane], line_size);
95 void ff_draw_rectangle(uint8_t *dst[4], int dst_linesize[4],
96 uint8_t *src[4], int pixelstep[4],
97 int hsub, int vsub, int x, int y, int w, int h)
102 for (plane = 0; plane < 4 && dst[plane]; plane++) {
103 int hsub1 = plane == 1 || plane == 2 ? hsub : 0;
104 int vsub1 = plane == 1 || plane == 2 ? vsub : 0;
106 p = dst[plane] + (y >> vsub1) * dst_linesize[plane];
107 for (i = 0; i < (h >> vsub1); i++) {
108 memcpy(p + (x >> hsub1) * pixelstep[plane],
109 src[plane], (w >> hsub1) * pixelstep[plane]);
110 p += dst_linesize[plane];
115 void ff_copy_rectangle(uint8_t *dst[4], int dst_linesize[4],
116 uint8_t *src[4], int src_linesize[4], int pixelstep[4],
117 int hsub, int vsub, int x, int y, int y2, int w, int h)
122 for (plane = 0; plane < 4 && dst[plane]; plane++) {
123 int hsub1 = plane == 1 || plane == 2 ? hsub : 0;
124 int vsub1 = plane == 1 || plane == 2 ? vsub : 0;
126 p = dst[plane] + (y >> vsub1) * dst_linesize[plane];
127 for (i = 0; i < (h >> vsub1); i++) {
128 memcpy(p + (x >> hsub1) * pixelstep[plane],
129 src[plane] + src_linesize[plane]*(i+(y2>>vsub1)), (w >> hsub1) * pixelstep[plane]);
130 p += dst_linesize[plane];
135 int ff_draw_init(FFDrawContext *draw, enum PixelFormat format, unsigned flags)
137 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[format];
138 const AVComponentDescriptor *c;
139 unsigned i, nb_planes = 0;
140 int pixelstep[MAX_PLANES] = { 0 };
143 return AVERROR(EINVAL);
144 if (desc->flags & ~(PIX_FMT_PLANAR | PIX_FMT_RGB | PIX_FMT_PSEUDOPAL))
145 return AVERROR(ENOSYS);
146 for (i = 0; i < desc->nb_components; i++) {
148 /* for now, only 8-bits formats */
149 if (c->depth_minus1 != 8 - 1)
150 return AVERROR(ENOSYS);
151 if (c->plane >= MAX_PLANES)
152 return AVERROR(ENOSYS);
153 /* strange interleaving */
154 if (pixelstep[c->plane] != 0 &&
155 pixelstep[c->plane] != c->step_minus1 + 1)
156 return AVERROR(ENOSYS);
157 pixelstep[c->plane] = c->step_minus1 + 1;
158 if (pixelstep[c->plane] >= 8)
159 return AVERROR(ENOSYS);
160 nb_planes = FFMAX(nb_planes, c->plane + 1);
162 if ((desc->log2_chroma_w || desc->log2_chroma_h) && nb_planes < 3)
163 return AVERROR(ENOSYS); /* exclude NV12 and NV21 */
164 memset(draw, 0, sizeof(*draw));
166 draw->format = format;
167 draw->nb_planes = nb_planes;
168 memcpy(draw->pixelstep, pixelstep, sizeof(draw->pixelstep));
169 if (nb_planes >= 3 && !(desc->flags & PIX_FMT_RGB)) {
170 draw->hsub[1] = draw->hsub[2] = draw->hsub_max = desc->log2_chroma_w;
171 draw->vsub[1] = draw->vsub[2] = draw->vsub_max = desc->log2_chroma_h;
173 for (i = 0; i < ((desc->nb_components - 1) | 1); i++)
174 draw->comp_mask[desc->comp[i].plane] |=
175 1 << (desc->comp[i].offset_plus1 - 1);
179 void ff_draw_color(FFDrawContext *draw, FFDrawColor *color, const uint8_t rgba[4])
184 if (rgba != color->rgba)
185 memcpy(color->rgba, rgba, sizeof(color->rgba));
186 if ((draw->desc->flags & PIX_FMT_RGB) && draw->nb_planes == 1 &&
187 ff_fill_rgba_map(rgba_map, draw->format) >= 0) {
188 for (i = 0; i < 4; i++)
189 color->comp[0].u8[rgba_map[i]] = rgba[i];
190 } else if (draw->nb_planes == 3 || draw->nb_planes == 4) {
192 color->comp[0].u8[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
193 color->comp[1].u8[0] = RGB_TO_U_CCIR(rgba[0], rgba[1], rgba[2], 0);
194 color->comp[2].u8[0] = RGB_TO_V_CCIR(rgba[0], rgba[1], rgba[2], 0);
195 color->comp[3].u8[0] = rgba[3];
196 } else if (draw->format == PIX_FMT_GRAY8 || draw->format == PIX_FMT_GRAY8A) {
197 color->comp[0].u8[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
198 color->comp[1].u8[0] = rgba[3];
200 av_log(NULL, AV_LOG_WARNING,
201 "Color conversion not implemented for %s\n", draw->desc->name);
202 memset(color, 128, sizeof(*color));
206 static uint8_t *pointer_at(FFDrawContext *draw, uint8_t *data[], int linesize[],
207 int plane, int x, int y)
210 (y >> draw->vsub[plane]) * linesize[plane] +
211 (x >> draw->hsub[plane]) * draw->pixelstep[plane];
214 void ff_copy_rectangle2(FFDrawContext *draw,
215 uint8_t *dst[], int dst_linesize[],
216 uint8_t *src[], int src_linesize[],
217 int dst_x, int dst_y, int src_x, int src_y,
220 int plane, y, wp, hp;
223 for (plane = 0; plane < draw->nb_planes; plane++) {
224 p = pointer_at(draw, src, src_linesize, plane, src_x, src_y);
225 q = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
226 wp = (w >> draw->hsub[plane]) * draw->pixelstep[plane];
227 hp = (h >> draw->vsub[plane]);
228 for (y = 0; y < hp; y++) {
230 p += src_linesize[plane];
231 q += dst_linesize[plane];
236 void ff_fill_rectangle(FFDrawContext *draw, FFDrawColor *color,
237 uint8_t *dst[], int dst_linesize[],
238 int dst_x, int dst_y, int w, int h)
240 int plane, x, y, wp, hp;
243 for (plane = 0; plane < draw->nb_planes; plane++) {
244 p0 = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
245 wp = (w >> draw->hsub[plane]);
246 hp = (h >> draw->vsub[plane]);
250 /* copy first line from color */
251 for (x = 0; x < wp; x++) {
252 memcpy(p, color->comp[plane].u8, draw->pixelstep[plane]);
253 p += draw->pixelstep[plane];
255 wp *= draw->pixelstep[plane];
256 /* copy next lines from first line */
257 p = p0 + dst_linesize[plane];
258 for (y = 1; y < hp; y++) {
260 p += dst_linesize[plane];
266 * Clip interval [x; x+w[ within [0; wmax[.
267 * The resulting w may be negative if the final interval is empty.
268 * dx, if not null, return the difference between in and out value of x.
270 static void clip_interval(int wmax, int *x, int *w, int *dx)
285 * Decompose w pixels starting at x
286 * into start + (w starting at x) + end
287 * with x and w aligned on multiples of 1<<sub.
289 static void subsampling_bounds(int sub, int *x, int *w, int *start, int *end)
291 int mask = (1 << sub) - 1;
293 *start = (-*x) & mask;
295 *start = FFMIN(*start, *w);
301 static int component_used(FFDrawContext *draw, int plane, int comp)
303 return (draw->comp_mask[plane] >> comp) & 1;
306 /* If alpha is in the [ 0 ; 0x1010101 ] range,
307 then alpha * value is in the [ 0 ; 0xFFFFFFFF ] range,
308 and >> 24 gives a correct rounding. */
309 static void blend_line(uint8_t *dst, unsigned src, unsigned alpha,
310 int dx, int w, unsigned hsub, int left, int right)
312 unsigned asrc = alpha * src;
313 unsigned tau = 0x1010101 - alpha;
318 unsigned suba = (left * alpha) >> hsub;
319 *dst = (*dst * (0x1010101 - suba) + src * suba) >> 24;
322 for (x = 0; x < w; x++) {
323 *dst = (*dst * tau + asrc) >> 24;
327 unsigned suba = (right * alpha) >> hsub;
328 *dst = (*dst * (0x1010101 - suba) + src * suba) >> 24;
332 void ff_blend_rectangle(FFDrawContext *draw, FFDrawColor *color,
333 uint8_t *dst[], int dst_linesize[],
334 int dst_w, int dst_h,
335 int x0, int y0, int w, int h)
337 unsigned alpha, nb_planes, nb_comp, plane, comp;
338 int w_sub, h_sub, x_sub, y_sub, left, right, top, bottom, y;
341 /* TODO optimize if alpha = 0xFF */
342 clip_interval(dst_w, &x0, &w, NULL);
343 clip_interval(dst_h, &y0, &h, NULL);
344 if (w <= 0 || h <= 0 || !color->rgba[3])
346 /* 0x10203 * alpha + 2 is in the [ 2 ; 0x1010101 - 2 ] range */
347 alpha = 0x10203 * color->rgba[3] + 0x2;
348 nb_planes = (draw->nb_planes - 1) | 1; /* eliminate alpha */
349 for (plane = 0; plane < nb_planes; plane++) {
350 nb_comp = draw->pixelstep[plane];
351 p0 = pointer_at(draw, dst, dst_linesize, plane, x0, y0);
356 subsampling_bounds(draw->hsub[plane], &x_sub, &w_sub, &left, &right);
357 subsampling_bounds(draw->vsub[plane], &y_sub, &h_sub, &top, &bottom);
358 for (comp = 0; comp < nb_comp; comp++) {
359 if (!component_used(draw, plane, comp))
363 blend_line(p, color->comp[plane].u8[comp], alpha >> 1,
364 draw->pixelstep[plane], w_sub,
365 draw->hsub[plane], left, right);
366 p += dst_linesize[plane];
368 for (y = 0; y < h_sub; y++) {
369 blend_line(p, color->comp[plane].u8[comp], alpha,
370 draw->pixelstep[plane], w_sub,
371 draw->hsub[plane], left, right);
372 p += dst_linesize[plane];
375 blend_line(p, color->comp[plane].u8[comp], alpha >> 1,
376 draw->pixelstep[plane], w_sub,
377 draw->hsub[plane], left, right);
382 static void blend_pixel(uint8_t *dst, unsigned src, unsigned alpha,
383 uint8_t *mask, int mask_linesize, int l2depth,
384 unsigned w, unsigned h, unsigned shift, unsigned xm0)
386 unsigned xm, x, y, t = 0;
387 unsigned xmshf = 3 - l2depth;
388 unsigned xmmod = 7 >> l2depth;
389 unsigned mbits = (1 << (1 << l2depth)) - 1;
390 unsigned mmult = 255 / mbits;
392 for (y = 0; y < h; y++) {
394 for (x = 0; x < w; x++) {
395 t += ((mask[xm >> xmshf] >> ((~xm & xmmod) << l2depth)) & mbits)
399 mask += mask_linesize;
401 alpha = (t >> shift) * alpha;
402 *dst = ((0x1010101 - alpha) * *dst + alpha * src) >> 24;
405 static void blend_line_hv(uint8_t *dst, int dst_delta,
406 unsigned src, unsigned alpha,
407 uint8_t *mask, int mask_linesize, int l2depth, int w,
408 unsigned hsub, unsigned vsub,
409 int xm, int left, int right, int hband)
414 blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
415 left, hband, hsub + vsub, xm);
419 for (x = 0; x < w; x++) {
420 blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
421 1 << hsub, hband, hsub + vsub, xm);
426 blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
427 right, hband, hsub + vsub, xm);
430 void ff_blend_mask(FFDrawContext *draw, FFDrawColor *color,
431 uint8_t *dst[], int dst_linesize[], int dst_w, int dst_h,
432 uint8_t *mask, int mask_linesize, int mask_w, int mask_h,
433 int l2depth, unsigned endianness, int x0, int y0)
435 unsigned alpha, nb_planes, nb_comp, plane, comp;
436 int xm0, ym0, w_sub, h_sub, x_sub, y_sub, left, right, top, bottom, y;
439 clip_interval(dst_w, &x0, &mask_w, &xm0);
440 clip_interval(dst_h, &y0, &mask_h, &ym0);
441 mask += ym0 * mask_linesize;
442 if (mask_w <= 0 || mask_h <= 0 || !color->rgba[3])
444 /* alpha is in the [ 0 ; 0x10203 ] range,
445 alpha * mask is in the [ 0 ; 0x1010101 - 4 ] range */
446 alpha = (0x10307 * color->rgba[3] + 0x3) >> 8;
447 nb_planes = (draw->nb_planes - 1) | 1; /* eliminate alpha */
448 for (plane = 0; plane < nb_planes; plane++) {
449 nb_comp = draw->pixelstep[plane];
450 p0 = pointer_at(draw, dst, dst_linesize, plane, x0, y0);
455 subsampling_bounds(draw->hsub[plane], &x_sub, &w_sub, &left, &right);
456 subsampling_bounds(draw->vsub[plane], &y_sub, &h_sub, &top, &bottom);
457 for (comp = 0; comp < nb_comp; comp++) {
458 if (!component_used(draw, plane, comp))
463 blend_line_hv(p, draw->pixelstep[plane],
464 color->comp[plane].u8[comp], alpha,
465 m, mask_linesize, l2depth, w_sub,
466 draw->hsub[plane], draw->vsub[plane],
467 xm0, left, right, top);
468 p += dst_linesize[plane];
469 m += top * mask_linesize;
471 for (y = 0; y < h_sub; y++) {
472 blend_line_hv(p, draw->pixelstep[plane],
473 color->comp[plane].u8[comp], alpha,
474 m, mask_linesize, l2depth, w_sub,
475 draw->hsub[plane], draw->vsub[plane],
476 xm0, left, right, 1 << draw->vsub[plane]);
477 p += dst_linesize[plane];
478 m += mask_linesize << draw->vsub[plane];
481 blend_line_hv(p, draw->pixelstep[plane],
482 color->comp[plane].u8[comp], alpha,
483 m, mask_linesize, l2depth, w_sub,
484 draw->hsub[plane], draw->vsub[plane],
485 xm0, left, right, bottom);
490 int ff_draw_round_to_sub(FFDrawContext *draw, int sub_dir, int round_dir,
493 unsigned shift = sub_dir ? draw->vsub_max : draw->hsub_max;
498 value += round_dir ? (1 << shift) - 1 : 1 << (shift - 1);
499 return (value >> shift) << shift;
502 AVFilterFormats *ff_draw_supported_pixel_formats(unsigned flags)
504 enum PixelFormat i, pix_fmts[PIX_FMT_NB + 1];
508 for (i = 0; i < PIX_FMT_NB; i++)
509 if (ff_draw_init(&draw, i, flags) >= 0)
511 pix_fmts[n++] = PIX_FMT_NONE;
512 return ff_make_format_list(pix_fmts);
522 const AVPixFmtDescriptor *desc;
527 for (f = 0; f < PIX_FMT_NB; f++) {
528 desc = &av_pix_fmt_descriptors[f];
531 printf("Testing %s...%*s", desc->name,
532 (int)(16 - strlen(desc->name)), "");
533 r = ff_draw_init(&draw, f, 0);
536 av_strerror(r, buf, sizeof(buf));
537 printf("no: %s\n", buf);
540 ff_draw_color(&draw, &color, (uint8_t[]) { 1, 0, 0, 1 });
541 for (i = 0; i < sizeof(color); i++)
542 if (((uint8_t *)&color)[i] != 128)
544 if (i == sizeof(color)) {
545 printf("fallback color\n");