IFF: decode last 8 pixels per line
[libav.git] / libavcodec / iff.c
index 7323f9b..d23837e 100644 (file)
@@ -1,6 +1,7 @@
 /*
  * IFF PBM/ILBM bitmap decoder
  * Copyright (c) 2010 Peter Ross <pross@xvid.org>
+ * Copyright (c) 2010 Sebastian Vater <cdgs.basty@googlemail.com>
  *
  * This file is part of FFmpeg.
  *
  */
 
 /**
- * @file libavcodec/iff.c
+ * @file
  * IFF PBM/ILBM bitmap decoder
  */
 
 #include "bytestream.h"
 #include "avcodec.h"
+#include "get_bits.h"
+#include "iff.h"
+
+typedef struct {
+    AVFrame frame;
+    int planesize;
+    uint8_t * planebuf;
+} IffContext;
+
+#define LUT8_PART(plane, v)                             \
+    AV_LE2ME64C(UINT64_C(0x0000000)<<32 | v) << plane,  \
+    AV_LE2ME64C(UINT64_C(0x1000000)<<32 | v) << plane,  \
+    AV_LE2ME64C(UINT64_C(0x0010000)<<32 | v) << plane,  \
+    AV_LE2ME64C(UINT64_C(0x1010000)<<32 | v) << plane,  \
+    AV_LE2ME64C(UINT64_C(0x0000100)<<32 | v) << plane,  \
+    AV_LE2ME64C(UINT64_C(0x1000100)<<32 | v) << plane,  \
+    AV_LE2ME64C(UINT64_C(0x0010100)<<32 | v) << plane,  \
+    AV_LE2ME64C(UINT64_C(0x1010100)<<32 | v) << plane,  \
+    AV_LE2ME64C(UINT64_C(0x0000001)<<32 | v) << plane,  \
+    AV_LE2ME64C(UINT64_C(0x1000001)<<32 | v) << plane,  \
+    AV_LE2ME64C(UINT64_C(0x0010001)<<32 | v) << plane,  \
+    AV_LE2ME64C(UINT64_C(0x1010001)<<32 | v) << plane,  \
+    AV_LE2ME64C(UINT64_C(0x0000101)<<32 | v) << plane,  \
+    AV_LE2ME64C(UINT64_C(0x1000101)<<32 | v) << plane,  \
+    AV_LE2ME64C(UINT64_C(0x0010101)<<32 | v) << plane,  \
+    AV_LE2ME64C(UINT64_C(0x1010101)<<32 | v) << plane
+
+#define LUT8(plane) {                           \
+    LUT8_PART(plane, 0x0000000),                \
+    LUT8_PART(plane, 0x1000000),                \
+    LUT8_PART(plane, 0x0010000),                \
+    LUT8_PART(plane, 0x1010000),                \
+    LUT8_PART(plane, 0x0000100),                \
+    LUT8_PART(plane, 0x1000100),                \
+    LUT8_PART(plane, 0x0010100),                \
+    LUT8_PART(plane, 0x1010100),                \
+    LUT8_PART(plane, 0x0000001),                \
+    LUT8_PART(plane, 0x1000001),                \
+    LUT8_PART(plane, 0x0010001),                \
+    LUT8_PART(plane, 0x1010001),                \
+    LUT8_PART(plane, 0x0000101),                \
+    LUT8_PART(plane, 0x1000101),                \
+    LUT8_PART(plane, 0x0010101),                \
+    LUT8_PART(plane, 0x1010101),                \
+}
+
+// 8 planes * 8-bit mask
+static const uint64_t plane8_lut[8][256] = {
+    LUT8(0), LUT8(1), LUT8(2), LUT8(3),
+    LUT8(4), LUT8(5), LUT8(6), LUT8(7),
+};
 
 /**
  * Convert CMAP buffer (stored in extradata) to lavc palette format
@@ -45,51 +97,73 @@ int ff_cmap_read_palette(AVCodecContext *avctx, uint32_t *pal)
         return AVERROR_INVALIDDATA;
     }
     for (i=0; i < count; i++) {
-        pal[i] = AV_RB24( avctx->extradata + i*3 );
+        pal[i] = 0xFF000000 | AV_RB24( avctx->extradata + i*3 );
     }
     return 0;
 }
 
 static av_cold int decode_init(AVCodecContext *avctx)
 {
-    AVFrame *frame = avctx->priv_data;
+    IffContext *s = avctx->priv_data;
+    int err;
 
-    avctx->pix_fmt = PIX_FMT_PAL8;
-    frame->reference = 1;
+    if (avctx->bits_per_coded_sample <= 8) {
+        avctx->pix_fmt = PIX_FMT_PAL8;
+    } else if (avctx->bits_per_coded_sample <= 32) {
+        avctx->pix_fmt = PIX_FMT_BGR32;
+    } else {
+        return AVERROR_INVALIDDATA;
+    }
+
+    s->planesize = FFALIGN(avctx->width, 16) >> 3; // Align plane size in bits to word-boundary
+    s->planebuf = av_malloc(s->planesize + FF_INPUT_BUFFER_PADDING_SIZE);
+    if (!s->planebuf)
+        return AVERROR(ENOMEM);
 
-    if (avctx->get_buffer(avctx, frame) < 0) {
+    s->frame.reference = 1;
+    if ((err = avctx->get_buffer(avctx, &s->frame) < 0)) {
         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
-        return AVERROR_UNKNOWN;
+        return err;
     }
-    return ff_cmap_read_palette(avctx, (uint32_t*)frame->data[1]);
+
+    return avctx->bits_per_coded_sample <= 8 ?
+       ff_cmap_read_palette(avctx, (uint32_t*)s->frame.data[1]) : 0;
 }
 
 /**
- * Interleaved memcpy
+ * Decode interleaved plane buffer up to 8bpp
+ * @param dst Destination buffer
+ * @param buf Source buffer
+ * @param buf_size
+ * @param bps bits_per_coded_sample (must be <= 8)
+ * @param plane plane number to decode as
  */
-static void imemcpy(uint8_t *dst, const uint8_t const *buf, int x, int bps, int plane, int length)
+static void decodeplane8(uint8_t *dst, const uint8_t *buf, int buf_size, int bps, int plane)
 {
-    int i, b;
-    for(i = 0; i < length; i++) {
-        int value = buf[i];
-        for (b = 0; b < bps; b++) {
-            if (value & (1<<b))
-                dst[ (x+i)*bps + 7 - b] |= 1<<plane;
-       }
+    const uint64_t *lut = plane8_lut[plane];
+    while (buf_size--) {
+        uint64_t v = AV_RN64A(dst) | lut[*buf++];
+        AV_WN64A(dst, v);
+        dst += 8;
     }
 }
 
 /**
- * Interleaved memset
+ * Decode interleaved plane buffer up to 24bpp
+ * @param dst Destination buffer
+ * @param buf Source buffer
+ * @param buf_size
+ * @param bps bits_per_coded_sample
+ * @param plane plane number to decode as
  */
-static void imemset(uint8_t *dst, int value, int x, int bps, int plane, int length)
+static void decodeplane32(uint32_t *dst, const uint8_t *const buf, int buf_size, int bps, int plane)
 {
-    int i, b;
-    for(i = 0; i < length; i++) {
-        for (b = 0; b < bps; b++) {
-            if (value & (1<<b))
-                dst[ (x+i)*bps + 7 - b] |= 1<<plane;
-       }
+    GetBitContext gb;
+    int i;
+    const int b = buf_size * 8;
+    init_get_bits(&gb, buf, buf_size * 8);
+    for(i = 0; i < b; i++) {
+        dst[i] |= get_bits1(&gb) << plane;
     }
 }
 
@@ -97,33 +171,39 @@ static int decode_frame_ilbm(AVCodecContext *avctx,
                             void *data, int *data_size,
                             AVPacket *avpkt)
 {
-    AVFrame *frame = avctx->priv_data;
+    IffContext *s = avctx->priv_data;
     const uint8_t *buf = avpkt->data;
     int buf_size = avpkt->size;
-    int planewidth = avctx->width / avctx->bits_per_coded_sample;
+    const uint8_t *buf_end = buf+buf_size;
     int y, plane;
 
-    if (avctx->reget_buffer(avctx, frame) < 0){
+    if (avctx->reget_buffer(avctx, &s->frame) < 0){
         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
         return -1;
     }
 
-    if (buf_size < avctx->width * avctx->height) {
-        av_log(avctx, AV_LOG_ERROR, "buffer underflow\n");
-        return -1;
-    }
-
-    for(y = 0; y < avctx->height; y++ ) {
-        uint8_t *row = &frame->data[0][ y*frame->linesize[0] ];
-        memset(row, 0, avctx->width);
-        for (plane = 0; plane < avctx->bits_per_coded_sample; plane++) {
-            imemcpy(row, buf, 0, avctx->bits_per_coded_sample, plane, planewidth);
-            buf += planewidth;
+    if (avctx->pix_fmt == PIX_FMT_PAL8) {
+        for(y = 0; y < avctx->height; y++ ) {
+            uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];
+            memset(row, 0, avctx->width);
+            for (plane = 0; plane < avctx->bits_per_coded_sample && buf < buf_end; plane++) {
+                decodeplane8(row, buf, FFMIN(s->planesize, buf_end - buf), avctx->bits_per_coded_sample, plane);
+                buf += s->planesize;
+            }
+        }
+    } else { // PIX_FMT_BGR32
+        for(y = 0; y < avctx->height; y++ ) {
+            uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
+            memset(row, 0, avctx->width << 2);
+            for (plane = 0; plane < avctx->bits_per_coded_sample && buf < buf_end; plane++) {
+                decodeplane32((uint32_t *) row, buf, FFMIN(s->planesize, buf_end - buf), avctx->bits_per_coded_sample, plane);
+                buf += s->planesize;
+            }
         }
     }
 
     *data_size = sizeof(AVFrame);
-    *(AVFrame*)data = *frame;
+    *(AVFrame*)data = s->frame;
     return buf_size;
 }
 
@@ -131,43 +211,71 @@ static int decode_frame_byterun1(AVCodecContext *avctx,
                             void *data, int *data_size,
                             AVPacket *avpkt)
 {
-    AVFrame *frame = avctx->priv_data;
+    IffContext *s = avctx->priv_data;
     const uint8_t *buf = avpkt->data;
     int buf_size = avpkt->size;
     const uint8_t *buf_end = buf+buf_size;
-    int planewidth = avctx->width / avctx->bits_per_coded_sample;
     int y, plane, x;
 
-    if (avctx->reget_buffer(avctx, frame) < 0){
+    if (avctx->reget_buffer(avctx, &s->frame) < 0){
         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
         return -1;
     }
 
-    for(y = 0; y < avctx->height ; y++ ) {
-        uint8_t *row = &frame->data[0][ y*frame->linesize[0] ];
-        if (avctx->codec_tag == MKTAG('I','L','B','M')) { //interleaved
-            memset(row, 0, avctx->width);
-            for (plane = 0; plane < avctx->bits_per_coded_sample; plane++) {
-                for(x = 0; x < planewidth && buf < buf_end; ) {
-                    char value = *buf++;
-                    int length;
-                    if (value >= 0) {
-                        length = value + 1;
-                        imemcpy(row, buf, x, avctx->bits_per_coded_sample, plane, FFMIN3(length, buf_end - buf, planewidth - x));
-                        buf += length;
-                    } else if (value > -128) {
-                        length = -value + 1;
-                        imemset(row, *buf++, x, avctx->bits_per_coded_sample, plane, FFMIN(length, planewidth - x));
-                    } else { //noop
-                        continue;
+    if (avctx->codec_tag == MKTAG('I','L','B','M')) { //interleaved
+        if (avctx->pix_fmt == PIX_FMT_PAL8) {
+            for(y = 0; y < avctx->height ; y++ ) {
+                uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];
+                memset(row, 0, avctx->width);
+                for (plane = 0; plane < avctx->bits_per_coded_sample; plane++) {
+                    for(x = 0; x < s->planesize && buf < buf_end; ) {
+                        int8_t value = *buf++;
+                        unsigned length;
+                        if (value >= 0) {
+                            length = value + 1;
+                            memcpy(s->planebuf + x, buf, FFMIN3(length, s->planesize - x, buf_end - buf));
+                            buf += length;
+                        } else if (value > -128) {
+                            length = -value + 1;
+                            memset(s->planebuf + x, *buf++, FFMIN(length, s->planesize - x));
+                        } else { //noop
+                            continue;
+                        }
+                        x += length;
                     }
-                    x += length;
+                    decodeplane8(row, s->planebuf, s->planesize, avctx->bits_per_coded_sample, plane);
                 }
             }
-        } else {
+        } else { //PIX_FMT_BGR32
+            for(y = 0; y < avctx->height ; y++ ) {
+                uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
+                memset(row, 0, avctx->width << 2);
+                for (plane = 0; plane < avctx->bits_per_coded_sample; plane++) {
+                    for(x = 0; x < s->planesize && buf < buf_end; ) {
+                        int8_t value = *buf++;
+                        unsigned length;
+                        if (value >= 0) {
+                            length = value + 1;
+                            memcpy(s->planebuf + x, buf, FFMIN3(length, s->planesize - x, buf_end - buf));
+                            buf += length;
+                        } else if (value > -128) {
+                            length = -value + 1;
+                            memset(s->planebuf + x, *buf++, FFMIN(length, s->planesize - x));
+                        } else { // noop
+                            continue;
+                        }
+                        x += length;
+                    }
+                    decodeplane32((uint32_t *) row, s->planebuf, s->planesize, avctx->bits_per_coded_sample, plane);
+                }
+            }
+        }
+    } else {
+        for(y = 0; y < avctx->height ; y++ ) {
+            uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
             for(x = 0; x < avctx->width && buf < buf_end; ) {
-                char value = *buf++;
-                int length;
+                int8_t value = *buf++;
+                unsigned length;
                 if (value >= 0) {
                     length = value + 1;
                     memcpy(row + x, buf, FFMIN3(length, buf_end - buf, avctx->width - x));
@@ -184,23 +292,24 @@ static int decode_frame_byterun1(AVCodecContext *avctx,
     }
 
     *data_size = sizeof(AVFrame);
-    *(AVFrame*)data = *frame;
+    *(AVFrame*)data = s->frame;
     return buf_size;
 }
 
 static av_cold int decode_end(AVCodecContext *avctx)
 {
-    AVFrame *frame = avctx->priv_data;
-    if (frame->data[0])
-        avctx->release_buffer(avctx, frame);
+    IffContext *s = avctx->priv_data;
+    if (s->frame.data[0])
+        avctx->release_buffer(avctx, &s->frame);
+    av_freep(&s->planebuf);
     return 0;
 }
 
 AVCodec iff_ilbm_decoder = {
     "iff_ilbm",
-    CODEC_TYPE_VIDEO,
+    AVMEDIA_TYPE_VIDEO,
     CODEC_ID_IFF_ILBM,
-    sizeof(AVFrame),
+    sizeof(IffContext),
     decode_init,
     NULL,
     decode_end,
@@ -211,9 +320,9 @@ AVCodec iff_ilbm_decoder = {
 
 AVCodec iff_byterun1_decoder = {
     "iff_byterun1",
-    CODEC_TYPE_VIDEO,
+    AVMEDIA_TYPE_VIDEO,
     CODEC_ID_IFF_BYTERUN1,
-    sizeof(AVFrame),
+    sizeof(IffContext),
     decode_init,
     NULL,
     decode_end,