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);
60 return AVERROR(EINVAL);
61 s = (max_step_comp == 1 || max_step_comp == 2) ? desc->log2_chroma_w : 0;
62 shifted_w = ((width + (1 << s) - 1)) >> s;
63 if (shifted_w && max_step > INT_MAX / shifted_w)
64 return AVERROR(EINVAL);
65 linesize = max_step * shifted_w;
67 if (desc->flags & PIX_FMT_BITSTREAM)
68 linesize = (linesize + 7) >> 3;
72 int av_image_get_linesize(enum AVPixelFormat pix_fmt, int width, int plane)
74 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
75 int max_step [4]; /* max pixel step for each plane */
76 int max_step_comp[4]; /* the component for each plane which has the max pixel step */
78 if ((unsigned)pix_fmt >= AV_PIX_FMT_NB || desc->flags & PIX_FMT_HWACCEL)
79 return AVERROR(EINVAL);
81 av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
82 return image_get_linesize(width, plane, max_step[plane], max_step_comp[plane], desc);
85 int av_image_fill_linesizes(int linesizes[4], enum AVPixelFormat pix_fmt, int width)
88 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
89 int max_step [4]; /* max pixel step for each plane */
90 int max_step_comp[4]; /* the component for each plane which has the max pixel step */
92 memset(linesizes, 0, 4*sizeof(linesizes[0]));
94 if (!desc || desc->flags & PIX_FMT_HWACCEL)
95 return AVERROR(EINVAL);
97 av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
98 for (i = 0; i < 4; i++) {
99 if ((ret = image_get_linesize(width, i, max_step[i], max_step_comp[i], desc)) < 0)
107 int av_image_fill_pointers(uint8_t *data[4], enum AVPixelFormat pix_fmt, int height,
108 uint8_t *ptr, const int linesizes[4])
110 int i, total_size, size[4] = { 0 }, has_plane[4] = { 0 };
112 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
113 memset(data , 0, sizeof(data[0])*4);
115 if (!desc || desc->flags & PIX_FMT_HWACCEL)
116 return AVERROR(EINVAL);
119 if (linesizes[0] > (INT_MAX - 1024) / height)
120 return AVERROR(EINVAL);
121 size[0] = linesizes[0] * height;
123 if (desc->flags & PIX_FMT_PAL ||
124 desc->flags & PIX_FMT_PSEUDOPAL) {
125 size[0] = (size[0] + 3) & ~3;
126 data[1] = ptr + size[0]; /* palette is stored here as 256 32 bits words */
127 return size[0] + 256 * 4;
130 for (i = 0; i < 4; i++)
131 has_plane[desc->comp[i].plane] = 1;
133 total_size = size[0];
134 for (i = 1; i < 4 && has_plane[i]; i++) {
135 int h, s = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
136 data[i] = data[i-1] + size[i-1];
137 h = (height + (1 << s) - 1) >> s;
138 if (linesizes[i] > INT_MAX / h)
139 return AVERROR(EINVAL);
140 size[i] = h * linesizes[i];
141 if (total_size > INT_MAX - size[i])
142 return AVERROR(EINVAL);
143 total_size += size[i];
149 int avpriv_set_systematic_pal2(uint32_t pal[256], enum AVPixelFormat pix_fmt)
153 for (i = 0; i < 256; i++) {
157 case AV_PIX_FMT_RGB8:
162 case AV_PIX_FMT_BGR8:
167 case AV_PIX_FMT_RGB4_BYTE:
172 case AV_PIX_FMT_BGR4_BYTE:
177 case AV_PIX_FMT_GRAY8:
181 return AVERROR(EINVAL);
183 pal[i] = b + (g<<8) + (r<<16) + (0xFFU<<24);
189 int av_image_alloc(uint8_t *pointers[4], int linesizes[4],
190 int w, int h, enum AVPixelFormat pix_fmt, int align)
192 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
197 return AVERROR(EINVAL);
199 if ((ret = av_image_check_size(w, h, 0, NULL)) < 0)
201 if ((ret = av_image_fill_linesizes(linesizes, pix_fmt, align>7 ? FFALIGN(w, 8) : w)) < 0)
204 for (i = 0; i < 4; i++)
205 linesizes[i] = FFALIGN(linesizes[i], align);
207 if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, NULL, linesizes)) < 0)
209 buf = av_malloc(ret + align);
211 return AVERROR(ENOMEM);
212 if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, buf, linesizes)) < 0) {
216 if (desc->flags & PIX_FMT_PAL || desc->flags & PIX_FMT_PSEUDOPAL)
217 avpriv_set_systematic_pal2((uint32_t*)pointers[1], pix_fmt);
222 typedef struct ImgUtils {
223 const AVClass *class;
228 static const AVClass imgutils_class = { "IMGUTILS", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT, offsetof(ImgUtils, log_offset), offsetof(ImgUtils, log_ctx) };
230 int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
232 ImgUtils imgutils = { &imgutils_class, log_offset, log_ctx };
234 if ((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/8)
237 av_log(&imgutils, AV_LOG_ERROR, "Picture size %ux%u is invalid\n", w, h);
238 return AVERROR(EINVAL);
241 void av_image_copy_plane(uint8_t *dst, int dst_linesize,
242 const uint8_t *src, int src_linesize,
243 int bytewidth, int height)
247 for (;height > 0; height--) {
248 memcpy(dst, src, bytewidth);
254 void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4],
255 const uint8_t *src_data[4], const int src_linesizes[4],
256 enum AVPixelFormat pix_fmt, int width, int height)
258 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
260 if (!desc || desc->flags & PIX_FMT_HWACCEL)
263 if (desc->flags & PIX_FMT_PAL ||
264 desc->flags & PIX_FMT_PSEUDOPAL) {
265 av_image_copy_plane(dst_data[0], dst_linesizes[0],
266 src_data[0], src_linesizes[0],
268 /* copy the palette */
269 memcpy(dst_data[1], src_data[1], 4*256);
271 int i, planes_nb = 0;
273 for (i = 0; i < desc->nb_components; i++)
274 planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1);
276 for (i = 0; i < planes_nb; i++) {
278 int bwidth = av_image_get_linesize(pix_fmt, width, i);
280 av_log(0, AV_LOG_ERROR, "av_image_get_linesize failed\n");
283 if (i == 1 || i == 2) {
284 h= -((-height)>>desc->log2_chroma_h);
286 av_image_copy_plane(dst_data[i], dst_linesizes[i],
287 src_data[i], src_linesizes[i],
293 int av_image_fill_arrays(uint8_t *dst_data[4], int dst_linesize[4],
295 enum AVPixelFormat pix_fmt, int width, int height, int align)
299 if ((ret = av_image_check_size(width, height, 0, NULL)) < 0)
302 if ((ret = av_image_fill_linesizes(dst_linesize, pix_fmt, width)) < 0)
305 for (i = 0; i < 4; i++)
306 dst_linesize[i] = FFALIGN(dst_linesize[i], align);
308 if ((ret = av_image_fill_pointers(dst_data, pix_fmt, width, NULL, dst_linesize)) < 0)
311 return av_image_fill_pointers(dst_data, pix_fmt, height, (uint8_t *)src, dst_linesize);
314 int av_image_get_buffer_size(enum AVPixelFormat pix_fmt, int width, int height, int align)
316 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
321 return AVERROR(EINVAL);
322 if (av_image_check_size(width, height, 0, NULL) < 0)
323 return AVERROR(EINVAL);
324 if (desc->flags & PIX_FMT_PSEUDOPAL)
325 // do not include palette for these pseudo-paletted formats
326 return width * height;
327 return av_image_fill_arrays(data, linesize, NULL, pix_fmt, width, height, align);
330 int av_image_copy_to_buffer(uint8_t *dst, int dst_size,
331 const uint8_t * const src_data[4], const int src_linesize[4],
332 enum AVPixelFormat pix_fmt, int width, int height, int align)
334 int i, j, nb_planes = 0, linesize[4];
335 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
336 int size = av_image_get_buffer_size(pix_fmt, width, height, align);
338 if (size > dst_size || size < 0)
339 return AVERROR(EINVAL);
341 for (i = 0; i < desc->nb_components; i++)
342 nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
345 av_image_fill_linesizes(linesize, pix_fmt, width);
346 for (i = 0; i < nb_planes; i++) {
347 int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
348 const uint8_t *src = src_data[i];
349 h = (height + (1 << shift) - 1) >> shift;
351 for (j = 0; j < h; j++) {
352 memcpy(dst, src, linesize[i]);
353 dst += FFALIGN(linesize[i], align);
354 src += src_linesize[i];
358 if (desc->flags & PIX_FMT_PAL) {
359 uint32_t *d32 = (uint32_t *)(((size_t)dst + 3) & ~3);
360 for (i = 0; i<256; i++)
361 AV_WL32(d32 + i, AV_RN32(src_data[1] + 4*i));