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",
97 case 108: // windib v4
98 case 124: // windib v5
99 width = bytestream_get_le32(&buf);
100 height = bytestream_get_le32(&buf);
103 width = bytestream_get_le16(&buf);
104 height = bytestream_get_le16(&buf);
107 av_log(avctx, AV_LOG_ERROR, "unsupported BMP file, patch welcome\n");
111 if(bytestream_get_le16(&buf) != 1){ /* planes */
112 av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n");
116 depth = bytestream_get_le16(&buf);
119 comp = bytestream_get_le32(&buf);
123 if(comp != BMP_RGB && comp != BMP_BITFIELDS && comp != BMP_RLE4 && comp != BMP_RLE8){
124 av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp);
128 if(comp == BMP_BITFIELDS){
130 rgb[0] = bytestream_get_le32(&buf);
131 rgb[1] = bytestream_get_le32(&buf);
132 rgb[2] = bytestream_get_le32(&buf);
135 avctx->width = width;
136 avctx->height = height > 0? height: -height;
138 avctx->pix_fmt = PIX_FMT_NONE;
142 if(comp == BMP_BITFIELDS){
143 rgb[0] = (rgb[0] >> 15) & 3;
144 rgb[1] = (rgb[1] >> 15) & 3;
145 rgb[2] = (rgb[2] >> 15) & 3;
147 if(rgb[0] + rgb[1] + rgb[2] != 3 ||
148 rgb[0] == rgb[1] || rgb[0] == rgb[2] || rgb[1] == rgb[2]){
157 avctx->pix_fmt = PIX_FMT_BGR24;
160 avctx->pix_fmt = PIX_FMT_BGR24;
164 avctx->pix_fmt = PIX_FMT_RGB555;
165 if(comp == BMP_BITFIELDS)
166 avctx->pix_fmt = rgb[1] == 0x07E0 ? PIX_FMT_RGB565 : PIX_FMT_RGB555;
169 if(hsize - ihsize - 14 > 0)
170 avctx->pix_fmt = PIX_FMT_PAL8;
172 avctx->pix_fmt = PIX_FMT_GRAY8;
175 if(hsize - ihsize - 14 > 0){
176 avctx->pix_fmt = PIX_FMT_PAL8;
178 av_log(avctx, AV_LOG_ERROR, "Unknown palette for 16-colour BMP\n");
183 avctx->pix_fmt = PIX_FMT_MONOBLACK;
186 av_log(avctx, AV_LOG_ERROR, "depth %d not supported\n", depth);
190 if(avctx->pix_fmt == PIX_FMT_NONE){
191 av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
196 avctx->release_buffer(avctx, p);
199 if(avctx->get_buffer(avctx, p) < 0){
200 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
203 p->pict_type = AV_PICTURE_TYPE_I;
207 dsize = buf_size - hsize;
209 /* Line size in file multiple of 4 */
210 n = ((avctx->width * depth) / 8 + 3) & ~3;
212 if(n * avctx->height > dsize && comp != BMP_RLE4 && comp != BMP_RLE8){
213 av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
214 dsize, n * avctx->height);
218 // RLE may skip decoding some picture areas, so blank picture before decoding
219 if(comp == BMP_RLE4 || comp == BMP_RLE8)
220 memset(p->data[0], 0, avctx->height * p->linesize[0]);
222 if(depth == 4 || depth == 8)
223 memset(p->data[1], 0, 1024);
226 ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
227 linesize = -p->linesize[0];
230 linesize = p->linesize[0];
233 if(avctx->pix_fmt == PIX_FMT_PAL8){
234 int colors = 1 << depth;
238 t = bytestream_get_le32(&buf);
239 if(t < 0 || t > (1 << depth)){
240 av_log(avctx, AV_LOG_ERROR, "Incorrect number of colors - %X for bitdepth %d\n", t, depth);
245 buf = buf0 + 14 + ihsize; //palette location
246 if((hsize-ihsize-14) < (colors << 2)){ // OS/2 bitmap, 3 bytes per palette entry
247 for(i = 0; i < colors; i++)
248 ((uint32_t*)p->data[1])[i] = (0xff<<24) | bytestream_get_le24(&buf);
250 for(i = 0; i < colors; i++)
251 ((uint32_t*)p->data[1])[i] = bytestream_get_le32(&buf);
255 if(comp == BMP_RLE4 || comp == BMP_RLE8){
257 p->data[0] += p->linesize[0] * (avctx->height - 1);
258 p->linesize[0] = -p->linesize[0];
260 ff_msrle_decode(avctx, (AVPicture*)p, depth, buf, dsize);
262 p->data[0] += p->linesize[0] * (avctx->height - 1);
263 p->linesize[0] = -p->linesize[0];
270 for(i = 0; i < avctx->height; i++){
277 for(i = 0; i < avctx->height; i++){
279 for(j = 0; j < n; j++){
280 ptr[j*2+0] = (buf[j] >> 4) & 0xF;
281 ptr[j*2+1] = buf[j] & 0xF;
288 for(i = 0; i < avctx->height; i++){
289 const uint16_t *src = (const uint16_t *) buf;
290 uint16_t *dst = (uint16_t *) ptr;
292 for(j = 0; j < avctx->width; j++)
293 *dst++ = av_le2ne16(*src++);
300 for(i = 0; i < avctx->height; i++){
301 const uint8_t *src = buf;
304 for(j = 0; j < avctx->width; j++){
305 dst[0] = src[rgb[2]];
306 dst[1] = src[rgb[1]];
307 dst[2] = src[rgb[0]];
317 av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
322 *picture = s->picture;
323 *data_size = sizeof(AVPicture);
328 static av_cold int bmp_decode_end(AVCodecContext *avctx)
330 BMPContext* c = avctx->priv_data;
332 if (c->picture.data[0])
333 avctx->release_buffer(avctx, &c->picture);
338 AVCodec ff_bmp_decoder = {
348 .long_name = NULL_IF_CONFIG_SMALL("BMP image"),