Skip to content

Commit

Permalink
Merge in option to build ffmpeg and x265 from main
Browse files Browse the repository at this point in the history
Now I don't have to maintain two branches all the time
  • Loading branch information
Vargol committed Aug 26, 2023
1 parent fc9495c commit db79ec8
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 23 deletions.
14 changes: 14 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
ENABLE_FFPLAY=FALSE
ENABLE_TOPAZ=FALSE
ENABLE_AVISYNTHPLUS=FALSE
BUILD_FROM_MAIN=FALSE


# set true for dependant features, export those needed in ffmpeg build script

Expand All @@ -23,6 +25,7 @@ then
echo
echo If you have logged out of your Topaz account you can log in using
echo out/bin/login topaz_account_email_address topaz_account_password
echo

fi

Expand All @@ -36,6 +39,17 @@ then
export ENABLE_AVISYNTHPLUS=TRUE
echo "Enabling AviSynthPlus will meaan this binary is not longer staticly built."
echo "To use AviSynthPlus you will need to run from the tool/lib directory or a directory with a link to the tool/lib/libavisynth.dylib file"
echo
fi

if [[ "${BUILD_FROM_MAIN}" == "TRUE" ]]
then
export BUILD_FROM_MAIN=TRUE
echo "Enabling Build from main."
echo "This will build ffmpeg and x265 from their respective source repositories."
echo "This may break things, or have unexpected effects, but x265 will be a little more performant"
echo "and no longer require that huge patch as the code has been incorporated and further enhancements made."
echo
fi

# get rid of macports - libiconv
Expand Down
3 changes: 2 additions & 1 deletion build/build-aom.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ download_code () {
cd "$2/${SOFTWARE}"
checkStatus $? "change directory failed"
# download source
git clone --depth=1 https://aomedia.googlesource.com/aom
git clone https://aomedia.googlesource.com/aom

checkStatus $? "download of ${SOFTWARE} failed"

}
Expand Down
37 changes: 25 additions & 12 deletions build/build-ffmpeg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

# load functions
. $1/functions.sh

set -x
SOFTWARE=ffmpeg

make_directories() {
Expand All @@ -28,20 +28,32 @@ download_code () {
cd "$2/${SOFTWARE}"
checkStatus $? "change directory failed"
# download source
curl -O https://ffmpeg.org/releases/ffmpeg-$6.tar.bz2
checkStatus $? "download of ${SOFTWARE} failed"

# unpack ffmpeg
bunzip2 ffmpeg-$6.tar.bz2
tar -xf ffmpeg-$6.tar
cd "ffmpeg-$6/"
checkStatus $? "change directory failed"

if [[ "${BUILD_FROM_MAIN}" == "TRUE" ]]
then
git clone https://git.ffmpeg.org/ffmpeg.git
cd ffmpeg
checkStatus $? "change directory failed"
export FFMPEG_DIR="ffmpeg"

else
curl -O https://ffmpeg.org/releases/ffmpeg-$6.tar.bz2
checkStatus $? "download of ${SOFTWARE} failed"

# unpack ffmpeg
bunzip2 ffmpeg-$6.tar.bz2
tar -xf ffmpeg-$6.tar
cd "ffmpeg-$6/"
checkStatus $? "change directory failed"
export FFMPEG_DIR="ffmpeg-$6"
fi

}

configure_build () {

cd "$2/${SOFTWARE}/ffmpeg-$6/"
cd "$2/${SOFTWARE}/${FFMPEG_DIR}/"
checkStatus $? "change directory failed"

# prepare build
Expand All @@ -67,15 +79,16 @@ configure_build () {
--enable-libaom --enable-libopenh264 --enable-libx264 --enable-libx265 --enable-libvpx \
--enable-libmp3lame --enable-libopus --enable-neon --enable-runtime-cpudetect \
--enable-audiotoolbox --enable-videotoolbox --enable-libvorbis --enable-libsvtav1 \
--enable-libass --enable-lto --enable-nonfree --enable-libfdk-aac --enable-opencl ${FFMPEG_EXTRAS}
--enable-libass --enable-nonfree --enable-libfdk-aac --enable-opencl ${FFMPEG_EXTRAS}
# --enable-libass --enable-lto --enable-nonfree --enable-libfdk-aac --enable-opencl ${FFMPEG_EXTRAS}

checkStatus $? "configuration of ${SOFTWARE} failed"

}

make_clean() {

cd "$2/${SOFTWARE}/ffmpeg-$6/"
cd "$2/${SOFTWARE}/${FFMPEG_DIR}/"
checkStatus $? "change directory failed"
make clean
checkStatus $? "make clean for $SOFTWARE failed"
Expand All @@ -85,7 +98,7 @@ make_clean() {

make_compile () {

cd "$2/${SOFTWARE}/ffmpeg-$6/"
cd "$2/${SOFTWARE}/${FFMPEG_DIR}/"
checkStatus $? "change directory failed"

# build
Expand Down
30 changes: 20 additions & 10 deletions build/build-x265.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@
# $4 = CPUs
# $5 = x265 version

set -x
set -x
# load functions
. $1/functions.sh

if [[ "${BUILD_FROM_MAIN}" == "TRUE" ]]
then
BRANCH=""
PATCH="git apply $1/x265_arm64_threading.patch"
else
BRANCH="--branch stable"
PATCH="patch -p1 -i $1/apple_arm64_x265.patch"
fi

make_directories() {

Expand Down Expand Up @@ -39,7 +47,7 @@ download_code() {

# Build fails when using git shallow clone
# https://bitbucket.org/multicoreware/x265_git/issues/572/pc-file-is-not-generated
git clone https://bitbucket.org/multicoreware/x265_git.git --branch stable
git clone https://bitbucket.org/multicoreware/x265_git.git ${BRANCH}
checkStatus $? "download of x265 failed"

# TODO: checksum validation (if available)
Expand All @@ -58,12 +66,15 @@ configure_build () {


#patch for arm64 / neon recognition
patch -p1 < $1/apple_arm64_x265.patch
#patch -p1 < $1/apple_arm64_x265.patch
${PATCH}


cd ../12bit

# prepare build

FF
cmake -DCMAKE_INSTALL_PREFIX:PATH=$3 -DENABLE_SHARED=NO -DHIGH_BIT_DEPTH=ON -DEXPORT_C_API=OFF -DENABLE_CLI=OFF -DMAIN12=ON ../$GIT_DIR/source
checkStatus $? "configuration of 12bit x265 failed"

Expand All @@ -78,7 +89,7 @@ configure_build () {

# prepare build

cmake -DCMAKE_INSTALL_PREFIX:PATH=$3 -DENABLE_SHARED=NO -DEXTRA_LIB="x265_main10.a;x265_main12.a" -DEXTRA_LINK_FLAGS=-L. -DLINKED_10BIT=ON -DLINKED_12BIT=ON -DENABLE_CLI=NO ../$GIT_DIR/source
cmake -DCMAKE_INSTALL_PREFIX:PATH=$3 -DENABLE_SHARED=NO -DEXTRA_LIB="x265_main10.a;x265_main12.a" -DEXTRA_LINK_FLAGS=-L. -DLINKED_10BIT=ON -DLINKED_12BIT=ON -DENABLE_CLI=OFF ../$GIT_DIR/source
checkStatus $? "configuration of 8bit x265 failed"

}
Expand Down Expand Up @@ -125,14 +136,15 @@ make_compile () {
cd "$2/x265/8bit"
checkStatus $? "change directory failed"

#soft symlink the libraies
ln -sf ../10bit/libx265.a libx265_main10.a
ln -sf ../12bit/libx265.a libx265_main12.a

# build
make -j $4
checkStatus $? "build of x265 failed"

#merge the libaries together
ln -sf ../10bit/libx265.a libx265_main10.a
ln -sf ../12bit/libx265.a libx265_main12.a

mv libx265.a libx265_main.a
libtool -static -o libx265.a libx265_main.a libx265_main10.a libx265_main12.a 2>/dev/null
checkStatus $? "merge of x265 objects failed"
Expand All @@ -141,9 +153,6 @@ make_compile () {
make install
checkStatus $? "installation of x265 failed"

# post-installation
# modify pkg-config file for usage with ffmpeg (it seems that the flag for threads is missing)
sed -i.original -e 's/lx265/lx265 -lpthread/g' $3/lib/pkgconfig/x265.pc

}

Expand Down Expand Up @@ -174,3 +183,4 @@ build_main () {


build_main $@

37 changes: 37 additions & 0 deletions build/x265_arm64_threading.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
diff --git a/source/common/threadpool.cpp b/source/common/threadpool.cpp
index 91e82a24e..e21ae9cd6 100644
--- a/source/common/threadpool.cpp
+++ b/source/common/threadpool.cpp
@@ -115,12 +115,6 @@ void WorkerThread::threadMain()
{
THREAD_NAME("Worker", m_id);

-#if _WIN32
- SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_BELOW_NORMAL);
-#else
- __attribute__((unused)) int val = nice(10);
-#endif
-
m_pool.setCurrentThreadAffinity();

sleepbitmap_t idBit = (sleepbitmap_t)1 << m_id;
@@ -667,9 +661,17 @@ void ThreadPool::getFrameThreadsCount(x265_param* p, int cpuCount)
else if (cpuCount >= 32)
p->frameNumThreads = (p->sourceHeight > 2000) ? 6 : 5;
else if (cpuCount >= 16)
- p->frameNumThreads = 4;
+#if MACOS && X265_ARCH_ARM64
+ p->frameNumThreads = 16;
+#else
+ p->frameNumThreads = 4;
+#endif
else if (cpuCount >= 8)
- p->frameNumThreads = 3;
+#if MACOS && X265_ARCH_ARM64
+ p->frameNumThreads = 8;
+#else
+ p->frameNumThreads = 3;
+#endif
else if (cpuCount >= 4)
p->frameNumThreads = 2;
else

0 comments on commit db79ec8

Please sign in to comment.