Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x264: add compilation script #84

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,6 @@ fi

[ "${PORTS_COREMARK}" = "y" ] && ./phoenix-rtos-ports/coremark/build.sh

[ "${PORTS_X264}" = "y" ] && ./phoenix-rtos-ports/x264/build.sh

exit 0
93 changes: 93 additions & 0 deletions x264/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/usr/bin/env bash

set -e

PREFIX_X264="${PREFIX_PROJECT}/phoenix-rtos-ports/x264"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to base new ports on this redesign: #78

I hope it will be merged soon @Darchiv

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for that! I didn`t notice this PR. I will wait for that PR to be merged and then redo this script.

PREFIX_X264_BUILD="${PREFIX_BUILD}/x264"
PREFIX_X264_CONFIG="${PREFIX_X264}/patches"
PREFIX_X264_MARKERS="${PREFIX_X264_BUILD}/markers"

PKG_COMMIT="31e19f92f00c7003fa115047ce50978bc98c3a0d"
PKG_NAME="x264-${PKG_COMMIT}.tar.gz"
PKG_URL="https://code.videolan.org/videolan/x264/-/archive/${PKG_COMMIT}"

b_log "Building x264"

#
# Download archived source code
#
if [ ! -f "$PREFIX_X264/${PKG_NAME}" ]; then
if ! wget "${PKG_URL}/${PKG_NAME}" -O "${PREFIX_X264}/${PKG_NAME}" --no-check-certificate; then
echo "Mirror unavailable"
exit 1
fi
fi

#
# Unpack source code
#
if [ ! -d "${PREFIX_X264_BUILD}" ]; then
tar -xf "${PREFIX_X264}/${PKG_NAME}" -C "${PREFIX_BUILD}"
(cd "${PREFIX_BUILD}" && mv "${PKG_NAME%.tar.gz}" x264)
mkdir -p "${PREFIX_X264_MARKERS}"
fi

#
# Apply patches
#
for patchfile in "$PREFIX_X264_CONFIG"/*.patch; do
if [ ! -f "$PREFIX_X264_MARKERS/$(basename "$patchfile").applied" ]; then
echo "applying patch: $patchfile"
patch -d "$PREFIX_X264_BUILD" -p1 < "$patchfile"
touch "$PREFIX_X264_MARKERS/$(basename "$patchfile").applied"
fi
done

#
# Prepare CFLAGS and LDFLAGS for x264 configure & Makefile
#
export LDFLAGS_EXTRA="${CFLAGS} ${LDFLAGS} -Wl,-z,stack-size=65536"
export CFLAGS_EXTRA="${CFLAGS}"
export LDFLAGS=""
export CFLAGS=""


#
# Prepare configuration options
#
if [ "${TARGET_FAMILY}" = "armv7a9" ]; then
CFG_OPT=("--host=arm-linux")
export AS=$CC
elif [ "${TARGET_FAMILY}" = "ia32" ]; then
# Assembly optimization for i386 platform is done using nasm and is not compatible with our toolchain
CFG_OPT=("--host=i386-linux" "--disable-asm")
else
CFG_OPT=("--host=arm-linux" "--disable-asm")
b_log "Warning! Phoenix-RTOS for ${TARGET_FAMILY} does not support x264 compilation (yet!)"
b_log "The compilation attempt as for arm-linux will start in 5 seconds..."
sleep 5
fi

# mimic linux platform, enable position independent code, and generate libx264.a
# avs, lavf and opencl force dynamic linking, thus they're disabled.
CFG_OPT+=("--enable-pic" "--enable-static")
CFG_OPT+=("--disable-avs" "--disable-lavf" "--disable-opencl")

#TODO: rewrite parts of x264/common/cpu.c to support Phoenix-RTOS multithreading
# x264 implementation uses GNU specific processor counting method for multithreading.
# Remove this flag and see where compilation stops to start working on it.
CFG_OPT+=("--disable-thread")


#
# Run configuration script
#
(cd "$PREFIX_X264_BUILD" && ./configure --extra-cflags="$CFLAGS_EXTRA" --extra-ldflags="$LDFLAGS_EXTRA" --cross-prefix="$CROSS" --sysroot="$PREFIX_BUILD/sysroot/" "${CFG_OPT[@]}")

#
# Build and install x264 binary
#
(cd "$PREFIX_X264_BUILD" && make)

cp -a "${PREFIX_X264_BUILD}/x264" "$PREFIX_PROG_STRIPPED"
b_install "$PREFIX_PORTS_INSTALL/x264" /bin/
12 changes: 12 additions & 0 deletions x264/patches/configure.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--- x264_org/configure 2024-09-06 10:29:54.767763739 +0200
+++ x264/configure 2024-09-06 10:35:58.053941778 +0200
@@ -680,7 +680,8 @@
;;
*linux*)
SYS="LINUX"
- define HAVE_MALLOC_H
+ # Phoenix-RTOS does not support malloc.h
+ # define HAVE_MALLOC_H
libm="-lm"
;;
gnu*)
45 changes: 45 additions & 0 deletions x264/patches/input.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
--- x264_org/input/input.c 2024-09-06 10:29:54.784756723 +0200
+++ x264/input/input.c 2024-09-06 11:05:56.206158357 +0200
@@ -227,7 +227,19 @@
}
#else
size_t padded_size = size + MMAP_PADDING;
+#ifdef phoenix
+ /**
+ * PHOENIX-RTOS PATCH
+ *
+ * Phoenix implementation of mmap() fails if `size` is not aligned to page size
+ * It is important to remove this patch when this issue is resolved.
+ *
+ * https://github.com/phoenix-rtos/phoenix-rtos-project/issues/1155
+ */
+ if( (base = mmap( NULL, ((padded_size + (SIZE_PAGE - 1)) & ~(SIZE_PAGE - 1)), PROT_READ, MAP_PRIVATE, h->fd, offset )) != MAP_FAILED )
+#else
if( (base = mmap( NULL, padded_size, PROT_READ, MAP_PRIVATE, h->fd, offset )) != MAP_FAILED )
+#endif
{
/* Ask the OS to readahead pages. This improves performance whereas
* forcing page faults by manually accessing every page does not.
@@ -242,7 +254,22 @@
* the file into a copy of the last valid page to prevent reads from invalid memory. */
size_t aligned_size = (padded_size - 1) & ~h->align_mask;
if( offset + aligned_size >= h->file_size )
+#ifdef phoenix
+ {
+ /**
+ * PHOENIX-RTOS PATCH
+ *
+ * Phoenix implementation of mmap() fails if `size` is not aligned to page size
+ * It is important to remove this patch when this issue is resolved.
+ *
+ * https://github.com/phoenix-rtos/phoenix-rtos-project/issues/1155
+ */
+ size_t phoenix_paddedMinusAligned = padded_size - aligned_size;
+ mmap( base + aligned_size, ((phoenix_paddedMinusAligned + (SIZE_PAGE - 1)) & ~(SIZE_PAGE - 1)), PROT_READ, MAP_PRIVATE|MAP_FIXED, h->fd, (offset + size - 1) & ~h->align_mask );
+ }
+#else
mmap( base + aligned_size, padded_size - aligned_size, PROT_READ, MAP_PRIVATE|MAP_FIXED, h->fd, (offset + size - 1) & ~h->align_mask );
+#endif

return base + align;
}
Loading