]> git.sesse.net Git - ffmpeg/blob - libavcodec/bmp.c
ff_parse_close() is not the correct function for H264Context.
[ffmpeg] / libavcodec / bmp.c
1 /*
2  * BMP image format decoder
3  * Copyright (c) 2005 Mans Rullgard
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "avcodec.h"
23 #include "bytestream.h"
24 #include "bmp.h"
25 #include "msrledec.h"
26
27 static av_cold int bmp_decode_init(AVCodecContext *avctx){
28     BMPContext *s = avctx->priv_data;
29
30     avcodec_get_frame_defaults((AVFrame*)&s->picture);
31     avctx->coded_frame = (AVFrame*)&s->picture;
32
33     return 0;
34 }
35
36 static int bmp_decode_frame(AVCodecContext *avctx,
37                             void *data, int *data_size,
38                             const uint8_t *buf, int buf_size)
39 {
40     BMPContext *s = avctx->priv_data;
41     AVFrame *picture = data;
42     AVFrame *p = &s->picture;
43     unsigned int fsize, hsize;
44     int width, height;
45     unsigned int depth;
46     BiCompression comp;
47     unsigned int ihsize;
48     int i, j, n, linesize;
49     uint32_t rgb[3];
50     uint8_t *ptr;
51     int dsize;
52     const uint8_t *buf0 = buf;
53
54     if(buf_size < 14){
55         av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\n", buf_size);
56         return -1;
57     }
58
59     if(bytestream_get_byte(&buf) != 'B' ||
60        bytestream_get_byte(&buf) != 'M') {
61         av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
62         return -1;
63     }
64
65     fsize = bytestream_get_le32(&buf);
66     if(buf_size < fsize){
67         av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
68                buf_size, fsize);
69         return -1;
70     }
71
72     buf += 2; /* reserved1 */
73     buf += 2; /* reserved2 */
74
75     hsize = bytestream_get_le32(&buf); /* header size */
76     if(fsize <= hsize){
77         av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
78                fsize, hsize);
79         return -1;
80     }
81
82     ihsize = bytestream_get_le32(&buf);       /* more header size */
83     if(ihsize + 14 > hsize){
84         av_log(avctx, AV_LOG_ERROR, "invalid header size %d\n", hsize);
85         return -1;
86     }
87
88     if (ihsize == 40) {
89         width = bytestream_get_le32(&buf);
90         height = bytestream_get_le32(&buf);
91     } else if (ihsize == 12) {
92         width  = bytestream_get_le16(&buf);
93         height = bytestream_get_le16(&buf);
94     } else {
95         av_log(avctx, AV_LOG_ERROR, "unsupported BMP file, patch welcome\n");
96         return -1;
97     }
98
99     if(bytestream_get_le16(&buf) != 1){ /* planes */
100         av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n");
101         return -1;
102     }
103
104     depth = bytestream_get_le16(&buf);
105
106     if(ihsize == 40)
107         comp = bytestream_get_le32(&buf);
108     else
109         comp = BMP_RGB;
110
111     if(comp != BMP_RGB && comp != BMP_BITFIELDS && comp != BMP_RLE4 && comp != BMP_RLE8){
112         av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp);
113         return -1;
114     }
115
116     if(comp == BMP_BITFIELDS){
117         buf += 20;
118         rgb[0] = bytestream_get_le32(&buf);
119         rgb[1] = bytestream_get_le32(&buf);
120         rgb[2] = bytestream_get_le32(&buf);
121     }
122
123     avctx->width = width;
124     avctx->height = height > 0? height: -height;
125
126     avctx->pix_fmt = PIX_FMT_NONE;
127
128     switch(depth){
129     case 32:
130         if(comp == BMP_BITFIELDS){
131             rgb[0] = (rgb[0] >> 15) & 3;
132             rgb[1] = (rgb[1] >> 15) & 3;
133             rgb[2] = (rgb[2] >> 15) & 3;
134
135             if(rgb[0] + rgb[1] + rgb[2] != 3 ||
136                rgb[0] == rgb[1] || rgb[0] == rgb[2] || rgb[1] == rgb[2]){
137                 break;
138             }
139         } else {
140             rgb[0] = 2;
141             rgb[1] = 1;
142             rgb[2] = 0;
143         }
144
145         avctx->pix_fmt = PIX_FMT_BGR24;
146         break;
147     case 24:
148         avctx->pix_fmt = PIX_FMT_BGR24;
149         break;
150     case 16:
151         if(comp == BMP_RGB)
152             avctx->pix_fmt = PIX_FMT_RGB555;
153         if(comp == BMP_BITFIELDS)
154             avctx->pix_fmt = rgb[1] == 0x07E0 ? PIX_FMT_RGB565 : PIX_FMT_RGB555;
155         break;
156     case 8:
157         if(hsize - ihsize - 14 > 0)
158             avctx->pix_fmt = PIX_FMT_PAL8;
159         else
160             avctx->pix_fmt = PIX_FMT_GRAY8;
161         break;
162     case 4:
163         if(hsize - ihsize - 14 > 0){
164             avctx->pix_fmt = PIX_FMT_PAL8;
165         }else{
166             av_log(avctx, AV_LOG_ERROR, "Unknown palette for 16-colour BMP\n");
167             return -1;
168         }
169         break;
170     case 1:
171         avctx->pix_fmt = PIX_FMT_MONOBLACK;
172         break;
173     default:
174         av_log(avctx, AV_LOG_ERROR, "depth %d not supported\n", depth);
175         return -1;
176     }
177
178     if(avctx->pix_fmt == PIX_FMT_NONE){
179         av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
180         return -1;
181     }
182
183     if(p->data[0])
184         avctx->release_buffer(avctx, p);
185
186     p->reference = 0;
187     if(avctx->get_buffer(avctx, p) < 0){
188         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
189         return -1;
190     }
191     p->pict_type = FF_I_TYPE;
192     p->key_frame = 1;
193
194     buf = buf0 + hsize;
195     dsize = buf_size - hsize;
196
197     /* Line size in file multiple of 4 */
198     n = ((avctx->width * depth) / 8 + 3) & ~3;
199
200     if(n * avctx->height > dsize && comp != BMP_RLE4 && comp != BMP_RLE8){
201         av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
202                dsize, n * avctx->height);
203         return -1;
204     }
205
206     // RLE may skip decoding some picture areas, so blank picture before decoding
207     if(comp == BMP_RLE4 || comp == BMP_RLE8)
208         memset(p->data[0], 0, avctx->height * p->linesize[0]);
209
210     if(depth == 4 || depth == 8)
211         memset(p->data[1], 0, 1024);
212
213     if(height > 0){
214         ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
215         linesize = -p->linesize[0];
216     } else {
217         ptr = p->data[0];
218         linesize = p->linesize[0];
219     }
220
221     if(avctx->pix_fmt == PIX_FMT_PAL8){
222         buf = buf0 + 14 + ihsize; //palette location
223         if((hsize-ihsize-14)>>depth < 4){ // OS/2 bitmap, 3 bytes per palette entry
224             for(i = 0; i < (1 << depth); i++)
225                 ((uint32_t*)p->data[1])[i] = bytestream_get_le24(&buf);
226         }else{
227             for(i = 0; i < (1 << depth); i++)
228                 ((uint32_t*)p->data[1])[i] = bytestream_get_le32(&buf);
229         }
230         buf = buf0 + hsize;
231     }
232     if(comp == BMP_RLE4 || comp == BMP_RLE8){
233         ff_msrle_decode(avctx, p, depth, buf, dsize);
234     }else{
235         switch(depth){
236         case 1:
237             for(i = 0; i < avctx->height; i++){
238                 memcpy(ptr, buf, n);
239                 buf += n;
240                 ptr += linesize;
241             }
242             break;
243         case 4:
244             for(i = 0; i < avctx->height; i++){
245                 int j;
246                 for(j = 0; j < n; j++){
247                     ptr[j*2+0] = (buf[j] >> 4) & 0xF;
248                     ptr[j*2+1] = buf[j] & 0xF;
249                 }
250                 buf += n;
251                 ptr += linesize;
252             }
253             break;
254         case 8:
255             for(i = 0; i < avctx->height; i++){
256                 memcpy(ptr, buf, avctx->width);
257                 buf += n;
258                 ptr += linesize;
259             }
260             break;
261         case 24:
262             for(i = 0; i < avctx->height; i++){
263                 memcpy(ptr, buf, avctx->width*(depth>>3));
264                 buf += n;
265                 ptr += linesize;
266             }
267             break;
268         case 16:
269             for(i = 0; i < avctx->height; i++){
270                 const uint16_t *src = (const uint16_t *) buf;
271                 uint16_t *dst = (uint16_t *) ptr;
272
273                 for(j = 0; j < avctx->width; j++)
274                     *dst++ = le2me_16(*src++);
275
276                 buf += n;
277                 ptr += linesize;
278             }
279             break;
280         case 32:
281             for(i = 0; i < avctx->height; i++){
282                 const uint8_t *src = buf;
283                 uint8_t *dst = ptr;
284
285                 for(j = 0; j < avctx->width; j++){
286                     dst[0] = src[rgb[2]];
287                     dst[1] = src[rgb[1]];
288                     dst[2] = src[rgb[0]];
289                     dst += 3;
290                     src += 4;
291                 }
292
293                 buf += n;
294                 ptr += linesize;
295             }
296             break;
297         default:
298             av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
299             return -1;
300         }
301     }
302
303     *picture = s->picture;
304     *data_size = sizeof(AVPicture);
305
306     return buf_size;
307 }
308
309 static av_cold int bmp_decode_end(AVCodecContext *avctx)
310 {
311     BMPContext* c = avctx->priv_data;
312
313     if (c->picture.data[0])
314         avctx->release_buffer(avctx, &c->picture);
315
316     return 0;
317 }
318
319 AVCodec bmp_decoder = {
320     "bmp",
321     CODEC_TYPE_VIDEO,
322     CODEC_ID_BMP,
323     sizeof(BMPContext),
324     bmp_decode_init,
325     NULL,
326     bmp_decode_end,
327     bmp_decode_frame,
328     .long_name = NULL_IF_CONFIG_SMALL("BMP image"),
329 };