From b42925264a910e6807e9e7134feaa44ae47bf911 Mon Sep 17 00:00:00 2001 From: Timo Rothenpieler Date: Wed, 10 Aug 2022 00:42:41 +0200 Subject: avutil: move half-precision float helper to avutil --- libavcodec/exr.c | 2 +- libavcodec/exrenc.c | 2 +- libavcodec/float2half.h | 67 -------------------------------------------- libavcodec/half2float.h | 74 ------------------------------------------------- libavcodec/pnmdec.c | 3 +- libavcodec/pnmenc.c | 2 +- libavutil/float2half.h | 67 ++++++++++++++++++++++++++++++++++++++++++++ libavutil/half2float.h | 74 +++++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 146 insertions(+), 145 deletions(-) delete mode 100644 libavcodec/float2half.h delete mode 100644 libavcodec/half2float.h create mode 100644 libavutil/float2half.h create mode 100644 libavutil/half2float.h diff --git a/libavcodec/exr.c b/libavcodec/exr.c index 3a6b9c3014..5c6ca9adbf 100644 --- a/libavcodec/exr.c +++ b/libavcodec/exr.c @@ -41,6 +41,7 @@ #include "libavutil/avstring.h" #include "libavutil/opt.h" #include "libavutil/color_utils.h" +#include "libavutil/half2float.h" #include "avcodec.h" #include "bytestream.h" @@ -53,7 +54,6 @@ #include "exrdsp.h" #include "get_bits.h" #include "internal.h" -#include "half2float.h" #include "mathops.h" #include "thread.h" diff --git a/libavcodec/exrenc.c b/libavcodec/exrenc.c index 8cf7827bb6..56c084d483 100644 --- a/libavcodec/exrenc.c +++ b/libavcodec/exrenc.c @@ -31,11 +31,11 @@ #include "libavutil/intreadwrite.h" #include "libavutil/imgutils.h" #include "libavutil/pixdesc.h" +#include "libavutil/float2half.h" #include "avcodec.h" #include "bytestream.h" #include "codec_internal.h" #include "encode.h" -#include "float2half.h" enum ExrCompr { EXR_RAW, diff --git a/libavcodec/float2half.h b/libavcodec/float2half.h deleted file mode 100644 index e05125088c..0000000000 --- a/libavcodec/float2half.h +++ /dev/null @@ -1,67 +0,0 @@ -/* - * 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 */ diff --git a/libavcodec/half2float.h b/libavcodec/half2float.h deleted file mode 100644 index 7df6747e50..0000000000 --- a/libavcodec/half2float.h +++ /dev/null @@ -1,74 +0,0 @@ -/* - * 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) -{ - int32_t m = i << 13; // Zero pad mantissa bits - int32_t e = 0; // Zero exponent - - while (!(m & 0x00800000)) { // While not normalized - e -= 0x00800000; // Decrement exponent (1<<23) - m <<= 1; // Shift mantissa - } - - m &= ~0x00800000; // Clear leading 1 bit - e += 0x38800000; // 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, const uint32_t *mantissatable, const uint32_t *exponenttable, - const uint16_t *offsettable) -{ - uint32_t f; - - f = mantissatable[offsettable[h >> 10] + (h & 0x3ff)] + exponenttable[h >> 10]; - - return f; -} - -#endif /* AVCODEC_HALF2FLOAT_H */ diff --git a/libavcodec/pnmdec.c b/libavcodec/pnmdec.c index 130407df25..9383dc8e60 100644 --- a/libavcodec/pnmdec.c +++ b/libavcodec/pnmdec.c @@ -21,12 +21,13 @@ #include "config_components.h" +#include "libavutil/half2float.h" + #include "avcodec.h" #include "codec_internal.h" #include "internal.h" #include "put_bits.h" #include "pnm.h" -#include "half2float.h" static void samplecpy(uint8_t *dst, const uint8_t *src, int n, int maxval) { diff --git a/libavcodec/pnmenc.c b/libavcodec/pnmenc.c index b16c93c88f..7ce534d06e 100644 --- a/libavcodec/pnmenc.c +++ b/libavcodec/pnmenc.c @@ -24,10 +24,10 @@ #include "libavutil/intreadwrite.h" #include "libavutil/imgutils.h" #include "libavutil/pixdesc.h" +#include "libavutil/float2half.h" #include "avcodec.h" #include "codec_internal.h" #include "encode.h" -#include "float2half.h" typedef struct PHMEncContext { uint16_t basetable[512]; diff --git a/libavutil/float2half.h b/libavutil/float2half.h new file mode 100644 index 0000000000..d6aaab8278 --- /dev/null +++ b/libavutil/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 AVUTIL_FLOAT2HALF_H +#define AVUTIL_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 /* AVUTIL_FLOAT2HALF_H */ diff --git a/libavutil/half2float.h b/libavutil/half2float.h new file mode 100644 index 0000000000..1f6deade07 --- /dev/null +++ b/libavutil/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 AVUTIL_HALF2FLOAT_H +#define AVUTIL_HALF2FLOAT_H + +#include + +static uint32_t convertmantissa(uint32_t i) +{ + int32_t m = i << 13; // Zero pad mantissa bits + int32_t e = 0; // Zero exponent + + while (!(m & 0x00800000)) { // While not normalized + e -= 0x00800000; // Decrement exponent (1<<23) + m <<= 1; // Shift mantissa + } + + m &= ~0x00800000; // Clear leading 1 bit + e += 0x38800000; // 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, const uint32_t *mantissatable, const uint32_t *exponenttable, + const uint16_t *offsettable) +{ + uint32_t f; + + f = mantissatable[offsettable[h >> 10] + (h & 0x3ff)] + exponenttable[h >> 10]; + + return f; +} + +#endif /* AVUTIL_HALF2FLOAT_H */ -- cgit v1.2.3