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

📄 umc_h264_bs.cpp

📁 audio-video-codecs.rar语音编解码器
💻 CPP
📖 第 1 页 / 共 5 页
字号:
//
//               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.
//

#include <string.h>
#include "umc_h264_bs.h"
#include "umc_h264_video_encoder.h"
#include "umc_h264_core_enc.h"
#include "umc_h264_tables.h"

// Bit stream field sizes
namespace UMC_H264_ENCODER
{

// ---------------------------------------------------------------------------
//  CH264pBs::CH264pBs()
//      H.263+ bitstream constructor used in the decoder
// ---------------------------------------------------------------------------

template <class PixType, class CoeffsType>
CH264pBs<PixType,CoeffsType>::CH264pBs( Ipp8u* const pb, const Ipp32u maxsize, Ipp32s chroma_format_idc, Status &plr)
  : CBaseBitstream(pb, maxsize)
{
    m_pbsRBSPBase = m_pbsBase;
    plr           = UMC_OK;

    if( chroma_format_idc )
        num8x8Cshift2 = chroma_format_idc-1;
    else num8x8Cshift2 = 0;
} // CH264pBs::CH264pBs()

// ---------------------------------------------------------------------------
//  CH264pBs::CH264pBs()
//      default constructor used in the decoder
// ---------------------------------------------------------------------------
template <class PixType, class CoeffsType>
CH264pBs<PixType,CoeffsType>::CH264pBs(Status &plr)
  : CBaseBitstream()
{
    m_pbsRBSPBase = m_pbsBase;
    plr           = UMC_OK;
} // CH264pBs::CH264pBs()

// ---------------------------------------------------------------------------
//  CH264pBs::~CH264pBs()
// ---------------------------------------------------------------------------
template <class PixType, class CoeffsType>
CH264pBs<PixType,CoeffsType>::~CH264pBs()
{
}

// ---------------------------------------------------------------------------
//  CH264pBs::Reset()
//      reset bitstream; used in the encoder
// ---------------------------------------------------------------------------
template <class PixType, class CoeffsType>
void CH264pBs<PixType,CoeffsType>::Reset()
{
    CBaseBitstream::Reset();
    m_pbsRBSPBase = m_pbsBase;
}

// ---------------------------------------------------------------------------
//  CH264pBs::ResetRBSP()
//      reset bitstream to beginning of current RBSP; used in the encoder
// ---------------------------------------------------------------------------
template <class PixType, class CoeffsType>
void CH264pBs<PixType,CoeffsType>::ResetRBSP()
{
    m_pbs = m_pbsRBSPBase;
    m_bitOffset = 0;
    m_pbs[0] = 0;   // Zero the first byte, since subsequent bits written will be OR'd
                    // with this byte.  Subsequent bytes will be completely overwritten
                    // or zeroed, so no need to clear them out.

} // CH264pBs::Reset()

// ---------------------------------------------------------------------------
//  CH264pBs::EndOfNAL()
// ---------------------------------------------------------------------------
template <class PixType, class CoeffsType>
Ipp32u CH264pBs<PixType,CoeffsType>::EndOfNAL(Ipp8u* const pout,
                          Ipp8u const uIDC,    // nal_ref_idc (could be 2 bits)
                          NAL_Unit_Type const uUnitType,
                          bool& startPicture )
{
    Ipp32u size, ExtraBytes;
    Ipp8u* curPtr, *endPtr, *outPtr;

    // get current RBSP compressed size
    size = (Ipp32u)(m_pbs - m_pbsRBSPBase);
    ExtraBytes = 0;

    // Set Pointers
    endPtr = m_pbsRBSPBase + size - 1;  // Point at Last byte with data in it.
    curPtr = m_pbsRBSPBase;
    outPtr = pout;

    // start access unit => should be zero_byte
    if (startPicture &&
        (((uUnitType >= NAL_UT_SLICE ) && (uUnitType <= NAL_UT_SEI)) ||
        (uUnitType == NAL_UT_AUD) ||
        (((uUnitType >= 0x0e) && (uUnitType <= 0x12)))) ) {
        *outPtr++ = 0;
        ExtraBytes = 1;
        startPicture = false;
    }

    // for SPS and PPS NAL units zero_byte should exist
    if( uUnitType == NAL_UT_SPS || uUnitType == NAL_UT_PPS ) {
        *outPtr++ = 0;
        ExtraBytes = 1;
    }

    *outPtr++ = 0;
    *outPtr++ = 0;
    *outPtr++ = 1;
    *outPtr++ = (Ipp8u) ((uIDC << 5) | uUnitType);  //nal_unit_type
    ExtraBytes += 4;

    while (curPtr < endPtr-1) { // Copy all but the last 2 bytes
        *outPtr++ = *curPtr;

        // Check for start code emulation
        if ((*curPtr++ == 0) && (*curPtr == 0) && (!(*(curPtr+1) & 0xfc))) {
            *outPtr++ = *curPtr++;
            *outPtr++ = 0x03;   // Emulation Prevention Byte
            ExtraBytes++;
        }
    }

    if (curPtr < endPtr) {
        *outPtr++ = *curPtr++;
    }
    // copy the last byte
    *outPtr = *curPtr;

    // Update RBSP Base Pointer
    m_pbsRBSPBase = m_pbs;

    // copy encoded frame to output
    return(size+ExtraBytes);

} // CH264pBs::EndOfNAL()

template <class PixType, class CoeffsType>
Status CH264pBs<PixType,CoeffsType>::PutSeqExParms(const H264SeqParamSet& seq_parms)
{
    PutVLCCode(seq_parms.seq_parameter_set_id);
    PutVLCCode(seq_parms.aux_format_idc);
    if(seq_parms.aux_format_idc != 0) {
        PutVLCCode(seq_parms.bit_depth_aux - 8);
        PutBit(seq_parms.alpha_incr_flag);
        PutBits(seq_parms.alpha_opaque_value, seq_parms.bit_depth_aux + 1);
        PutBits(seq_parms.alpha_transparent_value, seq_parms.bit_depth_aux + 1);
    }
    PutBit(seq_parms.additional_extension_flag);
    return(UMC_OK);
}
// ---------------------------------------------------------------------------
//  CH264pBs::PutSeqParms()
// ---------------------------------------------------------------------------
template <class PixType, class CoeffsType>
Status CH264pBs<PixType,CoeffsType>::PutSeqParms(const H264SeqParamSet& seq_parms)
{
    Status ps = UMC_OK;

    // Write profile and level information

    PutBits(seq_parms.profile_idc, 8);

    PutBit(seq_parms.constraint_set0_flag);
    PutBit(seq_parms.constraint_set1_flag);
    PutBit(seq_parms.constraint_set2_flag);
    PutBit(seq_parms.constraint_set3_flag);

    // 5 reserved zero bits
    PutBits(0, 4);

    PutBits(seq_parms.level_idc, 8);

    // Write the sequence parameter set id
    PutVLCCode(seq_parms.seq_parameter_set_id);

    if(seq_parms.profile_idc == H264_HIGH_PROFILE ||
        seq_parms.profile_idc == H264_HIGH10_PROFILE ||
        seq_parms.profile_idc == H264_HIGH422_PROFILE ||
        seq_parms.profile_idc == H264_HIGH444_PROFILE)
    {
        PutVLCCode(seq_parms.chroma_format_idc);
        if(seq_parms.chroma_format_idc == 3) {
            PutBit(seq_parms.residual_colour_transform_flag);
        }
        PutVLCCode(seq_parms.bit_depth_luma - 8);
        PutVLCCode(seq_parms.bit_depth_chroma - 8);
        PutBit(seq_parms.qpprime_y_zero_transform_bypass_flag);
        PutBit(seq_parms.seq_scaling_matrix_present_flag);
        if(seq_parms.seq_scaling_matrix_present_flag) {
            Ipp32s i;
            bool UseDefaultScalingMatrix;
            for( i=0; i<8 ; i++){
                //Put scaling list present flag
                PutBit(seq_parms.seq_scaling_list_present_flag[i]);
                if( seq_parms.seq_scaling_list_present_flag[i] ){
                   if( i<6 )
                      PutScalingList( &seq_parms.seq_scaling_list_4x4[i][0], 16, UseDefaultScalingMatrix);
                   else
                      PutScalingList( &seq_parms.seq_scaling_list_8x8[i-6][0], 64, UseDefaultScalingMatrix);
                }
            }
        }
    }

    // Write log2_max_frame_num_minus4
    PutVLCCode(seq_parms.log2_max_frame_num - 4);

    // Write pic_order_cnt_type and associated data
    PutVLCCode(seq_parms.pic_order_cnt_type);

    // Write data specific to various pic order cnt types

    // pic_order_cnt_type == 1 is NOT currently supported
    if (seq_parms.pic_order_cnt_type == 0) {
        PutVLCCode(seq_parms.log2_max_pic_order_cnt_lsb - 4);
    }

    // Write num_ref_frames
    PutVLCCode(seq_parms.num_ref_frames);

    // Write required_frame_num_update_behaviour_flag
    PutBit(seq_parms.gaps_in_frame_num_value_allowed_flag);

    // Write picture MB dimensions
    PutVLCCode(seq_parms.frame_width_in_mbs - 1);
    PutVLCCode(seq_parms.frame_height_in_mbs - 1);

    // Write other misc flags
    PutBit(seq_parms.frame_mbs_only_flag);

    if (!seq_parms.frame_mbs_only_flag) {
        PutBit(seq_parms.mb_adaptive_frame_field_flag);
    }

    // Right now, the decoder only supports this flag with
    // a value of zero.
    PutBit(seq_parms.direct_8x8_inference_flag);

    PutBit(seq_parms.frame_cropping_flag);

    if (seq_parms.frame_cropping_flag)  {
        PutVLCCode(seq_parms.frame_crop_left_offset);
        PutVLCCode(seq_parms.frame_crop_right_offset);
        PutVLCCode(seq_parms.frame_crop_top_offset);
        PutVLCCode(seq_parms.frame_crop_bottom_offset);
    }

    PutBit(seq_parms.vui_parameters_present_flag);

    if (seq_parms.vui_parameters_present_flag) {

        PutBit( seq_parms.vui_parameters.aspect_ratio_info_present_flag );
        if( seq_parms.vui_parameters.aspect_ratio_info_present_flag ){
            PutBits(seq_parms.vui_parameters.aspect_ratio_idc, 8);
            if( seq_parms.vui_parameters.aspect_ratio_idc == 255 ){ // == Extended_SAR
                PutBits(seq_parms.vui_parameters.sar_width,16);
                PutBits(seq_parms.vui_parameters.sar_height,16);
            }
        }

        PutBit( seq_parms.vui_parameters.overscan_info_present_flag );
        if(seq_parms.vui_parameters.overscan_info_present_flag){
            PutBit( seq_parms.vui_parameters.overscan_appropriate_flag );
        }

        PutBit( seq_parms.vui_parameters.video_signal_type_present_flag );
        if(seq_parms.vui_parameters.video_signal_type_present_flag){
            PutBits(seq_parms.vui_parameters.video_format,3);
            PutBit(seq_parms.vui_parameters.video_full_range_flag);
            PutBit(seq_parms.vui_parameters.colour_description_present_flag);
            if(seq_parms.vui_parameters.colour_description_present_flag){
                PutBits(seq_parms.vui_parameters.colour_primaries,8);
                PutBits(seq_parms.vui_parameters.transfer_characteristics,8);
                PutBits(seq_parms.vui_parameters.matrix_coefficients,8);
            }
        }

        PutBit( seq_parms.vui_parameters.chroma_loc_info_present_flag );
        if(seq_parms.vui_parameters.chroma_loc_info_present_flag){
            PutVLCCode(seq_parms.vui_parameters.chroma_sample_loc_type_top_field);
            PutVLCCode(seq_parms.vui_parameters.chroma_sample_loc_type_bottom_field);
        }

        PutBit( seq_parms.vui_parameters.timing_info_present_flag );
        if(seq_parms.vui_parameters.timing_info_present_flag){
            PutBits(seq_parms.vui_parameters.num_units_in_tick&0x00ffffff, 24); //Due to restrictions of PutBits
            PutBits(seq_parms.vui_parameters.num_units_in_tick>>24, 8);
            PutBits(seq_parms.vui_parameters.time_scale&0x00ffffff, 24);
            PutBits(seq_parms.vui_parameters.time_scale>>24, 8);
            PutBit(seq_parms.vui_parameters.fixed_frame_rate_flag);
        }

        PutBit( seq_parms.vui_parameters.nal_hrd_parameters_present_flag );
        if(seq_parms.vui_parameters.nal_hrd_parameters_present_flag){
            VM_ASSERT(0); //Not yet supported
        }

        PutBit( seq_parms.vui_parameters.vcl_hrd_parameters_present_flag );
        if(seq_parms.vui_parameters.vcl_hrd_parameters_present_flag){

⌨️ 快捷键说明

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