FFmpeg是一個跨平台的影音編解碼函式庫,我們可以用來實作一個在Android平台上的 media player。
首先先build出一個Android版本的FFmpeg。
以下是這次我所用到的開發環境:
- Cygwin
- Android NDK:android-ndk-r4b-windows
- FFmpeg:0.6.1
[b]《Step 1》[/b]
在Android NDK上建立一個project。
[b]《Step 2》[/b]
將FFmpeg的source code整包copy到 $(PROJECT)/jni 底下。
[b]《Step 3》[/b]
在 $(PROJECT)/jni/ffmpeg 底下建立一個 config.sh:
[code]#!/bin/bash
PREBUILT=/android-ndk-r4b/build/prebuilt/windows/arm-eabi-4.4.0
PLATFORM=/android-ndk-r4b/build/platforms/android-8/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-mpegaudio-hp \
--disable-avdevice \
--enable-cross-compile \
--cc=$PREBUILT/bin/arm-eabi-gcc \
--cross-prefix=$PREBUILT/bin/arm-eabi- \
--nm=$PREBUILT/bin/arm-eabi-nm \
--extra-cflags="-fPIC -DANDROID" \
--disable-asm \
--enable-neon \
--enable-armv5te \
--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"
[/code]
PREBUILT和PLATFORM應該要符合你自己的配置。
[b]《Step 4》[/b]
在Cygwin中,$(PROJECT)/jni/ffmpeg 底下執行config.sh:
[code]
$ ./config.sh
[/code]
configure會依據 config.sh內的設定將config.mak及config.h建立出來。
[b]《Step 5》[/b]
在剛剛生成的 config.h中找到這句:
[code]
#define restrict restrict[/code]
因為Android的gcc並不認得restrict這個keyword,所以把它改寫成:
[code]
#define restrict
[/code]
[b]《Step 6》[/b]
將/libavutil/libm.h中所有的static函式,全都mark起來。
[b]《Step 7》[/b]
分別修改libavcodec、libavfilter、libavformat、libavutil、libpostproc和libswscale下的 Makefile,將下面兩句mark起來:
[code]
include $(SUBDIR)../config.mak
include $(SUBDIR)../subdir.mak
[/code]
[b]《Step 8》[/b]
在$(PROJECT)/jni /ffmpeg下建立一個檔案av.mk,內容如下:
[code]
# LOCAL_PATH is one of libavutil, libavcodec, libavformat, or libswscale
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))
[/code]
[b]《Step 9》[/b]
在$(PROJECT)/jni底下修改Android.mk,內容如下:
[code]
include $(all-subdir-makefiles)
[/code]
[b]《Step 10》[/b]
在 /ffmpeg底下新增一個Android.mk:
[code]
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_STATIC_LIBRARIES := libavformat libavcodec libavutil libpostproc libswscale
LOCAL_MODULE := ffmpeg
include $(BUILD_SHARED_LIBRARY)
include $(call all-makefiles-under,$(LOCAL_PATH))
[/code]
[b]《Step 11》[/b]
在/ffmpeg /libavformat底下新增一個Android.mk:
[code]
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)
[/code]
[b]《Step 12》[/b]
在/ffmpeg/libavcodec 底下新增一個Android.mk:
[code]
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)
[/code]
[b]《Step 13》[/b]
在libavfilter、 libavutil、libpostproc和libswscale底下都分別新增一個Android.mk:
[code]
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)
[/code]
[b]《Step 14》[/b]
在$(PROJECT) 底下執行ndk-build:
[code]
$ /android-ndk-r4b/ndk-build
[/code]
[b]《Step 15》[/b]
完成,產生出libavcodec.a、libavformat.a、libavutil.a、libpostproc.a、 libswscale.a和libffmpeg.so。
PS:
生成config.sh时,總是出錯,出現Unknown C compiler
那是因為你的cc那個變數沒有設置正確
PREBUILT 這是定義編譯器的地方,如果你使用的不是盎r4而用的是r5的話是不能用這個路徑的
d:/android-ndk/toolchains/arm-eabi-4.4.0/prebuilt/windows
需要改成這樣
REF://http://www.chinavideo.org/archiver/?tid-10849.html
- Sep 15 Thu 2011 09:56
FFmpeg in Android
全站熱搜
留言列表