編碼器使用的是x264的開源庫,
很容易看懂的
簡(jiǎn)單的封裝了一個(gè)JNI庫
編碼庫在BBS里 CSDN的資源太難用了
http://www./forum.php?mod=viewthread&tid=52739&extra=
x264的編譯放方法
export ARM_ROOT=$ANDROID_NDK_ROOT
export ARM_INC=$ARM_ROOT/build/platforms/android-5/arch-arm/usr/include/
export ARM_LIB=$ARM_ROOT/build/platforms/android-5/arch-arm/usr/lib/
export ARM_TOOL=$ARM_ROOT/build/prebuilt/windows/arm-eabi-4.4.0
export ARM_LIBO=$ARM_TOOL/lib/gcc/arm-eabi/4.4.0
export PATH=$ARM_TOOL/bin:$PATH
export ARM_PRE=arm-eabi
./configure --prefix=/home/egmkang/libx264 --enable-shared \
-disable-asm --host=arm-linux --cross-prefix=arm-eabi-\
--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"
這里生成的是x264的靜態(tài)庫
整個(gè)工程唯一有點(diǎn)麻煩的是 生成 JNI 動(dòng)態(tài)庫的時(shí)候 報(bào)錯(cuò) ,。。
后來發(fā)現(xiàn)是少鏈接了一個(gè)庫,,
于是根據(jù)x264的編譯方法 在Android.mk添加一些配置就可以了,。當(dāng)然這就是難點(diǎn),在網(wǎng)上查了很多都沒有結(jié)果,。
有些目錄的參數(shù)自己調(diào)整哈
我把前面生成的libx264.a 和 x264.h 文件放到j(luò)ni的libx264目錄下了 有問題自己調(diào)整
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_C_INCLUDES +=$(LOCAL_PATH)/libx264/include
LOCAL_MODULE := H264Android
LOCAL_SRC_FILES := H264Android.c
LOCAL_LDFLAGS += $(LOCAL_PATH)/libx264/lib/libx264.a
LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -lgcc
include $(BUILD_SHARED_LIBRARY)
估計(jì)很多人都會(huì)發(fā)現(xiàn)很熟悉 嘻嘻 這個(gè)就是根據(jù)
http://www.cnblogs.com/mcodec/articles/1780598.html
改的 連文件名字都沒有換?。”容^懶
另: 編碼的效率很低下啊
AndroidVideo.java
- import java.io.File;
- import java.io.RandomAccessFile;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.content.res.Configuration;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
- import android.view.Window;
- import android.view.WindowManager;
- import android.view.SurfaceHolder.Callback;
- import android.graphics.PixelFormat;
- import android.hardware.Camera;
- public class AndroidVideo extends Activity implements Callback,
- Camera.PictureCallback {
- private SurfaceView mSurfaceView = null;
- private SurfaceHolder mSurfaceHolder = null;
- private Camera mCamera = null;
- private boolean mPreviewRunning = false;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- getWindow().setFormat(PixelFormat.TRANSLUCENT);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
- WindowManager.LayoutParams.FLAG_FULLSCREEN);
- setContentView(R.layout.camera);
- mSurfaceView = (SurfaceView) this.findViewById(R.id.surface_camera);
- mSurfaceHolder = mSurfaceView.getHolder();
- mSurfaceHolder.addCallback(this);
- mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
- }
-
- @Override
- public void onPictureTaken(byte[] data, Camera camera) {
- }
- @Override
- public void surfaceChanged(SurfaceHolder holder, int format, int width,
- int height) {
- if (mPreviewRunning) {
- mCamera.stopPreview();
- }
- Camera.Parameters p = mCamera.getParameters();
- p.setPreviewSize(352, 288);
- mCamera.setPreviewCallback(new H264Encoder(352, 288));
- mCamera.setParameters(p);
- try {
- mCamera.setPreviewDisplay(holder);
- } catch (Exception ex) {
- }
- mCamera.startPreview();
- mPreviewRunning = true;
- }
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
- mCamera = Camera.open();
- }
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {
- if (mCamera != null) {
- mCamera.setPreviewCallback(null);
- mCamera.stopPreview();
- mPreviewRunning = false;
- mCamera.release();
- mCamera = null;
- }
- }
- public void onConfigurationChanged(Configuration newConfig) {
- try {
- super.onConfigurationChanged(newConfig);
- if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
- } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
- }
- } catch (Exception ex) {
- }
- }
- }
- class H264Encoder implements Camera.PreviewCallback {
- long encoder=0;
- RandomAccessFile raf=null;
- byte[] h264Buff =null;
- static {
- System.loadLibrary("H264Android");
- }
- private H264Encoder(){};
-
- public H264Encoder(int width, int height) {
- encoder = CompressBegin(width, height);
- h264Buff = new byte[width * height *8];
- try {
- File file = new File("/sdcard/camera.h264");
- raf = new RandomAccessFile(file, "rw");
- } catch (Exception ex) {
- Log.v("System.out", ex.toString());
- }
-
- };
- protected void finalize()
- {
- CompressEnd(encoder);
- if (null != raf)
- {
- try {
- raf.close();
- } catch (Exception ex) {
- Log.v("System.out", ex.toString());
- }
- }
- try {
- super.finalize();
- } catch (Throwable e) {
-
- e.printStackTrace();
- }
- }
- private native long CompressBegin(int width,int height);
- private native int CompressBuffer(long encoder, int type,byte[] in, int insize,byte[] out);
- private native int CompressEnd(long encoder);
- @Override
- public void onPreviewFrame(byte[] data, Camera camera) {
-
- int result=CompressBuffer(encoder, -1, data, data.length,h264Buff);
- try {
- if (result>0)
- raf.write(h264Buff, 0, result);
- } catch (Exception ex) {
- Log.v("System.out", ex.toString());
- }
- }
-
- }
H264Android.c
- #include <string.h>
- #include <jni.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <arpa/inet.h>
- #include <x264.h>
- #define DATA_MAX 3000000
- #define H264_MTU 1024
- typedef struct
- {
- x264_param_t * param;
- x264_t *handle;
- x264_picture_t * picture;
- x264_nal_t *nal;
- } Encoder;
- jlong Java_h264_com_H264Encoder_CompressBegin(JNIEnv* env, jobject thiz,
- jint width, jint height) {
- Encoder * en = (Encoder *) malloc(sizeof(Encoder));
- 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);
-
- en->param->i_log_level = X264_LOG_NONE;
- en->param->i_width = width;
- en->param->i_height = height;
- en->param->rc.i_lookahead =0;
- en->param->i_bframe=0;
- en->param->i_fps_num =5;
- en->param->i_fps_den = 1;
- if ((en->handle = x264_encoder_open(en->param)) == 0) {
- return 0;
- }
-
- x264_picture_alloc(en->picture, X264_CSP_I420, en->param->i_width,
- en->param->i_height);
- return (jlong) en;
- }
- jint Java_h264_com_H264Encoder_CompressEnd(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_h264_com_H264Encoder_CompressBuffer(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;
- }