00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "avcodec.h"
00023 #include "bytestream.h"
00024 #include "bmp.h"
00025 #include "msrledec.h"
00026
00027 static av_cold int bmp_decode_init(AVCodecContext *avctx){
00028 BMPContext *s = avctx->priv_data;
00029
00030 avcodec_get_frame_defaults((AVFrame*)&s->picture);
00031 avctx->coded_frame = (AVFrame*)&s->picture;
00032
00033 return 0;
00034 }
00035
00036 static int bmp_decode_frame(AVCodecContext *avctx,
00037 void *data, int *data_size,
00038 AVPacket *avpkt)
00039 {
00040 const uint8_t *buf = avpkt->data;
00041 int buf_size = avpkt->size;
00042 BMPContext *s = avctx->priv_data;
00043 AVFrame *picture = data;
00044 AVFrame *p = &s->picture;
00045 unsigned int fsize, hsize;
00046 int width, height;
00047 unsigned int depth;
00048 BiCompression comp;
00049 unsigned int ihsize;
00050 int i, j, n, linesize;
00051 uint32_t rgb[3];
00052 uint8_t *ptr;
00053 int dsize;
00054 const uint8_t *buf0 = buf;
00055
00056 if(buf_size < 14){
00057 av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\n", buf_size);
00058 return -1;
00059 }
00060
00061 if(bytestream_get_byte(&buf) != 'B' ||
00062 bytestream_get_byte(&buf) != 'M') {
00063 av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
00064 return -1;
00065 }
00066
00067 fsize = bytestream_get_le32(&buf);
00068 if(buf_size < fsize){
00069 av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d), trying to decode anyway\n",
00070 buf_size, fsize);
00071 fsize = buf_size;
00072 }
00073
00074 buf += 2;
00075 buf += 2;
00076
00077 hsize = bytestream_get_le32(&buf);
00078 ihsize = bytestream_get_le32(&buf);
00079 if(ihsize + 14 > hsize){
00080 av_log(avctx, AV_LOG_ERROR, "invalid header size %d\n", hsize);
00081 return -1;
00082 }
00083
00084
00085 if(fsize == 14 || fsize == ihsize + 14)
00086 fsize = buf_size - 2;
00087
00088 if(fsize <= hsize){
00089 av_log(avctx, AV_LOG_ERROR, "declared file size is less than header size (%d < %d)\n",
00090 fsize, hsize);
00091 return -1;
00092 }
00093
00094 switch(ihsize){
00095 case 40:
00096 case 56:
00097 case 64:
00098 case 108:
00099 case 124:
00100 width = bytestream_get_le32(&buf);
00101 height = bytestream_get_le32(&buf);
00102 break;
00103 case 12:
00104 width = bytestream_get_le16(&buf);
00105 height = bytestream_get_le16(&buf);
00106 break;
00107 default:
00108 av_log(avctx, AV_LOG_ERROR, "unsupported BMP file, patch welcome\n");
00109 return -1;
00110 }
00111
00112 if(bytestream_get_le16(&buf) != 1){
00113 av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n");
00114 return -1;
00115 }
00116
00117 depth = bytestream_get_le16(&buf);
00118
00119 if(ihsize == 40 || ihsize == 64 || ihsize == 56)
00120 comp = bytestream_get_le32(&buf);
00121 else
00122 comp = BMP_RGB;
00123
00124 if(comp != BMP_RGB && comp != BMP_BITFIELDS && comp != BMP_RLE4 && comp != BMP_RLE8){
00125 av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp);
00126 return -1;
00127 }
00128
00129 if(comp == BMP_BITFIELDS){
00130 buf += 20;
00131 rgb[0] = bytestream_get_le32(&buf);
00132 rgb[1] = bytestream_get_le32(&buf);
00133 rgb[2] = bytestream_get_le32(&buf);
00134 }
00135
00136 avctx->width = width;
00137 avctx->height = height > 0? height: -height;
00138
00139 avctx->pix_fmt = PIX_FMT_NONE;
00140
00141 switch(depth){
00142 case 32:
00143 if(comp == BMP_BITFIELDS){
00144 rgb[0] = (rgb[0] >> 15) & 3;
00145 rgb[1] = (rgb[1] >> 15) & 3;
00146 rgb[2] = (rgb[2] >> 15) & 3;
00147
00148 if(rgb[0] + rgb[1] + rgb[2] != 3 ||
00149 rgb[0] == rgb[1] || rgb[0] == rgb[2] || rgb[1] == rgb[2]){
00150 break;
00151 }
00152 } else {
00153 rgb[0] = 2;
00154 rgb[1] = 1;
00155 rgb[2] = 0;
00156 }
00157
00158 avctx->pix_fmt = PIX_FMT_BGRA;
00159 break;
00160 case 24:
00161 avctx->pix_fmt = PIX_FMT_BGR24;
00162 break;
00163 case 16:
00164 if(comp == BMP_RGB)
00165 avctx->pix_fmt = PIX_FMT_RGB555;
00166 if(comp == BMP_BITFIELDS)
00167 avctx->pix_fmt = rgb[1] == 0x07E0 ? PIX_FMT_RGB565 : PIX_FMT_RGB555;
00168 break;
00169 case 8:
00170 if(hsize - ihsize - 14 > 0)
00171 avctx->pix_fmt = PIX_FMT_PAL8;
00172 else
00173 avctx->pix_fmt = PIX_FMT_GRAY8;
00174 break;
00175 case 1:
00176 case 4:
00177 if(hsize - ihsize - 14 > 0){
00178 avctx->pix_fmt = PIX_FMT_PAL8;
00179 }else{
00180 av_log(avctx, AV_LOG_ERROR, "Unknown palette for %d-colour BMP\n", 1<<depth);
00181 return -1;
00182 }
00183 break;
00184 default:
00185 av_log(avctx, AV_LOG_ERROR, "depth %d not supported\n", depth);
00186 return -1;
00187 }
00188
00189 if(avctx->pix_fmt == PIX_FMT_NONE){
00190 av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
00191 return -1;
00192 }
00193
00194 if(p->data[0])
00195 avctx->release_buffer(avctx, p);
00196
00197 p->reference = 0;
00198 if(avctx->get_buffer(avctx, p) < 0){
00199 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00200 return -1;
00201 }
00202 p->pict_type = AV_PICTURE_TYPE_I;
00203 p->key_frame = 1;
00204
00205 buf = buf0 + hsize;
00206 dsize = buf_size - hsize;
00207
00208
00209 n = ((avctx->width * depth + 31) / 8) & ~3;
00210
00211 if(n * avctx->height > dsize && comp != BMP_RLE4 && comp != BMP_RLE8){
00212 av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
00213 dsize, n * avctx->height);
00214 return -1;
00215 }
00216
00217
00218 if(comp == BMP_RLE4 || comp == BMP_RLE8)
00219 memset(p->data[0], 0, avctx->height * p->linesize[0]);
00220
00221 if(depth == 4 || depth == 8)
00222 memset(p->data[1], 0, 1024);
00223
00224 if(height > 0){
00225 ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
00226 linesize = -p->linesize[0];
00227 } else {
00228 ptr = p->data[0];
00229 linesize = p->linesize[0];
00230 }
00231
00232 if(avctx->pix_fmt == PIX_FMT_PAL8){
00233 int colors = 1 << depth;
00234 if(ihsize >= 36){
00235 int t;
00236 buf = buf0 + 46;
00237 t = bytestream_get_le32(&buf);
00238 if(t < 0 || t > (1 << depth)){
00239 av_log(avctx, AV_LOG_ERROR, "Incorrect number of colors - %X for bitdepth %d\n", t, depth);
00240 }else if(t){
00241 colors = t;
00242 }
00243 }
00244 buf = buf0 + 14 + ihsize;
00245 if((hsize-ihsize-14) < (colors << 2)){
00246 for(i = 0; i < colors; i++)
00247 ((uint32_t*)p->data[1])[i] = (0xff<<24) | bytestream_get_le24(&buf);
00248 }else{
00249 for(i = 0; i < colors; i++)
00250 ((uint32_t*)p->data[1])[i] = bytestream_get_le32(&buf);
00251 }
00252 buf = buf0 + hsize;
00253 }
00254 if(comp == BMP_RLE4 || comp == BMP_RLE8){
00255 if(height < 0){
00256 p->data[0] += p->linesize[0] * (avctx->height - 1);
00257 p->linesize[0] = -p->linesize[0];
00258 }
00259 ff_msrle_decode(avctx, (AVPicture*)p, depth, buf, dsize);
00260 if(height < 0){
00261 p->data[0] += p->linesize[0] * (avctx->height - 1);
00262 p->linesize[0] = -p->linesize[0];
00263 }
00264 }else{
00265 switch(depth){
00266 case 1:
00267 for (i = 0; i < avctx->height; i++) {
00268 int j;
00269 for (j = 0; j < n; j++) {
00270 ptr[j*8+0] = buf[j] >> 7;
00271 ptr[j*8+1] = (buf[j] >> 6) & 1;
00272 ptr[j*8+2] = (buf[j] >> 5) & 1;
00273 ptr[j*8+3] = (buf[j] >> 4) & 1;
00274 ptr[j*8+4] = (buf[j] >> 3) & 1;
00275 ptr[j*8+5] = (buf[j] >> 2) & 1;
00276 ptr[j*8+6] = (buf[j] >> 1) & 1;
00277 ptr[j*8+7] = buf[j] & 1;
00278 }
00279 buf += n;
00280 ptr += linesize;
00281 }
00282 break;
00283 case 8:
00284 case 24:
00285 for(i = 0; i < avctx->height; i++){
00286 memcpy(ptr, buf, n);
00287 buf += n;
00288 ptr += linesize;
00289 }
00290 break;
00291 case 4:
00292 for(i = 0; i < avctx->height; i++){
00293 int j;
00294 for(j = 0; j < n; j++){
00295 ptr[j*2+0] = (buf[j] >> 4) & 0xF;
00296 ptr[j*2+1] = buf[j] & 0xF;
00297 }
00298 buf += n;
00299 ptr += linesize;
00300 }
00301 break;
00302 case 16:
00303 for(i = 0; i < avctx->height; i++){
00304 const uint16_t *src = (const uint16_t *) buf;
00305 uint16_t *dst = (uint16_t *) ptr;
00306
00307 for(j = 0; j < avctx->width; j++)
00308 *dst++ = av_le2ne16(*src++);
00309
00310 buf += n;
00311 ptr += linesize;
00312 }
00313 break;
00314 case 32:
00315 for(i = 0; i < avctx->height; i++){
00316 const uint8_t *src = buf;
00317 uint8_t *dst = ptr;
00318
00319 for(j = 0; j < avctx->width; j++){
00320 dst[0] = src[rgb[2]];
00321 dst[1] = src[rgb[1]];
00322 dst[2] = src[rgb[0]];
00323
00324
00325
00326
00327
00328 dst[3] = src[3];
00329 dst += 4;
00330 src += 4;
00331 }
00332
00333 buf += n;
00334 ptr += linesize;
00335 }
00336 break;
00337 default:
00338 av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
00339 return -1;
00340 }
00341 }
00342
00343 *picture = s->picture;
00344 *data_size = sizeof(AVPicture);
00345
00346 return buf_size;
00347 }
00348
00349 static av_cold int bmp_decode_end(AVCodecContext *avctx)
00350 {
00351 BMPContext* c = avctx->priv_data;
00352
00353 if (c->picture.data[0])
00354 avctx->release_buffer(avctx, &c->picture);
00355
00356 return 0;
00357 }
00358
00359 AVCodec ff_bmp_decoder = {
00360 .name = "bmp",
00361 .type = AVMEDIA_TYPE_VIDEO,
00362 .id = CODEC_ID_BMP,
00363 .priv_data_size = sizeof(BMPContext),
00364 .init = bmp_decode_init,
00365 .close = bmp_decode_end,
00366 .decode = bmp_decode_frame,
00367 .capabilities = CODEC_CAP_DR1,
00368 .long_name = NULL_IF_CONFIG_SMALL("BMP image"),
00369 };