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