[FFmpeg-devel] [PATCH] Extend BMP decoding support

Kostya kostya.shishkov
Thu Sep 18 16:14:36 CEST 2008


This patch enables decoding of BMP files with depths <= 8
with RLE decoding as well.
Cosmetics for indenting raw data reading switch{} is omitted.

Tested on BMP files from mplayerhq.
-------------- next part --------------
Index: libavcodec/Makefile
===================================================================
--- libavcodec/Makefile	(revision 15358)
+++ libavcodec/Makefile	(working copy)
@@ -41,7 +41,7 @@
 OBJS-$(CONFIG_AVS_DECODER)             += avs.o
 OBJS-$(CONFIG_BETHSOFTVID_DECODER)     += bethsoftvideo.o
 OBJS-$(CONFIG_BFI_DECODER)             += bfi.o
-OBJS-$(CONFIG_BMP_DECODER)             += bmp.o
+OBJS-$(CONFIG_BMP_DECODER)             += bmp.o msrledec.o
 OBJS-$(CONFIG_BMP_ENCODER)             += bmpenc.o
 OBJS-$(CONFIG_C93_DECODER)             += c93.o
 OBJS-$(CONFIG_CAVS_DECODER)            += cavs.o cavsdec.o cavsdsp.o golomb.o mpeg12data.o mpegvideo.o
Index: libavcodec/bmp.c
===================================================================
--- libavcodec/bmp.c	(revision 15358)
+++ libavcodec/bmp.c	(working copy)
@@ -22,6 +22,7 @@
 #include "avcodec.h"
 #include "bytestream.h"
 #include "bmp.h"
+#include "msrledec.h"
 
 static av_cold int bmp_decode_init(AVCodecContext *avctx){
     BMPContext *s = avctx->priv_data;
@@ -107,7 +108,7 @@
     else
         comp = BMP_RGB;
 
-    if(comp != BMP_RGB && comp != BMP_BITFIELDS){
+    if(comp != BMP_RGB && comp != BMP_BITFIELDS && comp != BMP_RLE4 && comp != BMP_RLE8){
         av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp);
         return -1;
     }
@@ -149,7 +150,26 @@
     case 16:
         if(comp == BMP_RGB)
             avctx->pix_fmt = PIX_FMT_RGB555;
+        if(comp == BMP_BITFIELDS)
+            avctx->pix_fmt = rgb[1] == 0x07E0 ? PIX_FMT_RGB565 : PIX_FMT_RGB555;
         break;
+    case 8:
+        if(hsize - ihsize - 14 > 0)
+            avctx->pix_fmt = PIX_FMT_PAL8;
+        else
+            avctx->pix_fmt = PIX_FMT_GRAY8;
+        break;
+    case 4:
+        if(hsize - ihsize - 14 > 0){
+            avctx->pix_fmt = PIX_FMT_PAL8;
+        }else{
+            av_log(avctx, AV_LOG_ERROR, "Unknown palette for 16-colour BMP\n");
+            return -1;
+        }
+        break;
+    case 1:
+        avctx->pix_fmt = PIX_FMT_MONOBLACK;
+        break;
     default:
         av_log(avctx, AV_LOG_ERROR, "depth %d not supported\n", depth);
         return -1;
@@ -175,14 +195,21 @@
     dsize = buf_size - hsize;
 
     /* Line size in file multiple of 4 */
-    n = (avctx->width * (depth / 8) + 3) & ~3;
+    n = ((avctx->width * depth) / 8 + 3) & ~3;
 
-    if(n * avctx->height > dsize){
+    if(n * avctx->height > dsize && comp != BMP_RLE4 && comp != BMP_RLE8){
         av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
                dsize, n * avctx->height);
         return -1;
     }
 
+    // RLE may skip decoding some picture areas, so blank picture before decoding
+    if(comp == BMP_RLE4 || comp == BMP_RLE8)
+        memset(p->data[0], 0, avctx->height * p->linesize[0]);
+
+    if(depth == 4 || depth == 8)
+        memset(p->data[1], 0, 1024);
+
     if(height > 0){
         ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
         linesize = -p->linesize[0];
@@ -191,7 +218,46 @@
         linesize = p->linesize[0];
     }
 
+    if(avctx->pix_fmt == PIX_FMT_PAL8){
+        buf = buf0 + 14 + ihsize; //palette location
+        if((hsize-ihsize-14)>>depth < 4){ // OS/2 bitmap, 3 bytes per palette entry
+            for(i = 0; i < (1 << depth); i++)
+                ((uint32_t*)p->data[1])[i] = bytestream_get_le24(&buf);
+        }else{
+            for(i = 0; i < (1 << depth); i++)
+                ((uint32_t*)p->data[1])[i] = bytestream_get_le32(&buf);
+        }
+        buf = buf0 + hsize;
+    }
+    if(comp == BMP_RLE4 || comp == BMP_RLE8){
+        ff_msrle_decode(avctx, p, depth, buf, dsize);
+    }else{
     switch(depth){
+    case 1:
+        for(i = 0; i < avctx->height; i++){
+            memcpy(ptr, buf, n);
+            buf += n;
+            ptr += linesize;
+        }
+        break;
+    case 4:
+        for(i = 0; i < avctx->height; i++){
+            int j;
+            for(j = 0; j < n; j++){
+                ptr[j*2+0] = buf[j] >> 4;
+                ptr[j*2+1] = buf[j] & 0xF;
+            }
+            buf += n;
+            ptr += linesize;
+        }
+        break;
+    case 8:
+        for(i = 0; i < avctx->height; i++){
+            memcpy(ptr, buf, avctx->width);
+            buf += n;
+            ptr += linesize;
+        }
+        break;
     case 24:
         for(i = 0; i < avctx->height; i++){
             memcpy(ptr, buf, avctx->width*(depth>>3));
@@ -232,6 +298,7 @@
         av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
         return -1;
     }
+    }
 
     *picture = s->picture;
     *data_size = sizeof(AVPicture);



More information about the ffmpeg-devel mailing list