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
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;
43 uint32_t rgb[3] = {0};
47 const uint8_t *buf0 = buf;
51 av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\n", buf_size);
52 return AVERROR_INVALIDDATA;
55 if (bytestream_get_byte(&buf) != 'B' ||
56 bytestream_get_byte(&buf) != 'M') {
57 av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
58 return AVERROR_INVALIDDATA;
61 fsize = bytestream_get_le32(&buf);
62 if (buf_size < fsize) {
63 av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %u), trying to decode anyway\n",
68 buf += 2; /* reserved1 */
69 buf += 2; /* reserved2 */
71 hsize = bytestream_get_le32(&buf); /* header size */
72 ihsize = bytestream_get_le32(&buf); /* more header size */
73 if (ihsize + 14LL > hsize) {
74 av_log(avctx, AV_LOG_ERROR, "invalid header size %u\n", hsize);
75 return AVERROR_INVALIDDATA;
78 /* sometimes file size is set to some headers size, set a real size in that case */
79 if (fsize == 14 || fsize == ihsize + 14)
83 av_log(avctx, AV_LOG_ERROR,
84 "Declared file size is less than header size (%u < %u)\n",
86 return AVERROR_INVALIDDATA;
93 case 108: // windib v4
94 case 124: // windib v5
95 width = bytestream_get_le32(&buf);
96 height = bytestream_get_le32(&buf);
99 width = bytestream_get_le16(&buf);
100 height = bytestream_get_le16(&buf);
103 avpriv_report_missing_feature(avctx, "Information header size %u",
105 return AVERROR_PATCHWELCOME;
109 if (bytestream_get_le16(&buf) != 1) {
110 av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n");
111 return AVERROR_INVALIDDATA;
114 depth = bytestream_get_le16(&buf);
117 comp = bytestream_get_le32(&buf);
121 if (comp != BMP_RGB && comp != BMP_BITFIELDS && comp != BMP_RLE4 &&
123 av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp);
124 return AVERROR_INVALIDDATA;
127 if (comp == BMP_BITFIELDS) {
129 rgb[0] = bytestream_get_le32(&buf);
130 rgb[1] = bytestream_get_le32(&buf);
131 rgb[2] = bytestream_get_le32(&buf);
133 alpha = bytestream_get_le32(&buf);
136 avctx->width = width;
137 avctx->height = height > 0 ? height : -(unsigned)height;
139 avctx->pix_fmt = AV_PIX_FMT_NONE;
143 if (comp == BMP_BITFIELDS) {
144 if (rgb[0] == 0xFF000000 && rgb[1] == 0x00FF0000 && rgb[2] == 0x0000FF00)
145 avctx->pix_fmt = alpha ? AV_PIX_FMT_ABGR : AV_PIX_FMT_0BGR;
146 else if (rgb[0] == 0x00FF0000 && rgb[1] == 0x0000FF00 && rgb[2] == 0x000000FF)
147 avctx->pix_fmt = alpha ? AV_PIX_FMT_BGRA : AV_PIX_FMT_BGR0;
148 else if (rgb[0] == 0x0000FF00 && rgb[1] == 0x00FF0000 && rgb[2] == 0xFF000000)
149 avctx->pix_fmt = alpha ? AV_PIX_FMT_ARGB : AV_PIX_FMT_0RGB;
150 else if (rgb[0] == 0x000000FF && rgb[1] == 0x0000FF00 && rgb[2] == 0x00FF0000)
151 avctx->pix_fmt = alpha ? AV_PIX_FMT_RGBA : AV_PIX_FMT_RGB0;
153 av_log(avctx, AV_LOG_ERROR, "Unknown bitfields "
154 "%0"PRIX32" %0"PRIX32" %0"PRIX32"\n", rgb[0], rgb[1], rgb[2]);
155 return AVERROR(EINVAL);
158 avctx->pix_fmt = AV_PIX_FMT_BGRA;
162 avctx->pix_fmt = AV_PIX_FMT_BGR24;
166 avctx->pix_fmt = AV_PIX_FMT_RGB555;
167 else if (comp == BMP_BITFIELDS) {
168 if (rgb[0] == 0xF800 && rgb[1] == 0x07E0 && rgb[2] == 0x001F)
169 avctx->pix_fmt = AV_PIX_FMT_RGB565;
170 else if (rgb[0] == 0x7C00 && rgb[1] == 0x03E0 && rgb[2] == 0x001F)
171 avctx->pix_fmt = AV_PIX_FMT_RGB555;
172 else if (rgb[0] == 0x0F00 && rgb[1] == 0x00F0 && rgb[2] == 0x000F)
173 avctx->pix_fmt = AV_PIX_FMT_RGB444;
175 av_log(avctx, AV_LOG_ERROR,
176 "Unknown bitfields %0"PRIX32" %0"PRIX32" %0"PRIX32"\n",
177 rgb[0], rgb[1], rgb[2]);
178 return AVERROR(EINVAL);
183 if (hsize - ihsize - 14 > 0)
184 avctx->pix_fmt = AV_PIX_FMT_PAL8;
186 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
190 if (hsize - ihsize - 14 > 0) {
191 avctx->pix_fmt = AV_PIX_FMT_PAL8;
193 av_log(avctx, AV_LOG_ERROR, "Unknown palette for %u-colour BMP\n",
195 return AVERROR_INVALIDDATA;
199 av_log(avctx, AV_LOG_ERROR, "depth %u 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;
208 if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
210 p->pict_type = AV_PICTURE_TYPE_I;
214 dsize = buf_size - hsize;
216 /* Line size in file multiple of 4 */
217 n = ((avctx->width * depth + 31) / 8) & ~3;
219 if (n * avctx->height > dsize && comp != BMP_RLE4 && comp != BMP_RLE8) {
220 n = (avctx->width * depth + 7) / 8;
221 if (n * avctx->height > dsize) {
222 av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
223 dsize, n * avctx->height);
224 return AVERROR_INVALIDDATA;
226 av_log(avctx, AV_LOG_ERROR, "data size too small, assuming missing line alignment\n");
229 // RLE may skip decoding some picture areas, so blank picture before decoding
230 if (comp == BMP_RLE4 || comp == BMP_RLE8)
231 memset(p->data[0], 0, avctx->height * p->linesize[0]);
234 ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
235 linesize = -p->linesize[0];
238 linesize = p->linesize[0];
241 if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
242 int colors = 1 << depth;
244 memset(p->data[1], 0, 1024);
249 t = bytestream_get_le32(&buf);
250 if (t < 0 || t > (1 << depth)) {
251 av_log(avctx, AV_LOG_ERROR,
252 "Incorrect number of colors - %X for bitdepth %u\n",
258 colors = FFMIN(256, (hsize-ihsize-14) / 3);
260 buf = buf0 + 14 + ihsize; //palette location
261 // OS/2 bitmap, 3 bytes per palette entry
262 if ((hsize-ihsize-14) < (colors << 2)) {
263 if ((hsize-ihsize-14) < colors * 3) {
264 av_log(avctx, AV_LOG_ERROR, "palette doesn't fit in packet\n");
265 return AVERROR_INVALIDDATA;
267 for (i = 0; i < colors; i++)
268 ((uint32_t*)p->data[1])[i] = (0xFFU<<24) | bytestream_get_le24(&buf);
270 for (i = 0; i < colors; i++)
271 ((uint32_t*)p->data[1])[i] = 0xFFU << 24 | bytestream_get_le32(&buf);
275 if (comp == BMP_RLE4 || comp == BMP_RLE8) {
276 if (comp == BMP_RLE8 && height < 0) {
277 p->data[0] += p->linesize[0] * (avctx->height - 1);
278 p->linesize[0] = -p->linesize[0];
280 bytestream2_init(&gb, buf, dsize);
281 ff_msrle_decode(avctx, p, depth, &gb);
283 p->data[0] += p->linesize[0] * (avctx->height - 1);
284 p->linesize[0] = -p->linesize[0];
289 for (i = 0; i < avctx->height; i++) {
291 for (j = 0; j < n; j++) {
292 ptr[j*8+0] = buf[j] >> 7;
293 ptr[j*8+1] = (buf[j] >> 6) & 1;
294 ptr[j*8+2] = (buf[j] >> 5) & 1;
295 ptr[j*8+3] = (buf[j] >> 4) & 1;
296 ptr[j*8+4] = (buf[j] >> 3) & 1;
297 ptr[j*8+5] = (buf[j] >> 2) & 1;
298 ptr[j*8+6] = (buf[j] >> 1) & 1;
299 ptr[j*8+7] = buf[j] & 1;
308 for (i = 0; i < avctx->height; i++) {
315 for (i = 0; i < avctx->height; i++) {
317 for (j = 0; j < n; j++) {
318 ptr[j*2+0] = (buf[j] >> 4) & 0xF;
319 ptr[j*2+1] = buf[j] & 0xF;
326 for (i = 0; i < avctx->height; i++) {
327 const uint16_t *src = (const uint16_t *) buf;
328 uint16_t *dst = (uint16_t *) ptr;
330 for (j = 0; j < avctx->width; j++)
331 *dst++ = av_le2ne16(*src++);
338 av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
339 return AVERROR_INVALIDDATA;
342 if (avctx->pix_fmt == AV_PIX_FMT_BGRA) {
343 for (i = 0; i < avctx->height; i++) {
345 uint8_t *ptr = p->data[0] + p->linesize[0]*i + 3;
346 for (j = 0; j < avctx->width; j++) {
350 if (j < avctx->width)
353 if (i == avctx->height)
354 avctx->pix_fmt = p->format = AV_PIX_FMT_BGR0;
362 AVCodec ff_bmp_decoder = {
364 .long_name = NULL_IF_CONFIG_SMALL("BMP (Windows and OS/2 bitmap)"),
365 .type = AVMEDIA_TYPE_VIDEO,
366 .id = AV_CODEC_ID_BMP,
367 .decode = bmp_decode_frame,
368 .capabilities = AV_CODEC_CAP_DR1,