435 lines
14 KiB
Bash
435 lines
14 KiB
Bash
#!/usr/bin/env bash
|
|
#
|
|
# SPDX-License-Identifier: GPL-2.0
|
|
#
|
|
# Copyright (c) 2026 AMLOGIC
|
|
#
|
|
# Libamvenc-264 build script for AMLbian
|
|
# This script builds libamvenc_264 library (H264) for Amlogic HCODEC IP
|
|
|
|
set -e
|
|
|
|
# Get script directory
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
AMLBIAN_DIR="$(dirname "$(dirname "$(dirname "${SCRIPT_DIR}")")")"
|
|
BUILD_DIR="${AMLBIAN_DIR}/../build"
|
|
MODULE_BUILD_DIR="${SRC}/output/.tmp"
|
|
|
|
# Source common functions
|
|
if [[ -f "${AMLBIAN_DIR}/lib/function-common.sh" ]]; then
|
|
source "${AMLBIAN_DIR}/lib/function-common.sh"
|
|
fi
|
|
|
|
# ==============================================================================
|
|
# Section 2: Default Configuration
|
|
# ==============================================================================
|
|
|
|
BOARD="${BOARD:-s6-bq201}"
|
|
DEB_VERSION="${LIBAMVENC_264_VERSION:-1.0}"
|
|
ARCH="${ARCH:-arm64}"
|
|
|
|
# ==============================================================================
|
|
# Section 4: Libamvenc-264 Build Functions
|
|
# ==============================================================================
|
|
|
|
function __unset_libamvenc_264_config {
|
|
# Clean up any environment variables set during build
|
|
unset SYSROOT
|
|
unset CFLAGS
|
|
unset CXXFLAGS
|
|
unset LDFLAGS
|
|
}
|
|
|
|
function __libamvenc_264_prepare_source {
|
|
local build_dir="${1}"
|
|
|
|
local aml_comp_path="$(amlbian_get_aml_comp_dir)"
|
|
if [[ -z "${aml_comp_path}" ]] || [[ ! -d "${aml_comp_path}" ]]; then
|
|
display_alert "LIBAMVENC-264" "aml-comp not found: ${aml_comp_path}" "err"
|
|
exit 1
|
|
fi
|
|
|
|
local source_dir="${aml_comp_path}/hardware/amlogic/libencoder/amvenc_264"
|
|
|
|
if [[ ! -d "${source_dir}" ]]; then
|
|
display_alert "LIBAMVENC-264" "Source directory not found: ${source_dir}" "err"
|
|
exit 1
|
|
fi
|
|
|
|
display_alert "LIBAMVENC-264" "Copying source from ${source_dir} to ${build_dir}" "info"
|
|
|
|
# 清空构建目录
|
|
rm -rf "${build_dir}"
|
|
mkdir -p "${build_dir}"
|
|
|
|
# 复制源代码到构建目录
|
|
cp -r "${source_dir}"/* "${build_dir}/" 2>/dev/null || true
|
|
|
|
# Check for necessary source files
|
|
if [[ ! -d "${build_dir}/bjunion_enc" ]]; then
|
|
display_alert "LIBAMVENC-264" "bjunion_enc directory not found in source" "err"
|
|
exit 1
|
|
fi
|
|
|
|
display_alert "LIBAMVENC-264" "Source prepared successfully" "success"
|
|
}
|
|
|
|
function __libamvenc_264_build_depend {
|
|
local build_dir="${1}"
|
|
|
|
# libion dependency check
|
|
local libion_dir="${MODULE_BUILD_DIR}/libion-build-${ARCH:-arm64}"
|
|
if [ -z "${libion_dir}" ] || [ ! -d "${libion_dir}" ]; then
|
|
display_alert "LIBAMVENC-264" "libion not found in ${MODULE_BUILD_DIR} build." "info"
|
|
display_alert "AMLOGIC" "TODO>> Start build libion " "info"
|
|
|
|
if ! (
|
|
source "${AMLBIAN_DIR}/scripts/build-libion.sh"
|
|
); then
|
|
display_alert "ERROR" "build-libion failed" "err"
|
|
return 1
|
|
fi
|
|
|
|
if [ -z "${libion_dir}" ] || [ ! -d "${libion_dir}" ]; then
|
|
display_alert "ERROR" "libion build finished but directory still not found" "err"
|
|
return 1
|
|
fi
|
|
|
|
fi
|
|
|
|
display_alert "LIBAMVENC-264" "libion dir: ${libion_dir}" "info"
|
|
|
|
# Add libion headers to compilation flags
|
|
export CPPFLAGS="${CPPFLAGS} -I${libion_dir}/build/include -I${libion_dir}/build/include/ion"
|
|
export CFLAGS="${CFLAGS} -I${libion_dir}/build/include -I${libion_dir}/build/include/ion"
|
|
export CXXFLAGS="${CXXFLAGS} -I${libion_dir}/build/include -I${libion_dir}/build/include/ion"
|
|
|
|
export LDFLAGS="${LDFLAGS} -L${libion_dir}/build"
|
|
|
|
# h264bitstream dependency check
|
|
local h264bs_root="${MODULE_BUILD_DIR}/h264bitstream-build-${ARCH:-arm64}"
|
|
local h264bs_install="${h264bs_root}/install"
|
|
if [ -z "${h264bs_install}" ] || [ ! -d "${h264bs_install}/usr/lib" ]; then
|
|
display_alert "LIBAMVENC-264" "h264bitstream not found in ${MODULE_BUILD_DIR} build." "info"
|
|
display_alert "AMLOGIC" "TODO>> Start build h264bitstream " "info"
|
|
|
|
if ! (
|
|
source "${AMLBIAN_DIR}/scripts/multimedia/libencoder/build-h264bitstream.sh"
|
|
); then
|
|
display_alert "ERROR" "build-h264bitstream failed" "err"
|
|
return 1
|
|
fi
|
|
|
|
if [ -z "${h264bs_install}" ] || [ ! -d "${h264bs_install}/usr/lib" ]; then
|
|
display_alert "ERROR" "h264bitstream build finished but directory still not found" "err"
|
|
return 1
|
|
fi
|
|
|
|
fi
|
|
|
|
display_alert "LIBAMVENC-264" "h264bitstream dir: ${h264bs_install}" "info"
|
|
|
|
# libvpcodec.cpp includes <h264bitstream/h264_stream.h>, so headers must be
|
|
# reachable under an "h264bitstream/" subdirectory. Build a staging include
|
|
# dir with symlinks since h264bitstream installs headers flat in usr/include.
|
|
local h264bs_stage_inc="${h264bs_root}/stage-include"
|
|
mkdir -p "${h264bs_stage_inc}/h264bitstream"
|
|
ln -sf "${h264bs_install}"/usr/include/*.h "${h264bs_stage_inc}/h264bitstream/" 2>/dev/null || true
|
|
|
|
# Add h264bitstream headers and library to compilation flags
|
|
export CPPFLAGS="${CPPFLAGS} -I${h264bs_stage_inc} -I${h264bs_install}/usr/include"
|
|
export CFLAGS="${CFLAGS} -I${h264bs_stage_inc} -I${h264bs_install}/usr/include"
|
|
export CXXFLAGS="${CXXFLAGS} -I${h264bs_stage_inc} -I${h264bs_install}/usr/include"
|
|
|
|
export LDFLAGS="${LDFLAGS} -L${h264bs_install}/usr/lib"
|
|
}
|
|
|
|
function __libamvenc_264_build {
|
|
local build_dir="${1}"
|
|
local install_dir="${2}"
|
|
|
|
display_alert "LIBAMVENC-264" "Starting libamvenc_264 build..." "info"
|
|
|
|
# 检查源目录
|
|
if [[ ! -d "${build_dir}" ]]; then
|
|
display_alert "LIBAMVENC-264" "Build directory not found: ${build_dir}" "err"
|
|
return 1
|
|
fi
|
|
|
|
# 设置编译环境
|
|
ARCH="${ARCH:-arm64}"
|
|
CROSS_COMPILE="${CROSS_COMPILE:-aarch64-linux-gnu-}"
|
|
SYSROOT="${SYSROOT:-/opt/arm64-sysroot}"
|
|
|
|
export ARCH="${ARCH}"
|
|
export CROSS_COMPILE="${CROSS_COMPILE}"
|
|
|
|
__libamvenc_264_build_depend "${build_dir}"
|
|
|
|
# 创建输出目录
|
|
local out_dir="${build_dir}/output"
|
|
mkdir -p "${out_dir}"
|
|
|
|
# Set environment variables for Makefile
|
|
export CROSS_CC_PREFIX="${CROSS_COMPILE}"
|
|
export CC="${CROSS_COMPILE}gcc"
|
|
export CXX="${CROSS_COMPILE}g++"
|
|
export AR="${CROSS_COMPILE}ar"
|
|
|
|
# Build libamvenc_264 (bjunion_enc)
|
|
display_alert "LIBAMVENC-264" "Building libamvenc_264 library..." "info"
|
|
cd "${build_dir}"
|
|
|
|
make -C "${build_dir}/bjunion_enc" \
|
|
LDLIBS="${LDFLAGS} -lh264bitstream -lm -lrt -shared" \
|
|
-j$(nproc) \
|
|
2>&1 | tee "${out_dir}/make_lib.log" || {
|
|
display_alert "LIBAMVENC-264" "Library build failed" "err"
|
|
return 1
|
|
}
|
|
|
|
if [[ ! -f "${build_dir}/bjunion_enc/libamvenc_264.so" ]]; then
|
|
display_alert "LIBAMVENC-264" "libamvenc_264.so not found after build" "err"
|
|
return 1
|
|
fi
|
|
|
|
# Build testApi
|
|
display_alert "LIBAMVENC-264" "Building testApi..." "info"
|
|
|
|
make -C "${build_dir}" \
|
|
-j$(nproc) \
|
|
2>&1 | tee "${out_dir}/make_test.log" || {
|
|
display_alert "LIBAMVENC-264" "Test program build failed" "err"
|
|
return 1
|
|
}
|
|
|
|
# Copy built files to output directory
|
|
mkdir -p "${out_dir}/lib" "${out_dir}/bin"
|
|
if [[ -f "${build_dir}/bjunion_enc/libamvenc_264.so" ]]; then
|
|
cp "${build_dir}/bjunion_enc/libamvenc_264.so" "${out_dir}/lib/"
|
|
fi
|
|
if [[ -f "${build_dir}/testApi" ]]; then
|
|
cp "${build_dir}/testApi" "${out_dir}/bin/"
|
|
fi
|
|
|
|
display_alert "LIBAMVENC-264" "Build completed successfully" "success"
|
|
return 0
|
|
}
|
|
|
|
function __libamvenc_264_install {
|
|
local build_dir="${1}"
|
|
local install_dir="${2}"
|
|
|
|
display_alert "LIBAMVENC-264" "Installing libamvenc_264 library..." "info"
|
|
|
|
local out_dir="${build_dir}/output"
|
|
|
|
if [[ ! -d "${out_dir}" ]]; then
|
|
display_alert "LIBAMVENC-264" "Output directory not found: ${out_dir}" "err"
|
|
return 1
|
|
fi
|
|
|
|
# 创建安装目录结构
|
|
mkdir -p "${install_dir}/usr/include"
|
|
mkdir -p "${install_dir}/usr/lib"
|
|
mkdir -p "${install_dir}/usr/bin"
|
|
|
|
# 安装库文件
|
|
display_alert "LIBAMVENC-264" "Installing library..." "info"
|
|
|
|
local lib_file="${out_dir}/lib/libamvenc_264.so"
|
|
if [[ -f "${lib_file}" ]]; then
|
|
install -m 0644 "${lib_file}" "${install_dir}/usr/lib/"
|
|
display_alert "LIBAMVENC-264" "Library installed to ${install_dir}/usr/lib/" "info"
|
|
else
|
|
display_alert "LIBAMVENC-264" "Library file not found: ${lib_file}" "err"
|
|
return 1
|
|
fi
|
|
|
|
# 安装头文件
|
|
display_alert "LIBAMVENC-264" "Installing header..." "info"
|
|
local header_file="${build_dir}/bjunion_enc/vpcodec_1_0.h"
|
|
if [[ -f "${header_file}" ]]; then
|
|
install -m 0644 "${header_file}" "${install_dir}/usr/include/"
|
|
display_alert "LIBAMVENC-264" "Header installed: vpcodec_1_0.h" "debug"
|
|
else
|
|
display_alert "LIBAMVENC-264" "Header file not found: ${header_file}" "warning"
|
|
fi
|
|
|
|
# 安装测试程序
|
|
display_alert "LIBAMVENC-264" "Installing test program..." "info"
|
|
local test_program="${out_dir}/bin/testApi"
|
|
if [[ -f "${test_program}" ]]; then
|
|
install -m 0755 "${test_program}" "${install_dir}/usr/bin/"
|
|
display_alert "LIBAMVENC-264" "Binary installed: testApi" "debug"
|
|
else
|
|
display_alert "LIBAMVENC-264" "Test program not found: ${test_program}" "warning"
|
|
fi
|
|
|
|
display_alert "LIBAMVENC-264" "Installation completed successfully" "success"
|
|
return 0
|
|
}
|
|
|
|
function __libamvenc_264_create_deb {
|
|
local build_dir="${1}"
|
|
local install_dir="${2}"
|
|
|
|
# 设置必要的变量
|
|
local pkg_name="libamvenc-264"
|
|
local version="${DEB_VERSION}"
|
|
local architecture="${ARCH}"
|
|
local maintainer="Armbian Build <build@armbian.com>"
|
|
local description="Amlogic libamvenc_264 library (H264) for Amlogic HCODEC IP"
|
|
local description_long="This package provides the Amlogic libamvenc_264 library used by H264 encoding on Amlogic platforms."
|
|
|
|
display_alert "LIBAMVENC-264" "Creating Debian package for ${pkg_name} ${version}..." "info"
|
|
|
|
# 创建Debian包控制文件
|
|
local deb_dir="${build_dir}/deb/${pkg_name}-${version}"
|
|
local debian_dir="${deb_dir}/DEBIAN"
|
|
|
|
mkdir -p "${debian_dir}"
|
|
|
|
# 将安装目录的内容复制到debian-root
|
|
if [[ -d "${install_dir}" ]] && [[ "$(ls -A "${install_dir}")" ]]; then
|
|
cp -r "${install_dir}"/* "${deb_dir}/" 2>/dev/null || true
|
|
display_alert "LIBAMVENC-264" "Copied installed files to ${deb_dir}" "info"
|
|
else
|
|
display_alert "LIBAMVENC-264" "No installed files found in ${install_dir}" "warn"
|
|
return 0
|
|
fi
|
|
|
|
# 创建control文件
|
|
cat > "${debian_dir}/control" <<EOF
|
|
Package: ${pkg_name}
|
|
Version: ${version}
|
|
Architecture: ${architecture}
|
|
Maintainer: ${maintainer}
|
|
Description: ${description}
|
|
${description_long}
|
|
Depends: libc6, libh264bitstream, libion
|
|
Section: libs
|
|
Priority: optional
|
|
EOF
|
|
|
|
# 创建postinst脚本,用于更新ldconfig
|
|
cat > "${debian_dir}/postinst" <<EOF
|
|
#!/bin/sh
|
|
set -e
|
|
|
|
if [ "\$1" = "configure" ]; then
|
|
# 更新ldconfig缓存
|
|
if command -v ldconfig >/dev/null 2>&1; then
|
|
ldconfig
|
|
fi
|
|
|
|
echo "Libamvenc-264 library installed successfully"
|
|
fi
|
|
|
|
exit 0
|
|
EOF
|
|
|
|
chmod 755 "${debian_dir}/postinst"
|
|
|
|
# 创建prerm脚本
|
|
cat > "${debian_dir}/prerm" <<EOF
|
|
#!/bin/sh
|
|
set -e
|
|
|
|
if [ "\$1" = "remove" ] || [ "\$1" = "deconfigure" ]; then
|
|
# Update ldconfig
|
|
if command -v ldconfig >/dev/null 2>&1; then
|
|
ldconfig
|
|
fi
|
|
echo "Removing libamvenc-264 library"
|
|
fi
|
|
|
|
exit 0
|
|
EOF
|
|
|
|
chmod 755 "${debian_dir}/prerm"
|
|
|
|
display_alert "LIBAMVENC-264" "Building Debian package..." "info"
|
|
|
|
# 构建Debian包
|
|
cd "${build_dir}/deb"
|
|
dpkg-deb --build "${pkg_name}-${version}"
|
|
|
|
local deb_file="${pkg_name}-${version}.deb"
|
|
if [[ -f "${deb_file}" ]]; then
|
|
local output_dir="${BUILD_DIR}/output"
|
|
mkdir -p "${output_dir}"
|
|
mv "${deb_file}" "${output_dir}/linux-${pkg_name}-${BOARD}-amlbian_${version}_${architecture}.deb"
|
|
display_alert "LIBAMVENC-264" "Debian package created: ${output_dir}/linux-${pkg_name}-${BOARD}-amlbian_${version}_${architecture}.deb" "success"
|
|
else
|
|
display_alert "LIBAMVENC-264" "Failed to build Debian package" "err"
|
|
return 1
|
|
fi
|
|
|
|
return 0
|
|
}
|
|
|
|
function amlbian_build_libamvenc_264 {
|
|
if ! is_unit_enabled "libamvenc-264" && ! is_unit_enabled "libencoder"; then
|
|
display_alert "[SKIP] >> libamvenc-264 disable in BUILD_UNITS" "" "info"
|
|
return
|
|
fi
|
|
|
|
if ! is_feature_contains "$MEDIA_FEATURES" "aml-libamvenc-264"; then
|
|
display_alert "[SKIP] >> aml-libamvenc-264 disable in MEDIA_FEATURES" "" "info"
|
|
return
|
|
fi
|
|
|
|
display_alert "AMLOGIC: Building libamvenc-264 ${DEB_VERSION} ${BOARD}" "info"
|
|
|
|
# Define all directory paths (in .tmp directory)
|
|
local build_root="${MODULE_BUILD_DIR}/libamvenc-264-build-${ARCH:-arm64}"
|
|
local build_dir="${build_root}/build" # Build directory with source code
|
|
local install_dir="${build_root}/install" # Install directory
|
|
|
|
rm -rf "${build_root}"
|
|
|
|
# 创建目录
|
|
mkdir -p "${build_dir}" "${install_dir}"
|
|
|
|
display_alert "AMLOGIC: build_dir ${build_dir}" "info"
|
|
display_alert "AMLOGIC: install_dir ${install_dir}" "info"
|
|
display_alert "AMLOGIC: output_dir ${BUILD_DIR}/output" "info"
|
|
|
|
# 准备源代码
|
|
__libamvenc_264_prepare_source "${build_dir}"
|
|
|
|
# 构建
|
|
if ! __libamvenc_264_build "${build_dir}" "${install_dir}"; then
|
|
display_alert "AMLOGIC: Libamvenc-264 build failed" "err"
|
|
return 1
|
|
fi
|
|
|
|
# 安装到安装目录
|
|
if ! __libamvenc_264_install "${build_dir}" "${install_dir}"; then
|
|
display_alert "AMLOGIC: Libamvenc-264 installation failed" "err"
|
|
return 1
|
|
fi
|
|
|
|
display_alert "AMLOGIC: Creating Debian package..." "info"
|
|
__libamvenc_264_create_deb "${build_dir}" "${install_dir}"
|
|
|
|
__unset_libamvenc_264_config
|
|
|
|
return 0
|
|
}
|
|
|
|
# ==============================================================================
|
|
# Section 5: Main
|
|
# ==============================================================================
|
|
|
|
display_alert "AMLbian Libamvenc-264 Build Script" "Version: ${DEB_VERSION}, Board: ${BOARD}" "info"
|
|
|
|
amlbian_build_libamvenc_264
|
|
|
|
display_alert "AMLOGIC: Libamvenc-264 build completed" "${BUILD_DIR}/output" "info"
|
|
|
|
# ==============================================================================
|
|
# End of build-libamvenc-264.sh
|
|
# ==============================================================================
|