1/*
2 * BMP image format decoder
3 * Copyright (c) 2005 Mans Rullgard
4 *
5 * This file is part of Libav.
6 *
7 * Libav 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 * Libav 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 Libav; 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
27static 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
36static 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    GetByteContext gb;
56
57    if(buf_size < 14){
58        av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\n", buf_size);
59        return -1;
60    }
61
62    if(bytestream_get_byte(&buf) != 'B' ||
63       bytestream_get_byte(&buf) != 'M') {
64        av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
65        return -1;
66    }
67
68    fsize = bytestream_get_le32(&buf);
69    if(buf_size < fsize){
70        av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d), trying to decode anyway\n",
71               buf_size, fsize);
72        fsize = buf_size;
73    }
74
75    buf += 2; /* reserved1 */
76    buf += 2; /* reserved2 */
77
78    hsize = bytestream_get_le32(&buf); /* header size */
79    ihsize = bytestream_get_le32(&buf);       /* more header size */
80    if(ihsize + 14 > hsize){
81        av_log(avctx, AV_LOG_ERROR, "invalid header size %d\n", hsize);
82        return -1;
83    }
84
85    /* sometimes file size is set to some headers size, set a real size in that case */
86    if(fsize == 14 || fsize == ihsize + 14)
87        fsize = buf_size - 2;
88
89    if(fsize <= hsize){
90        av_log(avctx, AV_LOG_ERROR, "declared file size is less than header size (%d < %d)\n",
91               fsize, hsize);
92        return -1;
93    }
94
95    switch(ihsize){
96    case  40: // 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)
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_BGR24;
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        else if (comp == BMP_BITFIELDS) {
167            if (rgb[0] == 0xF800 && rgb[1] == 0x07E0 && rgb[2] == 0x001F)
168               avctx->pix_fmt = PIX_FMT_RGB565;
169            else if (rgb[0] == 0x7C00 && rgb[1] == 0x03E0 && rgb[2] == 0x001F)
170               avctx->pix_fmt = PIX_FMT_RGB555;
171            else if (rgb[0] == 0x0F00 && rgb[1] == 0x00F0 && rgb[2] == 0x000F)
172               avctx->pix_fmt = PIX_FMT_RGB444;
173            else {
174               av_log(avctx, AV_LOG_ERROR, "Unknown bitfields %0X %0X %0X\n", rgb[0], rgb[1], rgb[2]);
175               return AVERROR(EINVAL);
176            }
177        }
178        break;
179    case 8:
180        if(hsize - ihsize - 14 > 0)
181            avctx->pix_fmt = PIX_FMT_PAL8;
182        else
183            avctx->pix_fmt = PIX_FMT_GRAY8;
184        break;
185    case 1:
186    case 4:
187        if(hsize - ihsize - 14 > 0){
188            avctx->pix_fmt = PIX_FMT_PAL8;
189        }else{
190            av_log(avctx, AV_LOG_ERROR, "Unknown palette for %d-colour BMP\n", 1<<depth);
191            return -1;
192        }
193        break;
194    default:
195        av_log(avctx, AV_LOG_ERROR, "depth %d not supported\n", depth);
196        return -1;
197    }
198
199    if(avctx->pix_fmt == PIX_FMT_NONE){
200        av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
201        return -1;
202    }
203
204    if(p->data[0])
205        avctx->release_buffer(avctx, p);
206
207    p->reference = 0;
208    if(avctx->get_buffer(avctx, p) < 0){
209        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
210        return -1;
211    }
212    p->pict_type = AV_PICTURE_TYPE_I;
213    p->key_frame = 1;
214
215    buf = buf0 + hsize;
216    dsize = buf_size - hsize;
217
218    /* Line size in file multiple of 4 */
219    n = ((avctx->width * depth) / 8 + 3) & ~3;
220
221    if(n * avctx->height > dsize && comp != BMP_RLE4 && comp != BMP_RLE8){
222        av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
223               dsize, n * avctx->height);
224        return -1;
225    }
226
227    // RLE may skip decoding some picture areas, so blank picture before decoding
228    if(comp == BMP_RLE4 || comp == BMP_RLE8)
229        memset(p->data[0], 0, avctx->height * p->linesize[0]);
230
231    if(height > 0){
232        ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
233        linesize = -p->linesize[0];
234    } else {
235        ptr = p->data[0];
236        linesize = p->linesize[0];
237    }
238
239    if(avctx->pix_fmt == PIX_FMT_PAL8){
240        int colors = 1 << depth;
241
242        memset(p->data[1], 0, 1024);
243
244        if(ihsize >= 36){
245            int t;
246            buf = buf0 + 46;
247            t = bytestream_get_le32(&buf);
248            if(t < 0 || t > (1 << depth)){
249                av_log(avctx, AV_LOG_ERROR, "Incorrect number of colors - %X for bitdepth %d\n", t, depth);
250            }else if(t){
251                colors = t;
252            }
253        }
254        buf = buf0 + 14 + ihsize; //palette location
255        if((hsize-ihsize-14) < (colors << 2)){ // OS/2 bitmap, 3 bytes per palette entry
256            for(i = 0; i < colors; i++)
257                ((uint32_t*)p->data[1])[i] = bytestream_get_le24(&buf);
258        }else{
259            for(i = 0; i < colors; i++)
260                ((uint32_t*)p->data[1])[i] = bytestream_get_le32(&buf);
261        }
262        buf = buf0 + hsize;
263    }
264    if(comp == BMP_RLE4 || comp == BMP_RLE8){
265        if(height < 0){
266            p->data[0] += p->linesize[0] * (avctx->height - 1);
267            p->linesize[0] = -p->linesize[0];
268        }
269        bytestream2_init(&gb, buf, dsize);
270        ff_msrle_decode(avctx, (AVPicture*)p, depth, &gb);
271        if(height < 0){
272            p->data[0] += p->linesize[0] * (avctx->height - 1);
273            p->linesize[0] = -p->linesize[0];
274        }
275    }else{
276        switch(depth){
277        case 1:
278            for (i = 0; i < avctx->height; i++) {
279                int j;
280                for (j = 0; j < n; j++) {
281                    ptr[j*8+0] =  buf[j] >> 7;
282                    ptr[j*8+1] = (buf[j] >> 6) & 1;
283                    ptr[j*8+2] = (buf[j] >> 5) & 1;
284                    ptr[j*8+3] = (buf[j] >> 4) & 1;
285                    ptr[j*8+4] = (buf[j] >> 3) & 1;
286                    ptr[j*8+5] = (buf[j] >> 2) & 1;
287                    ptr[j*8+6] = (buf[j] >> 1) & 1;
288                    ptr[j*8+7] =  buf[j]       & 1;
289                }
290                buf += n;
291                ptr += linesize;
292            }
293            break;
294        case 8:
295        case 24:
296            for(i = 0; i < avctx->height; i++){
297                memcpy(ptr, buf, n);
298                buf += n;
299                ptr += linesize;
300            }
301            break;
302        case 4:
303            for(i = 0; i < avctx->height; i++){
304                int j;
305                for(j = 0; j < n; j++){
306                    ptr[j*2+0] = (buf[j] >> 4) & 0xF;
307                    ptr[j*2+1] = buf[j] & 0xF;
308                }
309                buf += n;
310                ptr += linesize;
311            }
312            break;
313        case 16:
314            for(i = 0; i < avctx->height; i++){
315                const uint16_t *src = (const uint16_t *) buf;
316                uint16_t *dst = (uint16_t *) ptr;
317
318                for(j = 0; j < avctx->width; j++)
319                    *dst++ = av_le2ne16(*src++);
320
321                buf += n;
322                ptr += linesize;
323            }
324            break;
325        case 32:
326            for(i = 0; i < avctx->height; i++){
327                const uint8_t *src = buf;
328                uint8_t *dst = ptr;
329
330                for(j = 0; j < avctx->width; j++){
331                    dst[0] = src[rgb[2]];
332                    dst[1] = src[rgb[1]];
333                    dst[2] = src[rgb[0]];
334                    dst += 3;
335                    src += 4;
336                }
337
338                buf += n;
339                ptr += linesize;
340            }
341            break;
342        default:
343            av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
344            return -1;
345        }
346    }
347
348    *picture = s->picture;
349    *data_size = sizeof(AVPicture);
350
351    return buf_size;
352}
353
354static av_cold int bmp_decode_end(AVCodecContext *avctx)
355{
356    BMPContext* c = avctx->priv_data;
357
358    if (c->picture.data[0])
359        avctx->release_buffer(avctx, &c->picture);
360
361    return 0;
362}
363
364AVCodec ff_bmp_decoder = {
365    .name           = "bmp",
366    .type           = AVMEDIA_TYPE_VIDEO,
367    .id             = CODEC_ID_BMP,
368    .priv_data_size = sizeof(BMPContext),
369    .init           = bmp_decode_init,
370    .close          = bmp_decode_end,
371    .decode         = bmp_decode_frame,
372    .capabilities   = CODEC_CAP_DR1,
373    .long_name = NULL_IF_CONFIG_SMALL("BMP image"),
374};
375