Android NDK r8b和FFmpeg 0.11.2移植
1、 准备Ubuntu 12.04虚拟机,推荐使用VirtualBox
2、 Ubuntu中安装相应的编译开发软件,如:build-essential之类的东西
3、 下载Android NDK r8b和Android SDK, all for linux,注意这个文档中使用的是Android NDK r8b和Android SDK 20
4、 下载FFmpeg,这个文档中使用的是:FFmpeg 0.11.2版本
5、 详细编译步骤:
5.1、创建工作目录:当前目录为:~/
(1)、
mkdir lpayer
(2)、
mkdir jni
5.2、进入目录jni,当前目录为:~/lplayer/jni,解压ffmpeg源码包
(1)、
tar zxvf filename
(2)、解压后,重命名ffmpeg源码文件为:
mv ffmpeg_original ffmpeg
(3)、得到目录为:~/lpayer/jni/ffmpeg
5.3、创建相应NDK配置文件:
(1)~/lplayer/jni/Android.mk
include $(all-subdir-makefiles)
(2)~/lplayer/jni/ffmpeg/Android.mk
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_WHOLE_STATIC_LIBRARIES := libavformat libavcodec libavutil libpostproc libswscale LOCAL_MODULE := ffmpeg include $(BUILD_SHARED_LIBRARY) include $(call all-makefiles-under,$(LOCAL_PATH))
(3)~/lplayer/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 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))
(4)~/lplayer/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_CFLAGS += $(FFCFLAGS) LOCAL_LDLIBS := -lz LOCAL_STATIC_LIBRARIES := $(FFLIBS) LOCAL_MODULE := $(FFNAME) include $(BUILD_STATIC_LIBRARY)
(5)~/lplayer/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)/.. 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)
(6)~/lplayer/jni/ffmpeg/libavfilter/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_CFLAGS += $(FFCFLAGS) LOCAL_STATIC_LIBRARIES := $(FFLIBS) LOCAL_MODULE := $(FFNAME) include $(BUILD_STATIC_LIBRARY)
(7)~/lplayer/jni/ffmpeg/libavutil/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_CFLAGS += $(FFCFLAGS) LOCAL_STATIC_LIBRARIES := $(FFLIBS) LOCAL_MODULE := $(FFNAME) include $(BUILD_STATIC_LIBRARY)
(8)、~/lplayer/jni/ffmpeg/libpostproc/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_CFLAGS += $(FFCFLAGS) LOCAL_STATIC_LIBRARIES := $(FFLIBS) LOCAL_MODULE := $(FFNAME) include $(BUILD_STATIC_LIBRARY)
(9)、~/lplayer/jni/ffmpeg/libswscale/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_CFLAGS += $(FFCFLAGS) LOCAL_STATIC_LIBRARIES := $(FFLIBS) LOCAL_MODULE := $(FFNAME) include $(BUILD_STATIC_LIBRARY)
5.4、配置ffmpeg
(1)编写配置脚本:~/lpayer/jni/ffmpeg/config.sh
注意其中的NDK路径,请修改成你系统中对应的安装路径
#!/bin/bash PREBUILT=/home/long/androiddev/android-ndk-r8b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86 PLATFORM=/home/long/androiddev/android-ndk-r8b/platforms/android-9/arch-arm ./configure --target-os=linux \ --arch=arm \ --enable-version3 \ --enable-gpl \ --enable-nonfree \ --disable-stripping \ --disable-ffmpeg \ --disable-ffplay \ --disable-ffserver \ --disable-ffprobe \ --disable-encoders \ --disable-muxers \ --disable-devices \ --disable-protocols \ --enable-protocol=file \ --enable-avfilter \ --disable-network \ --disable-avdevice \ --disable-asm \ --enable-cross-compile \ --cc=$PREBUILT/bin/arm-linux-androideabi-gcc \ --cross-prefix=$PREBUILT/bin/arm-linux-androideabi- \ --strip=$PREBUILT/bin/arm-linux-androideabi-strip \ --extra-cflags="-fPIC -DANDROID" \ --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 -lc -lm -ldl"
(2)、运行上配置文件:
chmod 755 config.sh ./config.sh
(3)、这时会在~/lplayer/jni/ffmpeg目录下生成三个文件: config.h,config.mak,config.log。
我们需要修改config.h文件,将其中的
#define restrict restrict
修改成
#define restrict
5.5、修改ffmpeg代码,以便在NDK中编译通过
(1)、修改~/lplayer/jni/ffmpeg/libavutil/libm.h文件,将其中的所有static方法都注释掉
(2)、修改ffmpeg源码目录中的libavcodev,libavfilter,libavformat,libavutil,libpostproc,libswscale中的Makefile,将Makefile中的开头:include $(SUBDIR)../config.mak都注释掉。
5.6、现在可以开始编译了
(1)、进入目录:~/lplayer
(2)、运行命令:ndk-build,找到该命令的前提是你已经把NDK目录加到了PATH系统环境变量中。
5.7、经过漫长的等待,可能会失败,不要紧张,看看下面的问题修复章节。
假如你没有错误,恭喜你,在目录~/lplayer/libs/armeabi下会有libffmpeg.so文件,这就是ffmpeg的库文件,可以直接拿来在通过JNI或者P/Invoke技术,使用了!
6、 编译问题修复
6.1、error: libavcodec/codec_names.h: No such file or directory
源文档 http://bbs.rosoo.net/thread-13749-1-1.html
在ffmpeg目录下运行
cat libavcodec/avcodec.h | libavcodec/codec_names.sh config.h libavcodec/codec_names.h
即可解决!
源文档 http://bbs.rosoo.net/thread-13749-1-1.html
6.2、ERROR:
按照Scott所写的增加系列mk文件以及对libm.h进行修改,并生成及修改config.h后,开始尝试编译,一直到最后链接时出现以下的问题:
/home/chf0001/android-ndk/android-ndk-r5/samples/ffmpeg/obj/local/armeabi/libavutil.a(inverse.o):(.rodata+0x0): multiple definition of `ff_inverse'
/home/chf0001/android-ndk/android-ndk-r5/samples/ffmpeg/obj/local/armeabi/libavcodec.a(inverse.o):(.rodata+0x0): first defined here
查看后发现:
在目录libavcodec下有文件inverse.c,内容为:
#include "libavutil/inverse.c"
而libavutil下的inverse.c中定义了变量ff_inverse,所以在libavutil.a与libavcodec.a中都存在变量ff_inverse的定义,当这二个文件一起链接成libffmpeg.so时,就出现了上面的问题。
了解了原因,解决办法就很简单,直接修改libavcodec/Makefile,把下面一行删掉:
OBJS-$(!CONFIG_SMALL) += inverse.o
那么这是不是ffmpeg的一个问题呢?应该不是,因为它的编译目标为libavutil.so, libavcodec.so,也就是说都是独立的小的so文件,而我们这边是最终产生一个统合的文件libffmpeg.so,其中包含了libavutil.a, libavcodec.a, 所以才产生了以上的问题。
源文档 http://blog.csdn.net/chf0001/article/details/6430667
6.3、要是还有其他问题就自己google去吧,我这里暂时没有遇到其他问题了,有问题也可以联系:[email protected]