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/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"
31 #include "formats.h"
32 
33 enum { RED = 0, GREEN, BLUE, ALPHA };
34 
36 {
37  switch (pix_fmt) {
38  case AV_PIX_FMT_0RGB:
39  case AV_PIX_FMT_ARGB: rgba_map[ALPHA] = 0; rgba_map[RED ] = 1; rgba_map[GREEN] = 2; rgba_map[BLUE ] = 3; break;
40  case AV_PIX_FMT_0BGR:
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:
46  case AV_PIX_FMT_RGB0:
47  case AV_PIX_FMT_RGBA:
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:
53  case AV_PIX_FMT_BGRA:
54  case AV_PIX_FMT_BGR0:
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_GBRP9LE:
57  case AV_PIX_FMT_GBRP9BE:
66  case AV_PIX_FMT_GBRAP:
71  case AV_PIX_FMT_GBRP: rgba_map[GREEN] = 0; rgba_map[BLUE ] = 1; rgba_map[RED ] = 2; rgba_map[ALPHA] = 3; break;
72  default: /* unsupported */
73  return AVERROR(EINVAL);
74  }
75  return 0;
76 }
77 
78 int ff_fill_line_with_color(uint8_t *line[4], int pixel_step[4], int w, uint8_t dst_color[4],
79  enum AVPixelFormat pix_fmt, uint8_t rgba_color[4],
80  int *is_packed_rgba, uint8_t rgba_map_ptr[4])
81 {
82  uint8_t rgba_map[4] = {0};
83  int i;
84  const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(pix_fmt);
85  int hsub;
86 
87  av_assert0(pix_desc);
88 
89  hsub = pix_desc->log2_chroma_w;
90 
91  *is_packed_rgba = ff_fill_rgba_map(rgba_map, pix_fmt) >= 0;
92 
93  if (*is_packed_rgba) {
94  pixel_step[0] = (av_get_bits_per_pixel(pix_desc))>>3;
95  for (i = 0; i < 4; i++)
96  dst_color[rgba_map[i]] = rgba_color[i];
97 
98  line[0] = av_malloc_array(w, pixel_step[0]);
99  if (!line[0])
100  return AVERROR(ENOMEM);
101  for (i = 0; i < w; i++)
102  memcpy(line[0] + i * pixel_step[0], dst_color, pixel_step[0]);
103  if (rgba_map_ptr)
104  memcpy(rgba_map_ptr, rgba_map, sizeof(rgba_map[0]) * 4);
105  } else {
106  int plane;
107 
108  dst_color[0] = RGB_TO_Y_CCIR(rgba_color[0], rgba_color[1], rgba_color[2]);
109  dst_color[1] = RGB_TO_U_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
110  dst_color[2] = RGB_TO_V_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
111  dst_color[3] = rgba_color[3];
112 
113  for (plane = 0; plane < 4; plane++) {
114  int line_size;
115  int hsub1 = (plane == 1 || plane == 2) ? hsub : 0;
116 
117  pixel_step[plane] = 1;
118  line_size = AV_CEIL_RSHIFT(w, hsub1) * pixel_step[plane];
119  line[plane] = av_malloc(line_size);
120  if (!line[plane]) {
121  while(plane && line[plane-1])
122  av_freep(&line[--plane]);
123  return AVERROR(ENOMEM);
124  }
125  memset(line[plane], dst_color[plane], line_size);
126  }
127  }
128 
129  return 0;
130 }
131 
132 void ff_draw_rectangle(uint8_t *dst[4], int dst_linesize[4],
133  uint8_t *src[4], int pixelstep[4],
134  int hsub, int vsub, int x, int y, int w, int h)
135 {
136  int i, plane;
137  uint8_t *p;
138 
139  for (plane = 0; plane < 4 && dst[plane]; plane++) {
140  int hsub1 = plane == 1 || plane == 2 ? hsub : 0;
141  int vsub1 = plane == 1 || plane == 2 ? vsub : 0;
142  int width = AV_CEIL_RSHIFT(w, hsub1);
143  int height = AV_CEIL_RSHIFT(h, vsub1);
144 
145  p = dst[plane] + (y >> vsub1) * dst_linesize[plane];
146  for (i = 0; i < height; i++) {
147  memcpy(p + (x >> hsub1) * pixelstep[plane],
148  src[plane], width * pixelstep[plane]);
149  p += dst_linesize[plane];
150  }
151  }
152 }
153 
154 void ff_copy_rectangle(uint8_t *dst[4], int dst_linesize[4],
155  uint8_t *src[4], int src_linesize[4], int pixelstep[4],
156  int hsub, int vsub, int x, int y, int y2, int w, int h)
157 {
158  int i, plane;
159  uint8_t *p;
160 
161  for (plane = 0; plane < 4 && dst[plane]; plane++) {
162  int hsub1 = plane == 1 || plane == 2 ? hsub : 0;
163  int vsub1 = plane == 1 || plane == 2 ? vsub : 0;
164  int width = AV_CEIL_RSHIFT(w, hsub1);
165  int height = AV_CEIL_RSHIFT(h, vsub1);
166 
167  p = dst[plane] + (y >> vsub1) * dst_linesize[plane];
168  for (i = 0; i < height; i++) {
169  memcpy(p + (x >> hsub1) * pixelstep[plane],
170  src[plane] + src_linesize[plane]*(i+(y2>>vsub1)), width * pixelstep[plane]);
171  p += dst_linesize[plane];
172  }
173  }
174 }
175 
177 {
179  const AVComponentDescriptor *c;
180  unsigned i, nb_planes = 0;
181  int pixelstep[MAX_PLANES] = { 0 };
182 
183  if (!desc || !desc->name)
184  return AVERROR(EINVAL);
186  return AVERROR(ENOSYS);
187  for (i = 0; i < desc->nb_components; i++) {
188  c = &desc->comp[i];
189  /* for now, only 8-16 bits formats */
190  if (c->depth < 8 || c->depth > 16)
191  return AVERROR(ENOSYS);
192  if (desc->flags & AV_PIX_FMT_FLAG_BE)
193  return AVERROR(ENOSYS);
194  if (c->plane >= MAX_PLANES)
195  return AVERROR(ENOSYS);
196  /* strange interleaving */
197  if (pixelstep[c->plane] != 0 &&
198  pixelstep[c->plane] != c->step)
199  return AVERROR(ENOSYS);
200  if (pixelstep[c->plane] == 6 &&
201  c->depth == 16)
202  return AVERROR(ENOSYS);
203  pixelstep[c->plane] = c->step;
204  if (pixelstep[c->plane] >= 8)
205  return AVERROR(ENOSYS);
206  nb_planes = FFMAX(nb_planes, c->plane + 1);
207  }
208  memset(draw, 0, sizeof(*draw));
209  draw->desc = desc;
210  draw->format = format;
211  draw->nb_planes = nb_planes;
212  memcpy(draw->pixelstep, pixelstep, sizeof(draw->pixelstep));
213  draw->hsub[1] = draw->hsub[2] = draw->hsub_max = desc->log2_chroma_w;
214  draw->vsub[1] = draw->vsub[2] = draw->vsub_max = desc->log2_chroma_h;
215  for (i = 0; i < (desc->nb_components - !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA)); i++)
216  draw->comp_mask[desc->comp[i].plane] |=
217  1 << desc->comp[i].offset;
218  return 0;
219 }
220 
222 {
223  unsigned i;
224  uint8_t rgba_map[4];
225 
226  if (rgba != color->rgba)
227  memcpy(color->rgba, rgba, sizeof(color->rgba));
228  if ((draw->desc->flags & AV_PIX_FMT_FLAG_RGB) &&
229  ff_fill_rgba_map(rgba_map, draw->format) >= 0) {
230  if (draw->nb_planes == 1) {
231  for (i = 0; i < 4; i++) {
232  color->comp[0].u8[rgba_map[i]] = rgba[i];
233  if (draw->desc->comp[rgba_map[i]].depth > 8) {
234  color->comp[0].u16[rgba_map[i]] = color->comp[0].u8[rgba_map[i]] << 8;
235  }
236  }
237  } else {
238  for (i = 0; i < 4; i++) {
239  color->comp[rgba_map[i]].u8[0] = rgba[i];
240  if (draw->desc->comp[rgba_map[i]].depth > 8)
241  color->comp[rgba_map[i]].u16[0] = color->comp[rgba_map[i]].u8[0] << (draw->desc->comp[rgba_map[i]].depth - 8);
242  }
243  }
244  } else if (draw->nb_planes >= 2) {
245  /* assume YUV */
246  const AVPixFmtDescriptor *desc = draw->desc;
247  color->comp[desc->comp[0].plane].u8[desc->comp[0].offset] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
248  color->comp[desc->comp[1].plane].u8[desc->comp[1].offset] = RGB_TO_U_CCIR(rgba[0], rgba[1], rgba[2], 0);
249  color->comp[desc->comp[2].plane].u8[desc->comp[2].offset] = RGB_TO_V_CCIR(rgba[0], rgba[1], rgba[2], 0);
250  color->comp[3].u8[0] = rgba[3];
251 #define EXPAND(compn) \
252  if (desc->comp[compn].depth > 8) \
253  color->comp[desc->comp[compn].plane].u16[desc->comp[compn].offset] = \
254  color->comp[desc->comp[compn].plane].u8[desc->comp[compn].offset] << (draw->desc->comp[compn].depth - 8)
255  EXPAND(3);
256  EXPAND(2);
257  EXPAND(1);
258  EXPAND(0);
259  } else if (draw->format == AV_PIX_FMT_GRAY8 || draw->format == AV_PIX_FMT_GRAY8A) {
260  color->comp[0].u8[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
261  color->comp[1].u8[0] = rgba[3];
262  } else if (draw->format == AV_PIX_FMT_GRAY16LE || draw->format == AV_PIX_FMT_YA16LE) {
263  color->comp[0].u8[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
264  color->comp[0].u16[0] = color->comp[0].u8[0] << 8;
265  color->comp[1].u8[0] = rgba[3];
266  color->comp[1].u16[0] = color->comp[1].u8[0] << 8;
267  } else {
269  "Color conversion not implemented for %s\n", draw->desc->name);
270  memset(color, 128, sizeof(*color));
271  }
272 }
273 
274 static uint8_t *pointer_at(FFDrawContext *draw, uint8_t *data[], int linesize[],
275  int plane, int x, int y)
276 {
277  return data[plane] +
278  (y >> draw->vsub[plane]) * linesize[plane] +
279  (x >> draw->hsub[plane]) * draw->pixelstep[plane];
280 }
281 
283  uint8_t *dst[], int dst_linesize[],
284  uint8_t *src[], int src_linesize[],
285  int dst_x, int dst_y, int src_x, int src_y,
286  int w, int h)
287 {
288  int plane, y, wp, hp;
289  uint8_t *p, *q;
290 
291  for (plane = 0; plane < draw->nb_planes; plane++) {
292  p = pointer_at(draw, src, src_linesize, plane, src_x, src_y);
293  q = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
294  wp = AV_CEIL_RSHIFT(w, draw->hsub[plane]) * draw->pixelstep[plane];
295  hp = AV_CEIL_RSHIFT(h, draw->vsub[plane]);
296  for (y = 0; y < hp; y++) {
297  memcpy(q, p, wp);
298  p += src_linesize[plane];
299  q += dst_linesize[plane];
300  }
301  }
302 }
303 
305  uint8_t *dst[], int dst_linesize[],
306  int dst_x, int dst_y, int w, int h)
307 {
308  int plane, x, y, wp, hp;
309  uint8_t *p0, *p;
310  FFDrawColor color_tmp = *color;
311 
312  for (plane = 0; plane < draw->nb_planes; plane++) {
313  p0 = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
314  wp = AV_CEIL_RSHIFT(w, draw->hsub[plane]);
315  hp = AV_CEIL_RSHIFT(h, draw->vsub[plane]);
316  if (!hp)
317  return;
318  p = p0;
319 
320  if (HAVE_BIGENDIAN && draw->desc->comp[0].depth > 8) {
321  for (x = 0; 2*x < draw->pixelstep[plane]; x++)
322  color_tmp.comp[plane].u16[x] = av_bswap16(color_tmp.comp[plane].u16[x]);
323  }
324 
325  /* copy first line from color */
326  for (x = 0; x < wp; x++) {
327  memcpy(p, color_tmp.comp[plane].u8, draw->pixelstep[plane]);
328  p += draw->pixelstep[plane];
329  }
330  wp *= draw->pixelstep[plane];
331  /* copy next lines from first line */
332  p = p0 + dst_linesize[plane];
333  for (y = 1; y < hp; y++) {
334  memcpy(p, p0, wp);
335  p += dst_linesize[plane];
336  }
337  }
338 }
339 
340 /**
341  * Clip interval [x; x+w[ within [0; wmax[.
342  * The resulting w may be negative if the final interval is empty.
343  * dx, if not null, return the difference between in and out value of x.
344  */
345 static void clip_interval(int wmax, int *x, int *w, int *dx)
346 {
347  if (dx)
348  *dx = 0;
349  if (*x < 0) {
350  if (dx)
351  *dx = -*x;
352  *w += *x;
353  *x = 0;
354  }
355  if (*x + *w > wmax)
356  *w = wmax - *x;
357 }
358 
359 /**
360  * Decompose w pixels starting at x
361  * into start + (w starting at x) + end
362  * with x and w aligned on multiples of 1<<sub.
363  */
364 static void subsampling_bounds(int sub, int *x, int *w, int *start, int *end)
365 {
366  int mask = (1 << sub) - 1;
367 
368  *start = (-*x) & mask;
369  *x += *start;
370  *start = FFMIN(*start, *w);
371  *w -= *start;
372  *end = *w & mask;
373  *w >>= sub;
374 }
375 
376 static int component_used(FFDrawContext *draw, int plane, int comp)
377 {
378  return (draw->comp_mask[plane] >> comp) & 1;
379 }
380 
381 /* If alpha is in the [ 0 ; 0x1010101 ] range,
382  then alpha * value is in the [ 0 ; 0xFFFFFFFF ] range,
383  and >> 24 gives a correct rounding. */
384 static void blend_line(uint8_t *dst, unsigned src, unsigned alpha,
385  int dx, int w, unsigned hsub, int left, int right)
386 {
387  unsigned asrc = alpha * src;
388  unsigned tau = 0x1010101 - alpha;
389  int x;
390 
391  if (left) {
392  unsigned suba = (left * alpha) >> hsub;
393  *dst = (*dst * (0x1010101 - suba) + src * suba) >> 24;
394  dst += dx;
395  }
396  for (x = 0; x < w; x++) {
397  *dst = (*dst * tau + asrc) >> 24;
398  dst += dx;
399  }
400  if (right) {
401  unsigned suba = (right * alpha) >> hsub;
402  *dst = (*dst * (0x1010101 - suba) + src * suba) >> 24;
403  }
404 }
405 
406 static void blend_line16(uint8_t *dst, unsigned src, unsigned alpha,
407  int dx, int w, unsigned hsub, int left, int right)
408 {
409  unsigned asrc = alpha * src;
410  unsigned tau = 0x10001 - alpha;
411  int x;
412 
413  if (left) {
414  unsigned suba = (left * alpha) >> hsub;
415  uint16_t value = AV_RL16(dst);
416  AV_WL16(dst, (value * (0x10001 - suba) + src * suba) >> 16);
417  dst += dx;
418  }
419  for (x = 0; x < w; x++) {
420  uint16_t value = AV_RL16(dst);
421  AV_WL16(dst, (value * tau + asrc) >> 16);
422  dst += dx;
423  }
424  if (right) {
425  unsigned suba = (right * alpha) >> hsub;
426  uint16_t value = AV_RL16(dst);
427  AV_WL16(dst, (value * (0x10001 - suba) + src * suba) >> 16);
428  }
429 }
430 
432  uint8_t *dst[], int dst_linesize[],
433  int dst_w, int dst_h,
434  int x0, int y0, int w, int h)
435 {
436  unsigned alpha, nb_planes, nb_comp, plane, comp;
437  int w_sub, h_sub, x_sub, y_sub, left, right, top, bottom, y;
438  uint8_t *p0, *p;
439 
440  /* TODO optimize if alpha = 0xFF */
441  clip_interval(dst_w, &x0, &w, NULL);
442  clip_interval(dst_h, &y0, &h, NULL);
443  if (w <= 0 || h <= 0 || !color->rgba[3])
444  return;
445  if (draw->desc->comp[0].depth <= 8) {
446  /* 0x10203 * alpha + 2 is in the [ 2 ; 0x1010101 - 2 ] range */
447  alpha = 0x10203 * color->rgba[3] + 0x2;
448  } else {
449  /* 0x101 * alpha is in the [ 2 ; 0x1001] range */
450  alpha = 0x101 * color->rgba[3] + 0x2;
451  }
452  nb_planes = draw->nb_planes - !!(draw->desc->flags & AV_PIX_FMT_FLAG_ALPHA);
453  for (plane = 0; plane < nb_planes; plane++) {
454  nb_comp = draw->pixelstep[plane];
455  p0 = pointer_at(draw, dst, dst_linesize, plane, x0, y0);
456  w_sub = w;
457  h_sub = h;
458  x_sub = x0;
459  y_sub = y0;
460  subsampling_bounds(draw->hsub[plane], &x_sub, &w_sub, &left, &right);
461  subsampling_bounds(draw->vsub[plane], &y_sub, &h_sub, &top, &bottom);
462  for (comp = 0; comp < nb_comp; comp++) {
463  const int depth = draw->desc->comp[comp].depth;
464 
465  if (!component_used(draw, plane, comp))
466  continue;
467  p = p0 + comp;
468  if (top) {
469  if (depth <= 8) {
470  blend_line(p, color->comp[plane].u8[comp], alpha >> 1,
471  draw->pixelstep[plane], w_sub,
472  draw->hsub[plane], left, right);
473  } else {
474  blend_line16(p, color->comp[plane].u16[comp], alpha >> 1,
475  draw->pixelstep[plane], w_sub,
476  draw->hsub[plane], left, right);
477  }
478  p += dst_linesize[plane];
479  }
480  if (depth <= 8) {
481  for (y = 0; y < h_sub; y++) {
482  blend_line(p, color->comp[plane].u8[comp], alpha,
483  draw->pixelstep[plane], w_sub,
484  draw->hsub[plane], left, right);
485  p += dst_linesize[plane];
486  }
487  } else {
488  for (y = 0; y < h_sub; y++) {
489  blend_line16(p, color->comp[plane].u16[comp], alpha,
490  draw->pixelstep[plane], w_sub,
491  draw->hsub[plane], left, right);
492  p += dst_linesize[plane];
493  }
494  }
495  if (bottom) {
496  if (depth <= 8) {
497  blend_line(p, color->comp[plane].u8[comp], alpha >> 1,
498  draw->pixelstep[plane], w_sub,
499  draw->hsub[plane], left, right);
500  } else {
501  blend_line16(p, color->comp[plane].u16[comp], alpha >> 1,
502  draw->pixelstep[plane], w_sub,
503  draw->hsub[plane], left, right);
504  }
505  }
506  }
507  }
508 }
509 
510 static void blend_pixel16(uint8_t *dst, unsigned src, unsigned alpha,
511  const uint8_t *mask, int mask_linesize, int l2depth,
512  unsigned w, unsigned h, unsigned shift, unsigned xm0)
513 {
514  unsigned xm, x, y, t = 0;
515  unsigned xmshf = 3 - l2depth;
516  unsigned xmmod = 7 >> l2depth;
517  unsigned mbits = (1 << (1 << l2depth)) - 1;
518  unsigned mmult = 255 / mbits;
519  uint16_t value = AV_RL16(dst);
520 
521  for (y = 0; y < h; y++) {
522  xm = xm0;
523  for (x = 0; x < w; x++) {
524  t += ((mask[xm >> xmshf] >> ((~xm & xmmod) << l2depth)) & mbits)
525  * mmult;
526  xm++;
527  }
528  mask += mask_linesize;
529  }
530  alpha = (t >> shift) * alpha;
531  AV_WL16(dst, ((0x10001 - alpha) * value + alpha * src) >> 16);
532 }
533 
534 static void blend_pixel(uint8_t *dst, unsigned src, unsigned alpha,
535  const uint8_t *mask, int mask_linesize, int l2depth,
536  unsigned w, unsigned h, unsigned shift, unsigned xm0)
537 {
538  unsigned xm, x, y, t = 0;
539  unsigned xmshf = 3 - l2depth;
540  unsigned xmmod = 7 >> l2depth;
541  unsigned mbits = (1 << (1 << l2depth)) - 1;
542  unsigned mmult = 255 / mbits;
543 
544  for (y = 0; y < h; y++) {
545  xm = xm0;
546  for (x = 0; x < w; x++) {
547  t += ((mask[xm >> xmshf] >> ((~xm & xmmod) << l2depth)) & mbits)
548  * mmult;
549  xm++;
550  }
551  mask += mask_linesize;
552  }
553  alpha = (t >> shift) * alpha;
554  *dst = ((0x1010101 - alpha) * *dst + alpha * src) >> 24;
555 }
556 
557 static void blend_line_hv16(uint8_t *dst, int dst_delta,
558  unsigned src, unsigned alpha,
559  const uint8_t *mask, int mask_linesize, int l2depth, int w,
560  unsigned hsub, unsigned vsub,
561  int xm, int left, int right, int hband)
562 {
563  int x;
564 
565  if (left) {
566  blend_pixel16(dst, src, alpha, mask, mask_linesize, l2depth,
567  left, hband, hsub + vsub, xm);
568  dst += dst_delta;
569  xm += left;
570  }
571  for (x = 0; x < w; x++) {
572  blend_pixel16(dst, src, alpha, mask, mask_linesize, l2depth,
573  1 << hsub, hband, hsub + vsub, xm);
574  dst += dst_delta;
575  xm += 1 << hsub;
576  }
577  if (right)
578  blend_pixel16(dst, src, alpha, mask, mask_linesize, l2depth,
579  right, hband, hsub + vsub, xm);
580 }
581 
582 static void blend_line_hv(uint8_t *dst, int dst_delta,
583  unsigned src, unsigned alpha,
584  const uint8_t *mask, int mask_linesize, int l2depth, int w,
585  unsigned hsub, unsigned vsub,
586  int xm, int left, int right, int hband)
587 {
588  int x;
589 
590  if (left) {
591  blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
592  left, hband, hsub + vsub, xm);
593  dst += dst_delta;
594  xm += left;
595  }
596  for (x = 0; x < w; x++) {
597  blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
598  1 << hsub, hband, hsub + vsub, xm);
599  dst += dst_delta;
600  xm += 1 << hsub;
601  }
602  if (right)
603  blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
604  right, hband, hsub + vsub, xm);
605 }
606 
608  uint8_t *dst[], int dst_linesize[], int dst_w, int dst_h,
609  const uint8_t *mask, int mask_linesize, int mask_w, int mask_h,
610  int l2depth, unsigned endianness, int x0, int y0)
611 {
612  unsigned alpha, nb_planes, nb_comp, plane, comp;
613  int xm0, ym0, w_sub, h_sub, x_sub, y_sub, left, right, top, bottom, y;
614  uint8_t *p0, *p;
615  const uint8_t *m;
616 
617  clip_interval(dst_w, &x0, &mask_w, &xm0);
618  clip_interval(dst_h, &y0, &mask_h, &ym0);
619  mask += ym0 * mask_linesize;
620  if (mask_w <= 0 || mask_h <= 0 || !color->rgba[3])
621  return;
622  if (draw->desc->comp[0].depth <= 8) {
623  /* alpha is in the [ 0 ; 0x10203 ] range,
624  alpha * mask is in the [ 0 ; 0x1010101 - 4 ] range */
625  alpha = (0x10307 * color->rgba[3] + 0x3) >> 8;
626  } else {
627  alpha = (0x101 * color->rgba[3] + 0x2) >> 8;
628  }
629  nb_planes = draw->nb_planes - !!(draw->desc->flags & AV_PIX_FMT_FLAG_ALPHA);
630  for (plane = 0; plane < nb_planes; plane++) {
631  nb_comp = draw->pixelstep[plane];
632  p0 = pointer_at(draw, dst, dst_linesize, plane, x0, y0);
633  w_sub = mask_w;
634  h_sub = mask_h;
635  x_sub = x0;
636  y_sub = y0;
637  subsampling_bounds(draw->hsub[plane], &x_sub, &w_sub, &left, &right);
638  subsampling_bounds(draw->vsub[plane], &y_sub, &h_sub, &top, &bottom);
639  for (comp = 0; comp < nb_comp; comp++) {
640  const int depth = draw->desc->comp[comp].depth;
641 
642  if (!component_used(draw, plane, comp))
643  continue;
644  p = p0 + comp;
645  m = mask;
646  if (top) {
647  if (depth <= 8) {
648  blend_line_hv(p, draw->pixelstep[plane],
649  color->comp[plane].u8[comp], alpha,
650  m, mask_linesize, l2depth, w_sub,
651  draw->hsub[plane], draw->vsub[plane],
652  xm0, left, right, top);
653  } else {
654  blend_line_hv16(p, draw->pixelstep[plane],
655  color->comp[plane].u16[comp], alpha,
656  m, mask_linesize, l2depth, w_sub,
657  draw->hsub[plane], draw->vsub[plane],
658  xm0, left, right, top);
659  }
660  p += dst_linesize[plane];
661  m += top * mask_linesize;
662  }
663  if (depth <= 8) {
664  for (y = 0; y < h_sub; y++) {
665  blend_line_hv(p, draw->pixelstep[plane],
666  color->comp[plane].u8[comp], alpha,
667  m, mask_linesize, l2depth, w_sub,
668  draw->hsub[plane], draw->vsub[plane],
669  xm0, left, right, 1 << draw->vsub[plane]);
670  p += dst_linesize[plane];
671  m += mask_linesize << draw->vsub[plane];
672  }
673  } else {
674  for (y = 0; y < h_sub; y++) {
675  blend_line_hv16(p, draw->pixelstep[plane],
676  color->comp[plane].u16[comp], alpha,
677  m, mask_linesize, l2depth, w_sub,
678  draw->hsub[plane], draw->vsub[plane],
679  xm0, left, right, 1 << draw->vsub[plane]);
680  p += dst_linesize[plane];
681  m += mask_linesize << draw->vsub[plane];
682  }
683  }
684  if (bottom) {
685  if (depth <= 8) {
686  blend_line_hv(p, draw->pixelstep[plane],
687  color->comp[plane].u8[comp], alpha,
688  m, mask_linesize, l2depth, w_sub,
689  draw->hsub[plane], draw->vsub[plane],
690  xm0, left, right, bottom);
691  } else {
692  blend_line_hv16(p, draw->pixelstep[plane],
693  color->comp[plane].u16[comp], alpha,
694  m, mask_linesize, l2depth, w_sub,
695  draw->hsub[plane], draw->vsub[plane],
696  xm0, left, right, bottom);
697  }
698  }
699  }
700  }
701 }
702 
703 int ff_draw_round_to_sub(FFDrawContext *draw, int sub_dir, int round_dir,
704  int value)
705 {
706  unsigned shift = sub_dir ? draw->vsub_max : draw->hsub_max;
707 
708  if (!shift)
709  return value;
710  if (round_dir >= 0)
711  value += round_dir ? (1 << shift) - 1 : 1 << (shift - 1);
712  return (value >> shift) << shift;
713 }
714 
716 {
717  enum AVPixelFormat i;
718  FFDrawContext draw;
719  AVFilterFormats *fmts = NULL;
720  int ret;
721 
722  for (i = 0; av_pix_fmt_desc_get(i); i++)
723  if (ff_draw_init(&draw, i, flags) >= 0 &&
724  (ret = ff_add_format(&fmts, i)) < 0)
725  return NULL;
726  return fmts;
727 }
int plane
Definition: avisynth_c.h:291
AVFilterFormats * ff_draw_supported_pixel_formats(unsigned flags)
Return the list of pixel formats supported by the draw functions.
Definition: drawutils.c:715
int plane
Which of the 4 planes contains the component.
Definition: pixdesc.h:35
#define NULL
Definition: coverity.c:32
static enum AVPixelFormat pix_fmt
static int shift(int a, int b)
Definition: sonic.c:82
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2222
void ff_copy_rectangle2(FFDrawContext *draw, uint8_t *dst[], int dst_linesize[], uint8_t *src[], int src_linesize[], int dst_x, int dst_y, int src_x, int src_y, int w, int h)
Copy a rectangle from an image to another.
Definition: drawutils.c:282
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
packed RGBA 16:16:16:16, 64bpp, 16B, 16G, 16R, 16A, the 2-byte value for each R/G/B/A component is st...
Definition: pixfmt.h:219
uint16_t u16[8]
Definition: drawutils.h:64
uint8_t hsub[MAX_PLANES]
Definition: drawutils.h:54
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:64
memory handling functions
const char * desc
Definition: nvenc.c:89
int av_get_bits_per_pixel(const AVPixFmtDescriptor *pixdesc)
Return the number of bits per pixel used by the pixel format described by pixdesc.
Definition: pixdesc.c:2174
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:180
packed RGBA 16:16:16:16, 64bpp, 16B, 16G, 16R, 16A, the 2-byte value for each R/G/B/A component is st...
Definition: pixfmt.h:218
#define av_bswap16
Definition: bswap.h:31
Various defines for YUV<->RGB conversion.
external API header
planar GBR 4:4:4 36bpp, little-endian
Definition: pixfmt.h:268
static void blend_line(uint8_t *dst, unsigned src, unsigned alpha, int dx, int w, unsigned hsub, int left, int right)
Definition: drawutils.c:384
packed BGR 8:8:8, 32bpp, XBGRXBGR... X=unused/undefined
Definition: pixfmt.h:252
planar GBR 4:4:4 36bpp, big-endian
Definition: pixfmt.h:267
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:87
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:92
static void clip_interval(int wmax, int *x, int *w, int *dx)
Clip interval [x; x+w[ within [0; wmax[.
Definition: drawutils.c:345
int ff_draw_round_to_sub(FFDrawContext *draw, int sub_dir, int round_dir, int value)
Round a dimension according to subsampling.
Definition: drawutils.c:703
Definition: drawutils.c:33
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
planar GBRA 4:4:4:4 64bpp, big-endian
Definition: pixfmt.h:229
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:117
uint8_t
#define AV_PIX_FMT_FLAG_ALPHA
The pixel format has an alpha channel.
Definition: pixdesc.h:173
#define av_malloc(s)
packed RGB 8:8:8, 32bpp, RGBXRGBX... X=unused/undefined
Definition: pixfmt.h:251
packed RGB 16:16:16, 48bpp, 16R, 16G, 16B, the 2-byte value for each R/G/B component is stored as lit...
Definition: pixfmt.h:111
static const uint32_t color[16+AV_CLASS_CATEGORY_NB]
Definition: log.c:94
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
packed RGBA 16:16:16:16, 64bpp, 16R, 16G, 16B, 16A, the 2-byte value for each R/G/B/A component is st...
Definition: pixfmt.h:216
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:95
uint8_t comp_mask[MAX_PLANES]
Definition: drawutils.h:53
#define height
planar GBR 4:4:4 48bpp, big-endian
Definition: pixfmt.h:185
#define av_log(a,...)
unsigned m
Definition: audioconvert.c:187
const char * name
Definition: pixdesc.h:82
planar GBR 4:4:4 27bpp, big-endian
Definition: pixfmt.h:181
static double alpha(void *priv, double x, double y)
Definition: vf_geq.c:99
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:101
static const uint16_t mask[17]
Definition: lzw.c:38
#define AVERROR(e)
Definition: error.h:43
#define AV_PIX_FMT_FLAG_RGB
The pixel format contains RGB-like data (as opposed to YUV/grayscale).
Definition: pixdesc.h:148
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:96
static void blend_line16(uint8_t *dst, unsigned src, unsigned alpha, int dx, int w, unsigned hsub, int left, int right)
Definition: drawutils.c:406
void ff_draw_color(FFDrawContext *draw, FFDrawColor *color, const uint8_t rgba[4])
Prepare a color.
Definition: drawutils.c:221
Definition: graph2dot.c:48
simple assert() macros that are a bit more flexible than ISO C assert().
static void blend_line_hv(uint8_t *dst, int dst_delta, unsigned src, unsigned alpha, const uint8_t *mask, int mask_linesize, int l2depth, int w, unsigned hsub, unsigned vsub, int xm, int left, int right, int hband)
Definition: drawutils.c:582
int ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition: formats.c:337
uint8_t vsub_max
Definition: drawutils.h:57
#define FFMAX(a, b)
Definition: common.h:94
uint8_t u8[16]
Definition: drawutils.h:65
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:93
packed RGB 16:16:16, 48bpp, 16B, 16G, 16R, the 2-byte value for each R/G/B component is stored as lit...
Definition: pixfmt.h:160
int depth
Definition: v4l.c:62
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:94
union FFDrawColor::@154 comp[MAX_PLANES]
uint64_t flags
Combination of AV_PIX_FMT_FLAG_...
Definition: pixdesc.h:106
planar GBR 4:4:4:4 48bpp, big-endian
Definition: pixfmt.h:300
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition: pixdesc.h:83
alias for AV_PIX_FMT_YA8
Definition: pixfmt.h:157
static void blend_line_hv16(uint8_t *dst, int dst_delta, unsigned src, unsigned alpha, const uint8_t *mask, int mask_linesize, int l2depth, int w, unsigned hsub, unsigned vsub, int xm, int left, int right, int hband)
Definition: drawutils.c:557
#define FFMIN(a, b)
Definition: common.h:96
#define width
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
#define AV_PIX_FMT_FLAG_PSEUDOPAL
The pixel format is "pseudo-paletted".
Definition: pixdesc.h:158
#define MAX_PLANES
Definition: ffv1.h:50
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:65
#define src
Definition: vp9dsp.c:530
packed RGB 16:16:16, 48bpp, 16B, 16G, 16R, the 2-byte value for each R/G/B component is stored as big...
Definition: pixfmt.h:159
static uint8_t * pointer_at(FFDrawContext *draw, uint8_t *data[], int linesize[], int plane, int x, int y)
Definition: drawutils.c:274
int ff_fill_rgba_map(uint8_t *rgba_map, enum AVPixelFormat pix_fmt)
Definition: drawutils.c:35
static void subsampling_bounds(int sub, int *x, int *w, int *start, int *end)
Decompose w pixels starting at x into start + (w starting at x) + end with x and w aligned on multipl...
Definition: drawutils.c:364
planar GBR 4:4:4:4 48bpp, little-endian
Definition: pixfmt.h:301
void ff_blend_mask(FFDrawContext *draw, FFDrawColor *color, uint8_t *dst[], int dst_linesize[], int dst_w, int dst_h, const uint8_t *mask, int mask_linesize, int mask_w, int mask_h, int l2depth, unsigned endianness, int x0, int y0)
Blend an alpha mask with an uniform color.
Definition: drawutils.c:607
misc drawing utilities
static void blend_pixel(uint8_t *dst, unsigned src, unsigned alpha, const uint8_t *mask, int mask_linesize, int l2depth, unsigned w, unsigned h, unsigned shift, unsigned xm0)
Definition: drawutils.c:534
unsigned nb_planes
Definition: drawutils.h:51
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
planar GBR 4:4:4 30bpp, big-endian
Definition: pixfmt.h:183
planar GBR 4:4:4 42bpp, little-endian
Definition: pixfmt.h:270
uint8_t hsub_max
Definition: drawutils.h:56
static const char * format
Definition: movenc.c:47
Definition: drawutils.c:33
void ff_blend_rectangle(FFDrawContext *draw, FFDrawColor *color, uint8_t *dst[], int dst_linesize[], int dst_w, int dst_h, int x0, int y0, int w, int h)
Blend a rectangle with an uniform color.
Definition: drawutils.c:431
void ff_copy_rectangle(uint8_t *dst[4], int dst_linesize[4], uint8_t *src[4], int src_linesize[4], int pixelstep[4], int hsub, int vsub, int x, int y, int y2, int w, int h)
Definition: drawutils.c:154
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
Definition: pixfmt.h:253
int ff_draw_init(FFDrawContext *draw, enum AVPixelFormat format, unsigned flags)
Init a draw context.
Definition: drawutils.c:176
planar GBR 4:4:4 42bpp, big-endian
Definition: pixfmt.h:269
static int component_used(FFDrawContext *draw, int plane, int comp)
Definition: drawutils.c:376
#define RGB_TO_U_CCIR(r1, g1, b1, shift)
Definition: colorspace.h:102
int offset
Number of elements before the component of the first pixel.
Definition: pixdesc.h:47
static int flags
Definition: cpu.c:47
#define RGB_TO_V_CCIR(r1, g1, b1, shift)
Definition: colorspace.h:106
Y , 8bpp.
Definition: pixfmt.h:70
planar GBRA 4:4:4:4 32bpp
Definition: pixfmt.h:228
planar GBR 4:4:4 27bpp, little-endian
Definition: pixfmt.h:182
static double c[64]
packed RGB 16:16:16, 48bpp, 16R, 16G, 16B, the 2-byte value for each R/G/B component is stored as big...
Definition: pixfmt.h:110
static void blend_pixel16(uint8_t *dst, unsigned src, unsigned alpha, const uint8_t *mask, int mask_linesize, int l2depth, unsigned w, unsigned h, unsigned shift, unsigned xm0)
Definition: drawutils.c:510
#define AV_WL16(p, v)
Definition: intreadwrite.h:412
#define AV_PIX_FMT_FLAG_BE
Pixel format is big-endian.
Definition: pixdesc.h:128
#define RGB_TO_Y_CCIR(r, g, b)
Definition: colorspace.h:98
int ff_fill_line_with_color(uint8_t *line[4], int pixel_step[4], int w, uint8_t dst_color[4], enum AVPixelFormat pix_fmt, uint8_t rgba_color[4], int *is_packed_rgba, uint8_t rgba_map_ptr[4])
Definition: drawutils.c:78
Y , 16bpp, little-endian.
Definition: pixfmt.h:99
int pixelstep[MAX_PLANES]
Definition: drawutils.h:52
A list of supported formats for one end of a filter link.
Definition: formats.h:64
16 bits gray, 16 bits alpha (little-endian)
Definition: pixfmt.h:226
const struct AVPixFmtDescriptor * desc
Definition: drawutils.h:49
void ff_fill_rectangle(FFDrawContext *draw, FFDrawColor *color, uint8_t *dst[], int dst_linesize[], int dst_x, int dst_y, int w, int h)
Fill a rectangle with an uniform color.
Definition: drawutils.c:304
uint8_t vsub[MAX_PLANES]
Definition: drawutils.h:55
#define av_freep(p)
static void comp(unsigned char *dst, int dst_stride, unsigned char *src, int src_stride, int add)
Definition: eamad.c:83
void INT64 start
Definition: avisynth_c.h:553
planar GBR 4:4:4 48bpp, little-endian
Definition: pixfmt.h:186
#define av_malloc_array(a, b)
void ff_draw_rectangle(uint8_t *dst[4], int dst_linesize[4], uint8_t *src[4], int pixelstep[4], int hsub, int vsub, int x, int y, int w, int h)
Definition: drawutils.c:132
#define EXPAND(compn)
int depth
Number of bits in the component.
Definition: pixdesc.h:58
planar GBRA 4:4:4:4 64bpp, little-endian
Definition: pixfmt.h:230
packed RGB 8:8:8, 32bpp, XRGBXRGB... X=unused/undefined
Definition: pixfmt.h:250
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
#define AV_PIX_FMT_FLAG_PLANAR
At least one pixel component is not in the first data plane.
Definition: pixdesc.h:144
enum AVPixelFormat format
Definition: drawutils.h:50
planar GBR 4:4:4 30bpp, little-endian
Definition: pixfmt.h:184
packed RGBA 16:16:16:16, 64bpp, 16R, 16G, 16B, 16A, the 2-byte value for each R/G/B/A component is st...
Definition: pixfmt.h:217
int step
Number of elements between 2 horizontally consecutive pixels.
Definition: pixdesc.h:41
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58
uint8_t rgba[4]
Definition: drawutils.h:61