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
22 #include "libavutil/avutil.h"
23 #include "libavutil/colorspace.h"
24 #include "libavutil/pixdesc.h"
25 #include "drawutils.h"
27 enum { RED = 0, GREEN, BLUE, ALPHA };
29 int ff_fill_rgba_map(uint8_t *rgba_map, enum PixelFormat pix_fmt)
33 case PIX_FMT_ARGB: rgba_map[ALPHA] = 0; rgba_map[RED ] = 1; rgba_map[GREEN] = 2; rgba_map[BLUE ] = 3; break;
35 case PIX_FMT_ABGR: rgba_map[ALPHA] = 0; rgba_map[BLUE ] = 1; rgba_map[GREEN] = 2; rgba_map[RED ] = 3; break;
38 case PIX_FMT_RGB24: rgba_map[RED ] = 0; rgba_map[GREEN] = 1; rgba_map[BLUE ] = 2; rgba_map[ALPHA] = 3; break;
41 case PIX_FMT_BGR24: rgba_map[BLUE ] = 0; rgba_map[GREEN] = 1; rgba_map[RED ] = 2; rgba_map[ALPHA] = 3; break;
42 default: /* unsupported */
43 return AVERROR(EINVAL);
48 int ff_fill_line_with_color(uint8_t *line[4], int pixel_step[4], int w, uint8_t dst_color[4],
49 enum PixelFormat pix_fmt, uint8_t rgba_color[4],
50 int *is_packed_rgba, uint8_t rgba_map_ptr[4])
52 uint8_t rgba_map[4] = {0};
54 const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[pix_fmt];
55 int hsub = pix_desc->log2_chroma_w;
57 *is_packed_rgba = ff_fill_rgba_map(rgba_map, pix_fmt) >= 0;
59 if (*is_packed_rgba) {
60 pixel_step[0] = (av_get_bits_per_pixel(pix_desc))>>3;
61 for (i = 0; i < 4; i++)
62 dst_color[rgba_map[i]] = rgba_color[i];
64 line[0] = av_malloc(w * pixel_step[0]);
65 for (i = 0; i < w; i++)
66 memcpy(line[0] + i * pixel_step[0], dst_color, pixel_step[0]);
68 memcpy(rgba_map_ptr, rgba_map, sizeof(rgba_map[0]) * 4);
72 dst_color[0] = RGB_TO_Y_CCIR(rgba_color[0], rgba_color[1], rgba_color[2]);
73 dst_color[1] = RGB_TO_U_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
74 dst_color[2] = RGB_TO_V_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
75 dst_color[3] = rgba_color[3];
77 for (plane = 0; plane < 4; plane++) {
79 int hsub1 = (plane == 1 || plane == 2) ? hsub : 0;
81 pixel_step[plane] = 1;
82 line_size = (w >> hsub1) * pixel_step[plane];
83 line[plane] = av_malloc(line_size);
84 memset(line[plane], dst_color[plane], line_size);
91 void ff_draw_rectangle(uint8_t *dst[4], int dst_linesize[4],
92 uint8_t *src[4], int pixelstep[4],
93 int hsub, int vsub, int x, int y, int w, int h)
98 for (plane = 0; plane < 4 && dst[plane]; plane++) {
99 int hsub1 = plane == 1 || plane == 2 ? hsub : 0;
100 int vsub1 = plane == 1 || plane == 2 ? vsub : 0;
102 p = dst[plane] + (y >> vsub1) * dst_linesize[plane];
103 for (i = 0; i < (h >> vsub1); i++) {
104 memcpy(p + (x >> hsub1) * pixelstep[plane],
105 src[plane], (w >> hsub1) * pixelstep[plane]);
106 p += dst_linesize[plane];
111 void ff_copy_rectangle(uint8_t *dst[4], int dst_linesize[4],
112 uint8_t *src[4], int src_linesize[4], int pixelstep[4],
113 int hsub, int vsub, int x, int y, int y2, int w, int h)
118 for (plane = 0; plane < 4 && dst[plane]; plane++) {
119 int hsub1 = plane == 1 || plane == 2 ? hsub : 0;
120 int vsub1 = plane == 1 || plane == 2 ? vsub : 0;
122 p = dst[plane] + (y >> vsub1) * dst_linesize[plane];
123 for (i = 0; i < (h >> vsub1); i++) {
124 memcpy(p + (x >> hsub1) * pixelstep[plane],
125 src[plane] + src_linesize[plane]*(i+(y2>>vsub1)), (w >> hsub1) * pixelstep[plane]);
126 p += dst_linesize[plane];
131 int ff_draw_init(FFDrawContext *draw, enum PixelFormat format, unsigned flags)
133 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[format];
134 const AVComponentDescriptor *c;
135 unsigned i, nb_planes = 0;
136 int pixelstep[MAX_PLANES] = { 0 };
139 return AVERROR(EINVAL);
140 if (desc->flags & ~(PIX_FMT_PLANAR | PIX_FMT_RGB | PIX_FMT_PSEUDOPAL))
141 return AVERROR(ENOSYS);
142 for (i = 0; i < desc->nb_components; i++) {
144 /* for now, only 8-bits formats */
145 if (c->depth_minus1 != 8 - 1)
146 return AVERROR(ENOSYS);
147 if (c->plane >= MAX_PLANES)
148 return AVERROR(ENOSYS);
149 /* strange interleaving */
150 if (pixelstep[c->plane] != 0 &&
151 pixelstep[c->plane] != c->step_minus1 + 1)
152 return AVERROR(ENOSYS);
153 pixelstep[c->plane] = c->step_minus1 + 1;
154 if (pixelstep[c->plane] >= 8)
155 return AVERROR(ENOSYS);
156 nb_planes = FFMAX(nb_planes, c->plane + 1);
158 if ((desc->log2_chroma_w || desc->log2_chroma_h) && nb_planes < 3)
159 return AVERROR(ENOSYS); /* exclude NV12 and NV21 */
160 memset(draw, 0, sizeof(*draw));
162 draw->format = format;
163 draw->nb_planes = nb_planes;
164 memcpy(draw->pixelstep, pixelstep, sizeof(draw->pixelstep));
165 if (nb_planes >= 3 && !(desc->flags & PIX_FMT_RGB)) {
166 draw->hsub[1] = draw->hsub[2] = draw->hsub_max = desc->log2_chroma_w;
167 draw->vsub[1] = draw->vsub[2] = draw->vsub_max = desc->log2_chroma_h;
169 for (i = 0; i < ((desc->nb_components - 1) | 1); i++)
170 draw->comp_mask[desc->comp[i].plane] |=
171 1 << (desc->comp[i].offset_plus1 - 1);
175 void ff_draw_color(FFDrawContext *draw, FFDrawColor *color, uint8_t rgba[4])
180 if (rgba != color->rgba)
181 memcpy(color->rgba, rgba, sizeof(color->rgba));
182 if ((draw->desc->flags & PIX_FMT_RGB) && draw->nb_planes == 1 &&
183 ff_fill_rgba_map(rgba_map, draw->format) >= 0) {
184 for (i = 0; i < 4; i++)
185 color->comp[0].u8[rgba_map[i]] = rgba[i];
186 } else if (draw->nb_planes == 3 || draw->nb_planes == 4) {
188 color->comp[0].u8[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
189 color->comp[1].u8[0] = RGB_TO_U_CCIR(rgba[0], rgba[1], rgba[2], 0);
190 color->comp[2].u8[0] = RGB_TO_V_CCIR(rgba[0], rgba[1], rgba[2], 0);
191 color->comp[3].u8[0] = rgba[3];
192 } else if (draw->format == PIX_FMT_GRAY8 || draw->format == PIX_FMT_GRAY8A) {
193 color->comp[0].u8[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
194 color->comp[1].u8[0] = rgba[3];
196 av_log(NULL, AV_LOG_WARNING,
197 "Color conversion not implemented for %s\n", draw->desc->name);
198 memset(color, 128, sizeof(*color));
202 static uint8_t *pointer_at(FFDrawContext *draw, uint8_t *data[], int linesize[],
203 int plane, int x, int y)
206 (y >> draw->vsub[plane]) * linesize[plane] +
207 (x >> draw->hsub[plane]) * draw->pixelstep[plane];
210 void ff_copy_rectangle2(FFDrawContext *draw,
211 uint8_t *dst[], int dst_linesize[],
212 uint8_t *src[], int src_linesize[],
213 int dst_x, int dst_y, int src_x, int src_y,
216 int plane, y, wp, hp;
219 for (plane = 0; plane < draw->nb_planes; plane++) {
220 p = pointer_at(draw, src, src_linesize, plane, src_x, src_y);
221 q = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
222 wp = (w >> draw->hsub[plane]) * draw->pixelstep[plane];
223 hp = (h >> draw->vsub[plane]);
224 for (y = 0; y < hp; y++) {
226 p += src_linesize[plane];
227 q += dst_linesize[plane];
232 void ff_fill_rectangle(FFDrawContext *draw, FFDrawColor *color,
233 uint8_t *dst[], int dst_linesize[],
234 int dst_x, int dst_y, int w, int h)
236 int plane, x, y, wp, hp;
239 for (plane = 0; plane < draw->nb_planes; plane++) {
240 p0 = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
241 wp = (w >> draw->hsub[plane]);
242 hp = (h >> draw->vsub[plane]);
246 /* copy first line from color */
247 for (x = 0; x < wp; x++) {
248 memcpy(p, color->comp[plane].u8, draw->pixelstep[plane]);
249 p += draw->pixelstep[plane];
251 wp *= draw->pixelstep[plane];
252 /* copy next lines from first line */
253 p = p0 + dst_linesize[plane];
254 for (y = 1; y < hp; y++) {
256 p += dst_linesize[plane];
262 * Clip interval [x; x+w[ within [0; wmax[.
263 * The resulting w may be negative if the final interval is empty.
264 * dx, if not null, return the difference between in and out value of x.
266 static void clip_interval(int wmax, int *x, int *w, int *dx)
281 * Decompose w pixels starting at x
282 * into start + (w starting at x) + end
283 * with x and w aligned on multiples of 1<<sub.
285 static void subsampling_bounds(int sub, int *x, int *w, int *start, int *end)
287 int mask = (1 << sub) - 1;
289 *start = (-*x) & mask;
291 *start = FFMIN(*start, *w);
297 static int component_used(FFDrawContext *draw, int plane, int comp)
299 return (draw->comp_mask[plane] >> comp) & 1;
302 /* If alpha is in the [ 0 ; 0x1010101 ] range,
303 then alpha * value is in the [ 0 ; 0xFFFFFFFF ] range,
304 and >> 24 gives a correct rounding. */
305 static void blend_line(uint8_t *dst, unsigned src, unsigned alpha,
306 int dx, int w, unsigned hsub, int left, int right)
308 unsigned asrc = alpha * src;
309 unsigned tau = 0x1010101 - alpha;
314 unsigned suba = (left * alpha) >> hsub;
315 *dst = (*dst * (0x1010101 - suba) + src * suba) >> 24;
318 for (x = 0; x < w; x++) {
319 *dst = (*dst * tau + asrc) >> 24;
323 unsigned suba = (right * alpha) >> hsub;
324 *dst = (*dst * (0x1010101 - suba) + src * suba) >> 24;
328 void ff_blend_rectangle(FFDrawContext *draw, FFDrawColor *color,
329 uint8_t *dst[], int dst_linesize[],
330 int dst_w, int dst_h,
331 int x0, int y0, int w, int h)
333 unsigned alpha, nb_planes, nb_comp, plane, comp;
334 int w_sub, h_sub, x_sub, y_sub, left, right, top, bottom, y;
337 /* TODO optimize if alpha = 0xFF */
338 clip_interval(dst_w, &x0, &w, NULL);
339 clip_interval(dst_h, &y0, &h, NULL);
340 if (w <= 0 || h <= 0 || !color->rgba[3])
342 /* 0x10203 * alpha + 2 is in the [ 2 ; 0x1010101 - 2 ] range */
343 alpha = 0x10203 * color->rgba[3] + 0x2;
344 nb_planes = (draw->nb_planes - 1) | 1; /* eliminate alpha */
345 for (plane = 0; plane < nb_planes; plane++) {
346 nb_comp = draw->pixelstep[plane];
347 p0 = pointer_at(draw, dst, dst_linesize, plane, x0, y0);
352 subsampling_bounds(draw->hsub[plane], &x_sub, &w_sub, &left, &right);
353 subsampling_bounds(draw->vsub[plane], &y_sub, &h_sub, &top, &bottom);
354 for (comp = 0; comp < nb_comp; comp++) {
355 if (!component_used(draw, plane, comp))
359 blend_line(p, color->comp[plane].u8[comp], alpha >> 1,
360 draw->pixelstep[plane], w_sub,
361 draw->hsub[plane], left, right);
362 p += dst_linesize[plane];
364 for (y = 0; y < h_sub; y++) {
365 blend_line(p, color->comp[plane].u8[comp], alpha,
366 draw->pixelstep[plane], w_sub,
367 draw->hsub[plane], left, right);
368 p += dst_linesize[plane];
371 blend_line(p, color->comp[plane].u8[comp], alpha >> 1,
372 draw->pixelstep[plane], w_sub,
373 draw->hsub[plane], left, right);
378 static void blend_pixel(uint8_t *dst, unsigned src, unsigned alpha,
379 uint8_t *mask, int mask_linesize, int l2depth,
380 unsigned w, unsigned h, unsigned shift, unsigned xm0)
382 unsigned xm, x, y, t = 0;
383 unsigned xmshf = 3 - l2depth;
384 unsigned xmmod = 7 >> l2depth;
385 unsigned mbits = (1 << (1 << l2depth)) - 1;
386 unsigned mmult = 255 / mbits;
388 for (y = 0; y < h; y++) {
390 for (x = 0; x < w; x++) {
391 t += ((mask[xm >> xmshf] >> ((~xm & xmmod) << l2depth)) & mbits)
395 mask += mask_linesize;
397 alpha = (t >> shift) * alpha;
398 *dst = ((0x1010101 - alpha) * *dst + alpha * src) >> 24;
401 static void blend_line_hv(uint8_t *dst, int dst_delta,
402 unsigned src, unsigned alpha,
403 uint8_t *mask, int mask_linesize, int l2depth, int w,
404 unsigned hsub, unsigned vsub,
405 int xm, int left, int right, int hband)
410 blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
411 left, hband, hsub + vsub, xm);
415 for (x = 0; x < w; x++) {
416 blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
417 1 << hsub, hband, hsub + vsub, xm);
422 blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
423 right, hband, hsub + vsub, xm);
426 void ff_blend_mask(FFDrawContext *draw, FFDrawColor *color,
427 uint8_t *dst[], int dst_linesize[], int dst_w, int dst_h,
428 uint8_t *mask, int mask_linesize, int mask_w, int mask_h,
429 int l2depth, unsigned endianness, int x0, int y0)
431 unsigned alpha, nb_planes, nb_comp, plane, comp;
432 int xm0, ym0, w_sub, h_sub, x_sub, y_sub, left, right, top, bottom, y;
435 clip_interval(dst_w, &x0, &mask_w, &xm0);
436 clip_interval(dst_h, &y0, &mask_h, &ym0);
437 mask += ym0 * mask_linesize;
438 if (mask_w <= 0 || mask_h <= 0 || !color->rgba[3])
440 /* alpha is in the [ 0 ; 0x10203 ] range,
441 alpha * mask is in the [ 0 ; 0x1010101 - 4 ] range */
442 alpha = (0x10307 * color->rgba[3] + 0x3) >> 8;
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 if (!component_used(draw, plane, comp))
459 blend_line_hv(p, draw->pixelstep[plane],
460 color->comp[plane].u8[comp], alpha,
461 m, mask_linesize, l2depth, w_sub,
462 draw->hsub[plane], draw->vsub[plane],
463 xm0, left, right, top);
464 p += dst_linesize[plane];
465 m += top * mask_linesize;
467 for (y = 0; y < h_sub; y++) {
468 blend_line_hv(p, draw->pixelstep[plane],
469 color->comp[plane].u8[comp], alpha,
470 m, mask_linesize, l2depth, w_sub,
471 draw->hsub[plane], draw->vsub[plane],
472 xm0, left, right, 1 << draw->vsub[plane]);
473 p += dst_linesize[plane];
474 m += mask_linesize << draw->vsub[plane];
477 blend_line_hv(p, draw->pixelstep[plane],
478 color->comp[plane].u8[comp], alpha,
479 m, mask_linesize, l2depth, w_sub,
480 draw->hsub[plane], draw->vsub[plane],
481 xm0, left, right, bottom);
486 int ff_draw_round_to_sub(FFDrawContext *draw, int sub_dir, int round_dir,
489 unsigned shift = sub_dir ? draw->vsub_max : draw->hsub_max;
494 value += round_dir ? (1 << shift) - 1 : 1 << (shift - 1);
495 return (value >> shift) << shift;
498 AVFilterFormats *ff_draw_supported_pixel_formats(unsigned flags)
500 enum PixelFormat i, pix_fmts[PIX_FMT_NB + 1];
504 for (i = 0; i < PIX_FMT_NB; i++)
505 if (ff_draw_init(&draw, i, flags) >= 0)
507 pix_fmts[n++] = PIX_FMT_NONE;
508 return avfilter_make_format_list(pix_fmts);
518 const AVPixFmtDescriptor *desc;
523 for (f = 0; f < PIX_FMT_NB; f++) {
524 desc = &av_pix_fmt_descriptors[f];
527 printf("Testing %s...%*s", desc->name,
528 (int)(16 - strlen(desc->name)), "");
529 r = ff_draw_init(&draw, f, 0);
532 av_strerror(r, buf, sizeof(buf));
533 printf("no: %s\n", buf);
536 ff_draw_color(&draw, &color, (uint8_t[]) { 1, 0, 0, 1 });
537 for (i = 0; i < sizeof(color); i++)
538 if (((uint8_t *)&color)[i] != 128)
540 if (i == sizeof(color)) {
541 printf("fallback color\n");