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"
27 static av_cold int bmp_decode_init(AVCodecContext *avctx){
28 BMPContext *s = avctx->priv_data;
30 avcodec_get_frame_defaults((AVFrame*)&s->picture);
31 avctx->coded_frame = (AVFrame*)&s->picture;
36 static int bmp_decode_frame(AVCodecContext *avctx,
37 void *data, int *data_size,
38 const uint8_t *buf, int buf_size)
40 BMPContext *s = avctx->priv_data;
41 AVFrame *picture = data;
42 AVFrame *p = &s->picture;
43 unsigned int fsize, hsize;
48 int i, j, n, linesize;
52 const uint8_t *buf0 = buf;
55 av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\n", buf_size);
59 if(bytestream_get_byte(&buf) != 'B' ||
60 bytestream_get_byte(&buf) != 'M') {
61 av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
65 fsize = bytestream_get_le32(&buf);
67 av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d), trying to decode anyway\n",
72 buf += 2; /* reserved1 */
73 buf += 2; /* reserved2 */
75 hsize = bytestream_get_le32(&buf); /* header size */
76 ihsize = bytestream_get_le32(&buf); /* more header size */
77 if(ihsize + 14 > hsize){
78 av_log(avctx, AV_LOG_ERROR, "invalid header size %d\n", hsize);
82 /* sometimes file size is set to some headers size, set a real size in that case */
83 if(fsize == 14 || fsize == ihsize + 14)
87 av_log(avctx, AV_LOG_ERROR, "declared file size is less than header size (%d < %d)\n",
95 case 108: // windib v4
96 case 124: // windib v5
97 width = bytestream_get_le32(&buf);
98 height = bytestream_get_le32(&buf);
101 width = bytestream_get_le16(&buf);
102 height = bytestream_get_le16(&buf);
105 av_log(avctx, AV_LOG_ERROR, "unsupported BMP file, patch welcome\n");
109 if(bytestream_get_le16(&buf) != 1){ /* planes */
110 av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n");
114 depth = bytestream_get_le16(&buf);
117 comp = bytestream_get_le32(&buf);
121 if(comp != BMP_RGB && comp != BMP_BITFIELDS && comp != BMP_RLE4 && comp != BMP_RLE8){
122 av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp);
126 if(comp == BMP_BITFIELDS){
128 rgb[0] = bytestream_get_le32(&buf);
129 rgb[1] = bytestream_get_le32(&buf);
130 rgb[2] = bytestream_get_le32(&buf);
133 avctx->width = width;
134 avctx->height = height > 0? height: -height;
136 avctx->pix_fmt = PIX_FMT_NONE;
140 if(comp == BMP_BITFIELDS){
141 rgb[0] = (rgb[0] >> 15) & 3;
142 rgb[1] = (rgb[1] >> 15) & 3;
143 rgb[2] = (rgb[2] >> 15) & 3;
145 if(rgb[0] + rgb[1] + rgb[2] != 3 ||
146 rgb[0] == rgb[1] || rgb[0] == rgb[2] || rgb[1] == rgb[2]){
155 avctx->pix_fmt = PIX_FMT_BGR24;
158 avctx->pix_fmt = PIX_FMT_BGR24;
162 avctx->pix_fmt = PIX_FMT_RGB555;
163 if(comp == BMP_BITFIELDS)
164 avctx->pix_fmt = rgb[1] == 0x07E0 ? PIX_FMT_RGB565 : PIX_FMT_RGB555;
167 if(hsize - ihsize - 14 > 0)
168 avctx->pix_fmt = PIX_FMT_PAL8;
170 avctx->pix_fmt = PIX_FMT_GRAY8;
173 if(hsize - ihsize - 14 > 0){
174 avctx->pix_fmt = PIX_FMT_PAL8;
176 av_log(avctx, AV_LOG_ERROR, "Unknown palette for 16-colour BMP\n");
181 avctx->pix_fmt = PIX_FMT_MONOBLACK;
184 av_log(avctx, AV_LOG_ERROR, "depth %d not supported\n", depth);
188 if(avctx->pix_fmt == PIX_FMT_NONE){
189 av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
194 avctx->release_buffer(avctx, p);
197 if(avctx->get_buffer(avctx, p) < 0){
198 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
201 p->pict_type = FF_I_TYPE;
205 dsize = buf_size - hsize;
207 /* Line size in file multiple of 4 */
208 n = ((avctx->width * depth) / 8 + 3) & ~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);
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]);
220 if(depth == 4 || depth == 8)
221 memset(p->data[1], 0, 1024);
224 ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
225 linesize = -p->linesize[0];
228 linesize = p->linesize[0];
231 if(avctx->pix_fmt == PIX_FMT_PAL8){
232 buf = buf0 + 14 + ihsize; //palette location
233 if((hsize-ihsize-14)>>depth < 4){ // OS/2 bitmap, 3 bytes per palette entry
234 for(i = 0; i < (1 << depth); i++)
235 ((uint32_t*)p->data[1])[i] = bytestream_get_le24(&buf);
237 for(i = 0; i < (1 << depth); i++)
238 ((uint32_t*)p->data[1])[i] = bytestream_get_le32(&buf);
242 if(comp == BMP_RLE4 || comp == BMP_RLE8){
243 ff_msrle_decode(avctx, (AVPicture*)p, depth, buf, dsize);
247 for(i = 0; i < avctx->height; i++){
254 for(i = 0; i < avctx->height; i++){
256 for(j = 0; j < n; j++){
257 ptr[j*2+0] = (buf[j] >> 4) & 0xF;
258 ptr[j*2+1] = buf[j] & 0xF;
265 for(i = 0; i < avctx->height; i++){
266 memcpy(ptr, buf, avctx->width);
272 for(i = 0; i < avctx->height; i++){
273 memcpy(ptr, buf, avctx->width*(depth>>3));
279 for(i = 0; i < avctx->height; i++){
280 const uint16_t *src = (const uint16_t *) buf;
281 uint16_t *dst = (uint16_t *) ptr;
283 for(j = 0; j < avctx->width; j++)
284 *dst++ = le2me_16(*src++);
291 for(i = 0; i < avctx->height; i++){
292 const uint8_t *src = buf;
295 for(j = 0; j < avctx->width; j++){
296 dst[0] = src[rgb[2]];
297 dst[1] = src[rgb[1]];
298 dst[2] = src[rgb[0]];
308 av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
313 *picture = s->picture;
314 *data_size = sizeof(AVPicture);
319 static av_cold int bmp_decode_end(AVCodecContext *avctx)
321 BMPContext* c = avctx->priv_data;
323 if (c->picture.data[0])
324 avctx->release_buffer(avctx, &c->picture);
329 AVCodec bmp_decoder = {
338 .long_name = NULL_IF_CONFIG_SMALL("BMP image"),