]> git.sesse.net Git - ffmpeg/blob - libavcodec/bmp.c
docs: remove extra sar entry for scale filter
[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 "msrledec.h"
26
27 static av_cold int bmp_decode_init(AVCodecContext *avctx){
28     BMPContext *s = avctx->priv_data;
29
30     avcodec_get_frame_defaults((AVFrame*)&s->picture);
31     avctx->coded_frame = (AVFrame*)&s->picture;
32
33     return 0;
34 }
35
36 static int bmp_decode_frame(AVCodecContext *avctx,
37                             void *data, int *data_size,
38                             AVPacket *avpkt)
39 {
40     const uint8_t *buf = avpkt->data;
41     int buf_size = avpkt->size;
42     BMPContext *s = avctx->priv_data;
43     AVFrame *picture = data;
44     AVFrame *p = &s->picture;
45     unsigned int fsize, hsize;
46     int width, height;
47     unsigned int depth;
48     BiCompression comp;
49     unsigned int ihsize;
50     int i, j, n, linesize;
51     uint32_t rgb[3];
52     uint32_t alpha = 0;
53     uint8_t *ptr;
54     int dsize;
55     const uint8_t *buf0 = buf;
56
57     if(buf_size < 14){
58         av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\n", buf_size);
59         return -1;
60     }
61
62     if(bytestream_get_byte(&buf) != 'B' ||
63        bytestream_get_byte(&buf) != 'M') {
64         av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
65         return -1;
66     }
67
68     fsize = bytestream_get_le32(&buf);
69     if(buf_size < fsize){
70         av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d), trying to decode anyway\n",
71                buf_size, fsize);
72         fsize = buf_size;
73     }
74
75     buf += 2; /* reserved1 */
76     buf += 2; /* reserved2 */
77
78     hsize = bytestream_get_le32(&buf); /* header size */
79     ihsize = bytestream_get_le32(&buf);       /* more header size */
80     if(ihsize + 14 > hsize){
81         av_log(avctx, AV_LOG_ERROR, "invalid header size %d\n", hsize);
82         return -1;
83     }
84
85     /* sometimes file size is set to some headers size, set a real size in that case */
86     if(fsize == 14 || fsize == ihsize + 14)
87         fsize = buf_size - 2;
88
89     if(fsize <= hsize){
90         av_log(avctx, AV_LOG_ERROR, "declared file size is less than header size (%d < %d)\n",
91                fsize, hsize);
92         return -1;
93     }
94
95     switch(ihsize){
96     case  40: // windib
97     case  56: // windib v3
98     case  64: // OS/2 v2
99     case 108: // windib v4
100     case 124: // windib v5
101         width = bytestream_get_le32(&buf);
102         height = bytestream_get_le32(&buf);
103         break;
104     case  12: // OS/2 v1
105         width  = bytestream_get_le16(&buf);
106         height = bytestream_get_le16(&buf);
107         break;
108     default:
109         av_log(avctx, AV_LOG_ERROR, "unsupported BMP file, patch welcome\n");
110         return -1;
111     }
112
113     if(bytestream_get_le16(&buf) != 1){ /* planes */
114         av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n");
115         return -1;
116     }
117
118     depth = bytestream_get_le16(&buf);
119
120     if(ihsize == 40 || ihsize == 64 || ihsize == 56)
121         comp = bytestream_get_le32(&buf);
122     else
123         comp = BMP_RGB;
124
125     if(comp != BMP_RGB && comp != BMP_BITFIELDS && comp != BMP_RLE4 && comp != BMP_RLE8){
126         av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp);
127         return -1;
128     }
129
130     if(comp == BMP_BITFIELDS){
131         buf += 20;
132         rgb[0] = bytestream_get_le32(&buf);
133         rgb[1] = bytestream_get_le32(&buf);
134         rgb[2] = bytestream_get_le32(&buf);
135         if (ihsize >= 108)
136             alpha = bytestream_get_le32(&buf);
137     }
138
139     avctx->width = width;
140     avctx->height = height > 0? height: -height;
141
142     avctx->pix_fmt = PIX_FMT_NONE;
143
144     switch(depth){
145     case 32:
146         if(comp == BMP_BITFIELDS){
147             if (rgb[0] == 0xFF000000 && rgb[1] == 0x00FF0000 && rgb[2] == 0x0000FF00)
148                 avctx->pix_fmt = alpha ? PIX_FMT_ABGR : PIX_FMT_0BGR;
149             else if (rgb[0] == 0x00FF0000 && rgb[1] == 0x0000FF00 && rgb[2] == 0x000000FF)
150                 avctx->pix_fmt = alpha ? PIX_FMT_BGRA : PIX_FMT_BGR0;
151             else if (rgb[0] == 0x0000FF00 && rgb[1] == 0x00FF0000 && rgb[2] == 0xFF000000)
152                 avctx->pix_fmt = alpha ? PIX_FMT_ARGB : PIX_FMT_0RGB;
153             else if (rgb[0] == 0x000000FF && rgb[1] == 0x0000FF00 && rgb[2] == 0x00FF0000)
154                 avctx->pix_fmt = alpha ? PIX_FMT_RGBA : PIX_FMT_RGB0;
155             else {
156                 av_log(avctx, AV_LOG_ERROR, "Unknown bitfields %0X %0X %0X\n", rgb[0], rgb[1], rgb[2]);
157                 return AVERROR(EINVAL);
158             }
159         } else {
160             avctx->pix_fmt = PIX_FMT_BGRA;
161         }
162         break;
163     case 24:
164         avctx->pix_fmt = PIX_FMT_BGR24;
165         break;
166     case 16:
167         if(comp == BMP_RGB)
168             avctx->pix_fmt = PIX_FMT_RGB555;
169         else if (comp == BMP_BITFIELDS) {
170             if (rgb[0] == 0xF800 && rgb[1] == 0x07E0 && rgb[2] == 0x001F)
171                avctx->pix_fmt = PIX_FMT_RGB565;
172             else if (rgb[0] == 0x7C00 && rgb[1] == 0x03E0 && rgb[2] == 0x001F)
173                avctx->pix_fmt = PIX_FMT_RGB555;
174             else if (rgb[0] == 0x0F00 && rgb[1] == 0x00F0 && rgb[2] == 0x000F)
175                avctx->pix_fmt = PIX_FMT_RGB444;
176             else {
177                av_log(avctx, AV_LOG_ERROR, "Unknown bitfields %0X %0X %0X\n", rgb[0], rgb[1], rgb[2]);
178                return AVERROR(EINVAL);
179             }
180         }
181         break;
182     case 8:
183         if(hsize - ihsize - 14 > 0)
184             avctx->pix_fmt = PIX_FMT_PAL8;
185         else
186             avctx->pix_fmt = PIX_FMT_GRAY8;
187         break;
188     case 1:
189     case 4:
190         if(hsize - ihsize - 14 > 0){
191             avctx->pix_fmt = PIX_FMT_PAL8;
192         }else{
193             av_log(avctx, AV_LOG_ERROR, "Unknown palette for %d-colour BMP\n", 1<<depth);
194             return -1;
195         }
196         break;
197     default:
198         av_log(avctx, AV_LOG_ERROR, "depth %d not supported\n", depth);
199         return -1;
200     }
201
202     if(avctx->pix_fmt == PIX_FMT_NONE){
203         av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
204         return -1;
205     }
206
207     if(p->data[0])
208         avctx->release_buffer(avctx, p);
209
210     p->reference = 0;
211     if(avctx->get_buffer(avctx, p) < 0){
212         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
213         return -1;
214     }
215     p->pict_type = AV_PICTURE_TYPE_I;
216     p->key_frame = 1;
217
218     buf = buf0 + hsize;
219     dsize = buf_size - hsize;
220
221     /* Line size in file multiple of 4 */
222     n = ((avctx->width * depth + 31) / 8) & ~3;
223
224     if(n * avctx->height > dsize && comp != BMP_RLE4 && comp != BMP_RLE8){
225         av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
226                dsize, n * avctx->height);
227         return -1;
228     }
229
230     // RLE may skip decoding some picture areas, so blank picture before decoding
231     if(comp == BMP_RLE4 || comp == BMP_RLE8)
232         memset(p->data[0], 0, avctx->height * p->linesize[0]);
233
234     if(depth == 4 || depth == 8)
235         memset(p->data[1], 0, 1024);
236
237     if(height > 0){
238         ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
239         linesize = -p->linesize[0];
240     } else {
241         ptr = p->data[0];
242         linesize = p->linesize[0];
243     }
244
245     if(avctx->pix_fmt == PIX_FMT_PAL8){
246         int colors = 1 << depth;
247         if(ihsize >= 36){
248             int t;
249             buf = buf0 + 46;
250             t = bytestream_get_le32(&buf);
251             if(t < 0 || t > (1 << depth)){
252                 av_log(avctx, AV_LOG_ERROR, "Incorrect number of colors - %X for bitdepth %d\n", t, depth);
253             }else if(t){
254                 colors = t;
255             }
256         }
257         buf = buf0 + 14 + ihsize; //palette location
258         if((hsize-ihsize-14) < (colors << 2)){ // OS/2 bitmap, 3 bytes per palette entry
259             for(i = 0; i < colors; i++)
260                 ((uint32_t*)p->data[1])[i] = (0xff<<24) | bytestream_get_le24(&buf);
261         }else{
262             for(i = 0; i < colors; i++)
263                 ((uint32_t*)p->data[1])[i] = 0xFFU << 24 | bytestream_get_le32(&buf);
264         }
265         buf = buf0 + hsize;
266     }
267     if(comp == BMP_RLE4 || comp == BMP_RLE8){
268         if(height < 0){
269             p->data[0] += p->linesize[0] * (avctx->height - 1);
270             p->linesize[0] = -p->linesize[0];
271         }
272         ff_msrle_decode(avctx, (AVPicture*)p, depth, buf, dsize);
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 -1;
331         }
332     }
333
334     *picture = s->picture;
335     *data_size = sizeof(AVPicture);
336
337     return buf_size;
338 }
339
340 static av_cold int bmp_decode_end(AVCodecContext *avctx)
341 {
342     BMPContext* c = avctx->priv_data;
343
344     if (c->picture.data[0])
345         avctx->release_buffer(avctx, &c->picture);
346
347     return 0;
348 }
349
350 AVCodec ff_bmp_decoder = {
351     .name           = "bmp",
352     .type           = AVMEDIA_TYPE_VIDEO,
353     .id             = CODEC_ID_BMP,
354     .priv_data_size = sizeof(BMPContext),
355     .init           = bmp_decode_init,
356     .close          = bmp_decode_end,
357     .decode         = bmp_decode_frame,
358     .capabilities   = CODEC_CAP_DR1,
359     .long_name = NULL_IF_CONFIG_SMALL("BMP image"),
360 };