[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 ); |
---|
| 418 | if ( pcCU->isIntra( uiAbsPartIdx ) ) |
---|
| 419 | { |
---|
| 420 | if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth ) |
---|
| 421 | { |
---|
| 422 | m_pcBinIf->encodeBin( eSize == SIZE_2Nx2N? 1 : 0, m_cCUPartSizeSCModel.get( 0, 0, 0 ) ); |
---|
| 423 | } |
---|
| 424 | return; |
---|
| 425 | } |
---|
| 426 | |
---|
| 427 | switch(eSize) |
---|
| 428 | { |
---|
| 429 | case SIZE_2Nx2N: |
---|
| 430 | { |
---|
| 431 | m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 0) ); |
---|
| 432 | break; |
---|
| 433 | } |
---|
| 434 | case SIZE_2NxN: |
---|
| 435 | case SIZE_2NxnU: |
---|
| 436 | case SIZE_2NxnD: |
---|
| 437 | { |
---|
| 438 | m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) ); |
---|
| 439 | m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 1) ); |
---|
| 440 | if ( pcCU->getSlice()->getSPS()->getAMPAcc( uiDepth ) ) |
---|
| 441 | { |
---|
| 442 | if (eSize == SIZE_2NxN) |
---|
| 443 | { |
---|
| 444 | m_pcBinIf->encodeBin(1, m_cCUAMPSCModel.get( 0, 0, 0 )); |
---|
| 445 | } |
---|
| 446 | else |
---|
| 447 | { |
---|
| 448 | m_pcBinIf->encodeBin(0, m_cCUAMPSCModel.get( 0, 0, 0 )); |
---|
| 449 | m_pcBinIf->encodeBinEP((eSize == SIZE_2NxnU? 0: 1)); |
---|
| 450 | } |
---|
| 451 | } |
---|
| 452 | break; |
---|
| 453 | } |
---|
| 454 | case SIZE_Nx2N: |
---|
| 455 | case SIZE_nLx2N: |
---|
| 456 | case SIZE_nRx2N: |
---|
| 457 | { |
---|
| 458 | m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) ); |
---|
| 459 | m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 1) ); |
---|
| 460 | if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth && !( pcCU->getWidth(uiAbsPartIdx) == 8 && pcCU->getHeight(uiAbsPartIdx) == 8 ) ) |
---|
| 461 | { |
---|
| 462 | m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 2) ); |
---|
| 463 | } |
---|
| 464 | if ( pcCU->getSlice()->getSPS()->getAMPAcc( uiDepth ) ) |
---|
| 465 | { |
---|
| 466 | if (eSize == SIZE_Nx2N) |
---|
| 467 | { |
---|
| 468 | m_pcBinIf->encodeBin(1, m_cCUAMPSCModel.get( 0, 0, 0 )); |
---|
| 469 | } |
---|
| 470 | else |
---|
| 471 | { |
---|
| 472 | m_pcBinIf->encodeBin(0, m_cCUAMPSCModel.get( 0, 0, 0 )); |
---|
| 473 | m_pcBinIf->encodeBinEP((eSize == SIZE_nLx2N? 0: 1)); |
---|
| 474 | } |
---|
| 475 | } |
---|
| 476 | break; |
---|
| 477 | } |
---|
| 478 | case SIZE_NxN: |
---|
| 479 | { |
---|
| 480 | if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth && !( pcCU->getWidth(uiAbsPartIdx) == 8 && pcCU->getHeight(uiAbsPartIdx) == 8 ) ) |
---|
| 481 | { |
---|
| 482 | m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) ); |
---|
| 483 | m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 1) ); |
---|
| 484 | m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 2) ); |
---|
| 485 | } |
---|
| 486 | break; |
---|
| 487 | } |
---|
| 488 | default: |
---|
| 489 | { |
---|
| 490 | assert(0); |
---|
| 491 | } |
---|
| 492 | } |
---|
| 493 | } |
---|
| 494 | |
---|
| 495 | /** code prediction mode |
---|
| 496 | * \param pcCU |
---|
| 497 | * \param uiAbsPartIdx |
---|
| 498 | * \returns Void |
---|
| 499 | */ |
---|
| 500 | Void TEncSbac::codePredMode( TComDataCU* pcCU, UInt uiAbsPartIdx ) |
---|
| 501 | { |
---|
| 502 | // get context function is here |
---|
| 503 | Int iPredMode = pcCU->getPredictionMode( uiAbsPartIdx ); |
---|
| 504 | m_pcBinIf->encodeBin( iPredMode == MODE_INTER ? 0 : 1, m_cCUPredModeSCModel.get( 0, 0, 0 ) ); |
---|
| 505 | } |
---|
| 506 | |
---|
| 507 | Void TEncSbac::codeCUTransquantBypassFlag( TComDataCU* pcCU, UInt uiAbsPartIdx ) |
---|
| 508 | { |
---|
| 509 | UInt uiSymbol = pcCU->getCUTransquantBypass(uiAbsPartIdx); |
---|
| 510 | m_pcBinIf->encodeBin( uiSymbol, m_CUTransquantBypassFlagSCModel.get( 0, 0, 0 ) ); |
---|
| 511 | } |
---|
| 512 | |
---|
| 513 | /** code skip flag |
---|
| 514 | * \param pcCU |
---|
| 515 | * \param uiAbsPartIdx |
---|
| 516 | * \returns Void |
---|
| 517 | */ |
---|
| 518 | Void TEncSbac::codeSkipFlag( TComDataCU* pcCU, UInt uiAbsPartIdx ) |
---|
| 519 | { |
---|
| 520 | // get context function is here |
---|
| 521 | UInt uiSymbol = pcCU->isSkipped( uiAbsPartIdx ) ? 1 : 0; |
---|
| 522 | UInt uiCtxSkip = pcCU->getCtxSkipFlag( uiAbsPartIdx ) ; |
---|
| 523 | m_pcBinIf->encodeBin( uiSymbol, m_cCUSkipFlagSCModel.get( 0, 0, uiCtxSkip ) ); |
---|
| 524 | DTRACE_CABAC_VL( g_nSymbolCounter++ ); |
---|
| 525 | DTRACE_CABAC_T( "\tSkipFlag" ); |
---|
| 526 | DTRACE_CABAC_T( "\tuiCtxSkip: "); |
---|
| 527 | DTRACE_CABAC_V( uiCtxSkip ); |
---|
| 528 | DTRACE_CABAC_T( "\tuiSymbol: "); |
---|
| 529 | DTRACE_CABAC_V( uiSymbol ); |
---|
| 530 | DTRACE_CABAC_T( "\n"); |
---|
| 531 | } |
---|
| 532 | |
---|
| 533 | /** code merge flag |
---|
| 534 | * \param pcCU |
---|
| 535 | * \param uiAbsPartIdx |
---|
| 536 | * \returns Void |
---|
| 537 | */ |
---|
| 538 | Void TEncSbac::codeMergeFlag( TComDataCU* pcCU, UInt uiAbsPartIdx ) |
---|
| 539 | { |
---|
| 540 | const UInt uiSymbol = pcCU->getMergeFlag( uiAbsPartIdx ) ? 1 : 0; |
---|
| 541 | m_pcBinIf->encodeBin( uiSymbol, *m_cCUMergeFlagExtSCModel.get( 0 ) ); |
---|
| 542 | |
---|
| 543 | DTRACE_CABAC_VL( g_nSymbolCounter++ ); |
---|
| 544 | DTRACE_CABAC_T( "\tMergeFlag: " ); |
---|
| 545 | DTRACE_CABAC_V( uiSymbol ); |
---|
| 546 | DTRACE_CABAC_T( "\tAddress: " ); |
---|
| 547 | DTRACE_CABAC_V( pcCU->getAddr() ); |
---|
| 548 | DTRACE_CABAC_T( "\tuiAbsPartIdx: " ); |
---|
| 549 | DTRACE_CABAC_V( uiAbsPartIdx ); |
---|
| 550 | DTRACE_CABAC_T( "\n" ); |
---|
| 551 | } |
---|
| 552 | |
---|
| 553 | /** code merge index |
---|
| 554 | * \param pcCU |
---|
| 555 | * \param uiAbsPartIdx |
---|
| 556 | * \returns Void |
---|
| 557 | */ |
---|
| 558 | Void TEncSbac::codeMergeIndex( TComDataCU* pcCU, UInt uiAbsPartIdx ) |
---|
| 559 | { |
---|
| 560 | UInt uiUnaryIdx = pcCU->getMergeIndex( uiAbsPartIdx ); |
---|
| 561 | UInt uiNumCand = pcCU->getSlice()->getMaxNumMergeCand(); |
---|
| 562 | if ( uiNumCand > 1 ) |
---|
| 563 | { |
---|
| 564 | for( UInt ui = 0; ui < uiNumCand - 1; ++ui ) |
---|
| 565 | { |
---|
| 566 | const UInt uiSymbol = ui == uiUnaryIdx ? 0 : 1; |
---|
| 567 | if ( ui==0 ) |
---|
| 568 | { |
---|
| 569 | m_pcBinIf->encodeBin( uiSymbol, m_cCUMergeIdxExtSCModel.get( 0, 0, 0 ) ); |
---|
| 570 | } |
---|
| 571 | else |
---|
| 572 | { |
---|
| 573 | m_pcBinIf->encodeBinEP( uiSymbol ); |
---|
| 574 | } |
---|
| 575 | if( uiSymbol == 0 ) |
---|
| 576 | { |
---|
| 577 | break; |
---|
| 578 | } |
---|
| 579 | } |
---|
| 580 | } |
---|
| 581 | DTRACE_CABAC_VL( g_nSymbolCounter++ ); |
---|
| 582 | DTRACE_CABAC_T( "\tparseMergeIndex()" ); |
---|
| 583 | DTRACE_CABAC_T( "\tuiMRGIdx= " ); |
---|
| 584 | DTRACE_CABAC_V( pcCU->getMergeIndex( uiAbsPartIdx ) ); |
---|
| 585 | DTRACE_CABAC_T( "\n" ); |
---|
| 586 | } |
---|
| 587 | |
---|
| 588 | Void TEncSbac::codeSplitFlag ( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth ) |
---|
| 589 | { |
---|
| 590 | if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth ) |
---|
| 591 | return; |
---|
| 592 | |
---|
| 593 | UInt uiCtx = pcCU->getCtxSplitFlag( uiAbsPartIdx, uiDepth ); |
---|
| 594 | UInt uiCurrSplitFlag = ( pcCU->getDepth( uiAbsPartIdx ) > uiDepth ) ? 1 : 0; |
---|
| 595 | |
---|
| 596 | assert( uiCtx < 3 ); |
---|
| 597 | m_pcBinIf->encodeBin( uiCurrSplitFlag, m_cCUSplitFlagSCModel.get( 0, 0, uiCtx ) ); |
---|
| 598 | DTRACE_CABAC_VL( g_nSymbolCounter++ ) |
---|
| 599 | DTRACE_CABAC_T( "\tSplitFlag\n" ) |
---|
| 600 | return; |
---|
| 601 | } |
---|
| 602 | |
---|
| 603 | Void TEncSbac::codeTransformSubdivFlag( UInt uiSymbol, UInt uiCtx ) |
---|
| 604 | { |
---|
| 605 | m_pcBinIf->encodeBin( uiSymbol, m_cCUTransSubdivFlagSCModel.get( 0, 0, uiCtx ) ); |
---|
| 606 | DTRACE_CABAC_VL( g_nSymbolCounter++ ) |
---|
| 607 | DTRACE_CABAC_T( "\tparseTransformSubdivFlag()" ) |
---|
| 608 | DTRACE_CABAC_T( "\tsymbol=" ) |
---|
| 609 | DTRACE_CABAC_V( uiSymbol ) |
---|
| 610 | DTRACE_CABAC_T( "\tctx=" ) |
---|
| 611 | DTRACE_CABAC_V( uiCtx ) |
---|
| 612 | DTRACE_CABAC_T( "\n" ) |
---|
| 613 | } |
---|
| 614 | |
---|
| 615 | Void TEncSbac::codeIntraDirLumaAng( TComDataCU* pcCU, UInt absPartIdx, Bool isMultiple) |
---|
| 616 | { |
---|
| 617 | UInt dir[4],j; |
---|
| 618 | Int preds[4][3] = {{-1, -1, -1},{-1, -1, -1},{-1, -1, -1},{-1, -1, -1}}; |
---|
| 619 | Int predNum[4], predIdx[4] ={ -1,-1,-1,-1}; |
---|
| 620 | PartSize mode = pcCU->getPartitionSize( absPartIdx ); |
---|
| 621 | UInt partNum = isMultiple?(mode==SIZE_NxN?4:1):1; |
---|
| 622 | UInt partOffset = ( pcCU->getPic()->getNumPartInCU() >> ( pcCU->getDepth(absPartIdx) << 1 ) ) >> 2; |
---|
| 623 | for (j=0;j<partNum;j++) |
---|
| 624 | { |
---|
| 625 | dir[j] = pcCU->getLumaIntraDir( absPartIdx+partOffset*j ); |
---|
| 626 | predNum[j] = pcCU->getIntraDirLumaPredictor(absPartIdx+partOffset*j, preds[j]); |
---|
| 627 | for(UInt i = 0; i < predNum[j]; i++) |
---|
| 628 | { |
---|
| 629 | if(dir[j] == preds[j][i]) |
---|
| 630 | { |
---|
| 631 | predIdx[j] = i; |
---|
| 632 | } |
---|
| 633 | } |
---|
| 634 | m_pcBinIf->encodeBin((predIdx[j] != -1)? 1 : 0, m_cCUIntraPredSCModel.get( 0, 0, 0 ) ); |
---|
| 635 | } |
---|
| 636 | for (j=0;j<partNum;j++) |
---|
| 637 | { |
---|
| 638 | if(predIdx[j] != -1) |
---|
| 639 | { |
---|
| 640 | m_pcBinIf->encodeBinEP( predIdx[j] ? 1 : 0 ); |
---|
| 641 | if (predIdx[j]) |
---|
| 642 | { |
---|
| 643 | m_pcBinIf->encodeBinEP( predIdx[j]-1 ); |
---|
| 644 | } |
---|
| 645 | } |
---|
| 646 | else |
---|
| 647 | { |
---|
| 648 | if (preds[j][0] > preds[j][1]) |
---|
| 649 | { |
---|
| 650 | std::swap(preds[j][0], preds[j][1]); |
---|
| 651 | } |
---|
| 652 | if (preds[j][0] > preds[j][2]) |
---|
| 653 | { |
---|
| 654 | std::swap(preds[j][0], preds[j][2]); |
---|
| 655 | } |
---|
| 656 | if (preds[j][1] > preds[j][2]) |
---|
| 657 | { |
---|
| 658 | std::swap(preds[j][1], preds[j][2]); |
---|
| 659 | } |
---|
| 660 | for(Int i = (predNum[j] - 1); i >= 0; i--) |
---|
| 661 | { |
---|
| 662 | dir[j] = dir[j] > preds[j][i] ? dir[j] - 1 : dir[j]; |
---|
| 663 | } |
---|
| 664 | m_pcBinIf->encodeBinsEP( dir[j], 5 ); |
---|
| 665 | } |
---|
| 666 | } |
---|
| 667 | return; |
---|
| 668 | } |
---|
| 669 | |
---|
| 670 | Void TEncSbac::codeIntraDirChroma( TComDataCU* pcCU, UInt uiAbsPartIdx ) |
---|
| 671 | { |
---|
| 672 | UInt uiIntraDirChroma = pcCU->getChromaIntraDir( uiAbsPartIdx ); |
---|
| 673 | |
---|
| 674 | if( uiIntraDirChroma == DM_CHROMA_IDX ) |
---|
| 675 | { |
---|
| 676 | m_pcBinIf->encodeBin( 0, m_cCUChromaPredSCModel.get( 0, 0, 0 ) ); |
---|
| 677 | } |
---|
| 678 | else |
---|
| 679 | { |
---|
| 680 | UInt uiAllowedChromaDir[ NUM_CHROMA_MODE ]; |
---|
| 681 | pcCU->getAllowedChromaDir( uiAbsPartIdx, uiAllowedChromaDir ); |
---|
| 682 | |
---|
| 683 | for( Int i = 0; i < NUM_CHROMA_MODE - 1; i++ ) |
---|
| 684 | { |
---|
| 685 | if( uiIntraDirChroma == uiAllowedChromaDir[i] ) |
---|
| 686 | { |
---|
| 687 | uiIntraDirChroma = i; |
---|
| 688 | break; |
---|
| 689 | } |
---|
| 690 | } |
---|
| 691 | m_pcBinIf->encodeBin( 1, m_cCUChromaPredSCModel.get( 0, 0, 0 ) ); |
---|
| 692 | |
---|
| 693 | m_pcBinIf->encodeBinsEP( uiIntraDirChroma, 2 ); |
---|
| 694 | } |
---|
| 695 | return; |
---|
| 696 | } |
---|
| 697 | |
---|
| 698 | Void TEncSbac::codeInterDir( TComDataCU* pcCU, UInt uiAbsPartIdx ) |
---|
| 699 | { |
---|
| 700 | const UInt uiInterDir = pcCU->getInterDir( uiAbsPartIdx ) - 1; |
---|
| 701 | const UInt uiCtx = pcCU->getCtxInterDir( uiAbsPartIdx ); |
---|
| 702 | ContextModel *pCtx = m_cCUInterDirSCModel.get( 0 ); |
---|
| 703 | if (pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N || pcCU->getHeight(uiAbsPartIdx) != 8 ) |
---|
| 704 | { |
---|
| 705 | m_pcBinIf->encodeBin( uiInterDir == 2 ? 1 : 0, *( pCtx + uiCtx ) ); |
---|
| 706 | } |
---|
| 707 | if (uiInterDir < 2) |
---|
| 708 | { |
---|
| 709 | m_pcBinIf->encodeBin( uiInterDir, *( pCtx + 4 ) ); |
---|
| 710 | } |
---|
| 711 | return; |
---|
| 712 | } |
---|
| 713 | |
---|
| 714 | Void TEncSbac::codeRefFrmIdx( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList ) |
---|
| 715 | { |
---|
| 716 | { |
---|
| 717 | Int iRefFrame = pcCU->getCUMvField( eRefList )->getRefIdx( uiAbsPartIdx ); |
---|
| 718 | ContextModel *pCtx = m_cCURefPicSCModel.get( 0 ); |
---|
| 719 | m_pcBinIf->encodeBin( ( iRefFrame == 0 ? 0 : 1 ), *pCtx ); |
---|
| 720 | |
---|
| 721 | if( iRefFrame > 0 ) |
---|
| 722 | { |
---|
| 723 | UInt uiRefNum = pcCU->getSlice()->getNumRefIdx( eRefList ) - 2; |
---|
| 724 | pCtx++; |
---|
| 725 | iRefFrame--; |
---|
| 726 | for( UInt ui = 0; ui < uiRefNum; ++ui ) |
---|
| 727 | { |
---|
| 728 | const UInt uiSymbol = ui == iRefFrame ? 0 : 1; |
---|
| 729 | if( ui == 0 ) |
---|
| 730 | { |
---|
| 731 | m_pcBinIf->encodeBin( uiSymbol, *pCtx ); |
---|
| 732 | } |
---|
| 733 | else |
---|
| 734 | { |
---|
| 735 | m_pcBinIf->encodeBinEP( uiSymbol ); |
---|
| 736 | } |
---|
| 737 | if( uiSymbol == 0 ) |
---|
| 738 | { |
---|
| 739 | break; |
---|
| 740 | } |
---|
| 741 | } |
---|
| 742 | } |
---|
| 743 | } |
---|
| 744 | return; |
---|
| 745 | } |
---|
| 746 | |
---|
| 747 | Void TEncSbac::codeMvd( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList ) |
---|
| 748 | { |
---|
| 749 | if(pcCU->getSlice()->getMvdL1ZeroFlag() && eRefList == REF_PIC_LIST_1 && pcCU->getInterDir(uiAbsPartIdx)==3) |
---|
| 750 | { |
---|
| 751 | return; |
---|
| 752 | } |
---|
| 753 | |
---|
| 754 | const TComCUMvField* pcCUMvField = pcCU->getCUMvField( eRefList ); |
---|
| 755 | const Int iHor = pcCUMvField->getMvd( uiAbsPartIdx ).getHor(); |
---|
| 756 | const Int iVer = pcCUMvField->getMvd( uiAbsPartIdx ).getVer(); |
---|
| 757 | ContextModel* pCtx = m_cCUMvdSCModel.get( 0 ); |
---|
| 758 | |
---|
| 759 | m_pcBinIf->encodeBin( iHor != 0 ? 1 : 0, *pCtx ); |
---|
| 760 | m_pcBinIf->encodeBin( iVer != 0 ? 1 : 0, *pCtx ); |
---|
| 761 | |
---|
| 762 | const Bool bHorAbsGr0 = iHor != 0; |
---|
| 763 | const Bool bVerAbsGr0 = iVer != 0; |
---|
| 764 | const UInt uiHorAbs = 0 > iHor ? -iHor : iHor; |
---|
| 765 | const UInt uiVerAbs = 0 > iVer ? -iVer : iVer; |
---|
| 766 | pCtx++; |
---|
| 767 | |
---|
| 768 | if( bHorAbsGr0 ) |
---|
| 769 | { |
---|
| 770 | m_pcBinIf->encodeBin( uiHorAbs > 1 ? 1 : 0, *pCtx ); |
---|
| 771 | } |
---|
| 772 | |
---|
| 773 | if( bVerAbsGr0 ) |
---|
| 774 | { |
---|
| 775 | m_pcBinIf->encodeBin( uiVerAbs > 1 ? 1 : 0, *pCtx ); |
---|
| 776 | } |
---|
| 777 | |
---|
| 778 | if( bHorAbsGr0 ) |
---|
| 779 | { |
---|
| 780 | if( uiHorAbs > 1 ) |
---|
| 781 | { |
---|
| 782 | xWriteEpExGolomb( uiHorAbs-2, 1 ); |
---|
| 783 | } |
---|
| 784 | |
---|
| 785 | m_pcBinIf->encodeBinEP( 0 > iHor ? 1 : 0 ); |
---|
| 786 | } |
---|
| 787 | |
---|
| 788 | if( bVerAbsGr0 ) |
---|
| 789 | { |
---|
| 790 | if( uiVerAbs > 1 ) |
---|
| 791 | { |
---|
| 792 | xWriteEpExGolomb( uiVerAbs-2, 1 ); |
---|
| 793 | } |
---|
| 794 | |
---|
| 795 | m_pcBinIf->encodeBinEP( 0 > iVer ? 1 : 0 ); |
---|
| 796 | } |
---|
| 797 | |
---|
| 798 | return; |
---|
| 799 | } |
---|
| 800 | |
---|
| 801 | Void TEncSbac::codeDeltaQP( TComDataCU* pcCU, UInt uiAbsPartIdx ) |
---|
| 802 | { |
---|
| 803 | Int iDQp = pcCU->getQP( uiAbsPartIdx ) - pcCU->getRefQP( uiAbsPartIdx ); |
---|
| 804 | |
---|
[442] | 805 | #if REPN_FORMAT_IN_VPS |
---|
| 806 | Int qpBdOffsetY = pcCU->getSlice()->getQpBDOffsetY(); |
---|
| 807 | #else |
---|
[313] | 808 | Int qpBdOffsetY = pcCU->getSlice()->getSPS()->getQpBDOffsetY(); |
---|
[442] | 809 | #endif |
---|
[313] | 810 | iDQp = (iDQp + 78 + qpBdOffsetY + (qpBdOffsetY/2)) % (52 + qpBdOffsetY) - 26 - (qpBdOffsetY/2); |
---|
| 811 | |
---|
| 812 | UInt uiAbsDQp = (UInt)((iDQp > 0)? iDQp : (-iDQp)); |
---|
| 813 | UInt TUValue = min((Int)uiAbsDQp, CU_DQP_TU_CMAX); |
---|
| 814 | xWriteUnaryMaxSymbol( TUValue, &m_cCUDeltaQpSCModel.get( 0, 0, 0 ), 1, CU_DQP_TU_CMAX); |
---|
| 815 | if( uiAbsDQp >= CU_DQP_TU_CMAX ) |
---|
| 816 | { |
---|
| 817 | xWriteEpExGolomb( uiAbsDQp - CU_DQP_TU_CMAX, CU_DQP_EG_k ); |
---|
| 818 | } |
---|
| 819 | |
---|
| 820 | if ( uiAbsDQp > 0) |
---|
| 821 | { |
---|
| 822 | UInt uiSign = (iDQp > 0 ? 0 : 1); |
---|
| 823 | m_pcBinIf->encodeBinEP(uiSign); |
---|
| 824 | } |
---|
| 825 | |
---|
| 826 | return; |
---|
| 827 | } |
---|
| 828 | |
---|
| 829 | Void TEncSbac::codeQtCbf( TComDataCU* pcCU, UInt uiAbsPartIdx, TextType eType, UInt uiTrDepth ) |
---|
| 830 | { |
---|
| 831 | UInt uiCbf = pcCU->getCbf ( uiAbsPartIdx, eType, uiTrDepth ); |
---|
| 832 | UInt uiCtx = pcCU->getCtxQtCbf( eType, uiTrDepth ); |
---|
| 833 | m_pcBinIf->encodeBin( uiCbf , m_cCUQtCbfSCModel.get( 0, eType ? TEXT_CHROMA : eType, uiCtx ) ); |
---|
| 834 | DTRACE_CABAC_VL( g_nSymbolCounter++ ) |
---|
| 835 | DTRACE_CABAC_T( "\tparseQtCbf()" ) |
---|
| 836 | DTRACE_CABAC_T( "\tsymbol=" ) |
---|
| 837 | DTRACE_CABAC_V( uiCbf ) |
---|
| 838 | DTRACE_CABAC_T( "\tctx=" ) |
---|
| 839 | DTRACE_CABAC_V( uiCtx ) |
---|
| 840 | DTRACE_CABAC_T( "\tetype=" ) |
---|
| 841 | DTRACE_CABAC_V( eType ) |
---|
| 842 | DTRACE_CABAC_T( "\tuiAbsPartIdx=" ) |
---|
| 843 | DTRACE_CABAC_V( uiAbsPartIdx ) |
---|
| 844 | DTRACE_CABAC_T( "\n" ) |
---|
| 845 | } |
---|
| 846 | |
---|
| 847 | void TEncSbac::codeTransformSkipFlags (TComDataCU* pcCU, UInt uiAbsPartIdx, UInt width, UInt height, TextType eTType ) |
---|
| 848 | { |
---|
| 849 | if (pcCU->getCUTransquantBypass(uiAbsPartIdx)) |
---|
| 850 | { |
---|
| 851 | return; |
---|
| 852 | } |
---|
| 853 | if(width != 4 || height != 4) |
---|
| 854 | { |
---|
| 855 | return; |
---|
| 856 | } |
---|
| 857 | |
---|
| 858 | UInt useTransformSkip = pcCU->getTransformSkip( uiAbsPartIdx,eTType); |
---|
| 859 | m_pcBinIf->encodeBin( useTransformSkip, m_cTransformSkipSCModel.get( 0, eTType? TEXT_CHROMA: TEXT_LUMA, 0 ) ); |
---|
| 860 | DTRACE_CABAC_VL( g_nSymbolCounter++ ) |
---|
| 861 | DTRACE_CABAC_T("\tparseTransformSkip()"); |
---|
| 862 | DTRACE_CABAC_T( "\tsymbol=" ) |
---|
| 863 | DTRACE_CABAC_V( useTransformSkip ) |
---|
| 864 | DTRACE_CABAC_T( "\tAddr=" ) |
---|
| 865 | DTRACE_CABAC_V( pcCU->getAddr() ) |
---|
| 866 | DTRACE_CABAC_T( "\tetype=" ) |
---|
| 867 | DTRACE_CABAC_V( eTType ) |
---|
| 868 | DTRACE_CABAC_T( "\tuiAbsPartIdx=" ) |
---|
| 869 | DTRACE_CABAC_V( uiAbsPartIdx ) |
---|
| 870 | DTRACE_CABAC_T( "\n" ) |
---|
| 871 | } |
---|
| 872 | |
---|
| 873 | /** Code I_PCM information. |
---|
| 874 | * \param pcCU pointer to CU |
---|
| 875 | * \param uiAbsPartIdx CU index |
---|
| 876 | * \returns Void |
---|
| 877 | */ |
---|
| 878 | Void TEncSbac::codeIPCMInfo( TComDataCU* pcCU, UInt uiAbsPartIdx ) |
---|
| 879 | { |
---|
| 880 | UInt uiIPCM = (pcCU->getIPCMFlag(uiAbsPartIdx) == true)? 1 : 0; |
---|
| 881 | |
---|
| 882 | Bool writePCMSampleFlag = pcCU->getIPCMFlag(uiAbsPartIdx); |
---|
| 883 | |
---|
| 884 | m_pcBinIf->encodeBinTrm (uiIPCM); |
---|
| 885 | |
---|
| 886 | if (writePCMSampleFlag) |
---|
| 887 | { |
---|
| 888 | m_pcBinIf->encodePCMAlignBits(); |
---|
| 889 | |
---|
| 890 | UInt uiMinCoeffSize = pcCU->getPic()->getMinCUWidth()*pcCU->getPic()->getMinCUHeight(); |
---|
| 891 | UInt uiLumaOffset = uiMinCoeffSize*uiAbsPartIdx; |
---|
| 892 | UInt uiChromaOffset = uiLumaOffset>>2; |
---|
| 893 | Pel* piPCMSample; |
---|
| 894 | UInt uiWidth; |
---|
| 895 | UInt uiHeight; |
---|
| 896 | UInt uiSampleBits; |
---|
| 897 | UInt uiX, uiY; |
---|
| 898 | |
---|
| 899 | piPCMSample = pcCU->getPCMSampleY() + uiLumaOffset; |
---|
| 900 | uiWidth = pcCU->getWidth(uiAbsPartIdx); |
---|
| 901 | uiHeight = pcCU->getHeight(uiAbsPartIdx); |
---|
| 902 | uiSampleBits = pcCU->getSlice()->getSPS()->getPCMBitDepthLuma(); |
---|
| 903 | |
---|
| 904 | for(uiY = 0; uiY < uiHeight; uiY++) |
---|
| 905 | { |
---|
| 906 | for(uiX = 0; uiX < uiWidth; uiX++) |
---|
| 907 | { |
---|
| 908 | UInt uiSample = piPCMSample[uiX]; |
---|
| 909 | |
---|
| 910 | m_pcBinIf->xWritePCMCode(uiSample, uiSampleBits); |
---|
| 911 | } |
---|
| 912 | piPCMSample += uiWidth; |
---|
| 913 | } |
---|
| 914 | |
---|
[494] | 915 | #if AUXILIARY_PICTURES |
---|
| 916 | if (pcCU->getSlice()->getChromaFormatIdc() != CHROMA_400) |
---|
| 917 | { |
---|
| 918 | #endif |
---|
[313] | 919 | piPCMSample = pcCU->getPCMSampleCb() + uiChromaOffset; |
---|
| 920 | uiWidth = pcCU->getWidth(uiAbsPartIdx)/2; |
---|
| 921 | uiHeight = pcCU->getHeight(uiAbsPartIdx)/2; |
---|
| 922 | uiSampleBits = pcCU->getSlice()->getSPS()->getPCMBitDepthChroma(); |
---|
| 923 | |
---|
| 924 | for(uiY = 0; uiY < uiHeight; uiY++) |
---|
| 925 | { |
---|
| 926 | for(uiX = 0; uiX < uiWidth; uiX++) |
---|
| 927 | { |
---|
| 928 | UInt uiSample = piPCMSample[uiX]; |
---|
| 929 | |
---|
| 930 | m_pcBinIf->xWritePCMCode(uiSample, uiSampleBits); |
---|
| 931 | } |
---|
| 932 | piPCMSample += uiWidth; |
---|
| 933 | } |
---|
| 934 | |
---|
| 935 | piPCMSample = pcCU->getPCMSampleCr() + uiChromaOffset; |
---|
| 936 | uiWidth = pcCU->getWidth(uiAbsPartIdx)/2; |
---|
| 937 | uiHeight = pcCU->getHeight(uiAbsPartIdx)/2; |
---|
| 938 | uiSampleBits = pcCU->getSlice()->getSPS()->getPCMBitDepthChroma(); |
---|
| 939 | |
---|
| 940 | for(uiY = 0; uiY < uiHeight; uiY++) |
---|
| 941 | { |
---|
| 942 | for(uiX = 0; uiX < uiWidth; uiX++) |
---|
| 943 | { |
---|
| 944 | UInt uiSample = piPCMSample[uiX]; |
---|
| 945 | |
---|
| 946 | m_pcBinIf->xWritePCMCode(uiSample, uiSampleBits); |
---|
| 947 | } |
---|
| 948 | piPCMSample += uiWidth; |
---|
| 949 | } |
---|
[494] | 950 | #if AUXILIARY_PICTURES |
---|
| 951 | } |
---|
| 952 | #endif |
---|
[313] | 953 | m_pcBinIf->resetBac(); |
---|
| 954 | } |
---|
| 955 | } |
---|
| 956 | |
---|
| 957 | Void TEncSbac::codeQtRootCbf( TComDataCU* pcCU, UInt uiAbsPartIdx ) |
---|
| 958 | { |
---|
| 959 | UInt uiCbf = pcCU->getQtRootCbf( uiAbsPartIdx ); |
---|
| 960 | UInt uiCtx = 0; |
---|
| 961 | m_pcBinIf->encodeBin( uiCbf , m_cCUQtRootCbfSCModel.get( 0, 0, uiCtx ) ); |
---|
| 962 | DTRACE_CABAC_VL( g_nSymbolCounter++ ) |
---|
| 963 | DTRACE_CABAC_T( "\tparseQtRootCbf()" ) |
---|
| 964 | DTRACE_CABAC_T( "\tsymbol=" ) |
---|
| 965 | DTRACE_CABAC_V( uiCbf ) |
---|
| 966 | DTRACE_CABAC_T( "\tctx=" ) |
---|
| 967 | DTRACE_CABAC_V( uiCtx ) |
---|
| 968 | DTRACE_CABAC_T( "\tuiAbsPartIdx=" ) |
---|
| 969 | DTRACE_CABAC_V( uiAbsPartIdx ) |
---|
| 970 | DTRACE_CABAC_T( "\n" ) |
---|
| 971 | } |
---|
| 972 | |
---|
| 973 | Void TEncSbac::codeQtCbfZero( TComDataCU* pcCU, TextType eType, UInt uiTrDepth ) |
---|
| 974 | { |
---|
| 975 | // this function is only used to estimate the bits when cbf is 0 |
---|
| 976 | // and will never be called when writing the bistream. do not need to write log |
---|
| 977 | UInt uiCbf = 0; |
---|
| 978 | UInt uiCtx = pcCU->getCtxQtCbf( eType, uiTrDepth ); |
---|
| 979 | m_pcBinIf->encodeBin( uiCbf , m_cCUQtCbfSCModel.get( 0, eType ? TEXT_CHROMA : eType, uiCtx ) ); |
---|
| 980 | } |
---|
| 981 | |
---|
| 982 | Void TEncSbac::codeQtRootCbfZero( TComDataCU* pcCU ) |
---|
| 983 | { |
---|
| 984 | // this function is only used to estimate the bits when cbf is 0 |
---|
| 985 | // and will never be called when writing the bistream. do not need to write log |
---|
| 986 | UInt uiCbf = 0; |
---|
| 987 | UInt uiCtx = 0; |
---|
| 988 | m_pcBinIf->encodeBin( uiCbf , m_cCUQtRootCbfSCModel.get( 0, 0, uiCtx ) ); |
---|
| 989 | } |
---|
| 990 | |
---|
| 991 | /** Encode (X,Y) position of the last significant coefficient |
---|
| 992 | * \param uiPosX X component of last coefficient |
---|
| 993 | * \param uiPosY Y component of last coefficient |
---|
| 994 | * \param width Block width |
---|
| 995 | * \param height Block height |
---|
| 996 | * \param eTType plane type / luminance or chrominance |
---|
| 997 | * \param uiScanIdx scan type (zig-zag, hor, ver) |
---|
| 998 | * This method encodes the X and Y component within a block of the last significant coefficient. |
---|
| 999 | */ |
---|
| 1000 | Void TEncSbac::codeLastSignificantXY( UInt uiPosX, UInt uiPosY, Int width, Int height, TextType eTType, UInt uiScanIdx ) |
---|
| 1001 | { |
---|
| 1002 | // swap |
---|
| 1003 | if( uiScanIdx == SCAN_VER ) |
---|
| 1004 | { |
---|
| 1005 | swap( uiPosX, uiPosY ); |
---|
| 1006 | } |
---|
| 1007 | |
---|
| 1008 | UInt uiCtxLast; |
---|
| 1009 | ContextModel *pCtxX = m_cCuCtxLastX.get( 0, eTType ); |
---|
| 1010 | ContextModel *pCtxY = m_cCuCtxLastY.get( 0, eTType ); |
---|
| 1011 | UInt uiGroupIdxX = g_uiGroupIdx[ uiPosX ]; |
---|
| 1012 | UInt uiGroupIdxY = g_uiGroupIdx[ uiPosY ]; |
---|
| 1013 | |
---|
| 1014 | |
---|
| 1015 | Int blkSizeOffsetX, blkSizeOffsetY, shiftX, shiftY; |
---|
| 1016 | blkSizeOffsetX = eTType ? 0: (g_aucConvertToBit[ width ] *3 + ((g_aucConvertToBit[ width ] +1)>>2)); |
---|
| 1017 | blkSizeOffsetY = eTType ? 0: (g_aucConvertToBit[ height ]*3 + ((g_aucConvertToBit[ height ]+1)>>2)); |
---|
| 1018 | shiftX= eTType ? g_aucConvertToBit[ width ] :((g_aucConvertToBit[ width ]+3)>>2); |
---|
| 1019 | shiftY= eTType ? g_aucConvertToBit[ height ] :((g_aucConvertToBit[ height ]+3)>>2); |
---|
| 1020 | // posX |
---|
| 1021 | for( uiCtxLast = 0; uiCtxLast < uiGroupIdxX; uiCtxLast++ ) |
---|
| 1022 | { |
---|
| 1023 | m_pcBinIf->encodeBin( 1, *( pCtxX + blkSizeOffsetX + (uiCtxLast >>shiftX) ) ); |
---|
| 1024 | } |
---|
| 1025 | if( uiGroupIdxX < g_uiGroupIdx[ width - 1 ]) |
---|
| 1026 | { |
---|
| 1027 | m_pcBinIf->encodeBin( 0, *( pCtxX + blkSizeOffsetX + (uiCtxLast >>shiftX) ) ); |
---|
| 1028 | } |
---|
| 1029 | |
---|
| 1030 | // posY |
---|
| 1031 | for( uiCtxLast = 0; uiCtxLast < uiGroupIdxY; uiCtxLast++ ) |
---|
| 1032 | { |
---|
| 1033 | m_pcBinIf->encodeBin( 1, *( pCtxY + blkSizeOffsetY + (uiCtxLast >>shiftY) ) ); |
---|
| 1034 | } |
---|
| 1035 | if( uiGroupIdxY < g_uiGroupIdx[ height - 1 ]) |
---|
| 1036 | { |
---|
| 1037 | m_pcBinIf->encodeBin( 0, *( pCtxY + blkSizeOffsetY + (uiCtxLast >>shiftY) ) ); |
---|
| 1038 | } |
---|
| 1039 | if ( uiGroupIdxX > 3 ) |
---|
| 1040 | { |
---|
| 1041 | UInt uiCount = ( uiGroupIdxX - 2 ) >> 1; |
---|
| 1042 | uiPosX = uiPosX - g_uiMinInGroup[ uiGroupIdxX ]; |
---|
| 1043 | for (Int i = uiCount - 1 ; i >= 0; i-- ) |
---|
| 1044 | { |
---|
| 1045 | m_pcBinIf->encodeBinEP( ( uiPosX >> i ) & 1 ); |
---|
| 1046 | } |
---|
| 1047 | } |
---|
| 1048 | if ( uiGroupIdxY > 3 ) |
---|
| 1049 | { |
---|
| 1050 | UInt uiCount = ( uiGroupIdxY - 2 ) >> 1; |
---|
| 1051 | uiPosY = uiPosY - g_uiMinInGroup[ uiGroupIdxY ]; |
---|
| 1052 | for ( Int i = uiCount - 1 ; i >= 0; i-- ) |
---|
| 1053 | { |
---|
| 1054 | m_pcBinIf->encodeBinEP( ( uiPosY >> i ) & 1 ); |
---|
| 1055 | } |
---|
| 1056 | } |
---|
| 1057 | } |
---|
| 1058 | |
---|
| 1059 | Void TEncSbac::codeCoeffNxN( TComDataCU* pcCU, TCoeff* pcCoef, UInt uiAbsPartIdx, UInt uiWidth, UInt uiHeight, UInt uiDepth, TextType eTType ) |
---|
| 1060 | { |
---|
| 1061 | DTRACE_CABAC_VL( g_nSymbolCounter++ ) |
---|
| 1062 | DTRACE_CABAC_T( "\tparseCoeffNxN()\teType=" ) |
---|
| 1063 | DTRACE_CABAC_V( eTType ) |
---|
| 1064 | DTRACE_CABAC_T( "\twidth=" ) |
---|
| 1065 | DTRACE_CABAC_V( uiWidth ) |
---|
| 1066 | DTRACE_CABAC_T( "\theight=" ) |
---|
| 1067 | DTRACE_CABAC_V( uiHeight ) |
---|
| 1068 | DTRACE_CABAC_T( "\tdepth=" ) |
---|
| 1069 | DTRACE_CABAC_V( uiDepth ) |
---|
| 1070 | DTRACE_CABAC_T( "\tabspartidx=" ) |
---|
| 1071 | DTRACE_CABAC_V( uiAbsPartIdx ) |
---|
| 1072 | DTRACE_CABAC_T( "\ttoCU-X=" ) |
---|
| 1073 | DTRACE_CABAC_V( pcCU->getCUPelX() ) |
---|
| 1074 | DTRACE_CABAC_T( "\ttoCU-Y=" ) |
---|
| 1075 | DTRACE_CABAC_V( pcCU->getCUPelY() ) |
---|
| 1076 | DTRACE_CABAC_T( "\tCU-addr=" ) |
---|
| 1077 | DTRACE_CABAC_V( pcCU->getAddr() ) |
---|
| 1078 | DTRACE_CABAC_T( "\tinCU-X=" ) |
---|
| 1079 | DTRACE_CABAC_V( g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ] ) |
---|
| 1080 | DTRACE_CABAC_T( "\tinCU-Y=" ) |
---|
| 1081 | DTRACE_CABAC_V( g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ] ) |
---|
| 1082 | DTRACE_CABAC_T( "\tpredmode=" ) |
---|
| 1083 | DTRACE_CABAC_V( pcCU->getPredictionMode( uiAbsPartIdx ) ) |
---|
| 1084 | DTRACE_CABAC_T( "\n" ) |
---|
| 1085 | |
---|
| 1086 | if( uiWidth > m_pcSlice->getSPS()->getMaxTrSize() ) |
---|
| 1087 | { |
---|
| 1088 | uiWidth = m_pcSlice->getSPS()->getMaxTrSize(); |
---|
| 1089 | uiHeight = m_pcSlice->getSPS()->getMaxTrSize(); |
---|
| 1090 | } |
---|
| 1091 | |
---|
| 1092 | UInt uiNumSig = 0; |
---|
| 1093 | |
---|
| 1094 | // compute number of significant coefficients |
---|
| 1095 | uiNumSig = TEncEntropy::countNonZeroCoeffs(pcCoef, uiWidth * uiHeight); |
---|
| 1096 | |
---|
| 1097 | if ( uiNumSig == 0 ) |
---|
| 1098 | return; |
---|
| 1099 | if(pcCU->getSlice()->getPPS()->getUseTransformSkip()) |
---|
| 1100 | { |
---|
| 1101 | codeTransformSkipFlags( pcCU,uiAbsPartIdx, uiWidth, uiHeight, eTType ); |
---|
| 1102 | } |
---|
| 1103 | eTType = eTType == TEXT_LUMA ? TEXT_LUMA : ( eTType == TEXT_NONE ? TEXT_NONE : TEXT_CHROMA ); |
---|
| 1104 | |
---|
| 1105 | //----- encode significance map ----- |
---|
| 1106 | const UInt uiLog2BlockSize = g_aucConvertToBit[ uiWidth ] + 2; |
---|
| 1107 | UInt uiScanIdx = pcCU->getCoefScanIdx(uiAbsPartIdx, uiWidth, eTType==TEXT_LUMA, pcCU->isIntra(uiAbsPartIdx)); |
---|
| 1108 | const UInt *scan = g_auiSigLastScan[ uiScanIdx ][ uiLog2BlockSize - 1 ]; |
---|
| 1109 | |
---|
| 1110 | Bool beValid; |
---|
| 1111 | if (pcCU->getCUTransquantBypass(uiAbsPartIdx)) |
---|
| 1112 | { |
---|
| 1113 | beValid = false; |
---|
| 1114 | } |
---|
| 1115 | else |
---|
| 1116 | { |
---|
| 1117 | beValid = pcCU->getSlice()->getPPS()->getSignHideFlag() > 0; |
---|
| 1118 | } |
---|
| 1119 | |
---|
| 1120 | // Find position of last coefficient |
---|
| 1121 | Int scanPosLast = -1; |
---|
| 1122 | Int posLast; |
---|
| 1123 | |
---|
| 1124 | const UInt * scanCG; |
---|
| 1125 | { |
---|
| 1126 | scanCG = g_auiSigLastScan[ uiScanIdx ][ uiLog2BlockSize > 3 ? uiLog2BlockSize-2-1 : 0 ]; |
---|
| 1127 | if( uiLog2BlockSize == 3 ) |
---|
| 1128 | { |
---|
| 1129 | scanCG = g_sigLastScan8x8[ uiScanIdx ]; |
---|
| 1130 | } |
---|
| 1131 | else if( uiLog2BlockSize == 5 ) |
---|
| 1132 | { |
---|
| 1133 | scanCG = g_sigLastScanCG32x32; |
---|
| 1134 | } |
---|
| 1135 | } |
---|
| 1136 | UInt uiSigCoeffGroupFlag[ MLS_GRP_NUM ]; |
---|
| 1137 | static const UInt uiShift = MLS_CG_SIZE >> 1; |
---|
| 1138 | const UInt uiNumBlkSide = uiWidth >> uiShift; |
---|
| 1139 | |
---|
| 1140 | ::memset( uiSigCoeffGroupFlag, 0, sizeof(UInt) * MLS_GRP_NUM ); |
---|
| 1141 | |
---|
| 1142 | do |
---|
| 1143 | { |
---|
| 1144 | posLast = scan[ ++scanPosLast ]; |
---|
| 1145 | |
---|
| 1146 | // get L1 sig map |
---|
| 1147 | UInt uiPosY = posLast >> uiLog2BlockSize; |
---|
| 1148 | UInt uiPosX = posLast - ( uiPosY << uiLog2BlockSize ); |
---|
| 1149 | UInt uiBlkIdx = uiNumBlkSide * (uiPosY >> uiShift) + (uiPosX >> uiShift); |
---|
| 1150 | if( pcCoef[ posLast ] ) |
---|
| 1151 | { |
---|
| 1152 | uiSigCoeffGroupFlag[ uiBlkIdx ] = 1; |
---|
| 1153 | } |
---|
| 1154 | |
---|
| 1155 | uiNumSig -= ( pcCoef[ posLast ] != 0 ); |
---|
| 1156 | } |
---|
| 1157 | while ( uiNumSig > 0 ); |
---|
| 1158 | |
---|
| 1159 | // Code position of last coefficient |
---|
| 1160 | Int posLastY = posLast >> uiLog2BlockSize; |
---|
| 1161 | Int posLastX = posLast - ( posLastY << uiLog2BlockSize ); |
---|
| 1162 | codeLastSignificantXY(posLastX, posLastY, uiWidth, uiHeight, eTType, uiScanIdx); |
---|
| 1163 | |
---|
| 1164 | //===== code significance flag ===== |
---|
| 1165 | ContextModel * const baseCoeffGroupCtx = m_cCUSigCoeffGroupSCModel.get( 0, eTType ); |
---|
| 1166 | ContextModel * const baseCtx = (eTType==TEXT_LUMA) ? m_cCUSigSCModel.get( 0, 0 ) : m_cCUSigSCModel.get( 0, 0 ) + NUM_SIG_FLAG_CTX_LUMA; |
---|
| 1167 | |
---|
| 1168 | |
---|
| 1169 | const Int iLastScanSet = scanPosLast >> LOG2_SCAN_SET_SIZE; |
---|
| 1170 | UInt c1 = 1; |
---|
| 1171 | UInt uiGoRiceParam = 0; |
---|
| 1172 | Int iScanPosSig = scanPosLast; |
---|
| 1173 | |
---|
| 1174 | for( Int iSubSet = iLastScanSet; iSubSet >= 0; iSubSet-- ) |
---|
| 1175 | { |
---|
| 1176 | Int numNonZero = 0; |
---|
| 1177 | Int iSubPos = iSubSet << LOG2_SCAN_SET_SIZE; |
---|
| 1178 | uiGoRiceParam = 0; |
---|
| 1179 | Int absCoeff[16]; |
---|
| 1180 | UInt coeffSigns = 0; |
---|
| 1181 | |
---|
| 1182 | Int lastNZPosInCG = -1, firstNZPosInCG = SCAN_SET_SIZE; |
---|
| 1183 | |
---|
| 1184 | if( iScanPosSig == scanPosLast ) |
---|
| 1185 | { |
---|
| 1186 | absCoeff[ 0 ] = abs( pcCoef[ posLast ] ); |
---|
| 1187 | coeffSigns = ( pcCoef[ posLast ] < 0 ); |
---|
| 1188 | numNonZero = 1; |
---|
| 1189 | lastNZPosInCG = iScanPosSig; |
---|
| 1190 | firstNZPosInCG = iScanPosSig; |
---|
| 1191 | iScanPosSig--; |
---|
| 1192 | } |
---|
| 1193 | |
---|
| 1194 | // encode significant_coeffgroup_flag |
---|
| 1195 | Int iCGBlkPos = scanCG[ iSubSet ]; |
---|
| 1196 | Int iCGPosY = iCGBlkPos / uiNumBlkSide; |
---|
| 1197 | Int iCGPosX = iCGBlkPos - (iCGPosY * uiNumBlkSide); |
---|
| 1198 | if( iSubSet == iLastScanSet || iSubSet == 0) |
---|
| 1199 | { |
---|
| 1200 | uiSigCoeffGroupFlag[ iCGBlkPos ] = 1; |
---|
| 1201 | } |
---|
| 1202 | else |
---|
| 1203 | { |
---|
| 1204 | UInt uiSigCoeffGroup = (uiSigCoeffGroupFlag[ iCGBlkPos ] != 0); |
---|
| 1205 | UInt uiCtxSig = TComTrQuant::getSigCoeffGroupCtxInc( uiSigCoeffGroupFlag, iCGPosX, iCGPosY, uiWidth, uiHeight ); |
---|
| 1206 | m_pcBinIf->encodeBin( uiSigCoeffGroup, baseCoeffGroupCtx[ uiCtxSig ] ); |
---|
| 1207 | } |
---|
| 1208 | |
---|
| 1209 | // encode significant_coeff_flag |
---|
| 1210 | if( uiSigCoeffGroupFlag[ iCGBlkPos ] ) |
---|
| 1211 | { |
---|
| 1212 | Int patternSigCtx = TComTrQuant::calcPatternSigCtx( uiSigCoeffGroupFlag, iCGPosX, iCGPosY, uiWidth, uiHeight ); |
---|
| 1213 | UInt uiBlkPos, uiPosY, uiPosX, uiSig, uiCtxSig; |
---|
| 1214 | for( ; iScanPosSig >= iSubPos; iScanPosSig-- ) |
---|
| 1215 | { |
---|
| 1216 | uiBlkPos = scan[ iScanPosSig ]; |
---|
| 1217 | uiPosY = uiBlkPos >> uiLog2BlockSize; |
---|
| 1218 | uiPosX = uiBlkPos - ( uiPosY << uiLog2BlockSize ); |
---|
| 1219 | uiSig = (pcCoef[ uiBlkPos ] != 0); |
---|
| 1220 | if( iScanPosSig > iSubPos || iSubSet == 0 || numNonZero ) |
---|
| 1221 | { |
---|
| 1222 | uiCtxSig = TComTrQuant::getSigCtxInc( patternSigCtx, uiScanIdx, uiPosX, uiPosY, uiLog2BlockSize, eTType ); |
---|
| 1223 | m_pcBinIf->encodeBin( uiSig, baseCtx[ uiCtxSig ] ); |
---|
| 1224 | } |
---|
| 1225 | if( uiSig ) |
---|
| 1226 | { |
---|
| 1227 | absCoeff[ numNonZero ] = abs( pcCoef[ uiBlkPos ] ); |
---|
| 1228 | coeffSigns = 2 * coeffSigns + ( pcCoef[ uiBlkPos ] < 0 ); |
---|
| 1229 | numNonZero++; |
---|
| 1230 | if( lastNZPosInCG == -1 ) |
---|
| 1231 | { |
---|
| 1232 | lastNZPosInCG = iScanPosSig; |
---|
| 1233 | } |
---|
| 1234 | firstNZPosInCG = iScanPosSig; |
---|
| 1235 | } |
---|
| 1236 | } |
---|
| 1237 | } |
---|
| 1238 | else |
---|
| 1239 | { |
---|
| 1240 | iScanPosSig = iSubPos - 1; |
---|
| 1241 | } |
---|
| 1242 | |
---|
| 1243 | if( numNonZero > 0 ) |
---|
| 1244 | { |
---|
| 1245 | Bool signHidden = ( lastNZPosInCG - firstNZPosInCG >= SBH_THRESHOLD ); |
---|
| 1246 | UInt uiCtxSet = (iSubSet > 0 && eTType==TEXT_LUMA) ? 2 : 0; |
---|
| 1247 | |
---|
| 1248 | if( c1 == 0 ) |
---|
| 1249 | { |
---|
| 1250 | uiCtxSet++; |
---|
| 1251 | } |
---|
| 1252 | c1 = 1; |
---|
| 1253 | ContextModel *baseCtxMod = ( eTType==TEXT_LUMA ) ? m_cCUOneSCModel.get( 0, 0 ) + 4 * uiCtxSet : m_cCUOneSCModel.get( 0, 0 ) + NUM_ONE_FLAG_CTX_LUMA + 4 * uiCtxSet; |
---|
| 1254 | |
---|
| 1255 | Int numC1Flag = min(numNonZero, C1FLAG_NUMBER); |
---|
| 1256 | Int firstC2FlagIdx = -1; |
---|
| 1257 | for( Int idx = 0; idx < numC1Flag; idx++ ) |
---|
| 1258 | { |
---|
| 1259 | UInt uiSymbol = absCoeff[ idx ] > 1; |
---|
| 1260 | m_pcBinIf->encodeBin( uiSymbol, baseCtxMod[c1] ); |
---|
| 1261 | if( uiSymbol ) |
---|
| 1262 | { |
---|
| 1263 | c1 = 0; |
---|
| 1264 | |
---|
| 1265 | if (firstC2FlagIdx == -1) |
---|
| 1266 | { |
---|
| 1267 | firstC2FlagIdx = idx; |
---|
| 1268 | } |
---|
| 1269 | } |
---|
| 1270 | else if( (c1 < 3) && (c1 > 0) ) |
---|
| 1271 | { |
---|
| 1272 | c1++; |
---|
| 1273 | } |
---|
| 1274 | } |
---|
| 1275 | |
---|
| 1276 | if (c1 == 0) |
---|
| 1277 | { |
---|
| 1278 | |
---|
| 1279 | baseCtxMod = ( eTType==TEXT_LUMA ) ? m_cCUAbsSCModel.get( 0, 0 ) + uiCtxSet : m_cCUAbsSCModel.get( 0, 0 ) + NUM_ABS_FLAG_CTX_LUMA + uiCtxSet; |
---|
| 1280 | if ( firstC2FlagIdx != -1) |
---|
| 1281 | { |
---|
| 1282 | UInt symbol = absCoeff[ firstC2FlagIdx ] > 2; |
---|
| 1283 | m_pcBinIf->encodeBin( symbol, baseCtxMod[0] ); |
---|
| 1284 | } |
---|
| 1285 | } |
---|
| 1286 | |
---|
| 1287 | if( beValid && signHidden ) |
---|
| 1288 | { |
---|
| 1289 | m_pcBinIf->encodeBinsEP( (coeffSigns >> 1), numNonZero-1 ); |
---|
| 1290 | } |
---|
| 1291 | else |
---|
| 1292 | { |
---|
| 1293 | m_pcBinIf->encodeBinsEP( coeffSigns, numNonZero ); |
---|
| 1294 | } |
---|
| 1295 | |
---|
| 1296 | Int iFirstCoeff2 = 1; |
---|
| 1297 | if (c1 == 0 || numNonZero > C1FLAG_NUMBER) |
---|
| 1298 | { |
---|
| 1299 | for ( Int idx = 0; idx < numNonZero; idx++ ) |
---|
| 1300 | { |
---|
| 1301 | UInt baseLevel = (idx < C1FLAG_NUMBER)? (2 + iFirstCoeff2 ) : 1; |
---|
| 1302 | |
---|
| 1303 | if( absCoeff[ idx ] >= baseLevel) |
---|
| 1304 | { |
---|
| 1305 | xWriteCoefRemainExGolomb( absCoeff[ idx ] - baseLevel, uiGoRiceParam ); |
---|
| 1306 | if(absCoeff[idx] > 3*(1<<uiGoRiceParam)) |
---|
| 1307 | { |
---|
| 1308 | uiGoRiceParam = min<UInt>(uiGoRiceParam+ 1, 4); |
---|
| 1309 | } |
---|
| 1310 | } |
---|
| 1311 | if(absCoeff[ idx ] >= 2) |
---|
| 1312 | { |
---|
| 1313 | iFirstCoeff2 = 0; |
---|
| 1314 | } |
---|
| 1315 | } |
---|
| 1316 | } |
---|
| 1317 | } |
---|
| 1318 | } |
---|
| 1319 | |
---|
| 1320 | return; |
---|
| 1321 | } |
---|
| 1322 | |
---|
| 1323 | /** code SAO offset sign |
---|
| 1324 | * \param code sign value |
---|
| 1325 | */ |
---|
| 1326 | Void TEncSbac::codeSAOSign( UInt code ) |
---|
| 1327 | { |
---|
| 1328 | m_pcBinIf->encodeBinEP( code ); |
---|
| 1329 | } |
---|
| 1330 | |
---|
| 1331 | Void TEncSbac::codeSaoMaxUvlc ( UInt code, UInt maxSymbol ) |
---|
| 1332 | { |
---|
| 1333 | if (maxSymbol == 0) |
---|
| 1334 | { |
---|
| 1335 | return; |
---|
| 1336 | } |
---|
| 1337 | |
---|
| 1338 | Int i; |
---|
| 1339 | Bool bCodeLast = ( maxSymbol > code ); |
---|
| 1340 | |
---|
| 1341 | if ( code == 0 ) |
---|
| 1342 | { |
---|
| 1343 | m_pcBinIf->encodeBinEP( 0 ); |
---|
| 1344 | } |
---|
| 1345 | else |
---|
| 1346 | { |
---|
| 1347 | m_pcBinIf->encodeBinEP( 1 ); |
---|
| 1348 | for ( i=0; i<code-1; i++ ) |
---|
| 1349 | { |
---|
| 1350 | m_pcBinIf->encodeBinEP( 1 ); |
---|
| 1351 | } |
---|
| 1352 | if( bCodeLast ) |
---|
| 1353 | { |
---|
| 1354 | m_pcBinIf->encodeBinEP( 0 ); |
---|
| 1355 | } |
---|
| 1356 | } |
---|
| 1357 | } |
---|
| 1358 | |
---|
| 1359 | |
---|
| 1360 | /** Code SAO EO class or BO band position |
---|
| 1361 | * \param uiLength |
---|
| 1362 | * \param uiCode |
---|
| 1363 | */ |
---|
| 1364 | Void TEncSbac::codeSaoUflc ( UInt uiLength, UInt uiCode ) |
---|
| 1365 | { |
---|
| 1366 | m_pcBinIf->encodeBinsEP ( uiCode, uiLength ); |
---|
| 1367 | } |
---|
| 1368 | /** Code SAO merge flags |
---|
| 1369 | * \param uiCode |
---|
| 1370 | * \param uiCompIdx |
---|
| 1371 | */ |
---|
| 1372 | Void TEncSbac::codeSaoMerge ( UInt uiCode ) |
---|
| 1373 | { |
---|
| 1374 | if (uiCode == 0) |
---|
| 1375 | { |
---|
| 1376 | m_pcBinIf->encodeBin(0, m_cSaoMergeSCModel.get( 0, 0, 0 )); |
---|
| 1377 | } |
---|
| 1378 | else |
---|
| 1379 | { |
---|
| 1380 | m_pcBinIf->encodeBin(1, m_cSaoMergeSCModel.get( 0, 0, 0 )); |
---|
| 1381 | } |
---|
| 1382 | } |
---|
| 1383 | /** Code SAO type index |
---|
| 1384 | * \param uiCode |
---|
| 1385 | */ |
---|
| 1386 | Void TEncSbac::codeSaoTypeIdx ( UInt uiCode) |
---|
| 1387 | { |
---|
| 1388 | if (uiCode == 0) |
---|
| 1389 | { |
---|
| 1390 | m_pcBinIf->encodeBin( 0, m_cSaoTypeIdxSCModel.get( 0, 0, 0 ) ); |
---|
| 1391 | } |
---|
| 1392 | else |
---|
| 1393 | { |
---|
| 1394 | m_pcBinIf->encodeBin( 1, m_cSaoTypeIdxSCModel.get( 0, 0, 0 ) ); |
---|
[532] | 1395 | #if HM_CLEANUP_SAO |
---|
| 1396 | m_pcBinIf->encodeBinEP( uiCode == 1 ? 0 : 1 ); |
---|
| 1397 | #else |
---|
[313] | 1398 | m_pcBinIf->encodeBinEP( uiCode <= 4 ? 1 : 0 ); |
---|
[532] | 1399 | #endif |
---|
[313] | 1400 | } |
---|
| 1401 | } |
---|
| 1402 | /*! |
---|
| 1403 | **************************************************************************** |
---|
| 1404 | * \brief |
---|
| 1405 | * estimate bit cost for CBP, significant map and significant coefficients |
---|
| 1406 | **************************************************************************** |
---|
| 1407 | */ |
---|
| 1408 | Void TEncSbac::estBit( estBitsSbacStruct* pcEstBitsSbac, Int width, Int height, TextType eTType ) |
---|
| 1409 | { |
---|
| 1410 | estCBFBit( pcEstBitsSbac ); |
---|
| 1411 | |
---|
| 1412 | estSignificantCoeffGroupMapBit( pcEstBitsSbac, eTType ); |
---|
| 1413 | |
---|
| 1414 | // encode significance map |
---|
| 1415 | estSignificantMapBit( pcEstBitsSbac, width, height, eTType ); |
---|
| 1416 | |
---|
| 1417 | // encode significant coefficients |
---|
| 1418 | estSignificantCoefficientsBit( pcEstBitsSbac, eTType ); |
---|
| 1419 | } |
---|
| 1420 | |
---|
| 1421 | /*! |
---|
| 1422 | **************************************************************************** |
---|
| 1423 | * \brief |
---|
| 1424 | * estimate bit cost for each CBP bit |
---|
| 1425 | **************************************************************************** |
---|
| 1426 | */ |
---|
| 1427 | Void TEncSbac::estCBFBit( estBitsSbacStruct* pcEstBitsSbac ) |
---|
| 1428 | { |
---|
| 1429 | ContextModel *pCtx = m_cCUQtCbfSCModel.get( 0 ); |
---|
| 1430 | |
---|
| 1431 | for( UInt uiCtxInc = 0; uiCtxInc < 3*NUM_QT_CBF_CTX; uiCtxInc++ ) |
---|
| 1432 | { |
---|
| 1433 | pcEstBitsSbac->blockCbpBits[ uiCtxInc ][ 0 ] = pCtx[ uiCtxInc ].getEntropyBits( 0 ); |
---|
| 1434 | pcEstBitsSbac->blockCbpBits[ uiCtxInc ][ 1 ] = pCtx[ uiCtxInc ].getEntropyBits( 1 ); |
---|
| 1435 | } |
---|
| 1436 | |
---|
| 1437 | pCtx = m_cCUQtRootCbfSCModel.get( 0 ); |
---|
| 1438 | |
---|
| 1439 | for( UInt uiCtxInc = 0; uiCtxInc < 4; uiCtxInc++ ) |
---|
| 1440 | { |
---|
| 1441 | pcEstBitsSbac->blockRootCbpBits[ uiCtxInc ][ 0 ] = pCtx[ uiCtxInc ].getEntropyBits( 0 ); |
---|
| 1442 | pcEstBitsSbac->blockRootCbpBits[ uiCtxInc ][ 1 ] = pCtx[ uiCtxInc ].getEntropyBits( 1 ); |
---|
| 1443 | } |
---|
| 1444 | } |
---|
| 1445 | |
---|
| 1446 | |
---|
| 1447 | /*! |
---|
| 1448 | **************************************************************************** |
---|
| 1449 | * \brief |
---|
| 1450 | * estimate SAMBAC bit cost for significant coefficient group map |
---|
| 1451 | **************************************************************************** |
---|
| 1452 | */ |
---|
| 1453 | Void TEncSbac::estSignificantCoeffGroupMapBit( estBitsSbacStruct* pcEstBitsSbac, TextType eTType ) |
---|
| 1454 | { |
---|
| 1455 | Int firstCtx = 0, numCtx = NUM_SIG_CG_FLAG_CTX; |
---|
| 1456 | |
---|
| 1457 | for ( Int ctxIdx = firstCtx; ctxIdx < firstCtx + numCtx; ctxIdx++ ) |
---|
| 1458 | { |
---|
| 1459 | for( UInt uiBin = 0; uiBin < 2; uiBin++ ) |
---|
| 1460 | { |
---|
| 1461 | pcEstBitsSbac->significantCoeffGroupBits[ ctxIdx ][ uiBin ] = m_cCUSigCoeffGroupSCModel.get( 0, eTType, ctxIdx ).getEntropyBits( uiBin ); |
---|
| 1462 | } |
---|
| 1463 | } |
---|
| 1464 | } |
---|
| 1465 | |
---|
| 1466 | |
---|
| 1467 | /*! |
---|
| 1468 | **************************************************************************** |
---|
| 1469 | * \brief |
---|
| 1470 | * estimate SAMBAC bit cost for significant coefficient map |
---|
| 1471 | **************************************************************************** |
---|
| 1472 | */ |
---|
| 1473 | Void TEncSbac::estSignificantMapBit( estBitsSbacStruct* pcEstBitsSbac, Int width, Int height, TextType eTType ) |
---|
| 1474 | { |
---|
| 1475 | Int firstCtx = 1, numCtx = 8; |
---|
| 1476 | if (max(width, height) >= 16) |
---|
| 1477 | { |
---|
| 1478 | firstCtx = (eTType == TEXT_LUMA) ? 21 : 12; |
---|
| 1479 | numCtx = (eTType == TEXT_LUMA) ? 6 : 3; |
---|
| 1480 | } |
---|
| 1481 | else if (width == 8) |
---|
| 1482 | { |
---|
| 1483 | firstCtx = 9; |
---|
| 1484 | numCtx = (eTType == TEXT_LUMA) ? 12 : 3; |
---|
| 1485 | } |
---|
| 1486 | |
---|
| 1487 | if (eTType == TEXT_LUMA ) |
---|
| 1488 | { |
---|
| 1489 | for( UInt bin = 0; bin < 2; bin++ ) |
---|
| 1490 | { |
---|
| 1491 | pcEstBitsSbac->significantBits[ 0 ][ bin ] = m_cCUSigSCModel.get( 0, 0, 0 ).getEntropyBits( bin ); |
---|
| 1492 | } |
---|
| 1493 | |
---|
| 1494 | for ( Int ctxIdx = firstCtx; ctxIdx < firstCtx + numCtx; ctxIdx++ ) |
---|
| 1495 | { |
---|
| 1496 | for( UInt uiBin = 0; uiBin < 2; uiBin++ ) |
---|
| 1497 | { |
---|
| 1498 | pcEstBitsSbac->significantBits[ ctxIdx ][ uiBin ] = m_cCUSigSCModel.get( 0, 0, ctxIdx ).getEntropyBits( uiBin ); |
---|
| 1499 | } |
---|
| 1500 | } |
---|
| 1501 | } |
---|
| 1502 | else |
---|
| 1503 | { |
---|
| 1504 | for( UInt bin = 0; bin < 2; bin++ ) |
---|
| 1505 | { |
---|
| 1506 | pcEstBitsSbac->significantBits[ 0 ][ bin ] = m_cCUSigSCModel.get( 0, 0, NUM_SIG_FLAG_CTX_LUMA + 0 ).getEntropyBits( bin ); |
---|
| 1507 | } |
---|
| 1508 | for ( Int ctxIdx = firstCtx; ctxIdx < firstCtx + numCtx; ctxIdx++ ) |
---|
| 1509 | { |
---|
| 1510 | for( UInt uiBin = 0; uiBin < 2; uiBin++ ) |
---|
| 1511 | { |
---|
| 1512 | pcEstBitsSbac->significantBits[ ctxIdx ][ uiBin ] = m_cCUSigSCModel.get( 0, 0, NUM_SIG_FLAG_CTX_LUMA + ctxIdx ).getEntropyBits( uiBin ); |
---|
| 1513 | } |
---|
| 1514 | } |
---|
| 1515 | } |
---|
| 1516 | Int iBitsX = 0, iBitsY = 0; |
---|
| 1517 | Int blkSizeOffsetX, blkSizeOffsetY, shiftX, shiftY; |
---|
| 1518 | |
---|
| 1519 | blkSizeOffsetX = eTType ? 0: (g_aucConvertToBit[ width ] *3 + ((g_aucConvertToBit[ width ] +1)>>2)); |
---|
| 1520 | blkSizeOffsetY = eTType ? 0: (g_aucConvertToBit[ height ]*3 + ((g_aucConvertToBit[ height ]+1)>>2)); |
---|
| 1521 | shiftX = eTType ? g_aucConvertToBit[ width ] :((g_aucConvertToBit[ width ]+3)>>2); |
---|
| 1522 | shiftY = eTType ? g_aucConvertToBit[ height ] :((g_aucConvertToBit[ height ]+3)>>2); |
---|
| 1523 | |
---|
| 1524 | Int ctx; |
---|
| 1525 | ContextModel *pCtxX = m_cCuCtxLastX.get( 0, eTType ); |
---|
| 1526 | for (ctx = 0; ctx < g_uiGroupIdx[ width - 1 ]; ctx++) |
---|
| 1527 | { |
---|
| 1528 | Int ctxOffset = blkSizeOffsetX + (ctx >>shiftX); |
---|
| 1529 | pcEstBitsSbac->lastXBits[ ctx ] = iBitsX + pCtxX[ ctxOffset ].getEntropyBits( 0 ); |
---|
| 1530 | iBitsX += pCtxX[ ctxOffset ].getEntropyBits( 1 ); |
---|
| 1531 | } |
---|
| 1532 | pcEstBitsSbac->lastXBits[ctx] = iBitsX; |
---|
| 1533 | ContextModel *pCtxY = m_cCuCtxLastY.get( 0, eTType ); |
---|
| 1534 | for (ctx = 0; ctx < g_uiGroupIdx[ height - 1 ]; ctx++) |
---|
| 1535 | { |
---|
| 1536 | Int ctxOffset = blkSizeOffsetY + (ctx >>shiftY); |
---|
| 1537 | pcEstBitsSbac->lastYBits[ ctx ] = iBitsY + pCtxY[ ctxOffset ].getEntropyBits( 0 ); |
---|
| 1538 | iBitsY += pCtxY[ ctxOffset ].getEntropyBits( 1 ); |
---|
| 1539 | } |
---|
| 1540 | pcEstBitsSbac->lastYBits[ctx] = iBitsY; |
---|
| 1541 | } |
---|
| 1542 | |
---|
| 1543 | /*! |
---|
| 1544 | **************************************************************************** |
---|
| 1545 | * \brief |
---|
| 1546 | * estimate bit cost of significant coefficient |
---|
| 1547 | **************************************************************************** |
---|
| 1548 | */ |
---|
| 1549 | Void TEncSbac::estSignificantCoefficientsBit( estBitsSbacStruct* pcEstBitsSbac, TextType eTType ) |
---|
| 1550 | { |
---|
| 1551 | if (eTType==TEXT_LUMA) |
---|
| 1552 | { |
---|
| 1553 | ContextModel *ctxOne = m_cCUOneSCModel.get(0, 0); |
---|
| 1554 | ContextModel *ctxAbs = m_cCUAbsSCModel.get(0, 0); |
---|
| 1555 | |
---|
| 1556 | for (Int ctxIdx = 0; ctxIdx < NUM_ONE_FLAG_CTX_LUMA; ctxIdx++) |
---|
| 1557 | { |
---|
| 1558 | pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 0 ] = ctxOne[ ctxIdx ].getEntropyBits( 0 ); |
---|
| 1559 | pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 1 ] = ctxOne[ ctxIdx ].getEntropyBits( 1 ); |
---|
| 1560 | } |
---|
| 1561 | |
---|
| 1562 | for (Int ctxIdx = 0; ctxIdx < NUM_ABS_FLAG_CTX_LUMA; ctxIdx++) |
---|
| 1563 | { |
---|
| 1564 | pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 0 ] = ctxAbs[ ctxIdx ].getEntropyBits( 0 ); |
---|
| 1565 | pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 1 ] = ctxAbs[ ctxIdx ].getEntropyBits( 1 ); |
---|
| 1566 | } |
---|
| 1567 | } |
---|
| 1568 | else |
---|
| 1569 | { |
---|
| 1570 | ContextModel *ctxOne = m_cCUOneSCModel.get(0, 0) + NUM_ONE_FLAG_CTX_LUMA; |
---|
| 1571 | ContextModel *ctxAbs = m_cCUAbsSCModel.get(0, 0) + NUM_ABS_FLAG_CTX_LUMA; |
---|
| 1572 | |
---|
| 1573 | for (Int ctxIdx = 0; ctxIdx < NUM_ONE_FLAG_CTX_CHROMA; ctxIdx++) |
---|
| 1574 | { |
---|
| 1575 | pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 0 ] = ctxOne[ ctxIdx ].getEntropyBits( 0 ); |
---|
| 1576 | pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 1 ] = ctxOne[ ctxIdx ].getEntropyBits( 1 ); |
---|
| 1577 | } |
---|
| 1578 | |
---|
| 1579 | for (Int ctxIdx = 0; ctxIdx < NUM_ABS_FLAG_CTX_CHROMA; ctxIdx++) |
---|
| 1580 | { |
---|
| 1581 | pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 0 ] = ctxAbs[ ctxIdx ].getEntropyBits( 0 ); |
---|
| 1582 | pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 1 ] = ctxAbs[ ctxIdx ].getEntropyBits( 1 ); |
---|
| 1583 | } |
---|
| 1584 | } |
---|
| 1585 | } |
---|
| 1586 | |
---|
| 1587 | /** |
---|
| 1588 | - Initialize our context information from the nominated source. |
---|
| 1589 | . |
---|
| 1590 | \param pSrc From where to copy context information. |
---|
| 1591 | */ |
---|
| 1592 | Void TEncSbac::xCopyContextsFrom( TEncSbac* pSrc ) |
---|
| 1593 | { |
---|
| 1594 | memcpy(m_contextModels, pSrc->m_contextModels, m_numContextModels*sizeof(m_contextModels[0])); |
---|
| 1595 | } |
---|
| 1596 | |
---|
| 1597 | Void TEncSbac::loadContexts ( TEncSbac* pScr) |
---|
| 1598 | { |
---|
| 1599 | this->xCopyContextsFrom(pScr); |
---|
| 1600 | } |
---|
| 1601 | |
---|
[532] | 1602 | #if HM_CLEANUP_SAO |
---|
| 1603 | Void TEncSbac::codeSAOOffsetParam(Int compIdx, SAOOffset& ctbParam, Bool sliceEnabled) |
---|
| 1604 | { |
---|
| 1605 | UInt uiSymbol; |
---|
| 1606 | if(!sliceEnabled) |
---|
| 1607 | { |
---|
| 1608 | assert(ctbParam.modeIdc == SAO_MODE_OFF); |
---|
| 1609 | return; |
---|
| 1610 | } |
---|
| 1611 | |
---|
| 1612 | //type |
---|
| 1613 | if(compIdx == SAO_Y || compIdx == SAO_Cb) |
---|
| 1614 | { |
---|
| 1615 | //sao_type_idx_luma or sao_type_idx_chroma |
---|
| 1616 | if(ctbParam.modeIdc == SAO_MODE_OFF) |
---|
| 1617 | { |
---|
| 1618 | uiSymbol =0; |
---|
| 1619 | } |
---|
| 1620 | else if(ctbParam.typeIdc == SAO_TYPE_BO) //BO |
---|
| 1621 | { |
---|
| 1622 | uiSymbol = 1; |
---|
| 1623 | } |
---|
| 1624 | else |
---|
| 1625 | { |
---|
| 1626 | assert(ctbParam.typeIdc < SAO_TYPE_START_BO); //EO |
---|
| 1627 | uiSymbol = 2; |
---|
| 1628 | } |
---|
| 1629 | codeSaoTypeIdx(uiSymbol); |
---|
| 1630 | } |
---|
| 1631 | |
---|
| 1632 | if(ctbParam.modeIdc == SAO_MODE_NEW) |
---|
| 1633 | { |
---|
| 1634 | Int numClasses = (ctbParam.typeIdc == SAO_TYPE_BO)?4:NUM_SAO_EO_CLASSES; |
---|
| 1635 | Int offset[4]; |
---|
| 1636 | Int k=0; |
---|
| 1637 | for(Int i=0; i< numClasses; i++) |
---|
| 1638 | { |
---|
| 1639 | if(ctbParam.typeIdc != SAO_TYPE_BO && i == SAO_CLASS_EO_PLAIN) |
---|
| 1640 | { |
---|
| 1641 | continue; |
---|
| 1642 | } |
---|
| 1643 | Int classIdx = (ctbParam.typeIdc == SAO_TYPE_BO)?( (ctbParam.typeAuxInfo+i)% NUM_SAO_BO_CLASSES ):i; |
---|
| 1644 | offset[k] = ctbParam.offset[classIdx]; |
---|
| 1645 | k++; |
---|
| 1646 | } |
---|
| 1647 | |
---|
| 1648 | for(Int i=0; i< 4; i++) |
---|
| 1649 | { |
---|
| 1650 | codeSaoMaxUvlc((offset[i]<0)?(-offset[i]):(offset[i]), g_saoMaxOffsetQVal[compIdx] ); //sao_offset_abs |
---|
| 1651 | } |
---|
| 1652 | |
---|
| 1653 | |
---|
| 1654 | if(ctbParam.typeIdc == SAO_TYPE_BO) |
---|
| 1655 | { |
---|
| 1656 | for(Int i=0; i< 4; i++) |
---|
| 1657 | { |
---|
| 1658 | if(offset[i] != 0) |
---|
| 1659 | { |
---|
| 1660 | codeSAOSign((offset[i]< 0)?1:0); |
---|
| 1661 | } |
---|
| 1662 | } |
---|
| 1663 | |
---|
| 1664 | codeSaoUflc(NUM_SAO_BO_CLASSES_LOG2, ctbParam.typeAuxInfo ); //sao_band_position |
---|
| 1665 | } |
---|
| 1666 | else //EO |
---|
| 1667 | { |
---|
| 1668 | if(compIdx == SAO_Y || compIdx == SAO_Cb) |
---|
| 1669 | { |
---|
| 1670 | assert(ctbParam.typeIdc - SAO_TYPE_START_EO >=0); |
---|
| 1671 | codeSaoUflc(NUM_SAO_EO_TYPES_LOG2, ctbParam.typeIdc - SAO_TYPE_START_EO ); //sao_eo_class_luma or sao_eo_class_chroma |
---|
| 1672 | } |
---|
| 1673 | } |
---|
| 1674 | |
---|
| 1675 | } |
---|
| 1676 | } |
---|
| 1677 | |
---|
| 1678 | |
---|
| 1679 | Void TEncSbac::codeSAOBlkParam(SAOBlkParam& saoBlkParam |
---|
| 1680 | , Bool* sliceEnabled |
---|
| 1681 | , Bool leftMergeAvail |
---|
| 1682 | , Bool aboveMergeAvail |
---|
| 1683 | , Bool onlyEstMergeInfo // = false |
---|
| 1684 | ) |
---|
| 1685 | { |
---|
| 1686 | |
---|
| 1687 | Bool isLeftMerge = false; |
---|
| 1688 | Bool isAboveMerge= false; |
---|
| 1689 | |
---|
| 1690 | if(leftMergeAvail) |
---|
| 1691 | { |
---|
| 1692 | isLeftMerge = ((saoBlkParam[SAO_Y].modeIdc == SAO_MODE_MERGE) && (saoBlkParam[SAO_Y].typeIdc == SAO_MERGE_LEFT)); |
---|
| 1693 | codeSaoMerge( isLeftMerge?1:0 ); //sao_merge_left_flag |
---|
| 1694 | } |
---|
| 1695 | |
---|
| 1696 | if( aboveMergeAvail && !isLeftMerge) |
---|
| 1697 | { |
---|
| 1698 | isAboveMerge = ((saoBlkParam[SAO_Y].modeIdc == SAO_MODE_MERGE) && (saoBlkParam[SAO_Y].typeIdc == SAO_MERGE_ABOVE)); |
---|
| 1699 | codeSaoMerge( isAboveMerge?1:0 ); //sao_merge_left_flag |
---|
| 1700 | } |
---|
| 1701 | |
---|
| 1702 | if(onlyEstMergeInfo) |
---|
| 1703 | { |
---|
| 1704 | return; //only for RDO |
---|
| 1705 | } |
---|
| 1706 | |
---|
| 1707 | if(!isLeftMerge && !isAboveMerge) //not merge mode |
---|
| 1708 | { |
---|
| 1709 | for(Int compIdx=0; compIdx < NUM_SAO_COMPONENTS; compIdx++) |
---|
| 1710 | { |
---|
| 1711 | codeSAOOffsetParam(compIdx, saoBlkParam[compIdx], sliceEnabled[compIdx]); |
---|
| 1712 | } |
---|
| 1713 | } |
---|
| 1714 | } |
---|
| 1715 | #endif |
---|
| 1716 | |
---|
[313] | 1717 | //! \} |
---|