2 * BMP image format decoder
3 * Copyright (c) 2005 Mans Rullgard
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "bytestream.h"
28 static int bmp_decode_frame(AVCodecContext *avctx,
29 void *data, int *got_frame,
32 const uint8_t *buf = avpkt->data;
33 int buf_size = avpkt->size;
35 unsigned int fsize, hsize;
40 int i, j, n, linesize, ret;
44 const uint8_t *buf0 = buf;
48 av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\n", buf_size);
49 return AVERROR_INVALIDDATA;
52 if (bytestream_get_byte(&buf) != 'B' ||
53 bytestream_get_byte(&buf) != 'M') {
54 av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
55 return AVERROR_INVALIDDATA;
58 fsize = bytestream_get_le32(&buf);
59 if (buf_size < fsize) {
60 av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d), trying to decode anyway\n",
65 buf += 2; /* reserved1 */
66 buf += 2; /* reserved2 */
68 hsize = bytestream_get_le32(&buf); /* header size */
69 ihsize = bytestream_get_le32(&buf); /* more header size */
70 if (ihsize + 14 > hsize) {
71 av_log(avctx, AV_LOG_ERROR, "invalid header size %d\n", hsize);
72 return AVERROR_INVALIDDATA;
75 /* sometimes file size is set to some headers size, set a real size in that case */
76 if (fsize == 14 || fsize == ihsize + 14)
80 av_log(avctx, AV_LOG_ERROR, "declared file size is less than header size (%d < %d)\n",
82 return AVERROR_INVALIDDATA;
88 case 108: // windib v4
89 case 124: // windib v5
90 width = bytestream_get_le32(&buf);
91 height = bytestream_get_le32(&buf);
94 width = bytestream_get_le16(&buf);
95 height = bytestream_get_le16(&buf);
98 av_log(avctx, AV_LOG_ERROR, "unsupported BMP file, patch welcome\n");
99 return AVERROR_PATCHWELCOME;
103 if (bytestream_get_le16(&buf) != 1) {
104 av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n");
105 return AVERROR_INVALIDDATA;
108 depth = bytestream_get_le16(&buf);
111 comp = bytestream_get_le32(&buf);
115 if (comp != BMP_RGB && comp != BMP_BITFIELDS && comp != BMP_RLE4 &&
117 av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp);
118 return AVERROR_INVALIDDATA;
121 if (comp == BMP_BITFIELDS) {
123 rgb[0] = bytestream_get_le32(&buf);
124 rgb[1] = bytestream_get_le32(&buf);
125 rgb[2] = bytestream_get_le32(&buf);
128 avctx->width = width;
129 avctx->height = height > 0 ? height : -height;
131 avctx->pix_fmt = AV_PIX_FMT_NONE;
135 if (comp == BMP_BITFIELDS) {
136 rgb[0] = (rgb[0] >> 15) & 3;
137 rgb[1] = (rgb[1] >> 15) & 3;
138 rgb[2] = (rgb[2] >> 15) & 3;
140 if (rgb[0] + rgb[1] + rgb[2] != 3 ||
141 rgb[0] == rgb[1] || rgb[0] == rgb[2] || rgb[1] == rgb[2]) {
150 avctx->pix_fmt = AV_PIX_FMT_BGR24;
153 avctx->pix_fmt = AV_PIX_FMT_BGR24;
157 avctx->pix_fmt = AV_PIX_FMT_RGB555;
158 else if (comp == BMP_BITFIELDS) {
159 if (rgb[0] == 0xF800 && rgb[1] == 0x07E0 && rgb[2] == 0x001F)
160 avctx->pix_fmt = AV_PIX_FMT_RGB565;
161 else if (rgb[0] == 0x7C00 && rgb[1] == 0x03E0 && rgb[2] == 0x001F)
162 avctx->pix_fmt = AV_PIX_FMT_RGB555;
163 else if (rgb[0] == 0x0F00 && rgb[1] == 0x00F0 && rgb[2] == 0x000F)
164 avctx->pix_fmt = AV_PIX_FMT_RGB444;
166 av_log(avctx, AV_LOG_ERROR, "Unknown bitfields %0X %0X %0X\n", rgb[0], rgb[1], rgb[2]);
167 return AVERROR(EINVAL);
172 if (hsize - ihsize - 14 > 0)
173 avctx->pix_fmt = AV_PIX_FMT_PAL8;
175 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
179 if (hsize - ihsize - 14 > 0) {
180 avctx->pix_fmt = AV_PIX_FMT_PAL8;
182 av_log(avctx, AV_LOG_ERROR, "Unknown palette for %d-colour BMP\n", 1<<depth);
183 return AVERROR_INVALIDDATA;
187 av_log(avctx, AV_LOG_ERROR, "depth %d not supported\n", depth);
188 return AVERROR_INVALIDDATA;
191 if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
192 av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
193 return AVERROR_INVALIDDATA;
196 if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
197 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
200 p->pict_type = AV_PICTURE_TYPE_I;
204 dsize = buf_size - hsize;
206 /* Line size in file multiple of 4 */
207 n = ((avctx->width * depth) / 8 + 3) & ~3;
209 if (n * avctx->height > dsize && comp != BMP_RLE4 && comp != BMP_RLE8) {
210 av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
211 dsize, n * avctx->height);
212 return AVERROR_INVALIDDATA;
215 // RLE may skip decoding some picture areas, so blank picture before decoding
216 if (comp == BMP_RLE4 || comp == BMP_RLE8)
217 memset(p->data[0], 0, avctx->height * p->linesize[0]);
220 ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
221 linesize = -p->linesize[0];
224 linesize = p->linesize[0];
227 if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
228 int colors = 1 << depth;
230 memset(p->data[1], 0, 1024);
235 t = bytestream_get_le32(&buf);
236 if (t < 0 || t > (1 << depth)) {
237 av_log(avctx, AV_LOG_ERROR, "Incorrect number of colors - %X for bitdepth %d\n", t, depth);
242 buf = buf0 + 14 + ihsize; //palette location
243 // OS/2 bitmap, 3 bytes per palette entry
244 if ((hsize-ihsize-14) < (colors << 2)) {
245 for (i = 0; i < colors; i++)
246 ((uint32_t*)p->data[1])[i] = bytestream_get_le24(&buf);
248 for (i = 0; i < colors; i++)
249 ((uint32_t*)p->data[1])[i] = bytestream_get_le32(&buf);
253 if (comp == BMP_RLE4 || comp == BMP_RLE8) {
255 p->data[0] += p->linesize[0] * (avctx->height - 1);
256 p->linesize[0] = -p->linesize[0];
258 bytestream2_init(&gb, buf, dsize);
259 ff_msrle_decode(avctx, (AVPicture*)p, depth, &gb);
261 p->data[0] += p->linesize[0] * (avctx->height - 1);
262 p->linesize[0] = -p->linesize[0];
267 for (i = 0; i < avctx->height; i++) {
269 for (j = 0; j < n; j++) {
270 ptr[j*8+0] = buf[j] >> 7;
271 ptr[j*8+1] = (buf[j] >> 6) & 1;
272 ptr[j*8+2] = (buf[j] >> 5) & 1;
273 ptr[j*8+3] = (buf[j] >> 4) & 1;
274 ptr[j*8+4] = (buf[j] >> 3) & 1;
275 ptr[j*8+5] = (buf[j] >> 2) & 1;
276 ptr[j*8+6] = (buf[j] >> 1) & 1;
277 ptr[j*8+7] = buf[j] & 1;
285 for (i = 0; i < avctx->height; i++) {
292 for (i = 0; i < avctx->height; i++) {
294 for (j = 0; j < n; j++) {
295 ptr[j*2+0] = (buf[j] >> 4) & 0xF;
296 ptr[j*2+1] = buf[j] & 0xF;
303 for (i = 0; i < avctx->height; i++) {
304 const uint16_t *src = (const uint16_t *) buf;
305 uint16_t *dst = (uint16_t *) ptr;
307 for (j = 0; j < avctx->width; j++)
308 *dst++ = av_le2ne16(*src++);
315 for (i = 0; i < avctx->height; i++) {
316 const uint8_t *src = buf;
319 for (j = 0; j < avctx->width; j++) {
320 dst[0] = src[rgb[2]];
321 dst[1] = src[rgb[1]];
322 dst[2] = src[rgb[0]];
332 av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
333 return AVERROR_INVALIDDATA;
342 AVCodec ff_bmp_decoder = {
344 .long_name = NULL_IF_CONFIG_SMALL("BMP (Windows and OS/2 bitmap)"),
345 .type = AVMEDIA_TYPE_VIDEO,
346 .id = AV_CODEC_ID_BMP,
347 .decode = bmp_decode_frame,
348 .capabilities = CODEC_CAP_DR1,