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 = {
223 .class_name = "IMGUTILS",
224 .item_name = av_default_item_name,
226 .version = LIBAVUTIL_VERSION_INT,
227 .log_level_offset_offset = offsetof(ImgUtils, log_offset),
228 .parent_log_context_offset = offsetof(ImgUtils, log_ctx),
231 int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
233 ImgUtils imgutils = { &imgutils_class, log_offset, log_ctx };
235 if ((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/8)
238 av_log(&imgutils, AV_LOG_ERROR, "Picture size %ux%u is invalid\n", w, h);
239 return AVERROR(EINVAL);
242 int av_image_check_sar(unsigned int w, unsigned int h, AVRational sar)
247 return AVERROR(EINVAL);
249 if (!sar.num || sar.num == sar.den)
252 if (sar.num < sar.den)
253 scaled_dim = av_rescale_rnd(w, sar.num, sar.den, AV_ROUND_ZERO);
255 scaled_dim = av_rescale_rnd(h, sar.den, sar.num, AV_ROUND_ZERO);
260 return AVERROR(EINVAL);
263 static void image_copy_plane(uint8_t *dst, ptrdiff_t dst_linesize,
264 const uint8_t *src, ptrdiff_t src_linesize,
265 ptrdiff_t bytewidth, int height)
269 for (;height > 0; height--) {
270 memcpy(dst, src, bytewidth);
276 static void image_copy_plane_uc_from(uint8_t *dst, ptrdiff_t dst_linesize,
277 const uint8_t *src, ptrdiff_t src_linesize,
278 ptrdiff_t bytewidth, int height)
283 ret = ff_image_copy_plane_uc_from_x86(dst, dst_linesize, src, src_linesize,
288 image_copy_plane(dst, dst_linesize, src, src_linesize, bytewidth, height);
291 void av_image_copy_plane(uint8_t *dst, int dst_linesize,
292 const uint8_t *src, int src_linesize,
293 int bytewidth, int height)
295 image_copy_plane(dst, dst_linesize, src, src_linesize, bytewidth, height);
298 static void image_copy(uint8_t *dst_data[4], const ptrdiff_t dst_linesizes[4],
299 const uint8_t *src_data[4], const ptrdiff_t src_linesizes[4],
300 enum AVPixelFormat pix_fmt, int width, int height,
301 void (*copy_plane)(uint8_t *, ptrdiff_t, const uint8_t *,
302 ptrdiff_t, ptrdiff_t, int))
304 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
306 if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
309 if (desc->flags & AV_PIX_FMT_FLAG_PAL ||
310 desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
311 copy_plane(dst_data[0], dst_linesizes[0],
312 src_data[0], src_linesizes[0],
314 /* copy the palette */
315 memcpy(dst_data[1], src_data[1], 4*256);
317 int i, planes_nb = 0;
319 for (i = 0; i < desc->nb_components; i++)
320 planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1);
322 for (i = 0; i < planes_nb; i++) {
324 ptrdiff_t bwidth = av_image_get_linesize(pix_fmt, width, i);
325 if (i == 1 || i == 2) {
326 h = AV_CEIL_RSHIFT(height, desc->log2_chroma_h);
328 copy_plane(dst_data[i], dst_linesizes[i],
329 src_data[i], src_linesizes[i],
335 void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4],
336 const uint8_t *src_data[4], const int src_linesizes[4],
337 enum AVPixelFormat pix_fmt, int width, int height)
339 ptrdiff_t dst_linesizes1[4], src_linesizes1[4];
342 for (i = 0; i < 4; i++) {
343 dst_linesizes1[i] = dst_linesizes[i];
344 src_linesizes1[i] = src_linesizes[i];
347 image_copy(dst_data, dst_linesizes1, src_data, src_linesizes1, pix_fmt,
348 width, height, image_copy_plane);
351 void av_image_copy_uc_from(uint8_t *dst_data[4], const ptrdiff_t dst_linesizes[4],
352 const uint8_t *src_data[4], const ptrdiff_t src_linesizes[4],
353 enum AVPixelFormat pix_fmt, int width, int height)
355 image_copy(dst_data, dst_linesizes, src_data, src_linesizes, pix_fmt,
356 width, height, image_copy_plane_uc_from);
359 int av_image_fill_arrays(uint8_t *dst_data[4], int dst_linesize[4],
360 const uint8_t *src, enum AVPixelFormat pix_fmt,
361 int width, int height, int align)
365 ret = av_image_check_size(width, height, 0, NULL);
369 ret = av_image_fill_linesizes(dst_linesize, pix_fmt, width);
373 for (i = 0; i < 4; i++)
374 dst_linesize[i] = FFALIGN(dst_linesize[i], align);
376 return av_image_fill_pointers(dst_data, pix_fmt, height, src, dst_linesize);
379 int av_image_get_buffer_size(enum AVPixelFormat pix_fmt,
380 int width, int height, int align)
385 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
389 ret = av_image_check_size(width, height, 0, NULL);
393 // do not include palette for these pseudo-paletted formats
394 if (desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)
395 return width * height;
397 return av_image_fill_arrays(data, linesize, NULL, pix_fmt,
398 width, height, align);
401 int av_image_copy_to_buffer(uint8_t *dst, int dst_size,
402 const uint8_t * const src_data[4],
403 const int src_linesize[4],
404 enum AVPixelFormat pix_fmt,
405 int width, int height, int align)
407 int i, j, nb_planes = 0, linesize[4];
408 int size = av_image_get_buffer_size(pix_fmt, width, height, align);
409 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
411 if (size > dst_size || size < 0 || !desc)
412 return AVERROR(EINVAL);
414 for (i = 0; i < desc->nb_components; i++)
415 nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
419 av_image_fill_linesizes(linesize, pix_fmt, width);
420 for (i = 0; i < nb_planes; i++) {
421 int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
422 const uint8_t *src = src_data[i];
423 h = (height + (1 << shift) - 1) >> shift;
425 for (j = 0; j < h; j++) {
426 memcpy(dst, src, linesize[i]);
427 dst += FFALIGN(linesize[i], align);
428 src += src_linesize[i];
432 if (desc->flags & AV_PIX_FMT_FLAG_PAL)
433 memcpy((unsigned char *)(((size_t)dst + 3) & ~3),
434 src_data[1], 256 * 4);