📄 mpeg2_video_encoder.h
字号:
/*////////////////////////////////////////////////////////////////////////////////// INTEL CORPORATION PROPRIETARY INFORMATION// This software is supplied under the terms of a license agreement or// nondisclosure agreement with Intel Corporation and may not be copied// or disclosed except in accordance with the terms of that agreement.// Copyright(c) 2002-2005 Intel Corporation. All Rights Reserved.//*/#ifndef MPEG2_VIDEO_ENCODER_H_#define MPEG2_VIDEO_ENCODER_H_#include "mpeg2_params.h"#include "ippvc.h"typedef struct _MBInfo // macroblock information{ Ipp32s mb_type; // intra/forward/backward/interpolated Ipp32s motion_type; // frame/field/16x8/dual_prime Ipp32s dct_type; // field/frame DCT Ipp32s prediction_type; // MV_FRAME/MV_FIELD Ipp32s cbp; // coded block pattern IppiPoint MV[2][2]; // motion vectors Ipp32s mv_field_sel[2][2]; // motion vertical field select: // the first index: 0-top field, 1-bottom field; // the second index:0-forward, 1-backward Ipp64f act; // activity measure Ipp32s var; // for debugging} MBInfo;typedef struct _IppMotionVector2{ Ipp32s x; Ipp32s y; Ipp32s mctype_l; Ipp32s offset_l; Ipp32s mctype_c; Ipp32s offset_c;} IppMotionVector2;typedef struct _VLCode_8u // variable length code{ Ipp8u code; // right justified Ipp8u len;} VLCode_8u;typedef struct _VLCode_16u // VL code longer than 8 bits{ Ipp16u code; // right justified Ipp8u len;} VLCode_16u;#if defined (_WIN32_WCE) && defined (_M_IX86) && defined (__stdcall) #define _IPP_STDCALL_CDECL #undef __stdcall#endiftypedef IppStatus (__STDCALL *functype_getdiff)( const Ipp8u* pSrcCur, Ipp32s srcCurStep, const Ipp8u* pSrcRef, Ipp32s srcRefStep, Ipp16s* pDstDiff, Ipp32s dstDiffStep, Ipp16s* pDstPredictor, Ipp32s dstPredictorStep, Ipp32s mcType, Ipp32s roundControl);typedef IppStatus (__STDCALL *functype_getdiffB)( const Ipp8u* pSrcCur, Ipp32s srcCurStep, const Ipp8u* pSrcRefF, Ipp32s srcRefStepF, Ipp32s mcTypeF, const Ipp8u* pSrcRefB, Ipp32s srcRefStepB, Ipp32s mcTypeB, Ipp16s* pDstDiff, Ipp32s dstDiffStep, Ipp32s roundControl);typedef IppStatus (__STDCALL *functype_mc)( const Ipp8u* pSrcRef, Ipp32s srcStep, const Ipp16s* pSrcYData, Ipp32s srcYDataStep, Ipp8u* pDst, Ipp32s dstStep, Ipp32s mcType, Ipp32s roundControl);typedef IppStatus (__STDCALL *functype_mcB)( const Ipp8u* pSrcRefF, Ipp32s srcStepF, Ipp32s mcTypeF, const Ipp8u* pSrcRefB, Ipp32s srcStepB, Ipp32s mcTypeB, const Ipp16s* pSrcYData, Ipp32s srcYDataStep, Ipp8u* pDst, Ipp32s dstStep, Ipp32s roundControl);#if defined (_IPP_STDCALL_CDECL) #undef _IPP_STDCALL_CDECL #define __stdcall __cdecl#endif#define ME_PARAMS \ const Ipp8u* pRef, \ Ipp32s RefStep, \ const Ipp8u* pBlock, \ Ipp32s BlockStep, \ Ipp32s* pSrcMean, \ Ipp32s* pDstVar, \ Ipp32s* pDstMean, \ Ipp32s limit_left, \ Ipp32s limit_right, \ Ipp32s limit_top, \ Ipp32s limit_bottom, \ IppiPoint InitialMV0, \ IppiPoint InitialMV1, \ IppMotionVector2 *vector, \ threadSpecificData *th, \ Ipp32s i, \ Ipp32s j, \ Ipp32s *vertical_field_select, \ Ipp32s *currMADclass ippMPEG2VideoEncoder;typedef struct{ int bit_offset; Ipp8u *start_pointer; Ipp32u *current_pointer; Ipp16s *pMBlock_; Ipp16s *pDiff_; Ipp16s *pDiff1_; Ipp8u *YBlock; Ipp8u *UBlock; Ipp8u *VBlock; IppiPoint PMV[2][2]; Ipp16s var_min; int start_row; // motion estimation int me_matrix_size; Ipp8u *me_matrix_buff; Ipp8u me_matrix_id;} threadSpecificData;typedef struct{ int numTh; ippMPEG2VideoEncoder *m_lpOwner; vm_event start_event; vm_event stop_event; vm_event quit_event; vm_thread thread;} threadInfo;class ippMPEG2VideoEncoder{public: //constructor ippMPEG2VideoEncoder(); //destructor ~ippMPEG2VideoEncoder() { Close(); }; // Initialize codec with specified parameter(s) IppStatus Init(int Width, int Height, int BitRate = 6000000, ippMPEG2EncoderParams *Params = NULL); // Get codec working (initialization) parameter(s) IppStatus GetInfo(int *pWidth, int *pHeight, double *pFrameRate, int *pBitRate, ippMPEG2EncoderParams *Params = NULL); // Close all codec resources IppStatus Close(); // Get source frame size (summary size of Y, U and V frames) Ipp32s GetYUVFrameSize(); // Get pointer to internal encoder memory, where // next YUV frame can be stored before passing // this frame to encode function Ipp8u* GetNextYUVPointer(); // Encode frames (in source order) // Three possibilities: // 1. YUV != NULL, YUV_pitch != NULL: // YUV must contain three pointers: Y, U and V frames // YUV_pitch must contain three pitches (horizontal steps) of Y, U, V frames // 2. YUV != NULL, YUV_pitch == NULL: // only YUV[0] is used as a pointer to contiguous Y, U and V frames, // pitches are equal to the corresponding widths. // 3. YUV == NULL // no source frame // On first frames the function can return zero encoded_size. This is because // of buffering of B frames until the reference P frame comes. After the end of // source stream function EncodeFrame() must be called (IPDistance - 1) times // with YUV == NULL in order to encode the buffered frames. IppStatus EncodeFrame(Ipp8u **YUV, Ipp32s *YUV_pitch, Ipp8u *out_buffer, Ipp32s out_buffer_size, Ipp32s *encoded_size); // Encode reordered frames (B frames following the corresponding I/P frames). // frame_type must be supplied (I_TYPE, P_TYPE, B_TYPE). // No buffering because the frames are already reordered. // Rules for YUV and YUV_pitch are the same as in EncodeFrame() function. IppStatus EncodeFrameReordered(Ipp8u **YVU, Ipp32s *YVU_pitch, Ipp32s frame_type, Ipp8u *out, Ipp32s out_buffer_size, Ipp32s *encoded_size); void ThreadWorkingRoutine(threadInfo* th);protected: void PrepareBuffers(); void flushBuffer(); void PutSequenceHeader(); void PutSequenceExt(); void PutSequenceDisplayExt(); void PutUserData(); void PutGOPHeader(int); void PutPictureHeader(); void PutPictureCodingExt(); void PutSequenceEnd(); void PutSliceHeader(int RowNumber, int numTh); void PutMV(Ipp32s delta, Ipp32s f_code, int numTh); void PutAddrIncrement(int increment, int numTh); int FrameToTimecode(int frame); void PutNonIntraBlock(short* block, int count, int numTh); void PutIntraBlock(short* block, int* dc_dct_pred, const IppVCHuffmanSpec_32u* DC_Tbl, int count, int numTh); void PutIntraMacroBlock(int numTh, int k, Ipp8u *BlockSrc[3], Ipp8u *BlockRef[3], Ipp32s *dc_dct_pred, int PictureType); void PutMacroBlockP(int numTh, int k, IppMotionVector2 *vector, IppMotionVector2 *vector2, Ipp32s CodedBlockPattern, int PictureType, int BW); void PutMV_FRAME(int numTh, int k, IppMotionVector2 *vector, int motion_type); void PutMV_FIELD(int numTh, int k, IppMotionVector2 *vector, IppMotionVector2 *vector2, int motion_type); IppStatus PutPicture(); void EncodeMacroBlock(int numTh, int dct_type, Ipp16s *pDiff, Ipp32s *Count, Ipp32s *pCodedBlockPattern); void encodeB(int numTh); void encodeP(int numTh); void encodeI(int numTh); /////Motion estimation Ipp32s MotionEstimation_Frame(ME_PARAMS); Ipp32s MotionEstimation_Field(ME_PARAMS); Ipp32s MotionEstimation_FieldPict(ME_PARAMS); /// debug functions#ifdef MPEG2_DEBUG_CODE void DumpPSNR(); void save_bmp(vm_char *file_name, int flag);#endif /* MPEG2_DEBUG_CODE */ ///////////////////////////////////////////////////////////////////// ippMPEG2EncoderParams encodeInfo; double FrameRate; int BitRate; int numEncodedFrames; Ipp8u *tmpFrame; Ipp8u *m_lpbReference; // (Ipp8u *) pointer to allocated space for references Ipp8u *YRefFrameF;// for forward ME Ipp8u *URefFrameF; Ipp8u *VRefFrameF; Ipp8u *YRefFrameB;// for backward ME Ipp8u *URefFrameB; Ipp8u *VRefFrameB; Ipp32s YFrameHSize; Ipp32s UVFrameHSize; Ipp32s YFrameVSize; Ipp32s UVFrameVSize; Ipp32s YFrameSize; Ipp32s UVFrameSize; Ipp32s YUVFrameSize; Ipp32s srcYFrameHSize; Ipp32s srcUVFrameHSize; Ipp32s srcYFrameVSize; Ipp32s srcUVFrameVSize; Ipp32s srcYFrameSize; Ipp32s srcUVFrameSize; Ipp32s srcYUVFrameSize; int N; // number of frames in Group of Pictures int M; // distance between I/P frames int picture_coding_type; // picture coding type (I, P or B) int picture_structure; // picture structure (frame, top / bottom field) int temporal_reference; int forw_hor_f_code; int forw_vert_f_code; int back_hor_f_code; int back_vert_f_code; // motion vector ranges int curr_frame_dct; int curr_frame_pred; int curr_intra_vlc_format; int curr_scan; int vbv_delay; // vbv_delay code for picture header int rc_vbv_max; // max allowed size of the frame to be encoded in bits int rc_vbv_min; // min allowed size of the frame to avoid overflow with next frame double rc_vbv_fullness; // buffer fullnes before frame removing in bits double rc_delay; // vbv examination delay for current picture double rc_ip_delay; // extra delay for I or P pictures in bits double rc_ave_frame_bits; // bits per frame int qscale[3]; // qscale codes for 3 frame types (signed!) int prsize[3]; // bitsize of previous frame of the type int prqscale[3]; // quant scale value, used with previous frame of the type int quantiser_scale_value; // for the current frame int q_scale_type; // 0 for linear 1 for nonlinear int quantiser_scale_code; // bitcode for curren scale value double rc_weight[3]; // frame weight (length proportion) double rc_tagsize[3]; // bitsize target of the type double rc_dev; // bitrate deviation (sum of GOP's frame diffs) int InitRateControl(); // inits scales int PictureRateControl(); // selects curr scale int PostPictureRateControl(Ipp64s bits_encoded); // adaptation here int changeQuant(int quant_value); // changes all realted int mapQuant(int quant_value); // returns mapped value int onlyIFrames; // true if no B-frames Ipp64s ByteCount; // total number of encoded bytes //int num_mb_skipped; int volatile B_count; // index of B frame int m_GOP_Start; // first frame of current GOP bool m_bFirstFrame; // true if numEncodedFrames==0 int block_count; // 6 or 8 or 12 depending on chroma format Ipp16s* IntraQMatrix; Ipp16s* NonIntraQMatrix; VM_ALIGN16_DECL(Ipp32f) _InvIntraQMatrix[64]; VM_ALIGN16_DECL(Ipp32f) _InvNonIntraQMatrix[64]; Ipp32f* InvIntraQMatrix; Ipp32f* InvNonIntraQMatrix; MBInfo *pMBInfo; /* macroblock side information array */ // threads int m_numThreads; threadSpecificData *threadSpec; threadInfo *threads; // Variable Length Coding tables IppVCHuffmanSpec_32s *vlcTableB1; IppVCHuffmanSpec_32s *vlcTableB2[3]; IppVCHuffmanSpec_32s *vlcTableB3 ; IppVCHuffmanSpec_32s *vlcTableB4; IppVCHuffmanSpec_32s *vlcTableB5a; IppVCHuffmanSpec_32s *vlcTableB5b; IppVCHuffmanSpec_32s *vlcTableB5c_e_first; IppVCHuffmanSpec_32s *vlcTableB5c_e; IppVCHuffmanSpec_32s *vlcTableB15; // Input frame Ipp8u *Y_src; Ipp8u *U_src; Ipp8u *V_src; Ipp32s Y_pitch; Ipp32s U_pitch; Ipp32s V_pitch; // Output bitstream Ipp8u *out_pointer; Ipp32s out_buffer_size; Ipp32s mEncodedSize; Ipp32s thread_buffer_size; // Offsets Ipp32s block_offset_frm[12]; Ipp32s block_offset_fld[12]; Ipp32s block_offset_frm_ref[12]; Ipp32s block_offset_fld_ref[12]; Ipp32s frm_dct_step[12]; Ipp32s fld_dct_step[12]; Ipp32s frm_diff_off[12]; Ipp32s fld_diff_off[12]; // GetDiff & MotionCompensation functions functype_getdiff func_getdiff_frame_c, func_getdiff_field_c; functype_getdiffB func_getdiffB_frame_c, func_getdiffB_field_c; functype_mc func_mc_frame_c, func_mc_field_c; functype_mcB func_mcB_frame_c, func_mcB_field_c; // Internal var's Ipp32s nPitch[3]; Ipp32s BlkWidth_c; Ipp32s BlkStride_c; Ipp32s BlkHeight_c; Ipp32s chroma_fld_flag; Ipp32s curr_field; Ipp32s YOffsetToOtherField; Ipp32s UVOffsetToOtherField; const IppVCHuffmanSpec_32u *DC_Tbl[3]; // YUV frames buffer Ipp32s buff_size; // in number of frames Ipp8u *frames_buff; Ipp32s buff_ind; Ipp32s num_btype; Ipp32s num_new; Ipp32s curr_gop; double cpu_freq; double motion_estimation_time; // const tables static const Ipp32s color_index[12]; static const VLCode_8u CBP_VLC_Tbl[64]; static const VLCode_8u AddrIncrementTbl[35]; static const VLCode_8u mbtypetab[3][32]; static const VLCode_8u MV_VLC_Tbl[33]; static const IppVCHuffmanSpec_32u Y_DC_Tbl[12]; static const IppVCHuffmanSpec_32u Cr_DC_Tbl[12]; static const int ResetTbl[4]; static const int dct_coeff_next_RL[]; static const int Table15[]; static const IppiPoint MV_ZERO;};#ifdef MPEG2_DEBUG_CODEint mpeg2_write_bmp(char *file_name, int width, int height, int pitch, Ipp8u *r, Ipp8u *g, Ipp8u *b);#endif#endif /* MPEG2_VIDEO_ENCODER_H_ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -