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