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
27 #include "intreadwrite.h"
29 #include "mathematics.h"
33 void av_image_fill_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4],
34 const AVPixFmtDescriptor *pixdesc)
37 memset(max_pixsteps, 0, 4*sizeof(max_pixsteps[0]));
38 if (max_pixstep_comps)
39 memset(max_pixstep_comps, 0, 4*sizeof(max_pixstep_comps[0]));
41 for (i = 0; i < 4; i++) {
42 const AVComponentDescriptor *comp = &(pixdesc->comp[i]);
43 if ((comp->step_minus1+1) > max_pixsteps[comp->plane]) {
44 max_pixsteps[comp->plane] = comp->step_minus1+1;
45 if (max_pixstep_comps)
46 max_pixstep_comps[comp->plane] = i;
51 int av_image_get_linesize(enum AVPixelFormat pix_fmt, int width, int plane)
53 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
54 int max_step [4]; /* max pixel step for each plane */
55 int max_step_comp[4]; /* the component for each plane which has the max pixel step */
59 return AVERROR(EINVAL);
61 if (desc->flags & AV_PIX_FMT_FLAG_BITSTREAM)
62 return (width * (desc->comp[0].step_minus1+1) + 7) >> 3;
64 av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
65 s = (max_step_comp[plane] == 1 || max_step_comp[plane] == 2) ? desc->log2_chroma_w : 0;
66 return max_step[plane] * (((width + (1 << s) - 1)) >> s);
69 int av_image_fill_linesizes(int linesizes[4], enum AVPixelFormat pix_fmt, int width)
72 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
73 int max_step [4]; /* max pixel step for each plane */
74 int max_step_comp[4]; /* the component for each plane which has the max pixel step */
76 memset(linesizes, 0, 4*sizeof(linesizes[0]));
78 if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
79 return AVERROR(EINVAL);
81 if (desc->flags & AV_PIX_FMT_FLAG_BITSTREAM) {
82 if (width > (INT_MAX -7) / (desc->comp[0].step_minus1+1))
83 return AVERROR(EINVAL);
84 linesizes[0] = (width * (desc->comp[0].step_minus1+1) + 7) >> 3;
88 av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
89 for (i = 0; i < 4; i++) {
90 int s = (max_step_comp[i] == 1 || max_step_comp[i] == 2) ? desc->log2_chroma_w : 0;
91 int shifted_w = ((width + (1 << s) - 1)) >> s;
92 if (max_step[i] > INT_MAX / shifted_w)
93 return AVERROR(EINVAL);
94 linesizes[i] = max_step[i] * shifted_w;
100 int av_image_fill_pointers(uint8_t *data[4], enum AVPixelFormat pix_fmt, int height,
101 uint8_t *ptr, const int linesizes[4])
103 int i, total_size, size[4] = { 0 }, has_plane[4] = { 0 };
105 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
106 memset(data , 0, sizeof(data[0])*4);
108 if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
109 return AVERROR(EINVAL);
112 if (linesizes[0] > (INT_MAX - 1024) / height)
113 return AVERROR(EINVAL);
114 size[0] = linesizes[0] * height;
116 if (desc->flags & AV_PIX_FMT_FLAG_PAL ||
117 desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
118 size[0] = (size[0] + 3) & ~3;
119 data[1] = ptr + size[0]; /* palette is stored here as 256 32 bits words */
120 return size[0] + 256 * 4;
123 for (i = 0; i < 4; i++)
124 has_plane[desc->comp[i].plane] = 1;
126 total_size = size[0];
127 for (i = 1; i < 4 && has_plane[i]; i++) {
128 int h, s = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
129 data[i] = data[i-1] + size[i-1];
130 h = (height + (1 << s) - 1) >> s;
131 if (linesizes[i] > INT_MAX / h)
132 return AVERROR(EINVAL);
133 size[i] = h * linesizes[i];
134 if (total_size > INT_MAX - size[i])
135 return AVERROR(EINVAL);
136 total_size += size[i];
142 int avpriv_set_systematic_pal2(uint32_t pal[256], enum AVPixelFormat pix_fmt)
146 for (i = 0; i < 256; i++) {
150 case AV_PIX_FMT_RGB8:
155 case AV_PIX_FMT_BGR8:
160 case AV_PIX_FMT_RGB4_BYTE:
165 case AV_PIX_FMT_BGR4_BYTE:
170 case AV_PIX_FMT_GRAY8:
174 return AVERROR(EINVAL);
176 pal[i] = b + (g << 8) + (r << 16) + (0xFFU << 24);
182 int av_image_alloc(uint8_t *pointers[4], int linesizes[4],
183 int w, int h, enum AVPixelFormat pix_fmt, int align)
185 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
190 return AVERROR(EINVAL);
192 if ((ret = av_image_check_size(w, h, 0, NULL)) < 0)
194 if ((ret = av_image_fill_linesizes(linesizes, pix_fmt, w)) < 0)
197 for (i = 0; i < 4; i++)
198 linesizes[i] = FFALIGN(linesizes[i], align);
200 if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, NULL, linesizes)) < 0)
202 buf = av_malloc(ret + align);
204 return AVERROR(ENOMEM);
205 if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, buf, linesizes)) < 0) {
209 if (desc->flags & AV_PIX_FMT_FLAG_PAL || desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)
210 avpriv_set_systematic_pal2((uint32_t*)pointers[1], pix_fmt);
215 typedef struct ImgUtils {
216 const AVClass *class;
221 static const AVClass imgutils_class = { "IMGUTILS", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT, offsetof(ImgUtils, log_offset), offsetof(ImgUtils, log_ctx) };
223 int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
225 ImgUtils imgutils = { &imgutils_class, log_offset, log_ctx };
227 if ((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/8)
230 av_log(&imgutils, AV_LOG_ERROR, "Picture size %ux%u is invalid\n", w, h);
231 return AVERROR(EINVAL);
234 int av_image_check_sar(unsigned int w, unsigned int h, AVRational sar)
239 return AVERROR(EINVAL);
241 if (!sar.num || sar.num == sar.den)
244 if (sar.num < sar.den)
245 scaled_dim = av_rescale_rnd(w, sar.num, sar.den, AV_ROUND_ZERO);
247 scaled_dim = av_rescale_rnd(h, sar.den, sar.num, AV_ROUND_ZERO);
252 return AVERROR(EINVAL);
255 void av_image_copy_plane(uint8_t *dst, int dst_linesize,
256 const uint8_t *src, int src_linesize,
257 int bytewidth, int height)
261 for (;height > 0; height--) {
262 memcpy(dst, src, bytewidth);
268 void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4],
269 const uint8_t *src_data[4], const int src_linesizes[4],
270 enum AVPixelFormat pix_fmt, int width, int height)
272 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
274 if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
277 if (desc->flags & AV_PIX_FMT_FLAG_PAL ||
278 desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
279 av_image_copy_plane(dst_data[0], dst_linesizes[0],
280 src_data[0], src_linesizes[0],
282 /* copy the palette */
283 memcpy(dst_data[1], src_data[1], 4*256);
285 int i, planes_nb = 0;
287 for (i = 0; i < desc->nb_components; i++)
288 planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1);
290 for (i = 0; i < planes_nb; i++) {
292 int bwidth = av_image_get_linesize(pix_fmt, width, i);
293 if (i == 1 || i == 2) {
294 h= -((-height)>>desc->log2_chroma_h);
296 av_image_copy_plane(dst_data[i], dst_linesizes[i],
297 src_data[i], src_linesizes[i],
303 int av_image_fill_arrays(uint8_t *dst_data[4], int dst_linesize[4],
304 const uint8_t *src, enum AVPixelFormat pix_fmt,
305 int width, int height, int align)
309 ret = av_image_check_size(width, height, 0, NULL);
313 ret = av_image_fill_linesizes(dst_linesize, pix_fmt, width);
317 for (i = 0; i < 4; i++)
318 dst_linesize[i] = FFALIGN(dst_linesize[i], align);
320 return av_image_fill_pointers(dst_data, pix_fmt, height, src, dst_linesize);
323 int av_image_get_buffer_size(enum AVPixelFormat pix_fmt,
324 int width, int height, int align)
329 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
333 ret = av_image_check_size(width, height, 0, NULL);
337 // do not include palette for these pseudo-paletted formats
338 if (desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)
339 return width * height;
341 return av_image_fill_arrays(data, linesize, NULL, pix_fmt,
342 width, height, align);
345 int av_image_copy_to_buffer(uint8_t *dst, int dst_size,
346 const uint8_t * const src_data[4],
347 const int src_linesize[4],
348 enum AVPixelFormat pix_fmt,
349 int width, int height, int align)
351 int i, j, nb_planes = 0, linesize[4];
352 int size = av_image_get_buffer_size(pix_fmt, width, height, align);
353 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
355 if (size > dst_size || size < 0 || !desc)
356 return AVERROR(EINVAL);
358 for (i = 0; i < desc->nb_components; i++)
359 nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
363 av_image_fill_linesizes(linesize, pix_fmt, width);
364 for (i = 0; i < nb_planes; i++) {
365 int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
366 const uint8_t *src = src_data[i];
367 h = (height + (1 << shift) - 1) >> shift;
369 for (j = 0; j < h; j++) {
370 memcpy(dst, src, linesize[i]);
371 dst += FFALIGN(linesize[i], align);
372 src += src_linesize[i];
376 if (desc->flags & AV_PIX_FMT_FLAG_PAL)
377 memcpy((unsigned char *)(((size_t)dst + 3) & ~3),
378 src_data[1], 256 * 4);