diff options
Diffstat (limited to 'libavcodec/exr.c')
-rw-r--r-- | libavcodec/exr.c | 169 |
1 files changed, 121 insertions, 48 deletions
diff --git a/libavcodec/exr.c b/libavcodec/exr.c index cdcdd7cc6c..3b6b245187 100644 --- a/libavcodec/exr.c +++ b/libavcodec/exr.c @@ -2,20 +2,20 @@ * OpenEXR (.exr) image decoder * Copyright (c) 2009 Jimmy Christensen * - * This file is part of Libav + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * FFmpeg is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * - * Libav is distributed in the hope that it will be useful, + * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -37,6 +37,7 @@ #include "libavutil/imgutils.h" #include "libavutil/intfloat.h" #include "libavutil/opt.h" +#include "libavutil/color_utils.h" #include "avcodec.h" #include "bytestream.h" @@ -110,6 +111,7 @@ typedef struct EXRContext { const char *layer; + enum AVColorTransferCharacteristic apply_trc_type; float gamma; uint16_t gamma_table[65536]; } EXRContext; @@ -722,8 +724,8 @@ static int piz_uncompress(EXRContext *s, const uint8_t *src, int ssize, if (!td->lut) td->lut = av_malloc(1 << 17); if (!td->bitmap || !td->lut) { - av_free(td->bitmap); - av_free(td->lut); + av_freep(&td->bitmap); + av_freep(&td->lut); return AVERROR(ENOMEM); } @@ -842,6 +844,7 @@ static int decode_block(AVCodecContext *avctx, void *tdata, int bxmin = s->xmin * 2 * s->desc->nb_components; int i, x, buf_size = s->buf_size; float one_gamma = 1.0f / s->gamma; + avpriv_trc_function trc_func = avpriv_get_trc_function_from_trc(s->apply_trc_type); int ret; line_offset = AV_RL64(s->gb.buffer + jobnr * 8); @@ -921,24 +924,43 @@ static int decode_block(AVCodecContext *avctx, void *tdata, ptr_x += s->xmin * s->desc->nb_components; if (s->pixel_type == EXR_FLOAT) { // 32-bit - for (x = 0; x < xdelta; x++) { - union av_intfloat32 t; - t.i = bytestream_get_le32(&r); - if (t.f > 0.0f) /* avoid negative values */ - t.f = powf(t.f, one_gamma); - *ptr_x++ = exr_flt2uint(t.i); - - t.i = bytestream_get_le32(&g); - if (t.f > 0.0f) - t.f = powf(t.f, one_gamma); - *ptr_x++ = exr_flt2uint(t.i); - - t.i = bytestream_get_le32(&b); - if (t.f > 0.0f) - t.f = powf(t.f, one_gamma); - *ptr_x++ = exr_flt2uint(t.i); - if (channel_buffer[3]) - *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a)); + if (trc_func) { + for (x = 0; x < xdelta; x++) { + union av_intfloat32 t; + t.i = bytestream_get_le32(&r); + t.f = trc_func(t.f); + *ptr_x++ = exr_flt2uint(t.i); + + t.i = bytestream_get_le32(&g); + t.f = trc_func(t.f); + *ptr_x++ = exr_flt2uint(t.i); + + t.i = bytestream_get_le32(&b); + t.f = trc_func(t.f); + *ptr_x++ = exr_flt2uint(t.i); + if (channel_buffer[3]) + *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a)); + } + } else { + for (x = 0; x < xdelta; x++) { + union av_intfloat32 t; + t.i = bytestream_get_le32(&r); + if (t.f > 0.0f) /* avoid negative values */ + t.f = powf(t.f, one_gamma); + *ptr_x++ = exr_flt2uint(t.i); + + t.i = bytestream_get_le32(&g); + if (t.f > 0.0f) + t.f = powf(t.f, one_gamma); + *ptr_x++ = exr_flt2uint(t.i); + + t.i = bytestream_get_le32(&b); + if (t.f > 0.0f) + t.f = powf(t.f, one_gamma); + *ptr_x++ = exr_flt2uint(t.i); + if (channel_buffer[3]) + *ptr_x++ = exr_flt2uint(bytestream_get_le32(&a)); + } } } else { // 16-bit @@ -1010,6 +1032,22 @@ static int decode_header(EXRContext *s) int current_channel_offset = 0; int magic_number, version, flags, i; + 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; + if (bytestream2_get_bytes_left(&s->gb) < 10) { av_log(s->avctx, AV_LOG_ERROR, "Header too short to parse.\n"); return AVERROR_INVALIDDATA; @@ -1271,6 +1309,9 @@ static int decode_frame(AVCodecContext *avctx, void *data, return AVERROR_INVALIDDATA; } + if (s->apply_trc_type != AVCOL_TRC_UNSPECIFIED) + avctx->color_trc = s->apply_trc_type; + switch (s->compression) { case EXR_RAW: case EXR_RLE: @@ -1348,36 +1389,31 @@ static av_cold int decode_init(AVCodecContext *avctx) uint32_t i; union av_intfloat32 t; float one_gamma = 1.0f / s->gamma; + avpriv_trc_function trc_func = NULL; 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; - if (one_gamma > 0.9999f && one_gamma < 1.0001f) { - for (i = 0; i < 65536; ++i) - s->gamma_table[i] = exr_halflt2uint(i); - } else { + trc_func = avpriv_get_trc_function_from_trc(s->apply_trc_type); + if (trc_func) { for (i = 0; i < 65536; ++i) { t = exr_half2float(i); - /* If negative value we reuse half value */ - if (t.f <= 0.0f) { + t.f = trc_func(t.f); + s->gamma_table[i] = exr_flt2uint(t.i); + } + } else { + if (one_gamma > 0.9999f && one_gamma < 1.0001f) { + for (i = 0; i < 65536; ++i) s->gamma_table[i] = exr_halflt2uint(i); - } else { - t.f = powf(t.f, one_gamma); - s->gamma_table[i] = exr_flt2uint(t.i); + } else { + for (i = 0; i < 65536; ++i) { + t = exr_half2float(i); + /* If negative value we reuse half value */ + if (t.f <= 0.0f) { + s->gamma_table[i] = exr_halflt2uint(i); + } else { + t.f = powf(t.f, one_gamma); + s->gamma_table[i] = exr_flt2uint(t.i); + } } } } @@ -1426,6 +1462,43 @@ static const AVOption options[] = { AV_OPT_TYPE_STRING, { .str = "" }, 0, 0, VD }, { "gamma", "Set the float gamma value when decoding", OFFSET(gamma), AV_OPT_TYPE_FLOAT, { .dbl = 1.0f }, 0.001, FLT_MAX, VD }, + + // XXX: Note the abuse of the enum using AVCOL_TRC_UNSPECIFIED to subsume the existing gamma option + { "apply_trc", "color transfer characteristics to apply to EXR linear input", OFFSET(apply_trc_type), + AV_OPT_TYPE_INT, {.i64 = AVCOL_TRC_UNSPECIFIED }, 1, AVCOL_TRC_NB-1, VD, "apply_trc_type"}, + { "bt709", "BT.709", 0, + AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT709 }, INT_MIN, INT_MAX, VD, "apply_trc_type"}, + { "gamma", "gamma", 0, + AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_UNSPECIFIED }, INT_MIN, INT_MAX, VD, "apply_trc_type"}, + { "gamma22", "BT.470 M", 0, + AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_GAMMA22 }, INT_MIN, INT_MAX, VD, "apply_trc_type"}, + { "gamma28", "BT.470 BG", 0, + AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_GAMMA28 }, INT_MIN, INT_MAX, VD, "apply_trc_type"}, + { "smpte170m", "SMPTE 170 M", 0, + AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTE170M }, INT_MIN, INT_MAX, VD, "apply_trc_type"}, + { "smpte240m", "SMPTE 240 M", 0, + AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTE240M }, INT_MIN, INT_MAX, VD, "apply_trc_type"}, + { "linear", "Linear", 0, + AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LINEAR }, INT_MIN, INT_MAX, VD, "apply_trc_type"}, + { "log", "Log", 0, + AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LOG }, INT_MIN, INT_MAX, VD, "apply_trc_type"}, + { "log_sqrt", "Log square root", 0, + AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_LOG_SQRT }, INT_MIN, INT_MAX, VD, "apply_trc_type"}, + { "iec61966_2_4", "IEC 61966-2-4", 0, + AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_IEC61966_2_4 }, INT_MIN, INT_MAX, VD, "apply_trc_type"}, + { "bt1361", "BT.1361", 0, + AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT1361_ECG }, INT_MIN, INT_MAX, VD, "apply_trc_type"}, + { "iec61966_2_1", "IEC 61966-2-1", 0, + AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_IEC61966_2_1 }, INT_MIN, INT_MAX, VD, "apply_trc_type"}, + { "bt2020_10bit", "BT.2020 - 10 bit", 0, + AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT2020_10 }, INT_MIN, INT_MAX, VD, "apply_trc_type"}, + { "bt2020_12bit", "BT.2020 - 12 bit", 0, + AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_BT2020_12 }, INT_MIN, INT_MAX, VD, "apply_trc_type"}, + { "smpte2084", "SMPTE ST 2084", 0, + AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTEST2084 }, INT_MIN, INT_MAX, VD, "apply_trc_type"}, + { "smpte428_1", "SMPTE ST 428-1", 0, + AV_OPT_TYPE_CONST, {.i64 = AVCOL_TRC_SMPTEST428_1 }, INT_MIN, INT_MAX, VD, "apply_trc_type"}, + { NULL }, }; |