121 #define HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP 0x38000000
124 #define HALF_FLOAT_MAX_BIASED_EXP_AS_SINGLE_FP_EXP 0x47800000
127 #define FLOAT_MAX_BIASED_EXP (0xFF << 23)
129 #define HALF_FLOAT_MAX_BIASED_EXP (0x1F << 10)
140 unsigned int sign = (
unsigned int)(hf >> 15);
141 unsigned int mantissa = (
unsigned int)(hf & ((1 << 10) - 1));
145 if (exp == HALF_FLOAT_MAX_BIASED_EXP) {
151 mantissa = (1 << 23) - 1;
152 }
else if (exp == 0x0) {
158 while ((mantissa & (1 << 10))) {
165 mantissa &= ((1 << 10) - 1);
176 f.
i = (sign << 31) | exp | mantissa;
191 unsigned int exp = v >> 23;
194 if (exp <= 127 + 7 - 24)
199 return (v + (1 << 23)) >> (127 + 7 - exp);
211 unsigned exp = 14 - (v >> 10);
216 return (v & 0x8000) ? 0 : 0xffff;
219 return (v + (1 << 16)) >> (exp + 1);
228 int d = (int) t[-1] + (
int) t[0] - 128;
236 const int8_t *
t1 =
src;
237 const int8_t *
t2 = src + (size + 1) / 2;
239 int8_t *stop = s +
size;
257 unsigned long dest_len = uncompressed_size;
259 if (uncompress(td->
tmp, &dest_len, src, compressed_size) != Z_OK ||
260 dest_len != uncompressed_size)
273 const int8_t *
s =
src;
274 int ssize = compressed_size;
275 int dsize = uncompressed_size;
285 if ((dsize -= count) < 0 ||
286 (ssize -= count + 1) < 0)
294 if ((dsize -= count) < 0 ||
314 #define USHORT_RANGE (1 << 16)
315 #define BITMAP_SIZE (1 << 13)
322 if ((i == 0) || (bitmap[i >> 3] & (1 << (i & 7))))
327 memset(lut + k, 0, (USHORT_RANGE - k) * 2);
332 static void apply_lut(
const uint16_t *lut, uint16_t *dst,
int dsize)
336 for (i = 0; i < dsize; ++
i)
337 dst[i] = lut[dst[i]];
340 #define HUF_ENCBITS 16 // literal (value) bit length
341 #define HUF_DECBITS 14 // decoding bit size (>= 8)
343 #define HUF_ENCSIZE ((1 << HUF_ENCBITS) + 1) // encoding table size
344 #define HUF_DECSIZE (1 << HUF_DECBITS) // decoding table size
345 #define HUF_DECMASK (HUF_DECSIZE - 1)
355 uint64_t
c,
n[59] = { 0 };
362 for (i = 58; i > 0; --i) {
363 uint64_t nc = ((c + n[i]) >> 1);
372 hcode[i] = l | (n[l]++ << 6);
376 #define SHORT_ZEROCODE_RUN 59
377 #define LONG_ZEROCODE_RUN 63
378 #define SHORTEST_LONG_RUN (2 + LONG_ZEROCODE_RUN - SHORT_ZEROCODE_RUN)
379 #define LONGEST_LONG_RUN (255 + SHORTEST_LONG_RUN)
389 for (; im <= iM; im++) {
395 if (im + zerun > iM + 1)
405 if (im + zerun > iM + 1)
424 for (; im <= iM; im++) {
425 uint64_t
c = hcode[
im] >> 6;
426 int i, l = hcode[
im] & 63;
446 for (i = 1 << (
HUF_DECBITS - l); i > 0; i--, pl++) {
447 if (pl->
len || pl->
p)
458 #define get_char(c, lc, gb) \
460 c = (c << 8) | bytestream2_get_byte(gb); \
464 #define get_code(po, rlc, c, lc, gb, out, oe) \
468 get_char(c, lc, gb); \
474 return AVERROR_INVALIDDATA; \
480 } else if (out < oe) { \
483 return AVERROR_INVALIDDATA; \
489 int rlc,
int no, uint16_t *
out)
492 uint16_t *outb =
out;
493 uint16_t *oe = out + no;
513 for (j = 0; j < pl.
lit; j++) {
514 int l = hcode[pl.
p[j]] & 63;
520 if ((hcode[pl.
p[j]] >> 6) ==
521 ((c >> (lc - l)) & ((1LL << l) - 1))) {
523 get_code(pl.
p[j], rlc, c, lc, gb, out, oe);
550 if (out - outb != no)
556 uint16_t *dst,
int dst_size)
564 src_size = bytestream2_get_le32(gb);
565 im = bytestream2_get_le32(gb);
566 iM = bytestream2_get_le32(gb);
568 nBits = bytestream2_get_le32(gb);
578 if (!freq || !hdec) {
593 ret =
huf_decode(freq, hdec, gb, nBits, iM, dst_size, dst);
606 static inline void wdec14(uint16_t l, uint16_t h, uint16_t *
a, uint16_t *
b)
611 int ai = ls + (hi & 1) + (hi >> 1);
613 int16_t bs = ai - hi;
620 #define A_OFFSET (1 << (NBITS - 1))
621 #define MOD_MASK ((1 << NBITS) - 1)
623 static inline void wdec16(uint16_t l, uint16_t h, uint16_t *
a, uint16_t *
b)
634 int ny,
int oy, uint16_t mx)
636 int w14 = (mx < (1 << 14));
637 int n = (nx > ny) ? ny : nx;
650 uint16_t *ey = in + oy * (ny - p2);
651 uint16_t i00, i01, i10, i11;
657 for (; py <= ey; py += oy2) {
659 uint16_t *ex = py + ox * (nx - p2);
661 for (; px <= ex; px += ox2) {
662 uint16_t *p01 = px + ox1;
663 uint16_t *p10 = px + oy1;
664 uint16_t *p11 = p10 + ox1;
667 wdec14(*px, *p10, &i00, &i10);
668 wdec14(*p01, *p11, &i01, &i11);
669 wdec14(i00, i01, px, p01);
670 wdec14(i10, i11, p10, p11);
672 wdec16(*px, *p10, &i00, &i10);
673 wdec16(*p01, *p11, &i01, &i11);
674 wdec16(i00, i01, px, p01);
675 wdec16(i10, i11, p10, p11);
680 uint16_t *p10 = px + oy1;
683 wdec14(*px, *p10, &i00, p10);
685 wdec16(*px, *p10, &i00, p10);
693 uint16_t *ex = py + ox * (nx - p2);
695 for (; px <= ex; px += ox2) {
696 uint16_t *p01 = px + ox1;
699 wdec14(*px, *p01, &i00, p01);
701 wdec16(*px, *p01, &i00, p01);
716 uint16_t maxval, min_non_zero, max_non_zero;
718 uint16_t *tmp = (uint16_t *)td->
tmp;
733 min_non_zero = bytestream2_get_le16(&gb);
734 max_non_zero = bytestream2_get_le16(&gb);
740 if (min_non_zero <= max_non_zero)
742 max_non_zero - min_non_zero + 1);
756 for (j = 0; j <
size; j++)
758 s->
xdelta * size, maxval);
765 for (i = 0; i < s->
ysize; i++)
776 int compressed_size,
int uncompressed_size,
779 unsigned long dest_len = uncompressed_size;
784 if (uncompress(td->
tmp, &dest_len, src, compressed_size) != Z_OK ||
785 dest_len != uncompressed_size)
789 for (i = 0; i < s->
ysize; i++)
798 ptr[1] = ptr[0] + s->
xdelta;
799 ptr[2] = ptr[1] + s->
xdelta;
802 for (j = 0; j < s->
xdelta; ++j) {
803 uint32_t diff = (*(ptr[0]++) << 24) |
804 (*(ptr[1]++) << 16) |
807 bytestream_put_le32(&out, pixel);
812 ptr[1] = ptr[0] + s->
xdelta;
814 for (j = 0; j < s->
xdelta; j++) {
815 uint32_t diff = (*(ptr[0]++) << 8) | *(ptr[1]++);
818 bytestream_put_le16(&out, pixel);
830 int jobnr,
int threadnr)
835 const uint8_t *channel_buffer[4] = { 0 };
837 uint64_t line_offset, uncompressed_size;
838 uint32_t xdelta = s->
xdelta;
841 uint32_t data_size,
line;
847 float one_gamma = 1.0f / s->
gamma;
851 if (line_offset > buf_size - 8)
854 src = buf + line_offset + 8;
860 if (data_size <= 0 || data_size > buf_size)
866 line_offset > buf_size - uncompressed_size)) ||
868 line_offset > buf_size - data_size))) {
872 if (data_size < uncompressed_size) {
913 r = channel_buffer[0];
914 g = channel_buffer[1];
915 b = channel_buffer[2];
916 if (channel_buffer[3])
917 a = channel_buffer[3];
919 ptr_x = (uint16_t *) ptr;
922 memset(ptr_x, 0, bxmin);
926 for (x = 0; x < xdelta; x++) {
928 t.
i = bytestream_get_le32(&r);
930 t.
f =
powf(t.
f, one_gamma);
933 t.
i = bytestream_get_le32(&g);
935 t.
f =
powf(t.
f, one_gamma);
938 t.
i = bytestream_get_le32(&b);
940 t.
f =
powf(t.
f, one_gamma);
942 if (channel_buffer[3])
947 for (x = 0; x < xdelta; x++) {
948 *ptr_x++ = s->
gamma_table[bytestream_get_le16(&r)];
949 *ptr_x++ = s->
gamma_table[bytestream_get_le16(&g)];
950 *ptr_x++ = s->
gamma_table[bytestream_get_le16(&b)];
951 if (channel_buffer[3])
957 memset(ptr_x, 0, axmax);
962 if (channel_buffer[3])
982 const char *value_name,
983 const char *value_type,
984 unsigned int minimum_length)
989 !strcmp(s->
gb.
buffer, value_name)) {
991 s->
gb.
buffer += strlen(value_name) + 1;
992 if (!strcmp(s->
gb.
buffer, value_type)) {
993 s->
gb.
buffer += strlen(value_type) + 1;
994 var_size = bytestream2_get_le32(&s->
gb);
1000 s->
gb.
buffer -= strlen(value_name) + 1;
1002 "Unknown data type %s for header variable %s.\n",
1003 value_type, value_name);
1012 int current_channel_offset = 0;
1020 magic_number = bytestream2_get_le32(&s->
gb);
1021 if (magic_number != 20000630) {
1028 version = bytestream2_get_byte(&s->
gb);
1034 flags = bytestream2_get_le24(&s->
gb);
1044 "chlist", 38)) >= 0) {
1054 int channel_index = -1;
1057 if (strcmp(s->
layer,
"") != 0) {
1060 if (*ch_gb.
buffer ==
'.')
1063 "Layer %s.%s matched.\n", s->
layer, ch_gb.
buffer);
1067 if (!strcmp(ch_gb.
buffer,
"R") ||
1068 !strcmp(ch_gb.
buffer,
"X") ||
1069 !strcmp(ch_gb.
buffer,
"U"))
1071 else if (!strcmp(ch_gb.
buffer,
"G") ||
1072 !strcmp(ch_gb.
buffer,
"Y") ||
1073 !strcmp(ch_gb.
buffer,
"V"))
1075 else if (!strcmp(ch_gb.
buffer,
"B") ||
1076 !strcmp(ch_gb.
buffer,
"Z") ||
1077 !strcmp(ch_gb.
buffer,
"W"))
1079 else if (!strcmp(ch_gb.
buffer,
"A"))
1083 "Unsupported channel %.256s.\n", ch_gb.
buffer);
1087 bytestream2_get_byte(&ch_gb))
1095 current_pixel_type = bytestream2_get_le32(&ch_gb);
1099 current_pixel_type);
1104 xsub = bytestream2_get_le32(&ch_gb);
1105 ysub = bytestream2_get_le32(&ch_gb);
1106 if (xsub != 1 || ysub != 1) {
1108 "Subsampling %dx%d",
1113 if (channel_index >= 0) {
1117 "RGB channels not of the same depth.\n");
1130 channel->
xsub = xsub;
1131 channel->
ysub = ysub;
1133 current_channel_offset += 1 << current_pixel_type;
1158 s->
xmin = bytestream2_get_le32(&s->
gb);
1159 s->
ymin = bytestream2_get_le32(&s->
gb);
1160 s->
xmax = bytestream2_get_le32(&s->
gb);
1161 s->
ymax = bytestream2_get_le32(&s->
gb);
1167 "box2i", 34)) >= 0) {
1172 s->
w = bytestream2_get_le32(&s->
gb) + 1;
1173 s->
h = bytestream2_get_le32(&s->
gb) + 1;
1177 "lineOrder", 25)) >= 0) {
1182 line_order = bytestream2_get_byte(&s->
gb);
1184 if (line_order > 2) {
1191 "float", 31)) >= 0) {
1200 "compression", 29)) >= 0) {
1208 "Found more than one compression attribute.\n");
1220 for (i = 0; i < 2; i++)
1221 while (bytestream2_get_byte(&s->
gb) != 0);
1253 int scan_line_blocks;
1324 ptr = picture->
data[0];
1327 for (y = 0; y < s->
ymin; y++) {
1328 memset(ptr, 0, out_line_size);
1336 for (y = s->
ymax + 1; y < avctx->
height; y++) {
1337 memset(ptr, 0, out_line_size);
1352 float one_gamma = 1.0f / s->gamma;
1361 s->channel_offsets[0] = -1;
1362 s->channel_offsets[1] = -1;
1363 s->channel_offsets[2] = -1;
1364 s->channel_offsets[3] = -1;
1371 if ( one_gamma > 0.9999
f && one_gamma < 1.0001
f ) {
1372 for ( i = 0; i < 65536; ++
i ) {
1376 for ( i = 0; i < 65536; ++
i ) {
1379 if ( t.
f <= 0.0f ) {
1382 t.
f =
powf(t.
f, one_gamma);
1390 if (!s->thread_data)
1425 #define OFFSET(x) offsetof(EXRContext, x)
1426 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1428 {
"layer",
"Set the decoding layer",
OFFSET(layer),
1430 {
"gamma",
"Set the float gamma value when decoding (experimental/unsupported)",
OFFSET(gamma),