e7e016cd5a0f7d3d84a7aa028f8070742919cb46
[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_plane_step [4];
32 int max_plane_step_comp[4];
33
34 memset(linesize, 0, 4*sizeof(linesize[0]));
35
36 if (desc->flags & PIX_FMT_HWACCEL)
37 return -1;
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_plane_step , 0, sizeof(max_plane_step ));
45 memset(max_plane_step_comp, 0, sizeof(max_plane_step_comp));
46 for (i = 0; i < 4; i++) {
47 const AVComponentDescriptor *comp = &(desc->comp[i]);
48 if ((comp->step_minus1+1) > max_plane_step[comp->plane]) {
49 max_plane_step [comp->plane] = comp->step_minus1+1;
50 max_plane_step_comp[comp->plane] = i;
51 }
52 }
53
54 for (i = 0; i < 4; i++) {
55 int s = (max_plane_step_comp[i] == 1 || max_plane_step_comp[i] == 2) ? desc->log2_chroma_w : 0;
56 linesize[i] = max_plane_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 linesize[4])
64 {
65 int size, h2, size2;
66 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
67
68 size = linesize[0] * height;
69 switch(pix_fmt) {
70 case PIX_FMT_YUV420P:
71 case PIX_FMT_YUV422P:
72 case PIX_FMT_YUV444P:
73 case PIX_FMT_YUV410P:
74 case PIX_FMT_YUV411P:
75 case PIX_FMT_YUV440P:
76 case PIX_FMT_YUVJ420P:
77 case PIX_FMT_YUVJ422P:
78 case PIX_FMT_YUVJ444P:
79 case PIX_FMT_YUVJ440P:
80 case PIX_FMT_YUV420P16LE:
81 case PIX_FMT_YUV422P16LE:
82 case PIX_FMT_YUV444P16LE:
83 case PIX_FMT_YUV420P16BE:
84 case PIX_FMT_YUV422P16BE:
85 case PIX_FMT_YUV444P16BE:
86 h2 = (height + (1 << desc->log2_chroma_h) - 1) >> desc->log2_chroma_h;
87 size2 = linesize[1] * h2;
88 data[0] = ptr;
89 data[1] = data[0] + size;
90 data[2] = data[1] + size2;
91 data[3] = NULL;
92 return size + 2 * size2;
93 case PIX_FMT_YUVA420P:
94 h2 = (height + (1 << desc->log2_chroma_h) - 1) >> desc->log2_chroma_h;
95 size2 = linesize[1] * h2;
96 data[0] = ptr;
97 data[1] = data[0] + size;
98 data[2] = data[1] + size2;
99 data[3] = data[1] + size2 + size2;
100 return 2 * size + 2 * size2;
101 case PIX_FMT_NV12:
102 case PIX_FMT_NV21:
103 h2 = (height + (1 << desc->log2_chroma_h) - 1) >> desc->log2_chroma_h;
104 size2 = linesize[1] * h2;
105 data[0] = ptr;
106 data[1] = data[0] + size;
107 data[2] = NULL;
108 data[3] = NULL;
109 return size + size2;
110 case PIX_FMT_RGB24:
111 case PIX_FMT_BGR24:
112 case PIX_FMT_ARGB:
113 case PIX_FMT_ABGR:
114 case PIX_FMT_RGBA:
115 case PIX_FMT_BGRA:
116 case PIX_FMT_RGB48BE:
117 case PIX_FMT_RGB48LE:
118 case PIX_FMT_GRAY16BE:
119 case PIX_FMT_GRAY16LE:
120 case PIX_FMT_BGR444BE:
121 case PIX_FMT_BGR444LE:
122 case PIX_FMT_BGR555BE:
123 case PIX_FMT_BGR555LE:
124 case PIX_FMT_BGR565BE:
125 case PIX_FMT_BGR565LE:
126 case PIX_FMT_RGB444BE:
127 case PIX_FMT_RGB444LE:
128 case PIX_FMT_RGB555BE:
129 case PIX_FMT_RGB555LE:
130 case PIX_FMT_RGB565BE:
131 case PIX_FMT_RGB565LE:
132 case PIX_FMT_YUYV422:
133 case PIX_FMT_UYVY422:
134 case PIX_FMT_UYYVYY411:
135 case PIX_FMT_RGB4:
136 case PIX_FMT_BGR4:
137 case PIX_FMT_MONOWHITE:
138 case PIX_FMT_MONOBLACK:
139 case PIX_FMT_Y400A:
140 data[0] = ptr;
141 data[1] = NULL;
142 data[2] = NULL;
143 data[3] = NULL;
144 return size;
145 case PIX_FMT_PAL8:
146 case PIX_FMT_RGB8:
147 case PIX_FMT_BGR8:
148 case PIX_FMT_RGB4_BYTE:
149 case PIX_FMT_BGR4_BYTE:
150 case PIX_FMT_GRAY8:
151 size2 = (size + 3) & ~3;
152 data[0] = ptr;
153 data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
154 data[2] = NULL;
155 data[3] = NULL;
156 return size2 + 256 * 4;
157 default:
158 data[0] = NULL;
159 data[1] = NULL;
160 data[2] = NULL;
161 data[3] = NULL;
162 return -1;
163 }
164 }