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-2015, 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 | #include "TLibCommon/TComTU.h" |
---|
41 | |
---|
42 | #include <map> |
---|
43 | #include <algorithm> |
---|
44 | |
---|
45 | #if ENVIRONMENT_VARIABLE_DEBUG_AND_TEST |
---|
46 | #include "../TLibCommon/Debug.h" |
---|
47 | #endif |
---|
48 | |
---|
49 | |
---|
50 | //! \ingroup TLibEncoder |
---|
51 | //! \{ |
---|
52 | |
---|
53 | // ==================================================================================================================== |
---|
54 | // Constructor / destructor / create / destroy |
---|
55 | // ==================================================================================================================== |
---|
56 | |
---|
57 | TEncSbac::TEncSbac() |
---|
58 | // new structure here |
---|
59 | : m_pcBitIf ( NULL ) |
---|
60 | , m_pcSlice ( NULL ) |
---|
61 | , m_pcBinIf ( NULL ) |
---|
62 | , m_numContextModels ( 0 ) |
---|
63 | , m_cCUSplitFlagSCModel ( 1, 1, NUM_SPLIT_FLAG_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
64 | , m_cCUSkipFlagSCModel ( 1, 1, NUM_SKIP_FLAG_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
65 | , m_cCUMergeFlagExtSCModel ( 1, 1, NUM_MERGE_FLAG_EXT_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
66 | , m_cCUMergeIdxExtSCModel ( 1, 1, NUM_MERGE_IDX_EXT_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
67 | , m_cCUPartSizeSCModel ( 1, 1, NUM_PART_SIZE_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
68 | , m_cCUPredModeSCModel ( 1, 1, NUM_PRED_MODE_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
69 | , m_cCUIntraPredSCModel ( 1, 1, NUM_ADI_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
70 | , m_cCUChromaPredSCModel ( 1, 1, NUM_CHROMA_PRED_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
71 | , m_cCUDeltaQpSCModel ( 1, 1, NUM_DELTA_QP_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
72 | , m_cCUInterDirSCModel ( 1, 1, NUM_INTER_DIR_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
73 | , m_cCURefPicSCModel ( 1, 1, NUM_REF_NO_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
74 | , m_cCUMvdSCModel ( 1, 1, NUM_MV_RES_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
75 | , m_cCUQtCbfSCModel ( 1, NUM_QT_CBF_CTX_SETS, NUM_QT_CBF_CTX_PER_SET , m_contextModels + m_numContextModels, m_numContextModels) |
---|
76 | , m_cCUTransSubdivFlagSCModel ( 1, 1, NUM_TRANS_SUBDIV_FLAG_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
77 | , m_cCUQtRootCbfSCModel ( 1, 1, NUM_QT_ROOT_CBF_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
78 | , m_cCUSigCoeffGroupSCModel ( 1, 2, NUM_SIG_CG_FLAG_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
79 | , m_cCUSigSCModel ( 1, 1, NUM_SIG_FLAG_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
80 | , m_cCuCtxLastX ( 1, NUM_CTX_LAST_FLAG_SETS, NUM_CTX_LAST_FLAG_XY , m_contextModels + m_numContextModels, m_numContextModels) |
---|
81 | , m_cCuCtxLastY ( 1, NUM_CTX_LAST_FLAG_SETS, NUM_CTX_LAST_FLAG_XY , m_contextModels + m_numContextModels, m_numContextModels) |
---|
82 | , m_cCUOneSCModel ( 1, 1, NUM_ONE_FLAG_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
83 | , m_cCUAbsSCModel ( 1, 1, NUM_ABS_FLAG_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
84 | , m_cMVPIdxSCModel ( 1, 1, NUM_MVP_IDX_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
85 | , m_cSaoMergeSCModel ( 1, 1, NUM_SAO_MERGE_FLAG_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
86 | , m_cSaoTypeIdxSCModel ( 1, 1, NUM_SAO_TYPE_IDX_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
87 | , m_cTransformSkipSCModel ( 1, MAX_NUM_CHANNEL_TYPE, NUM_TRANSFORMSKIP_FLAG_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
88 | , m_CUTransquantBypassFlagSCModel ( 1, 1, NUM_CU_TRANSQUANT_BYPASS_FLAG_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
89 | , m_explicitRdpcmFlagSCModel ( 1, MAX_NUM_CHANNEL_TYPE, NUM_EXPLICIT_RDPCM_FLAG_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
90 | , m_explicitRdpcmDirSCModel ( 1, MAX_NUM_CHANNEL_TYPE, NUM_EXPLICIT_RDPCM_DIR_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
91 | , m_cCrossComponentPredictionSCModel ( 1, 1, NUM_CROSS_COMPONENT_PREDICTION_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
92 | , m_ChromaQpAdjFlagSCModel ( 1, 1, NUM_CHROMA_QP_ADJ_FLAG_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
93 | , m_ChromaQpAdjIdcSCModel ( 1, 1, NUM_CHROMA_QP_ADJ_IDC_CTX , m_contextModels + m_numContextModels, m_numContextModels) |
---|
94 | { |
---|
95 | assert( m_numContextModels <= MAX_NUM_CTX_MOD ); |
---|
96 | } |
---|
97 | |
---|
98 | TEncSbac::~TEncSbac() |
---|
99 | { |
---|
100 | } |
---|
101 | |
---|
102 | // ==================================================================================================================== |
---|
103 | // Public member functions |
---|
104 | // ==================================================================================================================== |
---|
105 | |
---|
106 | Void TEncSbac::resetEntropy () |
---|
107 | { |
---|
108 | Int iQp = m_pcSlice->getSliceQp(); |
---|
109 | SliceType eSliceType = m_pcSlice->getSliceType(); |
---|
110 | |
---|
111 | SliceType encCABACTableIdx = m_pcSlice->getEncCABACTableIdx(); |
---|
112 | if (!m_pcSlice->isIntra() && (encCABACTableIdx==B_SLICE || encCABACTableIdx==P_SLICE) && m_pcSlice->getPPS()->getCabacInitPresentFlag()) |
---|
113 | { |
---|
114 | eSliceType = encCABACTableIdx; |
---|
115 | } |
---|
116 | |
---|
117 | m_cCUSplitFlagSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_SPLIT_FLAG ); |
---|
118 | m_cCUSkipFlagSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_SKIP_FLAG ); |
---|
119 | m_cCUMergeFlagExtSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_MERGE_FLAG_EXT); |
---|
120 | m_cCUMergeIdxExtSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_MERGE_IDX_EXT); |
---|
121 | m_cCUPartSizeSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_PART_SIZE ); |
---|
122 | m_cCUPredModeSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_PRED_MODE ); |
---|
123 | m_cCUIntraPredSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_INTRA_PRED_MODE ); |
---|
124 | m_cCUChromaPredSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_CHROMA_PRED_MODE ); |
---|
125 | m_cCUInterDirSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_INTER_DIR ); |
---|
126 | m_cCUMvdSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_MVD ); |
---|
127 | m_cCURefPicSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_REF_PIC ); |
---|
128 | m_cCUDeltaQpSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_DQP ); |
---|
129 | m_cCUQtCbfSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_QT_CBF ); |
---|
130 | m_cCUQtRootCbfSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_QT_ROOT_CBF ); |
---|
131 | m_cCUSigCoeffGroupSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_SIG_CG_FLAG ); |
---|
132 | m_cCUSigSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_SIG_FLAG ); |
---|
133 | m_cCuCtxLastX.initBuffer ( eSliceType, iQp, (UChar*)INIT_LAST ); |
---|
134 | m_cCuCtxLastY.initBuffer ( eSliceType, iQp, (UChar*)INIT_LAST ); |
---|
135 | m_cCUOneSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_ONE_FLAG ); |
---|
136 | m_cCUAbsSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_ABS_FLAG ); |
---|
137 | m_cMVPIdxSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_MVP_IDX ); |
---|
138 | m_cCUTransSubdivFlagSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_TRANS_SUBDIV_FLAG ); |
---|
139 | m_cSaoMergeSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_SAO_MERGE_FLAG ); |
---|
140 | m_cSaoTypeIdxSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_SAO_TYPE_IDX ); |
---|
141 | m_cTransformSkipSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_TRANSFORMSKIP_FLAG ); |
---|
142 | m_CUTransquantBypassFlagSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_CU_TRANSQUANT_BYPASS_FLAG ); |
---|
143 | m_explicitRdpcmFlagSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_EXPLICIT_RDPCM_FLAG); |
---|
144 | m_explicitRdpcmDirSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_EXPLICIT_RDPCM_DIR); |
---|
145 | m_cCrossComponentPredictionSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_CROSS_COMPONENT_PREDICTION ); |
---|
146 | m_ChromaQpAdjFlagSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_CHROMA_QP_ADJ_FLAG ); |
---|
147 | m_ChromaQpAdjIdcSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_CHROMA_QP_ADJ_IDC ); |
---|
148 | |
---|
149 | for (UInt statisticIndex = 0; statisticIndex < RExt__GOLOMB_RICE_ADAPTATION_STATISTICS_SETS ; statisticIndex++) |
---|
150 | { |
---|
151 | m_golombRiceAdaptationStatistics[statisticIndex] = 0; |
---|
152 | } |
---|
153 | |
---|
154 | m_pcBinIf->start(); |
---|
155 | |
---|
156 | return; |
---|
157 | } |
---|
158 | |
---|
159 | /** The function does the following: |
---|
160 | * If current slice type is P/B then it determines the distance of initialisation type 1 and 2 from the current CABAC states and |
---|
161 | * stores the index of the closest table. This index is used for the next P/B slice when cabac_init_present_flag is true. |
---|
162 | */ |
---|
163 | SliceType TEncSbac::determineCabacInitIdx() |
---|
164 | { |
---|
165 | Int qp = m_pcSlice->getSliceQp(); |
---|
166 | |
---|
167 | if (!m_pcSlice->isIntra()) |
---|
168 | { |
---|
169 | SliceType aSliceTypeChoices[] = {B_SLICE, P_SLICE}; |
---|
170 | |
---|
171 | UInt bestCost = MAX_UINT; |
---|
172 | SliceType bestSliceType = aSliceTypeChoices[0]; |
---|
173 | for (UInt idx=0; idx<2; idx++) |
---|
174 | { |
---|
175 | UInt curCost = 0; |
---|
176 | SliceType curSliceType = aSliceTypeChoices[idx]; |
---|
177 | |
---|
178 | curCost = m_cCUSplitFlagSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_SPLIT_FLAG ); |
---|
179 | curCost += m_cCUSkipFlagSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_SKIP_FLAG ); |
---|
180 | curCost += m_cCUMergeFlagExtSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_MERGE_FLAG_EXT); |
---|
181 | curCost += m_cCUMergeIdxExtSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_MERGE_IDX_EXT); |
---|
182 | curCost += m_cCUPartSizeSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_PART_SIZE ); |
---|
183 | curCost += m_cCUPredModeSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_PRED_MODE ); |
---|
184 | curCost += m_cCUIntraPredSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_INTRA_PRED_MODE ); |
---|
185 | curCost += m_cCUChromaPredSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_CHROMA_PRED_MODE ); |
---|
186 | curCost += m_cCUInterDirSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_INTER_DIR ); |
---|
187 | curCost += m_cCUMvdSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_MVD ); |
---|
188 | curCost += m_cCURefPicSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_REF_PIC ); |
---|
189 | curCost += m_cCUDeltaQpSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_DQP ); |
---|
190 | curCost += m_cCUQtCbfSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_QT_CBF ); |
---|
191 | curCost += m_cCUQtRootCbfSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_QT_ROOT_CBF ); |
---|
192 | curCost += m_cCUSigCoeffGroupSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_SIG_CG_FLAG ); |
---|
193 | curCost += m_cCUSigSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_SIG_FLAG ); |
---|
194 | curCost += m_cCuCtxLastX.calcCost ( curSliceType, qp, (UChar*)INIT_LAST ); |
---|
195 | curCost += m_cCuCtxLastY.calcCost ( curSliceType, qp, (UChar*)INIT_LAST ); |
---|
196 | curCost += m_cCUOneSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_ONE_FLAG ); |
---|
197 | curCost += m_cCUAbsSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_ABS_FLAG ); |
---|
198 | curCost += m_cMVPIdxSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_MVP_IDX ); |
---|
199 | curCost += m_cCUTransSubdivFlagSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_TRANS_SUBDIV_FLAG ); |
---|
200 | curCost += m_cSaoMergeSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_SAO_MERGE_FLAG ); |
---|
201 | curCost += m_cSaoTypeIdxSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_SAO_TYPE_IDX ); |
---|
202 | curCost += m_cTransformSkipSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_TRANSFORMSKIP_FLAG ); |
---|
203 | curCost += m_CUTransquantBypassFlagSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_CU_TRANSQUANT_BYPASS_FLAG ); |
---|
204 | curCost += m_explicitRdpcmFlagSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_EXPLICIT_RDPCM_FLAG); |
---|
205 | curCost += m_explicitRdpcmDirSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_EXPLICIT_RDPCM_DIR); |
---|
206 | curCost += m_cCrossComponentPredictionSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_CROSS_COMPONENT_PREDICTION ); |
---|
207 | curCost += m_ChromaQpAdjFlagSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_CHROMA_QP_ADJ_FLAG ); |
---|
208 | curCost += m_ChromaQpAdjIdcSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_CHROMA_QP_ADJ_IDC ); |
---|
209 | |
---|
210 | if (curCost < bestCost) |
---|
211 | { |
---|
212 | bestSliceType = curSliceType; |
---|
213 | bestCost = curCost; |
---|
214 | } |
---|
215 | } |
---|
216 | return bestSliceType; |
---|
217 | } |
---|
218 | else |
---|
219 | { |
---|
220 | return I_SLICE; |
---|
221 | } |
---|
222 | } |
---|
223 | |
---|
224 | Void TEncSbac::codeVPS( const TComVPS* pcVPS ) |
---|
225 | { |
---|
226 | assert (0); |
---|
227 | return; |
---|
228 | } |
---|
229 | |
---|
230 | Void TEncSbac::codeSPS( const TComSPS* pcSPS ) |
---|
231 | { |
---|
232 | assert (0); |
---|
233 | return; |
---|
234 | } |
---|
235 | |
---|
236 | #if CGS_3D_ASYMLUT |
---|
237 | Void TEncSbac::codePPS( const TComPPS* pcPPS, TEnc3DAsymLUT * pc3DAsymLUT ) |
---|
238 | #else |
---|
239 | Void TEncSbac::codePPS( const TComPPS* pcPPS ) |
---|
240 | #endif |
---|
241 | { |
---|
242 | assert (0); |
---|
243 | return; |
---|
244 | } |
---|
245 | |
---|
246 | Void TEncSbac::codeSliceHeader( TComSlice* pcSlice ) |
---|
247 | { |
---|
248 | assert (0); |
---|
249 | return; |
---|
250 | } |
---|
251 | |
---|
252 | Void TEncSbac::codeTilesWPPEntryPoint( TComSlice* pSlice ) |
---|
253 | { |
---|
254 | assert (0); |
---|
255 | return; |
---|
256 | } |
---|
257 | |
---|
258 | Void TEncSbac::codeTerminatingBit( UInt uilsLast ) |
---|
259 | { |
---|
260 | m_pcBinIf->encodeBinTrm( uilsLast ); |
---|
261 | } |
---|
262 | |
---|
263 | Void TEncSbac::codeSliceFinish() |
---|
264 | { |
---|
265 | m_pcBinIf->finish(); |
---|
266 | } |
---|
267 | |
---|
268 | Void TEncSbac::xWriteUnarySymbol( UInt uiSymbol, ContextModel* pcSCModel, Int iOffset ) |
---|
269 | { |
---|
270 | m_pcBinIf->encodeBin( uiSymbol ? 1 : 0, pcSCModel[0] ); |
---|
271 | |
---|
272 | if( 0 == uiSymbol) |
---|
273 | { |
---|
274 | return; |
---|
275 | } |
---|
276 | |
---|
277 | while( uiSymbol-- ) |
---|
278 | { |
---|
279 | m_pcBinIf->encodeBin( uiSymbol ? 1 : 0, pcSCModel[ iOffset ] ); |
---|
280 | } |
---|
281 | |
---|
282 | return; |
---|
283 | } |
---|
284 | |
---|
285 | Void TEncSbac::xWriteUnaryMaxSymbol( UInt uiSymbol, ContextModel* pcSCModel, Int iOffset, UInt uiMaxSymbol ) |
---|
286 | { |
---|
287 | if (uiMaxSymbol == 0) |
---|
288 | { |
---|
289 | return; |
---|
290 | } |
---|
291 | |
---|
292 | m_pcBinIf->encodeBin( uiSymbol ? 1 : 0, pcSCModel[ 0 ] ); |
---|
293 | |
---|
294 | if ( uiSymbol == 0 ) |
---|
295 | { |
---|
296 | return; |
---|
297 | } |
---|
298 | |
---|
299 | Bool bCodeLast = ( uiMaxSymbol > uiSymbol ); |
---|
300 | |
---|
301 | while( --uiSymbol ) |
---|
302 | { |
---|
303 | m_pcBinIf->encodeBin( 1, pcSCModel[ iOffset ] ); |
---|
304 | } |
---|
305 | if( bCodeLast ) |
---|
306 | { |
---|
307 | m_pcBinIf->encodeBin( 0, pcSCModel[ iOffset ] ); |
---|
308 | } |
---|
309 | |
---|
310 | return; |
---|
311 | } |
---|
312 | |
---|
313 | Void TEncSbac::xWriteEpExGolomb( UInt uiSymbol, UInt uiCount ) |
---|
314 | { |
---|
315 | UInt bins = 0; |
---|
316 | Int numBins = 0; |
---|
317 | |
---|
318 | while( uiSymbol >= (UInt)(1<<uiCount) ) |
---|
319 | { |
---|
320 | bins = 2 * bins + 1; |
---|
321 | numBins++; |
---|
322 | uiSymbol -= 1 << uiCount; |
---|
323 | uiCount ++; |
---|
324 | } |
---|
325 | bins = 2 * bins + 0; |
---|
326 | numBins++; |
---|
327 | |
---|
328 | bins = (bins << uiCount) | uiSymbol; |
---|
329 | numBins += uiCount; |
---|
330 | |
---|
331 | assert( numBins <= 32 ); |
---|
332 | m_pcBinIf->encodeBinsEP( bins, numBins ); |
---|
333 | } |
---|
334 | |
---|
335 | |
---|
336 | /** Coding of coeff_abs_level_minus3 |
---|
337 | * \param symbol value of coeff_abs_level_minus3 |
---|
338 | * \param rParam reference to Rice parameter |
---|
339 | * \param useLimitedPrefixLength |
---|
340 | * \param channelType plane type (luma/chroma) |
---|
341 | */ |
---|
342 | Void TEncSbac::xWriteCoefRemainExGolomb ( UInt symbol, UInt &rParam, const Bool useLimitedPrefixLength, const ChannelType channelType, const Int maxLog2TrDynamicRange ) |
---|
343 | { |
---|
344 | Int codeNumber = (Int)symbol; |
---|
345 | UInt length; |
---|
346 | |
---|
347 | if (codeNumber < (COEF_REMAIN_BIN_REDUCTION << rParam)) |
---|
348 | { |
---|
349 | length = codeNumber>>rParam; |
---|
350 | m_pcBinIf->encodeBinsEP( (1<<(length+1))-2 , length+1); |
---|
351 | m_pcBinIf->encodeBinsEP((codeNumber%(1<<rParam)),rParam); |
---|
352 | } |
---|
353 | else if (useLimitedPrefixLength) |
---|
354 | { |
---|
355 | const UInt maximumPrefixLength = (32 - (COEF_REMAIN_BIN_REDUCTION + maxLog2TrDynamicRange)); |
---|
356 | |
---|
357 | UInt prefixLength = 0; |
---|
358 | UInt suffixLength = MAX_UINT; |
---|
359 | UInt codeValue = (symbol >> rParam) - COEF_REMAIN_BIN_REDUCTION; |
---|
360 | |
---|
361 | if (codeValue >= ((1 << maximumPrefixLength) - 1)) |
---|
362 | { |
---|
363 | prefixLength = maximumPrefixLength; |
---|
364 | suffixLength = maxLog2TrDynamicRange - rParam; |
---|
365 | } |
---|
366 | else |
---|
367 | { |
---|
368 | while (codeValue > ((2 << prefixLength) - 2)) |
---|
369 | { |
---|
370 | prefixLength++; |
---|
371 | } |
---|
372 | |
---|
373 | suffixLength = prefixLength + 1; //+1 for the separator bit |
---|
374 | } |
---|
375 | |
---|
376 | const UInt suffix = codeValue - ((1 << prefixLength) - 1); |
---|
377 | |
---|
378 | const UInt totalPrefixLength = prefixLength + COEF_REMAIN_BIN_REDUCTION; |
---|
379 | const UInt prefix = (1 << totalPrefixLength) - 1; |
---|
380 | const UInt rParamBitMask = (1 << rParam) - 1; |
---|
381 | |
---|
382 | m_pcBinIf->encodeBinsEP( prefix, totalPrefixLength ); //prefix |
---|
383 | m_pcBinIf->encodeBinsEP(((suffix << rParam) | (symbol & rParamBitMask)), (suffixLength + rParam)); //separator, suffix, and rParam bits |
---|
384 | } |
---|
385 | else |
---|
386 | { |
---|
387 | length = rParam; |
---|
388 | codeNumber = codeNumber - ( COEF_REMAIN_BIN_REDUCTION << rParam); |
---|
389 | |
---|
390 | while (codeNumber >= (1<<length)) |
---|
391 | { |
---|
392 | codeNumber -= (1<<(length++)); |
---|
393 | } |
---|
394 | |
---|
395 | m_pcBinIf->encodeBinsEP((1<<(COEF_REMAIN_BIN_REDUCTION+length+1-rParam))-2,COEF_REMAIN_BIN_REDUCTION+length+1-rParam); |
---|
396 | m_pcBinIf->encodeBinsEP(codeNumber,length); |
---|
397 | } |
---|
398 | } |
---|
399 | |
---|
400 | // SBAC RD |
---|
401 | Void TEncSbac::load ( const TEncSbac* pSrc) |
---|
402 | { |
---|
403 | this->xCopyFrom(pSrc); |
---|
404 | } |
---|
405 | |
---|
406 | Void TEncSbac::loadIntraDirMode( const TEncSbac* pSrc, const ChannelType chType ) |
---|
407 | { |
---|
408 | m_pcBinIf->copyState( pSrc->m_pcBinIf ); |
---|
409 | if (isLuma(chType)) |
---|
410 | { |
---|
411 | this->m_cCUIntraPredSCModel .copyFrom( &pSrc->m_cCUIntraPredSCModel ); |
---|
412 | } |
---|
413 | else |
---|
414 | { |
---|
415 | this->m_cCUChromaPredSCModel .copyFrom( &pSrc->m_cCUChromaPredSCModel ); |
---|
416 | } |
---|
417 | } |
---|
418 | |
---|
419 | |
---|
420 | Void TEncSbac::store( TEncSbac* pDest) const |
---|
421 | { |
---|
422 | pDest->xCopyFrom( this ); |
---|
423 | } |
---|
424 | |
---|
425 | |
---|
426 | Void TEncSbac::xCopyFrom( const TEncSbac* pSrc ) |
---|
427 | { |
---|
428 | m_pcBinIf->copyState( pSrc->m_pcBinIf ); |
---|
429 | xCopyContextsFrom(pSrc); |
---|
430 | } |
---|
431 | |
---|
432 | Void TEncSbac::codeMVPIdx ( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList ) |
---|
433 | { |
---|
434 | Int iSymbol = pcCU->getMVPIdx(eRefList, uiAbsPartIdx); |
---|
435 | Int iNum = AMVP_MAX_NUM_CANDS; |
---|
436 | |
---|
437 | xWriteUnaryMaxSymbol(iSymbol, m_cMVPIdxSCModel.get(0), 1, iNum-1); |
---|
438 | } |
---|
439 | |
---|
440 | Void TEncSbac::codePartSize( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth ) |
---|
441 | { |
---|
442 | PartSize eSize = pcCU->getPartitionSize( uiAbsPartIdx ); |
---|
443 | |
---|
444 | if ( pcCU->isIntra( uiAbsPartIdx ) ) |
---|
445 | { |
---|
446 | if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth ) |
---|
447 | { |
---|
448 | m_pcBinIf->encodeBin( eSize == SIZE_2Nx2N? 1 : 0, m_cCUPartSizeSCModel.get( 0, 0, 0 ) ); |
---|
449 | } |
---|
450 | return; |
---|
451 | } |
---|
452 | |
---|
453 | switch(eSize) |
---|
454 | { |
---|
455 | case SIZE_2Nx2N: |
---|
456 | { |
---|
457 | m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 0) ); |
---|
458 | break; |
---|
459 | } |
---|
460 | case SIZE_2NxN: |
---|
461 | case SIZE_2NxnU: |
---|
462 | case SIZE_2NxnD: |
---|
463 | { |
---|
464 | m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) ); |
---|
465 | m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 1) ); |
---|
466 | if ( pcCU->getSlice()->getSPS()->getUseAMP() && uiDepth < g_uiMaxCUDepth-g_uiAddCUDepth ) |
---|
467 | { |
---|
468 | if (eSize == SIZE_2NxN) |
---|
469 | { |
---|
470 | m_pcBinIf->encodeBin(1, m_cCUPartSizeSCModel.get( 0, 0, 3 )); |
---|
471 | } |
---|
472 | else |
---|
473 | { |
---|
474 | m_pcBinIf->encodeBin(0, m_cCUPartSizeSCModel.get( 0, 0, 3 )); |
---|
475 | m_pcBinIf->encodeBinEP((eSize == SIZE_2NxnU? 0: 1)); |
---|
476 | } |
---|
477 | } |
---|
478 | break; |
---|
479 | } |
---|
480 | case SIZE_Nx2N: |
---|
481 | case SIZE_nLx2N: |
---|
482 | case SIZE_nRx2N: |
---|
483 | { |
---|
484 | m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) ); |
---|
485 | m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 1) ); |
---|
486 | |
---|
487 | if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth && !( pcCU->getWidth(uiAbsPartIdx) == 8 && pcCU->getHeight(uiAbsPartIdx) == 8 ) ) |
---|
488 | { |
---|
489 | m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 2) ); |
---|
490 | } |
---|
491 | |
---|
492 | if ( pcCU->getSlice()->getSPS()->getUseAMP() && uiDepth < g_uiMaxCUDepth-g_uiAddCUDepth ) |
---|
493 | { |
---|
494 | if (eSize == SIZE_Nx2N) |
---|
495 | { |
---|
496 | m_pcBinIf->encodeBin(1, m_cCUPartSizeSCModel.get( 0, 0, 3 )); |
---|
497 | } |
---|
498 | else |
---|
499 | { |
---|
500 | m_pcBinIf->encodeBin(0, m_cCUPartSizeSCModel.get( 0, 0, 3 )); |
---|
501 | m_pcBinIf->encodeBinEP((eSize == SIZE_nLx2N? 0: 1)); |
---|
502 | } |
---|
503 | } |
---|
504 | break; |
---|
505 | } |
---|
506 | case SIZE_NxN: |
---|
507 | { |
---|
508 | if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth && !( pcCU->getWidth(uiAbsPartIdx) == 8 && pcCU->getHeight(uiAbsPartIdx) == 8 ) ) |
---|
509 | { |
---|
510 | m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) ); |
---|
511 | m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 1) ); |
---|
512 | m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 2) ); |
---|
513 | } |
---|
514 | break; |
---|
515 | } |
---|
516 | default: |
---|
517 | { |
---|
518 | assert(0); |
---|
519 | break; |
---|
520 | } |
---|
521 | } |
---|
522 | } |
---|
523 | |
---|
524 | |
---|
525 | /** code prediction mode |
---|
526 | * \param pcCU |
---|
527 | * \param uiAbsPartIdx |
---|
528 | * \returns Void |
---|
529 | */ |
---|
530 | Void TEncSbac::codePredMode( TComDataCU* pcCU, UInt uiAbsPartIdx ) |
---|
531 | { |
---|
532 | // get context function is here |
---|
533 | m_pcBinIf->encodeBin( pcCU->isIntra( uiAbsPartIdx ) ? 1 : 0, m_cCUPredModeSCModel.get( 0, 0, 0 ) ); |
---|
534 | } |
---|
535 | |
---|
536 | Void TEncSbac::codeCUTransquantBypassFlag( TComDataCU* pcCU, UInt uiAbsPartIdx ) |
---|
537 | { |
---|
538 | UInt uiSymbol = pcCU->getCUTransquantBypass(uiAbsPartIdx); |
---|
539 | m_pcBinIf->encodeBin( uiSymbol, m_CUTransquantBypassFlagSCModel.get( 0, 0, 0 ) ); |
---|
540 | } |
---|
541 | |
---|
542 | /** code skip flag |
---|
543 | * \param pcCU |
---|
544 | * \param uiAbsPartIdx |
---|
545 | * \returns Void |
---|
546 | */ |
---|
547 | Void TEncSbac::codeSkipFlag( TComDataCU* pcCU, UInt uiAbsPartIdx ) |
---|
548 | { |
---|
549 | // get context function is here |
---|
550 | UInt uiSymbol = pcCU->isSkipped( uiAbsPartIdx ) ? 1 : 0; |
---|
551 | UInt uiCtxSkip = pcCU->getCtxSkipFlag( uiAbsPartIdx ) ; |
---|
552 | m_pcBinIf->encodeBin( uiSymbol, m_cCUSkipFlagSCModel.get( 0, 0, uiCtxSkip ) ); |
---|
553 | DTRACE_CABAC_VL( g_nSymbolCounter++ ); |
---|
554 | DTRACE_CABAC_T( "\tSkipFlag" ); |
---|
555 | DTRACE_CABAC_T( "\tuiCtxSkip: "); |
---|
556 | DTRACE_CABAC_V( uiCtxSkip ); |
---|
557 | DTRACE_CABAC_T( "\tuiSymbol: "); |
---|
558 | DTRACE_CABAC_V( uiSymbol ); |
---|
559 | DTRACE_CABAC_T( "\n"); |
---|
560 | } |
---|
561 | |
---|
562 | /** code merge flag |
---|
563 | * \param pcCU |
---|
564 | * \param uiAbsPartIdx |
---|
565 | * \returns Void |
---|
566 | */ |
---|
567 | Void TEncSbac::codeMergeFlag( TComDataCU* pcCU, UInt uiAbsPartIdx ) |
---|
568 | { |
---|
569 | const UInt uiSymbol = pcCU->getMergeFlag( uiAbsPartIdx ) ? 1 : 0; |
---|
570 | m_pcBinIf->encodeBin( uiSymbol, *m_cCUMergeFlagExtSCModel.get( 0 ) ); |
---|
571 | |
---|
572 | DTRACE_CABAC_VL( g_nSymbolCounter++ ); |
---|
573 | DTRACE_CABAC_T( "\tMergeFlag: " ); |
---|
574 | DTRACE_CABAC_V( uiSymbol ); |
---|
575 | DTRACE_CABAC_T( "\tAddress: " ); |
---|
576 | DTRACE_CABAC_V( pcCU->getCtuRsAddr() ); |
---|
577 | DTRACE_CABAC_T( "\tuiAbsPartIdx: " ); |
---|
578 | DTRACE_CABAC_V( uiAbsPartIdx ); |
---|
579 | DTRACE_CABAC_T( "\n" ); |
---|
580 | } |
---|
581 | |
---|
582 | /** code merge index |
---|
583 | * \param pcCU |
---|
584 | * \param uiAbsPartIdx |
---|
585 | * \returns Void |
---|
586 | */ |
---|
587 | Void TEncSbac::codeMergeIndex( TComDataCU* pcCU, UInt uiAbsPartIdx ) |
---|
588 | { |
---|
589 | UInt uiUnaryIdx = pcCU->getMergeIndex( uiAbsPartIdx ); |
---|
590 | UInt uiNumCand = pcCU->getSlice()->getMaxNumMergeCand(); |
---|
591 | if ( uiNumCand > 1 ) |
---|
592 | { |
---|
593 | for( UInt ui = 0; ui < uiNumCand - 1; ++ui ) |
---|
594 | { |
---|
595 | const UInt uiSymbol = ui == uiUnaryIdx ? 0 : 1; |
---|
596 | if ( ui==0 ) |
---|
597 | { |
---|
598 | m_pcBinIf->encodeBin( uiSymbol, m_cCUMergeIdxExtSCModel.get( 0, 0, 0 ) ); |
---|
599 | } |
---|
600 | else |
---|
601 | { |
---|
602 | m_pcBinIf->encodeBinEP( uiSymbol ); |
---|
603 | } |
---|
604 | if( uiSymbol == 0 ) |
---|
605 | { |
---|
606 | break; |
---|
607 | } |
---|
608 | } |
---|
609 | } |
---|
610 | DTRACE_CABAC_VL( g_nSymbolCounter++ ); |
---|
611 | DTRACE_CABAC_T( "\tparseMergeIndex()" ); |
---|
612 | DTRACE_CABAC_T( "\tuiMRGIdx= " ); |
---|
613 | DTRACE_CABAC_V( pcCU->getMergeIndex( uiAbsPartIdx ) ); |
---|
614 | DTRACE_CABAC_T( "\n" ); |
---|
615 | } |
---|
616 | |
---|
617 | Void TEncSbac::codeSplitFlag ( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth ) |
---|
618 | { |
---|
619 | if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth ) |
---|
620 | { |
---|
621 | return; |
---|
622 | } |
---|
623 | |
---|
624 | UInt uiCtx = pcCU->getCtxSplitFlag( uiAbsPartIdx, uiDepth ); |
---|
625 | UInt uiCurrSplitFlag = ( pcCU->getDepth( uiAbsPartIdx ) > uiDepth ) ? 1 : 0; |
---|
626 | |
---|
627 | assert( uiCtx < 3 ); |
---|
628 | m_pcBinIf->encodeBin( uiCurrSplitFlag, m_cCUSplitFlagSCModel.get( 0, 0, uiCtx ) ); |
---|
629 | DTRACE_CABAC_VL( g_nSymbolCounter++ ) |
---|
630 | DTRACE_CABAC_T( "\tSplitFlag\n" ) |
---|
631 | return; |
---|
632 | } |
---|
633 | |
---|
634 | Void TEncSbac::codeTransformSubdivFlag( UInt uiSymbol, UInt uiCtx ) |
---|
635 | { |
---|
636 | m_pcBinIf->encodeBin( uiSymbol, m_cCUTransSubdivFlagSCModel.get( 0, 0, uiCtx ) ); |
---|
637 | DTRACE_CABAC_VL( g_nSymbolCounter++ ) |
---|
638 | DTRACE_CABAC_T( "\tparseTransformSubdivFlag()" ) |
---|
639 | DTRACE_CABAC_T( "\tsymbol=" ) |
---|
640 | DTRACE_CABAC_V( uiSymbol ) |
---|
641 | DTRACE_CABAC_T( "\tctx=" ) |
---|
642 | DTRACE_CABAC_V( uiCtx ) |
---|
643 | DTRACE_CABAC_T( "\n" ) |
---|
644 | } |
---|
645 | |
---|
646 | |
---|
647 | Void TEncSbac::codeIntraDirLumaAng( TComDataCU* pcCU, UInt absPartIdx, Bool isMultiple) |
---|
648 | { |
---|
649 | UInt dir[4],j; |
---|
650 | Int preds[4][NUM_MOST_PROBABLE_MODES] = {{-1, -1, -1},{-1, -1, -1},{-1, -1, -1},{-1, -1, -1}}; |
---|
651 | Int predIdx[4] ={ -1,-1,-1,-1}; |
---|
652 | PartSize mode = pcCU->getPartitionSize( absPartIdx ); |
---|
653 | UInt partNum = isMultiple?(mode==SIZE_NxN?4:1):1; |
---|
654 | UInt partOffset = ( pcCU->getPic()->getNumPartitionsInCtu() >> ( pcCU->getDepth(absPartIdx) << 1 ) ) >> 2; |
---|
655 | for (j=0;j<partNum;j++) |
---|
656 | { |
---|
657 | dir[j] = pcCU->getIntraDir( CHANNEL_TYPE_LUMA, absPartIdx+partOffset*j ); |
---|
658 | pcCU->getIntraDirPredictor(absPartIdx+partOffset*j, preds[j], COMPONENT_Y); |
---|
659 | for(UInt i = 0; i < NUM_MOST_PROBABLE_MODES; i++) |
---|
660 | { |
---|
661 | if(dir[j] == preds[j][i]) |
---|
662 | { |
---|
663 | predIdx[j] = i; |
---|
664 | } |
---|
665 | } |
---|
666 | m_pcBinIf->encodeBin((predIdx[j] != -1)? 1 : 0, m_cCUIntraPredSCModel.get( 0, 0, 0 ) ); |
---|
667 | } |
---|
668 | for (j=0;j<partNum;j++) |
---|
669 | { |
---|
670 | if(predIdx[j] != -1) |
---|
671 | { |
---|
672 | m_pcBinIf->encodeBinEP( predIdx[j] ? 1 : 0 ); |
---|
673 | if (predIdx[j]) |
---|
674 | { |
---|
675 | m_pcBinIf->encodeBinEP( predIdx[j]-1 ); |
---|
676 | } |
---|
677 | } |
---|
678 | else |
---|
679 | { |
---|
680 | if (preds[j][0] > preds[j][1]) |
---|
681 | { |
---|
682 | std::swap(preds[j][0], preds[j][1]); |
---|
683 | } |
---|
684 | if (preds[j][0] > preds[j][2]) |
---|
685 | { |
---|
686 | std::swap(preds[j][0], preds[j][2]); |
---|
687 | } |
---|
688 | if (preds[j][1] > preds[j][2]) |
---|
689 | { |
---|
690 | std::swap(preds[j][1], preds[j][2]); |
---|
691 | } |
---|
692 | for(Int i = (Int(NUM_MOST_PROBABLE_MODES) - 1); i >= 0; i--) |
---|
693 | { |
---|
694 | dir[j] = dir[j] > preds[j][i] ? dir[j] - 1 : dir[j]; |
---|
695 | } |
---|
696 | m_pcBinIf->encodeBinsEP( dir[j], 5 ); |
---|
697 | } |
---|
698 | } |
---|
699 | return; |
---|
700 | } |
---|
701 | |
---|
702 | Void TEncSbac::codeIntraDirChroma( TComDataCU* pcCU, UInt uiAbsPartIdx ) |
---|
703 | { |
---|
704 | UInt uiIntraDirChroma = pcCU->getIntraDir( CHANNEL_TYPE_CHROMA, uiAbsPartIdx ); |
---|
705 | |
---|
706 | if( uiIntraDirChroma == DM_CHROMA_IDX ) |
---|
707 | { |
---|
708 | m_pcBinIf->encodeBin( 0, m_cCUChromaPredSCModel.get( 0, 0, 0 ) ); |
---|
709 | } |
---|
710 | else |
---|
711 | { |
---|
712 | m_pcBinIf->encodeBin( 1, m_cCUChromaPredSCModel.get( 0, 0, 0 ) ); |
---|
713 | |
---|
714 | UInt uiAllowedChromaDir[ NUM_CHROMA_MODE ]; |
---|
715 | pcCU->getAllowedChromaDir( uiAbsPartIdx, uiAllowedChromaDir ); |
---|
716 | |
---|
717 | for( Int i = 0; i < NUM_CHROMA_MODE - 1; i++ ) |
---|
718 | { |
---|
719 | if( uiIntraDirChroma == uiAllowedChromaDir[i] ) |
---|
720 | { |
---|
721 | uiIntraDirChroma = i; |
---|
722 | break; |
---|
723 | } |
---|
724 | } |
---|
725 | |
---|
726 | m_pcBinIf->encodeBinsEP( uiIntraDirChroma, 2 ); |
---|
727 | } |
---|
728 | |
---|
729 | return; |
---|
730 | } |
---|
731 | |
---|
732 | |
---|
733 | Void TEncSbac::codeInterDir( TComDataCU* pcCU, UInt uiAbsPartIdx ) |
---|
734 | { |
---|
735 | const UInt uiInterDir = pcCU->getInterDir( uiAbsPartIdx ) - 1; |
---|
736 | const UInt uiCtx = pcCU->getCtxInterDir( uiAbsPartIdx ); |
---|
737 | ContextModel *pCtx = m_cCUInterDirSCModel.get( 0 ); |
---|
738 | |
---|
739 | if (pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N || pcCU->getHeight(uiAbsPartIdx) != 8 ) |
---|
740 | { |
---|
741 | m_pcBinIf->encodeBin( uiInterDir == 2 ? 1 : 0, *( pCtx + uiCtx ) ); |
---|
742 | } |
---|
743 | |
---|
744 | if (uiInterDir < 2) |
---|
745 | { |
---|
746 | m_pcBinIf->encodeBin( uiInterDir, *( pCtx + 4 ) ); |
---|
747 | } |
---|
748 | |
---|
749 | return; |
---|
750 | } |
---|
751 | |
---|
752 | Void TEncSbac::codeRefFrmIdx( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList ) |
---|
753 | { |
---|
754 | Int iRefFrame = pcCU->getCUMvField( eRefList )->getRefIdx( uiAbsPartIdx ); |
---|
755 | ContextModel *pCtx = m_cCURefPicSCModel.get( 0 ); |
---|
756 | m_pcBinIf->encodeBin( ( iRefFrame == 0 ? 0 : 1 ), *pCtx ); |
---|
757 | |
---|
758 | if( iRefFrame > 0 ) |
---|
759 | { |
---|
760 | UInt uiRefNum = pcCU->getSlice()->getNumRefIdx( eRefList ) - 2; |
---|
761 | pCtx++; |
---|
762 | iRefFrame--; |
---|
763 | for( UInt ui = 0; ui < uiRefNum; ++ui ) |
---|
764 | { |
---|
765 | const UInt uiSymbol = ui == iRefFrame ? 0 : 1; |
---|
766 | if( ui == 0 ) |
---|
767 | { |
---|
768 | m_pcBinIf->encodeBin( uiSymbol, *pCtx ); |
---|
769 | } |
---|
770 | else |
---|
771 | { |
---|
772 | m_pcBinIf->encodeBinEP( uiSymbol ); |
---|
773 | } |
---|
774 | if( uiSymbol == 0 ) |
---|
775 | { |
---|
776 | break; |
---|
777 | } |
---|
778 | } |
---|
779 | } |
---|
780 | return; |
---|
781 | } |
---|
782 | |
---|
783 | Void TEncSbac::codeMvd( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList ) |
---|
784 | { |
---|
785 | if(pcCU->getSlice()->getMvdL1ZeroFlag() && eRefList == REF_PIC_LIST_1 && pcCU->getInterDir(uiAbsPartIdx)==3) |
---|
786 | { |
---|
787 | return; |
---|
788 | } |
---|
789 | |
---|
790 | const TComCUMvField* pcCUMvField = pcCU->getCUMvField( eRefList ); |
---|
791 | const Int iHor = pcCUMvField->getMvd( uiAbsPartIdx ).getHor(); |
---|
792 | const Int iVer = pcCUMvField->getMvd( uiAbsPartIdx ).getVer(); |
---|
793 | ContextModel* pCtx = m_cCUMvdSCModel.get( 0 ); |
---|
794 | |
---|
795 | m_pcBinIf->encodeBin( iHor != 0 ? 1 : 0, *pCtx ); |
---|
796 | m_pcBinIf->encodeBin( iVer != 0 ? 1 : 0, *pCtx ); |
---|
797 | |
---|
798 | const Bool bHorAbsGr0 = iHor != 0; |
---|
799 | const Bool bVerAbsGr0 = iVer != 0; |
---|
800 | const UInt uiHorAbs = 0 > iHor ? -iHor : iHor; |
---|
801 | const UInt uiVerAbs = 0 > iVer ? -iVer : iVer; |
---|
802 | pCtx++; |
---|
803 | |
---|
804 | if( bHorAbsGr0 ) |
---|
805 | { |
---|
806 | m_pcBinIf->encodeBin( uiHorAbs > 1 ? 1 : 0, *pCtx ); |
---|
807 | } |
---|
808 | |
---|
809 | if( bVerAbsGr0 ) |
---|
810 | { |
---|
811 | m_pcBinIf->encodeBin( uiVerAbs > 1 ? 1 : 0, *pCtx ); |
---|
812 | } |
---|
813 | |
---|
814 | if( bHorAbsGr0 ) |
---|
815 | { |
---|
816 | if( uiHorAbs > 1 ) |
---|
817 | { |
---|
818 | xWriteEpExGolomb( uiHorAbs-2, 1 ); |
---|
819 | } |
---|
820 | |
---|
821 | m_pcBinIf->encodeBinEP( 0 > iHor ? 1 : 0 ); |
---|
822 | } |
---|
823 | |
---|
824 | if( bVerAbsGr0 ) |
---|
825 | { |
---|
826 | if( uiVerAbs > 1 ) |
---|
827 | { |
---|
828 | xWriteEpExGolomb( uiVerAbs-2, 1 ); |
---|
829 | } |
---|
830 | |
---|
831 | m_pcBinIf->encodeBinEP( 0 > iVer ? 1 : 0 ); |
---|
832 | } |
---|
833 | |
---|
834 | return; |
---|
835 | } |
---|
836 | |
---|
837 | Void TEncSbac::codeCrossComponentPrediction( TComTU &rTu, ComponentID compID ) |
---|
838 | { |
---|
839 | TComDataCU *pcCU = rTu.getCU(); |
---|
840 | |
---|
841 | if( isLuma(compID) || !pcCU->getSlice()->getPPS()->getUseCrossComponentPrediction() ) |
---|
842 | { |
---|
843 | return; |
---|
844 | } |
---|
845 | |
---|
846 | const UInt uiAbsPartIdx = rTu.GetAbsPartIdxTU(); |
---|
847 | |
---|
848 | if (!pcCU->isIntra(uiAbsPartIdx) || (pcCU->getIntraDir( CHANNEL_TYPE_CHROMA, uiAbsPartIdx ) == DM_CHROMA_IDX)) |
---|
849 | { |
---|
850 | DTRACE_CABAC_VL( g_nSymbolCounter++ ) |
---|
851 | DTRACE_CABAC_T("\tparseCrossComponentPrediction()") |
---|
852 | DTRACE_CABAC_T( "\tAddr=" ) |
---|
853 | DTRACE_CABAC_V( compID ) |
---|
854 | DTRACE_CABAC_T( "\tuiAbsPartIdx=" ) |
---|
855 | DTRACE_CABAC_V( uiAbsPartIdx ) |
---|
856 | |
---|
857 | Int alpha = pcCU->getCrossComponentPredictionAlpha( uiAbsPartIdx, compID ); |
---|
858 | ContextModel *pCtx = m_cCrossComponentPredictionSCModel.get(0, 0) + ((compID == COMPONENT_Cr) ? (NUM_CROSS_COMPONENT_PREDICTION_CTX >> 1) : 0); |
---|
859 | m_pcBinIf->encodeBin(((alpha != 0) ? 1 : 0), pCtx[0]); |
---|
860 | |
---|
861 | if (alpha != 0) |
---|
862 | { |
---|
863 | static const Int log2AbsAlphaMinus1Table[8] = { 0, 1, 1, 2, 2, 2, 3, 3 }; |
---|
864 | assert(abs(alpha) <= 8); |
---|
865 | |
---|
866 | if (abs(alpha)>1) |
---|
867 | { |
---|
868 | m_pcBinIf->encodeBin(1, pCtx[1]); |
---|
869 | xWriteUnaryMaxSymbol( log2AbsAlphaMinus1Table[abs(alpha) - 1] - 1, (pCtx + 2), 1, 2 ); |
---|
870 | } |
---|
871 | else |
---|
872 | { |
---|
873 | m_pcBinIf->encodeBin(0, pCtx[1]); |
---|
874 | } |
---|
875 | m_pcBinIf->encodeBin( ((alpha < 0) ? 1 : 0), pCtx[4] ); |
---|
876 | } |
---|
877 | DTRACE_CABAC_T( "\tAlpha=" ) |
---|
878 | DTRACE_CABAC_V( pcCU->getCrossComponentPredictionAlpha( uiAbsPartIdx, compID ) ) |
---|
879 | DTRACE_CABAC_T( "\n" ) |
---|
880 | } |
---|
881 | } |
---|
882 | |
---|
883 | Void TEncSbac::codeDeltaQP( TComDataCU* pcCU, UInt uiAbsPartIdx ) |
---|
884 | { |
---|
885 | Int iDQp = pcCU->getQP( uiAbsPartIdx ) - pcCU->getRefQP( uiAbsPartIdx ); |
---|
886 | |
---|
887 | #if SVC_EXTENSION |
---|
888 | Int qpBdOffsetY = pcCU->getSlice()->getQpBDOffsetY(); |
---|
889 | #else |
---|
890 | Int qpBdOffsetY = pcCU->getSlice()->getSPS()->getQpBDOffset(CHANNEL_TYPE_LUMA); |
---|
891 | #endif |
---|
892 | iDQp = (iDQp + 78 + qpBdOffsetY + (qpBdOffsetY/2)) % (52 + qpBdOffsetY) - 26 - (qpBdOffsetY/2); |
---|
893 | |
---|
894 | UInt uiAbsDQp = (UInt)((iDQp > 0)? iDQp : (-iDQp)); |
---|
895 | UInt TUValue = min((Int)uiAbsDQp, CU_DQP_TU_CMAX); |
---|
896 | xWriteUnaryMaxSymbol( TUValue, &m_cCUDeltaQpSCModel.get( 0, 0, 0 ), 1, CU_DQP_TU_CMAX); |
---|
897 | if( uiAbsDQp >= CU_DQP_TU_CMAX ) |
---|
898 | { |
---|
899 | xWriteEpExGolomb( uiAbsDQp - CU_DQP_TU_CMAX, CU_DQP_EG_k ); |
---|
900 | } |
---|
901 | |
---|
902 | if ( uiAbsDQp > 0) |
---|
903 | { |
---|
904 | UInt uiSign = (iDQp > 0 ? 0 : 1); |
---|
905 | m_pcBinIf->encodeBinEP(uiSign); |
---|
906 | } |
---|
907 | |
---|
908 | return; |
---|
909 | } |
---|
910 | |
---|
911 | /** code chroma qp adjustment, converting from the internal table representation |
---|
912 | * \returns Void |
---|
913 | */ |
---|
914 | Void TEncSbac::codeChromaQpAdjustment( TComDataCU* cu, UInt absPartIdx ) |
---|
915 | { |
---|
916 | Int internalIdc = cu->getChromaQpAdj( absPartIdx ); |
---|
917 | Int tableSize = cu->getSlice()->getPPS()->getChromaQpAdjTableSize(); |
---|
918 | /* internal_idc == 0 => flag = 0 |
---|
919 | * internal_idc > 1 => code idc value (if table size warrents) */ |
---|
920 | m_pcBinIf->encodeBin( internalIdc > 0, m_ChromaQpAdjFlagSCModel.get( 0, 0, 0 ) ); |
---|
921 | |
---|
922 | if (internalIdc > 0 && tableSize > 1) |
---|
923 | { |
---|
924 | xWriteUnaryMaxSymbol( internalIdc - 1, &m_ChromaQpAdjIdcSCModel.get( 0, 0, 0 ), 0, tableSize - 1 ); |
---|
925 | } |
---|
926 | } |
---|
927 | |
---|
928 | Void TEncSbac::codeQtCbf( TComTU &rTu, const ComponentID compID, const Bool lowestLevel ) |
---|
929 | { |
---|
930 | TComDataCU* pcCU = rTu.getCU(); |
---|
931 | |
---|
932 | const UInt absPartIdx = rTu.GetAbsPartIdxTU(compID); |
---|
933 | const UInt TUDepth = rTu.GetTransformDepthRel(); |
---|
934 | UInt uiCtx = pcCU->getCtxQtCbf( rTu, toChannelType(compID) ); |
---|
935 | const UInt contextSet = toChannelType(compID); |
---|
936 | |
---|
937 | const UInt width = rTu.getRect(compID).width; |
---|
938 | const UInt height = rTu.getRect(compID).height; |
---|
939 | const Bool canQuadSplit = (width >= (MIN_TU_SIZE * 2)) && (height >= (MIN_TU_SIZE * 2)); |
---|
940 | |
---|
941 | // Since the CBF for chroma is coded at the highest level possible, if sub-TUs are |
---|
942 | // to be coded for a 4x8 chroma TU, their CBFs must be coded at the highest 4x8 level |
---|
943 | // (i.e. where luma TUs are 8x8 rather than 4x4) |
---|
944 | // ___ ___ |
---|
945 | // | | | <- 4 x (8x8 luma + 4x8 4:2:2 chroma) |
---|
946 | // |___|___| each quadrant has its own chroma CBF |
---|
947 | // | | | _ _ _ _ |
---|
948 | // |___|___| | |
---|
949 | // <--16---> V |
---|
950 | // _ _ |
---|
951 | // |_|_| <- 4 x 4x4 luma + 1 x 4x8 4:2:2 chroma |
---|
952 | // |_|_| no chroma CBF is coded - instead the parent CBF is inherited |
---|
953 | // <-8-> if sub-TUs are present, their CBFs had to be coded at the parent level |
---|
954 | |
---|
955 | const UInt lowestTUDepth = TUDepth + ((!lowestLevel && !canQuadSplit) ? 1 : 0); //unsplittable TUs inherit their parent's CBF |
---|
956 | |
---|
957 | if ((width != height) && (lowestLevel || !canQuadSplit)) //if sub-TUs are present |
---|
958 | { |
---|
959 | const UInt subTUDepth = lowestTUDepth + 1; //if this is the lowest level of the TU-tree, the sub-TUs are directly below. Otherwise, this must be the level above the lowest level (as specified above) |
---|
960 | const UInt partIdxesPerSubTU = rTu.GetAbsPartIdxNumParts(compID) >> 1; |
---|
961 | |
---|
962 | for (UInt subTU = 0; subTU < 2; subTU++) |
---|
963 | { |
---|
964 | const UInt subTUAbsPartIdx = absPartIdx + (subTU * partIdxesPerSubTU); |
---|
965 | const UInt uiCbf = pcCU->getCbf(subTUAbsPartIdx, compID, subTUDepth); |
---|
966 | |
---|
967 | m_pcBinIf->encodeBin(uiCbf, m_cCUQtCbfSCModel.get(0, contextSet, uiCtx)); |
---|
968 | |
---|
969 | DTRACE_CABAC_VL( g_nSymbolCounter++ ) |
---|
970 | DTRACE_CABAC_T( "\tparseQtCbf()" ) |
---|
971 | DTRACE_CABAC_T( "\tsub-TU=" ) |
---|
972 | DTRACE_CABAC_V( subTU ) |
---|
973 | DTRACE_CABAC_T( "\tsymbol=" ) |
---|
974 | DTRACE_CABAC_V( uiCbf ) |
---|
975 | DTRACE_CABAC_T( "\tctx=" ) |
---|
976 | DTRACE_CABAC_V( uiCtx ) |
---|
977 | DTRACE_CABAC_T( "\tetype=" ) |
---|
978 | DTRACE_CABAC_V( compID ) |
---|
979 | DTRACE_CABAC_T( "\tuiAbsPartIdx=" ) |
---|
980 | DTRACE_CABAC_V( subTUAbsPartIdx ) |
---|
981 | DTRACE_CABAC_T( "\n" ) |
---|
982 | } |
---|
983 | } |
---|
984 | else |
---|
985 | { |
---|
986 | const UInt uiCbf = pcCU->getCbf( absPartIdx, compID, lowestTUDepth ); |
---|
987 | m_pcBinIf->encodeBin( uiCbf , m_cCUQtCbfSCModel.get( 0, contextSet, uiCtx ) ); |
---|
988 | |
---|
989 | |
---|
990 | DTRACE_CABAC_VL( g_nSymbolCounter++ ) |
---|
991 | DTRACE_CABAC_T( "\tparseQtCbf()" ) |
---|
992 | DTRACE_CABAC_T( "\tsymbol=" ) |
---|
993 | DTRACE_CABAC_V( uiCbf ) |
---|
994 | DTRACE_CABAC_T( "\tctx=" ) |
---|
995 | DTRACE_CABAC_V( uiCtx ) |
---|
996 | DTRACE_CABAC_T( "\tetype=" ) |
---|
997 | DTRACE_CABAC_V( compID ) |
---|
998 | DTRACE_CABAC_T( "\tuiAbsPartIdx=" ) |
---|
999 | DTRACE_CABAC_V( rTu.GetAbsPartIdxTU(compID) ) |
---|
1000 | DTRACE_CABAC_T( "\n" ) |
---|
1001 | } |
---|
1002 | } |
---|
1003 | |
---|
1004 | |
---|
1005 | Void TEncSbac::codeTransformSkipFlags (TComTU &rTu, ComponentID component ) |
---|
1006 | { |
---|
1007 | TComDataCU* pcCU=rTu.getCU(); |
---|
1008 | const UInt uiAbsPartIdx=rTu.GetAbsPartIdxTU(); |
---|
1009 | |
---|
1010 | if (pcCU->getCUTransquantBypass(uiAbsPartIdx)) |
---|
1011 | { |
---|
1012 | return; |
---|
1013 | } |
---|
1014 | |
---|
1015 | if (!TUCompRectHasAssociatedTransformSkipFlag(rTu.getRect(component), pcCU->getSlice()->getPPS()->getTransformSkipLog2MaxSize())) |
---|
1016 | { |
---|
1017 | return; |
---|
1018 | } |
---|
1019 | |
---|
1020 | UInt useTransformSkip = pcCU->getTransformSkip( uiAbsPartIdx,component); |
---|
1021 | m_pcBinIf->encodeBin( useTransformSkip, m_cTransformSkipSCModel.get( 0, toChannelType(component), 0 ) ); |
---|
1022 | |
---|
1023 | DTRACE_CABAC_VL( g_nSymbolCounter++ ) |
---|
1024 | DTRACE_CABAC_T("\tparseTransformSkip()"); |
---|
1025 | DTRACE_CABAC_T( "\tsymbol=" ) |
---|
1026 | DTRACE_CABAC_V( useTransformSkip ) |
---|
1027 | DTRACE_CABAC_T( "\tAddr=" ) |
---|
1028 | DTRACE_CABAC_V( pcCU->getCtuRsAddr() ) |
---|
1029 | DTRACE_CABAC_T( "\tetype=" ) |
---|
1030 | DTRACE_CABAC_V( component ) |
---|
1031 | DTRACE_CABAC_T( "\tuiAbsPartIdx=" ) |
---|
1032 | DTRACE_CABAC_V( rTu.GetAbsPartIdxTU() ) |
---|
1033 | DTRACE_CABAC_T( "\n" ) |
---|
1034 | } |
---|
1035 | |
---|
1036 | |
---|
1037 | /** Code I_PCM information. |
---|
1038 | * \param pcCU pointer to CU |
---|
1039 | * \param uiAbsPartIdx CU index |
---|
1040 | * \returns Void |
---|
1041 | */ |
---|
1042 | Void TEncSbac::codeIPCMInfo( TComDataCU* pcCU, UInt uiAbsPartIdx ) |
---|
1043 | { |
---|
1044 | UInt uiIPCM = (pcCU->getIPCMFlag(uiAbsPartIdx) == true)? 1 : 0; |
---|
1045 | |
---|
1046 | Bool writePCMSampleFlag = pcCU->getIPCMFlag(uiAbsPartIdx); |
---|
1047 | |
---|
1048 | m_pcBinIf->encodeBinTrm (uiIPCM); |
---|
1049 | |
---|
1050 | if (writePCMSampleFlag) |
---|
1051 | { |
---|
1052 | m_pcBinIf->encodePCMAlignBits(); |
---|
1053 | |
---|
1054 | const UInt minCoeffSizeY = pcCU->getPic()->getMinCUWidth() * pcCU->getPic()->getMinCUHeight(); |
---|
1055 | const UInt offsetY = minCoeffSizeY * uiAbsPartIdx; |
---|
1056 | for (UInt ch=0; ch < pcCU->getPic()->getNumberValidComponents(); ch++) |
---|
1057 | { |
---|
1058 | const ComponentID compID = ComponentID(ch); |
---|
1059 | const UInt offset = offsetY >> (pcCU->getPic()->getComponentScaleX(compID) + pcCU->getPic()->getComponentScaleY(compID)); |
---|
1060 | Pel * pPCMSample = pcCU->getPCMSample(compID) + offset; |
---|
1061 | const UInt width = pcCU->getWidth (uiAbsPartIdx) >> pcCU->getPic()->getComponentScaleX(compID); |
---|
1062 | const UInt height = pcCU->getHeight(uiAbsPartIdx) >> pcCU->getPic()->getComponentScaleY(compID); |
---|
1063 | const UInt sampleBits = pcCU->getSlice()->getSPS()->getPCMBitDepth(toChannelType(compID)); |
---|
1064 | for (UInt y=0; y<height; y++) |
---|
1065 | { |
---|
1066 | for (UInt x=0; x<width; x++) |
---|
1067 | { |
---|
1068 | UInt sample = pPCMSample[x]; |
---|
1069 | m_pcBinIf->xWritePCMCode(sample, sampleBits); |
---|
1070 | } |
---|
1071 | pPCMSample += width; |
---|
1072 | } |
---|
1073 | } |
---|
1074 | |
---|
1075 | m_pcBinIf->resetBac(); |
---|
1076 | } |
---|
1077 | } |
---|
1078 | |
---|
1079 | Void TEncSbac::codeQtRootCbf( TComDataCU* pcCU, UInt uiAbsPartIdx ) |
---|
1080 | { |
---|
1081 | UInt uiCbf = pcCU->getQtRootCbf( uiAbsPartIdx ); |
---|
1082 | UInt uiCtx = 0; |
---|
1083 | m_pcBinIf->encodeBin( uiCbf , m_cCUQtRootCbfSCModel.get( 0, 0, uiCtx ) ); |
---|
1084 | DTRACE_CABAC_VL( g_nSymbolCounter++ ) |
---|
1085 | DTRACE_CABAC_T( "\tparseQtRootCbf()" ) |
---|
1086 | DTRACE_CABAC_T( "\tsymbol=" ) |
---|
1087 | DTRACE_CABAC_V( uiCbf ) |
---|
1088 | DTRACE_CABAC_T( "\tctx=" ) |
---|
1089 | DTRACE_CABAC_V( uiCtx ) |
---|
1090 | DTRACE_CABAC_T( "\tuiAbsPartIdx=" ) |
---|
1091 | DTRACE_CABAC_V( uiAbsPartIdx ) |
---|
1092 | DTRACE_CABAC_T( "\n" ) |
---|
1093 | } |
---|
1094 | |
---|
1095 | Void TEncSbac::codeQtCbfZero( TComTU & rTu, const ChannelType chType ) |
---|
1096 | { |
---|
1097 | // this function is only used to estimate the bits when cbf is 0 |
---|
1098 | // and will never be called when writing the bistream. do not need to write log |
---|
1099 | UInt uiCbf = 0; |
---|
1100 | UInt uiCtx = rTu.getCU()->getCtxQtCbf( rTu, chType ); |
---|
1101 | |
---|
1102 | m_pcBinIf->encodeBin( uiCbf , m_cCUQtCbfSCModel.get( 0, chType, uiCtx ) ); |
---|
1103 | } |
---|
1104 | |
---|
1105 | Void TEncSbac::codeQtRootCbfZero( TComDataCU* pcCU ) |
---|
1106 | { |
---|
1107 | // this function is only used to estimate the bits when cbf is 0 |
---|
1108 | // and will never be called when writing the bistream. do not need to write log |
---|
1109 | UInt uiCbf = 0; |
---|
1110 | UInt uiCtx = 0; |
---|
1111 | m_pcBinIf->encodeBin( uiCbf , m_cCUQtRootCbfSCModel.get( 0, 0, uiCtx ) ); |
---|
1112 | } |
---|
1113 | |
---|
1114 | /** Encode (X,Y) position of the last significant coefficient |
---|
1115 | * \param uiPosX X component of last coefficient |
---|
1116 | * \param uiPosY Y component of last coefficient |
---|
1117 | * \param width Block width |
---|
1118 | * \param height Block height |
---|
1119 | * \param component chroma component ID |
---|
1120 | * \param uiScanIdx scan type (zig-zag, hor, ver) |
---|
1121 | * This method encodes the X and Y component within a block of the last significant coefficient. |
---|
1122 | */ |
---|
1123 | Void TEncSbac::codeLastSignificantXY( UInt uiPosX, UInt uiPosY, Int width, Int height, ComponentID component, UInt uiScanIdx ) |
---|
1124 | { |
---|
1125 | // swap |
---|
1126 | if( uiScanIdx == SCAN_VER ) |
---|
1127 | { |
---|
1128 | swap( uiPosX, uiPosY ); |
---|
1129 | swap( width, height ); |
---|
1130 | } |
---|
1131 | |
---|
1132 | UInt uiCtxLast; |
---|
1133 | UInt uiGroupIdxX = g_uiGroupIdx[ uiPosX ]; |
---|
1134 | UInt uiGroupIdxY = g_uiGroupIdx[ uiPosY ]; |
---|
1135 | |
---|
1136 | ContextModel *pCtxX = m_cCuCtxLastX.get( 0, toChannelType(component) ); |
---|
1137 | ContextModel *pCtxY = m_cCuCtxLastY.get( 0, toChannelType(component) ); |
---|
1138 | |
---|
1139 | Int blkSizeOffsetX, blkSizeOffsetY, shiftX, shiftY; |
---|
1140 | getLastSignificantContextParameters(component, width, height, blkSizeOffsetX, blkSizeOffsetY, shiftX, shiftY); |
---|
1141 | |
---|
1142 | //------------------ |
---|
1143 | |
---|
1144 | // posX |
---|
1145 | |
---|
1146 | for( uiCtxLast = 0; uiCtxLast < uiGroupIdxX; uiCtxLast++ ) |
---|
1147 | { |
---|
1148 | m_pcBinIf->encodeBin( 1, *( pCtxX + blkSizeOffsetX + (uiCtxLast >>shiftX) ) ); |
---|
1149 | } |
---|
1150 | if( uiGroupIdxX < g_uiGroupIdx[ width - 1 ]) |
---|
1151 | { |
---|
1152 | m_pcBinIf->encodeBin( 0, *( pCtxX + blkSizeOffsetX + (uiCtxLast >>shiftX) ) ); |
---|
1153 | } |
---|
1154 | |
---|
1155 | // posY |
---|
1156 | |
---|
1157 | for( uiCtxLast = 0; uiCtxLast < uiGroupIdxY; uiCtxLast++ ) |
---|
1158 | { |
---|
1159 | m_pcBinIf->encodeBin( 1, *( pCtxY + blkSizeOffsetY + (uiCtxLast >>shiftY) ) ); |
---|
1160 | } |
---|
1161 | if( uiGroupIdxY < g_uiGroupIdx[ height - 1 ]) |
---|
1162 | { |
---|
1163 | m_pcBinIf->encodeBin( 0, *( pCtxY + blkSizeOffsetY + (uiCtxLast >>shiftY) ) ); |
---|
1164 | } |
---|
1165 | |
---|
1166 | // EP-coded part |
---|
1167 | |
---|
1168 | if ( uiGroupIdxX > 3 ) |
---|
1169 | { |
---|
1170 | UInt uiCount = ( uiGroupIdxX - 2 ) >> 1; |
---|
1171 | uiPosX = uiPosX - g_uiMinInGroup[ uiGroupIdxX ]; |
---|
1172 | for (Int i = uiCount - 1 ; i >= 0; i-- ) |
---|
1173 | { |
---|
1174 | m_pcBinIf->encodeBinEP( ( uiPosX >> i ) & 1 ); |
---|
1175 | } |
---|
1176 | } |
---|
1177 | if ( uiGroupIdxY > 3 ) |
---|
1178 | { |
---|
1179 | UInt uiCount = ( uiGroupIdxY - 2 ) >> 1; |
---|
1180 | uiPosY = uiPosY - g_uiMinInGroup[ uiGroupIdxY ]; |
---|
1181 | for ( Int i = uiCount - 1 ; i >= 0; i-- ) |
---|
1182 | { |
---|
1183 | m_pcBinIf->encodeBinEP( ( uiPosY >> i ) & 1 ); |
---|
1184 | } |
---|
1185 | } |
---|
1186 | } |
---|
1187 | |
---|
1188 | |
---|
1189 | Void TEncSbac::codeCoeffNxN( TComTU &rTu, TCoeff* pcCoef, const ComponentID compID ) |
---|
1190 | { |
---|
1191 | TComDataCU* pcCU=rTu.getCU(); |
---|
1192 | const UInt uiAbsPartIdx=rTu.GetAbsPartIdxTU(compID); |
---|
1193 | const TComRectangle &tuRect=rTu.getRect(compID); |
---|
1194 | const UInt uiWidth=tuRect.width; |
---|
1195 | const UInt uiHeight=tuRect.height; |
---|
1196 | |
---|
1197 | DTRACE_CABAC_VL( g_nSymbolCounter++ ) |
---|
1198 | DTRACE_CABAC_T( "\tparseCoeffNxN()\teType=" ) |
---|
1199 | DTRACE_CABAC_V( compID ) |
---|
1200 | DTRACE_CABAC_T( "\twidth=" ) |
---|
1201 | DTRACE_CABAC_V( uiWidth ) |
---|
1202 | DTRACE_CABAC_T( "\theight=" ) |
---|
1203 | DTRACE_CABAC_V( uiHeight ) |
---|
1204 | DTRACE_CABAC_T( "\tdepth=" ) |
---|
1205 | // DTRACE_CABAC_V( rTu.GetTransformDepthTotalAdj(compID) ) |
---|
1206 | DTRACE_CABAC_V( rTu.GetTransformDepthTotal() ) |
---|
1207 | DTRACE_CABAC_T( "\tabspartidx=" ) |
---|
1208 | DTRACE_CABAC_V( uiAbsPartIdx ) |
---|
1209 | DTRACE_CABAC_T( "\ttoCU-X=" ) |
---|
1210 | DTRACE_CABAC_V( pcCU->getCUPelX() ) |
---|
1211 | DTRACE_CABAC_T( "\ttoCU-Y=" ) |
---|
1212 | DTRACE_CABAC_V( pcCU->getCUPelY() ) |
---|
1213 | DTRACE_CABAC_T( "\tCU-addr=" ) |
---|
1214 | DTRACE_CABAC_V( pcCU->getCtuRsAddr() ) |
---|
1215 | DTRACE_CABAC_T( "\tinCU-X=" ) |
---|
1216 | // DTRACE_CABAC_V( g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ] ) |
---|
1217 | DTRACE_CABAC_V( g_auiRasterToPelX[ g_auiZscanToRaster[rTu.GetAbsPartIdxTU(compID)] ] ) |
---|
1218 | DTRACE_CABAC_T( "\tinCU-Y=" ) |
---|
1219 | // DTRACE_CABAC_V( g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ] ) |
---|
1220 | DTRACE_CABAC_V( g_auiRasterToPelY[ g_auiZscanToRaster[rTu.GetAbsPartIdxTU(compID)] ] ) |
---|
1221 | DTRACE_CABAC_T( "\tpredmode=" ) |
---|
1222 | DTRACE_CABAC_V( pcCU->getPredictionMode( uiAbsPartIdx ) ) |
---|
1223 | DTRACE_CABAC_T( "\n" ) |
---|
1224 | |
---|
1225 | //-------------------------------------------------------------------------------------------------- |
---|
1226 | |
---|
1227 | if( uiWidth > m_pcSlice->getSPS()->getMaxTrSize() ) |
---|
1228 | { |
---|
1229 | std::cerr << "ERROR: codeCoeffNxN was passed a TU with dimensions larger than the maximum allowed size" << std::endl; |
---|
1230 | assert(false); |
---|
1231 | exit(1); |
---|
1232 | } |
---|
1233 | |
---|
1234 | // compute number of significant coefficients |
---|
1235 | UInt uiNumSig = TEncEntropy::countNonZeroCoeffs(pcCoef, uiWidth * uiHeight); |
---|
1236 | |
---|
1237 | if ( uiNumSig == 0 ) |
---|
1238 | { |
---|
1239 | std::cerr << "ERROR: codeCoeffNxN called for empty TU!" << std::endl; |
---|
1240 | assert(false); |
---|
1241 | exit(1); |
---|
1242 | } |
---|
1243 | |
---|
1244 | //-------------------------------------------------------------------------------------------------- |
---|
1245 | |
---|
1246 | //set parameters |
---|
1247 | |
---|
1248 | const ChannelType chType = toChannelType(compID); |
---|
1249 | const UInt uiLog2BlockWidth = g_aucConvertToBit[ uiWidth ] + 2; |
---|
1250 | const UInt uiLog2BlockHeight = g_aucConvertToBit[ uiHeight ] + 2; |
---|
1251 | |
---|
1252 | const ChannelType channelType = toChannelType(compID); |
---|
1253 | const Bool extendedPrecision = pcCU->getSlice()->getSPS()->getUseExtendedPrecision(); |
---|
1254 | |
---|
1255 | const Bool alignCABACBeforeBypass = pcCU->getSlice()->getSPS()->getAlignCABACBeforeBypass(); |
---|
1256 | const Int maxLog2TrDynamicRange = pcCU->getSlice()->getSPS()->getMaxLog2TrDynamicRange(channelType); |
---|
1257 | |
---|
1258 | Bool beValid; |
---|
1259 | |
---|
1260 | { |
---|
1261 | Int uiIntraMode = -1; |
---|
1262 | const Bool bIsLuma = isLuma(compID); |
---|
1263 | Int isIntra = pcCU->isIntra(uiAbsPartIdx) ? 1 : 0; |
---|
1264 | if ( isIntra ) |
---|
1265 | { |
---|
1266 | uiIntraMode = pcCU->getIntraDir( toChannelType(compID), uiAbsPartIdx ); |
---|
1267 | |
---|
1268 | uiIntraMode = (uiIntraMode==DM_CHROMA_IDX && !bIsLuma) ? pcCU->getIntraDir(CHANNEL_TYPE_LUMA, getChromasCorrespondingPULumaIdx(uiAbsPartIdx, rTu.GetChromaFormat())) : uiIntraMode; |
---|
1269 | uiIntraMode = ((rTu.GetChromaFormat() == CHROMA_422) && !bIsLuma) ? g_chroma422IntraAngleMappingTable[uiIntraMode] : uiIntraMode; |
---|
1270 | } |
---|
1271 | |
---|
1272 | Int transformSkip = pcCU->getTransformSkip( uiAbsPartIdx,compID) ? 1 : 0; |
---|
1273 | Bool rdpcm_lossy = ( transformSkip && isIntra && ( (uiIntraMode == HOR_IDX) || (uiIntraMode == VER_IDX) ) ) && pcCU->isRDPCMEnabled(uiAbsPartIdx); |
---|
1274 | |
---|
1275 | if ( (pcCU->getCUTransquantBypass(uiAbsPartIdx)) || rdpcm_lossy ) |
---|
1276 | { |
---|
1277 | beValid = false; |
---|
1278 | if ( (!pcCU->isIntra(uiAbsPartIdx)) && pcCU->isRDPCMEnabled(uiAbsPartIdx)) |
---|
1279 | { |
---|
1280 | codeExplicitRdpcmMode( rTu, compID); |
---|
1281 | } |
---|
1282 | } |
---|
1283 | else |
---|
1284 | { |
---|
1285 | beValid = pcCU->getSlice()->getPPS()->getSignHideFlag(); |
---|
1286 | } |
---|
1287 | } |
---|
1288 | |
---|
1289 | //-------------------------------------------------------------------------------------------------- |
---|
1290 | |
---|
1291 | if(pcCU->getSlice()->getPPS()->getUseTransformSkip()) |
---|
1292 | { |
---|
1293 | codeTransformSkipFlags(rTu, compID); |
---|
1294 | if(pcCU->getTransformSkip(uiAbsPartIdx, compID) && !pcCU->isIntra(uiAbsPartIdx) && pcCU->isRDPCMEnabled(uiAbsPartIdx)) |
---|
1295 | { |
---|
1296 | // This TU has coefficients and is transform skipped. Check whether is inter coded and if yes encode the explicit RDPCM mode |
---|
1297 | codeExplicitRdpcmMode( rTu, compID); |
---|
1298 | |
---|
1299 | if(pcCU->getExplicitRdpcmMode(compID, uiAbsPartIdx) != RDPCM_OFF) |
---|
1300 | { |
---|
1301 | // Sign data hiding is avoided for horizontal and vertical explicit RDPCM modes |
---|
1302 | beValid = false; |
---|
1303 | } |
---|
1304 | } |
---|
1305 | } |
---|
1306 | |
---|
1307 | //-------------------------------------------------------------------------------------------------- |
---|
1308 | |
---|
1309 | const Bool bUseGolombRiceParameterAdaptation = pcCU->getSlice()->getSPS()->getUseGolombRiceParameterAdaptation(); |
---|
1310 | UInt ¤tGolombRiceStatistic = m_golombRiceAdaptationStatistics[rTu.getGolombRiceStatisticsIndex(compID)]; |
---|
1311 | |
---|
1312 | //select scans |
---|
1313 | TUEntropyCodingParameters codingParameters; |
---|
1314 | getTUEntropyCodingParameters(codingParameters, rTu, compID); |
---|
1315 | |
---|
1316 | //----- encode significance map ----- |
---|
1317 | |
---|
1318 | // Find position of last coefficient |
---|
1319 | Int scanPosLast = -1; |
---|
1320 | Int posLast; |
---|
1321 | |
---|
1322 | |
---|
1323 | UInt uiSigCoeffGroupFlag[ MLS_GRP_NUM ]; |
---|
1324 | |
---|
1325 | memset( uiSigCoeffGroupFlag, 0, sizeof(UInt) * MLS_GRP_NUM ); |
---|
1326 | do |
---|
1327 | { |
---|
1328 | posLast = codingParameters.scan[ ++scanPosLast ]; |
---|
1329 | |
---|
1330 | if( pcCoef[ posLast ] != 0 ) |
---|
1331 | { |
---|
1332 | // get L1 sig map |
---|
1333 | UInt uiPosY = posLast >> uiLog2BlockWidth; |
---|
1334 | UInt uiPosX = posLast - ( uiPosY << uiLog2BlockWidth ); |
---|
1335 | |
---|
1336 | UInt uiBlkIdx = (codingParameters.widthInGroups * (uiPosY >> MLS_CG_LOG2_HEIGHT)) + (uiPosX >> MLS_CG_LOG2_WIDTH); |
---|
1337 | uiSigCoeffGroupFlag[ uiBlkIdx ] = 1; |
---|
1338 | |
---|
1339 | uiNumSig--; |
---|
1340 | } |
---|
1341 | } while ( uiNumSig > 0 ); |
---|
1342 | |
---|
1343 | // Code position of last coefficient |
---|
1344 | Int posLastY = posLast >> uiLog2BlockWidth; |
---|
1345 | Int posLastX = posLast - ( posLastY << uiLog2BlockWidth ); |
---|
1346 | codeLastSignificantXY(posLastX, posLastY, uiWidth, uiHeight, compID, codingParameters.scanType); |
---|
1347 | |
---|
1348 | //===== code significance flag ===== |
---|
1349 | ContextModel * const baseCoeffGroupCtx = m_cCUSigCoeffGroupSCModel.get( 0, chType ); |
---|
1350 | ContextModel * const baseCtx = m_cCUSigSCModel.get( 0, 0 ) + getSignificanceMapContextOffset(compID); |
---|
1351 | |
---|
1352 | const Int iLastScanSet = scanPosLast >> MLS_CG_SIZE; |
---|
1353 | |
---|
1354 | UInt c1 = 1; |
---|
1355 | UInt uiGoRiceParam = 0; |
---|
1356 | Int iScanPosSig = scanPosLast; |
---|
1357 | |
---|
1358 | for( Int iSubSet = iLastScanSet; iSubSet >= 0; iSubSet-- ) |
---|
1359 | { |
---|
1360 | Int numNonZero = 0; |
---|
1361 | Int iSubPos = iSubSet << MLS_CG_SIZE; |
---|
1362 | uiGoRiceParam = currentGolombRiceStatistic / RExt__GOLOMB_RICE_INCREMENT_DIVISOR; |
---|
1363 | Bool updateGolombRiceStatistics = bUseGolombRiceParameterAdaptation; //leave the statistics at 0 when not using the adaptation system |
---|
1364 | UInt coeffSigns = 0; |
---|
1365 | |
---|
1366 | Int absCoeff[1 << MLS_CG_SIZE]; |
---|
1367 | |
---|
1368 | Int lastNZPosInCG = -1; |
---|
1369 | Int firstNZPosInCG = 1 << MLS_CG_SIZE; |
---|
1370 | |
---|
1371 | Bool escapeDataPresentInGroup = false; |
---|
1372 | |
---|
1373 | if( iScanPosSig == scanPosLast ) |
---|
1374 | { |
---|
1375 | absCoeff[ 0 ] = Int(abs( pcCoef[ posLast ] )); |
---|
1376 | coeffSigns = ( pcCoef[ posLast ] < 0 ); |
---|
1377 | numNonZero = 1; |
---|
1378 | lastNZPosInCG = iScanPosSig; |
---|
1379 | firstNZPosInCG = iScanPosSig; |
---|
1380 | iScanPosSig--; |
---|
1381 | } |
---|
1382 | |
---|
1383 | // encode significant_coeffgroup_flag |
---|
1384 | Int iCGBlkPos = codingParameters.scanCG[ iSubSet ]; |
---|
1385 | Int iCGPosY = iCGBlkPos / codingParameters.widthInGroups; |
---|
1386 | Int iCGPosX = iCGBlkPos - (iCGPosY * codingParameters.widthInGroups); |
---|
1387 | |
---|
1388 | if( iSubSet == iLastScanSet || iSubSet == 0) |
---|
1389 | { |
---|
1390 | uiSigCoeffGroupFlag[ iCGBlkPos ] = 1; |
---|
1391 | } |
---|
1392 | else |
---|
1393 | { |
---|
1394 | UInt uiSigCoeffGroup = (uiSigCoeffGroupFlag[ iCGBlkPos ] != 0); |
---|
1395 | UInt uiCtxSig = TComTrQuant::getSigCoeffGroupCtxInc( uiSigCoeffGroupFlag, iCGPosX, iCGPosY, codingParameters.widthInGroups, codingParameters.heightInGroups ); |
---|
1396 | m_pcBinIf->encodeBin( uiSigCoeffGroup, baseCoeffGroupCtx[ uiCtxSig ] ); |
---|
1397 | } |
---|
1398 | |
---|
1399 | // encode significant_coeff_flag |
---|
1400 | if( uiSigCoeffGroupFlag[ iCGBlkPos ] ) |
---|
1401 | { |
---|
1402 | const Int patternSigCtx = TComTrQuant::calcPatternSigCtx(uiSigCoeffGroupFlag, iCGPosX, iCGPosY, codingParameters.widthInGroups, codingParameters.heightInGroups); |
---|
1403 | |
---|
1404 | UInt uiBlkPos, uiSig, uiCtxSig; |
---|
1405 | for( ; iScanPosSig >= iSubPos; iScanPosSig-- ) |
---|
1406 | { |
---|
1407 | uiBlkPos = codingParameters.scan[ iScanPosSig ]; |
---|
1408 | uiSig = (pcCoef[ uiBlkPos ] != 0); |
---|
1409 | if( iScanPosSig > iSubPos || iSubSet == 0 || numNonZero ) |
---|
1410 | { |
---|
1411 | uiCtxSig = TComTrQuant::getSigCtxInc( patternSigCtx, codingParameters, iScanPosSig, uiLog2BlockWidth, uiLog2BlockHeight, chType ); |
---|
1412 | m_pcBinIf->encodeBin( uiSig, baseCtx[ uiCtxSig ] ); |
---|
1413 | } |
---|
1414 | if( uiSig ) |
---|
1415 | { |
---|
1416 | absCoeff[ numNonZero ] = Int(abs( pcCoef[ uiBlkPos ] )); |
---|
1417 | coeffSigns = 2 * coeffSigns + ( pcCoef[ uiBlkPos ] < 0 ); |
---|
1418 | numNonZero++; |
---|
1419 | if( lastNZPosInCG == -1 ) |
---|
1420 | { |
---|
1421 | lastNZPosInCG = iScanPosSig; |
---|
1422 | } |
---|
1423 | firstNZPosInCG = iScanPosSig; |
---|
1424 | } |
---|
1425 | } |
---|
1426 | } |
---|
1427 | else |
---|
1428 | { |
---|
1429 | iScanPosSig = iSubPos - 1; |
---|
1430 | } |
---|
1431 | |
---|
1432 | if( numNonZero > 0 ) |
---|
1433 | { |
---|
1434 | Bool signHidden = ( lastNZPosInCG - firstNZPosInCG >= SBH_THRESHOLD ); |
---|
1435 | |
---|
1436 | const UInt uiCtxSet = getContextSetIndex(compID, iSubSet, (c1 == 0)); |
---|
1437 | c1 = 1; |
---|
1438 | |
---|
1439 | ContextModel *baseCtxMod = m_cCUOneSCModel.get( 0, 0 ) + (NUM_ONE_FLAG_CTX_PER_SET * uiCtxSet); |
---|
1440 | |
---|
1441 | Int numC1Flag = min(numNonZero, C1FLAG_NUMBER); |
---|
1442 | Int firstC2FlagIdx = -1; |
---|
1443 | for( Int idx = 0; idx < numC1Flag; idx++ ) |
---|
1444 | { |
---|
1445 | UInt uiSymbol = absCoeff[ idx ] > 1; |
---|
1446 | m_pcBinIf->encodeBin( uiSymbol, baseCtxMod[c1] ); |
---|
1447 | if( uiSymbol ) |
---|
1448 | { |
---|
1449 | c1 = 0; |
---|
1450 | |
---|
1451 | if (firstC2FlagIdx == -1) |
---|
1452 | { |
---|
1453 | firstC2FlagIdx = idx; |
---|
1454 | } |
---|
1455 | else //if a greater-than-one has been encountered already this group |
---|
1456 | { |
---|
1457 | escapeDataPresentInGroup = true; |
---|
1458 | } |
---|
1459 | } |
---|
1460 | else if( (c1 < 3) && (c1 > 0) ) |
---|
1461 | { |
---|
1462 | c1++; |
---|
1463 | } |
---|
1464 | } |
---|
1465 | |
---|
1466 | if (c1 == 0) |
---|
1467 | { |
---|
1468 | baseCtxMod = m_cCUAbsSCModel.get( 0, 0 ) + (NUM_ABS_FLAG_CTX_PER_SET * uiCtxSet); |
---|
1469 | if ( firstC2FlagIdx != -1) |
---|
1470 | { |
---|
1471 | UInt symbol = absCoeff[ firstC2FlagIdx ] > 2; |
---|
1472 | m_pcBinIf->encodeBin( symbol, baseCtxMod[0] ); |
---|
1473 | if (symbol != 0) |
---|
1474 | { |
---|
1475 | escapeDataPresentInGroup = true; |
---|
1476 | } |
---|
1477 | } |
---|
1478 | } |
---|
1479 | |
---|
1480 | escapeDataPresentInGroup = escapeDataPresentInGroup || (numNonZero > C1FLAG_NUMBER); |
---|
1481 | |
---|
1482 | if (escapeDataPresentInGroup && alignCABACBeforeBypass) |
---|
1483 | { |
---|
1484 | m_pcBinIf->align(); |
---|
1485 | } |
---|
1486 | |
---|
1487 | if( beValid && signHidden ) |
---|
1488 | { |
---|
1489 | m_pcBinIf->encodeBinsEP( (coeffSigns >> 1), numNonZero-1 ); |
---|
1490 | } |
---|
1491 | else |
---|
1492 | { |
---|
1493 | m_pcBinIf->encodeBinsEP( coeffSigns, numNonZero ); |
---|
1494 | } |
---|
1495 | |
---|
1496 | Int iFirstCoeff2 = 1; |
---|
1497 | if (escapeDataPresentInGroup) |
---|
1498 | { |
---|
1499 | for ( Int idx = 0; idx < numNonZero; idx++ ) |
---|
1500 | { |
---|
1501 | UInt baseLevel = (idx < C1FLAG_NUMBER)? (2 + iFirstCoeff2 ) : 1; |
---|
1502 | |
---|
1503 | if( absCoeff[ idx ] >= baseLevel) |
---|
1504 | { |
---|
1505 | const UInt escapeCodeValue = absCoeff[idx] - baseLevel; |
---|
1506 | |
---|
1507 | xWriteCoefRemainExGolomb( escapeCodeValue, uiGoRiceParam, extendedPrecision, channelType, maxLog2TrDynamicRange ); |
---|
1508 | |
---|
1509 | if (absCoeff[idx] > (3 << uiGoRiceParam)) |
---|
1510 | { |
---|
1511 | uiGoRiceParam = bUseGolombRiceParameterAdaptation ? (uiGoRiceParam + 1) : (std::min<UInt>((uiGoRiceParam + 1), 4)); |
---|
1512 | } |
---|
1513 | |
---|
1514 | if (updateGolombRiceStatistics) |
---|
1515 | { |
---|
1516 | const UInt initialGolombRiceParameter = currentGolombRiceStatistic / RExt__GOLOMB_RICE_INCREMENT_DIVISOR; |
---|
1517 | |
---|
1518 | if (escapeCodeValue >= (3 << initialGolombRiceParameter)) |
---|
1519 | { |
---|
1520 | currentGolombRiceStatistic++; |
---|
1521 | } |
---|
1522 | else if (((escapeCodeValue * 2) < (1 << initialGolombRiceParameter)) && (currentGolombRiceStatistic > 0)) |
---|
1523 | { |
---|
1524 | currentGolombRiceStatistic--; |
---|
1525 | } |
---|
1526 | |
---|
1527 | updateGolombRiceStatistics = false; |
---|
1528 | } |
---|
1529 | } |
---|
1530 | |
---|
1531 | if(absCoeff[ idx ] >= 2) |
---|
1532 | { |
---|
1533 | iFirstCoeff2 = 0; |
---|
1534 | } |
---|
1535 | } |
---|
1536 | } |
---|
1537 | } |
---|
1538 | } |
---|
1539 | #if ENVIRONMENT_VARIABLE_DEBUG_AND_TEST |
---|
1540 | printSBACCoeffData(posLastX, posLastY, uiWidth, uiHeight, compID, uiAbsPartIdx, codingParameters.scanType, pcCoef, pcCU->getSlice()->getFinalized()); |
---|
1541 | #endif |
---|
1542 | |
---|
1543 | return; |
---|
1544 | } |
---|
1545 | |
---|
1546 | /** code SAO offset sign |
---|
1547 | * \param code sign value |
---|
1548 | */ |
---|
1549 | Void TEncSbac::codeSAOSign( UInt code ) |
---|
1550 | { |
---|
1551 | m_pcBinIf->encodeBinEP( code ); |
---|
1552 | } |
---|
1553 | |
---|
1554 | Void TEncSbac::codeSaoMaxUvlc ( UInt code, UInt maxSymbol ) |
---|
1555 | { |
---|
1556 | if (maxSymbol == 0) |
---|
1557 | { |
---|
1558 | return; |
---|
1559 | } |
---|
1560 | |
---|
1561 | Int i; |
---|
1562 | Bool bCodeLast = ( maxSymbol > code ); |
---|
1563 | |
---|
1564 | if ( code == 0 ) |
---|
1565 | { |
---|
1566 | m_pcBinIf->encodeBinEP( 0 ); |
---|
1567 | } |
---|
1568 | else |
---|
1569 | { |
---|
1570 | m_pcBinIf->encodeBinEP( 1 ); |
---|
1571 | for ( i=0; i<code-1; i++ ) |
---|
1572 | { |
---|
1573 | m_pcBinIf->encodeBinEP( 1 ); |
---|
1574 | } |
---|
1575 | if( bCodeLast ) |
---|
1576 | { |
---|
1577 | m_pcBinIf->encodeBinEP( 0 ); |
---|
1578 | } |
---|
1579 | } |
---|
1580 | } |
---|
1581 | |
---|
1582 | /** Code SAO EO class or BO band position |
---|
1583 | */ |
---|
1584 | Void TEncSbac::codeSaoUflc ( UInt uiLength, UInt uiCode ) |
---|
1585 | { |
---|
1586 | m_pcBinIf->encodeBinsEP ( uiCode, uiLength ); |
---|
1587 | } |
---|
1588 | |
---|
1589 | /** Code SAO merge flags |
---|
1590 | */ |
---|
1591 | Void TEncSbac::codeSaoMerge ( UInt uiCode ) |
---|
1592 | { |
---|
1593 | m_pcBinIf->encodeBin(((uiCode == 0) ? 0 : 1), m_cSaoMergeSCModel.get( 0, 0, 0 )); |
---|
1594 | } |
---|
1595 | |
---|
1596 | /** Code SAO type index |
---|
1597 | */ |
---|
1598 | Void TEncSbac::codeSaoTypeIdx ( UInt uiCode) |
---|
1599 | { |
---|
1600 | if (uiCode == 0) |
---|
1601 | { |
---|
1602 | m_pcBinIf->encodeBin( 0, m_cSaoTypeIdxSCModel.get( 0, 0, 0 ) ); |
---|
1603 | } |
---|
1604 | else |
---|
1605 | { |
---|
1606 | m_pcBinIf->encodeBin( 1, m_cSaoTypeIdxSCModel.get( 0, 0, 0 ) ); |
---|
1607 | m_pcBinIf->encodeBinEP( uiCode == 1 ? 0 : 1 ); |
---|
1608 | } |
---|
1609 | } |
---|
1610 | #if SVC_EXTENSION |
---|
1611 | Void TEncSbac::codeSAOOffsetParam(ComponentID compIdx, SAOOffset& ctbParam, Bool sliceEnabled, UInt* saoMaxOffsetQVal) |
---|
1612 | #else |
---|
1613 | Void TEncSbac::codeSAOOffsetParam(ComponentID compIdx, SAOOffset& ctbParam, Bool sliceEnabled) |
---|
1614 | #endif |
---|
1615 | { |
---|
1616 | UInt uiSymbol; |
---|
1617 | if(!sliceEnabled) |
---|
1618 | { |
---|
1619 | assert(ctbParam.modeIdc == SAO_MODE_OFF); |
---|
1620 | return; |
---|
1621 | } |
---|
1622 | const Bool bIsFirstCompOfChType = (getFirstComponentOfChannel(toChannelType(compIdx)) == compIdx); |
---|
1623 | |
---|
1624 | //type |
---|
1625 | if(bIsFirstCompOfChType) |
---|
1626 | { |
---|
1627 | //sao_type_idx_luma or sao_type_idx_chroma |
---|
1628 | if(ctbParam.modeIdc == SAO_MODE_OFF) |
---|
1629 | { |
---|
1630 | uiSymbol =0; |
---|
1631 | } |
---|
1632 | else if(ctbParam.typeIdc == SAO_TYPE_BO) //BO |
---|
1633 | { |
---|
1634 | uiSymbol = 1; |
---|
1635 | } |
---|
1636 | else |
---|
1637 | { |
---|
1638 | assert(ctbParam.typeIdc < SAO_TYPE_START_BO); //EO |
---|
1639 | uiSymbol = 2; |
---|
1640 | } |
---|
1641 | codeSaoTypeIdx(uiSymbol); |
---|
1642 | } |
---|
1643 | |
---|
1644 | if(ctbParam.modeIdc == SAO_MODE_NEW) |
---|
1645 | { |
---|
1646 | Int numClasses = (ctbParam.typeIdc == SAO_TYPE_BO)?4:NUM_SAO_EO_CLASSES; |
---|
1647 | Int offset[4]; |
---|
1648 | Int k=0; |
---|
1649 | for(Int i=0; i< numClasses; i++) |
---|
1650 | { |
---|
1651 | if(ctbParam.typeIdc != SAO_TYPE_BO && i == SAO_CLASS_EO_PLAIN) |
---|
1652 | { |
---|
1653 | continue; |
---|
1654 | } |
---|
1655 | Int classIdx = (ctbParam.typeIdc == SAO_TYPE_BO)?( (ctbParam.typeAuxInfo+i)% NUM_SAO_BO_CLASSES ):i; |
---|
1656 | offset[k] = ctbParam.offset[classIdx]; |
---|
1657 | k++; |
---|
1658 | } |
---|
1659 | |
---|
1660 | for(Int i=0; i< 4; i++) |
---|
1661 | { |
---|
1662 | #if SVC_EXTENSION |
---|
1663 | codeSaoMaxUvlc((offset[i]<0)?(-offset[i]):(offset[i]), saoMaxOffsetQVal[compIdx] ); //sao_offset_abs |
---|
1664 | #else |
---|
1665 | codeSaoMaxUvlc((offset[i]<0)?(-offset[i]):(offset[i]), g_saoMaxOffsetQVal[compIdx] ); //sao_offset_abs |
---|
1666 | #endif |
---|
1667 | } |
---|
1668 | |
---|
1669 | |
---|
1670 | if(ctbParam.typeIdc == SAO_TYPE_BO) |
---|
1671 | { |
---|
1672 | for(Int i=0; i< 4; i++) |
---|
1673 | { |
---|
1674 | if(offset[i] != 0) |
---|
1675 | { |
---|
1676 | codeSAOSign((offset[i]< 0)?1:0); |
---|
1677 | } |
---|
1678 | } |
---|
1679 | |
---|
1680 | codeSaoUflc(NUM_SAO_BO_CLASSES_LOG2, ctbParam.typeAuxInfo ); //sao_band_position |
---|
1681 | } |
---|
1682 | else //EO |
---|
1683 | { |
---|
1684 | if(bIsFirstCompOfChType) |
---|
1685 | { |
---|
1686 | assert(ctbParam.typeIdc - SAO_TYPE_START_EO >=0); |
---|
1687 | codeSaoUflc(NUM_SAO_EO_TYPES_LOG2, ctbParam.typeIdc - SAO_TYPE_START_EO ); //sao_eo_class_luma or sao_eo_class_chroma |
---|
1688 | } |
---|
1689 | } |
---|
1690 | |
---|
1691 | } |
---|
1692 | } |
---|
1693 | |
---|
1694 | |
---|
1695 | Void TEncSbac::codeSAOBlkParam(SAOBlkParam& saoBlkParam |
---|
1696 | #if SVC_EXTENSION |
---|
1697 | , UInt* saoMaxOffsetQVal |
---|
1698 | #endif |
---|
1699 | , Bool* sliceEnabled |
---|
1700 | , Bool leftMergeAvail |
---|
1701 | , Bool aboveMergeAvail |
---|
1702 | , Bool onlyEstMergeInfo // = false |
---|
1703 | ) |
---|
1704 | { |
---|
1705 | |
---|
1706 | Bool isLeftMerge = false; |
---|
1707 | Bool isAboveMerge= false; |
---|
1708 | |
---|
1709 | if(leftMergeAvail) |
---|
1710 | { |
---|
1711 | isLeftMerge = ((saoBlkParam[COMPONENT_Y].modeIdc == SAO_MODE_MERGE) && (saoBlkParam[COMPONENT_Y].typeIdc == SAO_MERGE_LEFT)); |
---|
1712 | codeSaoMerge( isLeftMerge?1:0 ); //sao_merge_left_flag |
---|
1713 | } |
---|
1714 | |
---|
1715 | if( aboveMergeAvail && !isLeftMerge) |
---|
1716 | { |
---|
1717 | isAboveMerge = ((saoBlkParam[COMPONENT_Y].modeIdc == SAO_MODE_MERGE) && (saoBlkParam[COMPONENT_Y].typeIdc == SAO_MERGE_ABOVE)); |
---|
1718 | codeSaoMerge( isAboveMerge?1:0 ); //sao_merge_left_flag |
---|
1719 | } |
---|
1720 | |
---|
1721 | if(onlyEstMergeInfo) |
---|
1722 | { |
---|
1723 | return; //only for RDO |
---|
1724 | } |
---|
1725 | |
---|
1726 | if(!isLeftMerge && !isAboveMerge) //not merge mode |
---|
1727 | { |
---|
1728 | for(Int compIdx=0; compIdx < MAX_NUM_COMPONENT; compIdx++) |
---|
1729 | { |
---|
1730 | #if SVC_EXTENSION |
---|
1731 | codeSAOOffsetParam(ComponentID(compIdx), saoBlkParam[compIdx], sliceEnabled[compIdx], saoMaxOffsetQVal); |
---|
1732 | #else |
---|
1733 | codeSAOOffsetParam(ComponentID(compIdx), saoBlkParam[compIdx], sliceEnabled[compIdx]); |
---|
1734 | #endif |
---|
1735 | } |
---|
1736 | } |
---|
1737 | } |
---|
1738 | |
---|
1739 | /*! |
---|
1740 | **************************************************************************** |
---|
1741 | * \brief |
---|
1742 | * estimate bit cost for CBP, significant map and significant coefficients |
---|
1743 | **************************************************************************** |
---|
1744 | */ |
---|
1745 | Void TEncSbac::estBit( estBitsSbacStruct* pcEstBitsSbac, Int width, Int height, ChannelType chType ) |
---|
1746 | { |
---|
1747 | estCBFBit( pcEstBitsSbac ); |
---|
1748 | |
---|
1749 | estSignificantCoeffGroupMapBit( pcEstBitsSbac, chType ); |
---|
1750 | |
---|
1751 | // encode significance map |
---|
1752 | estSignificantMapBit( pcEstBitsSbac, width, height, chType ); |
---|
1753 | |
---|
1754 | // encode last significant position |
---|
1755 | estLastSignificantPositionBit( pcEstBitsSbac, width, height, chType ); |
---|
1756 | |
---|
1757 | // encode significant coefficients |
---|
1758 | estSignificantCoefficientsBit( pcEstBitsSbac, chType ); |
---|
1759 | |
---|
1760 | memcpy(pcEstBitsSbac->golombRiceAdaptationStatistics, m_golombRiceAdaptationStatistics, (sizeof(UInt) * RExt__GOLOMB_RICE_ADAPTATION_STATISTICS_SETS)); |
---|
1761 | } |
---|
1762 | |
---|
1763 | /*! |
---|
1764 | **************************************************************************** |
---|
1765 | * \brief |
---|
1766 | * estimate bit cost for each CBP bit |
---|
1767 | **************************************************************************** |
---|
1768 | */ |
---|
1769 | Void TEncSbac::estCBFBit( estBitsSbacStruct* pcEstBitsSbac ) |
---|
1770 | { |
---|
1771 | ContextModel *pCtx = m_cCUQtCbfSCModel.get( 0 ); |
---|
1772 | |
---|
1773 | for( UInt uiCtxInc = 0; uiCtxInc < (NUM_QT_CBF_CTX_SETS * NUM_QT_CBF_CTX_PER_SET); uiCtxInc++ ) |
---|
1774 | { |
---|
1775 | pcEstBitsSbac->blockCbpBits[ uiCtxInc ][ 0 ] = pCtx[ uiCtxInc ].getEntropyBits( 0 ); |
---|
1776 | pcEstBitsSbac->blockCbpBits[ uiCtxInc ][ 1 ] = pCtx[ uiCtxInc ].getEntropyBits( 1 ); |
---|
1777 | } |
---|
1778 | |
---|
1779 | pCtx = m_cCUQtRootCbfSCModel.get( 0 ); |
---|
1780 | |
---|
1781 | for( UInt uiCtxInc = 0; uiCtxInc < 4; uiCtxInc++ ) |
---|
1782 | { |
---|
1783 | pcEstBitsSbac->blockRootCbpBits[ uiCtxInc ][ 0 ] = pCtx[ uiCtxInc ].getEntropyBits( 0 ); |
---|
1784 | pcEstBitsSbac->blockRootCbpBits[ uiCtxInc ][ 1 ] = pCtx[ uiCtxInc ].getEntropyBits( 1 ); |
---|
1785 | } |
---|
1786 | } |
---|
1787 | |
---|
1788 | |
---|
1789 | /*! |
---|
1790 | **************************************************************************** |
---|
1791 | * \brief |
---|
1792 | * estimate SAMBAC bit cost for significant coefficient group map |
---|
1793 | **************************************************************************** |
---|
1794 | */ |
---|
1795 | Void TEncSbac::estSignificantCoeffGroupMapBit( estBitsSbacStruct* pcEstBitsSbac, ChannelType chType ) |
---|
1796 | { |
---|
1797 | Int firstCtx = 0, numCtx = NUM_SIG_CG_FLAG_CTX; |
---|
1798 | |
---|
1799 | for ( Int ctxIdx = firstCtx; ctxIdx < firstCtx + numCtx; ctxIdx++ ) |
---|
1800 | { |
---|
1801 | for( UInt uiBin = 0; uiBin < 2; uiBin++ ) |
---|
1802 | { |
---|
1803 | pcEstBitsSbac->significantCoeffGroupBits[ ctxIdx ][ uiBin ] = m_cCUSigCoeffGroupSCModel.get( 0, chType, ctxIdx ).getEntropyBits( uiBin ); |
---|
1804 | } |
---|
1805 | } |
---|
1806 | } |
---|
1807 | |
---|
1808 | |
---|
1809 | /*! |
---|
1810 | **************************************************************************** |
---|
1811 | * \brief |
---|
1812 | * estimate SAMBAC bit cost for significant coefficient map |
---|
1813 | **************************************************************************** |
---|
1814 | */ |
---|
1815 | Void TEncSbac::estSignificantMapBit( estBitsSbacStruct* pcEstBitsSbac, Int width, Int height, ChannelType chType ) |
---|
1816 | { |
---|
1817 | //-------------------------------------------------------------------------------------------------- |
---|
1818 | |
---|
1819 | //set up the number of channels and context variables |
---|
1820 | |
---|
1821 | const UInt firstComponent = ((isLuma(chType)) ? (COMPONENT_Y) : (COMPONENT_Cb)); |
---|
1822 | const UInt lastComponent = ((isLuma(chType)) ? (COMPONENT_Y) : (COMPONENT_Cb)); |
---|
1823 | |
---|
1824 | //---------------------------------------------------------- |
---|
1825 | |
---|
1826 | Int firstCtx = MAX_INT; |
---|
1827 | Int numCtx = MAX_INT; |
---|
1828 | |
---|
1829 | if ((width == 4) && (height == 4)) |
---|
1830 | { |
---|
1831 | firstCtx = significanceMapContextSetStart[chType][CONTEXT_TYPE_4x4]; |
---|
1832 | numCtx = significanceMapContextSetSize [chType][CONTEXT_TYPE_4x4]; |
---|
1833 | } |
---|
1834 | else if ((width == 8) && (height == 8)) |
---|
1835 | { |
---|
1836 | firstCtx = significanceMapContextSetStart[chType][CONTEXT_TYPE_8x8]; |
---|
1837 | numCtx = significanceMapContextSetSize [chType][CONTEXT_TYPE_8x8]; |
---|
1838 | } |
---|
1839 | else |
---|
1840 | { |
---|
1841 | firstCtx = significanceMapContextSetStart[chType][CONTEXT_TYPE_NxN]; |
---|
1842 | numCtx = significanceMapContextSetSize [chType][CONTEXT_TYPE_NxN]; |
---|
1843 | } |
---|
1844 | |
---|
1845 | //-------------------------------------------------------------------------------------------------- |
---|
1846 | |
---|
1847 | //fill the data for the significace map |
---|
1848 | |
---|
1849 | for (UInt component = firstComponent; component <= lastComponent; component++) |
---|
1850 | { |
---|
1851 | const UInt contextOffset = getSignificanceMapContextOffset(ComponentID(component)); |
---|
1852 | |
---|
1853 | if (firstCtx > 0) |
---|
1854 | { |
---|
1855 | for( UInt bin = 0; bin < 2; bin++ ) //always get the DC |
---|
1856 | { |
---|
1857 | pcEstBitsSbac->significantBits[ contextOffset ][ bin ] = m_cCUSigSCModel.get( 0, 0, contextOffset ).getEntropyBits( bin ); |
---|
1858 | } |
---|
1859 | } |
---|
1860 | |
---|
1861 | // This could be made optional, but would require this function to have knowledge of whether the |
---|
1862 | // TU is transform-skipped or transquant-bypassed and whether the SPS flag is set |
---|
1863 | for( UInt bin = 0; bin < 2; bin++ ) |
---|
1864 | { |
---|
1865 | const Int ctxIdx = significanceMapContextSetStart[chType][CONTEXT_TYPE_SINGLE]; |
---|
1866 | pcEstBitsSbac->significantBits[ contextOffset + ctxIdx ][ bin ] = m_cCUSigSCModel.get( 0, 0, (contextOffset + ctxIdx) ).getEntropyBits( bin ); |
---|
1867 | } |
---|
1868 | |
---|
1869 | for ( Int ctxIdx = firstCtx; ctxIdx < firstCtx + numCtx; ctxIdx++ ) |
---|
1870 | { |
---|
1871 | for( UInt uiBin = 0; uiBin < 2; uiBin++ ) |
---|
1872 | { |
---|
1873 | pcEstBitsSbac->significantBits[ contextOffset + ctxIdx ][ uiBin ] = m_cCUSigSCModel.get( 0, 0, (contextOffset + ctxIdx) ).getEntropyBits( uiBin ); |
---|
1874 | } |
---|
1875 | } |
---|
1876 | } |
---|
1877 | |
---|
1878 | //-------------------------------------------------------------------------------------------------- |
---|
1879 | } |
---|
1880 | |
---|
1881 | |
---|
1882 | /*! |
---|
1883 | **************************************************************************** |
---|
1884 | * \brief |
---|
1885 | * estimate bit cost of significant coefficient |
---|
1886 | **************************************************************************** |
---|
1887 | */ |
---|
1888 | |
---|
1889 | Void TEncSbac::estLastSignificantPositionBit( estBitsSbacStruct* pcEstBitsSbac, Int width, Int height, ChannelType chType ) |
---|
1890 | { |
---|
1891 | //--------------------------------------------------------------------------------------------------. |
---|
1892 | |
---|
1893 | //set up the number of channels |
---|
1894 | |
---|
1895 | const UInt firstComponent = ((isLuma(chType)) ? (COMPONENT_Y) : (COMPONENT_Cb)); |
---|
1896 | const UInt lastComponent = ((isLuma(chType)) ? (COMPONENT_Y) : (COMPONENT_Cb)); |
---|
1897 | |
---|
1898 | //-------------------------------------------------------------------------------------------------- |
---|
1899 | |
---|
1900 | //fill the data for the last-significant-coefficient position |
---|
1901 | |
---|
1902 | for (UInt componentIndex = firstComponent; componentIndex <= lastComponent; componentIndex++) |
---|
1903 | { |
---|
1904 | const ComponentID component = ComponentID(componentIndex); |
---|
1905 | |
---|
1906 | Int iBitsX = 0, iBitsY = 0; |
---|
1907 | |
---|
1908 | Int blkSizeOffsetX, blkSizeOffsetY, shiftX, shiftY; |
---|
1909 | getLastSignificantContextParameters(ComponentID(component), width, height, blkSizeOffsetX, blkSizeOffsetY, shiftX, shiftY); |
---|
1910 | |
---|
1911 | Int ctx; |
---|
1912 | |
---|
1913 | const ChannelType channelType = toChannelType(ComponentID(component)); |
---|
1914 | |
---|
1915 | ContextModel *const pCtxX = m_cCuCtxLastX.get( 0, channelType ); |
---|
1916 | ContextModel *const pCtxY = m_cCuCtxLastY.get( 0, channelType ); |
---|
1917 | Int *const lastXBitsArray = pcEstBitsSbac->lastXBits[channelType]; |
---|
1918 | Int *const lastYBitsArray = pcEstBitsSbac->lastYBits[channelType]; |
---|
1919 | |
---|
1920 | //------------------------------------------------ |
---|
1921 | |
---|
1922 | //X-coordinate |
---|
1923 | |
---|
1924 | for (ctx = 0; ctx < g_uiGroupIdx[ width - 1 ]; ctx++) |
---|
1925 | { |
---|
1926 | Int ctxOffset = blkSizeOffsetX + (ctx >>shiftX); |
---|
1927 | lastXBitsArray[ ctx ] = iBitsX + pCtxX[ ctxOffset ].getEntropyBits( 0 ); |
---|
1928 | iBitsX += pCtxX[ ctxOffset ].getEntropyBits( 1 ); |
---|
1929 | } |
---|
1930 | |
---|
1931 | lastXBitsArray[ctx] = iBitsX; |
---|
1932 | |
---|
1933 | //------------------------------------------------ |
---|
1934 | |
---|
1935 | //Y-coordinate |
---|
1936 | |
---|
1937 | for (ctx = 0; ctx < g_uiGroupIdx[ height - 1 ]; ctx++) |
---|
1938 | { |
---|
1939 | Int ctxOffset = blkSizeOffsetY + (ctx >>shiftY); |
---|
1940 | lastYBitsArray[ ctx ] = iBitsY + pCtxY[ ctxOffset ].getEntropyBits( 0 ); |
---|
1941 | iBitsY += pCtxY[ ctxOffset ].getEntropyBits( 1 ); |
---|
1942 | } |
---|
1943 | |
---|
1944 | lastYBitsArray[ctx] = iBitsY; |
---|
1945 | |
---|
1946 | } //end of component loop |
---|
1947 | |
---|
1948 | //-------------------------------------------------------------------------------------------------- |
---|
1949 | } |
---|
1950 | |
---|
1951 | |
---|
1952 | /*! |
---|
1953 | **************************************************************************** |
---|
1954 | * \brief |
---|
1955 | * estimate bit cost of significant coefficient |
---|
1956 | **************************************************************************** |
---|
1957 | */ |
---|
1958 | Void TEncSbac::estSignificantCoefficientsBit( estBitsSbacStruct* pcEstBitsSbac, ChannelType chType ) |
---|
1959 | { |
---|
1960 | ContextModel *ctxOne = m_cCUOneSCModel.get(0, 0); |
---|
1961 | ContextModel *ctxAbs = m_cCUAbsSCModel.get(0, 0); |
---|
1962 | |
---|
1963 | const UInt oneStartIndex = ((isLuma(chType)) ? (0) : (NUM_ONE_FLAG_CTX_LUMA)); |
---|
1964 | const UInt oneStopIndex = ((isLuma(chType)) ? (NUM_ONE_FLAG_CTX_LUMA) : (NUM_ONE_FLAG_CTX)); |
---|
1965 | const UInt absStartIndex = ((isLuma(chType)) ? (0) : (NUM_ABS_FLAG_CTX_LUMA)); |
---|
1966 | const UInt absStopIndex = ((isLuma(chType)) ? (NUM_ABS_FLAG_CTX_LUMA) : (NUM_ABS_FLAG_CTX)); |
---|
1967 | |
---|
1968 | for (Int ctxIdx = oneStartIndex; ctxIdx < oneStopIndex; ctxIdx++) |
---|
1969 | { |
---|
1970 | pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 0 ] = ctxOne[ ctxIdx ].getEntropyBits( 0 ); |
---|
1971 | pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 1 ] = ctxOne[ ctxIdx ].getEntropyBits( 1 ); |
---|
1972 | } |
---|
1973 | |
---|
1974 | for (Int ctxIdx = absStartIndex; ctxIdx < absStopIndex; ctxIdx++) |
---|
1975 | { |
---|
1976 | pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 0 ] = ctxAbs[ ctxIdx ].getEntropyBits( 0 ); |
---|
1977 | pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 1 ] = ctxAbs[ ctxIdx ].getEntropyBits( 1 ); |
---|
1978 | } |
---|
1979 | } |
---|
1980 | |
---|
1981 | /** |
---|
1982 | - Initialize our context information from the nominated source. |
---|
1983 | . |
---|
1984 | \param pSrc From where to copy context information. |
---|
1985 | */ |
---|
1986 | Void TEncSbac::xCopyContextsFrom( const TEncSbac* pSrc ) |
---|
1987 | { |
---|
1988 | memcpy(m_contextModels, pSrc->m_contextModels, m_numContextModels*sizeof(m_contextModels[0])); |
---|
1989 | memcpy(m_golombRiceAdaptationStatistics, pSrc->m_golombRiceAdaptationStatistics, (sizeof(UInt) * RExt__GOLOMB_RICE_ADAPTATION_STATISTICS_SETS)); |
---|
1990 | } |
---|
1991 | |
---|
1992 | Void TEncSbac::loadContexts ( const TEncSbac* pSrc) |
---|
1993 | { |
---|
1994 | xCopyContextsFrom(pSrc); |
---|
1995 | } |
---|
1996 | |
---|
1997 | /** Performs CABAC encoding of the explicit RDPCM mode |
---|
1998 | * \param rTu current TU data structure |
---|
1999 | * \param compID component identifier |
---|
2000 | */ |
---|
2001 | Void TEncSbac::codeExplicitRdpcmMode( TComTU &rTu, const ComponentID compID ) |
---|
2002 | { |
---|
2003 | TComDataCU *cu = rTu.getCU(); |
---|
2004 | const TComRectangle &rect = rTu.getRect(compID); |
---|
2005 | const UInt absPartIdx = rTu.GetAbsPartIdxTU(compID); |
---|
2006 | const UInt tuHeight = g_aucConvertToBit[rect.height]; |
---|
2007 | const UInt tuWidth = g_aucConvertToBit[rect.width]; |
---|
2008 | |
---|
2009 | assert(tuHeight == tuWidth); |
---|
2010 | assert(tuHeight < 4); |
---|
2011 | |
---|
2012 | UInt explicitRdpcmMode = cu->getExplicitRdpcmMode(compID, absPartIdx); |
---|
2013 | |
---|
2014 | if( explicitRdpcmMode == RDPCM_OFF ) |
---|
2015 | { |
---|
2016 | m_pcBinIf->encodeBin (0, m_explicitRdpcmFlagSCModel.get (0, toChannelType(compID), 0)); |
---|
2017 | } |
---|
2018 | else if( explicitRdpcmMode == RDPCM_HOR || explicitRdpcmMode == RDPCM_VER ) |
---|
2019 | { |
---|
2020 | m_pcBinIf->encodeBin (1, m_explicitRdpcmFlagSCModel.get (0, toChannelType(compID), 0)); |
---|
2021 | if(explicitRdpcmMode == RDPCM_HOR) |
---|
2022 | { |
---|
2023 | m_pcBinIf->encodeBin ( 0, m_explicitRdpcmDirSCModel.get(0, toChannelType(compID), 0)); |
---|
2024 | } |
---|
2025 | else |
---|
2026 | { |
---|
2027 | m_pcBinIf->encodeBin ( 1, m_explicitRdpcmDirSCModel.get(0, toChannelType(compID), 0)); |
---|
2028 | } |
---|
2029 | } |
---|
2030 | else |
---|
2031 | { |
---|
2032 | assert(0); |
---|
2033 | } |
---|
2034 | } |
---|
2035 | |
---|
2036 | #if SVC_EXTENSION |
---|
2037 | Void TEncSbac::codeSliceHeaderExtn( TComSlice* pSlice, Int shBitsWrittenTillNow ) |
---|
2038 | { |
---|
2039 | assert (0); |
---|
2040 | return; |
---|
2041 | } |
---|
2042 | #endif |
---|
2043 | |
---|
2044 | //! \} |
---|