From 25f1cc26113d0bcb23d26127429914cf3fd08b41 Mon Sep 17 00:00:00 2001 From: gnattu Date: Fri, 20 Dec 2024 15:13:59 +0800 Subject: [PATCH 1/8] Add NE10 to enable more neon optimization for libopus Unlike on x86, where libopus provides inline assembly, most of the performance optimizations for ARM Neon are implemented in the NE10 library. We need to build it separately for optimal performance on arm64 targets. --- builder/scripts.d/45-libNE10.sh | 38 +++++++++++++++++++++++++++++++++ builder/scripts.d/50-libopus.sh | 17 +++++++++++++++ 2 files changed, 55 insertions(+) create mode 100755 builder/scripts.d/45-libNE10.sh diff --git a/builder/scripts.d/45-libNE10.sh b/builder/scripts.d/45-libNE10.sh new file mode 100755 index 0000000000..4ffdf95507 --- /dev/null +++ b/builder/scripts.d/45-libNE10.sh @@ -0,0 +1,38 @@ +#!/bin/bash + +SCRIPT_REPO="https://github.com/projectNe10/Ne10.git" +SCRIPT_COMMIT="1f059a764d0e1bc2481c0055c0e71538470baa83" + +ffbuild_enabled() { + [[ $TARGET == win* ]] && return -1 + [[ $TARGET == *arm64 ]] && return 0 + return -1 +} + +ffbuild_dockerbuild() { + git-mini-clone "$SCRIPT_REPO" "$SCRIPT_COMMIT" Ne10 + cd Ne10 + + mkdir -p build && cd build + + local myconf=( + -DGNULINUX_PLATFORM=ON # macOS is also "GNU Linux". This target means all unix-like target + ) + + if [[ $TARGET == linux* ]]; then + myconf+=( + -DCMAKE_TOOLCHAIN_FILE="$FFBUILD_CMAKE_TOOLCHAIN" + ) + elif [[ $TARGET == mac* ]]; then + : + else + echo "Unknown target" + return -1 + fi + + cmake .. "${myconf[@]}" + make -j$(nproc) + # NE10 does not have install method, we have to copy files with shell command + cp modules/libNE10.a "$FFBUILD_PREFIX"/lib/libNE10.a + cp -R ../inc "$FFBUILD_PREFIX"/include/libNE10 +} diff --git a/builder/scripts.d/50-libopus.sh b/builder/scripts.d/50-libopus.sh index 0a19f9f437..aff70e81fe 100755 --- a/builder/scripts.d/50-libopus.sh +++ b/builder/scripts.d/50-libopus.sh @@ -31,9 +31,26 @@ ffbuild_dockerbuild() { return -1 fi + if [[ $TARGET == linux* || $TARGET == mac* ]] && [[ $TARGET == *arm64 ]]; then + myconf+=( + --with-NE10-libraries="$FFBUILD_PREFIX"/lib + --with-NE10-includes="$FFBUILD_PREFIX"/include/libNE10 + ) + fi + ./configure "${myconf[@]}" make -j$(nproc) make install + + if [[ $TARGET == *arm64 ]]; then + if [[ $TARGET == mac* ]]; then + gsed -i 's/-lopus/-lopus -lNE10/' "$FFBUILD_PREFIX"/lib/pkgconfig/opus.pc + gsed -i 's/-I${includedir}\/opus/-I${includedir}\/opus -I${includedir}\/libNE10/' "$FFBUILD_PREFIX"/lib/pkgconfig/opus.pc + else + sed -i 's/-lopus/-lopus -lNE10/' "$FFBUILD_PREFIX"/lib/pkgconfig/opus.pc + sed -i 's/-I${includedir}\/opus/-I${includedir}\/opus -I${includedir}\/libNE10/' "$FFBUILD_PREFIX"/lib/pkgconfig/opus.pc + fi + fi } ffbuild_configure() { From 191e8f6cbac3e900eb0607ff8fbc5efb5beb8e5a Mon Sep 17 00:00:00 2001 From: gnattu Date: Fri, 20 Dec 2024 15:26:34 +0800 Subject: [PATCH 2/8] builder: explict set traget arch for libne10 --- builder/scripts.d/45-libNE10.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/builder/scripts.d/45-libNE10.sh b/builder/scripts.d/45-libNE10.sh index 4ffdf95507..6556da7c41 100755 --- a/builder/scripts.d/45-libNE10.sh +++ b/builder/scripts.d/45-libNE10.sh @@ -30,6 +30,7 @@ ffbuild_dockerbuild() { return -1 fi + export NE10_LINUX_TARGET_ARCH=aarch64 cmake .. "${myconf[@]}" make -j$(nproc) # NE10 does not have install method, we have to copy files with shell command From ef6fd8570b8ac92f46294e746f5d31ebd34df1f6 Mon Sep 17 00:00:00 2001 From: gnattu Date: Fri, 20 Dec 2024 21:58:50 +0800 Subject: [PATCH 3/8] builder: set explicit optimization CFLAGS for opus --- builder/scripts.d/50-libopus.sh | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/builder/scripts.d/50-libopus.sh b/builder/scripts.d/50-libopus.sh index aff70e81fe..054570b4f9 100755 --- a/builder/scripts.d/50-libopus.sh +++ b/builder/scripts.d/50-libopus.sh @@ -38,10 +38,16 @@ ffbuild_dockerbuild() { ) fi + # reset CLFAGS because libopus may give up optimization if current CFLAGS contains value + CFLAGS_BACKUP="$CFLAGS" + export CFLAGS="-O3" # For some reason libopus will not add optimization flag, we have to set it ourselves + ./configure "${myconf[@]}" make -j$(nproc) make install + export CFLAGS="$CFLAGS_BACKUP" + if [[ $TARGET == *arm64 ]]; then if [[ $TARGET == mac* ]]; then gsed -i 's/-lopus/-lopus -lNE10/' "$FFBUILD_PREFIX"/lib/pkgconfig/opus.pc From 80bf2798164a8e068736917b157c1f98d74f1816 Mon Sep 17 00:00:00 2001 From: gnattu Date: Fri, 20 Dec 2024 23:04:33 +0800 Subject: [PATCH 4/8] builder: use custom fork of libNE10 --- builder/scripts.d/45-libNE10.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/builder/scripts.d/45-libNE10.sh b/builder/scripts.d/45-libNE10.sh index 6556da7c41..65f610658a 100755 --- a/builder/scripts.d/45-libNE10.sh +++ b/builder/scripts.d/45-libNE10.sh @@ -1,7 +1,7 @@ #!/bin/bash -SCRIPT_REPO="https://github.com/projectNe10/Ne10.git" -SCRIPT_COMMIT="1f059a764d0e1bc2481c0055c0e71538470baa83" +SCRIPT_REPO="https://github.com/gnattu/Ne10.git" +SCRIPT_COMMIT="545f4f18014cdbf9fb5fb1a9f5d24000200dfa8b" ffbuild_enabled() { [[ $TARGET == win* ]] && return -1 From 3756ca70c5193ae020482aec578f141fde5e2ec7 Mon Sep 17 00:00:00 2001 From: gnattu Date: Sat, 21 Dec 2024 00:39:41 +0800 Subject: [PATCH 5/8] builder: use fPIC for Linux libopus --- builder/scripts.d/50-libopus.sh | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/builder/scripts.d/50-libopus.sh b/builder/scripts.d/50-libopus.sh index 054570b4f9..45e559d43c 100755 --- a/builder/scripts.d/50-libopus.sh +++ b/builder/scripts.d/50-libopus.sh @@ -40,7 +40,13 @@ ffbuild_dockerbuild() { # reset CLFAGS because libopus may give up optimization if current CFLAGS contains value CFLAGS_BACKUP="$CFLAGS" - export CFLAGS="-O3" # For some reason libopus will not add optimization flag, we have to set it ourselves + + # For some reason libopus will not add optimization flag, we have to set it ourselves + if [[ $TARGET == mac* ]]; then + export CFLAGS="-O3" + else + export CFLAGS="-O3 -fPIC -DPIC" + fi ./configure "${myconf[@]}" make -j$(nproc) From 453273efa5bc3f0dc0b0930842a357c7e2000dfa Mon Sep 17 00:00:00 2001 From: gnattu Date: Sat, 21 Dec 2024 00:44:23 +0800 Subject: [PATCH 6/8] Fix indent --- builder/scripts.d/50-libopus.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/builder/scripts.d/50-libopus.sh b/builder/scripts.d/50-libopus.sh index 45e559d43c..b173c762b1 100755 --- a/builder/scripts.d/50-libopus.sh +++ b/builder/scripts.d/50-libopus.sh @@ -43,10 +43,10 @@ ffbuild_dockerbuild() { # For some reason libopus will not add optimization flag, we have to set it ourselves if [[ $TARGET == mac* ]]; then - export CFLAGS="-O3" - else - export CFLAGS="-O3 -fPIC -DPIC" - fi + export CFLAGS="-O3" + else + export CFLAGS="-O3 -fPIC -DPIC" + fi ./configure "${myconf[@]}" make -j$(nproc) From cde108476060430a664584025feb337efcfd4792 Mon Sep 17 00:00:00 2001 From: gnattu Date: Sat, 21 Dec 2024 19:47:17 +0800 Subject: [PATCH 7/8] builder: just append optimization flags --- builder/scripts.d/50-libopus.sh | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/builder/scripts.d/50-libopus.sh b/builder/scripts.d/50-libopus.sh index b173c762b1..edb1cf3d40 100755 --- a/builder/scripts.d/50-libopus.sh +++ b/builder/scripts.d/50-libopus.sh @@ -38,22 +38,11 @@ ffbuild_dockerbuild() { ) fi - # reset CLFAGS because libopus may give up optimization if current CFLAGS contains value - CFLAGS_BACKUP="$CFLAGS" - - # For some reason libopus will not add optimization flag, we have to set it ourselves - if [[ $TARGET == mac* ]]; then - export CFLAGS="-O3" - else - export CFLAGS="-O3 -fPIC -DPIC" - fi - - ./configure "${myconf[@]}" + # Override previously set -O(n) option and the CC's default optimization options. + CFLAGS="$CFLAGS -O3" ./configure "${myconf[@]}" make -j$(nproc) make install - export CFLAGS="$CFLAGS_BACKUP" - if [[ $TARGET == *arm64 ]]; then if [[ $TARGET == mac* ]]; then gsed -i 's/-lopus/-lopus -lNE10/' "$FFBUILD_PREFIX"/lib/pkgconfig/opus.pc From 3a595cd3a06fcb7eabe543af24245f079c234a02 Mon Sep 17 00:00:00 2001 From: gnattu Date: Sat, 21 Dec 2024 20:27:39 +0800 Subject: [PATCH 8/8] Fix indent Co-authored-by: Nyanmisaka --- builder/scripts.d/50-libopus.sh | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/builder/scripts.d/50-libopus.sh b/builder/scripts.d/50-libopus.sh index edb1cf3d40..c26bd11878 100755 --- a/builder/scripts.d/50-libopus.sh +++ b/builder/scripts.d/50-libopus.sh @@ -38,19 +38,19 @@ ffbuild_dockerbuild() { ) fi - # Override previously set -O(n) option and the CC's default optimization options. + # Override previously set -O(n) option and the CC's default optimization options. CFLAGS="$CFLAGS -O3" ./configure "${myconf[@]}" make -j$(nproc) make install if [[ $TARGET == *arm64 ]]; then if [[ $TARGET == mac* ]]; then - gsed -i 's/-lopus/-lopus -lNE10/' "$FFBUILD_PREFIX"/lib/pkgconfig/opus.pc - gsed -i 's/-I${includedir}\/opus/-I${includedir}\/opus -I${includedir}\/libNE10/' "$FFBUILD_PREFIX"/lib/pkgconfig/opus.pc - else - sed -i 's/-lopus/-lopus -lNE10/' "$FFBUILD_PREFIX"/lib/pkgconfig/opus.pc - sed -i 's/-I${includedir}\/opus/-I${includedir}\/opus -I${includedir}\/libNE10/' "$FFBUILD_PREFIX"/lib/pkgconfig/opus.pc - fi + gsed -i 's/-lopus/-lopus -lNE10/' "$FFBUILD_PREFIX"/lib/pkgconfig/opus.pc + gsed -i 's/-I${includedir}\/opus/-I${includedir}\/opus -I${includedir}\/libNE10/' "$FFBUILD_PREFIX"/lib/pkgconfig/opus.pc + else + sed -i 's/-lopus/-lopus -lNE10/' "$FFBUILD_PREFIX"/lib/pkgconfig/opus.pc + sed -i 's/-I${includedir}\/opus/-I${includedir}\/opus -I${includedir}\/libNE10/' "$FFBUILD_PREFIX"/lib/pkgconfig/opus.pc + fi fi }