• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavcodec/bmp.c

Go to the documentation of this file.
00001 /*
00002  * BMP image format decoder
00003  * Copyright (c) 2005 Mans Rullgard
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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 
00056     if(buf_size < 14){
00057         av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\n", buf_size);
00058         return -1;
00059     }
00060 
00061     if(bytestream_get_byte(&buf) != 'B' ||
00062        bytestream_get_byte(&buf) != 'M') {
00063         av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
00064         return -1;
00065     }
00066 
00067     fsize = bytestream_get_le32(&buf);
00068     if(buf_size < fsize){
00069         av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d), trying to decode anyway\n",
00070                buf_size, fsize);
00071         fsize = buf_size;
00072     }
00073 
00074     buf += 2; /* reserved1 */
00075     buf += 2; /* reserved2 */
00076 
00077     hsize = bytestream_get_le32(&buf); /* header size */
00078     ihsize = bytestream_get_le32(&buf);       /* more header size */
00079     if(ihsize + 14 > hsize){
00080         av_log(avctx, AV_LOG_ERROR, "invalid header size %d\n", hsize);
00081         return -1;
00082     }
00083 
00084     /* sometimes file size is set to some headers size, set a real size in that case */
00085     if(fsize == 14 || fsize == ihsize + 14)
00086         fsize = buf_size - 2;
00087 
00088     if(fsize <= hsize){
00089         av_log(avctx, AV_LOG_ERROR, "declared file size is less than header size (%d < %d)\n",
00090                fsize, hsize);
00091         return -1;
00092     }
00093 
00094     switch(ihsize){
00095     case  40: // windib v3
00096     case  64: // OS/2 v2
00097     case 108: // windib v4
00098     case 124: // windib v5
00099         width = bytestream_get_le32(&buf);
00100         height = bytestream_get_le32(&buf);
00101         break;
00102     case  12: // OS/2 v1
00103         width  = bytestream_get_le16(&buf);
00104         height = bytestream_get_le16(&buf);
00105         break;
00106     default:
00107         av_log(avctx, AV_LOG_ERROR, "unsupported BMP file, patch welcome\n");
00108         return -1;
00109     }
00110 
00111     if(bytestream_get_le16(&buf) != 1){ /* planes */
00112         av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n");
00113         return -1;
00114     }
00115 
00116     depth = bytestream_get_le16(&buf);
00117 
00118     if(ihsize == 40)
00119         comp = bytestream_get_le32(&buf);
00120     else
00121         comp = BMP_RGB;
00122 
00123     if(comp != BMP_RGB && comp != BMP_BITFIELDS && comp != BMP_RLE4 && comp != BMP_RLE8){
00124         av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp);
00125         return -1;
00126     }
00127 
00128     if(comp == BMP_BITFIELDS){
00129         buf += 20;
00130         rgb[0] = bytestream_get_le32(&buf);
00131         rgb[1] = bytestream_get_le32(&buf);
00132         rgb[2] = bytestream_get_le32(&buf);
00133     }
00134 
00135     avctx->width = width;
00136     avctx->height = height > 0? height: -height;
00137 
00138     avctx->pix_fmt = PIX_FMT_NONE;
00139 
00140     switch(depth){
00141     case 32:
00142         if(comp == BMP_BITFIELDS){
00143             rgb[0] = (rgb[0] >> 15) & 3;
00144             rgb[1] = (rgb[1] >> 15) & 3;
00145             rgb[2] = (rgb[2] >> 15) & 3;
00146 
00147             if(rgb[0] + rgb[1] + rgb[2] != 3 ||
00148                rgb[0] == rgb[1] || rgb[0] == rgb[2] || rgb[1] == rgb[2]){
00149                 break;
00150             }
00151         } else {
00152             rgb[0] = 2;
00153             rgb[1] = 1;
00154             rgb[2] = 0;
00155         }
00156 
00157         avctx->pix_fmt = PIX_FMT_BGR24;
00158         break;
00159     case 24:
00160         avctx->pix_fmt = PIX_FMT_BGR24;
00161         break;
00162     case 16:
00163         if(comp == BMP_RGB)
00164             avctx->pix_fmt = PIX_FMT_RGB555;
00165         else if (comp == BMP_BITFIELDS) {
00166             if (rgb[0] == 0xF800 && rgb[1] == 0x07E0 && rgb[2] == 0x001F)
00167                avctx->pix_fmt = PIX_FMT_RGB565;
00168             else if (rgb[0] == 0x7C00 && rgb[1] == 0x03E0 && rgb[2] == 0x001F)
00169                avctx->pix_fmt = PIX_FMT_RGB555;
00170             else if (rgb[0] == 0x0F00 && rgb[1] == 0x00F0 && rgb[2] == 0x000F)
00171                avctx->pix_fmt = PIX_FMT_RGB444;
00172             else {
00173                av_log(avctx, AV_LOG_ERROR, "Unknown bitfields %0X %0X %0X\n", rgb[0], rgb[1], rgb[2]);
00174                return AVERROR(EINVAL);
00175             }
00176         }
00177         break;
00178     case 8:
00179         if(hsize - ihsize - 14 > 0)
00180             avctx->pix_fmt = PIX_FMT_PAL8;
00181         else
00182             avctx->pix_fmt = PIX_FMT_GRAY8;
00183         break;
00184     case 1:
00185     case 4:
00186         if(hsize - ihsize - 14 > 0){
00187             avctx->pix_fmt = PIX_FMT_PAL8;
00188         }else{
00189             av_log(avctx, AV_LOG_ERROR, "Unknown palette for %d-colour BMP\n", 1<<depth);
00190             return -1;
00191         }
00192         break;
00193     default:
00194         av_log(avctx, AV_LOG_ERROR, "depth %d not supported\n", depth);
00195         return -1;
00196     }
00197 
00198     if(avctx->pix_fmt == PIX_FMT_NONE){
00199         av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
00200         return -1;
00201     }
00202 
00203     if(p->data[0])
00204         avctx->release_buffer(avctx, p);
00205 
00206     p->reference = 0;
00207     if(avctx->get_buffer(avctx, p) < 0){
00208         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00209         return -1;
00210     }
00211     p->pict_type = AV_PICTURE_TYPE_I;
00212     p->key_frame = 1;
00213 
00214     buf = buf0 + hsize;
00215     dsize = buf_size - hsize;
00216 
00217     /* Line size in file multiple of 4 */
00218     n = ((avctx->width * depth) / 8 + 3) & ~3;
00219 
00220     if(n * avctx->height > dsize && comp != BMP_RLE4 && comp != BMP_RLE8){
00221         av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
00222                dsize, n * avctx->height);
00223         return -1;
00224     }
00225 
00226     // RLE may skip decoding some picture areas, so blank picture before decoding
00227     if(comp == BMP_RLE4 || comp == BMP_RLE8)
00228         memset(p->data[0], 0, avctx->height * p->linesize[0]);
00229 
00230     if(height > 0){
00231         ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
00232         linesize = -p->linesize[0];
00233     } else {
00234         ptr = p->data[0];
00235         linesize = p->linesize[0];
00236     }
00237 
00238     if(avctx->pix_fmt == PIX_FMT_PAL8){
00239         int colors = 1 << depth;
00240 
00241         memset(p->data[1], 0, 1024);
00242 
00243         if(ihsize >= 36){
00244             int t;
00245             buf = buf0 + 46;
00246             t = bytestream_get_le32(&buf);
00247             if(t < 0 || t > (1 << depth)){
00248                 av_log(avctx, AV_LOG_ERROR, "Incorrect number of colors - %X for bitdepth %d\n", t, depth);
00249             }else if(t){
00250                 colors = t;
00251             }
00252         }
00253         buf = buf0 + 14 + ihsize; //palette location
00254         if((hsize-ihsize-14) < (colors << 2)){ // OS/2 bitmap, 3 bytes per palette entry
00255             for(i = 0; i < colors; i++)
00256                 ((uint32_t*)p->data[1])[i] = bytestream_get_le24(&buf);
00257         }else{
00258             for(i = 0; i < colors; i++)
00259                 ((uint32_t*)p->data[1])[i] = bytestream_get_le32(&buf);
00260         }
00261         buf = buf0 + hsize;
00262     }
00263     if(comp == BMP_RLE4 || comp == BMP_RLE8){
00264         if(height < 0){
00265             p->data[0] += p->linesize[0] * (avctx->height - 1);
00266             p->linesize[0] = -p->linesize[0];
00267         }
00268         ff_msrle_decode(avctx, (AVPicture*)p, depth, buf, dsize);
00269         if(height < 0){
00270             p->data[0] += p->linesize[0] * (avctx->height - 1);
00271             p->linesize[0] = -p->linesize[0];
00272         }
00273     }else{
00274         switch(depth){
00275         case 1:
00276             for (i = 0; i < avctx->height; i++) {
00277                 int j;
00278                 for (j = 0; j < n; j++) {
00279                     ptr[j*8+0] =  buf[j] >> 7;
00280                     ptr[j*8+1] = (buf[j] >> 6) & 1;
00281                     ptr[j*8+2] = (buf[j] >> 5) & 1;
00282                     ptr[j*8+3] = (buf[j] >> 4) & 1;
00283                     ptr[j*8+4] = (buf[j] >> 3) & 1;
00284                     ptr[j*8+5] = (buf[j] >> 2) & 1;
00285                     ptr[j*8+6] = (buf[j] >> 1) & 1;
00286                     ptr[j*8+7] =  buf[j]       & 1;
00287                 }
00288                 buf += n;
00289                 ptr += linesize;
00290             }
00291             break;
00292         case 8:
00293         case 24:
00294             for(i = 0; i < avctx->height; i++){
00295                 memcpy(ptr, buf, n);
00296                 buf += n;
00297                 ptr += linesize;
00298             }
00299             break;
00300         case 4:
00301             for(i = 0; i < avctx->height; i++){
00302                 int j;
00303                 for(j = 0; j < n; j++){
00304                     ptr[j*2+0] = (buf[j] >> 4) & 0xF;
00305                     ptr[j*2+1] = buf[j] & 0xF;
00306                 }
00307                 buf += n;
00308                 ptr += linesize;
00309             }
00310             break;
00311         case 16:
00312             for(i = 0; i < avctx->height; i++){
00313                 const uint16_t *src = (const uint16_t *) buf;
00314                 uint16_t *dst = (uint16_t *) ptr;
00315 
00316                 for(j = 0; j < avctx->width; j++)
00317                     *dst++ = av_le2ne16(*src++);
00318 
00319                 buf += n;
00320                 ptr += linesize;
00321             }
00322             break;
00323         case 32:
00324             for(i = 0; i < avctx->height; i++){
00325                 const uint8_t *src = buf;
00326                 uint8_t *dst = ptr;
00327 
00328                 for(j = 0; j < avctx->width; j++){
00329                     dst[0] = src[rgb[2]];
00330                     dst[1] = src[rgb[1]];
00331                     dst[2] = src[rgb[0]];
00332                     dst += 3;
00333                     src += 4;
00334                 }
00335 
00336                 buf += n;
00337                 ptr += linesize;
00338             }
00339             break;
00340         default:
00341             av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
00342             return -1;
00343         }
00344     }
00345 
00346     *picture = s->picture;
00347     *data_size = sizeof(AVPicture);
00348 
00349     return buf_size;
00350 }
00351 
00352 static av_cold int bmp_decode_end(AVCodecContext *avctx)
00353 {
00354     BMPContext* c = avctx->priv_data;
00355 
00356     if (c->picture.data[0])
00357         avctx->release_buffer(avctx, &c->picture);
00358 
00359     return 0;
00360 }
00361 
00362 AVCodec ff_bmp_decoder = {
00363     .name           = "bmp",
00364     .type           = AVMEDIA_TYPE_VIDEO,
00365     .id             = CODEC_ID_BMP,
00366     .priv_data_size = sizeof(BMPContext),
00367     .init           = bmp_decode_init,
00368     .close          = bmp_decode_end,
00369     .decode         = bmp_decode_frame,
00370     .capabilities   = CODEC_CAP_DR1,
00371     .long_name = NULL_IF_CONFIG_SMALL("BMP image"),
00372 };
Generated on Thu Jan 24 2013 17:08:50 for Libav by doxygen 1.7.1