Use xforwarlib to encode the source code of the h264 stream

Source: Internet
Author: User

If you have any questions or ideas, let's talk about them. No one has commented on them and there is no motivation to continue... Directly provide the Code:

Xforwarenc. h:

[Cpp]
# Pragma once
# Include "inttypes. h"
Extern "C"
{
# Include "x264.h"
};
 
Class x264enc
{
Public:
Xforwarenc (void );
Virtual ~ Xforwarenc (void );
Public:
Bool initxincluencoder (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:
X1__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)
{
}
 
Xforwarenc ::~ Xforwarenc (void)
{
}
 
Bool xforwardedenc: initxforwardedencoder (unsigned short usWidth, unsigned short usHeight, int nKeyFrameInterval, int nFrameRate, int nQuality)
{
X1__param_t param;
X1__param_default (1_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. analyze. inter = X264_ANALYSE_I4x4 | x__analyse_i8x8 | x__analyse_psub16x16 | X264_ANALYSE_BSUB16x16;
// Param. analyze. intra = xpai_analyse_i4x4 | xpai_analyse_i8x8;
// If (param. analyze. inter | param. analyze. intra) & X264_ANALYSE_I8x8)
//{
// Param. analyze. 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 = x1__rc_crf;
// Param. rc. f_rf_constant =/* 8.5f */17;
// Param. rc. I _aq_mode = x1__aq_global;
// Param. rc. f_aq_strength = 0.3f;
// Param. analyze. I _me_method =/* x1__me_hex */x1__me_umh;
// Param. analyze. I _subpel_refine = 7; // add
// Param. analyze. B _mixed_references = 1; // add
// Param. analyze. I _trellis = 1; // add
// Param. analyze. B _weighted_bipred = 0; // add
// Param. analyze. I _me_range = 6; // add
// Param. analyze. I _direct_mv_pred = x1__direct_pred_auto; // add
//
// Param. I _log_level = x1__log_none;
 
Param. I _width = usWidth;
Param. I _height = usHeight;
Param. B _deblocking_filter = 1;
Param. B _cabac = 1;
Param. analyze. inter = X264_ANALYSE_I4x4 | x__analyse_i8x8 | X264_ANALYSE_PSUB16x16 | X264_ANALYSE_BSUB16x16;
Param. analyze. intra = X264_ANALYSE_I4x4 | X264_ANALYSE_I8x8;
If (param. analyze. inter | param. analyze. intra) & X264_ANALYSE_I8x8)
{
Param. analyze. B _transform_8x8 = 1;
}
Param. analyze. I _subpel_refine = 5;
Param. analyze. I _me_method = xpai_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 = x1__rc_crf;
Param. rc. f_rf_constant = 8.5f;
Param. rc. I _aq_mode = X264_AQ_GLOBAL/* x1__aq_autovariance */;
Param. rc. f_aq_strength = 0.3f;
Param. I _log_level = x1__log_none;
 
H = x1__encoder_open (1_m );
If (h = NULL)
{
Return false;
}
 
M_usWidth = usWidth;
M_usHeight = usHeight;
Return true;
}
 
Static int encode_nals (unsigned char * buf, x1__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 xforwarenc: xforwarencode (unsigned char * pInFrame, const int & nInLen, unsigned char * pOutFrame, int & nOutLen, bool & bKeyFrame)
{
X1__nal_t * nal;
X264_picture_t pic_out;
X264_picture_t pic;
Int nnal = 0;
If (pInFrame)
{
Pic. img. I _csp = x1__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 (x1__encoder_encode (h, & nal, & nnal, & pic, & pic_out) <0)
{
Return false;
}
}
Else
{
If (x1__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
Xforwarenc * px1_enc = new xforwarenc;
 
If (! PX264enc |! Px1_enc-> initx1_encoder (usWidth, usHeight, 100, 5, 26 ))
{
PX264enc-> ReleaseConnection ();
Delete px1_enc;
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 x 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 (px1_enc-> xforwarencode (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 (px1_enc-> xforwarencode (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;
}


Author: wchm_seu

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.