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
29 void av_image_fill_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4],
30 const AVPixFmtDescriptor *pixdesc)
33 memset(max_pixsteps, 0, 4*sizeof(max_pixsteps[0]));
34 if (max_pixstep_comps)
35 memset(max_pixstep_comps, 0, 4*sizeof(max_pixstep_comps[0]));
37 for (i = 0; i < 4; i++) {
38 const AVComponentDescriptor *comp = &(pixdesc->comp[i]);
39 if ((comp->step_minus1+1) > max_pixsteps[comp->plane]) {
40 max_pixsteps[comp->plane] = comp->step_minus1+1;
41 if (max_pixstep_comps)
42 max_pixstep_comps[comp->plane] = i;
48 int image_get_linesize(int width, int plane,
49 int max_step, int max_step_comp,
50 const AVPixFmtDescriptor *desc)
52 int s, shifted_w, linesize;
55 return AVERROR(EINVAL);
56 s = (max_step_comp == 1 || max_step_comp == 2) ? desc->log2_chroma_w : 0;
57 shifted_w = ((width + (1 << s) - 1)) >> s;
58 if (shifted_w && max_step > INT_MAX / shifted_w)
59 return AVERROR(EINVAL);
60 linesize = max_step * shifted_w;
61 if (desc->flags & PIX_FMT_BITSTREAM)
62 linesize = (linesize + 7) >> 3;
66 int av_image_get_linesize(enum PixelFormat pix_fmt, int width, int plane)
68 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
69 int max_step [4]; /* max pixel step for each plane */
70 int max_step_comp[4]; /* the component for each plane which has the max pixel step */
72 if ((unsigned)pix_fmt >= PIX_FMT_NB || desc->flags & PIX_FMT_HWACCEL)
73 return AVERROR(EINVAL);
75 av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
76 return image_get_linesize(width, plane, max_step[plane], max_step_comp[plane], desc);
79 int av_image_fill_linesizes(int linesizes[4], enum PixelFormat pix_fmt, int width)
82 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
83 int max_step [4]; /* max pixel step for each plane */
84 int max_step_comp[4]; /* the component for each plane which has the max pixel step */
86 memset(linesizes, 0, 4*sizeof(linesizes[0]));
88 if ((unsigned)pix_fmt >= PIX_FMT_NB || desc->flags & PIX_FMT_HWACCEL)
89 return AVERROR(EINVAL);
91 av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
92 for (i = 0; i < 4; i++) {
93 if ((ret = image_get_linesize(width, i, max_step[i], max_step_comp[i], desc)) < 0)
101 int av_image_fill_pointers(uint8_t *data[4], enum PixelFormat 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_descriptors[pix_fmt];
107 memset(data , 0, sizeof(data[0])*4);
109 if ((unsigned)pix_fmt >= PIX_FMT_NB || desc->flags & PIX_FMT_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 & PIX_FMT_PAL ||
118 desc->flags & PIX_FMT_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 ff_set_systematic_pal2(uint32_t pal[256], enum PixelFormat pix_fmt)
147 for (i = 0; i < 256; i++) {
161 case PIX_FMT_RGB4_BYTE:
166 case PIX_FMT_BGR4_BYTE:
175 return AVERROR(EINVAL);
177 pal[i] = b + (g<<8) + (r<<16) + (0xFF<<24);
183 int av_image_alloc(uint8_t *pointers[4], int linesizes[4],
184 int w, int h, enum PixelFormat pix_fmt, int align)
189 if ((ret = av_image_check_size(w, h, 0, NULL)) < 0)
191 if ((ret = av_image_fill_linesizes(linesizes, pix_fmt, align>7 ? FFALIGN(w, 8) : w)) < 0)
194 for (i = 0; i < 4; i++)
195 linesizes[i] = FFALIGN(linesizes[i], align);
197 if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, NULL, linesizes)) < 0)
199 buf = av_malloc(ret + align);
201 return AVERROR(ENOMEM);
202 if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, buf, linesizes)) < 0) {
206 if (av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_PAL ||
207 av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_PSEUDOPAL)
208 ff_set_systematic_pal2((uint32_t*)pointers[1], pix_fmt);
213 typedef struct ImgUtils {
214 const AVClass *class;
219 static const AVClass imgutils_class = { "IMGUTILS", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT, offsetof(ImgUtils, log_offset), offsetof(ImgUtils, log_ctx) };
221 int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
223 ImgUtils imgutils = { &imgutils_class, log_offset, log_ctx };
225 if ((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/8)
228 av_log(&imgutils, AV_LOG_ERROR, "Picture size %ux%u is invalid\n", w, h);
229 return AVERROR(EINVAL);
232 void av_image_copy_plane(uint8_t *dst, int dst_linesize,
233 const uint8_t *src, int src_linesize,
234 int bytewidth, int height)
238 for (;height > 0; height--) {
239 memcpy(dst, src, bytewidth);
245 void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4],
246 const uint8_t *src_data[4], const int src_linesizes[4],
247 enum PixelFormat pix_fmt, int width, int height)
249 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
251 if (desc->flags & PIX_FMT_HWACCEL)
254 if (desc->flags & PIX_FMT_PAL ||
255 desc->flags & PIX_FMT_PSEUDOPAL) {
256 av_image_copy_plane(dst_data[0], dst_linesizes[0],
257 src_data[0], src_linesizes[0],
259 /* copy the palette */
260 memcpy(dst_data[1], src_data[1], 4*256);
262 int i, planes_nb = 0;
264 for (i = 0; i < desc->nb_components; i++)
265 planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1);
267 for (i = 0; i < planes_nb; i++) {
269 int bwidth = av_image_get_linesize(pix_fmt, width, i);
270 if (i == 1 || i == 2) {
271 h= -((-height)>>desc->log2_chroma_h);
273 av_image_copy_plane(dst_data[i], dst_linesizes[i],
274 src_data[i], src_linesizes[i],