FFmpeg
imgutils.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 /**
20  * @file
21  * misc image utilities
22  */
23 
24 #include "avassert.h"
25 #include "common.h"
26 #include "imgutils.h"
27 #include "imgutils_internal.h"
28 #include "internal.h"
29 #include "intreadwrite.h"
30 #include "log.h"
31 #include "mathematics.h"
32 #include "pixdesc.h"
33 #include "rational.h"
34 
35 void av_image_fill_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4],
36  const AVPixFmtDescriptor *pixdesc)
37 {
38  int i;
39  memset(max_pixsteps, 0, 4*sizeof(max_pixsteps[0]));
40  if (max_pixstep_comps)
41  memset(max_pixstep_comps, 0, 4*sizeof(max_pixstep_comps[0]));
42 
43  for (i = 0; i < 4; i++) {
44  const AVComponentDescriptor *comp = &(pixdesc->comp[i]);
45  if (comp->step > max_pixsteps[comp->plane]) {
46  max_pixsteps[comp->plane] = comp->step;
47  if (max_pixstep_comps)
48  max_pixstep_comps[comp->plane] = i;
49  }
50  }
51 }
52 
53 static inline
54 int image_get_linesize(int width, int plane,
55  int max_step, int max_step_comp,
56  const AVPixFmtDescriptor *desc)
57 {
58  int s, shifted_w, linesize;
59 
60  if (!desc)
61  return AVERROR(EINVAL);
62 
63  if (width < 0)
64  return AVERROR(EINVAL);
65  s = (max_step_comp == 1 || max_step_comp == 2) ? desc->log2_chroma_w : 0;
66  shifted_w = ((width + (1 << s) - 1)) >> s;
67  if (shifted_w && max_step > INT_MAX / shifted_w)
68  return AVERROR(EINVAL);
69  linesize = max_step * shifted_w;
70 
71  if (desc->flags & AV_PIX_FMT_FLAG_BITSTREAM)
72  linesize = (linesize + 7) >> 3;
73  return linesize;
74 }
75 
77 {
79  int max_step [4]; /* max pixel step for each plane */
80  int max_step_comp[4]; /* the component for each plane which has the max pixel step */
81 
82  if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
83  return AVERROR(EINVAL);
84 
85  av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
86  return image_get_linesize(width, plane, max_step[plane], max_step_comp[plane], desc);
87 }
88 
89 int av_image_fill_linesizes(int linesizes[4], enum AVPixelFormat pix_fmt, int width)
90 {
91  int i, ret;
93  int max_step [4]; /* max pixel step for each plane */
94  int max_step_comp[4]; /* the component for each plane which has the max pixel step */
95 
96  memset(linesizes, 0, 4*sizeof(linesizes[0]));
97 
98  if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
99  return AVERROR(EINVAL);
100 
101  av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
102  for (i = 0; i < 4; i++) {
103  if ((ret = image_get_linesize(width, i, max_step[i], max_step_comp[i], desc)) < 0)
104  return ret;
105  linesizes[i] = ret;
106  }
107 
108  return 0;
109 }
110 
112  int height, const ptrdiff_t linesizes[4])
113 {
114  int i, has_plane[4] = { 0 };
115 
116  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
117  memset(sizes , 0, sizeof(sizes[0])*4);
118 
119  if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
120  return AVERROR(EINVAL);
121 
122  if (linesizes[0] > SIZE_MAX / height)
123  return AVERROR(EINVAL);
124  sizes[0] = linesizes[0] * (size_t)height;
125 
126  if (desc->flags & AV_PIX_FMT_FLAG_PAL ||
127  desc->flags & FF_PSEUDOPAL) {
128  sizes[1] = 256 * 4; /* palette is stored here as 256 32 bits words */
129  return 0;
130  }
131 
132  for (i = 0; i < 4; i++)
133  has_plane[desc->comp[i].plane] = 1;
134 
135  for (i = 1; i < 4 && has_plane[i]; i++) {
136  int h, s = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
137  h = (height + (1 << s) - 1) >> s;
138  if (linesizes[i] > SIZE_MAX / h)
139  return AVERROR(EINVAL);
140  sizes[i] = (size_t)h * linesizes[i];
141  }
142 
143  return 0;
144 }
145 
147  uint8_t *ptr, const int linesizes[4])
148 {
149  int i, ret;
150  ptrdiff_t linesizes1[4];
151  size_t sizes[4];
152 
153  memset(data , 0, sizeof(data[0])*4);
154 
155  for (i = 0; i < 4; i++)
156  linesizes1[i] = linesizes[i];
157 
158  ret = av_image_fill_plane_sizes(sizes, pix_fmt, height, linesizes1);
159  if (ret < 0)
160  return ret;
161 
162  ret = 0;
163  for (i = 0; i < 4; i++) {
164  if (sizes[i] > INT_MAX - ret)
165  return AVERROR(EINVAL);
166  ret += sizes[i];
167  }
168 
169  data[0] = ptr;
170  for (i = 1; i < 4 && sizes[i]; i++)
171  data[i] = data[i - 1] + sizes[i - 1];
172 
173  return ret;
174 }
175 
177 {
178  int i;
179 
180  for (i = 0; i < 256; i++) {
181  int r, g, b;
182 
183  switch (pix_fmt) {
184  case AV_PIX_FMT_RGB8:
185  r = (i>>5 )*36;
186  g = ((i>>2)&7)*36;
187  b = (i&3 )*85;
188  break;
189  case AV_PIX_FMT_BGR8:
190  b = (i>>6 )*85;
191  g = ((i>>3)&7)*36;
192  r = (i&7 )*36;
193  break;
195  r = (i>>3 )*255;
196  g = ((i>>1)&3)*85;
197  b = (i&1 )*255;
198  break;
200  b = (i>>3 )*255;
201  g = ((i>>1)&3)*85;
202  r = (i&1 )*255;
203  break;
204  case AV_PIX_FMT_GRAY8:
205  r = b = g = i;
206  break;
207  default:
208  return AVERROR(EINVAL);
209  }
210  pal[i] = b + (g << 8) + (r << 16) + (0xFFU << 24);
211  }
212 
213  return 0;
214 }
215 
216 int av_image_alloc(uint8_t *pointers[4], int linesizes[4],
217  int w, int h, enum AVPixelFormat pix_fmt, int align)
218 {
219  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
220  int i, ret;
221  ptrdiff_t linesizes1[4];
222  size_t total_size, sizes[4];
223  uint8_t *buf;
224 
225  if (!desc)
226  return AVERROR(EINVAL);
227 
228  if ((ret = av_image_check_size(w, h, 0, NULL)) < 0)
229  return ret;
230  if ((ret = av_image_fill_linesizes(linesizes, pix_fmt, align>7 ? FFALIGN(w, 8) : w)) < 0)
231  return ret;
232 
233  for (i = 0; i < 4; i++) {
234  linesizes[i] = FFALIGN(linesizes[i], align);
235  linesizes1[i] = linesizes[i];
236  }
237 
238  if ((ret = av_image_fill_plane_sizes(sizes, pix_fmt, h, linesizes1)) < 0)
239  return ret;
240  total_size = align;
241  for (i = 0; i < 4; i++) {
242  if (total_size > SIZE_MAX - sizes[i])
243  return AVERROR(EINVAL);
244  total_size += sizes[i];
245  }
246  buf = av_malloc(total_size);
247  if (!buf)
248  return AVERROR(ENOMEM);
249  if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, buf, linesizes)) < 0) {
250  av_free(buf);
251  return ret;
252  }
253  if (desc->flags & AV_PIX_FMT_FLAG_PAL || (desc->flags & FF_PSEUDOPAL && pointers[1])) {
254  avpriv_set_systematic_pal2((uint32_t*)pointers[1], pix_fmt);
255  if (align < 4) {
256  av_log(NULL, AV_LOG_ERROR, "Formats with a palette require a minimum alignment of 4\n");
257  av_free(buf);
258  return AVERROR(EINVAL);
259  }
260  }
261 
262  if ((desc->flags & AV_PIX_FMT_FLAG_PAL ||
263  desc->flags & FF_PSEUDOPAL) && pointers[1] &&
264  pointers[1] - pointers[0] > linesizes[0] * h) {
265  /* zero-initialize the padding before the palette */
266  memset(pointers[0] + linesizes[0] * h, 0,
267  pointers[1] - pointers[0] - linesizes[0] * h);
268  }
269 
270  return ret;
271 }
272 
273 typedef struct ImgUtils {
274  const AVClass *class;
276  void *log_ctx;
277 } ImgUtils;
278 
279 static const AVClass imgutils_class = {
280  .class_name = "IMGUTILS",
281  .item_name = av_default_item_name,
282  .option = NULL,
283  .version = LIBAVUTIL_VERSION_INT,
284  .log_level_offset_offset = offsetof(ImgUtils, log_offset),
285  .parent_log_context_offset = offsetof(ImgUtils, log_ctx),
286 };
287 
288 int av_image_check_size2(unsigned int w, unsigned int h, int64_t max_pixels, enum AVPixelFormat pix_fmt, int log_offset, void *log_ctx)
289 {
290  ImgUtils imgutils = {
291  .class = &imgutils_class,
292  .log_offset = log_offset,
293  .log_ctx = log_ctx,
294  };
295  int64_t stride = av_image_get_linesize(pix_fmt, w, 0);
296  if (stride <= 0)
297  stride = 8LL*w;
298  stride += 128*8;
299 
300  if ((int)w<=0 || (int)h<=0 || stride >= INT_MAX || stride*(uint64_t)(h+128) >= INT_MAX) {
301  av_log(&imgutils, AV_LOG_ERROR, "Picture size %ux%u is invalid\n", w, h);
302  return AVERROR(EINVAL);
303  }
304 
305  if (max_pixels < INT64_MAX) {
306  if (w*(int64_t)h > max_pixels) {
307  av_log(&imgutils, AV_LOG_ERROR,
308  "Picture size %ux%u exceeds specified max pixel count %"PRId64", see the documentation if you wish to increase it\n",
309  w, h, max_pixels);
310  return AVERROR(EINVAL);
311  }
312  }
313 
314  return 0;
315 }
316 
317 int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
318 {
319  return av_image_check_size2(w, h, INT64_MAX, AV_PIX_FMT_NONE, log_offset, log_ctx);
320 }
321 
322 int av_image_check_sar(unsigned int w, unsigned int h, AVRational sar)
323 {
324  int64_t scaled_dim;
325 
326  if (sar.den <= 0 || sar.num < 0)
327  return AVERROR(EINVAL);
328 
329  if (!sar.num || sar.num == sar.den)
330  return 0;
331 
332  if (sar.num < sar.den)
333  scaled_dim = av_rescale_rnd(w, sar.num, sar.den, AV_ROUND_ZERO);
334  else
335  scaled_dim = av_rescale_rnd(h, sar.den, sar.num, AV_ROUND_ZERO);
336 
337  if (scaled_dim > 0)
338  return 0;
339 
340  return AVERROR(EINVAL);
341 }
342 
343 static void image_copy_plane(uint8_t *dst, ptrdiff_t dst_linesize,
344  const uint8_t *src, ptrdiff_t src_linesize,
345  ptrdiff_t bytewidth, int height)
346 {
347  if (!dst || !src)
348  return;
349  av_assert0(FFABS(src_linesize) >= bytewidth);
350  av_assert0(FFABS(dst_linesize) >= bytewidth);
351  for (;height > 0; height--) {
352  memcpy(dst, src, bytewidth);
353  dst += dst_linesize;
354  src += src_linesize;
355  }
356 }
357 
358 static void image_copy_plane_uc_from(uint8_t *dst, ptrdiff_t dst_linesize,
359  const uint8_t *src, ptrdiff_t src_linesize,
360  ptrdiff_t bytewidth, int height)
361 {
362  int ret = -1;
363 
364 #if ARCH_X86
365  ret = ff_image_copy_plane_uc_from_x86(dst, dst_linesize, src, src_linesize,
366  bytewidth, height);
367 #endif
368 
369  if (ret < 0)
370  image_copy_plane(dst, dst_linesize, src, src_linesize, bytewidth, height);
371 }
372 
373 void av_image_copy_plane(uint8_t *dst, int dst_linesize,
374  const uint8_t *src, int src_linesize,
375  int bytewidth, int height)
376 {
377  image_copy_plane(dst, dst_linesize, src, src_linesize, bytewidth, height);
378 }
379 
380 static void image_copy(uint8_t *dst_data[4], const ptrdiff_t dst_linesizes[4],
381  const uint8_t *src_data[4], const ptrdiff_t src_linesizes[4],
382  enum AVPixelFormat pix_fmt, int width, int height,
383  void (*copy_plane)(uint8_t *, ptrdiff_t, const uint8_t *,
384  ptrdiff_t, ptrdiff_t, int))
385 {
386  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
387 
388  if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
389  return;
390 
391  if (desc->flags & AV_PIX_FMT_FLAG_PAL ||
392  desc->flags & FF_PSEUDOPAL) {
393  copy_plane(dst_data[0], dst_linesizes[0],
394  src_data[0], src_linesizes[0],
395  width, height);
396  /* copy the palette */
397  if ((desc->flags & AV_PIX_FMT_FLAG_PAL) || (dst_data[1] && src_data[1]))
398  memcpy(dst_data[1], src_data[1], 4*256);
399  } else {
400  int i, planes_nb = 0;
401 
402  for (i = 0; i < desc->nb_components; i++)
403  planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1);
404 
405  for (i = 0; i < planes_nb; i++) {
406  int h = height;
407  ptrdiff_t bwidth = av_image_get_linesize(pix_fmt, width, i);
408  if (bwidth < 0) {
409  av_log(NULL, AV_LOG_ERROR, "av_image_get_linesize failed\n");
410  return;
411  }
412  if (i == 1 || i == 2) {
413  h = AV_CEIL_RSHIFT(height, desc->log2_chroma_h);
414  }
415  copy_plane(dst_data[i], dst_linesizes[i],
416  src_data[i], src_linesizes[i],
417  bwidth, h);
418  }
419  }
420 }
421 
422 void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4],
423  const uint8_t *src_data[4], const int src_linesizes[4],
424  enum AVPixelFormat pix_fmt, int width, int height)
425 {
426  ptrdiff_t dst_linesizes1[4], src_linesizes1[4];
427  int i;
428 
429  for (i = 0; i < 4; i++) {
430  dst_linesizes1[i] = dst_linesizes[i];
431  src_linesizes1[i] = src_linesizes[i];
432  }
433 
434  image_copy(dst_data, dst_linesizes1, src_data, src_linesizes1, pix_fmt,
435  width, height, image_copy_plane);
436 }
437 
438 void av_image_copy_uc_from(uint8_t *dst_data[4], const ptrdiff_t dst_linesizes[4],
439  const uint8_t *src_data[4], const ptrdiff_t src_linesizes[4],
440  enum AVPixelFormat pix_fmt, int width, int height)
441 {
442  image_copy(dst_data, dst_linesizes, src_data, src_linesizes, pix_fmt,
443  width, height, image_copy_plane_uc_from);
444 }
445 
446 int av_image_fill_arrays(uint8_t *dst_data[4], int dst_linesize[4],
447  const uint8_t *src, enum AVPixelFormat pix_fmt,
448  int width, int height, int align)
449 {
450  int ret, i;
451 
452  ret = av_image_check_size(width, height, 0, NULL);
453  if (ret < 0)
454  return ret;
455 
456  ret = av_image_fill_linesizes(dst_linesize, pix_fmt, width);
457  if (ret < 0)
458  return ret;
459 
460  for (i = 0; i < 4; i++)
461  dst_linesize[i] = FFALIGN(dst_linesize[i], align);
462 
463  return av_image_fill_pointers(dst_data, pix_fmt, height, (uint8_t *)src, dst_linesize);
464 }
465 
467  int width, int height, int align)
468 {
469  int ret, i;
470  int linesize[4];
471  ptrdiff_t aligned_linesize[4];
472  size_t sizes[4];
473  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
474  if (!desc)
475  return AVERROR(EINVAL);
476 
477  ret = av_image_check_size(width, height, 0, NULL);
478  if (ret < 0)
479  return ret;
480 
481  // do not include palette for these pseudo-paletted formats
482  if (desc->flags & FF_PSEUDOPAL)
483  return FFALIGN(width, align) * height;
484 
485  ret = av_image_fill_linesizes(linesize, pix_fmt, width);
486  if (ret < 0)
487  return ret;
488 
489  for (i = 0; i < 4; i++)
490  aligned_linesize[i] = FFALIGN(linesize[i], align);
491 
492  ret = av_image_fill_plane_sizes(sizes, pix_fmt, height, aligned_linesize);
493  if (ret < 0)
494  return ret;
495 
496  ret = 0;
497  for (i = 0; i < 4; i++) {
498  if (sizes[i] > INT_MAX - ret)
499  return AVERROR(EINVAL);
500  ret += sizes[i];
501  }
502  return ret;
503 }
504 
505 int av_image_copy_to_buffer(uint8_t *dst, int dst_size,
506  const uint8_t * const src_data[4],
507  const int src_linesize[4],
508  enum AVPixelFormat pix_fmt,
509  int width, int height, int align)
510 {
511  int i, j, nb_planes = 0, linesize[4];
512  int size = av_image_get_buffer_size(pix_fmt, width, height, align);
513  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
514  int ret;
515 
516  if (size > dst_size || size < 0 || !desc)
517  return AVERROR(EINVAL);
518 
519  for (i = 0; i < desc->nb_components; i++)
520  nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
521 
522  nb_planes++;
523 
524  ret = av_image_fill_linesizes(linesize, pix_fmt, width);
525  av_assert0(ret >= 0); // was checked previously
526 
527  for (i = 0; i < nb_planes; i++) {
528  int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
529  const uint8_t *src = src_data[i];
530  h = (height + (1 << shift) - 1) >> shift;
531 
532  for (j = 0; j < h; j++) {
533  memcpy(dst, src, linesize[i]);
534  dst += FFALIGN(linesize[i], align);
535  src += src_linesize[i];
536  }
537  }
538 
539  if (desc->flags & AV_PIX_FMT_FLAG_PAL) {
540  uint32_t *d32 = (uint32_t *)dst;
541 
542  for (i = 0; i<256; i++)
543  AV_WL32(d32 + i, AV_RN32(src_data[1] + 4*i));
544  }
545 
546  return size;
547 }
548 
549 // Fill dst[0..dst_size] with the bytes in clear[0..clear_size]. The clear
550 // bytes are repeated until dst_size is reached. If dst_size is unaligned (i.e.
551 // dst_size%clear_size!=0), the remaining data will be filled with the beginning
552 // of the clear data only.
553 static void memset_bytes(uint8_t *dst, size_t dst_size, uint8_t *clear,
554  size_t clear_size)
555 {
556  int same = 1;
557  int i;
558 
559  if (!clear_size)
560  return;
561 
562  // Reduce to memset() if possible.
563  for (i = 0; i < clear_size; i++) {
564  if (clear[i] != clear[0]) {
565  same = 0;
566  break;
567  }
568  }
569  if (same)
570  clear_size = 1;
571 
572  if (clear_size == 1) {
573  memset(dst, clear[0], dst_size);
574  } else {
575  if (clear_size > dst_size)
576  clear_size = dst_size;
577  memcpy(dst, clear, clear_size);
578  av_memcpy_backptr(dst + clear_size, clear_size, dst_size - clear_size);
579  }
580 }
581 
582 // Maximum size in bytes of a plane element (usually a pixel, or multiple pixels
583 // if it's a subsampled packed format).
584 #define MAX_BLOCK_SIZE 32
585 
586 int av_image_fill_black(uint8_t *dst_data[4], const ptrdiff_t dst_linesize[4],
587  enum AVPixelFormat pix_fmt, enum AVColorRange range,
588  int width, int height)
589 {
590  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
591  int nb_planes = av_pix_fmt_count_planes(pix_fmt);
592  // A pixel or a group of pixels on each plane, with a value that represents black.
593  // Consider e.g. AV_PIX_FMT_UYVY422 for non-trivial cases.
594  uint8_t clear_block[4][MAX_BLOCK_SIZE] = {{0}}; // clear padding with 0
595  int clear_block_size[4] = {0};
596  ptrdiff_t plane_line_bytes[4] = {0};
597  int rgb, limited;
598  int plane, c;
599 
600  if (!desc || nb_planes < 1 || nb_planes > 4 || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
601  return AVERROR(EINVAL);
602 
603  rgb = !!(desc->flags & AV_PIX_FMT_FLAG_RGB);
604  limited = !rgb && range != AVCOL_RANGE_JPEG;
605 
606  if (desc->flags & AV_PIX_FMT_FLAG_BITSTREAM) {
607  ptrdiff_t bytewidth = av_image_get_linesize(pix_fmt, width, 0);
608  uint8_t *data;
609  int mono = pix_fmt == AV_PIX_FMT_MONOWHITE || pix_fmt == AV_PIX_FMT_MONOBLACK;
610  int fill = pix_fmt == AV_PIX_FMT_MONOWHITE ? 0xFF : 0;
611  if (nb_planes != 1 || !(rgb || mono) || bytewidth < 1)
612  return AVERROR(EINVAL);
613 
614  if (!dst_data)
615  return 0;
616 
617  data = dst_data[0];
618 
619  // (Bitstream + alpha will be handled incorrectly - it'll remain transparent.)
620  for (;height > 0; height--) {
621  memset(data, fill, bytewidth);
622  data += dst_linesize[0];
623  }
624  return 0;
625  }
626 
627  for (c = 0; c < desc->nb_components; c++) {
628  const AVComponentDescriptor comp = desc->comp[c];
629 
630  // We try to operate on entire non-subsampled pixel groups (for
631  // AV_PIX_FMT_UYVY422 this would mean two consecutive pixels).
632  clear_block_size[comp.plane] = FFMAX(clear_block_size[comp.plane], comp.step);
633 
634  if (clear_block_size[comp.plane] > MAX_BLOCK_SIZE)
635  return AVERROR(EINVAL);
636  }
637 
638  // Create a byte array for clearing 1 pixel (sometimes several pixels).
639  for (c = 0; c < desc->nb_components; c++) {
640  const AVComponentDescriptor comp = desc->comp[c];
641  // (Multiple pixels happen e.g. with AV_PIX_FMT_UYVY422.)
642  int w = clear_block_size[comp.plane] / comp.step;
643  uint8_t *c_data[4];
644  const int c_linesize[4] = {0};
645  uint16_t src_array[MAX_BLOCK_SIZE];
646  uint16_t src = 0;
647  int x;
648 
649  if (comp.depth > 16)
650  return AVERROR(EINVAL);
651  if (!rgb && comp.depth < 8)
652  return AVERROR(EINVAL);
653  if (w < 1)
654  return AVERROR(EINVAL);
655 
656  if (c == 0 && limited) {
657  src = 16 << (comp.depth - 8);
658  } else if ((c == 1 || c == 2) && !rgb) {
659  src = 128 << (comp.depth - 8);
660  } else if (c == 3) {
661  // (Assume even limited YUV uses full range alpha.)
662  src = (1 << comp.depth) - 1;
663  }
664 
665  for (x = 0; x < w; x++)
666  src_array[x] = src;
667 
668  for (x = 0; x < 4; x++)
669  c_data[x] = &clear_block[x][0];
670 
671  av_write_image_line(src_array, c_data, c_linesize, desc, 0, 0, c, w);
672  }
673 
674  for (plane = 0; plane < nb_planes; plane++) {
675  plane_line_bytes[plane] = av_image_get_linesize(pix_fmt, width, plane);
676  if (plane_line_bytes[plane] < 0)
677  return AVERROR(EINVAL);
678  }
679 
680  if (!dst_data)
681  return 0;
682 
683  for (plane = 0; plane < nb_planes; plane++) {
684  size_t bytewidth = plane_line_bytes[plane];
685  uint8_t *data = dst_data[plane];
686  int chroma_div = plane == 1 || plane == 2 ? desc->log2_chroma_h : 0;
687  int plane_h = ((height + ( 1 << chroma_div) - 1)) >> chroma_div;
688 
689  for (; plane_h > 0; plane_h--) {
690  memset_bytes(data, bytewidth, &clear_block[plane][0], clear_block_size[plane]);
691  data += dst_linesize[plane];
692  }
693  }
694 
695  return 0;
696 }
#define AV_PIX_FMT_FLAG_PAL
Pixel format has a palette in data[1], values are indexes in this palette.
Definition: pixdesc.h:132
int plane
Which of the 4 planes contains the component.
Definition: pixdesc.h:35
#define NULL
Definition: coverity.c:32
int av_image_get_linesize(enum AVPixelFormat pix_fmt, int width, int plane)
Compute the size of an image line with format pix_fmt and width width for the plane plane...
Definition: imgutils.c:76
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:2573
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
int av_image_copy_to_buffer(uint8_t *dst, int dst_size, const uint8_t *const src_data[4], const int src_linesize[4], enum AVPixelFormat pix_fmt, int width, int height, int align)
Copy image data from an image into a buffer.
Definition: imgutils.c:505
const char * desc
Definition: libsvtav1.c:79
misc image utilities
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2613
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
const char * g
Definition: vf_curves.c:115
int av_image_alloc(uint8_t *pointers[4], int linesizes[4], int w, int h, enum AVPixelFormat pix_fmt, int align)
Allocate an image with size w and h and pixel format pix_fmt, and fill pointers and linesizes accordi...
Definition: imgutils.c:216
int num
Numerator.
Definition: rational.h:59
static const AVClass imgutils_class
Definition: imgutils.c:279
int av_image_fill_arrays(uint8_t *dst_data[4], int dst_linesize[4], const uint8_t *src, enum AVPixelFormat pix_fmt, int width, int height, int align)
Setup the data pointers and linesizes based on the specified image parameters and the provided array...
Definition: imgutils.c:446
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
static void memset_bytes(uint8_t *dst, size_t dst_size, uint8_t *clear, size_t clear_size)
Definition: imgutils.c:553
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:92
packed RGB 1:2:1, 8bpp, (msb)1B 2G 1R(lsb)
Definition: pixfmt.h:85
void av_image_fill_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4], const AVPixFmtDescriptor *pixdesc)
Compute the max pixel step for each plane of an image with a format described by pixdesc.
Definition: imgutils.c:35
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
Undefined Behavior In the C some operations are like signed integer dereferencing freed pointers
Definition: undefined.txt:3
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:117
uint8_t
#define av_malloc(s)
int avpriv_set_systematic_pal2(uint32_t pal[256], enum AVPixelFormat pix_fmt)
Definition: imgutils.c:176
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
void av_write_image_line(const uint16_t *src, uint8_t *data[4], const int linesize[4], const AVPixFmtDescriptor *desc, int x, int y, int c, int w)
Definition: pixdesc.c:162
void * log_ctx
Definition: imgutils.c:276
int av_image_check_sar(unsigned int w, unsigned int h, AVRational sar)
Check if the given sample aspect ratio of an image is valid.
Definition: imgutils.c:322
#define height
#define MAX_BLOCK_SIZE
Definition: imgutils.c:584
void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
Overlapping memcpy() implementation.
Definition: mem.c:428
AVColorRange
Visual content value range.
Definition: pixfmt.h:551
ptrdiff_t size
Definition: opengl_enc.c:100
#define FFALIGN(x, a)
Definition: macros.h:48
#define av_log(a,...)
#define U(x)
Definition: vp56_arith.h:37
#define src
Definition: vp8dsp.c:254
int av_image_get_buffer_size(enum AVPixelFormat pix_fmt, int width, int height, int align)
Return the size in bytes of the amount of data required to store an image with the given parameters...
Definition: imgutils.c:466
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:101
static const int sizes[][2]
Definition: img2dec.c:53
#define AV_PIX_FMT_FLAG_RGB
The pixel format contains RGB-like data (as opposed to YUV/grayscale).
Definition: pixdesc.h:148
static void image_copy_plane_uc_from(uint8_t *dst, ptrdiff_t dst_linesize, const uint8_t *src, ptrdiff_t src_linesize, ptrdiff_t bytewidth, int height)
Definition: imgutils.c:358
const char * r
Definition: vf_curves.c:114
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
Definition: pixdesc.h:140
simple assert() macros that are a bit more flexible than ISO C assert().
#define FFMAX(a, b)
Definition: common.h:94
void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4], const uint8_t *src_data[4], const int src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height)
Copy image in src_data to dst_data.
Definition: imgutils.c:422
common internal API header
uint64_t flags
Combination of AV_PIX_FMT_FLAG_...
Definition: pixdesc.h:106
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:317
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition: pixdesc.h:83
#define b
Definition: input.c:41
packed RGB 1:2:1, 8bpp, (msb)1R 2G 1B(lsb)
Definition: pixfmt.h:88
Round toward zero.
Definition: mathematics.h:80
#define width
uint8_t w
Definition: llviddspenc.c:38
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
#define s(width, name)
Definition: cbs_vp9.c:257
static int image_get_linesize(int width, int plane, int max_step, int max_step_comp, const AVPixFmtDescriptor *desc)
Definition: imgutils.c:54
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
Rescale a 64-bit integer with specified rounding.
Definition: mathematics.c:58
Full range content.
Definition: pixfmt.h:586
static void comp(unsigned char *dst, ptrdiff_t dst_stride, unsigned char *src, ptrdiff_t src_stride, int add)
Definition: eamad.c:83
const AVClass * class
Definition: imgutils.c:274
int log_offset
Definition: imgutils.c:275
packed RGB 3:3:2, 8bpp, (msb)2B 3G 3R(lsb)
Definition: pixfmt.h:83
int av_image_fill_pointers(uint8_t *data[4], enum AVPixelFormat pix_fmt, int height, uint8_t *ptr, const int linesizes[4])
Fill plane data pointers for an image with pixel format pix_fmt and height height.
Definition: imgutils.c:146
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
static void copy_plane(AVCodecContext *avctx, AVFrame *src, AVFrame *dst)
Definition: rasc.c:79
Describe the class of an AVClass context structure.
Definition: log.h:67
static void image_copy(uint8_t *dst_data[4], const ptrdiff_t dst_linesizes[4], const uint8_t *src_data[4], const ptrdiff_t src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height, void(*copy_plane)(uint8_t *, ptrdiff_t, const uint8_t *, ptrdiff_t, ptrdiff_t, int))
Definition: imgutils.c:380
int av_image_fill_linesizes(int linesizes[4], enum AVPixelFormat pix_fmt, int width)
Fill plane linesizes for an image with pixel format pix_fmt and width width.
Definition: imgutils.c:89
Rational number (pair of numerator and denominator).
Definition: rational.h:58
const uint8_t * d32
Definition: yuv2rgb.c:502
int av_image_fill_plane_sizes(size_t sizes[4], enum AVPixelFormat pix_fmt, int height, const ptrdiff_t linesizes[4])
Fill plane sizes for an image with pixel format pix_fmt and height height.
Definition: imgutils.c:111
#define AV_RN32(p)
Definition: intreadwrite.h:364
#define AV_PIX_FMT_FLAG_BITSTREAM
All values of a component are bit-wise packed end to end.
Definition: pixdesc.h:136
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb...
Definition: pixfmt.h:76
GLint GLenum GLboolean GLsizei stride
Definition: opengl_enc.c:104
Y , 8bpp.
Definition: pixfmt.h:74
common internal and external API header
Y , 1bpp, 0 is white, 1 is black, in each byte pixels are ordered from the msb to the lsb...
Definition: pixfmt.h:75
Utilties for rational number calculation.
void av_image_copy_uc_from(uint8_t *dst_data[4], const ptrdiff_t dst_linesizes[4], const uint8_t *src_data[4], const ptrdiff_t src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height)
Copy image data located in uncacheable (e.g.
Definition: imgutils.c:438
packed RGB 3:3:2, 8bpp, (msb)2R 3G 3B(lsb)
Definition: pixfmt.h:86
int den
Denominator.
Definition: rational.h:60
int av_image_check_size2(unsigned int w, unsigned int h, int64_t max_pixels, enum AVPixelFormat pix_fmt, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of a plane of an image with...
Definition: imgutils.c:288
#define FF_PSEUDOPAL
Definition: internal.h:335
#define av_free(p)
int av_image_fill_black(uint8_t *dst_data[4], const ptrdiff_t dst_linesize[4], enum AVPixelFormat pix_fmt, enum AVColorRange range, int width, int height)
Overwrite the image data with black.
Definition: imgutils.c:586
static void image_copy_plane(uint8_t *dst, ptrdiff_t dst_linesize, const uint8_t *src, ptrdiff_t src_linesize, ptrdiff_t bytewidth, int height)
Definition: imgutils.c:343
void av_image_copy_plane(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int height)
Copy image plane from src to dst.
Definition: imgutils.c:373
int ff_image_copy_plane_uc_from_x86(uint8_t *dst, ptrdiff_t dst_linesize, const uint8_t *src, ptrdiff_t src_linesize, ptrdiff_t bytewidth, int height)
Definition: imgutils_init.c:34
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
int depth
Number of bits in the component.
Definition: pixdesc.h:58
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
Definition: rpzaenc.c:58
for(j=16;j >0;--j)
int i
Definition: input.c:407
int step
Number of elements between 2 horizontally consecutive pixels.
Definition: pixdesc.h:41
#define AV_WL32(p, v)
Definition: intreadwrite.h:426
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58