x264在Android平台上的编译和使用

x264在Android平台上的移植

gedit ~/.bashrc
将 /opt/android-sdk-linux/tools 添加到 "PATH"环境变量中
将 /opt/android-ndk-r8b 添加到 "PATH"环境变量中


将x264源码包last_x264.tar.bz2下载到~/x264-android目录下,并解压到目录~/x264-android/x264-snapshot-20130808-2245/下

cd ~/x264-android/x264-snapshot-20130808-2245

执行以下配置脚本:

./configure --enable-pic --enable-strip --enable-static --cross-prefix=/opt/android-ndk-r8b/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86/bin/arm-linux-androideabi- --sysroot=/opt/android-ndk-r8b/platforms/android-14/arch-arm --host=arm-linux --prefix=./build --extra-cflags="-march=armv7-a -mtune=cortex-a8 -mfloat-abi=softfp -mfpu=neon -D__ARM_ARCH_7__ -D__ARM_ARCH_7A__"

然后:make

最后:在当前目录下生成libx264.a的静态库

注:最好用android-ndk-r8b之前的ndk版本(包括android-ndk-r8b)编译,android-ndk-r8b之后的版本编译x264,最后会报"cannot scan executable section 1 of libx264.a(dct-a.o) for Cortex-A8 erratum because it has no mapping symbols"的警告

x264在android平台上的使用


1、创建工程x264_android

android list targets
android create project --target 1 --name x264_android --path ~/workspace/x264_android --package com.modukaikai.x264 --activity x264Activity


2、copy x264的库和头文件到jni目录

mkdir ~/workspace/x264_android/jni
mkdir ~/workspace/x264_android/jni/x264
cp ~/x264-android/x264-snapshot-20130808-2245/libx264.a ~/workspace/x264_android/jni/x264
cp ~/x264-android/x264-snapshot-20130808-2245/x264.h ~/workspace/x264_android/jni/x264
cp ~/x264-android/x264-snapshot-20130808-2245/x264_config.h ~/workspace/x264_android/jni/x264


3、创建x264encoder类封装x264的调用接口

以下是x264encoder.h(放在jni目录下)

#ifndef x264encoder_h
#define x264encoder_h
 
extern "C"
{
#include "stdint.h"
#include "x264.h"
 
    enum bitrate_level
    {
        HIGH_LEVEL = 0,
        STANDARD_LEVEL = 1,
        MEDIUM_LEVEL = 2,
        LOW_LEVEL = 3,
    };
   
    class X264Encoder
    {
    public:
        X264Encoder();
        ~X264Encoder();
       
        bool openX264Encoder();
        //    long x264EncoderProcess(uint8_t *pSrcData, int srcDataSize, x264_nal_t **nals, int& nalsCount);
        long x264EncoderProcess(x264_picture_t *pPicture, x264_nal_t **nals, int& nalsCount);
        bool closeX264Encoder();
       
        void setSourceFormat(unsigned int sourcformat);
        void setResolution(unsigned int w, unsigned int h);
        void setBitrate(unsigned int i_bitrate);
        void setFps(unsigned int fps);
        void setI_KeyInt_Max(unsigned int i_frame_max);
        void setQp_Max(unsigned int qp_max);
        void setQp_Min(unsigned int qp_min);
       
        void forceIDRFrame();
       
        void upgradeBitrateLevel();
        void declineBitrateLevel();
        void setLeastBitrateLevel();
       
    private:
       
        x264_param_t *pParameter;
        x264_t *x264EncoderHandle;
        //    x264_picture_t *pPicture;
        x264_picture_t *pOutput;
       
        unsigned int sourceFormat;
        //    unsigned int i_bitrate;
        unsigned int bitratelevel;
        unsigned int i_fps;
        unsigned int i_keyint_max;
        unsigned int width;
        unsigned int height;
        unsigned int qp_max;
        unsigned int qp_min;
       
        unsigned int current_f_rf_constant;
        unsigned int userSetting_f_rf_constant;
       
        int64_t frameNo;
       
        bool isForceIDRFrameEnabled;
    };
}
 
#endif


以下是x264encoder.cpp(放在jni目录下)

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/19373.html