| 1 | /* |
| 2 | * OpenEXR (.exr) image decoder |
| 3 | * Copyright (c) 2009 Jimmy Christensen |
| 4 | * |
| 5 | * This file is part of FFmpeg. |
| 6 | * |
| 7 | * FFmpeg is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation; either |
| 10 | * version 2.1 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * FFmpeg is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with FFmpeg; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | /** |
| 23 | * @file |
| 24 | * OpenEXR decoder |
| 25 | * @author Jimmy Christensen |
| 26 | * |
| 27 | * For more information on the OpenEXR format, visit: |
| 28 | * http://openexr.com/ |
| 29 | * |
| 30 | * exr_flt2uint() and exr_halflt2uint() is credited to Reimar Döffinger. |
| 31 | * exr_half2float() is credited to Aaftab Munshi; Dan Ginsburg, Dave Shreiner. |
| 32 | * |
| 33 | */ |
| 34 | |
| 35 | #include <zlib.h> |
| 36 | #include <float.h> |
| 37 | |
| 38 | #include "libavutil/imgutils.h" |
| 39 | #include "libavutil/opt.h" |
| 40 | #include "libavutil/intfloat.h" |
| 41 | |
| 42 | #include "avcodec.h" |
| 43 | #include "bytestream.h" |
| 44 | #include "get_bits.h" |
| 45 | #include "internal.h" |
| 46 | #include "mathops.h" |
| 47 | #include "thread.h" |
| 48 | |
| 49 | enum ExrCompr { |
| 50 | EXR_RAW, |
| 51 | EXR_RLE, |
| 52 | EXR_ZIP1, |
| 53 | EXR_ZIP16, |
| 54 | EXR_PIZ, |
| 55 | EXR_PXR24, |
| 56 | EXR_B44, |
| 57 | EXR_B44A, |
| 58 | EXR_UNKN, |
| 59 | }; |
| 60 | |
| 61 | enum ExrPixelType { |
| 62 | EXR_UINT, |
| 63 | EXR_HALF, |
| 64 | EXR_FLOAT, |
| 65 | EXR_UNKNOWN, |
| 66 | }; |
| 67 | |
| 68 | typedef struct EXRChannel { |
| 69 | int xsub, ysub; |
| 70 | enum ExrPixelType pixel_type; |
| 71 | } EXRChannel; |
| 72 | |
| 73 | typedef struct EXRThreadData { |
| 74 | uint8_t *uncompressed_data; |
| 75 | int uncompressed_size; |
| 76 | |
| 77 | uint8_t *tmp; |
| 78 | int tmp_size; |
| 79 | |
| 80 | uint8_t *bitmap; |
| 81 | uint16_t *lut; |
| 82 | } EXRThreadData; |
| 83 | |
| 84 | typedef struct EXRContext { |
| 85 | AVClass *class; |
| 86 | AVFrame *picture; |
| 87 | AVCodecContext *avctx; |
| 88 | |
| 89 | enum ExrCompr compression; |
| 90 | enum ExrPixelType pixel_type; |
| 91 | int channel_offsets[4]; // 0 = red, 1 = green, 2 = blue and 3 = alpha |
| 92 | const AVPixFmtDescriptor *desc; |
| 93 | |
| 94 | int w, h; |
| 95 | uint32_t xmax, xmin; |
| 96 | uint32_t ymax, ymin; |
| 97 | uint32_t xdelta, ydelta; |
| 98 | int ysize; |
| 99 | |
| 100 | uint64_t scan_line_size; |
| 101 | int scan_lines_per_block; |
| 102 | |
| 103 | GetByteContext gb; |
| 104 | const uint8_t *buf; |
| 105 | int buf_size; |
| 106 | |
| 107 | EXRChannel *channels; |
| 108 | int nb_channels; |
| 109 | |
| 110 | EXRThreadData *thread_data; |
| 111 | |
| 112 | const char *layer; |
| 113 | |
| 114 | float gamma; |
| 115 | |
| 116 | uint16_t gamma_table[65536]; |
| 117 | |
| 118 | } EXRContext; |
| 119 | |
| 120 | /* -15 stored using a single precision bias of 127 */ |
| 121 | #define HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP 0x38000000 |
| 122 | /* max exponent value in single precision that will be converted |
| 123 | * to Inf or Nan when stored as a half-float */ |
| 124 | #define HALF_FLOAT_MAX_BIASED_EXP_AS_SINGLE_FP_EXP 0x47800000 |
| 125 | |
| 126 | /* 255 is the max exponent biased value */ |
| 127 | #define FLOAT_MAX_BIASED_EXP (0xFF << 23) |
| 128 | |
| 129 | #define HALF_FLOAT_MAX_BIASED_EXP (0x1F << 10) |
| 130 | |
| 131 | /* |
| 132 | * Convert a half float as a uint16_t into a full float. |
| 133 | * |
| 134 | * @param hf half float as uint16_t |
| 135 | * |
| 136 | * @return float value |
| 137 | */ |
| 138 | static union av_intfloat32 exr_half2float(uint16_t hf) |
| 139 | { |
| 140 | unsigned int sign = (unsigned int)(hf >> 15); |
| 141 | unsigned int mantissa = (unsigned int)(hf & ((1 << 10) - 1)); |
| 142 | unsigned int exp = (unsigned int)(hf & HALF_FLOAT_MAX_BIASED_EXP); |
| 143 | union av_intfloat32 f; |
| 144 | |
| 145 | if (exp == HALF_FLOAT_MAX_BIASED_EXP) { |
| 146 | // we have a half-float NaN or Inf |
| 147 | // half-float NaNs will be converted to a single precision NaN |
| 148 | // half-float Infs will be converted to a single precision Inf |
| 149 | exp = FLOAT_MAX_BIASED_EXP; |
| 150 | if (mantissa) |
| 151 | mantissa = (1 << 23) - 1; // set all bits to indicate a NaN |
| 152 | } else if (exp == 0x0) { |
| 153 | // convert half-float zero/denorm to single precision value |
| 154 | if (mantissa) { |
| 155 | mantissa <<= 1; |
| 156 | exp = HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP; |
| 157 | // check for leading 1 in denorm mantissa |
| 158 | while ((mantissa & (1 << 10))) { |
| 159 | // for every leading 0, decrement single precision exponent by 1 |
| 160 | // and shift half-float mantissa value to the left |
| 161 | mantissa <<= 1; |
| 162 | exp -= (1 << 23); |
| 163 | } |
| 164 | // clamp the mantissa to 10-bits |
| 165 | mantissa &= ((1 << 10) - 1); |
| 166 | // shift left to generate single-precision mantissa of 23-bits |
| 167 | mantissa <<= 13; |
| 168 | } |
| 169 | } else { |
| 170 | // shift left to generate single-precision mantissa of 23-bits |
| 171 | mantissa <<= 13; |
| 172 | // generate single precision biased exponent value |
| 173 | exp = (exp << 13) + HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP; |
| 174 | } |
| 175 | |
| 176 | f.i = (sign << 31) | exp | mantissa; |
| 177 | |
| 178 | return f; |
| 179 | } |
| 180 | |
| 181 | |
| 182 | /** |
| 183 | * Convert from 32-bit float as uint32_t to uint16_t. |
| 184 | * |
| 185 | * @param v 32-bit float |
| 186 | * |
| 187 | * @return normalized 16-bit unsigned int |
| 188 | */ |
| 189 | static inline uint16_t exr_flt2uint(uint32_t v) |
| 190 | { |
| 191 | unsigned int exp = v >> 23; |
| 192 | // "HACK": negative values result in exp< 0, so clipping them to 0 |
| 193 | // is also handled by this condition, avoids explicit check for sign bit. |
| 194 | if (exp <= 127 + 7 - 24) // we would shift out all bits anyway |
| 195 | return 0; |
| 196 | if (exp >= 127) |
| 197 | return 0xffff; |
| 198 | v &= 0x007fffff; |
| 199 | return (v + (1 << 23)) >> (127 + 7 - exp); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Convert from 16-bit float as uint16_t to uint16_t. |
| 204 | * |
| 205 | * @param v 16-bit float |
| 206 | * |
| 207 | * @return normalized 16-bit unsigned int |
| 208 | */ |
| 209 | static inline uint16_t exr_halflt2uint(uint16_t v) |
| 210 | { |
| 211 | unsigned exp = 14 - (v >> 10); |
| 212 | if (exp >= 14) { |
| 213 | if (exp == 14) |
| 214 | return (v >> 9) & 1; |
| 215 | else |
| 216 | return (v & 0x8000) ? 0 : 0xffff; |
| 217 | } |
| 218 | v <<= 6; |
| 219 | return (v + (1 << 16)) >> (exp + 1); |
| 220 | } |
| 221 | |
| 222 | static void predictor(uint8_t *src, int size) |
| 223 | { |
| 224 | uint8_t *t = src + 1; |
| 225 | uint8_t *stop = src + size; |
| 226 | |
| 227 | while (t < stop) { |
| 228 | int d = (int) t[-1] + (int) t[0] - 128; |
| 229 | t[0] = d; |
| 230 | ++t; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | static void reorder_pixels(uint8_t *src, uint8_t *dst, int size) |
| 235 | { |
| 236 | const int8_t *t1 = src; |
| 237 | const int8_t *t2 = src + (size + 1) / 2; |
| 238 | int8_t *s = dst; |
| 239 | int8_t *stop = s + size; |
| 240 | |
| 241 | while (1) { |
| 242 | if (s < stop) |
| 243 | *(s++) = *(t1++); |
| 244 | else |
| 245 | break; |
| 246 | |
| 247 | if (s < stop) |
| 248 | *(s++) = *(t2++); |
| 249 | else |
| 250 | break; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | static int zip_uncompress(const uint8_t *src, int compressed_size, |
| 255 | int uncompressed_size, EXRThreadData *td) |
| 256 | { |
| 257 | unsigned long dest_len = uncompressed_size; |
| 258 | |
| 259 | if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK || |
| 260 | dest_len != uncompressed_size) |
| 261 | return AVERROR_INVALIDDATA; |
| 262 | |
| 263 | predictor(td->tmp, uncompressed_size); |
| 264 | reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size); |
| 265 | |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | static int rle_uncompress(const uint8_t *src, int compressed_size, |
| 270 | int uncompressed_size, EXRThreadData *td) |
| 271 | { |
| 272 | uint8_t *d = td->tmp; |
| 273 | const int8_t *s = src; |
| 274 | int ssize = compressed_size; |
| 275 | int dsize = uncompressed_size; |
| 276 | uint8_t *dend = d + dsize; |
| 277 | int count; |
| 278 | |
| 279 | while (ssize > 0) { |
| 280 | count = *s++; |
| 281 | |
| 282 | if (count < 0) { |
| 283 | count = -count; |
| 284 | |
| 285 | if ((dsize -= count) < 0 || |
| 286 | (ssize -= count + 1) < 0) |
| 287 | return AVERROR_INVALIDDATA; |
| 288 | |
| 289 | while (count--) |
| 290 | *d++ = *s++; |
| 291 | } else { |
| 292 | count++; |
| 293 | |
| 294 | if ((dsize -= count) < 0 || |
| 295 | (ssize -= 2) < 0) |
| 296 | return AVERROR_INVALIDDATA; |
| 297 | |
| 298 | while (count--) |
| 299 | *d++ = *s; |
| 300 | |
| 301 | s++; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | if (dend != d) |
| 306 | return AVERROR_INVALIDDATA; |
| 307 | |
| 308 | predictor(td->tmp, uncompressed_size); |
| 309 | reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size); |
| 310 | |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | #define USHORT_RANGE (1 << 16) |
| 315 | #define BITMAP_SIZE (1 << 13) |
| 316 | |
| 317 | static uint16_t reverse_lut(const uint8_t *bitmap, uint16_t *lut) |
| 318 | { |
| 319 | int i, k = 0; |
| 320 | |
| 321 | for (i = 0; i < USHORT_RANGE; i++) |
| 322 | if ((i == 0) || (bitmap[i >> 3] & (1 << (i & 7)))) |
| 323 | lut[k++] = i; |
| 324 | |
| 325 | i = k - 1; |
| 326 | |
| 327 | memset(lut + k, 0, (USHORT_RANGE - k) * 2); |
| 328 | |
| 329 | return i; |
| 330 | } |
| 331 | |
| 332 | static void apply_lut(const uint16_t *lut, uint16_t *dst, int dsize) |
| 333 | { |
| 334 | int i; |
| 335 | |
| 336 | for (i = 0; i < dsize; ++i) |
| 337 | dst[i] = lut[dst[i]]; |
| 338 | } |
| 339 | |
| 340 | #define HUF_ENCBITS 16 // literal (value) bit length |
| 341 | #define HUF_DECBITS 14 // decoding bit size (>= 8) |
| 342 | |
| 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) |
| 346 | |
| 347 | typedef struct HufDec { |
| 348 | int len; |
| 349 | int lit; |
| 350 | int *p; |
| 351 | } HufDec; |
| 352 | |
| 353 | static void huf_canonical_code_table(uint64_t *hcode) |
| 354 | { |
| 355 | uint64_t c, n[59] = { 0 }; |
| 356 | int i; |
| 357 | |
| 358 | for (i = 0; i < HUF_ENCSIZE; ++i) |
| 359 | n[hcode[i]] += 1; |
| 360 | |
| 361 | c = 0; |
| 362 | for (i = 58; i > 0; --i) { |
| 363 | uint64_t nc = ((c + n[i]) >> 1); |
| 364 | n[i] = c; |
| 365 | c = nc; |
| 366 | } |
| 367 | |
| 368 | for (i = 0; i < HUF_ENCSIZE; ++i) { |
| 369 | int l = hcode[i]; |
| 370 | |
| 371 | if (l > 0) |
| 372 | hcode[i] = l | (n[l]++ << 6); |
| 373 | } |
| 374 | } |
| 375 | |
| 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) |
| 380 | |
| 381 | static int huf_unpack_enc_table(GetByteContext *gb, |
| 382 | int32_t im, int32_t iM, uint64_t *hcode) |
| 383 | { |
| 384 | GetBitContext gbit; |
| 385 | int ret = init_get_bits8(&gbit, gb->buffer, bytestream2_get_bytes_left(gb)); |
| 386 | if (ret < 0) |
| 387 | return ret; |
| 388 | |
| 389 | for (; im <= iM; im++) { |
| 390 | uint64_t l = hcode[im] = get_bits(&gbit, 6); |
| 391 | |
| 392 | if (l == LONG_ZEROCODE_RUN) { |
| 393 | int zerun = get_bits(&gbit, 8) + SHORTEST_LONG_RUN; |
| 394 | |
| 395 | if (im + zerun > iM + 1) |
| 396 | return AVERROR_INVALIDDATA; |
| 397 | |
| 398 | while (zerun--) |
| 399 | hcode[im++] = 0; |
| 400 | |
| 401 | im--; |
| 402 | } else if (l >= SHORT_ZEROCODE_RUN) { |
| 403 | int zerun = l - SHORT_ZEROCODE_RUN + 2; |
| 404 | |
| 405 | if (im + zerun > iM + 1) |
| 406 | return AVERROR_INVALIDDATA; |
| 407 | |
| 408 | while (zerun--) |
| 409 | hcode[im++] = 0; |
| 410 | |
| 411 | im--; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | bytestream2_skip(gb, (get_bits_count(&gbit) + 7) / 8); |
| 416 | huf_canonical_code_table(hcode); |
| 417 | |
| 418 | return 0; |
| 419 | } |
| 420 | |
| 421 | static int huf_build_dec_table(const uint64_t *hcode, int im, |
| 422 | int iM, HufDec *hdecod) |
| 423 | { |
| 424 | for (; im <= iM; im++) { |
| 425 | uint64_t c = hcode[im] >> 6; |
| 426 | int i, l = hcode[im] & 63; |
| 427 | |
| 428 | if (c >> l) |
| 429 | return AVERROR_INVALIDDATA; |
| 430 | |
| 431 | if (l > HUF_DECBITS) { |
| 432 | HufDec *pl = hdecod + (c >> (l - HUF_DECBITS)); |
| 433 | if (pl->len) |
| 434 | return AVERROR_INVALIDDATA; |
| 435 | |
| 436 | pl->lit++; |
| 437 | |
| 438 | pl->p = av_realloc(pl->p, pl->lit * sizeof(int)); |
| 439 | if (!pl->p) |
| 440 | return AVERROR(ENOMEM); |
| 441 | |
| 442 | pl->p[pl->lit - 1] = im; |
| 443 | } else if (l) { |
| 444 | HufDec *pl = hdecod + (c << (HUF_DECBITS - l)); |
| 445 | |
| 446 | for (i = 1 << (HUF_DECBITS - l); i > 0; i--, pl++) { |
| 447 | if (pl->len || pl->p) |
| 448 | return AVERROR_INVALIDDATA; |
| 449 | pl->len = l; |
| 450 | pl->lit = im; |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | #define get_char(c, lc, gb) \ |
| 459 | { \ |
| 460 | c = (c << 8) | bytestream2_get_byte(gb); \ |
| 461 | lc += 8; \ |
| 462 | } |
| 463 | |
| 464 | #define get_code(po, rlc, c, lc, gb, out, oe) \ |
| 465 | { \ |
| 466 | if (po == rlc) { \ |
| 467 | if (lc < 8) \ |
| 468 | get_char(c, lc, gb); \ |
| 469 | lc -= 8; \ |
| 470 | \ |
| 471 | cs = c >> lc; \ |
| 472 | \ |
| 473 | if (out + cs > oe) \ |
| 474 | return AVERROR_INVALIDDATA; \ |
| 475 | \ |
| 476 | s = out[-1]; \ |
| 477 | \ |
| 478 | while (cs-- > 0) \ |
| 479 | *out++ = s; \ |
| 480 | } else if (out < oe) { \ |
| 481 | *out++ = po; \ |
| 482 | } else { \ |
| 483 | return AVERROR_INVALIDDATA; \ |
| 484 | } \ |
| 485 | } |
| 486 | |
| 487 | static int huf_decode(const uint64_t *hcode, const HufDec *hdecod, |
| 488 | GetByteContext *gb, int nbits, |
| 489 | int rlc, int no, uint16_t *out) |
| 490 | { |
| 491 | uint64_t c = 0; |
| 492 | uint16_t *outb = out; |
| 493 | uint16_t *oe = out + no; |
| 494 | const uint8_t *ie = gb->buffer + (nbits + 7) / 8; // input byte size |
| 495 | uint8_t cs, s; |
| 496 | int i, lc = 0; |
| 497 | |
| 498 | while (gb->buffer < ie) { |
| 499 | get_char(c, lc, gb); |
| 500 | |
| 501 | while (lc >= HUF_DECBITS) { |
| 502 | const HufDec pl = hdecod[(c >> (lc - HUF_DECBITS)) & HUF_DECMASK]; |
| 503 | |
| 504 | if (pl.len) { |
| 505 | lc -= pl.len; |
| 506 | get_code(pl.lit, rlc, c, lc, gb, out, oe); |
| 507 | } else { |
| 508 | int j; |
| 509 | |
| 510 | if (!pl.p) |
| 511 | return AVERROR_INVALIDDATA; |
| 512 | |
| 513 | for (j = 0; j < pl.lit; j++) { |
| 514 | int l = hcode[pl.p[j]] & 63; |
| 515 | |
| 516 | while (lc < l && bytestream2_get_bytes_left(gb) > 0) |
| 517 | get_char(c, lc, gb); |
| 518 | |
| 519 | if (lc >= l) { |
| 520 | if ((hcode[pl.p[j]] >> 6) == |
| 521 | ((c >> (lc - l)) & ((1LL << l) - 1))) { |
| 522 | lc -= l; |
| 523 | get_code(pl.p[j], rlc, c, lc, gb, out, oe); |
| 524 | break; |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | if (j == pl.lit) |
| 530 | return AVERROR_INVALIDDATA; |
| 531 | } |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | i = (8 - nbits) & 7; |
| 536 | c >>= i; |
| 537 | lc -= i; |
| 538 | |
| 539 | while (lc > 0) { |
| 540 | const HufDec pl = hdecod[(c << (HUF_DECBITS - lc)) & HUF_DECMASK]; |
| 541 | |
| 542 | if (pl.len) { |
| 543 | lc -= pl.len; |
| 544 | get_code(pl.lit, rlc, c, lc, gb, out, oe); |
| 545 | } else { |
| 546 | return AVERROR_INVALIDDATA; |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | if (out - outb != no) |
| 551 | return AVERROR_INVALIDDATA; |
| 552 | return 0; |
| 553 | } |
| 554 | |
| 555 | static int huf_uncompress(GetByteContext *gb, |
| 556 | uint16_t *dst, int dst_size) |
| 557 | { |
| 558 | int32_t src_size, im, iM; |
| 559 | uint32_t nBits; |
| 560 | uint64_t *freq; |
| 561 | HufDec *hdec; |
| 562 | int ret, i; |
| 563 | |
| 564 | src_size = bytestream2_get_le32(gb); |
| 565 | im = bytestream2_get_le32(gb); |
| 566 | iM = bytestream2_get_le32(gb); |
| 567 | bytestream2_skip(gb, 4); |
| 568 | nBits = bytestream2_get_le32(gb); |
| 569 | if (im < 0 || im >= HUF_ENCSIZE || |
| 570 | iM < 0 || iM >= HUF_ENCSIZE || |
| 571 | src_size < 0) |
| 572 | return AVERROR_INVALIDDATA; |
| 573 | |
| 574 | bytestream2_skip(gb, 4); |
| 575 | |
| 576 | freq = av_mallocz_array(HUF_ENCSIZE, sizeof(*freq)); |
| 577 | hdec = av_mallocz_array(HUF_DECSIZE, sizeof(*hdec)); |
| 578 | if (!freq || !hdec) { |
| 579 | ret = AVERROR(ENOMEM); |
| 580 | goto fail; |
| 581 | } |
| 582 | |
| 583 | if ((ret = huf_unpack_enc_table(gb, im, iM, freq)) < 0) |
| 584 | goto fail; |
| 585 | |
| 586 | if (nBits > 8 * bytestream2_get_bytes_left(gb)) { |
| 587 | ret = AVERROR_INVALIDDATA; |
| 588 | goto fail; |
| 589 | } |
| 590 | |
| 591 | if ((ret = huf_build_dec_table(freq, im, iM, hdec)) < 0) |
| 592 | goto fail; |
| 593 | ret = huf_decode(freq, hdec, gb, nBits, iM, dst_size, dst); |
| 594 | |
| 595 | fail: |
| 596 | for (i = 0; i < HUF_DECSIZE; i++) |
| 597 | if (hdec) |
| 598 | av_freep(&hdec[i].p); |
| 599 | |
| 600 | av_free(freq); |
| 601 | av_free(hdec); |
| 602 | |
| 603 | return ret; |
| 604 | } |
| 605 | |
| 606 | static inline void wdec14(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b) |
| 607 | { |
| 608 | int16_t ls = l; |
| 609 | int16_t hs = h; |
| 610 | int hi = hs; |
| 611 | int ai = ls + (hi & 1) + (hi >> 1); |
| 612 | int16_t as = ai; |
| 613 | int16_t bs = ai - hi; |
| 614 | |
| 615 | *a = as; |
| 616 | *b = bs; |
| 617 | } |
| 618 | |
| 619 | #define NBITS 16 |
| 620 | #define A_OFFSET (1 << (NBITS - 1)) |
| 621 | #define MOD_MASK ((1 << NBITS) - 1) |
| 622 | |
| 623 | static inline void wdec16(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b) |
| 624 | { |
| 625 | int m = l; |
| 626 | int d = h; |
| 627 | int bb = (m - (d >> 1)) & MOD_MASK; |
| 628 | int aa = (d + bb - A_OFFSET) & MOD_MASK; |
| 629 | *b = bb; |
| 630 | *a = aa; |
| 631 | } |
| 632 | |
| 633 | static void wav_decode(uint16_t *in, int nx, int ox, |
| 634 | int ny, int oy, uint16_t mx) |
| 635 | { |
| 636 | int w14 = (mx < (1 << 14)); |
| 637 | int n = (nx > ny) ? ny : nx; |
| 638 | int p = 1; |
| 639 | int p2; |
| 640 | |
| 641 | while (p <= n) |
| 642 | p <<= 1; |
| 643 | |
| 644 | p >>= 1; |
| 645 | p2 = p; |
| 646 | p >>= 1; |
| 647 | |
| 648 | while (p >= 1) { |
| 649 | uint16_t *py = in; |
| 650 | uint16_t *ey = in + oy * (ny - p2); |
| 651 | uint16_t i00, i01, i10, i11; |
| 652 | int oy1 = oy * p; |
| 653 | int oy2 = oy * p2; |
| 654 | int ox1 = ox * p; |
| 655 | int ox2 = ox * p2; |
| 656 | |
| 657 | for (; py <= ey; py += oy2) { |
| 658 | uint16_t *px = py; |
| 659 | uint16_t *ex = py + ox * (nx - p2); |
| 660 | |
| 661 | for (; px <= ex; px += ox2) { |
| 662 | uint16_t *p01 = px + ox1; |
| 663 | uint16_t *p10 = px + oy1; |
| 664 | uint16_t *p11 = p10 + ox1; |
| 665 | |
| 666 | if (w14) { |
| 667 | wdec14(*px, *p10, &i00, &i10); |
| 668 | wdec14(*p01, *p11, &i01, &i11); |
| 669 | wdec14(i00, i01, px, p01); |
| 670 | wdec14(i10, i11, p10, p11); |
| 671 | } else { |
| 672 | wdec16(*px, *p10, &i00, &i10); |
| 673 | wdec16(*p01, *p11, &i01, &i11); |
| 674 | wdec16(i00, i01, px, p01); |
| 675 | wdec16(i10, i11, p10, p11); |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | if (nx & p) { |
| 680 | uint16_t *p10 = px + oy1; |
| 681 | |
| 682 | if (w14) |
| 683 | wdec14(*px, *p10, &i00, p10); |
| 684 | else |
| 685 | wdec16(*px, *p10, &i00, p10); |
| 686 | |
| 687 | *px = i00; |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | if (ny & p) { |
| 692 | uint16_t *px = py; |
| 693 | uint16_t *ex = py + ox * (nx - p2); |
| 694 | |
| 695 | for (; px <= ex; px += ox2) { |
| 696 | uint16_t *p01 = px + ox1; |
| 697 | |
| 698 | if (w14) |
| 699 | wdec14(*px, *p01, &i00, p01); |
| 700 | else |
| 701 | wdec16(*px, *p01, &i00, p01); |
| 702 | |
| 703 | *px = i00; |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | p2 = p; |
| 708 | p >>= 1; |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | static int piz_uncompress(EXRContext *s, const uint8_t *src, int ssize, |
| 713 | int dsize, EXRThreadData *td) |
| 714 | { |
| 715 | GetByteContext gb; |
| 716 | uint16_t maxval, min_non_zero, max_non_zero; |
| 717 | uint16_t *ptr; |
| 718 | uint16_t *tmp = (uint16_t *)td->tmp; |
| 719 | uint8_t *out; |
| 720 | int ret, i, j; |
| 721 | |
| 722 | if (!td->bitmap) |
| 723 | td->bitmap = av_malloc(BITMAP_SIZE); |
| 724 | if (!td->lut) |
| 725 | td->lut = av_malloc(1 << 17); |
| 726 | if (!td->bitmap || !td->lut) { |
| 727 | av_freep(&td->bitmap); |
| 728 | av_freep(&td->lut); |
| 729 | return AVERROR(ENOMEM); |
| 730 | } |
| 731 | |
| 732 | bytestream2_init(&gb, src, ssize); |
| 733 | min_non_zero = bytestream2_get_le16(&gb); |
| 734 | max_non_zero = bytestream2_get_le16(&gb); |
| 735 | |
| 736 | if (max_non_zero >= BITMAP_SIZE) |
| 737 | return AVERROR_INVALIDDATA; |
| 738 | |
| 739 | memset(td->bitmap, 0, FFMIN(min_non_zero, BITMAP_SIZE)); |
| 740 | if (min_non_zero <= max_non_zero) |
| 741 | bytestream2_get_buffer(&gb, td->bitmap + min_non_zero, |
| 742 | max_non_zero - min_non_zero + 1); |
| 743 | memset(td->bitmap + max_non_zero, 0, BITMAP_SIZE - max_non_zero); |
| 744 | |
| 745 | maxval = reverse_lut(td->bitmap, td->lut); |
| 746 | |
| 747 | ret = huf_uncompress(&gb, tmp, dsize / sizeof(uint16_t)); |
| 748 | if (ret) |
| 749 | return ret; |
| 750 | |
| 751 | ptr = tmp; |
| 752 | for (i = 0; i < s->nb_channels; i++) { |
| 753 | EXRChannel *channel = &s->channels[i]; |
| 754 | int size = channel->pixel_type; |
| 755 | |
| 756 | for (j = 0; j < size; j++) |
| 757 | wav_decode(ptr + j, s->xdelta, size, s->ysize, |
| 758 | s->xdelta * size, maxval); |
| 759 | ptr += s->xdelta * s->ysize * size; |
| 760 | } |
| 761 | |
| 762 | apply_lut(td->lut, tmp, dsize / sizeof(uint16_t)); |
| 763 | |
| 764 | out = td->uncompressed_data; |
| 765 | for (i = 0; i < s->ysize; i++) |
| 766 | for (j = 0; j < s->nb_channels; j++) { |
| 767 | uint16_t *in = tmp + j * s->xdelta * s->ysize + i * s->xdelta; |
| 768 | memcpy(out, in, s->xdelta * 2); |
| 769 | out += s->xdelta * 2; |
| 770 | } |
| 771 | |
| 772 | return 0; |
| 773 | } |
| 774 | |
| 775 | static int pxr24_uncompress(EXRContext *s, const uint8_t *src, |
| 776 | int compressed_size, int uncompressed_size, |
| 777 | EXRThreadData *td) |
| 778 | { |
| 779 | unsigned long dest_len = uncompressed_size; |
| 780 | const uint8_t *in = td->tmp; |
| 781 | uint8_t *out; |
| 782 | int c, i, j; |
| 783 | |
| 784 | if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK || |
| 785 | dest_len != uncompressed_size) |
| 786 | return AVERROR_INVALIDDATA; |
| 787 | |
| 788 | out = td->uncompressed_data; |
| 789 | for (i = 0; i < s->ysize; i++) |
| 790 | for (c = 0; c < s->nb_channels; c++) { |
| 791 | EXRChannel *channel = &s->channels[c]; |
| 792 | const uint8_t *ptr[4]; |
| 793 | uint32_t pixel = 0; |
| 794 | |
| 795 | switch (channel->pixel_type) { |
| 796 | case EXR_FLOAT: |
| 797 | ptr[0] = in; |
| 798 | ptr[1] = ptr[0] + s->xdelta; |
| 799 | ptr[2] = ptr[1] + s->xdelta; |
| 800 | in = ptr[2] + s->xdelta; |
| 801 | |
| 802 | for (j = 0; j < s->xdelta; ++j) { |
| 803 | uint32_t diff = (*(ptr[0]++) << 24) | |
| 804 | (*(ptr[1]++) << 16) | |
| 805 | (*(ptr[2]++) << 8); |
| 806 | pixel += diff; |
| 807 | bytestream_put_le32(&out, pixel); |
| 808 | } |
| 809 | break; |
| 810 | case EXR_HALF: |
| 811 | ptr[0] = in; |
| 812 | ptr[1] = ptr[0] + s->xdelta; |
| 813 | in = ptr[1] + s->xdelta; |
| 814 | for (j = 0; j < s->xdelta; j++) { |
| 815 | uint32_t diff = (*(ptr[0]++) << 8) | *(ptr[1]++); |
| 816 | |
| 817 | pixel += diff; |
| 818 | bytestream_put_le16(&out, pixel); |
| 819 | } |
| 820 | break; |
| 821 | default: |
| 822 | return AVERROR_INVALIDDATA; |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | return 0; |
| 827 | } |
| 828 | |
| 829 | static int decode_block(AVCodecContext *avctx, void *tdata, |
| 830 | int jobnr, int threadnr) |
| 831 | { |
| 832 | EXRContext *s = avctx->priv_data; |
| 833 | AVFrame *const p = s->picture; |
| 834 | EXRThreadData *td = &s->thread_data[threadnr]; |
| 835 | const uint8_t *channel_buffer[4] = { 0 }; |
| 836 | const uint8_t *buf = s->buf; |
| 837 | uint64_t line_offset, uncompressed_size; |
| 838 | uint32_t xdelta = s->xdelta; |
| 839 | uint16_t *ptr_x; |
| 840 | uint8_t *ptr; |
| 841 | uint32_t data_size, line; |
| 842 | const uint8_t *src; |
| 843 | int axmax = (avctx->width - (s->xmax + 1)) * 2 * s->desc->nb_components; |
| 844 | int bxmin = s->xmin * 2 * s->desc->nb_components; |
| 845 | int i, x, buf_size = s->buf_size; |
| 846 | int ret; |
| 847 | float one_gamma = 1.0f / s->gamma; |
| 848 | |
| 849 | line_offset = AV_RL64(s->gb.buffer + jobnr * 8); |
| 850 | // Check if the buffer has the required bytes needed from the offset |
| 851 | if (line_offset > buf_size - 8) |
| 852 | return AVERROR_INVALIDDATA; |
| 853 | |
| 854 | src = buf + line_offset + 8; |
| 855 | line = AV_RL32(src - 8); |
| 856 | if (line < s->ymin || line > s->ymax) |
| 857 | return AVERROR_INVALIDDATA; |
| 858 | |
| 859 | data_size = AV_RL32(src - 4); |
| 860 | if (data_size <= 0 || data_size > buf_size) |
| 861 | return AVERROR_INVALIDDATA; |
| 862 | |
| 863 | s->ysize = FFMIN(s->scan_lines_per_block, s->ymax - line + 1); |
| 864 | uncompressed_size = s->scan_line_size * s->ysize; |
| 865 | if ((s->compression == EXR_RAW && (data_size != uncompressed_size || |
| 866 | line_offset > buf_size - uncompressed_size)) || |
| 867 | (s->compression != EXR_RAW && (data_size > uncompressed_size || |
| 868 | line_offset > buf_size - data_size))) { |
| 869 | return AVERROR_INVALIDDATA; |
| 870 | } |
| 871 | |
| 872 | if (data_size < uncompressed_size) { |
| 873 | av_fast_padded_malloc(&td->uncompressed_data, |
| 874 | &td->uncompressed_size, uncompressed_size); |
| 875 | av_fast_padded_malloc(&td->tmp, &td->tmp_size, uncompressed_size); |
| 876 | if (!td->uncompressed_data || !td->tmp) |
| 877 | return AVERROR(ENOMEM); |
| 878 | |
| 879 | ret = AVERROR_INVALIDDATA; |
| 880 | switch (s->compression) { |
| 881 | case EXR_ZIP1: |
| 882 | case EXR_ZIP16: |
| 883 | ret = zip_uncompress(src, data_size, uncompressed_size, td); |
| 884 | break; |
| 885 | case EXR_PIZ: |
| 886 | ret = piz_uncompress(s, src, data_size, uncompressed_size, td); |
| 887 | break; |
| 888 | case EXR_PXR24: |
| 889 | ret = pxr24_uncompress(s, src, data_size, uncompressed_size, td); |
| 890 | break; |
| 891 | case EXR_RLE: |
| 892 | ret = rle_uncompress(src, data_size, uncompressed_size, td); |
| 893 | } |
| 894 | if (ret < 0) { |
| 895 | av_log(avctx, AV_LOG_ERROR, "decode_block() failed.\n"); |
| 896 | return ret; |
| 897 | } |
| 898 | src = td->uncompressed_data; |
| 899 | } |
| 900 | |
| 901 | channel_buffer[0] = src + xdelta * s->channel_offsets[0]; |
| 902 | channel_buffer[1] = src + xdelta * s->channel_offsets[1]; |
| 903 | channel_buffer[2] = src + xdelta * s->channel_offsets[2]; |
| 904 | if (s->channel_offsets[3] >= 0) |
| 905 | channel_buffer[3] = src + xdelta * s->channel_offsets[3]; |
| 906 | |
| 907 | ptr = p->data[0] + line * p->linesize[0]; |
| 908 | for (i = 0; |
| 909 | i < s->scan_lines_per_block && line + i <= s->ymax; |
| 910 | i++, ptr += p->linesize[0]) { |
| 911 | const uint8_t *r, *g, *b, *a; |
| 912 | |
| 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]; |
| 918 | |
| 919 | ptr_x = (uint16_t *) ptr; |
| 920 | |
| 921 | // Zero out the start if xmin is not 0 |
| 922 | memset(ptr_x, 0, bxmin); |
| 923 | ptr_x += s->xmin * s->desc->nb_components; |
| 924 | if (s->pixel_type == EXR_FLOAT) { |
| 925 | // 32-bit |
| 926 | for (x = 0; x < xdelta; x++) { |
| 927 | union av_intfloat32 t; |
| 928 | t.i = bytestream_get_le32(&r); |
| 929 | if ( t.f > 0.0f ) /* avoid negative values */ |
| 930 | t.f = powf(t.f, one_gamma); |
| 931 | *ptr_x++ = exr_flt2uint(t.i); |
| 932 | |
| 933 | t.i = bytestream_get_le32(&g); |
| 934 | if ( t.f > 0.0f ) |
| 935 | t.f = powf(t.f, one_gamma); |
| 936 | *ptr_x++ = exr_flt2uint(t.i); |
| 937 | |
| 938 | t.i = bytestream_get_le32(&b); |
| 939 | if ( t.f > 0.0f ) |
| 940 | t.f = powf(t.f, one_gamma); |
| 941 | *ptr_x++ = exr_flt2uint(t.i); |
| 942 | if (channel_buffer[3]) |
| 943 | *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a)); |
| 944 | } |
| 945 | } else { |
| 946 | // 16-bit |
| 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]) |
| 952 | *ptr_x++ = exr_halflt2uint(bytestream_get_le16(&a)); |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | // Zero out the end if xmax+1 is not w |
| 957 | memset(ptr_x, 0, axmax); |
| 958 | |
| 959 | channel_buffer[0] += s->scan_line_size; |
| 960 | channel_buffer[1] += s->scan_line_size; |
| 961 | channel_buffer[2] += s->scan_line_size; |
| 962 | if (channel_buffer[3]) |
| 963 | channel_buffer[3] += s->scan_line_size; |
| 964 | } |
| 965 | |
| 966 | return 0; |
| 967 | } |
| 968 | |
| 969 | /** |
| 970 | * Check if the variable name corresponds to its data type. |
| 971 | * |
| 972 | * @param s the EXRContext |
| 973 | * @param value_name name of the variable to check |
| 974 | * @param value_type type of the variable to check |
| 975 | * @param minimum_length minimum length of the variable data |
| 976 | * |
| 977 | * @return bytes to read containing variable data |
| 978 | * -1 if variable is not found |
| 979 | * 0 if buffer ended prematurely |
| 980 | */ |
| 981 | static int check_header_variable(EXRContext *s, |
| 982 | const char *value_name, |
| 983 | const char *value_type, |
| 984 | unsigned int minimum_length) |
| 985 | { |
| 986 | int var_size = -1; |
| 987 | |
| 988 | if (bytestream2_get_bytes_left(&s->gb) >= minimum_length && |
| 989 | !strcmp(s->gb.buffer, value_name)) { |
| 990 | // found value_name, jump to value_type (null terminated strings) |
| 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); |
| 995 | // don't go read past boundaries |
| 996 | if (var_size > bytestream2_get_bytes_left(&s->gb)) |
| 997 | var_size = 0; |
| 998 | } else { |
| 999 | // value_type not found, reset the buffer |
| 1000 | s->gb.buffer -= strlen(value_name) + 1; |
| 1001 | av_log(s->avctx, AV_LOG_WARNING, |
| 1002 | "Unknown data type %s for header variable %s.\n", |
| 1003 | value_type, value_name); |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | return var_size; |
| 1008 | } |
| 1009 | |
| 1010 | static int decode_header(EXRContext *s) |
| 1011 | { |
| 1012 | int current_channel_offset = 0; |
| 1013 | int magic_number, version, flags, i; |
| 1014 | |
| 1015 | if (bytestream2_get_bytes_left(&s->gb) < 10) { |
| 1016 | av_log(s->avctx, AV_LOG_ERROR, "Header too short to parse.\n"); |
| 1017 | return AVERROR_INVALIDDATA; |
| 1018 | } |
| 1019 | |
| 1020 | magic_number = bytestream2_get_le32(&s->gb); |
| 1021 | if (magic_number != 20000630) { |
| 1022 | /* As per documentation of OpenEXR, it is supposed to be |
| 1023 | * int 20000630 little-endian */ |
| 1024 | av_log(s->avctx, AV_LOG_ERROR, "Wrong magic number %d.\n", magic_number); |
| 1025 | return AVERROR_INVALIDDATA; |
| 1026 | } |
| 1027 | |
| 1028 | version = bytestream2_get_byte(&s->gb); |
| 1029 | if (version != 2) { |
| 1030 | avpriv_report_missing_feature(s->avctx, "Version %d", version); |
| 1031 | return AVERROR_PATCHWELCOME; |
| 1032 | } |
| 1033 | |
| 1034 | flags = bytestream2_get_le24(&s->gb); |
| 1035 | if (flags & 0x02) { |
| 1036 | avpriv_report_missing_feature(s->avctx, "Tile support"); |
| 1037 | return AVERROR_PATCHWELCOME; |
| 1038 | } |
| 1039 | |
| 1040 | // Parse the header |
| 1041 | while (bytestream2_get_bytes_left(&s->gb) > 0 && *s->gb.buffer) { |
| 1042 | int var_size; |
| 1043 | if ((var_size = check_header_variable(s, "channels", |
| 1044 | "chlist", 38)) >= 0) { |
| 1045 | GetByteContext ch_gb; |
| 1046 | if (!var_size) |
| 1047 | return AVERROR_INVALIDDATA; |
| 1048 | |
| 1049 | bytestream2_init(&ch_gb, s->gb.buffer, var_size); |
| 1050 | |
| 1051 | while (bytestream2_get_bytes_left(&ch_gb) >= 19) { |
| 1052 | EXRChannel *channel; |
| 1053 | enum ExrPixelType current_pixel_type; |
| 1054 | int channel_index = -1; |
| 1055 | int xsub, ysub; |
| 1056 | |
| 1057 | if (strcmp(s->layer, "") != 0) { |
| 1058 | if (strncmp(ch_gb.buffer, s->layer, strlen(s->layer)) == 0) { |
| 1059 | ch_gb.buffer += strlen(s->layer); |
| 1060 | if (*ch_gb.buffer == '.') |
| 1061 | ch_gb.buffer++; /* skip dot if not given */ |
| 1062 | av_log(s->avctx, AV_LOG_INFO, |
| 1063 | "Layer %s.%s matched.\n", s->layer, ch_gb.buffer); |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | if (!strcmp(ch_gb.buffer, "R") || |
| 1068 | !strcmp(ch_gb.buffer, "X") || |
| 1069 | !strcmp(ch_gb.buffer, "U")) |
| 1070 | channel_index = 0; |
| 1071 | else if (!strcmp(ch_gb.buffer, "G") || |
| 1072 | !strcmp(ch_gb.buffer, "Y") || |
| 1073 | !strcmp(ch_gb.buffer, "V")) |
| 1074 | channel_index = 1; |
| 1075 | else if (!strcmp(ch_gb.buffer, "B") || |
| 1076 | !strcmp(ch_gb.buffer, "Z") || |
| 1077 | !strcmp(ch_gb.buffer, "W")) |
| 1078 | channel_index = 2; |
| 1079 | else if (!strcmp(ch_gb.buffer, "A")) |
| 1080 | channel_index = 3; |
| 1081 | else |
| 1082 | av_log(s->avctx, AV_LOG_WARNING, |
| 1083 | "Unsupported channel %.256s.\n", ch_gb.buffer); |
| 1084 | |
| 1085 | /* skip until you get a 0 */ |
| 1086 | while (bytestream2_get_bytes_left(&ch_gb) > 0 && |
| 1087 | bytestream2_get_byte(&ch_gb)) |
| 1088 | continue; |
| 1089 | |
| 1090 | if (bytestream2_get_bytes_left(&ch_gb) < 4) { |
| 1091 | av_log(s->avctx, AV_LOG_ERROR, "Incomplete header.\n"); |
| 1092 | return AVERROR_INVALIDDATA; |
| 1093 | } |
| 1094 | |
| 1095 | current_pixel_type = bytestream2_get_le32(&ch_gb); |
| 1096 | if (current_pixel_type >= EXR_UNKNOWN) { |
| 1097 | avpriv_report_missing_feature(s->avctx, |
| 1098 | "Pixel type %d.\n", |
| 1099 | current_pixel_type); |
| 1100 | return AVERROR_PATCHWELCOME; |
| 1101 | } |
| 1102 | |
| 1103 | bytestream2_skip(&ch_gb, 4); |
| 1104 | xsub = bytestream2_get_le32(&ch_gb); |
| 1105 | ysub = bytestream2_get_le32(&ch_gb); |
| 1106 | if (xsub != 1 || ysub != 1) { |
| 1107 | avpriv_report_missing_feature(s->avctx, |
| 1108 | "Subsampling %dx%d", |
| 1109 | xsub, ysub); |
| 1110 | return AVERROR_PATCHWELCOME; |
| 1111 | } |
| 1112 | |
| 1113 | if (channel_index >= 0) { |
| 1114 | if (s->pixel_type != EXR_UNKNOWN && |
| 1115 | s->pixel_type != current_pixel_type) { |
| 1116 | av_log(s->avctx, AV_LOG_ERROR, |
| 1117 | "RGB channels not of the same depth.\n"); |
| 1118 | return AVERROR_INVALIDDATA; |
| 1119 | } |
| 1120 | s->pixel_type = current_pixel_type; |
| 1121 | s->channel_offsets[channel_index] = current_channel_offset; |
| 1122 | } |
| 1123 | |
| 1124 | s->channels = av_realloc(s->channels, |
| 1125 | ++s->nb_channels * sizeof(EXRChannel)); |
| 1126 | if (!s->channels) |
| 1127 | return AVERROR(ENOMEM); |
| 1128 | channel = &s->channels[s->nb_channels - 1]; |
| 1129 | channel->pixel_type = current_pixel_type; |
| 1130 | channel->xsub = xsub; |
| 1131 | channel->ysub = ysub; |
| 1132 | |
| 1133 | current_channel_offset += 1 << current_pixel_type; |
| 1134 | } |
| 1135 | |
| 1136 | /* Check if all channels are set with an offset or if the channels |
| 1137 | * are causing an overflow */ |
| 1138 | if (FFMIN3(s->channel_offsets[0], |
| 1139 | s->channel_offsets[1], |
| 1140 | s->channel_offsets[2]) < 0) { |
| 1141 | if (s->channel_offsets[0] < 0) |
| 1142 | av_log(s->avctx, AV_LOG_ERROR, "Missing red channel.\n"); |
| 1143 | if (s->channel_offsets[1] < 0) |
| 1144 | av_log(s->avctx, AV_LOG_ERROR, "Missing green channel.\n"); |
| 1145 | if (s->channel_offsets[2] < 0) |
| 1146 | av_log(s->avctx, AV_LOG_ERROR, "Missing blue channel.\n"); |
| 1147 | return AVERROR_INVALIDDATA; |
| 1148 | } |
| 1149 | |
| 1150 | // skip one last byte and update main gb |
| 1151 | s->gb.buffer = ch_gb.buffer + 1; |
| 1152 | continue; |
| 1153 | } else if ((var_size = check_header_variable(s, "dataWindow", "box2i", |
| 1154 | 31)) >= 0) { |
| 1155 | if (!var_size) |
| 1156 | return AVERROR_INVALIDDATA; |
| 1157 | |
| 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); |
| 1162 | s->xdelta = (s->xmax - s->xmin) + 1; |
| 1163 | s->ydelta = (s->ymax - s->ymin) + 1; |
| 1164 | |
| 1165 | continue; |
| 1166 | } else if ((var_size = check_header_variable(s, "displayWindow", |
| 1167 | "box2i", 34)) >= 0) { |
| 1168 | if (!var_size) |
| 1169 | return AVERROR_INVALIDDATA; |
| 1170 | |
| 1171 | bytestream2_skip(&s->gb, 8); |
| 1172 | s->w = bytestream2_get_le32(&s->gb) + 1; |
| 1173 | s->h = bytestream2_get_le32(&s->gb) + 1; |
| 1174 | |
| 1175 | continue; |
| 1176 | } else if ((var_size = check_header_variable(s, "lineOrder", |
| 1177 | "lineOrder", 25)) >= 0) { |
| 1178 | int line_order; |
| 1179 | if (!var_size) |
| 1180 | return AVERROR_INVALIDDATA; |
| 1181 | |
| 1182 | line_order = bytestream2_get_byte(&s->gb); |
| 1183 | av_log(s->avctx, AV_LOG_DEBUG, "line order: %d.\n", line_order); |
| 1184 | if (line_order > 2) { |
| 1185 | av_log(s->avctx, AV_LOG_ERROR, "Unknown line order.\n"); |
| 1186 | return AVERROR_INVALIDDATA; |
| 1187 | } |
| 1188 | |
| 1189 | continue; |
| 1190 | } else if ((var_size = check_header_variable(s, "pixelAspectRatio", |
| 1191 | "float", 31)) >= 0) { |
| 1192 | if (!var_size) |
| 1193 | return AVERROR_INVALIDDATA; |
| 1194 | |
| 1195 | ff_set_sar(s->avctx, |
| 1196 | av_d2q(av_int2float(bytestream2_get_le32(&s->gb)), 255)); |
| 1197 | |
| 1198 | continue; |
| 1199 | } else if ((var_size = check_header_variable(s, "compression", |
| 1200 | "compression", 29)) >= 0) { |
| 1201 | if (!var_size) |
| 1202 | return AVERROR_INVALIDDATA; |
| 1203 | |
| 1204 | if (s->compression == EXR_UNKN) |
| 1205 | s->compression = bytestream2_get_byte(&s->gb); |
| 1206 | else |
| 1207 | av_log(s->avctx, AV_LOG_WARNING, |
| 1208 | "Found more than one compression attribute.\n"); |
| 1209 | |
| 1210 | continue; |
| 1211 | } |
| 1212 | |
| 1213 | // Check if there are enough bytes for a header |
| 1214 | if (bytestream2_get_bytes_left(&s->gb) <= 9) { |
| 1215 | av_log(s->avctx, AV_LOG_ERROR, "Incomplete header\n"); |
| 1216 | return AVERROR_INVALIDDATA; |
| 1217 | } |
| 1218 | |
| 1219 | // Process unknown variables |
| 1220 | for (i = 0; i < 2; i++) // value_name and value_type |
| 1221 | while (bytestream2_get_byte(&s->gb) != 0); |
| 1222 | |
| 1223 | // Skip variable length |
| 1224 | bytestream2_skip(&s->gb, bytestream2_get_le32(&s->gb)); |
| 1225 | } |
| 1226 | |
| 1227 | if (s->compression == EXR_UNKN) { |
| 1228 | av_log(s->avctx, AV_LOG_ERROR, "Missing compression attribute.\n"); |
| 1229 | return AVERROR_INVALIDDATA; |
| 1230 | } |
| 1231 | s->scan_line_size = s->xdelta * current_channel_offset; |
| 1232 | |
| 1233 | if (bytestream2_get_bytes_left(&s->gb) <= 0) { |
| 1234 | av_log(s->avctx, AV_LOG_ERROR, "Incomplete frame.\n"); |
| 1235 | return AVERROR_INVALIDDATA; |
| 1236 | } |
| 1237 | |
| 1238 | // aaand we are done |
| 1239 | bytestream2_skip(&s->gb, 1); |
| 1240 | return 0; |
| 1241 | } |
| 1242 | |
| 1243 | static int decode_frame(AVCodecContext *avctx, void *data, |
| 1244 | int *got_frame, AVPacket *avpkt) |
| 1245 | { |
| 1246 | EXRContext *s = avctx->priv_data; |
| 1247 | ThreadFrame frame = { .f = data }; |
| 1248 | AVFrame *picture = data; |
| 1249 | uint8_t *ptr; |
| 1250 | |
| 1251 | int y, ret; |
| 1252 | int out_line_size; |
| 1253 | int scan_line_blocks; |
| 1254 | |
| 1255 | bytestream2_init(&s->gb, avpkt->data, avpkt->size); |
| 1256 | |
| 1257 | if ((ret = decode_header(s)) < 0) |
| 1258 | return ret; |
| 1259 | |
| 1260 | switch (s->pixel_type) { |
| 1261 | case EXR_FLOAT: |
| 1262 | case EXR_HALF: |
| 1263 | if (s->channel_offsets[3] >= 0) |
| 1264 | avctx->pix_fmt = AV_PIX_FMT_RGBA64; |
| 1265 | else |
| 1266 | avctx->pix_fmt = AV_PIX_FMT_RGB48; |
| 1267 | break; |
| 1268 | case EXR_UINT: |
| 1269 | avpriv_request_sample(avctx, "32-bit unsigned int"); |
| 1270 | return AVERROR_PATCHWELCOME; |
| 1271 | default: |
| 1272 | av_log(avctx, AV_LOG_ERROR, "Missing channel list.\n"); |
| 1273 | return AVERROR_INVALIDDATA; |
| 1274 | } |
| 1275 | |
| 1276 | switch (s->compression) { |
| 1277 | case EXR_RAW: |
| 1278 | case EXR_RLE: |
| 1279 | case EXR_ZIP1: |
| 1280 | s->scan_lines_per_block = 1; |
| 1281 | break; |
| 1282 | case EXR_PXR24: |
| 1283 | case EXR_ZIP16: |
| 1284 | s->scan_lines_per_block = 16; |
| 1285 | break; |
| 1286 | case EXR_PIZ: |
| 1287 | s->scan_lines_per_block = 32; |
| 1288 | break; |
| 1289 | default: |
| 1290 | avpriv_report_missing_feature(avctx, "Compression %d", s->compression); |
| 1291 | return AVERROR_PATCHWELCOME; |
| 1292 | } |
| 1293 | |
| 1294 | /* Verify the xmin, xmax, ymin, ymax and xdelta before setting |
| 1295 | * the actual image size. */ |
| 1296 | if (s->xmin > s->xmax || |
| 1297 | s->ymin > s->ymax || |
| 1298 | s->xdelta != s->xmax - s->xmin + 1 || |
| 1299 | s->xmax >= s->w || |
| 1300 | s->ymax >= s->h) { |
| 1301 | av_log(avctx, AV_LOG_ERROR, "Wrong or missing size information.\n"); |
| 1302 | return AVERROR_INVALIDDATA; |
| 1303 | } |
| 1304 | |
| 1305 | if ((ret = ff_set_dimensions(avctx, s->w, s->h)) < 0) |
| 1306 | return ret; |
| 1307 | |
| 1308 | s->desc = av_pix_fmt_desc_get(avctx->pix_fmt); |
| 1309 | if (!s->desc) |
| 1310 | return AVERROR_INVALIDDATA; |
| 1311 | out_line_size = avctx->width * 2 * s->desc->nb_components; |
| 1312 | scan_line_blocks = (s->ydelta + s->scan_lines_per_block - 1) / |
| 1313 | s->scan_lines_per_block; |
| 1314 | |
| 1315 | if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0) |
| 1316 | return ret; |
| 1317 | |
| 1318 | if (bytestream2_get_bytes_left(&s->gb) < scan_line_blocks * 8) |
| 1319 | return AVERROR_INVALIDDATA; |
| 1320 | |
| 1321 | // save pointer we are going to use in decode_block |
| 1322 | s->buf = avpkt->data; |
| 1323 | s->buf_size = avpkt->size; |
| 1324 | ptr = picture->data[0]; |
| 1325 | |
| 1326 | // Zero out the start if ymin is not 0 |
| 1327 | for (y = 0; y < s->ymin; y++) { |
| 1328 | memset(ptr, 0, out_line_size); |
| 1329 | ptr += picture->linesize[0]; |
| 1330 | } |
| 1331 | |
| 1332 | s->picture = picture; |
| 1333 | avctx->execute2(avctx, decode_block, s->thread_data, NULL, scan_line_blocks); |
| 1334 | |
| 1335 | // Zero out the end if ymax+1 is not h |
| 1336 | for (y = s->ymax + 1; y < avctx->height; y++) { |
| 1337 | memset(ptr, 0, out_line_size); |
| 1338 | ptr += picture->linesize[0]; |
| 1339 | } |
| 1340 | |
| 1341 | picture->pict_type = AV_PICTURE_TYPE_I; |
| 1342 | *got_frame = 1; |
| 1343 | |
| 1344 | return avpkt->size; |
| 1345 | } |
| 1346 | |
| 1347 | static av_cold int decode_init(AVCodecContext *avctx) |
| 1348 | { |
| 1349 | uint32_t i; |
| 1350 | union av_intfloat32 t; |
| 1351 | EXRContext *s = avctx->priv_data; |
| 1352 | float one_gamma = 1.0f / s->gamma; |
| 1353 | |
| 1354 | s->avctx = avctx; |
| 1355 | s->xmin = ~0; |
| 1356 | s->xmax = ~0; |
| 1357 | s->ymin = ~0; |
| 1358 | s->ymax = ~0; |
| 1359 | s->xdelta = ~0; |
| 1360 | s->ydelta = ~0; |
| 1361 | s->channel_offsets[0] = -1; |
| 1362 | s->channel_offsets[1] = -1; |
| 1363 | s->channel_offsets[2] = -1; |
| 1364 | s->channel_offsets[3] = -1; |
| 1365 | s->pixel_type = EXR_UNKNOWN; |
| 1366 | s->compression = EXR_UNKN; |
| 1367 | s->nb_channels = 0; |
| 1368 | s->w = 0; |
| 1369 | s->h = 0; |
| 1370 | |
| 1371 | if ( one_gamma > 0.9999f && one_gamma < 1.0001f ) { |
| 1372 | for ( i = 0; i < 65536; ++i ) { |
| 1373 | s->gamma_table[i] = exr_halflt2uint(i); |
| 1374 | } |
| 1375 | } else { |
| 1376 | for ( i = 0; i < 65536; ++i ) { |
| 1377 | t = exr_half2float(i); |
| 1378 | /* If negative value we reuse half value */ |
| 1379 | if ( t.f <= 0.0f ) { |
| 1380 | s->gamma_table[i] = exr_halflt2uint(i); |
| 1381 | } else { |
| 1382 | t.f = powf(t.f, one_gamma); |
| 1383 | s->gamma_table[i] = exr_flt2uint(t.i); |
| 1384 | } |
| 1385 | } |
| 1386 | } |
| 1387 | |
| 1388 | // allocate thread data, used for non EXR_RAW compreesion types |
| 1389 | s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData)); |
| 1390 | if (!s->thread_data) |
| 1391 | return AVERROR_INVALIDDATA; |
| 1392 | |
| 1393 | return 0; |
| 1394 | } |
| 1395 | |
| 1396 | static int decode_init_thread_copy(AVCodecContext *avctx) |
| 1397 | { EXRContext *s = avctx->priv_data; |
| 1398 | |
| 1399 | // allocate thread data, used for non EXR_RAW compreesion types |
| 1400 | s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData)); |
| 1401 | if (!s->thread_data) |
| 1402 | return AVERROR_INVALIDDATA; |
| 1403 | |
| 1404 | return 0; |
| 1405 | } |
| 1406 | |
| 1407 | static av_cold int decode_end(AVCodecContext *avctx) |
| 1408 | { |
| 1409 | EXRContext *s = avctx->priv_data; |
| 1410 | int i; |
| 1411 | for (i = 0; i < avctx->thread_count; i++) { |
| 1412 | EXRThreadData *td = &s->thread_data[i]; |
| 1413 | av_freep(&td->uncompressed_data); |
| 1414 | av_freep(&td->tmp); |
| 1415 | av_freep(&td->bitmap); |
| 1416 | av_freep(&td->lut); |
| 1417 | } |
| 1418 | |
| 1419 | av_freep(&s->thread_data); |
| 1420 | av_freep(&s->channels); |
| 1421 | |
| 1422 | return 0; |
| 1423 | } |
| 1424 | |
| 1425 | #define OFFSET(x) offsetof(EXRContext, x) |
| 1426 | #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM |
| 1427 | static const AVOption options[] = { |
| 1428 | { "layer", "Set the decoding layer", OFFSET(layer), |
| 1429 | AV_OPT_TYPE_STRING, { .str = "" }, 0, 0, VD }, |
| 1430 | { "gamma", "Set the float gamma value when decoding (experimental/unsupported)", OFFSET(gamma), |
| 1431 | AV_OPT_TYPE_FLOAT, { .dbl = 1.0f }, 0.001, FLT_MAX, VD }, |
| 1432 | { NULL }, |
| 1433 | }; |
| 1434 | |
| 1435 | static const AVClass exr_class = { |
| 1436 | .class_name = "EXR", |
| 1437 | .item_name = av_default_item_name, |
| 1438 | .option = options, |
| 1439 | .version = LIBAVUTIL_VERSION_INT, |
| 1440 | }; |
| 1441 | |
| 1442 | AVCodec ff_exr_decoder = { |
| 1443 | .name = "exr", |
| 1444 | .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"), |
| 1445 | .type = AVMEDIA_TYPE_VIDEO, |
| 1446 | .id = AV_CODEC_ID_EXR, |
| 1447 | .priv_data_size = sizeof(EXRContext), |
| 1448 | .init = decode_init, |
| 1449 | .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy), |
| 1450 | .close = decode_end, |
| 1451 | .decode = decode_frame, |
| 1452 | .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS | |
| 1453 | CODEC_CAP_SLICE_THREADS, |
| 1454 | .priv_class = &exr_class, |
| 1455 | }; |