[FFmpeg-devel] [PATCH 2/3] avcodec/exrenc: move float2half code to own header

Paul B Mahol onemda at gmail.com
Sun Feb 28 14:53:26 EET 2021


Signed-off-by: Paul B Mahol <onemda at gmail.com>
---
 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 <stdint.h>
+
+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 */
-- 
2.17.1



More information about the ffmpeg-devel mailing list