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 GetByteContext gb;
00056
00057 if(buf_size < 14){
00058 av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\n", buf_size);
00059 return -1;
00060 }
00061
00062 if(bytestream_get_byte(&buf) != 'B' ||
00063 bytestream_get_byte(&buf) != 'M') {
00064 av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
00065 return -1;
00066 }
00067
00068 fsize = bytestream_get_le32(&buf);
00069 if(buf_size < fsize){
00070 av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d), trying to decode anyway\n",
00071 buf_size, fsize);
00072 fsize = buf_size;
00073 }
00074
00075 buf += 2;
00076 buf += 2;
00077
00078 hsize = bytestream_get_le32(&buf);
00079 ihsize = bytestream_get_le32(&buf);
00080 if(ihsize + 14 > hsize){
00081 av_log(avctx, AV_LOG_ERROR, "invalid header size %d\n", hsize);
00082 return -1;
00083 }
00084
00085
00086 if(fsize == 14 || fsize == ihsize + 14)
00087 fsize = buf_size - 2;
00088
00089 if(fsize <= hsize){
00090 av_log(avctx, AV_LOG_ERROR, "declared file size is less than header size (%d < %d)\n",
00091 fsize, hsize);
00092 return -1;
00093 }
00094
00095 switch(ihsize){
00096 case 40:
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)
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_BGR24;
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 else if (comp == BMP_BITFIELDS) {
00167 if (rgb[0] == 0xF800 && rgb[1] == 0x07E0 && rgb[2] == 0x001F)
00168 avctx->pix_fmt = PIX_FMT_RGB565;
00169 else if (rgb[0] == 0x7C00 && rgb[1] == 0x03E0 && rgb[2] == 0x001F)
00170 avctx->pix_fmt = PIX_FMT_RGB555;
00171 else if (rgb[0] == 0x0F00 && rgb[1] == 0x00F0 && rgb[2] == 0x000F)
00172 avctx->pix_fmt = PIX_FMT_RGB444;
00173 else {
00174 av_log(avctx, AV_LOG_ERROR, "Unknown bitfields %0X %0X %0X\n", rgb[0], rgb[1], rgb[2]);
00175 return AVERROR(EINVAL);
00176 }
00177 }
00178 break;
00179 case 8:
00180 if(hsize - ihsize - 14 > 0)
00181 avctx->pix_fmt = PIX_FMT_PAL8;
00182 else
00183 avctx->pix_fmt = PIX_FMT_GRAY8;
00184 break;
00185 case 1:
00186 case 4:
00187 if(hsize - ihsize - 14 > 0){
00188 avctx->pix_fmt = PIX_FMT_PAL8;
00189 }else{
00190 av_log(avctx, AV_LOG_ERROR, "Unknown palette for %d-colour BMP\n", 1<<depth);
00191 return -1;
00192 }
00193 break;
00194 default:
00195 av_log(avctx, AV_LOG_ERROR, "depth %d not supported\n", depth);
00196 return -1;
00197 }
00198
00199 if(avctx->pix_fmt == PIX_FMT_NONE){
00200 av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
00201 return -1;
00202 }
00203
00204 if(p->data[0])
00205 avctx->release_buffer(avctx, p);
00206
00207 p->reference = 0;
00208 if(avctx->get_buffer(avctx, p) < 0){
00209 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00210 return -1;
00211 }
00212 p->pict_type = AV_PICTURE_TYPE_I;
00213 p->key_frame = 1;
00214
00215 buf = buf0 + hsize;
00216 dsize = buf_size - hsize;
00217
00218
00219 n = ((avctx->width * depth) / 8 + 3) & ~3;
00220
00221 if(n * avctx->height > dsize && comp != BMP_RLE4 && comp != BMP_RLE8){
00222 av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
00223 dsize, n * avctx->height);
00224 return -1;
00225 }
00226
00227
00228 if(comp == BMP_RLE4 || comp == BMP_RLE8)
00229 memset(p->data[0], 0, avctx->height * p->linesize[0]);
00230
00231 if(height > 0){
00232 ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
00233 linesize = -p->linesize[0];
00234 } else {
00235 ptr = p->data[0];
00236 linesize = p->linesize[0];
00237 }
00238
00239 if(avctx->pix_fmt == PIX_FMT_PAL8){
00240 int colors = 1 << depth;
00241
00242 memset(p->data[1], 0, 1024);
00243
00244 if(ihsize >= 36){
00245 int t;
00246 buf = buf0 + 46;
00247 t = bytestream_get_le32(&buf);
00248 if(t < 0 || t > (1 << depth)){
00249 av_log(avctx, AV_LOG_ERROR, "Incorrect number of colors - %X for bitdepth %d\n", t, depth);
00250 }else if(t){
00251 colors = t;
00252 }
00253 }
00254 buf = buf0 + 14 + ihsize;
00255 if((hsize-ihsize-14) < (colors << 2)){
00256 for(i = 0; i < colors; i++)
00257 ((uint32_t*)p->data[1])[i] = bytestream_get_le24(&buf);
00258 }else{
00259 for(i = 0; i < colors; i++)
00260 ((uint32_t*)p->data[1])[i] = bytestream_get_le32(&buf);
00261 }
00262 buf = buf0 + hsize;
00263 }
00264 if(comp == BMP_RLE4 || comp == BMP_RLE8){
00265 if(height < 0){
00266 p->data[0] += p->linesize[0] * (avctx->height - 1);
00267 p->linesize[0] = -p->linesize[0];
00268 }
00269 bytestream2_init(&gb, buf, dsize);
00270 ff_msrle_decode(avctx, (AVPicture*)p, depth, &gb);
00271 if(height < 0){
00272 p->data[0] += p->linesize[0] * (avctx->height - 1);
00273 p->linesize[0] = -p->linesize[0];
00274 }
00275 }else{
00276 switch(depth){
00277 case 1:
00278 for (i = 0; i < avctx->height; i++) {
00279 int j;
00280 for (j = 0; j < n; j++) {
00281 ptr[j*8+0] = buf[j] >> 7;
00282 ptr[j*8+1] = (buf[j] >> 6) & 1;
00283 ptr[j*8+2] = (buf[j] >> 5) & 1;
00284 ptr[j*8+3] = (buf[j] >> 4) & 1;
00285 ptr[j*8+4] = (buf[j] >> 3) & 1;
00286 ptr[j*8+5] = (buf[j] >> 2) & 1;
00287 ptr[j*8+6] = (buf[j] >> 1) & 1;
00288 ptr[j*8+7] = buf[j] & 1;
00289 }
00290 buf += n;
00291 ptr += linesize;
00292 }
00293 break;
00294 case 8:
00295 case 24:
00296 for(i = 0; i < avctx->height; i++){
00297 memcpy(ptr, buf, n);
00298 buf += n;
00299 ptr += linesize;
00300 }
00301 break;
00302 case 4:
00303 for(i = 0; i < avctx->height; i++){
00304 int j;
00305 for(j = 0; j < n; j++){
00306 ptr[j*2+0] = (buf[j] >> 4) & 0xF;
00307 ptr[j*2+1] = buf[j] & 0xF;
00308 }
00309 buf += n;
00310 ptr += linesize;
00311 }
00312 break;
00313 case 16:
00314 for(i = 0; i < avctx->height; i++){
00315 const uint16_t *src = (const uint16_t *) buf;
00316 uint16_t *dst = (uint16_t *) ptr;
00317
00318 for(j = 0; j < avctx->width; j++)
00319 *dst++ = av_le2ne16(*src++);
00320
00321 buf += n;
00322 ptr += linesize;
00323 }
00324 break;
00325 case 32:
00326 for(i = 0; i < avctx->height; i++){
00327 const uint8_t *src = buf;
00328 uint8_t *dst = ptr;
00329
00330 for(j = 0; j < avctx->width; j++){
00331 dst[0] = src[rgb[2]];
00332 dst[1] = src[rgb[1]];
00333 dst[2] = src[rgb[0]];
00334 dst += 3;
00335 src += 4;
00336 }
00337
00338 buf += n;
00339 ptr += linesize;
00340 }
00341 break;
00342 default:
00343 av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
00344 return -1;
00345 }
00346 }
00347
00348 *picture = s->picture;
00349 *data_size = sizeof(AVPicture);
00350
00351 return buf_size;
00352 }
00353
00354 static av_cold int bmp_decode_end(AVCodecContext *avctx)
00355 {
00356 BMPContext* c = avctx->priv_data;
00357
00358 if (c->picture.data[0])
00359 avctx->release_buffer(avctx, &c->picture);
00360
00361 return 0;
00362 }
00363
00364 AVCodec ff_bmp_decoder = {
00365 .name = "bmp",
00366 .type = AVMEDIA_TYPE_VIDEO,
00367 .id = CODEC_ID_BMP,
00368 .priv_data_size = sizeof(BMPContext),
00369 .init = bmp_decode_init,
00370 .close = bmp_decode_end,
00371 .decode = bmp_decode_frame,
00372 .capabilities = CODEC_CAP_DR1,
00373 .long_name = NULL_IF_CONFIG_SMALL("BMP image"),
00374 };