118 #define HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP 0x38000000
122 #define HALF_FLOAT_MAX_BIASED_EXP_AS_SINGLE_FP_EXP 0x47800000
125 #define FLOAT_MAX_BIASED_EXP (0xFF << 23)
127 #define HALF_FLOAT_MAX_BIASED_EXP (0x1F << 10)
138 unsigned int sign = (
unsigned int) (hf >> 15);
139 unsigned int mantissa = (
unsigned int) (hf & ((1 << 10) - 1));
143 if (exp == HALF_FLOAT_MAX_BIASED_EXP) {
149 mantissa = (1 << 23) - 1;
150 }
else if (exp == 0x0) {
156 while ((mantissa & (1 << 10))) {
163 mantissa &= ((1 << 10) - 1);
174 f.
i = (sign << 31) | exp | mantissa;
189 unsigned int exp = v >> 23;
192 if (exp <= 127 + 7 - 24)
197 return (v + (1 << 23)) >> (127 + 7 - exp);
209 unsigned exp = 14 - (v >> 10);
214 return (v & 0x8000) ? 0 : 0xffff;
217 return (v + (1 << 16)) >> (exp + 1);
226 int d = (int) t[-1] + (
int) t[0] - 128;
234 const int8_t *
t1 =
src;
235 const int8_t *
t2 = src + (size + 1) / 2;
237 int8_t *stop = s +
size;
255 unsigned long dest_len = uncompressed_size;
257 if (uncompress(td->
tmp, &dest_len, src, compressed_size) != Z_OK ||
258 dest_len != uncompressed_size)
271 const int8_t *
s =
src;
272 int ssize = compressed_size;
273 int dsize = uncompressed_size;
283 if ((dsize -= count) < 0 ||
284 (ssize -= count + 1) < 0)
292 if ((dsize -= count) < 0 ||
312 #define USHORT_RANGE (1 << 16)
313 #define BITMAP_SIZE (1 << 13)
320 if ((i == 0) || (bitmap[i >> 3] & (1 << (i & 7))))
325 memset(lut + k * 2, 0, (USHORT_RANGE - k) * 2);
330 static void apply_lut(
const uint16_t *lut, uint16_t *dst,
int dsize)
334 for (i = 0; i < dsize; ++
i)
335 dst[i] = lut[dst[i]];
338 #define HUF_ENCBITS 16 // literal (value) bit length
339 #define HUF_DECBITS 14 // decoding bit size (>= 8)
341 #define HUF_ENCSIZE ((1 << HUF_ENCBITS) + 1) // encoding table size
342 #define HUF_DECSIZE (1 << HUF_DECBITS) // decoding table size
343 #define HUF_DECMASK (HUF_DECSIZE - 1)
353 uint64_t
c,
n[59] = { 0 };
360 for (i = 58; i > 0; --i) {
361 uint64_t nc = ((c + n[i]) >> 1);
370 hcode[i] = l | (n[l]++ << 6);
374 #define SHORT_ZEROCODE_RUN 59
375 #define LONG_ZEROCODE_RUN 63
376 #define SHORTEST_LONG_RUN (2 + LONG_ZEROCODE_RUN - SHORT_ZEROCODE_RUN)
377 #define LONGEST_LONG_RUN (255 + SHORTEST_LONG_RUN)
387 for (; im <= iM; im++) {
393 if (im + zerun > iM + 1)
403 if (im + zerun > iM + 1)
422 for (; im <= iM; im++) {
423 uint64_t
c = hcode[
im] >> 6;
424 int i, l = hcode[
im] & 63;
444 for (i = 1 << (
HUF_DECBITS - l); i > 0; i--, pl++) {
445 if (pl->
len || pl->
p)
456 #define get_char(c, lc, gb) \
458 c = (c << 8) | bytestream2_get_byte(gb); \
462 #define get_code(po, rlc, c, lc, gb, out, oe) \
466 get_char(c, lc, gb); \
472 return AVERROR_INVALIDDATA; \
478 } else if (out < oe) { \
481 return AVERROR_INVALIDDATA; \
487 int rlc,
int no, uint16_t *
out)
490 uint16_t *outb =
out;
491 uint16_t *oe = out + no;
511 for (j = 0; j < pl.
lit; j++) {
512 int l = hcode[pl.
p[j]] & 63;
518 if ((hcode[pl.
p[j]] >> 6) ==
519 ((c >> (lc - l)) & ((1LL << l) - 1))) {
521 get_code(pl.
p[j], rlc, c, lc, gb, out, oe);
548 if (out - outb != no)
554 uint16_t *dst,
int dst_size)
562 src_size = bytestream2_get_le32(gb);
563 im = bytestream2_get_le32(gb);
564 iM = bytestream2_get_le32(gb);
566 nBits = bytestream2_get_le32(gb);
576 if (!freq || !hdec) {
591 ret =
huf_decode(freq, hdec, gb, nBits, iM, dst_size, dst);
604 static inline void wdec14(uint16_t l, uint16_t h, uint16_t *
a, uint16_t *
b)
609 int ai = ls + (hi & 1) + (hi >> 1);
611 int16_t bs = ai - hi;
618 #define A_OFFSET (1 << (NBITS - 1))
619 #define MOD_MASK ((1 << NBITS) - 1)
621 static inline void wdec16(uint16_t l, uint16_t h, uint16_t *
a, uint16_t *
b)
632 int ny,
int oy, uint16_t mx)
634 int w14 = (mx < (1 << 14));
635 int n = (nx > ny) ? ny : nx;
648 uint16_t *ey = in + oy * (ny - p2);
649 uint16_t i00, i01, i10, i11;
655 for (; py <= ey; py += oy2) {
657 uint16_t *ex = py + ox * (nx - p2);
659 for (; px <= ex; px += ox2) {
660 uint16_t *p01 = px + ox1;
661 uint16_t *p10 = px + oy1;
662 uint16_t *p11 = p10 + ox1;
665 wdec14(*px, *p10, &i00, &i10);
666 wdec14(*p01, *p11, &i01, &i11);
667 wdec14(i00, i01, px, p01);
668 wdec14(i10, i11, p10, p11);
670 wdec16(*px, *p10, &i00, &i10);
671 wdec16(*p01, *p11, &i01, &i11);
672 wdec16(i00, i01, px, p01);
673 wdec16(i10, i11, p10, p11);
678 uint16_t *p10 = px + oy1;
681 wdec14(*px, *p10, &i00, p10);
683 wdec16(*px, *p10, &i00, p10);
691 uint16_t *ex = py + ox * (nx - p2);
693 for (; px <= ex; px += ox2) {
694 uint16_t *p01 = px + ox1;
697 wdec14(*px, *p01, &i00, p01);
699 wdec16(*px, *p01, &i00, p01);
714 uint16_t maxval, min_non_zero, max_non_zero;
716 uint16_t *tmp = (uint16_t *)td->
tmp;
731 min_non_zero = bytestream2_get_le16(&gb);
732 max_non_zero = bytestream2_get_le16(&gb);
738 if (min_non_zero <= max_non_zero)
740 max_non_zero - min_non_zero + 1);
754 for (j = 0; j <
size; j++)
756 s->
xdelta * size, maxval);
763 for (i = 0; i < s->
ysize; i++)
774 int compressed_size,
int uncompressed_size,
777 unsigned long dest_len = uncompressed_size;
782 if (uncompress(td->
tmp, &dest_len, src, compressed_size) != Z_OK ||
783 dest_len != uncompressed_size)
787 for (i = 0; i < s->
ysize; i++)
796 ptr[1] = ptr[0] + s->
xdelta;
797 ptr[2] = ptr[1] + s->
xdelta;
800 for (j = 0; j < s->
xdelta; ++j) {
801 uint32_t
diff = (*(ptr[0]++) << 24) |
802 (*(ptr[1]++) << 16) |
805 bytestream_put_le32(&out, pixel);
810 ptr[1] = ptr[0] + s->
xdelta;
812 for (j = 0; j < s->
xdelta; j++) {
813 uint32_t
diff = (*(ptr[0]++) << 8) | *(ptr[1]++);
816 bytestream_put_le16(&out, pixel);
828 int jobnr,
int threadnr)
833 const uint8_t *channel_buffer[4] = { 0 };
835 uint64_t line_offset, uncompressed_size;
836 uint32_t xdelta = s->
xdelta;
839 uint32_t data_size,
line;
844 float one_gamma = 1.0f / s->
gamma;
849 if (line_offset > buf_size - 8)
852 src = buf + line_offset + 8;
858 if (data_size <= 0 || data_size > buf_size)
864 line_offset > buf_size - uncompressed_size)) ||
866 line_offset > buf_size - data_size))) {
870 if (data_size < uncompressed_size) {
911 r = channel_buffer[0];
912 g = channel_buffer[1];
913 b = channel_buffer[2];
914 if (channel_buffer[3])
915 a = channel_buffer[3];
917 ptr_x = (uint16_t *) ptr;
920 memset(ptr_x, 0, bxmin);
924 for (x = 0; x < xdelta; x++) {
926 t.
i = bytestream_get_le32(&r);
928 t.
f =
powf(t.
f, one_gamma);
931 t.
i = bytestream_get_le32(&g);
933 t.
f =
powf(t.
f, one_gamma);
936 t.
i = bytestream_get_le32(&b);
938 t.
f =
powf(t.
f, one_gamma);
940 if (channel_buffer[3])
945 for (x = 0; x < xdelta; x++) {
946 *ptr_x++ = s->
gamma_table[bytestream_get_le16(&r)];
947 *ptr_x++ = s->
gamma_table[bytestream_get_le16(&g)];
948 *ptr_x++ = s->
gamma_table[bytestream_get_le16(&b)];
949 if (channel_buffer[3])
955 memset(ptr_x, 0, axmax);
960 if (channel_buffer[3])
980 const char *value_name,
981 const char *value_type,
982 unsigned int minimum_length)
987 !strcmp(s->
gb.
buffer, value_name)) {
989 s->
gb.
buffer += strlen(value_name) + 1;
990 if (!strcmp(s->
gb.
buffer, value_type)) {
991 s->
gb.
buffer += strlen(value_type) + 1;
992 var_size = bytestream2_get_le32(&s->
gb);
998 s->
gb.
buffer -= strlen(value_name) + 1;
1000 "Unknown data type %s for header variable %s.\n",
1001 value_type, value_name);
1010 int current_channel_offset = 0;
1018 magic_number = bytestream2_get_le32(&s->
gb);
1019 if (magic_number != 20000630) {
1026 version = bytestream2_get_byte(&s->
gb);
1032 flags = bytestream2_get_le24(&s->
gb);
1042 "chlist", 38)) >= 0) {
1052 int channel_index = -1;
1055 if (strcmp(s->
layer,
"") != 0) {
1058 if (*ch_gb.
buffer ==
'.')
1061 "Layer %s.%s matched.\n", s->
layer, ch_gb.
buffer);
1065 if (!strcmp(ch_gb.
buffer,
"R") ||
1066 !strcmp(ch_gb.
buffer,
"X") ||
1067 !strcmp(ch_gb.
buffer,
"U"))
1069 else if (!strcmp(ch_gb.
buffer,
"G") ||
1070 !strcmp(ch_gb.
buffer,
"Y") ||
1071 !strcmp(ch_gb.
buffer,
"V"))
1073 else if (!strcmp(ch_gb.
buffer,
"B") ||
1074 !strcmp(ch_gb.
buffer,
"Z") ||
1075 !strcmp(ch_gb.
buffer,
"W"))
1077 else if (!strcmp(ch_gb.
buffer,
"A"))
1081 "Unsupported channel %.256s.\n", ch_gb.
buffer);
1085 bytestream2_get_byte(&ch_gb))
1093 current_pixel_type = bytestream2_get_le32(&ch_gb);
1097 current_pixel_type);
1102 xsub = bytestream2_get_le32(&ch_gb);
1103 ysub = bytestream2_get_le32(&ch_gb);
1104 if (xsub != 1 || ysub != 1) {
1106 "Subsampling %dx%d",
1111 if (channel_index >= 0) {
1115 "RGB channels not of the same depth.\n");
1128 channel->
xsub = xsub;
1129 channel->
ysub = ysub;
1131 current_channel_offset += 1 << current_pixel_type;
1156 s->
xmin = bytestream2_get_le32(&s->
gb);
1157 s->
ymin = bytestream2_get_le32(&s->
gb);
1158 s->
xmax = bytestream2_get_le32(&s->
gb);
1159 s->
ymax = bytestream2_get_le32(&s->
gb);
1165 "box2i", 34)) >= 0) {
1170 s->
w = bytestream2_get_le32(&s->
gb) + 1;
1171 s->
h = bytestream2_get_le32(&s->
gb) + 1;
1175 "lineOrder", 25)) >= 0) {
1180 line_order = bytestream2_get_byte(&s->
gb);
1182 if (line_order > 2) {
1189 "float", 31)) >= 0) {
1198 "compression", 29)) >= 0) {
1206 "Found more than one compression attribute.\n");
1218 for (i = 0; i < 2; i++)
1219 while (bytestream2_get_byte(&s->
gb) != 0);
1251 int scan_line_blocks;
1322 ptr = picture->
data[0];
1325 for (y = 0; y < s->
ymin; y++) {
1326 memset(ptr, 0, out_line_size);
1334 for (y = s->
ymax + 1; y < avctx->
height; y++) {
1335 memset(ptr, 0, out_line_size);
1350 float one_gamma = 1.0
f / s->
gamma;
1369 if (one_gamma > 0.9999
f && one_gamma < 1.0001
f) {
1370 for (i = 0; i < 65536; ++
i)
1373 for (i = 0; i < 65536; ++
i) {
1379 t.
f =
powf(t.
f, one_gamma);
1422 #define OFFSET(x) offsetof(EXRContext, x)
1423 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1425 {
"layer",
"Set the decoding layer",
OFFSET(layer),
1427 {
"gamma",
"Set the float gamma value when decoding",
OFFSET(gamma),