bmp.c 11 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 6 7
 * This file is part of FFmpeg.
 *
 * FFmpeg 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
 * FFmpeg 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 FFmpeg; 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;
41
    uint32_t rgb[3] = {0};
42
    uint32_t alpha = 0;
Måns Rullgård's avatar
Måns Rullgård committed
43 44
    uint8_t *ptr;
    int dsize;
Michael Niedermayer's avatar
Michael Niedermayer committed
45
    const uint8_t *buf0 = buf;
46
    GetByteContext gb;
Måns Rullgård's avatar
Måns Rullgård committed
47

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

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

59
    fsize = bytestream_get_le32(&buf);
Anton Khirnov's avatar
Anton Khirnov committed
60
    if (buf_size < fsize) {
61
        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
62
               buf_size, fsize);
63
        fsize = buf_size;
Måns Rullgård's avatar
Måns Rullgård committed
64 65
    }

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

Anton Khirnov's avatar
Anton Khirnov committed
69 70 71
    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
72
        av_log(avctx, AV_LOG_ERROR, "invalid header size %d\n", hsize);
73
        return AVERROR_INVALIDDATA;
Måns Rullgård's avatar
Måns Rullgård committed
74 75
    }

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

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

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

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

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

112
    if (ihsize >= 40)
113
        comp = bytestream_get_le32(&buf);
Måns Rullgård's avatar
Måns Rullgård committed
114 115 116
    else
        comp = BMP_RGB;

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

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

Anton Khirnov's avatar
Anton Khirnov committed
131 132
    avctx->width  = width;
    avctx->height = height > 0 ? height : -height;
Måns Rullgård's avatar
Måns Rullgård committed
133

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

Anton Khirnov's avatar
Anton Khirnov committed
136
    switch (depth) {
Måns Rullgård's avatar
Måns Rullgård committed
137
    case 32:
Anton Khirnov's avatar
Anton Khirnov committed
138
        if (comp == BMP_BITFIELDS) {
139
            if (rgb[0] == 0xFF000000 && rgb[1] == 0x00FF0000 && rgb[2] == 0x0000FF00)
140
                avctx->pix_fmt = alpha ? AV_PIX_FMT_ABGR : AV_PIX_FMT_0BGR;
141
            else if (rgb[0] == 0x00FF0000 && rgb[1] == 0x0000FF00 && rgb[2] == 0x000000FF)
142
                avctx->pix_fmt = alpha ? AV_PIX_FMT_BGRA : AV_PIX_FMT_BGR0;
143
            else if (rgb[0] == 0x0000FF00 && rgb[1] == 0x00FF0000 && rgb[2] == 0xFF000000)
144
                avctx->pix_fmt = alpha ? AV_PIX_FMT_ARGB : AV_PIX_FMT_0RGB;
145
            else if (rgb[0] == 0x000000FF && rgb[1] == 0x0000FF00 && rgb[2] == 0x00FF0000)
146
                avctx->pix_fmt = alpha ? AV_PIX_FMT_RGBA : AV_PIX_FMT_RGB0;
147 148 149
            else {
                av_log(avctx, AV_LOG_ERROR, "Unknown bitfields %0X %0X %0X\n", rgb[0], rgb[1], rgb[2]);
                return AVERROR(EINVAL);
Måns Rullgård's avatar
Måns Rullgård committed
150 151
            }
        } else {
152
            avctx->pix_fmt = AV_PIX_FMT_BGRA;
Måns Rullgård's avatar
Måns Rullgård committed
153 154 155
        }
        break;
    case 24:
156
        avctx->pix_fmt = AV_PIX_FMT_BGR24;
Måns Rullgård's avatar
Måns Rullgård committed
157 158
        break;
    case 16:
Anton Khirnov's avatar
Anton Khirnov committed
159
        if (comp == BMP_RGB)
160
            avctx->pix_fmt = AV_PIX_FMT_RGB555;
161 162
        else if (comp == BMP_BITFIELDS) {
            if (rgb[0] == 0xF800 && rgb[1] == 0x07E0 && rgb[2] == 0x001F)
163
               avctx->pix_fmt = AV_PIX_FMT_RGB565;
164
            else if (rgb[0] == 0x7C00 && rgb[1] == 0x03E0 && rgb[2] == 0x001F)
165
               avctx->pix_fmt = AV_PIX_FMT_RGB555;
166
            else if (rgb[0] == 0x0F00 && rgb[1] == 0x00F0 && rgb[2] == 0x000F)
167
               avctx->pix_fmt = AV_PIX_FMT_RGB444;
168 169 170 171 172
            else {
               av_log(avctx, AV_LOG_ERROR, "Unknown bitfields %0X %0X %0X\n", rgb[0], rgb[1], rgb[2]);
               return AVERROR(EINVAL);
            }
        }
173 174
        break;
    case 8:
Anton Khirnov's avatar
Anton Khirnov committed
175
        if (hsize - ihsize - 14 > 0)
176
            avctx->pix_fmt = AV_PIX_FMT_PAL8;
177
        else
178
            avctx->pix_fmt = AV_PIX_FMT_GRAY8;
179
        break;
180
    case 1:
181
    case 4:
Anton Khirnov's avatar
Anton Khirnov committed
182
        if (hsize - ihsize - 14 > 0) {
183
            avctx->pix_fmt = AV_PIX_FMT_PAL8;
Anton Khirnov's avatar
Anton Khirnov committed
184
        } else {
185
            av_log(avctx, AV_LOG_ERROR, "Unknown palette for %d-colour BMP\n", 1<<depth);
186
            return AVERROR_INVALIDDATA;
187 188
        }
        break;
Måns Rullgård's avatar
Måns Rullgård committed
189 190
    default:
        av_log(avctx, AV_LOG_ERROR, "depth %d not supported\n", depth);
191
        return AVERROR_INVALIDDATA;
Måns Rullgård's avatar
Måns Rullgård committed
192 193
    }

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

199
    if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
200
        return ret;
201
    p->pict_type = AV_PICTURE_TYPE_I;
Måns Rullgård's avatar
Måns Rullgård committed
202 203
    p->key_frame = 1;

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

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

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

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

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

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

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

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

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

316 317
                buf += n;
                ptr += linesize;
Måns Rullgård's avatar
Måns Rullgård committed
318
            }
319 320 321
            break;
        default:
            av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
322
            return AVERROR_INVALIDDATA;
Måns Rullgård's avatar
Måns Rullgård committed
323
        }
324
    }
Måns Rullgård's avatar
Måns Rullgård committed
325

326
    *got_frame = 1;
Måns Rullgård's avatar
Måns Rullgård committed
327 328 329 330

    return buf_size;
}

331
AVCodec ff_bmp_decoder = {
332
    .name           = "bmp",
333
    .long_name      = NULL_IF_CONFIG_SMALL("BMP (Windows and OS/2 bitmap)"),
334
    .type           = AVMEDIA_TYPE_VIDEO,
335
    .id             = AV_CODEC_ID_BMP,
336 337
    .decode         = bmp_decode_frame,
    .capabilities   = CODEC_CAP_DR1,
Måns Rullgård's avatar
Måns Rullgård committed
338
};