用NDK编译FFmpeg

Android内置的编解码器实在太少,于是我们需要FFmpeg。Android提供了NDK,为我们使用FFmpeg这种C语言代码提供了方便。
首先创建一个标准的Android项目vPlayer
  1. android create project -n vPlayer -t 8 -p vPlayer -k me.abitno.vplayer -a PlayerView
复制代码
然后在vPlayer目录里
  1. mkdir jni && cd jni
  2. wget http://ffmpeg.org/releases/ffmpeg-0.6.tar.bz2
  3. tar xf ffmpeg-0.6.tar.bz2 && mv ffmpeg-0.6 ffmpeg && cd ffmpeg
复制代码
在ffmpeg下新建一个config.sh,内容如下,注意把PREBUILT和PLATFORM设置正确。另外里面有些参数你也可以自行调整,我主要是为了配置一个播放器而这样设置的。
  1. #!/bin/bash

  2. PREBUILT=/home/abitno/Android/android-ndk-r4/build/prebuilt/linux-x86/arm-eabi-4.4.0
  3. PLATFORM=/home/abitno/Android/android-ndk-r4/build/platforms/android-8/arch-arm

  4. ./configure --target-os=linux \
  5.         --arch=arm \
  6.         --enable-version3 \
  7.         --enable-gpl \
  8.         --enable-nonfree \
  9.         --disable-stripping \
  10.         --disable-ffmpeg \
  11.         --disable-ffplay \
  12.         --disable-ffserver \
  13.         --disable-ffprobe \
  14.         --disable-encoders \
  15.         --disable-muxers \
  16.         --disable-devices \
  17.         --disable-protocols \
  18.         --enable-protocol=file \
  19.         --enable-avfilter \
  20.         --disable-network \
  21.         --disable-mpegaudio-hp \
  22.         --disable-avdevice \
  23.         --enable-cross-compile \
  24.         --cc=$PREBUILT/bin/arm-eabi-gcc \
  25.         --cross-prefix=$PREBUILT/bin/arm-eabi- \
  26.         --nm=$PREBUILT/bin/arm-eabi-nm \
  27.         --extra-cflags="-fPIC -DANDROID" \
  28.         --disable-asm \
  29.         --enable-neon \
  30.         --enable-armv5te \
  31.         --extra-ldflags="-Wl,-T,$PREBUILT/arm-eabi/lib/ldscripts/armelf.x -Wl,-rpath-link=$PLATFORM/usr/lib -L$PLATFORM/usr/lib -nostdlib $PREBUILT/lib/gcc/arm-eabi/4.4.0/crtbegin.o $PREBUILT/lib/gcc/arm-eabi/4.4.0/crtend.o -lc -lm -ldl"
复制代码
运行config.sh开始configure
  1. chmod +x config.sh
  2. ./config.sh
复制代码
configure完成后,编辑刚刚生成的config.h,找到这句
  1. #define restrict restrict
复制代码
Android的GCC不支持restrict关键字,于是修改成下面这样
  1. #define restrict
复制代码
编辑libavutil/libm.h,把其中的static方法都删除。

分别修改libavcodec、libavfilter、libavformat、libavutil、libpostproc和libswscale下的Makefile,把下面两句删除
  1. include $(SUBDIR)../subdir.mak
  2. include $(SUBDIR)../config.mak
复制代码
在ffmpeg下添加一个文件av.mk,内容如下
  1. # LOCAL_PATH is one of libavutil, libavcodec, libavformat, or libswscale

  2. #include $(LOCAL_PATH)/../config-$(TARGET_ARCH).mak
  3. include $(LOCAL_PATH)/../config.mak

  4. OBJS :=
  5. OBJS-yes :=
  6. MMX-OBJS-yes :=
  7. include $(LOCAL_PATH)/Makefile

  8. # collect objects
  9. OBJS-$(HAVE_MMX) += $(MMX-OBJS-yes)
  10. OBJS += $(OBJS-yes)

  11. FFNAME := lib$(NAME)
  12. FFLIBS := $(foreach,NAME,$(FFLIBS),lib$(NAME))
  13. FFCFLAGS  = -DHAVE_AV_CONFIG_H -Wno-sign-compare -Wno-switch -Wno-pointer-sign
  14. FFCFLAGS += -DTARGET_CONFIG=\"config-$(TARGET_ARCH).h\"

  15. ALL_S_FILES := $(wildcard $(LOCAL_PATH)/$(TARGET_ARCH)/*.S)
  16. ALL_S_FILES := $(addprefix $(TARGET_ARCH)/, $(notdir $(ALL_S_FILES)))

  17. ifneq ($(ALL_S_FILES),)
  18. ALL_S_OBJS := $(patsubst %.S,%.o,$(ALL_S_FILES))
  19. C_OBJS := $(filter-out $(ALL_S_OBJS),$(OBJS))
  20. S_OBJS := $(filter $(ALL_S_OBJS),$(OBJS))
  21. else
  22. C_OBJS := $(OBJS)
  23. S_OBJS :=
  24. endif

  25. C_FILES := $(patsubst %.o,%.c,$(C_OBJS))
  26. S_FILES := $(patsubst %.o,%.S,$(S_OBJS))

  27. FFFILES := $(sort $(S_FILES)) $(sort $(C_FILES))
复制代码
接下来要添加一系列的Android.mk,在jni根目录下的内容如下
  1. include $(all-subdir-makefiles)
复制代码
在ffmpeg目录下,Android.mk
  1. LOCAL_PATH := $(call my-dir)
  2. include $(CLEAR_VARS)
  3. LOCAL_STATIC_LIBRARIES := libavformat libavcodec libavutil libpostproc libswscale
  4. LOCAL_MODULE := ffmpeg
  5. include $(BUILD_SHARED_LIBRARY)
  6. include $(call all-makefiles-under,$(LOCAL_PATH))
复制代码
libavformat/Android.mk
  1. LOCAL_PATH := $(call my-dir)
  2. include $(CLEAR_VARS)
  3. include $(LOCAL_PATH)/../av.mk
  4. LOCAL_SRC_FILES := $(FFFILES)
  5. LOCAL_C_INCLUDES :=                \
  6.         $(LOCAL_PATH)                \
  7.         $(LOCAL_PATH)/..
  8. LOCAL_CFLAGS += $(FFCFLAGS)
  9. LOCAL_CFLAGS += -include "string.h" -Dipv6mr_interface=ipv6mr_ifindex
  10. LOCAL_LDLIBS := -lz
  11. LOCAL_STATIC_LIBRARIES := $(FFLIBS)
  12. LOCAL_MODULE := $(FFNAME)
  13. include $(BUILD_STATIC_LIBRARY)
复制代码
libavcodec/Android.mk
  1. LOCAL_PATH := $(call my-dir)
  2. include $(CLEAR_VARS)
  3. include $(LOCAL_PATH)/../av.mk
  4. LOCAL_SRC_FILES := $(FFFILES)
  5. LOCAL_C_INCLUDES :=                \
  6.         $(LOCAL_PATH)                \
  7.         $(LOCAL_PATH)/..
  8. LOCAL_CFLAGS += $(FFCFLAGS)
  9. LOCAL_LDLIBS := -lz
  10. LOCAL_STATIC_LIBRARIES := $(FFLIBS)
  11. LOCAL_MODULE := $(FFNAME)
  12. include $(BUILD_STATIC_LIBRARY)
复制代码
libavfilter、libavutil、libpostproc和libswscale下的Android.mk内容如下
  1. LOCAL_PATH := $(call my-dir)
  2. include $(CLEAR_VARS)
  3. include $(LOCAL_PATH)/../av.mk
  4. LOCAL_SRC_FILES := $(FFFILES)
  5. LOCAL_C_INCLUDES :=                \
  6.         $(LOCAL_PATH)                \
  7.         $(LOCAL_PATH)/..
  8. LOCAL_CFLAGS += $(FFCFLAGS)
  9. LOCAL_STATIC_LIBRARIES := $(FFLIBS)
  10. LOCAL_MODULE := $(FFNAME)
  11. include $(BUILD_STATIC_LIBRARY)
复制代码
最外层的jni/Android.mk和jni/ffmpeg/Android.mk只是随便这样写的,你应该根据自己的需求改写。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值