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
25 #include "bytestream.h"
30 static int bmp_decode_frame(AVCodecContext *avctx,
31 void *data, int *got_frame,
34 const uint8_t *buf = avpkt->data;
35 int buf_size = avpkt->size;
37 unsigned int fsize, hsize;
42 int i, j, n, linesize, ret;
46 const uint8_t *buf0 = buf;
50 av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\n", buf_size);
51 return AVERROR_INVALIDDATA;
54 if (bytestream_get_byte(&buf) != 'B' ||
55 bytestream_get_byte(&buf) != 'M') {
56 av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
57 return AVERROR_INVALIDDATA;
60 fsize = bytestream_get_le32(&buf);
61 if (buf_size < fsize) {
62 av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %u), trying to decode anyway\n",
67 buf += 2; /* reserved1 */
68 buf += 2; /* reserved2 */
70 hsize = bytestream_get_le32(&buf); /* header size */
71 ihsize = bytestream_get_le32(&buf); /* more header size */
72 if (ihsize + 14 > hsize) {
73 av_log(avctx, AV_LOG_ERROR, "invalid header size %u\n", hsize);
74 return AVERROR_INVALIDDATA;
77 /* sometimes file size is set to some headers size, set a real size in that case */
78 if (fsize == 14 || fsize == ihsize + 14)
82 av_log(avctx, AV_LOG_ERROR,
83 "Declared file size is less than header size (%u < %u)\n",
85 return AVERROR_INVALIDDATA;
91 case 108: // windib v4
92 case 124: // windib v5
93 width = bytestream_get_le32(&buf);
94 height = bytestream_get_le32(&buf);
97 width = bytestream_get_le16(&buf);
98 height = bytestream_get_le16(&buf);
101 avpriv_report_missing_feature(avctx, "Information header size %u",
103 return AVERROR_PATCHWELCOME;
107 if (bytestream_get_le16(&buf) != 1) {
108 av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n");
109 return AVERROR_INVALIDDATA;
112 depth = bytestream_get_le16(&buf);
115 comp = bytestream_get_le32(&buf);
119 if (comp != BMP_RGB && comp != BMP_BITFIELDS && comp != BMP_RLE4 &&
121 av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp);
122 return AVERROR_INVALIDDATA;
125 if (comp == BMP_BITFIELDS) {
127 rgb[0] = bytestream_get_le32(&buf);
128 rgb[1] = bytestream_get_le32(&buf);
129 rgb[2] = bytestream_get_le32(&buf);
132 avctx->width = width;
133 avctx->height = height > 0 ? height : -height;
135 avctx->pix_fmt = AV_PIX_FMT_NONE;
139 if (comp == BMP_BITFIELDS) {
140 rgb[0] = (rgb[0] >> 15) & 3;
141 rgb[1] = (rgb[1] >> 15) & 3;
142 rgb[2] = (rgb[2] >> 15) & 3;
144 if (rgb[0] + rgb[1] + rgb[2] != 3 ||
145 rgb[0] == rgb[1] || rgb[0] == rgb[2] || rgb[1] == rgb[2]) {
154 avctx->pix_fmt = AV_PIX_FMT_BGR24;
157 avctx->pix_fmt = AV_PIX_FMT_BGR24;
161 avctx->pix_fmt = AV_PIX_FMT_RGB555;
162 else if (comp == BMP_BITFIELDS) {
163 if (rgb[0] == 0xF800 && rgb[1] == 0x07E0 && rgb[2] == 0x001F)
164 avctx->pix_fmt = AV_PIX_FMT_RGB565;
165 else if (rgb[0] == 0x7C00 && rgb[1] == 0x03E0 && rgb[2] == 0x001F)
166 avctx->pix_fmt = AV_PIX_FMT_RGB555;
167 else if (rgb[0] == 0x0F00 && rgb[1] == 0x00F0 && rgb[2] == 0x000F)
168 avctx->pix_fmt = AV_PIX_FMT_RGB444;
170 av_log(avctx, AV_LOG_ERROR,
171 "Unknown bitfields %0"PRIX32" %0"PRIX32" %0"PRIX32"\n",
172 rgb[0], rgb[1], rgb[2]);
173 return AVERROR(EINVAL);
178 if (hsize - ihsize - 14 > 0)
179 avctx->pix_fmt = AV_PIX_FMT_PAL8;
181 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
185 if (hsize - ihsize - 14 > 0) {
186 avctx->pix_fmt = AV_PIX_FMT_PAL8;
188 av_log(avctx, AV_LOG_ERROR, "Unknown palette for %u-colour BMP\n",
190 return AVERROR_INVALIDDATA;
194 av_log(avctx, AV_LOG_ERROR, "depth %u not supported\n", depth);
195 return AVERROR_INVALIDDATA;
198 if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
199 av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
200 return AVERROR_INVALIDDATA;
203 if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
204 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
207 p->pict_type = AV_PICTURE_TYPE_I;
211 dsize = buf_size - hsize;
213 /* Line size in file multiple of 4 */
214 n = ((avctx->width * depth) / 8 + 3) & ~3;
216 if (n * avctx->height > dsize && comp != BMP_RLE4 && comp != BMP_RLE8) {
217 av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
218 dsize, n * avctx->height);
219 return AVERROR_INVALIDDATA;
222 // RLE may skip decoding some picture areas, so blank picture before decoding
223 if (comp == BMP_RLE4 || comp == BMP_RLE8)
224 memset(p->data[0], 0, avctx->height * p->linesize[0]);
227 ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
228 linesize = -p->linesize[0];
231 linesize = p->linesize[0];
234 if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
235 int colors = 1 << depth;
237 memset(p->data[1], 0, 1024);
242 t = bytestream_get_le32(&buf);
243 if (t < 0 || t > (1 << depth)) {
244 av_log(avctx, AV_LOG_ERROR,
245 "Incorrect number of colors - %X for bitdepth %u\n",
251 buf = buf0 + 14 + ihsize; //palette location
252 // OS/2 bitmap, 3 bytes per palette entry
253 if ((hsize-ihsize-14) < (colors << 2)) {
254 for (i = 0; i < colors; i++)
255 ((uint32_t*)p->data[1])[i] = bytestream_get_le24(&buf);
257 for (i = 0; i < colors; i++)
258 ((uint32_t*)p->data[1])[i] = bytestream_get_le32(&buf);
262 if (comp == BMP_RLE4 || comp == BMP_RLE8) {
264 p->data[0] += p->linesize[0] * (avctx->height - 1);
265 p->linesize[0] = -p->linesize[0];
267 bytestream2_init(&gb, buf, dsize);
268 ff_msrle_decode(avctx, p, depth, &gb);
270 p->data[0] += p->linesize[0] * (avctx->height - 1);
271 p->linesize[0] = -p->linesize[0];
276 for (i = 0; i < avctx->height; i++) {
278 for (j = 0; j < n; j++) {
279 ptr[j*8+0] = buf[j] >> 7;
280 ptr[j*8+1] = (buf[j] >> 6) & 1;
281 ptr[j*8+2] = (buf[j] >> 5) & 1;
282 ptr[j*8+3] = (buf[j] >> 4) & 1;
283 ptr[j*8+4] = (buf[j] >> 3) & 1;
284 ptr[j*8+5] = (buf[j] >> 2) & 1;
285 ptr[j*8+6] = (buf[j] >> 1) & 1;
286 ptr[j*8+7] = buf[j] & 1;
294 for (i = 0; i < avctx->height; i++) {
301 for (i = 0; i < avctx->height; i++) {
303 for (j = 0; j < n; j++) {
304 ptr[j*2+0] = (buf[j] >> 4) & 0xF;
305 ptr[j*2+1] = buf[j] & 0xF;
312 for (i = 0; i < avctx->height; i++) {
313 const uint16_t *src = (const uint16_t *) buf;
314 uint16_t *dst = (uint16_t *) ptr;
316 for (j = 0; j < avctx->width; j++)
317 *dst++ = av_le2ne16(*src++);
324 for (i = 0; i < avctx->height; i++) {
325 const uint8_t *src = buf;
328 for (j = 0; j < avctx->width; j++) {
329 dst[0] = src[rgb[2]];
330 dst[1] = src[rgb[1]];
331 dst[2] = src[rgb[0]];
341 av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
342 return AVERROR_INVALIDDATA;
351 AVCodec ff_bmp_decoder = {
353 .long_name = NULL_IF_CONFIG_SMALL("BMP (Windows and OS/2 bitmap)"),
354 .type = AVMEDIA_TYPE_VIDEO,
355 .id = AV_CODEC_ID_BMP,
356 .decode = bmp_decode_frame,
357 .capabilities = AV_CODEC_CAP_DR1,