+ if (dend != d)
+ return AVERROR_INVALIDDATA;
+
+ predictor(td->tmp, uncompressed_size);
+ reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
+
+ return 0;
+}
+
+static int pxr24_uncompress(EXRContext *s, const uint8_t *src,
+ int compressed_size, int uncompressed_size,
+ EXRThreadData *td)
+{
+ unsigned long dest_len = uncompressed_size;
+ const uint8_t *in = td->tmp;
+ uint8_t *out;
+ int c, i, j;
+
+ if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
+ dest_len != uncompressed_size)
+ return AVERROR(EINVAL);
+
+ out = td->uncompressed_data;
+ for (i = 0; i < s->ysize; i++) {
+ for (c = 0; c < s->nb_channels; c++) {
+ EXRChannel *channel = &s->channels[c];
+ const uint8_t *ptr[4];
+ uint32_t pixel = 0;
+
+ switch (channel->pixel_type) {
+ case EXR_FLOAT:
+ ptr[0] = in;
+ ptr[1] = ptr[0] + s->xdelta;
+ ptr[2] = ptr[1] + s->xdelta;
+ in = ptr[2] + s->xdelta;
+
+ for (j = 0; j < s->xdelta; ++j) {
+ uint32_t diff = (*(ptr[0]++) << 24) |
+ (*(ptr[1]++) << 16) |
+ (*(ptr[2]++) << 8);
+ pixel += diff;
+ AV_WL32(out, pixel);
+ }
+ break;
+ case EXR_HALF:
+ ptr[0] = in;
+ ptr[1] = ptr[0] + s->xdelta;
+ in = ptr[1] + s->xdelta;
+ for (j = 0; j < s->xdelta; j++, out += 2) {
+ uint32_t diff = (*(ptr[0]++) << 8) | *(ptr[1]++);
+
+ pixel += diff;
+ AV_WL16(out, pixel);
+ }
+ break;
+ default:
+ av_assert1(0);
+ }
+ }
+ }
+
+ return 0;
+}
+
+static int decode_block(AVCodecContext *avctx, void *tdata,
+ int jobnr, int threadnr)
+{
+ EXRContext *s = avctx->priv_data;
+ AVFrame *const p = &s->picture;
+ EXRThreadData *td = &s->thread_data[threadnr];
+ const uint8_t *channel_buffer[4] = { 0 };
+ const uint8_t *buf = s->buf;
+ uint64_t line_offset, uncompressed_size;
+ uint32_t xdelta = s->xdelta;
+ uint16_t *ptr_x;
+ uint8_t *ptr;
+ int32_t data_size, line;
+ const uint8_t *src;
+ int axmax = (avctx->width - (s->xmax + 1)) * 2 * s->desc->nb_components;
+ int bxmin = s->xmin * 2 * s->desc->nb_components;
+ int ret, i, x, buf_size = s->buf_size;
+
+ line_offset = AV_RL64(s->table + jobnr * 8);
+ // Check if the buffer has the required bytes needed from the offset
+ if (line_offset > buf_size - 8)
+ return AVERROR_INVALIDDATA;
+
+ src = buf + line_offset + 8;
+ line = AV_RL32(src - 8);
+ if (line < s->ymin || line > s->ymax)
+ return AVERROR_INVALIDDATA;
+
+ data_size = AV_RL32(src - 4);
+ if (data_size <= 0 || data_size > buf_size)
+ return AVERROR_INVALIDDATA;
+
+ s->ysize = FFMIN(s->scan_lines_per_block, s->ymax - line + 1);
+ uncompressed_size = s->scan_line_size * s->ysize;
+ if ((s->compr == EXR_RAW && (data_size != uncompressed_size ||
+ line_offset > buf_size - uncompressed_size)) ||
+ (s->compr != EXR_RAW && (data_size > uncompressed_size ||
+ line_offset > buf_size - data_size))) {
+ return AVERROR_INVALIDDATA;
+ }
+
+ if (data_size < uncompressed_size) {
+ av_fast_padded_malloc(&td->uncompressed_data, &td->uncompressed_size, uncompressed_size);
+ av_fast_padded_malloc(&td->tmp, &td->tmp_size, uncompressed_size);
+ if (!td->uncompressed_data || !td->tmp)
+ return AVERROR(ENOMEM);
+
+ switch (s->compr) {
+ case EXR_ZIP1:
+ case EXR_ZIP16:
+ ret = zip_uncompress(src, data_size, uncompressed_size, td);
+ break;
+ case EXR_PXR24:
+ ret = pxr24_uncompress(s, src, data_size, uncompressed_size, td);
+ break;
+ case EXR_RLE:
+ ret = rle_uncompress(src, data_size, uncompressed_size, td);
+ }
+
+ src = td->uncompressed_data;
+ }
+
+ channel_buffer[0] = src + xdelta * s->channel_offsets[0];
+ channel_buffer[1] = src + xdelta * s->channel_offsets[1];
+ channel_buffer[2] = src + xdelta * s->channel_offsets[2];
+ if (s->channel_offsets[3] >= 0)
+ channel_buffer[3] = src + xdelta * s->channel_offsets[3];
+
+ ptr = p->data[0] + line * p->linesize[0];
+ for (i = 0; i < s->scan_lines_per_block && line + i <= s->ymax; i++, ptr += p->linesize[0]) {
+ const uint8_t *r, *g, *b, *a;
+
+ r = channel_buffer[0];
+ g = channel_buffer[1];
+ b = channel_buffer[2];
+ if (channel_buffer[3])
+ a = channel_buffer[3];
+
+ ptr_x = (uint16_t *)ptr;
+
+ // Zero out the start if xmin is not 0
+ memset(ptr_x, 0, bxmin);
+ ptr_x += s->xmin * s->desc->nb_components;
+ if (s->pixel_type == EXR_FLOAT) {
+ // 32-bit
+ for (x = 0; x < xdelta; x++) {
+ *ptr_x++ = exr_flt2uint(bytestream_get_le32(&r));
+ *ptr_x++ = exr_flt2uint(bytestream_get_le32(&g));
+ *ptr_x++ = exr_flt2uint(bytestream_get_le32(&b));
+ if (channel_buffer[3])
+ *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a));
+ }
+ } else {
+ // 16-bit
+ for (x = 0; x < xdelta; x++) {
+ *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&r));
+ *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&g));
+ *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&b));
+ if (channel_buffer[3])
+ *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&a));
+ }
+ }
+
+ // Zero out the end if xmax+1 is not w
+ memset(ptr_x, 0, axmax);
+
+ channel_buffer[0] += s->scan_line_size;
+ channel_buffer[1] += s->scan_line_size;
+ channel_buffer[2] += s->scan_line_size;
+ if (channel_buffer[3])
+ channel_buffer[3] += s->scan_line_size;
+ }
+
+ return 0;