110 unsigned int exp = v >> 23;
113 if (exp<= 127 + 7 - 24)
118 return (v + (1 << 23)) >> (127 + 7 - exp);
129 unsigned exp = 14 - (v >> 10);
131 if (exp == 14)
return (v >> 9) & 1;
132 else return (v & 0x8000) ? 0 : 0xffff;
135 return (v + (1 << 16)) >> (exp + 1);
149 unsigned int variable_buffer_data_size = bytestream_get_le32(buf);
150 if (variable_buffer_data_size >= buf_end - *buf)
152 return variable_buffer_data_size;
172 const char *value_name,
173 const char *value_type,
174 unsigned int minimum_length,
175 unsigned int *variable_buffer_data_size)
177 if (buf_end - *buf >= minimum_length && !strcmp(*buf, value_name)) {
178 *buf += strlen(value_name)+1;
179 if (!strcmp(*buf, value_type)) {
180 *buf += strlen(value_type)+1;
182 if (!*variable_buffer_data_size)
186 *buf -= strlen(value_name)+1;
198 int d = (int)t[-1] + (
int)t[0] - 128;
206 const int8_t *
t1 =
src;
207 const int8_t *
t2 = src + (size + 1) / 2;
209 int8_t *stop = s +
size;
227 unsigned long dest_len = uncompressed_size;
229 if (uncompress(td->
tmp, &dest_len, src, compressed_size) != Z_OK ||
230 dest_len != uncompressed_size)
242 int8_t *d = (int8_t *)td->
tmp;
243 const int8_t *
s = (
const int8_t *)
src;
244 int ssize = compressed_size;
245 int dsize = uncompressed_size;
246 int8_t *dend = d + dsize;
255 if ((dsize -= count ) < 0 ||
256 (ssize -= count + 1) < 0)
264 if ((dsize -= count) < 0 ||
284 #define USHORT_RANGE (1 << 16)
285 #define BITMAP_SIZE (1 << 13)
292 if ((i == 0) || (bitmap[i >> 3] & (1 << (i & 7))))
298 memset(lut + k, 0, (USHORT_RANGE - k) * 2);
303 static void apply_lut(
const uint16_t *lut, uint16_t *dst,
int dsize)
307 for (i = 0; i < dsize; ++i)
308 dst[i] = lut[dst[i]];
311 #define HUF_ENCBITS 16 // literal (value) bit length
312 #define HUF_DECBITS 14 // decoding bit size (>= 8)
314 #define HUF_ENCSIZE ((1 << HUF_ENCBITS) + 1) // encoding table size
315 #define HUF_DECSIZE (1 << HUF_DECBITS) // decoding table size
316 #define HUF_DECMASK (HUF_DECSIZE - 1)
326 uint64_t
c,
n[59] = { 0 };
333 for (i = 58; i > 0; --i) {
334 uint64_t nc = ((c + n[i]) >> 1);
343 hcode[i] = l | (n[l]++ << 6);
347 #define SHORT_ZEROCODE_RUN 59
348 #define LONG_ZEROCODE_RUN 63
349 #define SHORTEST_LONG_RUN (2 + LONG_ZEROCODE_RUN - SHORT_ZEROCODE_RUN)
350 #define LONGEST_LONG_RUN (255 + SHORTEST_LONG_RUN)
359 for (; im <= iM; im++) {
365 if (im + zerun > iM + 1)
373 int zerun = l - SHORT_ZEROCODE_RUN + 2;
375 if (im + zerun > iM + 1)
394 for (; im <= iM; im++) {
395 uint64_t
c = hcode[
im] >> 6;
396 int i, l = hcode[
im] & 63;
416 for (i = 1 << (
HUF_DECBITS - l); i > 0; i--, pl++) {
417 if (pl->
len || pl->
p)
428 #define get_char(c, lc, gb) { \
429 c = (c << 8) | bytestream2_get_byte(gb); \
433 #define get_code(po, rlc, c, lc, gb, out, oe) { \
436 get_char(c, lc, gb); \
442 return AVERROR_INVALIDDATA; \
448 } else if (out < oe) { \
451 return AVERROR_INVALIDDATA; \
457 int rlc,
int no, uint16_t *
out)
460 uint16_t *outb =
out;
461 uint16_t *oe = out + no;
481 for (j = 0; j < pl.
lit; j++) {
482 int l = hcode[pl.
p[j]] & 63;
488 if ((hcode[pl.
p[j]] >> 6) ==
489 ((c >> (lc - l)) & ((1LL << l) - 1))) {
491 get_code(pl.
p[j], rlc, c, lc, gb, out, oe);
518 if (out - outb != no)
524 uint16_t *dst,
int dst_size)
532 src_size = bytestream2_get_le32(gb);
533 im = bytestream2_get_le32(gb);
534 iM = bytestream2_get_le32(gb);
536 nBits = bytestream2_get_le32(gb);
546 if (!freq || !hdec) {
561 ret =
huf_decode(freq, hdec, gb, nBits, iM, dst_size, dst);
575 static inline void wdec14(uint16_t l, uint16_t h, uint16_t *
a, uint16_t *
b)
580 int ai = ls + (hi & 1) + (hi >> 1);
582 int16_t bs = ai - hi;
589 #define A_OFFSET (1 << (NBITS - 1))
590 #define MOD_MASK ((1 << NBITS) - 1)
592 static inline void wdec16(uint16_t l, uint16_t h, uint16_t *
a, uint16_t *
b)
603 int ny,
int oy, uint16_t mx)
605 int w14 = (mx < (1 << 14));
606 int n = (nx > ny) ? ny: nx;
619 uint16_t *ey = in + oy * (ny - p2);
620 uint16_t i00, i01, i10, i11;
626 for (; py <= ey; py += oy2) {
628 uint16_t *ex = py + ox * (nx - p2);
630 for (; px <= ex; px += ox2) {
631 uint16_t *p01 = px + ox1;
632 uint16_t *p10 = px + oy1;
633 uint16_t *p11 = p10 + ox1;
636 wdec14(*px, *p10, &i00, &i10);
637 wdec14(*p01, *p11, &i01, &i11);
638 wdec14(i00, i01, px, p01);
639 wdec14(i10, i11, p10, p11);
641 wdec16(*px, *p10, &i00, &i10);
642 wdec16(*p01, *p11, &i01, &i11);
643 wdec16(i00, i01, px, p01);
644 wdec16(i10, i11, p10, p11);
649 uint16_t *p10 = px + oy1;
652 wdec14(*px, *p10, &i00, p10);
654 wdec16(*px, *p10, &i00, p10);
662 uint16_t *ex = py + ox * (nx - p2);
664 for (; px <= ex; px += ox2) {
665 uint16_t *p01 = px + ox1;
668 wdec14(*px, *p01, &i00, p01);
670 wdec16(*px, *p01, &i00, p01);
684 uint16_t maxval, min_non_zero, max_non_zero;
685 uint16_t *ptr, *tmp = (uint16_t *)td->
tmp;
697 min_non_zero = bytestream2_get_le16(&gb);
698 max_non_zero = bytestream2_get_le16(&gb);
704 if (min_non_zero <= max_non_zero)
706 max_non_zero - min_non_zero + 1);
720 for (j = 0; j <
size; j++)
728 for (i = 0; i < s->
ysize; i++) {
740 int compressed_size,
int uncompressed_size,
743 unsigned long dest_len = uncompressed_size;
748 if (uncompress(td->
tmp, &dest_len, src, compressed_size) != Z_OK ||
749 dest_len != uncompressed_size)
753 for (i = 0; i < s->
ysize; i++) {
762 ptr[1] = ptr[0] + s->
xdelta;
763 ptr[2] = ptr[1] + s->
xdelta;
766 for (j = 0; j < s->
xdelta; ++j) {
767 uint32_t diff = (*(ptr[0]++) << 24) |
768 (*(ptr[1]++) << 16) |
771 bytestream_put_le32(&out, pixel);
776 ptr[1] = ptr[0] + s->
xdelta;
778 for (j = 0; j < s->
xdelta; j++) {
779 uint32_t diff = (*(ptr[0]++) << 8) | *(ptr[1]++);
782 bytestream_put_le16(&out, pixel);
795 int jobnr,
int threadnr)
800 const uint8_t *channel_buffer[4] = { 0 };
802 uint64_t line_offset, uncompressed_size;
803 uint32_t xdelta = s->
xdelta;
815 if (line_offset > buf_size - 8)
818 src = buf + line_offset + 8;
824 if (data_size <= 0 || data_size > buf_size)
829 if ((s->
compr ==
EXR_RAW && (data_size != uncompressed_size ||
830 line_offset > buf_size - uncompressed_size)) ||
831 (s->
compr !=
EXR_RAW && (data_size > uncompressed_size ||
832 line_offset > buf_size - data_size))) {
836 if (data_size < uncompressed_size) {
870 r = channel_buffer[0];
871 g = channel_buffer[1];
872 b = channel_buffer[2];
873 if (channel_buffer[3])
874 a = channel_buffer[3];
876 ptr_x = (uint16_t *)ptr;
879 memset(ptr_x, 0, bxmin);
883 for (x = 0; x < xdelta; x++) {
887 if (channel_buffer[3])
892 for (x = 0; x < xdelta; x++) {
896 if (channel_buffer[3])
902 memset(ptr_x, 0, axmax);
907 if (channel_buffer[3])
920 unsigned int buf_size = avpkt->
size;
921 const uint8_t *buf_end = buf + buf_size;
933 int scan_line_blocks;
935 unsigned int current_channel_offset = 0;
958 magic_number = bytestream_get_le32(&buf);
959 if (magic_number != 20000630) {
964 version = bytestream_get_byte(&buf);
970 flags = bytestream_get_le24(&buf);
977 while (buf < buf_end && buf[0]) {
978 unsigned int variable_buffer_data_size;
980 if (
check_header_variable(avctx, &buf, buf_end,
"channels",
"chlist", 38, &variable_buffer_data_size) >= 0) {
981 const uint8_t *channel_list_end;
982 if (!variable_buffer_data_size)
985 channel_list_end = buf + variable_buffer_data_size;
986 while (channel_list_end - buf >= 19) {
989 int channel_index = -1;
992 if (!strcmp(buf,
"R"))
994 else if (!strcmp(buf,
"G"))
996 else if (!strcmp(buf,
"B"))
998 else if (!strcmp(buf,
"A"))
1003 while (bytestream_get_byte(&buf) && buf < channel_list_end)
1006 if (channel_list_end - * &buf < 4) {
1011 current_pixel_type = bytestream_get_le32(&buf);
1012 if (current_pixel_type > 2) {
1018 xsub = bytestream_get_le32(&buf);
1019 ysub = bytestream_get_le32(&buf);
1020 if (xsub != 1 || ysub != 1) {
1025 if (channel_index >= 0) {
1039 channel->
xsub = xsub;
1040 channel->
ysub = ysub;
1042 current_channel_offset += 1 << current_pixel_type;
1060 buf = channel_list_end;
1062 }
else if (
check_header_variable(avctx, &buf, buf_end,
"dataWindow",
"box2i", 31, &variable_buffer_data_size) >= 0) {
1063 if (!variable_buffer_data_size)
1073 buf += variable_buffer_data_size;
1075 }
else if (
check_header_variable(avctx, &buf, buf_end,
"displayWindow",
"box2i", 34, &variable_buffer_data_size) >= 0) {
1076 if (!variable_buffer_data_size)
1082 buf += variable_buffer_data_size;
1084 }
else if (
check_header_variable(avctx, &buf, buf_end,
"lineOrder",
"lineOrder", 25, &variable_buffer_data_size) >= 0) {
1085 if (!variable_buffer_data_size)
1094 buf += variable_buffer_data_size;
1096 }
else if (
check_header_variable(avctx, &buf, buf_end,
"pixelAspectRatio",
"float", 31, &variable_buffer_data_size) >= 0) {
1097 if (!variable_buffer_data_size)
1102 buf += variable_buffer_data_size;
1104 }
else if (
check_header_variable(avctx, &buf, buf_end,
"compression",
"compression", 29, &variable_buffer_data_size) >= 0) {
1105 if (!variable_buffer_data_size)
1113 buf += variable_buffer_data_size;
1118 if (buf_end - buf <= 9) {
1124 for (i = 0; i < 2; i++) {
1126 while (++buf < buf_end)
1132 if (buf_end - buf >= 5) {
1134 if (!variable_buffer_data_size) {
1138 buf += variable_buffer_data_size;
1142 if (s->
compr == -1) {
1147 if (buf >= buf_end) {
1209 size_t thread_data_size, prev_size;
1226 if (buf_end - buf < scan_line_blocks * 8)
1229 ptr = picture->
data[0];
1232 for (y = 0; y < s->
ymin; y++) {
1233 memset(ptr, 0, out_line_size);
1241 for (y = s->
ymax + 1; y < avctx->
height; y++) {
1242 memset(ptr, 0, out_line_size);