FFmpeg编译多android cpu架构版本

FFmpeg 编译android OS使用的库,通常我们作为简单测试只需要编译arm-v7a版本so文件就可以满足。但为了兼容arm最新架构v8a我们需要编译v8a版本的so,用来支持arm64版本的CPU。为了增强ffmpeg的功能,通常我们都集成x264进行编译。

一、预备

编译系统:ubuntu 14.04 LTS

FFmpeg版本:ffmpeg-3.2.10

x264版本:x264-snapshot-20180320-2245

NDK版本:android-ndk-r13b

编译目标:arm-v7a、arm-v8a

二、编译x264

首先编译x264。我下载了x264-snapshot-20180320-2245作为编译代码,下载后的压缩包名称为last_x264.tar.bz2,解压此文件。

1.运行命令.tar.bz2 解压 tar jxvf last_x264.tar.bz2 解压;

2.进入x264-snapshot-20180320-2245目录,新建一个shell脚本文件,将以下内容复制到这个目录下,比如我命名为build_x264.sh;

其中NDK=/home/snake/NDK/android-ndk-r13b需要配置为自己的实际路径。

#!/bin/bash
NDK=/home/snake/NDK/android-ndk-r13b

configure()
{
    CPU=$1
    PREFIX=$(pwd)/android/$CPU
    HOST=""
    CROSS_PREFIX=""
    SYSROOT=""
    if [ "$CPU" == "armv7-a" ]
    then
        HOST=arm-linux
        SYSROOT=$NDK/platforms/android-21/arch-arm/
        CROSS_PREFIX=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-
    else
        HOST=aarch64-linux
        SYSROOT=$NDK/platforms/android-21/arch-arm64/
        CROSS_PREFIX=$NDK/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/bin/aarch64-linux-android-
    fi
    ./configure \
    --prefix=$PREFIX \
    --host=$HOST \
    --enable-pic \
    --enable-static \
    --enable-shared \
    --enalbe-neon \
    --extra-cflags="-fPIE -pie" \
    --extra-ldflags="-fPIE -pie -shared" \
    --cross-prefix=$CROSS_PREFIX \
    --sysroot=$SYSROOT
}

build()
{
    make clean
    cpu=$1
    echo "build $cpu"

    configure $cpu
    #-j
    make -j8
    make install
}

build arm64
build armv7-a

3.赋予build_x264.sh执行权限;

运行命令: chmod +x build_x264.sh

4.编辑configure文件;

修改前:

elif [ "$SYS" = "SunOS" ]; then
        echo "SOSUFFIX=so" >> config.mak
        echo "SONAME=libx264.so.$API" >> config.mak
        echo "SOFLAGS=-shared -Wl,-h,\$(SONAME) $SOFLAGS" >> config.mak
    else
        echo "SOSUFFIX=so" >> config.mak
        echo "SONAME=libx264.so.$API" >> config.mak
        echo "SOFLAGS=-shared -Wl,-soname,\$(SONAME) $SOFLAGS" >> config.mak
fi

修改后:

elif [ "$SYS" = "SunOS" ]; then
        echo "SOSUFFIX=so" >> config.mak
        echo "SONAME=libx264-$API.so" >> config.mak
        echo "SOFLAGS=-shared -Wl,-h,\$(SONAME) $SOFLAGS" >> config.mak
    else
        echo "SOSUFFIX=so" >> config.mak
        echo "SONAME=libx264-$API.so" >> config.mak
        echo "SOFLAGS=-shared -Wl,-soname,\$(SONAME) $SOFLAGS" >> config.mak
fi

运行命令:chmod +x configure

5.运行build_x264.sh进行编译。

./ build_x264.sh

编译成功后:

FFmpeg编译多android cpu架构版本_第1张图片

三、编译FFmpeg

1.解压ffmpeg压缩包;

运行命令:.tar 解包 tar xvf ffmpeg-3.2.10.tar

2.修改configure

修改前:

SLIBNAME_WITH_MAJOR='$(SLIBNAME).$(LIBMAJOR)'
LIB_INSTALL_EXTRA_CMD='$$(RANLIB)"$(LIBDIR)/$(LIBNAME)"'
SLIB_INSTALL_NAME='$(SLIBNAME_WITH_VERSION)'
SLIB_INSTALL_LINKS='$(SLIBNAME_WITH_MAJOR)$(SLIBNAME)'

修改后:

SLIBNAME_WITH_MAJOR='$(SLIBPREF)$(FULLNAME)-$(LIBMAJOR)$(SLIBSUF)'
LIB_INSTALL_EXTRA_CMD='$$(RANLIB)"$(LIBDIR)/$(LIBNAME)"'
SLIB_INSTALL_NAME='$(SLIBNAME_WITH_MAJOR)'
SLIB_INSTALL_LINKS='$(SLIBNAME)'

运行命令:chmod +x configure

3.加ffmpeg编译脚本,如命名为build_ffmpeg_x264.sh,内容如下:

#!/bin/bash

NDK=/home/snake/NDK/android-ndk-r13b
ADDI_CFLAGS=""
ADDI_LDFLAGS=""

ANDROID_ARMV5_CFLAGS="-march=armv5te" 
ANDROID_ARMV7_CFLAGS="-march=armv7-a -mfloat-abi=softfp -mfpu=neon"
ANDROID_ARMV8_CFLAGS="-march=armv8-a" 
ANDROID_X86_CFLAGS="-march=i686 -mtune=intel -mssse3 -mfpmath=sse -m32" 
ANDROID_X86_64_CFLAGS="-march=x86-64 -msse4.2 -mpopcnt -m64 -mtune=intel"

configure()
{
    CPU=$1
    PREFIX=$(pwd)/android/$CPU
    x264=$(pwd)/libx264/android/$CPU

    echo "x264 path=$x264"
    echo "PREFIX=$PREFIX"

    HOST=""
    CROSS_PREFIX=""
    SYSROOT=""
    ARCH=""
    CFLAGS=""

    if [ "$CPU" == "armv7-a" ]
    then
        ARCH="arm"
        HOST=arm-linux
        SYSROOT=$NDK/platforms/android-21/arch-arm/
        CROSS_PREFIX=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-
        CFLAGS=$ANDROID_ARMV7_CFLAGS
    else
        ARCH="aarch64"
        HOST=aarch64-linux
        SYSROOT=$NDK/platforms/android-21/arch-arm64/
        CROSS_PREFIX=$NDK/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/bin/aarch64-linux-android-
        CFLAGS=$ANDROID_ARMV8_CFLAGS
    fi
    ./configure \
    --prefix=$PREFIX \
    --enable-shared \
    --enable-nonfree \
    --enable-gpl \
    --enable-libx264 \
    --disable-doc \
    --disable-ffmpeg \
    --disable-ffplay \
    --disable-ffprobe \
    --disable-ffserver \
    --disable-avdevice \
    --disable-doc \
    --disable-symver \
    --cross-prefix=$CROSS_PREFIX \
    --target-os=linux \
    --arch=$ARCH \
    --enable-cross-compile \
    --sysroot=$SYSROOT \
    --extra-cflags="$CFLAGS -Os -fpic -I$x264/include -DANDROID -Wfatal-errors -Wno-deprecated" \
    --extra-ldflags="-L$x264/lib"
    $ADDITIONAL_CONFIGURE_FLAG
}

build()
{
    make clean
    cpu=$1
    echo "build $cpu"
    ADDITIONAL_CONFIGURE_FLAG=--enable-neon

    configure $cpu
    make -j8
    make install
}

build arm64
build armv7-a

运行命令:chmod +x build_ffmpeg_x264.sh赋予权限。

4.将整个编译好的x264-snapshot-20180320-2245文件夹重命名为libx264,并将此文件夹复制到ffmpeg-3.2.10下;

5.运行以上脚本编译;

编译通过后:

FFmpeg编译多android cpu架构版本_第2张图片

FFmpeg编译多android cpu架构版本_第3张图片

四、遇到的问题

1.arm64编译的时候出现的问题:
libavutil/…/compat/strtod.o: error adding symbols: File in wrong format

解决: 编译arm64时候删除"compat/strtod.o"和"compat/strtod.d"这两个文件,并在编译前运行命令make clean,如果脚本中已经包含make clean命名只需要删除以上两个文件即可。

2.要将x264-snapshot-20180320-2245重命名后在拷贝,不然修改ffmpeg编译脚本中的x264路径还是存在问题。

3.关于报错c compiler test failed.
这是由于–extra-cflags配置问题,引入编译器无法识别的选项。

你可能感兴趣的:(Android)