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