From patchwork Sun Feb 28 12:53:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 26023 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id AACEB44B64D for ; Sun, 28 Feb 2021 14:53:45 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 795CC68AA6E; Sun, 28 Feb 2021 14:53:45 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f52.google.com (mail-ej1-f52.google.com [209.85.218.52]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 10A0A687F3D for ; Sun, 28 Feb 2021 14:53:39 +0200 (EET) Received: by mail-ej1-f52.google.com with SMTP id a22so23048628ejv.9 for ; Sun, 28 Feb 2021 04:53:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=QXi+g6LkWH8sTX1SU+n2fvq2lKs2hZdQ08fgm4lDO00=; b=lddlrogi/CCMhb2wqWw3OW98HVNTL35SHNHyR5ZRa8ALWec/yzSZlH35mfec697tlF sxzNl45gMuQuUszdddQTdJQURkuwWCF5fZGGcPUdP5Z/wcwuc+FA2fyFtv6OOggEUAA7 5hhKLz/SL0acDSG61NWezPhYU2szNRNr5IKtVZeM0MWTr1qKoLULBv/+At1auikv+qiE FMwWVeh9ZxgrxnFVpqhN/YIW0BioDmDG7DNXxpBShl/rQOH3SgkCa9eOvYKTvdBVWcdz Ew4hY+b5SeD6KVIuiuyVUUsR2KkF3g3Di6XvPNzeIaekqsNMH/xM53xfAMOYxxSX7+4A TkmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=QXi+g6LkWH8sTX1SU+n2fvq2lKs2hZdQ08fgm4lDO00=; b=diF3urDhUFrJjy41IVCd2ouf6YNUazeZWjQGPOCayVrmZuGUBeBlPHx4rFkEEEPMrL OGCtdQ0lRSMcAT1N4IP1bqSktLPwIvNIjl/KKbiAyQJKWxHHI/l0UmRSTc5B9Q7jD/Jk 47o3MDPoA1k05iu/O0cZY09K1fPpDdRAiVQ+JWxQuxKjE4gEIBOinaITdkPU4vymJoOg IlZGk6/iY8vhcpxdr3rcAJidetE9VjDEy8gVhiy2fjzMy768r2WT/CAi/6jOQpzDcaV0 pOim9DCQ1DTFrpLcHzadaKjtTfpHU0v9BHECgBWX5iri/ULCqZtXBvvGHgl+e6tn00j4 NFRA== X-Gm-Message-State: AOAM530YPCchxRJVrLod8seiyWm8GPIutO+RlPdgugKCnr3mbHbk3lNV Ri10LZHQ4ExPfGqHCVoerXcXB60PAao= X-Google-Smtp-Source: ABdhPJwh5Ku7q163mN8U1kVDjm8MEyROpeVkJBvYuFMOJ8c1aNxRpVc09sKQ4AwLIDPu1tdZpPUFaA== X-Received: by 2002:a17:906:1b54:: with SMTP id p20mr6291205ejg.307.1614516818483; Sun, 28 Feb 2021 04:53:38 -0800 (PST) Received: from localhost.localdomain ([109.227.52.18]) by smtp.gmail.com with ESMTPSA id q12sm2841876ejd.51.2021.02.28.04.53.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 28 Feb 2021 04:53:37 -0800 (PST) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Sun, 28 Feb 2021 13:53:25 +0100 Message-Id: <20210228125327.3390-1-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH 1/3] avcodec/exr: add DWA decompression support X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Paul B Mahol --- libavcodec/exr.c | 343 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 331 insertions(+), 12 deletions(-) diff --git a/libavcodec/exr.c b/libavcodec/exr.c index 4559174ba2..e0e525e53c 100644 --- a/libavcodec/exr.c +++ b/libavcodec/exr.c @@ -66,8 +66,8 @@ enum ExrCompr { EXR_PXR24, EXR_B44, EXR_B44A, - EXR_DWA, - EXR_DWB, + EXR_DWAA, + EXR_DWAB, EXR_UNKN, }; @@ -119,6 +119,20 @@ typedef struct EXRThreadData { uint8_t *bitmap; uint16_t *lut; + uint8_t *ac_data; + unsigned ac_size; + + uint8_t *dc_data; + unsigned dc_size; + + uint8_t *rle_data; + unsigned rle_size; + + uint8_t *rle_raw_data; + unsigned rle_raw_size; + + float block[3][64]; + int ysize, xsize; int channel_line_size; @@ -256,10 +270,10 @@ static int zip_uncompress(EXRContext *s, const uint8_t *src, int compressed_size return 0; } -static int rle_uncompress(EXRContext *ctx, const uint8_t *src, int compressed_size, - int uncompressed_size, EXRThreadData *td) +static int rle(uint8_t *dst, const uint8_t *src, + int compressed_size, int uncompressed_size) { - uint8_t *d = td->tmp; + uint8_t *d = dst; const int8_t *s = src; int ssize = compressed_size; int dsize = uncompressed_size; @@ -295,6 +309,14 @@ static int rle_uncompress(EXRContext *ctx, const uint8_t *src, int compressed_si if (dend != d) return AVERROR_INVALIDDATA; + return 0; +} + +static int rle_uncompress(EXRContext *ctx, const uint8_t *src, int compressed_size, + int uncompressed_size, EXRThreadData *td) +{ + rle(td->tmp, src, compressed_size, uncompressed_size); + av_assert1(uncompressed_size % 2 == 0); ctx->dsp.predictor(td->tmp, uncompressed_size); @@ -475,18 +497,16 @@ static int huf_uncompress(EXRContext *s, GetByteContext *gb, uint16_t *dst, int dst_size) { - int32_t src_size, im, iM; + int32_t im, iM; uint32_t nBits; int ret; - src_size = bytestream2_get_le32(gb); im = bytestream2_get_le32(gb); iM = bytestream2_get_le32(gb); bytestream2_skip(gb, 4); nBits = bytestream2_get_le32(gb); if (im < 0 || im >= HUF_ENCSIZE || - iM < 0 || iM >= HUF_ENCSIZE || - src_size < 0) + iM < 0 || iM >= HUF_ENCSIZE) return AVERROR_INVALIDDATA; bytestream2_skip(gb, 4); @@ -659,6 +679,7 @@ static int piz_uncompress(EXRContext *s, const uint8_t *src, int ssize, maxval = reverse_lut(td->bitmap, td->lut); + bytestream2_skip(&gb, 4); ret = huf_uncompress(s, td, &gb, tmp, dsize / sizeof(uint16_t)); if (ret) return ret; @@ -917,6 +938,284 @@ static int b44_uncompress(EXRContext *s, const uint8_t *src, int compressed_size return 0; } +static int ac_uncompress(EXRContext *s, GetByteContext *gb, float *block) +{ + int ret = 0, n = 1; + + while (n < 64) { + uint16_t val = bytestream2_get_ne16(gb); + + if (val == 0xff00) { + n = 64; + } else if ((val >> 8) == 0xff) { + n += val & 0xff; + } else { + ret = n; + block[ff_zigzag_direct[n]] = exr_half2float(val).f; + n++; + } + } + + return ret; +} + +static void idct_1d(float *blk, int step) +{ + const float a = .5f * cosf( M_PI / 4.f); + const float b = .5f * cosf( M_PI / 16.f); + const float c = .5f * cosf( M_PI / 8.f); + const float d = .5f * cosf(3.f*M_PI / 16.f); + const float e = .5f * cosf(5.f*M_PI / 16.f); + const float f = .5f * cosf(3.f*M_PI / 8.f); + const float g = .5f * cosf(7.f*M_PI / 16.f); + + float alpha[4], beta[4], theta[4], gamma[4]; + + alpha[0] = c * blk[2 * step]; + alpha[1] = f * blk[2 * step]; + alpha[2] = c * blk[6 * step]; + alpha[3] = f * blk[6 * step]; + + beta[0] = b * blk[1 * step] + d * blk[3 * step] + e * blk[5 * step] + g * blk[7 * step]; + beta[1] = d * blk[1 * step] - g * blk[3 * step] - b * blk[5 * step] - e * blk[7 * step]; + beta[2] = e * blk[1 * step] - b * blk[3 * step] + g * blk[5 * step] + d * blk[7 * step]; + beta[3] = g * blk[1 * step] - e * blk[3 * step] + d * blk[5 * step] - b * blk[7 * step]; + + theta[0] = a * (blk[0 * step] + blk[4 * step]); + theta[3] = a * (blk[0 * step] - blk[4 * step]); + + theta[1] = alpha[0] + alpha[3]; + theta[2] = alpha[1] - alpha[2]; + + gamma[0] = theta[0] + theta[1]; + gamma[1] = theta[3] + theta[2]; + gamma[2] = theta[3] - theta[2]; + gamma[3] = theta[0] - theta[1]; + + blk[0 * step] = gamma[0] + beta[0]; + blk[1 * step] = gamma[1] + beta[1]; + blk[2 * step] = gamma[2] + beta[2]; + blk[3 * step] = gamma[3] + beta[3]; + + blk[4 * step] = gamma[3] - beta[3]; + blk[5 * step] = gamma[2] - beta[2]; + blk[6 * step] = gamma[1] - beta[1]; + blk[7 * step] = gamma[0] - beta[0]; +} + +static void dct_inverse(float *block) +{ + for (int i = 0; i < 8; i++) + idct_1d(block + i, 8); + + for (int i = 0; i < 8; i++) { + idct_1d(block, 1); + block += 8; + } +} + +static void convert(float y, float u, float v, + float *b, float *g, float *r) +{ + *r = y + 1.5747f * v; + *g = y - 0.1873f * u - 0.4682f * v; + *b = y + 1.8556f * u; +} + +static float to_linear(float x, float scale) +{ + float ax = fabsf(x); + + if (ax <= 1.f) { + return FFSIGN(x) * powf(ax, 2.2f * scale); + } else { + const float log_base = expf(2.2f * scale); + + return FFSIGN(x) * powf(log_base, ax - 1.f); + } +} + +static int dwa_uncompress(EXRContext *s, const uint8_t *src, int compressed_size, + int uncompressed_size, EXRThreadData *td) +{ + int64_t version, lo_usize, lo_size; + int64_t ac_size, dc_size, rle_usize, rle_csize, rle_raw_size; + int64_t ac_count, dc_count, ac_compression; + const int dc_w = td->xsize >> 3; + const int dc_h = td->ysize >> 3; + GetByteContext gb, agb; + int skip, ret; + + if (compressed_size <= 88) + return AVERROR_INVALIDDATA; + + version = AV_RL64(src + 0); + if (version != 2) + return AVERROR_INVALIDDATA; + + lo_usize = AV_RL64(src + 8); + lo_size = AV_RL64(src + 16); + ac_size = AV_RL64(src + 24); + dc_size = AV_RL64(src + 32); + rle_csize = AV_RL64(src + 40); + rle_usize = AV_RL64(src + 48); + rle_raw_size = AV_RL64(src + 56); + ac_count = AV_RL64(src + 64); + dc_count = AV_RL64(src + 72); + ac_compression = AV_RL64(src + 80); + + if (compressed_size < 88LL + lo_size + ac_size + dc_size + rle_csize) + return AVERROR_INVALIDDATA; + + bytestream2_init(&gb, src + 88, compressed_size - 88); + skip = bytestream2_get_le16(&gb); + if (skip < 2) + return AVERROR_INVALIDDATA; + + bytestream2_skip(&gb, skip - 2); + + if (lo_size > 0) { + if (lo_usize > uncompressed_size) + return AVERROR_INVALIDDATA; + bytestream2_skip(&gb, lo_size); + } + + if (ac_size > 0) { + unsigned long dest_len = ac_count * 2LL; + GetByteContext agb = gb; + + if (ac_count > 3LL * td->xsize * s->scan_lines_per_block) + return AVERROR_INVALIDDATA; + + av_fast_padded_malloc(&td->ac_data, &td->ac_size, dest_len); + if (!td->ac_data) + return AVERROR(ENOMEM); + + switch (ac_compression) { + case 0: + ret = huf_uncompress(s, td, &agb, (int16_t *)td->ac_data, ac_count); + if (ret < 0) + return ret; + break; + case 1: + if (uncompress(td->ac_data, &dest_len, agb.buffer, ac_size) != Z_OK || + dest_len != ac_count * 2LL) + return AVERROR_INVALIDDATA; + break; + default: + return AVERROR_INVALIDDATA; + } + + bytestream2_skip(&gb, ac_size); + } + + if (dc_size > 0) { + unsigned long dest_len = dc_count * 2LL; + GetByteContext agb = gb; + + if (dc_count > (6LL * td->xsize * td->ysize + 63) / 64) + return AVERROR_INVALIDDATA; + + av_fast_padded_malloc(&td->dc_data, &td->dc_size, FFALIGN(dest_len, 64) * 2); + if (!td->dc_data) + return AVERROR(ENOMEM); + + if (uncompress(td->dc_data + FFALIGN(dest_len, 64), &dest_len, agb.buffer, dc_size) != Z_OK || + (dest_len != dc_count * 2LL)) + return AVERROR_INVALIDDATA; + + s->dsp.predictor(td->dc_data + FFALIGN(dest_len, 64), dest_len); + s->dsp.reorder_pixels(td->dc_data, td->dc_data + FFALIGN(dest_len, 64), dest_len); + + bytestream2_skip(&gb, dc_size); + } + + if (rle_raw_size > 0 && rle_csize > 0 && rle_usize > 0) { + unsigned long dest_len = rle_usize; + + av_fast_padded_malloc(&td->rle_data, &td->rle_size, rle_usize); + if (!td->rle_data) + return AVERROR(ENOMEM); + + av_fast_padded_malloc(&td->rle_raw_data, &td->rle_raw_size, rle_raw_size); + if (!td->rle_raw_data) + return AVERROR(ENOMEM); + + if (uncompress(td->rle_data, &dest_len, gb.buffer, rle_csize) != Z_OK || + (dest_len != rle_usize)) + return AVERROR_INVALIDDATA; + + ret = rle(td->rle_raw_data, td->rle_data, rle_usize, rle_raw_size); + if (ret < 0) + return ret; + bytestream2_skip(&gb, rle_csize); + } + + bytestream2_init(&agb, td->ac_data, ac_count * 2); + + for (int y = 0; y < td->ysize; y += 8) { + for (int x = 0; x < td->xsize; x += 8) { + memset(td->block, 0, sizeof(td->block)); + + for (int j = 0; j < 3; j++) { + float *block = td->block[j]; + const int idx = (x >> 3) + (y >> 3) * dc_w + dc_w * dc_h * j; + uint16_t *dc = (uint16_t *)td->dc_data; + float dc_val = dc[idx]; + + dc_val = exr_half2float(dc_val).f; + block[0] = dc_val; + ac_uncompress(s, &agb, block); + dct_inverse(block); + } + + { + const float scale = s->pixel_type == EXR_FLOAT ? 2.f : 1.f; + const int o = s->nb_channels == 4; + float *bo = ((float *)td->uncompressed_data) + + y * td->xsize * s->nb_channels + td->xsize * (o + 0) + x; + float *go = ((float *)td->uncompressed_data) + + y * td->xsize * s->nb_channels + td->xsize * (o + 1) + x; + float *ro = ((float *)td->uncompressed_data) + + y * td->xsize * s->nb_channels + td->xsize * (o + 2) + x; + float *yb = td->block[0]; + float *ub = td->block[1]; + float *vb = td->block[2]; + + for (int yy = 0; yy < 8; yy++) { + for (int xx = 0; xx < 8; xx++) { + const int idx = xx + yy * 8; + + convert(yb[idx], ub[idx], vb[idx], &bo[xx], &go[xx], &ro[xx]); + + bo[xx] = to_linear(bo[xx], scale); + go[xx] = to_linear(go[xx], scale); + ro[xx] = to_linear(ro[xx], scale); + } + + bo += td->xsize * s->nb_channels; + go += td->xsize * s->nb_channels; + ro += td->xsize * s->nb_channels; + } + } + } + } + + if (s->nb_channels < 4) + return 0; + + for (int y = 0; y < td->ysize && td->rle_raw_data; y++) { + uint32_t *ao = ((uint32_t *)td->uncompressed_data) + y * td->xsize * s->nb_channels; + uint8_t *ai0 = td->rle_raw_data + y * td->xsize; + uint8_t *ai1 = td->rle_raw_data + y * td->xsize + rle_raw_size / 2; + + for (int x = 0; x < td->xsize; x++) + ao[x] = exr_half2float(ai0[x] | (ai1[x] << 8)).i; + } + + return 0; +} + static int decode_block(AVCodecContext *avctx, void *tdata, int jobnr, int threadnr) { @@ -1072,6 +1371,10 @@ static int decode_block(AVCodecContext *avctx, void *tdata, case EXR_B44A: ret = b44_uncompress(s, src, data_size, uncompressed_size, td); break; + case EXR_DWAA: + case EXR_DWAB: + ret = dwa_uncompress(s, src, data_size, uncompressed_size, td); + break; } if (ret < 0) { av_log(avctx, AV_LOG_ERROR, "decode_block() failed.\n"); @@ -1098,7 +1401,6 @@ static int decode_block(AVCodecContext *avctx, void *tdata, channel_buffer[3] = src + (td->xsize * s->channel_offsets[3]) + data_window_offset; if (s->desc->flags & AV_PIX_FMT_FLAG_FLOAT) { - /* todo: change this when a floating point pixel format with luma with alpha is implemented */ int channel_count = s->channel_offsets[3] >= 0 ? 4 : rgb_channel_count; if (s->is_luma) { @@ -1121,7 +1423,9 @@ static int decode_block(AVCodecContext *avctx, void *tdata, memset(ptr_x, 0, bxmin); ptr_x += window_xoffset; - if (s->pixel_type == EXR_FLOAT) { + if (s->pixel_type == EXR_FLOAT || + s->compression == EXR_DWAA || + s->compression == EXR_DWAB) { // 32-bit union av_intfloat32 t; if (trc_func && c < 3) { @@ -1146,7 +1450,7 @@ static int decode_block(AVCodecContext *avctx, void *tdata, } } else { for (x = 0; x < xsize; x++) { - *ptr_x++ = exr_half2float(bytestream_get_le16(&src));; + *ptr_x++ = exr_half2float(bytestream_get_le16(&src)); } } } @@ -1761,6 +2065,13 @@ static int decode_frame(AVCodecContext *avctx, void *data, if ((ret = decode_header(s, picture)) < 0) return ret; + if ((s->compression == EXR_DWAA || s->compression == EXR_DWAB) && + s->pixel_type == EXR_HALF) { + s->current_channel_offset *= 2; + for (int i = 0; i < 4; i++) + s->channel_offsets[i] *= 2; + } + switch (s->pixel_type) { case EXR_FLOAT: case EXR_HALF: @@ -1815,8 +2126,12 @@ static int decode_frame(AVCodecContext *avctx, void *data, case EXR_PIZ: case EXR_B44: case EXR_B44A: + case EXR_DWAA: s->scan_lines_per_block = 32; break; + case EXR_DWAB: + s->scan_lines_per_block = 256; + break; default: avpriv_report_missing_feature(avctx, "Compression %d", s->compression); return AVERROR_PATCHWELCOME; @@ -1976,6 +2291,10 @@ static av_cold int decode_end(AVCodecContext *avctx) av_freep(&td->lut); av_freep(&td->he); av_freep(&td->freq); + av_freep(&td->ac_data); + av_freep(&td->dc_data); + av_freep(&td->rle_data); + av_freep(&td->rle_raw_data); ff_free_vlc(&td->vlc); } From patchwork Sun Feb 28 12:53:26 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 26024 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 0FD1B44B64D for ; Sun, 28 Feb 2021 14:53:48 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id EBA9F68AA67; Sun, 28 Feb 2021 14:53:47 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ed1-f44.google.com (mail-ed1-f44.google.com [209.85.208.44]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id E927968AA3E for ; Sun, 28 Feb 2021 14:53:39 +0200 (EET) Received: by mail-ed1-f44.google.com with SMTP id l12so16980314edt.3 for ; Sun, 28 Feb 2021 04:53:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=corg42icEDfip2MLVwvalXFS5Q7JSmpQTeO49aAGizo=; b=mPO1idkK9QsaKJGyjtSk7Lmo2llq+dTI+BJpLs+Fk/7KvZlIffIIRhKMt0L4ne79gc 7b0vUpqMT65C2b/4x6YRt0zIQLBlXG59IH25OQbYsvBrCFgdwr0Alco6QLzo0fcTI5GU 0fQOODZkcHbFpP7NaZmE4M46FgVpXBdwE8R8WZwqxecV6RibuPS1KaZEVmwOZGP+A0ZR 7iPHTe6U/iFBK3F5iOrT66NjldC5nmM9j+dGKEY6skGiUCZ1ifyrj+BvVz3DYsI+zQ78 SRF87+lzjg/w06eYsrusLX0TamJmy9CyC/gUbHn4MZbQdldz+6Am0gBpfieb4g6APypD zAsA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=corg42icEDfip2MLVwvalXFS5Q7JSmpQTeO49aAGizo=; b=sRFf1GVcR7/qm1vzlnjP/PZTdwJtZAv4eXy5TfP5EePHNrRXmCAJGsaPnqdeKRe9dw IgPA6Lt0OYGxtbuVFo0t8aBpEQzMxKa1th2tNQulE7eNoLZkVwT9YQMyGT7P0BAie6cf SIyI45xs2pbnH5JDye95qZ85cKtYtkfBt530TKQsaa1/d4xuf9kZud8R9YbgXMnKz7Y3 gRNGxOV0nB2K+zeuAqHGdPfUWaHNkgBnjl4acpazs9C1tapbxq7wfYuevJe265xo7RYn zGUgtZFbp5X2Mwa2x90EKymCai9phocUvCVBFctPDMzzEucOuO1n89Yl4C2Gmk7SoH4r IkzQ== X-Gm-Message-State: AOAM533GKkTGUBmLiDJjvnHPsRIioxWk0kyk0e9iRyzEnCVigDP6tL38 onf99uHpDYVPvgHQFx839cvT7rNC0uA= X-Google-Smtp-Source: ABdhPJwULouiV+wPHPtFFnVB+4HLfVxz5kMFr28o6ZJ1m1RG0Qml9YI9c6t5IkaUNKSYnKtq1RNF0Q== X-Received: by 2002:a50:b5e4:: with SMTP id a91mr12021010ede.173.1614516819411; Sun, 28 Feb 2021 04:53:39 -0800 (PST) Received: from localhost.localdomain ([109.227.52.18]) by smtp.gmail.com with ESMTPSA id q12sm2841876ejd.51.2021.02.28.04.53.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 28 Feb 2021 04:53:39 -0800 (PST) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Sun, 28 Feb 2021 13:53:26 +0100 Message-Id: <20210228125327.3390-2-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210228125327.3390-1-onemda@gmail.com> References: <20210228125327.3390-1-onemda@gmail.com> Subject: [FFmpeg-devel] [PATCH 2/3] avcodec/exrenc: move float2half code to own header X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Paul B Mahol --- libavcodec/exrenc.c | 52 +++----------------------------- libavcodec/float2half.h | 67 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 47 deletions(-) create mode 100644 libavcodec/float2half.h diff --git a/libavcodec/exrenc.c b/libavcodec/exrenc.c index f9d802543d..db9000a85c 100644 --- a/libavcodec/exrenc.c +++ b/libavcodec/exrenc.c @@ -34,6 +34,7 @@ #include "avcodec.h" #include "bytestream.h" #include "internal.h" +#include "float2half.h" enum ExrCompr { EXR_RAW, @@ -87,54 +88,11 @@ typedef struct EXRContext { uint8_t shifttable[512]; } EXRContext; -static void half_tables(EXRContext *s) -{ - for (int i = 0; i < 256; i++) { - int e = i - 127; - - if (e < -24) { // Very small numbers map to zero - s->basetable[i|0x000] = 0x0000; - s->basetable[i|0x100] = 0x8000; - s->shifttable[i|0x000] = 24; - s->shifttable[i|0x100] = 24; - } else if (e < -14) { // Small numbers map to denorms - s->basetable[i|0x000] = (0x0400>>(-e-14)); - s->basetable[i|0x100] = (0x0400>>(-e-14)) | 0x8000; - s->shifttable[i|0x000] = -e-1; - s->shifttable[i|0x100] = -e-1; - } else if (e <= 15) { // Normal numbers just lose precision - s->basetable[i|0x000] = ((e + 15) << 10); - s->basetable[i|0x100] = ((e + 15) << 10) | 0x8000; - s->shifttable[i|0x000] = 13; - s->shifttable[i|0x100] = 13; - } else if (e < 128) { // Large numbers map to Infinity - s->basetable[i|0x000] = 0x7C00; - s->basetable[i|0x100] = 0xFC00; - s->shifttable[i|0x000] = 24; - s->shifttable[i|0x100] = 24; - } else{ // Infinity and NaN's stay Infinity and NaN's - s->basetable[i|0x000] = 0x7C00; - s->basetable[i|0x100] = 0xFC00; - s->shifttable[i|0x000] = 13; - s->shifttable[i|0x100] = 13; - } - } -} - -static uint16_t float2half(EXRContext *s, uint32_t f) -{ - uint16_t h; - - h = s->basetable[(f >> 23) & 0x1ff] + ((f & 0x007fffff) >> s->shifttable[(f >> 23) & 0x1ff]); - - return h; -} - static int encode_init(AVCodecContext *avctx) { EXRContext *s = avctx->priv_data; - half_tables(s); + float2half_tables(s->basetable, s->shifttable); switch (avctx->pix_fmt) { case AV_PIX_FMT_GBRPF32: @@ -290,7 +248,7 @@ static int encode_scanline_rle(EXRContext *s, const AVFrame *frame) uint32_t *src = (uint32_t *)(frame->data[ch] + y * frame->linesize[ch]); for (int x = 0; x < frame->width; x++) - dst[x] = float2half(s, src[x]); + dst[x] = float2half(src[x], s->basetable, s->shifttable); } break; } @@ -358,7 +316,7 @@ static int encode_scanline_zip(EXRContext *s, const AVFrame *frame) uint32_t *src = (uint32_t *)(frame->data[ch] + (y * s->scanline_height + l) * frame->linesize[ch]); for (int x = 0; x < frame->width; x++) - dst[x] = float2half(s, src[x]); + dst[x] = float2half(src[x], s->basetable, s->shifttable); } } break; @@ -516,7 +474,7 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, uint32_t *src = (uint32_t *)(frame->data[ch] + y * frame->linesize[ch]); for (int x = 0; x < frame->width; x++) - bytestream2_put_le16(pb, float2half(s, src[x])); + bytestream2_put_le16(pb, float2half(src[x], s->basetable, s->shifttable)); } } } diff --git a/libavcodec/float2half.h b/libavcodec/float2half.h new file mode 100644 index 0000000000..ff547b490c --- /dev/null +++ b/libavcodec/float2half.h @@ -0,0 +1,67 @@ +/* + * This file is part of FFmpeg. + * + * 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. + * + * 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AVCODEC_FLOAT2HALF_H +#define AVCODEC_FLOAT2HALF_H + +#include + +static void float2half_tables(uint16_t *basetable, uint8_t *shifttable) +{ + for (int i = 0; i < 256; i++) { + int e = i - 127; + + if (e < -24) { // Very small numbers map to zero + basetable[i|0x000] = 0x0000; + basetable[i|0x100] = 0x8000; + shifttable[i|0x000] = 24; + shifttable[i|0x100] = 24; + } else if (e < -14) { // Small numbers map to denorms + basetable[i|0x000] = (0x0400>>(-e-14)); + basetable[i|0x100] = (0x0400>>(-e-14)) | 0x8000; + shifttable[i|0x000] = -e-1; + shifttable[i|0x100] = -e-1; + } else if (e <= 15) { // Normal numbers just lose precision + basetable[i|0x000] = ((e + 15) << 10); + basetable[i|0x100] = ((e + 15) << 10) | 0x8000; + shifttable[i|0x000] = 13; + shifttable[i|0x100] = 13; + } else if (e < 128) { // Large numbers map to Infinity + basetable[i|0x000] = 0x7C00; + basetable[i|0x100] = 0xFC00; + shifttable[i|0x000] = 24; + shifttable[i|0x100] = 24; + } else{ // Infinity and NaN's stay Infinity and NaN's + basetable[i|0x000] = 0x7C00; + basetable[i|0x100] = 0xFC00; + shifttable[i|0x000] = 13; + shifttable[i|0x100] = 13; + } + } +} + +static uint16_t float2half(uint32_t f, uint16_t *basetable, uint8_t *shifttable) +{ + uint16_t h; + + h = basetable[(f >> 23) & 0x1ff] + ((f & 0x007fffff) >> shifttable[(f >> 23) & 0x1ff]); + + return h; +} + +#endif /* AVCODEC_FLOAT2HALF_H */ From patchwork Sun Feb 28 12:53:27 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 26025 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 2E1BC44B64D for ; Sun, 28 Feb 2021 14:53:51 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 18B4668AA95; Sun, 28 Feb 2021 14:53:51 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ed1-f44.google.com (mail-ed1-f44.google.com [209.85.208.44]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 24FCB68AA46 for ; Sun, 28 Feb 2021 14:53:45 +0200 (EET) Received: by mail-ed1-f44.google.com with SMTP id l12so16980464edt.3 for ; Sun, 28 Feb 2021 04:53:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=dJ2yGRm2xzGlRukVI9d0+dtDSGEsTmZoK2QDGQxol6A=; b=pD5v9dsJeyWHOVqfbYBMZ8tjeIFCjNomH1A0h+crEt6Debq4RTn9VHUFZNdBqsHHZ7 eoVtRDx76R36Ftm5ToBIkXCiuvrR0VGP/Cvs2pBwUsEUJAhMEc/CHFkDo8n4WBAZ58uZ BEsq3EwigT6pEW6trNd0fAIv4aYg9MRBVk+tyOsz6GtZz+RHKdic1NyzR2pezIaPdofE 2Q++ZJBPqFpDyHRT5Y55VcCMUHnt9dWfeYicuSGtYBgzM+P9qhhQVw7z3tX90MFq8BFH cm+E58sILahm/Tajw2gP1HFMhFFJcaAXGkVZRAewGM8wVdVG71z5uJ7WOtoNk+T/kz5B xndw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=dJ2yGRm2xzGlRukVI9d0+dtDSGEsTmZoK2QDGQxol6A=; b=XUChfXH1jRq+3uhZhpuf53PlJ/G31WhYBu43bYET35zqZGlLb8cd6+pi7XoNyvmUgo uJFtpdz36a0gT+7KJebqhYzmIv8KndZzExSpjToqu+bt2dslTo7XDfJpNAfNu5Eek7oB Sbd+5ulylSfpsDXIqdT+CFgspbNg1fNhl6wKGFoeJ4+5rCw/i2E3hlG2tPk1+Oj6ZISz ZmGX6m06RVvn2XN5/Yqr+ZVGcEZtYt7zv0oFrB3A2t+rvWjUIXelKXdL29RqXvehRnlL GNWEJl/TNpqioVS5aaI7RdHkixpCGItxnQDs6Vfia2LUsyJ2IAlIFLwAN87nIAriSzHN yxNQ== X-Gm-Message-State: AOAM533HndvqmNOGna1gU3ixOIIXT0rGDLfjyhIx+NRBs4Q+awzuGUfm LOG6uwWbHUGhj5v3J4yKQXz/8KATtcQ= X-Google-Smtp-Source: ABdhPJwQlA+YCi8MAV1cUxHTeBcW/yAc/tv/nfuaol+fDIieEtQAIvEbST2S/R37FnnIrvNwPQ1Fjw== X-Received: by 2002:a05:6402:216:: with SMTP id t22mr11834354edv.168.1614516824708; Sun, 28 Feb 2021 04:53:44 -0800 (PST) Received: from localhost.localdomain ([109.227.52.18]) by smtp.gmail.com with ESMTPSA id q12sm2841876ejd.51.2021.02.28.04.53.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 28 Feb 2021 04:53:44 -0800 (PST) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Sun, 28 Feb 2021 13:53:27 +0100 Message-Id: <20210228125327.3390-3-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210228125327.3390-1-onemda@gmail.com> References: <20210228125327.3390-1-onemda@gmail.com> Subject: [FFmpeg-devel] [PATCH 3/3] avcodec/exr: add lut oriented half to float conversion code X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Paul B Mahol --- libavcodec/exr.c | 90 +++++++++++++---------------------------- libavcodec/half2float.h | 74 +++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 61 deletions(-) create mode 100644 libavcodec/half2float.h diff --git a/libavcodec/exr.c b/libavcodec/exr.c index e0e525e53c..646b06bad9 100644 --- a/libavcodec/exr.c +++ b/libavcodec/exr.c @@ -29,8 +29,6 @@ * * For more information on the OpenEXR format, visit: * http://openexr.com/ - * - * exr_half2float() is credited to Aaftab Munshi, Dan Ginsburg, Dave Shreiner. */ #include @@ -54,6 +52,7 @@ #include "exrdsp.h" #include "get_bits.h" #include "internal.h" +#include "half2float.h" #include "mathops.h" #include "thread.h" @@ -190,6 +189,10 @@ typedef struct EXRContext { enum AVColorTransferCharacteristic apply_trc_type; float gamma; union av_intfloat32 gamma_table[65536]; + + uint32_t mantissatable[2048]; + uint32_t exponenttable[64]; + uint16_t offsettable[64]; } EXRContext; /* -15 stored using a single precision bias of 127 */ @@ -204,55 +207,6 @@ typedef struct EXRContext { #define HALF_FLOAT_MAX_BIASED_EXP (0x1F << 10) -/** - * Convert a half float as a uint16_t into a full float. - * - * @param hf half float as uint16_t - * - * @return float value - */ -static union av_intfloat32 exr_half2float(uint16_t hf) -{ - unsigned int sign = (unsigned int) (hf >> 15); - unsigned int mantissa = (unsigned int) (hf & ((1 << 10) - 1)); - unsigned int exp = (unsigned int) (hf & HALF_FLOAT_MAX_BIASED_EXP); - union av_intfloat32 f; - - if (exp == HALF_FLOAT_MAX_BIASED_EXP) { - // we have a half-float NaN or Inf - // half-float NaNs will be converted to a single precision NaN - // half-float Infs will be converted to a single precision Inf - exp = FLOAT_MAX_BIASED_EXP; - mantissa <<= 13; // preserve half-float NaN bits if set - } else if (exp == 0x0) { - // convert half-float zero/denorm to single precision value - if (mantissa) { - mantissa <<= 1; - exp = HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP; - // check for leading 1 in denorm mantissa - while (!(mantissa & (1 << 10))) { - // for every leading 0, decrement single precision exponent by 1 - // and shift half-float mantissa value to the left - mantissa <<= 1; - exp -= (1 << 23); - } - // clamp the mantissa to 10 bits - mantissa &= ((1 << 10) - 1); - // shift left to generate single-precision mantissa of 23 bits - mantissa <<= 13; - } - } else { - // shift left to generate single-precision mantissa of 23 bits - mantissa <<= 13; - // generate single precision biased exponent value - exp = (exp << 13) + HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP; - } - - f.i = (sign << 31) | exp | mantissa; - - return f; -} - static int zip_uncompress(EXRContext *s, const uint8_t *src, int compressed_size, int uncompressed_size, EXRThreadData *td) { @@ -951,7 +905,10 @@ static int ac_uncompress(EXRContext *s, GetByteContext *gb, float *block) n += val & 0xff; } else { ret = n; - block[ff_zigzag_direct[n]] = exr_half2float(val).f; + block[ff_zigzag_direct[n]] = av_int2float(half2float(val, + s->mantissatable, + s->exponenttable, + s->offsettable)); n++; } } @@ -1161,10 +1118,12 @@ static int dwa_uncompress(EXRContext *s, const uint8_t *src, int compressed_size float *block = td->block[j]; const int idx = (x >> 3) + (y >> 3) * dc_w + dc_w * dc_h * j; uint16_t *dc = (uint16_t *)td->dc_data; - float dc_val = dc[idx]; + union av_intfloat32 dc_val; - dc_val = exr_half2float(dc_val).f; - block[0] = dc_val; + dc_val.i = half2float(dc[idx], s->mantissatable, + s->exponenttable, s->offsettable); + + block[0] = dc_val.f; ac_uncompress(s, &agb, block); dct_inverse(block); } @@ -1209,8 +1168,11 @@ static int dwa_uncompress(EXRContext *s, const uint8_t *src, int compressed_size uint8_t *ai0 = td->rle_raw_data + y * td->xsize; uint8_t *ai1 = td->rle_raw_data + y * td->xsize + rle_raw_size / 2; - for (int x = 0; x < td->xsize; x++) - ao[x] = exr_half2float(ai0[x] | (ai1[x] << 8)).i; + for (int x = 0; x < td->xsize; x++) { + uint16_t ha = ai0[x] | (ai1[x] << 8); + + ao[x] = half2float(ha, s->mantissatable, s->exponenttable, s->offsettable); + } } return 0; @@ -1450,7 +1412,11 @@ static int decode_block(AVCodecContext *avctx, void *tdata, } } else { for (x = 0; x < xsize; x++) { - *ptr_x++ = exr_half2float(bytestream_get_le16(&src)); + ptr_x[0].i = half2float(bytestream_get_le16(&src), + s->mantissatable, + s->exponenttable, + s->offsettable); + ptr_x++; } } } @@ -2237,6 +2203,8 @@ static av_cold int decode_init(AVCodecContext *avctx) float one_gamma = 1.0f / s->gamma; avpriv_trc_function trc_func = NULL; + half2float_table(s->mantissatable, s->exponenttable, s->offsettable); + s->avctx = avctx; ff_exrdsp_init(&s->dsp); @@ -2248,18 +2216,18 @@ static av_cold int decode_init(AVCodecContext *avctx) 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); + t.i = half2float(i, s->mantissatable, s->exponenttable, s->offsettable); t.f = trc_func(t.f); s->gamma_table[i] = t; } } else { if (one_gamma > 0.9999f && one_gamma < 1.0001f) { for (i = 0; i < 65536; ++i) { - s->gamma_table[i] = exr_half2float(i); + s->gamma_table[i].i = half2float(i, s->mantissatable, s->exponenttable, s->offsettable); } } else { for (i = 0; i < 65536; ++i) { - t = exr_half2float(i); + t.i = half2float(i, s->mantissatable, s->exponenttable, s->offsettable); /* If negative value we reuse half value */ if (t.f <= 0.0f) { s->gamma_table[i] = t; diff --git a/libavcodec/half2float.h b/libavcodec/half2float.h new file mode 100644 index 0000000000..9cff12a309 --- /dev/null +++ b/libavcodec/half2float.h @@ -0,0 +1,74 @@ +/* + * This file is part of FFmpeg. + * + * 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. + * + * 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AVCODEC_HALF2FLOAT_H +#define AVCODEC_HALF2FLOAT_H + +#include + +static uint32_t convertmantissa(uint32_t i) +{ + uint32_t m = i << 13; // Zero pad mantissa bits + uint32_t e = 0; // Zero exponent + + while (!(m & 0x00800000UL)){ // While not normalized + e -= 0x00800000UL; // Decrement exponent (1<<23) + m <<= 1; // Shift mantissa + } + + m &= ~0x00800000UL; // Clear leading 1 bit + e += 0x38800000UL; // Adjust bias ((127-14)<<23) + + return m | e; // Return combined number +} + +static void half2float_table(uint32_t *mantissatable, uint32_t *exponenttable, + uint16_t *offsettable) +{ + mantissatable[0] = 0; + for (int i = 1; i < 1024; i++) + mantissatable[i] = convertmantissa(i); + for (int i = 1024; i < 2048; i++) + mantissatable[i] = 0x38000000UL + ((i - 1024) << 13UL); + + exponenttable[0] = 0; + for (int i = 1; i < 31; i++) + exponenttable[i] = i << 23; + for (int i = 33; i < 63; i++) + exponenttable[i] = 0x80000000UL + ((i - 32) << 23UL); + exponenttable[31]= 0x47800000UL; + exponenttable[32]= 0x80000000UL; + exponenttable[63]= 0xC7800000UL; + + offsettable[0] = 0; + for (int i = 1; i < 64; i++) + offsettable[i] = 1024; + offsettable[32] = 0; +} + +static uint32_t half2float(uint16_t h, uint32_t *mantissatable, uint32_t *exponenttable, + uint16_t *offsettable) +{ + uint32_t f; + + f = mantissatable[offsettable[h >> 10] + (h & 0x3ff)] + exponenttable[h >> 10]; + + return f; +} + +#endif /* AVCODEC_HALF2FLOAT_H */