从事多媒体软件开发的人几乎没有不知道FFmpeg的,很多视频播放器都是基于FFmpeg开发的。如今最火的智能手机操作系统Android上的很多第三方视频播放器也是基于FFmpeg实现全格式支持。由于Android通常跑在ARM处理器上,而且Android使用了自己的libc库(即bionic),因此要在Android上编译和使用FFmpeg需要做一些移植工作,好在FFmpeg本身用C写成,很好地支持跨平台移植,实现这个目的并不难,事实上已经有很多前辈做过这方面的工作并公开了他们的成果。
1. Rockplayer:
http://rockplayer.freecoder.org/index_cn.html
http://blog.csdn.net/harry_helei/article/details/6322767
2. havlenapetr:
http://github.com/havlenapetr/FFMpeg
http://blog.csdn.net/scut1135/article/details/6536157
http://hi.baidu.com/eefolks/blog/item/e0329e4682859129cefca351.html
http://ajavn.com/anzhuotuandui/4351.html
3. halfninja:
https://github.com/halfninja/android-ffmpeg-x264
4. olvaffe's:
http://gitorious.org/~olvaffe/ffmpeg/ffmpeg-android
http://www.nujk.com/ffmpeg-on-android
5. 流媒体开发论坛 - 罗索工作室:
http://www.rosoo.net/a/201108/14834.html
http://bbs.rosoo.net/thread-6252-1-1.html
当下本人也做这方面的事情,主要是优化ffmpeg编解码的性能,当然首先就是要搭建环境,编译代码,测试代码性能,然后优化模块,编译测试和调试,最终再测试性能:看了很多文章,对于一些新手是有点帮助,但是都不是很全面,下面简单记录下我如何搭建环境,如何编译,测试:
可以使用Rockplayer的方案进行实践。Rockplayer是Android上最有名的第三方视频播放器之一,其开发者根据LGPL协议公开了所使用的FFmpeg源码,该方案用Android NDK将FFmpeg源码编译生成一个单独的共享库libffmpeg.so,其中静态链接了libavformat、libavcodec、libavutil等模块,要使用FFmpeg只需要调用这一个.so即可。当然有了这个库还只是第一步,要利用FFmpeg开发一个自己的播放器或者把FFmpeg集成到Android本身的播放引擎stagefright中还需要很多其他工作,包括针对硬件平台进行优化。目前我做的只是编译出libffmpeg.so以及将FFmpeg自带的工具ffmpeg在adb shell中跑起来。我是在Android 3.2(Honeycomb)上编译FFmpeg,所用NDK版本为r7,在Android的其他版本上编译也大同小异。
题外话:Rockplayer之前也和他们谈过合作的事情,一家几个人搞得小型公司(其实也谈不上公司了),office在上海张江一个不起眼的小区里,也不能说他们没有技术含量,只能说他们找到一个突破口,能挣点小钱,他们的android手机播放器设计原则是支持要全,画质要清晰,性能不是主要,面向中高档手机,配置相对来说就高些。
下面把具体编译步骤描述如下,假定NDK安装在~/android-ndk-r7:
1. 首先从FFmpeg官网下载最新的release版本源码ffmpeg-0.11.tar.gz解压缩到Android源码树的ffmpeg/下。
2 准备一个编译脚本build_android.sh并放在ffmpeg/下面,这个脚本也是Rockplayer提供的,需做一些修改,其内容附在后面。我目前用的也会附在后面。
3 在ffmpeg目录下运行./build_android.sh开始编译FFmpeg,编译好的libffmpeg.so会放在文件夹android里面,一共有3个版本分别对应3种ARM体系结构,包括armv7-a、armv7-a-vfp、armv6_vfp,根据所运行的硬件平台选取其中一个版本。为了编译使用FFmpeg的程序时可以方便地找到libffmpeg.so,可将它复制到$OUT/system/lib/和$OUT/obj/lib/,当然这一步也可以加在build_android.sh中做。
4. 接下来就是编译可执行文件ffmpeg了,这个工具可以在命令行下完成FFmpeg提供的几乎所有功能包括编码、解码、转码等,也是用来调试和验证很有用的工具。其实上述编译完后在$ANDROID_BUILD_TOP/external/ffmpeg/下也会生成ffmpeg,但是在设备上无法运行。为了编出能在设备上运行的ffmpeg,可以写一个简单的Android.mk,其内容如下:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= \
cmdutils.c \
ffmpeg.c
LOCAL_C_INCLUDES :=
LOCAL_SHARED_LIBRARIES := \
libffmpeg
LOCAL_PRELINK_MODULE := false
LOCAL_MODULE := ffmpeg
include $(BUILD_EXECUTABLE)
也可以在E:\android-ndk-r7-windows\android-ndk-r7\samples\test-libstdc++\jni这个目录修改下Android.mk, 修改为:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := ffmpeg.c cmdutils.c
#LOCAL_PREBUILT_LIBS := libffmpeg
LOCAL_ARM_MODE := arm
LOCAL_C_INCLUDES += \
$(LOCAL_PATH)/include \
$(LOCAL_PATH)/include/libavcodec \
$(LOCAL_PATH)/ \
$(LOCAL_PATH)/include/libavfilter \
$(LOCAL_PATH)/include/libavdevice \
$(LOCAL_PATH)/include/libavutil
#LOCAL_LDLIBS := -L$(LOCAL_PATH) -lffmpeg
LOCAL_LDLIBS := libffmpeg.so
LOCAL_MODULE := ffmpeg
include $(BUILD_EXECUTABLE)
当然要把ffmpeg.c cmdutils.c和libffmpeg.so拷贝到当前目录。
5. 将上面编好的libffmpeg.so和ffmpeg分别用adb push复制到设备的/system/lib/和/system/bin/,push之前可先对libffmpeg.so做一下strip否则太大。现在就可以在adb shell中运行ffmpeg了,使用方法与在PC上相同。当然如果要用到其他开源库例如x264那么还需要把它们也移植到Android。
以上是基本的步骤,仅供参考,可以根据具体情况修改build_android.sh里面的配置选项。如前所述,编译FFmpeg只是第一步,后续还有很多工作需要做,今后如果有进展再贴上来。
最后附上build_android.sh供参考,在此也感谢Rockplayer的开发者:
#!/bin/bash
######################################################
# FFmpeg builds script for Android+ARM platform
#
# This script is released under term of
# CDDL (http://www.opensource.org/licenses/cddl1)
# Wrote by pinxue ([email protected]) from RockPlayer.com
# 2010-8 ~ 2011-4
######################################################
######################################################
# Usage:
# put this script in top of FFmpeg source tree
# ./build_android
#
# It generates binary for following architectures:
# ARMv6
# ARMv6+VFP
# ARMv7+VFPv3-d16 (Tegra2)
# ARMv7+Neon (Cortex-A8)
#
# Customizing:
# 1. Feel free to change ./configure parameters for more features
# 2. To adapt other ARM variants
# set $CPU and $OPTIMIZE_CFLAGS
# call build_one
######################################################
NDK=~/android-ndk-r7
PLATFORM=$NDK/platforms/android-8/arch-arm/
PREBUILT=../../prebuilt/linux-x86/toolchain/arm-eabi-4.4.3
function build_one
{
# -fasm : required. Android header file uses asm keyword instead of __asm__ , but most of c dialect (like ansi,c99,gnu99) implies -fno-asm.
# ~/android/android-ndk-r4/build/platforms/android-5/arch-arm//usr/include/asm/byteorder.h: In function '___arch__swab32':
# ~/android/android-ndk-r4/build/platforms/android-5/arch-arm//usr/include/asm/byteorder.h:25: error: expected ')' before ':' token
# -fno-short-enums : optimized. Else FFmpeg obj will generate a huge number of warning for variable-size enums,
# though we may suppress them by --no-enum-size-warning, it would be better to avoid it.
# .../ld: warning: cmdutils.o uses variable-size enums yet the output is to use 32-bit enums; use of enum values across objects may fail
# --extra-libs="-lgcc" : required. Else cannot solve some runtime function symbols
# ... undefined reference to `__aeabi_f2uiz'
# --enable-protocols : required. Without this option, the file open always fails mysteriously.
# FFmpeg's av_open_input_file will invoke file format probing functions, but because most of useful demuxers has flag of zero
# which cause them are ignored during file format probling and fall to url stream parsing,
# if protocols are disabled, the file:// url cannot be opened as well.
# $PREBUILT/bin/arm-eabi-ar d libavcodec/libavcodec.a inverse.o : required.
# FFmpeg includes two copies of inverse.c both in libavutil and libavcodec for performance consideration (not sure the benifit yet)
# Without this step, final ld of generating libffmpeg.so will fail silently, if invoke ld through gcc, gcc will collect more reasonable error message.
# -llog: debug only, FFmpeg itself doesn't require it at all.
# With this option, we may simply includes "utils/Log.h" and use LOGx() to observe FFmpeg's behavior
# PS, it seems the toolchain implies -DNDEBUG somewhere, it would be safer to use following syntax
# #ifdef NDEBUG
# #undef NDEBUG
# #define HAVE_NDEBUG
# #endif
# #include "utils/Log.h"
# #ifdef HAVE_NDEBUG
# #define NDEBUG
# #undef HAVE_NDEBUG
# #endif
# --whole-archive : required. Else ld generate a small .so file (about 15k)
# --no-stdlib : required. Android doesn't use standard c runtime but invited its own wheal (bionic libc) because of license consideration.
# space before \ of configure lines: required for some options. Else next line will be merged into previous lines's content and cause problem.
# Especially the --extra-cflags, the next line will pass to gcc in this case and configure will say gcc cannot create executable.
# many options mentioned by articles over internet are implied by -O2 or -O3 already, need not repeat at all.
# two or three common optimization cflags are omitted because not sure about the trade off yet. invoke NDK build system with V=1 to find them.
# -Wl,-T,$PREBUILT/arm-eabi/lib/ldscripts/armelf.x mentioned by almost every articles over internet, but it is not required to specify at all.
# -Dipv6mr_interface=ipv6mr_ifindex : required. Android inet header doesn't use ipv6mr_interface which is required by rfc, seems it generate this user space header file directly from kernel header file, but Linux kernel has decided to keep its own name for ever and ask user space header to use rfc name.
# HAVE_SYS_UIO_H : required. Else:
# In file included from ~/android/android-ndk-r4/build/platforms/android-5/arch-arm//usr/include/linux/socket.h:29,
# from ~/android/android-ndk-r4/build/platforms/android-5/arch-arm//usr/include/sys/socket.h:33,
# from libavformat/network.h:35,
# from libavformat/utils.c:46:
#~/android/android-ndk-r4/build/platforms/android-5/arch-arm//usr/include/linux/uio.h:19: error: redefinition of 'struct iovec'
#
# --disable-doc : required because of strange bug of toolchain.
./configure --target-os=linux \
--prefix=$PREFIX \
--enable-cross-compile \
--extra-libs="-lgcc" \
--arch=arm \
--cc=$PREBUILT/bin/arm-eabi-gcc \
--cross-prefix=$PREBUILT/bin/arm-eabi- \
--nm=$PREBUILT/bin/arm-eabi-nm \
--sysroot=$PLATFORM \
--extra-cflags=" -O3 -fpic -DANDROID -DHAVE_SYS_UIO_H=1 -Dipv6mr_interface=ipv6mr_ifindex -fasm -Wno-psabi -fno-short-enums -fno-strict-aliasing -finline-limit=300 $OPTIMIZE_CFLAGS " \
--disable-shared \
--enable-static \
--extra-ldflags="-Wl,-rpath-link=$PLATFORM/usr/lib -L$PLATFORM/usr/lib -nostdlib -lc -lm -ldl -llog" \
--enable-parsers \
--enable-encoders \
--enable-decoders \
--enable-muxers \
--enable-demuxers \
--enable-swscale \
--enable-swscale-alpha \
--disable-ffplay \
--disable-ffprobe \
--disable-ffserver \
--enable-network \
--enable-indevs \
--disable-bsfs \
--enable-filters \
--enable-avfilter \
--enable-protocols \
--enable-asm \
$ADDITIONAL_CONFIGURE_FLAG
#make clean
make -j4 install
$PREBUILT/bin/arm-eabi-ar d libavcodec/libavcodec.a inverse.o
$PREBUILT/bin/arm-eabi-ld -rpath-link=$PLATFORM/usr/lib -L$PLATFORM/usr/lib -soname libffmpeg.so -shared -nostdlib -z,noexecstack -Bsymbolic --whole-archive --no-undefined -o $PREFIX/libffmpeg.so libavcodec/libavcodec.a libavformat/libavformat.a libavutil/libavutil.a libavfilter/libavfilter.a libswscale/libswscale.a libavdevice/libavdevice.a libswresample/libswresample.a -lc -lm -lz -ldl -llog --warn-once --dynamic-linker=/system/bin/linker $PREBUILT/lib/gcc/arm-eabi/4.4.3/libgcc.a
}
#arm v6
CPU=armv6
OPTIMIZE_CFLAGS="-marm -march=$CPU"
PREFIX=./android/$CPU
ADDITIONAL_CONFIGURE_FLAG=
#build_one
#arm v7vfpv3
CPU=armv7-a
OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=vfpv3-d16 -marm -march=$CPU "
PREFIX=./android/$CPU
ADDITIONAL_CONFIGURE_FLAG=
build_one
#arm v7vfp
CPU=armv7-a
OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=vfp -marm -march=$CPU "
PREFIX=./android/$CPU-vfp
ADDITIONAL_CONFIGURE_FLAG=
build_one
#arm v7n
CPU=armv7-a
OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=neon -marm -march=$CPU -mtune=cortex-a8"
PREFIX=./android/$CPU
ADDITIONAL_CONFIGURE_FLAG=--enable-neon
build_one
#arm v6+vfp
CPU=armv6
OPTIMIZE_CFLAGS="-DCMP_HAVE_VFP -mfloat-abi=softfp -mfpu=vfp -marm -march=$CPU"
PREFIX=./android/${CPU}_vfp
ADDITIONAL_CONFIGURE_FLAG=
build_one
下面是我使用的 build_android.sh文件:
#!/bin/bash
######################################################
# FFmpeg builds script for Android+ARM platform
#
# This script is released under term of
# CDDL (http://www.opensource.org/licenses/cddl1)
######################################################
# Usage:
# put this script in top of FFmpeg source tree
# ./build_android
#
# It generates binary for following architectures:
# ARMv6
# ARMv6+VFP
# ARMv7+VFPv3-d16 (Tegra2)
# ARMv7+Neon (Cortex-A8)
#
# Customizing:
# 1. Feel free to change ./configure parameters for more features
# 2. To adapt other ARM variants
# set $CPU and $OPTIMIZE_CFLAGS
# call build_one
######################################################
NDK=E:/android-ndk-r7-windows/android-ndk-r7
PLATFORM=$NDK/platforms/android-8/arch-arm/
PREBUILT=$NDK/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows
function build_one
{
# -fasm : required. Android header file uses asm keyword instead of __asm__ , but most of c dialect (like ansi,c99,gnu99) implies -fno-asm.
# ~/android/android-ndk-r4/build/platforms/android-5/arch-arm//usr/include/asm/byteorder.h: In function '___arch__swab32':
# ~/android/android-ndk-r4/build/platforms/android-5/arch-arm//usr/include/asm/byteorder.h:25: error: expected ')' before ':' token
# -fno-short-enums : optimized. Else FFmpeg obj will generate a huge number of warning for variable-size enums,
# though we may suppress them by --no-enum-size-warning, it would be better to avoid it.
# .../ld: warning: cmdutils.o uses variable-size enums yet the output is to use 32-bit enums; use of enum values across objects may fail
# --extra-libs="-lgcc" : required. Else cannot solve some runtime function symbols
# ... undefined reference to `__aeabi_f2uiz'
# --enable-protocols : required. Without this option, the file open always fails mysteriously.
# FFmpeg's av_open_input_file will invoke file format probing functions, but because most of useful demuxers has flag of zero
# which cause them are ignored during file format probling and fall to url stream parsing,
# if protocols are disabled, the file:// url cannot be opened as well.
# $PREBUILT/bin/arm-linux-androideabi-ar d libavcodec/libavcodec.a inverse.o : required.
# FFmpeg includes two copies of inverse.c both in libavutil and libavcodec for performance consideration (not sure the benifit yet)
# Without this step, final ld of generating libffmpeg.so will fail silently, if invoke ld through gcc, gcc will collect more reasonable error message.
# -llog: debug only, FFmpeg itself doesn't require it at all.
# With this option, we may simply includes "utils/Log.h" and use LOGx() to observe FFmpeg's behavior
# PS, it seems the toolchain implies -DNDEBUG somewhere, it would be safer to use following syntax
# #ifdef NDEBUG
# #undef NDEBUG
# #define HAVE_NDEBUG
# #endif
# #include "utils/Log.h"
# #ifdef HAVE_NDEBUG
# #define NDEBUG
# #undef HAVE_NDEBUG
# #endif
# --whole-archive : required. Else ld generate a small .so file (about 15k)
# --no-stdlib : required. Android doesn't use standard c runtime but invited its own wheal (bionic libc) because of license consideration.
# space before \ of configure lines: required for some options. Else next line will be merged into previous lines's content and cause problem.
# Especially the --extra-cflags, the next line will pass to gcc in this case and configure will say gcc cannot create executable.
# many options mentioned by articles over internet are implied by -O2 or -O3 already, need not repeat at all.
# two or three common optimization cflags are omitted because not sure about the trade off yet. invoke NDK build system with V=1 to find them.
# -Wl,-T,$PREBUILT/arm-eabi/lib/ldscripts/armelf.x mentioned by almost every articles over internet, but it is not required to specify at all.
# -Dipv6mr_interface=ipv6mr_ifindex : required. Android inet header doesn't use ipv6mr_interface which is required by rfc, seems it generate this user space header file directly from kernel header file, but Linux kernel has decided to keep its own name for ever and ask user space header to use rfc name.
# HAVE_SYS_UIO_H : required. Else:
# In file included from ~/android/android-ndk-r4/build/platforms/android-5/arch-arm//usr/include/linux/socket.h:29,
# from ~/android/android-ndk-r4/build/platforms/android-5/arch-arm//usr/include/sys/socket.h:33,
# from libavformat/network.h:35,
# from libavformat/utils.c:46:
#~/android/android-ndk-r4/build/platforms/android-5/arch-arm//usr/include/linux/uio.h:19: error: redefinition of 'struct iovec'
#
# --disable-doc : required because of strange bug of toolchain.
./configure --target-os=linux \
--prefix=$PREFIX \
--enable-cross-compile \
--extra-libs="-lgcc" \
--arch=arm \
--enable-memalign-hack \
--enable-gpl \
--cc=$PREBUILT/bin/arm-linux-androideabi-gcc \
--cross-prefix=$PREBUILT/bin/arm-linux-androideabi- \
--nm=$PREBUILT/bin/arm-linux-androideabi-nm \
--sysroot=$PLATFORM \
--extra-cflags=" -O3 -fpic -DANDROID -DHAVE_SYS_UIO_H=1 -Dipv6mr_interface=ipv6mr_ifindex -fasm -Wno-psabi -fno-short-enums -fno-strict-aliasing -finline-limit=300 $OPTIMIZE_CFLAGS " \
--disable-shared \
--enable-static \
--extra-ldflags="-Wl,-T,$PREBUILT/arm-linux-androideabi/lib/ldscripts/armelf_linux_eabi.x -Wl,-rpath-link=$PLATFORM/usr/lib -L$PLATFORM/usr/lib -nostdlib $PREBUILT/lib/gcc/arm-linux-androideabi/4.4.3/crtbegin.o $PREBUILT/lib/gcc/arm-linux-androideabi/4.4.3/crtend.o -L$PLATFORM/usr/lib -lc -lm -ldl -llog" \
--enable-parsers \
--enable-decoders \
--enable-swscale \
--disable-ffplay \
--disable-ffprobe \
--disable-ffserver \
--enable-network \
--enable-indevs \
--enable-optimizations \
--enable-asm \
--enable-swresample \
--disable-debug
$ADDITIONAL_CONFIGURE_FLAG
#make clean
make -j4 install
$PREBUILT/bin/arm-linux-androideabi-ar d libavcodec/libavcodec.a inverse.o
$PREBUILT/bin/arm-linux-androideabi-ld -rpath-link=$PLATFORM/usr/lib -L$PLATFORM/usr/lib -soname libffmpeg.so -shared -nostdlib -z,noexecstack -Bsymbolic --whole-archive --no-undefined -o $PREFIX/libffmpeg.so libavcodec/libavcodec.a libavformat/libavformat.a libavutil/libavutil.a libavfilter/libavfilter.a libswscale/libswscale.a libswresample/libswresample.a libavresample/libavresample.a libpostproc/libpostproc.a -lc -lm -lz -ldl -llog --warn-once --dynamic-linker=/system/bin/linker $PREBUILT/lib/gcc/arm-linux-androideabi/4.4.3/libgcc.a
}
##arm v6
#CPU=armv6
#OPTIMIZE_CFLAGS="-marm -march=$CPU"
#PREFIX=./android/$CPU
#ADDITIONAL_CONFIGURE_FLAG=
##build_one
#
##arm v7vfpv3
#CPU=armv7-a
#OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=vfpv3-d16 -marm -march=$CPU "
#PREFIX=./android/$CPU
#ADDITIONAL_CONFIGURE_FLAG=
#build_one
#
##arm v7vfp
#CPU=armv7-a
#OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=vfp -marm -march=$CPU "
#PREFIX=./android/$CPU-vfp
#ADDITIONAL_CONFIGURE_FLAG=
#build_one
#arm v7n
CPU=armv7-a
OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=neon -marm -march=$CPU -mtune=cortex-a8"
PREFIX=./android/$CPU
ADDITIONAL_CONFIGURE_FLAG=--enable-neon
build_one
##arm v6+vfp
#CPU=armv6
#OPTIMIZE_CFLAGS="-DCMP_HAVE_VFP -mfloat-abi=softfp -mfpu=vfp -marm -march=$CPU"
#PREFIX=./android/${CPU}_vfp
#ADDITIONAL_CONFIGURE_FLAG=
#build_one
6. 把编译好的libffmpeg.so 和 可执行文件 ffmpeg 文件push到 手机里面运行测试! 下面是操作步骤:
a: 拷贝文件到手机sdcard
E:\android-ndk-r7-windows\android-ndk-r7\samples\test-libstdc++\libs\armeabi>
拷贝可执行文件
adb push ffmpeg /sdcard/ttt
4507 KB/s (144248 bytes in 0.031s)
拷贝库
adb push liffmpeg.so /sdcard/ttt
3756 KB/s (8233079 bytes in 2.140s)
b: 从手机sdcard 拷贝到系统bin目录和系统lib目录
E:\android-ndk-r7-windows\android-ndk-r7\samples\test-libstdc++\libs\armeabi>adb
shell
$ su
su
# cd /system/bin
cd /system/bin
# rm ffmpeg
rm ffmpeg
# cat /sdcard/ttt/ffmpeg > ffmpeg
cat /sdcard/ttt/ffmpeg > ffmpeg
# chmod 777 ffmpeg
chmod 777 ffmpeg
# cd ../lib
cd /system/lib
# rm libffmpeg.so
rm libffmpeg.so
# cat /sdcard/ttt/libffmpeg.so >libffmpeg.so
cat /sdcard/ttt/libffmpeg.so >libffmpeg.so
# chmod 777 libffmpeg.so
chmod 777 libffmpeg.so
# cd ../bin
cd /system/bin
运行可执行文件:
./ffmpeg -i /sdcard/ttt/test.264 /sdcard/ttt/output.yuv