利用x264lib编码h264流的源码

    
利用x264lib编码h264流的源码


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

x264enc.h:

[cpp] 
#pragma once 
#include "inttypes.h" 
extern "C"  

#include "x264.h" 
}; 
 
class x264enc  

public: 
    x264enc(void); 
    virtual ~x264enc(void); 
public: 
    bool InitX264Encoder(unsigned short usWidth,unsigned short usHeight,int nKeyFrameInterval,int nFrameRate,int nQuality); 
    bool X264Encode(unsigned char* pInFrame,const int& nInLen,unsigned char* pOutFrame,int& nOutLen,bool& bKeyFrame); 
    void ReleaseConnection(); 
private: 
    x264_t *h; 
 
    unsigned short  m_usWidth; 
    unsigned short  m_usHeight; 
}; 
x264enc.cpp:
[cpp] 
#include "StdAfx.h" 
#include "x264enc.h" 
 
x264enc::x264enc(void) 
: h(NULL) 
, m_usWidth(0) 
, m_usHeight(0) 


 
x264enc::~x264enc(void) 


 
bool x264enc::InitX264Encoder(unsigned short usWidth,unsigned short usHeight,int nKeyFrameInterval,int nFrameRate,int nQuality) 

    x264_param_t param; 
    x264_param_default(¶m); 
 
//  param.i_width = usWidth; 
//  param.i_height = usHeight; 
//  param.b_deblocking_filter = 1; 
//  param.i_deblocking_filter_alphac0 = 1;//add 
//  param.i_deblocking_filter_beta = 1;//add 
//  param.b_cabac = 1; 
//   
//  param.i_frame_reference = 1;//add 
//  param.i_bframe              = 0;//add 
//  param.i_keyint_max = 6;//add 
//  param.i_keyint_min = 5;//add 
//  
//  param.analyse.inter = X264_ANALYSE_I4x4|X264_ANALYSE_I8x8|X264_ANALYSE_PSUB16x16|X264_ANALYSE_BSUB16x16; 
//  param.analyse.intra = X264_ANALYSE_I4x4|X264_ANALYSE_I8x8; 
//  if((param.analyse.inter | param.analyse.intra) & X264_ANALYSE_I8x8) 
//  { 
//      param.analyse.b_transform_8x8=1; 
//  } 
//   
//  param.rc.i_bitrate=/*900*/320; 
// //   param.rc.i_vbv_max_bitrate = (int)(320 * 1.04);//add 
// //   param.rc.i_vbv_buffer_size = 320;//add 
//  
//     param.rc.i_qp_min=/*2*/6; 
//     param.rc.i_qp_max=/*31*/33; 
//     param.rc.f_qcompress=/*0.5f*/0.6f; 
//  param.rc.i_qp_constant=/*0*/17; 
//     param.rc.i_rc_method = X264_RC_CRF; 
//     param.rc.f_rf_constant = /*8.5f*/17; 
//     param.rc.i_aq_mode = X264_AQ_GLOBAL; 
//     param.rc.f_aq_strength = 0.3f; 
//  param.analyse.i_me_method = /*X264_ME_HEX*/X264_ME_UMH; 
//  param.analyse.i_subpel_refine = 7;//add 
//  param.analyse.b_mixed_references = 1;//add 
//  param.analyse.i_trellis = 1;//add 
//  param.analyse.b_weighted_bipred = 0;//add 
//  param.analyse.i_me_range = 6;//add 
//  param.analyse.i_direct_mv_pred  = X264_DIRECT_PRED_AUTO;//add 
//  
//  param.i_log_level = X264_LOG_NONE; 
 
    param.i_width = usWidth; 
    param.i_height = usHeight; 
    param.b_deblocking_filter = 1; 
    param.b_cabac = 1; 
    param.analyse.inter = X264_ANALYSE_I4x4|X264_ANALYSE_I8x8|X264_ANALYSE_PSUB16x16|X264_ANALYSE_BSUB16x16; 
    param.analyse.intra = X264_ANALYSE_I4x4|X264_ANALYSE_I8x8; 
    if((param.analyse.inter | param.analyse.intra) & X264_ANALYSE_I8x8) 
    { 
        param.analyse.b_transform_8x8=1; 
    } 
    param.analyse.i_subpel_refine = 5; 
    param.analyse.i_me_method = X264_ME_HEX; 
 
    param.rc.i_bitrate=900; 
    param.rc.i_qp_min=2; 
    param.rc.i_qp_max=31; 
    param.rc.f_qcompress=0.5f; 
    param.rc.i_qp_constant=0; 
    param.rc.i_rc_method = X264_RC_CRF; 
    param.rc.f_rf_constant = 8.5f; 
    param.rc.i_aq_mode = X264_AQ_GLOBAL/*X264_AQ_AUTOVARIANCE*/; 
    param.rc.f_aq_strength = 0.3f; 
    param.i_log_level = X264_LOG_NONE; 
 
    h = x264_encoder_open(¶m); 
    if(h == NULL) 
    { 
        return false; 
    } 
 
    m_usWidth=usWidth; 
    m_usHeight=usHeight; 
    return true; 

 
static int encode_nals(unsigned char *buf, x264_nal_t *nals, int nnal) 

    unsigned char *p = buf; 
    int i; 
 
    for(i = 0; i < nnal; i++) 
    { 
        int nsize = 0; 
        int s = x264_nal_encode(p, &nsize, 1, nals + i); 
        if(s < 0) 
            return -1; 
        p += s; 
    } 
 
    return p - buf; 

 
bool x264enc::X264Encode(unsigned char* pInFrame,const int& nInLen,unsigned char* pOutFrame,int& nOutLen,bool& bKeyFrame) 

    x264_nal_t *nal; 
    x264_picture_t pic_out; 
    x264_picture_t pic; 
    int nnal = 0; 
    if(pInFrame) 
    { 
        pic.img.i_csp = X264_CSP_I420; 
        pic.img.i_plane = 3; 
        pic.img.plane[0] = pInFrame; 
        pic.img.plane[1] = pInFrame + m_usWidth*m_usHeight; 
        pic.img.plane[2] = pic.img.plane[1] + (m_usWidth*m_usHeight / 4); 
        pic.img.i_stride[0] = m_usWidth; 
        pic.img.i_stride[1] = m_usWidth / 2; 
        pic.img.i_stride[2] = m_usWidth / 2; 
        pic.i_type = X264_TYPE_AUTO; 
 
        if(x264_encoder_encode(h, &nal, &nnal, &pic, &pic_out) < 0) 
        { 
            return false; 
        } 
    } 
    else 
    { 
        if(x264_encoder_encode(h, &nal, &nnal, NULL, &pic_out) < 0) 
        { 
            return false; 
        } 
    } 
 
    if(nnal <= 0) 
        return false; 
 
    nOutLen = encode_nals(pOutFrame, nal, nnal); 
 
    if(nOutLen < 0) 
    { 
        return false; 
    } 
    if(pic_out.i_type == X264_TYPE_IDR) 
    { 
        bKeyFrame = true; 
    } 
    else 
    { 
        bKeyFrame = false; 
    } 
    return true; 

 
void x264enc::ReleaseConnection() 

    if(h) 
    { 
        x264_encoder_close(h); 
        h = NULL; 
    } 
    delete this; 

main.cpp:

[cpp] 
#include "stdafx.h" 
#include <stdlib.h> 
#include "x264enc.h" 
 
int main(int argc, char* argv[]) 

    if (argc != 5) 
    { 
        printf("please input: Enc_Demo.exe filename1[input] Width Height filename2[output]\n"); 
    } 
 
     
    //params set 
    unsigned short usWidth = atoi(argv[2]); 
    unsigned short usHeight = atoi(argv[3]); 
 
    //create X264 instance 
    x264enc* pX264enc = new x264enc; 
 
    if(!pX264enc || !pX264enc->InitX264Encoder(usWidth, usHeight, 100, 5, 26)) 
    { 
        pX264enc->ReleaseConnection(); 
        delete pX264enc; 
        return -1; 
    } 
 
    unsigned char *p_In_Frame = new unsigned char[usWidth * usHeight * 3/2]; 
    unsigned char *p_Out_Frame = new unsigned char[usWidth * usHeight * 3/2]; 
    FILE* ifp = fopen(argv[1],"rb"); 
    FILE* ofp = fopen(argv[4],"wb"); 
 
    bool b_continue = true; 
    int nReadUnit = usWidth * usHeight * 3/2; 
    while (b_continue || !feof(ifp)) 
    { 
        int n_OutFrame_Size  = 0; 
        bool bKeyFrame = false; 
        int nCount = fread(p_In_Frame, 1, nReadUnit, ifp); 
        if(nCount != nReadUnit) 
        { 
            b_continue = false; 
            break; 
        } 
         
        unsigned char *pSrc = p_In_Frame; 
        if(pX264enc->X264Encode(pSrc, nCount, p_Out_Frame, n_OutFrame_Size,bKeyFrame)) 
        { 
            fwrite(p_Out_Frame, n_OutFrame_Size, 1, ofp); 
        } 
    } 
 
    do 
    { 
        int n_OutFrame_Size = 0; 
        bool b_KeyFrame = false; 
        if(pX264enc->X264Encode(NULL, 0, p_Out_Frame, n_OutFrame_Size, b_KeyFrame)) 
        { 
            fwrite(p_Out_Frame, n_OutFrame_Size, 1, ofp); 
        } 
        else 
        { 
            break; 
        } 
    }while(1); 
 
    //realse 
    delete []p_In_Frame; 
    delete []p_Out_Frame; 
    pX264enc->ReleaseConnection(); 
    fclose(ifp); 
    fclose(ofp); 
 
    return 0; 


你可能感兴趣的:(利用x264lib编码h264流的源码)