Path: blob/master/thirdparty/basis_universal/transcoder/basisu.h
9905 views
// basisu.h1// Copyright (C) 2019-2024 Binomial LLC. All Rights Reserved.2// Important: If compiling with gcc, be sure strict aliasing is disabled: -fno-strict-aliasing3//4// Licensed under the Apache License, Version 2.0 (the "License");5// you may not use this file except in compliance with the License.6// You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing, software11// distributed under the License is distributed on an "AS IS" BASIS,12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13// See the License for the specific language governing permissions and14// limitations under the License.15#pragma once1617#ifdef _MSC_VER1819#pragma warning (disable : 4201)20#pragma warning (disable : 4127) // warning C4127: conditional expression is constant21#pragma warning (disable : 4530) // C++ exception handler used, but unwind semantics are not enabled.2223#endif // _MSC_VER2425#include <stdlib.h>26#include <stdio.h>27#include <math.h>28#include <stdarg.h>29#include <string.h>30#include <memory.h>31#include <limits.h>32#include <stdint.h>3334#include <algorithm>35#include <limits>36#include <functional>37#include <iterator>38#include <type_traits>39#include <assert.h>40#include <random>41#include <inttypes.h>4243#include "basisu_containers.h"4445#ifdef max46#undef max47#endif4849#ifdef min50#undef min51#endif5253#ifdef _WIN3254#define strcasecmp _stricmp55#endif5657// Set to one to enable debug printf()'s when any errors occur, for development/debugging. Especially useful for WebGL development.58#ifndef BASISU_FORCE_DEVEL_MESSAGES59#define BASISU_FORCE_DEVEL_MESSAGES 060#endif6162#define BASISU_NOTE_UNUSED(x) (void)(x)63#define BASISU_ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))64#define BASISU_NO_EQUALS_OR_COPY_CONSTRUCT(x) x(const x &) = delete; x& operator= (const x &) = delete;65#define BASISU_ASSUME(x) static_assert(x, #x);66#define BASISU_OFFSETOF(s, m) offsetof(s, m)67#define BASISU_STRINGIZE(x) #x68#define BASISU_STRINGIZE2(x) BASISU_STRINGIZE(x)6970#if BASISU_FORCE_DEVEL_MESSAGES71#define BASISU_DEVEL_ERROR(...) do { basisu::debug_printf(__VA_ARGS__); } while(0)72#else73#define BASISU_DEVEL_ERROR(...)74#endif7576namespace basisu77{78// Types/utilities7980#ifdef _WIN3281const char BASISU_PATH_SEPERATOR_CHAR = '\\';82#else83const char BASISU_PATH_SEPERATOR_CHAR = '/';84#endif8586typedef basisu::vector<uint8_t> uint8_vec;87typedef basisu::vector<int16_t> int16_vec;88typedef basisu::vector<uint16_t> uint16_vec;89typedef basisu::vector<uint32_t> uint_vec;90typedef basisu::vector<size_t> size_t_vec;91typedef basisu::vector<uint64_t> uint64_vec;92typedef basisu::vector<int> int_vec;93typedef basisu::vector<bool> bool_vec;94typedef basisu::vector<float> float_vec;9596void enable_debug_printf(bool enabled);97void debug_printf(const char *pFmt, ...);98void debug_puts(const char* p);99100template <typename... Args>101inline void fmt_debug_printf(const char* pFmt, Args&&... args)102{103std::string res;104if (!fmt_variants(res, pFmt, fmt_variant_vec{ fmt_variant(std::forward<Args>(args))... }))105return;106debug_puts(res.c_str());107}108109#ifndef __EMSCRIPTEN__110#ifdef __GNUC__111#pragma GCC diagnostic push112#pragma GCC diagnostic ignored "-Wclass-memaccess"113#endif114#endif115116template <typename T> inline void clear_obj(T& obj) { memset(&obj, 0, sizeof(obj)); }117118#ifndef __EMSCRIPTEN__119#ifdef __GNUC__120#pragma GCC diagnostic pop121#endif122#endif123124constexpr double cPiD = 3.14159265358979323846264338327950288;125constexpr float REALLY_SMALL_FLOAT_VAL = .000000125f;126constexpr float SMALL_FLOAT_VAL = .0000125f;127constexpr float BIG_FLOAT_VAL = 1e+30f;128129template <typename T0, typename T1> inline T0 lerp(T0 a, T0 b, T1 c) { return a + (b - a) * c; }130131inline float clampf(float value, float low, float high) { if (value < low) value = low; else if (value > high) value = high; return value; }132inline float saturate(float value) { return clampf(value, 0, 1.0f); }133inline uint8_t minimumub(uint8_t a, uint8_t b) { return (a < b) ? a : b; }134inline uint32_t minimumu(uint32_t a, uint32_t b) { return (a < b) ? a : b; }135inline int32_t minimumi(int32_t a, int32_t b) { return (a < b) ? a : b; }136inline float minimumf(float a, float b) { return (a < b) ? a : b; }137inline uint8_t maximumub(uint8_t a, uint8_t b) { return (a > b) ? a : b; }138inline uint32_t maximumu(uint32_t a, uint32_t b) { return (a > b) ? a : b; }139inline int32_t maximumi(int32_t a, int32_t b) { return (a > b) ? a : b; }140inline float maximumf(float a, float b) { return (a > b) ? a : b; }141inline int squarei(int i) { return i * i; }142inline float squaref(float i) { return i * i; }143inline double squared(double i) { return i * i; }144template<typename T> inline T square(T a) { return a * a; }145template<typename T> inline T sign(T a) { return (a < 0) ? (T)-1 : ((a == 0) ? (T)0 : (T)1); }146147inline bool equal_tol(float a, float b, float t) { return fabsf(a - b) <= ((maximum(fabsf(a), fabsf(b)) + 1.0f) * t); }148inline bool equal_tol(double a, double b, double t) { return fabs(a - b) <= ((maximum(fabs(a), fabs(b)) + 1.0f) * t); }149150template <class T>151inline T prev_wrap(T i, T n)152{153T temp = i - 1;154if (temp < 0)155temp = n - 1;156return temp;157}158159template <class T>160inline T next_wrap(T i, T n)161{162T temp = i + 1;163if (temp >= n)164temp = 0;165return temp;166}167168inline uint32_t iabs(int32_t i) { return (i < 0) ? static_cast<uint32_t>(-i) : static_cast<uint32_t>(i); }169inline uint64_t iabs64(int64_t i) { return (i < 0) ? static_cast<uint64_t>(-i) : static_cast<uint64_t>(i); }170171template<typename T> inline void clear_vector(T &vec) { vec.erase(vec.begin(), vec.end()); }172template<typename T> inline typename T::value_type *enlarge_vector(T &vec, size_t n) { size_t cs = vec.size(); vec.resize(cs + n); return &vec[cs]; }173174inline bool is_pow2(uint32_t x) { return x && ((x & (x - 1U)) == 0U); }175inline bool is_pow2(uint64_t x) { return x && ((x & (x - 1U)) == 0U); }176177template<typename T> inline T open_range_check(T v, T minv, T maxv) { assert(v >= minv && v < maxv); BASISU_NOTE_UNUSED(minv); BASISU_NOTE_UNUSED(maxv); return v; }178template<typename T> inline T open_range_check(T v, T maxv) { assert(v < maxv); BASISU_NOTE_UNUSED(maxv); return v; }179180// Open interval181inline bool in_bounds(int v, int l, int h)182{183return (v >= l) && (v < h);184}185186// Closed interval187inline bool in_range(int v, int l, int h)188{189return (v >= l) && (v <= h);190}191192inline uint32_t total_bits(uint32_t v) { uint32_t l = 0; for ( ; v > 0U; ++l) v >>= 1; return l; }193194template<typename T> inline T saturate(T val) { return clamp(val, 0.0f, 1.0f); }195196inline uint32_t get_bit(uint32_t src, int ndx)197{198assert(in_bounds(ndx, 0, 32));199return (src >> ndx) & 1;200}201202inline bool is_bit_set(uint32_t src, int ndx)203{204return get_bit(src, ndx) != 0;205}206207inline uint32_t get_bits(uint32_t val, int low, int high)208{209const int num_bits = (high - low) + 1;210assert(in_range(num_bits, 1, 32));211212val >>= low;213if (num_bits != 32)214val &= ((1u << num_bits) - 1);215216return val;217}218219template<typename T, typename R> inline void append_vector(T &vec, const R *pObjs, size_t n)220{221if (n)222{223if (vec.size())224{225assert((pObjs + n) <= vec.begin() || (pObjs >= vec.end()));226}227const size_t cur_s = vec.size();228vec.resize(cur_s + n);229memcpy(&vec[cur_s], pObjs, sizeof(R) * n);230}231}232233template<typename T> inline void append_vector(T &vec, const T &other_vec)234{235assert(&vec != &other_vec);236if (other_vec.size())237append_vector(vec, &other_vec[0], other_vec.size());238}239240template<typename T> inline void vector_ensure_element_is_valid(T &vec, size_t idx)241{242if (idx >= vec.size())243vec.resize(idx + 1);244}245246template<typename T> inline void vector_sort(T &vec)247{248if (vec.size())249std::sort(vec.begin(), vec.end());250}251252template<typename T, typename U> inline bool unordered_set_contains(T& set, const U&obj)253{254return set.find(obj) != set.end();255}256257template<typename T> int vector_find(const T &vec, const typename T::value_type &obj)258{259assert(vec.size() <= INT_MAX);260for (size_t i = 0; i < vec.size(); i++)261if (vec[i] == obj)262return static_cast<int>(i);263return -1;264}265266template<typename T> void vector_set_all(T &vec, const typename T::value_type &obj)267{268for (size_t i = 0; i < vec.size(); i++)269vec[i] = obj;270}271272inline uint64_t read_be64(const void *p)273{274uint64_t val = 0;275for (uint32_t i = 0; i < 8; i++)276val |= (static_cast<uint64_t>(static_cast<const uint8_t *>(p)[7 - i]) << (i * 8));277return val;278}279280inline void write_be64(void *p, uint64_t x)281{282for (uint32_t i = 0; i < 8; i++)283static_cast<uint8_t *>(p)[7 - i] = static_cast<uint8_t>(x >> (i * 8));284}285286static inline uint16_t byteswap16(uint16_t x) { return static_cast<uint16_t>((x << 8) | (x >> 8)); }287static inline uint32_t byteswap32(uint32_t x) { return ((x << 24) | ((x << 8) & 0x00FF0000) | ((x >> 8) & 0x0000FF00) | (x >> 24)); }288289inline uint32_t floor_log2i(uint32_t v)290{291uint32_t b = 0;292for (; v > 1U; ++b)293v >>= 1;294return b;295}296297inline uint32_t ceil_log2i(uint32_t v)298{299uint32_t b = floor_log2i(v);300if ((b != 32) && (v > (1U << b)))301++b;302return b;303}304305inline int posmod(int x, int y)306{307if (x >= 0)308return (x < y) ? x : (x % y);309int m = (-x) % y;310return (m != 0) ? (y - m) : m;311}312313inline bool do_excl_ranges_overlap(int la, int ha, int lb, int hb)314{315assert(la < ha && lb < hb);316if ((ha <= lb) || (la >= hb)) return false;317return true;318}319320static inline uint32_t read_le_word(const uint8_t* pBytes)321{322return (pBytes[1] << 8U) | (pBytes[0]);323}324325static inline uint32_t read_le_dword(const uint8_t *pBytes)326{327return (pBytes[3] << 24U) | (pBytes[2] << 16U) | (pBytes[1] << 8U) | (pBytes[0]);328}329330static inline void write_le_dword(uint8_t* pBytes, uint32_t val)331{332pBytes[0] = (uint8_t)val;333pBytes[1] = (uint8_t)(val >> 8U);334pBytes[2] = (uint8_t)(val >> 16U);335pBytes[3] = (uint8_t)(val >> 24U);336}337338// Always little endian 1-8 byte unsigned int339template<uint32_t NumBytes>340struct packed_uint341{342uint8_t m_bytes[NumBytes];343344inline packed_uint() { static_assert(NumBytes <= sizeof(uint64_t), "Invalid NumBytes"); }345inline packed_uint(uint64_t v) { *this = v; }346inline packed_uint(const packed_uint& other) { *this = other; }347348inline packed_uint& operator= (uint64_t v)349{350for (uint32_t i = 0; i < NumBytes; i++)351m_bytes[i] = static_cast<uint8_t>(v >> (i * 8));352return *this;353}354355inline packed_uint& operator= (const packed_uint& rhs)356{357memcpy(m_bytes, rhs.m_bytes, sizeof(m_bytes));358return *this;359}360361#if 0362#ifdef __GNUC__363#pragma GCC diagnostic push364#pragma GCC diagnostic ignored "-Warray-bounds"365#endif366inline operator uint32_t() const367{368switch (NumBytes)369{370case 1:371{372return m_bytes[0];373}374case 2:375{376return (m_bytes[1] << 8U) | m_bytes[0];377}378case 3:379{380return (m_bytes[2] << 16U) | (m_bytes[1] << 8U) | m_bytes[0];381}382case 4:383{384return read_le_dword(m_bytes);385}386case 5:387{388uint32_t l = read_le_dword(m_bytes);389uint32_t h = m_bytes[4];390return static_cast<uint64_t>(l) | (static_cast<uint64_t>(h) << 32U);391}392case 6:393{394uint32_t l = read_le_dword(m_bytes);395uint32_t h = (m_bytes[5] << 8U) | m_bytes[4];396return static_cast<uint64_t>(l) | (static_cast<uint64_t>(h) << 32U);397}398case 7:399{400uint32_t l = read_le_dword(m_bytes);401uint32_t h = (m_bytes[6] << 16U) | (m_bytes[5] << 8U) | m_bytes[4];402return static_cast<uint64_t>(l) | (static_cast<uint64_t>(h) << 32U);403}404case 8:405{406uint32_t l = read_le_dword(m_bytes);407uint32_t h = read_le_dword(m_bytes + 4);408return static_cast<uint64_t>(l) | (static_cast<uint64_t>(h) << 32U);409}410default:411{412assert(0);413return 0;414}415}416}417#ifdef __GNUC__418#pragma GCC diagnostic pop419#endif420#else421inline operator uint32_t() const422{423if constexpr (NumBytes == 1)424{425return m_bytes[0];426}427else if constexpr (NumBytes == 2)428{429return (m_bytes[1] << 8U) | m_bytes[0];430}431else if constexpr (NumBytes == 3)432{433return (m_bytes[2] << 16U) | (m_bytes[1] << 8U) | m_bytes[0];434}435else if constexpr (NumBytes == 4)436{437return read_le_dword(m_bytes);438}439else if constexpr (NumBytes == 5)440{441uint32_t l = read_le_dword(m_bytes);442uint32_t h = m_bytes[4];443return static_cast<uint64_t>(l) | (static_cast<uint64_t>(h) << 32U);444}445else if constexpr (NumBytes == 6)446{447uint32_t l = read_le_dword(m_bytes);448uint32_t h = (m_bytes[5] << 8U) | m_bytes[4];449return static_cast<uint64_t>(l) | (static_cast<uint64_t>(h) << 32U);450}451else if constexpr (NumBytes == 7)452{453uint32_t l = read_le_dword(m_bytes);454uint32_t h = (m_bytes[6] << 16U) | (m_bytes[5] << 8U) | m_bytes[4];455return static_cast<uint64_t>(l) | (static_cast<uint64_t>(h) << 32U);456}457else if constexpr (NumBytes == 8)458{459uint32_t l = read_le_dword(m_bytes);460uint32_t h = read_le_dword(m_bytes + 4);461return static_cast<uint64_t>(l) | (static_cast<uint64_t>(h) << 32U);462}463else464{465static_assert(NumBytes <= 8, "Invalid NumBytes");466return 0;467}468}469#endif470471};472473enum eZero { cZero };474enum eNoClamp { cNoClamp };475476// Rice/Huffman entropy coding477478// This is basically Deflate-style canonical Huffman, except we allow for a lot more symbols.479enum480{481cHuffmanMaxSupportedCodeSize = 16, cHuffmanMaxSupportedInternalCodeSize = 31,482cHuffmanFastLookupBits = 10,483cHuffmanMaxSymsLog2 = 14, cHuffmanMaxSyms = 1 << cHuffmanMaxSymsLog2,484485// Small zero runs486cHuffmanSmallZeroRunSizeMin = 3, cHuffmanSmallZeroRunSizeMax = 10, cHuffmanSmallZeroRunExtraBits = 3,487488// Big zero run489cHuffmanBigZeroRunSizeMin = 11, cHuffmanBigZeroRunSizeMax = 138, cHuffmanBigZeroRunExtraBits = 7,490491// Small non-zero run492cHuffmanSmallRepeatSizeMin = 3, cHuffmanSmallRepeatSizeMax = 6, cHuffmanSmallRepeatExtraBits = 2,493494// Big non-zero run495cHuffmanBigRepeatSizeMin = 7, cHuffmanBigRepeatSizeMax = 134, cHuffmanBigRepeatExtraBits = 7,496497cHuffmanTotalCodelengthCodes = 21, cHuffmanSmallZeroRunCode = 17, cHuffmanBigZeroRunCode = 18, cHuffmanSmallRepeatCode = 19, cHuffmanBigRepeatCode = 20498};499500static const uint8_t g_huffman_sorted_codelength_codes[] = { cHuffmanSmallZeroRunCode, cHuffmanBigZeroRunCode, cHuffmanSmallRepeatCode, cHuffmanBigRepeatCode, 0, 8, 7, 9, 6, 0xA, 5, 0xB, 4, 0xC, 3, 0xD, 2, 0xE, 1, 0xF, 0x10 };501const uint32_t cHuffmanTotalSortedCodelengthCodes = sizeof(g_huffman_sorted_codelength_codes) / sizeof(g_huffman_sorted_codelength_codes[0]);502503// GPU texture formats and various uncompressed texture formats.504505enum class texture_format506{507cInvalidTextureFormat = -1,508509// Block-based formats510cETC1, // ETC1511cETC1S, // ETC1 (subset: diff colors only, no subblocks)512cETC2_RGB, // ETC2 color block (basisu doesn't support ETC2 planar/T/H modes - just basic ETC1)513cETC2_RGBA, // ETC2 EAC alpha block followed by ETC2 color block514cETC2_ALPHA, // ETC2 EAC alpha block515cBC1, // DXT1516cBC3, // DXT5 (BC4/DXT5A block followed by a BC1/DXT1 block)517cBC4, // DXT5A518cBC5, // 3DC/DXN (two BC4/DXT5A blocks)519cBC6HSigned, // HDR520cBC6HUnsigned, // HDR521cBC7,522cASTC_LDR_4x4, // ASTC 4x4 LDR only523cASTC_HDR_4x4, // ASTC 4x4 HDR only (but may use LDR ASTC blocks internally, although our encoders don't do this)524cASTC_HDR_6x6, // ASTC 6x6 HDR only (but may use LDR ASTC blocks internally, although our encoders don't do this)525cPVRTC1_4_RGB,526cPVRTC1_4_RGBA,527cATC_RGB,528cATC_RGBA_INTERPOLATED_ALPHA,529cFXT1_RGB,530cPVRTC2_4_RGBA,531cETC2_R11_EAC,532cETC2_RG11_EAC,533cUASTC4x4,534cUASTC_HDR_4x4,535cBC1_NV,536cBC1_AMD,537538// Uncompressed/raw pixels539cRGBA32,540cRGB565,541cBGR565,542cRGBA4444,543cABGR4444,544cRGBA_HALF,545cRGB_HALF,546cRGB_9E5547};548549inline bool is_uncompressed_texture_format(texture_format fmt)550{551switch (fmt)552{553case texture_format::cRGBA32:554case texture_format::cRGB565:555case texture_format::cBGR565:556case texture_format::cRGBA4444:557case texture_format::cABGR4444:558case texture_format::cRGBA_HALF:559case texture_format::cRGB_HALF:560case texture_format::cRGB_9E5:561return true;562default:563break;564}565566return false;567}568569inline bool is_block_based_texture_format(texture_format fmt)570{571return !is_uncompressed_texture_format(fmt);572}573574// This is bytes per block for GPU formats, or bytes per texel for uncompressed formats.575inline uint32_t get_bytes_per_block_or_pixel(texture_format fmt)576{577switch (fmt)578{579case texture_format::cETC1:580case texture_format::cETC1S:581case texture_format::cETC2_RGB:582case texture_format::cETC2_ALPHA:583case texture_format::cBC1:584case texture_format::cBC1_NV:585case texture_format::cBC1_AMD:586case texture_format::cBC4:587case texture_format::cPVRTC1_4_RGB:588case texture_format::cPVRTC1_4_RGBA:589case texture_format::cATC_RGB:590case texture_format::cPVRTC2_4_RGBA:591case texture_format::cETC2_R11_EAC:592return 8;593case texture_format::cRGBA32:594case texture_format::cRGB_9E5:595return sizeof(uint32_t);596case texture_format::cRGB_HALF:597return sizeof(uint16_t) * 3;598case texture_format::cRGBA_HALF:599return sizeof(uint16_t) * 4;600case texture_format::cRGB565:601case texture_format::cBGR565:602case texture_format::cRGBA4444:603case texture_format::cABGR4444:604return sizeof(uint16_t);605606default:607break;608}609610// Everything else is 16 bytes/block.611return 16;612}613614// This is qwords per block for GPU formats, or not valid for uncompressed formats.615inline uint32_t get_qwords_per_block(texture_format fmt)616{617assert(is_block_based_texture_format(fmt));618619const uint32_t bytes_per_block = get_bytes_per_block_or_pixel(fmt);620return bytes_per_block >> 3;621}622623inline uint32_t get_block_width(texture_format fmt)624{625assert(is_block_based_texture_format(fmt));626627switch (fmt)628{629case texture_format::cFXT1_RGB:630return 8;631case texture_format::cASTC_HDR_6x6:632return 6;633default:634break;635}636return 4;637}638639inline uint32_t get_block_height(texture_format fmt)640{641assert(is_block_based_texture_format(fmt));642643switch (fmt)644{645case texture_format::cASTC_HDR_6x6:646return 6;647default:648break;649}650return 4;651}652653inline bool is_hdr_texture_format(texture_format fmt)654{655switch (fmt)656{657case texture_format::cASTC_HDR_4x4:658case texture_format::cUASTC_HDR_4x4:659case texture_format::cASTC_HDR_6x6:660case texture_format::cBC6HSigned:661case texture_format::cBC6HUnsigned:662case texture_format::cRGBA_HALF:663case texture_format::cRGB_HALF:664case texture_format::cRGB_9E5:665return true;666default:667break;668}669670return false;671}672673inline bool is_ldr_texture_format(texture_format fmt)674{675return !is_hdr_texture_format(fmt);676}677678} // namespace basisu679680681682