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 "internal.h"
00026 #include "msrledec.h"
00027
00028 static av_cold int bmp_decode_init(AVCodecContext *avctx){
00029 BMPContext *s = avctx->priv_data;
00030
00031 avcodec_get_frame_defaults(&s->picture);
00032 avctx->coded_frame = &s->picture;
00033
00034 return 0;
00035 }
00036
00037 static int bmp_decode_frame(AVCodecContext *avctx,
00038 void *data, int *got_frame,
00039 AVPacket *avpkt)
00040 {
00041 const uint8_t *buf = avpkt->data;
00042 int buf_size = avpkt->size;
00043 BMPContext *s = avctx->priv_data;
00044 AVFrame *picture = data;
00045 AVFrame *p = &s->picture;
00046 unsigned int fsize, hsize;
00047 int width, height;
00048 unsigned int depth;
00049 BiCompression comp;
00050 unsigned int ihsize;
00051 int i, j, n, linesize;
00052 uint32_t rgb[3];
00053 uint32_t alpha = 0;
00054 uint8_t *ptr;
00055 int dsize;
00056 const uint8_t *buf0 = buf;
00057 GetByteContext gb;
00058
00059 if(buf_size < 14){
00060 av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\n", buf_size);
00061 return -1;
00062 }
00063
00064 if(bytestream_get_byte(&buf) != 'B' ||
00065 bytestream_get_byte(&buf) != 'M') {
00066 av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
00067 return -1;
00068 }
00069
00070 fsize = bytestream_get_le32(&buf);
00071 if(buf_size < fsize){
00072 av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d), trying to decode anyway\n",
00073 buf_size, fsize);
00074 fsize = buf_size;
00075 }
00076
00077 buf += 2;
00078 buf += 2;
00079
00080 hsize = bytestream_get_le32(&buf);
00081 ihsize = bytestream_get_le32(&buf);
00082 if(ihsize + 14 > hsize){
00083 av_log(avctx, AV_LOG_ERROR, "invalid header size %d\n", hsize);
00084 return -1;
00085 }
00086
00087
00088 if(fsize == 14 || fsize == ihsize + 14)
00089 fsize = buf_size - 2;
00090
00091 if(fsize <= hsize){
00092 av_log(avctx, AV_LOG_ERROR, "declared file size is less than header size (%d < %d)\n",
00093 fsize, hsize);
00094 return -1;
00095 }
00096
00097 switch(ihsize){
00098 case 40:
00099 case 56:
00100 case 64:
00101 case 108:
00102 case 124:
00103 width = bytestream_get_le32(&buf);
00104 height = bytestream_get_le32(&buf);
00105 break;
00106 case 12:
00107 width = bytestream_get_le16(&buf);
00108 height = bytestream_get_le16(&buf);
00109 break;
00110 default:
00111 av_log(avctx, AV_LOG_ERROR, "unsupported BMP file, patch welcome\n");
00112 return -1;
00113 }
00114
00115 if(bytestream_get_le16(&buf) != 1){
00116 av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n");
00117 return -1;
00118 }
00119
00120 depth = bytestream_get_le16(&buf);
00121
00122 if (ihsize >= 40)
00123 comp = bytestream_get_le32(&buf);
00124 else
00125 comp = BMP_RGB;
00126
00127 if(comp != BMP_RGB && comp != BMP_BITFIELDS && comp != BMP_RLE4 && comp != BMP_RLE8){
00128 av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp);
00129 return -1;
00130 }
00131
00132 if(comp == BMP_BITFIELDS){
00133 buf += 20;
00134 rgb[0] = bytestream_get_le32(&buf);
00135 rgb[1] = bytestream_get_le32(&buf);
00136 rgb[2] = bytestream_get_le32(&buf);
00137 alpha = bytestream_get_le32(&buf);
00138 }
00139
00140 avctx->width = width;
00141 avctx->height = height > 0? height: -height;
00142
00143 avctx->pix_fmt = AV_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 ? AV_PIX_FMT_ABGR : AV_PIX_FMT_0BGR;
00150 else if (rgb[0] == 0x00FF0000 && rgb[1] == 0x0000FF00 && rgb[2] == 0x000000FF)
00151 avctx->pix_fmt = alpha ? AV_PIX_FMT_BGRA : AV_PIX_FMT_BGR0;
00152 else if (rgb[0] == 0x0000FF00 && rgb[1] == 0x00FF0000 && rgb[2] == 0xFF000000)
00153 avctx->pix_fmt = alpha ? AV_PIX_FMT_ARGB : AV_PIX_FMT_0RGB;
00154 else if (rgb[0] == 0x000000FF && rgb[1] == 0x0000FF00 && rgb[2] == 0x00FF0000)
00155 avctx->pix_fmt = alpha ? AV_PIX_FMT_RGBA : AV_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 = AV_PIX_FMT_BGRA;
00162 }
00163 break;
00164 case 24:
00165 avctx->pix_fmt = AV_PIX_FMT_BGR24;
00166 break;
00167 case 16:
00168 if(comp == BMP_RGB)
00169 avctx->pix_fmt = AV_PIX_FMT_RGB555;
00170 else if (comp == BMP_BITFIELDS) {
00171 if (rgb[0] == 0xF800 && rgb[1] == 0x07E0 && rgb[2] == 0x001F)
00172 avctx->pix_fmt = AV_PIX_FMT_RGB565;
00173 else if (rgb[0] == 0x7C00 && rgb[1] == 0x03E0 && rgb[2] == 0x001F)
00174 avctx->pix_fmt = AV_PIX_FMT_RGB555;
00175 else if (rgb[0] == 0x0F00 && rgb[1] == 0x00F0 && rgb[2] == 0x000F)
00176 avctx->pix_fmt = AV_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 = AV_PIX_FMT_PAL8;
00186 else
00187 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
00188 break;
00189 case 1:
00190 case 4:
00191 if(hsize - ihsize - 14 > 0){
00192 avctx->pix_fmt = AV_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 == AV_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(ff_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 == AV_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] = (0xFFU<<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 *got_frame = 1;
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 };