]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavcodec/bmp.c
Add decode_end method to bmp decoder. Patch by Michel Bardiaux,
[frescor/ffmpeg.git] / 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
26 static int bmp_decode_init(AVCodecContext *avctx){
27     BMPContext *s = avctx->priv_data;
28
29     avcodec_get_frame_defaults((AVFrame*)&s->picture);
30     avctx->coded_frame = (AVFrame*)&s->picture;
31
32     return 0;
33 }
34
35 static int bmp_decode_frame(AVCodecContext *avctx,
36                             void *data, int *data_size,
37                             uint8_t *buf, int buf_size)
38 {
39     BMPContext *s = avctx->priv_data;
40     AVFrame *picture = data;
41     AVFrame *p = &s->picture;
42     unsigned int fsize, hsize;
43     int width, height;
44     unsigned int depth;
45     BiCompression comp;
46     unsigned int ihsize;
47     int i, j, n, linesize;
48     uint32_t rgb[3];
49     uint8_t *ptr;
50     int dsize;
51     uint8_t *buf0 = buf;
52
53     if(buf_size < 14){
54         av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\n", buf_size);
55         return -1;
56     }
57
58     if(bytestream_get_byte(&buf) != 'B' ||
59        bytestream_get_byte(&buf) != 'M') {
60         av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
61         return -1;
62     }
63
64     fsize = bytestream_get_le32(&buf);
65     if(buf_size < fsize){
66         av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
67                buf_size, fsize);
68         return -1;
69     }
70
71     buf += 2; /* reserved1 */
72     buf += 2; /* reserved2 */
73
74     hsize = bytestream_get_le32(&buf); /* header size */
75     if(fsize <= hsize){
76         av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
77                fsize, hsize);
78         return -1;
79     }
80
81     ihsize = bytestream_get_le32(&buf);       /* more header size */
82     if(ihsize + 14 > hsize){
83         av_log(avctx, AV_LOG_ERROR, "invalid header size %d\n", hsize);
84         return -1;
85     }
86
87     width = bytestream_get_le32(&buf);
88     height = bytestream_get_le32(&buf);
89
90     if(bytestream_get_le16(&buf) != 1){ /* planes */
91         av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n");
92         return -1;
93     }
94
95     depth = bytestream_get_le16(&buf);
96
97     if(ihsize > 16)
98         comp = bytestream_get_le32(&buf);
99     else
100         comp = BMP_RGB;
101
102     if(comp != BMP_RGB && comp != BMP_BITFIELDS){
103         av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp);
104         return -1;
105     }
106
107     if(comp == BMP_BITFIELDS){
108         buf += 20;
109         rgb[0] = bytestream_get_le32(&buf);
110         rgb[1] = bytestream_get_le32(&buf);
111         rgb[2] = bytestream_get_le32(&buf);
112     }
113
114     avctx->codec_id = CODEC_ID_BMP;
115     avctx->width = width;
116     avctx->height = height > 0? height: -height;
117
118     avctx->pix_fmt = PIX_FMT_NONE;
119
120     switch(depth){
121     case 32:
122         if(comp == BMP_BITFIELDS){
123             rgb[0] = (rgb[0] >> 15) & 3;
124             rgb[1] = (rgb[1] >> 15) & 3;
125             rgb[2] = (rgb[2] >> 15) & 3;
126
127             if(rgb[0] + rgb[1] + rgb[2] != 3 ||
128                rgb[0] == rgb[1] || rgb[0] == rgb[2] || rgb[1] == rgb[2]){
129                 break;
130             }
131         } else {
132             rgb[0] = 2;
133             rgb[1] = 1;
134             rgb[2] = 0;
135         }
136
137         avctx->pix_fmt = PIX_FMT_BGR24;
138         break;
139     case 24:
140         avctx->pix_fmt = PIX_FMT_BGR24;
141         break;
142     case 16:
143         if(comp == BMP_RGB)
144             avctx->pix_fmt = PIX_FMT_RGB555;
145         break;
146     default:
147         av_log(avctx, AV_LOG_ERROR, "depth %d not supported\n", depth);
148         return -1;
149     }
150
151     if(avctx->pix_fmt == PIX_FMT_NONE){
152         av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
153         return -1;
154     }
155
156     if(p->data[0])
157         avctx->release_buffer(avctx, p);
158
159     p->reference = 0;
160     if(avctx->get_buffer(avctx, p) < 0){
161         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
162         return -1;
163     }
164     p->pict_type = FF_I_TYPE;
165     p->key_frame = 1;
166
167     buf = buf0 + hsize;
168     dsize = buf_size - hsize;
169
170     /* Line size in file multiple of 4 */
171     n = (avctx->width * (depth / 8) + 3) & ~3;
172
173     if(n * avctx->height > dsize){
174         av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
175                dsize, n * avctx->height);
176         return -1;
177     }
178
179     if(height > 0){
180         ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
181         linesize = -p->linesize[0];
182     } else {
183         ptr = p->data[0];
184         linesize = p->linesize[0];
185     }
186
187     switch(depth){
188     case 24:
189         for(i = 0; i < avctx->height; i++){
190             memcpy(ptr, buf, avctx->width*(depth>>3));
191             buf += n;
192             ptr += linesize;
193         }
194         break;
195     case 16:
196         for(i = 0; i < avctx->height; i++){
197             uint16_t *src = (uint16_t *) buf;
198             uint16_t *dst = (uint16_t *) ptr;
199
200             for(j = 0; j < avctx->width; j++)
201                 *dst++ = le2me_16(*src++);
202
203             buf += n;
204             ptr += linesize;
205         }
206         break;
207     case 32:
208         for(i = 0; i < avctx->height; i++){
209             uint8_t *src = buf;
210             uint8_t *dst = ptr;
211
212             for(j = 0; j < avctx->width; j++){
213                 dst[0] = src[rgb[2]];
214                 dst[1] = src[rgb[1]];
215                 dst[2] = src[rgb[0]];
216                 dst += 3;
217                 src += 4;
218             }
219
220             buf += n;
221             ptr += linesize;
222         }
223         break;
224     default:
225         av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
226         return -1;
227     }
228
229     *picture = s->picture;
230     *data_size = sizeof(AVPicture);
231
232     return buf_size;
233 }
234
235 static int bmp_decode_end(AVCodecContext *avctx)
236 {
237     BMPContext* c = avctx->priv_data;
238
239     if (c->picture.data[0])
240         avctx->release_buffer(avctx, &c->picture);
241
242     return 0;
243 }
244
245 AVCodec bmp_decoder = {
246     "bmp",
247     CODEC_TYPE_VIDEO,
248     CODEC_ID_BMP,
249     sizeof(BMPContext),
250     bmp_decode_init,
251     NULL,
252     bmp_decode_end,
253     bmp_decode_frame
254 };