]> git.sesse.net Git - ffmpeg/blob - libavcodec/bmp.c
nvenc: Allow different const qps for I, P and B frames
[ffmpeg] / 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         avpriv_report_missing_feature(avctx, "Information header size %u",
102                                       ihsize);
103         return AVERROR_PATCHWELCOME;
104     }
105
106     /* planes */
107     if (bytestream_get_le16(&buf) != 1) {
108         av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n");
109         return AVERROR_INVALIDDATA;
110     }
111
112     depth = bytestream_get_le16(&buf);
113
114     if (ihsize == 40)
115         comp = bytestream_get_le32(&buf);
116     else
117         comp = BMP_RGB;
118
119     if (comp != BMP_RGB && comp != BMP_BITFIELDS && comp != BMP_RLE4 &&
120         comp != BMP_RLE8) {
121         av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp);
122         return AVERROR_INVALIDDATA;
123     }
124
125     if (comp == BMP_BITFIELDS) {
126         buf += 20;
127         rgb[0] = bytestream_get_le32(&buf);
128         rgb[1] = bytestream_get_le32(&buf);
129         rgb[2] = bytestream_get_le32(&buf);
130     }
131
132     avctx->width  = width;
133     avctx->height = height > 0 ? height : -height;
134
135     avctx->pix_fmt = AV_PIX_FMT_NONE;
136
137     switch (depth) {
138     case 32:
139         if (comp == BMP_BITFIELDS) {
140             rgb[0] = (rgb[0] >> 15) & 3;
141             rgb[1] = (rgb[1] >> 15) & 3;
142             rgb[2] = (rgb[2] >> 15) & 3;
143
144             if (rgb[0] + rgb[1] + rgb[2] != 3 ||
145                 rgb[0] == rgb[1] || rgb[0] == rgb[2] || rgb[1] == rgb[2]) {
146                 break;
147             }
148         } else {
149             rgb[0] = 2;
150             rgb[1] = 1;
151             rgb[2] = 0;
152         }
153
154         avctx->pix_fmt = AV_PIX_FMT_BGR24;
155         break;
156     case 24:
157         avctx->pix_fmt = AV_PIX_FMT_BGR24;
158         break;
159     case 16:
160         if (comp == BMP_RGB)
161             avctx->pix_fmt = AV_PIX_FMT_RGB555;
162         else if (comp == BMP_BITFIELDS) {
163             if (rgb[0] == 0xF800 && rgb[1] == 0x07E0 && rgb[2] == 0x001F)
164                avctx->pix_fmt = AV_PIX_FMT_RGB565;
165             else if (rgb[0] == 0x7C00 && rgb[1] == 0x03E0 && rgb[2] == 0x001F)
166                avctx->pix_fmt = AV_PIX_FMT_RGB555;
167             else if (rgb[0] == 0x0F00 && rgb[1] == 0x00F0 && rgb[2] == 0x000F)
168                avctx->pix_fmt = AV_PIX_FMT_RGB444;
169             else {
170                av_log(avctx, AV_LOG_ERROR,
171                       "Unknown bitfields %0"PRIX32" %0"PRIX32" %0"PRIX32"\n",
172                       rgb[0], rgb[1], rgb[2]);
173                return AVERROR(EINVAL);
174             }
175         }
176         break;
177     case 8:
178         if (hsize - ihsize - 14 > 0)
179             avctx->pix_fmt = AV_PIX_FMT_PAL8;
180         else
181             avctx->pix_fmt = AV_PIX_FMT_GRAY8;
182         break;
183     case 1:
184     case 4:
185         if (hsize - ihsize - 14 > 0) {
186             avctx->pix_fmt = AV_PIX_FMT_PAL8;
187         } else {
188             av_log(avctx, AV_LOG_ERROR, "Unknown palette for %u-colour BMP\n",
189                    1 << depth);
190             return AVERROR_INVALIDDATA;
191         }
192         break;
193     default:
194         av_log(avctx, AV_LOG_ERROR, "depth %u not supported\n", depth);
195         return AVERROR_INVALIDDATA;
196     }
197
198     if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
199         av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
200         return AVERROR_INVALIDDATA;
201     }
202
203     if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
204         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
205         return ret;
206     }
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) / 8 + 3) & ~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             for (i = 0; i < colors; i++)
255                 ((uint32_t*)p->data[1])[i] = bytestream_get_le24(&buf);
256         } else {
257             for (i = 0; i < colors; i++)
258                 ((uint32_t*)p->data[1])[i] = bytestream_get_le32(&buf);
259         }
260         buf = buf0 + hsize;
261     }
262     if (comp == BMP_RLE4 || comp == BMP_RLE8) {
263         if (height < 0) {
264             p->data[0]    +=  p->linesize[0] * (avctx->height - 1);
265             p->linesize[0] = -p->linesize[0];
266         }
267         bytestream2_init(&gb, buf, dsize);
268         ff_msrle_decode(avctx, p, depth, &gb);
269         if (height < 0) {
270             p->data[0]    +=  p->linesize[0] * (avctx->height - 1);
271             p->linesize[0] = -p->linesize[0];
272         }
273     } else {
274         switch (depth) {
275         case 1:
276             for (i = 0; i < avctx->height; i++) {
277                 int j;
278                 for (j = 0; j < n; j++) {
279                     ptr[j*8+0] =  buf[j] >> 7;
280                     ptr[j*8+1] = (buf[j] >> 6) & 1;
281                     ptr[j*8+2] = (buf[j] >> 5) & 1;
282                     ptr[j*8+3] = (buf[j] >> 4) & 1;
283                     ptr[j*8+4] = (buf[j] >> 3) & 1;
284                     ptr[j*8+5] = (buf[j] >> 2) & 1;
285                     ptr[j*8+6] = (buf[j] >> 1) & 1;
286                     ptr[j*8+7] =  buf[j]       & 1;
287                 }
288                 buf += n;
289                 ptr += linesize;
290             }
291             break;
292         case 8:
293         case 24:
294             for (i = 0; i < avctx->height; i++) {
295                 memcpy(ptr, buf, n);
296                 buf += n;
297                 ptr += linesize;
298             }
299             break;
300         case 4:
301             for (i = 0; i < avctx->height; i++) {
302                 int j;
303                 for (j = 0; j < n; j++) {
304                     ptr[j*2+0] = (buf[j] >> 4) & 0xF;
305                     ptr[j*2+1] = buf[j] & 0xF;
306                 }
307                 buf += n;
308                 ptr += linesize;
309             }
310             break;
311         case 16:
312             for (i = 0; i < avctx->height; i++) {
313                 const uint16_t *src = (const uint16_t *) buf;
314                 uint16_t *dst       = (uint16_t *) ptr;
315
316                 for (j = 0; j < avctx->width; j++)
317                     *dst++ = av_le2ne16(*src++);
318
319                 buf += n;
320                 ptr += linesize;
321             }
322             break;
323         case 32:
324             for (i = 0; i < avctx->height; i++) {
325                 const uint8_t *src = buf;
326                 uint8_t *dst       = ptr;
327
328                 for (j = 0; j < avctx->width; j++) {
329                     dst[0] = src[rgb[2]];
330                     dst[1] = src[rgb[1]];
331                     dst[2] = src[rgb[0]];
332                     dst += 3;
333                     src += 4;
334                 }
335
336                 buf += n;
337                 ptr += linesize;
338             }
339             break;
340         default:
341             av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
342             return AVERROR_INVALIDDATA;
343         }
344     }
345
346     *got_frame = 1;
347
348     return buf_size;
349 }
350
351 AVCodec ff_bmp_decoder = {
352     .name           = "bmp",
353     .long_name      = NULL_IF_CONFIG_SMALL("BMP (Windows and OS/2 bitmap)"),
354     .type           = AVMEDIA_TYPE_VIDEO,
355     .id             = AV_CODEC_ID_BMP,
356     .decode         = bmp_decode_frame,
357     .capabilities   = AV_CODEC_CAP_DR1,
358 };