]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/exr.c
exr: pxr24 decompression
[ffmpeg] / libavcodec / exr.c
index 7e9e68c17b965f3fca8fe0a49d36a3f0081fcca3..8c1cca4073b44e275ae8cd70292dedf1a2e43765 100644 (file)
@@ -37,6 +37,7 @@
 #include "mathops.h"
 #include "thread.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/avassert.h"
 
 enum ExrCompr {
     EXR_RAW   = 0,
@@ -44,10 +45,22 @@ enum ExrCompr {
     EXR_ZIP1  = 2,
     EXR_ZIP16 = 3,
     EXR_PIZ   = 4,
+    EXR_PXR24 = 5,
     EXR_B44   = 6,
     EXR_B44A  = 7,
 };
 
+enum ExrPixelType {
+    EXR_UINT,
+    EXR_HALF,
+    EXR_FLOAT
+};
+
+typedef struct EXRChannel {
+    int               xsub, ysub;
+    enum ExrPixelType pixel_type;
+} EXRChannel;
+
 typedef struct EXRThreadData {
     uint8_t *uncompressed_data;
     int uncompressed_size;
@@ -59,7 +72,7 @@ typedef struct EXRThreadData {
 typedef struct EXRContext {
     AVFrame picture;
     int compr;
-    int bits_per_color_id;
+    enum ExrPixelType pixel_type;
     int channel_offsets[4]; // 0 = red, 1 = green, 2 = blue and 3 = alpha
     const AVPixFmtDescriptor *desc;
 
@@ -67,12 +80,17 @@ typedef struct EXRContext {
     uint32_t ymax, ymin;
     uint32_t xdelta, ydelta;
 
+    int ysize;
+
     uint64_t scan_line_size;
     int scan_lines_per_block;
 
     const uint8_t *buf, *table;
     int buf_size;
 
+    EXRChannel *channels;
+    int nb_channels;
+
     EXRThreadData *thread_data;
     int thread_data_size;
 } EXRContext;
@@ -259,6 +277,61 @@ static int rle_uncompress(const uint8_t *src, int compressed_size,
     return 0;
 }
 
+static int pxr24_uncompress(EXRContext *s, const uint8_t *src,
+                            int compressed_size, int uncompressed_size,
+                            EXRThreadData *td)
+{
+    unsigned long dest_len = uncompressed_size;
+    const uint8_t *in = td->tmp;
+    uint8_t *out;
+    int c, i, j;
+
+    if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
+        dest_len != uncompressed_size)
+        return AVERROR(EINVAL);
+
+    out = td->uncompressed_data;
+    for (i = 0; i < s->ysize; i++) {
+        for (c = 0; c < s->nb_channels; c++) {
+            EXRChannel *channel = &s->channels[c];
+            const uint8_t *ptr[4];
+            uint32_t pixel = 0;
+
+            switch (channel->pixel_type) {
+            case EXR_FLOAT:
+                ptr[0] = in;
+                ptr[1] = ptr[0] + s->xdelta;
+                ptr[2] = ptr[1] + s->xdelta;
+                in = ptr[2] + s->xdelta;
+
+                for (j = 0; j < s->xdelta; ++j) {
+                    uint32_t diff = (*(ptr[0]++) << 24) |
+                                    (*(ptr[1]++) << 16) |
+                                    (*(ptr[2]++) <<  8);
+                    pixel += diff;
+                    AV_WL32(out, pixel);
+                }
+                break;
+            case EXR_HALF:
+                ptr[0] = in;
+                ptr[1] = ptr[0] + s->xdelta;
+                in = ptr[1] + s->xdelta;
+                for (j = 0; j < s->xdelta; j++, out += 2) {
+                    uint32_t diff = (*(ptr[0]++) << 8) | *(ptr[1]++);
+
+                    pixel += diff;
+                    AV_WL16(out, pixel);
+                }
+                break;
+            default:
+                av_assert1(0);
+            }
+        }
+    }
+
+    return 0;
+}
+
 static int decode_block(AVCodecContext *avctx, void *tdata,
                         int jobnr, int threadnr)
 {
@@ -291,7 +364,8 @@ static int decode_block(AVCodecContext *avctx, void *tdata,
     if (data_size <= 0 || data_size > buf_size)
         return AVERROR_INVALIDDATA;
 
-    uncompressed_size = s->scan_line_size * FFMIN(s->scan_lines_per_block, s->ymax - line + 1);
+    s->ysize = FFMIN(s->scan_lines_per_block, s->ymax - line + 1);
+    uncompressed_size = s->scan_line_size * s->ysize;
     if ((s->compr == EXR_RAW && (data_size != uncompressed_size ||
                                  line_offset > buf_size - uncompressed_size)) ||
         (s->compr != EXR_RAW && (data_size > uncompressed_size ||
@@ -310,6 +384,9 @@ static int decode_block(AVCodecContext *avctx, void *tdata,
         case EXR_ZIP16:
             ret = zip_uncompress(src, data_size, uncompressed_size, td);
             break;
+        case EXR_PXR24:
+            ret = pxr24_uncompress(s, src, data_size, uncompressed_size, td);
+            break;
         case EXR_RLE:
             ret = rle_uncompress(src, data_size, uncompressed_size, td);
         }
@@ -338,7 +415,7 @@ static int decode_block(AVCodecContext *avctx, void *tdata,
         // Zero out the start if xmin is not 0
         memset(ptr_x, 0, bxmin);
         ptr_x += s->xmin * s->desc->nb_components;
-        if (s->bits_per_color_id == 2) {
+        if (s->pixel_type == EXR_FLOAT) {
             // 32-bit
             for (x = 0; x < xdelta; x++) {
                 *ptr_x++ = exr_flt2uint(bytestream_get_le32(&r));
@@ -404,7 +481,8 @@ static int decode_frame(AVCodecContext *avctx,
     s->channel_offsets[1] = -1;
     s->channel_offsets[2] = -1;
     s->channel_offsets[3] = -1;
-    s->bits_per_color_id = -1;
+    s->pixel_type = -1;
+    s->nb_channels = 0;
     s->compr = -1;
     s->buf = buf;
     s->buf_size = buf_size;
@@ -443,7 +521,8 @@ static int decode_frame(AVCodecContext *avctx,
 
             channel_list_end = buf + variable_buffer_data_size;
             while (channel_list_end - buf >= 19) {
-                int current_bits_per_color_id = -1;
+                EXRChannel *channel;
+                int current_pixel_type = -1;
                 int channel_index = -1;
                 int xsub, ysub;
 
@@ -466,9 +545,9 @@ static int decode_frame(AVCodecContext *avctx,
                     return AVERROR_INVALIDDATA;
                 }
 
-                current_bits_per_color_id = bytestream_get_le32(&buf);
-                if (current_bits_per_color_id > 2) {
-                    av_log(avctx, AV_LOG_ERROR, "Unknown color format\n");
+                current_pixel_type = bytestream_get_le32(&buf);
+                if (current_pixel_type > 2) {
+                    av_log(avctx, AV_LOG_ERROR, "Unknown pixel type\n");
                     return AVERROR_INVALIDDATA;
                 }
 
@@ -481,15 +560,23 @@ static int decode_frame(AVCodecContext *avctx,
                 }
 
                 if (channel_index >= 0) {
-                    if (s->bits_per_color_id != -1 && s->bits_per_color_id != current_bits_per_color_id) {
+                    if (s->pixel_type != -1 && s->pixel_type != current_pixel_type) {
                         av_log(avctx, AV_LOG_ERROR, "RGB channels not of the same depth\n");
                         return AVERROR_INVALIDDATA;
                     }
-                    s->bits_per_color_id  = current_bits_per_color_id;
+                    s->pixel_type = current_pixel_type;
                     s->channel_offsets[channel_index] = current_channel_offset;
                 }
 
-                current_channel_offset += 1 << current_bits_per_color_id;
+                s->channels = av_realloc_f(s->channels, ++s->nb_channels, sizeof(EXRChannel));
+                if (!s->channels)
+                    return AVERROR(ENOMEM);
+                channel = &s->channels[s->nb_channels - 1];
+                channel->pixel_type = current_pixel_type;
+                channel->xsub = xsub;
+                channel->ysub = ysub;
+
+                current_channel_offset += 1 << current_pixel_type;
             }
 
             /* Check if all channels are set with an offset or if the channels
@@ -600,20 +687,19 @@ static int decode_frame(AVCodecContext *avctx,
     }
     buf++;
 
-    switch (s->bits_per_color_id) {
-    case 2: // 32-bit
-    case 1: // 16-bit
+    switch (s->pixel_type) {
+    case EXR_FLOAT:
+    case EXR_HALF:
         if (s->channel_offsets[3] >= 0)
             avctx->pix_fmt = AV_PIX_FMT_RGBA64;
         else
             avctx->pix_fmt = AV_PIX_FMT_RGB48;
         break;
-    // 8-bit
-    case 0:
-        av_log_missing_feature(avctx, "8-bit OpenEXR", 1);
+    case EXR_UINT:
+        av_log_missing_feature(avctx, "32-bit unsigned int", 1);
         return AVERROR_PATCHWELCOME;
     default:
-        av_log(avctx, AV_LOG_ERROR, "Unknown color format : %d\n", s->bits_per_color_id);
+        av_log(avctx, AV_LOG_ERROR, "Missing channel list\n");
         return AVERROR_INVALIDDATA;
     }
 
@@ -623,6 +709,7 @@ static int decode_frame(AVCodecContext *avctx,
     case EXR_ZIP1:
         s->scan_lines_per_block = 1;
         break;
+    case EXR_PXR24:
     case EXR_ZIP16:
         s->scan_lines_per_block = 16;
         break;
@@ -655,7 +742,7 @@ static int decode_frame(AVCodecContext *avctx,
     scan_line_blocks = (s->ydelta + s->scan_lines_per_block - 1) / s->scan_lines_per_block;
 
     if (s->compr != EXR_RAW) {
-        int thread_data_size, prev_size;
+        size_t thread_data_size, prev_size;
         EXRThreadData *m;
 
         prev_size = s->thread_data_size;
@@ -725,6 +812,7 @@ static av_cold int decode_end(AVCodecContext *avctx)
 
     av_freep(&s->thread_data);
     s->thread_data_size = 0;
+    av_freep(&s->channels);
 
     return 0;
 }