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 "intreadwrite.h"
31 void av_image_fill_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4],
32 const AVPixFmtDescriptor *pixdesc)
35 memset(max_pixsteps, 0, 4*sizeof(max_pixsteps[0]));
36 if (max_pixstep_comps)
37 memset(max_pixstep_comps, 0, 4*sizeof(max_pixstep_comps[0]));
39 for (i = 0; i < 4; i++) {
40 const AVComponentDescriptor *comp = &(pixdesc->comp[i]);
41 if ((comp->step_minus1+1) > max_pixsteps[comp->plane]) {
42 max_pixsteps[comp->plane] = comp->step_minus1+1;
43 if (max_pixstep_comps)
44 max_pixstep_comps[comp->plane] = i;
50 int image_get_linesize(int width, int plane,
51 int max_step, int max_step_comp,
52 const AVPixFmtDescriptor *desc)
54 int s, shifted_w, linesize;
57 return AVERROR(EINVAL);
58 s = (max_step_comp == 1 || max_step_comp == 2) ? desc->log2_chroma_w : 0;
59 shifted_w = ((width + (1 << s) - 1)) >> s;
60 if (shifted_w && max_step > INT_MAX / shifted_w)
61 return AVERROR(EINVAL);
62 linesize = max_step * shifted_w;
63 if (desc->flags & PIX_FMT_BITSTREAM)
64 linesize = (linesize + 7) >> 3;
68 int av_image_get_linesize(enum PixelFormat pix_fmt, int width, int plane)
70 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
71 int max_step [4]; /* max pixel step for each plane */
72 int max_step_comp[4]; /* the component for each plane which has the max pixel step */
74 if ((unsigned)pix_fmt >= PIX_FMT_NB || desc->flags & PIX_FMT_HWACCEL)
75 return AVERROR(EINVAL);
77 av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
78 return image_get_linesize(width, plane, max_step[plane], max_step_comp[plane], desc);
81 int av_image_fill_linesizes(int linesizes[4], enum PixelFormat pix_fmt, int width)
84 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
85 int max_step [4]; /* max pixel step for each plane */
86 int max_step_comp[4]; /* the component for each plane which has the max pixel step */
88 memset(linesizes, 0, 4*sizeof(linesizes[0]));
90 if ((unsigned)pix_fmt >= PIX_FMT_NB || desc->flags & PIX_FMT_HWACCEL)
91 return AVERROR(EINVAL);
93 av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
94 for (i = 0; i < 4; i++) {
95 if ((ret = image_get_linesize(width, i, max_step[i], max_step_comp[i], desc)) < 0)
103 int av_image_fill_pointers(uint8_t *data[4], enum PixelFormat pix_fmt, int height,
104 uint8_t *ptr, const int linesizes[4])
106 int i, total_size, size[4] = { 0 }, has_plane[4] = { 0 };
108 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
109 memset(data , 0, sizeof(data[0])*4);
111 if ((unsigned)pix_fmt >= PIX_FMT_NB || desc->flags & PIX_FMT_HWACCEL)
112 return AVERROR(EINVAL);
115 if (linesizes[0] > (INT_MAX - 1024) / height)
116 return AVERROR(EINVAL);
117 size[0] = linesizes[0] * height;
119 if (desc->flags & PIX_FMT_PAL ||
120 desc->flags & PIX_FMT_PSEUDOPAL) {
121 size[0] = (size[0] + 3) & ~3;
122 data[1] = ptr + size[0]; /* palette is stored here as 256 32 bits words */
123 return size[0] + 256 * 4;
126 for (i = 0; i < 4; i++)
127 has_plane[desc->comp[i].plane] = 1;
129 total_size = size[0];
130 for (i = 1; i < 4 && has_plane[i]; i++) {
131 int h, s = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
132 data[i] = data[i-1] + size[i-1];
133 h = (height + (1 << s) - 1) >> s;
134 if (linesizes[i] > INT_MAX / h)
135 return AVERROR(EINVAL);
136 size[i] = h * linesizes[i];
137 if (total_size > INT_MAX - size[i])
138 return AVERROR(EINVAL);
139 total_size += size[i];
145 int ff_set_systematic_pal2(uint32_t pal[256], enum PixelFormat pix_fmt)
149 for (i = 0; i < 256; i++) {
163 case PIX_FMT_RGB4_BYTE:
168 case PIX_FMT_BGR4_BYTE:
177 return AVERROR(EINVAL);
179 pal[i] = b + (g<<8) + (r<<16) + (0xFF<<24);
185 int av_image_alloc(uint8_t *pointers[4], int linesizes[4],
186 int w, int h, enum PixelFormat pix_fmt, int align)
191 if ((ret = av_image_check_size(w, h, 0, NULL)) < 0)
193 if ((ret = av_image_fill_linesizes(linesizes, pix_fmt, align>7 ? FFALIGN(w, 8) : w)) < 0)
196 for (i = 0; i < 4; i++)
197 linesizes[i] = FFALIGN(linesizes[i], align);
199 if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, NULL, linesizes)) < 0)
201 buf = av_malloc(ret + align);
203 return AVERROR(ENOMEM);
204 if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, buf, linesizes)) < 0) {
208 if (av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_PAL ||
209 av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_PSEUDOPAL)
210 ff_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 void av_image_copy_plane(uint8_t *dst, int dst_linesize,
235 const uint8_t *src, int src_linesize,
236 int bytewidth, int height)
240 for (;height > 0; height--) {
241 memcpy(dst, src, bytewidth);
247 void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4],
248 const uint8_t *src_data[4], const int src_linesizes[4],
249 enum PixelFormat pix_fmt, int width, int height)
251 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
253 if (desc->flags & PIX_FMT_HWACCEL)
256 if (desc->flags & PIX_FMT_PAL ||
257 desc->flags & PIX_FMT_PSEUDOPAL) {
258 av_image_copy_plane(dst_data[0], dst_linesizes[0],
259 src_data[0], src_linesizes[0],
261 /* copy the palette */
262 memcpy(dst_data[1], src_data[1], 4*256);
264 int i, planes_nb = 0;
266 for (i = 0; i < desc->nb_components; i++)
267 planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1);
269 for (i = 0; i < planes_nb; i++) {
271 int bwidth = av_image_get_linesize(pix_fmt, width, i);
272 if (i == 1 || i == 2) {
273 h= -((-height)>>desc->log2_chroma_h);
275 av_image_copy_plane(dst_data[i], dst_linesizes[i],
276 src_data[i], src_linesizes[i],
282 int av_image_fill_arrays(uint8_t *dst_data[4], int dst_linesize[4],
284 enum PixelFormat pix_fmt, int width, int height, int align)
288 if ((ret = av_image_check_size(width, height, 0, NULL)) < 0)
291 if ((ret = av_image_fill_linesizes(dst_linesize, pix_fmt, width)) < 0)
294 for (i = 0; i < 4; i++)
295 dst_linesize[i] = FFALIGN(dst_linesize[i], align);
297 if ((ret = av_image_fill_pointers(dst_data, pix_fmt, width, NULL, dst_linesize)) < 0)
300 return av_image_fill_pointers(dst_data, pix_fmt, height, (uint8_t *)src, dst_linesize);
303 int av_image_get_buffer_size(enum PixelFormat pix_fmt, int width, int height, int align)
307 if (av_image_check_size(width, height, 0, NULL) < 0)
308 return AVERROR(EINVAL);
309 if (av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_PSEUDOPAL)
310 // do not include palette for these pseudo-paletted formats
311 return width * height;
312 return av_image_fill_arrays(data, linesize, NULL, pix_fmt, width, height, align);
315 int av_image_copy_to_buffer(uint8_t *dst, int dst_size,
316 const uint8_t * const src_data[4], const int src_linesize[4],
317 enum PixelFormat pix_fmt, int width, int height, int align)
319 int i, j, nb_planes = 0, linesize[4];
320 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
321 int size = av_image_get_buffer_size(pix_fmt, width, height, align);
323 if (size > dst_size || size < 0)
324 return AVERROR(EINVAL);
326 for (i = 0; i < desc->nb_components; i++)
327 nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
330 av_image_fill_linesizes(linesize, pix_fmt, width);
331 for (i = 0; i < nb_planes; i++) {
332 int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
333 const uint8_t *src = src_data[i];
334 h = (height + (1 << shift) - 1) >> shift;
336 for (j = 0; j < h; j++) {
337 memcpy(dst, src, linesize[i]);
338 dst += FFALIGN(linesize[i], align);
339 src += src_linesize[i];
343 if (desc->flags & PIX_FMT_PAL) {
344 uint32_t *d32 = (uint32_t *)(((size_t)dst + 3) & ~3);
345 for (i = 0; i<256; i++)
346 AV_WL32(d32 + i, AV_RN32(src_data[1] + 4*i));