FFmpeg
Loading...
Searching...
No Matches
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 "intreadwrite.h"
29#include "log.h"
30#include "mathematics.h"
31#include "mem.h"
32#include "pixdesc.h"
33#include "rational.h"
34
35void 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
53static inline
54int image_get_linesize(int width, int plane,
55 int max_step, int max_step_comp,
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 = AV_CEIL_RSHIFT(width, s);
67 if (shifted_w && max_step > INT_MAX / shifted_w)
68 return AVERROR(EINVAL);
69 linesize = max_step * shifted_w;
70
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
89int 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
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 sizes[1] = 256 * 4; /* palette is stored here as 256 32 bits words */
128 return 0;
129 }
130
131 for (i = 0; i < 4; i++)
132 has_plane[desc->comp[i].plane] = 1;
133
134 for (i = 1; i < 4 && has_plane[i]; i++) {
135 int h, s = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
137 if (linesizes[i] > SIZE_MAX / h)
138 return AVERROR(EINVAL);
139 sizes[i] = (size_t)h * linesizes[i];
140 }
141
142 return 0;
143}
144
146 uint8_t *ptr, const int linesizes[4])
147{
148 int i, ret;
149 ptrdiff_t linesizes1[4];
150 size_t sizes[4];
151
152 memset(data , 0, sizeof(data[0])*4);
153
154 for (i = 0; i < 4; i++)
155 linesizes1[i] = linesizes[i];
156
157 ret = av_image_fill_plane_sizes(sizes, pix_fmt, height, linesizes1);
158 if (ret < 0)
159 return ret;
160
161 ret = 0;
162 for (i = 0; i < 4; i++) {
163 if (sizes[i] > INT_MAX - ret)
164 return AVERROR(EINVAL);
165 ret += sizes[i];
166 }
167
168 if (!ptr)
169 return ret;
170
171 data[0] = ptr;
172 for (i = 1; i < 4 && sizes[i]; i++)
173 data[i] = data[i - 1] + sizes[i - 1];
174
175 return ret;
176}
177
179{
180 int i;
181
182 for (i = 0; i < 256; i++) {
183 int r, g, b;
184
185 switch (pix_fmt) {
186 case AV_PIX_FMT_RGB8:
187 r = (i>>5 )*36;
188 g = ((i>>2)&7)*36;
189 b = (i&3 )*85;
190 break;
191 case AV_PIX_FMT_BGR8:
192 b = (i>>6 )*85;
193 g = ((i>>3)&7)*36;
194 r = (i&7 )*36;
195 break;
197 r = (i>>3 )*255;
198 g = ((i>>1)&3)*85;
199 b = (i&1 )*255;
200 break;
202 b = (i>>3 )*255;
203 g = ((i>>1)&3)*85;
204 r = (i&1 )*255;
205 break;
206 case AV_PIX_FMT_GRAY8:
207 r = b = g = i;
208 break;
209 default:
210 return AVERROR(EINVAL);
211 }
212 pal[i] = b + (g << 8) + (r << 16) + (0xFFU << 24);
213 }
214
215 return 0;
216}
217
218int av_image_alloc(uint8_t *pointers[4], int linesizes[4],
219 int w, int h, enum AVPixelFormat pix_fmt, int align)
220{
222 int i, ret;
223 ptrdiff_t linesizes1[4];
224 size_t total_size, sizes[4];
225 uint8_t *buf;
226
227 if (!desc)
228 return AVERROR(EINVAL);
229
230 if ((ret = av_image_check_size(w, h, 0, NULL)) < 0)
231 return ret;
232 if ((ret = av_image_fill_linesizes(linesizes, pix_fmt, align>7 ? FFALIGN(w, 8) : w)) < 0)
233 return ret;
234
235 for (i = 0; i < 4; i++) {
236 linesizes[i] = FFALIGN(linesizes[i], align);
237 linesizes1[i] = linesizes[i];
238 }
239
240 if ((ret = av_image_fill_plane_sizes(sizes, pix_fmt, h, linesizes1)) < 0)
241 return ret;
242 total_size = align;
243 for (i = 0; i < 4; i++) {
244 if (total_size > SIZE_MAX - sizes[i])
245 return AVERROR(EINVAL);
246 total_size += sizes[i];
247 }
248 buf = av_malloc(total_size);
249 if (!buf)
250 return AVERROR(ENOMEM);
251 if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, buf, linesizes)) < 0) {
252 av_free(buf);
253 return ret;
254 }
255 if (desc->flags & AV_PIX_FMT_FLAG_PAL) {
256 avpriv_set_systematic_pal2((uint32_t*)pointers[1], pix_fmt);
257 if (align < 4) {
258 av_log(NULL, AV_LOG_ERROR, "Formats with a palette require a minimum alignment of 4\n");
259 av_free(buf);
260 return AVERROR(EINVAL);
261 }
262 }
263
264 if (desc->flags & AV_PIX_FMT_FLAG_PAL && pointers[1] &&
265 pointers[1] - pointers[0] > linesizes[0] * h) {
266 /* zero-initialize the padding before the palette */
267 memset(pointers[0] + linesizes[0] * h, 0,
268 pointers[1] - pointers[0] - linesizes[0] * h);
269 }
270
271 return ret;
272}
273
274typedef struct ImgUtils {
275 const AVClass *class;
277 void *log_ctx;
278} ImgUtils;
279
280static const AVClass imgutils_class = {
281 .class_name = "IMGUTILS",
282 .item_name = av_default_item_name,
283 .option = NULL,
284 .version = LIBAVUTIL_VERSION_INT,
285 .log_level_offset_offset = offsetof(ImgUtils, log_offset),
286 .parent_log_context_offset = offsetof(ImgUtils, log_ctx),
287};
288
289int av_image_check_size2(unsigned int w, unsigned int h, int64_t max_pixels, enum AVPixelFormat pix_fmt, int log_offset, void *log_ctx)
290{
291 ImgUtils imgutils = {
292 .class = &imgutils_class,
293 .log_offset = log_offset,
294 .log_ctx = log_ctx,
295 };
297 if (stride <= 0)
298 stride = 8LL*w;
299 stride += 128*8;
300
301 if (w==0 || h==0 || w > INT32_MAX || h > INT32_MAX || stride >= INT_MAX || stride*(h + 128ULL) >= INT_MAX) {
302 av_log(&imgutils, AV_LOG_ERROR, "Picture size %ux%u is invalid\n", w, h);
303 return AVERROR(EINVAL);
304 }
305
306 if (max_pixels < INT64_MAX) {
307 if (w*(int64_t)h > max_pixels) {
308 av_log(&imgutils, AV_LOG_ERROR,
309 "Picture size %ux%u exceeds specified max pixel count %"PRId64", see the documentation if you wish to increase it\n",
310 w, h, max_pixels);
311 return AVERROR(EINVAL);
312 }
313 }
314
315 return 0;
316}
317
318int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
319{
320 return av_image_check_size2(w, h, INT64_MAX, AV_PIX_FMT_NONE, log_offset, log_ctx);
321}
322
323int av_image_check_sar(unsigned int w, unsigned int h, AVRational sar)
324{
325 int64_t scaled_dim;
326
327 if (sar.den <= 0 || sar.num < 0)
328 return AVERROR(EINVAL);
329
330 if (!sar.num || sar.num == sar.den)
331 return 0;
332
333 if (sar.num < sar.den)
334 scaled_dim = av_rescale_rnd(w, sar.num, sar.den, AV_ROUND_ZERO);
335 else
336 scaled_dim = av_rescale_rnd(h, sar.den, sar.num, AV_ROUND_ZERO);
337
338 if (scaled_dim > 0)
339 return 0;
340
341 return AVERROR(EINVAL);
342}
343
344static void image_copy_plane(uint8_t *dst, ptrdiff_t dst_linesize,
345 const uint8_t *src, ptrdiff_t src_linesize,
346 ptrdiff_t bytewidth, int height)
347{
348 if (!dst || !src)
349 return;
350 av_assert0(FFABS(src_linesize) >= bytewidth);
351 av_assert0(FFABS(dst_linesize) >= bytewidth);
352 for (;height > 0; height--) {
353 memcpy(dst, src, bytewidth);
354 dst += dst_linesize;
355 src += src_linesize;
356 }
357}
358
359void av_image_copy_plane_uc_from(uint8_t *dst, ptrdiff_t dst_linesize,
360 const uint8_t *src, ptrdiff_t src_linesize,
361 ptrdiff_t bytewidth, int height)
362{
363 int ret = -1;
364
365#if ARCH_X86 && HAVE_X86ASM
366 ret = ff_image_copy_plane_uc_from_x86(dst, dst_linesize, src, src_linesize,
367 bytewidth, height);
368#endif
369
370 if (ret < 0)
371 image_copy_plane(dst, dst_linesize, src, src_linesize, bytewidth, height);
372}
373
374void av_image_copy_plane(uint8_t *dst, int dst_linesize,
375 const uint8_t *src, int src_linesize,
376 int bytewidth, int height)
377{
378 image_copy_plane(dst, dst_linesize, src, src_linesize, bytewidth, height);
379}
380
381static void image_copy(uint8_t *const dst_data[4], const ptrdiff_t dst_linesizes[4],
382 const uint8_t *const src_data[4], const ptrdiff_t src_linesizes[4],
383 enum AVPixelFormat pix_fmt, int width, int height,
384 void (*copy_plane)(uint8_t *, ptrdiff_t, const uint8_t *,
385 ptrdiff_t, ptrdiff_t, int))
386{
388
389 if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
390 return;
391
392 if (desc->flags & AV_PIX_FMT_FLAG_PAL) {
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
422void av_image_copy(uint8_t *const dst_data[4], const int dst_linesizes[4],
423 const uint8_t * const 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,
436}
437
438void av_image_copy_uc_from(uint8_t * const dst_data[4], const ptrdiff_t dst_linesizes[4],
439 const uint8_t * const 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,
444}
445
446int 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
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];
474 if (!desc)
475 return AVERROR(EINVAL);
476
478 if (ret < 0)
479 return ret;
480
481 ret = av_image_fill_linesizes(linesize, pix_fmt, width);
482 if (ret < 0)
483 return ret;
484
485 for (i = 0; i < 4; i++)
486 aligned_linesize[i] = FFALIGN(linesize[i], align);
487
488 ret = av_image_fill_plane_sizes(sizes, pix_fmt, height, aligned_linesize);
489 if (ret < 0)
490 return ret;
491
492 ret = 0;
493 for (i = 0; i < 4; i++) {
494 if (sizes[i] > INT_MAX - ret)
495 return AVERROR(EINVAL);
496 ret += sizes[i];
497 }
498 return ret;
499}
500
501int av_image_copy_to_buffer(uint8_t *dst, int dst_size,
502 const uint8_t * const src_data[4],
503 const int src_linesize[4],
505 int width, int height, int align)
506{
507 int i, j, nb_planes = 0, linesize[4];
510 int ret;
511
512 if (size > dst_size || size < 0 || !desc)
513 return AVERROR(EINVAL);
514
515 for (i = 0; i < desc->nb_components; i++)
516 nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
517
518 nb_planes++;
519
520 ret = av_image_fill_linesizes(linesize, pix_fmt, width);
521 av_assert0(ret >= 0); // was checked previously
522
523 for (i = 0; i < nb_planes; i++) {
524 int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
525 const uint8_t *src = src_data[i];
527
528 for (j = 0; j < h; j++) {
529 memcpy(dst, src, linesize[i]);
530 dst += FFALIGN(linesize[i], align);
531 src += src_linesize[i];
532 }
533 }
534
535 if (desc->flags & AV_PIX_FMT_FLAG_PAL) {
536 uint32_t *d32 = (uint32_t *)dst;
537
538 for (i = 0; i<256; i++)
539 AV_WL32(d32 + i, AV_RN32(src_data[1] + 4*i));
540 }
541
542 return size;
543}
544
545// Fill dst[0..dst_size] with the bytes in clear[0..clear_size]. The clear
546// bytes are repeated until dst_size is reached. If dst_size is unaligned (i.e.
547// dst_size%clear_size!=0), the remaining data will be filled with the beginning
548// of the clear data only.
549static void memset_bytes(uint8_t *dst, size_t dst_size, uint8_t *clear,
550 size_t clear_size)
551{
552 int same = 1;
553 int i;
554
555 if (!clear_size)
556 return;
557
558 // Reduce to memset() if possible.
559 for (i = 0; i < clear_size; i++) {
560 if (clear[i] != clear[0]) {
561 same = 0;
562 break;
563 }
564 }
565 if (same)
566 clear_size = 1;
567
568 if (clear_size == 1) {
569 memset(dst, clear[0], dst_size);
570 } else {
571 if (clear_size > dst_size)
572 clear_size = dst_size;
573 memcpy(dst, clear, clear_size);
574 av_memcpy_backptr(dst + clear_size, clear_size, dst_size - clear_size);
575 }
576}
577
578// Maximum size in bytes of a plane element (usually a pixel, or multiple pixels
579// if it's a subsampled packed format).
580#define MAX_BLOCK_SIZE 32
581
582int av_image_fill_color(uint8_t * const dst_data[4], const ptrdiff_t dst_linesize[4],
583 enum AVPixelFormat pix_fmt, const uint32_t color[4],
584 int width, int height, int flags)
585{
587 int nb_planes = av_pix_fmt_count_planes(pix_fmt);
588 // A pixel or a group of pixels on each plane, with a value that represents the color.
589 // Consider e.g. AV_PIX_FMT_UYVY422 for non-trivial cases.
590 uint8_t clear_block[4][MAX_BLOCK_SIZE] = {{0}}; // clear padding with 0
591 int clear_block_size[4] = {0};
592 ptrdiff_t plane_line_bytes[4] = {0};
593 int bitstream;
594 int plane, c;
595
596 if (!desc || nb_planes < 1 || nb_planes > 4 || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
597 return AVERROR(EINVAL);
598
599 bitstream = !!(desc->flags & AV_PIX_FMT_FLAG_BITSTREAM);
600
601 for (c = 0; c < desc->nb_components; c++) {
602 const AVComponentDescriptor comp = desc->comp[c];
603
604 // We try to operate on entire non-subsampled pixel groups (for
605 // AV_PIX_FMT_UYVY422 this would mean two consecutive pixels).
606 clear_block_size[comp.plane] = FFMAX(clear_block_size[comp.plane], comp.step);
607
608 if (clear_block_size[comp.plane] > MAX_BLOCK_SIZE)
609 return AVERROR(EINVAL);
610 }
611
612 // Create a byte array for clearing 1 pixel (sometimes several pixels).
613 for (c = 0; c < desc->nb_components; c++) {
614 const AVComponentDescriptor comp = desc->comp[c];
615 // (Multiple pixels happen e.g. with AV_PIX_FMT_UYVY422.)
616 int w = (bitstream ? 8 : 1) * clear_block_size[comp.plane] / comp.step;
617 uint8_t *c_data[4];
618 const int c_linesize[4] = {0};
619 uint32_t src_array[MAX_BLOCK_SIZE];
620 int x;
621
622 if (comp.depth > 32)
623 return AVERROR(EINVAL);
624 if (w < 1)
625 return AVERROR(EINVAL);
626
627 for (x = 0; x < w; x++)
628 src_array[x] = color[c];
629
630 for (x = 0; x < 4; x++)
631 c_data[x] = &clear_block[x][0];
632
633 av_write_image_line2(src_array, c_data, c_linesize, desc, 0, 0, c, w, 4);
634 }
635
636 for (plane = 0; plane < nb_planes; plane++) {
637 plane_line_bytes[plane] = av_image_get_linesize(pix_fmt, width, plane);
638 if (plane_line_bytes[plane] < 0)
639 return AVERROR(EINVAL);
640 }
641
642 if (!dst_data)
643 return 0;
644
645 for (plane = 0; plane < nb_planes; plane++) {
646 size_t bytewidth = plane_line_bytes[plane];
647 uint8_t *data = dst_data[plane];
648 int chroma_div = plane == 1 || plane == 2 ? desc->log2_chroma_h : 0;
649 int plane_h = AV_CEIL_RSHIFT(height, chroma_div);
650
651 for (; plane_h > 0; plane_h--) {
652 memset_bytes(data, bytewidth, &clear_block[plane][0], clear_block_size[plane]);
653 data += dst_linesize[plane];
654 }
655 }
656
657 return 0;
658}
659
660int av_image_fill_black(uint8_t * const dst_data[4], const ptrdiff_t dst_linesize[4],
662 int width, int height)
663{
665 int nb_planes = av_pix_fmt_count_planes(pix_fmt);
666 int rgb, xyz, pal, limited, alpha, fltp;
667 uint32_t colors[4] = {0};
668
669 if (!desc || nb_planes < 1 || nb_planes > 4 || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
670 return AVERROR(EINVAL);
671
672 rgb = !!(desc->flags & AV_PIX_FMT_FLAG_RGB);
673 xyz = !!(desc->flags & AV_PIX_FMT_FLAG_XYZ);
674 pal = !!(desc->flags & AV_PIX_FMT_FLAG_PAL);
675 limited = !rgb && !xyz && !pal && range != AVCOL_RANGE_JPEG;
676 alpha = !pal && !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA);
677 fltp = !!(desc->flags & AV_PIX_FMT_FLAG_FLOAT);
678
679 for (int c = 0; c < desc->nb_components; c++) {
680 const AVComponentDescriptor comp = desc->comp[c];
681 uint32_t color = 0;
682
683 if (comp.depth > 32)
684 return AVERROR(EINVAL);
685
687 color = 1;
688 } else if (c + 1 == desc->nb_components && alpha) {
689 // (Assume even limited YUV uses full range alpha.)
690 if (fltp) {
691 if (comp.depth != 16 && comp.depth != 32)
692 return AVERROR(EINVAL);
693 color = (comp.depth == 16 ? 0x3C00 : 0x3F800000); // 1.0
694 } else {
695 color = (comp.depth == 32 ? 0 : (1 << comp.depth)) - 1;
696 }
697 } else if (c == 0 && limited && comp.depth > 1) {
698 if (comp.depth < 8 || (fltp && comp.depth != 16 && comp.depth != 32))
699 return AVERROR(EINVAL);
700 if (fltp)
701 color = (comp.depth == 16 ? 0x2C00 : 0x3D800000); // 0.0625
702 else
703 color = 16 << (comp.depth - 8);
704 } else if ((c == 1 || c == 2) && !rgb && !xyz) {
705 if (comp.depth < 8 || fltp && comp.depth != 16 && comp.depth != 32)
706 return AVERROR(EINVAL);
707 if (fltp)
708 color = (comp.depth == 16 ? 0x3800 : 0x3F000000); // 0.5
709 else
710 color = 128 << (comp.depth - 8);
711 }
712
713 colors[c] = color;
714 }
715
716 return av_image_fill_color(dst_data, dst_linesize, pix_fmt, colors, width, height, 0);
717}
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
static const uint8_t *BS_FUNC align(BSCTX *bc)
Skip bits to a byte boundary.
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
common internal and external API header
#define AV_CEIL_RSHIFT(a, b)
Definition common.h:60
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static enum AVPixelFormat pix_fmt
static void comp(unsigned char *dst, ptrdiff_t dst_stride, unsigned char *src, ptrdiff_t src_stride, int add)
Definition eamad.c:79
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
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
@ AV_ROUND_ZERO
Round toward zero.
void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
Overlapping memcpy() implementation.
Definition mem.c:445
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
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:318
int av_image_fill_black(uint8_t *const 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:660
void av_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)
Copy image data located in uncacheable (e.g.
Definition imgutils.c:359
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:145
void av_image_copy(uint8_t *const dst_data[4], const int dst_linesizes[4], const uint8_t *const 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
int av_image_fill_color(uint8_t *const dst_data[4], const ptrdiff_t dst_linesize[4], enum AVPixelFormat pix_fmt, const uint32_t color[4], int width, int height, int flags)
Overwrite the image data with a color.
Definition imgutils.c:582
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
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
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:501
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:374
void av_image_copy_uc_from(uint8_t *const dst_data[4], const ptrdiff_t dst_linesizes[4], const uint8_t *const 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
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:218
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
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:289
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
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
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:323
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
static const int16_t alpha[]
Definition ilbcdata.h:55
static const int sizes[][2]
Definition img2dec.c:62
int avpriv_set_systematic_pal2(uint32_t pal[256], enum AVPixelFormat pix_fmt)
Definition imgutils.c:178
static int image_get_linesize(int width, int plane, int max_step, int max_step_comp, const AVPixFmtDescriptor *desc)
Definition imgutils.c:54
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:344
#define MAX_BLOCK_SIZE
Definition imgutils.c:580
static const AVClass imgutils_class
Definition imgutils.c:280
static void image_copy(uint8_t *const dst_data[4], const ptrdiff_t dst_linesizes[4], const uint8_t *const 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:381
static void memset_bytes(uint8_t *dst, size_t dst_size, uint8_t *clear, size_t clear_size)
Definition imgutils.c:549
misc image utilities
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)
#define r
Definition input.c:42
#define b
Definition input.c:43
#define AV_RN32(p)
#define AV_WL32(p, v)
static int shift(int a, int b)
Definition bonk.c:261
const char * desc
Definition libsvtav1.c:83
uint8_t w
Definition llvidencdsp.c:39
#define FFMAX(a, b)
Definition macros.h:47
#define FFALIGN(x, a)
Definition macros.h:78
enum AVColorRange range
Memory handling functions.
const char data[16]
Definition mxf.c:149
#define av_malloc(s)
Definition ops_static.c:52
void av_write_image_line2(const void *src, uint8_t *data[4], const int linesize[4], const AVPixFmtDescriptor *desc, int x, int y, int c, int w, int src_element_size)
Write the values from src to the pixel format component c of an image line.
Definition pixdesc.c:117
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3500
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
#define AV_PIX_FMT_FLAG_ALPHA
The pixel format has an alpha channel.
Definition pixdesc.h:147
#define AV_PIX_FMT_FLAG_BITSTREAM
All values of a component are bit-wise packed end to end.
Definition pixdesc.h:124
#define AV_PIX_FMT_FLAG_RGB
The pixel format contains RGB-like data (as opposed to YUV/grayscale).
Definition pixdesc.h:136
#define AV_PIX_FMT_FLAG_FLOAT
The pixel format contains IEEE-754 floating point values.
Definition pixdesc.h:158
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
Definition pixdesc.h:128
#define AV_PIX_FMT_FLAG_XYZ
The pixel format contains XYZ-like data (as opposed to YUV/RGB/grayscale).
Definition pixdesc.h:163
#define AV_PIX_FMT_FLAG_PAL
Pixel format has a palette in data[1], values are indexes in this palette.
Definition pixdesc.h:120
AVColorRange
Visual content value range.
Definition pixfmt.h:748
@ AVCOL_RANGE_JPEG
Full range content.
Definition pixfmt.h:783
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ AV_PIX_FMT_RGB8
packed RGB 3:3:2, 8bpp, (msb)3R 3G 2B(lsb)
Definition pixfmt.h:93
@ AV_PIX_FMT_BGR8
packed RGB 3:3:2, 8bpp, (msb)2B 3G 3R(lsb)
Definition pixfmt.h:90
@ AV_PIX_FMT_RGB4_BYTE
packed RGB 1:2:1, 8bpp, (msb)1R 2G 1B(lsb)
Definition pixfmt.h:95
@ AV_PIX_FMT_BGR4_BYTE
packed RGB 1:2:1, 8bpp, (msb)1B 2G 1R(lsb)
Definition pixfmt.h:92
@ AV_PIX_FMT_MONOWHITE
Y , 1bpp, 0 is white, 1 is black, in each byte pixels are ordered from the msb to the lsb.
Definition pixfmt.h:82
static void copy_plane(AVCodecContext *avctx, AVFrame *src, AVFrame *dst)
Definition rasc.c:84
Utilities for rational number calculation.
Describe the class of an AVClass context structure.
Definition log.h:76
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition pixdesc.h:105
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
void * log_ctx
Definition imgutils.c:277
int log_offset
Definition imgutils.c:276
Definition rpzaenc.c:60
#define stride
#define av_free(p)
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
#define height
Definition dsp.h:89
#define width
Definition dsp.h:89
int size
const char * g
Definition vf_curves.c:128
static double c[64]