Reimplement av_fill_image_pointers() using the information stored in
[libav.git] / libavcore / imgutils.c
1 /*
2 * This file is part of FFmpeg.
3 *
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.
8 *
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.
13 *
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
17 */
18
19 /**
20 * @file
21 * misc image utilities
22 */
23
24 #include "imgutils.h"
25 #include "libavutil/pixdesc.h"
26
27 int av_fill_image_linesizes(int linesize[4], enum PixelFormat pix_fmt, int width)
28 {
29 int i;
30 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
31 int max_step [4]; /* max pixel step for each plane */
32 int max_step_comp[4]; /* the component for each plane which has the max pixel step */
33
34 memset(linesize, 0, 4*sizeof(linesize[0]));
35
36 if (desc->flags & PIX_FMT_HWACCEL)
37 return AVERROR(EINVAL);
38
39 if (desc->flags & PIX_FMT_BITSTREAM) {
40 linesize[0] = (width * (desc->comp[0].step_minus1+1) + 7) >> 3;
41 return 0;
42 }
43
44 memset(max_step , 0, sizeof(max_step ));
45 memset(max_step_comp, 0, sizeof(max_step_comp));
46 for (i = 0; i < 4; i++) {
47 const AVComponentDescriptor *comp = &(desc->comp[i]);
48 if ((comp->step_minus1+1) > max_step[comp->plane]) {
49 max_step [comp->plane] = comp->step_minus1+1;
50 max_step_comp[comp->plane] = i;
51 }
52 }
53
54 for (i = 0; i < 4; i++) {
55 int s = (max_step_comp[i] == 1 || max_step_comp[i] == 2) ? desc->log2_chroma_w : 0;
56 linesize[i] = max_step[i] * (((width + (1 << s) - 1)) >> s);
57 }
58
59 return 0;
60 }
61
62 int av_fill_image_pointers(uint8_t *data[4], enum PixelFormat pix_fmt, int height,
63 uint8_t *ptr, const int linesizes[4])
64 {
65 int i, total_size, size[4], has_plane[4];
66
67 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
68 memset(data , 0, sizeof(data[0])*4);
69 memset(size , 0, sizeof(size));
70 memset(has_plane, 0, sizeof(has_plane));
71
72 if (desc->flags & PIX_FMT_HWACCEL)
73 return AVERROR(EINVAL);
74
75 data[0] = ptr;
76 size[0] = linesizes[0] * height;
77
78 if (desc->flags & PIX_FMT_PAL) {
79 size[0] = (size[0] + 3) & ~3;
80 data[1] = ptr + size[0]; /* palette is stored here as 256 32 bits words */
81 return size[0] + 256 * 4;
82 }
83
84 for (i = 0; i < 4; i++)
85 has_plane[desc->comp[i].plane] = 1;
86
87 total_size = size[0];
88 for (i = 1; has_plane[i] && i < 4; i++) {
89 int h, s = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
90 data[i] = data[i-1] + size[i-1];
91 h = (height + (1 << s) - 1) >> s;
92 size[i] = h * linesizes[i];
93 total_size += size[i];
94 }
95
96 return total_size;
97 }