利用x264lib编码h264流的源码

本文提供了一个x264编码器的使用示例,包括如何初始化编码器、进行编码操作以及释放资源等步骤。通过具体的C++代码实现了从文件读取原始帧数据并将其编码为H.264格式。

有问题或者想法,大家交流下哈,没有人评论,都没有继续下去的动力了。。。直接给出代码吧:

x264enc.h:

  1. #pragma once  
  2. #include "inttypes.h"  
  3. extern "C"   
  4. {  
  5. #include "x264.h"  
  6. };  
  7.   
  8. class x264enc   
  9. {  
  10. public:  
  11.     x264enc(void);  
  12.     virtual ~x264enc(void);  
  13. public:  
  14.     bool InitX264Encoder(unsigned short usWidth,unsigned short usHeight,int nKeyFrameInterval,int nFrameRate,int nQuality);  
  15.     bool X264Encode(unsigned char* pInFrame,const int& nInLen,unsigned char* pOutFrame,int& nOutLen,bool& bKeyFrame);  
  16.     void ReleaseConnection();  
  17. private:  
  18.     x264_t *h;  
  19.   
  20.     unsigned short  m_usWidth;  
  21.     unsigned short  m_usHeight;  
  22. };  
x264enc.cpp:

  1. #include "StdAfx.h"  
  2. #include "x264enc.h"  
  3.   
  4. x264enc::x264enc(void)  
  5. : h(NULL)  
  6. , m_usWidth(0)  
  7. , m_usHeight(0)  
  8. {  
  9. }  
  10.   
  11. x264enc::~x264enc(void)  
  12. {  
  13. }  
  14.   
  15. bool x264enc::InitX264Encoder(unsigned short usWidth,unsigned short usHeight,int nKeyFrameInterval,int nFrameRate,int nQuality)  
  16. {  
  17.     x264_param_t param;  
  18.     x264_param_default(¶m);  
  19.   
  20. //  param.i_width = usWidth;  
  21. //  param.i_height = usHeight;  
  22. //  param.b_deblocking_filter = 1;  
  23. //  param.i_deblocking_filter_alphac0 = 1;//add  
  24. //  param.i_deblocking_filter_beta = 1;//add  
  25. //  param.b_cabac = 1;  
  26. //    
  27. //  param.i_frame_reference = 1;//add  
  28. //  param.i_bframe              = 0;//add  
  29. //  param.i_keyint_max = 6;//add  
  30. //  param.i_keyint_min = 5;//add  
  31. //   
  32. //  param.analyse.inter = X264_ANALYSE_I4x4|X264_ANALYSE_I8x8|X264_ANALYSE_PSUB16x16|X264_ANALYSE_BSUB16x16;  
  33. //  param.analyse.intra = X264_ANALYSE_I4x4|X264_ANALYSE_I8x8;  
  34. //  if((param.analyse.inter | param.analyse.intra) & X264_ANALYSE_I8x8)  
  35. //  {  
  36. //      param.analyse.b_transform_8x8=1;  
  37. //  }  
  38. //    
  39. //  param.rc.i_bitrate=/*900*/320;  
  40. // //   param.rc.i_vbv_max_bitrate = (int)(320 * 1.04);//add  
  41. // //   param.rc.i_vbv_buffer_size = 320;//add  
  42. //   
  43. //     param.rc.i_qp_min=/*2*/6;  
  44. //     param.rc.i_qp_max=/*31*/33;  
  45. //     param.rc.f_qcompress=/*0.5f*/0.6f;  
  46. //  param.rc.i_qp_constant=/*0*/17;  
  47. //     param.rc.i_rc_method = X264_RC_CRF;  
  48. //     param.rc.f_rf_constant = /*8.5f*/17;  
  49. //     param.rc.i_aq_mode = X264_AQ_GLOBAL;  
  50. //     param.rc.f_aq_strength = 0.3f;  
  51. //  param.analyse.i_me_method = /*X264_ME_HEX*/X264_ME_UMH;  
  52. //  param.analyse.i_subpel_refine = 7;//add  
  53. //  param.analyse.b_mixed_references = 1;//add  
  54. //  param.analyse.i_trellis = 1;//add  
  55. //  param.analyse.b_weighted_bipred = 0;//add  
  56. //  param.analyse.i_me_range = 6;//add  
  57. //  param.analyse.i_direct_mv_pred  = X264_DIRECT_PRED_AUTO;//add  
  58. //   
  59. //  param.i_log_level = X264_LOG_NONE;  
  60.   
  61.     param.i_width = usWidth;  
  62.     param.i_height = usHeight;  
  63.     param.b_deblocking_filter = 1;  
  64.     param.b_cabac = 1;  
  65.     param.analyse.inter = X264_ANALYSE_I4x4|X264_ANALYSE_I8x8|X264_ANALYSE_PSUB16x16|X264_ANALYSE_BSUB16x16;  
  66.     param.analyse.intra = X264_ANALYSE_I4x4|X264_ANALYSE_I8x8;  
  67.     if((param.analyse.inter | param.analyse.intra) & X264_ANALYSE_I8x8)  
  68.     {  
  69.         param.analyse.b_transform_8x8=1;  
  70.     }  
  71.     param.analyse.i_subpel_refine = 5;  
  72.     param.analyse.i_me_method = X264_ME_HEX;  
  73.   
  74.     param.rc.i_bitrate=900;  
  75.     param.rc.i_qp_min=2;  
  76.     param.rc.i_qp_max=31;  
  77.     param.rc.f_qcompress=0.5f;  
  78.     param.rc.i_qp_constant=0;  
  79.     param.rc.i_rc_method = X264_RC_CRF;  
  80.     param.rc.f_rf_constant = 8.5f;  
  81.     param.rc.i_aq_mode = X264_AQ_GLOBAL/*X264_AQ_AUTOVARIANCE*/;  
  82.     param.rc.f_aq_strength = 0.3f;  
  83.     param.i_log_level = X264_LOG_NONE;  
  84.   
  85.     h = x264_encoder_open(¶m);  
  86.     if(h == NULL)  
  87.     {  
  88.         return false;  
  89.     }  
  90.   
  91.     m_usWidth=usWidth;  
  92.     m_usHeight=usHeight;  
  93.     return true;  
  94. }  
  95.   
  96. static int encode_nals(unsigned char *buf, x264_nal_t *nals, int nnal)  
  97. {  
  98.     unsigned char *p = buf;  
  99.     int i;  
  100.   
  101.     for(i = 0; i < nnal; i++)  
  102.     {  
  103.         int nsize = 0;  
  104.         int s = x264_nal_encode(p, &nsize, 1, nals + i);  
  105.         if(s < 0)  
  106.             return -1;  
  107.         p += s;  
  108.     }  
  109.   
  110.     return p - buf;  
  111. }  
  112.   
  113. bool x264enc::X264Encode(unsigned char* pInFrame,const int& nInLen,unsigned char* pOutFrame,int& nOutLen,bool& bKeyFrame)  
  114. {  
  115.     x264_nal_t *nal;  
  116.     x264_picture_t pic_out;  
  117.     x264_picture_t pic;  
  118.     int nnal = 0;  
  119.     if(pInFrame)  
  120.     {  
  121.         pic.img.i_csp = X264_CSP_I420;  
  122.         pic.img.i_plane = 3;  
  123.         pic.img.plane[0] = pInFrame;  
  124.         pic.img.plane[1] = pInFrame + m_usWidth*m_usHeight;  
  125.         pic.img.plane[2] = pic.img.plane[1] + (m_usWidth*m_usHeight / 4);  
  126.         pic.img.i_stride[0] = m_usWidth;  
  127.         pic.img.i_stride[1] = m_usWidth / 2;  
  128.         pic.img.i_stride[2] = m_usWidth / 2;  
  129.         pic.i_type = X264_TYPE_AUTO;  
  130.   
  131.         if(x264_encoder_encode(h, &nal, &nnal, &pic, &pic_out) < 0)  
  132.         {  
  133.             return false;  
  134.         }  
  135.     }  
  136.     else  
  137.     {  
  138.         if(x264_encoder_encode(h, &nal, &nnal, NULL, &pic_out) < 0)  
  139.         {  
  140.             return false;  
  141.         }  
  142.     }  
  143.   
  144.     if(nnal <= 0)  
  145.         return false;  
  146.   
  147.     nOutLen = encode_nals(pOutFrame, nal, nnal);  
  148.   
  149.     if(nOutLen < 0)  
  150.     {  
  151.         return false;  
  152.     }  
  153.     if(pic_out.i_type == X264_TYPE_IDR)  
  154.     {  
  155.         bKeyFrame = true;  
  156.     }  
  157.     else  
  158.     {  
  159.         bKeyFrame = false;  
  160.     }  
  161.     return true;  
  162. }  
  163.   
  164. void x264enc::ReleaseConnection()  
  165. {  
  166.     if(h)  
  167.     {  
  168.         x264_encoder_close(h);  
  169.         h = NULL;  
  170.     }  
  171.     delete this;  
  172. }  

main.cpp:

  1. #include "stdafx.h"  
  2. #include <stdlib.h>  
  3. #include "x264enc.h"  
  4.   
  5. int main(int argc, char* argv[])  
  6. {  
  7.     if (argc != 5)  
  8.     {  
  9.         printf("please input: Enc_Demo.exe filename1[input] Width Height filename2[output]\n");  
  10.     }  
  11.   
  12.       
  13.     //params set  
  14.     unsigned short usWidth = atoi(argv[2]);  
  15.     unsigned short usHeight = atoi(argv[3]);  
  16.   
  17.     //create X264 instance  
  18.     x264enc* pX264enc = new x264enc;  
  19.   
  20.     if(!pX264enc || !pX264enc->InitX264Encoder(usWidth, usHeight, 100, 5, 26))  
  21.     {  
  22.         pX264enc->ReleaseConnection();  
  23.         delete pX264enc;  
  24.         return -1;  
  25.     }  
  26.   
  27.     unsigned char *p_In_Frame = new unsigned char[usWidth * usHeight * 3/2];  
  28.     unsigned char *p_Out_Frame = new unsigned char[usWidth * usHeight * 3/2];  
  29.     FILE* ifp = fopen(argv[1],"rb");  
  30.     FILE* ofp = fopen(argv[4],"wb");  
  31.   
  32.     bool b_continue = true;  
  33.     int nReadUnit = usWidth * usHeight * 3/2;  
  34.     while (b_continue || !feof(ifp))  
  35.     {  
  36.         int n_OutFrame_Size  = 0;  
  37.         bool bKeyFrame = false;  
  38.         int nCount = fread(p_In_Frame, 1, nReadUnit, ifp);  
  39.         if(nCount != nReadUnit)  
  40.         {  
  41.             b_continue = false;  
  42.             break;  
  43.         }  
  44.           
  45.         unsigned char *pSrc = p_In_Frame;  
  46.         if(pX264enc->X264Encode(pSrc, nCount, p_Out_Frame, n_OutFrame_Size,bKeyFrame))  
  47.         {  
  48.             fwrite(p_Out_Frame, n_OutFrame_Size, 1, ofp);  
  49.         }  
  50.     }  
  51.   
  52.     do  
  53.     {  
  54.         int n_OutFrame_Size = 0;  
  55.         bool b_KeyFrame = false;  
  56.         if(pX264enc->X264Encode(NULL, 0, p_Out_Frame, n_OutFrame_Size, b_KeyFrame))  
  57.         {  
  58.             fwrite(p_Out_Frame, n_OutFrame_Size, 1, ofp);  
  59.         }  
  60.         else  
  61.         {  
  62.             break;  
  63.         }  
  64.     }while(1);  
  65.   
  66.     //realse  
  67.     delete []p_In_Frame;  
  68.     delete []p_Out_Frame;  
  69.     pX264enc->ReleaseConnection();  
  70.     fclose(ifp);  
  71.     fclose(ofp);  
  72.   
  73.     return 0;  
  74. }  
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值