Commit 95582b5c authored by Michael Niedermayer's avatar Michael Niedermayer

Merge commit '38389058'

* commit '38389058':
  OpenEXR decoder

Conflicts:
	Changelog
	configure
	libavcodec/Makefile
	libavcodec/avcodec.h
	libavcodec/exr.c
	libavcodec/version.h
Merged-by: 's avatarMichael Niedermayer <michaelni@gmx.at>
parents 9e3c8f61 38389058
...@@ -11,6 +11,7 @@ version <next>: ...@@ -11,6 +11,7 @@ version <next>:
- Improvments to the BRender PIX image decoder - Improvments to the BRender PIX image decoder
- Improvments to the XBM decoder - Improvments to the XBM decoder
- QTKit input device - QTKit input device
- improvments to OpenEXR image decoder
version 2.2: version 2.2:
......
...@@ -289,6 +289,7 @@ enum AVCodecID { ...@@ -289,6 +289,7 @@ enum AVCodecID {
AV_CODEC_ID_ALIAS_PIX, AV_CODEC_ID_ALIAS_PIX,
AV_CODEC_ID_BRENDER_PIX_DEPRECATED, AV_CODEC_ID_BRENDER_PIX_DEPRECATED,
AV_CODEC_ID_PAF_VIDEO_DEPRECATED, AV_CODEC_ID_PAF_VIDEO_DEPRECATED,
AV_CODEC_ID_EXR_DEPRECATED,
AV_CODEC_ID_BRENDER_PIX= MKBETAG('B','P','I','X'), AV_CODEC_ID_BRENDER_PIX= MKBETAG('B','P','I','X'),
AV_CODEC_ID_Y41P = MKBETAG('Y','4','1','P'), AV_CODEC_ID_Y41P = MKBETAG('Y','4','1','P'),
......
...@@ -1102,14 +1102,6 @@ static const AVCodecDescriptor codec_descriptors[] = { ...@@ -1102,14 +1102,6 @@ static const AVCodecDescriptor codec_descriptors[] = {
.long_name = NULL_IF_CONFIG_SMALL("Escape 130"), .long_name = NULL_IF_CONFIG_SMALL("Escape 130"),
.props = AV_CODEC_PROP_LOSSY, .props = AV_CODEC_PROP_LOSSY,
}, },
{
.id = AV_CODEC_ID_EXR,
.type = AVMEDIA_TYPE_VIDEO,
.name = "exr",
.long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),
.props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY |
AV_CODEC_PROP_LOSSLESS,
},
{ {
.id = AV_CODEC_ID_AVRP, .id = AV_CODEC_ID_AVRP,
.type = AVMEDIA_TYPE_VIDEO, .type = AVMEDIA_TYPE_VIDEO,
...@@ -1271,6 +1263,14 @@ static const AVCodecDescriptor codec_descriptors[] = { ...@@ -1271,6 +1263,14 @@ static const AVCodecDescriptor codec_descriptors[] = {
.long_name = NULL_IF_CONFIG_SMALL("DPX (Digital Picture Exchange) image"), .long_name = NULL_IF_CONFIG_SMALL("DPX (Digital Picture Exchange) image"),
.props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSLESS, .props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSLESS,
}, },
{
.id = AV_CODEC_ID_EXR,
.type = AVMEDIA_TYPE_VIDEO,
.name = "exr",
.long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),
.props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY |
AV_CODEC_PROP_LOSSLESS,
},
{ {
.id = AV_CODEC_ID_GIF, .id = AV_CODEC_ID_GIF,
.type = AVMEDIA_TYPE_VIDEO, .type = AVMEDIA_TYPE_VIDEO,
......
...@@ -32,35 +32,37 @@ ...@@ -32,35 +32,37 @@
#include <zlib.h> #include <zlib.h>
#include "get_bits.h" #include "libavutil/imgutils.h"
#include "libavutil/opt.h"
#include "avcodec.h" #include "avcodec.h"
#include "bytestream.h" #include "bytestream.h"
#include "get_bits.h"
#include "internal.h" #include "internal.h"
#include "mathops.h" #include "mathops.h"
#include "thread.h" #include "thread.h"
#include "libavutil/imgutils.h"
#include "libavutil/avassert.h"
#include "libavutil/opt.h"
enum ExrCompr { enum ExrCompr {
EXR_RAW = 0, EXR_RAW,
EXR_RLE = 1, EXR_RLE,
EXR_ZIP1 = 2, EXR_ZIP1,
EXR_ZIP16 = 3, EXR_ZIP16,
EXR_PIZ = 4, EXR_PIZ,
EXR_PXR24 = 5, EXR_PXR24,
EXR_B44 = 6, EXR_B44,
EXR_B44A = 7, EXR_B44A,
EXR_UNKN,
}; };
enum ExrPixelType { enum ExrPixelType {
EXR_UINT, EXR_UINT,
EXR_HALF, EXR_HALF,
EXR_FLOAT EXR_FLOAT,
EXR_UNKNOWN,
}; };
typedef struct EXRChannel { typedef struct EXRChannel {
int xsub, ysub; int xsub, ysub;
enum ExrPixelType pixel_type; enum ExrPixelType pixel_type;
} EXRChannel; } EXRChannel;
...@@ -76,52 +78,41 @@ typedef struct EXRThreadData { ...@@ -76,52 +78,41 @@ typedef struct EXRThreadData {
} EXRThreadData; } EXRThreadData;
typedef struct EXRContext { typedef struct EXRContext {
AVClass *class; AVClass *class;
AVFrame *picture; AVFrame *picture;
int compr; AVCodecContext *avctx;
enum ExrCompr compression;
enum ExrPixelType pixel_type; enum ExrPixelType pixel_type;
int channel_offsets[4]; // 0 = red, 1 = green, 2 = blue and 3 = alpha int channel_offsets[4]; // 0 = red, 1 = green, 2 = blue and 3 = alpha
const AVPixFmtDescriptor *desc; const AVPixFmtDescriptor *desc;
int w, h;
uint32_t xmax, xmin; uint32_t xmax, xmin;
uint32_t ymax, ymin; uint32_t ymax, ymin;
uint32_t xdelta, ydelta; uint32_t xdelta, ydelta;
int ysize; int ysize;
uint64_t scan_line_size; uint64_t scan_line_size;
int scan_lines_per_block; int scan_lines_per_block;
const uint8_t *buf, *table; GetByteContext gb;
const uint8_t *buf;
int buf_size; int buf_size;
EXRChannel *channels; EXRChannel *channels;
int nb_channels; int nb_channels;
EXRThreadData *thread_data; EXRThreadData *thread_data;
int thread_data_size;
const char* layer; const char *layer;
} EXRContext; } EXRContext;
#define OFFSET(x) offsetof(EXRContext, x)
#define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
static const AVOption options[] = {
{ "layer", "Set the decoding layer", OFFSET(layer), AV_OPT_TYPE_STRING, { .str = "" }, 0, 0, VD},
{ NULL },
};
static const AVClass exr_class = {
.class_name = "EXR",
.item_name = av_default_item_name,
.option = options,
.version = LIBAVUTIL_VERSION_INT,
};
/** /**
* Converts from 32-bit float as uint32_t to uint16_t * Convert from 32-bit float as uint32_t to uint16_t.
* *
* @param v 32-bit float * @param v 32-bit float
*
* @return normalized 16-bit unsigned int * @return normalized 16-bit unsigned int
*/ */
static inline uint16_t exr_flt2uint(uint32_t v) static inline uint16_t exr_flt2uint(uint32_t v)
...@@ -129,7 +120,7 @@ static inline uint16_t exr_flt2uint(uint32_t v) ...@@ -129,7 +120,7 @@ static inline uint16_t exr_flt2uint(uint32_t v)
unsigned int exp = v >> 23; unsigned int exp = v >> 23;
// "HACK": negative values result in exp< 0, so clipping them to 0 // "HACK": negative values result in exp< 0, so clipping them to 0
// is also handled by this condition, avoids explicit check for sign bit. // is also handled by this condition, avoids explicit check for sign bit.
if (exp<= 127 + 7 - 24) // we would shift out all bits anyway if (exp <= 127 + 7 - 24) // we would shift out all bits anyway
return 0; return 0;
if (exp >= 127) if (exp >= 127)
return 0xffff; return 0xffff;
...@@ -138,83 +129,32 @@ static inline uint16_t exr_flt2uint(uint32_t v) ...@@ -138,83 +129,32 @@ static inline uint16_t exr_flt2uint(uint32_t v)
} }
/** /**
* Converts from 16-bit float as uint16_t to uint16_t * Convert from 16-bit float as uint16_t to uint16_t.
* *
* @param v 16-bit float * @param v 16-bit float
*
* @return normalized 16-bit unsigned int * @return normalized 16-bit unsigned int
*/ */
static inline uint16_t exr_halflt2uint(uint16_t v) static inline uint16_t exr_halflt2uint(uint16_t v)
{ {
unsigned exp = 14 - (v >> 10); unsigned exp = 14 - (v >> 10);
if (exp >= 14) { if (exp >= 14) {
if (exp == 14) return (v >> 9) & 1; if (exp == 14)
else return (v & 0x8000) ? 0 : 0xffff; return (v >> 9) & 1;
else
return (v & 0x8000) ? 0 : 0xffff;
} }
v <<= 6; v <<= 6;
return (v + (1 << 16)) >> (exp + 1); return (v + (1 << 16)) >> (exp + 1);
} }
/**
* Gets the size of the header variable
*
* @param **buf the current pointer location in the header where
* the variable data starts
* @param *buf_end pointer location of the end of the buffer
* @return size of variable data
*/
static unsigned int get_header_variable_length(const uint8_t **buf,
const uint8_t *buf_end)
{
unsigned int variable_buffer_data_size = bytestream_get_le32(buf);
if (variable_buffer_data_size >= buf_end - *buf)
return 0;
return variable_buffer_data_size;
}
/**
* Checks if the variable name corresponds with it's data type
*
* @param *avctx the AVCodecContext
* @param **buf the current pointer location in the header where
* the variable name starts
* @param *buf_end pointer location of the end of the buffer
* @param *value_name name of the varible to check
* @param *value_type type of the varible to check
* @param minimum_length minimum length of the variable data
* @param variable_buffer_data_size variable length read from the header
* after it's checked
* @return negative if variable is invalid
*/
static int check_header_variable(AVCodecContext *avctx,
const uint8_t **buf,
const uint8_t *buf_end,
const char *value_name,
const char *value_type,
unsigned int minimum_length,
unsigned int *variable_buffer_data_size)
{
if (buf_end - *buf >= minimum_length && !strcmp(*buf, value_name)) {
*buf += strlen(value_name)+1;
if (!strcmp(*buf, value_type)) {
*buf += strlen(value_type)+1;
*variable_buffer_data_size = get_header_variable_length(buf, buf_end);
if (!*variable_buffer_data_size)
av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
return 1;
}
*buf -= strlen(value_name)+1;
av_log(avctx, AV_LOG_WARNING, "Unknown data type for header variable %s\n", value_name);
}
return -1;
}
static void predictor(uint8_t *src, int size) static void predictor(uint8_t *src, int size)
{ {
uint8_t *t = src + 1; uint8_t *t = src + 1;
uint8_t *stop = src + size; uint8_t *stop = src + size;
while (t < stop) { while (t < stop) {
int d = (int)t[-1] + (int)t[0] - 128; int d = (int) t[-1] + (int) t[0] - 128;
t[0] = d; t[0] = d;
++t; ++t;
} }
...@@ -224,8 +164,8 @@ static void reorder_pixels(uint8_t *src, uint8_t *dst, int size) ...@@ -224,8 +164,8 @@ static void reorder_pixels(uint8_t *src, uint8_t *dst, int size)
{ {
const int8_t *t1 = src; const int8_t *t1 = src;
const int8_t *t2 = src + (size + 1) / 2; const int8_t *t2 = src + (size + 1) / 2;
int8_t *s = dst; int8_t *s = dst;
int8_t *stop = s + size; int8_t *stop = s + size;
while (1) { while (1) {
if (s < stop) if (s < stop)
...@@ -247,7 +187,7 @@ static int zip_uncompress(const uint8_t *src, int compressed_size, ...@@ -247,7 +187,7 @@ static int zip_uncompress(const uint8_t *src, int compressed_size,
if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK || if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
dest_len != uncompressed_size) dest_len != uncompressed_size)
return AVERROR(EINVAL); return AVERROR_INVALIDDATA;
predictor(td->tmp, uncompressed_size); predictor(td->tmp, uncompressed_size);
reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size); reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
...@@ -258,11 +198,11 @@ static int zip_uncompress(const uint8_t *src, int compressed_size, ...@@ -258,11 +198,11 @@ static int zip_uncompress(const uint8_t *src, int compressed_size,
static int rle_uncompress(const uint8_t *src, int compressed_size, static int rle_uncompress(const uint8_t *src, int compressed_size,
int uncompressed_size, EXRThreadData *td) int uncompressed_size, EXRThreadData *td)
{ {
int8_t *d = (int8_t *)td->tmp; uint8_t *d = td->tmp;
const int8_t *s = (const int8_t *)src; const int8_t *s = src;
int ssize = compressed_size; int ssize = compressed_size;
int dsize = uncompressed_size; int dsize = uncompressed_size;
int8_t *dend = d + dsize; uint8_t *dend = d + dsize;
int count; int count;
while (ssize > 0) { while (ssize > 0) {
...@@ -271,9 +211,9 @@ static int rle_uncompress(const uint8_t *src, int compressed_size, ...@@ -271,9 +211,9 @@ static int rle_uncompress(const uint8_t *src, int compressed_size,
if (count < 0) { if (count < 0) {
count = -count; count = -count;
if ((dsize -= count ) < 0 || if ((dsize -= count) < 0 ||
(ssize -= count + 1) < 0) (ssize -= count + 1) < 0)
return -1; return AVERROR_INVALIDDATA;
while (count--) while (count--)
*d++ = *s++; *d++ = *s++;
...@@ -281,8 +221,8 @@ static int rle_uncompress(const uint8_t *src, int compressed_size, ...@@ -281,8 +221,8 @@ static int rle_uncompress(const uint8_t *src, int compressed_size,
count++; count++;
if ((dsize -= count) < 0 || if ((dsize -= count) < 0 ||
(ssize -= 2 ) < 0) (ssize -= 2) < 0)
return -1; return AVERROR_INVALIDDATA;
while (count--) while (count--)
*d++ = *s; *d++ = *s;
...@@ -301,16 +241,15 @@ static int rle_uncompress(const uint8_t *src, int compressed_size, ...@@ -301,16 +241,15 @@ static int rle_uncompress(const uint8_t *src, int compressed_size,
} }
#define USHORT_RANGE (1 << 16) #define USHORT_RANGE (1 << 16)
#define BITMAP_SIZE (1 << 13) #define BITMAP_SIZE (1 << 13)
static uint16_t reverse_lut(const uint8_t *bitmap, uint16_t *lut) static uint16_t reverse_lut(const uint8_t *bitmap, uint16_t *lut)
{ {
int i, k = 0; int i, k = 0;
for (i = 0; i < USHORT_RANGE; i++) { for (i = 0; i < USHORT_RANGE; i++)
if ((i == 0) || (bitmap[i >> 3] & (1 << (i & 7)))) if ((i == 0) || (bitmap[i >> 3] & (1 << (i & 7))))
lut[k++] = i; lut[k++] = i;
}
i = k - 1; i = k - 1;
...@@ -331,7 +270,7 @@ static void apply_lut(const uint16_t *lut, uint16_t *dst, int dsize) ...@@ -331,7 +270,7 @@ static void apply_lut(const uint16_t *lut, uint16_t *dst, int dsize)
#define HUF_DECBITS 14 // decoding bit size (>= 8) #define HUF_DECBITS 14 // decoding bit size (>= 8)
#define HUF_ENCSIZE ((1 << HUF_ENCBITS) + 1) // encoding table size #define HUF_ENCSIZE ((1 << HUF_ENCBITS) + 1) // encoding table size
#define HUF_DECSIZE (1 << HUF_DECBITS) // decoding table size #define HUF_DECSIZE (1 << HUF_DECBITS) // decoding table size
#define HUF_DECMASK (HUF_DECSIZE - 1) #define HUF_DECMASK (HUF_DECSIZE - 1)
typedef struct HufDec { typedef struct HufDec {
...@@ -352,7 +291,7 @@ static void huf_canonical_code_table(uint64_t *hcode) ...@@ -352,7 +291,7 @@ static void huf_canonical_code_table(uint64_t *hcode)
for (i = 58; i > 0; --i) { for (i = 58; i > 0; --i) {
uint64_t nc = ((c + n[i]) >> 1); uint64_t nc = ((c + n[i]) >> 1);
n[i] = c; n[i] = c;
c = nc; c = nc;
} }
for (i = 0; i < HUF_ENCSIZE; ++i) { for (i = 0; i < HUF_ENCSIZE; ++i) {
...@@ -388,7 +327,7 @@ static int huf_unpack_enc_table(GetByteContext *gb, ...@@ -388,7 +327,7 @@ static int huf_unpack_enc_table(GetByteContext *gb,
hcode[im++] = 0; hcode[im++] = 0;
im--; im--;
} else if (l >= (uint64_t) SHORT_ZEROCODE_RUN) { } else if (l >= SHORT_ZEROCODE_RUN) {
int zerun = l - SHORT_ZEROCODE_RUN + 2; int zerun = l - SHORT_ZEROCODE_RUN + 2;
if (im + zerun > iM + 1) if (im + zerun > iM + 1)
...@@ -424,7 +363,7 @@ static int huf_build_dec_table(const uint64_t *hcode, int im, ...@@ -424,7 +363,7 @@ static int huf_build_dec_table(const uint64_t *hcode, int im,
pl->lit++; pl->lit++;
pl->p = av_realloc_f(pl->p, pl->lit, sizeof(int)); pl->p = av_realloc(pl->p, pl->lit * sizeof(int));
if (!pl->p) if (!pl->p)
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
...@@ -444,40 +383,42 @@ static int huf_build_dec_table(const uint64_t *hcode, int im, ...@@ -444,40 +383,42 @@ static int huf_build_dec_table(const uint64_t *hcode, int im,
return 0; return 0;
} }
#define get_char(c, lc, gb) { \ #define get_char(c, lc, gb) \
c = (c << 8) | bytestream2_get_byte(gb); \ { \
lc += 8; \ c = (c << 8) | bytestream2_get_byte(gb); \
lc += 8; \
} }
#define get_code(po, rlc, c, lc, gb, out, oe) { \ #define get_code(po, rlc, c, lc, gb, out, oe) \
if (po == rlc) { \ { \
if (lc < 8) \ if (po == rlc) { \
get_char(c, lc, gb); \ if (lc < 8) \
lc -= 8; \ get_char(c, lc, gb); \
\ lc -= 8; \
cs = c >> lc; \ \
\ cs = c >> lc; \
if (out + cs > oe) \ \
return AVERROR_INVALIDDATA; \ if (out + cs > oe) \
\ return AVERROR_INVALIDDATA; \
s = out[-1]; \ \
\ s = out[-1]; \
while (cs-- > 0) \ \
*out++ = s; \ while (cs-- > 0) \
} else if (out < oe) { \ *out++ = s; \
*out++ = po; \ } else if (out < oe) { \
} else { \ *out++ = po; \
return AVERROR_INVALIDDATA; \ } else { \
} \ return AVERROR_INVALIDDATA; \
} \
} }
static int huf_decode(const uint64_t *hcode, const HufDec *hdecod, static int huf_decode(const uint64_t *hcode, const HufDec *hdecod,
GetByteContext *gb, int nbits, GetByteContext *gb, int nbits,
int rlc, int no, uint16_t *out) int rlc, int no, uint16_t *out)
{ {
uint64_t c = 0; uint64_t c = 0;
uint16_t *outb = out; uint16_t *outb = out;
uint16_t *oe = out + no; uint16_t *oe = out + no;
const uint8_t *ie = gb->buffer + (nbits + 7) / 8; // input byte size const uint8_t *ie = gb->buffer + (nbits + 7) / 8; // input byte size
uint8_t cs, s; uint8_t cs, s;
int i, lc = 0; int i, lc = 0;
...@@ -486,7 +427,7 @@ static int huf_decode(const uint64_t *hcode, const HufDec *hdecod, ...@@ -486,7 +427,7 @@ static int huf_decode(const uint64_t *hcode, const HufDec *hdecod,
get_char(c, lc, gb); get_char(c, lc, gb);
while (lc >= HUF_DECBITS) { while (lc >= HUF_DECBITS) {
const HufDec pl = hdecod[(c >> (lc-HUF_DECBITS)) & HUF_DECMASK]; const HufDec pl = hdecod[(c >> (lc - HUF_DECBITS)) & HUF_DECMASK];
if (pl.len) { if (pl.len) {
lc -= pl.len; lc -= pl.len;
...@@ -519,7 +460,7 @@ static int huf_decode(const uint64_t *hcode, const HufDec *hdecod, ...@@ -519,7 +460,7 @@ static int huf_decode(const uint64_t *hcode, const HufDec *hdecod,
} }
} }
i = (8 - nbits) & 7; i = (8 - nbits) & 7;
c >>= i; c >>= i;
lc -= i; lc -= i;
...@@ -549,8 +490,8 @@ static int huf_uncompress(GetByteContext *gb, ...@@ -549,8 +490,8 @@ static int huf_uncompress(GetByteContext *gb,
int ret, i; int ret, i;
src_size = bytestream2_get_le32(gb); src_size = bytestream2_get_le32(gb);
im = bytestream2_get_le32(gb); im = bytestream2_get_le32(gb);
iM = bytestream2_get_le32(gb); iM = bytestream2_get_le32(gb);
bytestream2_skip(gb, 4); bytestream2_skip(gb, 4);
nBits = bytestream2_get_le32(gb); nBits = bytestream2_get_le32(gb);
if (im < 0 || im >= HUF_ENCSIZE || if (im < 0 || im >= HUF_ENCSIZE ||
...@@ -560,8 +501,8 @@ static int huf_uncompress(GetByteContext *gb, ...@@ -560,8 +501,8 @@ static int huf_uncompress(GetByteContext *gb,
bytestream2_skip(gb, 4); bytestream2_skip(gb, 4);
freq = av_calloc(HUF_ENCSIZE, sizeof(*freq)); freq = av_mallocz_array(HUF_ENCSIZE, sizeof(*freq));
hdec = av_calloc(HUF_DECSIZE, sizeof(*hdec)); hdec = av_mallocz_array(HUF_DECSIZE, sizeof(*hdec));
if (!freq || !hdec) { if (!freq || !hdec) {
ret = AVERROR(ENOMEM); ret = AVERROR(ENOMEM);
goto fail; goto fail;
...@@ -580,10 +521,9 @@ static int huf_uncompress(GetByteContext *gb, ...@@ -580,10 +521,9 @@ static int huf_uncompress(GetByteContext *gb,
ret = huf_decode(freq, hdec, gb, nBits, iM, dst_size, dst); ret = huf_decode(freq, hdec, gb, nBits, iM, dst_size, dst);
fail: fail:
for (i = 0; i < HUF_DECSIZE; i++) { for (i = 0; i < HUF_DECSIZE; i++)
if (hdec) if (hdec)
av_freep(&hdec[i].p); av_freep(&hdec[i].p);
}
av_free(freq); av_free(freq);
av_free(hdec); av_free(hdec);
...@@ -595,8 +535,8 @@ static inline void wdec14(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b) ...@@ -595,8 +535,8 @@ static inline void wdec14(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
{ {
int16_t ls = l; int16_t ls = l;
int16_t hs = h; int16_t hs = h;
int hi = hs; int hi = hs;
int ai = ls + (hi & 1) + (hi >> 1); int ai = ls + (hi & 1) + (hi >> 1);
int16_t as = ai; int16_t as = ai;
int16_t bs = ai - hi; int16_t bs = ai - hi;
...@@ -605,13 +545,13 @@ static inline void wdec14(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b) ...@@ -605,13 +545,13 @@ static inline void wdec14(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
} }
#define NBITS 16 #define NBITS 16
#define A_OFFSET (1 << (NBITS - 1)) #define A_OFFSET (1 << (NBITS - 1))
#define MOD_MASK ((1 << NBITS) - 1) #define MOD_MASK ((1 << NBITS) - 1)
static inline void wdec16(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b) static inline void wdec16(uint16_t l, uint16_t h, uint16_t *a, uint16_t *b)
{ {
int m = l; int m = l;
int d = h; int d = h;
int bb = (m - (d >> 1)) & MOD_MASK; int bb = (m - (d >> 1)) & MOD_MASK;
int aa = (d + bb - A_OFFSET) & MOD_MASK; int aa = (d + bb - A_OFFSET) & MOD_MASK;
*b = bb; *b = bb;
...@@ -622,8 +562,8 @@ static void wav_decode(uint16_t *in, int nx, int ox, ...@@ -622,8 +562,8 @@ static void wav_decode(uint16_t *in, int nx, int ox,
int ny, int oy, uint16_t mx) int ny, int oy, uint16_t mx)
{ {
int w14 = (mx < (1 << 14)); int w14 = (mx < (1 << 14));
int n = (nx > ny) ? ny: nx; int n = (nx > ny) ? ny : nx;
int p = 1; int p = 1;
int p2; int p2;
while (p <= n) while (p <= n)
...@@ -647,19 +587,19 @@ static void wav_decode(uint16_t *in, int nx, int ox, ...@@ -647,19 +587,19 @@ static void wav_decode(uint16_t *in, int nx, int ox,
uint16_t *ex = py + ox * (nx - p2); uint16_t *ex = py + ox * (nx - p2);
for (; px <= ex; px += ox2) { for (; px <= ex; px += ox2) {
uint16_t *p01 = px + ox1; uint16_t *p01 = px + ox1;
uint16_t *p10 = px + oy1; uint16_t *p10 = px + oy1;
uint16_t *p11 = p10 + ox1; uint16_t *p11 = p10 + ox1;
if (w14) { if (w14) {
wdec14(*px, *p10, &i00, &i10); wdec14(*px, *p10, &i00, &i10);
wdec14(*p01, *p11, &i01, &i11); wdec14(*p01, *p11, &i01, &i11);
wdec14(i00, i01, px, p01); wdec14(i00, i01, px, p01);
wdec14(i10, i11, p10, p11); wdec14(i10, i11, p10, p11);
} else { } else {
wdec16(*px, *p10, &i00, &i10); wdec16(*px, *p10, &i00, &i10);
wdec16(*p01, *p11, &i01, &i11); wdec16(*p01, *p11, &i01, &i11);
wdec16(i00, i01, px, p01); wdec16(i00, i01, px, p01);
wdec16(i10, i11, p10, p11); wdec16(i10, i11, p10, p11);
} }
} }
...@@ -692,25 +632,30 @@ static void wav_decode(uint16_t *in, int nx, int ox, ...@@ -692,25 +632,30 @@ static void wav_decode(uint16_t *in, int nx, int ox,
} }
} }
p2 = p; p2 = p;
p >>= 1; p >>= 1;
} }
} }
static int piz_uncompress(EXRContext *s, const uint8_t *src, int ssize, int dsize, EXRThreadData *td) static int piz_uncompress(EXRContext *s, const uint8_t *src, int ssize,
int dsize, EXRThreadData *td)
{ {
GetByteContext gb; GetByteContext gb;
uint16_t maxval, min_non_zero, max_non_zero; uint16_t maxval, min_non_zero, max_non_zero;
uint16_t *ptr, *tmp = (uint16_t *)td->tmp; uint16_t *ptr;
int8_t *out; uint16_t *tmp = (uint16_t *)td->tmp;
uint8_t *out;
int ret, i, j; int ret, i, j;
if (!td->bitmap) if (!td->bitmap)
td->bitmap = av_malloc(BITMAP_SIZE); td->bitmap = av_malloc(BITMAP_SIZE);
if (!td->lut) if (!td->lut)
td->lut = av_malloc(1 << 17); td->lut = av_malloc(1 << 17);
if (!td->bitmap || !td->lut) if (!td->bitmap || !td->lut) {
av_free(td->bitmap);
av_free(td->lut);
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
}
bytestream2_init(&gb, src, ssize); bytestream2_init(&gb, src, ssize);
min_non_zero = bytestream2_get_le16(&gb); min_non_zero = bytestream2_get_le16(&gb);
...@@ -727,7 +672,7 @@ static int piz_uncompress(EXRContext *s, const uint8_t *src, int ssize, int dsiz ...@@ -727,7 +672,7 @@ static int piz_uncompress(EXRContext *s, const uint8_t *src, int ssize, int dsiz
maxval = reverse_lut(td->bitmap, td->lut); maxval = reverse_lut(td->bitmap, td->lut);
ret = huf_uncompress(&gb, tmp, dsize / sizeof(int16_t)); ret = huf_uncompress(&gb, tmp, dsize / sizeof(uint16_t));
if (ret) if (ret)
return ret; return ret;
...@@ -737,20 +682,20 @@ static int piz_uncompress(EXRContext *s, const uint8_t *src, int ssize, int dsiz ...@@ -737,20 +682,20 @@ static int piz_uncompress(EXRContext *s, const uint8_t *src, int ssize, int dsiz
int size = channel->pixel_type; int size = channel->pixel_type;
for (j = 0; j < size; j++) for (j = 0; j < size; j++)
wav_decode(ptr + j, s->xdelta, size, s->ysize, s->xdelta * size, maxval); wav_decode(ptr + j, s->xdelta, size, s->ysize,
s->xdelta * size, maxval);
ptr += s->xdelta * s->ysize * size; ptr += s->xdelta * s->ysize * size;
} }
apply_lut(td->lut, tmp, dsize / sizeof(int16_t)); apply_lut(td->lut, tmp, dsize / sizeof(uint16_t));
out = td->uncompressed_data; out = td->uncompressed_data;
for (i = 0; i < s->ysize; i++) { for (i = 0; i < s->ysize; i++)
for (j = 0; j < s->nb_channels; j++) { for (j = 0; j < s->nb_channels; j++) {
uint16_t *in = tmp + j * s->xdelta * s->ysize + i * s->xdelta; uint16_t *in = tmp + j * s->xdelta * s->ysize + i * s->xdelta;
memcpy(out, in, s->xdelta * 2); memcpy(out, in, s->xdelta * 2);
out += s->xdelta * 2; out += s->xdelta * 2;
} }
}
return 0; return 0;
} }
...@@ -766,10 +711,10 @@ static int pxr24_uncompress(EXRContext *s, const uint8_t *src, ...@@ -766,10 +711,10 @@ static int pxr24_uncompress(EXRContext *s, const uint8_t *src,
if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK || if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
dest_len != uncompressed_size) dest_len != uncompressed_size)
return AVERROR(EINVAL); return AVERROR_INVALIDDATA;
out = td->uncompressed_data; out = td->uncompressed_data;
for (i = 0; i < s->ysize; i++) { for (i = 0; i < s->ysize; i++)
for (c = 0; c < s->nb_channels; c++) { for (c = 0; c < s->nb_channels; c++) {
EXRChannel *channel = &s->channels[c]; EXRChannel *channel = &s->channels[c];
const uint8_t *ptr[4]; const uint8_t *ptr[4];
...@@ -780,12 +725,12 @@ static int pxr24_uncompress(EXRContext *s, const uint8_t *src, ...@@ -780,12 +725,12 @@ static int pxr24_uncompress(EXRContext *s, const uint8_t *src,
ptr[0] = in; ptr[0] = in;
ptr[1] = ptr[0] + s->xdelta; ptr[1] = ptr[0] + s->xdelta;
ptr[2] = ptr[1] + s->xdelta; ptr[2] = ptr[1] + s->xdelta;
in = ptr[2] + s->xdelta; in = ptr[2] + s->xdelta;
for (j = 0; j < s->xdelta; ++j) { for (j = 0; j < s->xdelta; ++j) {
uint32_t diff = (*(ptr[0]++) << 24) | uint32_t diff = (*(ptr[0]++) << 24) |
(*(ptr[1]++) << 16) | (*(ptr[1]++) << 16) |
(*(ptr[2]++) << 8); (*(ptr[2]++) << 8);
pixel += diff; pixel += diff;
bytestream_put_le32(&out, pixel); bytestream_put_le32(&out, pixel);
} }
...@@ -793,7 +738,7 @@ static int pxr24_uncompress(EXRContext *s, const uint8_t *src, ...@@ -793,7 +738,7 @@ static int pxr24_uncompress(EXRContext *s, const uint8_t *src,
case EXR_HALF: case EXR_HALF:
ptr[0] = in; ptr[0] = in;
ptr[1] = ptr[0] + s->xdelta; ptr[1] = ptr[0] + s->xdelta;
in = ptr[1] + s->xdelta; in = ptr[1] + s->xdelta;
for (j = 0; j < s->xdelta; j++) { for (j = 0; j < s->xdelta; j++) {
uint32_t diff = (*(ptr[0]++) << 8) | *(ptr[1]++); uint32_t diff = (*(ptr[0]++) << 8) | *(ptr[1]++);
...@@ -802,10 +747,9 @@ static int pxr24_uncompress(EXRContext *s, const uint8_t *src, ...@@ -802,10 +747,9 @@ static int pxr24_uncompress(EXRContext *s, const uint8_t *src,
} }
break; break;
default: default:
av_assert1(0); return AVERROR_INVALIDDATA;
} }
} }
}
return 0; return 0;
} }
...@@ -822,19 +766,19 @@ static int decode_block(AVCodecContext *avctx, void *tdata, ...@@ -822,19 +766,19 @@ static int decode_block(AVCodecContext *avctx, void *tdata,
uint32_t xdelta = s->xdelta; uint32_t xdelta = s->xdelta;
uint16_t *ptr_x; uint16_t *ptr_x;
uint8_t *ptr; uint8_t *ptr;
int32_t data_size, line; uint32_t data_size, line;
const uint8_t *src; const uint8_t *src;
int axmax = (avctx->width - (s->xmax + 1)) * 2 * s->desc->nb_components; int axmax = (avctx->width - (s->xmax + 1)) * 2 * s->desc->nb_components;
int bxmin = s->xmin * 2 * s->desc->nb_components; int bxmin = s->xmin * 2 * s->desc->nb_components;
int i, x, buf_size = s->buf_size; int i, x, buf_size = s->buf_size;
int av_unused ret; int ret;
line_offset = AV_RL64(s->table + jobnr * 8); line_offset = AV_RL64(s->gb.buffer + jobnr * 8);
// Check if the buffer has the required bytes needed from the offset // Check if the buffer has the required bytes needed from the offset
if (line_offset > buf_size - 8) if (line_offset > buf_size - 8)
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
src = buf + line_offset + 8; src = buf + line_offset + 8;
line = AV_RL32(src - 8); line = AV_RL32(src - 8);
if (line < s->ymin || line > s->ymax) if (line < s->ymin || line > s->ymax)
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
...@@ -843,22 +787,24 @@ static int decode_block(AVCodecContext *avctx, void *tdata, ...@@ -843,22 +787,24 @@ static int decode_block(AVCodecContext *avctx, void *tdata,
if (data_size <= 0 || data_size > buf_size) if (data_size <= 0 || data_size > buf_size)
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
s->ysize = FFMIN(s->scan_lines_per_block, s->ymax - line + 1); s->ysize = FFMIN(s->scan_lines_per_block, s->ymax - line + 1);
uncompressed_size = s->scan_line_size * s->ysize; uncompressed_size = s->scan_line_size * s->ysize;
if ((s->compr == EXR_RAW && (data_size != uncompressed_size || if ((s->compression == EXR_RAW && (data_size != uncompressed_size ||
line_offset > buf_size - uncompressed_size)) || line_offset > buf_size - uncompressed_size)) ||
(s->compr != EXR_RAW && (data_size > uncompressed_size || (s->compression != EXR_RAW && (data_size > uncompressed_size ||
line_offset > buf_size - data_size))) { line_offset > buf_size - data_size))) {
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
} }
if (data_size < uncompressed_size) { if (data_size < uncompressed_size) {
av_fast_padded_malloc(&td->uncompressed_data, &td->uncompressed_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); av_fast_padded_malloc(&td->tmp, &td->tmp_size, uncompressed_size);
if (!td->uncompressed_data || !td->tmp) if (!td->uncompressed_data || !td->tmp)
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
switch (s->compr) { ret = AVERROR_INVALIDDATA;
switch (s->compression) {
case EXR_ZIP1: case EXR_ZIP1:
case EXR_ZIP16: case EXR_ZIP16:
ret = zip_uncompress(src, data_size, uncompressed_size, td); ret = zip_uncompress(src, data_size, uncompressed_size, td);
...@@ -872,7 +818,10 @@ static int decode_block(AVCodecContext *avctx, void *tdata, ...@@ -872,7 +818,10 @@ static int decode_block(AVCodecContext *avctx, void *tdata,
case EXR_RLE: case EXR_RLE:
ret = rle_uncompress(src, data_size, uncompressed_size, td); ret = rle_uncompress(src, data_size, uncompressed_size, td);
} }
if (ret < 0) {
av_log(avctx, AV_LOG_ERROR, "decode_block() failed.\n");
return ret;
}
src = td->uncompressed_data; src = td->uncompressed_data;
} }
...@@ -883,7 +832,9 @@ static int decode_block(AVCodecContext *avctx, void *tdata, ...@@ -883,7 +832,9 @@ static int decode_block(AVCodecContext *avctx, void *tdata,
channel_buffer[3] = src + xdelta * s->channel_offsets[3]; channel_buffer[3] = src + xdelta * s->channel_offsets[3];
ptr = p->data[0] + line * p->linesize[0]; 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]) { 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; const uint8_t *r, *g, *b, *a;
r = channel_buffer[0]; r = channel_buffer[0];
...@@ -892,7 +843,7 @@ static int decode_block(AVCodecContext *avctx, void *tdata, ...@@ -892,7 +843,7 @@ static int decode_block(AVCodecContext *avctx, void *tdata,
if (channel_buffer[3]) if (channel_buffer[3])
a = channel_buffer[3]; a = channel_buffer[3];
ptr_x = (uint16_t *)ptr; ptr_x = (uint16_t *) ptr;
// Zero out the start if xmin is not 0 // Zero out the start if xmin is not 0
memset(ptr_x, 0, bxmin); memset(ptr_x, 0, bxmin);
...@@ -930,256 +881,296 @@ static int decode_block(AVCodecContext *avctx, void *tdata, ...@@ -930,256 +881,296 @@ static int decode_block(AVCodecContext *avctx, void *tdata,
return 0; return 0;
} }
static int decode_frame(AVCodecContext *avctx, /**
void *data, * Check if the variable name corresponds to its data type.
int *got_frame, *
AVPacket *avpkt) * @param s the EXRContext
* @param value_name name of the variable to check
* @param value_type type of the variable to check
* @param minimum_length minimum length of the variable data
*
* @return bytes to read containing variable data
* -1 if variable is not found
* 0 if buffer ended prematurely
*/
static int check_header_variable(EXRContext *s,
const char *value_name,
const char *value_type,
unsigned int minimum_length)
{ {
const uint8_t *buf = avpkt->data; int var_size = -1;
unsigned int buf_size = avpkt->size;
const uint8_t *buf_end = buf + buf_size; if (bytestream2_get_bytes_left(&s->gb) >= minimum_length &&
!strcmp(s->gb.buffer, value_name)) {
EXRContext *const s = avctx->priv_data; // found value_name, jump to value_type (null terminated strings)
ThreadFrame frame = { .f = data }; s->gb.buffer += strlen(value_name) + 1;
AVFrame *picture = data; if (!strcmp(s->gb.buffer, value_type)) {
uint8_t *ptr; s->gb.buffer += strlen(value_type) + 1;
var_size = bytestream2_get_le32(&s->gb);
int i, y, magic_number, version, flags, ret; // don't go read past boundaries
int w = 0; if (var_size > bytestream2_get_bytes_left(&s->gb))
int h = 0; var_size = 0;
} else {
// value_type not found, reset the buffer
s->gb.buffer -= strlen(value_name) + 1;
av_log(s->avctx, AV_LOG_WARNING,
"Unknown data type %s for header variable %s.\n",
value_type, value_name);
}
}
int out_line_size; return var_size;
int scan_line_blocks; }
unsigned int current_channel_offset = 0; static int decode_header(EXRContext *s)
{
int current_channel_offset = 0;
int magic_number, version, flags, i;
s->xmin = ~0; if (bytestream2_get_bytes_left(&s->gb) < 10) {
s->xmax = ~0; av_log(s->avctx, AV_LOG_ERROR, "Header too short to parse.\n");
s->ymin = ~0;
s->ymax = ~0;
s->xdelta = ~0;
s->ydelta = ~0;
s->channel_offsets[0] = -1;
s->channel_offsets[1] = -1;
s->channel_offsets[2] = -1;
s->channel_offsets[3] = -1;
s->pixel_type = -1;
s->nb_channels = 0;
s->compr = -1;
s->buf = buf;
s->buf_size = buf_size;
if (buf_size < 10) {
av_log(avctx, AV_LOG_ERROR, "Too short header to parse\n");
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
} }
magic_number = bytestream_get_le32(&buf); magic_number = bytestream2_get_le32(&s->gb);
if (magic_number != 20000630) { // As per documentation of OpenEXR it's supposed to be int 20000630 little-endian if (magic_number != 20000630) {
av_log(avctx, AV_LOG_ERROR, "Wrong magic number %d\n", magic_number); /* As per documentation of OpenEXR, it is supposed to be
* int 20000630 little-endian */
av_log(s->avctx, AV_LOG_ERROR, "Wrong magic number %d.\n", magic_number);
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
} }
version = bytestream_get_byte(&buf); version = bytestream2_get_byte(&s->gb);
if (version != 2) { if (version != 2) {
avpriv_report_missing_feature(avctx, "Version %d", version); avpriv_report_missing_feature(s->avctx, "Version %d", version);
return AVERROR_PATCHWELCOME; return AVERROR_PATCHWELCOME;
} }
flags = bytestream_get_le24(&buf); flags = bytestream2_get_le24(&s->gb);
if (flags & 0x2) { if (flags & 0x02) {
avpriv_report_missing_feature(avctx, "Tile support"); avpriv_report_missing_feature(s->avctx, "Tile support");
return AVERROR_PATCHWELCOME; return AVERROR_PATCHWELCOME;
} }
// Parse the header // Parse the header
while (buf < buf_end && buf[0]) { while (bytestream2_get_bytes_left(&s->gb) > 0 && *s->gb.buffer) {
unsigned int variable_buffer_data_size; int var_size;
// Process the channel list if ((var_size = check_header_variable(s, "channels",
if (check_header_variable(avctx, &buf, buf_end, "channels", "chlist", 38, &variable_buffer_data_size) >= 0) { "chlist", 38)) >= 0) {
const uint8_t *channel_list_end; GetByteContext ch_gb;
if (!variable_buffer_data_size) if (!var_size)
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
channel_list_end = buf + variable_buffer_data_size; bytestream2_init(&ch_gb, s->gb.buffer, var_size);
while (channel_list_end - buf >= 19) {
while (bytestream2_get_bytes_left(&ch_gb) >= 19) {
EXRChannel *channel; EXRChannel *channel;
enum ExrPixelType current_pixel_type; enum ExrPixelType current_pixel_type;
int channel_index = -1; int channel_index = -1;
int xsub, ysub; int xsub, ysub;
const char* b = buf; if (strcmp(s->layer, "") != 0) {
if (strncmp(ch_gb.buffer, s->layer, strlen(s->layer)) == 0) {
if ( strcmp( s->layer, "" ) != 0 ) { ch_gb.buffer += strlen(s->layer);
if ( strncmp( b, s->layer, strlen(s->layer) ) == 0 ) { if (*ch_gb.buffer == '.')
b += strlen(s->layer); ch_gb.buffer++; /* skip dot if not given */
if ( *b == '.' ) ++b; /* skip dot if not given */ av_log(s->avctx, AV_LOG_INFO,
av_log( avctx, AV_LOG_INFO, "Layer %s.%s matched\n", "Layer %s.%s matched.\n", s->layer, ch_gb.buffer);
s->layer, b );
} }
} }
if (!strcmp(ch_gb.buffer, "R") ||
if (!strcmp(b, "R")||!strcmp(b, "X")||!strcmp(b,"U")) !strcmp(ch_gb.buffer, "X") ||
!strcmp(ch_gb.buffer, "U"))
channel_index = 0; channel_index = 0;
else if (!strcmp(b, "G")||!strcmp(b, "Y")||!strcmp(b,"V")) else if (!strcmp(ch_gb.buffer, "G") ||
!strcmp(ch_gb.buffer, "Y") ||
!strcmp(ch_gb.buffer, "V"))
channel_index = 1; channel_index = 1;
else if (!strcmp(b, "B")||!strcmp(b, "Z")||!strcmp(b,"W")) else if (!strcmp(ch_gb.buffer, "B") ||
!strcmp(ch_gb.buffer, "Z") ||
!strcmp(ch_gb.buffer, "W"))
channel_index = 2; channel_index = 2;
else if (!strcmp(b, "A")) else if (!strcmp(ch_gb.buffer, "A"))
channel_index = 3; channel_index = 3;
else else
av_log(avctx, AV_LOG_WARNING, "Unsupported channel %.256s\n", buf); av_log(s->avctx, AV_LOG_WARNING,
"Unsupported channel %.256s.\n", ch_gb.buffer);
while (bytestream_get_byte(&buf) && buf < channel_list_end) /* skip until you get a 0 */
continue; /* skip */ while (bytestream2_get_bytes_left(&ch_gb) > 0 &&
bytestream2_get_byte(&ch_gb))
continue;
if (channel_list_end - * &buf < 4) { if (bytestream2_get_bytes_left(&ch_gb) < 4) {
av_log(avctx, AV_LOG_ERROR, "Incomplete header\n"); av_log(s->avctx, AV_LOG_ERROR, "Incomplete header.\n");
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
} }
current_pixel_type = bytestream_get_le32(&buf); current_pixel_type = bytestream2_get_le32(&ch_gb);
if (current_pixel_type > 2) { if (current_pixel_type >= EXR_UNKNOWN) {
av_log(avctx, AV_LOG_ERROR, "Unknown pixel type\n"); avpriv_report_missing_feature(s->avctx,
return AVERROR_INVALIDDATA; "Pixel type %d.\n",
current_pixel_type);
return AVERROR_PATCHWELCOME;
} }
buf += 4; bytestream2_skip(&ch_gb, 4);
xsub = bytestream_get_le32(&buf); xsub = bytestream2_get_le32(&ch_gb);
ysub = bytestream_get_le32(&buf); ysub = bytestream2_get_le32(&ch_gb);
if (xsub != 1 || ysub != 1) { if (xsub != 1 || ysub != 1) {
avpriv_report_missing_feature(avctx, "Subsampling %dx%d", xsub, ysub); avpriv_report_missing_feature(s->avctx,
"Subsampling %dx%d",
xsub, ysub);
return AVERROR_PATCHWELCOME; return AVERROR_PATCHWELCOME;
} }
if (channel_index >= 0) { if (channel_index >= 0) {
if (s->pixel_type != -1 && s->pixel_type != current_pixel_type) { if (s->pixel_type != EXR_UNKNOWN &&
av_log(avctx, AV_LOG_ERROR, "RGB channels not of the same depth\n"); s->pixel_type != current_pixel_type) {
av_log(s->avctx, AV_LOG_ERROR,
"RGB channels not of the same depth.\n");
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
} }
s->pixel_type = current_pixel_type; s->pixel_type = current_pixel_type;
s->channel_offsets[channel_index] = current_channel_offset; s->channel_offsets[channel_index] = current_channel_offset;
} }
s->channels = av_realloc_f(s->channels, ++s->nb_channels, sizeof(EXRChannel)); s->channels = av_realloc(s->channels,
++s->nb_channels * sizeof(EXRChannel));
if (!s->channels) if (!s->channels)
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
channel = &s->channels[s->nb_channels - 1]; channel = &s->channels[s->nb_channels - 1];
channel->pixel_type = current_pixel_type; channel->pixel_type = current_pixel_type;
channel->xsub = xsub; channel->xsub = xsub;
channel->ysub = ysub; channel->ysub = ysub;
current_channel_offset += 1 << current_pixel_type; current_channel_offset += 1 << current_pixel_type;
} }
/* Check if all channels are set with an offset or if the channels /* Check if all channels are set with an offset or if the channels
* are causing an overflow */ * are causing an overflow */
if (FFMIN3(s->channel_offsets[0], if (FFMIN3(s->channel_offsets[0],
s->channel_offsets[1], s->channel_offsets[1],
s->channel_offsets[2]) < 0) { s->channel_offsets[2]) < 0) {
if (s->channel_offsets[0] < 0) if (s->channel_offsets[0] < 0)
av_log(avctx, AV_LOG_ERROR, "Missing red channel\n"); av_log(s->avctx, AV_LOG_ERROR, "Missing red channel.\n");
if (s->channel_offsets[1] < 0) if (s->channel_offsets[1] < 0)
av_log(avctx, AV_LOG_ERROR, "Missing green channel\n"); av_log(s->avctx, AV_LOG_ERROR, "Missing green channel.\n");
if (s->channel_offsets[2] < 0) if (s->channel_offsets[2] < 0)
av_log(avctx, AV_LOG_ERROR, "Missing blue channel\n"); av_log(s->avctx, AV_LOG_ERROR, "Missing blue channel.\n");
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
} }
buf = channel_list_end; // skip one last byte and update main gb
s->gb.buffer = ch_gb.buffer + 1;
continue; continue;
} else if (check_header_variable(avctx, &buf, buf_end, "dataWindow", "box2i", 31, &variable_buffer_data_size) >= 0) { } else if ((var_size = check_header_variable(s, "dataWindow", "box2i",
if (!variable_buffer_data_size) 31)) >= 0) {
if (!var_size)
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
s->xmin = AV_RL32(buf); s->xmin = bytestream2_get_le32(&s->gb);
s->ymin = AV_RL32(buf + 4); s->ymin = bytestream2_get_le32(&s->gb);
s->xmax = AV_RL32(buf + 8); s->xmax = bytestream2_get_le32(&s->gb);
s->ymax = AV_RL32(buf + 12); s->ymax = bytestream2_get_le32(&s->gb);
s->xdelta = (s->xmax - s->xmin) + 1; s->xdelta = (s->xmax - s->xmin) + 1;
s->ydelta = (s->ymax - s->ymin) + 1; s->ydelta = (s->ymax - s->ymin) + 1;
buf += variable_buffer_data_size;
continue; continue;
} else if (check_header_variable(avctx, &buf, buf_end, "displayWindow", "box2i", 34, &variable_buffer_data_size) >= 0) { } else if ((var_size = check_header_variable(s, "displayWindow",
if (!variable_buffer_data_size) "box2i", 34)) >= 0) {
if (!var_size)
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
w = AV_RL32(buf + 8) + 1; bytestream2_skip(&s->gb, 8);
h = AV_RL32(buf + 12) + 1; s->w = bytestream2_get_le32(&s->gb) + 1;
s->h = bytestream2_get_le32(&s->gb) + 1;
buf += variable_buffer_data_size;
continue; continue;
} else if (check_header_variable(avctx, &buf, buf_end, "lineOrder", "lineOrder", 25, &variable_buffer_data_size) >= 0) { } else if ((var_size = check_header_variable(s, "lineOrder",
if (!variable_buffer_data_size) "lineOrder", 25)) >= 0) {
int line_order;
if (!var_size)
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
av_log(avctx, AV_LOG_DEBUG, "line order : %d\n", *buf); line_order = bytestream2_get_byte(&s->gb);
if (*buf > 2) { av_log(s->avctx, AV_LOG_DEBUG, "line order: %d.\n", line_order);
av_log(avctx, AV_LOG_ERROR, "Unknown line order\n"); if (line_order > 2) {
av_log(s->avctx, AV_LOG_ERROR, "Unknown line order.\n");
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
} }
buf += variable_buffer_data_size;
continue; continue;
} else if (check_header_variable(avctx, &buf, buf_end, "pixelAspectRatio", "float", 31, &variable_buffer_data_size) >= 0) { } else if ((var_size = check_header_variable(s, "pixelAspectRatio",
if (!variable_buffer_data_size) "float", 31)) >= 0) {
if (!var_size)
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
avctx->sample_aspect_ratio = av_d2q(av_int2float(AV_RL32(buf)), 255); s->avctx->sample_aspect_ratio =
av_d2q(av_int2float(bytestream2_get_le32(&s->gb)), 255);
buf += variable_buffer_data_size;
continue; continue;
} else if (check_header_variable(avctx, &buf, buf_end, "compression", "compression", 29, &variable_buffer_data_size) >= 0) { } else if ((var_size = check_header_variable(s, "compression",
if (!variable_buffer_data_size) "compression", 29)) >= 0) {
if (!var_size)
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
if (s->compr == -1) if (s->compression == EXR_UNKN)
s->compr = *buf; s->compression = bytestream2_get_byte(&s->gb);
else else
av_log(avctx, AV_LOG_WARNING, "Found more than one compression attribute\n"); av_log(s->avctx, AV_LOG_WARNING,
"Found more than one compression attribute.\n");
buf += variable_buffer_data_size;
continue; continue;
} }
// Check if there is enough bytes for a header // Check if there are enough bytes for a header
if (buf_end - buf <= 9) { if (bytestream2_get_bytes_left(&s->gb) <= 9) {
av_log(avctx, AV_LOG_ERROR, "Incomplete header\n"); av_log(s->avctx, AV_LOG_ERROR, "Incomplete header\n");
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
} }
// Process unknown variables // Process unknown variables
for (i = 0; i < 2; i++) { for (i = 0; i < 2; i++) // value_name and value_type
// Skip variable name/type while (bytestream2_get_byte(&s->gb) != 0);
while (++buf < buf_end)
if (buf[0] == 0x0)
break;
}
buf++;
// Skip variable length // Skip variable length
if (buf_end - buf >= 5) { bytestream2_skip(&s->gb, bytestream2_get_le32(&s->gb));
variable_buffer_data_size = get_header_variable_length(&buf, buf_end);
if (!variable_buffer_data_size) {
av_log(avctx, AV_LOG_ERROR, "Incomplete header\n");
return AVERROR_INVALIDDATA;
}
buf += variable_buffer_data_size;
}
} }
if (s->compr == -1) { if (s->compression == EXR_UNKN) {
av_log(avctx, AV_LOG_ERROR, "Missing compression attribute\n"); av_log(s->avctx, AV_LOG_ERROR, "Missing compression attribute.\n");
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
} }
s->scan_line_size = s->xdelta * current_channel_offset;
if (buf >= buf_end) { if (bytestream2_get_bytes_left(&s->gb) <= 0) {
av_log(avctx, AV_LOG_ERROR, "Incomplete frame\n"); av_log(s->avctx, AV_LOG_ERROR, "Incomplete frame.\n");
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
} }
buf++;
// aaand we are done
bytestream2_skip(&s->gb, 1);
return 0;
}
static int decode_frame(AVCodecContext *avctx, void *data,
int *got_frame, AVPacket *avpkt)
{
EXRContext *s = avctx->priv_data;
ThreadFrame frame = { .f = data };
AVFrame *picture = data;
uint8_t *ptr;
int y, ret;
int out_line_size;
int scan_line_blocks;
bytestream2_init(&s->gb, avpkt->data, avpkt->size);
if ((ret = decode_header(s)) < 0)
return ret;
switch (s->pixel_type) { switch (s->pixel_type) {
case EXR_FLOAT: case EXR_FLOAT:
...@@ -1193,11 +1184,11 @@ static int decode_frame(AVCodecContext *avctx, ...@@ -1193,11 +1184,11 @@ static int decode_frame(AVCodecContext *avctx,
avpriv_request_sample(avctx, "32-bit unsigned int"); avpriv_request_sample(avctx, "32-bit unsigned int");
return AVERROR_PATCHWELCOME; return AVERROR_PATCHWELCOME;
default: default:
av_log(avctx, AV_LOG_ERROR, "Missing channel list\n"); av_log(avctx, AV_LOG_ERROR, "Missing channel list.\n");
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
} }
switch (s->compr) { switch (s->compression) {
case EXR_RAW: case EXR_RAW:
case EXR_RLE: case EXR_RLE:
case EXR_ZIP1: case EXR_ZIP1:
...@@ -1211,49 +1202,41 @@ static int decode_frame(AVCodecContext *avctx, ...@@ -1211,49 +1202,41 @@ static int decode_frame(AVCodecContext *avctx,
s->scan_lines_per_block = 32; s->scan_lines_per_block = 32;
break; break;
default: default:
avpriv_report_missing_feature(avctx, "Compression %d", s->compr); avpriv_report_missing_feature(avctx, "Compression %d", s->compression);
return AVERROR_PATCHWELCOME; return AVERROR_PATCHWELCOME;
} }
// Verify the xmin, xmax, ymin, ymax and xdelta before setting the actual image size /* Verify the xmin, xmax, ymin, ymax and xdelta before setting
if (s->xmin > s->xmax || * the actual image size. */
s->ymin > s->ymax || if (s->xmin > s->xmax ||
s->ymin > s->ymax ||
s->xdelta != s->xmax - s->xmin + 1 || s->xdelta != s->xmax - s->xmin + 1 ||
s->xmax >= w || s->ymax >= h) { s->xmax >= s->w ||
av_log(avctx, AV_LOG_ERROR, "Wrong sizing or missing size information\n"); s->ymax >= s->h) {
av_log(avctx, AV_LOG_ERROR, "Wrong or missing size information.\n");
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
} }
if ((ret = ff_set_dimensions(avctx, w, h)) < 0) if ((ret = ff_set_dimensions(avctx, s->w, s->h)) < 0)
return ret; return ret;
s->desc = av_pix_fmt_desc_get(avctx->pix_fmt); s->desc = av_pix_fmt_desc_get(avctx->pix_fmt);
out_line_size = avctx->width * 2 * s->desc->nb_components; if (!s->desc)
s->scan_line_size = s->xdelta * current_channel_offset; return AVERROR_INVALIDDATA;
scan_line_blocks = (s->ydelta + s->scan_lines_per_block - 1) / s->scan_lines_per_block; out_line_size = avctx->width * 2 * s->desc->nb_components;
scan_line_blocks = (s->ydelta + s->scan_lines_per_block - 1) /
if (s->compr != EXR_RAW) { s->scan_lines_per_block;
size_t thread_data_size, prev_size;
EXRThreadData *m;
prev_size = s->thread_data_size;
if (av_size_mult(avctx->thread_count, sizeof(EXRThreadData), &thread_data_size))
return AVERROR(EINVAL);
m = av_fast_realloc(s->thread_data, &s->thread_data_size, thread_data_size);
if (!m)
return AVERROR(ENOMEM);
s->thread_data = m;
memset(s->thread_data + prev_size, 0, s->thread_data_size - prev_size);
}
if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0) if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
return ret; return ret;
if (buf_end - buf < scan_line_blocks * 8) if (bytestream2_get_bytes_left(&s->gb) < scan_line_blocks * 8)
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
s->table = buf;
ptr = picture->data[0]; // save pointer we are going to use in decode_block
s->buf = avpkt->data;
s->buf_size = avpkt->size;
ptr = picture->data[0];
// Zero out the start if ymin is not 0 // Zero out the start if ymin is not 0
for (y = 0; y < s->ymin; y++) { for (y = 0; y < s->ymin; y++) {
...@@ -1273,15 +1256,54 @@ static int decode_frame(AVCodecContext *avctx, ...@@ -1273,15 +1256,54 @@ static int decode_frame(AVCodecContext *avctx,
picture->pict_type = AV_PICTURE_TYPE_I; picture->pict_type = AV_PICTURE_TYPE_I;
*got_frame = 1; *got_frame = 1;
return buf_size; return avpkt->size;
}
static av_cold int decode_init(AVCodecContext *avctx)
{
EXRContext *s = avctx->priv_data;
s->avctx = avctx;
s->xmin = ~0;
s->xmax = ~0;
s->ymin = ~0;
s->ymax = ~0;
s->xdelta = ~0;
s->ydelta = ~0;
s->channel_offsets[0] = -1;
s->channel_offsets[1] = -1;
s->channel_offsets[2] = -1;
s->channel_offsets[3] = -1;
s->pixel_type = EXR_UNKNOWN;
s->compression = EXR_UNKN;
s->nb_channels = 0;
s->w = 0;
s->h = 0;
// allocate thread data, used for non EXR_RAW compreesion types
s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData));
if (!s->thread_data)
return AVERROR_INVALIDDATA;
return 0;
}
static int decode_init_thread_copy(AVCodecContext *avctx)
{ EXRContext *s = avctx->priv_data;
// allocate thread data, used for non EXR_RAW compreesion types
s->thread_data = av_mallocz_array(avctx->thread_count, sizeof(EXRThreadData));
if (!s->thread_data)
return AVERROR_INVALIDDATA;
return 0;
} }
static av_cold int decode_end(AVCodecContext *avctx) static av_cold int decode_end(AVCodecContext *avctx)
{ {
EXRContext *s = avctx->priv_data; EXRContext *s = avctx->priv_data;
int i; int i;
for (i = 0; i < avctx->thread_count; i++) {
for (i = 0; i < s->thread_data_size / sizeof(EXRThreadData); i++) {
EXRThreadData *td = &s->thread_data[i]; EXRThreadData *td = &s->thread_data[i];
av_freep(&td->uncompressed_data); av_freep(&td->uncompressed_data);
av_freep(&td->tmp); av_freep(&td->tmp);
...@@ -1290,20 +1312,37 @@ static av_cold int decode_end(AVCodecContext *avctx) ...@@ -1290,20 +1312,37 @@ static av_cold int decode_end(AVCodecContext *avctx)
} }
av_freep(&s->thread_data); av_freep(&s->thread_data);
s->thread_data_size = 0;
av_freep(&s->channels); av_freep(&s->channels);
return 0; return 0;
} }
#define OFFSET(x) offsetof(EXRContext, x)
#define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
static const AVOption options[] = {
{ "layer", "Set the decoding layer", OFFSET(layer),
AV_OPT_TYPE_STRING, { .str = "" }, 0, 0, VD },
{ NULL },
};
static const AVClass exr_class = {
.class_name = "EXR",
.item_name = av_default_item_name,
.option = options,
.version = LIBAVUTIL_VERSION_INT,
};
AVCodec ff_exr_decoder = { AVCodec ff_exr_decoder = {
.name = "exr", .name = "exr",
.long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"), .long_name = NULL_IF_CONFIG_SMALL("OpenEXR image"),
.type = AVMEDIA_TYPE_VIDEO, .type = AVMEDIA_TYPE_VIDEO,
.id = AV_CODEC_ID_EXR, .id = AV_CODEC_ID_EXR,
.priv_data_size = sizeof(EXRContext), .priv_data_size = sizeof(EXRContext),
.close = decode_end, .init = decode_init,
.decode = decode_frame, .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
.capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS | CODEC_CAP_SLICE_THREADS, .close = decode_end,
.priv_class = &exr_class, .decode = decode_frame,
.capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS |
CODEC_CAP_SLICE_THREADS,
.priv_class = &exr_class,
}; };
...@@ -2682,6 +2682,7 @@ static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id) ...@@ -2682,6 +2682,7 @@ static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
case AV_CODEC_ID_PCM_S24LE_PLANAR_DEPRECATED : return AV_CODEC_ID_PCM_S24LE_PLANAR; case AV_CODEC_ID_PCM_S24LE_PLANAR_DEPRECATED : return AV_CODEC_ID_PCM_S24LE_PLANAR;
case AV_CODEC_ID_PCM_S32LE_PLANAR_DEPRECATED : return AV_CODEC_ID_PCM_S32LE_PLANAR; case AV_CODEC_ID_PCM_S32LE_PLANAR_DEPRECATED : return AV_CODEC_ID_PCM_S32LE_PLANAR;
case AV_CODEC_ID_ESCAPE130_DEPRECATED : return AV_CODEC_ID_ESCAPE130; case AV_CODEC_ID_ESCAPE130_DEPRECATED : return AV_CODEC_ID_ESCAPE130;
case AV_CODEC_ID_EXR_DEPRECATED : return AV_CODEC_ID_EXR;
case AV_CODEC_ID_G2M_DEPRECATED : return AV_CODEC_ID_G2M; case AV_CODEC_ID_G2M_DEPRECATED : return AV_CODEC_ID_G2M;
case AV_CODEC_ID_PAF_VIDEO_DEPRECATED : return AV_CODEC_ID_PAF_VIDEO; case AV_CODEC_ID_PAF_VIDEO_DEPRECATED : return AV_CODEC_ID_PAF_VIDEO;
case AV_CODEC_ID_WEBP_DEPRECATED: return AV_CODEC_ID_WEBP; case AV_CODEC_ID_WEBP_DEPRECATED: return AV_CODEC_ID_WEBP;
......
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
#define LIBAVCODEC_VERSION_MAJOR 55 #define LIBAVCODEC_VERSION_MAJOR 55
#define LIBAVCODEC_VERSION_MINOR 55 #define LIBAVCODEC_VERSION_MINOR 55
#define LIBAVCODEC_VERSION_MICRO 106 #define LIBAVCODEC_VERSION_MICRO 107
#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
LIBAVCODEC_VERSION_MINOR, \ LIBAVCODEC_VERSION_MINOR, \
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment