• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • 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 FFmpeg.
00006  *
00007  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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     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; /* reserved1 */
00076     buf += 2; /* reserved2 */
00077 
00078     hsize = bytestream_get_le32(&buf); /* header size */
00079     ihsize = bytestream_get_le32(&buf);       /* more header size */
00080     if(ihsize + 14 > hsize){
00081         av_log(avctx, AV_LOG_ERROR, "invalid header size %d\n", hsize);
00082         return -1;
00083     }
00084 
00085     /* sometimes file size is set to some headers size, set a real size in that case */
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: // windib
00097     case  56: // windib v3
00098     case  64: // OS/2 v2
00099     case 108: // windib v4
00100     case 124: // windib v5
00101         width = bytestream_get_le32(&buf);
00102         height = bytestream_get_le32(&buf);
00103         break;
00104     case  12: // OS/2 v1
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){ /* planes */
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     /* Line size in file multiple of 4 */
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     // RLE may skip decoding some picture areas, so blank picture before decoding
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; //palette location
00257         if((hsize-ihsize-14) < (colors << 2)){ // OS/2 bitmap, 3 bytes per palette entry
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 };
Generated on Fri Feb 1 2013 14:34:31 for FFmpeg by doxygen 1.7.1