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 av_cold int bmp_decode_init(AVCodecContext *avctx)
30 BMPContext *s = avctx->priv_data;
32 avcodec_get_frame_defaults(&s->picture);
33 avctx->coded_frame = &s->picture;
38 static int bmp_decode_frame(AVCodecContext *avctx,
39 void *data, int *got_frame,
42 const uint8_t *buf = avpkt->data;
43 int buf_size = avpkt->size;
44 BMPContext *s = avctx->priv_data;
45 AVFrame *picture = data;
46 AVFrame *p = &s->picture;
47 unsigned int fsize, hsize;
52 int i, j, n, linesize, ret;
56 const uint8_t *buf0 = buf;
60 av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\n", buf_size);
61 return AVERROR_INVALIDDATA;
64 if (bytestream_get_byte(&buf) != 'B' ||
65 bytestream_get_byte(&buf) != 'M') {
66 av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
67 return AVERROR_INVALIDDATA;
70 fsize = bytestream_get_le32(&buf);
71 if (buf_size < fsize) {
72 av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d), trying to decode anyway\n",
77 buf += 2; /* reserved1 */
78 buf += 2; /* reserved2 */
80 hsize = bytestream_get_le32(&buf); /* header size */
81 ihsize = bytestream_get_le32(&buf); /* more header size */
82 if (ihsize + 14 > hsize) {
83 av_log(avctx, AV_LOG_ERROR, "invalid header size %d\n", hsize);
84 return AVERROR_INVALIDDATA;
87 /* sometimes file size is set to some headers size, set a real size in that case */
88 if (fsize == 14 || fsize == ihsize + 14)
92 av_log(avctx, AV_LOG_ERROR, "declared file size is less than header size (%d < %d)\n",
94 return AVERROR_INVALIDDATA;
100 case 108: // windib v4
101 case 124: // windib v5
102 width = bytestream_get_le32(&buf);
103 height = bytestream_get_le32(&buf);
106 width = bytestream_get_le16(&buf);
107 height = bytestream_get_le16(&buf);
110 av_log(avctx, AV_LOG_ERROR, "unsupported BMP file, patch welcome\n");
111 return AVERROR_PATCHWELCOME;
115 if (bytestream_get_le16(&buf) != 1) {
116 av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n");
117 return AVERROR_INVALIDDATA;
120 depth = bytestream_get_le16(&buf);
123 comp = bytestream_get_le32(&buf);
127 if (comp != BMP_RGB && comp != BMP_BITFIELDS && comp != BMP_RLE4 &&
129 av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp);
130 return AVERROR_INVALIDDATA;
133 if (comp == BMP_BITFIELDS) {
135 rgb[0] = bytestream_get_le32(&buf);
136 rgb[1] = bytestream_get_le32(&buf);
137 rgb[2] = bytestream_get_le32(&buf);
140 avctx->width = width;
141 avctx->height = height > 0 ? height : -height;
143 avctx->pix_fmt = AV_PIX_FMT_NONE;
147 if (comp == BMP_BITFIELDS) {
148 rgb[0] = (rgb[0] >> 15) & 3;
149 rgb[1] = (rgb[1] >> 15) & 3;
150 rgb[2] = (rgb[2] >> 15) & 3;
152 if (rgb[0] + rgb[1] + rgb[2] != 3 ||
153 rgb[0] == rgb[1] || rgb[0] == rgb[2] || rgb[1] == rgb[2]) {
162 avctx->pix_fmt = AV_PIX_FMT_BGR24;
165 avctx->pix_fmt = AV_PIX_FMT_BGR24;
169 avctx->pix_fmt = AV_PIX_FMT_RGB555;
170 else if (comp == BMP_BITFIELDS) {
171 if (rgb[0] == 0xF800 && rgb[1] == 0x07E0 && rgb[2] == 0x001F)
172 avctx->pix_fmt = AV_PIX_FMT_RGB565;
173 else if (rgb[0] == 0x7C00 && rgb[1] == 0x03E0 && rgb[2] == 0x001F)
174 avctx->pix_fmt = AV_PIX_FMT_RGB555;
175 else if (rgb[0] == 0x0F00 && rgb[1] == 0x00F0 && rgb[2] == 0x000F)
176 avctx->pix_fmt = AV_PIX_FMT_RGB444;
178 av_log(avctx, AV_LOG_ERROR, "Unknown bitfields %0X %0X %0X\n", rgb[0], rgb[1], rgb[2]);
179 return AVERROR(EINVAL);
184 if (hsize - ihsize - 14 > 0)
185 avctx->pix_fmt = AV_PIX_FMT_PAL8;
187 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
191 if (hsize - ihsize - 14 > 0) {
192 avctx->pix_fmt = AV_PIX_FMT_PAL8;
194 av_log(avctx, AV_LOG_ERROR, "Unknown palette for %d-colour BMP\n", 1<<depth);
195 return AVERROR_INVALIDDATA;
199 av_log(avctx, AV_LOG_ERROR, "depth %d not supported\n", depth);
200 return AVERROR_INVALIDDATA;
203 if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
204 av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
205 return AVERROR_INVALIDDATA;
209 avctx->release_buffer(avctx, p);
212 if ((ret = ff_get_buffer(avctx, p)) < 0) {
213 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
216 p->pict_type = AV_PICTURE_TYPE_I;
220 dsize = buf_size - hsize;
222 /* Line size in file multiple of 4 */
223 n = ((avctx->width * depth) / 8 + 3) & ~3;
225 if (n * avctx->height > dsize && comp != BMP_RLE4 && comp != BMP_RLE8) {
226 av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
227 dsize, n * avctx->height);
228 return AVERROR_INVALIDDATA;
231 // RLE may skip decoding some picture areas, so blank picture before decoding
232 if (comp == BMP_RLE4 || comp == BMP_RLE8)
233 memset(p->data[0], 0, avctx->height * p->linesize[0]);
236 ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
237 linesize = -p->linesize[0];
240 linesize = p->linesize[0];
243 if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
244 int colors = 1 << depth;
246 memset(p->data[1], 0, 1024);
251 t = bytestream_get_le32(&buf);
252 if (t < 0 || t > (1 << depth)) {
253 av_log(avctx, AV_LOG_ERROR, "Incorrect number of colors - %X for bitdepth %d\n", t, depth);
258 buf = buf0 + 14 + ihsize; //palette location
259 // OS/2 bitmap, 3 bytes per palette entry
260 if ((hsize-ihsize-14) < (colors << 2)) {
261 for (i = 0; i < colors; i++)
262 ((uint32_t*)p->data[1])[i] = bytestream_get_le24(&buf);
264 for (i = 0; i < colors; i++)
265 ((uint32_t*)p->data[1])[i] = bytestream_get_le32(&buf);
269 if (comp == BMP_RLE4 || comp == BMP_RLE8) {
271 p->data[0] += p->linesize[0] * (avctx->height - 1);
272 p->linesize[0] = -p->linesize[0];
274 bytestream2_init(&gb, buf, dsize);
275 ff_msrle_decode(avctx, (AVPicture*)p, depth, &gb);
277 p->data[0] += p->linesize[0] * (avctx->height - 1);
278 p->linesize[0] = -p->linesize[0];
283 for (i = 0; i < avctx->height; i++) {
285 for (j = 0; j < n; j++) {
286 ptr[j*8+0] = buf[j] >> 7;
287 ptr[j*8+1] = (buf[j] >> 6) & 1;
288 ptr[j*8+2] = (buf[j] >> 5) & 1;
289 ptr[j*8+3] = (buf[j] >> 4) & 1;
290 ptr[j*8+4] = (buf[j] >> 3) & 1;
291 ptr[j*8+5] = (buf[j] >> 2) & 1;
292 ptr[j*8+6] = (buf[j] >> 1) & 1;
293 ptr[j*8+7] = buf[j] & 1;
301 for (i = 0; i < avctx->height; i++) {
308 for (i = 0; i < avctx->height; i++) {
310 for (j = 0; j < n; j++) {
311 ptr[j*2+0] = (buf[j] >> 4) & 0xF;
312 ptr[j*2+1] = buf[j] & 0xF;
319 for (i = 0; i < avctx->height; i++) {
320 const uint16_t *src = (const uint16_t *) buf;
321 uint16_t *dst = (uint16_t *) ptr;
323 for (j = 0; j < avctx->width; j++)
324 *dst++ = av_le2ne16(*src++);
331 for (i = 0; i < avctx->height; i++) {
332 const uint8_t *src = buf;
335 for (j = 0; j < avctx->width; j++) {
336 dst[0] = src[rgb[2]];
337 dst[1] = src[rgb[1]];
338 dst[2] = src[rgb[0]];
348 av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
349 return AVERROR_INVALIDDATA;
353 *picture = s->picture;
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,
372 .id = AV_CODEC_ID_BMP,
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 (Windows and OS/2 bitmap)"),