概述
x264的版本是:x264-snapshot-20131228-2245
NDK的版本:android-ndk-r8b
虚拟机VMware9.0.2,Ubuntu12.04,
Linux环境下开发需要进行环境变量配置。Linux环境下主要用于x264/ffmpeg使用ndk编译成动态库文件。至于使用Eclipse可以在Linux环境或者Windows环境下。环境配置主要是为了编译动态库使用的,与Eclipse开发无关。
Linux环境下,下载好上面的软件之后,进行如下配置:
一、终端输入:gedit ~/.bashrc
二、打开的脚本文件最后输入下面两行命令:
export NDK_HOME=/home/huaguanglu/src/android-ndk-r8b
export PATH=$PATH:$NDK_HOME
三、终端输入:gedit /etc/profile
四、打开的脚本文件最后输入下面两行命令:
#set arm-linux-androideabi-gcc
export
ARM_LINUX_ANDROIDEABI_GCC=/home/huaguanglu/src/android-ndk-r8b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin
export PATH=$ARM_LINUX_ANDROIDEABI_GCC:$PATH
可以将上面的命令直接复制使用,注意改一下用户名,即标蓝色的地方。
测试一下ndk可不可以使用。终端到ndk的samples/hello-jni/jni目录下,输入ndk-build。如图:
如果生成so文件,则说明ndk环境配置成功。
编译x264动态库
在主目录下新建文件夹bysj,x264代码放在~/bysj/目录下,在目录~/bysj/x264-snapshot-20131228-2245/下,新建脚本文件myconfig.sh.其代码为:
export ARM_ROOT=/home/huaguanglu/bysj/android-ndk-r8b export ARM_INC=$ARM_ROOT/platforms/android-8/arch-arm/usr/include/ export ARM_LIB=$ARM_ROOT/platforms/android-8/arch-arm/usr/lib/ export ARM_TOOL=$ARM_ROOT/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86 export ARM_LIBO=$ARM_TOOL/lib/gcc/arm-linux-androideabi/4.4.3 export PATH=$ARM_TOOL/bin:$PATH export ARM_PRE=arm-linux-androideabi ./configure --disable-gpac --extra-cflags=" -I$ARM_INC -fPIC -DANDROID -fpic -mthumb-interwork -ffunction-sections -funwind-tables -fstack-protector -fno-short-enums -D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ -Wno-psabi -march=armv5te -mtune=xscale -msoft-float -mthumb -Os -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64 -DANDROID -Wa,--noexecstack -MMD -MP " --extra-ldflags="-nostdlib -Bdynamic -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,nocopyreloc -Wl,-soname,/system/lib/libz.so -Wl,-rpath-link=$ARM_LIB,-dynamic-linker=/system/bin/linker -L$ARM_LIB -nostdlib $ARM_LIB/crtbegin_dynamic.o $ARM_LIB/crtend_android.o -lc -lm -ldl -lgcc" --cross-prefix=${ARM_PRE}- --disable-asm --host=arm-linux --disable-shared |
将上面代码复制到myconfig.sh中,终端到~/bysj/x264-snapshot-20131228-2245/目录下,输入命令:./myconfig.sh运行。运行完了之后,输入make。就会在当前目录下生成libx264.a静态库文件。
在~/workspace/VideoQT/目录下,新建jni目录,在jni目录下,新建libx264目录,在libx264目录下,新建include和lib目录。将~/bysj/x264-snapshot-20131228-2245/目录下的x264.h,x264_config.h拷贝到include目录下,将libx264.a拷贝到lib目录下。在jni目录下,新建Android.mk文件,其脚本程序为:
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_C_INCLUDES +=$(LOCAL_PATH)/libx264/include LOCAL_MODULE := x264jni LOCAL_SRC_FILES := VideoQianru.c #LOCAL_LDLIBS := $(LOCAL_PATH)/libx264.a LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog -lgcc LOCAL_LDFLAGS += $(LOCAL_PATH)/libx264/lib/libx264.a #LOCAL_LDLIBS += $(LOCAL_PATH)/libx264.a #LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -lgcc include $(BUILD_SHARED_LIBRARY) |
在jni目录下,新建文件VideoQianru.c文件,写jni程序,链接JAVA与C层。然后,终端到jni目录,输入ndk-build,完了之后,就会在VideoQT/libs/armeabi/目录下生成libx264jni.so文件。如图:
VideoQianru.C文件的代码为
#include <string.h> #include <jni.h> #include <stdio.h> #include <stdlib.h> #include <arpa/inet.h> #include <x264.h> typedef struct { x264_param_t * param; x264_t *handle; x264_picture_t * picture; x264_nal_t *nal; } Encoder;
jlong Java_com_videoqt_H264Encoder_EncodeBegin(JNIEnv* env, jobject thiz, jint width, jint height) { // int i=0; // static int j=0; static hua_k=0; Encoder * en = (Encoder *) malloc(sizeof(Encoder)); //FILE *hua_fp=fopen("/sdcard/x264H264Androidx264.txt","w+"); //fprintf(hua_fp,"hua_k=%d,Java_h264_com_H264Encoder_CompressBegin2014-02-19:1n",hua_k); hua_k++; //fflush(hua_fp); en->param = (x264_param_t *) malloc(sizeof(x264_param_t)); en->picture = (x264_param_t *) malloc(sizeof(x264_picture_t));
//x264_param_default(en->param); //set default param 浣跨敤//by hgl
x264_param_default(en->param); //set default param by other person
//en->param->rc.i_rc_method = X264_RC_CQP; //x264_param_default_preset(en->param, "fast", "zerolatency");
x264_param_apply_profile(en->param,"baseline");
en->param->i_log_level = X264_LOG_NONE; en->param->i_width = width; //set frame width en->param->i_height = height; //set frame height en->param->rc.i_lookahead =0; //en->param->rc.f_rf_constant=25;
//en->param->i_level_idc=12; en->param->ref=16; //en->param->i_frame_total=300; //en->param->rc.i_rc_method = X264_RC_CRF; /en->param.rc.i_vbv_max_bitrate=(int)((m_bitRate*1.2)/1000) ; // en->param.rc.i_bitrate = (int)m_bitRate/1000; ///*from here add by hgl 2013-12-31 10:06*/ en->param->i_csp=X264_CSP_I420; en->param->i_frame_reference=1; // //
//en->param->i_threads=1; //en->param->b_intra_refresh=1; //en->param->i_bframe=0;//0 //en->param->b_annexb=1;
//en->param->b_cabac=0;//0 //en->param->i_cabac_init_idc=-1;//-1
//if(i<1000){ //en->param->hua_level[0]=-255; //i++; //fprintf(hua_fp,"绗?d娆★紝%dn",j,en->param->hua_level[0]) ; //j++; //} //en->param->rc.b_cbr=1; en->param->i_fps_num =25; en->param->i_fps_den = 1; //en-> param->i_keyint_max =25;
//en->param->i_fps_num =5 ; /*to here add by hgl 2013-12-31 10:06*/ //en->param->b_open_gop = 1; //en->param->i_fps_den = 1; //by hgl from //if ((en->handle = x264_encoder_open(en->param)) != 0) {
//for(i=0;i<1000;i++){ //fprintf(hua_fp,"绗?d娆★紝%dn",j,en->param->hua_level[0]) ; // } //j++; //} //else{ // return 0; //} //by hgl to
if ((en->handle = x264_encoder_open(en->param)) == 0) {
return 0; } /* Create a new pic */
x264_picture_alloc(en->picture, X264_CSP_I420, en->param->i_width, en->param->i_height);
return (jlong) en;
} jint Java_com_videoqt_H264Encoder_EncodeEnd(JNIEnv* env, jobject thiz,jlong handle) { Encoder * en = (Encoder *) handle; if(en->picture) { x264_picture_clean(en->picture); free(en->picture); en->picture = 0; } if(en->param) { free(en->param); en->param=0; } if(en->handle) { x264_encoder_close(en->handle); } free(en); return 0; } jint Java_com_videoqt_H264Encoder_EncodeContinue(JNIEnv* env, jobject thiz,jlong handle,jint type,jbyteArray in, jint insize,jbyteArray out) { Encoder * en = (Encoder *) handle; x264_picture_t pic_out;
int i_data=0; int nNal=-1; int result=0; int i=0,j=0; int nPix=0;
jbyte * Buf = (jbyte*)(*env)->GetByteArrayElements(env, in, 0); jbyte * h264Buf = (jbyte*)(*env)->GetByteArrayElements(env, out, 0); unsigned char * pTmpOut = h264Buf;
int nPicSize=en->param->i_width*en->param->i_height; jbyte * y=en->picture->img.plane[0]; jbyte * v=en->picture->img.plane[1]; jbyte * u=en->picture->img.plane[2]; memcpy(en->picture->img.plane[0],Buf,nPicSize); for (i=0;i<nPicSize/4;i++) { *(u+i)=*(Buf+nPicSize+i*2); *(v+i)=*(Buf+nPicSize+i*2+1); }
switch (type) { case 0: en->picture->i_type = X264_TYPE_P; break; case 1: en->picture->i_type = X264_TYPE_IDR; break; case 2: en->picture->i_type = X264_TYPE_I; break; default: en->picture->i_type = X264_TYPE_AUTO; break; }
if( x264_encoder_encode( en->handle, &(en->nal), &nNal, en->picture ,&pic_out) < 0 ) { return -1; } for (i = 0; i < nNal; i++){ memcpy(pTmpOut, en->nal[i].p_payload, en->nal[i].i_payload); pTmpOut += en->nal[i].i_payload; result+=en->nal[i].i_payload; } return result; }
|
最后
以上就是动人柚子为你收集整理的将x264移植到Android的全部内容,希望文章能够帮你解决将x264移植到Android所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复