歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

將ffmpeg移植到Android

首先要配置ndk,見前面一節:http://www.linuxidc.com/Linux/2014-05/102423.htm

Ubuntu 14.04 x64配置Android 4.4 kitkat編譯環境的方法 http://www.linuxidc.com/Linux/2014-05/101148.htm

Ubuntu 12.04搭建Android開發環境 http://www.linuxidc.com/Linux/2012-09/69961.htm

Ubuntu 14.04 配置 Android SDK 開發環境 http://www.linuxidc.com/Linux/2014-05/101039.htm

64位Ubuntu 11.10下Android開發環境的搭建(JDK+Eclipse+ADT+Android SDK詳細) http://www.linuxidc.com/Linux/2013-06/85303.htm

Ubuntu 12.10 x64 安裝 Android SDK http://www.linuxidc.com/Linux/2013-03/82005.htm

下面介紹如何將ffmpeg編譯成動態庫libffmpegjni.so。
 
在主目錄下新建文件夾ayer/jni,將ffmpeg拷貝到jni目錄中。在ayer目錄下新建Android.mk文件,程序為:
 
~/ayer/jni/Android.mk
include $(all-subdir-makefiles)
 

~/ayer/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))
 

~/ayer/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))
 

~/ayer/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)
 

~/ayer/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)
 

~/ayer/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_LDLIBS := -lz
 
LOCAL_STATIC_LIBRARIES := $(FFLIBS)
 
LOCAL_MODULE := $(FFNAME)
 
include $(BUILD_STATIC_LIBRARY)
 

~/ayer/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_LDLIBS := -lz
 
LOCAL_STATIC_LIBRARIES := $(FFLIBS)
 
LOCAL_MODULE := $(FFNAME)
 
include $(BUILD_STATIC_LIBRARY)
 

~/ayer/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_LDLIBS := -lz
 
LOCAL_STATIC_LIBRARIES := $(FFLIBS)
 
LOCAL_MODULE := $(FFNAME)
 
include $(BUILD_STATIC_LIBRARY)
 

~/ayer/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_LDLIBS := -lz
 
LOCAL_STATIC_LIBRARIES := $(FFLIBS)
 
LOCAL_MODULE := $(FFNAME)
 
include $(BUILD_STATIC_LIBRARY)
 

編寫配置腳本:~/ayer/jni/ffmpeg/config.sh
 
注意其中的NDK路徑,請修改成你系統中對應的安裝路徑
 
 
 

 

#!/bin/bash
 
source config_common.sh
 
PREBUILT=/home/huaguanglu/bysj/android-ndk-r8b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86
 
PLATFORM=/home/huaguanglu/bysj/android-ndk-r8b/platforms/android-9/arch-arm
 
NDK_ROOT=/home/huaguanglu/bysj/android-ndk-r8b
 
./configure --target-os=linux \
 
--arch=arm \
 
--extra-cflags="-I${NDK_ROOT}/platforms/android-9/arch-arm/usr/include -fPIC -DANDROID -std=c99" \
 
--disable-everything \
 
--enable-version3 \
 
--enable-gpl \
 
--enable-nonfree \
 
--disable-stripping \
 
--disable-ffmpeg \
 
--disable-ffplay \
 
--disable-ffserver \
 
--disable-ffprobe \
 
--disable-encoders \
 
--disable-muxers \
 
--disable-devices \
 
--disable-protocols \
 
--disable-network \
 
--disable-avdevice \
 
--disable-asm \
 
--enable-decoder=h264 \
 
--enable-swscale \
 
--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"
 

終端輸入命令:
 

 

chmod 755 config.sh
 
./config.sh
 

這時會在~/ayer/jni/ffmpeg目錄下生成三個文件: config.h,config.mak,config.log。
 
我們需要修改config.h文件,將其中的
 
#define av_restrict restrict
 
修改成
 
#define av_restrict
 
修改ffmpeg代碼,以便在NDK中編譯通過
 
在libavfilter目錄中的Makefile的末尾處多了 Clean 這個玩意兒 將其注釋掉或者刪掉。
 
修改~/ayer/jni/ffmpeg/libavutil/libm.h文件,將其中的所有static方法都注釋掉
 
修改ffmpeg源碼目錄中的libavcodev,libavfilter,libavformat,libavutil,libpostproc,libswscale中的Makefile,將Makefile中的開頭:include $(SUBDIR)../config.mak都注釋掉。
 
進入目錄:~/ayer

更多詳情見請繼續閱讀下一頁的精彩內容: http://www.linuxidc.com/Linux/2014-05/102424p2.htm 

Copyright © Linux教程網 All Rights Reserved