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

📄 umc_h264_gen_enc.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.
//
#if defined _OPENMP
#include "omp.h"
#endif // _OPENMP
#if defined _WIN32
#include <windows.h>
#undef true
#undef false
#endif

#include <math.h>
#include <string.h>
#include "vm_strings.h"
#include "vm_thread.h"
#include "umc_h264_video_encoder.h"
#include "umc_h264_core_enc.h"
#include "umc_h264_npf.h"
#include "umc_video_data.h"
#include "umc_video_processing.h"
#include "umc_h264_to_ipp.h"

#include "umc_h264_tables.h"

namespace UMC_H264_ENCODER
{
//
// Constructor for the EncoderH264 class.
//
template <class PixType, class CoeffsType>
H264EncoderThreadPrivateSlice<PixType, CoeffsType>::H264EncoderThreadPrivateSlice()
    :
    m_pbitstream(NULL),
    fakeBitstream(NULL),
    m_iLastXmittedQP(0),
    m_Intra_MB_Counter(0),
    m_MB_Counter(0),
    m_is_cur_mb_bottom_field(false),
    m_is_cur_mb_field(false),
    m_disable_deblocking_filter_idc(0),
    m_slice_alpha_c0_offset(0),
    m_slice_beta_offset(0),
    m_slice_qp_delta(0),
    m_first_mb_in_slice(0),
    m_slice_type(INTRASLICE),
    m_pAllocatedMBEncodeBuffer(NULL),
    m_InitialOffset(0),
    m_pMEMap(0),
    num_ref_idx_active_override_flag(0),
    num_ref_idx_l0_active(0),
    num_ref_idx_l1_active(0),
    m_cabac_init_idc(0),
    m_use_transform_for_intra_decision(false)
{
}

template <class PixType, class CoeffsType>
Status H264EncoderThreadPrivateSlice<PixType,CoeffsType>::Init(H264EncoderParams &info)
{
    Ipp32s status;
    Ipp32s me_map_size = (2*info.me_search_x + 1)* (2*info.me_search_y + 1) + 16; // + 16 for alignment.
    Ipp32s chunk_size = 16*16*3+100;

    m_pAllocatedMBEncodeBuffer = (Ipp8u *) H264_Allocate(256*sizeof(PixType) // pred for direct
                                                        +256*sizeof(PixType) // temp working for direct
                                                        +256*sizeof(PixType) // pred for BiPred
                                                        +256*sizeof(PixType) // temp buf for BiPred
                                                        +256*sizeof(PixType) // temp buf for ChromaPred
                                                        +256*sizeof(PixType) // MC
                                                        +256*sizeof(PixType) // ME
                                                        +6*512*sizeof(PixType) // Intra 4x4 prediction & reconstruct
                                                        +6*256*sizeof(CoeffsType) //Intra 4x4 transform result
                                                        +64*sizeof(Ipp16s)     // Diff
                                                        +64*sizeof(CoeffsType) // TransformResults
                                                        +64*sizeof(CoeffsType) // QuantResult
                                                        +64*sizeof(CoeffsType) // DequantResult
                                                        +16*sizeof(CoeffsType) // luma dc
                                                        +256*sizeof(Ipp16s)    // MassDiff
                                                        +512 + me_map_size
                                                        + 3*chunk_size  //Bitstreams
                                                        , false /*zero-init*/);

    if (!m_pAllocatedMBEncodeBuffer) return(UMC::UMC_ERR_ALLOC);

    // 16-byte align buffer start
    m_pPred4DirectB = (PixType*)align_pointer<Ipp8u*>(m_pAllocatedMBEncodeBuffer, 16);
    m_pTempBuff4DirectB = m_pPred4DirectB + 256;
    m_pPred4BiPred = m_pTempBuff4DirectB + 256;
    m_pTempBuff4BiPred = m_pPred4BiPred + 256;
    m_pTempChromaPred = m_pTempBuff4BiPred + 256;

    m_cur_mb.mb4x4.prediction = m_pTempChromaPred + 256; // 256 for pred_intra and 256 for reconstructed blocks
    m_cur_mb.mb4x4.reconstruct = m_cur_mb.mb4x4.prediction + 256; // 256 for pred_intra and 256 for reconstructed blocks
    m_cur_mb.mb4x4.transform = (CoeffsType*)(m_cur_mb.mb4x4.reconstruct + 256); // 256 for pred_intra and 256 for reconstructed blocks

    m_cur_mb.mb8x8.prediction = (PixType*)(m_cur_mb.mb4x4.transform + 256); // 256 for pred_intra and 256 for reconstructed blocks
    m_cur_mb.mb8x8.reconstruct = m_cur_mb.mb8x8.prediction + 256; // 256 for pred_intra and 256 for reconstructed blocks
    m_cur_mb.mb8x8.transform = (CoeffsType*)(m_cur_mb.mb8x8.reconstruct + 256); // 256 for pred_intra and 256 for reconstructed blocks

    m_cur_mb.mb16x16.prediction = (PixType*)(m_cur_mb.mb8x8.transform + 256); // 256 for pred_intra and 256 for reconstructed blocks
    m_cur_mb.mb16x16.reconstruct = m_cur_mb.mb16x16.prediction + 256; // 256 for pred_intra and 256 for reconstructed blocks
    m_cur_mb.mb16x16.transform = (CoeffsType*)(m_cur_mb.mb16x16.reconstruct + 256); // 256 for pred_intra and 256 for reconstructed blocks

    m_cur_mb.mbInter.prediction = (PixType*)(m_cur_mb.mb16x16.transform + 256); // 256 for pred_intra and 256 for reconstructed blocks
    m_cur_mb.mbInter.reconstruct = m_cur_mb.mbInter.prediction + 256; // 256 for pred_intra and 256 for reconstructed blocks
    m_cur_mb.mbInter.transform = (CoeffsType*)(m_cur_mb.mbInter.reconstruct + 256); // 256 for pred_intra and 256 for reconstructed blocks

    m_cur_mb.mbChromaIntra.prediction = (PixType*)(m_cur_mb.mbInter.transform + 256); // 256 for pred_intra and 256 for reconstructed blocks
    m_cur_mb.mbChromaIntra.reconstruct = m_cur_mb.mbChromaIntra.prediction + 256; // 256 for pred_intra and 256 for reconstructed blocks
    m_cur_mb.mbChromaIntra.transform = (CoeffsType*)(m_cur_mb.mbChromaIntra.reconstruct + 256); // 256 for pred_intra and 256 for reconstructed blocks

    m_cur_mb.mbChromaInter.prediction = (PixType*)(m_cur_mb.mbChromaIntra.transform + 256); // 256 for pred_intra and 256 for reconstructed blocks
    m_cur_mb.mbChromaInter.reconstruct = m_cur_mb.mbChromaInter.prediction + 256; // 256 for pred_intra and 256 for reconstructed blocks
    m_cur_mb.mbChromaInter.transform = (CoeffsType*)(m_cur_mb.mbChromaInter.reconstruct + 256); // 256 for pred_intra and 256 for reconstructed blocks

    m_pMEMap = (Ipp8u*)(m_cur_mb.mbChromaInter.transform + 256);
    m_pMBEncodeBuffer = (PixType*)align_pointer<Ipp8u*>(m_pMEMap + me_map_size, 16);

    //Init bitstreams
#ifdef H264_RD_OPT
    m_cur_mb.mb[0].bitstream = new CH264pBs<PixType,CoeffsType>((Ipp8u*)(m_pMBEncodeBuffer+512), chunk_size, info.chroma_format_idc, status);
    m_cur_mb.mb[1].bitstream = new CH264pBs<PixType,CoeffsType>((Ipp8u*)(m_pMBEncodeBuffer+512+chunk_size), chunk_size, info.chroma_format_idc, status);
    m_cur_mb.mb[2].bitstream = new CH264pBs<PixType,CoeffsType>((Ipp8u*)(m_pMBEncodeBuffer+512+2*chunk_size), chunk_size, info.chroma_format_idc, status);
    m_cur_mb.mb[3].bitstream = new CH264pBs<PixType,CoeffsType>((Ipp8u*)(m_pMBEncodeBuffer+512+2*chunk_size), chunk_size, info.chroma_format_idc, status);
//    m_cur_mb.mbInter.bitstream = new CH264pBs<PixType,CoeffsType>((Ipp8u*)(m_pMBEncodeBuffer+512+2*chunk_size), chunk_size, info.chroma_format_idc, status);
#endif

    if(fakeBitstream == NULL ) delete fakeBitstream;
    fakeBitstream = new CH264pBsFake<PixType,CoeffsType>(0, 0,info.chroma_format_idc, status);

    return(UMC_OK);
}

template <class PixType, class CoeffsType>
H264EncoderThreadPrivateSlice<PixType, CoeffsType>::~H264EncoderThreadPrivateSlice()
{
    if(m_pAllocatedMBEncodeBuffer != NULL) {
        H264_Free(m_pAllocatedMBEncodeBuffer);
        m_pAllocatedMBEncodeBuffer = NULL;
    }
    m_pPred4DirectB = NULL;
    m_pPred4BiPred = NULL;
    m_pTempBuff4DirectB = NULL;
    m_pTempBuff4BiPred = NULL;
    m_pMBEncodeBuffer = NULL;
    m_cur_mb.mb4x4.prediction = NULL;
    m_cur_mb.mb4x4.reconstruct = NULL;
    m_cur_mb.mb4x4.transform = NULL;
    m_cur_mb.mb8x8.prediction = NULL;
    m_cur_mb.mb8x8.reconstruct = NULL;
    m_cur_mb.mb8x8.transform = NULL;

#ifdef H264_RD_OPT
    for( Ipp32s i=0; i<4; i++ ){
        if( m_cur_mb.mb[i].bitstream != NULL ){
            delete  m_cur_mb.mb[i].bitstream;
            m_cur_mb.mb[i].bitstream = NULL;
        }
    }
#endif
    if( fakeBitstream != NULL ){
        delete fakeBitstream;
    }
}

template <class PixType, class CoeffsType>
H264CoreEncoder<PixType, CoeffsType>::H264CoreEncoder(Status &status)
    :
       // m_implementation_id gets refined in GetBackdoorEncoderOptions
    m_pBitStream(NULL),
    m_bBuffersAllocated(false),
    memAlloc(NULL),
    profile_frequency(1),
    m_iProfileIndex(0),
    m_bScratchBufferAllocated(false),
    m_noisepf(NULL),
    m_is_mb_data_initialized(false),
    m_pAllocEncoderInst(NULL),
    m_pbitstreams(NULL),
    eFrameType(new EnumPicCodType[1]),
    eFrameSeq(new H264EncoderFrame<PixType>*[1]),
    m_bs1(NULL),
    m_dpb(NULL),
    m_cpb(NULL),
    m_uIntraFrameInterval(0),
    m_uIDRFrameInterval(0),
    m_PicOrderCnt(0),
    m_PicOrderCnt_Accu(0),
    m_pParsedDataNew(0),
    m_pParsedFrameData(0),
    m_pReconstructFrame(NULL),
    m_nAllocatedLimitedSliceInfo(0),
    m_pLimitedSliceInfo(0),
    m_l1_cnt_to_start_B(0),
    m_pMBOffsets (NULL),
    m_EmptyThreshold (NULL),
    m_DirectBSkipMEThres (NULL),
    m_PSkipMEThres (NULL),
    m_BestOf5EarlyExitThres (NULL),
    use_implicit_weighted_bipred(false),
    m_is_cur_pic_afrm(false),
    m_Slices(NULL),
    m_total_bits_encoded(0)
{
    Ipp32s i;
    m_PaddedSize.width = m_PaddedSize.height = 0;
    status = UMC_OK;

    m_bQPMapIsReady = false;

    // Initialize the BRCState local variables based on the default
    // settings in m_info.

    // If these assertions fail, then uTargetFrmSize needs to be set to
    // something other than 0.
    // Initialize the sequence parameter set structure.

    m_SeqParamSet.profile_idc = H264_MAIN_PROFILE;
    m_SeqParamSet.level_idc = 0;
    m_SeqParamSet.constraint_set0_flag = 0;
    m_SeqParamSet.constraint_set1_flag = 0;
    m_SeqParamSet.constraint_set2_flag = 0;
    m_SeqParamSet.chroma_format_idc = 1;
    m_SeqParamSet.seq_parameter_set_id = 0;
    m_SeqParamSet.log2_max_frame_num = 0;
    m_SeqParamSet.pic_order_cnt_type = 0;
    m_SeqParamSet.delta_pic_order_always_zero_flag = 0;
    m_SeqParamSet.frame_mbs_only_flag = 0;
    m_SeqParamSet.gaps_in_frame_num_value_allowed_flag = 0;
    m_SeqParamSet.mb_adaptive_frame_field_flag = 0;
    m_SeqParamSet.direct_8x8_inference_flag = 0;
    m_SeqParamSet.vui_parameters_present_flag = 0;
    m_SeqParamSet.log2_max_pic_order_cnt_lsb = 0;
    m_SeqParamSet.offset_for_non_ref_pic = 0;
    m_SeqParamSet.offset_for_top_to_bottom_field = 0;
    m_SeqParamSet.num_ref_frames_in_pic_order_cnt_cycle = 0;
    m_SeqParamSet.poffset_for_ref_frame = NULL;
    m_SeqParamSet.num_ref_frames = 0;
    m_SeqParamSet.frame_width_in_mbs = 0;
    m_SeqParamSet.frame_height_in_mbs = 0;
    m_SeqParamSet.frame_cropping_flag = 0;
    m_SeqParamSet.frame_crop_left_offset = 0;
    m_SeqParamSet.frame_crop_right_offset = 0;
    m_SeqParamSet.frame_crop_top_offset = 0;
    m_SeqParamSet.frame_crop_bottom_offset = 0;
    m_SeqParamSet.bit_depth_luma = 8;
    m_SeqParamSet.bit_depth_chroma = 8;
    m_SeqParamSet.bit_depth_aux = 8;
    m_SeqParamSet.alpha_incr_flag = 0;
    m_SeqParamSet.alpha_opaque_value = 8;
    m_SeqParamSet.alpha_transparent_value = 0;
    m_SeqParamSet.aux_format_idc = 0;
    m_SeqParamSet.seq_scaling_matrix_present_flag = false;
    for( i=0; i<8; i++) m_SeqParamSet.seq_scaling_list_present_flag[i]=false;
    m_SeqParamSet.pack_sequence_extension = false;
    m_SeqParamSet.qpprime_y_zero_transform_bypass_flag = 0;
    m_SeqParamSet.residual_colour_transform_flag = false;
    m_SeqParamSet.additional_extension_flag = 0;

    // Initialize the picture parameter set structure.

    m_PicParamSet.pic_parameter_set_id = 0;
    m_PicParamSet.seq_parameter_set_id = 0;
    m_PicParamSet.entropy_coding_mode = 0;
    m_PicParamSet.pic_order_present_flag = 0;
    m_PicParamSet.weighted_pred_flag = 0;
    m_PicParamSet.weighted_bipred_idc = 0;
    m_PicParamSet.pic_init_qp = 0;
    m_PicParamSet.pic_init_qs = 0;
    m_PicParamSet.chroma_qp_index_offset = 0;
    m_PicParamSet.deblocking_filter_variables_present_flag = 0;
    m_PicParamSet.constrained_intra_pred_flag = 0;
    m_PicParamSet.redundant_pic_cnt_present_flag = 0;
    m_PicParamSet.num_slice_groups = 0;
    m_PicParamSet.SliceGroupInfo.slice_group_map_type = 0;
    m_PicParamSet.SliceGroupInfo.t3.pic_size_in_map_units = 0;
    m_PicParamSet.SliceGroupInfo.t3.pSliceGroupIDMap = NULL;
    m_PicParamSet.num_ref_idx_l0_active = 0;
    m_PicParamSet.num_ref_idx_l1_active = 0;

    m_PicParamSet.second_chroma_qp_index_offset = m_PicParamSet.chroma_qp_index_offset;
    m_PicParamSet.pic_scaling_matrix_present_flag = 0;
    m_PicParamSet.transform_8x8_mode_flag = false;

    // Initialize the slice header structure.
    m_SliceHeader.pic_parameter_set_id = 0;
    m_SliceHeader.field_pic_flag = 0;

⌨️ 快捷键说明

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