FFmpeg  4.4.9
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:
77  case AV_PIX_FMT_GBRP: rgba_map[GREEN] = 0; rgba_map[BLUE ] = 1; rgba_map[RED ] = 2; rgba_map[ALPHA] = 3; break;
78  default: /* unsupported */
79  return AVERROR(EINVAL);
80  }
81  return 0;
82 }
83 
85 {
87  const AVComponentDescriptor *c;
88  unsigned i, nb_planes = 0;
89  int pixelstep[MAX_PLANES] = { 0 };
90  int full_range = 0;
91  int depthb = 0;
92 
93  if (!desc || !desc->name)
94  return AVERROR(EINVAL);
96  return AVERROR(ENOSYS);
98  return AVERROR(ENOSYS);
101  full_range = 1;
102  for (i = 0; i < desc->nb_components; i++) {
103  int db;
104  c = &desc->comp[i];
105  /* for now, only 8-16 bits formats */
106  if (c->depth < 8 || c->depth > 16)
107  return AVERROR(ENOSYS);
108  if (desc->flags & AV_PIX_FMT_FLAG_BE)
109  return AVERROR(ENOSYS);
110  if (c->plane >= MAX_PLANES)
111  return AVERROR(ENOSYS);
112  /* mixed >8 and <=8 depth */
113  db = (c->depth + 7) / 8;
114  if (depthb && (depthb != db))
115  return AVERROR(ENOSYS);
116  depthb = db;
117  /* strange interleaving */
118  if (pixelstep[c->plane] != 0 &&
119  pixelstep[c->plane] != c->step)
120  return AVERROR(ENOSYS);
121  if (pixelstep[c->plane] == 6 &&
122  c->depth == 16)
123  return AVERROR(ENOSYS);
124  pixelstep[c->plane] = c->step;
125  if (pixelstep[c->plane] >= 8)
126  return AVERROR(ENOSYS);
127  nb_planes = FFMAX(nb_planes, c->plane + 1);
128  }
129  memset(draw, 0, sizeof(*draw));
130  draw->desc = desc;
131  draw->format = format;
132  draw->nb_planes = nb_planes;
133  draw->flags = flags;
134  draw->full_range = full_range;
135  memcpy(draw->pixelstep, pixelstep, sizeof(draw->pixelstep));
136  draw->hsub[1] = draw->hsub[2] = draw->hsub_max = desc->log2_chroma_w;
137  draw->vsub[1] = draw->vsub[2] = draw->vsub_max = desc->log2_chroma_h;
138  for (i = 0; i < (desc->nb_components - !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA && !(flags & FF_DRAW_PROCESS_ALPHA))); i++)
139  draw->comp_mask[desc->comp[i].plane] |=
140  1 << desc->comp[i].offset;
141  return 0;
142 }
143 
145 {
146  unsigned i;
147  uint8_t rgba_map[4];
148 
149  if (rgba != color->rgba)
150  memcpy(color->rgba, rgba, sizeof(color->rgba));
151  if ((draw->desc->flags & AV_PIX_FMT_FLAG_RGB) &&
152  ff_fill_rgba_map(rgba_map, draw->format) >= 0) {
153  if (draw->nb_planes == 1) {
154  for (i = 0; i < 4; i++) {
155  color->comp[0].u8[rgba_map[i]] = rgba[i];
156  if (draw->desc->comp[rgba_map[i]].depth > 8) {
157  color->comp[0].u16[rgba_map[i]] = color->comp[0].u8[rgba_map[i]] << 8;
158  }
159  }
160  } else {
161  for (i = 0; i < 4; i++) {
162  color->comp[rgba_map[i]].u8[0] = rgba[i];
163  if (draw->desc->comp[rgba_map[i]].depth > 8)
164  color->comp[rgba_map[i]].u16[0] = color->comp[rgba_map[i]].u8[0] << (draw->desc->comp[rgba_map[i]].depth - 8);
165  }
166  }
167  } else if (draw->nb_planes >= 2) {
168  /* assume YUV */
169  const AVPixFmtDescriptor *desc = draw->desc;
170  color->comp[desc->comp[0].plane].u8[desc->comp[0].offset] = draw->full_range ? RGB_TO_Y_JPEG(rgba[0], rgba[1], rgba[2]) : RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
171  color->comp[desc->comp[1].plane].u8[desc->comp[1].offset] = draw->full_range ? RGB_TO_U_JPEG(rgba[0], rgba[1], rgba[2]) : RGB_TO_U_CCIR(rgba[0], rgba[1], rgba[2], 0);
172  color->comp[desc->comp[2].plane].u8[desc->comp[2].offset] = draw->full_range ? RGB_TO_V_JPEG(rgba[0], rgba[1], rgba[2]) : RGB_TO_V_CCIR(rgba[0], rgba[1], rgba[2], 0);
173  color->comp[3].u8[0] = rgba[3];
174 #define EXPAND(compn) \
175  if (desc->comp[compn].depth > 8) \
176  color->comp[desc->comp[compn].plane].u16[desc->comp[compn].offset] = \
177  color->comp[desc->comp[compn].plane].u8[desc->comp[compn].offset] << \
178  (draw->desc->comp[compn].depth + draw->desc->comp[compn].shift - 8)
179  EXPAND(3);
180  EXPAND(2);
181  EXPAND(1);
182  EXPAND(0);
183  } else if (draw->format == AV_PIX_FMT_GRAY8 || draw->format == AV_PIX_FMT_GRAY8A ||
184  draw->format == AV_PIX_FMT_GRAY16LE || draw->format == AV_PIX_FMT_YA16LE ||
185  draw->format == AV_PIX_FMT_GRAY9LE ||
186  draw->format == AV_PIX_FMT_GRAY10LE ||
187  draw->format == AV_PIX_FMT_GRAY12LE ||
188  draw->format == AV_PIX_FMT_GRAY14LE) {
189  const AVPixFmtDescriptor *desc = draw->desc;
190  color->comp[0].u8[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
191  EXPAND(0);
192  color->comp[1].u8[0] = rgba[3];
193  EXPAND(1);
194  } else {
196  "Color conversion not implemented for %s\n", draw->desc->name);
197  memset(color, 128, sizeof(*color));
198  }
199 }
200 
201 static uint8_t *pointer_at(FFDrawContext *draw, uint8_t *data[], int linesize[],
202  int plane, int x, int y)
203 {
204  return data[plane] +
205  (y >> draw->vsub[plane]) * linesize[plane] +
206  (x >> draw->hsub[plane]) * draw->pixelstep[plane];
207 }
208 
210  uint8_t *dst[], int dst_linesize[],
211  uint8_t *src[], int src_linesize[],
212  int dst_x, int dst_y, int src_x, int src_y,
213  int w, int h)
214 {
215  int plane, y, wp, hp;
216  uint8_t *p, *q;
217 
218  for (plane = 0; plane < draw->nb_planes; plane++) {
219  p = pointer_at(draw, src, src_linesize, plane, src_x, src_y);
220  q = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
221  wp = AV_CEIL_RSHIFT(w, draw->hsub[plane]) * draw->pixelstep[plane];
222  hp = AV_CEIL_RSHIFT(h, draw->vsub[plane]);
223  for (y = 0; y < hp; y++) {
224  memcpy(q, p, wp);
225  p += src_linesize[plane];
226  q += dst_linesize[plane];
227  }
228  }
229 }
230 
232  uint8_t *dst[], int dst_linesize[],
233  int dst_x, int dst_y, int w, int h)
234 {
235  int plane, x, y, wp, hp;
236  uint8_t *p0, *p;
237  FFDrawColor color_tmp = *color;
238 
239  for (plane = 0; plane < draw->nb_planes; plane++) {
240  p0 = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
241  wp = AV_CEIL_RSHIFT(w, draw->hsub[plane]);
242  hp = AV_CEIL_RSHIFT(h, draw->vsub[plane]);
243  if (!hp)
244  return;
245  p = p0;
246 
247  if (HAVE_BIGENDIAN && draw->desc->comp[0].depth > 8) {
248  for (x = 0; 2*x < draw->pixelstep[plane]; x++)
249  color_tmp.comp[plane].u16[x] = av_bswap16(color_tmp.comp[plane].u16[x]);
250  }
251 
252  /* copy first line from color */
253  for (x = 0; x < wp; x++) {
254  memcpy(p, color_tmp.comp[plane].u8, draw->pixelstep[plane]);
255  p += draw->pixelstep[plane];
256  }
257  wp *= draw->pixelstep[plane];
258  /* copy next lines from first line */
259  p = p0 + dst_linesize[plane];
260  for (y = 1; y < hp; y++) {
261  memcpy(p, p0, wp);
262  p += dst_linesize[plane];
263  }
264  }
265 }
266 
267 /**
268  * Clip interval [x; x+w[ within [0; wmax[.
269  * The resulting w may be negative if the final interval is empty.
270  * dx, if not null, return the difference between in and out value of x.
271  */
272 static void clip_interval(int wmax, int *x, int *w, int *dx)
273 {
274  if (dx)
275  *dx = 0;
276  if (*x < 0) {
277  if (dx)
278  *dx = -*x;
279  *w += *x;
280  *x = 0;
281  }
282  if (*x + *w > wmax)
283  *w = wmax - *x;
284 }
285 
286 /**
287  * Decompose w pixels starting at x
288  * into start + (w starting at x) + end
289  * with x and w aligned on multiples of 1<<sub.
290  */
291 static void subsampling_bounds(int sub, int *x, int *w, int *start, int *end)
292 {
293  int mask = (1 << sub) - 1;
294 
295  *start = (-*x) & mask;
296  *x += *start;
297  *start = FFMIN(*start, *w);
298  *w -= *start;
299  *end = *w & mask;
300  *w >>= sub;
301 }
302 
303 static int component_used(FFDrawContext *draw, int plane, int comp)
304 {
305  return (draw->comp_mask[plane] >> comp) & 1;
306 }
307 
308 /* If alpha is in the [ 0 ; 0x1010101 ] range,
309  then alpha * value is in the [ 0 ; 0xFFFFFFFF ] range,
310  and >> 24 gives a correct rounding. */
311 static void blend_line(uint8_t *dst, unsigned src, unsigned alpha,
312  int dx, int w, unsigned hsub, int left, int right)
313 {
314  unsigned asrc = alpha * src;
315  unsigned tau = 0x1010101 - alpha;
316  int x;
317 
318  if (left) {
319  unsigned suba = (left * alpha) >> hsub;
320  *dst = (*dst * (0x1010101 - suba) + src * suba) >> 24;
321  dst += dx;
322  }
323  for (x = 0; x < w; x++) {
324  *dst = (*dst * tau + asrc) >> 24;
325  dst += dx;
326  }
327  if (right) {
328  unsigned suba = (right * alpha) >> hsub;
329  *dst = (*dst * (0x1010101 - suba) + src * suba) >> 24;
330  }
331 }
332 
333 static void blend_line16(uint8_t *dst, unsigned src, unsigned alpha,
334  int dx, int w, unsigned hsub, int left, int right)
335 {
336  unsigned asrc = alpha * src;
337  unsigned tau = 0x10001 - alpha;
338  int x;
339 
340  if (left) {
341  unsigned suba = (left * alpha) >> hsub;
342  uint16_t value = AV_RL16(dst);
343  AV_WL16(dst, (value * (0x10001 - suba) + src * suba) >> 16);
344  dst += dx;
345  }
346  for (x = 0; x < w; x++) {
347  uint16_t value = AV_RL16(dst);
348  AV_WL16(dst, (value * tau + asrc) >> 16);
349  dst += dx;
350  }
351  if (right) {
352  unsigned suba = (right * alpha) >> hsub;
353  uint16_t value = AV_RL16(dst);
354  AV_WL16(dst, (value * (0x10001 - suba) + src * suba) >> 16);
355  }
356 }
357 
359  uint8_t *dst[], int dst_linesize[],
360  int dst_w, int dst_h,
361  int x0, int y0, int w, int h)
362 {
363  unsigned alpha, nb_planes, nb_comp, plane, comp;
364  int w_sub, h_sub, x_sub, y_sub, left, right, top, bottom, y;
365  uint8_t *p0, *p;
366 
367  /* TODO optimize if alpha = 0xFF */
368  clip_interval(dst_w, &x0, &w, NULL);
369  clip_interval(dst_h, &y0, &h, NULL);
370  if (w <= 0 || h <= 0 || !color->rgba[3])
371  return;
372  if (draw->desc->comp[0].depth <= 8) {
373  /* 0x10203 * alpha + 2 is in the [ 2 ; 0x1010101 - 2 ] range */
374  alpha = 0x10203 * color->rgba[3] + 0x2;
375  } else {
376  /* 0x101 * alpha is in the [ 2 ; 0x1001] range */
377  alpha = 0x101 * color->rgba[3] + 0x2;
378  }
379  nb_planes = draw->nb_planes - !!(draw->desc->flags & AV_PIX_FMT_FLAG_ALPHA && !(draw->flags & FF_DRAW_PROCESS_ALPHA));
380  nb_planes += !nb_planes;
381  for (plane = 0; plane < nb_planes; plane++) {
382  nb_comp = draw->pixelstep[plane];
383  p0 = pointer_at(draw, dst, dst_linesize, plane, x0, y0);
384  w_sub = w;
385  h_sub = h;
386  x_sub = x0;
387  y_sub = y0;
388  subsampling_bounds(draw->hsub[plane], &x_sub, &w_sub, &left, &right);
389  subsampling_bounds(draw->vsub[plane], &y_sub, &h_sub, &top, &bottom);
390  for (comp = 0; comp < nb_comp; comp++) {
391  const int depth = draw->desc->comp[comp].depth;
392 
393  if (!component_used(draw, plane, comp))
394  continue;
395  p = p0 + comp;
396  if (top) {
397  if (depth <= 8) {
398  blend_line(p, color->comp[plane].u8[comp], alpha >> 1,
399  draw->pixelstep[plane], w_sub,
400  draw->hsub[plane], left, right);
401  } else {
402  blend_line16(p, color->comp[plane].u16[comp], alpha >> 1,
403  draw->pixelstep[plane], w_sub,
404  draw->hsub[plane], left, right);
405  }
406  p += dst_linesize[plane];
407  }
408  if (depth <= 8) {
409  for (y = 0; y < h_sub; y++) {
410  blend_line(p, color->comp[plane].u8[comp], alpha,
411  draw->pixelstep[plane], w_sub,
412  draw->hsub[plane], left, right);
413  p += dst_linesize[plane];
414  }
415  } else {
416  for (y = 0; y < h_sub; y++) {
417  blend_line16(p, color->comp[plane].u16[comp], alpha,
418  draw->pixelstep[plane], w_sub,
419  draw->hsub[plane], left, right);
420  p += dst_linesize[plane];
421  }
422  }
423  if (bottom) {
424  if (depth <= 8) {
425  blend_line(p, color->comp[plane].u8[comp], alpha >> 1,
426  draw->pixelstep[plane], w_sub,
427  draw->hsub[plane], left, right);
428  } else {
429  blend_line16(p, color->comp[plane].u16[comp], alpha >> 1,
430  draw->pixelstep[plane], w_sub,
431  draw->hsub[plane], left, right);
432  }
433  }
434  }
435  }
436 }
437 
438 static void blend_pixel16(uint8_t *dst, unsigned src, unsigned alpha,
439  const uint8_t *mask, int mask_linesize, int l2depth,
440  unsigned w, unsigned h, unsigned shift, unsigned xm0)
441 {
442  unsigned xm, x, y, t = 0;
443  unsigned xmshf = 3 - l2depth;
444  unsigned xmmod = 7 >> l2depth;
445  unsigned mbits = (1 << (1 << l2depth)) - 1;
446  unsigned mmult = 255 / mbits;
447  uint16_t value = AV_RL16(dst);
448 
449  for (y = 0; y < h; y++) {
450  xm = xm0;
451  for (x = 0; x < w; x++) {
452  t += ((mask[xm >> xmshf] >> ((~xm & xmmod) << l2depth)) & mbits)
453  * mmult;
454  xm++;
455  }
456  mask += mask_linesize;
457  }
458  alpha = (t >> shift) * alpha;
459  AV_WL16(dst, ((0x10001 - alpha) * value + alpha * src) >> 16);
460 }
461 
462 static void blend_pixel(uint8_t *dst, unsigned src, unsigned alpha,
463  const uint8_t *mask, int mask_linesize, int l2depth,
464  unsigned w, unsigned h, unsigned shift, unsigned xm0)
465 {
466  unsigned xm, x, y, t = 0;
467  unsigned xmshf = 3 - l2depth;
468  unsigned xmmod = 7 >> l2depth;
469  unsigned mbits = (1 << (1 << l2depth)) - 1;
470  unsigned mmult = 255 / mbits;
471 
472  for (y = 0; y < h; y++) {
473  xm = xm0;
474  for (x = 0; x < w; x++) {
475  t += ((mask[xm >> xmshf] >> ((~xm & xmmod) << l2depth)) & mbits)
476  * mmult;
477  xm++;
478  }
479  mask += mask_linesize;
480  }
481  alpha = (t >> shift) * alpha;
482  *dst = ((0x1010101 - alpha) * *dst + alpha * src) >> 24;
483 }
484 
485 static void blend_line_hv16(uint8_t *dst, int dst_delta,
486  unsigned src, unsigned alpha,
487  const uint8_t *mask, int mask_linesize, int l2depth, int w,
488  unsigned hsub, unsigned vsub,
489  int xm, int left, int right, int hband)
490 {
491  int x;
492 
493  if (left) {
494  blend_pixel16(dst, src, alpha, mask, mask_linesize, l2depth,
495  left, hband, hsub + vsub, xm);
496  dst += dst_delta;
497  xm += left;
498  }
499  for (x = 0; x < w; x++) {
500  blend_pixel16(dst, src, alpha, mask, mask_linesize, l2depth,
501  1 << hsub, hband, hsub + vsub, xm);
502  dst += dst_delta;
503  xm += 1 << hsub;
504  }
505  if (right)
506  blend_pixel16(dst, src, alpha, mask, mask_linesize, l2depth,
507  right, hband, hsub + vsub, xm);
508 }
509 
510 static void blend_line_hv(uint8_t *dst, int dst_delta,
511  unsigned src, unsigned alpha,
512  const uint8_t *mask, int mask_linesize, int l2depth, int w,
513  unsigned hsub, unsigned vsub,
514  int xm, int left, int right, int hband)
515 {
516  int x;
517 
518  if (left) {
519  blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
520  left, hband, hsub + vsub, xm);
521  dst += dst_delta;
522  xm += left;
523  }
524  for (x = 0; x < w; x++) {
525  blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
526  1 << hsub, hband, hsub + vsub, xm);
527  dst += dst_delta;
528  xm += 1 << hsub;
529  }
530  if (right)
531  blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
532  right, hband, hsub + vsub, xm);
533 }
534 
536  uint8_t *dst[], int dst_linesize[], int dst_w, int dst_h,
537  const uint8_t *mask, int mask_linesize, int mask_w, int mask_h,
538  int l2depth, unsigned endianness, int x0, int y0)
539 {
540  unsigned alpha, nb_planes, nb_comp, plane, comp;
541  int xm0, ym0, w_sub, h_sub, x_sub, y_sub, left, right, top, bottom, y;
542  uint8_t *p0, *p;
543  const uint8_t *m;
544 
545  clip_interval(dst_w, &x0, &mask_w, &xm0);
546  clip_interval(dst_h, &y0, &mask_h, &ym0);
547  mask += ym0 * mask_linesize;
548  if (mask_w <= 0 || mask_h <= 0 || !color->rgba[3])
549  return;
550  if (draw->desc->comp[0].depth <= 8) {
551  /* alpha is in the [ 0 ; 0x10203 ] range,
552  alpha * mask is in the [ 0 ; 0x1010101 - 4 ] range */
553  alpha = (0x10307 * color->rgba[3] + 0x3) >> 8;
554  } else {
555  alpha = (0x101 * color->rgba[3] + 0x2) >> 8;
556  }
557  nb_planes = draw->nb_planes - !!(draw->desc->flags & AV_PIX_FMT_FLAG_ALPHA && !(draw->flags & FF_DRAW_PROCESS_ALPHA));
558  nb_planes += !nb_planes;
559  for (plane = 0; plane < nb_planes; plane++) {
560  nb_comp = draw->pixelstep[plane];
561  p0 = pointer_at(draw, dst, dst_linesize, plane, x0, y0);
562  w_sub = mask_w;
563  h_sub = mask_h;
564  x_sub = x0;
565  y_sub = y0;
566  subsampling_bounds(draw->hsub[plane], &x_sub, &w_sub, &left, &right);
567  subsampling_bounds(draw->vsub[plane], &y_sub, &h_sub, &top, &bottom);
568  for (comp = 0; comp < nb_comp; comp++) {
569  const int depth = draw->desc->comp[comp].depth;
570 
571  if (!component_used(draw, plane, comp))
572  continue;
573  p = p0 + comp;
574  m = mask;
575  if (top) {
576  if (depth <= 8) {
577  blend_line_hv(p, draw->pixelstep[plane],
578  color->comp[plane].u8[comp], alpha,
579  m, mask_linesize, l2depth, w_sub,
580  draw->hsub[plane], draw->vsub[plane],
581  xm0, left, right, top);
582  } else {
583  blend_line_hv16(p, draw->pixelstep[plane],
584  color->comp[plane].u16[comp], alpha,
585  m, mask_linesize, l2depth, w_sub,
586  draw->hsub[plane], draw->vsub[plane],
587  xm0, left, right, top);
588  }
589  p += dst_linesize[plane];
590  m += top * mask_linesize;
591  }
592  if (depth <= 8) {
593  for (y = 0; y < h_sub; y++) {
594  blend_line_hv(p, draw->pixelstep[plane],
595  color->comp[plane].u8[comp], alpha,
596  m, mask_linesize, l2depth, w_sub,
597  draw->hsub[plane], draw->vsub[plane],
598  xm0, left, right, 1 << draw->vsub[plane]);
599  p += dst_linesize[plane];
600  m += mask_linesize << draw->vsub[plane];
601  }
602  } else {
603  for (y = 0; y < h_sub; y++) {
604  blend_line_hv16(p, draw->pixelstep[plane],
605  color->comp[plane].u16[comp], alpha,
606  m, mask_linesize, l2depth, w_sub,
607  draw->hsub[plane], draw->vsub[plane],
608  xm0, left, right, 1 << draw->vsub[plane]);
609  p += dst_linesize[plane];
610  m += mask_linesize << draw->vsub[plane];
611  }
612  }
613  if (bottom) {
614  if (depth <= 8) {
615  blend_line_hv(p, draw->pixelstep[plane],
616  color->comp[plane].u8[comp], alpha,
617  m, mask_linesize, l2depth, w_sub,
618  draw->hsub[plane], draw->vsub[plane],
619  xm0, left, right, bottom);
620  } else {
621  blend_line_hv16(p, draw->pixelstep[plane],
622  color->comp[plane].u16[comp], alpha,
623  m, mask_linesize, l2depth, w_sub,
624  draw->hsub[plane], draw->vsub[plane],
625  xm0, left, right, bottom);
626  }
627  }
628  }
629  }
630 }
631 
632 int ff_draw_round_to_sub(FFDrawContext *draw, int sub_dir, int round_dir,
633  int value)
634 {
635  unsigned shift = sub_dir ? draw->vsub_max : draw->hsub_max;
636 
637  if (!shift)
638  return value;
639  if (round_dir >= 0)
640  value += round_dir ? (1 << shift) - 1 : 1 << (shift - 1);
641  return (value >> shift) << shift;
642 }
643 
645 {
646  enum AVPixelFormat i;
647  FFDrawContext draw;
648  AVFilterFormats *fmts = NULL;
649  int ret;
650 
651  for (i = 0; av_pix_fmt_desc_get(i); i++)
652  if (ff_draw_init(&draw, i, flags) >= 0 &&
653  (ret = ff_add_format(&fmts, i)) < 0)
654  return NULL;
655  return fmts;
656 }
static const char *const format[]
Definition: af_aiir.c:457
uint8_t
simple assert() macros that are a bit more flexible than ISO C assert().
#define AV_RL16
Definition: intreadwrite.h:42
Convenience header that includes libavutil's core.
#define flags(name, subs,...)
Definition: cbs_av1.c:572
#define FFMIN(a, b)
Definition: common.h:105
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58
#define FFMAX(a, b)
Definition: common.h:103
#define HAVE_BIGENDIAN
Definition: config.h:202
#define NULL
Definition: coverity.c:32
static enum AVPixelFormat pix_fmt
static float sub(float src0, float src1)
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:358
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:462
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:209
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:632
static void clip_interval(int wmax, int *x, int *w, int *dx)
Clip interval [x; x+w[ within [0; wmax[.
Definition: drawutils.c:272
#define EXPAND(compn)
static void blend_line16(uint8_t *dst, unsigned src, unsigned alpha, int dx, int w, unsigned hsub, int left, int right)
Definition: drawutils.c:333
int ff_draw_init(FFDrawContext *draw, enum AVPixelFormat format, unsigned flags)
Init a draw context.
Definition: drawutils.c:84
void ff_draw_color(FFDrawContext *draw, FFDrawColor *color, const uint8_t rgba[4])
Prepare a color.
Definition: drawutils.c:144
@ BLUE
Definition: drawutils.c:33
@ ALPHA
Definition: drawutils.c:33
@ GREEN
Definition: drawutils.c:33
@ RED
Definition: drawutils.c:33
static uint8_t * pointer_at(FFDrawContext *draw, uint8_t *data[], int linesize[], int plane, int x, int y)
Definition: drawutils.c:201
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:438
int ff_fill_rgba_map(uint8_t *rgba_map, enum AVPixelFormat pix_fmt)
Definition: drawutils.c:35
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:510
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:291
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:485
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:231
static void blend_line(uint8_t *dst, unsigned src, unsigned alpha, int dx, int w, unsigned hsub, int left, int right)
Definition: drawutils.c:311
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:535
AVFilterFormats * ff_draw_supported_pixel_formats(unsigned flags)
Return the list of pixel formats supported by the draw functions.
Definition: drawutils.c:644
static int component_used(FFDrawContext *draw, int plane, int comp)
Definition: drawutils.c:303
misc drawing utilities
#define FF_DRAW_PROCESS_ALPHA
Process alpha pixel component.
Definition: drawutils.h:61
static void comp(unsigned char *dst, ptrdiff_t dst_stride, unsigned char *src, ptrdiff_t src_stride, int add)
Definition: eamad.c:85
double value
Definition: eval.c:100
#define MAX_PLANES
Definition: ffv1.h:49
int ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition: formats.c:332
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
bool full_range
static const int16_t alpha[]
Definition: ilbcdata.h:55
int i
Definition: input.c:407
#define AV_WL16(p, v)
Definition: intreadwrite.h:412
Various defines for YUV<->RGB conversion.
#define RGB_TO_V_JPEG(r1, g1, b1)
Definition: colorspace.h:118
#define RGB_TO_U_CCIR(r1, g1, b1, shift)
Definition: colorspace.h:102
#define RGB_TO_Y_CCIR(r, g, b)
Definition: colorspace.h:98
#define RGB_TO_Y_JPEG(r, g, b)
Definition: colorspace.h:110
#define RGB_TO_V_CCIR(r1, g1, b1, shift)
Definition: colorspace.h:106
#define RGB_TO_U_JPEG(r1, g1, b1)
Definition: colorspace.h:114
#define FF_PSEUDOPAL
Definition: internal.h:299
const char * desc
Definition: libsvtav1.c:83
uint8_t w
Definition: llviddspenc.c:39
static const uint32_t color[16+AV_CLASS_CATEGORY_NB]
Definition: log.c:92
static const uint16_t mask[17]
Definition: lzw.c:38
Memory handling functions.
const char data[16]
Definition: mxf.c:142
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2573
#define AV_PIX_FMT_FLAG_ALPHA
The pixel format has an alpha channel.
Definition: pixdesc.h:179
#define AV_PIX_FMT_FLAG_RGB
The pixel format contains RGB-like data (as opposed to YUV/grayscale).
Definition: pixdesc.h:148
#define AV_PIX_FMT_FLAG_PLANAR
At least one pixel component is not in the first data plane.
Definition: pixdesc.h:144
#define AV_PIX_FMT_FLAG_BE
Pixel format is big-endian.
Definition: pixdesc.h:128
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
@ AV_PIX_FMT_P010LE
like NV12, with 10bpp per component, data in the high bits, zeros in the low bits,...
Definition: pixfmt.h:284
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:68
@ AV_PIX_FMT_GRAY10LE
Y , 10bpp, little-endian.
Definition: pixfmt.h:298
@ AV_PIX_FMT_GBRP10BE
planar GBR 4:4:4 30bpp, big-endian
Definition: pixfmt.h:172
@ AV_PIX_FMT_GBRPF32BE
IEEE-754 single precision planar GBR 4:4:4, 96bpp, big-endian.
Definition: pixfmt.h:318
@ AV_PIX_FMT_BGR0
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
Definition: pixfmt.h:240
@ AV_PIX_FMT_GBRP9LE
planar GBR 4:4:4 27bpp, little-endian
Definition: pixfmt.h:171
@ AV_PIX_FMT_P016LE
like NV12, with 16bpp per component, little-endian
Definition: pixfmt.h:300
@ AV_PIX_FMT_P010BE
like NV12, with 10bpp per component, data in the high bits, zeros in the low bits,...
Definition: pixfmt.h:285
@ AV_PIX_FMT_ARGB
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:92
@ AV_PIX_FMT_GBRP12BE
planar GBR 4:4:4 36bpp, big-endian
Definition: pixfmt.h:254
@ AV_PIX_FMT_GRAY8A
alias for AV_PIX_FMT_YA8
Definition: pixfmt.h:146
@ AV_PIX_FMT_GRAY12LE
Y , 12bpp, little-endian.
Definition: pixfmt.h:296
@ AV_PIX_FMT_GBRAP12BE
planar GBR 4:4:4:4 48bpp, big-endian
Definition: pixfmt.h:287
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:95
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
@ AV_PIX_FMT_BGR48BE
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:148
@ AV_PIX_FMT_YA16LE
16 bits gray, 16 bits alpha (little-endian)
Definition: pixfmt.h:213
@ AV_PIX_FMT_GRAY14LE
Y , 14bpp, little-endian.
Definition: pixfmt.h:338
@ AV_PIX_FMT_RGB48BE
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:102
@ AV_PIX_FMT_ABGR
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:94
@ AV_PIX_FMT_P016BE
like NV12, with 16bpp per component, big-endian
Definition: pixfmt.h:301
@ AV_PIX_FMT_YUVJ440P
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range
Definition: pixfmt.h:100
@ AV_PIX_FMT_RGBA64BE
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:205
@ AV_PIX_FMT_RGBA64LE
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:206
@ AV_PIX_FMT_0BGR
packed BGR 8:8:8, 32bpp, XBGRXBGR... X=unused/undefined
Definition: pixfmt.h:239
@ AV_PIX_FMT_GBRAP16BE
planar GBRA 4:4:4:4 64bpp, big-endian
Definition: pixfmt.h:216
@ AV_PIX_FMT_GBRPF32LE
IEEE-754 single precision planar GBR 4:4:4, 96bpp, little-endian.
Definition: pixfmt.h:319
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
@ AV_PIX_FMT_YUVJ411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) full scale (JPEG), deprecated in favor ...
Definition: pixfmt.h:258
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
Definition: pixfmt.h:215
@ AV_PIX_FMT_GBRP12LE
planar GBR 4:4:4 36bpp, little-endian
Definition: pixfmt.h:255
@ AV_PIX_FMT_BGRA64BE
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:207
@ AV_PIX_FMT_GBRP16BE
planar GBR 4:4:4 48bpp, big-endian
Definition: pixfmt.h:174
@ AV_PIX_FMT_GBRAP12LE
planar GBR 4:4:4:4 48bpp, little-endian
Definition: pixfmt.h:288
@ AV_PIX_FMT_GBRP9BE
planar GBR 4:4:4 27bpp, big-endian
Definition: pixfmt.h:170
@ AV_PIX_FMT_RGB48LE
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:103
@ AV_PIX_FMT_GBRAPF32BE
IEEE-754 single precision planar GBRA 4:4:4:4, 128bpp, big-endian.
Definition: pixfmt.h:320
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition: pixfmt.h:79
@ AV_PIX_FMT_GBRAPF32LE
IEEE-754 single precision planar GBRA 4:4:4:4, 128bpp, little-endian.
Definition: pixfmt.h:321
@ AV_PIX_FMT_BGR48LE
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:149
@ AV_PIX_FMT_GBRP14LE
planar GBR 4:4:4 42bpp, little-endian
Definition: pixfmt.h:257
@ AV_PIX_FMT_RGB0
packed RGB 8:8:8, 32bpp, RGBXRGBX... X=unused/undefined
Definition: pixfmt.h:238
@ AV_PIX_FMT_GRAY16LE
Y , 16bpp, little-endian.
Definition: pixfmt.h:98
@ AV_PIX_FMT_GBRP10LE
planar GBR 4:4:4 30bpp, little-endian
Definition: pixfmt.h:173
@ AV_PIX_FMT_GBRAP10BE
planar GBR 4:4:4:4 40bpp, big-endian
Definition: pixfmt.h:290
@ AV_PIX_FMT_BGRA64LE
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:208
@ AV_PIX_FMT_GBRAP10LE
planar GBR 4:4:4:4 40bpp, little-endian
Definition: pixfmt.h:291
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:69
@ AV_PIX_FMT_GRAY9LE
Y , 9bpp, little-endian.
Definition: pixfmt.h:316
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:168
@ AV_PIX_FMT_GBRAP16LE
planar GBRA 4:4:4:4 64bpp, little-endian
Definition: pixfmt.h:217
@ AV_PIX_FMT_0RGB
packed RGB 8:8:8, 32bpp, XRGBXRGB... X=unused/undefined
Definition: pixfmt.h:237
@ AV_PIX_FMT_GBRP14BE
planar GBR 4:4:4 42bpp, big-endian
Definition: pixfmt.h:256
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition: pixfmt.h:80
@ AV_PIX_FMT_GBRP16LE
planar GBR 4:4:4 48bpp, little-endian
Definition: pixfmt.h:175
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:78
#define av_bswap16
Definition: bswap.h:31
static int shift(int a, int b)
Definition: sonic.c:82
int depth
Number of bits in the component.
Definition: pixdesc.h:58
A list of supported formats for one end of a filter link.
Definition: formats.h:65
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
const char * name
Definition: pixdesc.h:82
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:117
uint64_t flags
Combination of AV_PIX_FMT_FLAG_...
Definition: pixdesc.h:106
union FFDrawColor::@204 comp[MAX_PLANES]
uint16_t u16[8]
Definition: drawutils.h:53
uint8_t u8[16]
Definition: drawutils.h:54
unsigned nb_planes
Definition: drawutils.h:38
unsigned flags
Definition: drawutils.h:46
const struct AVPixFmtDescriptor * desc
Definition: drawutils.h:36
uint8_t vsub[MAX_PLANES]
Definition: drawutils.h:42
enum AVPixelFormat format
Definition: drawutils.h:37
int full_range
Definition: drawutils.h:45
uint8_t hsub[MAX_PLANES]
Definition: drawutils.h:41
uint8_t vsub_max
Definition: drawutils.h:44
uint8_t comp_mask[MAX_PLANES]
Definition: drawutils.h:40
int pixelstep[MAX_PLANES]
Definition: drawutils.h:39
uint8_t hsub_max
Definition: drawutils.h:43
#define av_log(a,...)
#define src
Definition: vp8dsp.c:255
static void hsub(htype *dst, const htype *src, int bins)
Definition: vf_median.c:76
static double c[64]