Mercurial > libavcodec.hg
annotate bmp.c @ 4438:fe3179006730 libavcodec
Remove the getbe16 functions and use the AV_RB16 macro instead. Patch by Ian
Caulfield, ian dot caulfield gmail dot com.
author | takis |
---|---|
date | Tue, 30 Jan 2007 14:24:12 +0000 |
parents | a848b652f0ac |
children | 3af19e140d67 |
rev | line source |
---|---|
2949 | 1 /* |
4415
f792b146869b
Segregate code common to BMP decoder and future encoder
diego
parents:
4394
diff
changeset
|
2 * BMP image format decoder |
2949 | 3 * Copyright (c) 2005 Mans Rullgard |
4 * | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3036
diff
changeset
|
5 * This file is part of FFmpeg. |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3036
diff
changeset
|
6 * |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3036
diff
changeset
|
7 * FFmpeg is free software; you can redistribute it and/or |
2949 | 8 * modify it under the terms of the GNU Lesser General Public |
9 * License as published by the Free Software Foundation; either | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3036
diff
changeset
|
10 * version 2.1 of the License, or (at your option) any later version. |
2949 | 11 * |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3036
diff
changeset
|
12 * FFmpeg is distributed in the hope that it will be useful, |
2949 | 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 | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3036
diff
changeset
|
18 * License along with FFmpeg; if not, write to the Free Software |
3036
0b546eab515d
Update licensing information: The FSF changed postal address.
diego
parents:
2967
diff
changeset
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
2949 | 20 */ |
21 | |
22 #include "avcodec.h" | |
23 #include "bitstream.h" | |
24 #include "bswap.h" | |
4415
f792b146869b
Segregate code common to BMP decoder and future encoder
diego
parents:
4394
diff
changeset
|
25 #include "bmp.h" |
2949 | 26 |
27 #define read16(bits) bswap_16(get_bits(bits, 16)) | |
28 #define read32(bits) bswap_32(get_bits_long(bits, 32)) | |
29 | |
30 static int bmp_decode_init(AVCodecContext *avctx){ | |
31 BMPContext *s = avctx->priv_data; | |
32 | |
33 avcodec_get_frame_defaults((AVFrame*)&s->picture); | |
34 avctx->coded_frame = (AVFrame*)&s->picture; | |
35 | |
36 return 0; | |
37 } | |
38 | |
2967 | 39 static int bmp_decode_frame(AVCodecContext *avctx, |
2949 | 40 void *data, int *data_size, |
41 uint8_t *buf, int buf_size) | |
42 { | |
43 BMPContext *s = avctx->priv_data; | |
44 AVFrame *picture = data; | |
45 AVFrame *p = &s->picture; | |
46 GetBitContext bits; | |
47 unsigned int fsize, hsize; | |
48 int width, height; | |
49 unsigned int depth; | |
4393 | 50 BiCompression comp; |
2949 | 51 unsigned int ihsize; |
52 int i, j, n, linesize; | |
53 uint32_t rgb[3]; | |
54 uint8_t *ptr; | |
55 int dsize; | |
56 | |
57 if(buf_size < 14){ | |
58 av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\n", buf_size); | |
59 return -1; | |
60 } | |
61 | |
62 init_get_bits(&bits, buf, buf_size); | |
63 | |
64 if(get_bits(&bits, 16) != 0x424d){ /* 'BM' */ | |
65 av_log(avctx, AV_LOG_ERROR, "bad magic number\n"); | |
66 return -1; | |
67 } | |
68 | |
69 fsize = read32(&bits); | |
70 if(buf_size < fsize){ | |
71 av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n", | |
72 buf_size, fsize); | |
73 return -1; | |
74 } | |
75 | |
76 skip_bits(&bits, 16); /* reserved1 */ | |
77 skip_bits(&bits, 16); /* reserved2 */ | |
78 | |
79 hsize = read32(&bits); /* header size */ | |
80 if(fsize <= hsize){ | |
81 av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n", | |
82 fsize, hsize); | |
83 return -1; | |
84 } | |
85 | |
86 ihsize = read32(&bits); /* more header size */ | |
87 if(ihsize + 14 > hsize){ | |
88 av_log(avctx, AV_LOG_ERROR, "invalid header size %d\n", hsize); | |
89 return -1; | |
90 } | |
91 | |
92 width = read32(&bits); | |
93 height = read32(&bits); | |
94 | |
95 if(read16(&bits) != 1){ /* planes */ | |
96 av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n"); | |
97 return -1; | |
98 } | |
99 | |
100 depth = read16(&bits); | |
101 | |
102 if(ihsize > 16) | |
103 comp = read32(&bits); | |
104 else | |
105 comp = BMP_RGB; | |
106 | |
107 if(comp != BMP_RGB && comp != BMP_BITFIELDS){ | |
108 av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp); | |
109 return -1; | |
110 } | |
111 | |
112 if(comp == BMP_BITFIELDS){ | |
113 skip_bits(&bits, 20 * 8); | |
114 rgb[0] = read32(&bits); | |
115 rgb[1] = read32(&bits); | |
116 rgb[2] = read32(&bits); | |
117 } | |
118 | |
119 avctx->codec_id = CODEC_ID_BMP; | |
120 avctx->width = width; | |
121 avctx->height = height > 0? height: -height; | |
122 | |
123 avctx->pix_fmt = PIX_FMT_NONE; | |
124 | |
125 switch(depth){ | |
126 case 32: | |
127 if(comp == BMP_BITFIELDS){ | |
128 rgb[0] = (rgb[0] >> 15) & 3; | |
129 rgb[1] = (rgb[1] >> 15) & 3; | |
130 rgb[2] = (rgb[2] >> 15) & 3; | |
131 | |
132 if(rgb[0] + rgb[1] + rgb[2] != 3 || | |
133 rgb[0] == rgb[1] || rgb[0] == rgb[2] || rgb[1] == rgb[2]){ | |
134 break; | |
135 } | |
136 } else { | |
137 rgb[0] = 2; | |
138 rgb[1] = 1; | |
139 rgb[2] = 0; | |
140 } | |
141 | |
142 avctx->pix_fmt = PIX_FMT_BGR24; | |
143 break; | |
144 case 24: | |
145 avctx->pix_fmt = PIX_FMT_BGR24; | |
146 break; | |
147 case 16: | |
148 if(comp == BMP_RGB) | |
149 avctx->pix_fmt = PIX_FMT_RGB555; | |
150 break; | |
151 default: | |
152 av_log(avctx, AV_LOG_ERROR, "depth %d not supported\n", depth); | |
153 return -1; | |
154 } | |
155 | |
156 if(avctx->pix_fmt == PIX_FMT_NONE){ | |
157 av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n"); | |
158 return -1; | |
159 } | |
160 | |
4432
a848b652f0ac
Fix segfault in bmp decoder. Patch by Michel Bardiaux mbardiaux mediaxim dot be.
takis
parents:
4415
diff
changeset
|
161 if(p->data[0]) |
a848b652f0ac
Fix segfault in bmp decoder. Patch by Michel Bardiaux mbardiaux mediaxim dot be.
takis
parents:
4415
diff
changeset
|
162 avctx->release_buffer(avctx, p); |
a848b652f0ac
Fix segfault in bmp decoder. Patch by Michel Bardiaux mbardiaux mediaxim dot be.
takis
parents:
4415
diff
changeset
|
163 |
2949 | 164 p->reference = 0; |
165 if(avctx->get_buffer(avctx, p) < 0){ | |
166 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); | |
167 return -1; | |
168 } | |
169 p->pict_type = FF_I_TYPE; | |
170 p->key_frame = 1; | |
171 | |
172 buf += hsize; | |
173 dsize = buf_size - hsize; | |
174 | |
4109 | 175 /* Line size in file multiple of 4 */ |
176 n = (avctx->width * (depth / 8) + 3) & ~3; | |
2949 | 177 |
178 if(n * avctx->height > dsize){ | |
179 av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n", | |
180 dsize, n * avctx->height); | |
181 return -1; | |
182 } | |
183 | |
184 if(height > 0){ | |
185 ptr = p->data[0] + (avctx->height - 1) * p->linesize[0]; | |
186 linesize = -p->linesize[0]; | |
187 } else { | |
188 ptr = p->data[0]; | |
189 linesize = p->linesize[0]; | |
190 } | |
191 | |
192 switch(depth){ | |
193 case 24: | |
194 for(i = 0; i < avctx->height; i++){ | |
195 memcpy(ptr, buf, n); | |
196 buf += n; | |
197 ptr += linesize; | |
198 } | |
199 break; | |
200 case 16: | |
201 for(i = 0; i < avctx->height; i++){ | |
202 uint16_t *src = (uint16_t *) buf; | |
203 uint16_t *dst = (uint16_t *) ptr; | |
204 | |
205 for(j = 0; j < avctx->width; j++) | |
206 *dst++ = le2me_16(*src++); | |
207 | |
208 buf += n; | |
209 ptr += linesize; | |
210 } | |
211 break; | |
212 case 32: | |
213 for(i = 0; i < avctx->height; i++){ | |
214 uint8_t *src = buf; | |
215 uint8_t *dst = ptr; | |
216 | |
217 for(j = 0; j < avctx->width; j++){ | |
218 dst[0] = src[rgb[2]]; | |
219 dst[1] = src[rgb[1]]; | |
220 dst[2] = src[rgb[0]]; | |
221 dst += 3; | |
222 src += 4; | |
223 } | |
224 | |
225 buf += n; | |
226 ptr += linesize; | |
227 } | |
228 break; | |
229 default: | |
230 av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n"); | |
231 return -1; | |
232 } | |
233 | |
234 *picture = s->picture; | |
235 *data_size = sizeof(AVPicture); | |
236 | |
237 return buf_size; | |
238 } | |
239 | |
240 AVCodec bmp_decoder = { | |
241 "bmp", | |
242 CODEC_TYPE_VIDEO, | |
243 CODEC_ID_BMP, | |
244 sizeof(BMPContext), | |
245 bmp_decode_init, | |
246 NULL, | |
247 NULL, | |
248 bmp_decode_frame | |
249 }; |