3
* Copyright (c) 2005 Mans Rullgard
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Lesser General Public
7
* License as published by the Free Software Foundation; either
8
* version 2 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Lesser General Public License for more details.
15
* You should have received a copy of the GNU Lesser General Public
16
* License along with this library; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
#include "bitstream.h"
24
typedef struct BMPContext {
31
#define BMP_BITFIELDS 3
33
#define read16(bits) bswap_16(get_bits(bits, 16))
34
#define read32(bits) bswap_32(get_bits_long(bits, 32))
36
static int bmp_decode_init(AVCodecContext *avctx){
37
BMPContext *s = avctx->priv_data;
39
avcodec_get_frame_defaults((AVFrame*)&s->picture);
40
avctx->coded_frame = (AVFrame*)&s->picture;
45
static int bmp_decode_frame(AVCodecContext *avctx,
46
void *data, int *data_size,
47
uint8_t *buf, int buf_size)
49
BMPContext *s = avctx->priv_data;
50
AVFrame *picture = data;
51
AVFrame *p = &s->picture;
53
unsigned int fsize, hsize;
58
int i, j, n, linesize;
64
av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\n", buf_size);
68
init_get_bits(&bits, buf, buf_size);
70
if(get_bits(&bits, 16) != 0x424d){ /* 'BM' */
71
av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
75
fsize = read32(&bits);
77
av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
82
skip_bits(&bits, 16); /* reserved1 */
83
skip_bits(&bits, 16); /* reserved2 */
85
hsize = read32(&bits); /* header size */
87
av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
92
ihsize = read32(&bits); /* more header size */
93
if(ihsize + 14 > hsize){
94
av_log(avctx, AV_LOG_ERROR, "invalid header size %d\n", hsize);
98
width = read32(&bits);
99
height = read32(&bits);
101
if(read16(&bits) != 1){ /* planes */
102
av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n");
106
depth = read16(&bits);
109
comp = read32(&bits);
113
if(comp != BMP_RGB && comp != BMP_BITFIELDS){
114
av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp);
118
if(comp == BMP_BITFIELDS){
119
skip_bits(&bits, 20 * 8);
120
rgb[0] = read32(&bits);
121
rgb[1] = read32(&bits);
122
rgb[2] = read32(&bits);
125
avctx->codec_id = CODEC_ID_BMP;
126
avctx->width = width;
127
avctx->height = height > 0? height: -height;
129
avctx->pix_fmt = PIX_FMT_NONE;
133
if(comp == BMP_BITFIELDS){
134
rgb[0] = (rgb[0] >> 15) & 3;
135
rgb[1] = (rgb[1] >> 15) & 3;
136
rgb[2] = (rgb[2] >> 15) & 3;
138
if(rgb[0] + rgb[1] + rgb[2] != 3 ||
139
rgb[0] == rgb[1] || rgb[0] == rgb[2] || rgb[1] == rgb[2]){
148
avctx->pix_fmt = PIX_FMT_BGR24;
151
avctx->pix_fmt = PIX_FMT_BGR24;
155
avctx->pix_fmt = PIX_FMT_RGB555;
158
av_log(avctx, AV_LOG_ERROR, "depth %d not supported\n", depth);
162
if(avctx->pix_fmt == PIX_FMT_NONE){
163
av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
168
if(avctx->get_buffer(avctx, p) < 0){
169
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
172
p->pict_type = FF_I_TYPE;
176
dsize = buf_size - hsize;
178
n = avctx->width * (depth / 8);
180
if(n * avctx->height > dsize){
181
av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
182
dsize, n * avctx->height);
187
ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
188
linesize = -p->linesize[0];
191
linesize = p->linesize[0];
196
for(i = 0; i < avctx->height; i++){
203
for(i = 0; i < avctx->height; i++){
204
uint16_t *src = (uint16_t *) buf;
205
uint16_t *dst = (uint16_t *) ptr;
207
for(j = 0; j < avctx->width; j++)
208
*dst++ = le2me_16(*src++);
215
for(i = 0; i < avctx->height; i++){
219
for(j = 0; j < avctx->width; j++){
220
dst[0] = src[rgb[2]];
221
dst[1] = src[rgb[1]];
222
dst[2] = src[rgb[0]];
232
av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
236
*picture = s->picture;
237
*data_size = sizeof(AVPicture);
242
AVCodec bmp_decoder = {