107 #define OFFSET(x) offsetof(EXRContext, x)
108 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
129 unsigned int exp = v >> 23;
132 if (exp<= 127 + 7 - 24)
137 return (v + (1 << 23)) >> (127 + 7 - exp);
148 unsigned exp = 14 - (v >> 10);
150 if (exp == 14)
return (v >> 9) & 1;
151 else return (v & 0x8000) ? 0 : 0xffff;
154 return (v + (1 << 16)) >> (exp + 1);
168 unsigned int variable_buffer_data_size = bytestream_get_le32(buf);
169 if (variable_buffer_data_size >= buf_end - *buf)
171 return variable_buffer_data_size;
191 const char *value_name,
192 const char *value_type,
193 unsigned int minimum_length,
194 unsigned int *variable_buffer_data_size)
196 if (buf_end - *buf >= minimum_length && !strcmp(*buf, value_name)) {
197 *buf += strlen(value_name)+1;
198 if (!strcmp(*buf, value_type)) {
199 *buf += strlen(value_type)+1;
201 if (!*variable_buffer_data_size)
205 *buf -= strlen(value_name)+1;
217 int d = (int)t[-1] + (
int)t[0] - 128;
225 const int8_t *
t1 =
src;
226 const int8_t *
t2 = src + (size + 1) / 2;
228 int8_t *stop = s +
size;
246 unsigned long dest_len = uncompressed_size;
248 if (uncompress(td->
tmp, &dest_len, src, compressed_size) != Z_OK ||
249 dest_len != uncompressed_size)
261 int8_t *d = (int8_t *)td->
tmp;
262 const int8_t *
s = (
const int8_t *)
src;
263 int ssize = compressed_size;
264 int dsize = uncompressed_size;
265 int8_t *dend = d + dsize;
274 if ((dsize -= count ) < 0 ||
275 (ssize -= count + 1) < 0)
283 if ((dsize -= count) < 0 ||
303 #define USHORT_RANGE (1 << 16)
304 #define BITMAP_SIZE (1 << 13)
311 if ((i == 0) || (bitmap[i >> 3] & (1 << (i & 7))))
317 memset(lut + k, 0, (USHORT_RANGE - k) * 2);
322 static void apply_lut(
const uint16_t *lut, uint16_t *dst,
int dsize)
326 for (i = 0; i < dsize; ++i)
327 dst[i] = lut[dst[i]];
330 #define HUF_ENCBITS 16 // literal (value) bit length
331 #define HUF_DECBITS 14 // decoding bit size (>= 8)
333 #define HUF_ENCSIZE ((1 << HUF_ENCBITS) + 1) // encoding table size
334 #define HUF_DECSIZE (1 << HUF_DECBITS) // decoding table size
335 #define HUF_DECMASK (HUF_DECSIZE - 1)
345 uint64_t
c,
n[59] = { 0 };
352 for (i = 58; i > 0; --i) {
353 uint64_t nc = ((c + n[i]) >> 1);
362 hcode[i] = l | (n[l]++ << 6);
366 #define SHORT_ZEROCODE_RUN 59
367 #define LONG_ZEROCODE_RUN 63
368 #define SHORTEST_LONG_RUN (2 + LONG_ZEROCODE_RUN - SHORT_ZEROCODE_RUN)
369 #define LONGEST_LONG_RUN (255 + SHORTEST_LONG_RUN)
378 for (; im <= iM; im++) {
384 if (im + zerun > iM + 1)
392 int zerun = l - SHORT_ZEROCODE_RUN + 2;
394 if (im + zerun > iM + 1)
413 for (; im <= iM; im++) {
414 uint64_t
c = hcode[
im] >> 6;
415 int i, l = hcode[
im] & 63;
435 for (i = 1 << (
HUF_DECBITS - l); i > 0; i--, pl++) {
436 if (pl->
len || pl->
p)
447 #define get_char(c, lc, gb) { \
448 c = (c << 8) | bytestream2_get_byte(gb); \
452 #define get_code(po, rlc, c, lc, gb, out, oe) { \
455 get_char(c, lc, gb); \
461 return AVERROR_INVALIDDATA; \
467 } else if (out < oe) { \
470 return AVERROR_INVALIDDATA; \
476 int rlc,
int no, uint16_t *
out)
479 uint16_t *outb =
out;
480 uint16_t *oe = out + no;
500 for (j = 0; j < pl.
lit; j++) {
501 int l = hcode[pl.
p[j]] & 63;
507 if ((hcode[pl.
p[j]] >> 6) ==
508 ((c >> (lc - l)) & ((1LL << l) - 1))) {
510 get_code(pl.
p[j], rlc, c, lc, gb, out, oe);
537 if (out - outb != no)
543 uint16_t *dst,
int dst_size)
551 src_size = bytestream2_get_le32(gb);
552 im = bytestream2_get_le32(gb);
553 iM = bytestream2_get_le32(gb);
555 nBits = bytestream2_get_le32(gb);
565 if (!freq || !hdec) {
580 ret =
huf_decode(freq, hdec, gb, nBits, iM, dst_size, dst);
594 static inline void wdec14(uint16_t l, uint16_t h, uint16_t *
a, uint16_t *
b)
599 int ai = ls + (hi & 1) + (hi >> 1);
601 int16_t bs = ai - hi;
608 #define A_OFFSET (1 << (NBITS - 1))
609 #define MOD_MASK ((1 << NBITS) - 1)
611 static inline void wdec16(uint16_t l, uint16_t h, uint16_t *
a, uint16_t *
b)
622 int ny,
int oy, uint16_t mx)
624 int w14 = (mx < (1 << 14));
625 int n = (nx > ny) ? ny: nx;
638 uint16_t *ey = in + oy * (ny - p2);
639 uint16_t i00, i01, i10, i11;
645 for (; py <= ey; py += oy2) {
647 uint16_t *ex = py + ox * (nx - p2);
649 for (; px <= ex; px += ox2) {
650 uint16_t *p01 = px + ox1;
651 uint16_t *p10 = px + oy1;
652 uint16_t *p11 = p10 + ox1;
655 wdec14(*px, *p10, &i00, &i10);
656 wdec14(*p01, *p11, &i01, &i11);
657 wdec14(i00, i01, px, p01);
658 wdec14(i10, i11, p10, p11);
660 wdec16(*px, *p10, &i00, &i10);
661 wdec16(*p01, *p11, &i01, &i11);
662 wdec16(i00, i01, px, p01);
663 wdec16(i10, i11, p10, p11);
668 uint16_t *p10 = px + oy1;
671 wdec14(*px, *p10, &i00, p10);
673 wdec16(*px, *p10, &i00, p10);
681 uint16_t *ex = py + ox * (nx - p2);
683 for (; px <= ex; px += ox2) {
684 uint16_t *p01 = px + ox1;
687 wdec14(*px, *p01, &i00, p01);
689 wdec16(*px, *p01, &i00, p01);
703 uint16_t maxval, min_non_zero, max_non_zero;
704 uint16_t *ptr, *tmp = (uint16_t *)td->
tmp;
716 min_non_zero = bytestream2_get_le16(&gb);
717 max_non_zero = bytestream2_get_le16(&gb);
723 if (min_non_zero <= max_non_zero)
725 max_non_zero - min_non_zero + 1);
739 for (j = 0; j <
size; j++)
747 for (i = 0; i < s->
ysize; i++) {
759 int compressed_size,
int uncompressed_size,
762 unsigned long dest_len = uncompressed_size;
767 if (uncompress(td->
tmp, &dest_len, src, compressed_size) != Z_OK ||
768 dest_len != uncompressed_size)
772 for (i = 0; i < s->
ysize; i++) {
781 ptr[1] = ptr[0] + s->
xdelta;
782 ptr[2] = ptr[1] + s->
xdelta;
785 for (j = 0; j < s->
xdelta; ++j) {
786 uint32_t diff = (*(ptr[0]++) << 24) |
787 (*(ptr[1]++) << 16) |
790 bytestream_put_le32(&out, pixel);
795 ptr[1] = ptr[0] + s->
xdelta;
797 for (j = 0; j < s->
xdelta; j++) {
798 uint32_t diff = (*(ptr[0]++) << 8) | *(ptr[1]++);
801 bytestream_put_le16(&out, pixel);
814 int jobnr,
int threadnr)
819 const uint8_t *channel_buffer[4] = { 0 };
821 uint64_t line_offset, uncompressed_size;
822 uint32_t xdelta = s->
xdelta;
834 if (line_offset > buf_size - 8)
837 src = buf + line_offset + 8;
843 if (data_size <= 0 || data_size > buf_size)
848 if ((s->
compr ==
EXR_RAW && (data_size != uncompressed_size ||
849 line_offset > buf_size - uncompressed_size)) ||
850 (s->
compr !=
EXR_RAW && (data_size > uncompressed_size ||
851 line_offset > buf_size - data_size))) {
855 if (data_size < uncompressed_size) {
889 r = channel_buffer[0];
890 g = channel_buffer[1];
891 b = channel_buffer[2];
892 if (channel_buffer[3])
893 a = channel_buffer[3];
895 ptr_x = (uint16_t *)ptr;
898 memset(ptr_x, 0, bxmin);
902 for (x = 0; x < xdelta; x++) {
906 if (channel_buffer[3])
911 for (x = 0; x < xdelta; x++) {
915 if (channel_buffer[3])
921 memset(ptr_x, 0, axmax);
926 if (channel_buffer[3])
939 unsigned int buf_size = avpkt->
size;
940 const uint8_t *buf_end = buf + buf_size;
952 int scan_line_blocks;
954 unsigned int current_channel_offset = 0;
977 magic_number = bytestream_get_le32(&buf);
978 if (magic_number != 20000630) {
983 version = bytestream_get_byte(&buf);
989 flags = bytestream_get_le24(&buf);
996 while (buf < buf_end && buf[0]) {
997 unsigned int variable_buffer_data_size;
999 if (
check_header_variable(avctx, &buf, buf_end,
"channels",
"chlist", 38, &variable_buffer_data_size) >= 0) {
1000 const uint8_t *channel_list_end;
1001 if (!variable_buffer_data_size)
1004 channel_list_end = buf + variable_buffer_data_size;
1005 while (channel_list_end - buf >= 19) {
1008 int channel_index = -1;
1011 const char*
b =
buf;
1013 if ( strcmp( s->
layer,
"" ) != 0 ) {
1014 if ( strncmp( b, s->
layer, strlen(s->
layer) ) == 0 ) {
1015 b += strlen(s->
layer);
1016 if ( *b ==
'.' ) ++
b;
1023 if (!strcmp(b,
"R")||!strcmp(b,
"X")||!strcmp(b,
"U"))
1025 else if (!strcmp(b,
"G")||!strcmp(b,
"Y")||!strcmp(b,
"V"))
1027 else if (!strcmp(b,
"B")||!strcmp(b,
"Z")||!strcmp(b,
"W"))
1029 else if (!strcmp(b,
"A"))
1034 while (bytestream_get_byte(&buf) && buf < channel_list_end)
1037 if (channel_list_end - * &buf < 4) {
1042 current_pixel_type = bytestream_get_le32(&buf);
1043 if (current_pixel_type > 2) {
1049 xsub = bytestream_get_le32(&buf);
1050 ysub = bytestream_get_le32(&buf);
1051 if (xsub != 1 || ysub != 1) {
1056 if (channel_index >= 0) {
1070 channel->
xsub = xsub;
1071 channel->
ysub = ysub;
1073 current_channel_offset += 1 << current_pixel_type;
1091 buf = channel_list_end;
1093 }
else if (
check_header_variable(avctx, &buf, buf_end,
"dataWindow",
"box2i", 31, &variable_buffer_data_size) >= 0) {
1094 if (!variable_buffer_data_size)
1104 buf += variable_buffer_data_size;
1106 }
else if (
check_header_variable(avctx, &buf, buf_end,
"displayWindow",
"box2i", 34, &variable_buffer_data_size) >= 0) {
1107 if (!variable_buffer_data_size)
1113 buf += variable_buffer_data_size;
1115 }
else if (
check_header_variable(avctx, &buf, buf_end,
"lineOrder",
"lineOrder", 25, &variable_buffer_data_size) >= 0) {
1116 if (!variable_buffer_data_size)
1125 buf += variable_buffer_data_size;
1127 }
else if (
check_header_variable(avctx, &buf, buf_end,
"pixelAspectRatio",
"float", 31, &variable_buffer_data_size) >= 0) {
1128 if (!variable_buffer_data_size)
1133 buf += variable_buffer_data_size;
1135 }
else if (
check_header_variable(avctx, &buf, buf_end,
"compression",
"compression", 29, &variable_buffer_data_size) >= 0) {
1136 if (!variable_buffer_data_size)
1144 buf += variable_buffer_data_size;
1149 if (buf_end - buf <= 9) {
1155 for (i = 0; i < 2; i++) {
1157 while (++buf < buf_end)
1163 if (buf_end - buf >= 5) {
1165 if (!variable_buffer_data_size) {
1169 buf += variable_buffer_data_size;
1173 if (s->
compr == -1) {
1178 if (buf >= buf_end) {
1236 size_t thread_data_size, prev_size;
1253 if (buf_end - buf < scan_line_blocks * 8)
1256 ptr = picture->
data[0];
1259 for (y = 0; y < s->
ymin; y++) {
1260 memset(ptr, 0, out_line_size);
1268 for (y = s->
ymax + 1; y < avctx->
height; y++) {
1269 memset(ptr, 0, out_line_size);
1308 .priv_class = &exr_class,