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