亲宝软件园·资讯

展开

Android接入ffmpeg库

lorienzhang 人气:1

一、准备交叉编译环境

我的环境如下:

参考教程及NDK构建交叉工具链:https:

下载之后解压,我的NDK解压路径为:/work/android/sdk/ndk/android-ndk-r17c/

安装独立工具链需要安装python,如果没有,安装下:

lorien@ubuntu-20: sudo apt-get install python

然后执行工具链构建脚本:

lorien@ubuntu-20: cd /work/android/sdk/ndk/android-ndk-r17c/build/tools/
lorien@ubuntu-20: ./make_standalone_toolchain.py \
    --arch arm64 \
    --api 21 \
    --install-dir /work/tmp/android-toolchain

–install-dir参数指定了交叉工具链的路径,进入该目录看下:

lorien@ubuntu-20: cd /work/tmp/android-toolchain/
lorien@ubuntu-20: ls
aarch64-linux-android  COPYING       COPYING.LIB      lib      manifest_4691093.xml     MODULE_LICENSE_MIT  repo.prop  test
AndroidVersion.txt     COPYING3      COPYING.RUNTIME  lib64    MODULE_LICENSE_BSD_LIKE  NOTICE              share
bin                    COPYING3.LIB  include          libexec  MODULE_LICENSE_GPL       prebuilt_include    sysroot

几个比较重要的目录:

二、交叉编译ffmpeg+libx264+libfdk-aac

1. 交叉编译libfdk-aac

下载libfdk-aac源码:https://sourceforge.net/projects/opencore-amr/files/fdk-aac/
编写配置configure脚本config-arm64.sh,将文件放入libfdk-aac源码跟目录中,脚本内容如下:

#!/bin/bash
export PATH=$PATH:/work/tmp/android-toolchain/bin

SYSROOT=/work/tmp/android-toolchain/sysroot
target_host=aarch64-linux-android
export AR=$target_host-ar
export AS=$target_host-clang
export CC=$target_host-clang
export CXX=$target_host-clang++
export LD=$target_host-ld
export STRIP=$target_host-strip
export RANLIB=$target_host-ranlib
# 注意要加-D__ANDROID_API__选项,否则可能会遇到undefined reference to 'stderr'错误
export CFLAGS="-fPIC -D__ANDROID_API__=21 -I$SYSROOT/usr/include"
export CPPFLAGS="-fPIC -D__ANDROID_API__=21 -I$SYSROOT/usr/include"
export CXXFLAGS="-fPIC -D__ANDROID_API__=21 -I$SYSROOT/usr/include"
export LDFLAGS="-L$SYSROOT/usr/lib"
./configure --host=aarch64-linux-android \
	--enable-static \
	--disable-shared \
	--prefix=$(pwd)/install/

执行脚本:

lorien@ubuntu-20: cd /work/android/media/fdk-aac-2.0.2/
lorien@ubuntu-20: ./config-arm64.sh 

可能会报错:

/work/tmp/android-toolchain/bin/clang60: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such file or directory

是缺少 libtinfo5导致的,需要安装下:

lorien@ubuntu-20: sudo apt-get install libtinfo5

再重新执行config-arm64.sh脚本,即可配置成功。

接下来,编译安装

lorien@ubuntu-20: make
lorien@ubuntu-20: make install

编译期间可能会碰到一些错误:
错误1:
libSBRdec/src/lpp_tran.cpp:122:10: fatal error: ‘log/log.h’ file not found
解决:
删除代码 include “log/log.h”

错误2:
libSBRdec/src/lpp_tran.cpp:341:5: error: use of undeclared identifier ‘android_errorWriteLog’
解决:
删除调用代码即可

错误处理后再重新编译&安装,成功后进入/work/android/media/fdk-aac-2.0.2/install 目录下可看到编译产物:

lorien@ubuntu-20: cd install/
include  lib

include目录下是产生头文件,lib目录下是一些.a的库文件。

2. 交叉编译libx264

下载libx264源码:git clone https://code.videolan.org/videolan/x264.git

和上面类似,进入x264源码根目录,编写config脚本config-arm64.sh:

#!/bin/bash

export PATH=$PATH:/work/tmp/android-toolchain/bin
SYSROOT=/work/tmp/android-toolchain/sysroot
target_host=aarch64-linux-android
export CFLAGS="-fPIC -D__ANDROID_API__=21 -I$SYSROOT/usr/include"
export CPPFLAGS="-fPIC -D__ANDROID_API__=21 -I$SYSROOT/usr/include"
export CXXFLAGS="-fPIC -D__ANDROID_API__=21 -I$SYSROOT/usr/include"
export LDFLAGS="-L$SYSROOT/usr/lib"
./configure --host=aarch64-linux-android \
	--cross-prefix=$target_host- \
	--sysroot=$SYSROOT \
	--enable-static \
	--enable-shared \
	--prefix=$(pwd)/install/

执行脚本,然后make&make install即可。根据-prefix选项配置,编译产物会输出到 /work/android/media/x264/install目录下面。

3. 交叉编译ffmpeg

fdk-aac和x264编译完成之后,我们可以编译ffmpeg了,ffmpeg会使用上面编译出来的两个库。

首先,下载ffmpeg源码:

lorien@ubuntu-20: git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg

进入ffmpeg源码根目录,编写配置脚本config-arm64.sh:

#!/bin/bash

export PATH=$PATH:/work/tmp/android-toolchain/bin
SYSROOT=/work/tmp/android-toolchain/sysroot
target_host=aarch64-linux-android
export CFLAGS="-fPIC -D__ANDROID_API__=21 -I$SYSROOT/usr/include -I$(pwd)/../fdk-aac-2.0.2/install/include  -I$(pwd)/../x264/install/include"
export LDFLAGS="-L$SYSROOT/usr/lib -L$(pwd)/../fdk-aac-2.0.2/install/lib  -L$(pwd)/../x264/install/lib"
./configure --cross-prefix=$target_host- \
	--enable-cross-compile \
	--sysroot=$SYSROOT \
	--arch=aarch64 \
	--target-os=linux \
	--enable-static \
	--disable-shared \
	--enable-gpl \
	--enable-nonfree \
	--disable-ffmpeg \
	--disable-ffplay \
	--disable-ffprobe \
	--disable-doc \
	--disable-devices \
	--disable-avdevice \
	--disable-postproc \
	--disable-avfilter \
	--disable-filters \
	--disable-protocols \
	--enable-libx264 \
	--enable-libfdk-aac \
	--prefix=$(pwd)/install/

在配置ffmpeg时,添加*–enable-libx264选项和–enable-libfdk-aac* 即可使ffmpeg打开x264和fdk-aac。需要注意的是,我们要在CFLAGS编译选项中添加*-I参数来指明ffmpeg编译x264和fdk-aac库使用两个外部库的头文件目录,以及通过-L* 选项指明ffmpeg在链接外部库时库文件目录。

配置完之后,我们通过make & make install进行编译安装。

编译可能遇到错误:

错误一:

libavcodec/aaccoder.c: In function 'search_for_ms':
libavcodec/aaccoder.c:803:25: error: expected identifier or '(' before numeric constant
                     int B0 = 0, B1 = 0;
                         ^
libavcodec/aaccoder.c:865:28: error: lvalue required as left operand of assignment
                         B0 += b1+b2;
                            ^
libavcodec/aaccoder.c:866:25: error: 'B1' undeclared (first use in this function)
                         B1 += b3+b4;
libavcodec/aaccoder.c:866:25: note: each undeclared identifier is reported only once for each function it appears in
make: *** [libavcodec/aaccoder.o] Error 1

解决:将libavcodec/aaccoder.c文件B0变量替换成b0

错误二:

libavcodec/hevc_mvs.c: In function 'derive_spatial_merge_candidates':
libavcodec/hevc_mvs.c:208:15: error: 'y0000000' undeclared (first use in this function)
             ((y ## v) >> s->ps.sps->log2_min_pu_size))
               ^
libavcodec/hevc_mvs.c:204:14: note: in definition of macro 'TAB_MVF'
     tab_mvf[(y) * min_pu_width + x]
              ^
libavcodec/hevc_mvs.c:274:16: note: in expansion of macro 'TAB_MVF_PU'
     (cand && !(TAB_MVF_PU(v).pred_flag == PF_INTRA))
                ^
libavcodec/hevc_mvs.c:368:23: note: in expansion of macro 'AVAILABLE'
     is_available_b0 = AVAILABLE(cand_up_right, B0) &&
                       ^
libavcodec/hevc_mvs.c:208:15: note: each undeclared identifier is reported only once for each function it appears in

解决: 将libavcodec/hevc_mvs.c文件的变量B0改成b0,xB0改成xb0,yB0改成yb0

错误三:

libavcodec/opus_pvq.c: In function ‘quant_band_template’:
libavcodec/opus_pvq.c:498:9: error: expected identifier or ‘(’ before
numeric constant
int B0 = blocks;
^ libavcodec/opus_pvq.c:559:12: error: lvalue required as left operand of assignment
B0 = blocks;
^ make: *** [libavcodec/opus_pvq.o] Error 1

解决:将libavcodec/opus_pvq.c文件的变量B0改成b0

解完所有错误后,重新编译、安装。看下编译产物

lorien@ubuntu-20: cd /work/android/media/ffmpeg/install/
lorien@ubuntu-20: ls
include  lib  share

lorien@ubuntu-20: cd lib
lorien@ubuntu-20: ls
libavcodec.a  libavformat.a  libavutil.a  libswresample.a  libswscale.a  pkgconfig

可以看到在lib下有一系列静态库文件。.a文件有点多,下面我们将所有.a文件打包一个so文件,方便后面引入,打包脚本如下:

#!/bin/bash

export PATH=$PATH:/work/tmp/android-toolchain/bin
SYSROOT=/work/tmp/android-toolchain/sysroot
aarch64-linux-android-ld -rpath-link=-L$SYSROOT/usr/lib \
	-L$SYSROOT/usr/lib \
	-L$(pwd)/x264 \
	-lc -lm -lz -ldl -lx264 \
	-soname libffmpeg.so \
	-shared \
	-nostdlib \
	-Bsymbolic \
	--whole-archive \
	--no-undefined \
	-o libffmpeg.so \
	libavcodec.a libavformat.a libavutil.a libswresample.a libswscale.a \
	$(pwd)/fdk-aac/libfdk-aac.a \
	$SYSROOT/../lib/gcc/aarch64-linux-android/4.9.x/libgcc.a

通过ld命令,将所有.a文件打包成libffmpeg.so文件。接下来再用strip命令优化下so

lorien@ubuntu-20: aarch64-linux-android-strip libffmpeg.so

至此,libffmpeg.so库文件就已经制作好了。

加载全部内容

相关教程
猜你喜欢
用户评论