织梦CMS - 轻松建站从此开始!

罗索

庆祝一下,Android视频采集+H264编码成功(2)

jackyhwei 发布于 2011-06-28 09:49 点击:次 
H264Android.c #includestring.h #includejni.h #includestdio.h #includestdlib.h #includearpa/inet.h #includex264.h #defineDATA_MAX3000000 #defineH264_MTU1024 typedef struct { x264_param_t*param; x264_t*
TAG:

H264Android.c

  1. #include <string.h> 
  2. #include <jni.h> 
  3. #include <stdio.h> 
  4. #include <stdlib.h> 
  5. #include <arpa/inet.h> 
  6. #include <x264.h> 
  7. #define DATA_MAX 3000000 
  8. #define H264_MTU 1024 
  9. typedef struct 
  10.     x264_param_t * param; 
  11.     x264_t *handle; 
  12.     x264_picture_t * picture; 
  13.     x264_nal_t  *nal; 
  14. } Encoder; 
  15. jlong Java_h264_com_H264Encoder_CompressBegin(JNIEnv* env, jobject thiz, 
  16.         jint width, jint height) { 
  17.     Encoder * en = (Encoder *) malloc(sizeof(Encoder)); 
  18.     en->param = (x264_param_t *) malloc(sizeof(x264_param_t)); 
  19.     en->picture = (x264_param_t *) malloc(sizeof(x264_picture_t)); 
  20.     x264_param_default(en->param); //set default param 
  21.     //en->param->rc.i_rc_method = X264_RC_CQP; 
  22.     en->param->i_log_level = X264_LOG_NONE; 
  23.     en->param->i_width = width; //set frame width 
  24.     en->param->i_height = height; //set frame height 
  25.     en->param->rc.i_lookahead =0; 
  26.     en->param->i_bframe=0; 
  27.     en->param->i_fps_num =5; 
  28.     en->param->i_fps_den = 1; 
  29.     if ((en->handle = x264_encoder_open(en->param)) == 0) { 
  30.         return 0; 
  31.     } 
  32.     /* Create a new pic */
  33.     x264_picture_alloc(en->picture, X264_CSP_I420, en->param->i_width, 
  34.             en->param->i_height); 
  35.     return (jlong) en; 
  36. jint Java_h264_com_H264Encoder_CompressEnd(JNIEnv* env, jobject thiz,jlong handle) 
  37.     Encoder * en = (Encoder *) handle; 
  38.     if(en->picture) 
  39.     { 
  40.         x264_picture_clean(en->picture); 
  41.         free(en->picture); 
  42.         en->picture = 0; 
  43.     } 
  44.     if(en->param) 
  45.     { 
  46.         free(en->param); 
  47.         en->param=0; 
  48.     } 
  49.     if(en->handle) 
  50.     { 
  51.         x264_encoder_close(en->handle); 
  52.     } 
  53.     free(en); 
  54.     return 0; 
  55. jint Java_h264_com_H264Encoder_CompressBuffer(JNIEnv* env, jobject thiz,jlong handle
  56. ,jint type,jbyteArray in, jint insize,jbyteArray out) 
  57.     Encoder * en = (Encoder *) handle; 
  58.     x264_picture_t pic_out; 
  59.     int i_data=0; 
  60.     int nNal=-1; 
  61.     int result=0; 
  62.     int i=0,j=0; 
  63.     int nPix=0; 
  64.     jbyte * Buf = (jbyte*)(*env)->GetByteArrayElements(env, in, 0); 
  65.     jbyte * h264Buf = (jbyte*)(*env)->GetByteArrayElements(env, out, 0); 
  66.     unsigned char * pTmpOut = h264Buf; 
  67.     int nPicSize=en->param->i_width*en->param->i_height; 
  68.     /* 
  69.     Y数据全部从在一块,UV数据使用interleave方式存储 
  70.     YYYY 
  71.     YYYY 
  72.     UVUV 
  73.      */ 
  74.     jbyte * y=en->picture->img.plane[0]; 
  75.     jbyte * v=en->picture->img.plane[1]; 
  76.     jbyte * u=en->picture->img.plane[2]; 
  77.     memcpy(en->picture->img.plane[0],Buf,nPicSize); 
  78.     for (i=0;i<nPicSize/4;i++) 
  79.     { 
  80.         *(u+i)=*(Buf+nPicSize+i*2); 
  81.         *(v+i)=*(Buf+nPicSize+i*2+1); 
  82.     } 
  83.     switch (type) 
  84.     { 
  85.     case 0: 
  86.         en->picture->i_type = X264_TYPE_P; 
  87.         break
  88.     case 1: 
  89.         en->picture->i_type = X264_TYPE_IDR; 
  90.         break
  91.     case 2: 
  92.         en->picture->i_type = X264_TYPE_I; 
  93.         break
  94.     default
  95.         en->picture->i_type = X264_TYPE_AUTO; 
  96.         break
  97.     } 
  98.     if( x264_encoder_encode( en->handle, &(en->nal), &nNal, en->picture ,&pic_out) < 0 ) 
  99.     { 
  100.         return -1; 
  101.     } 
  102.     for (i = 0; i < nNal; i++){ 
  103.           memcpy(pTmpOut, en->nal[i].p_payload, en->nal[i].i_payload); 
  104.           pTmpOut += en->nal[i].i_payload; 
  105.           result+=en->nal[i].i_payload; 
  106.     } 
  107.     return result; 

 

(zblue78)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201106/14633.html]
本文出处:blog.csdn.net/zblue78 作者:zblue78
顶一下
(15)
75%
踩一下
(5)
25%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容