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