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"
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;
41 uint32_t rgb[3] = {0};
45 const uint8_t *buf0 = buf;
49 av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\n", buf_size);
50 return AVERROR_INVALIDDATA;
53 if (bytestream_get_byte(&buf) != 'B' ||
54 bytestream_get_byte(&buf) != 'M') {
55 av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
56 return AVERROR_INVALIDDATA;
59 fsize = bytestream_get_le32(&buf);
60 if (buf_size < fsize) {
61 av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d), trying to decode anyway\n",
66 buf += 2; /* reserved1 */
67 buf += 2; /* reserved2 */
69 hsize = bytestream_get_le32(&buf); /* header size */
70 ihsize = bytestream_get_le32(&buf); /* more header size */
71 if (ihsize + 14 > hsize) {
72 av_log(avctx, AV_LOG_ERROR, "invalid header size %d\n", hsize);
73 return AVERROR_INVALIDDATA;
76 /* sometimes file size is set to some headers size, set a real size in that case */
77 if (fsize == 14 || fsize == ihsize + 14)
81 av_log(avctx, AV_LOG_ERROR, "declared file size is less than header size (%d < %d)\n",
83 return AVERROR_INVALIDDATA;
90 case 108: // windib v4
91 case 124: // windib v5
92 width = bytestream_get_le32(&buf);
93 height = bytestream_get_le32(&buf);
96 width = bytestream_get_le16(&buf);
97 height = bytestream_get_le16(&buf);
100 av_log(avctx, AV_LOG_ERROR, "unsupported BMP file, patch welcome\n");
101 return AVERROR_PATCHWELCOME;
105 if (bytestream_get_le16(&buf) != 1) {
106 av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n");
107 return AVERROR_INVALIDDATA;
110 depth = bytestream_get_le16(&buf);
113 comp = bytestream_get_le32(&buf);
117 if (comp != BMP_RGB && comp != BMP_BITFIELDS && comp != BMP_RLE4 &&
119 av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp);
120 return AVERROR_INVALIDDATA;
123 if (comp == BMP_BITFIELDS) {
125 rgb[0] = bytestream_get_le32(&buf);
126 rgb[1] = bytestream_get_le32(&buf);
127 rgb[2] = bytestream_get_le32(&buf);
128 alpha = bytestream_get_le32(&buf);
131 avctx->width = width;
132 avctx->height = height > 0 ? height : -height;
134 avctx->pix_fmt = AV_PIX_FMT_NONE;
138 if (comp == BMP_BITFIELDS) {
139 if (rgb[0] == 0xFF000000 && rgb[1] == 0x00FF0000 && rgb[2] == 0x0000FF00)
140 avctx->pix_fmt = alpha ? AV_PIX_FMT_ABGR : AV_PIX_FMT_0BGR;
141 else if (rgb[0] == 0x00FF0000 && rgb[1] == 0x0000FF00 && rgb[2] == 0x000000FF)
142 avctx->pix_fmt = alpha ? AV_PIX_FMT_BGRA : AV_PIX_FMT_BGR0;
143 else if (rgb[0] == 0x0000FF00 && rgb[1] == 0x00FF0000 && rgb[2] == 0xFF000000)
144 avctx->pix_fmt = alpha ? AV_PIX_FMT_ARGB : AV_PIX_FMT_0RGB;
145 else if (rgb[0] == 0x000000FF && rgb[1] == 0x0000FF00 && rgb[2] == 0x00FF0000)
146 avctx->pix_fmt = alpha ? AV_PIX_FMT_RGBA : AV_PIX_FMT_RGB0;
148 av_log(avctx, AV_LOG_ERROR, "Unknown bitfields %0X %0X %0X\n", rgb[0], rgb[1], rgb[2]);
149 return AVERROR(EINVAL);
152 avctx->pix_fmt = AV_PIX_FMT_BGRA;
156 avctx->pix_fmt = AV_PIX_FMT_BGR24;
160 avctx->pix_fmt = AV_PIX_FMT_RGB555;
161 else if (comp == BMP_BITFIELDS) {
162 if (rgb[0] == 0xF800 && rgb[1] == 0x07E0 && rgb[2] == 0x001F)
163 avctx->pix_fmt = AV_PIX_FMT_RGB565;
164 else if (rgb[0] == 0x7C00 && rgb[1] == 0x03E0 && rgb[2] == 0x001F)
165 avctx->pix_fmt = AV_PIX_FMT_RGB555;
166 else if (rgb[0] == 0x0F00 && rgb[1] == 0x00F0 && rgb[2] == 0x000F)
167 avctx->pix_fmt = AV_PIX_FMT_RGB444;
169 av_log(avctx, AV_LOG_ERROR, "Unknown bitfields %0X %0X %0X\n", rgb[0], rgb[1], rgb[2]);
170 return AVERROR(EINVAL);
175 if (hsize - ihsize - 14 > 0)
176 avctx->pix_fmt = AV_PIX_FMT_PAL8;
178 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
182 if (hsize - ihsize - 14 > 0) {
183 avctx->pix_fmt = AV_PIX_FMT_PAL8;
185 av_log(avctx, AV_LOG_ERROR, "Unknown palette for %d-colour BMP\n", 1<<depth);
186 return AVERROR_INVALIDDATA;
190 av_log(avctx, AV_LOG_ERROR, "depth %d not supported\n", depth);
191 return AVERROR_INVALIDDATA;
194 if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
195 av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
196 return AVERROR_INVALIDDATA;
199 if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
201 p->pict_type = AV_PICTURE_TYPE_I;
205 dsize = buf_size - hsize;
207 /* Line size in file multiple of 4 */
208 n = ((avctx->width * depth + 31) / 8) & ~3;
210 if (n * avctx->height > dsize && comp != BMP_RLE4 && comp != BMP_RLE8) {
211 av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
212 dsize, n * avctx->height);
213 return AVERROR_INVALIDDATA;
216 // RLE may skip decoding some picture areas, so blank picture before decoding
217 if (comp == BMP_RLE4 || comp == BMP_RLE8)
218 memset(p->data[0], 0, avctx->height * p->linesize[0]);
221 ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
222 linesize = -p->linesize[0];
225 linesize = p->linesize[0];
228 if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
229 int colors = 1 << depth;
231 memset(p->data[1], 0, 1024);
236 t = bytestream_get_le32(&buf);
237 if (t < 0 || t > (1 << depth)) {
238 av_log(avctx, AV_LOG_ERROR, "Incorrect number of colors - %X for bitdepth %d\n", t, depth);
243 buf = buf0 + 14 + ihsize; //palette location
244 // OS/2 bitmap, 3 bytes per palette entry
245 if ((hsize-ihsize-14) < (colors << 2)) {
246 if ((hsize-ihsize-14) < colors * 3) {
247 av_log(avctx, AV_LOG_ERROR, "palette doesn't fit in packet\n");
248 return AVERROR_INVALIDDATA;
250 for (i = 0; i < colors; i++)
251 ((uint32_t*)p->data[1])[i] = (0xFFU<<24) | bytestream_get_le24(&buf);
253 for (i = 0; i < colors; i++)
254 ((uint32_t*)p->data[1])[i] = 0xFFU << 24 | bytestream_get_le32(&buf);
258 if (comp == BMP_RLE4 || comp == BMP_RLE8) {
259 if (comp == BMP_RLE8 && height < 0) {
260 p->data[0] += p->linesize[0] * (avctx->height - 1);
261 p->linesize[0] = -p->linesize[0];
263 bytestream2_init(&gb, buf, dsize);
264 ff_msrle_decode(avctx, (AVPicture*)p, depth, &gb);
266 p->data[0] += p->linesize[0] * (avctx->height - 1);
267 p->linesize[0] = -p->linesize[0];
272 for (i = 0; i < avctx->height; i++) {
274 for (j = 0; j < n; j++) {
275 ptr[j*8+0] = buf[j] >> 7;
276 ptr[j*8+1] = (buf[j] >> 6) & 1;
277 ptr[j*8+2] = (buf[j] >> 5) & 1;
278 ptr[j*8+3] = (buf[j] >> 4) & 1;
279 ptr[j*8+4] = (buf[j] >> 3) & 1;
280 ptr[j*8+5] = (buf[j] >> 2) & 1;
281 ptr[j*8+6] = (buf[j] >> 1) & 1;
282 ptr[j*8+7] = buf[j] & 1;
291 for (i = 0; i < avctx->height; i++) {
298 for (i = 0; i < avctx->height; i++) {
300 for (j = 0; j < n; j++) {
301 ptr[j*2+0] = (buf[j] >> 4) & 0xF;
302 ptr[j*2+1] = buf[j] & 0xF;
309 for (i = 0; i < avctx->height; i++) {
310 const uint16_t *src = (const uint16_t *) buf;
311 uint16_t *dst = (uint16_t *) ptr;
313 for (j = 0; j < avctx->width; j++)
314 *dst++ = av_le2ne16(*src++);
321 av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
322 return AVERROR_INVALIDDATA;
331 AVCodec ff_bmp_decoder = {
333 .long_name = NULL_IF_CONFIG_SMALL("BMP (Windows and OS/2 bitmap)"),
334 .type = AVMEDIA_TYPE_VIDEO,
335 .id = AV_CODEC_ID_BMP,
336 .decode = bmp_decode_frame,
337 .capabilities = CODEC_CAP_DR1,