]> git.sesse.net Git - ffmpeg/blob - libavcodec/bmp.c
Merge remote-tracking branch 'qatar/master'
[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                             AVPacket *avpkt)
39 {
40     const uint8_t *buf = avpkt->data;
41     int buf_size = avpkt->size;
42     BMPContext *s = avctx->priv_data;
43     AVFrame *picture = data;
44     AVFrame *p = &s->picture;
45     unsigned int fsize, hsize;
46     int width, height;
47     unsigned int depth;
48     BiCompression comp;
49     unsigned int ihsize;
50     int i, j, n, linesize;
51     uint32_t rgb[3];
52     uint8_t *ptr;
53     int dsize;
54     const uint8_t *buf0 = buf;
55
56     if(buf_size < 14){
57         av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\n", buf_size);
58         return -1;
59     }
60
61     if(bytestream_get_byte(&buf) != 'B' ||
62        bytestream_get_byte(&buf) != 'M') {
63         av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
64         return -1;
65     }
66
67     fsize = bytestream_get_le32(&buf);
68     if(buf_size < fsize){
69         av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d), trying to decode anyway\n",
70                buf_size, fsize);
71         fsize = buf_size;
72     }
73
74     buf += 2; /* reserved1 */
75     buf += 2; /* reserved2 */
76
77     hsize = bytestream_get_le32(&buf); /* header size */
78     ihsize = bytestream_get_le32(&buf);       /* more header size */
79     if(ihsize + 14 > hsize){
80         av_log(avctx, AV_LOG_ERROR, "invalid header size %d\n", hsize);
81         return -1;
82     }
83
84     /* sometimes file size is set to some headers size, set a real size in that case */
85     if(fsize == 14 || fsize == ihsize + 14)
86         fsize = buf_size - 2;
87
88     if(fsize <= hsize){
89         av_log(avctx, AV_LOG_ERROR, "declared file size is less than header size (%d < %d)\n",
90                fsize, hsize);
91         return -1;
92     }
93
94     switch(ihsize){
95     case  40: // windib v3
96     case  64: // OS/2 v2
97     case 108: // windib v4
98     case 124: // windib v5
99         width = bytestream_get_le32(&buf);
100         height = bytestream_get_le32(&buf);
101         break;
102     case  12: // OS/2 v1
103         width  = bytestream_get_le16(&buf);
104         height = bytestream_get_le16(&buf);
105         break;
106     default:
107         av_log(avctx, AV_LOG_ERROR, "unsupported BMP file, patch welcome\n");
108         return -1;
109     }
110
111     if(bytestream_get_le16(&buf) != 1){ /* planes */
112         av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n");
113         return -1;
114     }
115
116     depth = bytestream_get_le16(&buf);
117
118     if(ihsize == 40)
119         comp = bytestream_get_le32(&buf);
120     else
121         comp = BMP_RGB;
122
123     if(comp != BMP_RGB && comp != BMP_BITFIELDS && comp != BMP_RLE4 && comp != BMP_RLE8){
124         av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp);
125         return -1;
126     }
127
128     if(comp == BMP_BITFIELDS){
129         buf += 20;
130         rgb[0] = bytestream_get_le32(&buf);
131         rgb[1] = bytestream_get_le32(&buf);
132         rgb[2] = bytestream_get_le32(&buf);
133     }
134
135     avctx->width = width;
136     avctx->height = height > 0? height: -height;
137
138     avctx->pix_fmt = PIX_FMT_NONE;
139
140     switch(depth){
141     case 32:
142         if(comp == BMP_BITFIELDS){
143             rgb[0] = (rgb[0] >> 15) & 3;
144             rgb[1] = (rgb[1] >> 15) & 3;
145             rgb[2] = (rgb[2] >> 15) & 3;
146
147             if(rgb[0] + rgb[1] + rgb[2] != 3 ||
148                rgb[0] == rgb[1] || rgb[0] == rgb[2] || rgb[1] == rgb[2]){
149                 break;
150             }
151         } else {
152             rgb[0] = 2;
153             rgb[1] = 1;
154             rgb[2] = 0;
155         }
156
157         avctx->pix_fmt = PIX_FMT_BGR24;
158         break;
159     case 24:
160         avctx->pix_fmt = PIX_FMT_BGR24;
161         break;
162     case 16:
163         if(comp == BMP_RGB)
164             avctx->pix_fmt = PIX_FMT_RGB555;
165         if(comp == BMP_BITFIELDS)
166             avctx->pix_fmt = rgb[1] == 0x07E0 ? PIX_FMT_RGB565 : PIX_FMT_RGB555;
167         break;
168     case 8:
169         if(hsize - ihsize - 14 > 0)
170             avctx->pix_fmt = PIX_FMT_PAL8;
171         else
172             avctx->pix_fmt = PIX_FMT_GRAY8;
173         break;
174     case 1:
175     case 4:
176         if(hsize - ihsize - 14 > 0){
177             avctx->pix_fmt = PIX_FMT_PAL8;
178         }else{
179             av_log(avctx, AV_LOG_ERROR, "Unknown palette for %d-colour BMP\n", 1<<depth);
180             return -1;
181         }
182         break;
183     default:
184         av_log(avctx, AV_LOG_ERROR, "depth %d not supported\n", depth);
185         return -1;
186     }
187
188     if(avctx->pix_fmt == PIX_FMT_NONE){
189         av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
190         return -1;
191     }
192
193     if(p->data[0])
194         avctx->release_buffer(avctx, p);
195
196     p->reference = 0;
197     if(avctx->get_buffer(avctx, p) < 0){
198         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
199         return -1;
200     }
201     p->pict_type = AV_PICTURE_TYPE_I;
202     p->key_frame = 1;
203
204     buf = buf0 + hsize;
205     dsize = buf_size - hsize;
206
207     /* Line size in file multiple of 4 */
208     n = ((avctx->width * depth) / 8 + 3) & ~3;
209
210     if(n * avctx->height > dsize && comp != BMP_RLE4 && comp != BMP_RLE8){
211         av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
212                dsize, n * avctx->height);
213         return -1;
214     }
215
216     // RLE may skip decoding some picture areas, so blank picture before decoding
217     if(comp == BMP_RLE4 || comp == BMP_RLE8)
218         memset(p->data[0], 0, avctx->height * p->linesize[0]);
219
220     if(depth == 4 || depth == 8)
221         memset(p->data[1], 0, 1024);
222
223     if(height > 0){
224         ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
225         linesize = -p->linesize[0];
226     } else {
227         ptr = p->data[0];
228         linesize = p->linesize[0];
229     }
230
231     if(avctx->pix_fmt == PIX_FMT_PAL8){
232         int colors = 1 << depth;
233         if(ihsize >= 36){
234             int t;
235             buf = buf0 + 46;
236             t = bytestream_get_le32(&buf);
237             if(t < 0 || t > (1 << depth)){
238                 av_log(avctx, AV_LOG_ERROR, "Incorrect number of colors - %X for bitdepth %d\n", t, depth);
239             }else if(t){
240                 colors = t;
241             }
242         }
243         buf = buf0 + 14 + ihsize; //palette location
244         if((hsize-ihsize-14) < (colors << 2)){ // OS/2 bitmap, 3 bytes per palette entry
245             for(i = 0; i < colors; i++)
246                 ((uint32_t*)p->data[1])[i] = (0xff<<24) | bytestream_get_le24(&buf);
247         }else{
248             for(i = 0; i < colors; i++)
249                 ((uint32_t*)p->data[1])[i] = bytestream_get_le32(&buf);
250         }
251         buf = buf0 + hsize;
252     }
253     if(comp == BMP_RLE4 || comp == BMP_RLE8){
254         if(height < 0){
255             p->data[0] += p->linesize[0] * (avctx->height - 1);
256             p->linesize[0] = -p->linesize[0];
257         }
258         ff_msrle_decode(avctx, (AVPicture*)p, depth, buf, dsize);
259         if(height < 0){
260             p->data[0] += p->linesize[0] * (avctx->height - 1);
261             p->linesize[0] = -p->linesize[0];
262         }
263     }else{
264         switch(depth){
265         case 1:
266             for(i = 0; i < avctx->height; i++){
267                 int j;
268                 for(j = 0; j < n; j++){
269                     ptr[j*8+0] =  buf[j] >> 7;
270                     ptr[j*8+1] = (buf[j] >> 6) & 1;
271                     ptr[j*8+2] = (buf[j] >> 5) & 1;
272                     ptr[j*8+3] = (buf[j] >> 4) & 1;
273                     ptr[j*8+4] = (buf[j] >> 3) & 1;
274                     ptr[j*8+5] = (buf[j] >> 2) & 1;
275                     ptr[j*8+6] = (buf[j] >> 1) & 1;
276                     ptr[j*8+7] =  buf[j]       & 1;
277                 }
278                 buf += n;
279                 ptr += linesize;
280             }
281             break;
282         case 8:
283         case 24:
284             for(i = 0; i < avctx->height; i++){
285                 memcpy(ptr, buf, n);
286                 buf += n;
287                 ptr += linesize;
288             }
289             break;
290         case 4:
291             for(i = 0; i < avctx->height; i++){
292                 int j;
293                 for(j = 0; j < n; j++){
294                     ptr[j*2+0] = (buf[j] >> 4) & 0xF;
295                     ptr[j*2+1] = buf[j] & 0xF;
296                 }
297                 buf += n;
298                 ptr += linesize;
299             }
300             break;
301         case 16:
302             for(i = 0; i < avctx->height; i++){
303                 const uint16_t *src = (const uint16_t *) buf;
304                 uint16_t *dst = (uint16_t *) ptr;
305
306                 for(j = 0; j < avctx->width; j++)
307                     *dst++ = av_le2ne16(*src++);
308
309                 buf += n;
310                 ptr += linesize;
311             }
312             break;
313         case 32:
314             for(i = 0; i < avctx->height; i++){
315                 const uint8_t *src = buf;
316                 uint8_t *dst = ptr;
317
318                 for(j = 0; j < avctx->width; j++){
319                     dst[0] = src[rgb[2]];
320                     dst[1] = src[rgb[1]];
321                     dst[2] = src[rgb[0]];
322                     dst += 3;
323                     src += 4;
324                 }
325
326                 buf += n;
327                 ptr += linesize;
328             }
329             break;
330         default:
331             av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
332             return -1;
333         }
334     }
335
336     *picture = s->picture;
337     *data_size = sizeof(AVPicture);
338
339     return buf_size;
340 }
341
342 static av_cold int bmp_decode_end(AVCodecContext *avctx)
343 {
344     BMPContext* c = avctx->priv_data;
345
346     if (c->picture.data[0])
347         avctx->release_buffer(avctx, &c->picture);
348
349     return 0;
350 }
351
352 AVCodec ff_bmp_decoder = {
353     .name           = "bmp",
354     .type           = AVMEDIA_TYPE_VIDEO,
355     .id             = CODEC_ID_BMP,
356     .priv_data_size = sizeof(BMPContext),
357     .init           = bmp_decode_init,
358     .close          = bmp_decode_end,
359     .decode         = bmp_decode_frame,
360     .capabilities   = CODEC_CAP_DR1,
361     .long_name = NULL_IF_CONFIG_SMALL("BMP image"),
362 };