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