FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
drawutils.c
Go to the documentation of this file.
1 /*
2  * Copyright 2011 Stefano Sabatini <stefano.sabatini-lala poste it>
3  * Copyright 2012 Nicolas George <nicolas.george normalesup org>
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21 
22 #include <string.h>
23 
24 #include "libavutil/avutil.h"
25 #include "libavutil/colorspace.h"
26 #include "libavutil/mem.h"
27 #include "libavutil/pixdesc.h"
28 #include "drawutils.h"
29 #include "formats.h"
30 
31 enum { RED = 0, GREEN, BLUE, ALPHA };
32 
34 {
35  switch (pix_fmt) {
36  case AV_PIX_FMT_0RGB:
37  case AV_PIX_FMT_ARGB: rgba_map[ALPHA] = 0; rgba_map[RED ] = 1; rgba_map[GREEN] = 2; rgba_map[BLUE ] = 3; break;
38  case AV_PIX_FMT_0BGR:
39  case AV_PIX_FMT_ABGR: rgba_map[ALPHA] = 0; rgba_map[BLUE ] = 1; rgba_map[GREEN] = 2; rgba_map[RED ] = 3; break;
40  case AV_PIX_FMT_RGB48LE:
41  case AV_PIX_FMT_RGB48BE:
44  case AV_PIX_FMT_RGB0:
45  case AV_PIX_FMT_RGBA:
46  case AV_PIX_FMT_RGB24: rgba_map[RED ] = 0; rgba_map[GREEN] = 1; rgba_map[BLUE ] = 2; rgba_map[ALPHA] = 3; break;
47  case AV_PIX_FMT_BGR48LE:
48  case AV_PIX_FMT_BGR48BE:
51  case AV_PIX_FMT_BGRA:
52  case AV_PIX_FMT_BGR0:
53  case AV_PIX_FMT_BGR24: rgba_map[BLUE ] = 0; rgba_map[GREEN] = 1; rgba_map[RED ] = 2; rgba_map[ALPHA] = 3; break;
54  case AV_PIX_FMT_GBRAP:
55  case AV_PIX_FMT_GBRP: rgba_map[GREEN] = 0; rgba_map[BLUE ] = 1; rgba_map[RED ] = 2; rgba_map[ALPHA] = 3; break;
56  default: /* unsupported */
57  return AVERROR(EINVAL);
58  }
59  return 0;
60 }
61 
62 int ff_fill_line_with_color(uint8_t *line[4], int pixel_step[4], int w, uint8_t dst_color[4],
63  enum AVPixelFormat pix_fmt, uint8_t rgba_color[4],
64  int *is_packed_rgba, uint8_t rgba_map_ptr[4])
65 {
66  uint8_t rgba_map[4] = {0};
67  int i;
68  const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(pix_fmt);
69  int hsub = pix_desc->log2_chroma_w;
70 
71  *is_packed_rgba = ff_fill_rgba_map(rgba_map, pix_fmt) >= 0;
72 
73  if (*is_packed_rgba) {
74  pixel_step[0] = (av_get_bits_per_pixel(pix_desc))>>3;
75  for (i = 0; i < 4; i++)
76  dst_color[rgba_map[i]] = rgba_color[i];
77 
78  line[0] = av_malloc(w * pixel_step[0]);
79  for (i = 0; i < w; i++)
80  memcpy(line[0] + i * pixel_step[0], dst_color, pixel_step[0]);
81  if (rgba_map_ptr)
82  memcpy(rgba_map_ptr, rgba_map, sizeof(rgba_map[0]) * 4);
83  } else {
84  int plane;
85 
86  dst_color[0] = RGB_TO_Y_CCIR(rgba_color[0], rgba_color[1], rgba_color[2]);
87  dst_color[1] = RGB_TO_U_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
88  dst_color[2] = RGB_TO_V_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
89  dst_color[3] = rgba_color[3];
90 
91  for (plane = 0; plane < 4; plane++) {
92  int line_size;
93  int hsub1 = (plane == 1 || plane == 2) ? hsub : 0;
94 
95  pixel_step[plane] = 1;
96  line_size = (w >> hsub1) * pixel_step[plane];
97  line[plane] = av_malloc(line_size);
98  memset(line[plane], dst_color[plane], line_size);
99  }
100  }
101 
102  return 0;
103 }
104 
105 void ff_draw_rectangle(uint8_t *dst[4], int dst_linesize[4],
106  uint8_t *src[4], int pixelstep[4],
107  int hsub, int vsub, int x, int y, int w, int h)
108 {
109  int i, plane;
110  uint8_t *p;
111 
112  for (plane = 0; plane < 4 && dst[plane]; plane++) {
113  int hsub1 = plane == 1 || plane == 2 ? hsub : 0;
114  int vsub1 = plane == 1 || plane == 2 ? vsub : 0;
115 
116  p = dst[plane] + (y >> vsub1) * dst_linesize[plane];
117  for (i = 0; i < (h >> vsub1); i++) {
118  memcpy(p + (x >> hsub1) * pixelstep[plane],
119  src[plane], (w >> hsub1) * pixelstep[plane]);
120  p += dst_linesize[plane];
121  }
122  }
123 }
124 
125 void ff_copy_rectangle(uint8_t *dst[4], int dst_linesize[4],
126  uint8_t *src[4], int src_linesize[4], int pixelstep[4],
127  int hsub, int vsub, int x, int y, int y2, int w, int h)
128 {
129  int i, plane;
130  uint8_t *p;
131 
132  for (plane = 0; plane < 4 && dst[plane]; plane++) {
133  int hsub1 = plane == 1 || plane == 2 ? hsub : 0;
134  int vsub1 = plane == 1 || plane == 2 ? vsub : 0;
135 
136  p = dst[plane] + (y >> vsub1) * dst_linesize[plane];
137  for (i = 0; i < (h >> vsub1); i++) {
138  memcpy(p + (x >> hsub1) * pixelstep[plane],
139  src[plane] + src_linesize[plane]*(i+(y2>>vsub1)), (w >> hsub1) * pixelstep[plane]);
140  p += dst_linesize[plane];
141  }
142  }
143 }
144 
145 int ff_draw_init(FFDrawContext *draw, enum AVPixelFormat format, unsigned flags)
146 {
147  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);
148  const AVComponentDescriptor *c;
149  unsigned i, nb_planes = 0;
150  int pixelstep[MAX_PLANES] = { 0 };
151 
152  if (!desc->name)
153  return AVERROR(EINVAL);
155  return AVERROR(ENOSYS);
156  for (i = 0; i < desc->nb_components; i++) {
157  c = &desc->comp[i];
158  /* for now, only 8-bits formats */
159  if (c->depth_minus1 != 8 - 1)
160  return AVERROR(ENOSYS);
161  if (c->plane >= MAX_PLANES)
162  return AVERROR(ENOSYS);
163  /* strange interleaving */
164  if (pixelstep[c->plane] != 0 &&
165  pixelstep[c->plane] != c->step_minus1 + 1)
166  return AVERROR(ENOSYS);
167  pixelstep[c->plane] = c->step_minus1 + 1;
168  if (pixelstep[c->plane] >= 8)
169  return AVERROR(ENOSYS);
170  nb_planes = FFMAX(nb_planes, c->plane + 1);
171  }
172  if ((desc->log2_chroma_w || desc->log2_chroma_h) && nb_planes < 3)
173  return AVERROR(ENOSYS); /* exclude NV12 and NV21 */
174  memset(draw, 0, sizeof(*draw));
175  draw->desc = desc;
176  draw->format = format;
177  draw->nb_planes = nb_planes;
178  memcpy(draw->pixelstep, pixelstep, sizeof(draw->pixelstep));
179  if (nb_planes >= 3 && !(desc->flags & AV_PIX_FMT_FLAG_RGB)) {
180  draw->hsub[1] = draw->hsub[2] = draw->hsub_max = desc->log2_chroma_w;
181  draw->vsub[1] = draw->vsub[2] = draw->vsub_max = desc->log2_chroma_h;
182  }
183  for (i = 0; i < ((desc->nb_components - 1) | 1); i++)
184  draw->comp_mask[desc->comp[i].plane] |=
185  1 << (desc->comp[i].offset_plus1 - 1);
186  return 0;
187 }
188 
190 {
191  unsigned i;
192  uint8_t rgba_map[4];
193 
194  if (rgba != color->rgba)
195  memcpy(color->rgba, rgba, sizeof(color->rgba));
196  if ((draw->desc->flags & AV_PIX_FMT_FLAG_RGB) && draw->nb_planes == 1 &&
197  ff_fill_rgba_map(rgba_map, draw->format) >= 0) {
198  for (i = 0; i < 4; i++)
199  color->comp[0].u8[rgba_map[i]] = rgba[i];
200  } else if (draw->nb_planes == 3 || draw->nb_planes == 4) {
201  /* assume YUV */
202  color->comp[0].u8[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
203  color->comp[1].u8[0] = RGB_TO_U_CCIR(rgba[0], rgba[1], rgba[2], 0);
204  color->comp[2].u8[0] = RGB_TO_V_CCIR(rgba[0], rgba[1], rgba[2], 0);
205  color->comp[3].u8[0] = rgba[3];
206  } else if (draw->format == AV_PIX_FMT_GRAY8 || draw->format == AV_PIX_FMT_GRAY8A) {
207  color->comp[0].u8[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
208  color->comp[1].u8[0] = rgba[3];
209  } else {
211  "Color conversion not implemented for %s\n", draw->desc->name);
212  memset(color, 128, sizeof(*color));
213  }
214 }
215 
216 static uint8_t *pointer_at(FFDrawContext *draw, uint8_t *data[], int linesize[],
217  int plane, int x, int y)
218 {
219  return data[plane] +
220  (y >> draw->vsub[plane]) * linesize[plane] +
221  (x >> draw->hsub[plane]) * draw->pixelstep[plane];
222 }
223 
225  uint8_t *dst[], int dst_linesize[],
226  uint8_t *src[], int src_linesize[],
227  int dst_x, int dst_y, int src_x, int src_y,
228  int w, int h)
229 {
230  int plane, y, wp, hp;
231  uint8_t *p, *q;
232 
233  for (plane = 0; plane < draw->nb_planes; plane++) {
234  p = pointer_at(draw, src, src_linesize, plane, src_x, src_y);
235  q = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
236  wp = (w >> draw->hsub[plane]) * draw->pixelstep[plane];
237  hp = (h >> draw->vsub[plane]);
238  for (y = 0; y < hp; y++) {
239  memcpy(q, p, wp);
240  p += src_linesize[plane];
241  q += dst_linesize[plane];
242  }
243  }
244 }
245 
247  uint8_t *dst[], int dst_linesize[],
248  int dst_x, int dst_y, int w, int h)
249 {
250  int plane, x, y, wp, hp;
251  uint8_t *p0, *p;
252 
253  for (plane = 0; plane < draw->nb_planes; plane++) {
254  p0 = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
255  wp = (w >> draw->hsub[plane]);
256  hp = (h >> draw->vsub[plane]);
257  if (!hp)
258  return;
259  p = p0;
260  /* copy first line from color */
261  for (x = 0; x < wp; x++) {
262  memcpy(p, color->comp[plane].u8, draw->pixelstep[plane]);
263  p += draw->pixelstep[plane];
264  }
265  wp *= draw->pixelstep[plane];
266  /* copy next lines from first line */
267  p = p0 + dst_linesize[plane];
268  for (y = 1; y < hp; y++) {
269  memcpy(p, p0, wp);
270  p += dst_linesize[plane];
271  }
272  }
273 }
274 
275 /**
276  * Clip interval [x; x+w[ within [0; wmax[.
277  * The resulting w may be negative if the final interval is empty.
278  * dx, if not null, return the difference between in and out value of x.
279  */
280 static void clip_interval(int wmax, int *x, int *w, int *dx)
281 {
282  if (dx)
283  *dx = 0;
284  if (*x < 0) {
285  if (dx)
286  *dx = -*x;
287  *w += *x;
288  *x = 0;
289  }
290  if (*x + *w > wmax)
291  *w = wmax - *x;
292 }
293 
294 /**
295  * Decompose w pixels starting at x
296  * into start + (w starting at x) + end
297  * with x and w aligned on multiples of 1<<sub.
298  */
299 static void subsampling_bounds(int sub, int *x, int *w, int *start, int *end)
300 {
301  int mask = (1 << sub) - 1;
302 
303  *start = (-*x) & mask;
304  *x += *start;
305  *start = FFMIN(*start, *w);
306  *w -= *start;
307  *end = *w & mask;
308  *w >>= sub;
309 }
310 
311 static int component_used(FFDrawContext *draw, int plane, int comp)
312 {
313  return (draw->comp_mask[plane] >> comp) & 1;
314 }
315 
316 /* If alpha is in the [ 0 ; 0x1010101 ] range,
317  then alpha * value is in the [ 0 ; 0xFFFFFFFF ] range,
318  and >> 24 gives a correct rounding. */
319 static void blend_line(uint8_t *dst, unsigned src, unsigned alpha,
320  int dx, int w, unsigned hsub, int left, int right)
321 {
322  unsigned asrc = alpha * src;
323  unsigned tau = 0x1010101 - alpha;
324  int x;
325 
326  if (left) {
327  unsigned suba = (left * alpha) >> hsub;
328  *dst = (*dst * (0x1010101 - suba) + src * suba) >> 24;
329  dst += dx;
330  }
331  for (x = 0; x < w; x++) {
332  *dst = (*dst * tau + asrc) >> 24;
333  dst += dx;
334  }
335  if (right) {
336  unsigned suba = (right * alpha) >> hsub;
337  *dst = (*dst * (0x1010101 - suba) + src * suba) >> 24;
338  }
339 }
340 
342  uint8_t *dst[], int dst_linesize[],
343  int dst_w, int dst_h,
344  int x0, int y0, int w, int h)
345 {
346  unsigned alpha, nb_planes, nb_comp, plane, comp;
347  int w_sub, h_sub, x_sub, y_sub, left, right, top, bottom, y;
348  uint8_t *p0, *p;
349 
350  /* TODO optimize if alpha = 0xFF */
351  clip_interval(dst_w, &x0, &w, NULL);
352  clip_interval(dst_h, &y0, &h, NULL);
353  if (w <= 0 || h <= 0 || !color->rgba[3])
354  return;
355  /* 0x10203 * alpha + 2 is in the [ 2 ; 0x1010101 - 2 ] range */
356  alpha = 0x10203 * color->rgba[3] + 0x2;
357  nb_planes = (draw->nb_planes - 1) | 1; /* eliminate alpha */
358  for (plane = 0; plane < nb_planes; plane++) {
359  nb_comp = draw->pixelstep[plane];
360  p0 = pointer_at(draw, dst, dst_linesize, plane, x0, y0);
361  w_sub = w;
362  h_sub = h;
363  x_sub = x0;
364  y_sub = y0;
365  subsampling_bounds(draw->hsub[plane], &x_sub, &w_sub, &left, &right);
366  subsampling_bounds(draw->vsub[plane], &y_sub, &h_sub, &top, &bottom);
367  for (comp = 0; comp < nb_comp; comp++) {
368  if (!component_used(draw, plane, comp))
369  continue;
370  p = p0 + comp;
371  if (top) {
372  blend_line(p, color->comp[plane].u8[comp], alpha >> 1,
373  draw->pixelstep[plane], w_sub,
374  draw->hsub[plane], left, right);
375  p += dst_linesize[plane];
376  }
377  for (y = 0; y < h_sub; y++) {
378  blend_line(p, color->comp[plane].u8[comp], alpha,
379  draw->pixelstep[plane], w_sub,
380  draw->hsub[plane], left, right);
381  p += dst_linesize[plane];
382  }
383  if (bottom)
384  blend_line(p, color->comp[plane].u8[comp], alpha >> 1,
385  draw->pixelstep[plane], w_sub,
386  draw->hsub[plane], left, right);
387  }
388  }
389 }
390 
391 static void blend_pixel(uint8_t *dst, unsigned src, unsigned alpha,
392  uint8_t *mask, int mask_linesize, int l2depth,
393  unsigned w, unsigned h, unsigned shift, unsigned xm0)
394 {
395  unsigned xm, x, y, t = 0;
396  unsigned xmshf = 3 - l2depth;
397  unsigned xmmod = 7 >> l2depth;
398  unsigned mbits = (1 << (1 << l2depth)) - 1;
399  unsigned mmult = 255 / mbits;
400 
401  for (y = 0; y < h; y++) {
402  xm = xm0;
403  for (x = 0; x < w; x++) {
404  t += ((mask[xm >> xmshf] >> ((~xm & xmmod) << l2depth)) & mbits)
405  * mmult;
406  xm++;
407  }
408  mask += mask_linesize;
409  }
410  alpha = (t >> shift) * alpha;
411  *dst = ((0x1010101 - alpha) * *dst + alpha * src) >> 24;
412 }
413 
414 static void blend_line_hv(uint8_t *dst, int dst_delta,
415  unsigned src, unsigned alpha,
416  uint8_t *mask, int mask_linesize, int l2depth, int w,
417  unsigned hsub, unsigned vsub,
418  int xm, int left, int right, int hband)
419 {
420  int x;
421 
422  if (left) {
423  blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
424  left, hband, hsub + vsub, xm);
425  dst += dst_delta;
426  xm += left;
427  }
428  for (x = 0; x < w; x++) {
429  blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
430  1 << hsub, hband, hsub + vsub, xm);
431  dst += dst_delta;
432  xm += 1 << hsub;
433  }
434  if (right)
435  blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
436  right, hband, hsub + vsub, xm);
437 }
438 
440  uint8_t *dst[], int dst_linesize[], int dst_w, int dst_h,
441  uint8_t *mask, int mask_linesize, int mask_w, int mask_h,
442  int l2depth, unsigned endianness, int x0, int y0)
443 {
444  unsigned alpha, nb_planes, nb_comp, plane, comp;
445  int xm0, ym0, w_sub, h_sub, x_sub, y_sub, left, right, top, bottom, y;
446  uint8_t *p0, *p, *m;
447 
448  clip_interval(dst_w, &x0, &mask_w, &xm0);
449  clip_interval(dst_h, &y0, &mask_h, &ym0);
450  mask += ym0 * mask_linesize;
451  if (mask_w <= 0 || mask_h <= 0 || !color->rgba[3])
452  return;
453  /* alpha is in the [ 0 ; 0x10203 ] range,
454  alpha * mask is in the [ 0 ; 0x1010101 - 4 ] range */
455  alpha = (0x10307 * color->rgba[3] + 0x3) >> 8;
456  nb_planes = (draw->nb_planes - 1) | 1; /* eliminate alpha */
457  for (plane = 0; plane < nb_planes; plane++) {
458  nb_comp = draw->pixelstep[plane];
459  p0 = pointer_at(draw, dst, dst_linesize, plane, x0, y0);
460  w_sub = mask_w;
461  h_sub = mask_h;
462  x_sub = x0;
463  y_sub = y0;
464  subsampling_bounds(draw->hsub[plane], &x_sub, &w_sub, &left, &right);
465  subsampling_bounds(draw->vsub[plane], &y_sub, &h_sub, &top, &bottom);
466  for (comp = 0; comp < nb_comp; comp++) {
467  if (!component_used(draw, plane, comp))
468  continue;
469  p = p0 + comp;
470  m = mask;
471  if (top) {
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, top);
477  p += dst_linesize[plane];
478  m += top * mask_linesize;
479  }
480  for (y = 0; y < h_sub; y++) {
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, 1 << draw->vsub[plane]);
486  p += dst_linesize[plane];
487  m += mask_linesize << draw->vsub[plane];
488  }
489  if (bottom)
490  blend_line_hv(p, draw->pixelstep[plane],
491  color->comp[plane].u8[comp], alpha,
492  m, mask_linesize, l2depth, w_sub,
493  draw->hsub[plane], draw->vsub[plane],
494  xm0, left, right, bottom);
495  }
496  }
497 }
498 
499 int ff_draw_round_to_sub(FFDrawContext *draw, int sub_dir, int round_dir,
500  int value)
501 {
502  unsigned shift = sub_dir ? draw->vsub_max : draw->hsub_max;
503 
504  if (!shift)
505  return value;
506  if (round_dir >= 0)
507  value += round_dir ? (1 << shift) - 1 : 1 << (shift - 1);
508  return (value >> shift) << shift;
509 }
510 
512 {
513  enum AVPixelFormat i, pix_fmts[AV_PIX_FMT_NB + 1];
514  unsigned n = 0;
515  FFDrawContext draw;
516 
517  for (i = 0; i < AV_PIX_FMT_NB; i++)
518  if (ff_draw_init(&draw, i, flags) >= 0)
519  pix_fmts[n++] = i;
520  pix_fmts[n++] = AV_PIX_FMT_NONE;
521  return ff_make_format_list(pix_fmts);
522 }
523 
524 #ifdef TEST
525 
526 #undef printf
527 
528 int main(void)
529 {
530  enum AVPixelFormat f;
531  const AVPixFmtDescriptor *desc;
532  FFDrawContext draw;
534  int r, i;
535 
536  for (f = 0; f < AV_PIX_FMT_NB; f++) {
537  desc = av_pix_fmt_desc_get(f);
538  if (!desc->name)
539  continue;
540  printf("Testing %s...%*s", desc->name,
541  (int)(16 - strlen(desc->name)), "");
542  r = ff_draw_init(&draw, f, 0);
543  if (r < 0) {
544  char buf[128];
545  av_strerror(r, buf, sizeof(buf));
546  printf("no: %s\n", buf);
547  continue;
548  }
549  ff_draw_color(&draw, &color, (uint8_t[]) { 1, 0, 0, 1 });
550  for (i = 0; i < sizeof(color); i++)
551  if (((uint8_t *)&color)[i] != 128)
552  break;
553  if (i == sizeof(color)) {
554  printf("fallback color\n");
555  continue;
556  }
557  printf("ok\n");
558  }
559  return 0;
560 }
561 
562 #endif