]> 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
96     case  56: // windib v3
97     case  64: // OS/2 v2
98     case 108: // windib v4
99     case 124: // windib v5
100         width = bytestream_get_le32(&buf);
101         height = bytestream_get_le32(&buf);
102         break;
103     case  12: // OS/2 v1
104         width  = bytestream_get_le16(&buf);
105         height = bytestream_get_le16(&buf);
106         break;
107     default:
108         av_log(avctx, AV_LOG_ERROR, "unsupported BMP file, patch welcome\n");
109         return -1;
110     }
111
112     if(bytestream_get_le16(&buf) != 1){ /* planes */
113         av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n");
114         return -1;
115     }
116
117     depth = bytestream_get_le16(&buf);
118
119     if(ihsize == 40 || ihsize == 64 || ihsize == 56)
120         comp = bytestream_get_le32(&buf);
121     else
122         comp = BMP_RGB;
123
124     if(comp != BMP_RGB && comp != BMP_BITFIELDS && comp != BMP_RLE4 && comp != BMP_RLE8){
125         av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp);
126         return -1;
127     }
128
129     if(comp == BMP_BITFIELDS){
130         buf += 20;
131         rgb[0] = bytestream_get_le32(&buf);
132         rgb[1] = bytestream_get_le32(&buf);
133         rgb[2] = bytestream_get_le32(&buf);
134     }
135
136     avctx->width = width;
137     avctx->height = height > 0? height: -height;
138
139     avctx->pix_fmt = PIX_FMT_NONE;
140
141     switch(depth){
142     case 32:
143         if(comp == BMP_BITFIELDS){
144             rgb[0] = (rgb[0] >> 15) & 3;
145             rgb[1] = (rgb[1] >> 15) & 3;
146             rgb[2] = (rgb[2] >> 15) & 3;
147
148             if(rgb[0] + rgb[1] + rgb[2] != 3 ||
149                rgb[0] == rgb[1] || rgb[0] == rgb[2] || rgb[1] == rgb[2]){
150                 break;
151             }
152         } else {
153             rgb[0] = 2;
154             rgb[1] = 1;
155             rgb[2] = 0;
156         }
157
158         avctx->pix_fmt = PIX_FMT_BGRA;
159         break;
160     case 24:
161         avctx->pix_fmt = PIX_FMT_BGR24;
162         break;
163     case 16:
164         if(comp == BMP_RGB)
165             avctx->pix_fmt = PIX_FMT_RGB555;
166         if(comp == BMP_BITFIELDS)
167             avctx->pix_fmt = rgb[1] == 0x07E0 ? PIX_FMT_RGB565 : PIX_FMT_RGB555;
168         break;
169     case 8:
170         if(hsize - ihsize - 14 > 0)
171             avctx->pix_fmt = PIX_FMT_PAL8;
172         else
173             avctx->pix_fmt = PIX_FMT_GRAY8;
174         break;
175     case 1:
176     case 4:
177         if(hsize - ihsize - 14 > 0){
178             avctx->pix_fmt = PIX_FMT_PAL8;
179         }else{
180             av_log(avctx, AV_LOG_ERROR, "Unknown palette for %d-colour BMP\n", 1<<depth);
181             return -1;
182         }
183         break;
184     default:
185         av_log(avctx, AV_LOG_ERROR, "depth %d not supported\n", depth);
186         return -1;
187     }
188
189     if(avctx->pix_fmt == PIX_FMT_NONE){
190         av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
191         return -1;
192     }
193
194     if(p->data[0])
195         avctx->release_buffer(avctx, p);
196
197     p->reference = 0;
198     if(avctx->get_buffer(avctx, p) < 0){
199         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
200         return -1;
201     }
202     p->pict_type = AV_PICTURE_TYPE_I;
203     p->key_frame = 1;
204
205     buf = buf0 + hsize;
206     dsize = buf_size - hsize;
207
208     /* Line size in file multiple of 4 */
209     n = ((avctx->width * depth + 31) / 8) & ~3;
210
211     if(n * avctx->height > dsize && comp != BMP_RLE4 && comp != BMP_RLE8){
212         av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
213                dsize, n * avctx->height);
214         return -1;
215     }
216
217     // RLE may skip decoding some picture areas, so blank picture before decoding
218     if(comp == BMP_RLE4 || comp == BMP_RLE8)
219         memset(p->data[0], 0, avctx->height * p->linesize[0]);
220
221     if(depth == 4 || depth == 8)
222         memset(p->data[1], 0, 1024);
223
224     if(height > 0){
225         ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
226         linesize = -p->linesize[0];
227     } else {
228         ptr = p->data[0];
229         linesize = p->linesize[0];
230     }
231
232     if(avctx->pix_fmt == PIX_FMT_PAL8){
233         int colors = 1 << depth;
234         if(ihsize >= 36){
235             int t;
236             buf = buf0 + 46;
237             t = bytestream_get_le32(&buf);
238             if(t < 0 || t > (1 << depth)){
239                 av_log(avctx, AV_LOG_ERROR, "Incorrect number of colors - %X for bitdepth %d\n", t, depth);
240             }else if(t){
241                 colors = t;
242             }
243         }
244         buf = buf0 + 14 + ihsize; //palette location
245         if((hsize-ihsize-14) < (colors << 2)){ // OS/2 bitmap, 3 bytes per palette entry
246             for(i = 0; i < colors; i++)
247                 ((uint32_t*)p->data[1])[i] = (0xff<<24) | bytestream_get_le24(&buf);
248         }else{
249             for(i = 0; i < colors; i++)
250                 ((uint32_t*)p->data[1])[i] = bytestream_get_le32(&buf);
251         }
252         buf = buf0 + hsize;
253     }
254     if(comp == BMP_RLE4 || comp == BMP_RLE8){
255         if(height < 0){
256             p->data[0] += p->linesize[0] * (avctx->height - 1);
257             p->linesize[0] = -p->linesize[0];
258         }
259         ff_msrle_decode(avctx, (AVPicture*)p, depth, buf, dsize);
260         if(height < 0){
261             p->data[0] += p->linesize[0] * (avctx->height - 1);
262             p->linesize[0] = -p->linesize[0];
263         }
264     }else{
265         switch(depth){
266         case 1:
267             for (i = 0; i < avctx->height; i++) {
268                 int j;
269                 for (j = 0; j < n; j++) {
270                     ptr[j*8+0] =  buf[j] >> 7;
271                     ptr[j*8+1] = (buf[j] >> 6) & 1;
272                     ptr[j*8+2] = (buf[j] >> 5) & 1;
273                     ptr[j*8+3] = (buf[j] >> 4) & 1;
274                     ptr[j*8+4] = (buf[j] >> 3) & 1;
275                     ptr[j*8+5] = (buf[j] >> 2) & 1;
276                     ptr[j*8+6] = (buf[j] >> 1) & 1;
277                     ptr[j*8+7] =  buf[j]       & 1;
278                 }
279                 buf += n;
280                 ptr += linesize;
281             }
282             break;
283         case 8:
284         case 24:
285             for(i = 0; i < avctx->height; i++){
286                 memcpy(ptr, buf, n);
287                 buf += n;
288                 ptr += linesize;
289             }
290             break;
291         case 4:
292             for(i = 0; i < avctx->height; i++){
293                 int j;
294                 for(j = 0; j < n; j++){
295                     ptr[j*2+0] = (buf[j] >> 4) & 0xF;
296                     ptr[j*2+1] = buf[j] & 0xF;
297                 }
298                 buf += n;
299                 ptr += linesize;
300             }
301             break;
302         case 16:
303             for(i = 0; i < avctx->height; i++){
304                 const uint16_t *src = (const uint16_t *) buf;
305                 uint16_t *dst = (uint16_t *) ptr;
306
307                 for(j = 0; j < avctx->width; j++)
308                     *dst++ = av_le2ne16(*src++);
309
310                 buf += n;
311                 ptr += linesize;
312             }
313             break;
314         case 32:
315             for(i = 0; i < avctx->height; i++){
316                 const uint8_t *src = buf;
317                 uint8_t *dst = ptr;
318
319                 for(j = 0; j < avctx->width; j++){
320                     dst[0] = src[rgb[2]];
321                     dst[1] = src[rgb[1]];
322                     dst[2] = src[rgb[0]];
323 /* The Microsoft documentation states:
324  * "The high byte in each DWORD is not used."
325  * Both GIMP and ImageMagick store the alpha transparency value
326  * in the high byte for 32bit bmp files.
327  */
328                     dst[3] = src[3];
329                     dst += 4;
330                     src += 4;
331                 }
332
333                 buf += n;
334                 ptr += linesize;
335             }
336             break;
337         default:
338             av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
339             return -1;
340         }
341     }
342
343     *picture = s->picture;
344     *data_size = sizeof(AVPicture);
345
346     return buf_size;
347 }
348
349 static av_cold int bmp_decode_end(AVCodecContext *avctx)
350 {
351     BMPContext* c = avctx->priv_data;
352
353     if (c->picture.data[0])
354         avctx->release_buffer(avctx, &c->picture);
355
356     return 0;
357 }
358
359 AVCodec ff_bmp_decoder = {
360     .name           = "bmp",
361     .type           = AVMEDIA_TYPE_VIDEO,
362     .id             = CODEC_ID_BMP,
363     .priv_data_size = sizeof(BMPContext),
364     .init           = bmp_decode_init,
365     .close          = bmp_decode_end,
366     .decode         = bmp_decode_frame,
367     .capabilities   = CODEC_CAP_DR1,
368     .long_name = NULL_IF_CONFIG_SMALL("BMP image"),
369 };