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