2 * This file is part of FFmpeg.
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.
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.
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
21 * misc image utilities
27 #include "imgutils_internal.h"
29 #include "intreadwrite.h"
31 #include "mathematics.h"
35 void av_image_fill_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4],
36 const AVPixFmtDescriptor *pixdesc)
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]));
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;
54 int image_get_linesize(int width, int plane,
55 int max_step, int max_step_comp,
56 const AVPixFmtDescriptor *desc)
58 int s, shifted_w, linesize;
61 return AVERROR(EINVAL);
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;
71 if (desc->flags & AV_PIX_FMT_FLAG_BITSTREAM)
72 linesize = (linesize + 7) >> 3;
76 int av_image_get_linesize(enum AVPixelFormat pix_fmt, int width, int plane)
78 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
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 */
82 if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
83 return AVERROR(EINVAL);
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);
89 int av_image_fill_linesizes(int linesizes[4], enum AVPixelFormat pix_fmt, int width)
92 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
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 */
96 memset(linesizes, 0, 4*sizeof(linesizes[0]));
98 if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
99 return AVERROR(EINVAL);
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)
111 int av_image_fill_pointers(uint8_t *data[4], enum AVPixelFormat pix_fmt, int height,
112 uint8_t *ptr, const int linesizes[4])
114 int i, total_size, size[4] = { 0 }, has_plane[4] = { 0 };
116 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
117 memset(data , 0, sizeof(data[0])*4);
119 if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
120 return AVERROR(EINVAL);
123 if (linesizes[0] > (INT_MAX - 1024) / height)
124 return AVERROR(EINVAL);
125 size[0] = linesizes[0] * height;
127 if (desc->flags & AV_PIX_FMT_FLAG_PAL ||
128 desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
129 data[1] = ptr + size[0]; /* palette is stored here as 256 32 bits words */
130 return size[0] + 256 * 4;
133 for (i = 0; i < 4; i++)
134 has_plane[desc->comp[i].plane] = 1;
136 total_size = size[0];
137 for (i = 1; i < 4 && has_plane[i]; i++) {
138 int h, s = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
139 data[i] = data[i-1] + size[i-1];
140 h = (height + (1 << s) - 1) >> s;
141 if (linesizes[i] > INT_MAX / h)
142 return AVERROR(EINVAL);
143 size[i] = h * linesizes[i];
144 if (total_size > INT_MAX - size[i])
145 return AVERROR(EINVAL);
146 total_size += size[i];
152 int avpriv_set_systematic_pal2(uint32_t pal[256], enum AVPixelFormat pix_fmt)
156 for (i = 0; i < 256; i++) {
160 case AV_PIX_FMT_RGB8:
165 case AV_PIX_FMT_BGR8:
170 case AV_PIX_FMT_RGB4_BYTE:
175 case AV_PIX_FMT_BGR4_BYTE:
180 case AV_PIX_FMT_GRAY8:
184 return AVERROR(EINVAL);
186 pal[i] = b + (g << 8) + (r << 16) + (0xFFU << 24);
192 int av_image_alloc(uint8_t *pointers[4], int linesizes[4],
193 int w, int h, enum AVPixelFormat pix_fmt, int align)
195 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
200 return AVERROR(EINVAL);
202 if ((ret = av_image_check_size(w, h, 0, NULL)) < 0)
204 if ((ret = av_image_fill_linesizes(linesizes, pix_fmt, align>7 ? FFALIGN(w, 8) : w)) < 0)
207 for (i = 0; i < 4; i++)
208 linesizes[i] = FFALIGN(linesizes[i], align);
210 if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, NULL, linesizes)) < 0)
212 buf = av_malloc(ret + align);
214 return AVERROR(ENOMEM);
215 if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, buf, linesizes)) < 0) {
219 if (desc->flags & AV_PIX_FMT_FLAG_PAL || desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
220 avpriv_set_systematic_pal2((uint32_t*)pointers[1], pix_fmt);
222 av_log(NULL, AV_LOG_ERROR, "Formats with a palette require a minimum alignment of 4\n");
223 return AVERROR(EINVAL);
227 if ((desc->flags & AV_PIX_FMT_FLAG_PAL ||
228 desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) &&
229 pointers[1] - pointers[0] > linesizes[0] * h) {
230 /* zero-initialize the padding before the palette */
231 memset(pointers[0] + linesizes[0] * h, 0,
232 pointers[1] - pointers[0] - linesizes[0] * h);
238 typedef struct ImgUtils {
239 const AVClass *class;
244 static const AVClass imgutils_class = {
245 .class_name = "IMGUTILS",
246 .item_name = av_default_item_name,
247 .version = LIBAVUTIL_VERSION_INT,
248 .log_level_offset_offset = offsetof(ImgUtils, log_offset),
249 .parent_log_context_offset = offsetof(ImgUtils, log_ctx),
252 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)
254 ImgUtils imgutils = {
255 .class = &imgutils_class,
256 .log_offset = log_offset,
259 int64_t stride = av_image_get_linesize(pix_fmt, w, 0);
264 if ((int)w<=0 || (int)h<=0 || stride >= INT_MAX || stride*(uint64_t)(h+128) >= INT_MAX) {
265 av_log(&imgutils, AV_LOG_ERROR, "Picture size %ux%u is invalid\n", w, h);
266 return AVERROR(EINVAL);
269 if (max_pixels < INT64_MAX) {
270 if (w*(int64_t)h > max_pixels) {
271 av_log(&imgutils, AV_LOG_ERROR,
272 "Picture size %ux%u exceeds specified max pixel count %"PRId64", see the documentation if you wish to increase it\n",
274 return AVERROR(EINVAL);
281 int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
283 return av_image_check_size2(w, h, INT64_MAX, AV_PIX_FMT_NONE, log_offset, log_ctx);
286 int av_image_check_sar(unsigned int w, unsigned int h, AVRational sar)
290 if (sar.den <= 0 || sar.num < 0)
291 return AVERROR(EINVAL);
293 if (!sar.num || sar.num == sar.den)
296 if (sar.num < sar.den)
297 scaled_dim = av_rescale_rnd(w, sar.num, sar.den, AV_ROUND_ZERO);
299 scaled_dim = av_rescale_rnd(h, sar.den, sar.num, AV_ROUND_ZERO);
304 return AVERROR(EINVAL);
307 static void image_copy_plane(uint8_t *dst, ptrdiff_t dst_linesize,
308 const uint8_t *src, ptrdiff_t src_linesize,
309 ptrdiff_t bytewidth, int height)
313 av_assert0(abs(src_linesize) >= bytewidth);
314 av_assert0(abs(dst_linesize) >= bytewidth);
315 for (;height > 0; height--) {
316 memcpy(dst, src, bytewidth);
322 static void image_copy_plane_uc_from(uint8_t *dst, ptrdiff_t dst_linesize,
323 const uint8_t *src, ptrdiff_t src_linesize,
324 ptrdiff_t bytewidth, int height)
329 ret = ff_image_copy_plane_uc_from_x86(dst, dst_linesize, src, src_linesize,
334 image_copy_plane(dst, dst_linesize, src, src_linesize, bytewidth, height);
337 void av_image_copy_plane(uint8_t *dst, int dst_linesize,
338 const uint8_t *src, int src_linesize,
339 int bytewidth, int height)
341 image_copy_plane(dst, dst_linesize, src, src_linesize, bytewidth, height);
344 static void image_copy(uint8_t *dst_data[4], const ptrdiff_t dst_linesizes[4],
345 const uint8_t *src_data[4], const ptrdiff_t src_linesizes[4],
346 enum AVPixelFormat pix_fmt, int width, int height,
347 void (*copy_plane)(uint8_t *, ptrdiff_t, const uint8_t *,
348 ptrdiff_t, ptrdiff_t, int))
350 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
352 if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
355 if (desc->flags & AV_PIX_FMT_FLAG_PAL ||
356 desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
357 copy_plane(dst_data[0], dst_linesizes[0],
358 src_data[0], src_linesizes[0],
360 /* copy the palette */
361 memcpy(dst_data[1], src_data[1], 4*256);
363 int i, planes_nb = 0;
365 for (i = 0; i < desc->nb_components; i++)
366 planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1);
368 for (i = 0; i < planes_nb; i++) {
370 ptrdiff_t bwidth = av_image_get_linesize(pix_fmt, width, i);
372 av_log(NULL, AV_LOG_ERROR, "av_image_get_linesize failed\n");
375 if (i == 1 || i == 2) {
376 h = AV_CEIL_RSHIFT(height, desc->log2_chroma_h);
378 copy_plane(dst_data[i], dst_linesizes[i],
379 src_data[i], src_linesizes[i],
385 void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4],
386 const uint8_t *src_data[4], const int src_linesizes[4],
387 enum AVPixelFormat pix_fmt, int width, int height)
389 ptrdiff_t dst_linesizes1[4], src_linesizes1[4];
392 for (i = 0; i < 4; i++) {
393 dst_linesizes1[i] = dst_linesizes[i];
394 src_linesizes1[i] = src_linesizes[i];
397 image_copy(dst_data, dst_linesizes1, src_data, src_linesizes1, pix_fmt,
398 width, height, image_copy_plane);
401 void av_image_copy_uc_from(uint8_t *dst_data[4], const ptrdiff_t dst_linesizes[4],
402 const uint8_t *src_data[4], const ptrdiff_t src_linesizes[4],
403 enum AVPixelFormat pix_fmt, int width, int height)
405 image_copy(dst_data, dst_linesizes, src_data, src_linesizes, pix_fmt,
406 width, height, image_copy_plane_uc_from);
409 int av_image_fill_arrays(uint8_t *dst_data[4], int dst_linesize[4],
410 const uint8_t *src, enum AVPixelFormat pix_fmt,
411 int width, int height, int align)
415 ret = av_image_check_size(width, height, 0, NULL);
419 ret = av_image_fill_linesizes(dst_linesize, pix_fmt, width);
423 for (i = 0; i < 4; i++)
424 dst_linesize[i] = FFALIGN(dst_linesize[i], align);
426 return av_image_fill_pointers(dst_data, pix_fmt, height, (uint8_t *)src, dst_linesize);
429 int av_image_get_buffer_size(enum AVPixelFormat pix_fmt,
430 int width, int height, int align)
435 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
437 return AVERROR(EINVAL);
439 ret = av_image_check_size(width, height, 0, NULL);
443 // do not include palette for these pseudo-paletted formats
444 if (desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)
445 return FFALIGN(width, align) * height;
447 return av_image_fill_arrays(data, linesize, NULL, pix_fmt,
448 width, height, align);
451 int av_image_copy_to_buffer(uint8_t *dst, int dst_size,
452 const uint8_t * const src_data[4],
453 const int src_linesize[4],
454 enum AVPixelFormat pix_fmt,
455 int width, int height, int align)
457 int i, j, nb_planes = 0, linesize[4];
458 int size = av_image_get_buffer_size(pix_fmt, width, height, align);
459 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
462 if (size > dst_size || size < 0 || !desc)
463 return AVERROR(EINVAL);
465 for (i = 0; i < desc->nb_components; i++)
466 nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
470 ret = av_image_fill_linesizes(linesize, pix_fmt, width);
471 av_assert0(ret >= 0); // was checked previously
473 for (i = 0; i < nb_planes; i++) {
474 int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
475 const uint8_t *src = src_data[i];
476 h = (height + (1 << shift) - 1) >> shift;
478 for (j = 0; j < h; j++) {
479 memcpy(dst, src, linesize[i]);
480 dst += FFALIGN(linesize[i], align);
481 src += src_linesize[i];
485 if (desc->flags & AV_PIX_FMT_FLAG_PAL) {
486 uint32_t *d32 = (uint32_t *)dst;
488 for (i = 0; i<256; i++)
489 AV_WL32(d32 + i, AV_RN32(src_data[1] + 4*i));
495 // Fill dst[0..dst_size] with the bytes in clear[0..clear_size]. The clear
496 // bytes are repeated until dst_size is reached. If dst_size is unaligned (i.e.
497 // dst_size%clear_size!=0), the remaining data will be filled with the beginning
498 // of the clear data only.
499 static void memset_bytes(uint8_t *dst, size_t dst_size, uint8_t *clear,
509 // Reduce to memset() if possible.
510 for (i = 0; i < clear_size; i++) {
511 if (clear[i] != clear[0]) {
519 if (clear_size == 1) {
520 memset(dst, clear[0], dst_size);
522 } else if (clear_size == 2) {
523 uint16_t val = AV_RN16(clear);
524 for (; dst_size >= 2; dst_size -= 2) {
528 } else if (clear_size == 4) {
529 uint32_t val = AV_RN32(clear);
530 for (; dst_size >= 4; dst_size -= 4) {
534 } else if (clear_size == 8) {
535 uint32_t val = AV_RN64(clear);
536 for (; dst_size >= 8; dst_size -= 8) {
542 for (; dst_size; dst_size--)
543 *dst++ = clear[pos++ % clear_size];
546 // Maximum size in bytes of a plane element (usually a pixel, or multiple pixels
547 // if it's a subsampled packed format).
548 #define MAX_BLOCK_SIZE 32
550 int av_image_fill_black(uint8_t *dst_data[4], const ptrdiff_t dst_linesize[4],
551 enum AVPixelFormat pix_fmt, enum AVColorRange range,
552 int width, int height)
554 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
555 int nb_planes = av_pix_fmt_count_planes(pix_fmt);
556 // A pixel or a group of pixels on each plane, with a value that represents black.
557 // Consider e.g. AV_PIX_FMT_UYVY422 for non-trivial cases.
558 uint8_t clear_block[4][MAX_BLOCK_SIZE] = {{0}}; // clear padding with 0
559 int clear_block_size[4] = {0};
560 ptrdiff_t plane_line_bytes[4] = {0};
564 if (!desc || nb_planes < 1 || nb_planes > 4 || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
565 return AVERROR(EINVAL);
567 rgb = !!(desc->flags & AV_PIX_FMT_FLAG_RGB);
568 limited = !rgb && range != AVCOL_RANGE_JPEG;
570 if (desc->flags & AV_PIX_FMT_FLAG_BITSTREAM) {
571 ptrdiff_t bytewidth = av_image_get_linesize(pix_fmt, width, 0);
573 int mono = pix_fmt == AV_PIX_FMT_MONOWHITE || pix_fmt == AV_PIX_FMT_MONOBLACK;
574 int fill = pix_fmt == AV_PIX_FMT_MONOWHITE ? 0xFF : 0;
575 if (nb_planes != 1 || !(rgb || mono) || bytewidth < 1)
576 return AVERROR(EINVAL);
583 // (Bitstream + alpha will be handled incorrectly - it'll remain transparent.)
584 for (;height > 0; height--) {
585 memset(data, fill, bytewidth);
586 data += dst_linesize[0];
591 for (c = 0; c < desc->nb_components; c++) {
592 const AVComponentDescriptor comp = desc->comp[c];
594 // We try to operate on entire non-subsampled pixel groups (for
595 // AV_PIX_FMT_UYVY422 this would mean two consecutive pixels).
596 clear_block_size[comp.plane] = FFMAX(clear_block_size[comp.plane], comp.step);
598 if (clear_block_size[comp.plane] > MAX_BLOCK_SIZE)
599 return AVERROR(EINVAL);
602 // Create a byte array for clearing 1 pixel (sometimes several pixels).
603 for (c = 0; c < desc->nb_components; c++) {
604 const AVComponentDescriptor comp = desc->comp[c];
605 // (Multiple pixels happen e.g. with AV_PIX_FMT_UYVY422.)
606 int w = clear_block_size[comp.plane] / comp.step;
608 const int c_linesize[4] = {0};
609 uint16_t src_array[MAX_BLOCK_SIZE];
614 return AVERROR(EINVAL);
615 if (!rgb && comp.depth < 8)
616 return AVERROR(EINVAL);
618 return AVERROR(EINVAL);
620 if (c == 0 && limited) {
621 src = 16 << (comp.depth - 8);
622 } else if ((c == 1 || c == 2) && !rgb) {
623 src = 128 << (comp.depth - 8);
625 // (Assume even limited YUV uses full range alpha.)
626 src = (1 << comp.depth) - 1;
629 for (x = 0; x < w; x++)
632 for (x = 0; x < 4; x++)
633 c_data[x] = &clear_block[x][0];
635 av_write_image_line(src_array, c_data, c_linesize, desc, 0, 0, c, w);
638 for (plane = 0; plane < nb_planes; plane++) {
639 plane_line_bytes[plane] = av_image_get_linesize(pix_fmt, width, plane);
640 if (plane_line_bytes[plane] < 0)
641 return AVERROR(EINVAL);
647 for (plane = 0; plane < nb_planes; plane++) {
648 size_t bytewidth = plane_line_bytes[plane];
649 uint8_t *data = dst_data[plane];
650 int chroma_div = plane == 1 || plane == 2 ? desc->log2_chroma_h : 0;
651 int plane_h = ((height + ( 1 << chroma_div) - 1)) >> chroma_div;
653 for (; plane_h > 0; plane_h--) {
654 memset_bytes(data, bytewidth, &clear_block[plane][0], clear_block_size[plane]);
655 data += dst_linesize[plane];