如何使用Ndk r9d(android.mk文件)编译ffmpeg,并只生成一个动态链接(so)库?

做事一步步,稳步前进

之前已经使用ndk交叉编译出了ffmpeg(目标平台是android的arm架构),文章记录见这里:

[记录]在ubuntu12.04上用ndk编译ffmpeg的又一种方式》、《[记录]在window+cygwin中编译ffmpeg,采用的拓展库使用linux+mingw编译》、《[记录]linux+mingw32交叉编译window平台的ffmpeg与其拓展库(x264、vpx、lame等)》、《[解决]windows7+cygwin编译和使用ffmpeg+x264》、《[记录]Dx trouble writing output: already prepared 解决办法

根据上面的文章记录,可以使用ffmpeg在android上进行音视频开发了;但是,上面的文章并没有生成一个可复用的ffmpeg动态链接库,而如果能生成一个动态库(单一个),是非常重要的一件事,所以结合上面几篇记录,和网络上的资料,经过不断尝试,终于把ffmpeg编译出单一个可以使用的so库,现大概记录一下。

1、前提

首先,说明一下大概环境,我使用的操作系统与开发工具是:ubuntu server 64位,ndk-r9d,eclipse(android adt里面带的),主要参考的是havlenapetr的项目.

2、开始工作

这里是使用ndk-build编译,需要有一定的项目结构,且需要编写android.mk文件。

我使用eclipse创建了一个android项目CidyMediaUtil,增加android native support,并把ffmpeg源项目复制到jni目录下。

开始写android.mk之前,需要先有一个可用的配置脚本,这个脚本可以先依据最开始列举的几篇记录文章写出来,并且保证脚本可用;这里使用脚本生成一些配置数据供android.mk引用,这里不需要make命令。在ffmpeg目录里创建一个脚本文件cidyconfig_ffmpeg.sh,下面是我的配置脚本内容:

#!/bin/bash
#created by cidy0106, 2014-6-5

export PREFIX=/home/cidy0106/libraries/armeabi-v7a
export NDK_ROOT=/home/cidy0106/android-ndk-r9d
export default_toolchain_name="arm-linux-androideabi-4.8" #mipsel-linux-android-4.8 x86-4.8
export API_Levels="android-9"  #android2.3.3   sdl最小要android-10,但r9d没有这个
export toolchain_install_dir="/home/cidy0106/arm-toolchain"
export NDK_SYSROOT="$toolchain_install_dir/sysroot"
export my_host_system="linux-x86_64"
export use_stl="gnustl"
export ARCH="arm"    # just for ffmpeg,fontconfig
export CPU="armv7-a"   #just for ffmpeg
export target_for_vpx="armv5te-android-gcc"   #just for vpx
export build_for_configure=x86_64-unknown-linux-gnu   #for configure --build
export compile_for_debug="yes"  #如果不想生成调试版本的话就留空,否则就yes

export PATH=$toolchain_install_dir/bin:$PATH
export CROSS_PREFIX="arm-linux-androideabi-"
export CC="arm-linux-androideabi-gcc -fPIC -DANDROID -nostdlib"
export CXX="arm-linux-androideabi-g++ -fPIC -DANDROID -nostdlib"
export LD="arm-linux-androideabi-ld"
export STRIP="arm-linux-androideabi-strip"
export AR="arm-linux-androideabi-ar"
export PKG_CONFIG_PATH="$PREFIX/lib/pkgconfig"

export CFLAGS="-march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -fPIC -DANDROID -nostdlib -I$PREFIX/include -Wl,--fix-cortex-a8 -fasm -Dipv6mr_interface=ipv6mr_ifindex -DHAVE_SYS_UIO_H=1"  #armeabi-v7a
#export defualt_cflags="-march=armv7-a -mfloat-abi=softfp -mfpu=neon -I$PREFIX/include"  #neon
export LDFLAGS="-march=armv7-a -Wl,--fix-cortex-a8 -nostdlib -L$PREFIX/lib -L$toolchain_install_dir/arm-linux-androideabi/lib -lgnustl_shared -lc -lgcc "
export CXXFLAGS="$CFLAGS"
export CPPFLAGS="$CFLAGS"

create_toolchain(){
toolchain_install_sign_cidy0106=$toolchain_install_dir/.sign_cidy0106
if [ -f $toolchain_install_sign_cidy0106 ];then
echo 'toolchain has been created'
else
$NDK_ROOT/build/tools/make-standalone-toolchain.sh --platform=$API_Levels --install-dir=$toolchain_install_dir --toolchain=$default_toolchain_name --system=$my_host_system --stl=$use_stl || exit 1
echo 'toolchain created' > $toolchain_install_sign_cidy0106
fi
}


base_dir_path=$(pwd)





#ffmpeg
ffmpeg_path=.   #ffmpeg-2.1.1
build_ffmpeg(){
cd $ffmpeg_path || exit 1
ffmpeg_enable_libs="  --e做事一步步,稳步前进

之前已经使用ndk交叉编译出了ffmpeg(目标平台是android的arm架构),文章记录见这里:

《[记录]在ubuntu12.04上用ndk编译ffmpeg的又一种方式》、《[记录]在window+cygwin中编译ffmpeg,采用的拓展库使用linux+mingw编译》、《[记录]linux+mingw32交叉编译window平台的ffmpeg与其拓展库(x264、vpx、lame等)》、《[解决]windows7+cygwin编译和使用ffmpeg+x264》、《[记录]Dx trouble writing output: already prepared 解决办法》

根据上面的文章记录,可以使用ffmpeg在android上进行音视频开发了;但是,上面的文章并没有生成一个可复用的ffmpeg动态链接库,而如果能生成一个动态库(单一个),是非常重要的一件事,所以结合上面几篇记录,和网络上的资料,经过不断尝试,终于把ffmpeg编译出单一个可以使用的so库,现大概记录一下。
1、前提

首先,说明一下大概环境,我使用的操作系统与开发工具是:ubuntu server 64位,ndk-r9d,eclipse(android adt里面带的),主要参考的是havlenapetr的项目.
2、开始工作

这里是使用ndk-build编译,需要有一定的项目结构,且需要编写android.mk文件。

我使用eclipse创建了一个android项目CidyMediaUtil,增加android native support,并把ffmpeg源项目复制到jni目录下。

开始写android.mk之前,需要先有一个可用的配置脚本,这个脚本可以先依据最开始列举的几篇记录文章写出来,并且保证脚本可用;这里使用脚本生成一些配置数据供android.mk引用,这里不需要make命令。在ffmpeg目录里创建一个脚本文件cidyconfig_ffmpeg.sh,下面是我的配置脚本内容:

#!/bin/bash
#created by cidy0106, 2014-6-5

export PREFIX=/home/cidy0106/libraries/armeabi-v7a
export NDK_ROOT=/home/cidy0106/android-ndk-r9d
export default_toolchain_name="arm-linux-androideabi-4.8" #mipsel-linux-android-4.8 x86-4.8
export API_Levels="android-9"  #android2.3.3   sdl最小要android-10,但r9d没有这个
export toolchain_install_dir="/home/cidy0106/arm-toolchain"
export NDK_SYSROOT="$toolchain_install_dir/sysroot"
export my_host_system="linux-x86_64"
export use_stl="gnustl"
export ARCH="arm"    # just for ffmpeg,fontconfig
export CPU="armv7-a"   #just for ffmpeg
export target_for_vpx="armv5te-android-gcc"   #just for vpx
export build_for_configure=x86_64-unknown-linux-gnu   #for configure --build
export compile_for_debug="yes"  #如果不想生成调试版本的话就留空,否则就yes

export PATH=$toolchain_install_dir/bin:$PATH
export CROSS_PREFIX="arm-linux-androideabi-"
export CC="arm-linux-androideabi-gcc -fPIC -DANDROID -nostdlib"
export CXX="arm-linux-androideabi-g++ -fPIC -DANDROID -nostdlib"
export LD="arm-linux-androideabi-ld"
export STRIP="arm-linux-androideabi-strip"
export AR="arm-linux-androideabi-ar"
export PKG_CONFIG_PATH="$PREFIX/lib/pkgconfig"

export CFLAGS="-march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -fPIC -DANDROID -nostdlib -I$PREFIX/include -Wl,--fix-cortex-a8 -fasm -Dipv6mr_interface=ipv6mr_ifindex -DHAVE_SYS_UIO_H=1"  #armeabi-v7a
#export defualt_cflags="-march=armv7-a -mfloat-abi=softfp -mfpu=neon -I$PREFIX/include"  #neon
export LDFLAGS="-march=armv7-a -Wl,--fix-cortex-a8 -nostdlib -L$PREFIX/lib -L$toolchain_install_dir/arm-linux-androideabi/lib -lgnustl_shared -lc -lgcc "
export CXXFLAGS="$CFLAGS"
export CPPFLAGS="$CFLAGS"

create_toolchain(){
toolchain_install_sign_cidy0106=$toolchain_install_dir/.sign_cidy0106
if [ -f $toolchain_install_sign_cidy0106 ];then
echo 'toolchain has been created'
else
$NDK_ROOT/build/tools/make-standalone-toolchain.sh --platform=$API_Levels --install-dir=$toolchain_install_dir --toolchain=$default_toolchain_name --system=$my_host_system --stl=$use_stl || exit 1
echo 'toolchain created' > $toolchain_install_sign_cidy0106
fi
}


base_dir_path=$(pwd)





#ffmpeg
ffmpeg_path=.   #ffmpeg-2.1.1
build_ffmpeg(){
cd $ffmpeg_path || exit 1
ffmpeg_enable_libs="  --enable-libfaac --enable-libfdk-aac  --enable-libgsm  --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libspeex --enable-libtheora --enable-libtwolame  --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxavs --enable-libxvid"
if [ $compile_for_debug = "yes" ] ;then
echo "调试版本。。。。。。"
./configure --prefix=$PREFIX \
--enable-cross-compile --arch=$ARCH --cpu=$CPU \
--cross-prefix="$CROSS_PREFIX" \
--target-os=linux --sysroot=$NDK_SYSROOT \
--cc="$CC" --cxx="$CXX" \
--extra-cflags="$CFLAGS" \
--extra-ldflags="$LDFLAGS" \
--extra-libs="-lgnustl_shared -ldl -lc -lgcc " \
--enable-static --disable-shared \
--disable-ffplay --disable-ffprobe --disable-ffserver --disable-doc \
--disable-htmlpages  --disable-manpages  --disable-podpages  --disable-txtpages  --disable-symver --enable-debug --disable-asm --disable-stripping --disable-optimizations \
--enable-pic --enable-gpl --enable-version3 --enable-nonfree \
--disable-iconv --disable-bzlib $ffmpeg_enable_libs || exit 1
else
./configure --prefix=$PREFIX \
--enable-cross-compile --arch=$ARCH --cpu=$CPU \
--cross-prefix="$CROSS_PREFIX" \
--target-os=linux --sysroot=$NDK_SYSROOT \
--cc="$CC" --cxx="$CXX" \
--extra-cflags="$CFLAGS" \
--extra-ldflags="$LDFLAGS" \
--extra-libs="-lgnustl_shared -ldl -lc -lgcc " \
--enable-static --disable-shared \
--disable-ffplay --disable-ffprobe --disable-ffserver --disable-doc \
--disable-htmlpages  --disable-manpages  --disable-podpages  --disable-txtpages  --disable-symver \
--enable-pic --enable-asm --enable-gpl --enable-version3 --enable-nonfree \
--disable-iconv --disable-bzlib $ffmpeg_enable_libs || exit 1
fi

#sed -i "s/HAVE_LLRINT 0/HAVE_LLRINT 1/g" config.h
#sed -i "s/HAVE_LLRINTF 0/HAVE_LLRINTF 1/g" config.h
#sed -i "s/HAVE_LRINT 0/HAVE_LRINT 1/g" config.h
#sed -i "s/HAVE_LRINTF 0/HAVE_LRINTF 1/g" config.h
#sed -i "s/HAVE_ROUND 0/HAVE_ROUND 1/g" config.h
#sed -i "s/HAVE_ROUNDF 0/HAVE_ROUNDF 1/g" config.h
#sed -i "s/HAVE_ISINF 0/HAVE_ISINF 1/g" config.h
#sed -i "s/HAVE_ISNAN 0/HAVE_ISNAN 1/g" config.h
#make clean && make -j4 && make install || exit 1
#cp libavutil/colorspace.h $PREFIX/include/libavutil/colorspace.h
#cp config.h $PREFIX/include/config.h
cd $base_dir_path
}

create_toolchain

build_ffmpeg

执行配置脚本即可

 
3、开始写android.mk文件

首先,在jni目录下,先增加两个文件:Android.mk和Application.mk,内容分别如下:

#android.mk
include $(call all-subdir-makefiles)

 #application.mk

# Uncomment this if you're using STL in your project
# See CPLUSPLUS-SUPPORT.html in the NDK documentation for more information
APP_STL :=gnustl_shared  #  stlport_shared
#ARM_MODE := arm
APP_ABI :=armeabi-v7a  # armeabi-v7a x86 
#APP_CPPFLAGS += -fexceptions -frtti
APP_PLATFORM:=android-9

这里假定ffmpeg项目的源文件目录路径是jni/ffmpeg ,下面将在jni/ffmpeg下各个目录编写Android.mk文件,内容如下(为了方便,在文件开始处注释了路径信息):

#jni/ffmpeg/android.mk

LOCAL_PATH := $(call my-dir)  
include $(CLEAR_VARS)  
LOCAL_WHOLE_STATIC_LIBRARIES := libavformat libavcodec libavutil libpostproc libswscale libswresample  
LOCAL_MODULE := ffmpeg 
##LOCAL_C_INCLUDES := /home/cidy0106/libraries/armeabi-v7a/include
LOCAL_CFLAGS += -DGL_GLEXT_PROTOTYPES  -D__STDC_CONSTANT_MACROS -I/home/cidy0106/libraries/armeabi-v7a/include 
LOCAL_SHARED_LIBRARIES := gnustl_shared SDL2 # ffmpeg
LOCAL_STATIC_LIBRARIES := avformat avcodec avutil  swscale swresample avdevice x264 fdk-aac faac  xavs  gsm mp3lame opencore-amrwb opencore-amrnb opus vorbis vorbisfile vorbisenc  theora theoraenc theoradec speex  speexdsp  \
twolame vo-aacenc vo-amrwbenc vpx  xvidcore ogg avfilter postproc #avresample 
LOCAL_LDFLAGS :=-L/home/cidy0106/libraries/armeabi-v7a/lib -lx264  -lfdk-aac  \
 -lxavs -logg -lfaac -lgsm -lmp3lame -lopencore-amrwb -lopencore-amrnb -lopus -lvorbis -lvorbisfile -lvorbisenc  -ltheora -ltheoraenc -ltheoradec -lspeex  -lspeexdsp  \
-ltwolame -lvo-aacenc -lvo-amrwbenc -lvpx -lxvidcore

LOCAL_LDLIBS :=-ldl -lz -lm -lc -lGLESv1_CM -lGLESv2 -llog  #\
-L/home/cidy0106/libraries/armeabi-v7a/lib \
-lavformat -lavcodec -lavutil -lswscale -lpostproc -lavfilter  -lswresample -lavresample -lavdevice #\
-logg -lfaac -lfdk-aac -lgsm -lmp3lame -lopencore-amrnb -lopencore-amrwb \
-lopus -lspeex -lvorbis -lvorbisenc -ltheora -ltheoradec -ltheoraenc -ltwolame -lvo-aacenc -lvo-amrwbenc  \
-lvpx -lx264 -lxavs -lxvidcore  

#ifeq ($(TARGET_ARCH_ABI),armeabi-v7a)    
## 采用NEON优化技术    
#LOCAL_ARM_NEON := true   
#endif
include $(BUILD_SHARED_LIBRARY)  
include $(call all-makefiles-under,$(LOCAL_PATH))

#jni/ffmpeg/av.mk

 # LOCAL_PATH is one of libavutil, libavcodec, libavformat, or libswscale
#include $(LOCAL_PATH)/../config-$(TARGET_ARCH).mak
include $(LOCAL_PATH)/../config.mak
EXTEND_LOCAL_C_INCLUDES := /home/cidy0106/libraries/armeabi-v7a/include
OBJS :=
OBJS-yes :=
MMX-OBJS-yes :=
include $(LOCAL_PATH)/Makefile
# collect objects
OBJS-$(HAVE_MMX) += $(MMX-OBJS-yes)
OBJS += $(OBJS-yes)
FFNAME := lib$(NAME)
FFLIBS := $(foreach,NAME,$(FFLIBS),lib$(NAME))
FFCFLAGS= -DHAVE_AV_CONFIG_H -Wno-sign-compare -Wno-switch -Wno-pointer-sign
FFCFLAGS += -DTARGET_CONFIG=\"config-$(TARGET_ARCH).h\"
ALL_S_FILES := $(wildcard $(LOCAL_PATH)/$(TARGET_ARCH)/*.S)
ALL_S_FILES := $(addprefix $(TARGET_ARCH)/, $(notdir $(ALL_S_FILES)))
ifneq ($(ALL_S_FILES),)
ALL_S_OBJS := $(patsubst %.S,%.o,$(ALL_S_FILES))
C_OBJS := $(filter-out $(ALL_S_OBJS),$(OBJS))
S_OBJS := $(filter $(ALL_S_OBJS),$(OBJS))
else
C_OBJS := $(OBJS)
S_OBJS :=
endif
C_FILES := $(patsubst %.o,%.c,$(C_OBJS))
S_FILES := $(patsubst %.o,%.S,$(S_OBJS))
FFFILES := $(sort $(S_FILES)) $(sort $(C_FILES))

 

#jni/ffmpeg/libavformat/android.mk

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include $(LOCAL_PATH)/../av.mk
LOCAL_SRC_FILES := $(FFFILES)
#LOCAL_C_INCLUDES :=        \
#$(LOCAL_PATH)        \
#$(LOCAL_PATH)/..
$(warning $(LOCAL_C_INCLUDES))

LOCAL_C_INCLUDES :=
LOCAL_C_INCLUDES += $(SYSROOT_INC)/usr/include
LOCAL_C_INCLUDES += $(LOCAL_PATH)/.. $(EXTEND_LOCAL_C_INCLUDES)   $(LOCAL_PATH) 
LOCAL_CFLAGS += $(FFCFLAGS)
LOCAL_CFLAGS += -include "string.h" -Dipv6mr_interface=ipv6mr_ifindex
LOCAL_LDLIBS := -lz
LOCAL_STATIC_LIBRARIES := $(FFLIBS)
LOCAL_MODULE := $(FFNAME)
include $(BUILD_STATIC_LIBRARY)

#jni/ffmpeg/libavcodec/android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include $(LOCAL_PATH)/../av.mk
LOCAL_SRC_FILES := $(FFFILES)
#LOCAL_C_INCLUDES :=   \
#$(LOCAL_PATH)        \
#$(LOCAL_PATH)/..  
LOCAL_C_INCLUDES :=
LOCAL_C_INCLUDES += $(SYSROOT_INC)/usr/include
LOCAL_C_INCLUDES += $(LOCAL_PATH)/.. $(EXTEND_LOCAL_C_INCLUDES)   
LOCAL_CFLAGS += $(FFCFLAGS)
LOCAL_LDLIBS := -lz -L/home/cidy0106/libraries/armeabi-v7a/lib -lx264  -lfdk-aac  \
 -lxavs -logg -lfaac -lgsm -lmp3lame -lopencore-amrwb -lopencore-amrnb -lopus -lvorbis -lvorbisfile -lvorbisenc  -ltheora -ltheoraenc -ltheoradec -lspeex  -lspeexdsp  \
-ltwolame -lvo-aacenc -lvo-amrwbenc -lvpx -lxvidcore
LOCAL_STATIC_LIBRARIES := $(FFLIBS)
LOCAL_MODULE := $(FFNAME)
include $(BUILD_STATIC_LIBRARY)

#jni/ffmpeg/libavutil/android.mk
#jni/ffmpeg/libpostproc/android.mk
#jni/ffmpeg/libswscale/android.mk
#jni/ffmpeg/libswresample/android.mk
#jni/ffmpeg/libavfilter/android.mk
#jni/ffmpeg/libavdevice/android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include $(LOCAL_PATH)/../av.mk
LOCAL_SRC_FILES := $(FFFILES)
#LOCAL_C_INCLUDES :=        \
#$(LOCAL_PATH)        \
#$(LOCAL_PATH)/..
LOCAL_C_INCLUDES :=
LOCAL_C_INCLUDES += $(SYSROOT_INC)/usr/include
LOCAL_C_INCLUDES += $(LOCAL_PATH)/.. $(EXTEND_LOCAL_C_INCLUDES)   
LOCAL_CFLAGS += $(FFCFLAGS)
LOCAL_STATIC_LIBRARIES := $(FFLIBS)
LOCAL_MODULE := $(FFNAME)
include $(BUILD_STATIC_LIBRARY)

4、错误与修改部分

经过上面可以试着ndk-build,我用eclipse编译了一下,出现了一些错误,下面是解决记录:

a、提示找到文件$(SUBDIR)../config.mak的错误,该错误出现在ffmpeg下的各个子目录(libavformat libavcodec libavutil libpostproc libswscale libswresample等)的makefile里,打开相关的makefile文件,注释掉即可,如下:

# include $(SUBDIR)../config.mak

b、提示restrict 相关错误,这可能是无法识别这个语法吧?在config.h里,修改为以下内容:

  #define av_restrict   //#define av_restrict restrict

c、出现av_small_strptime相关错误,那么重新使用我上面给出的android.mk,应该就可以解决了,应该跟ndk搜索头文件的顺序有关的,最主要就是注意其中的配置,如下部分:

 

#LOCAL_C_INCLUDES :=   \
#$(LOCAL_PATH)        \
#$(LOCAL_PATH)/..  
LOCAL_C_INCLUDES :=
LOCAL_C_INCLUDES += $(SYSROOT_INC)/usr/include
LOCAL_C_INCLUDES += $(LOCAL_PATH)/.. $(EXTEND_LOCAL_C_INCLUDES)   

 

 

 

 

 

 nable-libfaac --enable-libfdk-aac  --enable-libgsm  --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libspeex --enable-libtheora --enable-libtwolame  --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxavs --enable-libxvid"
if [ $compile_for_debug = "yes" ] ;then
echo "调试版本。。。。。。"
./configure --prefix=$PREFIX \
--enable-cross-compile --arch=$ARCH --cpu=$CPU \
--cross-prefix="$CROSS_PREFIX" \

做事一步步,稳步前进

之前已经使用ndk交叉编译出了ffmpeg(目标平台是android的arm架构),文章记录见这里:

[记录]在ubuntu12.04上用ndk编译ffmpeg的又一种方式》、《[记录]在window+cygwin中编译ffmpeg,采用的拓展库使用linux+mingw编译》、《[记录]linux+mingw32交叉编译window平台的ffmpeg与其拓展库(x264、vpx、lame等)》、《[解决]windows7+cygwin编译和使用ffmpeg+x264》、《[记录]Dx trouble writing output: already prepared 解决办法

根 据上面的文章记录,可以使用ffmpeg在android上进行音视频开发了;但是,上面的文章并没有生成一个可复用的ffmpeg动态链接库,而如果能 生成一个动态库(单一个),是非常重要的一件事,所以结合上面几篇记录,和网络上的资料,经过不断尝试,终于把ffmpeg编译出单一个可以使用的so 库,现大概记录一下。

1、前提

首先,说明一下大概环境,我使用的操作系统与开发工具是:ubuntu server 64位,ndk-r9d,eclipse(android adt里面带的),主要参考的是havlenapetr的项目.

2、开始工作

这里是使用ndk-build编译,需要有一定的项目结构,且需要编写android.mk文件。

我使用eclipse创建了一个android项目CidyMediaUtil,增加android native support,并把ffmpeg源项目复制到jni目录下。

开 始写android.mk之前,需要先有一个可用的配置脚本,这个脚本可以先依据最开始列举的几篇记录文章写出来,并且保证脚本可用;这里使用脚本生成一 些配置数据供android.mk引用,这里不需要make命令。在ffmpeg目录里创建一个脚本文件cidyconfig_ffmpeg.sh,下面 是我的配置脚本内容:

#!/bin/bash
#created by cidy0106, 2014-6-5

export PREFIX=/home/cidy0106/libraries/armeabi-v7a
export NDK_ROOT=/home/cidy0106/android-ndk-r9d
export default_toolchain_name="arm-linux-androideabi-4.8" #mipsel-linux-android-4.8 x86-4.8
export API_Levels="android-9"  #android2.3.3   sdl最小要android-10,但r9d没有这个
export toolchain_install_dir="/home/cidy0106/arm-toolchain"
export NDK_SYSROOT="$toolchain_install_dir/sysroot"
export my_host_system="linux-x86_64"
export use_stl="gnustl"
export ARCH="arm"    # just for ffmpeg,fontconfig
export CPU="armv7-a"   #just for ffmpeg
export target_for_vpx="armv5te-android-gcc"   #just for vpx
export build_for_configure=x86_64-unknown-linux-gnu   #for configure --build
export compile_for_debug="yes"  #如果不想生成调试版本的话就留空,否则就yes

export PATH=$toolchain_install_dir/bin:$PATH
export CROSS_PREFIX="arm-linux-androideabi-"
export CC="arm-linux-androideabi-gcc -fPIC -DANDROID -nostdlib"
export CXX="arm-linux-androideabi-g++ -fPIC -DANDROID -nostdlib"
export LD="arm-linux-androideabi-ld"
export STRIP="arm-linux-androideabi-strip"
export AR="arm-linux-androideabi-ar"
export PKG_CONFIG_PATH="$PREFIX/lib/pkgconfig"

export CFLAGS="-march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -fPIC -DANDROID -nostdlib -I$PREFIX/include -Wl,--fix-cortex-a8 -fasm -Dipv6mr_interface=ipv6mr_ifindex -DHAVE_SYS_UIO_H=1"  #armeabi-v7a
#export defualt_cflags="-march=armv7-a -mfloat-abi=softfp -mfpu=neon -I$PREFIX/include"  #neon
export LDFLAGS="-march=armv7-a -Wl,--fix-cortex-a8 -nostdlib -L$PREFIX/lib -L$toolchain_install_dir/arm-linux-androideabi/lib -lgnustl_shared -lc -lgcc "
export CXXFLAGS="$CFLAGS"
export CPPFLAGS="$CFLAGS"

create_toolchain(){
toolchain_install_sign_cidy0106=$toolchain_install_dir/.sign_cidy0106
if [ -f $toolchain_install_sign_cidy0106 ];then
echo 'toolchain has been created'
else
$NDK_ROOT/build/tools/make-standalone-toolchain.sh --platform=$API_Levels --install-dir=$toolchain_install_dir --toolchain=$default_toolchain_name --system=$my_host_system --stl=$use_stl || exit 1
echo 'toolchain created' > $toolchain_install_sign_cidy0106
fi
}


base_dir_path=$(pwd)





#ffmpeg
ffmpeg_path=.   #ffmpeg-2.1.1
build_ffmpeg(){
cd $ffmpeg_path || exit 1
ffmpeg_enable_libs="  --e做事一步步,稳步前进

之前已经使用ndk交叉编译出了ffmpeg(目标平台是android的arm架构),文章记录见这里:

《[记录]在ubuntu12.04上用ndk编译ffmpeg的又一种方式》、《[记录]在window+cygwin中编译ffmpeg,采用的拓展库使用linux+mingw编译》、《[记录]linux+mingw32交叉编译window平台的ffmpeg与其拓展库(x264、vpx、lame等)》、《[解决]windows7+cygwin编译和使用ffmpeg+x264》、《[记录]Dx trouble writing output: already prepared 解决办法》

根据上面的文章记录,可以使用ffmpeg在android上进行音视频开发了;但是,上面的文章并没有生成一个可复用的ffmpeg动态链接库,而如果能生成一个动态库(单一个),是非常重要的一件事,所以结合上面几篇记录,和网络上的资料,经过不断尝试,终于把ffmpeg编译出单一个可以使用的so库,现大概记录一下。
1、前提

首先,说明一下大概环境,我使用的操作系统与开发工具是:ubuntu server 64位,ndk-r9d,eclipse(android adt里面带的),主要参考的是havlenapetr的项目.
2、开始工作

这里是使用ndk-build编译,需要有一定的项目结构,且需要编写android.mk文件。

我使用eclipse创建了一个android项目CidyMediaUtil,增加android native support,并把ffmpeg源项目复制到jni目录下。

开始写android.mk之前,需要先有一个可用的配置脚本,这个脚本可以先依据最开始列举的几篇记录文章写出来,并且保证脚本可用;这里使用脚本生成一些配置数据供android.mk引用,这里不需要make命令。在ffmpeg目录里创建一个脚本文件cidyconfig_ffmpeg.sh,下面是我的配置脚本内容:

#!/bin/bash
#created by cidy0106, 2014-6-5

export PREFIX=/home/cidy0106/libraries/armeabi-v7a
export NDK_ROOT=/home/cidy0106/android-ndk-r9d
export default_toolchain_name="arm-linux-androideabi-4.8" #mipsel-linux-android-4.8 x86-4.8
export API_Levels="android-9"  #android2.3.3   sdl最小要android-10,但r9d没有这个
export toolchain_install_dir="/home/cidy0106/arm-toolchain"
export NDK_SYSROOT="$toolchain_install_dir/sysroot"
export my_host_system="linux-x86_64"
export use_stl="gnustl"
export ARCH="arm"    # just for ffmpeg,fontconfig
export CPU="armv7-a"   #just for ffmpeg
export target_for_vpx="armv5te-android-gcc"   #just for vpx
export build_for_configure=x86_64-unknown-linux-gnu   #for configure --build
export compile_for_debug="yes"  #如果不想生成调试版本的话就留空,否则就yes

export PATH=$toolchain_install_dir/bin:$PATH
export CROSS_PREFIX="arm-linux-androideabi-"
export CC="arm-linux-androideabi-gcc -fPIC -DANDROID -nostdlib"
export CXX="arm-linux-androideabi-g++ -fPIC -DANDROID -nostdlib"
export LD="arm-linux-androideabi-ld"
export STRIP="arm-linux-androideabi-strip"
export AR="arm-linux-androideabi-ar"
export PKG_CONFIG_PATH="$PREFIX/lib/pkgconfig"

export CFLAGS="-march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -fPIC -DANDROID -nostdlib -I$PREFIX/include -Wl,--fix-cortex-a8 -fasm -Dipv6mr_interface=ipv6mr_ifindex -DHAVE_SYS_UIO_H=1"  #armeabi-v7a
#export defualt_cflags="-march=armv7-a -mfloat-abi=softfp -mfpu=neon -I$PREFIX/include"  #neon
export LDFLAGS="-march=armv7-a -Wl,--fix-cortex-a8 -nostdlib -L$PREFIX/lib -L$toolchain_install_dir/arm-linux-androideabi/lib -lgnustl_shared -lc -lgcc "
export CXXFLAGS="$CFLAGS"
export CPPFLAGS="$CFLAGS"

create_toolchain(){
toolchain_install_sign_cidy0106=$toolchain_install_dir/.sign_cidy0106
if [ -f $toolchain_install_sign_cidy0106 ];then
echo 'toolchain has been created'
else
$NDK_ROOT/build/tools/make-standalone-toolchain.sh --platform=$API_Levels --install-dir=$toolchain_install_dir --toolchain=$default_toolchain_name --system=$my_host_system --stl=$use_stl || exit 1
echo 'toolchain created' > $toolchain_install_sign_cidy0106
fi
}


base_dir_path=$(pwd)





#ffmpeg
ffmpeg_path=.   #ffmpeg-2.1.1
build_ffmpeg(){
cd $ffmpeg_path || exit 1
ffmpeg_enable_libs="  --enable-libfaac --enable-libfdk-aac  --enable-libgsm  --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libspeex --enable-libtheora --enable-libtwolame  --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxavs --enable-libxvid"
if [ $compile_for_debug = "yes" ] ;then
echo "调试版本。。。。。。"
./configure --prefix=$PREFIX \
--enable-cross-compile --arch=$ARCH --cpu=$CPU \
--cross-prefix="$CROSS_PREFIX" \
--target-os=linux --sysroot=$NDK_SYSROOT \
--cc="$CC" --cxx="$CXX" \
--extra-cflags="$CFLAGS" \
--extra-ldflags="$LDFLAGS" \
--extra-libs="-lgnustl_shared -ldl -lc -lgcc " \
--enable-static --disable-shared \
--disable-ffplay --disable-ffprobe --disable-ffserver --disable-doc \
--disable-htmlpages  --disable-manpages  --disable-podpages  --disable-txtpages  --disable-symver --enable-debug --disable-asm --disable-stripping --disable-optimizations \
--enable-pic --enable-gpl --enable-version3 --enable-nonfree \
--disable-iconv --disable-bzlib $ffmpeg_enable_libs || exit 1
else
./configure --prefix=$PREFIX \
--enable-cross-compile --arch=$ARCH --cpu=$CPU \
--cross-prefix="$CROSS_PREFIX" \
--target-os=linux --sysroot=$NDK_SYSROOT \
--cc="$CC" --cxx="$CXX" \
--extra-cflags="$CFLAGS" \
--extra-ldflags="$LDFLAGS" \
--extra-libs="-lgnustl_shared -ldl -lc -lgcc " \
--enable-static --disable-shared \
--disable-ffplay --disable-ffprobe --disable-ffserver --disable-doc \
--disable-htmlpages  --disable-manpages  --disable-podpages  --disable-txtpages  --disable-symver \
--enable-pic --enable-asm --enable-gpl --enable-version3 --enable-nonfree \
--disable-iconv --disable-bzlib $ffmpeg_enable_libs || exit 1
fi

#sed -i "s/HAVE_LLRINT 0/HAVE_LLRINT 1/g" config.h
#sed -i "s/HAVE_LLRINTF 0/HAVE_LLRINTF 1/g" config.h
#sed -i "s/HAVE_LRINT 0/HAVE_LRINT 1/g" config.h
#sed -i "s/HAVE_LRINTF 0/HAVE_LRINTF 1/g" config.h
#sed -i "s/HAVE_ROUND 0/HAVE_ROUND 1/g" config.h
#sed -i "s/HAVE_ROUNDF 0/HAVE_ROUNDF 1/g" config.h
#sed -i "s/HAVE_ISINF 0/HAVE_ISINF 1/g" config.h
#sed -i "s/HAVE_ISNAN 0/HAVE_ISNAN 1/g" config.h
#make clean && make -j4 && make install || exit 1
#cp libavutil/colorspace.h $PREFIX/include/libavutil/colorspace.h
#cp config.h $PREFIX/include/config.h
cd $base_dir_path
}

create_toolchain

build_ffmpeg

执行配置脚本即可

 
3、开始写android.mk文件

首先,在jni目录下,先增加两个文件:Android.mk和Application.mk,内容分别如下:

#android.mk
include $(call all-subdir-makefiles)

 #application.mk

# Uncomment this if you're using STL in your project
# See CPLUSPLUS-SUPPORT.html in the NDK documentation for more information
APP_STL :=gnustl_shared  #  stlport_shared
#ARM_MODE := arm
APP_ABI :=armeabi-v7a  # armeabi-v7a x86 
#APP_CPPFLAGS += -fexceptions -frtti
APP_PLATFORM:=android-9

这里假定ffmpeg项目的源文件目录路径是jni/ffmpeg ,下面将在jni/ffmpeg下各个目录编写Android.mk文件,内容如下(为了方便,在文件开始处注释了路径信息):

#jni/ffmpeg/android.mk

LOCAL_PATH := $(call my-dir)  
include $(CLEAR_VARS)  
LOCAL_WHOLE_STATIC_LIBRARIES := libavformat libavcodec libavutil libpostproc libswscale libswresample  
LOCAL_MODULE := ffmpeg 
##LOCAL_C_INCLUDES := /home/cidy0106/libraries/armeabi-v7a/include
LOCAL_CFLAGS += -DGL_GLEXT_PROTOTYPES  -D__STDC_CONSTANT_MACROS -I/home/cidy0106/libraries/armeabi-v7a/include 
LOCAL_SHARED_LIBRARIES := gnustl_shared SDL2 # ffmpeg
LOCAL_STATIC_LIBRARIES := avformat avcodec avutil  swscale swresample avdevice x264 fdk-aac faac  xavs  gsm mp3lame opencore-amrwb opencore-amrnb opus vorbis vorbisfile vorbisenc  theora theoraenc theoradec speex  speexdsp  \
twolame vo-aacenc vo-amrwbenc vpx  xvidcore ogg avfilter postproc #avresample 
LOCAL_LDFLAGS :=-L/home/cidy0106/libraries/armeabi-v7a/lib -lx264  -lfdk-aac  \
 -lxavs -logg -lfaac -lgsm -lmp3lame -lopencore-amrwb -lopencore-amrnb -lopus -lvorbis -lvorbisfile -lvorbisenc  -ltheora -ltheoraenc -ltheoradec -lspeex  -lspeexdsp  \
-ltwolame -lvo-aacenc -lvo-amrwbenc -lvpx -lxvidcore

LOCAL_LDLIBS :=-ldl -lz -lm -lc -lGLESv1_CM -lGLESv2 -llog  #\
-L/home/cidy0106/libraries/armeabi-v7a/lib \
-lavformat -lavcodec -lavutil -lswscale -lpostproc -lavfilter  -lswresample -lavresample -lavdevice #\
-logg -lfaac -lfdk-aac -lgsm -lmp3lame -lopencore-amrnb -lopencore-amrwb \
-lopus -lspeex -lvorbis -lvorbisenc -ltheora -ltheoradec -ltheoraenc -ltwolame -lvo-aacenc -lvo-amrwbenc  \
-lvpx -lx264 -lxavs -lxvidcore  

#ifeq ($(TARGET_ARCH_ABI),armeabi-v7a)    
## 采用NEON优化技术    
#LOCAL_ARM_NEON := true   
#endif
include $(BUILD_SHARED_LIBRARY)  
include $(call all-makefiles-under,$(LOCAL_PATH))

#jni/ffmpeg/av.mk

 # LOCAL_PATH is one of libavutil, libavcodec, libavformat, or libswscale
#include $(LOCAL_PATH)/../config-$(TARGET_ARCH).mak
include $(LOCAL_PATH)/../config.mak
EXTEND_LOCAL_C_INCLUDES := /home/cidy0106/libraries/armeabi-v7a/include
OBJS :=
OBJS-yes :=
MMX-OBJS-yes :=
include $(LOCAL_PATH)/Makefile
# collect objects
OBJS-$(HAVE_MMX) += $(MMX-OBJS-yes)
OBJS += $(OBJS-yes)
FFNAME := lib$(NAME)
FFLIBS := $(foreach,NAME,$(FFLIBS),lib$(NAME))
FFCFLAGS= -DHAVE_AV_CONFIG_H -Wno-sign-compare -Wno-switch -Wno-pointer-sign
FFCFLAGS += -DTARGET_CONFIG=\"config-$(TARGET_ARCH).h\"
ALL_S_FILES := $(wildcard $(LOCAL_PATH)/$(TARGET_ARCH)/*.S)
ALL_S_FILES := $(addprefix $(TARGET_ARCH)/, $(notdir $(ALL_S_FILES)))
ifneq ($(ALL_S_FILES),)
ALL_S_OBJS := $(patsubst %.S,%.o,$(ALL_S_FILES))
C_OBJS := $(filter-out $(ALL_S_OBJS),$(OBJS))
S_OBJS := $(filter $(ALL_S_OBJS),$(OBJS))
else
C_OBJS := $(OBJS)
S_OBJS :=
endif
C_FILES := $(patsubst %.o,%.c,$(C_OBJS))
S_FILES := $(patsubst %.o,%.S,$(S_OBJS))
FFFILES := $(sort $(S_FILES)) $(sort $(C_FILES))

 

#jni/ffmpeg/libavformat/android.mk

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include $(LOCAL_PATH)/../av.mk
LOCAL_SRC_FILES := $(FFFILES)
#LOCAL_C_INCLUDES :=        \
#$(LOCAL_PATH)        \
#$(LOCAL_PATH)/..
$(warning $(LOCAL_C_INCLUDES))

LOCAL_C_INCLUDES :=
LOCAL_C_INCLUDES += $(SYSROOT_INC)/usr/include
LOCAL_C_INCLUDES += $(LOCAL_PATH)/.. $(EXTEND_LOCAL_C_INCLUDES)   $(LOCAL_PATH) 
LOCAL_CFLAGS += $(FFCFLAGS)
LOCAL_CFLAGS += -include "string.h" -Dipv6mr_interface=ipv6mr_ifindex
LOCAL_LDLIBS := -lz
LOCAL_STATIC_LIBRARIES := $(FFLIBS)
LOCAL_MODULE := $(FFNAME)
include $(BUILD_STATIC_LIBRARY)

#jni/ffmpeg/libavcodec/android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include $(LOCAL_PATH)/../av.mk
LOCAL_SRC_FILES := $(FFFILES)
#LOCAL_C_INCLUDES :=   \
#$(LOCAL_PATH)        \
#$(LOCAL_PATH)/..  
LOCAL_C_INCLUDES :=
LOCAL_C_INCLUDES += $(SYSROOT_INC)/usr/include
LOCAL_C_INCLUDES += $(LOCAL_PATH)/.. $(EXTEND_LOCAL_C_INCLUDES)   
LOCAL_CFLAGS += $(FFCFLAGS)
LOCAL_LDLIBS := -lz -L/home/cidy0106/libraries/armeabi-v7a/lib -lx264  -lfdk-aac  \
 -lxavs -logg -lfaac -lgsm -lmp3lame -lopencore-amrwb -lopencore-amrnb -lopus -lvorbis -lvorbisfile -lvorbisenc  -ltheora -ltheoraenc -ltheoradec -lspeex  -lspeexdsp  \
-ltwolame -lvo-aacenc -lvo-amrwbenc -lvpx -lxvidcore
LOCAL_STATIC_LIBRARIES := $(FFLIBS)
LOCAL_MODULE := $(FFNAME)
include $(BUILD_STATIC_LIBRARY)

#jni/ffmpeg/libavutil/android.mk
#jni/ffmpeg/libpostproc/android.mk
#jni/ffmpeg/libswscale/android.mk
#jni/ffmpeg/libswresample/android.mk
#jni/ffmpeg/libavfilter/android.mk
#jni/ffmpeg/libavdevice/android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include $(LOCAL_PATH)/../av.mk
LOCAL_SRC_FILES := $(FFFILES)
#LOCAL_C_INCLUDES :=        \
#$(LOCAL_PATH)        \
#$(LOCAL_PATH)/..
LOCAL_C_INCLUDES :=
LOCAL_C_INCLUDES += $(SYSROOT_INC)/usr/include
LOCAL_C_INCLUDES += $(LOCAL_PATH)/.. $(EXTEND_LOCAL_C_INCLUDES)   
LOCAL_CFLAGS += $(FFCFLAGS)
LOCAL_STATIC_LIBRARIES := $(FFLIBS)
LOCAL_MODULE := $(FFNAME)
include $(BUILD_STATIC_LIBRARY)

4、错误与修改部分

经过上面可以试着ndk-build,我用eclipse编译了一下,出现了一些错误,下面是解决记录:

a、提示找到文件$(SUBDIR)../config.mak的错误,该错误出现在ffmpeg下的各个子目录(libavformat libavcodec libavutil libpostproc libswscale libswresample等)的makefile里,打开相关的makefile文件,注释掉即可,如下:

# include $(SUBDIR)../config.mak

b、提示restrict 相关错误,这可能是无法识别这个语法吧?在config.h里,修改为以下内容:

  #define av_restrict   //#define av_restrict restrict

c、出现av_small_strptime相关错误,那么重新使用我上面给出的android.mk,应该就可以解决了,应该跟ndk搜索头文件的顺序有关的,最主要就是注意其中的配置,如下部分:

 

#LOCAL_C_INCLUDES :=   \
#$(LOCAL_PATH)        \
#$(LOCAL_PATH)/..  
LOCAL_C_INCLUDES :=
LOCAL_C_INCLUDES += $(SYSROOT_INC)/usr/include
LOCAL_C_INCLUDES += $(LOCAL_PATH)/.. $(EXTEND_LOCAL_C_INCLUDES)   

 

 

 

 

 

 nable-libfaac --enable-libfdk-aac  --enable-libgsm  --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libspeex --enable-libtheora --enable-libtwolame  --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxavs --enable-libxvid"
if [ $compile_for_debug = "yes" ] ;then
echo "调试版本。。。。。。"
./configure --prefix=$PREFIX \
--enable-cross-compile --arch=$ARCH --cpu=$CPU \
--cross-prefix="$CROSS_PREFIX" \
--target-os=linux --sysroot=$NDK_SYSROOT \
--cc="$CC" --cxx="$CXX" \
--extra-cflags="$CFLAGS" \
--extra-ldflags="$LDFLAGS" \
--extra-libs="-lgnustl_shared -ldl -lc -lgcc " \
--enable-static --disable-shared \
--disable-ffplay --disable-ffprobe --disable-ffserver --disable-doc \
--disable-htmlpages  --disable-manpages  --disable-podpages  --disable-txtpages  --disable-symver --enable-debug --disable-asm --disable-stripping --disable-optimizations \
--enable-pic --enable-gpl --enable-version3 --enable-nonfree \
--disable-iconv --disable-bzlib $ffmpeg_enable_libs || exit 1
else
./configure --prefix=$PREFIX \
--enable-cross-compile --arch=$ARCH --cpu=$CPU \
--cross-prefix="$CROSS_PREFIX" \
--target-os=linux --sysroot=$NDK_SYSROOT \
--cc="$CC" --cxx="$CXX" \
--extra-cflags="$CFLAGS" \
--extra-ldflags="$LDFLAGS" \
--extra-libs="-lgnustl_shared -ldl -lc -lgcc " \
--enable-static --disable-shared \
--disable-ffplay --disable-ffprobe --disable-ffserver --disable-doc \
--disable-htmlpages  --disable-manpages  --disable-podpages  --disable-txtpages  --disable-symver \
--enable-pic --enable-asm --enable-gpl --enable-version3 --enable-nonfree \
--disable-iconv --disable-bzlib $ffmpeg_enable_libs || exit 1
fi

#sed -i "s/HAVE_LLRINT 0/HAVE_LLRINT 1/g" config.h
#sed -i "s/HAVE_LLRINTF 0/HAVE_LLRINTF 1/g" config.h
#sed -i "s/HAVE_LRINT 0/HAVE_LRINT 1/g" config.h
#sed -i "s/HAVE_LRINTF 0/HAVE_LRINTF 1/g" config.h
#sed -i "s/HAVE_ROUND 0/HAVE_ROUND 1/g" config.h
#sed -i "s/HAVE_ROUNDF 0/HAVE_ROUNDF 1/g" config.h
#sed -i "s/HAVE_ISINF 0/HAVE_ISINF 1/g" config.h
#sed -i "s/HAVE_ISNAN 0/HAVE_ISNAN 1/g" config.h
#make clean && make -j4 && make install || exit 1
#cp libavutil/colorspace.h $PREFIX/include/libavutil/colorspace.h
#cp config.h $PREFIX/include/config.h
cd $base_dir_path
}

create_toolchain

build_ffmpeg

执行配置脚本即可

 

3、开始写android.mk文件

首先,在jni目录下,先增加两个文件:Android.mk和Application.mk,内容分别如下:

#android.mk
include $(call all-subdir-makefiles)
 #application.mk

# Uncomment this if you're using STL in your project
# See CPLUSPLUS-SUPPORT.html in the NDK documentation for more information
APP_STL :=gnustl_shared  #  stlport_shared
#ARM_MODE := arm
APP_ABI :=armeabi-v7a  # armeabi-v7a x86 
#APP_CPPFLAGS += -fexceptions -frtti
APP_PLATFORM:=android-9

这里假定ffmpeg项目的源文件目录路径是jni/ffmpeg ,下面将在jni/ffmpeg下各个目录编写Android.mk文件,内容如下(为了方便,在文件开始处注释了路径信息):

#jni/ffmpeg/android.mk

LOCAL_PATH := $(call my-dir)  
include $(CLEAR_VARS)  
LOCAL_WHOLE_STATIC_LIBRARIES := libavformat libavcodec libavutil libpostproc libswscale libswresample  
LOCAL_MODULE := ffmpeg 
##LOCAL_C_INCLUDES := /home/cidy0106/libraries/armeabi-v7a/include
LOCAL_CFLAGS += -DGL_GLEXT_PROTOTYPES  -D__STDC_CONSTANT_MACROS -I/home/cidy0106/libraries/armeabi-v7a/include 
LOCAL_SHARED_LIBRARIES := gnustl_shared SDL2 # ffmpeg
LOCAL_STATIC_LIBRARIES := avformat avcodec avutil  swscale swresample avdevice x264 fdk-aac faac  xavs  gsm mp3lame opencore-amrwb opencore-amrnb opus vorbis vorbisfile vorbisenc  theora theoraenc theoradec speex  speexdsp  \
twolame vo-aacenc vo-amrwbenc vpx  xvidcore ogg avfilter postproc #avresample 
LOCAL_LDFLAGS :=-L/home/cidy0106/libraries/armeabi-v7a/lib -lx264  -lfdk-aac  \
 -lxavs -logg -lfaac -lgsm -lmp3lame -lopencore-amrwb -lopencore-amrnb -lopus -lvorbis -lvorbisfile -lvorbisenc  -ltheora -ltheoraenc -ltheoradec -lspeex  -lspeexdsp  \
-ltwolame -lvo-aacenc -lvo-amrwbenc -lvpx -lxvidcore

LOCAL_LDLIBS :=-ldl -lz -lm -lc -lGLESv1_CM -lGLESv2 -llog  #\
-L/home/cidy0106/libraries/armeabi-v7a/lib \
-lavformat -lavcodec -lavutil -lswscale -lpostproc -lavfilter  -lswresample -lavresample -lavdevice #\
-logg -lfaac -lfdk-aac -lgsm -lmp3lame -lopencore-amrnb -lopencore-amrwb \
-lopus -lspeex -lvorbis -lvorbisenc -ltheora -ltheoradec -ltheoraenc -ltwolame -lvo-aacenc -lvo-amrwbenc  \
-lvpx -lx264 -lxavs -lxvidcore  

#ifeq ($(TARGET_ARCH_ABI),armeabi-v7a)    
## 采用NEON优化技术    
#LOCAL_ARM_NEON := true   
#endif
include $(BUILD_SHARED_LIBRARY)  
include $(call all-makefiles-under,$(LOCAL_PATH))
#jni/ffmpeg/av.mk

 # LOCAL_PATH is one of libavutil, libavcodec, libavformat, or libswscale
#include $(LOCAL_PATH)/../config-$(TARGET_ARCH).mak
include $(LOCAL_PATH)/../config.mak
EXTEND_LOCAL_C_INCLUDES := /home/cidy0106/libraries/armeabi-v7a/include
OBJS :=
OBJS-yes :=
MMX-OBJS-yes :=
include $(LOCAL_PATH)/Makefile
# collect objects
OBJS-$(HAVE_MMX) += $(MMX-OBJS-yes)
OBJS += $(OBJS-yes)
FFNAME := lib$(NAME)
FFLIBS := $(foreach,NAME,$(FFLIBS),lib$(NAME))
FFCFLAGS= -DHAVE_AV_CONFIG_H -Wno-sign-compare -Wno-switch -Wno-pointer-sign
FFCFLAGS += -DTARGET_CONFIG=\"config-$(TARGET_ARCH).h\"
ALL_S_FILES := $(wildcard $(LOCAL_PATH)/$(TARGET_ARCH)/*.S)
ALL_S_FILES := $(addprefix $(TARGET_ARCH)/, $(notdir $(ALL_S_FILES)))
ifneq ($(ALL_S_FILES),)
ALL_S_OBJS := $(patsubst %.S,%.o,$(ALL_S_FILES))
C_OBJS := $(filter-out $(ALL_S_OBJS),$(OBJS))
S_OBJS := $(filter $(ALL_S_OBJS),$(OBJS))
else
C_OBJS := $(OBJS)
S_OBJS :=
endif
C_FILES := $(patsubst %.o,%.c,$(C_OBJS))
S_FILES := $(patsubst %.o,%.S,$(S_OBJS))
FFFILES := $(sort $(S_FILES)) $(sort $(C_FILES))

 

#jni/ffmpeg/libavformat/android.mk

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include $(LOCAL_PATH)/../av.mk
LOCAL_SRC_FILES := $(FFFILES)
#LOCAL_C_INCLUDES :=        \
#$(LOCAL_PATH)        \
#$(LOCAL_PATH)/..
$(warning $(LOCAL_C_INCLUDES))

LOCAL_C_INCLUDES :=
LOCAL_C_INCLUDES += $(SYSROOT_INC)/usr/include
LOCAL_C_INCLUDES += $(LOCAL_PATH)/.. $(EXTEND_LOCAL_C_INCLUDES)   $(LOCAL_PATH) 
LOCAL_CFLAGS += $(FFCFLAGS)
LOCAL_CFLAGS += -include "string.h" -Dipv6mr_interface=ipv6mr_ifindex
LOCAL_LDLIBS := -lz
LOCAL_STATIC_LIBRARIES := $(FFLIBS)
LOCAL_MODULE := $(FFNAME)
include $(BUILD_STATIC_LIBRARY)
#jni/ffmpeg/libavcodec/android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include $(LOCAL_PATH)/../av.mk
LOCAL_SRC_FILES := $(FFFILES)
#LOCAL_C_INCLUDES :=   \
#$(LOCAL_PATH)        \
#$(LOCAL_PATH)/..  
LOCAL_C_INCLUDES :=
LOCAL_C_INCLUDES += $(SYSROOT_INC)/usr/include
LOCAL_C_INCLUDES += $(LOCAL_PATH)/.. $(EXTEND_LOCAL_C_INCLUDES)   
LOCAL_CFLAGS += $(FFCFLAGS)
LOCAL_LDLIBS := -lz -L/home/cidy0106/libraries/armeabi-v7a/lib -lx264  -lfdk-aac  \
 -lxavs -logg -lfaac -lgsm -lmp3lame -lopencore-amrwb -lopencore-amrnb -lopus -lvorbis -lvorbisfile -lvorbisenc  -ltheora -ltheoraenc -ltheoradec -lspeex  -lspeexdsp  \
-ltwolame -lvo-aacenc -lvo-amrwbenc -lvpx -lxvidcore
LOCAL_STATIC_LIBRARIES := $(FFLIBS)
LOCAL_MODULE := $(FFNAME)
include $(BUILD_STATIC_LIBRARY)
#jni/ffmpeg/libavutil/android.mk
#jni/ffmpeg/libpostproc/android.mk
#jni/ffmpeg/libswscale/android.mk
#jni/ffmpeg/libswresample/android.mk
#jni/ffmpeg/libavfilter/android.mk
#jni/ffmpeg/libavdevice/android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include $(LOCAL_PATH)/../av.mk
LOCAL_SRC_FILES := $(FFFILES)
#LOCAL_C_INCLUDES :=        \
#$(LOCAL_PATH)        \
#$(LOCAL_PATH)/..
LOCAL_C_INCLUDES :=
LOCAL_C_INCLUDES += $(SYSROOT_INC)/usr/include
LOCAL_C_INCLUDES += $(LOCAL_PATH)/.. $(EXTEND_LOCAL_C_INCLUDES)   
LOCAL_CFLAGS += $(FFCFLAGS)
LOCAL_STATIC_LIBRARIES := $(FFLIBS)
LOCAL_MODULE := $(FFNAME)
include $(BUILD_STATIC_LIBRARY)

4、错误与修改部分

经过上面可以试着ndk-build,我用eclipse编译了一下,出现了一些错误,下面是解决记录:

a、 提示找到文件$(SUBDIR)../config.mak的错误,该错误出现在ffmpeg下的各个子目录(libavformat libavcodec libavutil libpostproc libswscale libswresample等)的makefile里,打开相关的makefile文件,注释掉即可,如下:

# include $(SUBDIR)../config.mak

b、提示restrict 相关错误,这可能是无法识别这个语法吧?在config.h里,修改为以下内容:

  #define av_restrict   //#define av_restrict restrict

c、出现av_small_strptime相关错误,那么重新使用我上面给出的android.mk,应该就可以解决了,应该跟ndk搜索头文件的顺序有关的,最主要就是注意其中的LOCAL_C_INCLUDES配置,如下部分:

#LOCAL_C_INCLUDES :=   \
#$(LOCAL_PATH)        \
#$(LOCAL_PATH)/..  
LOCAL_C_INCLUDES :=
LOCAL_C_INCLUDES += $(SYSROOT_INC)/usr/include
LOCAL_C_INCLUDES += $(LOCAL_PATH)/.. $(EXTEND_LOCAL_C_INCLUDES)

 


 


 


 


 


 

--target-os=linux --sysroot=$NDK_SYSROOT \ --cc="$CC" --cxx="$CXX" \ --extra-cflags="$CFLAGS" \ --extra-ldflags="$LDFLAGS" \ --extra-libs="-lgnustl_shared -ldl -lc -lgcc " \ --enable-static --disable-shared \ --disable-ffplay --disable-ffprobe --disable-ffserver --disable-doc \ --disable-htmlpages  --disable-manpages  --disable-podpages  --disable-txtpages  --disable-symver --enable-debug --disable-asm --disable-stripping --disable-optimizations \ --enable-pic --enable-gpl --enable-version3 --enable-nonfree \ --disable-iconv --disable-bzlib $ffmpeg_enable_libs || exit 1 else ./configure --prefix=$PREFIX \ --enable-cross-compile --arch=$ARCH --cpu=$CPU \ --cross-prefix="$CROSS_PREFIX" \ --target-os=linux --sysroot=$NDK_SYSROOT \ --cc="$CC" --cxx="$CXX" \ --extra-cflags="$CFLAGS" \ --extra-ldflags="$LDFLAGS" \ --extra-libs="-lgnustl_shared -ldl -lc -lgcc " \ --enable-static --disable-shared \ --disable-ffplay --disable-ffprobe --disable-ffserver --disable-doc \ --disable-htmlpages  --disable-manpages  --disable-podpages  --disable-txtpages  --disable-symver \ --enable-pic --enable-asm --enable-gpl --enable-version3 --enable-nonfree \ --disable-iconv --disable-bzlib $ffmpeg_enable_libs || exit 1 fi #sed -i "s/HAVE_LLRINT 0/HAVE_LLRINT 1/g" config.h #sed -i "s/HAVE_LLRINTF 0/HAVE_LLRINTF 1/g" config.h #sed -i "s/HAVE_LRINT 0/HAVE_LRINT 1/g" config.h #sed -i "s/HAVE_LRINTF 0/HAVE_LRINTF 1/g" config.h #sed -i "s/HAVE_ROUND 0/HAVE_ROUND 1/g" config.h #sed -i "s/HAVE_ROUNDF 0/HAVE_ROUNDF 1/g" config.h #sed -i "s/HAVE_ISINF 0/HAVE_ISINF 1/g" config.h #sed -i "s/HAVE_ISNAN 0/HAVE_ISNAN 1/g" config.h #make clean && make -j4 && make install || exit 1 #cp libavutil/colorspace.h $PREFIX/include/libavutil/colorspace.h #cp config.h $PREFIX/include/config.h cd $base_dir_path } create_toolchain build_ffmpeg 

执行配置脚本即可

 

3、开始写android.mk文件

首先,在jni目录下,先增加两个文件:Android.mk和Application.mk,内容分别如下:

#android.mk
include $(call all-subdir-makefiles)
 #application.mk

# Uncomment this if you're using STL in your project
# See CPLUSPLUS-SUPPORT.html in the NDK documentation for more information
APP_STL :=gnustl_shared  #  stlport_shared
#ARM_MODE := arm
APP_ABI :=armeabi-v7a  # armeabi-v7a x86 
#APP_CPPFLAGS += -fexceptions -frtti
APP_PLATFORM:=android-9

这里假定ffmpeg项目的源文件目录路径是jni/ffmpeg ,下面将在jni/ffmpeg下各个目录编写Android.mk文件,内容如下(为了方便,在文件开始处注释了路径信息):

#jni/ffmpeg/android.mk

LOCAL_PATH := $(call my-dir)  
include $(CLEAR_VARS)  
LOCAL_WHOLE_STATIC_LIBRARIES := libavformat libavcodec libavutil libpostproc libswscale libswresample  
LOCAL_MODULE := ffmpeg 
##LOCAL_C_INCLUDES := /home/cidy0106/libraries/armeabi-v7a/include
LOCAL_CFLAGS += -DGL_GLEXT_PROTOTYPES  -D__STDC_CONSTANT_MACROS -I/home/cidy0106/libraries/armeabi-v7a/include 
LOCAL_SHARED_LIBRARIES := gnustl_shared SDL2 # ffmpeg
LOCAL_STATIC_LIBRARIES := avformat avcodec avutil  swscale swresample avdevice x264 fdk-aac faac  xavs  gsm mp3lame opencore-amrwb opencore-amrnb opus vorbis vorbisfile vorbisenc  theora theoraenc theoradec speex  speexdsp  \
twolame vo-aacenc vo-amrwbenc vpx  xvidcore ogg avfilter postproc #avresample 
LOCAL_LDFLAGS :=-L/home/cidy0106/libraries/armeabi-v7a/lib -lx264  -lfdk-aac  \
 -lxavs -logg -lfaac -lgsm -lmp3lame -lopencore-amrwb -lopencore-amrnb -lopus -lvorbis -lvorbisfile -lvorbisenc  -ltheora -ltheoraenc -ltheoradec -lspeex  -lspeexdsp  \
-ltwolame -lvo-aacenc -lvo-amrwbenc -lvpx -lxvidcore

LOCAL_LDLIBS :=-ldl -lz -lm -lc -lGLESv1_CM -lGLESv2 -llog  #\
-L/home/cidy0106/libraries/armeabi-v7a/lib \
-lavformat -lavcodec -lavutil -lswscale -lpostproc -lavfilter  -lswresample -lavresample -lavdevice #\
-logg -lfaac -lfdk-aac -lgsm -lmp3lame -lopencore-amrnb -lopencore-amrwb \
-lopus -lspeex -lvorbis -lvorbisenc -ltheora -ltheoradec -ltheoraenc -ltwolame -lvo-aacenc -lvo-amrwbenc  \
-lvpx -lx264 -lxavs -lxvidcore  

#ifeq ($(TARGET_ARCH_ABI),armeabi-v7a)    
## 采用NEON优化技术    
#LOCAL_ARM_NEON := true   
#endif
include $(BUILD_SHARED_LIBRARY)  
include $(call all-makefiles-under,$(LOCAL_PATH))
#jni/ffmpeg/av.mk

 # LOCAL_PATH is one of libavutil, libavcodec, libavformat, or libswscale
#include $(LOCAL_PATH)/../config-$(TARGET_ARCH).mak
include $(LOCAL_PATH)/../config.mak
EXTEND_LOCAL_C_INCLUDES := /home/cidy0106/libraries/armeabi-v7a/include
OBJS :=
OBJS-yes :=
MMX-OBJS-yes :=
include $(LOCAL_PATH)/Makefile
# collect objects
OBJS-$(HAVE_MMX) += $(MMX-OBJS-yes)
OBJS += $(OBJS-yes)
FFNAME := lib$(NAME)
FFLIBS := $(foreach,NAME,$(FFLIBS),lib$(NAME))
FFCFLAGS= -DHAVE_AV_CONFIG_H -Wno-sign-compare -Wno-switch -Wno-pointer-sign
FFCFLAGS += -DTARGET_CONFIG=\"config-$(TARGET_ARCH).h\"
ALL_S_FILES := $(wildcard $(LOCAL_PATH)/$(TARGET_ARCH)/*.S)
ALL_S_FILES := $(addprefix $(TARGET_ARCH)/, $(notdir $(ALL_S_FILES)))
ifneq ($(ALL_S_FILES),)
ALL_S_OBJS := $(patsubst %.S,%.o,$(ALL_S_FILES))
C_OBJS := $(filter-out $(ALL_S_OBJS),$(OBJS))
S_OBJS := $(filter $(ALL_S_OBJS),$(OBJS))
else
C_OBJS := $(OBJS)
S_OBJS :=
endif
C_FILES := $(patsubst %.o,%.c,$(C_OBJS))
S_FILES := $(patsubst %.o,%.S,$(S_OBJS))
FFFILES := $(sort $(S_FILES)) $(sort $(C_FILES))

 

#jni/ffmpeg/libavformat/android.mk

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include $(LOCAL_PATH)/../av.mk
LOCAL_SRC_FILES := $(FFFILES)
#LOCAL_C_INCLUDES :=        \
#$(LOCAL_PATH)        \
#$(LOCAL_PATH)/..
$(warning $(LOCAL_C_INCLUDES))

LOCAL_C_INCLUDES :=
LOCAL_C_INCLUDES += $(SYSROOT_INC)/usr/include
LOCAL_C_INCLUDES += $(LOCAL_PATH)/.. $(EXTEND_LOCAL_C_INCLUDES)   $(LOCAL_PATH) 
LOCAL_CFLAGS += $(FFCFLAGS)
LOCAL_CFLAGS += -include "string.h" -Dipv6mr_interface=ipv6mr_ifindex
LOCAL_LDLIBS := -lz
LOCAL_STATIC_LIBRARIES := $(FFLIBS)
LOCAL_MODULE := $(FFNAME)
include $(BUILD_STATIC_LIBRARY)
#jni/ffmpeg/libavcodec/android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include $(LOCAL_PATH)/../av.mk
LOCAL_SRC_FILES := $(FFFILES)
#LOCAL_C_INCLUDES :=   \
#$(LOCAL_PATH)        \
#$(LOCAL_PATH)/..  
LOCAL_C_INCLUDES :=
LOCAL_C_INCLUDES += $(SYSROOT_INC)/usr/include
LOCAL_C_INCLUDES += $(LOCAL_PATH)/.. $(EXTEND_LOCAL_C_INCLUDES)   
LOCAL_CFLAGS += $(FFCFLAGS)
LOCAL_LDLIBS := -lz -L/home/cidy0106/libraries/armeabi-v7a/lib -lx264  -lfdk-aac  \
 -lxavs -logg -lfaac -lgsm -lmp3lame -lopencore-amrwb -lopencore-amrnb -lopus -lvorbis -lvorbisfile -lvorbisenc  -ltheora -ltheoraenc -ltheoradec -lspeex  -lspeexdsp  \
-ltwolame -lvo-aacenc -lvo-amrwbenc -lvpx -lxvidcore
LOCAL_STATIC_LIBRARIES := $(FFLIBS)
LOCAL_MODULE := $(FFNAME)
include $(BUILD_STATIC_LIBRARY)
#jni/ffmpeg/libavutil/android.mk
#jni/ffmpeg/libpostproc/android.mk
#jni/ffmpeg/libswscale/android.mk
#jni/ffmpeg/libswresample/android.mk
#jni/ffmpeg/libavfilter/android.mk
#jni/ffmpeg/libavdevice/android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include $(LOCAL_PATH)/../av.mk
LOCAL_SRC_FILES := $(FFFILES)
#LOCAL_C_INCLUDES :=        \
#$(LOCAL_PATH)        \
#$(LOCAL_PATH)/..
LOCAL_C_INCLUDES :=
LOCAL_C_INCLUDES += $(SYSROOT_INC)/usr/include
LOCAL_C_INCLUDES += $(LOCAL_PATH)/.. $(EXTEND_LOCAL_C_INCLUDES)   
LOCAL_CFLAGS += $(FFCFLAGS)
LOCAL_STATIC_LIBRARIES := $(FFLIBS)
LOCAL_MODULE := $(FFNAME)
include $(BUILD_STATIC_LIBRARY)

4、错误与修改部分

经过上面可以试着ndk-build,我用eclipse编译了一下,出现了一些错误,下面是解决记录:

a、提示找到文件$(SUBDIR)../config.mak的错误,该错误出现在ffmpeg下的各个子目录(libavformat libavcodec libavutil libpostproc libswscale libswresample等)的makefile里,打开相关的makefile文件,注释掉即可,如下:

# include $(SUBDIR)../config.mak

b、提示restrict 相关错误,这可能是无法识别这个语法吧?在config.h里,修改为以下内容:

  #define av_restrict   //#define av_restrict restrict

c、出现av_small_strptime相关错误,那么重新使用我上面给出的android.mk,应该就可以解决了,应该跟ndk搜索头文件的顺序有关的,最主要就是注意其中的LOCAL_C_INCLUDES配置,如下部分:

#LOCAL_C_INCLUDES :=   \
#$(LOCAL_PATH)        \
#$(LOCAL_PATH)/..  
LOCAL_C_INCLUDES :=
LOCAL_C_INCLUDES += $(SYSROOT_INC)/usr/include
LOCAL_C_INCLUDES += $(LOCAL_PATH)/.. $(EXTEND_LOCAL_C_INCLUDES)

 

 

 

注意:这里再强调一下,如果是参考着别人的文章进行操作的话,不要修改未出现的错误!!!因为之前的错误可能是某个步骤错误、某个版本错误等等造成的,可能都不需要修改,所以“不要修改未出现的错误”。这也是我把问题解决记录放在最后的主要原因。

» 本文链接地址:https://www.xidige.com/707

打开支付宝扫一扫,即可进行扫码打赏哦

扫码支持
扫码打赏,你说多少就多少

标签:

分享到:

扫一扫 在手机阅读、分享本文

上一篇: 下一篇:
评论区0人评论1,078人参与

电子邮件地址不会被公开。 必填项已用*标注

*

loading

赞助商广告