]> git.sesse.net Git - ffmpeg/blob - libavcodec/bmp.c
Merge commit '0c00fd80ee4791bd70b634084307fc9f179e0412'
[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(&s->picture);
31     avctx->coded_frame = &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     uint32_t alpha = 0;
53     uint8_t *ptr;
54     int dsize;
55     const uint8_t *buf0 = buf;
56     GetByteContext gb;
57
58     if(buf_size < 14){
59         av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\n", buf_size);
60         return -1;
61     }
62
63     if(bytestream_get_byte(&buf) != 'B' ||
64        bytestream_get_byte(&buf) != 'M') {
65         av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
66         return -1;
67     }
68
69     fsize = bytestream_get_le32(&buf);
70     if(buf_size < fsize){
71         av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d), trying to decode anyway\n",
72                buf_size, fsize);
73         fsize = buf_size;
74     }
75
76     buf += 2; /* reserved1 */
77     buf += 2; /* reserved2 */
78
79     hsize = bytestream_get_le32(&buf); /* header size */
80     ihsize = bytestream_get_le32(&buf);       /* more header size */
81     if(ihsize + 14 > hsize){
82         av_log(avctx, AV_LOG_ERROR, "invalid header size %d\n", hsize);
83         return -1;
84     }
85
86     /* sometimes file size is set to some headers size, set a real size in that case */
87     if(fsize == 14 || fsize == ihsize + 14)
88         fsize = buf_size - 2;
89
90     if(fsize <= hsize){
91         av_log(avctx, AV_LOG_ERROR, "declared file size is less than header size (%d < %d)\n",
92                fsize, hsize);
93         return -1;
94     }
95
96     switch(ihsize){
97     case  40: // windib
98     case  56: // windib v3
99     case  64: // OS/2 v2
100     case 108: // windib v4
101     case 124: // windib v5
102         width = bytestream_get_le32(&buf);
103         height = bytestream_get_le32(&buf);
104         break;
105     case  12: // OS/2 v1
106         width  = bytestream_get_le16(&buf);
107         height = bytestream_get_le16(&buf);
108         break;
109     default:
110         av_log(avctx, AV_LOG_ERROR, "unsupported BMP file, patch welcome\n");
111         return -1;
112     }
113
114     if(bytestream_get_le16(&buf) != 1){ /* planes */
115         av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n");
116         return -1;
117     }
118
119     depth = bytestream_get_le16(&buf);
120
121     if(ihsize == 40 || ihsize == 64 || ihsize == 56)
122         comp = bytestream_get_le32(&buf);
123     else
124         comp = BMP_RGB;
125
126     if(comp != BMP_RGB && comp != BMP_BITFIELDS && comp != BMP_RLE4 && comp != BMP_RLE8){
127         av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp);
128         return -1;
129     }
130
131     if(comp == BMP_BITFIELDS){
132         buf += 20;
133         rgb[0] = bytestream_get_le32(&buf);
134         rgb[1] = bytestream_get_le32(&buf);
135         rgb[2] = bytestream_get_le32(&buf);
136         if (ihsize >= 108)
137             alpha = bytestream_get_le32(&buf);
138     }
139
140     avctx->width = width;
141     avctx->height = height > 0? height: -height;
142
143     avctx->pix_fmt = PIX_FMT_NONE;
144
145     switch(depth){
146     case 32:
147         if(comp == BMP_BITFIELDS){
148             if (rgb[0] == 0xFF000000 && rgb[1] == 0x00FF0000 && rgb[2] == 0x0000FF00)
149                 avctx->pix_fmt = alpha ? PIX_FMT_ABGR : PIX_FMT_0BGR;
150             else if (rgb[0] == 0x00FF0000 && rgb[1] == 0x0000FF00 && rgb[2] == 0x000000FF)
151                 avctx->pix_fmt = alpha ? PIX_FMT_BGRA : PIX_FMT_BGR0;
152             else if (rgb[0] == 0x0000FF00 && rgb[1] == 0x00FF0000 && rgb[2] == 0xFF000000)
153                 avctx->pix_fmt = alpha ? PIX_FMT_ARGB : PIX_FMT_0RGB;
154             else if (rgb[0] == 0x000000FF && rgb[1] == 0x0000FF00 && rgb[2] == 0x00FF0000)
155                 avctx->pix_fmt = alpha ? PIX_FMT_RGBA : PIX_FMT_RGB0;
156             else {
157                 av_log(avctx, AV_LOG_ERROR, "Unknown bitfields %0X %0X %0X\n", rgb[0], rgb[1], rgb[2]);
158                 return AVERROR(EINVAL);
159             }
160         } else {
161             avctx->pix_fmt = PIX_FMT_BGRA;
162         }
163         break;
164     case 24:
165         avctx->pix_fmt = PIX_FMT_BGR24;
166         break;
167     case 16:
168         if(comp == BMP_RGB)
169             avctx->pix_fmt = PIX_FMT_RGB555;
170         else if (comp == BMP_BITFIELDS) {
171             if (rgb[0] == 0xF800 && rgb[1] == 0x07E0 && rgb[2] == 0x001F)
172                avctx->pix_fmt = PIX_FMT_RGB565;
173             else if (rgb[0] == 0x7C00 && rgb[1] == 0x03E0 && rgb[2] == 0x001F)
174                avctx->pix_fmt = PIX_FMT_RGB555;
175             else if (rgb[0] == 0x0F00 && rgb[1] == 0x00F0 && rgb[2] == 0x000F)
176                avctx->pix_fmt = PIX_FMT_RGB444;
177             else {
178                av_log(avctx, AV_LOG_ERROR, "Unknown bitfields %0X %0X %0X\n", rgb[0], rgb[1], rgb[2]);
179                return AVERROR(EINVAL);
180             }
181         }
182         break;
183     case 8:
184         if(hsize - ihsize - 14 > 0)
185             avctx->pix_fmt = PIX_FMT_PAL8;
186         else
187             avctx->pix_fmt = PIX_FMT_GRAY8;
188         break;
189     case 1:
190     case 4:
191         if(hsize - ihsize - 14 > 0){
192             avctx->pix_fmt = PIX_FMT_PAL8;
193         }else{
194             av_log(avctx, AV_LOG_ERROR, "Unknown palette for %d-colour BMP\n", 1<<depth);
195             return -1;
196         }
197         break;
198     default:
199         av_log(avctx, AV_LOG_ERROR, "depth %d not supported\n", depth);
200         return -1;
201     }
202
203     if(avctx->pix_fmt == PIX_FMT_NONE){
204         av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
205         return -1;
206     }
207
208     if(p->data[0])
209         avctx->release_buffer(avctx, p);
210
211     p->reference = 0;
212     if(avctx->get_buffer(avctx, p) < 0){
213         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
214         return -1;
215     }
216     p->pict_type = AV_PICTURE_TYPE_I;
217     p->key_frame = 1;
218
219     buf = buf0 + hsize;
220     dsize = buf_size - hsize;
221
222     /* Line size in file multiple of 4 */
223     n = ((avctx->width * depth + 31) / 8) & ~3;
224
225     if(n * avctx->height > dsize && comp != BMP_RLE4 && comp != BMP_RLE8){
226         av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
227                dsize, n * avctx->height);
228         return -1;
229     }
230
231     // RLE may skip decoding some picture areas, so blank picture before decoding
232     if(comp == BMP_RLE4 || comp == BMP_RLE8)
233         memset(p->data[0], 0, avctx->height * p->linesize[0]);
234
235     if(depth == 4 || depth == 8)
236         memset(p->data[1], 0, 1024);
237
238     if(height > 0){
239         ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
240         linesize = -p->linesize[0];
241     } else {
242         ptr = p->data[0];
243         linesize = p->linesize[0];
244     }
245
246     if(avctx->pix_fmt == PIX_FMT_PAL8){
247         int colors = 1 << depth;
248         if(ihsize >= 36){
249             int t;
250             buf = buf0 + 46;
251             t = bytestream_get_le32(&buf);
252             if(t < 0 || t > (1 << depth)){
253                 av_log(avctx, AV_LOG_ERROR, "Incorrect number of colors - %X for bitdepth %d\n", t, depth);
254             }else if(t){
255                 colors = t;
256             }
257         }
258         buf = buf0 + 14 + ihsize; //palette location
259         if((hsize-ihsize-14) < (colors << 2)){ // OS/2 bitmap, 3 bytes per palette entry
260             for(i = 0; i < colors; i++)
261                 ((uint32_t*)p->data[1])[i] = (0xff<<24) | bytestream_get_le24(&buf);
262         }else{
263             for(i = 0; i < colors; i++)
264                 ((uint32_t*)p->data[1])[i] = 0xFFU << 24 | bytestream_get_le32(&buf);
265         }
266         buf = buf0 + hsize;
267     }
268     if(comp == BMP_RLE4 || comp == BMP_RLE8){
269         if(height < 0){
270             p->data[0] += p->linesize[0] * (avctx->height - 1);
271             p->linesize[0] = -p->linesize[0];
272         }
273         bytestream2_init(&gb, buf, dsize);
274         ff_msrle_decode(avctx, (AVPicture*)p, depth, &gb);
275         if(height < 0){
276             p->data[0] += p->linesize[0] * (avctx->height - 1);
277             p->linesize[0] = -p->linesize[0];
278         }
279     }else{
280         switch(depth){
281         case 1:
282             for (i = 0; i < avctx->height; i++) {
283                 int j;
284                 for (j = 0; j < n; j++) {
285                     ptr[j*8+0] =  buf[j] >> 7;
286                     ptr[j*8+1] = (buf[j] >> 6) & 1;
287                     ptr[j*8+2] = (buf[j] >> 5) & 1;
288                     ptr[j*8+3] = (buf[j] >> 4) & 1;
289                     ptr[j*8+4] = (buf[j] >> 3) & 1;
290                     ptr[j*8+5] = (buf[j] >> 2) & 1;
291                     ptr[j*8+6] = (buf[j] >> 1) & 1;
292                     ptr[j*8+7] =  buf[j]       & 1;
293                 }
294                 buf += n;
295                 ptr += linesize;
296             }
297             break;
298         case 8:
299         case 24:
300         case 32:
301             for(i = 0; i < avctx->height; i++){
302                 memcpy(ptr, buf, n);
303                 buf += n;
304                 ptr += linesize;
305             }
306             break;
307         case 4:
308             for(i = 0; i < avctx->height; i++){
309                 int j;
310                 for(j = 0; j < n; j++){
311                     ptr[j*2+0] = (buf[j] >> 4) & 0xF;
312                     ptr[j*2+1] = buf[j] & 0xF;
313                 }
314                 buf += n;
315                 ptr += linesize;
316             }
317             break;
318         case 16:
319             for(i = 0; i < avctx->height; i++){
320                 const uint16_t *src = (const uint16_t *) buf;
321                 uint16_t *dst = (uint16_t *) ptr;
322
323                 for(j = 0; j < avctx->width; j++)
324                     *dst++ = av_le2ne16(*src++);
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             = AV_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 (Windows and OS/2 bitmap)"),
362 };