]> git.sesse.net Git - ffmpeg/blob - libavcodec/avpicture.c
mpegvideo_enc: fix indentation in load_input_picture()
[ffmpeg] / libavcodec / avpicture.c
1 /*
2  * AVPicture management routines
3  * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
4  *
5  * This file is part of Libav.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 /**
23  * @file
24  * AVPicture management routines
25  */
26
27 #include "avcodec.h"
28 #include "internal.h"
29 #include "libavutil/common.h"
30 #include "libavutil/pixdesc.h"
31 #include "libavutil/imgutils.h"
32 #include "libavutil/colorspace.h"
33
34 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
35                    enum AVPixelFormat pix_fmt, int width, int height)
36 {
37     int ret;
38
39     if ((ret = av_image_check_size(width, height, 0, NULL)) < 0)
40         return ret;
41
42     if ((ret = av_image_fill_linesizes(picture->linesize, pix_fmt, width)) < 0)
43         return ret;
44
45     return av_image_fill_pointers(picture->data, pix_fmt,
46                                   height, ptr, picture->linesize);
47 }
48
49 int avpicture_layout(const AVPicture* src, enum AVPixelFormat pix_fmt,
50                      int width, int height,
51                      unsigned char *dest, int dest_size)
52 {
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);
56
57     if (size > dest_size || size < 0)
58         return AVERROR(EINVAL);
59
60     for (i = 0; i < desc->nb_components; i++)
61         nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
62
63     nb_planes++;
64
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;
70
71         for (j = 0; j < h; j++) {
72             memcpy(dest, s, linesizes[i]);
73             dest += linesizes[i];
74             s += src->linesize[i];
75         }
76     }
77
78     if (desc->flags & PIX_FMT_PAL)
79         memcpy((unsigned char *)(((size_t)dest + 3) & ~3),
80                src->data[1], 256 * 4);
81
82     return size;
83 }
84
85 int avpicture_get_size(enum AVPixelFormat pix_fmt, int width, int height)
86 {
87     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
88     AVPicture dummy_pict;
89     int ret;
90
91     if (!desc)
92         return AVERROR(EINVAL);
93     if ((ret = av_image_check_size(width, height, 0, NULL)) < 0)
94         return ret;
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);
99 }
100
101 int avpicture_alloc(AVPicture *picture,
102                     enum AVPixelFormat pix_fmt, int width, int height)
103 {
104     int ret = av_image_alloc(picture->data, picture->linesize,
105                              width, height, pix_fmt, 1);
106     if (ret < 0) {
107         memset(picture, 0, sizeof(AVPicture));
108         return ret;
109     }
110
111     return 0;
112 }
113
114 void avpicture_free(AVPicture *picture)
115 {
116     av_free(picture->data[0]);
117 }
118
119 void av_picture_copy(AVPicture *dst, const AVPicture *src,
120                      enum AVPixelFormat pix_fmt, int width, int height)
121 {
122     av_image_copy(dst->data, dst->linesize, src->data,
123                   src->linesize, pix_fmt, width, height);
124 }
125