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 "bitstream.h"
26 typedef struct BMPContext {
33 #define BMP_BITFIELDS 3
35 #define read16(bits) bswap_16(get_bits(bits, 16))
36 #define read32(bits) bswap_32(get_bits_long(bits, 32))
38 static int bmp_decode_init(AVCodecContext *avctx){
39 BMPContext *s = avctx->priv_data;
41 avcodec_get_frame_defaults((AVFrame*)&s->picture);
42 avctx->coded_frame = (AVFrame*)&s->picture;
47 static int bmp_decode_frame(AVCodecContext *avctx,
48 void *data, int *data_size,
49 uint8_t *buf, int buf_size)
51 BMPContext *s = avctx->priv_data;
52 AVFrame *picture = data;
53 AVFrame *p = &s->picture;
55 unsigned int fsize, hsize;
60 int i, j, n, linesize;
66 av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\n", buf_size);
70 init_get_bits(&bits, buf, buf_size);
72 if(get_bits(&bits, 16) != 0x424d){ /* 'BM' */
73 av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
77 fsize = read32(&bits);
79 av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
84 skip_bits(&bits, 16); /* reserved1 */
85 skip_bits(&bits, 16); /* reserved2 */
87 hsize = read32(&bits); /* header size */
89 av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
94 ihsize = read32(&bits); /* more header size */
95 if(ihsize + 14 > hsize){
96 av_log(avctx, AV_LOG_ERROR, "invalid header size %d\n", hsize);
100 width = read32(&bits);
101 height = read32(&bits);
103 if(read16(&bits) != 1){ /* planes */
104 av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n");
108 depth = read16(&bits);
111 comp = read32(&bits);
115 if(comp != BMP_RGB && comp != BMP_BITFIELDS){
116 av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp);
120 if(comp == BMP_BITFIELDS){
121 skip_bits(&bits, 20 * 8);
122 rgb[0] = read32(&bits);
123 rgb[1] = read32(&bits);
124 rgb[2] = read32(&bits);
127 avctx->codec_id = CODEC_ID_BMP;
128 avctx->width = width;
129 avctx->height = height > 0? height: -height;
131 avctx->pix_fmt = PIX_FMT_NONE;
135 if(comp == BMP_BITFIELDS){
136 rgb[0] = (rgb[0] >> 15) & 3;
137 rgb[1] = (rgb[1] >> 15) & 3;
138 rgb[2] = (rgb[2] >> 15) & 3;
140 if(rgb[0] + rgb[1] + rgb[2] != 3 ||
141 rgb[0] == rgb[1] || rgb[0] == rgb[2] || rgb[1] == rgb[2]){
150 avctx->pix_fmt = PIX_FMT_BGR24;
153 avctx->pix_fmt = PIX_FMT_BGR24;
157 avctx->pix_fmt = PIX_FMT_RGB555;
160 av_log(avctx, AV_LOG_ERROR, "depth %d not supported\n", depth);
164 if(avctx->pix_fmt == PIX_FMT_NONE){
165 av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
170 if(avctx->get_buffer(avctx, p) < 0){
171 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
174 p->pict_type = FF_I_TYPE;
178 dsize = buf_size - hsize;
180 n = avctx->width * (depth / 8);
182 if(n * avctx->height > dsize){
183 av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
184 dsize, n * avctx->height);
189 ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
190 linesize = -p->linesize[0];
193 linesize = p->linesize[0];
198 for(i = 0; i < avctx->height; i++){
205 for(i = 0; i < avctx->height; i++){
206 uint16_t *src = (uint16_t *) buf;
207 uint16_t *dst = (uint16_t *) ptr;
209 for(j = 0; j < avctx->width; j++)
210 *dst++ = le2me_16(*src++);
217 for(i = 0; i < avctx->height; i++){
221 for(j = 0; j < avctx->width; j++){
222 dst[0] = src[rgb[2]];
223 dst[1] = src[rgb[1]];
224 dst[2] = src[rgb[0]];
234 av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
238 *picture = s->picture;
239 *data_size = sizeof(AVPicture);
244 AVCodec bmp_decoder = {