Statistics
| Branch: | Revision:

ffmpeg / libavcodec / bmp.c @ 5d44b2a1

History | View | Annotate | Download (9.45 KB)

1 9fa62f2a Måns Rullgård
/*
2 76ebb18f Michel Bardiaux
 * BMP image format decoder
3 9fa62f2a Måns Rullgård
 * Copyright (c) 2005 Mans Rullgard
4
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 9fa62f2a Måns Rullgård
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 9fa62f2a Måns Rullgård
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 9fa62f2a Måns Rullgård
 * 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 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 9fa62f2a Måns Rullgård
 */
21
22
#include "avcodec.h"
23 e96cc09d Michel Bardiaux
#include "bytestream.h"
24 76ebb18f Michel Bardiaux
#include "bmp.h"
25 fca506df Kostya Shishkov
#include "msrledec.h"
26 9fa62f2a Måns Rullgård
27 98a6fff9 Zuxy Meng
static av_cold int bmp_decode_init(AVCodecContext *avctx){
28 9fa62f2a Måns Rullgård
    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
36 115329f1 Diego Biurrun
static int bmp_decode_frame(AVCodecContext *avctx,
37 9fa62f2a Måns Rullgård
                            void *data, int *data_size,
38 4bae195f Michael Niedermayer
                            const uint8_t *buf, int buf_size)
39 9fa62f2a Måns Rullgård
{
40
    BMPContext *s = avctx->priv_data;
41
    AVFrame *picture = data;
42
    AVFrame *p = &s->picture;
43
    unsigned int fsize, hsize;
44
    int width, height;
45
    unsigned int depth;
46 3e997aa4 Michael Niedermayer
    BiCompression comp;
47 9fa62f2a Måns Rullgård
    unsigned int ihsize;
48
    int i, j, n, linesize;
49
    uint32_t rgb[3];
50
    uint8_t *ptr;
51
    int dsize;
52 4bae195f Michael Niedermayer
    const uint8_t *buf0 = buf;
53 9fa62f2a Måns Rullgård
54
    if(buf_size < 14){
55
        av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\n", buf_size);
56
        return -1;
57
    }
58
59 e96cc09d Michel Bardiaux
    if(bytestream_get_byte(&buf) != 'B' ||
60
       bytestream_get_byte(&buf) != 'M') {
61 9fa62f2a Måns Rullgård
        av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
62
        return -1;
63
    }
64
65 e96cc09d Michel Bardiaux
    fsize = bytestream_get_le32(&buf);
66 9fa62f2a Måns Rullgård
    if(buf_size < fsize){
67
        av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
68
               buf_size, fsize);
69
        return -1;
70
    }
71
72 e96cc09d Michel Bardiaux
    buf += 2; /* reserved1 */
73
    buf += 2; /* reserved2 */
74 9fa62f2a Måns Rullgård
75 e96cc09d Michel Bardiaux
    hsize = bytestream_get_le32(&buf); /* header size */
76
    ihsize = bytestream_get_le32(&buf);       /* more header size */
77 9fa62f2a Måns Rullgård
    if(ihsize + 14 > hsize){
78
        av_log(avctx, AV_LOG_ERROR, "invalid header size %d\n", hsize);
79
        return -1;
80
    }
81
82 c5b2fe16 Kostya Shishkov
    /* sometimes file size is set to some headers size, set a real size in that case */
83
    if(fsize == 14 || fsize == ihsize + 14)
84
        fsize = buf_size - 2;
85
86
    if(fsize <= hsize){
87
        av_log(avctx, AV_LOG_ERROR, "declared file size is less than header size (%d < %d)\n",
88
               fsize, hsize);
89
        return -1;
90
    }
91
92 4a14e666 Kostya Shishkov
    switch(ihsize){
93
    case  40: // windib v3
94
    case  64: // OS/2 v2
95
    case 108: // windib v4
96
    case 124: // windib v5
97 6f7b915a Benoit Fouet
        width = bytestream_get_le32(&buf);
98
        height = bytestream_get_le32(&buf);
99 4a14e666 Kostya Shishkov
        break;
100
    case  12: // OS/2 v1
101 b65213c0 Benoit Fouet
        width  = bytestream_get_le16(&buf);
102
        height = bytestream_get_le16(&buf);
103 4a14e666 Kostya Shishkov
        break;
104
    default:
105 3a9a15c4 Kostya Shishkov
        av_log(avctx, AV_LOG_ERROR, "unsupported BMP file, patch welcome\n");
106 b65213c0 Benoit Fouet
        return -1;
107
    }
108 9fa62f2a Måns Rullgård
109 e96cc09d Michel Bardiaux
    if(bytestream_get_le16(&buf) != 1){ /* planes */
110 9fa62f2a Måns Rullgård
        av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n");
111
        return -1;
112
    }
113
114 e96cc09d Michel Bardiaux
    depth = bytestream_get_le16(&buf);
115 9fa62f2a Måns Rullgård
116 b65213c0 Benoit Fouet
    if(ihsize == 40)
117 e96cc09d Michel Bardiaux
        comp = bytestream_get_le32(&buf);
118 9fa62f2a Måns Rullgård
    else
119
        comp = BMP_RGB;
120
121 fca506df Kostya Shishkov
    if(comp != BMP_RGB && comp != BMP_BITFIELDS && comp != BMP_RLE4 && comp != BMP_RLE8){
122 9fa62f2a Måns Rullgård
        av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp);
123
        return -1;
124
    }
125
126
    if(comp == BMP_BITFIELDS){
127 e96cc09d Michel Bardiaux
        buf += 20;
128
        rgb[0] = bytestream_get_le32(&buf);
129
        rgb[1] = bytestream_get_le32(&buf);
130
        rgb[2] = bytestream_get_le32(&buf);
131 9fa62f2a Måns Rullgård
    }
132
133
    avctx->width = width;
134
    avctx->height = height > 0? height: -height;
135
136
    avctx->pix_fmt = PIX_FMT_NONE;
137
138
    switch(depth){
139
    case 32:
140
        if(comp == BMP_BITFIELDS){
141
            rgb[0] = (rgb[0] >> 15) & 3;
142
            rgb[1] = (rgb[1] >> 15) & 3;
143
            rgb[2] = (rgb[2] >> 15) & 3;
144
145
            if(rgb[0] + rgb[1] + rgb[2] != 3 ||
146
               rgb[0] == rgb[1] || rgb[0] == rgb[2] || rgb[1] == rgb[2]){
147
                break;
148
            }
149
        } else {
150
            rgb[0] = 2;
151
            rgb[1] = 1;
152
            rgb[2] = 0;
153
        }
154
155
        avctx->pix_fmt = PIX_FMT_BGR24;
156
        break;
157
    case 24:
158
        avctx->pix_fmt = PIX_FMT_BGR24;
159
        break;
160
    case 16:
161
        if(comp == BMP_RGB)
162
            avctx->pix_fmt = PIX_FMT_RGB555;
163 15501c32 Kostya Shishkov
        if(comp == BMP_BITFIELDS)
164
            avctx->pix_fmt = rgb[1] == 0x07E0 ? PIX_FMT_RGB565 : PIX_FMT_RGB555;
165
        break;
166
    case 8:
167
        if(hsize - ihsize - 14 > 0)
168
            avctx->pix_fmt = PIX_FMT_PAL8;
169
        else
170
            avctx->pix_fmt = PIX_FMT_GRAY8;
171
        break;
172
    case 4:
173
        if(hsize - ihsize - 14 > 0){
174
            avctx->pix_fmt = PIX_FMT_PAL8;
175
        }else{
176
            av_log(avctx, AV_LOG_ERROR, "Unknown palette for 16-colour BMP\n");
177
            return -1;
178
        }
179
        break;
180
    case 1:
181
        avctx->pix_fmt = PIX_FMT_MONOBLACK;
182 9fa62f2a Måns Rullgård
        break;
183
    default:
184
        av_log(avctx, AV_LOG_ERROR, "depth %d not supported\n", depth);
185
        return -1;
186
    }
187
188
    if(avctx->pix_fmt == PIX_FMT_NONE){
189
        av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
190
        return -1;
191
    }
192
193 d8b7b352 Michel Bardiaux
    if(p->data[0])
194
        avctx->release_buffer(avctx, p);
195
196 9fa62f2a Måns Rullgård
    p->reference = 0;
197
    if(avctx->get_buffer(avctx, p) < 0){
198
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
199
        return -1;
200
    }
201
    p->pict_type = FF_I_TYPE;
202
    p->key_frame = 1;
203
204 e96cc09d Michel Bardiaux
    buf = buf0 + hsize;
205 9fa62f2a Måns Rullgård
    dsize = buf_size - hsize;
206
207 743311a1 Måns Rullgård
    /* Line size in file multiple of 4 */
208 a79cae79 Kostya Shishkov
    n = ((avctx->width * depth) / 8 + 3) & ~3;
209 9fa62f2a Måns Rullgård
210 fca506df Kostya Shishkov
    if(n * avctx->height > dsize && comp != BMP_RLE4 && comp != BMP_RLE8){
211 9fa62f2a Måns Rullgård
        av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
212
               dsize, n * avctx->height);
213
        return -1;
214
    }
215
216 fca506df Kostya Shishkov
    // RLE may skip decoding some picture areas, so blank picture before decoding
217
    if(comp == BMP_RLE4 || comp == BMP_RLE8)
218
        memset(p->data[0], 0, avctx->height * p->linesize[0]);
219
220 15501c32 Kostya Shishkov
    if(depth == 4 || depth == 8)
221
        memset(p->data[1], 0, 1024);
222
223 9fa62f2a Måns Rullgård
    if(height > 0){
224
        ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
225
        linesize = -p->linesize[0];
226
    } else {
227
        ptr = p->data[0];
228
        linesize = p->linesize[0];
229
    }
230
231 15501c32 Kostya Shishkov
    if(avctx->pix_fmt == PIX_FMT_PAL8){
232
        buf = buf0 + 14 + ihsize; //palette location
233
        if((hsize-ihsize-14)>>depth < 4){ // OS/2 bitmap, 3 bytes per palette entry
234
            for(i = 0; i < (1 << depth); i++)
235
                ((uint32_t*)p->data[1])[i] = bytestream_get_le24(&buf);
236
        }else{
237
            for(i = 0; i < (1 << depth); i++)
238
                ((uint32_t*)p->data[1])[i] = bytestream_get_le32(&buf);
239
        }
240
        buf = buf0 + hsize;
241
    }
242 fca506df Kostya Shishkov
    if(comp == BMP_RLE4 || comp == BMP_RLE8){
243 5d44b2a1 Kostya Shishkov
        ff_msrle_decode(avctx, (AVPicture*)p, depth, buf, dsize);
244 fca506df Kostya Shishkov
    }else{
245 b94a631f Kostya Shishkov
        switch(depth){
246
        case 1:
247
            for(i = 0; i < avctx->height; i++){
248
                memcpy(ptr, buf, n);
249
                buf += n;
250
                ptr += linesize;
251 15501c32 Kostya Shishkov
            }
252 b94a631f Kostya Shishkov
            break;
253
        case 4:
254
            for(i = 0; i < avctx->height; i++){
255
                int j;
256
                for(j = 0; j < n; j++){
257
                    ptr[j*2+0] = (buf[j] >> 4) & 0xF;
258
                    ptr[j*2+1] = buf[j] & 0xF;
259
                }
260
                buf += n;
261
                ptr += linesize;
262
            }
263
            break;
264
        case 8:
265
            for(i = 0; i < avctx->height; i++){
266
                memcpy(ptr, buf, avctx->width);
267
                buf += n;
268
                ptr += linesize;
269
            }
270
            break;
271
        case 24:
272
            for(i = 0; i < avctx->height; i++){
273
                memcpy(ptr, buf, avctx->width*(depth>>3));
274
                buf += n;
275
                ptr += linesize;
276
            }
277
            break;
278
        case 16:
279
            for(i = 0; i < avctx->height; i++){
280
                const uint16_t *src = (const uint16_t *) buf;
281
                uint16_t *dst = (uint16_t *) ptr;
282 9fa62f2a Måns Rullgård
283 b94a631f Kostya Shishkov
                for(j = 0; j < avctx->width; j++)
284
                    *dst++ = le2me_16(*src++);
285 9fa62f2a Måns Rullgård
286 b94a631f Kostya Shishkov
                buf += n;
287
                ptr += linesize;
288 9fa62f2a Måns Rullgård
            }
289 b94a631f Kostya Shishkov
            break;
290
        case 32:
291
            for(i = 0; i < avctx->height; i++){
292
                const uint8_t *src = buf;
293
                uint8_t *dst = ptr;
294
295
                for(j = 0; j < avctx->width; j++){
296
                    dst[0] = src[rgb[2]];
297
                    dst[1] = src[rgb[1]];
298
                    dst[2] = src[rgb[0]];
299
                    dst += 3;
300
                    src += 4;
301
                }
302
303
                buf += n;
304
                ptr += linesize;
305
            }
306
            break;
307
        default:
308
            av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
309
            return -1;
310 9fa62f2a Måns Rullgård
        }
311 fca506df Kostya Shishkov
    }
312 9fa62f2a Måns Rullgård
313
    *picture = s->picture;
314
    *data_size = sizeof(AVPicture);
315
316
    return buf_size;
317
}
318
319 98a6fff9 Zuxy Meng
static av_cold int bmp_decode_end(AVCodecContext *avctx)
320 286c7107 Michel Bardiaux
{
321
    BMPContext* c = avctx->priv_data;
322
323
    if (c->picture.data[0])
324
        avctx->release_buffer(avctx, &c->picture);
325
326
    return 0;
327
}
328
329 9fa62f2a Måns Rullgård
AVCodec bmp_decoder = {
330
    "bmp",
331
    CODEC_TYPE_VIDEO,
332
    CODEC_ID_BMP,
333
    sizeof(BMPContext),
334
    bmp_decode_init,
335
    NULL,
336 286c7107 Michel Bardiaux
    bmp_decode_end,
337 bcdb2378 Stefano Sabatini
    bmp_decode_frame,
338 fe4bf374 Stefano Sabatini
    .long_name = NULL_IF_CONFIG_SMALL("BMP image"),
339 9fa62f2a Måns Rullgård
};