1 修改FFmpeg的configure
下载FFmpeg
源代码之后,首先需要对源代码中的configure
文件进行修改。由于编译出来的动态库文件名的版本号在.so
之后(例如libavcodec.so.5.100.1
),而Android
平台不能识别这样文件名,所以需要修改这种文件名。在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)'
2 编库
软解库
编译多个库的脚本如下,生成libavcodec-57.so libavdevice.so libavformat-57.so libavutil.so libswscale-4.so libavcodec.so libavfilter-6.so libavformat.so libswresample-2.so libswscale.so libavdevice-57.so libavfilter.so libavutil-55.so libswresample.so
等库:
#! /bin/sh
NDK=../../NDK/android-ndk-r12b
SYSROOT=$NDK/platforms/android-9/arch-arm/
TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64
CPU=arm
PREFIX=../test
ADDI_CFLAGS="-marm"
build_one()
{
./configure --prefix=$PREFIX --enable-shared --disable-static --disable-doc --disable-ffserver --enable-cross-compile --cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- --target-os=linux --arch=arm --sysroot=$SYSROOT --extra-cflags="-Os -fpic $ADDI_CFLAGS" --extra-ldflags="$ADDI_LDFLAGS" $ADDITIONAL_CONFIGURE_FLAG
make clean
make
make install
}
build_one
将编译的多个库整合为一个ffmpeg.so
库的编译脚本如下,注意:当整合为一个动态库时,要先编译出.a
的静态库(修改./configure
后的参数--enable-shared --disable-static
为--disable-shared --enable-static
),再将各静态库合并为一个动态库:
#! /bin/sh
NDK=../../NDK/android-ndk-r12b
SYSROOT=$NDK/platforms/android-9/arch-arm/
TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64
CPU=arm
PREFIX=../test
ADDI_CFLAGS="-marm"
export PREBUILT=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt
export PLATFORM=$NDK/platforms/android-14/arch-arm
build_one()
{
./configure --prefix=$PREFIX --disable-shared --enable-static --disable-doc --disable-ffserver --enable-cross-compile --cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- --target-os=linux --arch=arm --sysroot=$SYSROOT --extra-cflags="-Os -fpic $ADDI_CFLAGS" --extra-ldflags="$ADDI_LDFLAGS" $ADDITIONAL_CONFIGURE_FLAG
make clean
make
make install
}
build_one
$PREBUILT/linux-x86_64/bin/arm-linux-androideabi-ld -rpath-link=$PLATFORM/usr/lib -L$PLATFORM/usr/lib -L$PREFIX/lib -soname libffmpeg.so -shared -nostdlib -Bsymbolic --whole-archive --no-undefined -o $PREFIX/libffmpeg.so libavcodec/libavcodec.a libavfilter/libavfilter.a libswresample/libswresample.a libavformat/libavformat.a libavutil/libavutil.a libswscale/libswscale.a libavdevice/libavdevice.a -lc -lm -lz -ldl -llog --dynamic-linker=/system/bin/linker $PREBUILT/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/libgcc.a
最初编译脚本如下:
#! /bin/bash
NDK=../../NDK/android-ndk-r12b
SYSROOT=$NDK/platforms/android-9/arch-arm/
TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64
CPU=arm
PREFIX=../test
ADDI_CFLAGS="-marm"
function build_one
{
./configure \
--prefix=$PREFIX \
--enable-shared \
--disable-static \
--disable-doc \
--disable-ffmpeg \
--disable-ffplay \
--disable-ffprobe \
--disable-ffserver \
--disable-doc \
--disable-symver \
--enable-small \
--cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \
--target-os=linux \
--arch=arm \
--enable-cross-compile \
--sysroot=$SYSROOT \
--extra-cflags="-Os -fpic $ADDI_CFLAGS" \
--extra-ldflags="$ADDI_LDFLAGS" \
$ADDITIONAL_CONFIGURE_FLAG
make clean
make
make install
}
build_one
遇到的问题:
0 Cygwin
编不出,要在Linux
环境下编译
1 执行xxx.sh
时提示Permission denied
- 原因:没有执行权限
- 解决:执行
chmod a+x xxx.sh
命令,赋予执行权限
2 --prefix=../test command not found, --enable-shared command not found, --disable-staticcommand not found......
- 解决:将
./configure
后的\
换行删除,该命令并成一行
3 arm-linux-androideabi-gcc is unable to create an executable file.
- 原因:
TOOLCHAIN
路径错误 - 解决:将
arm-linux-androideabi-4.8
改为arm-linux-androideabi-4.9
,android-ndk-r12b
下没有4.8
路径
硬解库
参考:ffmpeg编译android 硬解码支持库 libstagefright
编译脚本如下:
#!/bin/bash
export NDK=../../NDK/android-ndk-r12b
echo "Fetching Android system headers"
git clone --depth=1 --branch gingerbread-release git://github.com/CyanogenMod/android_frameworks_base.git ../android-source/frameworks/base
git clone --depth=1 --branch gingerbread-release git://github.com/CyanogenMod/android_system_core.git ../android-source/system/core
echo "Fetching Android libraries for linking"
# Libraries from any froyo/gingerbread device/emulator should work
# fine, since the symbols used should be available on most of them.
if [ ! -d "../android-libs" ]; then
if [ ! -f "../update-cm-7.0.3-N1-signed.zip" ]; then
wget https://jaist.dl.sourceforge.net/project/nexusoneandroid/update-cm-7.0.3-N1-signed.zip -P../
fi
unzip ../update-cm-7.0.3-N1-signed.zip system/lib/* -d../
mv ../system/lib ../android-libs
rmdir ../system
fi
SYSROOT=$NDK/platforms/android-9/arch-arm
# Expand the prebuilt/* path into the correct one
TOOLCHAIN=`echo $NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64`
export PATH=$TOOLCHAIN/bin:$PATH
ANDROID_SOURCE=../android-source
ANDROID_LIBS=../android-libs
ABI="armeabi-v7a"
rm -rf ../build/stagefright
mkdir -p ../build/stagefright
DEST=../build/stagefright
FLAGS="--target-os=linux --cross-prefix=arm-linux-androideabi- --arch=arm --cpu=armv7-a"
FLAGS="$FLAGS --sysroot=$SYSROOT"
FLAGS="$FLAGS --disable-avdevice --disable-decoder=h264 --disable-decoder=h264_vdpau --enable-decoder=libstagefright_h264"
EXTRA_CFLAGS="-I$ANDROID_SOURCE/frameworks/base/include -I$ANDROID_SOURCE/system/core/include"
EXTRA_CFLAGS="$EXTRA_CFLAGS -I$ANDROID_SOURCE/frameworks/base/media/libstagefright"
EXTRA_CFLAGS="$EXTRA_CFLAGS -I$ANDROID_SOURCE/frameworks/base/include/media/stagefright/openmax"
EXTRA_CFLAGS="$EXTRA_CFLAGS -I$NDK/sources/cxx-stl/gnu-libstdc++/4.9/include -I$NDK/sources/cxx-stl/gnu-libstdc++/4.9/libs/$ABI/include"
EXTRA_CFLAGS="$EXTRA_CFLAGS -march=armv7-a -mfloat-abi=softfp -mfpu=neon"
EXTRA_LDFLAGS="-Wl,--fix-cortex-a8 -L$ANDROID_LIBS -Wl,-rpath-link,$ANDROID_LIBS -L$NDK/sources/cxx-stl/gnu-libstdc++/4.9/libs/$ABI -lstagefright -lstdc++ -lutils -lbinder -lgnustl_static -lgnustl_shared"
#EXTRA_LDFLAGS="$EXTRA_LDFLAGS -lcutils -ldl -lstlport -lc -lm -lz -ldl -llog -lstdc++ -lstagefright -lbinder -lstlport -lstdc++ -lstl"
EXTRA_CXXFLAGS="-Wno-multichar -fno-exceptions -fno-rtti"
DEST="$DEST/$ABI"
FLAGS="$FLAGS --prefix=$DEST"
mkdir -p $DEST
echo $FLAGS --extra-cflags="$EXTRA_CFLAGS" --extra-ldflags="$EXTRA_LDFLAGS" --extra-cxxflags="$EXTRA_CXXFLAGS" > $DEST/info.txt
./configure $FLAGS --extra-cflags="$EXTRA_CFLAGS" --extra-ldflags="$EXTRA_LDFLAGS" --extra-cxxflags="$EXTRA_CXXFLAGS" | tee $DEST/configuration.txt
[ $PIPESTATUS == 0 ] || exit 1
make clean
make -j4 || exit 1
make install
export PREBUILT=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt
export PLATFORM=$NDK/platforms/android-14/arch-arm
PREFIX=../build/stagefright/armeabi-v7a
#合并包
$PREBUILT/linux-x86_64/bin/arm-linux-androideabi-ld -rpath-link=$PLATFORM/usr/lib -L$PLATFORM/usr/lib -L$PREFIX/lib -soname libffmpeg.so -shared -nostdlib -Bsymbolic --whole-archive --no-undefined -o $PREFIX/libffmpeg.so libavcodec/libavcodec.a libavfilter/libavfilter.a libswresample/libswresample.a libavformat/libavformat.a libavutil/libavutil.a libswscale/libswscale.a -lc -lm -lz -ldl -llog --dynamic-linker=/system/bin/linker $PREBUILT/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/libgcc.a