⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 umc_base_bitstream.h

📁 audio-video-codecs.rar语音编解码器
💻 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) 2004 - 2007 Intel Corporation. All Rights Reserved.
//

#ifndef UMC_BASE_BITSTREAM_H__
#define UMC_BASE_BITSTREAM_H__

#include "umc_h264_pub.h"

// ---------------------------------------------------------------------------
//  CBaseBitstream - bitstream base class
// ---------------------------------------------------------------------------
namespace UMC_H264_ENCODER
{

// This macro is used to convert a Ipp32s VLC code into an
// Ipp32u VLC code, ready to pipe into PutVLCCode below.
// This saves having another function for that purpose and
// should be faster.
#define SIGNED_VLC_CODE(code) (2*ABS(code) - (code > 0))

typedef struct CABAC_CONTEXT
{
    Ipp32u pStateIdx;                       // probability state index
    bool valMPS;                            // bool value of most probable symbol

} CABAC_CONTEXT;

const Ipp32s MB_TRANSFORM_SIZE_8X8_FLAG = 399; // ctxIdxOffset for transform_size_8x8_flag

enum // Syntax element type
{
    MB_SKIP_FLAG_P_SP               = 0,
    MB_SKIP_FLAG_B                  = 1,
    MB_FIELD_DECODING_FLAG          = 2,
    MB_TYPE_SI                      = 3,
    MB_TYPE_I                       = 4,
    MB_TYPE_P_SP                    = 5,
    MB_TYPE_B                       = 6,
    CODED_BLOCK_PATTERN_LUMA        = 7,
    CODED_BLOCK_PATTERN_CHROMA      = 8,
    MB_QP_DELTA                     = 9,
    PREV_INTRA4X4_PRED_MODE_FLAG    = 10,
    REM_INTRA4X4_PRED_MODE          = 11,
    INTRA_CHROMA_PRED_MODE          = 12,
    REF_IDX_L0                      = 13,
    REF_IDX_L1                      = 14,
    MVD_L0_0                        = 15,
    MVD_L1_0                        = 16,
    MVD_L0_1                        = 17,
    MVD_L1_1                        = 18,
    SUB_MB_TYPE_P_SP                = 19,
    SUB_MB_TYPE_B                   = 20,

    MAIN_SYNTAX_ELEMENT_NUMBER
};

// See table 9-30 of H.264 standard
enum // Syntax element type
{
    CODED_BLOCK_FLAG                = 0,
    SIGNIFICANT_COEFF_FLAG          = 1,
    LAST_SIGNIFICANT_COEFF_FLAG     = 2,
    COEFF_ABS_LEVEL_MINUS1          = 3,

    SYNTAX_ELEMENT_NUMBER
};

// See table 9-32 of H.264 standard
enum // Context block category
{
    BLOCK_LUMA_DC_LEVELS            = 0,
    BLOCK_LUMA_AC_LEVELS            = 1,
    BLOCK_LUMA_LEVELS               = 2,
    BLOCK_CHROMA_DC_LEVELS          = 3,
    BLOCK_CHROMA_AC_LEVELS          = 4,
    BLOCK_LUMA_64_LEVELS            = 5,

    BLOCK_CATEGORY_NUMBER
};
// See table 9-11 of H.264 standard
const Ipp32s ctxIdxOffset[MAIN_SYNTAX_ELEMENT_NUMBER] =
{
    11,
    24,
    70,
    0,
    3,
    14,
    27,
    73,
    77,
    60,
    68,
    69,
    64,
    54,
    54,
    40,
    40,
    47,
    47,
    21,
    36,
};

// See table 9-24 of H.264 standard
const Ipp32s ctxIdxOffsetFrameCoded[SYNTAX_ELEMENT_NUMBER] =
{
    85,
    105,
    166,
    227,

};

// See table 9-24 of H.264 standard
const Ipp32s ctxIdxOffsetFieldCoded[SYNTAX_ELEMENT_NUMBER] =
{
    85,
    277,
    338,
    227,
};

// See table 9-24 of H.264 standard
const Ipp32s ctxIdxOffsetFrameCoded_BlockCat_5[SYNTAX_ELEMENT_NUMBER] =
{
    0xffffffff, // na
    402,
    417,
    426
};

// See table 9-24 of H.264 standard
const Ipp32s ctxIdxOffsetFieldCoded_BlockCat_5[SYNTAX_ELEMENT_NUMBER] =
{
    0xffffffff, // na
    436,
    451,
    426
};

// See table 9-30 of H.264 standard
const Ipp32s ctxIdxBlockCatOffset[SYNTAX_ELEMENT_NUMBER][BLOCK_CATEGORY_NUMBER] =
{
    {0,  4,  8, 12, 16, -1},
    {0, 15, 29, 44, 47,  0},
    {0, 15, 29, 44, 47,  0},
    {0, 10, 20, 30, 39,  0}
};

// See table 9-34 of H.264 standard
extern const Ipp8s Table_9_34[3][63];

class H264PureBitstream{
    protected:
        Ipp8u* m_pbs;  // m_pbs points to the current position of the buffer.
        Ipp8u* m_pbsBase; // m_pbsBase points to the first byte of the buffer.
        Ipp32u m_bitOffset; // Indicates the bit position (0 to 7) in the byte pointed by m_pbs.
        Ipp32u m_maxBsSize; // Maximum buffer size in bytes.};

    public:
        Ipp32u  GetBsSize();         // Returns the size of the bitstream data in bytes based on the current position of the buffer pointer, m_pbs.
        Ipp32u  GetBsOffset();         // Returns the bit position of the buffer pointer relative to the beginning of the buffer.
        Ipp8u*  GetBsBase(); // Returns the base pointer to the beginning of the bitstream.
        Ipp32u  GetMaxBsSize(); // Returns the maximum bitstream size.
        bool    CheckBsLimit(); // Checks if read/write passed the maximum buffer size.
        void    Reset(); // Resets pointer to the beginning and clears the bitstream buffer.
        void    SetState(Ipp8u* const pbs, const Ipp32u bitOffset); // Assigns new position to the buffer pointer.
        void    GetState(Ipp8u** pbs,Ipp32u* bitOffset);         // Obtains current position of the buffer pointer.
        void    UpdateState(const Ipp32u nbits); // Advances buffer pointer with given number of bits.
        void    ClearBs(); // Clears the bitstream buffer.
};

class CBaseBitstream : public H264PureBitstream
{
    protected:
        CABAC_CONTEXT context_array_copy[460];                       // (CABAC_CONTEXT []) array of cabac context(s)
        Ipp32u m_lcodIRange_copy;                                    // arithmetic encoding engine variable
        Ipp32u m_lcodIOffset_copy;                                   // arithmetic encoding engine variable
        Ipp32u m_nRegister_copy;
        Ipp32u m_nReadyBits_copy;
        Ipp32u m_nOutstandingBits_copy;
        Ipp32u m_nSymCnt_copy;

    public:
        CABAC_CONTEXT context_array[460];                       // (CABAC_CONTEXT []) array of cabac context(s)
        Ipp32u m_lcodIRange;                                    // arithmetic encoding engine variable
        Ipp32u m_lcodIOffset;                                   // arithmetic encoding engine variable
        Ipp32u m_nRegister;
        Ipp32u m_nReadyBits;
        Ipp32u m_nOutstandingBits;
        Ipp32u m_nSymCnt;

        CBaseBitstream();
        virtual ~CBaseBitstream();
        // Constructs a new object.  Sets base pointer to point to the
        // bitstream buffer with given buffer size.
        CBaseBitstream(Ipp8u* const pbs, const Ipp32u maxsize);

        void InitializeContextVariablesIntra_CABAC(Ipp32s SliceQPy); // Initialize CABAC context(s) in intra slices
        void InitializeContextVariablesInter_CABAC(Ipp32s SliceQPy, Ipp32s cabac_init_idc); // Initialize CABAC context(s) in inter slices

        void SaveContext_CABAC();
        void RestoreContext_CABAC();
        void CopyContext_CABAC( CBaseBitstream& bitsream );
        void ResetBitStream_CABAC(void);

        // Appends bits into the bitstream buffer.
        virtual void WriteBit_CABAC(bool code);
        virtual void WriteOutstandingZeroBit_CABAC();
        virtual void WriteOutstandingOneBit_CABAC();
        virtual void WriteOutstandingBit_CABAC(bool code);
        virtual void TerminateEncode_CABAC(void);

        // Encode single bin from stream
        void EncodeSingleBin_CABAC(Ipp32s ctxIdx,bool code);
        void EncodeBypass_CABAC(bool code);
        void EncodeFinalSingleBin_CABAC(bool code);

        //unary binarization
        void EncodeUnaryRepresentedSymbol_CABAC(Ipp32s ctxBase, Ipp32s ctxIdx, Ipp32s code,Ipp32s suppremum=0x7fffffff);
        //Exp Golomb binarization
        void EncodeExGRepresentedSymbol_CABAC(Ipp32s code,Ipp32s log2ex);
        void EncodeExGRepresentedLevels_CABAC(Ipp32s ctxIdx, Ipp32s code);
        void EncodeExGRepresentedMVS_CABAC(Ipp32s ctxIdx, Ipp32s code,Ipp32s supp);

        // Appends bits into the bitstream buffer.
        virtual void PutBit(Ipp32u code);
        virtual void PutBits(Ipp32u code, Ipp32u length);
        virtual void PutVLCBits(const Ipp32u val, const Ipp32u  len); // Writes one general VLC code to the bitstream
        virtual Ipp32u PutVLCCode(const Ipp32u val);// Writes one general VLC code to the bitstream without knowing the code length... Returns the length of the code written.

        void WriteTrailingBits(); // Write RBSP Trailing Bits to Byte Align
        void ByteAlignWithZeros();  // Add zero bits to byte-align the buffer.
        void ByteAlignWithOnes(); // Add one bits to byte-align the buffer.
};

//Inline functions
inline void H264PureBitstream::Reset()
{
    m_pbs       = m_pbsBase;
    m_bitOffset = 0;
    ClearBs();
}

inline Ipp32u H264PureBitstream::GetBsSize()
{
//    Ipp32u size;
//    size = Ipp32u(m_pbs - m_pbsBase) + 1;
//    return(!m_bitOffset ? (size - 1) : size);
    return (GetBsOffset()+7)>>3;
}

inline Ipp32u H264PureBitstream::GetBsOffset()
{
    return(Ipp32u(m_pbs - m_pbsBase) * 8 + m_bitOffset);
}

inline Ipp8u* H264PureBitstream::GetBsBase()
{
    return(m_pbsBase);
}

inline Ipp32u H264PureBitstream::GetMaxBsSize()
{
    return(m_maxBsSize);
}

inline bool H264PureBitstream::CheckBsLimit()
{
    Ipp32u size;

    size = GetBsSize();
    if (size > m_maxBsSize) return(false);
    return(true);
}

inline void H264PureBitstream::SetState(Ipp8u* const pbs, const Ipp32u bitOffset)
{
    m_pbs       = pbs;
    m_bitOffset = bitOffset;

}

inline void H264PureBitstream::GetState(Ipp8u** pbs, Ipp32u* bitOffset)
{
    *pbs       = m_pbs;
    *bitOffset = m_bitOffset;
}

inline void H264PureBitstream::UpdateState(const Ipp32u nbits)
{
    m_pbs      += (nbits + m_bitOffset) >> 3;
    m_bitOffset = (nbits + m_bitOffset) & 0x7;
}

inline void H264PureBitstream::ClearBs()
{
    memset(m_pbsBase, 0, m_maxBsSize);
}

} //namespace UMC_H264_ENCODER


#endif // UMC_BASE_BITSTREAM_H__

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -