測試編譯ffmpeg庫

操做系統:Linux (window我還沒試過哦)html

1.建立一個新的Android Project 命名爲FFmpegTestjava

目標平臺最好選擇與編譯好的libffmpeg.so相同的平臺,在FFmpegTest目錄下建立一個jni目錄。linux

2.下載ffmpeg源代碼並放到jni目錄下android

下載地址:http://ffmpeg.org/download.html 這是0.8 「Love」版本的ffmpeg,若是你想獲取最新版本的ffmpeg源代碼能夠經過Git或者SVN獲取(具體問google)。shell

下載完後放到jni目錄下的ffmpeg目錄。ubuntu

3.編譯ffmpegbash

3.1複製粘貼如下腳本代碼,保存命名爲 build_android.sh ,並放到ffmpeg目錄下。app

注意:這幾行代碼是須要修改的ide

NDK=~/Desktop/android/android-ndk-r5b
PLATFORM=$NDK/platforms/android-8/arch-arm/
PREBUILT=$NDK/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86

根據你本身的狀況指定NDK的各個目錄,你也能夠根據你項目的目標平臺調整 PLATFORM 變量的值, SDK 2.2 對應的是android-8.函數

#!/bin/bash
######################################################
# Usage:
# put this script in top of FFmpeg source tree
# ./build_android
# It generates binary for following architectures:
# ARMv6 
# ARMv6+VFP 
# ARMv7+VFPv3-d16 (Tegra2) 
# ARMv7+Neon (Cortex-A8)
# Customizing:
# 1. Feel free to change ./configure parameters for more features
# 2. To adapt other ARM variants
# set $CPU and $OPTIMIZE_CFLAGS 
# call build_one
######################################################
NDK=/home/guokai/workspace/ffmpeg-test/android-ndk-r8/
PLATFORM=$NDK/platforms/android-14/arch-arm/
PREBUILT=$NDK/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/
function build_one
{
./configure --target-os=linux \
    --prefix=$PREFIX \
    --enable-cross-compile \
    --extra-libs="-lgcc" \
    --arch=arm \
    --cc=$PREBUILT/bin/arm-linux-androideabi-gcc \
    --cross-prefix=$PREBUILT/bin/arm-linux-androideabi- \
    --nm=$PREBUILT/bin/arm-linux-androideabi-nm \
    --sysroot=$PLATFORM \
    --extra-cflags=" -O3 -fpic -DANDROID -DHAVE_SYS_UIO_H=1 -Dipv6mr_interface=ipv6mr_ifindex -fasm -Wno-psabi -fno-short-enums -fno-strict-aliasing -finline-limit=300 $OPTIMIZE_CFLAGS " \
    --disable-shared \
    --enable-static \
    --extra-ldflags="-Wl,-rpath-link=$PLATFORM/usr/lib -L$PLATFORM/usr/lib -nostdlib -lc -lm -ldl -llog" \
    --disable-everything \
    --enable-demuxer=mov \
    --enable-demuxer=h264 \
    --disable-ffplay \
    --enable-protocol=file \
    --enable-avformat \
    --enable-avcodec \
    --enable-decoder=rawvideo \
    --enable-decoder=mjpeg \
    --enable-decoder=h263 \
    --enable-decoder=mpeg4 \
    --enable-decoder=h264 \
    --enable-parser=h264 \
    --disable-network \
    --enable-zlib \
    --disable-avfilter \
    --disable-avdevice \
    $ADDITIONAL_CONFIGURE_FLAG

make clean
make  -j4 install
$PREBUILT/bin/arm-linux-androideabi-ar d libavcodec/libavcodec.a inverse.o
$PREBUILT/bin/arm-linux-androideabi-ld -rpath-link=$PLATFORM/usr/lib -L$PLATFORM/usr/lib  -soname libffmpeg.so -shared -nostdlib  -z,noexecstack -Bsymbolic --whole-archive --no-undefined -o $PREFIX/libffmpeg.so libavcodec/libavcodec.a libavformat/libavformat.a libavutil/libavutil.a libswscale/libswscale.a -lc -lm -lz -ldl -llog  --warn-once  --dynamic-linker=/system/bin/linker $PREBUILT/lib/gcc/arm-linux-androideabi/4.4.3/libgcc.a
}

#arm v6
#CPU=armv6
#OPTIMIZE_CFLAGS="-marm -march=$CPU"
#PREFIX=./android/$CPU 
#ADDITIONAL_CONFIGURE_FLAG=
#build_one

#arm v7vfpv3
CPU=armv7-a
OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=vfpv3-d16 -marm -march=$CPU "
PREFIX=./android/$CPU
ADDITIONAL_CONFIGURE_FLAG=
build_one

#arm v7vfp
#CPU=armv7-a
#OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=vfp -marm -march=$CPU "
#PREFIX=./android/$CPU-vfp
#ADDITIONAL_CONFIGURE_FLAG=
#build_one

#arm v7n
#CPU=armv7-a
#OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=neon -marm -march=$CPU -mtune=cortex-a8"
#PREFIX=./android/$CPU 
#ADDITIONAL_CONFIGURE_FLAG=--enable-neon
#build_one

#arm v6+vfp
#CPU=armv6
#OPTIMIZE_CFLAGS="-DCMP_HAVE_VFP -mfloat-abi=softfp -mfpu=vfp -marm -march=$CPU"
#PREFIX=./android/${CPU}_vfp 
#ADDITIONAL_CONFIGURE_FLAG=
#build_one

爲了提升編譯速度此腳本放棄了ffmpeg的不少東西,你能夠根據本身的需求改變腳本文件的配置,此外,你還能夠編譯多硬件平臺的庫文件,本例只編譯 arm v7vfpv3,由於編譯arm的庫速度比較快~~╮(╯▽╰)╭  請原諒 湯姆貓的懶惰。


3.2 確保 bash 腳本文件是可執行的. Linux系統使用終端進入項目jin目錄下的ffmpeg目錄,輸入如下命令:

sudo chmod 755 build_android.sh  ,這樣就能確保腳本文件時可執行的啦~

3.3 使用終端執行腳本文件.

進入到 bash 腳本文件所在目錄也就是jni/ffmpeg下,輸入如下命令

./build_android.sh

執行腳本文件(應該是幾分鐘就編譯完成了)。


注意:NDK-r6或者以上的版本若是沒法編譯經過,能夠試試下面這個腳本文件

<span style="color:#333333;">#!/bin/bash
######################################################
# Usage:
# put this script in top of FFmpeg source tree
# ./build_android
#
# It generates binary for following architectures:
# ARMv6 
# ARMv6+VFP 
# ARMv7+VFPv3-d16 (Tegra2)
# ARMv7+Neon (Cortex-A8)
#
# Customizing:
# 1. Feel free to change ./configure parameters for more features
# 2. To adapt other ARM variants
# set $CPU and $OPTIMIZE_CFLAGS 
# call build_one
######################################################
#change these three lines if you want to build using different vesion of Android ndk
#build_one is for ndk 5, and build_one_r6 is for ndk 6
NDK=~/ffmpeg/android-ndk-r6
PLATFORM=$NDK/platforms/android-8/arch-arm/
PREBUILT=$NDK/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86
function build_one_r6
{
./configure \
    --disable-shared \
    --enable-static \
    --enable-gpl \
    --enable-version3 \
    --enable-nonfree \
    --disable-doc \
    --disable-ffmpeg \
    --disable-ffplay \
    --disable-ffprobe \
    --disable-ffserver \
    --disable-avdevice \
    --disable-avfilter \
    --disable-postproc \
    --enable-small \
    --cross-prefix=$PREBUILT/bin/arm-linux-androideabi- \
    --enable-cross-compile \
    --target-os=linux \
    --extra-cflags="-I$PLATFORM/usr/include" \
    --extra-ldflags="-L$PLATFORM/usr/lib -nostdlib" \
    --arch=arm \
    --disable-symver \
    --disable-debug \
    --disable-stripping \
    $ADDITIONAL_CONFIGURE_FLAG
sed -i 's/HAVE_LRINT 0/HAVE_LRINT 1/g' config.h
sed -i 's/HAVE_LRINTF 0/HAVE_LRINTF 1/g' config.h
sed -i 's/HAVE_ROUND 0/HAVE_ROUND 1/g' config.h
sed -i 's/HAVE_ROUNDF 0/HAVE_ROUNDF 1/g' config.h
sed -i 's/HAVE_TRUNC 0/HAVE_TRUNC 1/g' config.h
sed -i 's/HAVE_TRUNCF 0/HAVE_TRUNCF 1/g' config.h
make clean
make  -j4 install
$PREBUILT/bin/arm-linux-androideabi-ar d libavcodec/libavcodec.a inverse.o
$PREBUILT/bin/arm-linux-androideabi-ld -rpath-link=$PLATFORM/usr/lib -L$PLATFORM/usr/lib  -soname libffmpeg.so -shared -nostdlib  -z,noexecstack -Bsymbolic --whole-archive --no-undefined -o $PREFIX/libffmpeg.so libavcodec/libavcodec.a libavformat/libavformat.a libavutil/libavutil.a libswscale/libswscale.a -lc -lm -lz -ldl -llog  --warn-once  --dynamic-linker=/system/bin/linker $PREBUILT/lib/gcc/arm-linux-androideabi/4.4.3/libgcc.a
}
function build_one_r6_2
{
$PREBUILT/bin/arm-linux-androideabi-ar d libavcodec/libavcodec.a inverse.o
$PREBUILT/bin/arm-linux-androideabi-ld -rpath-link=$PLATFORM/usr/lib -L$PLATFORM/usr/lib  -soname libffmpeg.so -shared -nostdlib  -z,noexecstack -Bsymbolic --whole-archive --no-undefined -o $PREFIX/libffmpeg.so libavcodec/libavcodec.a libavformat/libavformat.a libavutil/libavutil.a libswscale/libswscale.a -lc -lm -lz -ldl -llog  --warn-once  --dynamic-linker=/system/bin/linker $PREBUILT/lib/gcc/arm-linux-androideabi/4.4.3/libgcc.a
}
#arm v6
#CPU=armv6
#OPTIMIZE_CFLAGS="-marm -march=$CPU"
#PREFIX=./android/$CPU 
#ADDITIONAL_CONFIGURE_FLAG=
#build_one
#arm v7vfpv3
CPU=armv7-a
OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=vfpv3-d16 -marm -march=$CPU "
PREFIX=./android/$CPU
ADDITIONAL_CONFIGURE_FLAG=
#build_one
build_one_r6
#arm v7vfp
#CPU=armv7-a
#OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=vfp -marm -march=$CPU "
#PREFIX=./android/$CPU-vfp
#ADDITIONAL_CONFIGURE_FLAG=
#build_one
#arm v7n
#CPU=armv7-a
#OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=neon -marm -march=$CPU -mtune=cortex-a8"
#PREFIX=./android/$CPU 
#ADDITIONAL_CONFIGURE_FLAG=--enable-neon
#build_one
#arm v6+vfp
#CPU=armv6
#OPTIMIZE_CFLAGS="-DCMP_HAVE_VFP -mfloat-abi=softfp -mfpu=vfp -marm -march=$CPU"
#PREFIX=./android/${CPU}_vfp 
#ADDITIONAL_CONFIGURE_FLAG=
#build_one</span>

網上說可能還須要在ffmpeg目錄下建立 ./android/armv7-a/的目錄結構.


4.完成編譯

當腳本文件執行完畢,會在ffmpeg目錄下生成一個android的文件夾,裏面就包含了全部的編譯成果。

5.將編譯完成的.so或.a庫,使用JNI規範集成到android應用中供java代碼調用。

5.1下面的代碼是調用ffmpeg庫的jni層代碼,能夠在jni目錄下建立一個ffmpeg-test-jni.c文件,將代碼粘貼過去,文章末尾會提供 本例完整的工程項目的 下載連接。。

/**
this is the wrapper of the native functions 
**/
/*android specific headers*/
#include <jni.h>
#include <android/log.h>
#include <android/bitmap.h>
/*standard library*/
#include <time.h>
#include <math.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <unistd.h>
#include <assert.h>
/*ffmpeg headers*/
#include <libavutil/avstring.h>
#include <libavutil/pixdesc.h>
#include <libavutil/imgutils.h>
#include <libavutil/samplefmt.h>

#include <libavformat/avformat.h>

#include <libswscale/swscale.h>

#include <libavcodec/avcodec.h>
#include <libavcodec/opt.h>
#include <libavcodec/avfft.h>

/*for android logs*/
#define LOG_TAG "FFmpegTest"
#define LOG_LEVEL 10
#define LOGI(level, ...) if (level <= LOG_LEVEL) {__android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__);}
#define LOGE(level, ...) if (level <= LOG_LEVEL) {__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__);}

/**/
char *gFileName;	  //the file name of the video

AVFormatContext *gFormatCtx;
int gVideoStreamIndex;    //video stream index

AVCodecContext *gVideoCodecCtx;

static void get_video_info(char *prFilename);

/*parsing the video file, done by parse thread*/
static void get_video_info(char *prFilename) {
    AVCodec *lVideoCodec;
    int lError;
    /*some global variables initialization*/
    LOGI(10, "get video info starts!");
    /*register the codec*/
    extern AVCodec ff_h263_decoder;
    avcodec_register(&ff_h263_decoder);
    extern AVCodec ff_h264_decoder;
    avcodec_register(&ff_h264_decoder);
    extern AVCodec ff_mpeg4_decoder;
    avcodec_register(&ff_mpeg4_decoder);
    extern AVCodec ff_mjpeg_decoder;
    avcodec_register(&ff_mjpeg_decoder);
    /*register parsers*/
    //extern AVCodecParser ff_h264_parser;
    //av_register_codec_parser(&ff_h264_parser);
    //extern AVCodecParser ff_mpeg4video_parser;
    //av_register_codec_parser(&ff_mpeg4video_parser);
    /*register demux*/
    extern AVInputFormat ff_mov_demuxer;
    av_register_input_format(&ff_mov_demuxer);
    //extern AVInputFormat ff_h264_demuxer;
    //av_register_input_format(&ff_h264_demuxer);
    /*register the protocol*/
    extern URLProtocol ff_file_protocol;
    av_register_protocol2(&ff_file_protocol, sizeof(ff_file_protocol));

/*open the video file--根據文件名來初始化AVFormatContext*/
    if ((lError = av_open_input_file(&gFormatCtx, gFileName, NULL, 0, NULL)) !=0 ) {
        LOGE(1, "Error open video file: %d", lError);
        return;	//open file failed
    }

/*retrieve stream information--從AVFormatContext提取流信息*/
    if ((lError = av_find_stream_info(gFormatCtx)) < 0) {
        LOGE(1, "Error find stream information: %d", lError);
        return;
    }
 
/*find the video stream and its decoder--經過AVFormatContext找到文件的stream和編碼類型*/
    gVideoStreamIndex = av_find_best_stream(gFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, &lVideoCodec, 0);
    if (gVideoStreamIndex == AVERROR_STREAM_NOT_FOUND) {
        LOGE(1, "Error: cannot find a video stream");
        return;
    } else {
	LOGI(10, "video codec: %s", lVideoCodec->name);
    }
    if (gVideoStreamIndex == AVERROR_DECODER_NOT_FOUND) {
        LOGE(1, "Error: video stream found, but no decoder is found!");
        return;
    }  
 
/*open the codec--從編碼類型打開解碼器*/
    gVideoCodecCtx = gFormatCtx->streams[gVideoStreamIndex]->codec;
    LOGI(10, "open codec: (%d, %d)", gVideoCodecCtx->height, gVideoCodecCtx->width);
#ifdef SELECTIVE_DECODING
    gVideoCodecCtx->allow_selective_decoding = 1;
#endif
    if (avcodec_open(gVideoCodecCtx, lVideoCodec) < 0) {/*用找的編碼類別lVideoCodec類初始化AVCodecContext*/
	LOGE(1, "Error: cannot open the video codec!");
        return;
    }
    LOGI(10, "get video info ends");
}


JNIEXPORT void JNICALL Java_roman10_ffmpegTest_VideoBrowser_naClose(JNIEnv *pEnv, jobject pObj) {
    int l_mbH = (gVideoCodecCtx->height + 15) / 16;
    /*close the video codec*/
    avcodec_close(gVideoCodecCtx);
    /*close the video file*/
    av_close_input_file(gFormatCtx);
}

JNIEXPORT void JNICALL Java_roman10_ffmpegTest_VideoBrowser_naInit(JNIEnv *pEnv, jobject pObj, jstring pFileName) {
    int l_mbH, l_mbW;
    /*get the video file name*/
    gFileName = (char *)(*pEnv)->GetStringUTFChars(pEnv, pFileName, NULL);
    if (gFileName == NULL) {
        LOGE(1, "Error: cannot get the video file name!");
        return;
    } 
    LOGI(10, "video file name is %s", gFileName);
    get_video_info(gFileName);
    LOGI(10, "initialization done");
}

JNIEXPORT jstring JNICALL Java_roman10_ffmpegTest_VideoBrowser_naGetVideoCodecName(JNIEnv *pEnv, jobject pObj) {
    char* lCodecName = gVideoCodecCtx->codec->name;
    return (*pEnv)->NewStringUTF(pEnv, lCodecName);
}

JNIEXPORT jstring JNICALL Java_roman10_ffmpegTest_VideoBrowser_naGetVideoFormatName(JNIEnv *pEnv, jobject pObj) {
    char* lFormatName = gFormatCtx->iformat->name;
    return (*pEnv)->NewStringUTF(pEnv, lFormatName);
}


JNIEXPORT jintArray JNICALL Java_roman10_ffmpegTest_VideoBrowser_naGetVideoResolution(JNIEnv *pEnv, jobject pObj) {
    jintArray lRes;
    lRes = (*pEnv)->NewIntArray(pEnv, 2);
    if (lRes == NULL) {
        LOGI(1, "cannot allocate memory for video size");
        return NULL;
    }
    jint lVideoRes[2];
    lVideoRes[0] = gVideoCodecCtx->width;
    lVideoRes[1] = gVideoCodecCtx->height;
    (*pEnv)->SetIntArrayRegion(pEnv, lRes, 0, 2, lVideoRes);
    return lRes;
}

若是你不熟悉java的JNI開發, 你可能須要先閱讀JNI規範後才能看懂以上的代碼。


5.2編譯本地代碼,將ffmpeg.so集成到本身的.so庫

在jni目錄下建立一個 Android.mk 文件,此文件是用於告訴NDK如何去編譯這個項目的jni層代碼

LOCAL_PATH := $(call my-dir)

#declare the prebuilt library

include $(CLEAR_VARS)

LOCAL_MODULE := ffmpeg-prebuilt

LOCAL_SRC_FILES := ffmpeg-0.8/android/armv7-a/libffmpeg.so  

LOCAL_EXPORT_C_INCLUDES := ffmpeg-0.8/android/armv7-a/include 

LOCAL_EXPORT_LDLIBS := ffmpeg-0.8/android/armv7-a/libffmpeg.so

LOCAL_PRELINK_MODULE := true # 該模塊是否被啓動就加載。該項設置依具體程序的特性而定。

include $(PREBUILT_SHARED_LIBRARY)

#the ffmpeg-test-jni library

include $(CLEAR_VARS)

LOCAL_ALLOW_UNDEFINED_SYMBOLS=false

LOCAL_MODULE := ffmpeg-test-jni  #當前模塊的名稱,也就是第一步中咱們提到的LIBNAME。
				#注意這個須要加上lib前綴,但不須要加.so後綴,也就是說應該是libLIBNAME
LOCAL_SRC_FILES := ffmpeg-test-jni.c  # 包含的源文件

LOCAL_C_INCLUDES := $(LOCAL_PATH)/ffmpeg-0.8/android/armv7-a/include 
						 #包含的頭文件。這裏是須要包含JNI的頭文件。
LOCAL_SHARED_LIBRARY := ffmpeg-prebuilt # 當前模塊須要依賴的共享庫

LOCAL_LDLIBS    := -llog -ljnigraphics -lz -lm $(LOCAL_PATH)/ffmpeg-0.8/android/armv7-a/libffmpeg.so

include $(BUILD_SHARED_LIBRARY)

再在jni目錄新建另外一個新的文件命名爲 Application.mk,代碼以下:

# The ARMv7 is significanly faster due to the use of the hardware FPU
 

APP_ABI := armeabi-v7a
 

APP_PLATFORM := android-8

關於Android.mk 與 Application.mk的更多信息請參考 android NDK的官方文檔 .

如今你的工程目錄結構應該是醬紫的....



5.3 編寫java native 方法調用 本地代碼(Native Code)

要調用本地方法也就是.so庫中的方法,須要先把.so加載到虛擬機中,如下是代碼片斷,後面會提供項目的下載連接

/*this part communicates with native code through jni (java native interface)*/
 

    //load the native library
 

    static {
 

        System.loadLibrary("ffmpeg");
 

        System.loadLibrary("ffmpeg-test-jni");
 

    }
 

    //declare the jni functions
 

    private static native void naInit(String _videoFileName);
 

    private static native int[] naGetVideoResolution();
 

    private static native String naGetVideoCodecName();
 

    private static native String naGetVideoFormatName();
 

    private static native void naClose();
 

 

    private void showVideoInfo(final File _file) {
 

        String videoFilename = _file.getAbsolutePath();
 

        naInit(videoFilename);
 

        int[] prVideoRes = naGetVideoResolution();
 

        String prVideoCodecName = naGetVideoCodecName();
 

        String prVideoFormatName = naGetVideoFormatName();
 

        naClose();
 

        String displayText = "Video: " + videoFilename + "\n";
 

        displayText += "Video Resolution: " + prVideoRes[0] + "x" + prVideoRes[1] + "\n";
 

        displayText += "Video Codec: " + prVideoCodecName + "\n";
 

        displayText += "Video Format: " + prVideoFormatName + "\n";
 

        text_titlebar_text.setText(displayText);
 

    }

這幾個java 的native方法與上面實現的c 代碼中的幾個方法是一 一對應的,或者你能夠先在命令行中cd 到工程的classes目錄下使用javah工具生成這些java native方法對應的.h頭文件,再新建一個.c文件將.h文件include進來再一 一去實現其中的函數。.h頭文件並非必須的...但生成.h文件比較規範.


5.4 編譯集成ffmpeg 這一步在5.3以前作也能夠...

Android.mk 、Application.mk、ffmpeg-test-jni.c、ffmpeg/android中的庫文件都準備齊全後,

使用終端cd 到工程目錄(FFmpegTest) 執行命令:

ndk-build


出現以上提示則表示編譯經過了 ╮(╯▽╰)╭ 終於搞定了

刷新工程目錄後會生成一個libs文件夾,裏面就包含了咱們編譯好的.so庫



6.最後安裝應用開始測試吧~

項目工程下載: http://download.csdn.net/detail/teisun/4276777


個人開發環境是ubuntu 10.04 和ndk-r五、ndk-r六、 ndk-r7都測試經過,window平臺不敢保證能夠 順利編譯哦。

相關文章
相關標籤/搜索