]> git.sesse.net Git - ffmpeg/blob - libavcodec/bmp.c
Merge commit '9a07fac678a8540d076e635061bbaa4ed09a9431'
[ffmpeg] / libavcodec / bmp.c
1 /*
2  * BMP image format decoder
3  * Copyright (c) 2005 Mans Rullgard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include <inttypes.h>
23
24 #include "avcodec.h"
25 #include "bytestream.h"
26 #include "bmp.h"
27 #include "internal.h"
28 #include "msrledec.h"
29
30 static int bmp_decode_frame(AVCodecContext *avctx,
31                             void *data, int *got_frame,
32                             AVPacket *avpkt)
33 {
34     const uint8_t *buf = avpkt->data;
35     int buf_size       = avpkt->size;
36     AVFrame *p         = data;
37     unsigned int fsize, hsize;
38     int width, height;
39     unsigned int depth;
40     BiCompression comp;
41     unsigned int ihsize;
42     int i, j, n, linesize, ret;
43     uint32_t rgb[3] = {0};
44     uint32_t alpha = 0;
45     uint8_t *ptr;
46     int dsize;
47     const uint8_t *buf0 = buf;
48     GetByteContext gb;
49
50     if (buf_size < 14) {
51         av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\n", buf_size);
52         return AVERROR_INVALIDDATA;
53     }
54
55     if (bytestream_get_byte(&buf) != 'B' ||
56         bytestream_get_byte(&buf) != 'M') {
57         av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
58         return AVERROR_INVALIDDATA;
59     }
60
61     fsize = bytestream_get_le32(&buf);
62     if (buf_size < fsize) {
63         av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %u), trying to decode anyway\n",
64                buf_size, fsize);
65         fsize = buf_size;
66     }
67
68     buf += 2; /* reserved1 */
69     buf += 2; /* reserved2 */
70
71     hsize  = bytestream_get_le32(&buf); /* header size */
72     ihsize = bytestream_get_le32(&buf); /* more header size */
73     if (ihsize + 14 > hsize) {
74         av_log(avctx, AV_LOG_ERROR, "invalid header size %u\n", hsize);
75         return AVERROR_INVALIDDATA;
76     }
77
78     /* sometimes file size is set to some headers size, set a real size in that case */
79     if (fsize == 14 || fsize == ihsize + 14)
80         fsize = buf_size - 2;
81
82     if (fsize <= hsize) {
83         av_log(avctx, AV_LOG_ERROR,
84                "Declared file size is less than header size (%u < %u)\n",
85                fsize, hsize);
86         return AVERROR_INVALIDDATA;
87     }
88
89     switch (ihsize) {
90     case  40: // windib
91     case  56: // windib v3
92     case  64: // OS/2 v2
93     case 108: // windib v4
94     case 124: // windib v5
95         width  = bytestream_get_le32(&buf);
96         height = bytestream_get_le32(&buf);
97         break;
98     case  12: // OS/2 v1
99         width  = bytestream_get_le16(&buf);
100         height = bytestream_get_le16(&buf);
101         break;
102     default:
103         av_log(avctx, AV_LOG_ERROR, "unsupported BMP file, patch welcome\n");
104         return AVERROR_PATCHWELCOME;
105     }
106
107     /* planes */
108     if (bytestream_get_le16(&buf) != 1) {
109         av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n");
110         return AVERROR_INVALIDDATA;
111     }
112
113     depth = bytestream_get_le16(&buf);
114
115     if (ihsize >= 40)
116         comp = bytestream_get_le32(&buf);
117     else
118         comp = BMP_RGB;
119
120     if (comp != BMP_RGB && comp != BMP_BITFIELDS && comp != BMP_RLE4 &&
121         comp != BMP_RLE8) {
122         av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp);
123         return AVERROR_INVALIDDATA;
124     }
125
126     if (comp == BMP_BITFIELDS) {
127         buf += 20;
128         rgb[0] = bytestream_get_le32(&buf);
129         rgb[1] = bytestream_get_le32(&buf);
130         rgb[2] = bytestream_get_le32(&buf);
131         alpha = bytestream_get_le32(&buf);
132     }
133
134     avctx->width  = width;
135     avctx->height = height > 0 ? height : -height;
136
137     avctx->pix_fmt = AV_PIX_FMT_NONE;
138
139     switch (depth) {
140     case 32:
141         if (comp == BMP_BITFIELDS) {
142             if (rgb[0] == 0xFF000000 && rgb[1] == 0x00FF0000 && rgb[2] == 0x0000FF00)
143                 avctx->pix_fmt = alpha ? AV_PIX_FMT_ABGR : AV_PIX_FMT_0BGR;
144             else if (rgb[0] == 0x00FF0000 && rgb[1] == 0x0000FF00 && rgb[2] == 0x000000FF)
145                 avctx->pix_fmt = alpha ? AV_PIX_FMT_BGRA : AV_PIX_FMT_BGR0;
146             else if (rgb[0] == 0x0000FF00 && rgb[1] == 0x00FF0000 && rgb[2] == 0xFF000000)
147                 avctx->pix_fmt = alpha ? AV_PIX_FMT_ARGB : AV_PIX_FMT_0RGB;
148             else if (rgb[0] == 0x000000FF && rgb[1] == 0x0000FF00 && rgb[2] == 0x00FF0000)
149                 avctx->pix_fmt = alpha ? AV_PIX_FMT_RGBA : AV_PIX_FMT_RGB0;
150             else {
151                 av_log(avctx, AV_LOG_ERROR, "Unknown bitfields %0X %0X %0X\n", rgb[0], rgb[1], rgb[2]);
152                 return AVERROR(EINVAL);
153             }
154         } else {
155             avctx->pix_fmt = AV_PIX_FMT_BGRA;
156         }
157         break;
158     case 24:
159         avctx->pix_fmt = AV_PIX_FMT_BGR24;
160         break;
161     case 16:
162         if (comp == BMP_RGB)
163             avctx->pix_fmt = AV_PIX_FMT_RGB555;
164         else if (comp == BMP_BITFIELDS) {
165             if (rgb[0] == 0xF800 && rgb[1] == 0x07E0 && rgb[2] == 0x001F)
166                avctx->pix_fmt = AV_PIX_FMT_RGB565;
167             else if (rgb[0] == 0x7C00 && rgb[1] == 0x03E0 && rgb[2] == 0x001F)
168                avctx->pix_fmt = AV_PIX_FMT_RGB555;
169             else if (rgb[0] == 0x0F00 && rgb[1] == 0x00F0 && rgb[2] == 0x000F)
170                avctx->pix_fmt = AV_PIX_FMT_RGB444;
171             else {
172                av_log(avctx, AV_LOG_ERROR,
173                       "Unknown bitfields %0"PRIX32" %0"PRIX32" %0"PRIX32"\n",
174                       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 = AV_PIX_FMT_PAL8;
182         else
183             avctx->pix_fmt = AV_PIX_FMT_GRAY8;
184         break;
185     case 1:
186     case 4:
187         if (hsize - ihsize - 14 > 0) {
188             avctx->pix_fmt = AV_PIX_FMT_PAL8;
189         } else {
190             av_log(avctx, AV_LOG_ERROR, "Unknown palette for %u-colour BMP\n",
191                    1 << depth);
192             return AVERROR_INVALIDDATA;
193         }
194         break;
195     default:
196         av_log(avctx, AV_LOG_ERROR, "depth %u not supported\n", depth);
197         return AVERROR_INVALIDDATA;
198     }
199
200     if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
201         av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
202         return AVERROR_INVALIDDATA;
203     }
204
205     if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
206         return ret;
207     p->pict_type = AV_PICTURE_TYPE_I;
208     p->key_frame = 1;
209
210     buf   = buf0 + hsize;
211     dsize = buf_size - hsize;
212
213     /* Line size in file multiple of 4 */
214     n = ((avctx->width * depth + 31) / 8) & ~3;
215
216     if (n * avctx->height > dsize && comp != BMP_RLE4 && comp != BMP_RLE8) {
217         av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
218                dsize, n * avctx->height);
219         return AVERROR_INVALIDDATA;
220     }
221
222     // RLE may skip decoding some picture areas, so blank picture before decoding
223     if (comp == BMP_RLE4 || comp == BMP_RLE8)
224         memset(p->data[0], 0, avctx->height * p->linesize[0]);
225
226     if (height > 0) {
227         ptr      = p->data[0] + (avctx->height - 1) * p->linesize[0];
228         linesize = -p->linesize[0];
229     } else {
230         ptr      = p->data[0];
231         linesize = p->linesize[0];
232     }
233
234     if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
235         int colors = 1 << depth;
236
237         memset(p->data[1], 0, 1024);
238
239         if (ihsize >= 36) {
240             int t;
241             buf = buf0 + 46;
242             t   = bytestream_get_le32(&buf);
243             if (t < 0 || t > (1 << depth)) {
244                 av_log(avctx, AV_LOG_ERROR,
245                        "Incorrect number of colors - %X for bitdepth %u\n",
246                        t, depth);
247             } else if (t) {
248                 colors = t;
249             }
250         }
251         buf = buf0 + 14 + ihsize; //palette location
252         // OS/2 bitmap, 3 bytes per palette entry
253         if ((hsize-ihsize-14) < (colors << 2)) {
254             if ((hsize-ihsize-14) < colors * 3) {
255                 av_log(avctx, AV_LOG_ERROR, "palette doesn't fit in packet\n");
256                 return AVERROR_INVALIDDATA;
257             }
258             for (i = 0; i < colors; i++)
259                 ((uint32_t*)p->data[1])[i] = (0xFFU<<24) | bytestream_get_le24(&buf);
260         } else {
261             for (i = 0; i < colors; i++)
262                 ((uint32_t*)p->data[1])[i] = 0xFFU << 24 | bytestream_get_le32(&buf);
263         }
264         buf = buf0 + hsize;
265     }
266     if (comp == BMP_RLE4 || comp == BMP_RLE8) {
267         if (comp == BMP_RLE8 && height < 0) {
268             p->data[0]    +=  p->linesize[0] * (avctx->height - 1);
269             p->linesize[0] = -p->linesize[0];
270         }
271         bytestream2_init(&gb, buf, dsize);
272         ff_msrle_decode(avctx, (AVPicture*)p, depth, &gb);
273         if (height < 0) {
274             p->data[0]    +=  p->linesize[0] * (avctx->height - 1);
275             p->linesize[0] = -p->linesize[0];
276         }
277     } else {
278         switch (depth) {
279         case 1:
280             for (i = 0; i < avctx->height; i++) {
281                 int j;
282                 for (j = 0; j < n; j++) {
283                     ptr[j*8+0] =  buf[j] >> 7;
284                     ptr[j*8+1] = (buf[j] >> 6) & 1;
285                     ptr[j*8+2] = (buf[j] >> 5) & 1;
286                     ptr[j*8+3] = (buf[j] >> 4) & 1;
287                     ptr[j*8+4] = (buf[j] >> 3) & 1;
288                     ptr[j*8+5] = (buf[j] >> 2) & 1;
289                     ptr[j*8+6] = (buf[j] >> 1) & 1;
290                     ptr[j*8+7] =  buf[j]       & 1;
291                 }
292                 buf += n;
293                 ptr += linesize;
294             }
295             break;
296         case 8:
297         case 24:
298         case 32:
299             for (i = 0; i < avctx->height; i++) {
300                 memcpy(ptr, buf, n);
301                 buf += n;
302                 ptr += linesize;
303             }
304             break;
305         case 4:
306             for (i = 0; i < avctx->height; i++) {
307                 int j;
308                 for (j = 0; j < n; j++) {
309                     ptr[j*2+0] = (buf[j] >> 4) & 0xF;
310                     ptr[j*2+1] = buf[j] & 0xF;
311                 }
312                 buf += n;
313                 ptr += linesize;
314             }
315             break;
316         case 16:
317             for (i = 0; i < avctx->height; i++) {
318                 const uint16_t *src = (const uint16_t *) buf;
319                 uint16_t *dst       = (uint16_t *) ptr;
320
321                 for (j = 0; j < avctx->width; j++)
322                     *dst++ = av_le2ne16(*src++);
323
324                 buf += n;
325                 ptr += linesize;
326             }
327             break;
328         default:
329             av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
330             return AVERROR_INVALIDDATA;
331         }
332     }
333
334     *got_frame = 1;
335
336     return buf_size;
337 }
338
339 AVCodec ff_bmp_decoder = {
340     .name           = "bmp",
341     .long_name      = NULL_IF_CONFIG_SMALL("BMP (Windows and OS/2 bitmap)"),
342     .type           = AVMEDIA_TYPE_VIDEO,
343     .id             = AV_CODEC_ID_BMP,
344     .decode         = bmp_decode_frame,
345     .capabilities   = CODEC_CAP_DR1,
346 };