[313] | 1 | /* The copyright in this software is being made available under the BSD |
---|
| 2 | * License, included below. This software may be subject to other third party |
---|
| 3 | * and contributor rights, including patent rights, and no such rights are |
---|
| 4 | * granted under this license. |
---|
| 5 | * |
---|
| 6 | * Copyright (c) 2010-2013, ITU/ISO/IEC |
---|
| 7 | * All rights reserved. |
---|
| 8 | * |
---|
| 9 | * Redistribution and use in source and binary forms, with or without |
---|
| 10 | * modification, are permitted provided that the following conditions are met: |
---|
| 11 | * |
---|
| 12 | * * Redistributions of source code must retain the above copyright notice, |
---|
| 13 | * this list of conditions and the following disclaimer. |
---|
| 14 | * * Redistributions in binary form must reproduce the above copyright notice, |
---|
| 15 | * this list of conditions and the following disclaimer in the documentation |
---|
| 16 | * and/or other materials provided with the distribution. |
---|
| 17 | * * Neither the name of the ITU/ISO/IEC nor the names of its contributors may |
---|
| 18 | * be used to endorse or promote products derived from this software without |
---|
| 19 | * specific prior written permission. |
---|
| 20 | * |
---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
| 22 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS |
---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
| 28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
| 29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
| 30 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
---|
| 31 | * THE POSSIBILITY OF SUCH DAMAGE. |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | /** \file TEncSbac.cpp |
---|
| 35 | \brief SBAC encoder class |
---|
| 36 | */ |
---|
| 37 | |
---|
| 38 | #include "TEncTop.h" |
---|
| 39 | #include "TEncSbac.h" |
---|
| 40 | |
---|
| 41 | #include <map> |
---|
| 42 | #include <algorithm> |
---|
| 43 | |
---|
| 44 | //! \ingroup TLibEncoder |
---|
| 45 | //! \{ |
---|
| 46 | |
---|
| 47 | // ==================================================================================================================== |
---|
| 48 | // Constructor / destructor / create / destroy |
---|
| 49 | // ==================================================================================================================== |
---|
| 50 | |
---|
| 51 | TEncSbac::TEncSbac() |
---|
| 52 | // new structure here |
---|
| 53 | : m_pcBitIf ( NULL ) |
---|
| 54 | , m_pcSlice ( NULL ) |
---|
| 55 | , m_pcBinIf ( NULL ) |
---|
| 56 | , m_uiCoeffCost ( 0 ) |
---|
| 57 | , m_numContextModels ( 0 ) |
---|
| 58 | , m_cCUSplitFlagSCModel ( 1, 1, NUM_SPLIT_FLAG_CTX , m_contextModels + m_numContextModels, m_numContextModels ) |
---|
| 59 | , m_cCUSkipFlagSCModel ( 1, 1, NUM_SKIP_FLAG_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
| 60 | , m_cCUMergeFlagExtSCModel ( 1, 1, NUM_MERGE_FLAG_EXT_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
| 61 | , m_cCUMergeIdxExtSCModel ( 1, 1, NUM_MERGE_IDX_EXT_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
| 62 | , m_cCUPartSizeSCModel ( 1, 1, NUM_PART_SIZE_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
| 63 | , m_cCUPredModeSCModel ( 1, 1, NUM_PRED_MODE_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
| 64 | , m_cCUIntraPredSCModel ( 1, 1, NUM_ADI_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
| 65 | , m_cCUChromaPredSCModel ( 1, 1, NUM_CHROMA_PRED_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
| 66 | , m_cCUDeltaQpSCModel ( 1, 1, NUM_DELTA_QP_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
| 67 | , m_cCUInterDirSCModel ( 1, 1, NUM_INTER_DIR_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
| 68 | , m_cCURefPicSCModel ( 1, 1, NUM_REF_NO_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
| 69 | , m_cCUMvdSCModel ( 1, 1, NUM_MV_RES_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
| 70 | , m_cCUQtCbfSCModel ( 1, 2, NUM_QT_CBF_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
| 71 | , m_cCUTransSubdivFlagSCModel ( 1, 1, NUM_TRANS_SUBDIV_FLAG_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
| 72 | , m_cCUQtRootCbfSCModel ( 1, 1, NUM_QT_ROOT_CBF_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
| 73 | , m_cCUSigCoeffGroupSCModel ( 1, 2, NUM_SIG_CG_FLAG_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
| 74 | , m_cCUSigSCModel ( 1, 1, NUM_SIG_FLAG_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
| 75 | , m_cCuCtxLastX ( 1, 2, NUM_CTX_LAST_FLAG_XY , m_contextModels + m_numContextModels, m_numContextModels) |
---|
| 76 | , m_cCuCtxLastY ( 1, 2, NUM_CTX_LAST_FLAG_XY , m_contextModels + m_numContextModels, m_numContextModels) |
---|
| 77 | , m_cCUOneSCModel ( 1, 1, NUM_ONE_FLAG_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
| 78 | , m_cCUAbsSCModel ( 1, 1, NUM_ABS_FLAG_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
| 79 | , m_cMVPIdxSCModel ( 1, 1, NUM_MVP_IDX_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
| 80 | , m_cCUAMPSCModel ( 1, 1, NUM_CU_AMP_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
| 81 | , m_cSaoMergeSCModel ( 1, 1, NUM_SAO_MERGE_FLAG_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
| 82 | , m_cSaoTypeIdxSCModel ( 1, 1, NUM_SAO_TYPE_IDX_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
| 83 | , m_cTransformSkipSCModel ( 1, 2, NUM_TRANSFORMSKIP_FLAG_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
| 84 | , m_CUTransquantBypassFlagSCModel( 1, 1, NUM_CU_TRANSQUANT_BYPASS_FLAG_CTX, m_contextModels + m_numContextModels, m_numContextModels) |
---|
| 85 | { |
---|
| 86 | assert( m_numContextModels <= MAX_NUM_CTX_MOD ); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | TEncSbac::~TEncSbac() |
---|
| 90 | { |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | // ==================================================================================================================== |
---|
| 94 | // Public member functions |
---|
| 95 | // ==================================================================================================================== |
---|
| 96 | |
---|
| 97 | Void TEncSbac::resetEntropy () |
---|
| 98 | { |
---|
| 99 | Int iQp = m_pcSlice->getSliceQp(); |
---|
| 100 | SliceType eSliceType = m_pcSlice->getSliceType(); |
---|
| 101 | |
---|
| 102 | Int encCABACTableIdx = m_pcSlice->getPPS()->getEncCABACTableIdx(); |
---|
| 103 | if (!m_pcSlice->isIntra() && (encCABACTableIdx==B_SLICE || encCABACTableIdx==P_SLICE) && m_pcSlice->getPPS()->getCabacInitPresentFlag()) |
---|
| 104 | { |
---|
| 105 | eSliceType = (SliceType) encCABACTableIdx; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | m_cCUSplitFlagSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_SPLIT_FLAG ); |
---|
| 109 | |
---|
| 110 | m_cCUSkipFlagSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_SKIP_FLAG ); |
---|
| 111 | m_cCUMergeFlagExtSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_MERGE_FLAG_EXT); |
---|
| 112 | m_cCUMergeIdxExtSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_MERGE_IDX_EXT); |
---|
| 113 | m_cCUPartSizeSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_PART_SIZE ); |
---|
| 114 | m_cCUAMPSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_CU_AMP_POS ); |
---|
| 115 | m_cCUPredModeSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_PRED_MODE ); |
---|
| 116 | m_cCUIntraPredSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_INTRA_PRED_MODE ); |
---|
| 117 | m_cCUChromaPredSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_CHROMA_PRED_MODE ); |
---|
| 118 | m_cCUInterDirSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_INTER_DIR ); |
---|
| 119 | m_cCUMvdSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_MVD ); |
---|
| 120 | m_cCURefPicSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_REF_PIC ); |
---|
| 121 | m_cCUDeltaQpSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_DQP ); |
---|
| 122 | m_cCUQtCbfSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_QT_CBF ); |
---|
| 123 | m_cCUQtRootCbfSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_QT_ROOT_CBF ); |
---|
| 124 | m_cCUSigCoeffGroupSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_SIG_CG_FLAG ); |
---|
| 125 | m_cCUSigSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_SIG_FLAG ); |
---|
| 126 | m_cCuCtxLastX.initBuffer ( eSliceType, iQp, (UChar*)INIT_LAST ); |
---|
| 127 | m_cCuCtxLastY.initBuffer ( eSliceType, iQp, (UChar*)INIT_LAST ); |
---|
| 128 | m_cCUOneSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_ONE_FLAG ); |
---|
| 129 | m_cCUAbsSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_ABS_FLAG ); |
---|
| 130 | m_cMVPIdxSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_MVP_IDX ); |
---|
| 131 | m_cCUTransSubdivFlagSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_TRANS_SUBDIV_FLAG ); |
---|
| 132 | m_cSaoMergeSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_SAO_MERGE_FLAG ); |
---|
| 133 | m_cSaoTypeIdxSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_SAO_TYPE_IDX ); |
---|
| 134 | m_cTransformSkipSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_TRANSFORMSKIP_FLAG ); |
---|
| 135 | m_CUTransquantBypassFlagSCModel.initBuffer( eSliceType, iQp, (UChar*)INIT_CU_TRANSQUANT_BYPASS_FLAG ); |
---|
| 136 | // new structure |
---|
| 137 | m_uiLastQp = iQp; |
---|
| 138 | |
---|
| 139 | m_pcBinIf->start(); |
---|
| 140 | |
---|
| 141 | return; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | /** The function does the following: |
---|
| 145 | * If current slice type is P/B then it determines the distance of initialisation type 1 and 2 from the current CABAC states and |
---|
| 146 | * stores the index of the closest table. This index is used for the next P/B slice when cabac_init_present_flag is true. |
---|
| 147 | */ |
---|
| 148 | Void TEncSbac::determineCabacInitIdx() |
---|
| 149 | { |
---|
| 150 | Int qp = m_pcSlice->getSliceQp(); |
---|
| 151 | |
---|
| 152 | if (!m_pcSlice->isIntra()) |
---|
| 153 | { |
---|
| 154 | SliceType aSliceTypeChoices[] = {B_SLICE, P_SLICE}; |
---|
| 155 | |
---|
| 156 | UInt bestCost = MAX_UINT; |
---|
| 157 | SliceType bestSliceType = aSliceTypeChoices[0]; |
---|
| 158 | for (UInt idx=0; idx<2; idx++) |
---|
| 159 | { |
---|
| 160 | UInt curCost = 0; |
---|
| 161 | SliceType curSliceType = aSliceTypeChoices[idx]; |
---|
| 162 | |
---|
| 163 | curCost = m_cCUSplitFlagSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_SPLIT_FLAG ); |
---|
| 164 | curCost += m_cCUSkipFlagSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_SKIP_FLAG ); |
---|
| 165 | curCost += m_cCUMergeFlagExtSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_MERGE_FLAG_EXT); |
---|
| 166 | curCost += m_cCUMergeIdxExtSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_MERGE_IDX_EXT); |
---|
| 167 | curCost += m_cCUPartSizeSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_PART_SIZE ); |
---|
| 168 | curCost += m_cCUAMPSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_CU_AMP_POS ); |
---|
| 169 | curCost += m_cCUPredModeSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_PRED_MODE ); |
---|
| 170 | curCost += m_cCUIntraPredSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_INTRA_PRED_MODE ); |
---|
| 171 | curCost += m_cCUChromaPredSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_CHROMA_PRED_MODE ); |
---|
| 172 | curCost += m_cCUInterDirSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_INTER_DIR ); |
---|
| 173 | curCost += m_cCUMvdSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_MVD ); |
---|
| 174 | curCost += m_cCURefPicSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_REF_PIC ); |
---|
| 175 | curCost += m_cCUDeltaQpSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_DQP ); |
---|
| 176 | curCost += m_cCUQtCbfSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_QT_CBF ); |
---|
| 177 | curCost += m_cCUQtRootCbfSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_QT_ROOT_CBF ); |
---|
| 178 | curCost += m_cCUSigCoeffGroupSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_SIG_CG_FLAG ); |
---|
| 179 | curCost += m_cCUSigSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_SIG_FLAG ); |
---|
| 180 | curCost += m_cCuCtxLastX.calcCost ( curSliceType, qp, (UChar*)INIT_LAST ); |
---|
| 181 | curCost += m_cCuCtxLastY.calcCost ( curSliceType, qp, (UChar*)INIT_LAST ); |
---|
| 182 | curCost += m_cCUOneSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_ONE_FLAG ); |
---|
| 183 | curCost += m_cCUAbsSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_ABS_FLAG ); |
---|
| 184 | curCost += m_cMVPIdxSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_MVP_IDX ); |
---|
| 185 | curCost += m_cCUTransSubdivFlagSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_TRANS_SUBDIV_FLAG ); |
---|
| 186 | curCost += m_cSaoMergeSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_SAO_MERGE_FLAG ); |
---|
| 187 | curCost += m_cSaoTypeIdxSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_SAO_TYPE_IDX ); |
---|
| 188 | curCost += m_cTransformSkipSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_TRANSFORMSKIP_FLAG ); |
---|
| 189 | curCost += m_CUTransquantBypassFlagSCModel.calcCost( curSliceType, qp, (UChar*)INIT_CU_TRANSQUANT_BYPASS_FLAG ); |
---|
| 190 | if (curCost < bestCost) |
---|
| 191 | { |
---|
| 192 | bestSliceType = curSliceType; |
---|
| 193 | bestCost = curCost; |
---|
| 194 | } |
---|
| 195 | } |
---|
| 196 | m_pcSlice->getPPS()->setEncCABACTableIdx( bestSliceType ); |
---|
| 197 | } |
---|
| 198 | else |
---|
| 199 | { |
---|
| 200 | m_pcSlice->getPPS()->setEncCABACTableIdx( I_SLICE ); |
---|
| 201 | } |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | |
---|
| 205 | /** The function does the followng: Write out terminate bit. Flush CABAC. Intialize CABAC states. Start CABAC. |
---|
| 206 | */ |
---|
| 207 | Void TEncSbac::updateContextTables( SliceType eSliceType, Int iQp, Bool bExecuteFinish ) |
---|
| 208 | { |
---|
| 209 | m_pcBinIf->encodeBinTrm(1); |
---|
| 210 | if (bExecuteFinish) m_pcBinIf->finish(); |
---|
| 211 | m_cCUSplitFlagSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_SPLIT_FLAG ); |
---|
| 212 | |
---|
| 213 | m_cCUSkipFlagSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_SKIP_FLAG ); |
---|
| 214 | m_cCUMergeFlagExtSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_MERGE_FLAG_EXT); |
---|
| 215 | m_cCUMergeIdxExtSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_MERGE_IDX_EXT); |
---|
| 216 | m_cCUPartSizeSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_PART_SIZE ); |
---|
| 217 | m_cCUAMPSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_CU_AMP_POS ); |
---|
| 218 | m_cCUPredModeSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_PRED_MODE ); |
---|
| 219 | m_cCUIntraPredSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_INTRA_PRED_MODE ); |
---|
| 220 | m_cCUChromaPredSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_CHROMA_PRED_MODE ); |
---|
| 221 | m_cCUInterDirSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_INTER_DIR ); |
---|
| 222 | m_cCUMvdSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_MVD ); |
---|
| 223 | m_cCURefPicSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_REF_PIC ); |
---|
| 224 | m_cCUDeltaQpSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_DQP ); |
---|
| 225 | m_cCUQtCbfSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_QT_CBF ); |
---|
| 226 | m_cCUQtRootCbfSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_QT_ROOT_CBF ); |
---|
| 227 | m_cCUSigCoeffGroupSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_SIG_CG_FLAG ); |
---|
| 228 | m_cCUSigSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_SIG_FLAG ); |
---|
| 229 | m_cCuCtxLastX.initBuffer ( eSliceType, iQp, (UChar*)INIT_LAST ); |
---|
| 230 | m_cCuCtxLastY.initBuffer ( eSliceType, iQp, (UChar*)INIT_LAST ); |
---|
| 231 | m_cCUOneSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_ONE_FLAG ); |
---|
| 232 | m_cCUAbsSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_ABS_FLAG ); |
---|
| 233 | m_cMVPIdxSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_MVP_IDX ); |
---|
| 234 | m_cCUTransSubdivFlagSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_TRANS_SUBDIV_FLAG ); |
---|
| 235 | m_cSaoMergeSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_SAO_MERGE_FLAG ); |
---|
| 236 | m_cSaoTypeIdxSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_SAO_TYPE_IDX ); |
---|
| 237 | m_cTransformSkipSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_TRANSFORMSKIP_FLAG ); |
---|
| 238 | m_CUTransquantBypassFlagSCModel.initBuffer( eSliceType, iQp, (UChar*)INIT_CU_TRANSQUANT_BYPASS_FLAG ); |
---|
| 239 | m_pcBinIf->start(); |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | Void TEncSbac::codeVPS( TComVPS* pcVPS ) |
---|
| 243 | { |
---|
| 244 | assert (0); |
---|
| 245 | return; |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | Void TEncSbac::codeSPS( TComSPS* pcSPS ) |
---|
| 249 | { |
---|
| 250 | assert (0); |
---|
| 251 | return; |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | Void TEncSbac::codePPS( TComPPS* pcPPS ) |
---|
| 255 | { |
---|
| 256 | assert (0); |
---|
| 257 | return; |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | Void TEncSbac::codeSliceHeader( TComSlice* pcSlice ) |
---|
| 261 | { |
---|
| 262 | assert (0); |
---|
| 263 | return; |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | Void TEncSbac::codeTilesWPPEntryPoint( TComSlice* pSlice ) |
---|
| 267 | { |
---|
| 268 | assert (0); |
---|
| 269 | return; |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | Void TEncSbac::codeTerminatingBit( UInt uilsLast ) |
---|
| 273 | { |
---|
| 274 | m_pcBinIf->encodeBinTrm( uilsLast ); |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | Void TEncSbac::codeSliceFinish() |
---|
| 278 | { |
---|
| 279 | m_pcBinIf->finish(); |
---|
| 280 | } |
---|
| 281 | |
---|
| 282 | Void TEncSbac::xWriteUnarySymbol( UInt uiSymbol, ContextModel* pcSCModel, Int iOffset ) |
---|
| 283 | { |
---|
| 284 | m_pcBinIf->encodeBin( uiSymbol ? 1 : 0, pcSCModel[0] ); |
---|
| 285 | |
---|
| 286 | if( 0 == uiSymbol) |
---|
| 287 | { |
---|
| 288 | return; |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | while( uiSymbol-- ) |
---|
| 292 | { |
---|
| 293 | m_pcBinIf->encodeBin( uiSymbol ? 1 : 0, pcSCModel[ iOffset ] ); |
---|
| 294 | } |
---|
| 295 | |
---|
| 296 | return; |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | Void TEncSbac::xWriteUnaryMaxSymbol( UInt uiSymbol, ContextModel* pcSCModel, Int iOffset, UInt uiMaxSymbol ) |
---|
| 300 | { |
---|
| 301 | if (uiMaxSymbol == 0) |
---|
| 302 | { |
---|
| 303 | return; |
---|
| 304 | } |
---|
| 305 | |
---|
| 306 | m_pcBinIf->encodeBin( uiSymbol ? 1 : 0, pcSCModel[ 0 ] ); |
---|
| 307 | |
---|
| 308 | if ( uiSymbol == 0 ) |
---|
| 309 | { |
---|
| 310 | return; |
---|
| 311 | } |
---|
| 312 | |
---|
| 313 | Bool bCodeLast = ( uiMaxSymbol > uiSymbol ); |
---|
| 314 | |
---|
| 315 | while( --uiSymbol ) |
---|
| 316 | { |
---|
| 317 | m_pcBinIf->encodeBin( 1, pcSCModel[ iOffset ] ); |
---|
| 318 | } |
---|
| 319 | if( bCodeLast ) |
---|
| 320 | { |
---|
| 321 | m_pcBinIf->encodeBin( 0, pcSCModel[ iOffset ] ); |
---|
| 322 | } |
---|
| 323 | |
---|
| 324 | return; |
---|
| 325 | } |
---|
| 326 | |
---|
| 327 | Void TEncSbac::xWriteEpExGolomb( UInt uiSymbol, UInt uiCount ) |
---|
| 328 | { |
---|
| 329 | UInt bins = 0; |
---|
| 330 | Int numBins = 0; |
---|
| 331 | |
---|
| 332 | while( uiSymbol >= (UInt)(1<<uiCount) ) |
---|
| 333 | { |
---|
| 334 | bins = 2 * bins + 1; |
---|
| 335 | numBins++; |
---|
| 336 | uiSymbol -= 1 << uiCount; |
---|
| 337 | uiCount ++; |
---|
| 338 | } |
---|
| 339 | bins = 2 * bins + 0; |
---|
| 340 | numBins++; |
---|
| 341 | |
---|
| 342 | bins = (bins << uiCount) | uiSymbol; |
---|
| 343 | numBins += uiCount; |
---|
| 344 | |
---|
| 345 | assert( numBins <= 32 ); |
---|
| 346 | m_pcBinIf->encodeBinsEP( bins, numBins ); |
---|
| 347 | } |
---|
| 348 | |
---|
| 349 | /** Coding of coeff_abs_level_minus3 |
---|
| 350 | * \param uiSymbol value of coeff_abs_level_minus3 |
---|
| 351 | * \param ruiGoRiceParam reference to Rice parameter |
---|
| 352 | * \returns Void |
---|
| 353 | */ |
---|
| 354 | Void TEncSbac::xWriteCoefRemainExGolomb ( UInt symbol, UInt &rParam ) |
---|
| 355 | { |
---|
| 356 | Int codeNumber = (Int)symbol; |
---|
| 357 | UInt length; |
---|
| 358 | if (codeNumber < (COEF_REMAIN_BIN_REDUCTION << rParam)) |
---|
| 359 | { |
---|
| 360 | length = codeNumber>>rParam; |
---|
| 361 | m_pcBinIf->encodeBinsEP( (1<<(length+1))-2 , length+1); |
---|
| 362 | m_pcBinIf->encodeBinsEP((codeNumber%(1<<rParam)),rParam); |
---|
| 363 | } |
---|
| 364 | else |
---|
| 365 | { |
---|
| 366 | length = rParam; |
---|
| 367 | codeNumber = codeNumber - ( COEF_REMAIN_BIN_REDUCTION << rParam); |
---|
| 368 | while (codeNumber >= (1<<length)) |
---|
| 369 | { |
---|
| 370 | codeNumber -= (1<<(length++)); |
---|
| 371 | } |
---|
| 372 | m_pcBinIf->encodeBinsEP((1<<(COEF_REMAIN_BIN_REDUCTION+length+1-rParam))-2,COEF_REMAIN_BIN_REDUCTION+length+1-rParam); |
---|
| 373 | m_pcBinIf->encodeBinsEP(codeNumber,length); |
---|
| 374 | } |
---|
| 375 | } |
---|
| 376 | |
---|
| 377 | // SBAC RD |
---|
| 378 | Void TEncSbac::load ( TEncSbac* pSrc) |
---|
| 379 | { |
---|
| 380 | this->xCopyFrom(pSrc); |
---|
| 381 | } |
---|
| 382 | |
---|
| 383 | Void TEncSbac::loadIntraDirModeLuma( TEncSbac* pSrc) |
---|
| 384 | { |
---|
| 385 | m_pcBinIf->copyState( pSrc->m_pcBinIf ); |
---|
| 386 | |
---|
| 387 | this->m_cCUIntraPredSCModel .copyFrom( &pSrc->m_cCUIntraPredSCModel ); |
---|
| 388 | } |
---|
| 389 | |
---|
| 390 | |
---|
| 391 | Void TEncSbac::store( TEncSbac* pDest) |
---|
| 392 | { |
---|
| 393 | pDest->xCopyFrom( this ); |
---|
| 394 | } |
---|
| 395 | |
---|
| 396 | |
---|
| 397 | Void TEncSbac::xCopyFrom( TEncSbac* pSrc ) |
---|
| 398 | { |
---|
| 399 | m_pcBinIf->copyState( pSrc->m_pcBinIf ); |
---|
| 400 | |
---|
| 401 | this->m_uiCoeffCost = pSrc->m_uiCoeffCost; |
---|
| 402 | this->m_uiLastQp = pSrc->m_uiLastQp; |
---|
| 403 | |
---|
| 404 | memcpy( m_contextModels, pSrc->m_contextModels, m_numContextModels * sizeof( ContextModel ) ); |
---|
| 405 | } |
---|
| 406 | |
---|
| 407 | Void TEncSbac::codeMVPIdx ( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList ) |
---|
| 408 | { |
---|
| 409 | Int iSymbol = pcCU->getMVPIdx(eRefList, uiAbsPartIdx); |
---|
| 410 | Int iNum = AMVP_MAX_NUM_CANDS; |
---|
| 411 | |
---|
| 412 | xWriteUnaryMaxSymbol(iSymbol, m_cMVPIdxSCModel.get(0), 1, iNum-1); |
---|
| 413 | } |
---|
| 414 | |
---|
| 415 | Void TEncSbac::codePartSize( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth ) |
---|
| 416 | { |
---|
| 417 | PartSize eSize = pcCU->getPartitionSize( uiAbsPartIdx ); |
---|
[345] | 418 | |
---|
[313] | 419 | if ( pcCU->isIntra( uiAbsPartIdx ) ) |
---|
| 420 | { |
---|
| 421 | if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth ) |
---|
| 422 | { |
---|
| 423 | m_pcBinIf->encodeBin( eSize == SIZE_2Nx2N? 1 : 0, m_cCUPartSizeSCModel.get( 0, 0, 0 ) ); |
---|
| 424 | } |
---|
| 425 | return; |
---|
| 426 | } |
---|
| 427 | |
---|
| 428 | switch(eSize) |
---|
| 429 | { |
---|
| 430 | case SIZE_2Nx2N: |
---|
| 431 | { |
---|
| 432 | m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 0) ); |
---|
| 433 | break; |
---|
| 434 | } |
---|
| 435 | case SIZE_2NxN: |
---|
| 436 | case SIZE_2NxnU: |
---|
| 437 | case SIZE_2NxnD: |
---|
| 438 | { |
---|
| 439 | m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) ); |
---|
| 440 | m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 1) ); |
---|
| 441 | if ( pcCU->getSlice()->getSPS()->getAMPAcc( uiDepth ) ) |
---|
| 442 | { |
---|
| 443 | if (eSize == SIZE_2NxN) |
---|
| 444 | { |
---|
| 445 | m_pcBinIf->encodeBin(1, m_cCUAMPSCModel.get( 0, 0, 0 )); |
---|
| 446 | } |
---|
| 447 | else |
---|
| 448 | { |
---|
| 449 | m_pcBinIf->encodeBin(0, m_cCUAMPSCModel.get( 0, 0, 0 )); |
---|
| 450 | m_pcBinIf->encodeBinEP((eSize == SIZE_2NxnU? 0: 1)); |
---|
| 451 | } |
---|
| 452 | } |
---|
| 453 | break; |
---|
| 454 | } |
---|
| 455 | case SIZE_Nx2N: |
---|
| 456 | case SIZE_nLx2N: |
---|
| 457 | case SIZE_nRx2N: |
---|
| 458 | { |
---|
| 459 | m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) ); |
---|
| 460 | m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 1) ); |
---|
| 461 | if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth && !( pcCU->getWidth(uiAbsPartIdx) == 8 && pcCU->getHeight(uiAbsPartIdx) == 8 ) ) |
---|
| 462 | { |
---|
| 463 | m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 2) ); |
---|
| 464 | } |
---|
| 465 | if ( pcCU->getSlice()->getSPS()->getAMPAcc( uiDepth ) ) |
---|
| 466 | { |
---|
| 467 | if (eSize == SIZE_Nx2N) |
---|
| 468 | { |
---|
| 469 | m_pcBinIf->encodeBin(1, m_cCUAMPSCModel.get( 0, 0, 0 )); |
---|
| 470 | } |
---|
| 471 | else |
---|
| 472 | { |
---|
| 473 | m_pcBinIf->encodeBin(0, m_cCUAMPSCModel.get( 0, 0, 0 )); |
---|
| 474 | m_pcBinIf->encodeBinEP((eSize == SIZE_nLx2N? 0: 1)); |
---|
| 475 | } |
---|
| 476 | } |
---|
| 477 | break; |
---|
| 478 | } |
---|
| 479 | case SIZE_NxN: |
---|
| 480 | { |
---|
| 481 | if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth && !( pcCU->getWidth(uiAbsPartIdx) == 8 && pcCU->getHeight(uiAbsPartIdx) == 8 ) ) |
---|
| 482 | { |
---|
| 483 | m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) ); |
---|
| 484 | m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 1) ); |
---|
| 485 | m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 2) ); |
---|
| 486 | } |
---|
| 487 | break; |
---|
| 488 | } |
---|
| 489 | default: |
---|
| 490 | { |
---|
| 491 | assert(0); |
---|
| 492 | } |
---|
| 493 | } |
---|
| 494 | } |
---|
| 495 | |
---|
| 496 | /** code prediction mode |
---|
| 497 | * \param pcCU |
---|
| 498 | * \param uiAbsPartIdx |
---|
| 499 | * \returns Void |
---|
| 500 | */ |
---|
| 501 | Void TEncSbac::codePredMode( TComDataCU* pcCU, UInt uiAbsPartIdx ) |
---|
| 502 | { |
---|
| 503 | // get context function is here |
---|
| 504 | Int iPredMode = pcCU->getPredictionMode( uiAbsPartIdx ); |
---|
| 505 | m_pcBinIf->encodeBin( iPredMode == MODE_INTER ? 0 : 1, m_cCUPredModeSCModel.get( 0, 0, 0 ) ); |
---|
| 506 | } |
---|
| 507 | |
---|
| 508 | Void TEncSbac::codeCUTransquantBypassFlag( TComDataCU* pcCU, UInt uiAbsPartIdx ) |
---|
| 509 | { |
---|
| 510 | UInt uiSymbol = pcCU->getCUTransquantBypass(uiAbsPartIdx); |
---|
| 511 | m_pcBinIf->encodeBin( uiSymbol, m_CUTransquantBypassFlagSCModel.get( 0, 0, 0 ) ); |
---|
| 512 | } |
---|
| 513 | |
---|
| 514 | /** code skip flag |
---|
| 515 | * \param pcCU |
---|
| 516 | * \param uiAbsPartIdx |
---|
| 517 | * \returns Void |
---|
| 518 | */ |
---|
| 519 | Void TEncSbac::codeSkipFlag( TComDataCU* pcCU, UInt uiAbsPartIdx ) |
---|
| 520 | { |
---|
| 521 | // get context function is here |
---|
| 522 | UInt uiSymbol = pcCU->isSkipped( uiAbsPartIdx ) ? 1 : 0; |
---|
| 523 | UInt uiCtxSkip = pcCU->getCtxSkipFlag( uiAbsPartIdx ) ; |
---|
| 524 | m_pcBinIf->encodeBin( uiSymbol, m_cCUSkipFlagSCModel.get( 0, 0, uiCtxSkip ) ); |
---|
| 525 | DTRACE_CABAC_VL( g_nSymbolCounter++ ); |
---|
| 526 | DTRACE_CABAC_T( "\tSkipFlag" ); |
---|
| 527 | DTRACE_CABAC_T( "\tuiCtxSkip: "); |
---|
| 528 | DTRACE_CABAC_V( uiCtxSkip ); |
---|
| 529 | DTRACE_CABAC_T( "\tuiSymbol: "); |
---|
| 530 | DTRACE_CABAC_V( uiSymbol ); |
---|
| 531 | DTRACE_CABAC_T( "\n"); |
---|
| 532 | } |
---|
| 533 | |
---|
| 534 | /** code merge flag |
---|
| 535 | * \param pcCU |
---|
| 536 | * \param uiAbsPartIdx |
---|
| 537 | * \returns Void |
---|
| 538 | */ |
---|
| 539 | Void TEncSbac::codeMergeFlag( TComDataCU* pcCU, UInt uiAbsPartIdx ) |
---|
| 540 | { |
---|
| 541 | const UInt uiSymbol = pcCU->getMergeFlag( uiAbsPartIdx ) ? 1 : 0; |
---|
| 542 | m_pcBinIf->encodeBin( uiSymbol, *m_cCUMergeFlagExtSCModel.get( 0 ) ); |
---|
| 543 | |
---|
| 544 | DTRACE_CABAC_VL( g_nSymbolCounter++ ); |
---|
| 545 | DTRACE_CABAC_T( "\tMergeFlag: " ); |
---|
| 546 | DTRACE_CABAC_V( uiSymbol ); |
---|
| 547 | DTRACE_CABAC_T( "\tAddress: " ); |
---|
| 548 | DTRACE_CABAC_V( pcCU->getAddr() ); |
---|
| 549 | DTRACE_CABAC_T( "\tuiAbsPartIdx: " ); |
---|
| 550 | DTRACE_CABAC_V( uiAbsPartIdx ); |
---|
| 551 | DTRACE_CABAC_T( "\n" ); |
---|
| 552 | } |
---|
| 553 | |
---|
| 554 | /** code merge index |
---|
| 555 | * \param pcCU |
---|
| 556 | * \param uiAbsPartIdx |
---|
| 557 | * \returns Void |
---|
| 558 | */ |
---|
| 559 | Void TEncSbac::codeMergeIndex( TComDataCU* pcCU, UInt uiAbsPartIdx ) |
---|
| 560 | { |
---|
| 561 | UInt uiUnaryIdx = pcCU->getMergeIndex( uiAbsPartIdx ); |
---|
| 562 | UInt uiNumCand = pcCU->getSlice()->getMaxNumMergeCand(); |
---|
| 563 | if ( uiNumCand > 1 ) |
---|
| 564 | { |
---|
| 565 | for( UInt ui = 0; ui < uiNumCand - 1; ++ui ) |
---|
| 566 | { |
---|
| 567 | const UInt uiSymbol = ui == uiUnaryIdx ? 0 : 1; |
---|
| 568 | if ( ui==0 ) |
---|
| 569 | { |
---|
| 570 | m_pcBinIf->encodeBin( uiSymbol, m_cCUMergeIdxExtSCModel.get( 0, 0, 0 ) ); |
---|
| 571 | } |
---|
| 572 | else |
---|
| 573 | { |
---|
| 574 | m_pcBinIf->encodeBinEP( uiSymbol ); |
---|
| 575 | } |
---|
| 576 | if( uiSymbol == 0 ) |
---|
| 577 | { |
---|
| 578 | break; |
---|
| 579 | } |
---|
| 580 | } |
---|
| 581 | } |
---|
| 582 | DTRACE_CABAC_VL( g_nSymbolCounter++ ); |
---|
| 583 | DTRACE_CABAC_T( "\tparseMergeIndex()" ); |
---|
| 584 | DTRACE_CABAC_T( "\tuiMRGIdx= " ); |
---|
| 585 | DTRACE_CABAC_V( pcCU->getMergeIndex( uiAbsPartIdx ) ); |
---|
| 586 | DTRACE_CABAC_T( "\n" ); |
---|
| 587 | } |
---|
| 588 | |
---|
| 589 | Void TEncSbac::codeSplitFlag ( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth ) |
---|
| 590 | { |
---|
| 591 | if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth ) |
---|
| 592 | return; |
---|
| 593 | |
---|
| 594 | UInt uiCtx = pcCU->getCtxSplitFlag( uiAbsPartIdx, uiDepth ); |
---|
| 595 | UInt uiCurrSplitFlag = ( pcCU->getDepth( uiAbsPartIdx ) > uiDepth ) ? 1 : 0; |
---|
| 596 | |
---|
| 597 | assert( uiCtx < 3 ); |
---|
| 598 | m_pcBinIf->encodeBin( uiCurrSplitFlag, m_cCUSplitFlagSCModel.get( 0, 0, uiCtx ) ); |
---|
| 599 | DTRACE_CABAC_VL( g_nSymbolCounter++ ) |
---|
| 600 | DTRACE_CABAC_T( "\tSplitFlag\n" ) |
---|
| 601 | return; |
---|
| 602 | } |
---|
| 603 | |
---|
| 604 | Void TEncSbac::codeTransformSubdivFlag( UInt uiSymbol, UInt uiCtx ) |
---|
| 605 | { |
---|
| 606 | m_pcBinIf->encodeBin( uiSymbol, m_cCUTransSubdivFlagSCModel.get( 0, 0, uiCtx ) ); |
---|
| 607 | DTRACE_CABAC_VL( g_nSymbolCounter++ ) |
---|
| 608 | DTRACE_CABAC_T( "\tparseTransformSubdivFlag()" ) |
---|
| 609 | DTRACE_CABAC_T( "\tsymbol=" ) |
---|
| 610 | DTRACE_CABAC_V( uiSymbol ) |
---|
| 611 | DTRACE_CABAC_T( "\tctx=" ) |
---|
| 612 | DTRACE_CABAC_V( uiCtx ) |
---|
| 613 | DTRACE_CABAC_T( "\n" ) |
---|
| 614 | } |
---|
| 615 | |
---|
| 616 | Void TEncSbac::codeIntraDirLumaAng( TComDataCU* pcCU, UInt absPartIdx, Bool isMultiple) |
---|
| 617 | { |
---|
| 618 | UInt dir[4],j; |
---|
| 619 | Int preds[4][3] = {{-1, -1, -1},{-1, -1, -1},{-1, -1, -1},{-1, -1, -1}}; |
---|
| 620 | Int predNum[4], predIdx[4] ={ -1,-1,-1,-1}; |
---|
| 621 | PartSize mode = pcCU->getPartitionSize( absPartIdx ); |
---|
| 622 | UInt partNum = isMultiple?(mode==SIZE_NxN?4:1):1; |
---|
| 623 | UInt partOffset = ( pcCU->getPic()->getNumPartInCU() >> ( pcCU->getDepth(absPartIdx) << 1 ) ) >> 2; |
---|
| 624 | for (j=0;j<partNum;j++) |
---|
| 625 | { |
---|
| 626 | dir[j] = pcCU->getLumaIntraDir( absPartIdx+partOffset*j ); |
---|
| 627 | predNum[j] = pcCU->getIntraDirLumaPredictor(absPartIdx+partOffset*j, preds[j]); |
---|
| 628 | for(UInt i = 0; i < predNum[j]; i++) |
---|
| 629 | { |
---|
| 630 | if(dir[j] == preds[j][i]) |
---|
| 631 | { |
---|
| 632 | predIdx[j] = i; |
---|
| 633 | } |
---|
| 634 | } |
---|
| 635 | m_pcBinIf->encodeBin((predIdx[j] != -1)? 1 : 0, m_cCUIntraPredSCModel.get( 0, 0, 0 ) ); |
---|
| 636 | } |
---|
| 637 | for (j=0;j<partNum;j++) |
---|
| 638 | { |
---|
| 639 | if(predIdx[j] != -1) |
---|
| 640 | { |
---|
| 641 | m_pcBinIf->encodeBinEP( predIdx[j] ? 1 : 0 ); |
---|
| 642 | if (predIdx[j]) |
---|
| 643 | { |
---|
| 644 | m_pcBinIf->encodeBinEP( predIdx[j]-1 ); |
---|
| 645 | } |
---|
| 646 | } |
---|
| 647 | else |
---|
| 648 | { |
---|
| 649 | if (preds[j][0] > preds[j][1]) |
---|
| 650 | { |
---|
| 651 | std::swap(preds[j][0], preds[j][1]); |
---|
| 652 | } |
---|
| 653 | if (preds[j][0] > preds[j][2]) |
---|
| 654 | { |
---|
| 655 | std::swap(preds[j][0], preds[j][2]); |
---|
| 656 | } |
---|
| 657 | if (preds[j][1] > preds[j][2]) |
---|
| 658 | { |
---|
| 659 | std::swap(preds[j][1], preds[j][2]); |
---|
| 660 | } |
---|
| 661 | for(Int i = (predNum[j] - 1); i >= 0; i--) |
---|
| 662 | { |
---|
| 663 | dir[j] = dir[j] > preds[j][i] ? dir[j] - 1 : dir[j]; |
---|
| 664 | } |
---|
| 665 | m_pcBinIf->encodeBinsEP( dir[j], 5 ); |
---|
| 666 | } |
---|
| 667 | } |
---|
| 668 | return; |
---|
| 669 | } |
---|
| 670 | |
---|
| 671 | Void TEncSbac::codeIntraDirChroma( TComDataCU* pcCU, UInt uiAbsPartIdx ) |
---|
| 672 | { |
---|
| 673 | UInt uiIntraDirChroma = pcCU->getChromaIntraDir( uiAbsPartIdx ); |
---|
| 674 | |
---|
| 675 | if( uiIntraDirChroma == DM_CHROMA_IDX ) |
---|
| 676 | { |
---|
| 677 | m_pcBinIf->encodeBin( 0, m_cCUChromaPredSCModel.get( 0, 0, 0 ) ); |
---|
| 678 | } |
---|
| 679 | else |
---|
| 680 | { |
---|
| 681 | UInt uiAllowedChromaDir[ NUM_CHROMA_MODE ]; |
---|
| 682 | pcCU->getAllowedChromaDir( uiAbsPartIdx, uiAllowedChromaDir ); |
---|
| 683 | |
---|
| 684 | for( Int i = 0; i < NUM_CHROMA_MODE - 1; i++ ) |
---|
| 685 | { |
---|
| 686 | if( uiIntraDirChroma == uiAllowedChromaDir[i] ) |
---|
| 687 | { |
---|
| 688 | uiIntraDirChroma = i; |
---|
| 689 | break; |
---|
| 690 | } |
---|
| 691 | } |
---|
| 692 | m_pcBinIf->encodeBin( 1, m_cCUChromaPredSCModel.get( 0, 0, 0 ) ); |
---|
| 693 | |
---|
| 694 | m_pcBinIf->encodeBinsEP( uiIntraDirChroma, 2 ); |
---|
| 695 | } |
---|
| 696 | return; |
---|
| 697 | } |
---|
| 698 | |
---|
| 699 | Void TEncSbac::codeInterDir( TComDataCU* pcCU, UInt uiAbsPartIdx ) |
---|
| 700 | { |
---|
| 701 | const UInt uiInterDir = pcCU->getInterDir( uiAbsPartIdx ) - 1; |
---|
| 702 | const UInt uiCtx = pcCU->getCtxInterDir( uiAbsPartIdx ); |
---|
| 703 | ContextModel *pCtx = m_cCUInterDirSCModel.get( 0 ); |
---|
| 704 | if (pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N || pcCU->getHeight(uiAbsPartIdx) != 8 ) |
---|
| 705 | { |
---|
| 706 | m_pcBinIf->encodeBin( uiInterDir == 2 ? 1 : 0, *( pCtx + uiCtx ) ); |
---|
| 707 | } |
---|
| 708 | if (uiInterDir < 2) |
---|
| 709 | { |
---|
| 710 | m_pcBinIf->encodeBin( uiInterDir, *( pCtx + 4 ) ); |
---|
| 711 | } |
---|
| 712 | return; |
---|
| 713 | } |
---|
| 714 | |
---|
| 715 | Void TEncSbac::codeRefFrmIdx( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList ) |
---|
| 716 | { |
---|
| 717 | { |
---|
| 718 | Int iRefFrame = pcCU->getCUMvField( eRefList )->getRefIdx( uiAbsPartIdx ); |
---|
| 719 | ContextModel *pCtx = m_cCURefPicSCModel.get( 0 ); |
---|
| 720 | m_pcBinIf->encodeBin( ( iRefFrame == 0 ? 0 : 1 ), *pCtx ); |
---|
| 721 | |
---|
| 722 | if( iRefFrame > 0 ) |
---|
| 723 | { |
---|
| 724 | UInt uiRefNum = pcCU->getSlice()->getNumRefIdx( eRefList ) - 2; |
---|
| 725 | pCtx++; |
---|
| 726 | iRefFrame--; |
---|
| 727 | for( UInt ui = 0; ui < uiRefNum; ++ui ) |
---|
| 728 | { |
---|
| 729 | const UInt uiSymbol = ui == iRefFrame ? 0 : 1; |
---|
| 730 | if( ui == 0 ) |
---|
| 731 | { |
---|
| 732 | m_pcBinIf->encodeBin( uiSymbol, *pCtx ); |
---|
| 733 | } |
---|
| 734 | else |
---|
| 735 | { |
---|
| 736 | m_pcBinIf->encodeBinEP( uiSymbol ); |
---|
| 737 | } |
---|
| 738 | if( uiSymbol == 0 ) |
---|
| 739 | { |
---|
| 740 | break; |
---|
| 741 | } |
---|
| 742 | } |
---|
| 743 | } |
---|
| 744 | } |
---|
| 745 | return; |
---|
| 746 | } |
---|
| 747 | |
---|
| 748 | Void TEncSbac::codeMvd( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList ) |
---|
| 749 | { |
---|
| 750 | if(pcCU->getSlice()->getMvdL1ZeroFlag() && eRefList == REF_PIC_LIST_1 && pcCU->getInterDir(uiAbsPartIdx)==3) |
---|
| 751 | { |
---|
| 752 | return; |
---|
| 753 | } |
---|
| 754 | |
---|
| 755 | const TComCUMvField* pcCUMvField = pcCU->getCUMvField( eRefList ); |
---|
| 756 | const Int iHor = pcCUMvField->getMvd( uiAbsPartIdx ).getHor(); |
---|
| 757 | const Int iVer = pcCUMvField->getMvd( uiAbsPartIdx ).getVer(); |
---|
| 758 | ContextModel* pCtx = m_cCUMvdSCModel.get( 0 ); |
---|
| 759 | |
---|
| 760 | m_pcBinIf->encodeBin( iHor != 0 ? 1 : 0, *pCtx ); |
---|
| 761 | m_pcBinIf->encodeBin( iVer != 0 ? 1 : 0, *pCtx ); |
---|
| 762 | |
---|
| 763 | const Bool bHorAbsGr0 = iHor != 0; |
---|
| 764 | const Bool bVerAbsGr0 = iVer != 0; |
---|
| 765 | const UInt uiHorAbs = 0 > iHor ? -iHor : iHor; |
---|
| 766 | const UInt uiVerAbs = 0 > iVer ? -iVer : iVer; |
---|
| 767 | pCtx++; |
---|
| 768 | |
---|
| 769 | if( bHorAbsGr0 ) |
---|
| 770 | { |
---|
| 771 | m_pcBinIf->encodeBin( uiHorAbs > 1 ? 1 : 0, *pCtx ); |
---|
| 772 | } |
---|
| 773 | |
---|
| 774 | if( bVerAbsGr0 ) |
---|
| 775 | { |
---|
| 776 | m_pcBinIf->encodeBin( uiVerAbs > 1 ? 1 : 0, *pCtx ); |
---|
| 777 | } |
---|
| 778 | |
---|
| 779 | if( bHorAbsGr0 ) |
---|
| 780 | { |
---|
| 781 | if( uiHorAbs > 1 ) |
---|
| 782 | { |
---|
| 783 | xWriteEpExGolomb( uiHorAbs-2, 1 ); |
---|
| 784 | } |
---|
| 785 | |
---|
| 786 | m_pcBinIf->encodeBinEP( 0 > iHor ? 1 : 0 ); |
---|
| 787 | } |
---|
| 788 | |
---|
| 789 | if( bVerAbsGr0 ) |
---|
| 790 | { |
---|
| 791 | if( uiVerAbs > 1 ) |
---|
| 792 | { |
---|
| 793 | xWriteEpExGolomb( uiVerAbs-2, 1 ); |
---|
| 794 | } |
---|
| 795 | |
---|
| 796 | m_pcBinIf->encodeBinEP( 0 > iVer ? 1 : 0 ); |
---|
| 797 | } |
---|
| 798 | |
---|
| 799 | return; |
---|
| 800 | } |
---|
| 801 | |
---|
| 802 | Void TEncSbac::codeDeltaQP( TComDataCU* pcCU, UInt uiAbsPartIdx ) |
---|
| 803 | { |
---|
| 804 | Int iDQp = pcCU->getQP( uiAbsPartIdx ) - pcCU->getRefQP( uiAbsPartIdx ); |
---|
| 805 | |
---|
[442] | 806 | #if REPN_FORMAT_IN_VPS |
---|
| 807 | Int qpBdOffsetY = pcCU->getSlice()->getQpBDOffsetY(); |
---|
| 808 | #else |
---|
[313] | 809 | Int qpBdOffsetY = pcCU->getSlice()->getSPS()->getQpBDOffsetY(); |
---|
[442] | 810 | #endif |
---|
[313] | 811 | iDQp = (iDQp + 78 + qpBdOffsetY + (qpBdOffsetY/2)) % (52 + qpBdOffsetY) - 26 - (qpBdOffsetY/2); |
---|
| 812 | |
---|
| 813 | UInt uiAbsDQp = (UInt)((iDQp > 0)? iDQp : (-iDQp)); |
---|
| 814 | UInt TUValue = min((Int)uiAbsDQp, CU_DQP_TU_CMAX); |
---|
| 815 | xWriteUnaryMaxSymbol( TUValue, &m_cCUDeltaQpSCModel.get( 0, 0, 0 ), 1, CU_DQP_TU_CMAX); |
---|
| 816 | if( uiAbsDQp >= CU_DQP_TU_CMAX ) |
---|
| 817 | { |
---|
| 818 | xWriteEpExGolomb( uiAbsDQp - CU_DQP_TU_CMAX, CU_DQP_EG_k ); |
---|
| 819 | } |
---|
| 820 | |
---|
| 821 | if ( uiAbsDQp > 0) |
---|
| 822 | { |
---|
| 823 | UInt uiSign = (iDQp > 0 ? 0 : 1); |
---|
| 824 | m_pcBinIf->encodeBinEP(uiSign); |
---|
| 825 | } |
---|
| 826 | |
---|
| 827 | return; |
---|
| 828 | } |
---|
| 829 | |
---|
| 830 | Void TEncSbac::codeQtCbf( TComDataCU* pcCU, UInt uiAbsPartIdx, TextType eType, UInt uiTrDepth ) |
---|
| 831 | { |
---|
| 832 | UInt uiCbf = pcCU->getCbf ( uiAbsPartIdx, eType, uiTrDepth ); |
---|
| 833 | UInt uiCtx = pcCU->getCtxQtCbf( eType, uiTrDepth ); |
---|
| 834 | m_pcBinIf->encodeBin( uiCbf , m_cCUQtCbfSCModel.get( 0, eType ? TEXT_CHROMA : eType, uiCtx ) ); |
---|
| 835 | DTRACE_CABAC_VL( g_nSymbolCounter++ ) |
---|
| 836 | DTRACE_CABAC_T( "\tparseQtCbf()" ) |
---|
| 837 | DTRACE_CABAC_T( "\tsymbol=" ) |
---|
| 838 | DTRACE_CABAC_V( uiCbf ) |
---|
| 839 | DTRACE_CABAC_T( "\tctx=" ) |
---|
| 840 | DTRACE_CABAC_V( uiCtx ) |
---|
| 841 | DTRACE_CABAC_T( "\tetype=" ) |
---|
| 842 | DTRACE_CABAC_V( eType ) |
---|
| 843 | DTRACE_CABAC_T( "\tuiAbsPartIdx=" ) |
---|
| 844 | DTRACE_CABAC_V( uiAbsPartIdx ) |
---|
| 845 | DTRACE_CABAC_T( "\n" ) |
---|
| 846 | } |
---|
| 847 | |
---|
| 848 | void TEncSbac::codeTransformSkipFlags (TComDataCU* pcCU, UInt uiAbsPartIdx, UInt width, UInt height, TextType eTType ) |
---|
| 849 | { |
---|
| 850 | if (pcCU->getCUTransquantBypass(uiAbsPartIdx)) |
---|
| 851 | { |
---|
| 852 | return; |
---|
| 853 | } |
---|
| 854 | if(width != 4 || height != 4) |
---|
| 855 | { |
---|
| 856 | return; |
---|
| 857 | } |
---|
| 858 | |
---|
| 859 | UInt useTransformSkip = pcCU->getTransformSkip( uiAbsPartIdx,eTType); |
---|
| 860 | m_pcBinIf->encodeBin( useTransformSkip, m_cTransformSkipSCModel.get( 0, eTType? TEXT_CHROMA: TEXT_LUMA, 0 ) ); |
---|
| 861 | DTRACE_CABAC_VL( g_nSymbolCounter++ ) |
---|
| 862 | DTRACE_CABAC_T("\tparseTransformSkip()"); |
---|
| 863 | DTRACE_CABAC_T( "\tsymbol=" ) |
---|
| 864 | DTRACE_CABAC_V( useTransformSkip ) |
---|
| 865 | DTRACE_CABAC_T( "\tAddr=" ) |
---|
| 866 | DTRACE_CABAC_V( pcCU->getAddr() ) |
---|
| 867 | DTRACE_CABAC_T( "\tetype=" ) |
---|
| 868 | DTRACE_CABAC_V( eTType ) |
---|
| 869 | DTRACE_CABAC_T( "\tuiAbsPartIdx=" ) |
---|
| 870 | DTRACE_CABAC_V( uiAbsPartIdx ) |
---|
| 871 | DTRACE_CABAC_T( "\n" ) |
---|
| 872 | } |
---|
| 873 | |
---|
| 874 | /** Code I_PCM information. |
---|
| 875 | * \param pcCU pointer to CU |
---|
| 876 | * \param uiAbsPartIdx CU index |
---|
| 877 | * \returns Void |
---|
| 878 | */ |
---|
| 879 | Void TEncSbac::codeIPCMInfo( TComDataCU* pcCU, UInt uiAbsPartIdx ) |
---|
| 880 | { |
---|
| 881 | UInt uiIPCM = (pcCU->getIPCMFlag(uiAbsPartIdx) == true)? 1 : 0; |
---|
| 882 | |
---|
| 883 | Bool writePCMSampleFlag = pcCU->getIPCMFlag(uiAbsPartIdx); |
---|
| 884 | |
---|
| 885 | m_pcBinIf->encodeBinTrm (uiIPCM); |
---|
| 886 | |
---|
| 887 | if (writePCMSampleFlag) |
---|
| 888 | { |
---|
| 889 | m_pcBinIf->encodePCMAlignBits(); |
---|
| 890 | |
---|
| 891 | UInt uiMinCoeffSize = pcCU->getPic()->getMinCUWidth()*pcCU->getPic()->getMinCUHeight(); |
---|
| 892 | UInt uiLumaOffset = uiMinCoeffSize*uiAbsPartIdx; |
---|
| 893 | UInt uiChromaOffset = uiLumaOffset>>2; |
---|
| 894 | Pel* piPCMSample; |
---|
| 895 | UInt uiWidth; |
---|
| 896 | UInt uiHeight; |
---|
| 897 | UInt uiSampleBits; |
---|
| 898 | UInt uiX, uiY; |
---|
| 899 | |
---|
| 900 | piPCMSample = pcCU->getPCMSampleY() + uiLumaOffset; |
---|
| 901 | uiWidth = pcCU->getWidth(uiAbsPartIdx); |
---|
| 902 | uiHeight = pcCU->getHeight(uiAbsPartIdx); |
---|
| 903 | uiSampleBits = pcCU->getSlice()->getSPS()->getPCMBitDepthLuma(); |
---|
| 904 | |
---|
| 905 | for(uiY = 0; uiY < uiHeight; uiY++) |
---|
| 906 | { |
---|
| 907 | for(uiX = 0; uiX < uiWidth; uiX++) |
---|
| 908 | { |
---|
| 909 | UInt uiSample = piPCMSample[uiX]; |
---|
| 910 | |
---|
| 911 | m_pcBinIf->xWritePCMCode(uiSample, uiSampleBits); |
---|
| 912 | } |
---|
| 913 | piPCMSample += uiWidth; |
---|
| 914 | } |
---|
| 915 | |
---|
| 916 | piPCMSample = pcCU->getPCMSampleCb() + uiChromaOffset; |
---|
| 917 | uiWidth = pcCU->getWidth(uiAbsPartIdx)/2; |
---|
| 918 | uiHeight = pcCU->getHeight(uiAbsPartIdx)/2; |
---|
| 919 | uiSampleBits = pcCU->getSlice()->getSPS()->getPCMBitDepthChroma(); |
---|
| 920 | |
---|
| 921 | for(uiY = 0; uiY < uiHeight; uiY++) |
---|
| 922 | { |
---|
| 923 | for(uiX = 0; uiX < uiWidth; uiX++) |
---|
| 924 | { |
---|
| 925 | UInt uiSample = piPCMSample[uiX]; |
---|
| 926 | |
---|
| 927 | m_pcBinIf->xWritePCMCode(uiSample, uiSampleBits); |
---|
| 928 | } |
---|
| 929 | piPCMSample += uiWidth; |
---|
| 930 | } |
---|
| 931 | |
---|
| 932 | piPCMSample = pcCU->getPCMSampleCr() + uiChromaOffset; |
---|
| 933 | uiWidth = pcCU->getWidth(uiAbsPartIdx)/2; |
---|
| 934 | uiHeight = pcCU->getHeight(uiAbsPartIdx)/2; |
---|
| 935 | uiSampleBits = pcCU->getSlice()->getSPS()->getPCMBitDepthChroma(); |
---|
| 936 | |
---|
| 937 | for(uiY = 0; uiY < uiHeight; uiY++) |
---|
| 938 | { |
---|
| 939 | for(uiX = 0; uiX < uiWidth; uiX++) |
---|
| 940 | { |
---|
| 941 | UInt uiSample = piPCMSample[uiX]; |
---|
| 942 | |
---|
| 943 | m_pcBinIf->xWritePCMCode(uiSample, uiSampleBits); |
---|
| 944 | } |
---|
| 945 | piPCMSample += uiWidth; |
---|
| 946 | } |
---|
| 947 | m_pcBinIf->resetBac(); |
---|
| 948 | } |
---|
| 949 | } |
---|
| 950 | |
---|
| 951 | Void TEncSbac::codeQtRootCbf( TComDataCU* pcCU, UInt uiAbsPartIdx ) |
---|
| 952 | { |
---|
| 953 | UInt uiCbf = pcCU->getQtRootCbf( uiAbsPartIdx ); |
---|
| 954 | UInt uiCtx = 0; |
---|
| 955 | m_pcBinIf->encodeBin( uiCbf , m_cCUQtRootCbfSCModel.get( 0, 0, uiCtx ) ); |
---|
| 956 | DTRACE_CABAC_VL( g_nSymbolCounter++ ) |
---|
| 957 | DTRACE_CABAC_T( "\tparseQtRootCbf()" ) |
---|
| 958 | DTRACE_CABAC_T( "\tsymbol=" ) |
---|
| 959 | DTRACE_CABAC_V( uiCbf ) |
---|
| 960 | DTRACE_CABAC_T( "\tctx=" ) |
---|
| 961 | DTRACE_CABAC_V( uiCtx ) |
---|
| 962 | DTRACE_CABAC_T( "\tuiAbsPartIdx=" ) |
---|
| 963 | DTRACE_CABAC_V( uiAbsPartIdx ) |
---|
| 964 | DTRACE_CABAC_T( "\n" ) |
---|
| 965 | } |
---|
| 966 | |
---|
| 967 | Void TEncSbac::codeQtCbfZero( TComDataCU* pcCU, TextType eType, UInt uiTrDepth ) |
---|
| 968 | { |
---|
| 969 | // this function is only used to estimate the bits when cbf is 0 |
---|
| 970 | // and will never be called when writing the bistream. do not need to write log |
---|
| 971 | UInt uiCbf = 0; |
---|
| 972 | UInt uiCtx = pcCU->getCtxQtCbf( eType, uiTrDepth ); |
---|
| 973 | m_pcBinIf->encodeBin( uiCbf , m_cCUQtCbfSCModel.get( 0, eType ? TEXT_CHROMA : eType, uiCtx ) ); |
---|
| 974 | } |
---|
| 975 | |
---|
| 976 | Void TEncSbac::codeQtRootCbfZero( TComDataCU* pcCU ) |
---|
| 977 | { |
---|
| 978 | // this function is only used to estimate the bits when cbf is 0 |
---|
| 979 | // and will never be called when writing the bistream. do not need to write log |
---|
| 980 | UInt uiCbf = 0; |
---|
| 981 | UInt uiCtx = 0; |
---|
| 982 | m_pcBinIf->encodeBin( uiCbf , m_cCUQtRootCbfSCModel.get( 0, 0, uiCtx ) ); |
---|
| 983 | } |
---|
| 984 | |
---|
| 985 | /** Encode (X,Y) position of the last significant coefficient |
---|
| 986 | * \param uiPosX X component of last coefficient |
---|
| 987 | * \param uiPosY Y component of last coefficient |
---|
| 988 | * \param width Block width |
---|
| 989 | * \param height Block height |
---|
| 990 | * \param eTType plane type / luminance or chrominance |
---|
| 991 | * \param uiScanIdx scan type (zig-zag, hor, ver) |
---|
| 992 | * This method encodes the X and Y component within a block of the last significant coefficient. |
---|
| 993 | */ |
---|
| 994 | Void TEncSbac::codeLastSignificantXY( UInt uiPosX, UInt uiPosY, Int width, Int height, TextType eTType, UInt uiScanIdx ) |
---|
| 995 | { |
---|
| 996 | // swap |
---|
| 997 | if( uiScanIdx == SCAN_VER ) |
---|
| 998 | { |
---|
| 999 | swap( uiPosX, uiPosY ); |
---|
| 1000 | } |
---|
| 1001 | |
---|
| 1002 | UInt uiCtxLast; |
---|
| 1003 | ContextModel *pCtxX = m_cCuCtxLastX.get( 0, eTType ); |
---|
| 1004 | ContextModel *pCtxY = m_cCuCtxLastY.get( 0, eTType ); |
---|
| 1005 | UInt uiGroupIdxX = g_uiGroupIdx[ uiPosX ]; |
---|
| 1006 | UInt uiGroupIdxY = g_uiGroupIdx[ uiPosY ]; |
---|
| 1007 | |
---|
| 1008 | |
---|
| 1009 | Int blkSizeOffsetX, blkSizeOffsetY, shiftX, shiftY; |
---|
| 1010 | blkSizeOffsetX = eTType ? 0: (g_aucConvertToBit[ width ] *3 + ((g_aucConvertToBit[ width ] +1)>>2)); |
---|
| 1011 | blkSizeOffsetY = eTType ? 0: (g_aucConvertToBit[ height ]*3 + ((g_aucConvertToBit[ height ]+1)>>2)); |
---|
| 1012 | shiftX= eTType ? g_aucConvertToBit[ width ] :((g_aucConvertToBit[ width ]+3)>>2); |
---|
| 1013 | shiftY= eTType ? g_aucConvertToBit[ height ] :((g_aucConvertToBit[ height ]+3)>>2); |
---|
| 1014 | // posX |
---|
| 1015 | for( uiCtxLast = 0; uiCtxLast < uiGroupIdxX; uiCtxLast++ ) |
---|
| 1016 | { |
---|
| 1017 | m_pcBinIf->encodeBin( 1, *( pCtxX + blkSizeOffsetX + (uiCtxLast >>shiftX) ) ); |
---|
| 1018 | } |
---|
| 1019 | if( uiGroupIdxX < g_uiGroupIdx[ width - 1 ]) |
---|
| 1020 | { |
---|
| 1021 | m_pcBinIf->encodeBin( 0, *( pCtxX + blkSizeOffsetX + (uiCtxLast >>shiftX) ) ); |
---|
| 1022 | } |
---|
| 1023 | |
---|
| 1024 | // posY |
---|
| 1025 | for( uiCtxLast = 0; uiCtxLast < uiGroupIdxY; uiCtxLast++ ) |
---|
| 1026 | { |
---|
| 1027 | m_pcBinIf->encodeBin( 1, *( pCtxY + blkSizeOffsetY + (uiCtxLast >>shiftY) ) ); |
---|
| 1028 | } |
---|
| 1029 | if( uiGroupIdxY < g_uiGroupIdx[ height - 1 ]) |
---|
| 1030 | { |
---|
| 1031 | m_pcBinIf->encodeBin( 0, *( pCtxY + blkSizeOffsetY + (uiCtxLast >>shiftY) ) ); |
---|
| 1032 | } |
---|
| 1033 | if ( uiGroupIdxX > 3 ) |
---|
| 1034 | { |
---|
| 1035 | UInt uiCount = ( uiGroupIdxX - 2 ) >> 1; |
---|
| 1036 | uiPosX = uiPosX - g_uiMinInGroup[ uiGroupIdxX ]; |
---|
| 1037 | for (Int i = uiCount - 1 ; i >= 0; i-- ) |
---|
| 1038 | { |
---|
| 1039 | m_pcBinIf->encodeBinEP( ( uiPosX >> i ) & 1 ); |
---|
| 1040 | } |
---|
| 1041 | } |
---|
| 1042 | if ( uiGroupIdxY > 3 ) |
---|
| 1043 | { |
---|
| 1044 | UInt uiCount = ( uiGroupIdxY - 2 ) >> 1; |
---|
| 1045 | uiPosY = uiPosY - g_uiMinInGroup[ uiGroupIdxY ]; |
---|
| 1046 | for ( Int i = uiCount - 1 ; i >= 0; i-- ) |
---|
| 1047 | { |
---|
| 1048 | m_pcBinIf->encodeBinEP( ( uiPosY >> i ) & 1 ); |
---|
| 1049 | } |
---|
| 1050 | } |
---|
| 1051 | } |
---|
| 1052 | |
---|
| 1053 | Void TEncSbac::codeCoeffNxN( TComDataCU* pcCU, TCoeff* pcCoef, UInt uiAbsPartIdx, UInt uiWidth, UInt uiHeight, UInt uiDepth, TextType eTType ) |
---|
| 1054 | { |
---|
| 1055 | DTRACE_CABAC_VL( g_nSymbolCounter++ ) |
---|
| 1056 | DTRACE_CABAC_T( "\tparseCoeffNxN()\teType=" ) |
---|
| 1057 | DTRACE_CABAC_V( eTType ) |
---|
| 1058 | DTRACE_CABAC_T( "\twidth=" ) |
---|
| 1059 | DTRACE_CABAC_V( uiWidth ) |
---|
| 1060 | DTRACE_CABAC_T( "\theight=" ) |
---|
| 1061 | DTRACE_CABAC_V( uiHeight ) |
---|
| 1062 | DTRACE_CABAC_T( "\tdepth=" ) |
---|
| 1063 | DTRACE_CABAC_V( uiDepth ) |
---|
| 1064 | DTRACE_CABAC_T( "\tabspartidx=" ) |
---|
| 1065 | DTRACE_CABAC_V( uiAbsPartIdx ) |
---|
| 1066 | DTRACE_CABAC_T( "\ttoCU-X=" ) |
---|
| 1067 | DTRACE_CABAC_V( pcCU->getCUPelX() ) |
---|
| 1068 | DTRACE_CABAC_T( "\ttoCU-Y=" ) |
---|
| 1069 | DTRACE_CABAC_V( pcCU->getCUPelY() ) |
---|
| 1070 | DTRACE_CABAC_T( "\tCU-addr=" ) |
---|
| 1071 | DTRACE_CABAC_V( pcCU->getAddr() ) |
---|
| 1072 | DTRACE_CABAC_T( "\tinCU-X=" ) |
---|
| 1073 | DTRACE_CABAC_V( g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ] ) |
---|
| 1074 | DTRACE_CABAC_T( "\tinCU-Y=" ) |
---|
| 1075 | DTRACE_CABAC_V( g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ] ) |
---|
| 1076 | DTRACE_CABAC_T( "\tpredmode=" ) |
---|
| 1077 | DTRACE_CABAC_V( pcCU->getPredictionMode( uiAbsPartIdx ) ) |
---|
| 1078 | DTRACE_CABAC_T( "\n" ) |
---|
| 1079 | |
---|
| 1080 | if( uiWidth > m_pcSlice->getSPS()->getMaxTrSize() ) |
---|
| 1081 | { |
---|
| 1082 | uiWidth = m_pcSlice->getSPS()->getMaxTrSize(); |
---|
| 1083 | uiHeight = m_pcSlice->getSPS()->getMaxTrSize(); |
---|
| 1084 | } |
---|
| 1085 | |
---|
| 1086 | UInt uiNumSig = 0; |
---|
| 1087 | |
---|
| 1088 | // compute number of significant coefficients |
---|
| 1089 | uiNumSig = TEncEntropy::countNonZeroCoeffs(pcCoef, uiWidth * uiHeight); |
---|
| 1090 | |
---|
| 1091 | if ( uiNumSig == 0 ) |
---|
| 1092 | return; |
---|
| 1093 | if(pcCU->getSlice()->getPPS()->getUseTransformSkip()) |
---|
| 1094 | { |
---|
| 1095 | codeTransformSkipFlags( pcCU,uiAbsPartIdx, uiWidth, uiHeight, eTType ); |
---|
| 1096 | } |
---|
| 1097 | eTType = eTType == TEXT_LUMA ? TEXT_LUMA : ( eTType == TEXT_NONE ? TEXT_NONE : TEXT_CHROMA ); |
---|
| 1098 | |
---|
| 1099 | //----- encode significance map ----- |
---|
| 1100 | const UInt uiLog2BlockSize = g_aucConvertToBit[ uiWidth ] + 2; |
---|
| 1101 | UInt uiScanIdx = pcCU->getCoefScanIdx(uiAbsPartIdx, uiWidth, eTType==TEXT_LUMA, pcCU->isIntra(uiAbsPartIdx)); |
---|
| 1102 | const UInt *scan = g_auiSigLastScan[ uiScanIdx ][ uiLog2BlockSize - 1 ]; |
---|
| 1103 | |
---|
| 1104 | Bool beValid; |
---|
| 1105 | if (pcCU->getCUTransquantBypass(uiAbsPartIdx)) |
---|
| 1106 | { |
---|
| 1107 | beValid = false; |
---|
| 1108 | } |
---|
| 1109 | else |
---|
| 1110 | { |
---|
| 1111 | beValid = pcCU->getSlice()->getPPS()->getSignHideFlag() > 0; |
---|
| 1112 | } |
---|
| 1113 | |
---|
| 1114 | // Find position of last coefficient |
---|
| 1115 | Int scanPosLast = -1; |
---|
| 1116 | Int posLast; |
---|
| 1117 | |
---|
| 1118 | const UInt * scanCG; |
---|
| 1119 | { |
---|
| 1120 | scanCG = g_auiSigLastScan[ uiScanIdx ][ uiLog2BlockSize > 3 ? uiLog2BlockSize-2-1 : 0 ]; |
---|
| 1121 | if( uiLog2BlockSize == 3 ) |
---|
| 1122 | { |
---|
| 1123 | scanCG = g_sigLastScan8x8[ uiScanIdx ]; |
---|
| 1124 | } |
---|
| 1125 | else if( uiLog2BlockSize == 5 ) |
---|
| 1126 | { |
---|
| 1127 | scanCG = g_sigLastScanCG32x32; |
---|
| 1128 | } |
---|
| 1129 | } |
---|
| 1130 | UInt uiSigCoeffGroupFlag[ MLS_GRP_NUM ]; |
---|
| 1131 | static const UInt uiShift = MLS_CG_SIZE >> 1; |
---|
| 1132 | const UInt uiNumBlkSide = uiWidth >> uiShift; |
---|
| 1133 | |
---|
| 1134 | ::memset( uiSigCoeffGroupFlag, 0, sizeof(UInt) * MLS_GRP_NUM ); |
---|
| 1135 | |
---|
| 1136 | do |
---|
| 1137 | { |
---|
| 1138 | posLast = scan[ ++scanPosLast ]; |
---|
| 1139 | |
---|
| 1140 | // get L1 sig map |
---|
| 1141 | UInt uiPosY = posLast >> uiLog2BlockSize; |
---|
| 1142 | UInt uiPosX = posLast - ( uiPosY << uiLog2BlockSize ); |
---|
| 1143 | UInt uiBlkIdx = uiNumBlkSide * (uiPosY >> uiShift) + (uiPosX >> uiShift); |
---|
| 1144 | if( pcCoef[ posLast ] ) |
---|
| 1145 | { |
---|
| 1146 | uiSigCoeffGroupFlag[ uiBlkIdx ] = 1; |
---|
| 1147 | } |
---|
| 1148 | |
---|
| 1149 | uiNumSig -= ( pcCoef[ posLast ] != 0 ); |
---|
| 1150 | } |
---|
| 1151 | while ( uiNumSig > 0 ); |
---|
| 1152 | |
---|
| 1153 | // Code position of last coefficient |
---|
| 1154 | Int posLastY = posLast >> uiLog2BlockSize; |
---|
| 1155 | Int posLastX = posLast - ( posLastY << uiLog2BlockSize ); |
---|
| 1156 | codeLastSignificantXY(posLastX, posLastY, uiWidth, uiHeight, eTType, uiScanIdx); |
---|
| 1157 | |
---|
| 1158 | //===== code significance flag ===== |
---|
| 1159 | ContextModel * const baseCoeffGroupCtx = m_cCUSigCoeffGroupSCModel.get( 0, eTType ); |
---|
| 1160 | ContextModel * const baseCtx = (eTType==TEXT_LUMA) ? m_cCUSigSCModel.get( 0, 0 ) : m_cCUSigSCModel.get( 0, 0 ) + NUM_SIG_FLAG_CTX_LUMA; |
---|
| 1161 | |
---|
| 1162 | |
---|
| 1163 | const Int iLastScanSet = scanPosLast >> LOG2_SCAN_SET_SIZE; |
---|
| 1164 | UInt c1 = 1; |
---|
| 1165 | UInt uiGoRiceParam = 0; |
---|
| 1166 | Int iScanPosSig = scanPosLast; |
---|
| 1167 | |
---|
| 1168 | for( Int iSubSet = iLastScanSet; iSubSet >= 0; iSubSet-- ) |
---|
| 1169 | { |
---|
| 1170 | Int numNonZero = 0; |
---|
| 1171 | Int iSubPos = iSubSet << LOG2_SCAN_SET_SIZE; |
---|
| 1172 | uiGoRiceParam = 0; |
---|
| 1173 | Int absCoeff[16]; |
---|
| 1174 | UInt coeffSigns = 0; |
---|
| 1175 | |
---|
| 1176 | Int lastNZPosInCG = -1, firstNZPosInCG = SCAN_SET_SIZE; |
---|
| 1177 | |
---|
| 1178 | if( iScanPosSig == scanPosLast ) |
---|
| 1179 | { |
---|
| 1180 | absCoeff[ 0 ] = abs( pcCoef[ posLast ] ); |
---|
| 1181 | coeffSigns = ( pcCoef[ posLast ] < 0 ); |
---|
| 1182 | numNonZero = 1; |
---|
| 1183 | lastNZPosInCG = iScanPosSig; |
---|
| 1184 | firstNZPosInCG = iScanPosSig; |
---|
| 1185 | iScanPosSig--; |
---|
| 1186 | } |
---|
| 1187 | |
---|
| 1188 | // encode significant_coeffgroup_flag |
---|
| 1189 | Int iCGBlkPos = scanCG[ iSubSet ]; |
---|
| 1190 | Int iCGPosY = iCGBlkPos / uiNumBlkSide; |
---|
| 1191 | Int iCGPosX = iCGBlkPos - (iCGPosY * uiNumBlkSide); |
---|
| 1192 | if( iSubSet == iLastScanSet || iSubSet == 0) |
---|
| 1193 | { |
---|
| 1194 | uiSigCoeffGroupFlag[ iCGBlkPos ] = 1; |
---|
| 1195 | } |
---|
| 1196 | else |
---|
| 1197 | { |
---|
| 1198 | UInt uiSigCoeffGroup = (uiSigCoeffGroupFlag[ iCGBlkPos ] != 0); |
---|
| 1199 | UInt uiCtxSig = TComTrQuant::getSigCoeffGroupCtxInc( uiSigCoeffGroupFlag, iCGPosX, iCGPosY, uiWidth, uiHeight ); |
---|
| 1200 | m_pcBinIf->encodeBin( uiSigCoeffGroup, baseCoeffGroupCtx[ uiCtxSig ] ); |
---|
| 1201 | } |
---|
| 1202 | |
---|
| 1203 | // encode significant_coeff_flag |
---|
| 1204 | if( uiSigCoeffGroupFlag[ iCGBlkPos ] ) |
---|
| 1205 | { |
---|
| 1206 | Int patternSigCtx = TComTrQuant::calcPatternSigCtx( uiSigCoeffGroupFlag, iCGPosX, iCGPosY, uiWidth, uiHeight ); |
---|
| 1207 | UInt uiBlkPos, uiPosY, uiPosX, uiSig, uiCtxSig; |
---|
| 1208 | for( ; iScanPosSig >= iSubPos; iScanPosSig-- ) |
---|
| 1209 | { |
---|
| 1210 | uiBlkPos = scan[ iScanPosSig ]; |
---|
| 1211 | uiPosY = uiBlkPos >> uiLog2BlockSize; |
---|
| 1212 | uiPosX = uiBlkPos - ( uiPosY << uiLog2BlockSize ); |
---|
| 1213 | uiSig = (pcCoef[ uiBlkPos ] != 0); |
---|
| 1214 | if( iScanPosSig > iSubPos || iSubSet == 0 || numNonZero ) |
---|
| 1215 | { |
---|
| 1216 | uiCtxSig = TComTrQuant::getSigCtxInc( patternSigCtx, uiScanIdx, uiPosX, uiPosY, uiLog2BlockSize, eTType ); |
---|
| 1217 | m_pcBinIf->encodeBin( uiSig, baseCtx[ uiCtxSig ] ); |
---|
| 1218 | } |
---|
| 1219 | if( uiSig ) |
---|
| 1220 | { |
---|
| 1221 | absCoeff[ numNonZero ] = abs( pcCoef[ uiBlkPos ] ); |
---|
| 1222 | coeffSigns = 2 * coeffSigns + ( pcCoef[ uiBlkPos ] < 0 ); |
---|
| 1223 | numNonZero++; |
---|
| 1224 | if( lastNZPosInCG == -1 ) |
---|
| 1225 | { |
---|
| 1226 | lastNZPosInCG = iScanPosSig; |
---|
| 1227 | } |
---|
| 1228 | firstNZPosInCG = iScanPosSig; |
---|
| 1229 | } |
---|
| 1230 | } |
---|
| 1231 | } |
---|
| 1232 | else |
---|
| 1233 | { |
---|
| 1234 | iScanPosSig = iSubPos - 1; |
---|
| 1235 | } |
---|
| 1236 | |
---|
| 1237 | if( numNonZero > 0 ) |
---|
| 1238 | { |
---|
| 1239 | Bool signHidden = ( lastNZPosInCG - firstNZPosInCG >= SBH_THRESHOLD ); |
---|
| 1240 | UInt uiCtxSet = (iSubSet > 0 && eTType==TEXT_LUMA) ? 2 : 0; |
---|
| 1241 | |
---|
| 1242 | if( c1 == 0 ) |
---|
| 1243 | { |
---|
| 1244 | uiCtxSet++; |
---|
| 1245 | } |
---|
| 1246 | c1 = 1; |
---|
| 1247 | ContextModel *baseCtxMod = ( eTType==TEXT_LUMA ) ? m_cCUOneSCModel.get( 0, 0 ) + 4 * uiCtxSet : m_cCUOneSCModel.get( 0, 0 ) + NUM_ONE_FLAG_CTX_LUMA + 4 * uiCtxSet; |
---|
| 1248 | |
---|
| 1249 | Int numC1Flag = min(numNonZero, C1FLAG_NUMBER); |
---|
| 1250 | Int firstC2FlagIdx = -1; |
---|
| 1251 | for( Int idx = 0; idx < numC1Flag; idx++ ) |
---|
| 1252 | { |
---|
| 1253 | UInt uiSymbol = absCoeff[ idx ] > 1; |
---|
| 1254 | m_pcBinIf->encodeBin( uiSymbol, baseCtxMod[c1] ); |
---|
| 1255 | if( uiSymbol ) |
---|
| 1256 | { |
---|
| 1257 | c1 = 0; |
---|
| 1258 | |
---|
| 1259 | if (firstC2FlagIdx == -1) |
---|
| 1260 | { |
---|
| 1261 | firstC2FlagIdx = idx; |
---|
| 1262 | } |
---|
| 1263 | } |
---|
| 1264 | else if( (c1 < 3) && (c1 > 0) ) |
---|
| 1265 | { |
---|
| 1266 | c1++; |
---|
| 1267 | } |
---|
| 1268 | } |
---|
| 1269 | |
---|
| 1270 | if (c1 == 0) |
---|
| 1271 | { |
---|
| 1272 | |
---|
| 1273 | baseCtxMod = ( eTType==TEXT_LUMA ) ? m_cCUAbsSCModel.get( 0, 0 ) + uiCtxSet : m_cCUAbsSCModel.get( 0, 0 ) + NUM_ABS_FLAG_CTX_LUMA + uiCtxSet; |
---|
| 1274 | if ( firstC2FlagIdx != -1) |
---|
| 1275 | { |
---|
| 1276 | UInt symbol = absCoeff[ firstC2FlagIdx ] > 2; |
---|
| 1277 | m_pcBinIf->encodeBin( symbol, baseCtxMod[0] ); |
---|
| 1278 | } |
---|
| 1279 | } |
---|
| 1280 | |
---|
| 1281 | if( beValid && signHidden ) |
---|
| 1282 | { |
---|
| 1283 | m_pcBinIf->encodeBinsEP( (coeffSigns >> 1), numNonZero-1 ); |
---|
| 1284 | } |
---|
| 1285 | else |
---|
| 1286 | { |
---|
| 1287 | m_pcBinIf->encodeBinsEP( coeffSigns, numNonZero ); |
---|
| 1288 | } |
---|
| 1289 | |
---|
| 1290 | Int iFirstCoeff2 = 1; |
---|
| 1291 | if (c1 == 0 || numNonZero > C1FLAG_NUMBER) |
---|
| 1292 | { |
---|
| 1293 | for ( Int idx = 0; idx < numNonZero; idx++ ) |
---|
| 1294 | { |
---|
| 1295 | UInt baseLevel = (idx < C1FLAG_NUMBER)? (2 + iFirstCoeff2 ) : 1; |
---|
| 1296 | |
---|
| 1297 | if( absCoeff[ idx ] >= baseLevel) |
---|
| 1298 | { |
---|
| 1299 | xWriteCoefRemainExGolomb( absCoeff[ idx ] - baseLevel, uiGoRiceParam ); |
---|
| 1300 | if(absCoeff[idx] > 3*(1<<uiGoRiceParam)) |
---|
| 1301 | { |
---|
| 1302 | uiGoRiceParam = min<UInt>(uiGoRiceParam+ 1, 4); |
---|
| 1303 | } |
---|
| 1304 | } |
---|
| 1305 | if(absCoeff[ idx ] >= 2) |
---|
| 1306 | { |
---|
| 1307 | iFirstCoeff2 = 0; |
---|
| 1308 | } |
---|
| 1309 | } |
---|
| 1310 | } |
---|
| 1311 | } |
---|
| 1312 | } |
---|
| 1313 | |
---|
| 1314 | return; |
---|
| 1315 | } |
---|
| 1316 | |
---|
| 1317 | /** code SAO offset sign |
---|
| 1318 | * \param code sign value |
---|
| 1319 | */ |
---|
| 1320 | Void TEncSbac::codeSAOSign( UInt code ) |
---|
| 1321 | { |
---|
| 1322 | m_pcBinIf->encodeBinEP( code ); |
---|
| 1323 | } |
---|
| 1324 | |
---|
| 1325 | Void TEncSbac::codeSaoMaxUvlc ( UInt code, UInt maxSymbol ) |
---|
| 1326 | { |
---|
| 1327 | if (maxSymbol == 0) |
---|
| 1328 | { |
---|
| 1329 | return; |
---|
| 1330 | } |
---|
| 1331 | |
---|
| 1332 | Int i; |
---|
| 1333 | Bool bCodeLast = ( maxSymbol > code ); |
---|
| 1334 | |
---|
| 1335 | if ( code == 0 ) |
---|
| 1336 | { |
---|
| 1337 | m_pcBinIf->encodeBinEP( 0 ); |
---|
| 1338 | } |
---|
| 1339 | else |
---|
| 1340 | { |
---|
| 1341 | m_pcBinIf->encodeBinEP( 1 ); |
---|
| 1342 | for ( i=0; i<code-1; i++ ) |
---|
| 1343 | { |
---|
| 1344 | m_pcBinIf->encodeBinEP( 1 ); |
---|
| 1345 | } |
---|
| 1346 | if( bCodeLast ) |
---|
| 1347 | { |
---|
| 1348 | m_pcBinIf->encodeBinEP( 0 ); |
---|
| 1349 | } |
---|
| 1350 | } |
---|
| 1351 | } |
---|
| 1352 | |
---|
| 1353 | |
---|
| 1354 | /** Code SAO EO class or BO band position |
---|
| 1355 | * \param uiLength |
---|
| 1356 | * \param uiCode |
---|
| 1357 | */ |
---|
| 1358 | Void TEncSbac::codeSaoUflc ( UInt uiLength, UInt uiCode ) |
---|
| 1359 | { |
---|
| 1360 | m_pcBinIf->encodeBinsEP ( uiCode, uiLength ); |
---|
| 1361 | } |
---|
| 1362 | /** Code SAO merge flags |
---|
| 1363 | * \param uiCode |
---|
| 1364 | * \param uiCompIdx |
---|
| 1365 | */ |
---|
| 1366 | Void TEncSbac::codeSaoMerge ( UInt uiCode ) |
---|
| 1367 | { |
---|
| 1368 | if (uiCode == 0) |
---|
| 1369 | { |
---|
| 1370 | m_pcBinIf->encodeBin(0, m_cSaoMergeSCModel.get( 0, 0, 0 )); |
---|
| 1371 | } |
---|
| 1372 | else |
---|
| 1373 | { |
---|
| 1374 | m_pcBinIf->encodeBin(1, m_cSaoMergeSCModel.get( 0, 0, 0 )); |
---|
| 1375 | } |
---|
| 1376 | } |
---|
| 1377 | /** Code SAO type index |
---|
| 1378 | * \param uiCode |
---|
| 1379 | */ |
---|
| 1380 | Void TEncSbac::codeSaoTypeIdx ( UInt uiCode) |
---|
| 1381 | { |
---|
| 1382 | if (uiCode == 0) |
---|
| 1383 | { |
---|
| 1384 | m_pcBinIf->encodeBin( 0, m_cSaoTypeIdxSCModel.get( 0, 0, 0 ) ); |
---|
| 1385 | } |
---|
| 1386 | else |
---|
| 1387 | { |
---|
| 1388 | m_pcBinIf->encodeBin( 1, m_cSaoTypeIdxSCModel.get( 0, 0, 0 ) ); |
---|
| 1389 | m_pcBinIf->encodeBinEP( uiCode <= 4 ? 1 : 0 ); |
---|
| 1390 | } |
---|
| 1391 | } |
---|
| 1392 | /*! |
---|
| 1393 | **************************************************************************** |
---|
| 1394 | * \brief |
---|
| 1395 | * estimate bit cost for CBP, significant map and significant coefficients |
---|
| 1396 | **************************************************************************** |
---|
| 1397 | */ |
---|
| 1398 | Void TEncSbac::estBit( estBitsSbacStruct* pcEstBitsSbac, Int width, Int height, TextType eTType ) |
---|
| 1399 | { |
---|
| 1400 | estCBFBit( pcEstBitsSbac ); |
---|
| 1401 | |
---|
| 1402 | estSignificantCoeffGroupMapBit( pcEstBitsSbac, eTType ); |
---|
| 1403 | |
---|
| 1404 | // encode significance map |
---|
| 1405 | estSignificantMapBit( pcEstBitsSbac, width, height, eTType ); |
---|
| 1406 | |
---|
| 1407 | // encode significant coefficients |
---|
| 1408 | estSignificantCoefficientsBit( pcEstBitsSbac, eTType ); |
---|
| 1409 | } |
---|
| 1410 | |
---|
| 1411 | /*! |
---|
| 1412 | **************************************************************************** |
---|
| 1413 | * \brief |
---|
| 1414 | * estimate bit cost for each CBP bit |
---|
| 1415 | **************************************************************************** |
---|
| 1416 | */ |
---|
| 1417 | Void TEncSbac::estCBFBit( estBitsSbacStruct* pcEstBitsSbac ) |
---|
| 1418 | { |
---|
| 1419 | ContextModel *pCtx = m_cCUQtCbfSCModel.get( 0 ); |
---|
| 1420 | |
---|
| 1421 | for( UInt uiCtxInc = 0; uiCtxInc < 3*NUM_QT_CBF_CTX; uiCtxInc++ ) |
---|
| 1422 | { |
---|
| 1423 | pcEstBitsSbac->blockCbpBits[ uiCtxInc ][ 0 ] = pCtx[ uiCtxInc ].getEntropyBits( 0 ); |
---|
| 1424 | pcEstBitsSbac->blockCbpBits[ uiCtxInc ][ 1 ] = pCtx[ uiCtxInc ].getEntropyBits( 1 ); |
---|
| 1425 | } |
---|
| 1426 | |
---|
| 1427 | pCtx = m_cCUQtRootCbfSCModel.get( 0 ); |
---|
| 1428 | |
---|
| 1429 | for( UInt uiCtxInc = 0; uiCtxInc < 4; uiCtxInc++ ) |
---|
| 1430 | { |
---|
| 1431 | pcEstBitsSbac->blockRootCbpBits[ uiCtxInc ][ 0 ] = pCtx[ uiCtxInc ].getEntropyBits( 0 ); |
---|
| 1432 | pcEstBitsSbac->blockRootCbpBits[ uiCtxInc ][ 1 ] = pCtx[ uiCtxInc ].getEntropyBits( 1 ); |
---|
| 1433 | } |
---|
| 1434 | } |
---|
| 1435 | |
---|
| 1436 | |
---|
| 1437 | /*! |
---|
| 1438 | **************************************************************************** |
---|
| 1439 | * \brief |
---|
| 1440 | * estimate SAMBAC bit cost for significant coefficient group map |
---|
| 1441 | **************************************************************************** |
---|
| 1442 | */ |
---|
| 1443 | Void TEncSbac::estSignificantCoeffGroupMapBit( estBitsSbacStruct* pcEstBitsSbac, TextType eTType ) |
---|
| 1444 | { |
---|
| 1445 | Int firstCtx = 0, numCtx = NUM_SIG_CG_FLAG_CTX; |
---|
| 1446 | |
---|
| 1447 | for ( Int ctxIdx = firstCtx; ctxIdx < firstCtx + numCtx; ctxIdx++ ) |
---|
| 1448 | { |
---|
| 1449 | for( UInt uiBin = 0; uiBin < 2; uiBin++ ) |
---|
| 1450 | { |
---|
| 1451 | pcEstBitsSbac->significantCoeffGroupBits[ ctxIdx ][ uiBin ] = m_cCUSigCoeffGroupSCModel.get( 0, eTType, ctxIdx ).getEntropyBits( uiBin ); |
---|
| 1452 | } |
---|
| 1453 | } |
---|
| 1454 | } |
---|
| 1455 | |
---|
| 1456 | |
---|
| 1457 | /*! |
---|
| 1458 | **************************************************************************** |
---|
| 1459 | * \brief |
---|
| 1460 | * estimate SAMBAC bit cost for significant coefficient map |
---|
| 1461 | **************************************************************************** |
---|
| 1462 | */ |
---|
| 1463 | Void TEncSbac::estSignificantMapBit( estBitsSbacStruct* pcEstBitsSbac, Int width, Int height, TextType eTType ) |
---|
| 1464 | { |
---|
| 1465 | Int firstCtx = 1, numCtx = 8; |
---|
| 1466 | if (max(width, height) >= 16) |
---|
| 1467 | { |
---|
| 1468 | firstCtx = (eTType == TEXT_LUMA) ? 21 : 12; |
---|
| 1469 | numCtx = (eTType == TEXT_LUMA) ? 6 : 3; |
---|
| 1470 | } |
---|
| 1471 | else if (width == 8) |
---|
| 1472 | { |
---|
| 1473 | firstCtx = 9; |
---|
| 1474 | numCtx = (eTType == TEXT_LUMA) ? 12 : 3; |
---|
| 1475 | } |
---|
| 1476 | |
---|
| 1477 | if (eTType == TEXT_LUMA ) |
---|
| 1478 | { |
---|
| 1479 | for( UInt bin = 0; bin < 2; bin++ ) |
---|
| 1480 | { |
---|
| 1481 | pcEstBitsSbac->significantBits[ 0 ][ bin ] = m_cCUSigSCModel.get( 0, 0, 0 ).getEntropyBits( bin ); |
---|
| 1482 | } |
---|
| 1483 | |
---|
| 1484 | for ( Int ctxIdx = firstCtx; ctxIdx < firstCtx + numCtx; ctxIdx++ ) |
---|
| 1485 | { |
---|
| 1486 | for( UInt uiBin = 0; uiBin < 2; uiBin++ ) |
---|
| 1487 | { |
---|
| 1488 | pcEstBitsSbac->significantBits[ ctxIdx ][ uiBin ] = m_cCUSigSCModel.get( 0, 0, ctxIdx ).getEntropyBits( uiBin ); |
---|
| 1489 | } |
---|
| 1490 | } |
---|
| 1491 | } |
---|
| 1492 | else |
---|
| 1493 | { |
---|
| 1494 | for( UInt bin = 0; bin < 2; bin++ ) |
---|
| 1495 | { |
---|
| 1496 | pcEstBitsSbac->significantBits[ 0 ][ bin ] = m_cCUSigSCModel.get( 0, 0, NUM_SIG_FLAG_CTX_LUMA + 0 ).getEntropyBits( bin ); |
---|
| 1497 | } |
---|
| 1498 | for ( Int ctxIdx = firstCtx; ctxIdx < firstCtx + numCtx; ctxIdx++ ) |
---|
| 1499 | { |
---|
| 1500 | for( UInt uiBin = 0; uiBin < 2; uiBin++ ) |
---|
| 1501 | { |
---|
| 1502 | pcEstBitsSbac->significantBits[ ctxIdx ][ uiBin ] = m_cCUSigSCModel.get( 0, 0, NUM_SIG_FLAG_CTX_LUMA + ctxIdx ).getEntropyBits( uiBin ); |
---|
| 1503 | } |
---|
| 1504 | } |
---|
| 1505 | } |
---|
| 1506 | Int iBitsX = 0, iBitsY = 0; |
---|
| 1507 | Int blkSizeOffsetX, blkSizeOffsetY, shiftX, shiftY; |
---|
| 1508 | |
---|
| 1509 | blkSizeOffsetX = eTType ? 0: (g_aucConvertToBit[ width ] *3 + ((g_aucConvertToBit[ width ] +1)>>2)); |
---|
| 1510 | blkSizeOffsetY = eTType ? 0: (g_aucConvertToBit[ height ]*3 + ((g_aucConvertToBit[ height ]+1)>>2)); |
---|
| 1511 | shiftX = eTType ? g_aucConvertToBit[ width ] :((g_aucConvertToBit[ width ]+3)>>2); |
---|
| 1512 | shiftY = eTType ? g_aucConvertToBit[ height ] :((g_aucConvertToBit[ height ]+3)>>2); |
---|
| 1513 | |
---|
| 1514 | Int ctx; |
---|
| 1515 | ContextModel *pCtxX = m_cCuCtxLastX.get( 0, eTType ); |
---|
| 1516 | for (ctx = 0; ctx < g_uiGroupIdx[ width - 1 ]; ctx++) |
---|
| 1517 | { |
---|
| 1518 | Int ctxOffset = blkSizeOffsetX + (ctx >>shiftX); |
---|
| 1519 | pcEstBitsSbac->lastXBits[ ctx ] = iBitsX + pCtxX[ ctxOffset ].getEntropyBits( 0 ); |
---|
| 1520 | iBitsX += pCtxX[ ctxOffset ].getEntropyBits( 1 ); |
---|
| 1521 | } |
---|
| 1522 | pcEstBitsSbac->lastXBits[ctx] = iBitsX; |
---|
| 1523 | ContextModel *pCtxY = m_cCuCtxLastY.get( 0, eTType ); |
---|
| 1524 | for (ctx = 0; ctx < g_uiGroupIdx[ height - 1 ]; ctx++) |
---|
| 1525 | { |
---|
| 1526 | Int ctxOffset = blkSizeOffsetY + (ctx >>shiftY); |
---|
| 1527 | pcEstBitsSbac->lastYBits[ ctx ] = iBitsY + pCtxY[ ctxOffset ].getEntropyBits( 0 ); |
---|
| 1528 | iBitsY += pCtxY[ ctxOffset ].getEntropyBits( 1 ); |
---|
| 1529 | } |
---|
| 1530 | pcEstBitsSbac->lastYBits[ctx] = iBitsY; |
---|
| 1531 | } |
---|
| 1532 | |
---|
| 1533 | /*! |
---|
| 1534 | **************************************************************************** |
---|
| 1535 | * \brief |
---|
| 1536 | * estimate bit cost of significant coefficient |
---|
| 1537 | **************************************************************************** |
---|
| 1538 | */ |
---|
| 1539 | Void TEncSbac::estSignificantCoefficientsBit( estBitsSbacStruct* pcEstBitsSbac, TextType eTType ) |
---|
| 1540 | { |
---|
| 1541 | if (eTType==TEXT_LUMA) |
---|
| 1542 | { |
---|
| 1543 | ContextModel *ctxOne = m_cCUOneSCModel.get(0, 0); |
---|
| 1544 | ContextModel *ctxAbs = m_cCUAbsSCModel.get(0, 0); |
---|
| 1545 | |
---|
| 1546 | for (Int ctxIdx = 0; ctxIdx < NUM_ONE_FLAG_CTX_LUMA; ctxIdx++) |
---|
| 1547 | { |
---|
| 1548 | pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 0 ] = ctxOne[ ctxIdx ].getEntropyBits( 0 ); |
---|
| 1549 | pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 1 ] = ctxOne[ ctxIdx ].getEntropyBits( 1 ); |
---|
| 1550 | } |
---|
| 1551 | |
---|
| 1552 | for (Int ctxIdx = 0; ctxIdx < NUM_ABS_FLAG_CTX_LUMA; ctxIdx++) |
---|
| 1553 | { |
---|
| 1554 | pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 0 ] = ctxAbs[ ctxIdx ].getEntropyBits( 0 ); |
---|
| 1555 | pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 1 ] = ctxAbs[ ctxIdx ].getEntropyBits( 1 ); |
---|
| 1556 | } |
---|
| 1557 | } |
---|
| 1558 | else |
---|
| 1559 | { |
---|
| 1560 | ContextModel *ctxOne = m_cCUOneSCModel.get(0, 0) + NUM_ONE_FLAG_CTX_LUMA; |
---|
| 1561 | ContextModel *ctxAbs = m_cCUAbsSCModel.get(0, 0) + NUM_ABS_FLAG_CTX_LUMA; |
---|
| 1562 | |
---|
| 1563 | for (Int ctxIdx = 0; ctxIdx < NUM_ONE_FLAG_CTX_CHROMA; ctxIdx++) |
---|
| 1564 | { |
---|
| 1565 | pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 0 ] = ctxOne[ ctxIdx ].getEntropyBits( 0 ); |
---|
| 1566 | pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 1 ] = ctxOne[ ctxIdx ].getEntropyBits( 1 ); |
---|
| 1567 | } |
---|
| 1568 | |
---|
| 1569 | for (Int ctxIdx = 0; ctxIdx < NUM_ABS_FLAG_CTX_CHROMA; ctxIdx++) |
---|
| 1570 | { |
---|
| 1571 | pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 0 ] = ctxAbs[ ctxIdx ].getEntropyBits( 0 ); |
---|
| 1572 | pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 1 ] = ctxAbs[ ctxIdx ].getEntropyBits( 1 ); |
---|
| 1573 | } |
---|
| 1574 | } |
---|
| 1575 | } |
---|
| 1576 | |
---|
| 1577 | /** |
---|
| 1578 | - Initialize our context information from the nominated source. |
---|
| 1579 | . |
---|
| 1580 | \param pSrc From where to copy context information. |
---|
| 1581 | */ |
---|
| 1582 | Void TEncSbac::xCopyContextsFrom( TEncSbac* pSrc ) |
---|
| 1583 | { |
---|
| 1584 | memcpy(m_contextModels, pSrc->m_contextModels, m_numContextModels*sizeof(m_contextModels[0])); |
---|
| 1585 | } |
---|
| 1586 | |
---|
| 1587 | Void TEncSbac::loadContexts ( TEncSbac* pScr) |
---|
| 1588 | { |
---|
| 1589 | this->xCopyContextsFrom(pScr); |
---|
| 1590 | } |
---|
| 1591 | |
---|
| 1592 | //! \} |
---|