lavc/htmlsubtitles: handle colors starting with many '#'
[ffmpeg.git] / libavcodec / exr.c
index 6960a29..7598807 100644 (file)
@@ -3,7 +3,7 @@
  * Copyright (c) 2006 Industrial Light & Magic, a division of Lucas Digital Ltd. LLC
  * Copyright (c) 2009 Jimmy Christensen
  *
- * B44/B44A, Tile added by Jokyo Images support by CNC - French National Center for Cinema
+ * B44/B44A, Tile, UINT32 added by Jokyo Images support by CNC - French National Center for Cinema
  *
  * This file is part of FFmpeg.
  *
@@ -37,6 +37,7 @@
 #include <float.h>
 #include <zlib.h>
 
+#include "libavutil/avassert.h"
 #include "libavutil/common.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/intfloat.h"
 
 #include "avcodec.h"
 #include "bytestream.h"
+
+#if HAVE_BIGENDIAN
+#include "bswapdsp.h"
+#endif
+
 #include "get_bits.h"
 #include "internal.h"
 #include "mathops.h"
@@ -59,6 +65,8 @@ enum ExrCompr {
     EXR_PXR24,
     EXR_B44,
     EXR_B44A,
+    EXR_DWA,
+    EXR_DWB,
     EXR_UNKN,
 };
 
@@ -114,6 +122,10 @@ typedef struct EXRContext {
     AVFrame *picture;
     AVCodecContext *avctx;
 
+#if HAVE_BIGENDIAN
+    BswapDSPContext bbdsp;
+#endif
+
     enum ExrCompr compression;
     enum ExrPixelType pixel_type;
     int channel_offsets[4]; // 0 = red, 1 = green, 2 = blue and 3 = alpha
@@ -218,9 +230,9 @@ static union av_intfloat32 exr_half2float(uint16_t hf)
  *
  * @return normalized 16-bit unsigned int
  */
-static inline uint16_t exr_flt2uint(uint32_t v)
+static inline uint16_t exr_flt2uint(int32_t v)
 {
-    unsigned int exp = v >> 23;
+    int32_t exp = v >> 23;
     // "HACK": negative values result in exp<  0, so clipping them to 0
     // is also handled by this condition, avoids explicit check for sign bit.
     if (exp <= 127 + 7 - 24) // we would shift out all bits anyway
@@ -265,21 +277,17 @@ static void predictor(uint8_t *src, int size)
 
 static void reorder_pixels(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;
+    const uint8_t *t1 = src;
+    int half_size     = size / 2;
+    const uint8_t *t2 = src + half_size;
+    uint8_t *s        = dst;
+    int i;
 
-        if (s < stop)
-            *(s++) = *(t2++);
-        else
-            break;
+    av_assert1(size % 2 == 0);
+
+    for (i = 0; i < half_size; i++) {
+        *(s++) = *(t1++);
+        *(s++) = *(t2++);
     }
 }
 
@@ -749,7 +757,8 @@ static int piz_uncompress(EXRContext *s, const uint8_t *src, int ssize,
     uint16_t maxval, min_non_zero, max_non_zero;
     uint16_t *ptr;
     uint16_t *tmp = (uint16_t *)td->tmp;
-    uint8_t *out;
+    uint16_t *out;
+    uint16_t *in;
     int ret, i, j;
     int pixel_half_size;/* 1 for half, 2 for float and uint32 */
     EXRChannel *channel;
@@ -801,12 +810,11 @@ static int piz_uncompress(EXRContext *s, const uint8_t *src, int ssize,
 
     apply_lut(td->lut, tmp, dsize / sizeof(uint16_t));
 
-    out = td->uncompressed_data;
+    out = (uint16_t *)td->uncompressed_data;
     for (i = 0; i < td->ysize; i++) {
         tmp_offset = 0;
         for (j = 0; j < s->nb_channels; j++) {
-            uint16_t *in;
-            EXRChannel *channel = &s->channels[j];
+            channel = &s->channels[j];
             if (channel->pixel_type == EXR_HALF)
                 pixel_half_size = 1;
             else
@@ -814,8 +822,13 @@ static int piz_uncompress(EXRContext *s, const uint8_t *src, int ssize,
 
             in = tmp + tmp_offset * td->xsize * td->ysize + i * td->xsize * pixel_half_size;
             tmp_offset += pixel_half_size;
+
+#if HAVE_BIGENDIAN
+            s->bbdsp.bswap16_buf(out, in, td->xsize * pixel_half_size);
+#else
             memcpy(out, in, td->xsize * 2 * pixel_half_size);
-            out += td->xsize * 2 * pixel_half_size;
+#endif
+            out += td->xsize * pixel_half_size;
         }
     }
 
@@ -882,6 +895,22 @@ static int pxr24_uncompress(EXRContext *s, const uint8_t *src,
                     bytestream_put_le16(&out, pixel);
                 }
                 break;
+            case EXR_UINT:
+                ptr[0] = in;
+                ptr[1] = ptr[0] + s->xdelta;
+                ptr[2] = ptr[1] + s->xdelta;
+                ptr[3] = ptr[2] + s->xdelta;
+                in     = ptr[3] + s->xdelta;
+
+                for (j = 0; j < s->xdelta; ++j) {
+                    uint32_t diff = (*(ptr[0]++) << 24) |
+                    (*(ptr[1]++) << 16) |
+                    (*(ptr[2]++) << 8 ) |
+                    (*(ptr[3]++));
+                    pixel += diff;
+                    bytestream_put_le32(&out, pixel);
+                }
+                break;
             default:
                 return AVERROR_INVALIDDATA;
             }
@@ -944,74 +973,74 @@ static void unpack_3(const uint8_t b[3], uint16_t s[16])
 static int b44_uncompress(EXRContext *s, const uint8_t *src, int compressed_size,
                           int uncompressed_size, EXRThreadData *td) {
     const int8_t *sr = src;
-    int stayToUncompress = compressed_size;
-    int nbB44BlockW, nbB44BlockH;
-    int indexHgX, indexHgY, indexOut, indexTmp;
-    uint16_t tmpBuffer[16]; /* B44 use 4x4 half float pixel */
+    int stay_to_uncompress = compressed_size;
+    int nb_b44_block_w, nb_b44_block_h;
+    int index_tl_x, index_tl_y, index_out, index_tmp;
+    uint16_t tmp_buffer[16]; /* B44 use 4x4 half float pixel */
     int c, iY, iX, y, x;
     int target_channel_offset = 0;
 
     /* calc B44 block count */
-    nbB44BlockW = td->xsize / 4;
+    nb_b44_block_w = td->xsize / 4;
     if ((td->xsize % 4) != 0)
-        nbB44BlockW++;
+        nb_b44_block_w++;
 
-    nbB44BlockH = td->ysize / 4;
+    nb_b44_block_h = td->ysize / 4;
     if ((td->ysize % 4) != 0)
-        nbB44BlockH++;
+        nb_b44_block_h++;
 
     for (c = 0; c < s->nb_channels; c++) {
         if (s->channels[c].pixel_type == EXR_HALF) {/* B44 only compress half float data */
-            for (iY = 0; iY < nbB44BlockH; iY++) {
-                for (iX = 0; iX < nbB44BlockW; iX++) {/* For each B44 block */
-                    if (stayToUncompress < 3) {
-                        av_log(s, AV_LOG_ERROR, "Not enough data for B44A block: %d", stayToUncompress);
+            for (iY = 0; iY < nb_b44_block_h; iY++) {
+                for (iX = 0; iX < nb_b44_block_w; iX++) {/* For each B44 block */
+                    if (stay_to_uncompress < 3) {
+                        av_log(s, AV_LOG_ERROR, "Not enough data for B44A block: %d", stay_to_uncompress);
                         return AVERROR_INVALIDDATA;
                     }
 
-                    if (src[compressed_size - stayToUncompress + 2] == 0xfc) { /* B44A block */
-                        unpack_3(sr, tmpBuffer);
+                    if (src[compressed_size - stay_to_uncompress + 2] == 0xfc) { /* B44A block */
+                        unpack_3(sr, tmp_buffer);
                         sr += 3;
-                        stayToUncompress -= 3;
+                        stay_to_uncompress -= 3;
                     }  else {/* B44 Block */
-                        if (stayToUncompress < 14) {
-                            av_log(s, AV_LOG_ERROR, "Not enough data for B44 block: %d", stayToUncompress);
+                        if (stay_to_uncompress < 14) {
+                            av_log(s, AV_LOG_ERROR, "Not enough data for B44 block: %d", stay_to_uncompress);
                             return AVERROR_INVALIDDATA;
                         }
-                        unpack_14(sr, tmpBuffer);
+                        unpack_14(sr, tmp_buffer);
                         sr += 14;
-                        stayToUncompress -= 14;
+                        stay_to_uncompress -= 14;
                     }
 
                     /* copy data to uncompress buffer (B44 block can exceed target resolution)*/
-                    indexHgX = iX * 4;
-                    indexHgY = iY * 4;
-
-                    for (y = indexHgY; y < FFMIN(indexHgY + 4, td->ysize); y++) {
-                        for (x = indexHgX; x < FFMIN(indexHgX + 4, td->xsize); x++) {
-                            indexOut = target_channel_offset * td->xsize + y * td->channel_line_size + 2 * x;
-                            indexTmp = (y-indexHgY) * 4 + (x-indexHgX);
-                            td->uncompressed_data[indexOut] = tmpBuffer[indexTmp] & 0xff;
-                            td->uncompressed_data[indexOut + 1] = tmpBuffer[indexTmp] >> 8;
+                    index_tl_x = iX * 4;
+                    index_tl_y = iY * 4;
+
+                    for (y = index_tl_y; y < FFMIN(index_tl_y + 4, td->ysize); y++) {
+                        for (x = index_tl_x; x < FFMIN(index_tl_x + 4, td->xsize); x++) {
+                            index_out = target_channel_offset * td->xsize + y * td->channel_line_size + 2 * x;
+                            index_tmp = (y-index_tl_y) * 4 + (x-index_tl_x);
+                            td->uncompressed_data[index_out] = tmp_buffer[index_tmp] & 0xff;
+                            td->uncompressed_data[index_out + 1] = tmp_buffer[index_tmp] >> 8;
                         }
                     }
                 }
             }
             target_channel_offset += 2;
         } else {/* Float or UINT 32 channel */
-            if (stayToUncompress < td->ysize * td->xsize * 4) {
-                av_log(s, AV_LOG_ERROR, "Not enough data for uncompress channel: %d", stayToUncompress);
+            if (stay_to_uncompress < td->ysize * td->xsize * 4) {
+                av_log(s, AV_LOG_ERROR, "Not enough data for uncompress channel: %d", stay_to_uncompress);
                 return AVERROR_INVALIDDATA;
             }
 
             for (y = 0; y < td->ysize; y++) {
-                indexOut = target_channel_offset * td->xsize + y * td->channel_line_size;
-                memcpy(&td->uncompressed_data[indexOut], sr, td->xsize * 4);
+                index_out = target_channel_offset * td->xsize + y * td->channel_line_size;
+                memcpy(&td->uncompressed_data[index_out], sr, td->xsize * 4);
                 sr += td->xsize * 4;
             }
             target_channel_offset += 4;
 
-            stayToUncompress -= td->ysize * td->xsize * 4;
+            stay_to_uncompress -= td->ysize * td->xsize * 4;
         }
     }
 
@@ -1029,8 +1058,9 @@ static int decode_block(AVCodecContext *avctx, void *tdata,
     uint64_t line_offset, uncompressed_size;
     uint16_t *ptr_x;
     uint8_t *ptr;
-    uint32_t data_size, line, col = 0;
-    uint32_t tileX, tileY, tileLevelX, tileLevelY;
+    uint32_t data_size;
+    uint64_t line, col = 0;
+    uint64_t tile_x, tile_y, tile_level_x, tile_level_y;
     const uint8_t *src;
     int axmax = (avctx->width - (s->xmax + 1)) * 2 * s->desc->nb_components; /* nb pixel to add at the right of the datawindow */
     int bxmin = s->xmin * 2 * s->desc->nb_components; /* nb pixel to add at the left of the datawindow */
@@ -1048,25 +1078,34 @@ static int decode_block(AVCodecContext *avctx, void *tdata,
 
         src  = buf + line_offset + 20;
 
-        tileX = AV_RL32(src - 20);
-        tileY = AV_RL32(src - 16);
-        tileLevelX = AV_RL32(src - 12);
-        tileLevelY = AV_RL32(src - 8);
+        tile_x = AV_RL32(src - 20);
+        tile_y = AV_RL32(src - 16);
+        tile_level_x = AV_RL32(src - 12);
+        tile_level_y = AV_RL32(src - 8);
 
         data_size = AV_RL32(src - 4);
         if (data_size <= 0 || data_size > buf_size)
             return AVERROR_INVALIDDATA;
 
-        if (tileLevelX || tileLevelY) { /* tile level, is not the full res level */
+        if (tile_level_x || tile_level_y) { /* tile level, is not the full res level */
             avpriv_report_missing_feature(s->avctx, "Subres tile before full res tile");
             return AVERROR_PATCHWELCOME;
         }
 
-        line = s->tile_attr.ySize * tileY;
-        col = s->tile_attr.xSize * tileX;
+        if (s->xmin || s->ymin) {
+            avpriv_report_missing_feature(s->avctx, "Tiles with xmin/ymin");
+            return AVERROR_PATCHWELCOME;
+        }
+
+        line = s->tile_attr.ySize * tile_y;
+        col = s->tile_attr.xSize * tile_x;
+
+        if (line < s->ymin || line > s->ymax ||
+            col  < s->xmin || col  > s->xmax)
+            return AVERROR_INVALIDDATA;
 
-        td->ysize = FFMIN(s->tile_attr.ySize, s->ydelta - tileY * s->tile_attr.ySize);
-        td->xsize = FFMIN(s->tile_attr.xSize, s->xdelta - tileX * s->tile_attr.xSize);
+        td->ysize = FFMIN(s->tile_attr.ySize, s->ydelta - tile_y * s->tile_attr.ySize);
+        td->xsize = FFMIN(s->tile_attr.xSize, s->xdelta - tile_x * s->tile_attr.xSize);
 
         if (col) { /* not the first tile of the line */
             bxmin = 0; /* doesn't add pixel at the left of the datawindow */
@@ -1208,7 +1247,7 @@ static int decode_block(AVCodecContext *avctx, void *tdata,
                         *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
                 }
             }
-        } else {
+        } else if (s->pixel_type == EXR_HALF) {
             // 16-bit
             for (x = 0; x < td->xsize; x++) {
                 int c;
@@ -1219,6 +1258,15 @@ static int decode_block(AVCodecContext *avctx, void *tdata,
                 if (channel_buffer[3])
                     *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&a));
             }
+        } else if (s->pixel_type == EXR_UINT) {
+            for (x = 0; x < td->xsize; x++) {
+                for (c = 0; c < rgb_channel_count; c++) {
+                    *ptr_x++ = bytestream_get_le32(&rgb[c]) >> 16;
+                }
+
+                if (channel_buffer[3])
+                    *ptr_x++ = bytestream_get_le32(&a) >> 16;
+            }
         }
 
         // Zero out the end if xmax+1 is not w
@@ -1275,8 +1323,9 @@ static int check_header_variable(EXRContext *s,
     return var_size;
 }
 
-static int decode_header(EXRContext *s)
+static int decode_header(EXRContext *s, AVFrame *frame)
 {
+    AVDictionary *metadata = NULL;
     int magic_number, version, i, flags, sar = 0;
     int layer_match = 0;
 
@@ -1418,17 +1467,15 @@ static int decode_header(EXRContext *s)
                     return AVERROR_PATCHWELCOME;
                 }
 
-                if (s->channel_offsets[channel_index] == -1){/* channel have not been previously assign */
-                    if (channel_index >= 0) {
-                        if (s->pixel_type != EXR_UNKNOWN &&
-                            s->pixel_type != current_pixel_type) {
-                            av_log(s->avctx, AV_LOG_ERROR,
-                                   "RGB channels not of the same depth.\n");
-                            return AVERROR_INVALIDDATA;
-                        }
-                        s->pixel_type                     = current_pixel_type;
-                        s->channel_offsets[channel_index] = s->current_channel_offset;
+                if (channel_index >= 0 && s->channel_offsets[channel_index] == -1) { /* channel has not been previously assigned */
+                    if (s->pixel_type != EXR_UNKNOWN &&
+                        s->pixel_type != current_pixel_type) {
+                        av_log(s->avctx, AV_LOG_ERROR,
+                               "RGB channels not of the same depth.\n");
+                        return AVERROR_INVALIDDATA;
                     }
+                    s->pixel_type                     = current_pixel_type;
+                    s->channel_offsets[channel_index] = s->current_channel_offset;
                 }
 
                 s->channels = av_realloc(s->channels,
@@ -1440,7 +1487,11 @@ static int decode_header(EXRContext *s)
                 channel->xsub       = xsub;
                 channel->ysub       = ysub;
 
-                s->current_channel_offset += 1 << current_pixel_type;
+                if (current_pixel_type == EXR_HALF) {
+                    s->current_channel_offset += 2;
+                } else {/* Float or UINT32 */
+                    s->current_channel_offset += 4;
+                }
             }
 
             /* Check if all channels are set with an offset or if the channels
@@ -1546,6 +1597,14 @@ static int decode_header(EXRContext *s)
                 return AVERROR_PATCHWELCOME;
             }
 
+            continue;
+        } else if ((var_size = check_header_variable(s, "writer",
+                                                     "string", 1)) >= 0) {
+            uint8_t key[256] = { 0 };
+
+            bytestream2_get_buffer(&s->gb, key, FFMIN(sizeof(key) - 1, var_size));
+            av_dict_set(&metadata, "writer", key, 0);
+
             continue;
         }
 
@@ -1582,6 +1641,8 @@ static int decode_header(EXRContext *s)
         return AVERROR_INVALIDDATA;
     }
 
+    frame->metadata = metadata;
+
     // aaand we are done
     bytestream2_skip(&s->gb, 1);
     return 0;
@@ -1597,16 +1658,20 @@ static int decode_frame(AVCodecContext *avctx, void *data,
 
     int y, ret;
     int out_line_size;
-    int nb_blocks;/* nb scanline or nb tile */
+    int nb_blocks;   /* nb scanline or nb tile */
+    uint64_t start_offset_table;
+    uint64_t start_next_scanline;
+    PutByteContext offset_table_writer;
 
     bytestream2_init(&s->gb, avpkt->data, avpkt->size);
 
-    if ((ret = decode_header(s)) < 0)
+    if ((ret = decode_header(s, picture)) < 0)
         return ret;
 
     switch (s->pixel_type) {
     case EXR_FLOAT:
     case EXR_HALF:
+    case EXR_UINT:
         if (s->channel_offsets[3] >= 0) {
             if (!s->is_luma) {
                 avctx->pix_fmt = AV_PIX_FMT_RGBA64;
@@ -1621,9 +1686,6 @@ static int decode_frame(AVCodecContext *avctx, void *data,
             }
         }
         break;
-    case EXR_UINT:
-        avpriv_request_sample(avctx, "32-bit unsigned int");
-        return AVERROR_PATCHWELCOME;
     default:
         av_log(avctx, AV_LOG_ERROR, "Missing channel list.\n");
         return AVERROR_INVALIDDATA;
@@ -1685,6 +1747,25 @@ static int decode_frame(AVCodecContext *avctx, void *data,
     if (bytestream2_get_bytes_left(&s->gb) < nb_blocks * 8)
         return AVERROR_INVALIDDATA;
 
+    // check offset table and recreate it if need
+    if (!s->is_tile && bytestream2_peek_le64(&s->gb) == 0) {
+        av_log(s->avctx, AV_LOG_DEBUG, "recreating invalid scanline offset table\n");
+
+        start_offset_table = bytestream2_tell(&s->gb);
+        start_next_scanline = start_offset_table + nb_blocks * 8;
+        bytestream2_init_writer(&offset_table_writer, &avpkt->data[start_offset_table], nb_blocks * 8);
+
+        for (y = 0; y < nb_blocks; y++) {
+            /* write offset of prev scanline in offset table */
+            bytestream2_put_le64(&offset_table_writer, start_next_scanline);
+
+            /* get len of next scanline */
+            bytestream2_seek(&s->gb, start_next_scanline + 4, SEEK_SET);/* skip line number */
+            start_next_scanline += (bytestream2_get_le32(&s->gb) + 8);
+        }
+        bytestream2_seek(&s->gb, start_offset_table, SEEK_SET);
+    }
+
     // save pointer we are going to use in decode_block
     s->buf      = avpkt->data;
     s->buf_size = avpkt->size;
@@ -1701,6 +1782,7 @@ static int decode_frame(AVCodecContext *avctx, void *data,
     avctx->execute2(avctx, decode_block, s->thread_data, NULL, nb_blocks);
 
     // Zero out the end if ymax+1 is not h
+    ptr = picture->data[0] + ((s->ymax+1) * picture->linesize[0]);
     for (y = s->ymax + 1; y < avctx->height; y++) {
         memset(ptr, 0, out_line_size);
         ptr += picture->linesize[0];
@@ -1722,6 +1804,10 @@ static av_cold int decode_init(AVCodecContext *avctx)
 
     s->avctx              = avctx;
 
+#if HAVE_BIGENDIAN
+    ff_bswapdsp_init(&s->bbdsp);
+#endif
+
     trc_func = avpriv_get_trc_function_from_trc(s->apply_trc_type);
     if (trc_func) {
         for (i = 0; i < 65536; ++i) {