2 * AVPicture management routines
3 * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * AVPicture management routines
29 #include "libavutil/common.h"
30 #include "libavutil/pixdesc.h"
31 #include "libavutil/imgutils.h"
32 #include "libavutil/colorspace.h"
34 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
35 enum AVPixelFormat pix_fmt, int width, int height)
39 if ((ret = av_image_check_size(width, height, 0, NULL)) < 0)
42 if ((ret = av_image_fill_linesizes(picture->linesize, pix_fmt, width)) < 0)
45 return av_image_fill_pointers(picture->data, pix_fmt,
46 height, ptr, picture->linesize);
49 int avpicture_layout(const AVPicture* src, enum AVPixelFormat pix_fmt,
50 int width, int height,
51 unsigned char *dest, int dest_size)
53 int i, j, nb_planes = 0, linesizes[4];
54 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
55 int size = avpicture_get_size(pix_fmt, width, height);
57 if (size > dest_size || size < 0)
58 return AVERROR(EINVAL);
60 for (i = 0; i < desc->nb_components; i++)
61 nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
65 av_image_fill_linesizes(linesizes, pix_fmt, width);
66 for (i = 0; i < nb_planes; i++) {
67 int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
68 const unsigned char *s = src->data[i];
69 h = (height + (1 << shift) - 1) >> shift;
71 for (j = 0; j < h; j++) {
72 memcpy(dest, s, linesizes[i]);
74 s += src->linesize[i];
78 if (desc->flags & PIX_FMT_PAL)
79 memcpy((unsigned char *)(((size_t)dest + 3) & ~3),
80 src->data[1], 256 * 4);
85 int avpicture_get_size(enum AVPixelFormat pix_fmt, int width, int height)
87 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
92 return AVERROR(EINVAL);
93 if ((ret = av_image_check_size(width, height, 0, NULL)) < 0)
95 if (desc->flags & PIX_FMT_PSEUDOPAL)
96 // do not include palette for these pseudo-paletted formats
97 return width * height;
98 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
101 int avpicture_alloc(AVPicture *picture,
102 enum AVPixelFormat pix_fmt, int width, int height)
104 int ret = av_image_alloc(picture->data, picture->linesize,
105 width, height, pix_fmt, 1);
107 memset(picture, 0, sizeof(AVPicture));
114 void avpicture_free(AVPicture *picture)
116 av_free(picture->data[0]);
119 void av_picture_copy(AVPicture *dst, const AVPicture *src,
120 enum AVPixelFormat pix_fmt, int width, int height)
122 av_image_copy(dst->data, dst->linesize, src->data,
123 src->linesize, pix_fmt, width, height);