diff options
author | Timo Rothenpieler <timo@rothenpieler.org> | 2022-08-10 00:42:41 +0200 |
---|---|---|
committer | Timo Rothenpieler <timo@rothenpieler.org> | 2022-08-19 22:09:36 +0200 |
commit | b42925264a910e6807e9e7134feaa44ae47bf911 (patch) | |
tree | 20f78737fa23c9bf7b878ade9c794a7fe13c40e6 /libavcodec | |
parent | f921c583353bde2403334b94c1a3ca0785364924 (diff) | |
download | ffmpeg-b42925264a910e6807e9e7134feaa44ae47bf911.tar.gz |
avutil: move half-precision float helper to avutil
Diffstat (limited to 'libavcodec')
-rw-r--r-- | libavcodec/exr.c | 2 | ||||
-rw-r--r-- | libavcodec/exrenc.c | 2 | ||||
-rw-r--r-- | libavcodec/float2half.h | 67 | ||||
-rw-r--r-- | libavcodec/half2float.h | 74 | ||||
-rw-r--r-- | libavcodec/pnmdec.c | 3 | ||||
-rw-r--r-- | libavcodec/pnmenc.c | 2 |
6 files changed, 5 insertions, 145 deletions
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 <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 */ 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 <stdint.h> - -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]; |