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