___________________________编译环境__________________________________
NDK 环境:android-ndk-r13b
MinGW环境:Basic Setup下Mark了所有的项目,FFmpeg需要使用msys环境进行编译
ffmpeg :3.2.4 下载地址
libx264:官网最新
_____________________________________________________________________
___________________________编译配置___________________________________
在解压的ffmpeg目录下新建一个sh文件,并填入如下内容
关键变量设置是NDK路径
#!/bin/bash NDK=E:/android-ndk-r13b SYSROOT=$NDK/platforms/android-19/arch-arm/ TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64 function build_one { ./configure \ --prefix=$PREFIX \ --enable-asm \ --enable-neon \ --enable-static \ --enable-small \ --disable-shared \ --disable-ffmpeg \ --disable-ffplay \ --disable-ffprobe \ --disable-ffserver \ --disable-avdevice \ --disable-doc \ --disable-symver \ --cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \ --target-os=linux \ --arch=arm \ --cpu=armv7-a \ --enable-cross-compile \ --sysroot=$SYSROOT \ --extra-cflags="-fPIC -DANDROID -mfpu=neon -mfloat-abi=softfp -I$NDK/platforms/android-19/arch-arm/usr/include" \ --extra-ldflags="$ADDI_LDFLAGS" make clean make make install } CPU=arm PREFIX=$(pwd)/android/$CPU ADDI_CFLAGS="-marm" build_one
附带上libx264库加入进来后的编译脚本(EXTRA_LIB 指向的是 libx264编译后生成目录,包括头文件[include]和静态库[lib]文件夹):
#!/bin/bash NDK=E:/android-ndk-r13b SYSROOT=$NDK/platforms/android-19/arch-arm/ TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64 EXTRA_LIB=E:/FFmpeg/android-lib function build_one { ./configure \ --prefix=$PREFIX \ --enable-asm \ --enable-neon \ --enable-static \ --enable-small \ --enable-libx264 \ --enable-gpl \ --enable-encoder=libx264 \ --disable-shared \ --disable-doc \ --disable-ffmpeg \ --disable-ffplay \ --disable-ffprobe \ --disable-ffserver \ --disable-avdevice \ --disable-symver \ --cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \ --target-os=linux \ --arch=arm \ --cpu=armv7-a \ --enable-cross-compile \ --sysroot=$SYSROOT \ --extra-cflags="-I$EXTRA_LIB/include -fPIC -DANDROID -mfpu=neon -mfloat-abi=softfp -I$NDK/platforms/android-19/arch-arm/usr/include" \ --extra-ldflags="-L$EXTRA_LIB/lib -lx264 $ADDI_LDFLAGS" make clean make make install } CPU=arm PREFIX=$(pwd)/android/x264_lib/$CPU ADDI_CFLAGS="-marm" build_one
编译好后,Eclipse新建工程,添加C++库支持,编写java jni接口,在bin下的classes目录下,使用javah可以生成头文件。
附上主要实现代码,以及两个编译mk文件写法:
Android.mk
LOCAL_PATH := $(call my-dir)
# prepare libX
include $(CLEAR_VARS)
TARGET_ARCH_ABI := armeabi-v7a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_MODULE := avcodec
LOCAL_SRC_FILES := libavcodec.a
include $(PREBUILT_STATIC_LIBRARY)
# prepare libX
include $(CLEAR_VARS)
TARGET_ARCH_ABI := armeabi-v7a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_MODULE := avfilter
LOCAL_SRC_FILES := libavfilter.a
include $(PREBUILT_STATIC_LIBRARY)
# prepare libX
include $(CLEAR_VARS)
TARGET_ARCH_ABI := armeabi-v7a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_MODULE := avformat
LOCAL_SRC_FILES := libavformat.a
include $(PREBUILT_STATIC_LIBRARY)
# prepare libX
include $(CLEAR_VARS)
TARGET_ARCH_ABI := armeabi-v7a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_MODULE := avutil
LOCAL_SRC_FILES := libavutil.a
include $(PREBUILT_STATIC_LIBRARY)
# prepare libX
include $(CLEAR_VARS)
TARGET_ARCH_ABI := armeabi-v7a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_MODULE := postproc
LOCAL_SRC_FILES := libpostproc.a
include $(PREBUILT_STATIC_LIBRARY)
# prepare libX
include $(CLEAR_VARS)
TARGET_ARCH_ABI := armeabi-v7a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_MODULE := swresample
LOCAL_SRC_FILES := libswresample.a
include $(PREBUILT_STATIC_LIBRARY)
# prepare libX
include $(CLEAR_VARS)
TARGET_ARCH_ABI := armeabi-v7a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_MODULE := swscale
LOCAL_SRC_FILES := libswscale.a
include $(PREBUILT_STATIC_LIBRARY)
# prepare libX
include $(CLEAR_VARS)
TARGET_ARCH_ABI := armeabi-v7a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_MODULE := x264
LOCAL_SRC_FILES := libx264.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
TARGET_ARCH_ABI := armeabi-v7a
LOCAL_MODULE := NativeAppTest
LOCAL_SRC_FILES := NativeAppTest.cpp
LOCAL_C_INCLUDES += $(LOCAL_PATH)/include
LOCAL_CFLAGS := -D__STDC_CONSTANT_MACROS -Wno-sign-compare -Wno-switch -Wno-pointer-sign -DHAVE_NEON=1 -mfpu=neon -mfloat-abi=softfp -fPIC -DANDROID
LOCAL_STATIC_LIBRARIES := avfilter avformat avcodec postproc swresample swscale avutil x264
LOCAL_LDLIBS := -L$(NDK_ROOT)/platforms/$(APP_PLATFORM)/arch-arm/usr/lib -L$(LOCAL_PATH) -llog -ljnigraphics -lz -ldl
include $(BUILD_SHARED_LIBRARY)
Application.mk
APP_STL := gnustl_static
APP_LDFLAGS := -latomic
APP_ABI := armeabi-v7a
APP_PLATFORM := android-19
NativeApp.cpp (因为FFmpeg是纯C实现,所以在cpp文件中引用头的时候,需加上[extern "C"]包裹)否则编译将出现如下错误:
jni/NativeAppTest.cpp:21: error: undefined reference to 'avcodec_configuration()' clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation) make: *** [obj/local/armeabi-v7a/libNativeAppTest.so] Error 1
#include <jni.h> extern "C"{ #include "libavcodec/avcodec.h" } #include <com_ltz_nativeapptest_anative_DoInFastMode.h> /* * Class: com_ltz_nativeapptest_anative_DoInFastMode * Method: getVersion * Signature: ()I */ JNIEXPORT jint JNICALL Java_com_ltz_nativeapptest_anative_DoInFastMode_getVersion (JNIEnv *, jclass){ return 0; } JNIEXPORT jstring JNICALL Java_com_ltz_nativeapptest_anative_DoInFastMode_getCfg (JNIEnv *env, jclass){ // TODO char info[10000] = {0}; sprintf(info, "%s\n", avcodec_configuration()); return env->NewStringUTF(info); }
另外附上JNI目录结构:
______________________________失败部分总结________________________________________
FFmpeg使用了master分支的最新代码,使用NDK r10c和r13b在编译 avcodec都出现了莫名其妙的错误:
1处是aaccoder.c 文件中search_for_ms函数 在初始化B0,B1报符号错误。怀疑变量名与宏冲突,所以改为了BC0,BC1,改文件编译通过。
2处:hevc_mvs.c 文件的宏定义
#define TAB_MVF_PU(v) \
TAB_MVF(((x ## v) >> s->ps.sps->log2_min_pu_size), \
((y ## v) >> s->ps.sps->log2_min_pu_size))
提示找不到x,y 由于不清楚操作符 “##” 代表什么含义,所以暂时还没解决该问题。