1、 操作系统Ubuntu 12.04

2、 开发工具eclipse(Indigo)

3、 下载/安装/配置Android-SDK_r21、Android-NDK-r9

4、 下载FFmpeg版本0.11.3


5、 详细编译步骤:


5.1、创建工作目录:当前目录为:~/

1

mkdir lplayer
2

mkdir jni


5.2、进入目录jni,当前目录为:~/lplayer/jni

1解压ffmpeg源码包

tar -zxvf filename

2)解压后,重命名ffmpeg源码文件为:

mv ffmpeg_original ffmpeg


5.3、创建相应NDK配置文件:

1~/lplayer/jni/Android.mk

include $(call 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=/usr/local/app/android-ndk-r9/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86
PLATFORM=/usr/local/app/android-ndk-r9/platforms/android-9/arch-arm

./configure --target-os=linux \
	--arch=arm \
	--enable-version3 \
	--enable-gpl \
	--enable-nonfree \
	--disable-doc \
	--disable-stripping \
	--disable-ffmpeg \
	--disable-ffplay \
	--disable-ffserver \
	--disable-ffprobe \
	--disable-encoders \
	--disable-muxers \
	--disable-devices \
	--enable-avfilter \
	--enable-network \
	--enable-protocol=tcp \
	--enable-avfilter \
	--enable-demuxer=flv \
	--enable-demuxer=h264 \
	--enable-parser=h264 \
	--enable-parser=aac \
	--enable-decoder=h264 \
	--enable-decoder=rawvideo \
	--enable-librtmp \
	--disable-avdevice \
	--disable-asm \
	--enable-neon \
	--enable-cross-compile \
	--cc=$PREBUILT/bin/arm-linux-androideabi-gcc \
	--cross-prefix=$PREBUILT/bin/arm-linux-androideabi- \
	--nm=$PREBUILT/bin/arm-linux-androideabi-nm \
	--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.6/crtbegin.o $PREBUILT/lib/gcc/arm-linux-androideabi/4.6/crtend.o -lc -lm -ldl"

(2)运行上配置文件:

chmod u+x config.sh
chmod u+x configure
./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、现在可以开始编译了

方式一:进入目录:~/lplayer,运行命令:

ndk-build

找到该命令的前提是你已经把NDK目录加到了PATH系统环境变量中。
方式二:在eclipse中右击project->Debug As->Android Application
假如没有错误,在目录~/lplayer/libs/armeabi下会有libffmpeg.so文件,这就是ffmpeg的库文件,可以直接拿来在通过JNI或者P/Invoke技术,使用了!

6、 编译问题修复

6.1、error: libavcodec/codec_names.h: No such file or directory
解决办法:在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,把下面一行删掉后,重新编译(先删除obj下生成的文件)。
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

参考:http://my.oschina.net/wangqin/blog/80345


Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐