[FFmpeg-devel] [PATCH] exr: zip1 decompression

Paul B Mahol onemda at gmail.com
Wed Jul 11 01:26:07 CEST 2012


Signed-off-by: Paul B Mahol <onemda at gmail.com>
---
 configure        |    1 +
 libavcodec/exr.c |   98 +++++++++++++++++++++++++++++++++++++++++++++++------
 2 files changed, 88 insertions(+), 11 deletions(-)

diff --git a/configure b/configure
index 1958fa6..8411836 100755
--- a/configure
+++ b/configure
@@ -1467,6 +1467,7 @@ eac3_encoder_select="mdct ac3dsp"
 eamad_decoder_select="aandct"
 eatgq_decoder_select="aandct"
 eatqi_decoder_select="aandct"
+exr_decoder_select="zlib"
 ffv1_decoder_select="golomb"
 flac_decoder_select="golomb"
 flac_encoder_select="golomb lpc"
diff --git a/libavcodec/exr.c b/libavcodec/exr.c
index 2a91722..ee82d77 100644
--- a/libavcodec/exr.c
+++ b/libavcodec/exr.c
@@ -30,6 +30,8 @@
  * exr_flt2uint() and exr_halflt2uint() is credited to  Reimar Döffinger
  */
 
+#include <zlib.h>
+
 #include "avcodec.h"
 #include "bytestream.h"
 #include "libavutil/imgutils.h"
@@ -48,6 +50,12 @@ typedef struct EXRContext {
     int compr;
     int bits_per_color_id;
     int8_t channel_offsets[4]; // 0 = red, 1 = green, 2 = blue and 3 = alpha
+
+    uint8_t *uncompressed_data;
+    int uncompressed_data_size;
+
+    uint8_t *tmp;
+    int tmp_size;;
 } EXRContext;
 
 /**
@@ -142,6 +150,38 @@ static int check_header_variable(AVCodecContext *avctx,
     return -1;
 }
 
+static void predictor(uint8_t *src, int size)
+{
+    uint8_t *t = src + 1;
+    uint8_t *stop = src + size;
+
+    while (t < stop) {
+        int d = (int)t[-1] + (int)t[0] - 128;
+        t[0] = d;
+        ++t;
+    }
+}
+
+static void reorder_data(uint8_t *src, uint8_t *dst, int size)
+{
+    const int8_t *t1 = src;
+    const int8_t *t2 = src + (size + 1) / 2;
+    int8_t *s = dst;
+    int8_t *stop = s + size;
+
+    while (1) {
+        if (s < stop)
+            *(s++) = *(t1++);
+        else
+            break;
+
+        if (s < stop)
+            *(s++) = *(t2++);
+        else
+            break;
+    }
+}
+
 static int decode_frame(AVCodecContext *avctx,
                         void *data,
                         int *data_size,
@@ -165,6 +205,8 @@ static int decode_frame(AVCodecContext *avctx,
     unsigned int ymax   = ~0;
     unsigned int xdelta = ~0;
 
+    unsigned long uncompressed_data_size;
+
     unsigned int current_channel_offset = 0;
 
     s->channel_offsets[0] = -1;
@@ -319,10 +361,10 @@ static int decode_frame(AVCodecContext *avctx,
             s->compr = *buf;
             switch (s->compr) {
             case EXR_RAW:
-                break;
-            case EXR_RLE:
             case EXR_ZIP1:
+                break;
             case EXR_ZIP16:
+            case EXR_RLE:
             case EXR_PIZ:
             case EXR_B44:
             default:
@@ -382,6 +424,8 @@ static int decode_frame(AVCodecContext *avctx,
         return AVERROR_INVALIDDATA;
     }
 
+    uncompressed_data_size = xdelta * av_pix_fmt_descriptors[avctx->pix_fmt].nb_components * FFMAX(2 * s->bits_per_color_id, 1);
+
     if (s->picture.data[0])
         avctx->release_buffer(avctx, &s->picture);
     if (av_image_check_size(w, h, 0, avctx))
@@ -412,25 +456,53 @@ static int decode_frame(AVCodecContext *avctx,
     }
 
     // Process the actual lines
-    for (y = ymin; y <= ymax; y++) {
+    for (y = ymin; y <= ymax; y++, ptr += stride) {
         uint16_t *ptr_x = (uint16_t *)ptr;
         if (buf_end - buf > 8) {
             /* Read the lineoffset from the line offset table and add 8 bytes
                to skip the coordinates and data size fields */
             const uint64_t line_offset = bytestream_get_le64(&buf) + 8;
+            int32_t data_size;
+
             // Check if the buffer has the required bytes needed from the offset
-            if (line_offset > avpkt->size - xdelta * current_channel_offset) {
+            if ((line_offset > buf_size) ||
+                (s->compr == EXR_RAW && line_offset > avpkt->size - xdelta * current_channel_offset) ||
+                (s->compr != EXR_RAW && line_offset > buf_size - (data_size = AV_RL32(avpkt->data + line_offset - 4)))) {
                 // Line offset is probably wrong and not inside the buffer
                 av_log(avctx, AV_LOG_WARNING, "Line offset for line %d is out of reach setting it to black\n", y);
                 memset(ptr_x, 0, avctx->width * 2 * av_pix_fmt_descriptors[avctx->pix_fmt].nb_components);
             } else {
-                const uint8_t *red_channel_buffer   = avpkt->data + line_offset + xdelta * s->channel_offsets[0];
-                const uint8_t *green_channel_buffer = avpkt->data + line_offset + xdelta * s->channel_offsets[1];
-                const uint8_t *blue_channel_buffer  = avpkt->data + line_offset + xdelta * s->channel_offsets[2];
+                const uint8_t *red_channel_buffer;
+                const uint8_t *green_channel_buffer;
+                const uint8_t *blue_channel_buffer;
                 const uint8_t *alpha_channel_buffer = 0;
 
-                if (s->channel_offsets[3] >= 0)
-                    alpha_channel_buffer = avpkt->data + line_offset + xdelta * s->channel_offsets[3];
+                if (s->compr == EXR_ZIP1 && data_size < uncompressed_data_size) {
+                    av_fast_padded_malloc(&s->uncompressed_data, &s->uncompressed_data_size, uncompressed_data_size);
+                    av_fast_padded_malloc(&s->tmp, &s->tmp_size, uncompressed_data_size);
+                    if (!s->uncompressed_data || !s->tmp)
+                        return AVERROR(ENOMEM);
+
+                    if (uncompress(s->tmp, &uncompressed_data_size, avpkt->data + line_offset, data_size) != Z_OK) {
+                        av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n");
+                        return AVERROR(EINVAL);
+                    }
+
+                    predictor(s->tmp, uncompressed_data_size);
+                    reorder_data(s->tmp, s->uncompressed_data, uncompressed_data_size);
+
+                    red_channel_buffer   = s->uncompressed_data + xdelta * s->channel_offsets[0];
+                    green_channel_buffer = s->uncompressed_data + xdelta * s->channel_offsets[1];
+                    blue_channel_buffer  = s->uncompressed_data + xdelta * s->channel_offsets[2];
+                    if (s->channel_offsets[3] >= 0)
+                        alpha_channel_buffer = s->uncompressed_data + xdelta * s->channel_offsets[3];
+                } else {
+                    red_channel_buffer   = avpkt->data + line_offset + xdelta * s->channel_offsets[0];
+                    green_channel_buffer = avpkt->data + line_offset + xdelta * s->channel_offsets[1];
+                    blue_channel_buffer  = avpkt->data + line_offset + xdelta * s->channel_offsets[2];
+                    if (s->channel_offsets[3] >= 0)
+                        alpha_channel_buffer = avpkt->data + line_offset + xdelta * s->channel_offsets[3];
+                }
 
                 // Zero out the start if xmin is not 0
                 memset(ptr_x, 0, xmin * 2 * av_pix_fmt_descriptors[avctx->pix_fmt].nb_components);
@@ -460,8 +532,6 @@ static int decode_frame(AVCodecContext *avctx,
                 ptr_x += (avctx->width - (xmax + 1)) * av_pix_fmt_descriptors[avctx->pix_fmt].nb_components;
 
             }
-            // Move to next line
-            ptr += stride;
         }
     }
 
@@ -480,17 +550,23 @@ static int decode_frame(AVCodecContext *avctx,
 static av_cold int decode_init(AVCodecContext *avctx)
 {
     EXRContext *s = avctx->priv_data;
+
     avcodec_get_frame_defaults(&s->picture);
     avctx->coded_frame = &s->picture;
+
     return 0;
 }
 
 static av_cold int decode_end(AVCodecContext *avctx)
 {
     EXRContext *s = avctx->priv_data;
+
     if (s->picture.data[0])
         avctx->release_buffer(avctx, &s->picture);
 
+    av_freep(&s->uncompressed_data);
+    av_freep(&s->tmp);
+
     return 0;
 }
 
-- 
1.7.7



More information about the ffmpeg-devel mailing list