From c6a707042840e96ecfb3371c361fdd49262cb69b Mon Sep 17 00:00:00 2001 From: KatieFrogs <23621460+KatieFrogs@users.noreply.github.com> Date: Fri, 22 Oct 2021 10:09:33 +0300 Subject: [PATCH] Cmake: Build celt for wasm target --- CMakeLists.txt | 2 +- cmake/dependencies/celt.cmake | 49 +++++++-- cmake/dependencies/g719.cmake | 2 +- cmake/dependencies/maitrac3plus.cmake | 2 +- cmake/dependencies/ogg.cmake | 25 +++++ cmake/dependencies/vorbis.cmake | 17 +--- ext_libs/celt-0110/ecintrin.h | 140 ++++++++++++++++++++++++++ 7 files changed, 213 insertions(+), 24 deletions(-) create mode 100644 cmake/dependencies/ogg.cmake create mode 100644 ext_libs/celt-0110/ecintrin.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 1b68a013..9f942078 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -100,7 +100,6 @@ endif() if(EMSCRIPTEN) set(USE_MPEG OFF) - set(USE_CELT OFF) set(USE_JANSSON OFF) set(BUILD_V123 OFF) set(BUILD_AUDACIOUS OFF) @@ -144,6 +143,7 @@ include(FetchDependency) include(dependencies/ffmpeg) include(dependencies/mpg123) +include(dependencies/ogg) include(dependencies/vorbis) include(dependencies/libao) include(dependencies/maitrac3plus) diff --git a/cmake/dependencies/celt.cmake b/cmake/dependencies/celt.cmake index 628f1521..274cbef2 100644 --- a/cmake/dependencies/celt.cmake +++ b/cmake/dependencies/celt.cmake @@ -9,11 +9,16 @@ if(NOT WIN32 AND USE_CELT) GIT_REPOSITORY https://gitlab.xiph.org/xiph/celt GIT_TAG 0b405d1170122c859faab435405666506d52fa2e ) - if(CELT_0061_PATH AND CELT_0110_PATH) + if(CELT_0061_PATH AND CELT_0110_PATH AND (OGG_PATH OR OGG_FOUND)) set(CELT_0061_LINK_PATH ${CELT_0061_BIN}/libcelt/.libs/libcelt.a) set(CELT_0110_LINK_PATH ${CELT_0110_BIN}/libcelt/.libs/libcelt0.a) - set(CELT_CONF + configure_file( + ${VGM_SOURCE_DIR}/ext_libs/celt-0110/ecintrin.h + ${CELT_0110_PATH}/libcelt/ecintrin.h + COPYONLY) + + set(CELT_CFLAGS alg_quant alg_unquant celt_decode @@ -76,20 +81,48 @@ if(NOT WIN32 AND USE_CELT) log2_frac icwrs get_required_bits + ec_ilog + mdct_forward + mdct_backward + mdct_init + mdct_clear ) foreach(ver 0061 0110) - foreach(source ${CELT_CONF}) + foreach(source ${CELT_CFLAGS}) string(REGEX REPLACE "^([^_]+)" "\\1_${ver}" target ${source}) if(source STREQUAL ${target}) set(target "${source}_${ver}") endif() - list(APPEND CELT_${ver}_CONF "-D${source}=${target}") + list(APPEND CELT_${ver}_CFLAGS "-D${source}=${target}") endforeach() if(ver STREQUAL "0110") - list(APPEND CELT_${ver}_CONF "-DCUSTOM_MODES=1") + list(APPEND CELT_${ver}_CFLAGS "-DCUSTOM_MODES=1") + endif() + list(APPEND CELT_${ver}_CFLAGS "-fPIC") + + set(CELT_${ver}_CONF + --enable-static + --disable-shared + CC="${CMAKE_C_COMPILER}" + AR="${CMAKE_AR}" + CFLAGS="${CELT_${ver}_CFLAGS}" + ) + set(CELT_${ver}_CONFIGURE_DEPENDS + ${CELT_${ver}_PATH}/configure + ) + if(OGG_PATH) + foreach(ogg_include ${OGG_INCLUDE_DIR}) + list(APPEND OGG_INCLUDES -I${ogg_include}) + endforeach() + list(APPEND CELT_${ver}_CONF + LDFLAGS="-L${OGG_BIN}" + CPPFLAGS="${OGG_INCLUDES}" + ) + list(APPEND CELT_${ver}_CONFIGURE_DEPENDS + ${OGG_BIN}/libogg.a + ) endif() - list(APPEND CELT_${ver}_CONF "-fPIC") if(NOT EXISTS ${CELT_${ver}_PATH}/configure) add_custom_target(CELT_${ver}_AUTOGEN @@ -101,8 +134,8 @@ if(NOT WIN32 AND USE_CELT) file(MAKE_DIRECTORY ${CELT_${ver}_BIN}) add_custom_target(CELT_${ver}_CONFIGURE - COMMAND "${CELT_${ver}_PATH}/configure" --enable-static --disable-shared CC="${CMAKE_C_COMPILER}" AR="${CMAKE_AR}" CFLAGS="${CELT_${ver}_CONF}" - DEPENDS ${CELT_${ver}_PATH}/configure + COMMAND "${CELT_${ver}_PATH}/configure" ${CELT_${ver}_CONF} + DEPENDS ${CELT_${ver}_CONFIGURE_DEPENDS} BYPRODUCTS ${CELT_${ver}_BIN}/Makefile WORKING_DIRECTORY ${CELT_${ver}_BIN} ) diff --git a/cmake/dependencies/g719.cmake b/cmake/dependencies/g719.cmake index c24a6151..6df599d5 100644 --- a/cmake/dependencies/g719.cmake +++ b/cmake/dependencies/g719.cmake @@ -10,7 +10,7 @@ if(NOT WIN32 AND USE_G719) ${VGM_SOURCE_DIR}/ext_libs/libg719_decode/CMakeLists.txt ${G719_PATH}/CMakeLists.txt COPYONLY) - add_subdirectory(${G719_PATH} ${G719_BIN}) + add_subdirectory(${G719_PATH} ${G719_BIN} EXCLUDE_FROM_ALL) endif() endif() if(NOT USE_G719) diff --git a/cmake/dependencies/maitrac3plus.cmake b/cmake/dependencies/maitrac3plus.cmake index 42eb6fce..a5aeb046 100644 --- a/cmake/dependencies/maitrac3plus.cmake +++ b/cmake/dependencies/maitrac3plus.cmake @@ -16,7 +16,7 @@ if(USE_MAIATRAC3PLUS) ${MAIATRAC3PLUS_PATH}/MaiAT3PlusDecoder/src/base/Mai_mem.cc ${MAIATRAC3PLUS_PATH}/MaiAT3PlusDecoder/src/base/Mai_Mem.cc COPYONLY) - add_subdirectory(${MAIATRAC3PLUS_PATH}/MaiAT3PlusDecoder ${MAIATRAC3PLUS_BIN}) + add_subdirectory(${MAIATRAC3PLUS_PATH}/MaiAT3PlusDecoder ${MAIATRAC3PLUS_BIN} EXCLUDE_FROM_ALL) else() message(FATAL_ERROR "Path to MAIATRAC3+ must be set. (Use MAIATRAC3PLUS_PATH)") endif() diff --git a/cmake/dependencies/ogg.cmake b/cmake/dependencies/ogg.cmake new file mode 100644 index 00000000..4b55996f --- /dev/null +++ b/cmake/dependencies/ogg.cmake @@ -0,0 +1,25 @@ +if(NOT WIN32 AND (USE_VORBIS OR USE_CELT)) + if(NOT OGG_PATH) + find_package(Ogg QUIET) + + if(OGG_FOUND) + set(OGG_SOURCE "(system)") + endif() + endif() + if(OGG_PATH OR NOT OGG_FOUND) + FetchDependency(OGG + DIR ogg + GIT_REPOSITORY https://gitlab.xiph.org/xiph/ogg + GIT_TAG v1.3.5 + ) + + if(OGG_PATH) + add_subdirectory(${OGG_PATH} ${OGG_BIN} EXCLUDE_FROM_ALL) + set(OGG_INCLUDE_DIR ${OGG_PATH}/include ${OGG_BIN}/include) + set(OGG_LIBRARY ogg) + endif() + endif() +endif() +if(NOT OGG_PATH) + unset(OGG_SOURCE) +endif() diff --git a/cmake/dependencies/vorbis.cmake b/cmake/dependencies/vorbis.cmake index 85f4b77b..23857a14 100644 --- a/cmake/dependencies/vorbis.cmake +++ b/cmake/dependencies/vorbis.cmake @@ -1,29 +1,20 @@ if(NOT WIN32 AND USE_VORBIS) - if(NOT VORBIS_PATH OR NOT OGG_PATH) + if(NOT VORBIS_PATH) find_package(VorbisFile QUIET) if(VORBISFILE_FOUND) set(VORBIS_SOURCE "(system)") endif() endif() - if(VORBIS_PATH AND OGG_PATH OR NOT VORBISFILE_FOUND) - FetchDependency(OGG - DIR ogg - GIT_REPOSITORY https://gitlab.xiph.org/xiph/ogg - GIT_TAG v1.3.5 - ) + if(VORBIS_PATH AND (OGG_PATH OR OGG_FOUND) OR NOT VORBISFILE_FOUND) FetchDependency(VORBIS DIR vorbis GIT_REPOSITORY https://gitlab.xiph.org/xiph/vorbis GIT_TAG v1.3.7 ) - if(OGG_PATH AND VORBIS_PATH) - add_subdirectory(${OGG_PATH} ${OGG_BIN}) - set(OGG_INCLUDE_DIR ${OGG_PATH}/include ${OGG_BIN}/include) - set(OGG_LIBRARY ogg) - - add_subdirectory(${VORBIS_PATH} ${VORBIS_BIN}) + if(VORBIS_PATH) + add_subdirectory(${VORBIS_PATH} ${VORBIS_BIN} EXCLUDE_FROM_ALL) set(OGG_VORBIS_INCLUDE_DIR ${VORBIS_PATH}/include) set(OGG_VORBIS_LIBRARY vorbis) diff --git a/ext_libs/celt-0110/ecintrin.h b/ext_libs/celt-0110/ecintrin.h new file mode 100644 index 00000000..0348e6d7 --- /dev/null +++ b/ext_libs/celt-0110/ecintrin.h @@ -0,0 +1,140 @@ +/* Copyright (c) 2003-2008 Timothy B. Terriberry + Copyright (c) 2008 Xiph.Org Foundation */ +/* + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + - Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + - Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + - Neither the name of the Xiph.org Foundation nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +/*Some common macros for potential platform-specific optimization.*/ +#include +#include +#if !defined(_ecintrin_H) +# define _ecintrin_H (1) + +/*Some specific platforms may have optimized intrinsic or inline assembly + versions of these functions which can substantially improve performance. + We define macros for them to allow easy incorporation of these non-ANSI + features.*/ + +/*Note that we do not provide a macro for abs(), because it is provided as a + library function, which we assume is translated into an intrinsic to avoid + the function call overhead and then implemented in the smartest way for the + target platform. + With modern gcc (4.x), this is true: it uses cmov instructions if the + architecture supports it and branchless bit-twiddling if it does not (the + speed difference between the two approaches is not measurable). + Interestingly, the bit-twiddling method was patented in 2000 (US 6,073,150) + by Sun Microsystems, despite prior art dating back to at least 1996: + http://web.archive.org/web/19961201174141/www.x86.org/ftp/articles/pentopt/PENTOPT.TXT + On gcc 3.x, however, our assumption is not true, as abs() is translated to a + conditional jump, which is horrible on deeply piplined architectures (e.g., + all consumer architectures for the past decade or more) when the sign cannot + be reliably predicted.*/ + +/*Modern gcc (4.x) can compile the naive versions of min and max with cmov if + given an appropriate architecture, but the branchless bit-twiddling versions + are just as fast, and do not require any special target architecture. + Earlier gcc versions (3.x) compiled both code to the same assembly + instructions, because of the way they represented ((_b)>(_a)) internally.*/ +#define EC_MAXI(_a,_b) ((_a)-((_a)-(_b)&-((_b)>(_a)))) +#define EC_MINI(_a,_b) ((_a)+((_b)-(_a)&-((_b)<(_a)))) +/*This has a chance of compiling branchless, and is just as fast as the + bit-twiddling method, which is slightly less portable, since it relies on a + sign-extended rightshift, which is not guaranteed by ANSI (but present on + every relevant platform).*/ +#define EC_SIGNI(_a) (((_a)>0)-((_a)<0)) +/*Slightly more portable than relying on a sign-extended right-shift (which is + not guaranteed by ANSI), and just as fast, since gcc (3.x and 4.x both) + compile it into the right-shift anyway.*/ +#define EC_SIGNMASK(_a) (-((_a)<0)) +/*Clamps an integer into the given range. + If _a>_c, then the lower bound _a is respected over the upper bound _c (this + behavior is required to meet our documented API behavior). + _a: The lower bound. + _b: The value to clamp. + _c: The upper boud.*/ +#define EC_CLAMPI(_a,_b,_c) (EC_MAXI(_a,EC_MINI(_b,_c))) + + +/*Count leading zeros. + This macro should only be used for implementing ec_ilog(), if it is defined. + All other code should use EC_ILOG() instead.*/ +#if defined(_MSC_VER) +# include +static __inline int ec_bsr(unsigned long _x){ + unsigned long ret; + _BitScanReverse(&ret,_x); + return (int)ret; +} +# define EC_CLZ0 (1) +# define EC_CLZ(_x) (-ec_bsr(_x)) +#elif defined(ENABLE_TI_DSPLIB) +# include "dsplib.h" +# define EC_CLZ0 (31) +# define EC_CLZ(_x) (_lnorm(x)) +#elif defined(__GNUC_PREREQ) +# if __GNUC_PREREQ(3,4) +# if INT_MAX>=2147483647 +# define EC_CLZ0 ((int)sizeof(unsigned)*CHAR_BIT) +# define EC_CLZ(_x) (__builtin_clz(_x)) +# elif LONG_MAX>=2147483647L +# define EC_CLZ0 ((int)sizeof(unsigned long)*CHAR_BIT) +# define EC_CLZ(_x) (__builtin_clzl(_x)) +# endif +# endif +#endif + +#if defined(EC_CLZ) +/*Note that __builtin_clz is not defined when _x==0, according to the gcc + documentation (and that of the BSR instruction that implements it on x86). + The majority of the time we can never pass it zero. + When we need to, it can be special cased.*/ +# define EC_ILOG(_x) (EC_CLZ0-EC_CLZ(_x)) +#else +static int ec_ilog2(celt_uint32 _v){ + int ret; + int m; + ret=!!_v; + m=!!(_v&0xFFFF0000)<<4; + _v>>=m; + ret|=m; + m=!!(_v&0xFF00)<<3; + _v>>=m; + ret|=m; + m=!!(_v&0xF0)<<2; + _v>>=m; + ret|=m; + m=!!(_v&0xC)<<1; + _v>>=m; + ret|=m; + ret+=!!(_v&0x2); + return ret; +} +# define EC_ILOG(_x) (ec_ilog2(_x)) +#endif + +#endif