*
* For more information on the OpenEXR format, visit:
* http://openexr.com/
- *
- * exr_half2float() is credited to Aaftab Munshi, Dan Ginsburg, Dave Shreiner.
*/
#include <float.h>
#include "exrdsp.h"
#include "get_bits.h"
#include "internal.h"
+#include "half2float.h"
#include "mathops.h"
#include "thread.h"
EXR_PXR24,
EXR_B44,
EXR_B44A,
- EXR_DWA,
- EXR_DWB,
+ EXR_DWAA,
+ EXR_DWAB,
EXR_UNKN,
};
uint8_t *bitmap;
uint16_t *lut;
+ uint8_t *ac_data;
+ unsigned ac_size;
+
+ uint8_t *dc_data;
+ unsigned dc_size;
+
+ uint8_t *rle_data;
+ unsigned rle_size;
+
+ uint8_t *rle_raw_data;
+ unsigned rle_raw_size;
+
+ float block[3][64];
+
int ysize, xsize;
int channel_line_size;
enum AVColorTransferCharacteristic apply_trc_type;
float gamma;
union av_intfloat32 gamma_table[65536];
-} EXRContext;
-
-/* -15 stored using a single precision bias of 127 */
-#define HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP 0x38000000
-
-/* max exponent value in single precision that will be converted
- * to Inf or Nan when stored as a half-float */
-#define HALF_FLOAT_MAX_BIASED_EXP_AS_SINGLE_FP_EXP 0x47800000
-
-/* 255 is the max exponent biased value */
-#define FLOAT_MAX_BIASED_EXP (0xFF << 23)
-#define HALF_FLOAT_MAX_BIASED_EXP (0x1F << 10)
-
-/**
- * Convert a half float as a uint16_t into a full float.
- *
- * @param hf half float as uint16_t
- *
- * @return float value
- */
-static union av_intfloat32 exr_half2float(uint16_t hf)
-{
- unsigned int sign = (unsigned int) (hf >> 15);
- unsigned int mantissa = (unsigned int) (hf & ((1 << 10) - 1));
- unsigned int exp = (unsigned int) (hf & HALF_FLOAT_MAX_BIASED_EXP);
- union av_intfloat32 f;
-
- if (exp == HALF_FLOAT_MAX_BIASED_EXP) {
- // we have a half-float NaN or Inf
- // half-float NaNs will be converted to a single precision NaN
- // half-float Infs will be converted to a single precision Inf
- exp = FLOAT_MAX_BIASED_EXP;
- mantissa <<= 13; // preserve half-float NaN bits if set
- } else if (exp == 0x0) {
- // convert half-float zero/denorm to single precision value
- if (mantissa) {
- mantissa <<= 1;
- exp = HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP;
- // check for leading 1 in denorm mantissa
- while (!(mantissa & (1 << 10))) {
- // for every leading 0, decrement single precision exponent by 1
- // and shift half-float mantissa value to the left
- mantissa <<= 1;
- exp -= (1 << 23);
- }
- // clamp the mantissa to 10 bits
- mantissa &= ((1 << 10) - 1);
- // shift left to generate single-precision mantissa of 23 bits
- mantissa <<= 13;
- }
- } else {
- // shift left to generate single-precision mantissa of 23 bits
- mantissa <<= 13;
- // generate single precision biased exponent value
- exp = (exp << 13) + HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP;
- }
-
- f.i = (sign << 31) | exp | mantissa;
-
- return f;
-}
+ uint32_t mantissatable[2048];
+ uint32_t exponenttable[64];
+ uint16_t offsettable[64];
+} EXRContext;
static int zip_uncompress(EXRContext *s, const uint8_t *src, int compressed_size,
int uncompressed_size, EXRThreadData *td)
return 0;
}
-static int rle_uncompress(EXRContext *ctx, const uint8_t *src, int compressed_size,
- int uncompressed_size, EXRThreadData *td)
+static int rle(uint8_t *dst, const uint8_t *src,
+ int compressed_size, int uncompressed_size)
{
- uint8_t *d = td->tmp;
+ uint8_t *d = dst;
const int8_t *s = src;
int ssize = compressed_size;
int dsize = uncompressed_size;
if (dend != d)
return AVERROR_INVALIDDATA;
+ return 0;
+}
+
+static int rle_uncompress(EXRContext *ctx, const uint8_t *src, int compressed_size,
+ int uncompressed_size, EXRThreadData *td)
+{
+ rle(td->tmp, src, compressed_size, uncompressed_size);
+
av_assert1(uncompressed_size % 2 == 0);
ctx->dsp.predictor(td->tmp, uncompressed_size);
init_get_bits(&gbit, gb->buffer, nbits);
while (get_bits_left(&gbit) > 0 && oe < no) {
- uint16_t x = get_vlc2(&gbit, vlc->table, 12, 2);
+ uint16_t x = get_vlc2(&gbit, vlc->table, 12, 3);
if (x == run_sym) {
int run = get_bits(&gbit, 8);
- uint16_t fill = out[oe - 1];
+ uint16_t fill;
+
+ if (oe == 0 || oe + run > no)
+ return AVERROR_INVALIDDATA;
+
+ fill = out[oe - 1];
while (run-- > 0)
out[oe++] = fill;
GetByteContext *gb,
uint16_t *dst, int dst_size)
{
- int32_t src_size, im, iM;
+ int32_t im, iM;
uint32_t nBits;
int ret;
- src_size = bytestream2_get_le32(gb);
im = bytestream2_get_le32(gb);
iM = bytestream2_get_le32(gb);
bytestream2_skip(gb, 4);
nBits = bytestream2_get_le32(gb);
if (im < 0 || im >= HUF_ENCSIZE ||
- iM < 0 || iM >= HUF_ENCSIZE ||
- src_size < 0)
+ iM < 0 || iM >= HUF_ENCSIZE)
return AVERROR_INVALIDDATA;
bytestream2_skip(gb, 4);
maxval = reverse_lut(td->bitmap, td->lut);
+ bytestream2_skip(&gb, 4);
ret = huf_uncompress(s, td, &gb, tmp, dsize / sizeof(uint16_t));
if (ret)
return ret;
return 0;
}
+static int ac_uncompress(EXRContext *s, GetByteContext *gb, float *block)
+{
+ int ret = 0, n = 1;
+
+ while (n < 64) {
+ uint16_t val = bytestream2_get_ne16(gb);
+
+ if (val == 0xff00) {
+ n = 64;
+ } else if ((val >> 8) == 0xff) {
+ n += val & 0xff;
+ } else {
+ ret = n;
+ block[ff_zigzag_direct[n]] = av_int2float(half2float(val,
+ s->mantissatable,
+ s->exponenttable,
+ s->offsettable));
+ n++;
+ }
+ }
+
+ return ret;
+}
+
+static void idct_1d(float *blk, int step)
+{
+ const float a = .5f * cosf( M_PI / 4.f);
+ const float b = .5f * cosf( M_PI / 16.f);
+ const float c = .5f * cosf( M_PI / 8.f);
+ const float d = .5f * cosf(3.f*M_PI / 16.f);
+ const float e = .5f * cosf(5.f*M_PI / 16.f);
+ const float f = .5f * cosf(3.f*M_PI / 8.f);
+ const float g = .5f * cosf(7.f*M_PI / 16.f);
+
+ float alpha[4], beta[4], theta[4], gamma[4];
+
+ alpha[0] = c * blk[2 * step];
+ alpha[1] = f * blk[2 * step];
+ alpha[2] = c * blk[6 * step];
+ alpha[3] = f * blk[6 * step];
+
+ beta[0] = b * blk[1 * step] + d * blk[3 * step] + e * blk[5 * step] + g * blk[7 * step];
+ beta[1] = d * blk[1 * step] - g * blk[3 * step] - b * blk[5 * step] - e * blk[7 * step];
+ beta[2] = e * blk[1 * step] - b * blk[3 * step] + g * blk[5 * step] + d * blk[7 * step];
+ beta[3] = g * blk[1 * step] - e * blk[3 * step] + d * blk[5 * step] - b * blk[7 * step];
+
+ theta[0] = a * (blk[0 * step] + blk[4 * step]);
+ theta[3] = a * (blk[0 * step] - blk[4 * step]);
+
+ theta[1] = alpha[0] + alpha[3];
+ theta[2] = alpha[1] - alpha[2];
+
+ gamma[0] = theta[0] + theta[1];
+ gamma[1] = theta[3] + theta[2];
+ gamma[2] = theta[3] - theta[2];
+ gamma[3] = theta[0] - theta[1];
+
+ blk[0 * step] = gamma[0] + beta[0];
+ blk[1 * step] = gamma[1] + beta[1];
+ blk[2 * step] = gamma[2] + beta[2];
+ blk[3 * step] = gamma[3] + beta[3];
+
+ blk[4 * step] = gamma[3] - beta[3];
+ blk[5 * step] = gamma[2] - beta[2];
+ blk[6 * step] = gamma[1] - beta[1];
+ blk[7 * step] = gamma[0] - beta[0];
+}
+
+static void dct_inverse(float *block)
+{
+ for (int i = 0; i < 8; i++)
+ idct_1d(block + i, 8);
+
+ for (int i = 0; i < 8; i++) {
+ idct_1d(block, 1);
+ block += 8;
+ }
+}
+
+static void convert(float y, float u, float v,
+ float *b, float *g, float *r)
+{
+ *r = y + 1.5747f * v;
+ *g = y - 0.1873f * u - 0.4682f * v;
+ *b = y + 1.8556f * u;
+}
+
+static float to_linear(float x, float scale)
+{
+ float ax = fabsf(x);
+
+ if (ax <= 1.f) {
+ return FFSIGN(x) * powf(ax, 2.2f * scale);
+ } else {
+ const float log_base = expf(2.2f * scale);
+
+ return FFSIGN(x) * powf(log_base, ax - 1.f);
+ }
+}
+
+static int dwa_uncompress(EXRContext *s, const uint8_t *src, int compressed_size,
+ int uncompressed_size, EXRThreadData *td)
+{
+ int64_t version, lo_usize, lo_size;
+ int64_t ac_size, dc_size, rle_usize, rle_csize, rle_raw_size;
+ int64_t ac_count, dc_count, ac_compression;
+ const int dc_w = td->xsize >> 3;
+ const int dc_h = td->ysize >> 3;
+ GetByteContext gb, agb;
+ int skip, ret;
+
+ if (compressed_size <= 88)
+ return AVERROR_INVALIDDATA;
+
+ version = AV_RL64(src + 0);
+ if (version != 2)
+ return AVERROR_INVALIDDATA;
+
+ lo_usize = AV_RL64(src + 8);
+ lo_size = AV_RL64(src + 16);
+ ac_size = AV_RL64(src + 24);
+ dc_size = AV_RL64(src + 32);
+ rle_csize = AV_RL64(src + 40);
+ rle_usize = AV_RL64(src + 48);
+ rle_raw_size = AV_RL64(src + 56);
+ ac_count = AV_RL64(src + 64);
+ dc_count = AV_RL64(src + 72);
+ ac_compression = AV_RL64(src + 80);
+
+ if (compressed_size < 88LL + lo_size + ac_size + dc_size + rle_csize)
+ return AVERROR_INVALIDDATA;
+
+ bytestream2_init(&gb, src + 88, compressed_size - 88);
+ skip = bytestream2_get_le16(&gb);
+ if (skip < 2)
+ return AVERROR_INVALIDDATA;
+
+ bytestream2_skip(&gb, skip - 2);
+
+ if (lo_size > 0) {
+ if (lo_usize > uncompressed_size)
+ return AVERROR_INVALIDDATA;
+ bytestream2_skip(&gb, lo_size);
+ }
+
+ if (ac_size > 0) {
+ unsigned long dest_len = ac_count * 2LL;
+ GetByteContext agb = gb;
+
+ if (ac_count > 3LL * td->xsize * s->scan_lines_per_block)
+ return AVERROR_INVALIDDATA;
+
+ av_fast_padded_malloc(&td->ac_data, &td->ac_size, dest_len);
+ if (!td->ac_data)
+ return AVERROR(ENOMEM);
+
+ switch (ac_compression) {
+ case 0:
+ ret = huf_uncompress(s, td, &agb, (int16_t *)td->ac_data, ac_count);
+ if (ret < 0)
+ return ret;
+ break;
+ case 1:
+ if (uncompress(td->ac_data, &dest_len, agb.buffer, ac_size) != Z_OK ||
+ dest_len != ac_count * 2LL)
+ return AVERROR_INVALIDDATA;
+ break;
+ default:
+ return AVERROR_INVALIDDATA;
+ }
+
+ bytestream2_skip(&gb, ac_size);
+ }
+
+ if (dc_size > 0) {
+ unsigned long dest_len = dc_count * 2LL;
+ GetByteContext agb = gb;
+
+ if (dc_count > (6LL * td->xsize * td->ysize + 63) / 64)
+ return AVERROR_INVALIDDATA;
+
+ av_fast_padded_malloc(&td->dc_data, &td->dc_size, FFALIGN(dest_len, 64) * 2);
+ if (!td->dc_data)
+ return AVERROR(ENOMEM);
+
+ if (uncompress(td->dc_data + FFALIGN(dest_len, 64), &dest_len, agb.buffer, dc_size) != Z_OK ||
+ (dest_len != dc_count * 2LL))
+ return AVERROR_INVALIDDATA;
+
+ s->dsp.predictor(td->dc_data + FFALIGN(dest_len, 64), dest_len);
+ s->dsp.reorder_pixels(td->dc_data, td->dc_data + FFALIGN(dest_len, 64), dest_len);
+
+ bytestream2_skip(&gb, dc_size);
+ }
+
+ if (rle_raw_size > 0 && rle_csize > 0 && rle_usize > 0) {
+ unsigned long dest_len = rle_usize;
+
+ av_fast_padded_malloc(&td->rle_data, &td->rle_size, rle_usize);
+ if (!td->rle_data)
+ return AVERROR(ENOMEM);
+
+ av_fast_padded_malloc(&td->rle_raw_data, &td->rle_raw_size, rle_raw_size);
+ if (!td->rle_raw_data)
+ return AVERROR(ENOMEM);
+
+ if (uncompress(td->rle_data, &dest_len, gb.buffer, rle_csize) != Z_OK ||
+ (dest_len != rle_usize))
+ return AVERROR_INVALIDDATA;
+
+ ret = rle(td->rle_raw_data, td->rle_data, rle_usize, rle_raw_size);
+ if (ret < 0)
+ return ret;
+ bytestream2_skip(&gb, rle_csize);
+ }
+
+ bytestream2_init(&agb, td->ac_data, ac_count * 2);
+
+ for (int y = 0; y < td->ysize; y += 8) {
+ for (int x = 0; x < td->xsize; x += 8) {
+ memset(td->block, 0, sizeof(td->block));
+
+ for (int j = 0; j < 3; j++) {
+ float *block = td->block[j];
+ const int idx = (x >> 3) + (y >> 3) * dc_w + dc_w * dc_h * j;
+ uint16_t *dc = (uint16_t *)td->dc_data;
+ union av_intfloat32 dc_val;
+
+ dc_val.i = half2float(dc[idx], s->mantissatable,
+ s->exponenttable, s->offsettable);
+
+ block[0] = dc_val.f;
+ ac_uncompress(s, &agb, block);
+ dct_inverse(block);
+ }
+
+ {
+ const float scale = s->pixel_type == EXR_FLOAT ? 2.f : 1.f;
+ const int o = s->nb_channels == 4;
+ float *bo = ((float *)td->uncompressed_data) +
+ y * td->xsize * s->nb_channels + td->xsize * (o + 0) + x;
+ float *go = ((float *)td->uncompressed_data) +
+ y * td->xsize * s->nb_channels + td->xsize * (o + 1) + x;
+ float *ro = ((float *)td->uncompressed_data) +
+ y * td->xsize * s->nb_channels + td->xsize * (o + 2) + x;
+ float *yb = td->block[0];
+ float *ub = td->block[1];
+ float *vb = td->block[2];
+
+ for (int yy = 0; yy < 8; yy++) {
+ for (int xx = 0; xx < 8; xx++) {
+ const int idx = xx + yy * 8;
+
+ convert(yb[idx], ub[idx], vb[idx], &bo[xx], &go[xx], &ro[xx]);
+
+ bo[xx] = to_linear(bo[xx], scale);
+ go[xx] = to_linear(go[xx], scale);
+ ro[xx] = to_linear(ro[xx], scale);
+ }
+
+ bo += td->xsize * s->nb_channels;
+ go += td->xsize * s->nb_channels;
+ ro += td->xsize * s->nb_channels;
+ }
+ }
+ }
+ }
+
+ if (s->nb_channels < 4)
+ return 0;
+
+ for (int y = 0; y < td->ysize && td->rle_raw_data; y++) {
+ uint32_t *ao = ((uint32_t *)td->uncompressed_data) + y * td->xsize * s->nb_channels;
+ uint8_t *ai0 = td->rle_raw_data + y * td->xsize;
+ uint8_t *ai1 = td->rle_raw_data + y * td->xsize + rle_raw_size / 2;
+
+ for (int x = 0; x < td->xsize; x++) {
+ uint16_t ha = ai0[x] | (ai1[x] << 8);
+
+ ao[x] = half2float(ha, s->mantissatable, s->exponenttable, s->offsettable);
+ }
+ }
+
+ return 0;
+}
+
static int decode_block(AVCodecContext *avctx, void *tdata,
int jobnr, int threadnr)
{
return AVERROR_PATCHWELCOME;
}
+ if (tile_x && s->tile_attr.xSize + (int64_t)FFMAX(s->xmin, 0) >= INT_MAX / tile_x )
+ return AVERROR_INVALIDDATA;
+ if (tile_y && s->tile_attr.ySize + (int64_t)FFMAX(s->ymin, 0) >= INT_MAX / tile_y )
+ return AVERROR_INVALIDDATA;
+
line = s->ymin + s->tile_attr.ySize * tile_y;
col = s->tile_attr.xSize * tile_x;
case EXR_B44A:
ret = b44_uncompress(s, src, data_size, uncompressed_size, td);
break;
+ case EXR_DWAA:
+ case EXR_DWAB:
+ ret = dwa_uncompress(s, src, data_size, uncompressed_size, td);
+ break;
}
if (ret < 0) {
av_log(avctx, AV_LOG_ERROR, "decode_block() failed.\n");
channel_buffer[3] = src + (td->xsize * s->channel_offsets[3]) + data_window_offset;
if (s->desc->flags & AV_PIX_FMT_FLAG_FLOAT) {
-
/* todo: change this when a floating point pixel format with luma with alpha is implemented */
int channel_count = s->channel_offsets[3] >= 0 ? 4 : rgb_channel_count;
if (s->is_luma) {
memset(ptr_x, 0, bxmin);
ptr_x += window_xoffset;
- if (s->pixel_type == EXR_FLOAT) {
+ if (s->pixel_type == EXR_FLOAT ||
+ s->compression == EXR_DWAA ||
+ s->compression == EXR_DWAB) {
// 32-bit
union av_intfloat32 t;
if (trc_func && c < 3) {
}
} else {
for (x = 0; x < xsize; x++) {
- *ptr_x++ = exr_half2float(bytestream_get_le16(&src));
+ ptr_x[0].i = half2float(bytestream_get_le16(&src),
+ s->mantissatable,
+ s->exponenttable,
+ s->offsettable);
+ ptr_x++;
}
}
}
if ((ret = decode_header(s, picture)) < 0)
return ret;
+ if ((s->compression == EXR_DWAA || s->compression == EXR_DWAB) &&
+ s->pixel_type == EXR_HALF) {
+ s->current_channel_offset *= 2;
+ for (int i = 0; i < 4; i++)
+ s->channel_offsets[i] *= 2;
+ }
+
switch (s->pixel_type) {
case EXR_FLOAT:
case EXR_HALF:
case EXR_PIZ:
case EXR_B44:
case EXR_B44A:
+ case EXR_DWAA:
s->scan_lines_per_block = 32;
break;
+ case EXR_DWAB:
+ s->scan_lines_per_block = 256;
+ break;
default:
avpriv_report_missing_feature(avctx, "Compression %d", s->compression);
return AVERROR_PATCHWELCOME;
float one_gamma = 1.0f / s->gamma;
avpriv_trc_function trc_func = NULL;
+ half2float_table(s->mantissatable, s->exponenttable, s->offsettable);
+
s->avctx = avctx;
ff_exrdsp_init(&s->dsp);
trc_func = avpriv_get_trc_function_from_trc(s->apply_trc_type);
if (trc_func) {
for (i = 0; i < 65536; ++i) {
- t = exr_half2float(i);
+ t.i = half2float(i, s->mantissatable, s->exponenttable, s->offsettable);
t.f = trc_func(t.f);
s->gamma_table[i] = t;
}
} else {
if (one_gamma > 0.9999f && one_gamma < 1.0001f) {
for (i = 0; i < 65536; ++i) {
- s->gamma_table[i] = exr_half2float(i);
+ s->gamma_table[i].i = half2float(i, s->mantissatable, s->exponenttable, s->offsettable);
}
} else {
for (i = 0; i < 65536; ++i) {
- t = exr_half2float(i);
+ t.i = half2float(i, s->mantissatable, s->exponenttable, s->offsettable);
/* If negative value we reuse half value */
if (t.f <= 0.0f) {
s->gamma_table[i] = t;
// allocate thread data, used for non EXR_RAW compression types
s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData));
if (!s->thread_data)
- return AVERROR_INVALIDDATA;
+ return AVERROR(ENOMEM);
return 0;
}
av_freep(&td->lut);
av_freep(&td->he);
av_freep(&td->freq);
+ av_freep(&td->ac_data);
+ av_freep(&td->dc_data);
+ av_freep(&td->rle_data);
+ av_freep(&td->rle_raw_data);
ff_free_vlc(&td->vlc);
}