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 {
37 #define read16(bits) bswap_16(get_bits(bits, 16))
38 #define read32(bits) bswap_32(get_bits_long(bits, 32))
40 static int bmp_decode_init(AVCodecContext *avctx){
41 BMPContext *s = avctx->priv_data;
43 avcodec_get_frame_defaults((AVFrame*)&s->picture);
44 avctx->coded_frame = (AVFrame*)&s->picture;
49 static int bmp_decode_frame(AVCodecContext *avctx,
50 void *data, int *data_size,
51 uint8_t *buf, int buf_size)
53 BMPContext *s = avctx->priv_data;
54 AVFrame *picture = data;
55 AVFrame *p = &s->picture;
57 unsigned int fsize, hsize;
62 int i, j, n, linesize;
68 av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\n", buf_size);
72 init_get_bits(&bits, buf, buf_size);
74 if(get_bits(&bits, 16) != 0x424d){ /* 'BM' */
75 av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
79 fsize = read32(&bits);
81 av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
86 skip_bits(&bits, 16); /* reserved1 */
87 skip_bits(&bits, 16); /* reserved2 */
89 hsize = read32(&bits); /* header size */
91 av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
96 ihsize = read32(&bits); /* more header size */
97 if(ihsize + 14 > hsize){
98 av_log(avctx, AV_LOG_ERROR, "invalid header size %d\n", hsize);
102 width = read32(&bits);
103 height = read32(&bits);
105 if(read16(&bits) != 1){ /* planes */
106 av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n");
110 depth = read16(&bits);
113 comp = read32(&bits);
117 if(comp != BMP_RGB && comp != BMP_BITFIELDS){
118 av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp);
122 if(comp == BMP_BITFIELDS){
123 skip_bits(&bits, 20 * 8);
124 rgb[0] = read32(&bits);
125 rgb[1] = read32(&bits);
126 rgb[2] = read32(&bits);
129 avctx->codec_id = CODEC_ID_BMP;
130 avctx->width = width;
131 avctx->height = height > 0? height: -height;
133 avctx->pix_fmt = PIX_FMT_NONE;
137 if(comp == BMP_BITFIELDS){
138 rgb[0] = (rgb[0] >> 15) & 3;
139 rgb[1] = (rgb[1] >> 15) & 3;
140 rgb[2] = (rgb[2] >> 15) & 3;
142 if(rgb[0] + rgb[1] + rgb[2] != 3 ||
143 rgb[0] == rgb[1] || rgb[0] == rgb[2] || rgb[1] == rgb[2]){
152 avctx->pix_fmt = PIX_FMT_BGR24;
155 avctx->pix_fmt = PIX_FMT_BGR24;
159 avctx->pix_fmt = PIX_FMT_RGB555;
162 av_log(avctx, AV_LOG_ERROR, "depth %d not supported\n", depth);
166 if(avctx->pix_fmt == PIX_FMT_NONE){
167 av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
172 if(avctx->get_buffer(avctx, p) < 0){
173 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
176 p->pict_type = FF_I_TYPE;
180 dsize = buf_size - hsize;
182 /* Line size in file multiple of 4 */
183 n = (avctx->width * (depth / 8) + 3) & ~3;
185 if(n * avctx->height > dsize){
186 av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
187 dsize, n * avctx->height);
192 ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
193 linesize = -p->linesize[0];
196 linesize = p->linesize[0];
201 for(i = 0; i < avctx->height; i++){
208 for(i = 0; i < avctx->height; i++){
209 uint16_t *src = (uint16_t *) buf;
210 uint16_t *dst = (uint16_t *) ptr;
212 for(j = 0; j < avctx->width; j++)
213 *dst++ = le2me_16(*src++);
220 for(i = 0; i < avctx->height; i++){
224 for(j = 0; j < avctx->width; j++){
225 dst[0] = src[rgb[2]];
226 dst[1] = src[rgb[1]];
227 dst[2] = src[rgb[0]];
237 av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
241 *picture = s->picture;
242 *data_size = sizeof(AVPicture);
247 AVCodec bmp_decoder = {