OSDN Git Service

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