1. Android FFmpeg开发基本流程
(1)X264/FFmpeg + NDK编译
(2)ffmpeg.so + 编解码C代码
(3)Android.mk 编译
(4)JNI
(5)JAVA代码调用
基本流程如下图所示:
本文涉及FFmpeg,x264编译。
2. NDK配置
首先需要配置NDK开发环境,略
(1)设置$NDK环境变量
# Detect NDK
if [[ -z "$NDK" ]]; then
echo "The NDK dir is empty, If the shell can not run normally, you should set the NDK variable to your local ndk.dir"
exit 1
fi
(2)检查系统类型
# Detect OS
OS=`uname`
HOST_ARCH=`uname -m`
export CCACHE=; type ccache >/dev/null 2>&1 && export CCACHE=ccache
if [ $OS == 'Linux' ]; then
export HOST_SYSTEM=linux-$HOST_ARCH
elif [ $OS == 'Darwin' ]; then
export HOST_SYSTEM=darwin-$HOST_ARCH
fi
(3)配置Sysroot和cross_prefix
SYSROOT=$NDK/platforms/android-16/arch-arm
CROSS_PREFIX=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/$HOST_SYSTEM/bin/arm-linux-androideabi-
2. x264编译
2.1 下载源代码
git clone http://git.videolan.org/git/x264.git
2.2 编译脚本
#!/bin/bash
echo "###### 开始编译 x264 ######"
SOURCE=$TARGET_X264_DIR
cd $SOURCE
#PREFIX指定的是编译输出路径,不指定默认是/usr/local/lib和/usr/local/include
#PREFIX=../build
EXTRA_CFLAGS="-march=armv7-a -mfloat-abi=softfp -mfpu=neon -D__ARM_ARCH_7__ -D__ARM_ARCH_7A__"
EXTRA_LDFLAGS="-nostdlib"
./configure \
--prefix=$PREFIX \
--cross-prefix=$CROSS_PREFIX \
--extra-cflags="$EXTRA_CFLAGS" \
--extra-ldflags="$EXTRA_LDFLAGS" \
--enable-static \
--enable-pic \
--enable-strip \
--disable-cli \
--host=arm-linux \
--sysroot=$SYSROOT
make clean
make && make install
这里面需要注意的是:
(1)PREFIX指定的是编译输出路径,不指定默认是/usr/local/lib和/usr/local/include
这里讲ffmpeg和x264统一放在工程根目录中build文件夹
(2)编译时不能进行多线程,否则汇编优化报错,如下:
make -j4
make: [common/arm/deblock-a.o] Error 127 (ignored)
/Users/guohe/Android/android-ndk/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-gcc -I. -I. -c -DSTACK_ALIGNMENT=4 -DPIC -DHIGH_BIT_DEPTH=0 -DBIT_DEPTH=8 -o common/arm/predict-a.o common/arm/predict-a.S
/bin/sh: j4: command not found
2.3. 结论
Linux中编译x264:
sudo ./configure --enable-shared --prefix=/usr/local
sudo make
sudo make install
通过对比发现:Android中编译x264,最基本的就是配置SYSROOT和CROSS_PREFIX
3. 编译FFmpeg
3.1 下载源代码
git clone git://source.ffmpeg.org/ffmpeg.git $FFMPEG_SOURCE_DIR
3.2 编译脚本
ADD_H264_FEATURE="--enable-encoder=aac \
--enable-decoder=aac \
--enable-encoder=libx264 \
--enable-libx264 \
--extra-cflags=-I$PREFIX/include \
--extra-ldflags=-L$PREFIX/lib "
./configure \
--prefix=$PREFIX \
--enable-pthreads \
--enable-gpl \
--enable-version3 \
--enable-nonfree \
--enable-static \
--enable-small \
--enable-asm \
--enable-neon \
--cross-prefix=$CROSS_PREFIX \
--target-os=linux \
--arch=arm \
--enable-cross-compile \
--sysroot=$SYSROOT \
$ADD_H264_FEATURE
make clean
make -j4
make install
注:
(1)跨平台编译特殊配置
–sysroot=