ios编译和精简ffmpeg

本文记录如何在iOS下编译ffmpeg,并通过参数精简大小。这里仅仅编译arm64架构

1.基础知识

1.1. osx下如何交叉编译iOS 程序

众所周知开发iOS程序使用xcode,xcode是可以开发iOS程序的,但是命令行下的不能直接通过clang或者makefile来编译iOS程序,需要使用xcrun系列命令来获取相应的toolchain

调用clang编译

xcrun -sdk iphoneos clang test.c

获取toolchain路径

  xcrun -sdk iphoneos -f clang # clang path
 xcrun -sdk iphoneos -f ar # ar path
 xcrun -sdk iphoneos --show-sdk-path # SDK path
 

2.基本库编译

2.1 基本脚本

arch-common.sh:

#!/bin/bash

export MOBILE_FFMPEG_TMPDIR="${BASEDIR}/.tmp"

autoreconf_library() {
  echo -e "\nDEBUG: Running full autoreconf for $1\n" 1>>${BASEDIR}/build.log 2>&1

  # TRY FULL RECONF
  (autoreconf --force --install)

  local EXTRACT_RC=$?
  if [ ${EXTRACT_RC} -eq 0 ]; then
    return
  fi

  echo -e "\nDEBUG: Full autoreconf failed. Running full autoreconf with include for $1\n" 1>>${BASEDIR}/build.log 2>&1

  # TRY FULL RECONF WITH m4
  (autoreconf --force --install -I m4)

  EXTRACT_RC=$?
  if [ ${EXTRACT_RC} -eq 0 ]; then
    return
  fi

  echo -e "\nDEBUG: Full autoreconf with include failed. Running autoreconf without force for $1\n" 1>>${BASEDIR}/build.log 2>&1

  # TRY RECONF WITHOUT FORCE
  (autoreconf --install)

  EXTRACT_RC=$?
  if [ ${EXTRACT_RC} -eq 0 ]; then
    return
  fi

  echo -e "\nDEBUG: Autoreconf without force failed. Running autoreconf without force with include for $1\n" 1>>${BASEDIR}/build.log 2>&1

  # TRY RECONF WITHOUT FORCE WITH m4
  (autoreconf --install -I m4)

  EXTRACT_RC=$?
  if [ ${EXTRACT_RC} -eq 0 ]; then
    return
  fi

  echo -e "\nDEBUG: Autoreconf without force with include failed. Running default autoreconf for $1\n" 1>>${BASEDIR}/build.log 2>&1

  # TRY DEFAULT RECONF
  (autoreconf)

  EXTRACT_RC=$?
  if [ ${EXTRACT_RC} -eq 0 ]; then
    return
  fi

  echo -e "\nDEBUG: Default autoreconf failed. Running default autoreconf with include for $1\n" 1>>${BASEDIR}/build.log 2>&1

  # TRY DEFAULT RECONF WITH m4
  (autoreconf -I m4)

  EXTRACT_RC=$?
  if [ ${EXTRACT_RC} -eq 0 ]; then
    return
  fi
}

#
# 1. <repo url>
# 2. <local folder path>
# 3. <commit id>
#
clone_git_repository_with_commit_id() {
  local RC

  (mkdir -p $2 1>>${BASEDIR}/build.log 2>&1)

  RC=$?

  if [ ${RC} -ne 0 ]; then
    echo -e "\nDEBUG: Failed to create local directory $2\n" 1>>${BASEDIR}/build.log 2>&1
    rm -rf $2 1>>${BASEDIR}/build.log 2>&1
    echo ${RC}
    return
  fi

  (git clone $1 $2 --depth 1 1>>${BASEDIR}/build.log 2>&1)

  RC=$?

  if [ ${RC} -ne 0 ]; then
    echo -e "\nDEBUG: Failed to clone $1\n" 1>>${BASEDIR}/build.log 2>&1
    rm -rf $2 1>>${BASEDIR}/build.log 2>&1
    echo ${RC}
    return
  fi

  cd $2 1>>${BASEDIR}/build.log 2>&1

  RC=$?

  if [ ${RC} -ne 0 ]; then
    echo -e "\nDEBUG: Failed to cd into $2\n" 1>>${BASEDIR}/build.log 2>&1
    rm -rf $2 1>>${BASEDIR}/build.log 2>&1
    echo ${RC}
    return
  fi

  (git fetch --depth 1 origin $3 1>>${BASEDIR}/build.log 2>&1)

  RC=$?

  if [ ${RC} -ne 0 ]; then
    echo -e "\nDEBUG: Failed to fetch commit id $3 from $1\n" 1>>${BASEDIR}/build.log 2>&1
    rm -rf $2 1>>${BASEDIR}/build.log 2>&1
    echo ${RC}
    return
  fi

  (git checkout $3 1>>${BASEDIR}/build.log 2>&1)

  RC=$?

  if [ ${RC} -ne 0 ]; then
    echo -e "\nDEBUG: Failed to checkout commit id $3 from $1\n" 1>>${BASEDIR}/build.log 2>&1
    echo ${RC}
    return
  fi

  echo ${RC}
}

#
# 1. <repo url>
# 2. <tag name>
# 3. <local folder path>
#
clone_git_repository_with_tag() {
  local RC

  (mkdir -p $3 1>>${BASEDIR}/build.log 2>&1)

  RC=$?

  if [ ${RC} -ne 0 ]; then
    echo -e "\nDEBUG: Failed to create local directory $3\n" 1>>${BASEDIR}/build.log 2>&1
    rm -rf $3 1>>${BASEDIR}/build.log 2>&1
    echo ${RC}
    return
  fi

  (git clone --depth 1 --branch $2 $1 $3 1>>${BASEDIR}/build.log 2>&1)

  RC=$?

  if [ ${RC} -ne 0 ]; then
    echo -e "\nDEBUG: Failed to clone $1 -> $2\n" 1>>${BASEDIR}/build.log 2>&1
    rm -rf $3 1>>${BASEDIR}/build.log 2>&1
    echo ${RC}
    return
  fi

  echo ${RC}
}

#
# 1. <url>
# 2. <local file name>
# 3. <on error action>
#
download() {
  if [ ! -d "${MOBILE_FFMPEG_TMPDIR}" ]; then
    mkdir -p "${MOBILE_FFMPEG_TMPDIR}"
  fi

  (curl --fail --location $1 -o ${MOBILE_FFMPEG_TMPDIR}/$2 1>>${BASEDIR}/build.log 2>&1)

  local RC=$?

  if [ ${RC} -eq 0 ]; then
    echo -e "\nDEBUG: Downloaded $1 to ${MOBILE_FFMPEG_TMPDIR}/$2\n" 1>>${BASEDIR}/build.log 2>&1
  else
    rm -f ${MOBILE_FFMPEG_TMPDIR}/$2 1>>${BASEDIR}/build.log 2>&1

    echo -e -n "\nINFO: Failed to download $1 to ${MOBILE_FFMPEG_TMPDIR}/$2, rc=${RC}. " 1>>${BASEDIR}/build.log 2>&1

    if [ "$3" == "exit" ]; then
      echo -e "DEBUG: Build will now exit.\n" 1>>${BASEDIR}/build.log 2>&1
      exit 1
    else
      echo -e "DEBUG: Build will continue.\n" 1>>${BASEDIR}/build.log 2>&1
    fi
  fi

  echo ${RC}
}

download_library_source() {
  local LIB_REPO_URL=""
  local LIB_NAME="$1"
  local LIB_LOCAL_PATH=${BASEDIR}/src/${LIB_NAME}
  local SOURCE_ID=""
  local LIBRARY_RC=""
  local DOWNLOAD_RC=""
  local SOURCE_TYPE=""

  echo -e "\nDEBUG: Downloading library source: $1\n" 1>>${BASEDIR}/build.log 2>&1

  case $1 in
  cpu-features)
    LIB_REPO_URL="https://github.com/tanersener/cpu_features"
    SOURCE_ID="v0.4.1.1"                                      # TAG
    SOURCE_TYPE="TAG"
    ;;
  ffmpeg)
    LIB_REPO_URL="https://github.com/tanersener/FFmpeg"
    SOURCE_ID="d222da435e63a2665b85c0305ad2cf8a07b1af6d"      # COMMIT -> v4.4-dev-416
    SOURCE_TYPE="COMMIT"
    ;;
  esac

  LIBRARY_RC=$(library_is_downloaded "${LIB_NAME}")

  if [ ${LIBRARY_RC} -eq 0 ]; then
    echo -e "INFO: $1 already downloaded. Source folder found at ${LIB_LOCAL_PATH}\n" 1>>${BASEDIR}/build.log 2>&1
    echo 0
    return
  fi

  if [ ${SOURCE_TYPE} == "TAG" ]; then
    DOWNLOAD_RC=$(clone_git_repository_with_tag "${LIB_REPO_URL}" "${SOURCE_ID}" "${LIB_LOCAL_PATH}")
  else
    DOWNLOAD_RC=$(clone_git_repository_with_commit_id "${LIB_REPO_URL}" "${LIB_LOCAL_PATH}" "${SOURCE_ID}")
  fi

  if [ ${DOWNLOAD_RC} -ne 0 ]; then
    echo -e "INFO: Downloading library $1 failed. Can not get library from ${LIB_REPO_URL}\n" 1>>${BASEDIR}/build.log 2>&1
    echo ${DOWNLOAD_RC}
  else
    echo -e "DEBUG: $1 library downloaded\n" 1>>${BASEDIR}/build.log 2>&1
  fi
}

download_gpl_library_source() {
  local GPL_LIB_URL=""
  local GPL_LIB_FILE=""
  local GPL_LIB_ORIG_DIR=""
  local GPL_LIB_DEST_DIR="$1"
  local GPL_LIB_SOURCE_PATH="${BASEDIR}/src/${GPL_LIB_DEST_DIR}"
  local LIBRARY_RC=""
  local DOWNLOAD_RC=""

  echo -e "\nDEBUG: Downloading GPL library source: $1\n" 1>>${BASEDIR}/build.log 2>&1

  case $1 in
  libvidstab)
    GPL_LIB_URL="https://github.com/georgmartius/vid.stab/archive/v1.1.0.tar.gz"
    GPL_LIB_FILE="v1.1.0.tar.gz"
    GPL_LIB_ORIG_DIR="vid.stab-1.1.0"
    ;;
  x264)
    GPL_LIB_URL="https://code.videolan.org/videolan/x264/-/archive/cde9a93319bea766a92e306d69059c76de970190/x264-cde9a93319bea766a92e306d69059c76de970190.tar.bz2"
    GPL_LIB_FILE="x264-cde9a93319bea766a92e306d69059c76de970190.tar.bz2"
    GPL_LIB_ORIG_DIR="x264-cde9a93319bea766a92e306d69059c76de970190"
    ;;
  x265)
    GPL_LIB_URL="https://github.com/videolan/x265/archive/3.4.tar.gz"
    GPL_LIB_FILE="x265_3.4.tar.gz"
    GPL_LIB_ORIG_DIR="x265-3.4"
    ;;
  xvidcore)
    GPL_LIB_URL="https://downloads.xvid.com/downloads/xvidcore-1.3.7.tar.gz"
    GPL_LIB_FILE="xvidcore-1.3.7.tar.gz"
    GPL_LIB_ORIG_DIR="xvidcore"
    ;;
  rubberband)
    GPL_LIB_URL="https://breakfastquay.com/files/releases/rubberband-1.8.2.tar.bz2"
    GPL_LIB_FILE="rubberband-1.8.2.tar.bz2"
    GPL_LIB_ORIG_DIR="rubberband-1.8.2"
    ;;
  esac

  LIBRARY_RC=$(library_is_downloaded "${GPL_LIB_DEST_DIR}")

  if [ ${LIBRARY_RC} -eq 0 ]; then
    echo -e "INFO: $1 already downloaded. Source folder found at ${GPL_LIB_SOURCE_PATH}\n" 1>>${BASEDIR}/build.log 2>&1
    echo 0
    return
  fi

  local GPL_LIB_PACKAGE_PATH="${MOBILE_FFMPEG_TMPDIR}/${GPL_LIB_FILE}"

  echo -e "DEBUG: $1 source not found. Checking if library package ${GPL_LIB_FILE} is downloaded at ${GPL_LIB_PACKAGE_PATH} \n" 1>>${BASEDIR}/build.log 2>&1

  if [ ! -f "${GPL_LIB_PACKAGE_PATH}" ]; then
    echo -e "DEBUG: $1 library package not found. Downloading from ${GPL_LIB_URL}\n" 1>>${BASEDIR}/build.log 2>&1

    DOWNLOAD_RC=$(download "${GPL_LIB_URL}" "${GPL_LIB_FILE}")

    if [ ${DOWNLOAD_RC} -ne 0 ]; then
      echo -e "INFO: Downloading GPL library $1 failed. Can not get library package from ${GPL_LIB_URL}\n" 1>>${BASEDIR}/build.log 2>&1
      echo ${DOWNLOAD_RC}
      return
    else
      echo -e "DEBUG: $1 library package downloaded\n" 1>>${BASEDIR}/build.log 2>&1
    fi
  else
    echo -e "DEBUG: $1 library package already downloaded\n" 1>>${BASEDIR}/build.log 2>&1
  fi

  local EXTRACT_COMMAND=""

  if [[ ${GPL_LIB_FILE} == *bz2 ]]; then
    EXTRACT_COMMAND="tar jxf ${GPL_LIB_PACKAGE_PATH} --directory ${MOBILE_FFMPEG_TMPDIR}"
  else
    EXTRACT_COMMAND="tar zxf ${GPL_LIB_PACKAGE_PATH} --directory ${MOBILE_FFMPEG_TMPDIR}"
  fi

  echo -e "DEBUG: Extracting library package ${GPL_LIB_FILE} inside ${MOBILE_FFMPEG_TMPDIR}\n" 1>>${BASEDIR}/build.log 2>&1

  ${EXTRACT_COMMAND} 1>>${BASEDIR}/build.log 2>&1

  local EXTRACT_RC=$?

  if [ ${EXTRACT_RC} -ne 0 ]; then
    echo -e "\nINFO: Downloading GPL library $1 failed. Extract for library package ${GPL_LIB_FILE} completed with rc=${EXTRACT_RC}. Deleting failed files.\n" 1>>${BASEDIR}/build.log 2>&1
    rm -f ${GPL_LIB_PACKAGE_PATH} 1>>${BASEDIR}/build.log 2>&1
    rm -rf ${MOBILE_FFMPEG_TMPDIR}/${GPL_LIB_ORIG_DIR} 1>>${BASEDIR}/build.log 2>&1
    echo ${EXTRACT_RC}
    return
  fi

  echo -e "DEBUG: Extract completed. Copying library source to ${GPL_LIB_SOURCE_PATH}\n" 1>>${BASEDIR}/build.log 2>&1

  COPY_COMMAND="cp -r ${MOBILE_FFMPEG_TMPDIR}/${GPL_LIB_ORIG_DIR} ${GPL_LIB_SOURCE_PATH}"

  ${COPY_COMMAND} 1>>${BASEDIR}/build.log 2>&1

  local COPY_RC=$?

  if [ ${COPY_RC} -eq 0 ]; then
    echo -e "DEBUG: Downloading GPL library source $1 completed successfully\n" 1>>${BASEDIR}/build.log 2>&1
  else
    echo -e "\nINFO: Downloading GPL library $1 failed. Copying library source to ${GPL_LIB_SOURCE_PATH} completed with rc=${COPY_RC}\n" 1>>${BASEDIR}/build.log 2>&1
    rm -rf ${GPL_LIB_SOURCE_PATH} 1>>${BASEDIR}/build.log 2>&1
    echo ${COPY_RC}
    return
  fi
}

get_cpu_count() {
  if [ "$(uname)" == "Darwin" ]; then
    echo $(sysctl -n hw.physicalcpu)
  else
    echo $(nproc)
  fi
}

#
# 1. <lib name>
#
library_is_downloaded() {
  local LOCAL_PATH
  local LIB_NAME=$1
  local FILE_COUNT
  local REDOWNLOAD_VARIABLE
  REDOWNLOAD_VARIABLE=$(echo "REDOWNLOAD_$1" | sed "s/\-/\_/g")

  LOCAL_PATH=${BASEDIR}/src/${LIB_NAME}

  echo -e "DEBUG: Checking if ${LIB_NAME} is already downloaded at ${LOCAL_PATH}\n" 1>>${BASEDIR}/build.log 2>&1

  if [ ! -d ${LOCAL_PATH} ]; then
    echo -e "DEBUG: ${LOCAL_PATH} directory not found\n" 1>>${BASEDIR}/build.log 2>&1
    echo 1
    return
  fi

  FILE_COUNT=$(ls -l ${LOCAL_PATH} | wc -l)

  if [[ ${FILE_COUNT} -eq 0 ]]; then
    echo -e "DEBUG: No files found under ${LOCAL_PATH}\n" 1>>${BASEDIR}/build.log 2>&1
    echo 1
    return
  fi

  if [[ ${REDOWNLOAD_VARIABLE} -eq 1 ]]; then
    echo -e "INFO: ${LIB_NAME} library already downloaded but re-download requested\n" 1>>${BASEDIR}/build.log 2>&1
    rm -rf ${LOCAL_PATH} 1>>${BASEDIR}/build.log 2>&1
    echo 1
  else
    echo -e "INFO: ${LIB_NAME} library already downloaded\n" 1>>${BASEDIR}/build.log 2>&1
    echo 0
  fi
}

library_is_installed() {
  local INSTALL_PATH=$1
  local LIB_NAME=$2
  local HEADER_COUNT
  local LIB_COUNT

  echo -e "DEBUG: Checking if ${LIB_NAME} is already built and installed at ${INSTALL_PATH}/${LIB_NAME}\n" 1>>${BASEDIR}/build.log 2>&1

  if [ ! -d ${INSTALL_PATH}/${LIB_NAME} ]; then
    echo -e "DEBUG: ${INSTALL_PATH}/${LIB_NAME} directory not found\n" 1>>${BASEDIR}/build.log 2>&1
    echo 1
    return
  fi

  if [ ! -d ${INSTALL_PATH}/${LIB_NAME}/lib ]; then
    echo -e "DEBUG: ${INSTALL_PATH}/${LIB_NAME}/lib directory not found\n" 1>>${BASEDIR}/build.log 2>&1
    echo 1
    return
  fi

  if [ ! -d ${INSTALL_PATH}/${LIB_NAME}/include ]; then
    echo -e "DEBUG: ${INSTALL_PATH}/${LIB_NAME}/include directory not found\n" 1>>${BASEDIR}/build.log 2>&1
    echo 1
    return
  fi

  HEADER_COUNT=$(ls -l ${INSTALL_PATH}/${LIB_NAME}/include | wc -l)
  LIB_COUNT=$(ls -l ${INSTALL_PATH}/${LIB_NAME}/lib | wc -l)

  if [[ ${HEADER_COUNT} -eq 0 ]]; then
    echo -e "DEBUG: No headers found under ${INSTALL_PATH}/${LIB_NAME}/include\n" 1>>${BASEDIR}/build.log 2>&1
    echo 1
    return
  fi

  if [[ ${LIB_COUNT} -eq 0 ]]; then
    echo -e "DEBUG: No libraries found under ${INSTALL_PATH}/${LIB_NAME}/lib\n" 1>>${BASEDIR}/build.log 2>&1
    echo 1
    return
  fi

  echo -e "INFO: ${LIB_NAME} library is already built and installed\n" 1>>${BASEDIR}/build.log 2>&1

  echo 0
}

prepare_inline_sed() {
  if [ "$(uname)" == "Darwin" ]; then
    export SED_INLINE="sed -i .tmp"
  else
    export SED_INLINE="sed -i"
  fi
}

to_capital_case() {
  echo "$(echo ${1:0:1} | tr '[a-z]' '[A-Z]')${1:1}"
}

ios-common.sh:

    #!/bin/bash

source "${BASEDIR}/build/arch-common.sh"
IOS_MIN_VERSION=12
get_library_name() {
    case $1 in
        0) echo "fontconfig" ;;
        1) echo "freetype" ;;
        2) echo "fribidi" ;;
        3) echo "gmp" ;;
        4) echo "gnutls" ;;
        5) echo "lame" ;;
        6) echo "libass" ;;
        7) echo "libtheora" ;;
        8) echo "libvorbis" ;;
        9) echo "libvpx" ;;
        10) echo "libwebp" ;;
        11) echo "libxml2" ;;
        12) echo "opencore-amr" ;;
        13) echo "shine" ;;
        14) echo "speex" ;;
        15) echo "wavpack" ;;
        16) echo "kvazaar" ;;
        17) echo "x264" ;;
        18) echo "xvidcore" ;;
        19) echo "x265" ;;
        20) echo "libvidstab" ;;
        21) echo "rubberband" ;;
        22) echo "libilbc" ;;
        23) echo "opus" ;;
        24) echo "snappy" ;;
        25) echo "soxr" ;;
        26) echo "libaom" ;;
        27) echo "chromaprint" ;;
        28) echo "twolame" ;;
        29) echo "sdl" ;;
        30) echo "tesseract" ;;
        31) echo "openh264" ;;
        32) echo "vo-amrwbenc" ;;
        33) echo "giflib" ;;
        34) echo "jpeg" ;;
        35) echo "libogg" ;;
        36) echo "libpng" ;;
        37) echo "nettle" ;;
        38) echo "tiff" ;;
        39) echo "expat" ;;
        40) echo "libsndfile" ;;
        41) echo "leptonica" ;;
        42) echo "libsamplerate" ;;
        43) echo "ios-zlib" ;;
        44) echo "ios-audiotoolbox" ;;
        45) echo "ios-bzip2" ;;
        46) echo "ios-videotoolbox" ;;
        47) echo "ios-avfoundation" ;;
        48) echo "ios-libiconv" ;;
        49) echo "ios-libuuid" ;;
    esac
}

get_package_config_file_name() {
    case $1 in
        1) echo "freetype2" ;;
        5) echo "libmp3lame" ;;
        7) echo "theora" ;;
        8) echo "vorbis" ;;
        9) echo "vpx" ;;
        11) echo "libxml-2.0" ;;
        12) echo "opencore-amrnb" ;;
        20) echo "vidstab" ;;
        26) echo "aom" ;;
        27) echo "libchromaprint" ;;
        29) echo "sdl2" ;;
        34) echo "libjpeg" ;;
        35) echo "ogg" ;;
        38) echo "libtiff-4" ;;
        40) echo "sndfile" ;;
        41) echo "lept" ;;
        42) echo "samplerate" ;;
        50) echo "uuid" ;;
        *) echo $(get_library_name $1)
    esac
}

get_static_archive_name() {
    case $1 in
        5) echo "libmp3lame.a" ;;
        6) echo "libass.a" ;;
        9) echo "libvpx.a" ;;
        11) echo "libxml2.a" ;;
        20) echo "libvidstab.a" ;;
        22) echo "libilbc.a" ;;
        26) echo "libaom.a" ;;
        28) echo "libtwolame.a" ;;
        29) echo "libSDL2.a" ;;
        30) echo "libtesseract.a" ;;
        33) echo "libgif.a" ;;
        35) echo "libogg.a" ;;
        36) echo "libpng.a" ;;
        40) echo "libsndfile.a" ;;
        41) echo "liblept.a" ;;
        42) echo "libsamplerate.a" ;;
        *) echo lib$(get_library_name $1).a
    esac
}

get_arch_name() {
    case $1 in
        0) echo "armv7" ;;
        1) echo "armv7s" ;;
        2) echo "arm64" ;;
        3) echo "arm64e" ;;
        4) echo "i386" ;;
        5) echo "x86-64" ;;
        6) echo "x86-64-mac-catalyst" ;;
    esac
}

get_target_host() {
      case ${ARCH} in
        x86-64-mac-catalyst)
            echo "x86_64-apple-ios13.0-macabi"
        ;;
        *)
            echo "$(get_target_arch)-ios-darwin"
        ;;
    esac
}

get_build_host() {
    echo "$(get_target_arch)-ios-darwin"
}

get_target_build_directory() {
    case ${ARCH} in
        x86-64)
            echo "ios-x86_64"
        ;;
        x86-64-mac-catalyst)
            echo "ios-x86_64-mac-catalyst"
        ;;
        *)
            echo "ios-${ARCH}"
        ;;
    esac
}

get_target_arch() {
    case ${ARCH} in
        arm64 | arm64e)
            echo "aarch64"
        ;;
        x86-64 | x86-64-mac-catalyst)
            echo "x86_64"
        ;;
        *)
            echo "${ARCH}"
        ;;
    esac
}

get_target_sdk() {
    echo "$(get_target_arch)-apple-ios${IOS_MIN_VERSION}"
}

get_sdk_name() {
    case ${ARCH} in
        armv7 | armv7s | arm64 | arm64e)
            echo "iphoneos"
        ;;
        i386 | x86-64)
            echo "iphonesimulator"
        ;;
        x86-64-mac-catalyst)
            echo "macosx"
        ;;
    esac
}

get_sdk_path() {
    echo "$(xcrun --sdk $(get_sdk_name) --show-sdk-path)"
}

get_min_version_cflags() {
    case ${ARCH} in
        armv7 | armv7s | arm64 | arm64e)
            echo "-miphoneos-version-min=${IOS_MIN_VERSION}"
        ;;
        i386 | x86-64)
            echo "-mios-simulator-version-min=${IOS_MIN_VERSION}"
        ;;
        x86-64-mac-catalyst)
            echo "-miphoneos-version-min=13.0"
        ;;
    esac
}

get_common_includes() {
    echo "-I${SDK_PATH}/usr/include"
}

get_common_cflags() {
    if [[ ! -z ${MOBILE_FFMPEG_LTS_BUILD} ]]; then
        local LTS_BUILD_FLAG="-DMOBILE_FFMPEG_LTS "
    fi

    local BUILD_DATE="-DMOBILE_FFMPEG_BUILD_DATE=$(date +%Y%m%d 2>>${BASEDIR}/build.log)"

    case ${ARCH} in
        i386 | x86-64)
            echo "-fstrict-aliasing -DIOS ${LTS_BUILD_FLAG}${BUILD_DATE} -isysroot ${SDK_PATH}"
        ;;
        x86-64-mac-catalyst)
            echo "-fstrict-aliasing -fembed-bitcode ${LTS_BUILD_FLAG}${BUILD_DATE} -isysroot ${SDK_PATH}"
        ;;
        *)
            echo "-fstrict-aliasing -fembed-bitcode -DIOS ${LTS_BUILD_FLAG}${BUILD_DATE} -isysroot ${SDK_PATH}"
        ;;
    esac
}

get_arch_specific_cflags() {
    case ${ARCH} in
        armv7)
            echo "-arch armv7 -target $(get_target_host) -march=armv7 -mcpu=cortex-a8 -mfpu=neon -mfloat-abi=softfp -DMOBILE_FFMPEG_ARMV7"
        ;;
        armv7s)
            echo "-arch armv7s -target $(get_target_host) -march=armv7s -mcpu=generic -mfpu=neon -mfloat-abi=softfp -DMOBILE_FFMPEG_ARMV7S"
        ;;
        arm64)
            echo "-arch arm64 -target $(get_target_host) -march=armv8-a+crc+crypto -mcpu=generic -DMOBILE_FFMPEG_ARM64"
        ;;
        arm64e)
            echo "-arch arm64e -target $(get_target_host) -march=armv8.3-a+crc+crypto -mcpu=generic -DMOBILE_FFMPEG_ARM64E"
        ;;
        i386)
            echo "-arch i386 -target $(get_target_host) -march=i386 -mtune=intel -mssse3 -mfpmath=sse -m32 -DMOBILE_FFMPEG_I386"
        ;;
        x86-64)
            echo "-arch x86_64 -target $(get_target_host) -march=x86-64 -msse4.2 -mpopcnt -m64 -mtune=intel -DMOBILE_FFMPEG_X86_64"
        ;;
        x86-64-mac-catalyst)
            echo "-arch x86_64 -target $(get_target_host) -march=x86-64 -msse4.2 -mpopcnt -m64 -mtune=intel -DMOBILE_FFMPEG_X86_64_MAC_CATALYST -isysroot ${SDK_PATH} -isystem ${SDK_PATH}/System/iOSSupport/usr/include -iframework ${SDK_PATH}/System/iOSSupport/System/Library/Frameworks"
        ;;
    esac
}

get_size_optimization_cflags() {

    local ARCH_OPTIMIZATION=""
    case ${ARCH} in
        armv7 | armv7s | arm64 | arm64e | x86-64-mac-catalyst)
          ARCH_OPTIMIZATION="-Oz -Wno-ignored-optimization-argument"
        ;;
        i386 | x86-64)
          ARCH_OPTIMIZATION="-O2 -Wno-ignored-optimization-argument"
        ;;
    esac

    echo "${ARCH_OPTIMIZATION}"
}

get_size_optimization_asm_cflags() {

    local ARCH_OPTIMIZATION=""
    case $1 in
        jpeg | ffmpeg)
            case ${ARCH} in
                armv7 | armv7s | arm64 | arm64e | x86-64-mac-catalyst)
                    ARCH_OPTIMIZATION="-Oz"
                ;;
                i386 | x86-64)
                    ARCH_OPTIMIZATION="-O2"
                ;;
            esac
        ;;
        *)
            ARCH_OPTIMIZATION=$(get_size_optimization_cflags $1)
        ;;
    esac

    echo "${ARCH_OPTIMIZATION}"
}

get_app_specific_cflags() {

    local APP_FLAGS=""
    case $1 in
        fontconfig)
            case ${ARCH} in
                armv7 | armv7s | arm64 | arm64e)
                    APP_FLAGS="-std=c99 -Wno-unused-function -D__IPHONE_OS_MIN_REQUIRED -D__IPHONE_VERSION_MIN_REQUIRED=30000"
                ;;
                *)
                    APP_FLAGS="-std=c99 -Wno-unused-function"
                ;;
            esac
        ;;
        ffmpeg)
            APP_FLAGS="-Wno-unused-function -Wno-deprecated-declarations"
        ;;
        jpeg)
            APP_FLAGS="-Wno-nullability-completeness"
        ;;
        kvazaar)
            APP_FLAGS="-std=gnu99 -Wno-unused-function"
        ;;
        leptonica)
            APP_FLAGS="-std=c99 -Wno-unused-function -DOS_IOS"
        ;;
        libwebp | xvidcore)
            APP_FLAGS="-fno-common -DPIC"
        ;;
        mobile-ffmpeg)
            APP_FLAGS="-std=c99 -Wno-unused-function -Wall -Wno-deprecated-declarations -Wno-pointer-sign -Wno-switch -Wno-unused-result -Wno-unused-variable -DPIC -fobjc-arc"
        ;;
        sdl2)
            APP_FLAGS="-DPIC -Wno-unused-function -D__IPHONEOS__"
        ;;
        shine)
            APP_FLAGS="-Wno-unused-function"
        ;;
        soxr | snappy)
            APP_FLAGS="-std=gnu99 -Wno-unused-function -DPIC"
        ;;
        openh264 | x265)
            APP_FLAGS="-Wno-unused-function"
        ;;
        *)
            APP_FLAGS="-std=c99 -Wno-unused-function"
        ;;
    esac

    echo "${APP_FLAGS}"
}

get_cflags() {
    local ARCH_FLAGS=$(get_arch_specific_cflags)
    local APP_FLAGS=$(get_app_specific_cflags $1)
    local COMMON_FLAGS=$(get_common_cflags)
    if [[ -z ${MOBILE_FFMPEG_DEBUG} ]]; then
        local OPTIMIZATION_FLAGS=$(get_size_optimization_cflags $1)
    else
        local OPTIMIZATION_FLAGS="${MOBILE_FFMPEG_DEBUG}"
    fi
    local MIN_VERSION_FLAGS=$(get_min_version_cflags $1)
    local COMMON_INCLUDES=$(get_common_includes)

    echo "${ARCH_FLAGS} ${APP_FLAGS} ${COMMON_FLAGS} ${OPTIMIZATION_FLAGS} ${MIN_VERSION_FLAGS} ${COMMON_INCLUDES}"
}

get_asmflags() {
    local ARCH_FLAGS=$(get_arch_specific_cflags)
    local APP_FLAGS=$(get_app_specific_cflags $1)
    local COMMON_FLAGS=$(get_common_cflags)
    if [[ -z ${MOBILE_FFMPEG_DEBUG} ]]; then
        local OPTIMIZATION_FLAGS=$(get_size_optimization_asm_cflags $1)
    else
        local OPTIMIZATION_FLAGS="${MOBILE_FFMPEG_DEBUG}"
    fi
    local MIN_VERSION_FLAGS=$(get_min_version_cflags $1)
    local COMMON_INCLUDES=$(get_common_includes)

    echo "${ARCH_FLAGS} ${APP_FLAGS} ${COMMON_FLAGS} ${OPTIMIZATION_FLAGS} ${MIN_VERSION_FLAGS} ${COMMON_INCLUDES}"
}

get_cxxflags() {
    local COMMON_CFLAGS="$(get_common_cflags $1) $(get_common_includes $1) $(get_arch_specific_cflags) $(get_min_version_cflags $1)"
    if [[ -z ${MOBILE_FFMPEG_DEBUG} ]]; then
        local OPTIMIZATION_FLAGS="-Oz"
    else
        local OPTIMIZATION_FLAGS="${MOBILE_FFMPEG_DEBUG}"
    fi

    local BITCODE_FLAGS=""
    case ${ARCH} in
        armv7 | armv7s | arm64 | arm64e | x86-64-mac-catalyst)
            local BITCODE_FLAGS="-fembed-bitcode"
        ;;
    esac

    case $1 in
        x265)
            echo "-std=c++11 -fno-exceptions ${BITCODE_FLAGS} ${COMMON_CFLAGS}"
        ;;
        gnutls)
            echo "-std=c++11 -fno-rtti ${BITCODE_FLAGS} ${COMMON_CFLAGS} ${OPTIMIZATION_FLAGS}"
        ;;
        libwebp | xvidcore)
            echo "-std=c++11 -fno-exceptions -fno-rtti ${BITCODE_FLAGS} -fno-common -DPIC ${COMMON_CFLAGS} ${OPTIMIZATION_FLAGS}"
        ;;
        libaom)
            echo "-std=c++11 -fno-exceptions ${BITCODE_FLAGS} ${COMMON_CFLAGS} ${OPTIMIZATION_FLAGS}"
        ;;
        opencore-amr)
            echo "-fno-rtti ${BITCODE_FLAGS} ${COMMON_CFLAGS} ${OPTIMIZATION_FLAGS}"
        ;;
        rubberband)
            echo "-fno-rtti ${BITCODE_FLAGS} ${COMMON_CFLAGS} ${OPTIMIZATION_FLAGS}"
        ;;
        *)
            echo "-std=c++11 -fno-exceptions -fno-rtti ${BITCODE_FLAGS} ${COMMON_CFLAGS} ${OPTIMIZATION_FLAGS}"
        ;;
    esac
}

get_common_linked_libraries() {
    echo "-L${SDK_PATH}/usr/lib -lc++"
}

get_common_ldflags() {
    echo "-isysroot ${SDK_PATH}"
}

get_size_optimization_ldflags() {
    case ${ARCH} in
        armv7 | armv7s | arm64 | arm64e | x86-64-mac-catalyst)
            case $1 in
                ffmpeg | mobile-ffmpeg)
                    echo "-Oz -dead_strip"
                ;;
                *)
                    echo "-Oz -dead_strip"
                ;;
            esac
        ;;
        *)
            case $1 in
                ffmpeg)
                    echo "-O2"
                ;;
                *)
                    echo "-O2"
                ;;
            esac
        ;;
    esac
}

get_arch_specific_ldflags() {
    case ${ARCH} in
        armv7)
            echo "-arch armv7 -march=armv7 -mfpu=neon -mfloat-abi=softfp -fembed-bitcode -target $(get_target_host)"
        ;;
        armv7s)
            echo "-arch armv7s -march=armv7s -mfpu=neon -mfloat-abi=softfp -fembed-bitcode -target $(get_target_host)"
        ;;
        arm64)
            echo "-arch arm64 -march=armv8-a+crc+crypto -fembed-bitcode -target $(get_target_host)"
        ;;
        arm64e)
            echo "-arch arm64e -march=armv8.3-a+crc+crypto -fembed-bitcode -target $(get_target_host)"
        ;;
        i386)
            echo "-arch i386 -march=i386 -target $(get_target_host)"
        ;;
        x86-64)
            echo "-arch x86_64 -march=x86-64 -target $(get_target_host)"
        ;;
        x86-64-mac-catalyst)
            echo "-arch x86_64 -march=x86-64 -target $(get_target_host) -isysroot ${SDK_PATH} -L${SDK_PATH}/System/iOSSupport/usr/lib -iframework ${SDK_PATH}/System/iOSSupport/System/Library/Frameworks"
        ;;
    esac
}

get_ldflags() {
    local ARCH_FLAGS=$(get_arch_specific_ldflags)
    local LINKED_LIBRARIES=$(get_common_linked_libraries)
    if [[ -z ${MOBILE_FFMPEG_DEBUG} ]]; then
        local OPTIMIZATION_FLAGS="$(get_size_optimization_ldflags $1)"
    else
        local OPTIMIZATION_FLAGS="${MOBILE_FFMPEG_DEBUG}"
    fi
    local COMMON_FLAGS=$(get_common_ldflags)

    case $1 in
        mobile-ffmpeg)
            case ${ARCH} in
                armv7 | armv7s | arm64 | arm64e | x86-64-mac-catalyst)
                    echo "${ARCH_FLAGS} ${LINKED_LIBRARIES} ${COMMON_FLAGS} -fembed-bitcode -Wc,-fembed-bitcode ${OPTIMIZATION_FLAGS}"
                ;;
                *)
                    echo "${ARCH_FLAGS} ${LINKED_LIBRARIES} ${COMMON_FLAGS} ${OPTIMIZATION_FLAGS}"
                ;;
            esac
        ;;
        *)
            echo "${ARCH_FLAGS} ${LINKED_LIBRARIES} ${COMMON_FLAGS} ${OPTIMIZATION_FLAGS}"
        ;;
    esac
}

create_fontconfig_package_config() {
    local FONTCONFIG_VERSION="$1"

    cat > "${INSTALL_PKG_CONFIG_DIR}/fontconfig.pc" << EOF
prefix=${BASEDIR}/prebuilt/$(get_target_build_directory)/fontconfig
exec_prefix=\${prefix}
libdir=\${exec_prefix}/lib
includedir=\${prefix}/include
sysconfdir=\${prefix}/etc
localstatedir=\${prefix}/var
PACKAGE=fontconfig
confdir=\${sysconfdir}/fonts
cachedir=\${localstatedir}/cache/\${PACKAGE}

Name: Fontconfig
Description: Font configuration and customization library
Version: ${FONTCONFIG_VERSION}
Requires:  freetype2 >= 21.0.15, uuid, expat >= 2.2.0, libiconv
Requires.private:
Libs: -L\${libdir} -lfontconfig
Libs.private:
Cflags: -I\${includedir}
EOF
}

create_freetype_package_config() {
    local FREETYPE_VERSION="$1"

    cat > "${INSTALL_PKG_CONFIG_DIR}/freetype2.pc" << EOF
prefix=${BASEDIR}/prebuilt/$(get_target_build_directory)/freetype
exec_prefix=\${prefix}
libdir=\${exec_prefix}/lib
includedir=\${prefix}/include

Name: FreeType 2
URL: https://freetype.org
Description: A free, high-quality, and portable font engine.
Version: ${FREETYPE_VERSION}
Requires: libpng
Requires.private:
Libs: -L\${libdir} -lfreetype
Libs.private:
Cflags: -I\${includedir}/freetype2
EOF
}

create_giflib_package_config() {
    local GIFLIB_VERSION="$1"

    cat > "${INSTALL_PKG_CONFIG_DIR}/giflib.pc" << EOF
prefix=${BASEDIR}/prebuilt/$(get_target_build_directory)/giflib
exec_prefix=\${prefix}
libdir=\${prefix}/lib
includedir=\${prefix}/include

Name: giflib
Description: gif library
Version: ${GIFLIB_VERSION}

Requires:
Libs: -L\${libdir} -lgif
Cflags: -I\${includedir}
EOF
}

create_gmp_package_config() {
    local GMP_VERSION="$1"

    cat > "${INSTALL_PKG_CONFIG_DIR}/gmp.pc" << EOF
prefix=${BASEDIR}/prebuilt/$(get_target_build_directory)/gmp
exec_prefix=\${prefix}
libdir=\${prefix}/lib
includedir=\${prefix}/include

Name: gmp
Description: gnu mp library
Version: ${GMP_VERSION}

Requires:
Libs: -L\${libdir} -lgmp
Cflags: -I\${includedir}
EOF
}

create_gnutls_package_config() {
    local GNUTLS_VERSION="$1"

    cat > "${INSTALL_PKG_CONFIG_DIR}/gnutls.pc" << EOF
prefix=${BASEDIR}/prebuilt/$(get_target_build_directory)/gnutls
exec_prefix=\${prefix}
libdir=\${exec_prefix}/lib
includedir=\${prefix}/include

Name: gnutls
Description: GNU TLS Implementation

Version: ${GNUTLS_VERSION}
Requires: nettle, hogweed
Cflags: -I\${includedir}
Libs: -L\${libdir} -lgnutls
Libs.private: -lgmp
EOF
}

create_libmp3lame_package_config() {
    local LAME_VERSION="$1"

    cat > "${INSTALL_PKG_CONFIG_DIR}/libmp3lame.pc" << EOF
prefix=${BASEDIR}/prebuilt/$(get_target_build_directory)/lame
exec_prefix=\${prefix}
libdir=\${exec_prefix}/lib
includedir=\${prefix}/include

Name: libmp3lame
Description: lame mp3 encoder library
Version: ${LAME_VERSION}

Requires:
Libs: -L\${libdir} -lmp3lame
Cflags: -I\${includedir}
EOF
}

create_libiconv_system_package_config() {
    local LIB_ICONV_VERSION=$(grep '_LIBICONV_VERSION' ${SDK_PATH}/usr/include/iconv.h | grep -Eo '0x.*' | grep -Eo '.*    ')

    cat > "${INSTALL_PKG_CONFIG_DIR}/libiconv.pc" << EOF
prefix=${SDK_PATH}/usr
exec_prefix=\${prefix}
libdir=\${exec_prefix}/lib
includedir=\${prefix}/include

Name: libiconv
Description: Character set conversion library
Version: ${LIB_ICONV_VERSION}

Requires:
Libs: -L\${libdir} -liconv -lcharset
Cflags: -I\${includedir}
EOF
}

create_libpng_package_config() {
    local LIBPNG_VERSION="$1"

    cat > "${INSTALL_PKG_CONFIG_DIR}/libpng.pc" << EOF
prefix=${BASEDIR}/prebuilt/$(get_target_build_directory)/libpng
exec_prefix=\${prefix}
libdir=\${exec_prefix}/lib
includedir=\${prefix}/include

Name: libpng
Description: Loads and saves PNG files
Version: ${LIBPNG_VERSION}
Requires:
Cflags: -I\${includedir}
Libs: -L\${libdir} -lpng
EOF
}

create_libvorbis_package_config() {
    local LIBVORBIS_VERSION="$1"

    cat > "${INSTALL_PKG_CONFIG_DIR}/vorbis.pc" << EOF
prefix=${BASEDIR}/prebuilt/$(get_target_build_directory)/libvorbis
exec_prefix=\${prefix}
libdir=\${prefix}/lib
includedir=\${prefix}/include

Name: vorbis
Description: vorbis is the primary Ogg Vorbis library
Version: ${LIBVORBIS_VERSION}

Requires: ogg
Libs: -L\${libdir} -lvorbis -lm
Cflags: -I\${includedir}
EOF

cat > "${INSTALL_PKG_CONFIG_DIR}/vorbisenc.pc" << EOF
prefix=${BASEDIR}/prebuilt/$(get_target_build_directory)/libvorbis
exec_prefix=\${prefix}
libdir=\${prefix}/lib
includedir=\${prefix}/include

Name: vorbisenc
Description: vorbisenc is a library that provides a convenient API for setting up an encoding environment using libvorbis
Version: ${LIBVORBIS_VERSION}

Requires: vorbis
Conflicts:
Libs: -L\${libdir} -lvorbisenc
Cflags: -I\${includedir}
EOF

cat > "${INSTALL_PKG_CONFIG_DIR}/vorbisfile.pc" << EOF
prefix=${BASEDIR}/prebuilt/$(get_target_build_directory)/libvorbis
exec_prefix=\${prefix}
libdir=\${prefix}/lib
includedir=\${prefix}/include

Name: vorbisfile
Description: vorbisfile is a library that provides a convenient high-level API for decoding and basic manipulation of all Vorbis I audio streams
Version: ${LIBVORBIS_VERSION}

Requires: vorbis
Conflicts:
Libs: -L\${libdir} -lvorbisfile
Cflags: -I\${includedir}
EOF
}

create_libxml2_package_config() {
    local LIBXML2_VERSION="$1"

    cat > "${INSTALL_PKG_CONFIG_DIR}/libxml-2.0.pc" << EOF
prefix=${BASEDIR}/prebuilt/$(get_target_build_directory)/libxml2
exec_prefix=\${prefix}
libdir=\${exec_prefix}/lib
includedir=\${prefix}/include
modules=1

Name: libXML
Version: ${LIBXML2_VERSION}
Description: libXML library version2.
Requires: libiconv
Libs: -L\${libdir} -lxml2
Libs.private:   -lz -lm
Cflags: -I\${includedir} -I\${includedir}/libxml2
EOF
}

create_snappy_package_config() {
    local SNAPPY_VERSION="$1"

    cat > "${INSTALL_PKG_CONFIG_DIR}/snappy.pc" << EOF
prefix=${BASEDIR}/prebuilt/$(get_target_build_directory)/snappy
exec_prefix=\${prefix}
libdir=\${prefix}/lib
includedir=\${prefix}/include

Name: snappy
Description: a fast compressor/decompressor
Version: ${SNAPPY_VERSION}

Requires:
Libs: -L\${libdir} -lz -lc++
Cflags: -I\${includedir}
EOF
}

create_soxr_package_config() {
    local SOXR_VERSION="$1"

    cat > "${INSTALL_PKG_CONFIG_DIR}/soxr.pc" << EOF
prefix=${BASEDIR}/prebuilt/$(get_target_build_directory)/soxr
exec_prefix=\${prefix}
libdir=\${prefix}/lib
includedir=\${prefix}/include

Name: soxr
Description: High quality, one-dimensional sample-rate conversion library
Version: ${SOXR_VERSION}

Requires:
Libs: -L\${libdir} -lsoxr
Cflags: -I\${includedir}
EOF
}

create_tesseract_package_config() {
    local TESSERACT_VERSION="$1"

    cat > "${INSTALL_PKG_CONFIG_DIR}/tesseract.pc" << EOF
prefix=${BASEDIR}/prebuilt/$(get_target_build_directory)/tesseract
exec_prefix=\${prefix}
bindir=\${exec_prefix}/bin
datarootdir=\${prefix}/share
datadir=\${datarootdir}
libdir=\${exec_prefix}/lib
includedir=\${prefix}/include

Name: tesseract
Description: An OCR Engine that was developed at HP Labs between 1985 and 1995... and now at Google.
URL: https://github.com/tesseract-ocr/tesseract
Version: ${TESSERACT_VERSION}

Requires: lept
Libs: -L\${libdir} -ltesseract
Cflags: -I\${includedir}
EOF
}

create_libuuid_system_package_config() {
    local UUID_VERSION="$1"

    cat > "${INSTALL_PKG_CONFIG_DIR}/uuid.pc" << EOF
prefix=${SDK_PATH}
exec_prefix=\${prefix}
libdir=\${exec_prefix}/usr/lib
includedir=\${prefix}/include

Name: uuid
Description: Universally unique id library
Version: ${UUID_VERSION}
Requires:
Cflags: -I\${includedir}
Libs: -L\${libdir}
EOF
}

create_xvidcore_package_config() {
    local XVIDCORE_VERSION="$1"

    cat > "${INSTALL_PKG_CONFIG_DIR}/xvidcore.pc" << EOF
prefix=${BASEDIR}/prebuilt/$(get_target_build_directory)/xvidcore
exec_prefix=\${prefix}
libdir=\${prefix}/lib
includedir=\${prefix}/include

Name: xvidcore
Description: the main MPEG-4 de-/encoding library
Version: ${XVIDCORE_VERSION}

Requires:
Libs: -L\${libdir}
Cflags: -I\${includedir}
EOF
}

create_zlib_system_package_config() {
    ZLIB_VERSION=$(grep '#define ZLIB_VERSION' ${SDK_PATH}/usr/include/zlib.h | grep -Eo '\".*\"' | sed -e 's/\"//g')

    cat > "${INSTALL_PKG_CONFIG_DIR}/zlib.pc" << EOF
prefix=${SDK_PATH}/usr
exec_prefix=\${prefix}
libdir=\${exec_prefix}/lib
includedir=\${prefix}/include

Name: zlib
Description: zlib compression library
Version: ${ZLIB_VERSION}

Requires:
Libs: -L\${libdir} -lz
Cflags: -I\${includedir}
EOF
}

create_bzip2_system_package_config() {
    BZIP2_VERSION=$(grep -Eo 'version.*of' ${SDK_PATH}/usr/include/bzlib.h | sed -e 's/of//;s/version//g;s/\ //g')

    cat > "${INSTALL_PKG_CONFIG_DIR}/bzip2.pc" << EOF
prefix=${SDK_PATH}/usr
exec_prefix=\${prefix}
libdir=\${exec_prefix}/lib
includedir=\${prefix}/include

Name: bzip2
Description: library for lossless, block-sorting data compression
Version: ${BZIP2_VERSION}

Requires:
Libs: -L\${libdir} -lbz2
Cflags: -I\${includedir}
EOF
}

set_toolchain_clang_paths() {

    if [ ! -f "${MOBILE_FFMPEG_TMPDIR}/gas-preprocessor.pl" ]; then

        DOWNLOAD_RESULT=$(download "https://github.com/libav/gas-preprocessor/raw/master/gas-preprocessor.pl" "gas-preprocessor.pl" "exit")
        if [[ ${DOWNLOAD_RESULT} -ne 0 ]]; then
            echo 'tt'
            exit 1
        fi
        (chmod +x ${MOBILE_FFMPEG_TMPDIR}/gas-preprocessor.pl 1>>${BASEDIR}/build.log 2>&1) || exit 1

        # patch gas-preprocessor.pl against the following warning
        # Unescaped left brace in regex is deprecated here (and will be fatal in Perl 5.32), passed through in regex; marked by <-- HERE in m/(?:ld|st)\d\s+({ <-- HERE \s*v(\d+)\.(\d[bhsdBHSD])\s*-\s*v(\d+)\.(\d[bhsdBHSD])\s*})/ at /Users/taner/Projects/mobile-ffmpeg/.tmp/gas-preprocessor.pl line 1065.
        sed -i .tmp "s/s\+({/s\+(\\\\{/g;s/s\*})/s\*\\\\})/g" ${MOBILE_FFMPEG_TMPDIR}/gas-preprocessor.pl
    fi

    LOCAL_GAS_PREPROCESSOR="${MOBILE_FFMPEG_TMPDIR}/gas-preprocessor.pl"
    if [ "$1" == "x264" ]; then
        LOCAL_GAS_PREPROCESSOR="${BASEDIR}/src/x264/tools/gas-preprocessor.pl"
    fi

    export AR="$(xcrun --sdk $(get_sdk_name) -f ar)"
    export CC="$(xcrun --sdk $(get_sdk_name) -f clang)"
    export OBJC="$(xcrun --sdk $(get_sdk_name) -f clang)"
    export CXX="$(xcrun --sdk $(get_sdk_name) -f clang++)"

    LOCAL_ASMFLAGS="$(get_asmflags $1)"
    case ${ARCH} in
        armv7 | armv7s)
            if [ "$1" == "x265" ]; then
                export AS="${LOCAL_GAS_PREPROCESSOR}"
                export AS_ARGUMENTS="-arch arm"
                export ASM_FLAGS="${LOCAL_ASMFLAGS}"
            else
                export AS="${LOCAL_GAS_PREPROCESSOR} -arch arm -- ${CC} ${LOCAL_ASMFLAGS}"
            fi
        ;;
        arm64 | arm64e)
            if [ "$1" == "x265" ]; then
                export AS="${LOCAL_GAS_PREPROCESSOR}"
                export AS_ARGUMENTS="-arch aarch64"
                export ASM_FLAGS="${LOCAL_ASMFLAGS}"
            else
                export AS="${LOCAL_GAS_PREPROCESSOR} -arch aarch64 -- ${CC} ${LOCAL_ASMFLAGS}"
            fi
        ;;
        *)
            export AS="${CC} ${LOCAL_ASMFLAGS}"
        ;;
    esac

    export LD="$(xcrun --sdk $(get_sdk_name) -f ld)"
    export RANLIB="$(xcrun --sdk $(get_sdk_name) -f ranlib)"
    export STRIP="$(xcrun --sdk $(get_sdk_name) -f strip)"

    export INSTALL_PKG_CONFIG_DIR="${BASEDIR}/prebuilt/$(get_target_build_directory)/pkgconfig"
    export ZLIB_PACKAGE_CONFIG_PATH="${INSTALL_PKG_CONFIG_DIR}/zlib.pc"
    export BZIP2_PACKAGE_CONFIG_PATH="${INSTALL_PKG_CONFIG_DIR}/bzip2.pc"
    export LIB_ICONV_PACKAGE_CONFIG_PATH="${INSTALL_PKG_CONFIG_DIR}/libiconv.pc"
    export LIB_UUID_PACKAGE_CONFIG_PATH="${INSTALL_PKG_CONFIG_DIR}/uuid.pc"

    if [ ! -d ${INSTALL_PKG_CONFIG_DIR} ]; then
        mkdir -p ${INSTALL_PKG_CONFIG_DIR}
    fi

    if [ ! -f ${ZLIB_PACKAGE_CONFIG_PATH} ]; then
        create_zlib_system_package_config
    fi

    if [ ! -f ${LIB_ICONV_PACKAGE_CONFIG_PATH} ]; then
        create_libiconv_system_package_config
    fi

    if [ ! -f ${BZIP2_PACKAGE_CONFIG_PATH} ]; then
        create_bzip2_system_package_config
    fi

    if [ ! -f ${LIB_UUID_PACKAGE_CONFIG_PATH} ]; then
        create_libuuid_system_package_config
    fi

    prepare_inline_sed
}

2.1 libx264

先下载libx265代码

#!/bin/bash
ARCH=arm64
TARGET_SDK=iphoneos
SDK_PATH=`xcrun --show-sdk-path --sdk iphoneos`
BASEDIR=/Volumes/data/dev/ffmpeg/mobile/ios

if [[ -z ${ARCH} ]]; then
    echo -e "(*) ARCH not defined\n"
    exit 1
fi

if [[ -z ${TARGET_SDK} ]]; then
    echo -e "(*) TARGET_SDK not defined\n"
    exit 1
fi

if [[ -z ${SDK_PATH} ]]; then
    echo -e "(*) SDK_PATH not defined\n"
    exit 1
fi

if [[ -z ${BASEDIR} ]]; then
    echo -e "(*) BASEDIR not defined\n"
    exit 1
fi

if ! [ -x "$(command -v tar)" ]; then
    echo -e "(*) tar command not found\n"
    exit 1
fi

# ENABLE COMMON FUNCTIONS
if [[ ${APPLE_TVOS_BUILD} -eq 1 ]]; then
    . ${BASEDIR}/build/tvos-common.sh
else
    . ${BASEDIR}/build/ios-common.sh
fi

# PREPARE PATHS & DEFINE ${INSTALL_PKG_CONFIG_DIR}
LIB_NAME="x264"

set_toolchain_clang_paths ${LIB_NAME}

# PREPARING FLAGS
BUILD_HOST=$(get_build_host)
export CFLAGS=$(get_cflags ${LIB_NAME})
export CXXFLAGS=$(get_cxxflags ${LIB_NAME})
export LDFLAGS=$(get_ldflags ${LIB_NAME})

testt=${BASEDIR}/src/${LIB_NAME}

echo '111ttdc'
cd ${BASEDIR}/src/${LIB_NAME} || exit 1

make distclean 2>/dev/null 1>/dev/null
# make distclean
# echo 'test'
ASM_FLAS=""
case ${ARCH} in
    i386 | x86-64 | x86-64-mac-catalyst)
        ASM_FLAGS="--disable-asm"

        if ! [ -x "$(command -v nasm)" ]; then
            echo -e "(*) nasm command not found\n"
            exit 1
        fi

        export AS="$(command -v nasm)"
    ;;
esac

# DISABLE INLINE -arch DEFINITIONS
${SED_INLINE} 's/CFLAGS=\"\$CFLAGS \-arch x86_64/CFLAGS=\"\$CFLAGS/g' configure
${SED_INLINE} 's/LDFLAGS=\"\$LDFLAGS \-arch x86_64/LDFLAGS=\"\$CFLAGS/g' configure

# echo $CC
echo "./configure \
    --prefix=${BASEDIR}/prebuilt/$(get_target_build_directory)/${LIB_NAME} \
    --enable-pic \
    --sysroot=${SDK_PATH} \
    --enable-static \
    ${ASM_FLAGS} \
    --disable-cli \
    --host=${BUILD_HOST} "
cd /Volumes/data/dev/ffmpeg/mobile/ios/src/x264
./configure \
    --prefix=${BASEDIR}/prebuilt/$(get_target_build_directory)/${LIB_NAME} \
    --enable-pic \
    --sysroot=${SDK_PATH} \
    --enable-static \
    ${ASM_FLAGS} \
    --disable-cli \
    --host=${BUILD_HOST} || exit 1

make -j$(get_cpu_count) || exit 1

# MANUALLY COPY PKG-CONFIG FILES
cp x264.pc ${INSTALL_PKG_CONFIG_DIR} || exit 1

make install || exit 1

2.3 libx265

依赖cmake

    #!/bin/bash
ARCH=arm64
TARGET_SDK=iphoneos
SDK_PATH=`xcrun --show-sdk-path --sdk iphoneos`
BASEDIR=/Volumes/data/dev/ffmpeg/mobile/ios
if [[ -z ${ARCH} ]]; then
    echo -e "(*) ARCH not defined\n"
    exit 1
fi

if [[ -z ${TARGET_SDK} ]]; then
    echo -e "(*) TARGET_SDK not defined\n"
    exit 1
fi

if [[ -z ${SDK_PATH} ]]; then
    echo -e "(*) SDK_PATH not defined\n"
    exit 1
fi

if [[ -z ${BASEDIR} ]]; then
    echo -e "(*) BASEDIR not defined\n"
    exit 1
fi

# ENABLE COMMON FUNCTIONS
if [[ ${APPLE_TVOS_BUILD} -eq 1 ]]; then
    . ${BASEDIR}/build/tvos-common.sh
else
    . ${BASEDIR}/build/ios-common.sh
fi

# PREPARE PATHS & DEFINE ${INSTALL_PKG_CONFIG_DIR}
LIB_NAME="x265"
set_toolchain_clang_paths ${LIB_NAME}

# PREPARING FLAGS
BUILD_HOST=$(get_build_host)
CFLAGS=$(get_cflags ${LIB_NAME})
CXXFLAGS=$(get_cxxflags ${LIB_NAME})
LDFLAGS=$(get_ldflags ${LIB_NAME})

# USE CLEAN SOURCE ON EACH BUILD
cd ${BASEDIR}/src || exit 1
rm -rf ${LIB_NAME} || exit 1
DOWNLOAD_RESULT=$(download_gpl_library_source ${LIB_NAME})
if [[ ${DOWNLOAD_RESULT} -ne 0 ]]; then
    exit 1
fi
cd ${BASEDIR}/src/${LIB_NAME} || exit 1

ARCH_OPTIONS=""
case ${ARCH} in
    armv7 | armv7s)
        ARCH_OPTIONS="-DENABLE_ASSEMBLY=1 -DCROSS_COMPILE_ARM=1"
    ;;
    arm64 | arm64e)
        ARCH_OPTIONS="-DENABLE_ASSEMBLY=0 -DCROSS_COMPILE_ARM=1"
    ;;
    x86-64-mac-catalyst)
        ARCH_OPTIONS="-DENABLE_ASSEMBLY=0 -DCROSS_COMPILE_ARM=0"
    ;;
    *)
        ARCH_OPTIONS="-DENABLE_ASSEMBLY=1 -DCROSS_COMPILE_ARM=0"
    ;;
esac

if [ -d "cmake-build" ]; then
    rm -rf cmake-build
fi

mkdir cmake-build || exit 1
cd cmake-build || exit 1

# fix x86 and x86_64 assembly
${SED_INLINE} 's/win64/macho64 -DPREFIX/g' ${BASEDIR}/src/x265/source/cmake/CMakeASM_NASMInformation.cmake
${SED_INLINE} 's/win/macho/g' ${BASEDIR}/src/x265/source/cmake/CMakeASM_NASMInformation.cmake

# fixing constant shift
${SED_INLINE} 's/lsr 16/lsr #16/g' ${BASEDIR}/src/x265/source/common/arm/blockcopy8.S

# fixing leading underscores
${SED_INLINE} 's/function x265_/function _x265_/g' ${BASEDIR}/src/x265/source/common/arm/*.S
${SED_INLINE} 's/ x265_/ _x265_/g' ${BASEDIR}/src/x265/source/common/arm/pixel-util.S

# fixing relocation errors
${SED_INLINE} 's/sad12_mask:/sad12_mask_bytes:/g' ${BASEDIR}/src/x265/source/common/arm/sad-a.S
${SED_INLINE} 's/g_lumaFilter:/g_lumaFilter_bytes:/g' ${BASEDIR}/src/x265/source/common/arm/ipfilter8.S
${SED_INLINE} 's/g_chromaFilter:/g_chromaFilter_bytes:/g' ${BASEDIR}/src/x265/source/common/arm/ipfilter8.S
${SED_INLINE} 's/\.text/.equ sad12_mask, .-sad12_mask_bytes\
\
.text/g' ${BASEDIR}/src/x265/source/common/arm/sad-a.S
${SED_INLINE} 's/\.text/.equ g_lumaFilter, .-g_lumaFilter_bytes\
.equ g_chromaFilter, .-g_chromaFilter_bytes\
\
.text/g' ${BASEDIR}/src/x265/source/common/arm/ipfilter8.S

# using customized build file
rm -f ${BASEDIR}/src/${LIB_NAME}/source/CMakeLists.txt || exit 1
cp ${BASEDIR}/tools/cmake/CMakeLists.x265.ios.txt ${BASEDIR}/src/${LIB_NAME}/source/CMakeLists.txt || exit 1

echo ${CFLAGS}

cmake -Wno-dev \
    -DCMAKE_VERBOSE_MAKEFILE=0 \
    -DCMAKE_C_FLAGS="${CFLAGS}" \
    -DCMAKE_CXX_FLAGS="${CXXFLAGS}" \
    -DCMAKE_EXE_LINKER_FLAGS="${LDFLAGS}" \
    -DCMAKE_SYSROOT="${SDK_PATH}" \
    -DCMAKE_FIND_ROOT_PATH="${SDK_PATH}" \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX="${BASEDIR}/prebuilt/$(get_target_build_directory)/${LIB_NAME}" \
    -DCMAKE_SYSTEM_NAME=Generic \
    -DCMAKE_C_COMPILER="$CC" \
    -DCMAKE_CXX_COMPILER="$CXX" \
    -DCMAKE_LINKER="$LD" \
    -DCMAKE_AR="$(xcrun --sdk $(get_sdk_name) -f ar)" \
    -DCMAKE_AS="$AS" \
    -DSTATIC_LINK_CRT=1 \
    -DENABLE_PIC=1 \
    -DENABLE_CLI=0 \
    ${ARCH_OPTIONS} \
    -DCMAKE_SYSTEM_PROCESSOR=$(get_target_arch) \
    -DENABLE_SHARED=0 ../source || exit 1

make -j$(get_cpu_count) || exit 1

# MANUALLY COPY PKG-CONFIG FILES
cp x265.pc ${INSTALL_PKG_CONFIG_DIR} || exit 1

make install || exit 1

3 ffmpeg编译

    #!/bin/bash

ARCH=arm64
TARGET_SDK=iphoneos
SDK_PATH=`xcrun --show-sdk-path --sdk iphoneos`
BASEDIR=/Volumes/data/dev/ffmpeg/mobile/ios

if [[ -z ${ARCH} ]]; then
    echo -e "(*) ARCH not defined\n"
    exit 1
fi

if [[ -z ${TARGET_SDK} ]]; then
    echo -e "(*) TARGET_SDK not defined\n"
    exit 1
fi

if [[ -z ${SDK_PATH} ]]; then
    echo -e "(*) SDK_PATH not defined\n"
    exit 1
fi

if [[ -z ${BASEDIR} ]]; then
    echo -e "(*) BASEDIR not defined\n"
    exit 1
fi

if ! [ -x "$(command -v pkg-config)" ]; then
    echo -e "(*) pkg-config command not found\n"
    exit 1
fi

# ENABLE COMMON FUNCTIONS
if [[ ${APPLE_TVOS_BUILD} -eq 1 ]]; then
    . ${BASEDIR}/build/tvos-common.sh
else
    . ${BASEDIR}/build/ios-common.sh
fi

# PREPARE PATHS & DEFINE ${INSTALL_PKG_CONFIG_DIR}
LIB_NAME="ffmpeg"
set_toolchain_clang_paths ${LIB_NAME}

# PREPARING FLAGS
BUILD_HOST=$(get_build_host)
FFMPEG_CFLAGS=""
FFMPEG_LDFLAGS=""
export PKG_CONFIG_LIBDIR="${INSTALL_PKG_CONFIG_DIR}"

TARGET_CPU=""
TARGET_ARCH=""
BITCODE_FLAGS=""
NEON_FLAG=""
ARCH_OPTIONS="--enable-asm"
case ${ARCH} in
    armv7)
        TARGET_CPU="armv7"
        TARGET_ARCH="armv7"
        NEON_FLAG="    --enable-neon"
        BITCODE_FLAGS="-fembed-bitcode -Wc,-fembed-bitcode"
    ;;
    armv7s)
        TARGET_CPU="armv7s"
        TARGET_ARCH="armv7s"
        NEON_FLAG="    --enable-neon"
        BITCODE_FLAGS="-fembed-bitcode -Wc,-fembed-bitcode"
    ;;
    arm64)
        TARGET_CPU="armv8"
        TARGET_ARCH="aarch64"
        NEON_FLAG="    --enable-neon"
        BITCODE_FLAGS="-fembed-bitcode -Wc,-fembed-bitcode"
    ;;
    arm64e)
        TARGET_CPU="armv8.3-a"
        TARGET_ARCH="aarch64"
        NEON_FLAG="    --enable-neon"
        BITCODE_FLAGS="-fembed-bitcode -Wc,-fembed-bitcode"
    ;;
    i386)
        TARGET_CPU="i386"
        TARGET_ARCH="i386"
        NEON_FLAG="    --disable-neon"
        BITCODE_FLAGS=""
    ;;
    x86-64)
        ARCH_OPTIONS="--disable-asm"
        TARGET_CPU="x86_64"
        TARGET_ARCH="x86_64"
        NEON_FLAG="    --disable-neon"
        BITCODE_FLAGS=""
    ;;
    x86-64-mac-catalyst)
        ARCH_OPTIONS="--disable-asm"
        TARGET_CPU="x86_64"
        TARGET_ARCH="x86_64"
        NEON_FLAG="    --disable-neon"
        BITCODE_FLAGS="-fembed-bitcode -Wc,-fembed-bitcode"
    ;;
esac

library=1
while [[ ${library} -le 49 ]]
do
    if [[ ${!library} -eq 1 ]]; then
        ENABLED_LIBRARY=$(get_library_name $((library - 1)))
        echo -e "INFO: Enabling library ${ENABLED_LIBRARY}" 1>>${BASEDIR}/build.log 2>&1

        case ${ENABLED_LIBRARY} in
            chromaprint)
                FFMPEG_CFLAGS+=" $(pkg-config --cflags libchromaprint)"
                FFMPEG_LDFLAGS+=" $(pkg-config --libs --static libchromaprint)"
                CONFIGURE_POSTFIX+=" --enable-chromaprint"
            ;;
            fontconfig)
                FFMPEG_CFLAGS+=" $(pkg-config --cflags fontconfig)"
                FFMPEG_LDFLAGS+=" $(pkg-config --libs --static fontconfig)"
                CONFIGURE_POSTFIX+=" --enable-libfontconfig"
            ;;
            freetype)
                FFMPEG_CFLAGS+=" $(pkg-config --cflags freetype2)"
                FFMPEG_LDFLAGS+=" $(pkg-config --libs --static freetype2)"
                CONFIGURE_POSTFIX+=" --enable-libfreetype"
            ;;
            fribidi)
                FFMPEG_CFLAGS+=" $(pkg-config --cflags fribidi)"
                FFMPEG_LDFLAGS+=" $(pkg-config --libs --static fribidi)"
                CONFIGURE_POSTFIX+=" --enable-libfribidi"
            ;;
            gmp)
                FFMPEG_CFLAGS+=" $(pkg-config --cflags gmp)"
                FFMPEG_LDFLAGS+=" $(pkg-config --libs --static gmp)"
                CONFIGURE_POSTFIX+=" --enable-gmp"
            ;;
            gnutls)
                FFMPEG_CFLAGS+=" $(pkg-config --cflags gnutls)"
                FFMPEG_LDFLAGS+=" $(pkg-config --libs --static gnutls)"
                CONFIGURE_POSTFIX+=" --enable-gnutls"
            ;;
            kvazaar)
                FFMPEG_CFLAGS+=" $(pkg-config --cflags kvazaar)"
                FFMPEG_LDFLAGS+=" $(pkg-config --libs --static kvazaar)"
                CONFIGURE_POSTFIX+=" --enable-libkvazaar"
            ;;
            lame)
                FFMPEG_CFLAGS+=" $(pkg-config --cflags libmp3lame)"
                FFMPEG_LDFLAGS+=" $(pkg-config --libs --static libmp3lame)"
                CONFIGURE_POSTFIX+=" --enable-libmp3lame"
            ;;
            libaom)
                FFMPEG_CFLAGS+=" $(pkg-config --cflags aom)"
                FFMPEG_LDFLAGS+=" $(pkg-config --libs --static aom)"
                CONFIGURE_POSTFIX+=" --enable-libaom"
            ;;
            libass)
                FFMPEG_CFLAGS+=" $(pkg-config --cflags libass)"
                FFMPEG_LDFLAGS+=" $(pkg-config --libs --static libass)"
                CONFIGURE_POSTFIX+=" --enable-libass"
            ;;
            libilbc)
                FFMPEG_CFLAGS+=" $(pkg-config --cflags libilbc)"
                FFMPEG_LDFLAGS+=" $(pkg-config --libs --static libilbc)"
                CONFIGURE_POSTFIX+=" --enable-libilbc"
            ;;
            libtheora)
                FFMPEG_CFLAGS+=" $(pkg-config --cflags theora)"
                FFMPEG_LDFLAGS+=" $(pkg-config --libs --static theora)"
                CONFIGURE_POSTFIX+=" --enable-libtheora"
            ;;
            libvidstab)
                FFMPEG_CFLAGS+=" $(pkg-config --cflags vidstab)"
                FFMPEG_LDFLAGS+=" $(pkg-config --libs --static vidstab)"
                CONFIGURE_POSTFIX+=" --enable-libvidstab --enable-gpl"
            ;;
            libvorbis)
                FFMPEG_CFLAGS+=" $(pkg-config --cflags vorbis)"
                FFMPEG_LDFLAGS+=" $(pkg-config --libs --static vorbis)"
                CONFIGURE_POSTFIX+=" --enable-libvorbis"
            ;;
            libvpx)
                FFMPEG_CFLAGS+=" $(pkg-config --cflags vpx)"
                FFMPEG_LDFLAGS+=" $(pkg-config --libs --static vpx)"
                CONFIGURE_POSTFIX+=" --enable-libvpx"
            ;;
            libwebp)
                FFMPEG_CFLAGS+=" $(pkg-config --cflags libwebp)"
                FFMPEG_LDFLAGS+=" $(pkg-config --libs --static libwebp)"
                CONFIGURE_POSTFIX+=" --enable-libwebp"
            ;;
            libxml2)
                FFMPEG_CFLAGS+=" $(pkg-config --cflags libxml-2.0)"
                FFMPEG_LDFLAGS+=" $(pkg-config --libs --static libxml-2.0)"
                CONFIGURE_POSTFIX+=" --enable-libxml2"
            ;;
            opencore-amr)
                FFMPEG_CFLAGS+=" $(pkg-config --cflags opencore-amrnb)"
                FFMPEG_LDFLAGS+=" $(pkg-config --libs --static opencore-amrnb)"
                CONFIGURE_POSTFIX+=" --enable-libopencore-amrnb"
            ;;
            openh264)
                FFMPEG_CFLAGS+=" $(pkg-config --cflags openh264)"
                FFMPEG_LDFLAGS+=" $(pkg-config --libs --static openh264)"
                CONFIGURE_POSTFIX+=" --enable-libopenh264"
            ;;
            opus)
                FFMPEG_CFLAGS+=" $(pkg-config --cflags opus)"
                FFMPEG_LDFLAGS+=" $(pkg-config --libs --static opus)"
                CONFIGURE_POSTFIX+=" --enable-libopus"
            ;;
            rubberband)
                FFMPEG_CFLAGS+=" $(pkg-config --cflags rubberband)"
                FFMPEG_LDFLAGS+=" $(pkg-config --libs --static rubberband)"
                FFMPEG_LDFLAGS+=" -framework Accelerate"
                CONFIGURE_POSTFIX+=" --enable-librubberband --enable-gpl"
            ;;
            sdl)
                FFMPEG_CFLAGS+=" $(pkg-config --cflags sdl2)"
                FFMPEG_LDFLAGS+=" $(pkg-config --libs --static sdl2)"
                CONFIGURE_POSTFIX+=" --enable-sdl2"
            ;;
            shine)
                FFMPEG_CFLAGS+=" $(pkg-config --cflags shine)"
                FFMPEG_LDFLAGS+=" $(pkg-config --libs --static shine)"
                CONFIGURE_POSTFIX+=" --enable-libshine"
            ;;
            snappy)
                FFMPEG_CFLAGS+=" $(pkg-config --cflags snappy)"
                FFMPEG_LDFLAGS+=" $(pkg-config --libs --static snappy)"
                CONFIGURE_POSTFIX+=" --enable-libsnappy"
            ;;
            soxr)
                FFMPEG_CFLAGS+=" $(pkg-config --cflags soxr)"
                FFMPEG_LDFLAGS+=" $(pkg-config --libs --static soxr)"
                CONFIGURE_POSTFIX+=" --enable-libsoxr"
            ;;
            speex)
                FFMPEG_CFLAGS+=" $(pkg-config --cflags speex)"
                FFMPEG_LDFLAGS+=" $(pkg-config --libs --static speex)"
                CONFIGURE_POSTFIX+=" --enable-libspeex"
            ;;
            tesseract)
                FFMPEG_CFLAGS+=" $(pkg-config --cflags tesseract)"
                FFMPEG_LDFLAGS+=" $(pkg-config --libs --static tesseract)"
                FFMPEG_CFLAGS+=" $(pkg-config --cflags giflib)"
                FFMPEG_LDFLAGS+=" $(pkg-config --libs --static giflib)"
                CONFIGURE_POSTFIX+=" --enable-libtesseract"
            ;;
            twolame)
                FFMPEG_CFLAGS+=" $(pkg-config --cflags twolame)"
                FFMPEG_LDFLAGS+=" $(pkg-config --libs --static twolame)"
                CONFIGURE_POSTFIX+=" --enable-libtwolame"
            ;;
            vo-amrwbenc)
                FFMPEG_CFLAGS+=" $(pkg-config --cflags vo-amrwbenc)"
                FFMPEG_LDFLAGS+=" $(pkg-config --libs --static vo-amrwbenc)"
                CONFIGURE_POSTFIX+=" --enable-libvo-amrwbenc"
            ;;
            wavpack)
                FFMPEG_CFLAGS+=" $(pkg-config --cflags wavpack)"
                FFMPEG_LDFLAGS+=" $(pkg-config --libs --static wavpack)"
                CONFIGURE_POSTFIX+=" --enable-libwavpack"
            ;;
            x264)
                FFMPEG_CFLAGS+=" $(pkg-config --cflags x264)"
                FFMPEG_LDFLAGS+=" $(pkg-config --libs --static x264)"
                CONFIGURE_POSTFIX+=" --enable-libx264 --enable-gpl"
            ;;
            x265)
                FFMPEG_CFLAGS+=" $(pkg-config --cflags x265)"
                FFMPEG_LDFLAGS+=" $(pkg-config --libs --static x265)"
                CONFIGURE_POSTFIX+=" --enable-libx265 --enable-gpl"
            ;;
            xvidcore)
                FFMPEG_CFLAGS+=" $(pkg-config --cflags xvidcore)"
                FFMPEG_LDFLAGS+=" $(pkg-config --libs --static xvidcore)"
                CONFIGURE_POSTFIX+=" --enable-libxvid --enable-gpl"
            ;;
            expat)
                FFMPEG_CFLAGS+=" $(pkg-config --cflags expat)"
                FFMPEG_LDFLAGS+=" $(pkg-config --libs --static expat)"
            ;;
            libogg)
                FFMPEG_CFLAGS+=" $(pkg-config --cflags ogg)"
                FFMPEG_LDFLAGS+=" $(pkg-config --libs --static ogg)"
            ;;
            libpng)
                FFMPEG_CFLAGS+=" $(pkg-config --cflags libpng)"
                FFMPEG_LDFLAGS+=" $(pkg-config --libs --static libpng)"
            ;;
            nettle)
                FFMPEG_CFLAGS+=" $(pkg-config --cflags nettle)"
                FFMPEG_LDFLAGS+=" $(pkg-config --libs --static nettle)"
                FFMPEG_CFLAGS+=" $(pkg-config --cflags hogweed)"
                FFMPEG_LDFLAGS+=" $(pkg-config --libs --static hogweed)"
            ;;
            ios-*|tvos-*)

                # BUILT-IN LIBRARIES SHARE INCLUDE AND LIB DIRECTORIES
                # INCLUDING ONLY ONE OF THEM IS ENOUGH
                FFMPEG_CFLAGS+=" $(pkg-config --cflags zlib)"
                FFMPEG_LDFLAGS+=" $(pkg-config --libs --static zlib)"

                case ${ENABLED_LIBRARY} in
                    *-audiotoolbox)
                        CONFIGURE_POSTFIX+=" --enable-audiotoolbox"
                    ;;
                    *-avfoundation)
                        CONFIGURE_POSTFIX+=" --enable-avfoundation"
                    ;;
                    *-bzip2)
                        CONFIGURE_POSTFIX+=" --enable-bzlib"
                    ;;
                    *-videotoolbox)
                        CONFIGURE_POSTFIX+=" --enable-videotoolbox"
                    ;;
                    *-zlib)
                        CONFIGURE_POSTFIX+=" --enable-zlib"
                    ;;
                    *-libiconv)
                        CONFIGURE_POSTFIX+=" --enable-iconv"
                    ;;
                esac
            ;;
        esac
    else

        # THE FOLLOWING LIBRARIES SHOULD BE EXPLICITLY DISABLED TO PREVENT AUTODETECT
        # NOTE THAT IDS MUST BE +1 OF THE INDEX VALUE
        if [[ ${library} -eq 30 ]]; then
            CONFIGURE_POSTFIX+=" --disable-sdl2"
        elif [[ ${library} -eq 44 ]]; then
            CONFIGURE_POSTFIX+=" --disable-zlib"
        elif [[ ${library} -eq 45 ]]; then
            CONFIGURE_POSTFIX+=" --disable-audiotoolbox"
        elif [[ ${library} -eq 46 ]]; then
            CONFIGURE_POSTFIX+=" --disable-bzlib"
        elif [[ ${library} -eq 47 ]]; then
            CONFIGURE_POSTFIX+=" --disable-videotoolbox"
        elif [[ ${library} -eq 48 ]]; then
            CONFIGURE_POSTFIX+=" --disable-avfoundation"
        elif [[ ${library} -eq 49 ]]; then
            CONFIGURE_POSTFIX+=" --disable-iconv"
        fi
    fi

    ((library++))
done

# ALWAYS BUILD STATIC LIBRARIES
BUILD_LIBRARY_OPTIONS="--enable-static --disable-shared";

# OPTIMIZE FOR SPEED INSTEAD OF SIZE
if [[ -z ${MOBILE_FFMPEG_OPTIMIZED_FOR_SPEED} ]]; then
    SIZE_OPTIONS="--enable-small";
else
    SIZE_OPTIONS="";
fi

# SET DEBUG OPTIONS
if [[ -z ${MOBILE_FFMPEG_DEBUG} ]]; then
    DEBUG_OPTIONS="--disable-debug";
else
    DEBUG_OPTIONS="--enable-debug --disable-stripping";
fi

# CFLAGS PARTS
ARCH_CFLAGS=$(get_arch_specific_cflags);
APP_CFLAGS=$(get_app_specific_cflags ${LIB_NAME});
COMMON_CFLAGS=$(get_common_cflags);
if [[ -z ${MOBILE_FFMPEG_DEBUG} ]]; then
    OPTIMIZATION_CFLAGS="$(get_size_optimization_cflags ${LIB_NAME})"
else
    OPTIMIZATION_CFLAGS="${MOBILE_FFMPEG_DEBUG}"
fi
MIN_VERSION_CFLAGS=$(get_min_version_cflags);
COMMON_INCLUDES=$(get_common_includes);

# LDFLAGS PARTS
ARCH_LDFLAGS=$(get_arch_specific_ldflags);
if [[ -z ${MOBILE_FFMPEG_DEBUG} ]]; then
    OPTIMIZATION_FLAGS="$(get_size_optimization_ldflags ${LIB_NAME})"
else
    OPTIMIZATION_FLAGS="${MOBILE_FFMPEG_DEBUG}"
fi
LINKED_LIBRARIES=$(get_common_linked_libraries);
COMMON_LDFLAGS=$(get_common_ldflags);

# REORDERED FLAGS
export CFLAGS="${ARCH_CFLAGS} ${APP_CFLAGS} ${COMMON_CFLAGS} ${OPTIMIZATION_CFLAGS} ${MIN_VERSION_CFLAGS}${FFMPEG_CFLAGS} ${COMMON_INCLUDES}"
export CXXFLAGS=$(get_cxxflags ${LIB_NAME})
export LDFLAGS="${ARCH_LDFLAGS}${FFMPEG_LDFLAGS} ${LINKED_LIBRARIES} ${COMMON_LDFLAGS} ${BITCODE_FLAGS} ${OPTIMIZATION_FLAGS}"

echo -n -e "\n${LIB_NAME}: "

# DOWNLOAD LIBRARY
DOWNLOAD_RESULT=$(download_library_source ${LIB_NAME})
if [[ ${DOWNLOAD_RESULT} -ne 0 ]]; then
    exit 1
fi

cd ${BASEDIR}/src/${LIB_NAME} || exit 1

if [[ -z ${NO_WORKSPACE_CLEANUP_ffmpeg} ]]; then
    echo -e "INFO: Cleaning workspace for ${LIB_NAME}" 1>>${BASEDIR}/build.log 2>&1
    make distclean 2>/dev/null 1>/dev/null
fi

########################### CUSTOMIZATIONS #######################

# 1. Workaround to prevent adding of -mdynamic-no-pic flag
${SED_INLINE} 's/check_cflags -mdynamic-no-pic && add_asflags -mdynamic-no-pic;/check_cflags -mdynamic-no-pic;/g' ./configure 1>>${BASEDIR}/build.log 2>&1

# 2. Workaround for videotoolbox on mac catalyst
if [ ${ARCH} == "x86-64-mac-catalyst" ]; then
    ${SED_INLINE} 's/    CFDictionarySetValue(buffer_attributes\, kCVPixelBufferOpenGLESCompatibilityKey/   \/\/ CFDictionarySetValue(buffer_attributes\, kCVPixelBufferOpenGLESCompatibilityKey/g' ${BASEDIR}/src/${LIB_NAME}/libavcodec/videotoolbox.c
else
    ${SED_INLINE} 's/   \/\/ CFDictionarySetValue(buffer_attributes\, kCVPixelBufferOpenGLESCompatibilityKey/    CFDictionarySetValue(buffer_attributes\, kCVPixelBufferOpenGLESCompatibilityKey/g' ${BASEDIR}/src/${LIB_NAME}/libavcodec/videotoolbox.c
fi

# 3. Use thread local log level
${SED_INLINE} 's/static int av_log_level/__thread int av_log_level/g' ${BASEDIR}/src/${LIB_NAME}/libavutil/log.c 1>>${BASEDIR}/build.log 2>&1

###################################################################

./configure \
    --sysroot=${SDK_PATH} \
    --prefix=${BASEDIR}/prebuilt/$(get_target_build_directory)/${LIB_NAME} \
    --enable-version3 \
    --arch="${TARGET_ARCH}" \
    --cpu="${TARGET_CPU}" \
    --target-os=darwin \
    --ar="${AR}" \
    --cc="${CC}" \
    --cxx="${CXX}" \
    --as="${AS}" \
    --ranlib="${RANLIB}" \
    --strip="${STRIP}" \
    ${NEON_FLAG} \
    --enable-cross-compile \
    --enable-pic \
    ${ARCH_OPTIONS} \
    --enable-inline-asm \
    --enable-optimizations \
    --enable-swscale \
    --extra-cflags='-I/Volumes/data/dev/ffmpeg/mobile/ios/prebuilt/ios-arm64/x264/include -I/Volumes/data/dev/ffmpeg/mobile/ios/prebuilt/ios-arm64/x265/include' \
    --extra-ldflags='-L/Volumes/data/dev/ffmpeg/mobile/ios/prebuilt/ios-arm64/x264/lib -lx264 -L/Volumes/data/dev/ffmpeg/mobile/ios/prebuilt/ios-arm64/x265/lib -lx265 -lstdc++' \
    --enable-libx264 \
    --enable-gpl \
    --enable-libx265 \
    ${BUILD_LIBRARY_OPTIONS} \
    ${SIZE_OPTIONS}  \
    --disable-v4l2-m2m \
    --disable-outdev=v4l2 \
    --disable-outdev=fbdev \
    --disable-outdev=audiotoolbox \
    --disable-indev=v4l2 \
    --disable-indev=fbdev \
    --disable-openssl \
    --disable-xmm-clobber-test \
    ${DEBUG_OPTIONS} \
    --disable-neon-clobber-test \
    --disable-programs \
    --disable-postproc \
    --disable-doc \
    --disable-htmlpages \
    --disable-manpages \
    --disable-podpages \
    --disable-txtpages \
    --disable-sndio \
    --disable-schannel \
    --disable-securetransport \
    --disable-xlib \
    --disable-cuda \
    --disable-cuvid \
    --disable-nvenc \
    --disable-vaapi \
    --disable-vdpau \
    --disable-appkit \
    --disable-alsa \
    --disable-cuda \
    --disable-cuvid \
    --disable-nvenc \
    --disable-vaapi \
    --disable-vdpau \
    --disable-network \
    --disable-sdl2 \
    --disable-protocols \
    --enable-protocol=file \
    --disable-demuxers \
    --enable-demuxer=mov \
    --disable-muxers \
    --enable-muxer=mov \
    --enable-muxer=mp4 \
    --disable-libxcb \
    --disable-decoders \
    --enable-decoder=aac \
    --enable-decoder=h264 \
    --enable-decoder=hevc \
    --disable-encoders \
    --enable-encoder=aac \
    --enable-encoder=libx264 \
    --enable-encoder=libx265 \
    --enable-encoder=h264_videotoolbox \
    --enable-encoder=hevc_videotoolbox \
    --enable-encoder=prores_videotoolbox \
    --disable-filter=yadif_videotoolbox 
    # ${CONFIGURE_POSTFIX} #1>>${BASEDIR}/build.log 2>&1

if [ $? -ne 0 ]; then
    echo "failed"
    exit 1
fi

if [[ -z ${NO_OUTPUT_REDIRECTION} ]]; then
    make -j$(get_cpu_count) #1>>${BASEDIR}/build.log 2>&1

    if [ $? -ne 0 ]; then
        echo "failed"
        exit 1
    fi
else
    echo -e "started\n"
    make -j$(get_cpu_count) #1>>${BASEDIR}/build.log 2>&1

    if [ $? -ne 0 ]; then
        echo -n -e "\n${LIB_NAME}: failed\n"
        exit 1
    else
        echo -n -e "\n${LIB_NAME}: "
    fi
fi

rm -rf ${BASEDIR}/prebuilt/$(get_target_build_directory)/${LIB_NAME} 1>>${BASEDIR}/build.log 2>&1
make install #1>>${BASEDIR}/build.log 2>&1

if [ $? -ne 0 ]; then
    echo "failed"
    exit 1
fi

# MANUALLY ADD REQUIRED HEADERS
mkdir -p ${BASEDIR}/prebuilt/$(get_target_build_directory)/ffmpeg/include/libavutil/x86
mkdir -p ${BASEDIR}/prebuilt/$(get_target_build_directory)/ffmpeg/include/libavutil/arm
mkdir -p ${BASEDIR}/prebuilt/$(get_target_build_directory)/ffmpeg/include/libavutil/aarch64
mkdir -p ${BASEDIR}/prebuilt/$(get_target_build_directory)/ffmpeg/include/libavcodec/x86
mkdir -p ${BASEDIR}/prebuilt/$(get_target_build_directory)/ffmpeg/include/libavcodec/arm
cp -f ${BASEDIR}/src/ffmpeg/config.h ${BASEDIR}/prebuilt/$(get_target_build_directory)/ffmpeg/include
cp -f ${BASEDIR}/src/ffmpeg/libavcodec/mathops.h ${BASEDIR}/prebuilt/$(get_target_build_directory)/ffmpeg/include/libavcodec
cp -f ${BASEDIR}/src/ffmpeg/libavcodec/x86/mathops.h ${BASEDIR}/prebuilt/$(get_target_build_directory)/ffmpeg/include/libavcodec/x86
cp -f ${BASEDIR}/src/ffmpeg/libavcodec/arm/mathops.h ${BASEDIR}/prebuilt/$(get_target_build_directory)/ffmpeg/include/libavcodec/arm
cp -f ${BASEDIR}/src/ffmpeg/libavformat/network.h ${BASEDIR}/prebuilt/$(get_target_build_directory)/ffmpeg/include/libavformat
cp -f ${BASEDIR}/src/ffmpeg/libavformat/os_support.h ${BASEDIR}/prebuilt/$(get_target_build_directory)/ffmpeg/include/libavformat
cp -f ${BASEDIR}/src/ffmpeg/libavformat/url.h ${BASEDIR}/prebuilt/$(get_target_build_directory)/ffmpeg/include/libavformat
cp -f ${BASEDIR}/src/ffmpeg/libavutil/internal.h ${BASEDIR}/prebuilt/$(get_target_build_directory)/ffmpeg/include/libavutil
cp -f ${BASEDIR}/src/ffmpeg/libavutil/libm.h ${BASEDIR}/prebuilt/$(get_target_build_directory)/ffmpeg/include/libavutil
cp -f ${BASEDIR}/src/ffmpeg/libavutil/reverse.h ${BASEDIR}/prebuilt/$(get_target_build_directory)/ffmpeg/include/libavutil
cp -f ${BASEDIR}/src/ffmpeg/libavutil/thread.h ${BASEDIR}/prebuilt/$(get_target_build_directory)/ffmpeg/include/libavutil
cp -f ${BASEDIR}/src/ffmpeg/libavutil/timer.h ${BASEDIR}/prebuilt/$(get_target_build_directory)/ffmpeg/include/libavutil
cp -f ${BASEDIR}/src/ffmpeg/libavutil/x86/asm.h ${BASEDIR}/prebuilt/$(get_target_build_directory)/ffmpeg/include/libavutil/x86
cp -f ${BASEDIR}/src/ffmpeg/libavutil/x86/timer.h ${BASEDIR}/prebuilt/$(get_target_build_directory)/ffmpeg/include/libavutil/x86
cp -f ${BASEDIR}/src/ffmpeg/libavutil/arm/timer.h ${BASEDIR}/prebuilt/$(get_target_build_directory)/ffmpeg/include/libavutil/arm
cp -f ${BASEDIR}/src/ffmpeg/libavutil/aarch64/timer.h ${BASEDIR}/prebuilt/$(get_target_build_directory)/ffmpeg/include/libavutil/aarch64
cp -f ${BASEDIR}/src/ffmpeg/libavutil/x86/emms.h ${BASEDIR}/prebuilt/$(get_target_build_directory)/ffmpeg/include/libavutil/x86

if [ $? -eq 0 ]; then
    echo "ok"
else
    echo "failed"
    exit 1
fi

Lokie博客
请先登录后发表评论
  • 最新评论
  • 总共0条评论
  • 本博客使用免费开源的 laravel-bjyblog v5.5.1.1 搭建 © 2014-2018 lokie.wang 版权所有 ICP证:沪ICP备18016993号
  • 联系邮箱:kitche1985@hotmail.com