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 */