LCOV - code coverage report
Current view: top level - libavcodec - exr.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 756 952 79.4 %
Date: 2017-12-16 01:21:47 Functions: 24 27 88.9 %

          Line data    Source code
       1             : /*
       2             :  * OpenEXR (.exr) image decoder
       3             :  * Copyright (c) 2006 Industrial Light & Magic, a division of Lucas Digital Ltd. LLC
       4             :  * Copyright (c) 2009 Jimmy Christensen
       5             :  *
       6             :  * B44/B44A, Tile, UINT32 added by Jokyo Images support by CNC - French National Center for Cinema
       7             :  *
       8             :  * This file is part of FFmpeg.
       9             :  *
      10             :  * FFmpeg is free software; you can redistribute it and/or
      11             :  * modify it under the terms of the GNU Lesser General Public
      12             :  * License as published by the Free Software Foundation; either
      13             :  * version 2.1 of the License, or (at your option) any later version.
      14             :  *
      15             :  * FFmpeg is distributed in the hope that it will be useful,
      16             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      17             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      18             :  * Lesser General Public License for more details.
      19             :  *
      20             :  * You should have received a copy of the GNU Lesser General Public
      21             :  * License along with FFmpeg; if not, write to the Free Software
      22             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      23             :  */
      24             : 
      25             : /**
      26             :  * @file
      27             :  * OpenEXR decoder
      28             :  * @author Jimmy Christensen
      29             :  *
      30             :  * For more information on the OpenEXR format, visit:
      31             :  *  http://openexr.com/
      32             :  *
      33             :  * exr_flt2uint() and exr_halflt2uint() is credited to Reimar Döffinger.
      34             :  * exr_half2float() is credited to Aaftab Munshi, Dan Ginsburg, Dave Shreiner.
      35             :  */
      36             : 
      37             : #include <float.h>
      38             : #include <zlib.h>
      39             : 
      40             : #include "libavutil/avassert.h"
      41             : #include "libavutil/common.h"
      42             : #include "libavutil/imgutils.h"
      43             : #include "libavutil/intfloat.h"
      44             : #include "libavutil/opt.h"
      45             : #include "libavutil/color_utils.h"
      46             : 
      47             : #include "avcodec.h"
      48             : #include "bytestream.h"
      49             : 
      50             : #if HAVE_BIGENDIAN
      51             : #include "bswapdsp.h"
      52             : #endif
      53             : 
      54             : #include "exrdsp.h"
      55             : #include "get_bits.h"
      56             : #include "internal.h"
      57             : #include "mathops.h"
      58             : #include "thread.h"
      59             : 
      60             : enum ExrCompr {
      61             :     EXR_RAW,
      62             :     EXR_RLE,
      63             :     EXR_ZIP1,
      64             :     EXR_ZIP16,
      65             :     EXR_PIZ,
      66             :     EXR_PXR24,
      67             :     EXR_B44,
      68             :     EXR_B44A,
      69             :     EXR_DWA,
      70             :     EXR_DWB,
      71             :     EXR_UNKN,
      72             : };
      73             : 
      74             : enum ExrPixelType {
      75             :     EXR_UINT,
      76             :     EXR_HALF,
      77             :     EXR_FLOAT,
      78             :     EXR_UNKNOWN,
      79             : };
      80             : 
      81             : enum ExrTileLevelMode {
      82             :     EXR_TILE_LEVEL_ONE,
      83             :     EXR_TILE_LEVEL_MIPMAP,
      84             :     EXR_TILE_LEVEL_RIPMAP,
      85             :     EXR_TILE_LEVEL_UNKNOWN,
      86             : };
      87             : 
      88             : enum ExrTileLevelRound {
      89             :     EXR_TILE_ROUND_UP,
      90             :     EXR_TILE_ROUND_DOWN,
      91             :     EXR_TILE_ROUND_UNKNOWN,
      92             : };
      93             : 
      94             : typedef struct EXRChannel {
      95             :     int xsub, ysub;
      96             :     enum ExrPixelType pixel_type;
      97             : } EXRChannel;
      98             : 
      99             : typedef struct EXRTileAttribute {
     100             :     int32_t xSize;
     101             :     int32_t ySize;
     102             :     enum ExrTileLevelMode level_mode;
     103             :     enum ExrTileLevelRound level_round;
     104             : } EXRTileAttribute;
     105             : 
     106             : typedef struct EXRThreadData {
     107             :     uint8_t *uncompressed_data;
     108             :     int uncompressed_size;
     109             : 
     110             :     uint8_t *tmp;
     111             :     int tmp_size;
     112             : 
     113             :     uint8_t *bitmap;
     114             :     uint16_t *lut;
     115             : 
     116             :     int ysize, xsize;
     117             : 
     118             :     int channel_line_size;
     119             : } EXRThreadData;
     120             : 
     121             : typedef struct EXRContext {
     122             :     AVClass *class;
     123             :     AVFrame *picture;
     124             :     AVCodecContext *avctx;
     125             :     ExrDSPContext dsp;
     126             : 
     127             : #if HAVE_BIGENDIAN
     128             :     BswapDSPContext bbdsp;
     129             : #endif
     130             : 
     131             :     enum ExrCompr compression;
     132             :     enum ExrPixelType pixel_type;
     133             :     int channel_offsets[4]; // 0 = red, 1 = green, 2 = blue and 3 = alpha
     134             :     const AVPixFmtDescriptor *desc;
     135             : 
     136             :     int w, h;
     137             :     uint32_t xmax, xmin;
     138             :     uint32_t ymax, ymin;
     139             :     uint32_t xdelta, ydelta;
     140             : 
     141             :     int scan_lines_per_block;
     142             : 
     143             :     EXRTileAttribute tile_attr; /* header data attribute of tile */
     144             :     int is_tile; /* 0 if scanline, 1 if tile */
     145             : 
     146             :     int is_luma;/* 1 if there is an Y plane */
     147             : 
     148             :     GetByteContext gb;
     149             :     const uint8_t *buf;
     150             :     int buf_size;
     151             : 
     152             :     EXRChannel *channels;
     153             :     int nb_channels;
     154             :     int current_channel_offset;
     155             : 
     156             :     EXRThreadData *thread_data;
     157             : 
     158             :     const char *layer;
     159             : 
     160             :     enum AVColorTransferCharacteristic apply_trc_type;
     161             :     float gamma;
     162             :     uint16_t gamma_table[65536];
     163             : } EXRContext;
     164             : 
     165             : /* -15 stored using a single precision bias of 127 */
     166             : #define HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP 0x38000000
     167             : 
     168             : /* max exponent value in single precision that will be converted
     169             :  * to Inf or Nan when stored as a half-float */
     170             : #define HALF_FLOAT_MAX_BIASED_EXP_AS_SINGLE_FP_EXP 0x47800000
     171             : 
     172             : /* 255 is the max exponent biased value */
     173             : #define FLOAT_MAX_BIASED_EXP (0xFF << 23)
     174             : 
     175             : #define HALF_FLOAT_MAX_BIASED_EXP (0x1F << 10)
     176             : 
     177             : /**
     178             :  * Convert a half float as a uint16_t into a full float.
     179             :  *
     180             :  * @param hf half float as uint16_t
     181             :  *
     182             :  * @return float value
     183             :  */
     184           0 : static union av_intfloat32 exr_half2float(uint16_t hf)
     185             : {
     186           0 :     unsigned int sign = (unsigned int) (hf >> 15);
     187           0 :     unsigned int mantissa = (unsigned int) (hf & ((1 << 10) - 1));
     188           0 :     unsigned int exp = (unsigned int) (hf & HALF_FLOAT_MAX_BIASED_EXP);
     189             :     union av_intfloat32 f;
     190             : 
     191           0 :     if (exp == HALF_FLOAT_MAX_BIASED_EXP) {
     192             :         // we have a half-float NaN or Inf
     193             :         // half-float NaNs will be converted to a single precision NaN
     194             :         // half-float Infs will be converted to a single precision Inf
     195           0 :         exp = FLOAT_MAX_BIASED_EXP;
     196           0 :         if (mantissa)
     197           0 :             mantissa = (1 << 23) - 1;    // set all bits to indicate a NaN
     198           0 :     } else if (exp == 0x0) {
     199             :         // convert half-float zero/denorm to single precision value
     200           0 :         if (mantissa) {
     201           0 :             mantissa <<= 1;
     202           0 :             exp = HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP;
     203             :             // check for leading 1 in denorm mantissa
     204           0 :             while ((mantissa & (1 << 10))) {
     205             :                 // for every leading 0, decrement single precision exponent by 1
     206             :                 // and shift half-float mantissa value to the left
     207           0 :                 mantissa <<= 1;
     208           0 :                 exp -= (1 << 23);
     209             :             }
     210             :             // clamp the mantissa to 10 bits
     211           0 :             mantissa &= ((1 << 10) - 1);
     212             :             // shift left to generate single-precision mantissa of 23 bits
     213           0 :             mantissa <<= 13;
     214             :         }
     215             :     } else {
     216             :         // shift left to generate single-precision mantissa of 23 bits
     217           0 :         mantissa <<= 13;
     218             :         // generate single precision biased exponent value
     219           0 :         exp = (exp << 13) + HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP;
     220             :     }
     221             : 
     222           0 :     f.i = (sign << 31) | exp | mantissa;
     223             : 
     224           0 :     return f;
     225             : }
     226             : 
     227             : 
     228             : /**
     229             :  * Convert from 32-bit float as uint32_t to uint16_t.
     230             :  *
     231             :  * @param v 32-bit float
     232             :  *
     233             :  * @return normalized 16-bit unsigned int
     234             :  */
     235      141816 : static inline uint16_t exr_flt2uint(int32_t v)
     236             : {
     237      141816 :     int32_t exp = v >> 23;
     238             :     // "HACK": negative values result in exp<  0, so clipping them to 0
     239             :     // is also handled by this condition, avoids explicit check for sign bit.
     240      141816 :     if (exp <= 127 + 7 - 24) // we would shift out all bits anyway
     241        4830 :         return 0;
     242      136986 :     if (exp >= 127)
     243        3156 :         return 0xffff;
     244      133830 :     v &= 0x007fffff;
     245      133830 :     return (v + (1 << 23)) >> (127 + 7 - exp);
     246             : }
     247             : 
     248             : /**
     249             :  * Convert from 16-bit float as uint16_t to uint16_t.
     250             :  *
     251             :  * @param v 16-bit float
     252             :  *
     253             :  * @return normalized 16-bit unsigned int
     254             :  */
     255    11766870 : static inline uint16_t exr_halflt2uint(uint16_t v)
     256             : {
     257    11766870 :     unsigned exp = 14 - (v >> 10);
     258    11766870 :     if (exp >= 14) {
     259     9980246 :         if (exp == 14)
     260      414716 :             return (v >> 9) & 1;
     261             :         else
     262     9565530 :             return (v & 0x8000) ? 0 : 0xffff;
     263             :     }
     264     1786624 :     v <<= 6;
     265     1786624 :     return (v + (1 << 16)) >> (exp + 1);
     266             : }
     267             : 
     268        1500 : static int zip_uncompress(EXRContext *s, const uint8_t *src, int compressed_size,
     269             :                           int uncompressed_size, EXRThreadData *td)
     270             : {
     271        1500 :     unsigned long dest_len = uncompressed_size;
     272             : 
     273        3000 :     if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
     274        1500 :         dest_len != uncompressed_size)
     275           0 :         return AVERROR_INVALIDDATA;
     276             : 
     277             :     av_assert1(uncompressed_size % 2 == 0);
     278             : 
     279        1500 :     s->dsp.predictor(td->tmp, uncompressed_size);
     280        1500 :     s->dsp.reorder_pixels(td->uncompressed_data, td->tmp, uncompressed_size);
     281             : 
     282        1500 :     return 0;
     283             : }
     284             : 
     285        1386 : static int rle_uncompress(EXRContext *ctx, const uint8_t *src, int compressed_size,
     286             :                           int uncompressed_size, EXRThreadData *td)
     287             : {
     288        1386 :     uint8_t *d      = td->tmp;
     289        1386 :     const int8_t *s = src;
     290        1386 :     int ssize       = compressed_size;
     291        1386 :     int dsize       = uncompressed_size;
     292        1386 :     uint8_t *dend   = d + dsize;
     293             :     int count;
     294             : 
     295      379514 :     while (ssize > 0) {
     296      376742 :         count = *s++;
     297             : 
     298      376742 :         if (count < 0) {
     299      186494 :             count = -count;
     300             : 
     301      372988 :             if ((dsize -= count) < 0 ||
     302      186494 :                 (ssize -= count + 1) < 0)
     303           0 :                 return AVERROR_INVALIDDATA;
     304             : 
     305     3649504 :             while (count--)
     306     3276516 :                 *d++ = *s++;
     307             :         } else {
     308      190248 :             count++;
     309             : 
     310      380496 :             if ((dsize -= count) < 0 ||
     311      190248 :                 (ssize -= 2) < 0)
     312           0 :                 return AVERROR_INVALIDDATA;
     313             : 
     314     3460476 :             while (count--)
     315     3079980 :                 *d++ = *s;
     316             : 
     317      190248 :             s++;
     318             :         }
     319             :     }
     320             : 
     321        1386 :     if (dend != d)
     322           0 :         return AVERROR_INVALIDDATA;
     323             : 
     324             :     av_assert1(uncompressed_size % 2 == 0);
     325             : 
     326        1386 :     ctx->dsp.predictor(td->tmp, uncompressed_size);
     327        1386 :     ctx->dsp.reorder_pixels(td->uncompressed_data, td->tmp, uncompressed_size);
     328             : 
     329        1386 :     return 0;
     330             : }
     331             : 
     332             : #define USHORT_RANGE (1 << 16)
     333             : #define BITMAP_SIZE  (1 << 13)
     334             : 
     335          48 : static uint16_t reverse_lut(const uint8_t *bitmap, uint16_t *lut)
     336             : {
     337          48 :     int i, k = 0;
     338             : 
     339     3145776 :     for (i = 0; i < USHORT_RANGE; i++)
     340     3145728 :         if ((i == 0) || (bitmap[i >> 3] & (1 << (i & 7))))
     341        5798 :             lut[k++] = i;
     342             : 
     343          48 :     i = k - 1;
     344             : 
     345          48 :     memset(lut + k, 0, (USHORT_RANGE - k) * 2);
     346             : 
     347          48 :     return i;
     348             : }
     349             : 
     350          48 : static void apply_lut(const uint16_t *lut, uint16_t *dst, int dsize)
     351             : {
     352             :     int i;
     353             : 
     354     1472148 :     for (i = 0; i < dsize; ++i)
     355     1472100 :         dst[i] = lut[dst[i]];
     356          48 : }
     357             : 
     358             : #define HUF_ENCBITS 16  // literal (value) bit length
     359             : #define HUF_DECBITS 14  // decoding bit size (>= 8)
     360             : 
     361             : #define HUF_ENCSIZE ((1 << HUF_ENCBITS) + 1)  // encoding table size
     362             : #define HUF_DECSIZE (1 << HUF_DECBITS)        // decoding table size
     363             : #define HUF_DECMASK (HUF_DECSIZE - 1)
     364             : 
     365             : typedef struct HufDec {
     366             :     int len;
     367             :     int lit;
     368             :     int *p;
     369             : } HufDec;
     370             : 
     371          48 : static void huf_canonical_code_table(uint64_t *hcode)
     372             : {
     373          48 :     uint64_t c, n[59] = { 0 };
     374             :     int i;
     375             : 
     376     3145824 :     for (i = 0; i < HUF_ENCSIZE; ++i)
     377     3145776 :         n[hcode[i]] += 1;
     378             : 
     379          48 :     c = 0;
     380        2832 :     for (i = 58; i > 0; --i) {
     381        2784 :         uint64_t nc = ((c + n[i]) >> 1);
     382        2784 :         n[i] = c;
     383        2784 :         c    = nc;
     384             :     }
     385             : 
     386     3145824 :     for (i = 0; i < HUF_ENCSIZE; ++i) {
     387     3145776 :         int l = hcode[i];
     388             : 
     389     3145776 :         if (l > 0)
     390        3404 :             hcode[i] = l | (n[l]++ << 6);
     391             :     }
     392          48 : }
     393             : 
     394             : #define SHORT_ZEROCODE_RUN  59
     395             : #define LONG_ZEROCODE_RUN   63
     396             : #define SHORTEST_LONG_RUN   (2 + LONG_ZEROCODE_RUN - SHORT_ZEROCODE_RUN)
     397             : #define LONGEST_LONG_RUN    (255 + SHORTEST_LONG_RUN)
     398             : 
     399          48 : static int huf_unpack_enc_table(GetByteContext *gb,
     400             :                                 int32_t im, int32_t iM, uint64_t *hcode)
     401             : {
     402             :     GetBitContext gbit;
     403          48 :     int ret = init_get_bits8(&gbit, gb->buffer, bytestream2_get_bytes_left(gb));
     404          48 :     if (ret < 0)
     405           0 :         return ret;
     406             : 
     407       16678 :     for (; im <= iM; im++) {
     408       16630 :         uint64_t l = hcode[im] = get_bits(&gbit, 6);
     409             : 
     410       16630 :         if (l == LONG_ZEROCODE_RUN) {
     411       12358 :             int zerun = get_bits(&gbit, 8) + SHORTEST_LONG_RUN;
     412             : 
     413       12358 :             if (im + zerun > iM + 1)
     414           0 :                 return AVERROR_INVALIDDATA;
     415             : 
     416     3165278 :             while (zerun--)
     417     3140562 :                 hcode[im++] = 0;
     418             : 
     419       12358 :             im--;
     420        4272 :         } else if (l >= SHORT_ZEROCODE_RUN) {
     421         458 :             int zerun = l - SHORT_ZEROCODE_RUN + 2;
     422             : 
     423         458 :             if (im + zerun > iM + 1)
     424           0 :                 return AVERROR_INVALIDDATA;
     425             : 
     426        2316 :             while (zerun--)
     427        1400 :                 hcode[im++] = 0;
     428             : 
     429         458 :             im--;
     430             :         }
     431             :     }
     432             : 
     433          48 :     bytestream2_skip(gb, (get_bits_count(&gbit) + 7) / 8);
     434          48 :     huf_canonical_code_table(hcode);
     435             : 
     436          48 :     return 0;
     437             : }
     438             : 
     439          48 : static int huf_build_dec_table(const uint64_t *hcode, int im,
     440             :                                int iM, HufDec *hdecod)
     441             : {
     442     3145824 :     for (; im <= iM; im++) {
     443     3145776 :         uint64_t c = hcode[im] >> 6;
     444     3145776 :         int i, l = hcode[im] & 63;
     445             : 
     446     3145776 :         if (c >> l)
     447           0 :             return AVERROR_INVALIDDATA;
     448             : 
     449     3145776 :         if (l > HUF_DECBITS) {
     450           0 :             HufDec *pl = hdecod + (c >> (l - HUF_DECBITS));
     451           0 :             if (pl->len)
     452           0 :                 return AVERROR_INVALIDDATA;
     453             : 
     454           0 :             pl->lit++;
     455             : 
     456           0 :             pl->p = av_realloc(pl->p, pl->lit * sizeof(int));
     457           0 :             if (!pl->p)
     458           0 :                 return AVERROR(ENOMEM);
     459             : 
     460           0 :             pl->p[pl->lit - 1] = im;
     461     3145776 :         } else if (l) {
     462        3404 :             HufDec *pl = hdecod + (c << (HUF_DECBITS - l));
     463             : 
     464      789836 :             for (i = 1 << (HUF_DECBITS - l); i > 0; i--, pl++) {
     465      786432 :                 if (pl->len || pl->p)
     466           0 :                     return AVERROR_INVALIDDATA;
     467      786432 :                 pl->len = l;
     468      786432 :                 pl->lit = im;
     469             :             }
     470             :         }
     471             :     }
     472             : 
     473          48 :     return 0;
     474             : }
     475             : 
     476             : #define get_char(c, lc, gb)                                                   \
     477             : {                                                                             \
     478             :         c   = (c << 8) | bytestream2_get_byte(gb);                            \
     479             :         lc += 8;                                                              \
     480             : }
     481             : 
     482             : #define get_code(po, rlc, c, lc, gb, out, oe, outb)                           \
     483             : {                                                                             \
     484             :         if (po == rlc) {                                                      \
     485             :             if (lc < 8)                                                       \
     486             :                 get_char(c, lc, gb);                                          \
     487             :             lc -= 8;                                                          \
     488             :                                                                               \
     489             :             cs = c >> lc;                                                     \
     490             :                                                                               \
     491             :             if (out + cs > oe || out == outb)                                 \
     492             :                 return AVERROR_INVALIDDATA;                                   \
     493             :                                                                               \
     494             :             s = out[-1];                                                      \
     495             :                                                                               \
     496             :             while (cs-- > 0)                                                  \
     497             :                 *out++ = s;                                                   \
     498             :         } else if (out < oe) {                                                \
     499             :             *out++ = po;                                                      \
     500             :         } else {                                                              \
     501             :             return AVERROR_INVALIDDATA;                                       \
     502             :         }                                                                     \
     503             : }
     504             : 
     505          48 : static int huf_decode(const uint64_t *hcode, const HufDec *hdecod,
     506             :                       GetByteContext *gb, int nbits,
     507             :                       int rlc, int no, uint16_t *out)
     508             : {
     509          48 :     uint64_t c        = 0;
     510          48 :     uint16_t *outb    = out;
     511          48 :     uint16_t *oe      = out + no;
     512          48 :     const uint8_t *ie = gb->buffer + (nbits + 7) / 8; // input byte size
     513             :     uint8_t cs;
     514             :     uint16_t s;
     515          48 :     int i, lc = 0;
     516             : 
     517       76952 :     while (gb->buffer < ie) {
     518       76856 :         get_char(c, lc, gb);
     519             : 
     520      462012 :         while (lc >= HUF_DECBITS) {
     521      308300 :             const HufDec pl = hdecod[(c >> (lc - HUF_DECBITS)) & HUF_DECMASK];
     522             : 
     523      308300 :             if (pl.len) {
     524      308300 :                 lc -= pl.len;
     525      308300 :                 get_code(pl.lit, rlc, c, lc, gb, out, oe, outb);
     526             :             } else {
     527             :                 int j;
     528             : 
     529           0 :                 if (!pl.p)
     530           0 :                     return AVERROR_INVALIDDATA;
     531             : 
     532           0 :                 for (j = 0; j < pl.lit; j++) {
     533           0 :                     int l = hcode[pl.p[j]] & 63;
     534             : 
     535           0 :                     while (lc < l && bytestream2_get_bytes_left(gb) > 0)
     536           0 :                         get_char(c, lc, gb);
     537             : 
     538           0 :                     if (lc >= l) {
     539           0 :                         if ((hcode[pl.p[j]] >> 6) ==
     540           0 :                             ((c >> (lc - l)) & ((1LL << l) - 1))) {
     541           0 :                             lc -= l;
     542           0 :                             get_code(pl.p[j], rlc, c, lc, gb, out, oe, outb);
     543           0 :                             break;
     544             :                         }
     545             :                     }
     546             :                 }
     547             : 
     548           0 :                 if (j == pl.lit)
     549           0 :                     return AVERROR_INVALIDDATA;
     550             :             }
     551             :         }
     552             :     }
     553             : 
     554          48 :     i   = (8 - nbits) & 7;
     555          48 :     c >>= i;
     556          48 :     lc -= i;
     557             : 
     558         172 :     while (lc > 0) {
     559          76 :         const HufDec pl = hdecod[(c << (HUF_DECBITS - lc)) & HUF_DECMASK];
     560             : 
     561          76 :         if (pl.len) {
     562          76 :             lc -= pl.len;
     563          76 :             get_code(pl.lit, rlc, c, lc, gb, out, oe, outb);
     564             :         } else {
     565           0 :             return AVERROR_INVALIDDATA;
     566             :         }
     567             :     }
     568             : 
     569          48 :     if (out - outb != no)
     570           0 :         return AVERROR_INVALIDDATA;
     571          48 :     return 0;
     572             : }
     573             : 
     574          48 : static int huf_uncompress(GetByteContext *gb,
     575             :                           uint16_t *dst, int dst_size)
     576             : {
     577             :     int32_t src_size, im, iM;
     578             :     uint32_t nBits;
     579             :     uint64_t *freq;
     580             :     HufDec *hdec;
     581             :     int ret, i;
     582             : 
     583          48 :     src_size = bytestream2_get_le32(gb);
     584          48 :     im       = bytestream2_get_le32(gb);
     585          48 :     iM       = bytestream2_get_le32(gb);
     586          48 :     bytestream2_skip(gb, 4);
     587          48 :     nBits = bytestream2_get_le32(gb);
     588          48 :     if (im < 0 || im >= HUF_ENCSIZE ||
     589          48 :         iM < 0 || iM >= HUF_ENCSIZE ||
     590             :         src_size < 0)
     591           0 :         return AVERROR_INVALIDDATA;
     592             : 
     593          48 :     bytestream2_skip(gb, 4);
     594             : 
     595          48 :     freq = av_mallocz_array(HUF_ENCSIZE, sizeof(*freq));
     596          48 :     hdec = av_mallocz_array(HUF_DECSIZE, sizeof(*hdec));
     597          48 :     if (!freq || !hdec) {
     598           0 :         ret = AVERROR(ENOMEM);
     599           0 :         goto fail;
     600             :     }
     601             : 
     602          48 :     if ((ret = huf_unpack_enc_table(gb, im, iM, freq)) < 0)
     603           0 :         goto fail;
     604             : 
     605          48 :     if (nBits > 8 * bytestream2_get_bytes_left(gb)) {
     606           0 :         ret = AVERROR_INVALIDDATA;
     607           0 :         goto fail;
     608             :     }
     609             : 
     610          48 :     if ((ret = huf_build_dec_table(freq, im, iM, hdec)) < 0)
     611           0 :         goto fail;
     612          48 :     ret = huf_decode(freq, hdec, gb, nBits, iM, dst_size, dst);
     613             : 
     614          48 : fail:
     615      786480 :     for (i = 0; i < HUF_DECSIZE; i++)
     616      786432 :         if (hdec)
     617      786432 :             av_freep(&hdec[i].p);
     618             : 
     619          48 :     av_free(freq);
     620          48 :     av_free(hdec);
     621             : 
     622          48 :     return ret;
     623             : }
     624             : 
     625     1958562 : static inline void wdec14(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
     626             : {
     627     1958562 :     int16_t ls = l;
     628     1958562 :     int16_t hs = h;
     629     1958562 :     int hi     = hs;
     630     1958562 :     int ai     = ls + (hi & 1) + (hi >> 1);
     631     1958562 :     int16_t as = ai;
     632     1958562 :     int16_t bs = ai - hi;
     633             : 
     634     1958562 :     *a = as;
     635     1958562 :     *b = bs;
     636     1958562 : }
     637             : 
     638             : #define NBITS      16
     639             : #define A_OFFSET  (1 << (NBITS - 1))
     640             : #define MOD_MASK  ((1 << NBITS) - 1)
     641             : 
     642           0 : static inline void wdec16(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
     643             : {
     644           0 :     int m  = l;
     645           0 :     int d  = h;
     646           0 :     int bb = (m - (d >> 1)) & MOD_MASK;
     647           0 :     int aa = (d + bb - A_OFFSET) & MOD_MASK;
     648           0 :     *b = bb;
     649           0 :     *a = aa;
     650           0 : }
     651             : 
     652         150 : static void wav_decode(uint16_t *in, int nx, int ox,
     653             :                        int ny, int oy, uint16_t mx)
     654             : {
     655         150 :     int w14 = (mx < (1 << 14));
     656         150 :     int n   = (nx > ny) ? ny : nx;
     657         150 :     int p   = 1;
     658             :     int p2;
     659             : 
     660        1158 :     while (p <= n)
     661         858 :         p <<= 1;
     662             : 
     663         150 :     p >>= 1;
     664         150 :     p2  = p;
     665         150 :     p >>= 1;
     666             : 
     667        1008 :     while (p >= 1) {
     668         708 :         uint16_t *py = in;
     669         708 :         uint16_t *ey = in + oy * (ny - p2);
     670             :         uint16_t i00, i01, i10, i11;
     671         708 :         int oy1 = oy * p;
     672         708 :         int oy2 = oy * p2;
     673         708 :         int ox1 = ox * p;
     674         708 :         int ox2 = ox * p2;
     675             : 
     676        4914 :         for (; py <= ey; py += oy2) {
     677        4206 :             uint16_t *px = py;
     678        4206 :             uint16_t *ex = py + ox * (nx - p2);
     679             : 
     680      493602 :             for (; px <= ex; px += ox2) {
     681      489396 :                 uint16_t *p01 = px + ox1;
     682      489396 :                 uint16_t *p10 = px + oy1;
     683      489396 :                 uint16_t *p11 = p10 + ox1;
     684             : 
     685      489396 :                 if (w14) {
     686      489396 :                     wdec14(*px, *p10, &i00, &i10);
     687      489396 :                     wdec14(*p01, *p11, &i01, &i11);
     688      489396 :                     wdec14(i00, i01, px, p01);
     689      489396 :                     wdec14(i10, i11, p10, p11);
     690             :                 } else {
     691           0 :                     wdec16(*px, *p10, &i00, &i10);
     692           0 :                     wdec16(*p01, *p11, &i01, &i11);
     693           0 :                     wdec16(i00, i01, px, p01);
     694           0 :                     wdec16(i10, i11, p10, p11);
     695             :                 }
     696             :             }
     697             : 
     698        4206 :             if (nx & p) {
     699         216 :                 uint16_t *p10 = px + oy1;
     700             : 
     701         216 :                 if (w14)
     702         216 :                     wdec14(*px, *p10, &i00, p10);
     703             :                 else
     704           0 :                     wdec16(*px, *p10, &i00, p10);
     705             : 
     706         216 :                 *px = i00;
     707             :             }
     708             :         }
     709             : 
     710         708 :         if (ny & p) {
     711          30 :             uint16_t *px = py;
     712          30 :             uint16_t *ex = py + ox * (nx - p2);
     713             : 
     714         792 :             for (; px <= ex; px += ox2) {
     715         762 :                 uint16_t *p01 = px + ox1;
     716             : 
     717         762 :                 if (w14)
     718         762 :                     wdec14(*px, *p01, &i00, p01);
     719             :                 else
     720           0 :                     wdec16(*px, *p01, &i00, p01);
     721             : 
     722         762 :                 *px = i00;
     723             :             }
     724             :         }
     725             : 
     726         708 :         p2  = p;
     727         708 :         p >>= 1;
     728             :     }
     729         150 : }
     730             : 
     731          48 : static int piz_uncompress(EXRContext *s, const uint8_t *src, int ssize,
     732             :                           int dsize, EXRThreadData *td)
     733             : {
     734             :     GetByteContext gb;
     735             :     uint16_t maxval, min_non_zero, max_non_zero;
     736             :     uint16_t *ptr;
     737          48 :     uint16_t *tmp = (uint16_t *)td->tmp;
     738             :     uint16_t *out;
     739             :     uint16_t *in;
     740             :     int ret, i, j;
     741             :     int pixel_half_size;/* 1 for half, 2 for float and uint32 */
     742             :     EXRChannel *channel;
     743             :     int tmp_offset;
     744             : 
     745          48 :     if (!td->bitmap)
     746          10 :         td->bitmap = av_malloc(BITMAP_SIZE);
     747          48 :     if (!td->lut)
     748          10 :         td->lut = av_malloc(1 << 17);
     749          48 :     if (!td->bitmap || !td->lut) {
     750           0 :         av_freep(&td->bitmap);
     751           0 :         av_freep(&td->lut);
     752           0 :         return AVERROR(ENOMEM);
     753             :     }
     754             : 
     755          48 :     bytestream2_init(&gb, src, ssize);
     756          48 :     min_non_zero = bytestream2_get_le16(&gb);
     757          48 :     max_non_zero = bytestream2_get_le16(&gb);
     758             : 
     759          48 :     if (max_non_zero >= BITMAP_SIZE)
     760           0 :         return AVERROR_INVALIDDATA;
     761             : 
     762          48 :     memset(td->bitmap, 0, FFMIN(min_non_zero, BITMAP_SIZE));
     763          48 :     if (min_non_zero <= max_non_zero)
     764          48 :         bytestream2_get_buffer(&gb, td->bitmap + min_non_zero,
     765          48 :                                max_non_zero - min_non_zero + 1);
     766          48 :     memset(td->bitmap + max_non_zero + 1, 0, BITMAP_SIZE - max_non_zero - 1);
     767             : 
     768          48 :     maxval = reverse_lut(td->bitmap, td->lut);
     769             : 
     770          48 :     ret = huf_uncompress(&gb, tmp, dsize / sizeof(uint16_t));
     771          48 :     if (ret)
     772           0 :         return ret;
     773             : 
     774          48 :     ptr = tmp;
     775         192 :     for (i = 0; i < s->nb_channels; i++) {
     776         144 :         channel = &s->channels[i];
     777             : 
     778         144 :         if (channel->pixel_type == EXR_HALF)
     779         138 :             pixel_half_size = 1;
     780             :         else
     781           6 :             pixel_half_size = 2;
     782             : 
     783         294 :         for (j = 0; j < pixel_half_size; j++)
     784         300 :             wav_decode(ptr + j, td->xsize, pixel_half_size, td->ysize,
     785         150 :                        td->xsize * pixel_half_size, maxval);
     786         144 :         ptr += td->xsize * td->ysize * pixel_half_size;
     787             :     }
     788             : 
     789          48 :     apply_lut(td->lut, tmp, dsize / sizeof(uint16_t));
     790             : 
     791          48 :     out = (uint16_t *)td->uncompressed_data;
     792        1446 :     for (i = 0; i < td->ysize; i++) {
     793        1398 :         tmp_offset = 0;
     794        5592 :         for (j = 0; j < s->nb_channels; j++) {
     795        4194 :             channel = &s->channels[j];
     796        4194 :             if (channel->pixel_type == EXR_HALF)
     797        4002 :                 pixel_half_size = 1;
     798             :             else
     799         192 :                 pixel_half_size = 2;
     800             : 
     801        4194 :             in = tmp + tmp_offset * td->xsize * td->ysize + i * td->xsize * pixel_half_size;
     802        4194 :             tmp_offset += pixel_half_size;
     803             : 
     804             : #if HAVE_BIGENDIAN
     805             :             s->bbdsp.bswap16_buf(out, in, td->xsize * pixel_half_size);
     806             : #else
     807        4194 :             memcpy(out, in, td->xsize * 2 * pixel_half_size);
     808             : #endif
     809        4194 :             out += td->xsize * pixel_half_size;
     810             :         }
     811             :     }
     812             : 
     813          48 :     return 0;
     814             : }
     815             : 
     816         122 : static int pxr24_uncompress(EXRContext *s, const uint8_t *src,
     817             :                             int compressed_size, int uncompressed_size,
     818             :                             EXRThreadData *td)
     819             : {
     820         122 :     unsigned long dest_len, expected_len = 0;
     821         122 :     const uint8_t *in = td->tmp;
     822             :     uint8_t *out;
     823             :     int c, i, j;
     824             : 
     825         558 :     for (i = 0; i < s->nb_channels; i++) {
     826         436 :         if (s->channels[i].pixel_type == EXR_FLOAT) {
     827          62 :             expected_len += (td->xsize * td->ysize * 3);/* PRX 24 store float in 24 bit instead of 32 */
     828         374 :         } else if (s->channels[i].pixel_type == EXR_HALF) {
     829         362 :             expected_len += (td->xsize * td->ysize * 2);
     830             :         } else {//UINT 32
     831          12 :             expected_len += (td->xsize * td->ysize * 4);
     832             :         }
     833             :     }
     834             : 
     835         122 :     dest_len = expected_len;
     836             : 
     837         122 :     if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK) {
     838           0 :         return AVERROR_INVALIDDATA;
     839         122 :     } else if (dest_len != expected_len) {
     840           0 :         return AVERROR_INVALIDDATA;
     841             :     }
     842             : 
     843         122 :     out = td->uncompressed_data;
     844        1900 :     for (i = 0; i < td->ysize; i++)
     845        7678 :         for (c = 0; c < s->nb_channels; c++) {
     846        5900 :             EXRChannel *channel = &s->channels[c];
     847             :             const uint8_t *ptr[4];
     848        5900 :             uint32_t pixel = 0;
     849             : 
     850        5900 :             switch (channel->pixel_type) {
     851         496 :             case EXR_FLOAT:
     852         496 :                 ptr[0] = in;
     853         496 :                 ptr[1] = ptr[0] + td->xsize;
     854         496 :                 ptr[2] = ptr[1] + td->xsize;
     855         496 :                 in     = ptr[2] + td->xsize;
     856             : 
     857        6448 :                 for (j = 0; j < td->xsize; ++j) {
     858       17856 :                     uint32_t diff = ((unsigned)*(ptr[0]++) << 24) |
     859        5952 :                                     (*(ptr[1]++) << 16) |
     860        5952 :                                     (*(ptr[2]++) << 8);
     861        5952 :                     pixel += diff;
     862        5952 :                     bytestream_put_le32(&out, pixel);
     863             :                 }
     864         496 :                 break;
     865        5302 :             case EXR_HALF:
     866        5302 :                 ptr[0] = in;
     867        5302 :                 ptr[1] = ptr[0] + td->xsize;
     868        5302 :                 in     = ptr[1] + td->xsize;
     869     3851380 :                 for (j = 0; j < td->xsize; j++) {
     870     3846078 :                     uint32_t diff = (*(ptr[0]++) << 8) | *(ptr[1]++);
     871             : 
     872     3846078 :                     pixel += diff;
     873     3846078 :                     bytestream_put_le16(&out, pixel);
     874             :                 }
     875        5302 :                 break;
     876         102 :             case EXR_UINT:
     877         102 :                 ptr[0] = in;
     878         102 :                 ptr[1] = ptr[0] + s->xdelta;
     879         102 :                 ptr[2] = ptr[1] + s->xdelta;
     880         102 :                 ptr[3] = ptr[2] + s->xdelta;
     881         102 :                 in     = ptr[3] + s->xdelta;
     882             : 
     883        1380 :                 for (j = 0; j < s->xdelta; ++j) {
     884        3834 :                     uint32_t diff = (*(ptr[0]++) << 24) |
     885        2556 :                     (*(ptr[1]++) << 16) |
     886        2556 :                     (*(ptr[2]++) << 8 ) |
     887        1278 :                     (*(ptr[3]++));
     888        1278 :                     pixel += diff;
     889        1278 :                     bytestream_put_le32(&out, pixel);
     890             :                 }
     891         102 :                 break;
     892           0 :             default:
     893           0 :                 return AVERROR_INVALIDDATA;
     894             :             }
     895             :         }
     896             : 
     897         122 :     return 0;
     898             : }
     899             : 
     900      118558 : static void unpack_14(const uint8_t b[14], uint16_t s[16])
     901             : {
     902      118558 :     unsigned short shift = (b[ 2] >> 2);
     903      118558 :     unsigned short bias = (0x20 << shift);
     904             :     int i;
     905             : 
     906      118558 :     s[ 0] = (b[0] << 8) | b[1];
     907             : 
     908      118558 :     s[ 4] = s[ 0] + ((((b[ 2] << 4) | (b[ 3] >> 4)) & 0x3f) << shift) - bias;
     909      118558 :     s[ 8] = s[ 4] + ((((b[ 3] << 2) | (b[ 4] >> 6)) & 0x3f) << shift) - bias;
     910      118558 :     s[12] = s[ 8] +   ((b[ 4]                       & 0x3f) << shift) - bias;
     911             : 
     912      118558 :     s[ 1] = s[ 0] +   ((b[ 5] >> 2)                         << shift) - bias;
     913      118558 :     s[ 5] = s[ 4] + ((((b[ 5] << 4) | (b[ 6] >> 4)) & 0x3f) << shift) - bias;
     914      118558 :     s[ 9] = s[ 8] + ((((b[ 6] << 2) | (b[ 7] >> 6)) & 0x3f) << shift) - bias;
     915      118558 :     s[13] = s[12] +   ((b[ 7]                       & 0x3f) << shift) - bias;
     916             : 
     917      118558 :     s[ 2] = s[ 1] +   ((b[ 8] >> 2)                         << shift) - bias;
     918      118558 :     s[ 6] = s[ 5] + ((((b[ 8] << 4) | (b[ 9] >> 4)) & 0x3f) << shift) - bias;
     919      118558 :     s[10] = s[ 9] + ((((b[ 9] << 2) | (b[10] >> 6)) & 0x3f) << shift) - bias;
     920      118558 :     s[14] = s[13] +   ((b[10]                       & 0x3f) << shift) - bias;
     921             : 
     922      118558 :     s[ 3] = s[ 2] +   ((b[11] >> 2)                         << shift) - bias;
     923      118558 :     s[ 7] = s[ 6] + ((((b[11] << 4) | (b[12] >> 4)) & 0x3f) << shift) - bias;
     924      118558 :     s[11] = s[10] + ((((b[12] << 2) | (b[13] >> 6)) & 0x3f) << shift) - bias;
     925      118558 :     s[15] = s[14] +   ((b[13]                       & 0x3f) << shift) - bias;
     926             : 
     927     2015486 :     for (i = 0; i < 16; ++i) {
     928     1896928 :         if (s[i] & 0x8000)
     929     1884694 :             s[i] &= 0x7fff;
     930             :         else
     931       12234 :             s[i] = ~s[i];
     932             :     }
     933      118558 : }
     934             : 
     935         212 : static void unpack_3(const uint8_t b[3], uint16_t s[16])
     936             : {
     937             :     int i;
     938             : 
     939         212 :     s[0] = (b[0] << 8) | b[1];
     940             : 
     941         212 :     if (s[0] & 0x8000)
     942         210 :         s[0] &= 0x7fff;
     943             :     else
     944           2 :         s[0] = ~s[0];
     945             : 
     946        3392 :     for (i = 1; i < 16; i++)
     947        3180 :         s[i] = s[0];
     948         212 : }
     949             : 
     950             : 
     951          46 : static int b44_uncompress(EXRContext *s, const uint8_t *src, int compressed_size,
     952             :                           int uncompressed_size, EXRThreadData *td) {
     953          46 :     const int8_t *sr = src;
     954          46 :     int stay_to_uncompress = compressed_size;
     955             :     int nb_b44_block_w, nb_b44_block_h;
     956             :     int index_tl_x, index_tl_y, index_out, index_tmp;
     957             :     uint16_t tmp_buffer[16]; /* B44 use 4x4 half float pixel */
     958             :     int c, iY, iX, y, x;
     959          46 :     int target_channel_offset = 0;
     960             : 
     961             :     /* calc B44 block count */
     962          46 :     nb_b44_block_w = td->xsize / 4;
     963          46 :     if ((td->xsize % 4) != 0)
     964          26 :         nb_b44_block_w++;
     965             : 
     966          46 :     nb_b44_block_h = td->ysize / 4;
     967          46 :     if ((td->ysize % 4) != 0)
     968          12 :         nb_b44_block_h++;
     969             : 
     970         312 :     for (c = 0; c < s->nb_channels; c++) {
     971         266 :         if (s->channels[c].pixel_type == EXR_HALF) {/* B44 only compress half float data */
     972         918 :             for (iY = 0; iY < nb_b44_block_h; iY++) {
     973      119518 :                 for (iX = 0; iX < nb_b44_block_w; iX++) {/* For each B44 block */
     974      118770 :                     if (stay_to_uncompress < 3) {
     975           0 :                         av_log(s, AV_LOG_ERROR, "Not enough data for B44A block: %d", stay_to_uncompress);
     976           0 :                         return AVERROR_INVALIDDATA;
     977             :                     }
     978             : 
     979      118770 :                     if (src[compressed_size - stay_to_uncompress + 2] == 0xfc) { /* B44A block */
     980         212 :                         unpack_3(sr, tmp_buffer);
     981         212 :                         sr += 3;
     982         212 :                         stay_to_uncompress -= 3;
     983             :                     }  else {/* B44 Block */
     984      118558 :                         if (stay_to_uncompress < 14) {
     985           0 :                             av_log(s, AV_LOG_ERROR, "Not enough data for B44 block: %d", stay_to_uncompress);
     986           0 :                             return AVERROR_INVALIDDATA;
     987             :                         }
     988      118558 :                         unpack_14(sr, tmp_buffer);
     989      118558 :                         sr += 14;
     990      118558 :                         stay_to_uncompress -= 14;
     991             :                     }
     992             : 
     993             :                     /* copy data to uncompress buffer (B44 block can exceed target resolution)*/
     994      118770 :                     index_tl_x = iX * 4;
     995      118770 :                     index_tl_y = iY * 4;
     996             : 
     997      591522 :                     for (y = index_tl_y; y < FFMIN(index_tl_y + 4, td->ysize); y++) {
     998     2360726 :                         for (x = index_tl_x; x < FFMIN(index_tl_x + 4, td->xsize); x++) {
     999     1887974 :                             index_out = target_channel_offset * td->xsize + y * td->channel_line_size + 2 * x;
    1000     1887974 :                             index_tmp = (y-index_tl_y) * 4 + (x-index_tl_x);
    1001     1887974 :                             td->uncompressed_data[index_out] = tmp_buffer[index_tmp] & 0xff;
    1002     1887974 :                             td->uncompressed_data[index_out + 1] = tmp_buffer[index_tmp] >> 8;
    1003             :                         }
    1004             :                     }
    1005             :                 }
    1006             :             }
    1007         170 :             target_channel_offset += 2;
    1008             :         } else {/* Float or UINT 32 channel */
    1009          96 :             if (stay_to_uncompress < td->ysize * td->xsize * 4) {
    1010           0 :                 av_log(s, AV_LOG_ERROR, "Not enough data for uncompress channel: %d", stay_to_uncompress);
    1011           0 :                 return AVERROR_INVALIDDATA;
    1012             :             }
    1013             : 
    1014         896 :             for (y = 0; y < td->ysize; y++) {
    1015         800 :                 index_out = target_channel_offset * td->xsize + y * td->channel_line_size;
    1016         800 :                 memcpy(&td->uncompressed_data[index_out], sr, td->xsize * 4);
    1017         800 :                 sr += td->xsize * 4;
    1018             :             }
    1019          96 :             target_channel_offset += 4;
    1020             : 
    1021          96 :             stay_to_uncompress -= td->ysize * td->xsize * 4;
    1022             :         }
    1023             :     }
    1024             : 
    1025          46 :     return 0;
    1026             : }
    1027             : 
    1028        4522 : static int decode_block(AVCodecContext *avctx, void *tdata,
    1029             :                         int jobnr, int threadnr)
    1030             : {
    1031        4522 :     EXRContext *s = avctx->priv_data;
    1032        4522 :     AVFrame *const p = s->picture;
    1033        4522 :     EXRThreadData *td = &s->thread_data[threadnr];
    1034        4522 :     const uint8_t *channel_buffer[4] = { 0 };
    1035        4522 :     const uint8_t *buf = s->buf;
    1036             :     uint64_t line_offset, uncompressed_size;
    1037             :     uint16_t *ptr_x;
    1038             :     uint8_t *ptr;
    1039             :     uint32_t data_size;
    1040        4522 :     uint64_t line, col = 0;
    1041             :     uint64_t tile_x, tile_y, tile_level_x, tile_level_y;
    1042             :     const uint8_t *src;
    1043        4522 :     int axmax = (avctx->width - (s->xmax + 1)) * 2 * s->desc->nb_components; /* nb pixel to add at the right of the datawindow */
    1044        4522 :     int bxmin = s->xmin * 2 * s->desc->nb_components; /* nb pixel to add at the left of the datawindow */
    1045        4522 :     int i, x, buf_size = s->buf_size;
    1046             :     int c, rgb_channel_count;
    1047        4522 :     float one_gamma = 1.0f / s->gamma;
    1048        4522 :     avpriv_trc_function trc_func = avpriv_get_trc_function_from_trc(s->apply_trc_type);
    1049             :     int ret;
    1050             : 
    1051        4522 :     line_offset = AV_RL64(s->gb.buffer + jobnr * 8);
    1052             : 
    1053        4522 :     if (s->is_tile) {
    1054          52 :         if (line_offset > buf_size - 20)
    1055           0 :             return AVERROR_INVALIDDATA;
    1056             : 
    1057          52 :         src  = buf + line_offset + 20;
    1058             : 
    1059          52 :         tile_x = AV_RL32(src - 20);
    1060          52 :         tile_y = AV_RL32(src - 16);
    1061          52 :         tile_level_x = AV_RL32(src - 12);
    1062          52 :         tile_level_y = AV_RL32(src - 8);
    1063             : 
    1064          52 :         data_size = AV_RL32(src - 4);
    1065          52 :         if (data_size <= 0 || data_size > buf_size)
    1066           0 :             return AVERROR_INVALIDDATA;
    1067             : 
    1068          52 :         if (tile_level_x || tile_level_y) { /* tile level, is not the full res level */
    1069           0 :             avpriv_report_missing_feature(s->avctx, "Subres tile before full res tile");
    1070           0 :             return AVERROR_PATCHWELCOME;
    1071             :         }
    1072             : 
    1073          52 :         if (s->xmin || s->ymin) {
    1074           0 :             avpriv_report_missing_feature(s->avctx, "Tiles with xmin/ymin");
    1075           0 :             return AVERROR_PATCHWELCOME;
    1076             :         }
    1077             : 
    1078          52 :         line = s->tile_attr.ySize * tile_y;
    1079          52 :         col = s->tile_attr.xSize * tile_x;
    1080             : 
    1081         104 :         if (line < s->ymin || line > s->ymax ||
    1082         104 :             col  < s->xmin || col  > s->xmax)
    1083           0 :             return AVERROR_INVALIDDATA;
    1084             : 
    1085          52 :         td->ysize = FFMIN(s->tile_attr.ySize, s->ydelta - tile_y * s->tile_attr.ySize);
    1086          52 :         td->xsize = FFMIN(s->tile_attr.xSize, s->xdelta - tile_x * s->tile_attr.xSize);
    1087             : 
    1088          52 :         if (col) { /* not the first tile of the line */
    1089          12 :             bxmin = 0; /* doesn't add pixel at the left of the datawindow */
    1090             :         }
    1091             : 
    1092          52 :         if ((col + td->xsize) != s->xdelta)/* not the last tile of the line */
    1093          12 :             axmax = 0; /* doesn't add pixel at the right of the datawindow */
    1094             : 
    1095          52 :         td->channel_line_size = td->xsize * s->current_channel_offset;/* uncompress size of one line */
    1096          52 :         uncompressed_size = td->channel_line_size * (uint64_t)td->ysize;/* uncompress size of the block */
    1097             :     } else {
    1098        4470 :         if (line_offset > buf_size - 8)
    1099           0 :             return AVERROR_INVALIDDATA;
    1100             : 
    1101        4470 :         src  = buf + line_offset + 8;
    1102        4470 :         line = AV_RL32(src - 8);
    1103             : 
    1104        4470 :         if (line < s->ymin || line > s->ymax)
    1105           0 :             return AVERROR_INVALIDDATA;
    1106             : 
    1107        4470 :         data_size = AV_RL32(src - 4);
    1108        4470 :         if (data_size <= 0 || data_size > buf_size)
    1109           0 :             return AVERROR_INVALIDDATA;
    1110             : 
    1111        4470 :         td->ysize          = FFMIN(s->scan_lines_per_block, s->ymax - line + 1); /* s->ydelta - line ?? */
    1112        4470 :         td->xsize          = s->xdelta;
    1113             : 
    1114        4470 :         td->channel_line_size = td->xsize * s->current_channel_offset;/* uncompress size of one line */
    1115        4470 :         uncompressed_size = td->channel_line_size * (uint64_t)td->ysize;/* uncompress size of the block */
    1116             : 
    1117        5860 :         if ((s->compression == EXR_RAW && (data_size != uncompressed_size ||
    1118        5860 :                                            line_offset > buf_size - uncompressed_size)) ||
    1119       10630 :             (s->compression != EXR_RAW && (data_size > uncompressed_size ||
    1120        3080 :                                            line_offset > buf_size - data_size))) {
    1121           0 :             return AVERROR_INVALIDDATA;
    1122             :         }
    1123             :     }
    1124             : 
    1125        4522 :     if (data_size < uncompressed_size || s->is_tile) { /* td->tmp is use for tile reorganization */
    1126        3128 :         av_fast_padded_malloc(&td->tmp, &td->tmp_size, uncompressed_size);
    1127        3128 :         if (!td->tmp)
    1128           0 :             return AVERROR(ENOMEM);
    1129             :     }
    1130             : 
    1131        4522 :     if (data_size < uncompressed_size) {
    1132        6204 :         av_fast_padded_malloc(&td->uncompressed_data,
    1133        3102 :                               &td->uncompressed_size, uncompressed_size + 64);/* Force 64 padding for AVX2 reorder_pixels dst */
    1134             : 
    1135        3102 :         if (!td->uncompressed_data)
    1136           0 :             return AVERROR(ENOMEM);
    1137             : 
    1138        3102 :         ret = AVERROR_INVALIDDATA;
    1139        3102 :         switch (s->compression) {
    1140        1500 :         case EXR_ZIP1:
    1141             :         case EXR_ZIP16:
    1142        1500 :             ret = zip_uncompress(s, src, data_size, uncompressed_size, td);
    1143        1500 :             break;
    1144          48 :         case EXR_PIZ:
    1145          48 :             ret = piz_uncompress(s, src, data_size, uncompressed_size, td);
    1146          48 :             break;
    1147         122 :         case EXR_PXR24:
    1148         122 :             ret = pxr24_uncompress(s, src, data_size, uncompressed_size, td);
    1149         122 :             break;
    1150        1386 :         case EXR_RLE:
    1151        1386 :             ret = rle_uncompress(s, src, data_size, uncompressed_size, td);
    1152        1386 :             break;
    1153          46 :         case EXR_B44:
    1154             :         case EXR_B44A:
    1155          46 :             ret = b44_uncompress(s, src, data_size, uncompressed_size, td);
    1156          46 :             break;
    1157             :         }
    1158        3102 :         if (ret < 0) {
    1159           0 :             av_log(avctx, AV_LOG_ERROR, "decode_block() failed.\n");
    1160           0 :             return ret;
    1161             :         }
    1162        3102 :         src = td->uncompressed_data;
    1163             :     }
    1164             : 
    1165        4522 :     if (!s->is_luma) {
    1166        4518 :         channel_buffer[0] = src + td->xsize * s->channel_offsets[0];
    1167        4518 :         channel_buffer[1] = src + td->xsize * s->channel_offsets[1];
    1168        4518 :         channel_buffer[2] = src + td->xsize * s->channel_offsets[2];
    1169        4518 :         rgb_channel_count = 3;
    1170             :     } else { /* put y data in the first channel_buffer */
    1171           4 :         channel_buffer[0] = src + td->xsize * s->channel_offsets[1];
    1172           4 :         rgb_channel_count = 1;
    1173             :     }
    1174        4522 :     if (s->channel_offsets[3] >= 0)
    1175        4238 :         channel_buffer[3] = src + td->xsize * s->channel_offsets[3];
    1176             : 
    1177        4522 :     ptr = p->data[0] + line * p->linesize[0] + (col * s->desc->nb_components * 2);
    1178             : 
    1179       19520 :     for (i = 0;
    1180       10476 :          i < td->ysize; i++, ptr += p->linesize[0]) {
    1181             : 
    1182             :         const uint8_t * a;
    1183             :         const uint8_t *rgb[3];
    1184             : 
    1185       41840 :         for (c = 0; c < rgb_channel_count; c++){
    1186       31364 :             rgb[c] = channel_buffer[c];
    1187             :         }
    1188             : 
    1189       10476 :         if (channel_buffer[3])
    1190        6262 :             a = channel_buffer[3];
    1191             : 
    1192       10476 :         ptr_x = (uint16_t *) ptr;
    1193             : 
    1194             :         // Zero out the start if xmin is not 0
    1195       10476 :         memset(ptr_x, 0, bxmin);
    1196       10476 :         ptr_x += s->xmin * s->desc->nb_components;
    1197             : 
    1198       10476 :         if (s->pixel_type == EXR_FLOAT) {
    1199             :             // 32-bit
    1200        1192 :             if (trc_func) {
    1201           0 :                 for (x = 0; x < td->xsize; x++) {
    1202             :                     union av_intfloat32 t;
    1203             : 
    1204           0 :                     for (c = 0; c < rgb_channel_count; c++) {
    1205           0 :                         t.i = bytestream_get_le32(&rgb[c]);
    1206           0 :                         t.f = trc_func(t.f);
    1207           0 :                         *ptr_x++ = exr_flt2uint(t.i);
    1208             :                     }
    1209           0 :                     if (channel_buffer[3])
    1210           0 :                         *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
    1211             :                 }
    1212             :             } else {
    1213       47412 :                 for (x = 0; x < td->xsize; x++) {
    1214             :                     union av_intfloat32 t;
    1215             :                     int c;
    1216             : 
    1217      184880 :                     for (c = 0; c < rgb_channel_count; c++) {
    1218      138660 :                         t.i = bytestream_get_le32(&rgb[c]);
    1219      138660 :                         if (t.f > 0.0f)  /* avoid negative values */
    1220      136236 :                             t.f = powf(t.f, one_gamma);
    1221      138660 :                         *ptr_x++ = exr_flt2uint(t.i);
    1222             :                     }
    1223             : 
    1224       46220 :                     if (channel_buffer[3])
    1225        3156 :                         *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
    1226             :                 }
    1227             :             }
    1228        9284 :         } else if (s->pixel_type == EXR_HALF) {
    1229             :             // 16-bit
    1230     5417978 :             for (x = 0; x < td->xsize; x++) {
    1231             :                 int c;
    1232    21634136 :                 for (c = 0; c < rgb_channel_count; c++) {
    1233    16225410 :                     *ptr_x++ = s->gamma_table[bytestream_get_le16(&rgb[c])];
    1234             :                 }
    1235             : 
    1236     5408726 :                 if (channel_buffer[3])
    1237     3640406 :                     *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&a));
    1238             :             }
    1239          32 :         } else if (s->pixel_type == EXR_UINT) {
    1240         416 :             for (x = 0; x < td->xsize; x++) {
    1241        1536 :                 for (c = 0; c < rgb_channel_count; c++) {
    1242        1152 :                     *ptr_x++ = bytestream_get_le32(&rgb[c]) >> 16;
    1243             :                 }
    1244             : 
    1245         384 :                 if (channel_buffer[3])
    1246           0 :                     *ptr_x++ = bytestream_get_le32(&a) >> 16;
    1247             :             }
    1248             :         }
    1249             : 
    1250             :         // Zero out the end if xmax+1 is not w
    1251       10476 :         memset(ptr_x, 0, axmax);
    1252             : 
    1253       10476 :         channel_buffer[0] += td->channel_line_size;
    1254       10476 :         channel_buffer[1] += td->channel_line_size;
    1255       10476 :         channel_buffer[2] += td->channel_line_size;
    1256       10476 :         if (channel_buffer[3])
    1257        6262 :             channel_buffer[3] += td->channel_line_size;
    1258             :     }
    1259             : 
    1260        4522 :     return 0;
    1261             : }
    1262             : 
    1263             : /**
    1264             :  * Check if the variable name corresponds to its data type.
    1265             :  *
    1266             :  * @param s              the EXRContext
    1267             :  * @param value_name     name of the variable to check
    1268             :  * @param value_type     type of the variable to check
    1269             :  * @param minimum_length minimum length of the variable data
    1270             :  *
    1271             :  * @return bytes to read containing variable data
    1272             :  *         -1 if variable is not found
    1273             :  *         0 if buffer ended prematurely
    1274             :  */
    1275       13032 : static int check_header_variable(EXRContext *s,
    1276             :                                  const char *value_name,
    1277             :                                  const char *value_type,
    1278             :                                  unsigned int minimum_length)
    1279             : {
    1280       13032 :     int var_size = -1;
    1281             : 
    1282       26064 :     if (bytestream2_get_bytes_left(&s->gb) >= minimum_length &&
    1283       13032 :         !strcmp(s->gb.buffer, value_name)) {
    1284             :         // found value_name, jump to value_type (null terminated strings)
    1285         796 :         s->gb.buffer += strlen(value_name) + 1;
    1286         796 :         if (!strcmp(s->gb.buffer, value_type)) {
    1287         796 :             s->gb.buffer += strlen(value_type) + 1;
    1288         796 :             var_size = bytestream2_get_le32(&s->gb);
    1289             :             // don't go read past boundaries
    1290         796 :             if (var_size > bytestream2_get_bytes_left(&s->gb))
    1291           0 :                 var_size = 0;
    1292             :         } else {
    1293             :             // value_type not found, reset the buffer
    1294           0 :             s->gb.buffer -= strlen(value_name) + 1;
    1295           0 :             av_log(s->avctx, AV_LOG_WARNING,
    1296             :                    "Unknown data type %s for header variable %s.\n",
    1297             :                    value_type, value_name);
    1298             :         }
    1299             :     }
    1300             : 
    1301       13032 :     return var_size;
    1302             : }
    1303             : 
    1304         124 : static int decode_header(EXRContext *s, AVFrame *frame)
    1305             : {
    1306         124 :     AVDictionary *metadata = NULL;
    1307         124 :     int magic_number, version, i, flags, sar = 0;
    1308         124 :     int layer_match = 0;
    1309             : 
    1310         124 :     s->current_channel_offset = 0;
    1311         124 :     s->xmin               = ~0;
    1312         124 :     s->xmax               = ~0;
    1313         124 :     s->ymin               = ~0;
    1314         124 :     s->ymax               = ~0;
    1315         124 :     s->xdelta             = ~0;
    1316         124 :     s->ydelta             = ~0;
    1317         124 :     s->channel_offsets[0] = -1;
    1318         124 :     s->channel_offsets[1] = -1;
    1319         124 :     s->channel_offsets[2] = -1;
    1320         124 :     s->channel_offsets[3] = -1;
    1321         124 :     s->pixel_type         = EXR_UNKNOWN;
    1322         124 :     s->compression        = EXR_UNKN;
    1323         124 :     s->nb_channels        = 0;
    1324         124 :     s->w                  = 0;
    1325         124 :     s->h                  = 0;
    1326         124 :     s->tile_attr.xSize    = -1;
    1327         124 :     s->tile_attr.ySize    = -1;
    1328         124 :     s->is_tile            = 0;
    1329         124 :     s->is_luma            = 0;
    1330             : 
    1331         124 :     if (bytestream2_get_bytes_left(&s->gb) < 10) {
    1332           0 :         av_log(s->avctx, AV_LOG_ERROR, "Header too short to parse.\n");
    1333           0 :         return AVERROR_INVALIDDATA;
    1334             :     }
    1335             : 
    1336         124 :     magic_number = bytestream2_get_le32(&s->gb);
    1337         124 :     if (magic_number != 20000630) {
    1338             :         /* As per documentation of OpenEXR, it is supposed to be
    1339             :          * int 20000630 little-endian */
    1340           0 :         av_log(s->avctx, AV_LOG_ERROR, "Wrong magic number %d.\n", magic_number);
    1341           0 :         return AVERROR_INVALIDDATA;
    1342             :     }
    1343             : 
    1344         124 :     version = bytestream2_get_byte(&s->gb);
    1345         124 :     if (version != 2) {
    1346           0 :         avpriv_report_missing_feature(s->avctx, "Version %d", version);
    1347           0 :         return AVERROR_PATCHWELCOME;
    1348             :     }
    1349             : 
    1350         124 :     flags = bytestream2_get_le24(&s->gb);
    1351             : 
    1352         124 :     if (flags == 0x00)
    1353          88 :         s->is_tile = 0;
    1354          36 :     else if (flags & 0x02)
    1355          36 :         s->is_tile = 1;
    1356             :     else{
    1357           0 :         avpriv_report_missing_feature(s->avctx, "flags %d", flags);
    1358           0 :         return AVERROR_PATCHWELCOME;
    1359             :     }
    1360             : 
    1361             :     // Parse the header
    1362        2300 :     while (bytestream2_get_bytes_left(&s->gb) > 0 && *s->gb.buffer) {
    1363             :         int var_size;
    1364        2052 :         if ((var_size = check_header_variable(s, "channels",
    1365             :                                               "chlist", 38)) >= 0) {
    1366             :             GetByteContext ch_gb;
    1367         124 :             if (!var_size)
    1368           0 :                 return AVERROR_INVALIDDATA;
    1369             : 
    1370         124 :             bytestream2_init(&ch_gb, s->gb.buffer, var_size);
    1371             : 
    1372         940 :             while (bytestream2_get_bytes_left(&ch_gb) >= 19) {
    1373             :                 EXRChannel *channel;
    1374             :                 enum ExrPixelType current_pixel_type;
    1375         692 :                 int channel_index = -1;
    1376             :                 int xsub, ysub;
    1377             : 
    1378         692 :                 if (strcmp(s->layer, "") != 0) {
    1379         260 :                     if (strncmp(ch_gb.buffer, s->layer, strlen(s->layer)) == 0) {
    1380         144 :                         layer_match = 1;
    1381         144 :                         av_log(s->avctx, AV_LOG_INFO,
    1382             :                                "Channel match layer : %s.\n", ch_gb.buffer);
    1383         144 :                         ch_gb.buffer += strlen(s->layer);
    1384         144 :                         if (*ch_gb.buffer == '.')
    1385         144 :                             ch_gb.buffer++;         /* skip dot if not given */
    1386             :                     } else {
    1387         116 :                         av_log(s->avctx, AV_LOG_INFO,
    1388             :                                "Channel doesn't match layer : %s.\n", ch_gb.buffer);
    1389             :                     }
    1390             :                 } else {
    1391         432 :                     layer_match = 1;
    1392             :                 }
    1393             : 
    1394         692 :                 if (layer_match) { /* only search channel if the layer match is valid */
    1395        1032 :                     if (!strcmp(ch_gb.buffer, "R") ||
    1396         912 :                         !strcmp(ch_gb.buffer, "X") ||
    1397         456 :                         !strcmp(ch_gb.buffer, "U")) {
    1398         120 :                         channel_index = 0;
    1399         120 :                         s->is_luma = 0;
    1400         792 :                     } else if (!strcmp(ch_gb.buffer, "G") ||
    1401         336 :                                !strcmp(ch_gb.buffer, "V")) {
    1402         120 :                         channel_index = 1;
    1403         120 :                         s->is_luma = 0;
    1404         336 :                     } else if (!strcmp(ch_gb.buffer, "Y")) {
    1405           4 :                         channel_index = 1;
    1406           4 :                         s->is_luma = 1;
    1407         544 :                     } else if (!strcmp(ch_gb.buffer, "B") ||
    1408         424 :                                !strcmp(ch_gb.buffer, "Z") ||
    1409         212 :                                !strcmp(ch_gb.buffer, "W")){
    1410         120 :                                channel_index = 2;
    1411         120 :                         s->is_luma = 0;
    1412         212 :                     } else if (!strcmp(ch_gb.buffer, "A")) {
    1413          54 :                         channel_index = 3;
    1414             :                     } else {
    1415         158 :                         av_log(s->avctx, AV_LOG_WARNING,
    1416             :                                "Unsupported channel %.256s.\n", ch_gb.buffer);
    1417             :                     }
    1418             :                 }
    1419             : 
    1420             :                 /* skip until you get a 0 */
    1421        8516 :                 while (bytestream2_get_bytes_left(&ch_gb) > 0 &&
    1422        3912 :                        bytestream2_get_byte(&ch_gb))
    1423        3220 :                     continue;
    1424             : 
    1425         692 :                 if (bytestream2_get_bytes_left(&ch_gb) < 4) {
    1426           0 :                     av_log(s->avctx, AV_LOG_ERROR, "Incomplete header.\n");
    1427           0 :                     return AVERROR_INVALIDDATA;
    1428             :                 }
    1429             : 
    1430         692 :                 current_pixel_type = bytestream2_get_le32(&ch_gb);
    1431         692 :                 if (current_pixel_type >= EXR_UNKNOWN) {
    1432           0 :                     avpriv_report_missing_feature(s->avctx, "Pixel type %d",
    1433             :                                                   current_pixel_type);
    1434           0 :                     return AVERROR_PATCHWELCOME;
    1435             :                 }
    1436             : 
    1437         692 :                 bytestream2_skip(&ch_gb, 4);
    1438         692 :                 xsub = bytestream2_get_le32(&ch_gb);
    1439         692 :                 ysub = bytestream2_get_le32(&ch_gb);
    1440             : 
    1441         692 :                 if (xsub != 1 || ysub != 1) {
    1442           0 :                     avpriv_report_missing_feature(s->avctx,
    1443             :                                                   "Subsampling %dx%d",
    1444             :                                                   xsub, ysub);
    1445           0 :                     return AVERROR_PATCHWELCOME;
    1446             :                 }
    1447             : 
    1448         692 :                 if (channel_index >= 0 && s->channel_offsets[channel_index] == -1) { /* channel has not been previously assigned */
    1449         712 :                     if (s->pixel_type != EXR_UNKNOWN &&
    1450         294 :                         s->pixel_type != current_pixel_type) {
    1451           0 :                         av_log(s->avctx, AV_LOG_ERROR,
    1452             :                                "RGB channels not of the same depth.\n");
    1453           0 :                         return AVERROR_INVALIDDATA;
    1454             :                     }
    1455         418 :                     s->pixel_type                     = current_pixel_type;
    1456         418 :                     s->channel_offsets[channel_index] = s->current_channel_offset;
    1457             :                 }
    1458             : 
    1459        1384 :                 s->channels = av_realloc(s->channels,
    1460        1384 :                                          ++s->nb_channels * sizeof(EXRChannel));
    1461         692 :                 if (!s->channels)
    1462           0 :                     return AVERROR(ENOMEM);
    1463         692 :                 channel             = &s->channels[s->nb_channels - 1];
    1464         692 :                 channel->pixel_type = current_pixel_type;
    1465         692 :                 channel->xsub       = xsub;
    1466         692 :                 channel->ysub       = ysub;
    1467             : 
    1468         692 :                 if (current_pixel_type == EXR_HALF) {
    1469         350 :                     s->current_channel_offset += 2;
    1470             :                 } else {/* Float or UINT32 */
    1471         342 :                     s->current_channel_offset += 4;
    1472             :                 }
    1473             :             }
    1474             : 
    1475             :             /* Check if all channels are set with an offset or if the channels
    1476             :              * are causing an overflow  */
    1477         124 :             if (!s->is_luma){/* if we expected to have at least 3 channels */
    1478         120 :                 if (FFMIN3(s->channel_offsets[0],
    1479             :                            s->channel_offsets[1],
    1480             :                            s->channel_offsets[2]) < 0) {
    1481           0 :                     if (s->channel_offsets[0] < 0)
    1482           0 :                         av_log(s->avctx, AV_LOG_ERROR, "Missing red channel.\n");
    1483           0 :                     if (s->channel_offsets[1] < 0)
    1484           0 :                         av_log(s->avctx, AV_LOG_ERROR, "Missing green channel.\n");
    1485           0 :                     if (s->channel_offsets[2] < 0)
    1486           0 :                         av_log(s->avctx, AV_LOG_ERROR, "Missing blue channel.\n");
    1487           0 :                     return AVERROR_INVALIDDATA;
    1488             :                 }
    1489             :             }
    1490             : 
    1491             :             // skip one last byte and update main gb
    1492         124 :             s->gb.buffer = ch_gb.buffer + 1;
    1493         124 :             continue;
    1494        1928 :         } else if ((var_size = check_header_variable(s, "dataWindow", "box2i",
    1495             :                                                      31)) >= 0) {
    1496         124 :             if (!var_size)
    1497           0 :                 return AVERROR_INVALIDDATA;
    1498             : 
    1499         124 :             s->xmin   = bytestream2_get_le32(&s->gb);
    1500         124 :             s->ymin   = bytestream2_get_le32(&s->gb);
    1501         124 :             s->xmax   = bytestream2_get_le32(&s->gb);
    1502         124 :             s->ymax   = bytestream2_get_le32(&s->gb);
    1503         124 :             s->xdelta = (s->xmax - s->xmin) + 1;
    1504         124 :             s->ydelta = (s->ymax - s->ymin) + 1;
    1505             : 
    1506         124 :             continue;
    1507        1804 :         } else if ((var_size = check_header_variable(s, "displayWindow",
    1508             :                                                      "box2i", 34)) >= 0) {
    1509         124 :             if (!var_size)
    1510           0 :                 return AVERROR_INVALIDDATA;
    1511             : 
    1512         124 :             bytestream2_skip(&s->gb, 8);
    1513         124 :             s->w = bytestream2_get_le32(&s->gb) + 1;
    1514         124 :             s->h = bytestream2_get_le32(&s->gb) + 1;
    1515             : 
    1516         124 :             continue;
    1517        1680 :         } else if ((var_size = check_header_variable(s, "lineOrder",
    1518             :                                                      "lineOrder", 25)) >= 0) {
    1519             :             int line_order;
    1520         124 :             if (!var_size)
    1521           0 :                 return AVERROR_INVALIDDATA;
    1522             : 
    1523         124 :             line_order = bytestream2_get_byte(&s->gb);
    1524         124 :             av_log(s->avctx, AV_LOG_DEBUG, "line order: %d.\n", line_order);
    1525         124 :             if (line_order > 2) {
    1526           0 :                 av_log(s->avctx, AV_LOG_ERROR, "Unknown line order.\n");
    1527           0 :                 return AVERROR_INVALIDDATA;
    1528             :             }
    1529             : 
    1530         124 :             continue;
    1531        1556 :         } else if ((var_size = check_header_variable(s, "pixelAspectRatio",
    1532             :                                                      "float", 31)) >= 0) {
    1533         124 :             if (!var_size)
    1534           0 :                 return AVERROR_INVALIDDATA;
    1535             : 
    1536         124 :             sar = bytestream2_get_le32(&s->gb);
    1537             : 
    1538         124 :             continue;
    1539        1432 :         } else if ((var_size = check_header_variable(s, "compression",
    1540             :                                                      "compression", 29)) >= 0) {
    1541         124 :             if (!var_size)
    1542           0 :                 return AVERROR_INVALIDDATA;
    1543             : 
    1544         124 :             if (s->compression == EXR_UNKN)
    1545         124 :                 s->compression = bytestream2_get_byte(&s->gb);
    1546             :             else
    1547           0 :                 av_log(s->avctx, AV_LOG_WARNING,
    1548             :                        "Found more than one compression attribute.\n");
    1549             : 
    1550         124 :             continue;
    1551        1308 :         } else if ((var_size = check_header_variable(s, "tiles",
    1552             :                                                      "tiledesc", 22)) >= 0) {
    1553             :             char tileLevel;
    1554             : 
    1555          36 :             if (!s->is_tile)
    1556           0 :                 av_log(s->avctx, AV_LOG_WARNING,
    1557             :                        "Found tile attribute and scanline flags. Exr will be interpreted as scanline.\n");
    1558             : 
    1559          36 :             s->tile_attr.xSize = bytestream2_get_le32(&s->gb);
    1560          36 :             s->tile_attr.ySize = bytestream2_get_le32(&s->gb);
    1561             : 
    1562          36 :             tileLevel = bytestream2_get_byte(&s->gb);
    1563          36 :             s->tile_attr.level_mode = tileLevel & 0x0f;
    1564          36 :             s->tile_attr.level_round = (tileLevel >> 4) & 0x0f;
    1565             : 
    1566          36 :             if (s->tile_attr.level_mode >= EXR_TILE_LEVEL_UNKNOWN){
    1567           0 :                 avpriv_report_missing_feature(s->avctx, "Tile level mode %d",
    1568           0 :                                               s->tile_attr.level_mode);
    1569           0 :                 return AVERROR_PATCHWELCOME;
    1570             :             }
    1571             : 
    1572          36 :             if (s->tile_attr.level_round >= EXR_TILE_ROUND_UNKNOWN) {
    1573           0 :                 avpriv_report_missing_feature(s->avctx, "Tile level round %d",
    1574           0 :                                               s->tile_attr.level_round);
    1575           0 :                 return AVERROR_PATCHWELCOME;
    1576             :             }
    1577             : 
    1578          36 :             continue;
    1579        1272 :         } else if ((var_size = check_header_variable(s, "writer",
    1580             :                                                      "string", 1)) >= 0) {
    1581          16 :             uint8_t key[256] = { 0 };
    1582             : 
    1583          16 :             bytestream2_get_buffer(&s->gb, key, FFMIN(sizeof(key) - 1, var_size));
    1584          16 :             av_dict_set(&metadata, "writer", key, 0);
    1585             : 
    1586          16 :             continue;
    1587             :         }
    1588             : 
    1589             :         // Check if there are enough bytes for a header
    1590        1256 :         if (bytestream2_get_bytes_left(&s->gb) <= 9) {
    1591           0 :             av_log(s->avctx, AV_LOG_ERROR, "Incomplete header\n");
    1592           0 :             return AVERROR_INVALIDDATA;
    1593             :         }
    1594             : 
    1595             :         // Process unknown variables
    1596        3768 :         for (i = 0; i < 2; i++) // value_name and value_type
    1597        2512 :             while (bytestream2_get_byte(&s->gb) != 0);
    1598             : 
    1599             :         // Skip variable length
    1600        1256 :         bytestream2_skip(&s->gb, bytestream2_get_le32(&s->gb));
    1601             :     }
    1602             : 
    1603         124 :     ff_set_sar(s->avctx, av_d2q(av_int2float(sar), 255));
    1604             : 
    1605         124 :     if (s->compression == EXR_UNKN) {
    1606           0 :         av_log(s->avctx, AV_LOG_ERROR, "Missing compression attribute.\n");
    1607           0 :         return AVERROR_INVALIDDATA;
    1608             :     }
    1609             : 
    1610         124 :     if (s->is_tile) {
    1611          36 :         if (s->tile_attr.xSize < 1 || s->tile_attr.ySize < 1) {
    1612           0 :             av_log(s->avctx, AV_LOG_ERROR, "Invalid tile attribute.\n");
    1613           0 :             return AVERROR_INVALIDDATA;
    1614             :         }
    1615             :     }
    1616             : 
    1617         124 :     if (bytestream2_get_bytes_left(&s->gb) <= 0) {
    1618           0 :         av_log(s->avctx, AV_LOG_ERROR, "Incomplete frame.\n");
    1619           0 :         return AVERROR_INVALIDDATA;
    1620             :     }
    1621             : 
    1622         124 :     frame->metadata = metadata;
    1623             : 
    1624             :     // aaand we are done
    1625         124 :     bytestream2_skip(&s->gb, 1);
    1626         124 :     return 0;
    1627             : }
    1628             : 
    1629         124 : static int decode_frame(AVCodecContext *avctx, void *data,
    1630             :                         int *got_frame, AVPacket *avpkt)
    1631             : {
    1632         124 :     EXRContext *s = avctx->priv_data;
    1633         124 :     ThreadFrame frame = { .f = data };
    1634         124 :     AVFrame *picture = data;
    1635             :     uint8_t *ptr;
    1636             : 
    1637             :     int y, ret;
    1638             :     int out_line_size;
    1639             :     int nb_blocks;   /* nb scanline or nb tile */
    1640             :     uint64_t start_offset_table;
    1641             :     uint64_t start_next_scanline;
    1642             :     PutByteContext offset_table_writer;
    1643             : 
    1644         124 :     bytestream2_init(&s->gb, avpkt->data, avpkt->size);
    1645             : 
    1646         124 :     if ((ret = decode_header(s, picture)) < 0)
    1647           0 :         return ret;
    1648             : 
    1649         124 :     switch (s->pixel_type) {
    1650         124 :     case EXR_FLOAT:
    1651             :     case EXR_HALF:
    1652             :     case EXR_UINT:
    1653         124 :         if (s->channel_offsets[3] >= 0) {
    1654          54 :             if (!s->is_luma) {
    1655          54 :                 avctx->pix_fmt = AV_PIX_FMT_RGBA64;
    1656             :             } else {
    1657           0 :                 avctx->pix_fmt = AV_PIX_FMT_YA16;
    1658             :             }
    1659             :         } else {
    1660          70 :             if (!s->is_luma) {
    1661          66 :                 avctx->pix_fmt = AV_PIX_FMT_RGB48;
    1662             :             } else {
    1663           4 :                 avctx->pix_fmt = AV_PIX_FMT_GRAY16;
    1664             :             }
    1665             :         }
    1666         124 :         break;
    1667           0 :     default:
    1668           0 :         av_log(avctx, AV_LOG_ERROR, "Missing channel list.\n");
    1669           0 :         return AVERROR_INVALIDDATA;
    1670             :     }
    1671             : 
    1672         124 :     if (s->apply_trc_type != AVCOL_TRC_UNSPECIFIED)
    1673           0 :         avctx->color_trc = s->apply_trc_type;
    1674             : 
    1675         124 :     switch (s->compression) {
    1676          40 :     case EXR_RAW:
    1677             :     case EXR_RLE:
    1678             :     case EXR_ZIP1:
    1679          40 :         s->scan_lines_per_block = 1;
    1680          40 :         break;
    1681          38 :     case EXR_PXR24:
    1682             :     case EXR_ZIP16:
    1683          38 :         s->scan_lines_per_block = 16;
    1684          38 :         break;
    1685          46 :     case EXR_PIZ:
    1686             :     case EXR_B44:
    1687             :     case EXR_B44A:
    1688          46 :         s->scan_lines_per_block = 32;
    1689          46 :         break;
    1690           0 :     default:
    1691           0 :         avpriv_report_missing_feature(avctx, "Compression %d", s->compression);
    1692           0 :         return AVERROR_PATCHWELCOME;
    1693             :     }
    1694             : 
    1695             :     /* Verify the xmin, xmax, ymin, ymax and xdelta before setting
    1696             :      * the actual image size. */
    1697         248 :     if (s->xmin > s->xmax                  ||
    1698         248 :         s->ymin > s->ymax                  ||
    1699         248 :         s->xdelta != s->xmax - s->xmin + 1 ||
    1700         248 :         s->xmax >= s->w                    ||
    1701         124 :         s->ymax >= s->h) {
    1702           0 :         av_log(avctx, AV_LOG_ERROR, "Wrong or missing size information.\n");
    1703           0 :         return AVERROR_INVALIDDATA;
    1704             :     }
    1705             : 
    1706         124 :     if ((ret = ff_set_dimensions(avctx, s->w, s->h)) < 0)
    1707           0 :         return ret;
    1708             : 
    1709         124 :     s->desc          = av_pix_fmt_desc_get(avctx->pix_fmt);
    1710         124 :     if (!s->desc)
    1711           0 :         return AVERROR_INVALIDDATA;
    1712         124 :     out_line_size    = avctx->width * 2 * s->desc->nb_components;
    1713             : 
    1714         124 :     if (s->is_tile) {
    1715          72 :         nb_blocks = ((s->xdelta + s->tile_attr.xSize - 1) / s->tile_attr.xSize) *
    1716          36 :         ((s->ydelta + s->tile_attr.ySize - 1) / s->tile_attr.ySize);
    1717             :     } else { /* scanline */
    1718         176 :         nb_blocks = (s->ydelta + s->scan_lines_per_block - 1) /
    1719          88 :         s->scan_lines_per_block;
    1720             :     }
    1721             : 
    1722         124 :     if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
    1723           0 :         return ret;
    1724             : 
    1725         124 :     if (bytestream2_get_bytes_left(&s->gb) < nb_blocks * 8)
    1726           0 :         return AVERROR_INVALIDDATA;
    1727             : 
    1728             :     // check offset table and recreate it if need
    1729         124 :     if (!s->is_tile && bytestream2_peek_le64(&s->gb) == 0) {
    1730           1 :         av_log(s->avctx, AV_LOG_DEBUG, "recreating invalid scanline offset table\n");
    1731             : 
    1732           1 :         start_offset_table = bytestream2_tell(&s->gb);
    1733           1 :         start_next_scanline = start_offset_table + nb_blocks * 8;
    1734           1 :         bytestream2_init_writer(&offset_table_writer, &avpkt->data[start_offset_table], nb_blocks * 8);
    1735             : 
    1736           9 :         for (y = 0; y < nb_blocks; y++) {
    1737             :             /* write offset of prev scanline in offset table */
    1738           8 :             bytestream2_put_le64(&offset_table_writer, start_next_scanline);
    1739             : 
    1740             :             /* get len of next scanline */
    1741           8 :             bytestream2_seek(&s->gb, start_next_scanline + 4, SEEK_SET);/* skip line number */
    1742           8 :             start_next_scanline += (bytestream2_get_le32(&s->gb) + 8);
    1743             :         }
    1744           1 :         bytestream2_seek(&s->gb, start_offset_table, SEEK_SET);
    1745             :     }
    1746             : 
    1747             :     // save pointer we are going to use in decode_block
    1748         124 :     s->buf      = avpkt->data;
    1749         124 :     s->buf_size = avpkt->size;
    1750         124 :     ptr         = picture->data[0];
    1751             : 
    1752             :     // Zero out the start if ymin is not 0
    1753         204 :     for (y = 0; y < s->ymin; y++) {
    1754          80 :         memset(ptr, 0, out_line_size);
    1755          80 :         ptr += picture->linesize[0];
    1756             :     }
    1757             : 
    1758         124 :     s->picture = picture;
    1759             : 
    1760         124 :     avctx->execute2(avctx, decode_block, s->thread_data, NULL, nb_blocks);
    1761             : 
    1762             :     // Zero out the end if ymax+1 is not h
    1763         124 :     ptr = picture->data[0] + ((s->ymax+1) * picture->linesize[0]);
    1764         246 :     for (y = s->ymax + 1; y < avctx->height; y++) {
    1765         122 :         memset(ptr, 0, out_line_size);
    1766         122 :         ptr += picture->linesize[0];
    1767             :     }
    1768             : 
    1769         124 :     picture->pict_type = AV_PICTURE_TYPE_I;
    1770         124 :     *got_frame = 1;
    1771             : 
    1772         124 :     return avpkt->size;
    1773             : }
    1774             : 
    1775         124 : static av_cold int decode_init(AVCodecContext *avctx)
    1776             : {
    1777         124 :     EXRContext *s = avctx->priv_data;
    1778             :     uint32_t i;
    1779             :     union av_intfloat32 t;
    1780         124 :     float one_gamma = 1.0f / s->gamma;
    1781         124 :     avpriv_trc_function trc_func = NULL;
    1782             : 
    1783         124 :     s->avctx              = avctx;
    1784             : 
    1785         124 :     ff_exrdsp_init(&s->dsp);
    1786             : 
    1787             : #if HAVE_BIGENDIAN
    1788             :     ff_bswapdsp_init(&s->bbdsp);
    1789             : #endif
    1790             : 
    1791         124 :     trc_func = avpriv_get_trc_function_from_trc(s->apply_trc_type);
    1792         124 :     if (trc_func) {
    1793           0 :         for (i = 0; i < 65536; ++i) {
    1794           0 :             t = exr_half2float(i);
    1795           0 :             t.f = trc_func(t.f);
    1796           0 :             s->gamma_table[i] = exr_flt2uint(t.i);
    1797             :         }
    1798             :     } else {
    1799         248 :         if (one_gamma > 0.9999f && one_gamma < 1.0001f) {
    1800     8126588 :             for (i = 0; i < 65536; ++i)
    1801     8126464 :                 s->gamma_table[i] = exr_halflt2uint(i);
    1802             :         } else {
    1803           0 :             for (i = 0; i < 65536; ++i) {
    1804           0 :                 t = exr_half2float(i);
    1805             :                 /* If negative value we reuse half value */
    1806           0 :                 if (t.f <= 0.0f) {
    1807           0 :                     s->gamma_table[i] = exr_halflt2uint(i);
    1808             :                 } else {
    1809           0 :                     t.f = powf(t.f, one_gamma);
    1810           0 :                     s->gamma_table[i] = exr_flt2uint(t.i);
    1811             :                 }
    1812             :             }
    1813             :         }
    1814             :     }
    1815             : 
    1816             :     // allocate thread data, used for non EXR_RAW compression types
    1817         124 :     s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData));
    1818         124 :     if (!s->thread_data)
    1819           0 :         return AVERROR_INVALIDDATA;
    1820             : 
    1821         124 :     return 0;
    1822             : }
    1823             : 
    1824             : #if HAVE_THREADS
    1825           0 : static int decode_init_thread_copy(AVCodecContext *avctx)
    1826           0 : {    EXRContext *s = avctx->priv_data;
    1827             : 
    1828             :     // allocate thread data, used for non EXR_RAW compression types
    1829           0 :     s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData));
    1830           0 :     if (!s->thread_data)
    1831           0 :         return AVERROR_INVALIDDATA;
    1832             : 
    1833           0 :     return 0;
    1834             : }
    1835             : #endif
    1836             : 
    1837         124 : static av_cold int decode_end(AVCodecContext *avctx)
    1838             : {
    1839         124 :     EXRContext *s = avctx->priv_data;
    1840             :     int i;
    1841         248 :     for (i = 0; i < avctx->thread_count; i++) {
    1842         124 :         EXRThreadData *td = &s->thread_data[i];
    1843         124 :         av_freep(&td->uncompressed_data);
    1844         124 :         av_freep(&td->tmp);
    1845         124 :         av_freep(&td->bitmap);
    1846         124 :         av_freep(&td->lut);
    1847             :     }
    1848             : 
    1849         124 :     av_freep(&s->thread_data);
    1850         124 :     av_freep(&s->channels);
    1851             : 
    1852         124 :     return 0;
    1853             : }
    1854             : 
    1855             : #define OFFSET(x) offsetof(EXRContext, x)
    1856             : #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
    1857             : static const AVOption options[] = {
    1858             :     { "layer", "Set the decoding layer", OFFSET(layer),
    1859             :         AV_OPT_TYPE_STRING, { .str = "" }, 0, 0, VD },
    1860             :     { "gamma", "Set the float gamma value when decoding", OFFSET(gamma),
    1861             :         AV_OPT_TYPE_FLOAT, { .dbl = 1.0f }, 0.001, FLT_MAX, VD },
    1862             : 
    1863             :     // XXX: Note the abuse of the enum using AVCOL_TRC_UNSPECIFIED to subsume the existing gamma option
    1864             :     { "apply_trc", "color transfer characteristics to apply to EXR linear input", OFFSET(apply_trc_type),
    1865             :         AV_OPT_TYPE_INT, {.i64 = AVCOL_TRC_UNSPECIFIED }, 1, AVCOL_TRC_NB-1, VD, "apply_trc_type"},
    1866             :     { "bt709",        "BT.709",           0,
    1867             :         AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT709 },        INT_MIN, INT_MAX, VD, "apply_trc_type"},
    1868             :     { "gamma",        "gamma",            0,
    1869             :         AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_UNSPECIFIED },  INT_MIN, INT_MAX, VD, "apply_trc_type"},
    1870             :     { "gamma22",      "BT.470 M",         0,
    1871             :         AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_GAMMA22 },      INT_MIN, INT_MAX, VD, "apply_trc_type"},
    1872             :     { "gamma28",      "BT.470 BG",        0,
    1873             :         AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_GAMMA28 },      INT_MIN, INT_MAX, VD, "apply_trc_type"},
    1874             :     { "smpte170m",    "SMPTE 170 M",      0,
    1875             :         AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTE170M },    INT_MIN, INT_MAX, VD, "apply_trc_type"},
    1876             :     { "smpte240m",    "SMPTE 240 M",      0,
    1877             :         AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTE240M },    INT_MIN, INT_MAX, VD, "apply_trc_type"},
    1878             :     { "linear",       "Linear",           0,
    1879             :         AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LINEAR },       INT_MIN, INT_MAX, VD, "apply_trc_type"},
    1880             :     { "log",          "Log",              0,
    1881             :         AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LOG },          INT_MIN, INT_MAX, VD, "apply_trc_type"},
    1882             :     { "log_sqrt",     "Log square root",  0,
    1883             :         AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LOG_SQRT },     INT_MIN, INT_MAX, VD, "apply_trc_type"},
    1884             :     { "iec61966_2_4", "IEC 61966-2-4",    0,
    1885             :         AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_IEC61966_2_4 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
    1886             :     { "bt1361",       "BT.1361",          0,
    1887             :         AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT1361_ECG },   INT_MIN, INT_MAX, VD, "apply_trc_type"},
    1888             :     { "iec61966_2_1", "IEC 61966-2-1",    0,
    1889             :         AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_IEC61966_2_1 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
    1890             :     { "bt2020_10bit", "BT.2020 - 10 bit", 0,
    1891             :         AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT2020_10 },    INT_MIN, INT_MAX, VD, "apply_trc_type"},
    1892             :     { "bt2020_12bit", "BT.2020 - 12 bit", 0,
    1893             :         AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT2020_12 },    INT_MIN, INT_MAX, VD, "apply_trc_type"},
    1894             :     { "smpte2084",    "SMPTE ST 2084",    0,
    1895             :         AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTEST2084 },  INT_MIN, INT_MAX, VD, "apply_trc_type"},
    1896             :     { "smpte428_1",   "SMPTE ST 428-1",   0,
    1897             :         AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTEST428_1 }, INT_MIN, INT_MAX, VD, "apply_trc_type"},
    1898             : 
    1899             :     { NULL },
    1900             : };
    1901             : 
    1902             : static const AVClass exr_class = {
    1903             :     .class_name = "EXR",
    1904             :     .item_name  = av_default_item_name,
    1905             :     .option     = options,
    1906             :     .version    = LIBAVUTIL_VERSION_INT,
    1907             : };
    1908             : 
    1909             : AVCodec ff_exr_decoder = {
    1910             :     .name             = "exr",
    1911             :     .long_name        = NULL_IF_CONFIG_SMALL("OpenEXR image"),
    1912             :     .type             = AVMEDIA_TYPE_VIDEO,
    1913             :     .id               = AV_CODEC_ID_EXR,
    1914             :     .priv_data_size   = sizeof(EXRContext),
    1915             :     .init             = decode_init,
    1916             :     .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
    1917             :     .close            = decode_end,
    1918             :     .decode           = decode_frame,
    1919             :     .capabilities     = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS |
    1920             :                         AV_CODEC_CAP_SLICE_THREADS,
    1921             :     .priv_class       = &exr_class,
    1922             : };

Generated by: LCOV version 1.13