2 * BMP image format decoder
3 * Copyright (c) 2005 Mans Rullgard
5 * This file is part of FFmpeg.
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "bytestream.h"
27 static av_cold int bmp_decode_init(AVCodecContext *avctx){
28 BMPContext *s = avctx->priv_data;
30 avcodec_get_frame_defaults((AVFrame*)&s->picture);
31 avctx->coded_frame = (AVFrame*)&s->picture;
36 static int bmp_decode_frame(AVCodecContext *avctx,
37 void *data, int *data_size,
40 const uint8_t *buf = avpkt->data;
41 int buf_size = avpkt->size;
42 BMPContext *s = avctx->priv_data;
43 AVFrame *picture = data;
44 AVFrame *p = &s->picture;
45 unsigned int fsize, hsize;
50 int i, j, n, linesize;
54 const uint8_t *buf0 = buf;
57 av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\n", buf_size);
61 if(bytestream_get_byte(&buf) != 'B' ||
62 bytestream_get_byte(&buf) != 'M') {
63 av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
67 fsize = bytestream_get_le32(&buf);
69 av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d), trying to decode anyway\n",
74 buf += 2; /* reserved1 */
75 buf += 2; /* reserved2 */
77 hsize = bytestream_get_le32(&buf); /* header size */
78 ihsize = bytestream_get_le32(&buf); /* more header size */
79 if(ihsize + 14 > hsize){
80 av_log(avctx, AV_LOG_ERROR, "invalid header size %d\n", hsize);
84 /* sometimes file size is set to some headers size, set a real size in that case */
85 if(fsize == 14 || fsize == ihsize + 14)
89 av_log(avctx, AV_LOG_ERROR, "declared file size is less than header size (%d < %d)\n",
98 case 108: // windib v4
99 case 124: // windib v5
100 width = bytestream_get_le32(&buf);
101 height = bytestream_get_le32(&buf);
104 width = bytestream_get_le16(&buf);
105 height = bytestream_get_le16(&buf);
108 av_log(avctx, AV_LOG_ERROR, "unsupported BMP file, patch welcome\n");
112 if(bytestream_get_le16(&buf) != 1){ /* planes */
113 av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n");
117 depth = bytestream_get_le16(&buf);
119 if(ihsize == 40 || ihsize == 64 || ihsize == 56)
120 comp = bytestream_get_le32(&buf);
124 if(comp != BMP_RGB && comp != BMP_BITFIELDS && comp != BMP_RLE4 && comp != BMP_RLE8){
125 av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp);
129 if(comp == BMP_BITFIELDS){
131 rgb[0] = bytestream_get_le32(&buf);
132 rgb[1] = bytestream_get_le32(&buf);
133 rgb[2] = bytestream_get_le32(&buf);
136 avctx->width = width;
137 avctx->height = height > 0? height: -height;
139 avctx->pix_fmt = PIX_FMT_NONE;
143 if(comp == BMP_BITFIELDS){
144 rgb[0] = (rgb[0] >> 15) & 3;
145 rgb[1] = (rgb[1] >> 15) & 3;
146 rgb[2] = (rgb[2] >> 15) & 3;
148 if(rgb[0] + rgb[1] + rgb[2] != 3 ||
149 rgb[0] == rgb[1] || rgb[0] == rgb[2] || rgb[1] == rgb[2]){
158 avctx->pix_fmt = PIX_FMT_BGRA;
161 avctx->pix_fmt = PIX_FMT_BGR24;
165 avctx->pix_fmt = PIX_FMT_RGB555;
166 else if (comp == BMP_BITFIELDS) {
167 if (rgb[0] == 0xF800 && rgb[1] == 0x07E0 && rgb[2] == 0x001F)
168 avctx->pix_fmt = PIX_FMT_RGB565;
169 else if (rgb[0] == 0x7C00 && rgb[1] == 0x03E0 && rgb[2] == 0x001F)
170 avctx->pix_fmt = PIX_FMT_RGB555;
171 else if (rgb[0] == 0x0F00 && rgb[1] == 0x00F0 && rgb[2] == 0x000F)
172 avctx->pix_fmt = PIX_FMT_RGB444;
174 av_log(avctx, AV_LOG_ERROR, "Unknown bitfields %0X %0X %0X\n", rgb[0], rgb[1], rgb[2]);
175 return AVERROR(EINVAL);
180 if(hsize - ihsize - 14 > 0)
181 avctx->pix_fmt = PIX_FMT_PAL8;
183 avctx->pix_fmt = PIX_FMT_GRAY8;
187 if(hsize - ihsize - 14 > 0){
188 avctx->pix_fmt = PIX_FMT_PAL8;
190 av_log(avctx, AV_LOG_ERROR, "Unknown palette for %d-colour BMP\n", 1<<depth);
195 av_log(avctx, AV_LOG_ERROR, "depth %d not supported\n", depth);
199 if(avctx->pix_fmt == PIX_FMT_NONE){
200 av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
205 avctx->release_buffer(avctx, p);
208 if(avctx->get_buffer(avctx, p) < 0){
209 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
212 p->pict_type = AV_PICTURE_TYPE_I;
216 dsize = buf_size - hsize;
218 /* Line size in file multiple of 4 */
219 n = ((avctx->width * depth + 31) / 8) & ~3;
221 if(n * avctx->height > dsize && comp != BMP_RLE4 && comp != BMP_RLE8){
222 av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
223 dsize, n * avctx->height);
227 // RLE may skip decoding some picture areas, so blank picture before decoding
228 if(comp == BMP_RLE4 || comp == BMP_RLE8)
229 memset(p->data[0], 0, avctx->height * p->linesize[0]);
231 if(depth == 4 || depth == 8)
232 memset(p->data[1], 0, 1024);
235 ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
236 linesize = -p->linesize[0];
239 linesize = p->linesize[0];
242 if(avctx->pix_fmt == PIX_FMT_PAL8){
243 int colors = 1 << depth;
247 t = bytestream_get_le32(&buf);
248 if(t < 0 || t > (1 << depth)){
249 av_log(avctx, AV_LOG_ERROR, "Incorrect number of colors - %X for bitdepth %d\n", t, depth);
254 buf = buf0 + 14 + ihsize; //palette location
255 if((hsize-ihsize-14) < (colors << 2)){ // OS/2 bitmap, 3 bytes per palette entry
256 for(i = 0; i < colors; i++)
257 ((uint32_t*)p->data[1])[i] = (0xff<<24) | bytestream_get_le24(&buf);
259 for(i = 0; i < colors; i++)
260 ((uint32_t*)p->data[1])[i] = 0xFFU << 24 | bytestream_get_le32(&buf);
264 if(comp == BMP_RLE4 || comp == BMP_RLE8){
266 p->data[0] += p->linesize[0] * (avctx->height - 1);
267 p->linesize[0] = -p->linesize[0];
269 ff_msrle_decode(avctx, (AVPicture*)p, depth, buf, dsize);
271 p->data[0] += p->linesize[0] * (avctx->height - 1);
272 p->linesize[0] = -p->linesize[0];
277 for (i = 0; i < avctx->height; i++) {
279 for (j = 0; j < n; j++) {
280 ptr[j*8+0] = buf[j] >> 7;
281 ptr[j*8+1] = (buf[j] >> 6) & 1;
282 ptr[j*8+2] = (buf[j] >> 5) & 1;
283 ptr[j*8+3] = (buf[j] >> 4) & 1;
284 ptr[j*8+4] = (buf[j] >> 3) & 1;
285 ptr[j*8+5] = (buf[j] >> 2) & 1;
286 ptr[j*8+6] = (buf[j] >> 1) & 1;
287 ptr[j*8+7] = buf[j] & 1;
295 for(i = 0; i < avctx->height; i++){
302 for(i = 0; i < avctx->height; i++){
304 for(j = 0; j < n; j++){
305 ptr[j*2+0] = (buf[j] >> 4) & 0xF;
306 ptr[j*2+1] = buf[j] & 0xF;
313 for(i = 0; i < avctx->height; i++){
314 const uint16_t *src = (const uint16_t *) buf;
315 uint16_t *dst = (uint16_t *) ptr;
317 for(j = 0; j < avctx->width; j++)
318 *dst++ = av_le2ne16(*src++);
325 for(i = 0; i < avctx->height; i++){
326 const uint8_t *src = buf;
329 for(j = 0; j < avctx->width; j++){
330 dst[0] = src[rgb[2]];
331 dst[1] = src[rgb[1]];
332 dst[2] = src[rgb[0]];
333 /* The Microsoft documentation states:
334 * "The high byte in each DWORD is not used."
335 * Both GIMP and ImageMagick store the alpha transparency value
336 * in the high byte for 32bit bmp files.
348 av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
353 *picture = s->picture;
354 *data_size = sizeof(AVPicture);
359 static av_cold int bmp_decode_end(AVCodecContext *avctx)
361 BMPContext* c = avctx->priv_data;
363 if (c->picture.data[0])
364 avctx->release_buffer(avctx, &c->picture);
369 AVCodec ff_bmp_decoder = {
371 .type = AVMEDIA_TYPE_VIDEO,
373 .priv_data_size = sizeof(BMPContext),
374 .init = bmp_decode_init,
375 .close = bmp_decode_end,
376 .decode = bmp_decode_frame,
377 .capabilities = CODEC_CAP_DR1,
378 .long_name = NULL_IF_CONFIG_SMALL("BMP image"),