bmp.c 10.8 KB
Newer Older
Måns Rullgård's avatar
Måns Rullgård committed
1
/*
2
 * BMP image format decoder
Måns Rullgård's avatar
Måns Rullgård committed
3 4
 * Copyright (c) 2005 Mans Rullgard
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav is free software; you can redistribute it and/or
Måns Rullgård's avatar
Måns Rullgård committed
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
Måns Rullgård's avatar
Måns Rullgård committed
11
 *
12
 * Libav is distributed in the hope that it will be useful,
Måns Rullgård's avatar
Måns Rullgård committed
13 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * 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
Måns Rullgård's avatar
Måns Rullgård committed
20 21 22
 */

#include "avcodec.h"
23
#include "bytestream.h"
24
#include "bmp.h"
25
#include "internal.h"
26
#include "msrledec.h"
Måns Rullgård's avatar
Måns Rullgård committed
27

28
static int bmp_decode_frame(AVCodecContext *avctx,
29
                            void *data, int *got_frame,
30
                            AVPacket *avpkt)
Måns Rullgård's avatar
Måns Rullgård committed
31
{
32
    const uint8_t *buf = avpkt->data;
Anton Khirnov's avatar
Anton Khirnov committed
33
    int buf_size       = avpkt->size;
34
    AVFrame *p         = data;
Måns Rullgård's avatar
Måns Rullgård committed
35 36 37
    unsigned int fsize, hsize;
    int width, height;
    unsigned int depth;
Michael Niedermayer's avatar
Michael Niedermayer committed
38
    BiCompression comp;
Måns Rullgård's avatar
Måns Rullgård committed
39
    unsigned int ihsize;
40
    int i, j, n, linesize, ret;
Måns Rullgård's avatar
Måns Rullgård committed
41 42 43
    uint32_t rgb[3];
    uint8_t *ptr;
    int dsize;
Michael Niedermayer's avatar
Michael Niedermayer committed
44
    const uint8_t *buf0 = buf;
45
    GetByteContext gb;
Måns Rullgård's avatar
Måns Rullgård committed
46

Anton Khirnov's avatar
Anton Khirnov committed
47
    if (buf_size < 14) {
Måns Rullgård's avatar
Måns Rullgård committed
48
        av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\n", buf_size);
49
        return AVERROR_INVALIDDATA;
Måns Rullgård's avatar
Måns Rullgård committed
50 51
    }

Anton Khirnov's avatar
Anton Khirnov committed
52 53
    if (bytestream_get_byte(&buf) != 'B' ||
        bytestream_get_byte(&buf) != 'M') {
Måns Rullgård's avatar
Måns Rullgård committed
54
        av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
55
        return AVERROR_INVALIDDATA;
Måns Rullgård's avatar
Måns Rullgård committed
56 57
    }

58
    fsize = bytestream_get_le32(&buf);
Anton Khirnov's avatar
Anton Khirnov committed
59
    if (buf_size < fsize) {
60
        av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d), trying to decode anyway\n",
Måns Rullgård's avatar
Måns Rullgård committed
61
               buf_size, fsize);
62
        fsize = buf_size;
Måns Rullgård's avatar
Måns Rullgård committed
63 64
    }

65 66
    buf += 2; /* reserved1 */
    buf += 2; /* reserved2 */
Måns Rullgård's avatar
Måns Rullgård committed
67

Anton Khirnov's avatar
Anton Khirnov committed
68 69 70
    hsize  = bytestream_get_le32(&buf); /* header size */
    ihsize = bytestream_get_le32(&buf); /* more header size */
    if (ihsize + 14 > hsize) {
Måns Rullgård's avatar
Måns Rullgård committed
71
        av_log(avctx, AV_LOG_ERROR, "invalid header size %d\n", hsize);
72
        return AVERROR_INVALIDDATA;
Måns Rullgård's avatar
Måns Rullgård committed
73 74
    }

75
    /* sometimes file size is set to some headers size, set a real size in that case */
Anton Khirnov's avatar
Anton Khirnov committed
76
    if (fsize == 14 || fsize == ihsize + 14)
77 78
        fsize = buf_size - 2;

Anton Khirnov's avatar
Anton Khirnov committed
79
    if (fsize <= hsize) {
80 81
        av_log(avctx, AV_LOG_ERROR, "declared file size is less than header size (%d < %d)\n",
               fsize, hsize);
82
        return AVERROR_INVALIDDATA;
83 84
    }

Anton Khirnov's avatar
Anton Khirnov committed
85
    switch (ihsize) {
86 87 88 89
    case  40: // windib v3
    case  64: // OS/2 v2
    case 108: // windib v4
    case 124: // windib v5
Anton Khirnov's avatar
Anton Khirnov committed
90
        width  = bytestream_get_le32(&buf);
Benoit Fouet's avatar
Benoit Fouet committed
91
        height = bytestream_get_le32(&buf);
92 93
        break;
    case  12: // OS/2 v1
Benoit Fouet's avatar
Benoit Fouet committed
94 95
        width  = bytestream_get_le16(&buf);
        height = bytestream_get_le16(&buf);
96 97
        break;
    default:
98
        av_log(avctx, AV_LOG_ERROR, "unsupported BMP file, patch welcome\n");
99
        return AVERROR_PATCHWELCOME;
Benoit Fouet's avatar
Benoit Fouet committed
100
    }
Måns Rullgård's avatar
Måns Rullgård committed
101

Anton Khirnov's avatar
Anton Khirnov committed
102 103
    /* planes */
    if (bytestream_get_le16(&buf) != 1) {
Måns Rullgård's avatar
Måns Rullgård committed
104
        av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n");
105
        return AVERROR_INVALIDDATA;
Måns Rullgård's avatar
Måns Rullgård committed
106 107
    }

108
    depth = bytestream_get_le16(&buf);
Måns Rullgård's avatar
Måns Rullgård committed
109

Anton Khirnov's avatar
Anton Khirnov committed
110
    if (ihsize == 40)
111
        comp = bytestream_get_le32(&buf);
Måns Rullgård's avatar
Måns Rullgård committed
112 113 114
    else
        comp = BMP_RGB;

Anton Khirnov's avatar
Anton Khirnov committed
115 116
    if (comp != BMP_RGB && comp != BMP_BITFIELDS && comp != BMP_RLE4 &&
        comp != BMP_RLE8) {
Måns Rullgård's avatar
Måns Rullgård committed
117
        av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp);
118
        return AVERROR_INVALIDDATA;
Måns Rullgård's avatar
Måns Rullgård committed
119 120
    }

Anton Khirnov's avatar
Anton Khirnov committed
121
    if (comp == BMP_BITFIELDS) {
122 123 124 125
        buf += 20;
        rgb[0] = bytestream_get_le32(&buf);
        rgb[1] = bytestream_get_le32(&buf);
        rgb[2] = bytestream_get_le32(&buf);
Måns Rullgård's avatar
Måns Rullgård committed
126 127
    }

Anton Khirnov's avatar
Anton Khirnov committed
128 129
    avctx->width  = width;
    avctx->height = height > 0 ? height : -height;
Måns Rullgård's avatar
Måns Rullgård committed
130

131
    avctx->pix_fmt = AV_PIX_FMT_NONE;
Måns Rullgård's avatar
Måns Rullgård committed
132

Anton Khirnov's avatar
Anton Khirnov committed
133
    switch (depth) {
Måns Rullgård's avatar
Måns Rullgård committed
134
    case 32:
Anton Khirnov's avatar
Anton Khirnov committed
135
        if (comp == BMP_BITFIELDS) {
Måns Rullgård's avatar
Måns Rullgård committed
136 137 138 139
            rgb[0] = (rgb[0] >> 15) & 3;
            rgb[1] = (rgb[1] >> 15) & 3;
            rgb[2] = (rgb[2] >> 15) & 3;

Anton Khirnov's avatar
Anton Khirnov committed
140 141
            if (rgb[0] + rgb[1] + rgb[2] != 3 ||
                rgb[0] == rgb[1] || rgb[0] == rgb[2] || rgb[1] == rgb[2]) {
Måns Rullgård's avatar
Måns Rullgård committed
142 143 144 145 146 147 148 149
                break;
            }
        } else {
            rgb[0] = 2;
            rgb[1] = 1;
            rgb[2] = 0;
        }

150
        avctx->pix_fmt = AV_PIX_FMT_BGR24;
Måns Rullgård's avatar
Måns Rullgård committed
151 152
        break;
    case 24:
153
        avctx->pix_fmt = AV_PIX_FMT_BGR24;
Måns Rullgård's avatar
Måns Rullgård committed
154 155
        break;
    case 16:
Anton Khirnov's avatar
Anton Khirnov committed
156
        if (comp == BMP_RGB)
157
            avctx->pix_fmt = AV_PIX_FMT_RGB555;
158 159
        else if (comp == BMP_BITFIELDS) {
            if (rgb[0] == 0xF800 && rgb[1] == 0x07E0 && rgb[2] == 0x001F)
160
               avctx->pix_fmt = AV_PIX_FMT_RGB565;
161
            else if (rgb[0] == 0x7C00 && rgb[1] == 0x03E0 && rgb[2] == 0x001F)
162
               avctx->pix_fmt = AV_PIX_FMT_RGB555;
163
            else if (rgb[0] == 0x0F00 && rgb[1] == 0x00F0 && rgb[2] == 0x000F)
164
               avctx->pix_fmt = AV_PIX_FMT_RGB444;
165 166 167 168 169
            else {
               av_log(avctx, AV_LOG_ERROR, "Unknown bitfields %0X %0X %0X\n", rgb[0], rgb[1], rgb[2]);
               return AVERROR(EINVAL);
            }
        }
170 171
        break;
    case 8:
Anton Khirnov's avatar
Anton Khirnov committed
172
        if (hsize - ihsize - 14 > 0)
173
            avctx->pix_fmt = AV_PIX_FMT_PAL8;
174
        else
175
            avctx->pix_fmt = AV_PIX_FMT_GRAY8;
176
        break;
177
    case 1:
178
    case 4:
Anton Khirnov's avatar
Anton Khirnov committed
179
        if (hsize - ihsize - 14 > 0) {
180
            avctx->pix_fmt = AV_PIX_FMT_PAL8;
Anton Khirnov's avatar
Anton Khirnov committed
181
        } else {
182
            av_log(avctx, AV_LOG_ERROR, "Unknown palette for %d-colour BMP\n", 1<<depth);
183
            return AVERROR_INVALIDDATA;
184 185
        }
        break;
Måns Rullgård's avatar
Måns Rullgård committed
186 187
    default:
        av_log(avctx, AV_LOG_ERROR, "depth %d not supported\n", depth);
188
        return AVERROR_INVALIDDATA;
Måns Rullgård's avatar
Måns Rullgård committed
189 190
    }

Anton Khirnov's avatar
Anton Khirnov committed
191
    if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
Måns Rullgård's avatar
Måns Rullgård committed
192
        av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
193
        return AVERROR_INVALIDDATA;
Måns Rullgård's avatar
Måns Rullgård committed
194 195
    }

196
    if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
Måns Rullgård's avatar
Måns Rullgård committed
197
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
198
        return ret;
Måns Rullgård's avatar
Måns Rullgård committed
199
    }
200
    p->pict_type = AV_PICTURE_TYPE_I;
Måns Rullgård's avatar
Måns Rullgård committed
201 202
    p->key_frame = 1;

Anton Khirnov's avatar
Anton Khirnov committed
203
    buf   = buf0 + hsize;
Måns Rullgård's avatar
Måns Rullgård committed
204 205
    dsize = buf_size - hsize;

206
    /* Line size in file multiple of 4 */
207
    n = ((avctx->width * depth) / 8 + 3) & ~3;
Måns Rullgård's avatar
Måns Rullgård committed
208

Anton Khirnov's avatar
Anton Khirnov committed
209
    if (n * avctx->height > dsize && comp != BMP_RLE4 && comp != BMP_RLE8) {
Måns Rullgård's avatar
Måns Rullgård committed
210 211
        av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
               dsize, n * avctx->height);
212
        return AVERROR_INVALIDDATA;
Måns Rullgård's avatar
Måns Rullgård committed
213 214
    }

215
    // RLE may skip decoding some picture areas, so blank picture before decoding
Anton Khirnov's avatar
Anton Khirnov committed
216
    if (comp == BMP_RLE4 || comp == BMP_RLE8)
217 218
        memset(p->data[0], 0, avctx->height * p->linesize[0]);

Anton Khirnov's avatar
Anton Khirnov committed
219 220
    if (height > 0) {
        ptr      = p->data[0] + (avctx->height - 1) * p->linesize[0];
Måns Rullgård's avatar
Måns Rullgård committed
221 222
        linesize = -p->linesize[0];
    } else {
Anton Khirnov's avatar
Anton Khirnov committed
223
        ptr      = p->data[0];
Måns Rullgård's avatar
Måns Rullgård committed
224 225 226
        linesize = p->linesize[0];
    }

Anton Khirnov's avatar
Anton Khirnov committed
227
    if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
228
        int colors = 1 << depth;
229 230 231

        memset(p->data[1], 0, 1024);

Anton Khirnov's avatar
Anton Khirnov committed
232
        if (ihsize >= 36) {
233 234
            int t;
            buf = buf0 + 46;
Anton Khirnov's avatar
Anton Khirnov committed
235 236
            t   = bytestream_get_le32(&buf);
            if (t < 0 || t > (1 << depth)) {
237
                av_log(avctx, AV_LOG_ERROR, "Incorrect number of colors - %X for bitdepth %d\n", t, depth);
Anton Khirnov's avatar
Anton Khirnov committed
238
            } else if (t) {
239 240 241
                colors = t;
            }
        }
242
        buf = buf0 + 14 + ihsize; //palette location
Anton Khirnov's avatar
Anton Khirnov committed
243 244 245
        // OS/2 bitmap, 3 bytes per palette entry
        if ((hsize-ihsize-14) < (colors << 2)) {
            for (i = 0; i < colors; i++)
246
                ((uint32_t*)p->data[1])[i] = bytestream_get_le24(&buf);
Anton Khirnov's avatar
Anton Khirnov committed
247 248
        } else {
            for (i = 0; i < colors; i++)
249 250 251 252
                ((uint32_t*)p->data[1])[i] = bytestream_get_le32(&buf);
        }
        buf = buf0 + hsize;
    }
Anton Khirnov's avatar
Anton Khirnov committed
253 254 255
    if (comp == BMP_RLE4 || comp == BMP_RLE8) {
        if (height < 0) {
            p->data[0]    +=  p->linesize[0] * (avctx->height - 1);
256 257
            p->linesize[0] = -p->linesize[0];
        }
258 259
        bytestream2_init(&gb, buf, dsize);
        ff_msrle_decode(avctx, (AVPicture*)p, depth, &gb);
Anton Khirnov's avatar
Anton Khirnov committed
260 261
        if (height < 0) {
            p->data[0]    +=  p->linesize[0] * (avctx->height - 1);
262 263
            p->linesize[0] = -p->linesize[0];
        }
Anton Khirnov's avatar
Anton Khirnov committed
264 265
    } else {
        switch (depth) {
266
        case 1:
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
            for (i = 0; i < avctx->height; i++) {
                int j;
                for (j = 0; j < n; j++) {
                    ptr[j*8+0] =  buf[j] >> 7;
                    ptr[j*8+1] = (buf[j] >> 6) & 1;
                    ptr[j*8+2] = (buf[j] >> 5) & 1;
                    ptr[j*8+3] = (buf[j] >> 4) & 1;
                    ptr[j*8+4] = (buf[j] >> 3) & 1;
                    ptr[j*8+5] = (buf[j] >> 2) & 1;
                    ptr[j*8+6] = (buf[j] >> 1) & 1;
                    ptr[j*8+7] =  buf[j]       & 1;
                }
                buf += n;
                ptr += linesize;
            }
            break;
283 284
        case 8:
        case 24:
Anton Khirnov's avatar
Anton Khirnov committed
285
            for (i = 0; i < avctx->height; i++) {
286 287 288
                memcpy(ptr, buf, n);
                buf += n;
                ptr += linesize;
289
            }
290 291
            break;
        case 4:
Anton Khirnov's avatar
Anton Khirnov committed
292
            for (i = 0; i < avctx->height; i++) {
293
                int j;
Anton Khirnov's avatar
Anton Khirnov committed
294
                for (j = 0; j < n; j++) {
295 296 297 298 299 300 301 302
                    ptr[j*2+0] = (buf[j] >> 4) & 0xF;
                    ptr[j*2+1] = buf[j] & 0xF;
                }
                buf += n;
                ptr += linesize;
            }
            break;
        case 16:
Anton Khirnov's avatar
Anton Khirnov committed
303
            for (i = 0; i < avctx->height; i++) {
304
                const uint16_t *src = (const uint16_t *) buf;
Anton Khirnov's avatar
Anton Khirnov committed
305
                uint16_t *dst       = (uint16_t *) ptr;
Måns Rullgård's avatar
Måns Rullgård committed
306

Anton Khirnov's avatar
Anton Khirnov committed
307
                for (j = 0; j < avctx->width; j++)
308
                    *dst++ = av_le2ne16(*src++);
Måns Rullgård's avatar
Måns Rullgård committed
309

310 311
                buf += n;
                ptr += linesize;
Måns Rullgård's avatar
Måns Rullgård committed
312
            }
313 314
            break;
        case 32:
Anton Khirnov's avatar
Anton Khirnov committed
315
            for (i = 0; i < avctx->height; i++) {
316
                const uint8_t *src = buf;
Anton Khirnov's avatar
Anton Khirnov committed
317
                uint8_t *dst       = ptr;
318

Anton Khirnov's avatar
Anton Khirnov committed
319
                for (j = 0; j < avctx->width; j++) {
320 321 322 323 324 325 326 327 328 329 330 331 332
                    dst[0] = src[rgb[2]];
                    dst[1] = src[rgb[1]];
                    dst[2] = src[rgb[0]];
                    dst += 3;
                    src += 4;
                }

                buf += n;
                ptr += linesize;
            }
            break;
        default:
            av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
333
            return AVERROR_INVALIDDATA;
Måns Rullgård's avatar
Måns Rullgård committed
334
        }
335
    }
Måns Rullgård's avatar
Måns Rullgård committed
336

337
    *got_frame = 1;
Måns Rullgård's avatar
Måns Rullgård committed
338 339 340 341

    return buf_size;
}

342
AVCodec ff_bmp_decoder = {
343
    .name           = "bmp",
344
    .long_name      = NULL_IF_CONFIG_SMALL("BMP (Windows and OS/2 bitmap)"),
345
    .type           = AVMEDIA_TYPE_VIDEO,
346
    .id             = AV_CODEC_ID_BMP,
347 348
    .decode         = bmp_decode_frame,
    .capabilities   = CODEC_CAP_DR1,
Måns Rullgård's avatar
Måns Rullgård committed
349
};