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 = { "IMGUTILS", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT, offsetof(ImgUtils, log_offset), offsetof(ImgUtils, log_ctx) };
224 int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
226 ImgUtils imgutils = { &imgutils_class, log_offset, log_ctx };
228 if ((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/8)
231 av_log(&imgutils, AV_LOG_ERROR, "Picture size %ux%u is invalid\n", w, h);
232 return AVERROR(EINVAL);
235 int av_image_check_sar(unsigned int w, unsigned int h, AVRational sar)
240 return AVERROR(EINVAL);
242 if (!sar.num || sar.num == sar.den)
245 if (sar.num < sar.den)
246 scaled_dim = av_rescale_rnd(w, sar.num, sar.den, AV_ROUND_ZERO);
248 scaled_dim = av_rescale_rnd(h, sar.den, sar.num, AV_ROUND_ZERO);
253 return AVERROR(EINVAL);
256 static void image_copy_plane(uint8_t *dst, ptrdiff_t dst_linesize,
257 const uint8_t *src, ptrdiff_t src_linesize,
258 ptrdiff_t bytewidth, int height)
262 for (;height > 0; height--) {
263 memcpy(dst, src, bytewidth);
269 static void image_copy_plane_uc_from(uint8_t *dst, ptrdiff_t dst_linesize,
270 const uint8_t *src, ptrdiff_t src_linesize,
271 ptrdiff_t bytewidth, int height)
276 ret = ff_image_copy_plane_uc_from_x86(dst, dst_linesize, src, src_linesize,
281 image_copy_plane(dst, dst_linesize, src, src_linesize, bytewidth, height);
284 void av_image_copy_plane(uint8_t *dst, int dst_linesize,
285 const uint8_t *src, int src_linesize,
286 int bytewidth, int height)
288 image_copy_plane(dst, dst_linesize, src, src_linesize, bytewidth, height);
291 static void image_copy(uint8_t *dst_data[4], const ptrdiff_t dst_linesizes[4],
292 const uint8_t *src_data[4], const ptrdiff_t src_linesizes[4],
293 enum AVPixelFormat pix_fmt, int width, int height,
294 void (*copy_plane)(uint8_t *, ptrdiff_t, const uint8_t *,
295 ptrdiff_t, ptrdiff_t, int))
297 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
299 if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
302 if (desc->flags & AV_PIX_FMT_FLAG_PAL ||
303 desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
304 copy_plane(dst_data[0], dst_linesizes[0],
305 src_data[0], src_linesizes[0],
307 /* copy the palette */
308 memcpy(dst_data[1], src_data[1], 4*256);
310 int i, planes_nb = 0;
312 for (i = 0; i < desc->nb_components; i++)
313 planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1);
315 for (i = 0; i < planes_nb; i++) {
317 ptrdiff_t bwidth = av_image_get_linesize(pix_fmt, width, i);
318 if (i == 1 || i == 2) {
319 h = AV_CEIL_RSHIFT(height, desc->log2_chroma_h);
321 copy_plane(dst_data[i], dst_linesizes[i],
322 src_data[i], src_linesizes[i],
328 void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4],
329 const uint8_t *src_data[4], const int src_linesizes[4],
330 enum AVPixelFormat pix_fmt, int width, int height)
332 ptrdiff_t dst_linesizes1[4], src_linesizes1[4];
335 for (i = 0; i < 4; i++) {
336 dst_linesizes1[i] = dst_linesizes[i];
337 src_linesizes1[i] = src_linesizes[i];
340 image_copy(dst_data, dst_linesizes1, src_data, src_linesizes1, pix_fmt,
341 width, height, image_copy_plane);
344 void av_image_copy_uc_from(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)
348 image_copy(dst_data, dst_linesizes, src_data, src_linesizes, pix_fmt,
349 width, height, image_copy_plane_uc_from);
352 int av_image_fill_arrays(uint8_t *dst_data[4], int dst_linesize[4],
353 const uint8_t *src, enum AVPixelFormat pix_fmt,
354 int width, int height, int align)
358 ret = av_image_check_size(width, height, 0, NULL);
362 ret = av_image_fill_linesizes(dst_linesize, pix_fmt, width);
366 for (i = 0; i < 4; i++)
367 dst_linesize[i] = FFALIGN(dst_linesize[i], align);
369 return av_image_fill_pointers(dst_data, pix_fmt, height, src, dst_linesize);
372 int av_image_get_buffer_size(enum AVPixelFormat pix_fmt,
373 int width, int height, int align)
378 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
382 ret = av_image_check_size(width, height, 0, NULL);
386 // do not include palette for these pseudo-paletted formats
387 if (desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)
388 return width * height;
390 return av_image_fill_arrays(data, linesize, NULL, pix_fmt,
391 width, height, align);
394 int av_image_copy_to_buffer(uint8_t *dst, int dst_size,
395 const uint8_t * const src_data[4],
396 const int src_linesize[4],
397 enum AVPixelFormat pix_fmt,
398 int width, int height, int align)
400 int i, j, nb_planes = 0, linesize[4];
401 int size = av_image_get_buffer_size(pix_fmt, width, height, align);
402 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
404 if (size > dst_size || size < 0 || !desc)
405 return AVERROR(EINVAL);
407 for (i = 0; i < desc->nb_components; i++)
408 nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
412 av_image_fill_linesizes(linesize, pix_fmt, width);
413 for (i = 0; i < nb_planes; i++) {
414 int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
415 const uint8_t *src = src_data[i];
416 h = (height + (1 << shift) - 1) >> shift;
418 for (j = 0; j < h; j++) {
419 memcpy(dst, src, linesize[i]);
420 dst += FFALIGN(linesize[i], align);
421 src += src_linesize[i];
425 if (desc->flags & AV_PIX_FMT_FLAG_PAL)
426 memcpy((unsigned char *)(((size_t)dst + 3) & ~3),
427 src_data[1], 256 * 4);