2 * This file is part of Libav.
4 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 * misc image utilities
26 #include "imgutils_internal.h"
28 #include "intreadwrite.h"
30 #include "mathematics.h"
34 void av_image_fill_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4],
35 const AVPixFmtDescriptor *pixdesc)
38 memset(max_pixsteps, 0, 4*sizeof(max_pixsteps[0]));
39 if (max_pixstep_comps)
40 memset(max_pixstep_comps, 0, 4*sizeof(max_pixstep_comps[0]));
42 for (i = 0; i < 4; i++) {
43 const AVComponentDescriptor *comp = &(pixdesc->comp[i]);
44 if (comp->step > max_pixsteps[comp->plane]) {
45 max_pixsteps[comp->plane] = comp->step;
46 if (max_pixstep_comps)
47 max_pixstep_comps[comp->plane] = i;
52 int av_image_get_linesize(enum AVPixelFormat pix_fmt, int width, int plane)
54 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
55 int max_step [4]; /* max pixel step for each plane */
56 int max_step_comp[4]; /* the component for each plane which has the max pixel step */
60 return AVERROR(EINVAL);
62 if (desc->flags & AV_PIX_FMT_FLAG_BITSTREAM)
63 return (width * desc->comp[0].step + 7) >> 3;
65 av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
66 s = (max_step_comp[plane] == 1 || max_step_comp[plane] == 2) ? desc->log2_chroma_w : 0;
67 return max_step[plane] * (((width + (1 << s) - 1)) >> s);
70 int av_image_fill_linesizes(int linesizes[4], enum AVPixelFormat pix_fmt, int width)
73 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
74 int max_step [4]; /* max pixel step for each plane */
75 int max_step_comp[4]; /* the component for each plane which has the max pixel step */
77 memset(linesizes, 0, 4*sizeof(linesizes[0]));
79 if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
80 return AVERROR(EINVAL);
82 if (desc->flags & AV_PIX_FMT_FLAG_BITSTREAM) {
83 if (width > (INT_MAX - 7) / desc->comp[0].step)
84 return AVERROR(EINVAL);
85 linesizes[0] = (width * desc->comp[0].step + 7) >> 3;
89 av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
90 for (i = 0; i < 4; i++) {
91 int s = (max_step_comp[i] == 1 || max_step_comp[i] == 2) ? desc->log2_chroma_w : 0;
92 int shifted_w = ((width + (1 << s) - 1)) >> s;
93 if (max_step[i] > INT_MAX / shifted_w)
94 return AVERROR(EINVAL);
95 linesizes[i] = max_step[i] * shifted_w;
101 int av_image_fill_pointers(uint8_t *data[4], enum AVPixelFormat pix_fmt, int height,
102 uint8_t *ptr, const int linesizes[4])
104 int i, total_size, size[4] = { 0 }, has_plane[4] = { 0 };
106 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
107 memset(data , 0, sizeof(data[0])*4);
109 if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
110 return AVERROR(EINVAL);
113 if (linesizes[0] > (INT_MAX - 1024) / height)
114 return AVERROR(EINVAL);
115 size[0] = linesizes[0] * height;
117 if (desc->flags & AV_PIX_FMT_FLAG_PAL ||
118 desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
119 size[0] = (size[0] + 3) & ~3;
120 data[1] = ptr + size[0]; /* palette is stored here as 256 32 bits words */
121 return size[0] + 256 * 4;
124 for (i = 0; i < 4; i++)
125 has_plane[desc->comp[i].plane] = 1;
127 total_size = size[0];
128 for (i = 1; i < 4 && has_plane[i]; i++) {
129 int h, s = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
130 data[i] = data[i-1] + size[i-1];
131 h = (height + (1 << s) - 1) >> s;
132 if (linesizes[i] > INT_MAX / h)
133 return AVERROR(EINVAL);
134 size[i] = h * linesizes[i];
135 if (total_size > INT_MAX - size[i])
136 return AVERROR(EINVAL);
137 total_size += size[i];
143 int avpriv_set_systematic_pal2(uint32_t pal[256], enum AVPixelFormat pix_fmt)
147 for (i = 0; i < 256; i++) {
151 case AV_PIX_FMT_RGB8:
156 case AV_PIX_FMT_BGR8:
161 case AV_PIX_FMT_RGB4_BYTE:
166 case AV_PIX_FMT_BGR4_BYTE:
171 case AV_PIX_FMT_GRAY8:
175 return AVERROR(EINVAL);
177 pal[i] = b + (g << 8) + (r << 16) + (0xFFU << 24);
183 int av_image_alloc(uint8_t *pointers[4], int linesizes[4],
184 int w, int h, enum AVPixelFormat pix_fmt, int align)
186 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
191 return AVERROR(EINVAL);
193 if ((ret = av_image_check_size(w, h, 0, NULL)) < 0)
195 if ((ret = av_image_fill_linesizes(linesizes, pix_fmt, w)) < 0)
198 for (i = 0; i < 4; i++)
199 linesizes[i] = FFALIGN(linesizes[i], align);
201 if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, NULL, linesizes)) < 0)
203 buf = av_malloc(ret + align);
205 return AVERROR(ENOMEM);
206 if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, buf, linesizes)) < 0) {
210 if (desc->flags & AV_PIX_FMT_FLAG_PAL || desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)
211 avpriv_set_systematic_pal2((uint32_t*)pointers[1], pix_fmt);
216 typedef struct ImgUtils {
217 const AVClass *class;
222 static const AVClass imgutils_class = {
223 .class_name = "IMGUTILS",
224 .item_name = av_default_item_name,
226 .version = LIBAVUTIL_VERSION_INT,
227 .log_level_offset_offset = offsetof(ImgUtils, log_offset),
228 .parent_log_context_offset = offsetof(ImgUtils, log_ctx),
231 int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
233 ImgUtils imgutils = { &imgutils_class, log_offset, log_ctx };
235 if ((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/8)
238 av_log(&imgutils, AV_LOG_ERROR, "Picture size %ux%u is invalid\n", w, h);
239 return AVERROR(EINVAL);
242 int av_image_check_sar(unsigned int w, unsigned int h, AVRational sar)
247 return AVERROR(EINVAL);
249 if (!sar.num || sar.num == sar.den)
252 if (sar.num < sar.den)
253 scaled_dim = av_rescale_rnd(w, sar.num, sar.den, AV_ROUND_ZERO);
255 scaled_dim = av_rescale_rnd(h, sar.den, sar.num, AV_ROUND_ZERO);
260 return AVERROR(EINVAL);
263 static void image_copy_plane(uint8_t *dst, ptrdiff_t dst_linesize,
264 const uint8_t *src, ptrdiff_t src_linesize,
265 ptrdiff_t bytewidth, int height)
269 for (;height > 0; height--) {
270 memcpy(dst, src, bytewidth);
276 static void image_copy_plane_uc_from(uint8_t *dst, ptrdiff_t dst_linesize,
277 const uint8_t *src, ptrdiff_t src_linesize,
278 ptrdiff_t bytewidth, int height)
283 ret = ff_image_copy_plane_uc_from_x86(dst, dst_linesize, src, src_linesize,
288 image_copy_plane(dst, dst_linesize, src, src_linesize, bytewidth, height);
291 void av_image_copy_plane(uint8_t *dst, int dst_linesize,
292 const uint8_t *src, int src_linesize,
293 int bytewidth, int height)
295 image_copy_plane(dst, dst_linesize, src, src_linesize, bytewidth, height);
298 static void image_copy(uint8_t *dst_data[4], const ptrdiff_t dst_linesizes[4],
299 const uint8_t *src_data[4], const ptrdiff_t src_linesizes[4],
300 enum AVPixelFormat pix_fmt, int width, int height,
301 void (*copy_plane)(uint8_t *, ptrdiff_t, const uint8_t *,
302 ptrdiff_t, ptrdiff_t, int))
304 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
306 if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
309 if (desc->flags & AV_PIX_FMT_FLAG_PAL ||
310 desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
311 copy_plane(dst_data[0], dst_linesizes[0],
312 src_data[0], src_linesizes[0],
314 /* copy the palette */
315 memcpy(dst_data[1], src_data[1], 4*256);
317 int i, planes_nb = 0;
319 for (i = 0; i < desc->nb_components; i++)
320 planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1);
322 for (i = 0; i < planes_nb; i++) {
324 ptrdiff_t bwidth = av_image_get_linesize(pix_fmt, width, i);
325 if (i == 1 || i == 2) {
326 h = AV_CEIL_RSHIFT(height, desc->log2_chroma_h);
328 copy_plane(dst_data[i], dst_linesizes[i],
329 src_data[i], src_linesizes[i],
335 void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4],
336 const uint8_t *src_data[4], const int src_linesizes[4],
337 enum AVPixelFormat pix_fmt, int width, int height)
339 ptrdiff_t dst_linesizes1[4], src_linesizes1[4];
342 for (i = 0; i < 4; i++) {
343 dst_linesizes1[i] = dst_linesizes[i];
344 src_linesizes1[i] = src_linesizes[i];
347 image_copy(dst_data, dst_linesizes1, src_data, src_linesizes1, pix_fmt,
348 width, height, image_copy_plane);
351 void av_image_copy_uc_from(uint8_t *dst_data[4], const ptrdiff_t dst_linesizes[4],
352 const uint8_t *src_data[4], const ptrdiff_t src_linesizes[4],
353 enum AVPixelFormat pix_fmt, int width, int height)
355 image_copy(dst_data, dst_linesizes, src_data, src_linesizes, pix_fmt,
356 width, height, image_copy_plane_uc_from);
359 int av_image_fill_arrays(uint8_t *dst_data[4], int dst_linesize[4],
360 const uint8_t *src, enum AVPixelFormat pix_fmt,
361 int width, int height, int align)
365 ret = av_image_check_size(width, height, 0, NULL);
369 ret = av_image_fill_linesizes(dst_linesize, pix_fmt, width);
373 for (i = 0; i < 4; i++)
374 dst_linesize[i] = FFALIGN(dst_linesize[i], align);
376 return av_image_fill_pointers(dst_data, pix_fmt, height, src, dst_linesize);
379 int av_image_get_buffer_size(enum AVPixelFormat pix_fmt,
380 int width, int height, int align)
385 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
389 ret = av_image_check_size(width, height, 0, NULL);
393 // do not include palette for these pseudo-paletted formats
394 if (desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)
395 return width * height;
397 return av_image_fill_arrays(data, linesize, NULL, pix_fmt,
398 width, height, align);
401 int av_image_copy_to_buffer(uint8_t *dst, int dst_size,
402 const uint8_t * const src_data[4],
403 const int src_linesize[4],
404 enum AVPixelFormat pix_fmt,
405 int width, int height, int align)
407 int i, j, nb_planes = 0, linesize[4];
408 int size = av_image_get_buffer_size(pix_fmt, width, height, align);
409 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
411 if (size > dst_size || size < 0 || !desc)
412 return AVERROR(EINVAL);
414 for (i = 0; i < desc->nb_components; i++)
415 nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
419 av_image_fill_linesizes(linesize, pix_fmt, width);
420 for (i = 0; i < nb_planes; i++) {
421 int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
422 const uint8_t *src = src_data[i];
423 h = (height + (1 << shift) - 1) >> shift;
425 for (j = 0; j < h; j++) {
426 memcpy(dst, src, linesize[i]);
427 dst += FFALIGN(linesize[i], align);
428 src += src_linesize[i];
432 if (desc->flags & AV_PIX_FMT_FLAG_PAL)
433 memcpy((unsigned char *)(((size_t)dst + 3) & ~3),
434 src_data[1], 256 * 4);
439 // Fill dst[0..dst_size] with the bytes in clear[0..clear_size]. The clear
440 // bytes are repeated until dst_size is reached. If dst_size is unaligned (i.e.
441 // dst_size%clear_size!=0), the remaining data will be filled with the beginning
442 // of the clear data only.
443 static void memset_bytes(uint8_t *dst, size_t dst_size, uint8_t *clear,
453 // Reduce to memset() if possible.
454 for (i = 0; i < clear_size; i++) {
455 if (clear[i] != clear[0]) {
463 if (clear_size == 1) {
464 memset(dst, clear[0], dst_size);
466 } else if (clear_size == 2) {
467 uint16_t val = AV_RN16(clear);
468 for (; dst_size >= 2; dst_size -= 2) {
472 } else if (clear_size == 4) {
473 uint32_t val = AV_RN32(clear);
474 for (; dst_size >= 4; dst_size -= 4) {
478 } else if (clear_size == 8) {
479 uint32_t val = AV_RN64(clear);
480 for (; dst_size >= 8; dst_size -= 8) {
486 for (; dst_size; dst_size--)
487 *dst++ = clear[pos++ % clear_size];
490 // Maximum size in bytes of a plane element (usually a pixel, or multiple pixels
491 // if it's a subsampled packed format).
492 #define MAX_BLOCK_SIZE 32
494 int av_image_fill_black(uint8_t *dst_data[4], const ptrdiff_t dst_linesize[4],
495 enum AVPixelFormat pix_fmt, enum AVColorRange range,
496 int width, int height)
498 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
499 int nb_planes = av_pix_fmt_count_planes(pix_fmt);
500 // A pixel or a group of pixels on each plane, with a value that represents black.
501 // Consider e.g. AV_PIX_FMT_UYVY422 for non-trivial cases.
502 uint8_t clear_block[4][MAX_BLOCK_SIZE] = {0}; // clear padding with 0
503 int clear_block_size[4] = {0};
504 ptrdiff_t plane_line_bytes[4] = {0};
508 if (!desc || nb_planes < 1 || nb_planes > 4 || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
509 return AVERROR(EINVAL);
511 rgb = !!(desc->flags & AV_PIX_FMT_FLAG_RGB);
512 limited = !rgb && range != AVCOL_RANGE_JPEG;
514 if (desc->flags & AV_PIX_FMT_FLAG_BITSTREAM) {
515 ptrdiff_t bytewidth = av_image_get_linesize(pix_fmt, width, 0);
517 int mono = pix_fmt == AV_PIX_FMT_MONOWHITE || pix_fmt == AV_PIX_FMT_MONOBLACK;
518 int fill = pix_fmt == AV_PIX_FMT_MONOWHITE ? 0xFF : 0;
519 if (nb_planes != 1 || !(rgb || mono) || bytewidth < 1)
520 return AVERROR(EINVAL);
527 // (Bitstream + alpha will be handled incorrectly - it'll remain transparent.)
528 for (;height > 0; height--) {
529 memset(data, fill, bytewidth);
530 data += dst_linesize[0];
535 for (c = 0; c < desc->nb_components; c++) {
536 const AVComponentDescriptor comp = desc->comp[c];
538 // We try to operate on entire non-subsampled pixel groups (for
539 // AV_PIX_FMT_UYVY422 this would mean two consecutive pixels).
540 clear_block_size[comp.plane] = FFMAX(clear_block_size[comp.plane], comp.step);
542 if (clear_block_size[comp.plane] > MAX_BLOCK_SIZE)
543 return AVERROR(EINVAL);
546 // Create a byte array for clearing 1 pixel (sometimes several pixels).
547 for (c = 0; c < desc->nb_components; c++) {
548 const AVComponentDescriptor comp = desc->comp[c];
549 // (Multiple pixels happen e.g. with AV_PIX_FMT_UYVY422.)
550 int w = clear_block_size[comp.plane] / comp.step;
552 const int c_linesize[4] = {0};
553 uint16_t src_array[MAX_BLOCK_SIZE];
558 return AVERROR(EINVAL);
559 if (!rgb && comp.depth < 8)
560 return AVERROR(EINVAL);
562 return AVERROR(EINVAL);
564 if (c == 0 && limited) {
565 src = 16 << (comp.depth - 8);
566 } else if ((c == 1 || c == 2) && !rgb) {
567 src = 128 << (comp.depth - 8);
569 // (Assume even limited YUV uses full range alpha.)
570 src = (1 << comp.depth) - 1;
573 for (x = 0; x < w; x++)
576 for (x = 0; x < 4; x++)
577 c_data[x] = &clear_block[x][0];
579 av_write_image_line(src_array, c_data, c_linesize, desc, 0, 0, c, w);
582 for (plane = 0; plane < nb_planes; plane++) {
583 plane_line_bytes[plane] = av_image_get_linesize(pix_fmt, width, plane);
584 if (plane_line_bytes[plane] < 0)
585 return AVERROR(EINVAL);
591 for (plane = 0; plane < nb_planes; plane++) {
592 size_t bytewidth = plane_line_bytes[plane];
593 uint8_t *data = dst_data[plane];
594 int chroma_div = plane == 1 || plane == 2 ? desc->log2_chroma_h : 0;
595 int plane_h = ((height + ( 1 << chroma_div) - 1)) >> chroma_div;
597 for (; plane_h > 0; plane_h--) {
598 memset_bytes(data, bytewidth, &clear_block[plane][0], clear_block_size[plane]);
599 data += dst_linesize[plane];