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"
26 static av_cold
int bmp_decode_init(AVCodecContext
*avctx
){
27 BMPContext
*s
= avctx
->priv_data
;
29 avcodec_get_frame_defaults((AVFrame
*)&s
->picture
);
30 avctx
->coded_frame
= (AVFrame
*)&s
->picture
;
35 static int bmp_decode_frame(AVCodecContext
*avctx
,
36 void *data
, int *data_size
,
37 const uint8_t *buf
, int buf_size
)
39 BMPContext
*s
= avctx
->priv_data
;
40 AVFrame
*picture
= data
;
41 AVFrame
*p
= &s
->picture
;
42 unsigned int fsize
, hsize
;
47 int i
, j
, n
, linesize
;
51 const uint8_t *buf0
= buf
;
54 av_log(avctx
, AV_LOG_ERROR
, "buf size too small (%d)\n", buf_size
);
58 if(bytestream_get_byte(&buf
) != 'B' ||
59 bytestream_get_byte(&buf
) != 'M') {
60 av_log(avctx
, AV_LOG_ERROR
, "bad magic number\n");
64 fsize
= bytestream_get_le32(&buf
);
66 av_log(avctx
, AV_LOG_ERROR
, "not enough data (%d < %d)\n",
71 buf
+= 2; /* reserved1 */
72 buf
+= 2; /* reserved2 */
74 hsize
= bytestream_get_le32(&buf
); /* header size */
76 av_log(avctx
, AV_LOG_ERROR
, "not enough data (%d < %d)\n",
81 ihsize
= bytestream_get_le32(&buf
); /* more header size */
82 if(ihsize
+ 14 > hsize
){
83 av_log(avctx
, AV_LOG_ERROR
, "invalid header size %d\n", hsize
);
88 width
= bytestream_get_le32(&buf
);
89 height
= bytestream_get_le32(&buf
);
90 } else if (ihsize
== 12) {
91 width
= bytestream_get_le16(&buf
);
92 height
= bytestream_get_le16(&buf
);
94 av_log(avctx
, AV_LOG_ERROR
, "unsupported BMP file, patch welcome");
98 if(bytestream_get_le16(&buf
) != 1){ /* planes */
99 av_log(avctx
, AV_LOG_ERROR
, "invalid BMP header\n");
103 depth
= bytestream_get_le16(&buf
);
106 comp
= bytestream_get_le32(&buf
);
110 if(comp
!= BMP_RGB
&& comp
!= BMP_BITFIELDS
){
111 av_log(avctx
, AV_LOG_ERROR
, "BMP coding %d not supported\n", comp
);
115 if(comp
== BMP_BITFIELDS
){
117 rgb
[0] = bytestream_get_le32(&buf
);
118 rgb
[1] = bytestream_get_le32(&buf
);
119 rgb
[2] = bytestream_get_le32(&buf
);
122 avctx
->width
= width
;
123 avctx
->height
= height
> 0? height
: -height
;
125 avctx
->pix_fmt
= PIX_FMT_NONE
;
129 if(comp
== BMP_BITFIELDS
){
130 rgb
[0] = (rgb
[0] >> 15) & 3;
131 rgb
[1] = (rgb
[1] >> 15) & 3;
132 rgb
[2] = (rgb
[2] >> 15) & 3;
134 if(rgb
[0] + rgb
[1] + rgb
[2] != 3 ||
135 rgb
[0] == rgb
[1] || rgb
[0] == rgb
[2] || rgb
[1] == rgb
[2]){
144 avctx
->pix_fmt
= PIX_FMT_BGR24
;
147 avctx
->pix_fmt
= PIX_FMT_BGR24
;
151 avctx
->pix_fmt
= PIX_FMT_RGB555
;
154 av_log(avctx
, AV_LOG_ERROR
, "depth %d not supported\n", depth
);
158 if(avctx
->pix_fmt
== PIX_FMT_NONE
){
159 av_log(avctx
, AV_LOG_ERROR
, "unsupported pixel format\n");
164 avctx
->release_buffer(avctx
, p
);
167 if(avctx
->get_buffer(avctx
, p
) < 0){
168 av_log(avctx
, AV_LOG_ERROR
, "get_buffer() failed\n");
171 p
->pict_type
= FF_I_TYPE
;
175 dsize
= buf_size
- hsize
;
177 /* Line size in file multiple of 4 */
178 n
= (avctx
->width
* (depth
/ 8) + 3) & ~3;
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
++){
197 memcpy(ptr
, buf
, avctx
->width
*(depth
>>3));
203 for(i
= 0; i
< avctx
->height
; i
++){
204 const uint16_t *src
= (const 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
++){
216 const uint8_t *src
= buf
;
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 static av_cold
int bmp_decode_end(AVCodecContext
*avctx
)
244 BMPContext
* c
= avctx
->priv_data
;
246 if (c
->picture
.data
[0])
247 avctx
->release_buffer(avctx
, &c
->picture
);
252 AVCodec bmp_decoder
= {
261 .long_name
= NULL_IF_CONFIG_SMALL("BMP image"),