bmp.c 12.3 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 23
#include <inttypes.h>

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

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

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

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

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

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

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

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

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

Anton Khirnov's avatar
Anton Khirnov committed
89
    switch (ihsize) {
90 91
    case  40: // windib
    case  56: // windib v3
92 93 94
    case  64: // OS/2 v2
    case 108: // windib v4
    case 124: // windib v5
Anton Khirnov's avatar
Anton Khirnov committed
95
        width  = bytestream_get_le32(&buf);
Benoit Fouet's avatar
Benoit Fouet committed
96
        height = bytestream_get_le32(&buf);
97 98
        break;
    case  12: // OS/2 v1
Benoit Fouet's avatar
Benoit Fouet committed
99 100
        width  = bytestream_get_le16(&buf);
        height = bytestream_get_le16(&buf);
101 102
        break;
    default:
103 104
        avpriv_report_missing_feature(avctx, "Information header size %u",
                                      ihsize);
105
        return AVERROR_PATCHWELCOME;
Benoit Fouet's avatar
Benoit Fouet committed
106
    }
Måns Rullgård's avatar
Måns Rullgård committed
107

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

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

116
    if (ihsize >= 40)
117
        comp = bytestream_get_le32(&buf);
Måns Rullgård's avatar
Måns Rullgård committed
118 119 120
    else
        comp = BMP_RGB;

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

Anton Khirnov's avatar
Anton Khirnov committed
127
    if (comp == BMP_BITFIELDS) {
128 129 130 131
        buf += 20;
        rgb[0] = bytestream_get_le32(&buf);
        rgb[1] = bytestream_get_le32(&buf);
        rgb[2] = bytestream_get_le32(&buf);
132
        if (ihsize > 40)
133
        alpha = bytestream_get_le32(&buf);
Måns Rullgård's avatar
Måns Rullgård committed
134 135
    }

136 137 138 139 140
    ret = ff_set_dimensions(avctx, width, height > 0 ? height : -(unsigned)height);
    if (ret < 0) {
        av_log(avctx, AV_LOG_ERROR, "Failed to set dimensions %d %d\n", width, height);
        return AVERROR_INVALIDDATA;
    }
Måns Rullgård's avatar
Måns Rullgård committed
141

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

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

Anton Khirnov's avatar
Anton Khirnov committed
206
    if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
Måns Rullgård's avatar
Måns Rullgård committed
207
        av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
208
        return AVERROR_INVALIDDATA;
Måns Rullgård's avatar
Måns Rullgård committed
209 210
    }

211
    if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
212
        return ret;
213
    p->pict_type = AV_PICTURE_TYPE_I;
Måns Rullgård's avatar
Måns Rullgård committed
214 215
    p->key_frame = 1;

Anton Khirnov's avatar
Anton Khirnov committed
216
    buf   = buf0 + hsize;
Måns Rullgård's avatar
Måns Rullgård committed
217 218
    dsize = buf_size - hsize;

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

Anton Khirnov's avatar
Anton Khirnov committed
222
    if (n * avctx->height > dsize && comp != BMP_RLE4 && comp != BMP_RLE8) {
223 224 225 226 227 228 229
        n = (avctx->width * depth + 7) / 8;
        if (n * avctx->height > dsize) {
            av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
                   dsize, n * avctx->height);
            return AVERROR_INVALIDDATA;
        }
        av_log(avctx, AV_LOG_ERROR, "data size too small, assuming missing line alignment\n");
Måns Rullgård's avatar
Måns Rullgård committed
230 231
    }

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

Anton Khirnov's avatar
Anton Khirnov committed
236 237
    if (height > 0) {
        ptr      = p->data[0] + (avctx->height - 1) * p->linesize[0];
Måns Rullgård's avatar
Måns Rullgård committed
238 239
        linesize = -p->linesize[0];
    } else {
Anton Khirnov's avatar
Anton Khirnov committed
240
        ptr      = p->data[0];
Måns Rullgård's avatar
Måns Rullgård committed
241 242 243
        linesize = p->linesize[0];
    }

Anton Khirnov's avatar
Anton Khirnov committed
244
    if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
245
        int colors = 1 << depth;
246 247 248

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

Anton Khirnov's avatar
Anton Khirnov committed
249
        if (ihsize >= 36) {
250 251
            int t;
            buf = buf0 + 46;
Anton Khirnov's avatar
Anton Khirnov committed
252 253
            t   = bytestream_get_le32(&buf);
            if (t < 0 || t > (1 << depth)) {
254 255 256
                av_log(avctx, AV_LOG_ERROR,
                       "Incorrect number of colors - %X for bitdepth %u\n",
                       t, depth);
Anton Khirnov's avatar
Anton Khirnov committed
257
            } else if (t) {
258 259
                colors = t;
            }
260 261
        } else {
            colors = FFMIN(256, (hsize-ihsize-14) / 3);
262
        }
263
        buf = buf0 + 14 + ihsize; //palette location
Anton Khirnov's avatar
Anton Khirnov committed
264 265
        // OS/2 bitmap, 3 bytes per palette entry
        if ((hsize-ihsize-14) < (colors << 2)) {
266
            if ((hsize-ihsize-14) < colors * 3) {
267
                av_log(avctx, AV_LOG_ERROR, "palette doesn't fit in packet\n");
268 269
                return AVERROR_INVALIDDATA;
            }
Anton Khirnov's avatar
Anton Khirnov committed
270
            for (i = 0; i < colors; i++)
271
                ((uint32_t*)p->data[1])[i] = (0xFFU<<24) | bytestream_get_le24(&buf);
Anton Khirnov's avatar
Anton Khirnov committed
272 273
        } else {
            for (i = 0; i < colors; i++)
274
                ((uint32_t*)p->data[1])[i] = 0xFFU << 24 | bytestream_get_le32(&buf);
275 276 277
        }
        buf = buf0 + hsize;
    }
Anton Khirnov's avatar
Anton Khirnov committed
278
    if (comp == BMP_RLE4 || comp == BMP_RLE8) {
279
        if (comp == BMP_RLE8 && height < 0) {
Anton Khirnov's avatar
Anton Khirnov committed
280
            p->data[0]    +=  p->linesize[0] * (avctx->height - 1);
281 282
            p->linesize[0] = -p->linesize[0];
        }
283
        bytestream2_init(&gb, buf, dsize);
284
        ff_msrle_decode(avctx, p, depth, &gb);
Anton Khirnov's avatar
Anton Khirnov committed
285 286
        if (height < 0) {
            p->data[0]    +=  p->linesize[0] * (avctx->height - 1);
287 288
            p->linesize[0] = -p->linesize[0];
        }
Anton Khirnov's avatar
Anton Khirnov committed
289 290
    } else {
        switch (depth) {
291
        case 1:
292
            for (i = 0; i < avctx->height; i++) {
293
                int j;
294
                for (j = 0; j < avctx->width >> 3; j++) {
295 296 297 298 299 300 301 302 303
                    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;
                }
304 305 306
                for (j = 0; j < (avctx->width & 7); j++) {
                    ptr[avctx->width - (avctx->width & 7) + j] = buf[avctx->width >> 3] >> (7 - j) & 1;
                }
307 308 309 310
                buf += n;
                ptr += linesize;
            }
            break;
311 312
        case 8:
        case 24:
313
        case 32:
Anton Khirnov's avatar
Anton Khirnov committed
314
            for (i = 0; i < avctx->height; i++) {
315 316 317
                memcpy(ptr, buf, n);
                buf += n;
                ptr += linesize;
318
            }
319 320
            break;
        case 4:
Anton Khirnov's avatar
Anton Khirnov committed
321
            for (i = 0; i < avctx->height; i++) {
322
                int j;
Anton Khirnov's avatar
Anton Khirnov committed
323
                for (j = 0; j < n; j++) {
324 325 326 327 328 329 330 331
                    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
332
            for (i = 0; i < avctx->height; i++) {
333
                const uint16_t *src = (const uint16_t *) buf;
Anton Khirnov's avatar
Anton Khirnov committed
334
                uint16_t *dst       = (uint16_t *) ptr;
Måns Rullgård's avatar
Måns Rullgård committed
335

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

339 340
                buf += n;
                ptr += linesize;
Måns Rullgård's avatar
Måns Rullgård committed
341
            }
342 343 344
            break;
        default:
            av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
345
            return AVERROR_INVALIDDATA;
Måns Rullgård's avatar
Måns Rullgård committed
346
        }
347
    }
348 349 350 351 352 353 354 355 356 357 358 359 360 361
    if (avctx->pix_fmt == AV_PIX_FMT_BGRA) {
        for (i = 0; i < avctx->height; i++) {
            int j;
            uint8_t *ptr = p->data[0] + p->linesize[0]*i + 3;
            for (j = 0; j < avctx->width; j++) {
                if (ptr[4*j])
                    break;
            }
            if (j < avctx->width)
                break;
        }
        if (i == avctx->height)
            avctx->pix_fmt = p->format = AV_PIX_FMT_BGR0;
    }
Måns Rullgård's avatar
Måns Rullgård committed
362

363
    *got_frame = 1;
Måns Rullgård's avatar
Måns Rullgård committed
364 365 366 367

    return buf_size;
}

368
AVCodec ff_bmp_decoder = {
369
    .name           = "bmp",
370
    .long_name      = NULL_IF_CONFIG_SMALL("BMP (Windows and OS/2 bitmap)"),
371
    .type           = AVMEDIA_TYPE_VIDEO,
372
    .id             = AV_CODEC_ID_BMP,
373
    .decode         = bmp_decode_frame,
374
    .capabilities   = AV_CODEC_CAP_DR1,
Måns Rullgård's avatar
Måns Rullgård committed
375
};