[5] | 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 |
---|
[1200] | 4 | * granted under this license. |
---|
[5] | 5 | * |
---|
[1200] | 6 | * Copyright (c) 2010-2015, ITU/ISO/IEC |
---|
[5] | 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. |
---|
[56] | 17 | * * Neither the name of the ITU/ISO/IEC nor the names of its contributors may |
---|
[5] | 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 | */ |
---|
[2] | 33 | |
---|
| 34 | /** \file TDecBinCoderCABAC.cpp |
---|
| 35 | \brief binary entropy decoder of CABAC |
---|
| 36 | */ |
---|
| 37 | |
---|
| 38 | #include "TDecBinCoderCABAC.h" |
---|
[1200] | 39 | #include "TLibCommon/Debug.h" |
---|
| 40 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
| 41 | #include "TLibCommon/TComCodingStatistics.h" |
---|
| 42 | #endif |
---|
[2] | 43 | |
---|
[56] | 44 | //! \ingroup TLibDecoder |
---|
| 45 | //! \{ |
---|
[2] | 46 | |
---|
| 47 | TDecBinCABAC::TDecBinCABAC() |
---|
| 48 | : m_pcTComBitstream( 0 ) |
---|
| 49 | { |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | TDecBinCABAC::~TDecBinCABAC() |
---|
| 53 | { |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | Void |
---|
[56] | 57 | TDecBinCABAC::init( TComInputBitstream* pcTComBitstream ) |
---|
[2] | 58 | { |
---|
| 59 | m_pcTComBitstream = pcTComBitstream; |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | Void |
---|
| 63 | TDecBinCABAC::uninit() |
---|
| 64 | { |
---|
| 65 | m_pcTComBitstream = 0; |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | Void |
---|
| 69 | TDecBinCABAC::start() |
---|
| 70 | { |
---|
[56] | 71 | assert( m_pcTComBitstream->getNumBitsUntilByteAligned() == 0 ); |
---|
[1200] | 72 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
| 73 | TComCodingStatistics::UpdateCABACStat(STATS__CABAC_INITIALISATION, 512, 510, 0); |
---|
| 74 | #endif |
---|
[2] | 75 | m_uiRange = 510; |
---|
[56] | 76 | m_bitsNeeded = -8; |
---|
[608] | 77 | m_uiValue = (m_pcTComBitstream->readByte() << 8); |
---|
[56] | 78 | m_uiValue |= m_pcTComBitstream->readByte(); |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | Void |
---|
| 82 | TDecBinCABAC::finish() |
---|
| 83 | { |
---|
[608] | 84 | UInt lastByte; |
---|
[56] | 85 | |
---|
[608] | 86 | m_pcTComBitstream->peekPreviousByte( lastByte ); |
---|
| 87 | // Check for proper stop/alignment pattern |
---|
| 88 | assert( ((lastByte << (8 + m_bitsNeeded)) & 0xff) == 0x80 ); |
---|
[2] | 89 | } |
---|
| 90 | |
---|
[56] | 91 | /** |
---|
| 92 | - Copy CABAC state. |
---|
| 93 | . |
---|
| 94 | \param pcTDecBinIf The source CABAC engine. |
---|
| 95 | */ |
---|
[2] | 96 | Void |
---|
[1200] | 97 | TDecBinCABAC::copyState( const TDecBinIf* pcTDecBinIf ) |
---|
[2] | 98 | { |
---|
[1200] | 99 | const TDecBinCABAC* pcTDecBinCABAC = pcTDecBinIf->getTDecBinCABAC(); |
---|
[56] | 100 | m_uiRange = pcTDecBinCABAC->m_uiRange; |
---|
| 101 | m_uiValue = pcTDecBinCABAC->m_uiValue; |
---|
| 102 | m_bitsNeeded= pcTDecBinCABAC->m_bitsNeeded; |
---|
[2] | 103 | } |
---|
| 104 | |
---|
[56] | 105 | |
---|
[1200] | 106 | |
---|
| 107 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
| 108 | Void TDecBinCABAC::decodeBin( UInt& ruiBin, ContextModel &rcCtxModel, const TComCodingStatisticsClassType &whichStat ) |
---|
| 109 | #else |
---|
| 110 | Void TDecBinCABAC::decodeBin( UInt& ruiBin, ContextModel &rcCtxModel ) |
---|
| 111 | #endif |
---|
[2] | 112 | { |
---|
[1200] | 113 | #if DEBUG_CABAC_BINS |
---|
| 114 | const UInt startingRange = m_uiRange; |
---|
| 115 | #endif |
---|
| 116 | |
---|
[56] | 117 | UInt uiLPS = TComCABACTables::sm_aucLPSTable[ rcCtxModel.getState() ][ ( m_uiRange >> 6 ) - 4 ]; |
---|
| 118 | m_uiRange -= uiLPS; |
---|
| 119 | UInt scaledRange = m_uiRange << 7; |
---|
[1200] | 120 | |
---|
[56] | 121 | if( m_uiValue < scaledRange ) |
---|
[2] | 122 | { |
---|
[56] | 123 | // MPS path |
---|
| 124 | ruiBin = rcCtxModel.getMps(); |
---|
[1200] | 125 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
| 126 | TComCodingStatistics::UpdateCABACStat(whichStat, m_uiRange+uiLPS, m_uiRange, Int(ruiBin)); |
---|
| 127 | #endif |
---|
[2] | 128 | rcCtxModel.updateMPS(); |
---|
[1200] | 129 | |
---|
| 130 | if ( scaledRange < ( 256 << 7 ) ) |
---|
[56] | 131 | { |
---|
[1200] | 132 | m_uiRange = scaledRange >> 6; |
---|
| 133 | m_uiValue += m_uiValue; |
---|
| 134 | |
---|
| 135 | if ( ++m_bitsNeeded == 0 ) |
---|
| 136 | { |
---|
| 137 | m_bitsNeeded = -8; |
---|
| 138 | m_uiValue += m_pcTComBitstream->readByte(); |
---|
| 139 | } |
---|
[56] | 140 | } |
---|
[2] | 141 | } |
---|
| 142 | else |
---|
| 143 | { |
---|
[56] | 144 | // LPS path |
---|
[1200] | 145 | ruiBin = 1 - rcCtxModel.getMps(); |
---|
| 146 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
| 147 | TComCodingStatistics::UpdateCABACStat(whichStat, m_uiRange+uiLPS, uiLPS, Int(ruiBin)); |
---|
| 148 | #endif |
---|
[56] | 149 | Int numBits = TComCABACTables::sm_aucRenormTable[ uiLPS >> 3 ]; |
---|
| 150 | m_uiValue = ( m_uiValue - scaledRange ) << numBits; |
---|
| 151 | m_uiRange = uiLPS << numBits; |
---|
[2] | 152 | rcCtxModel.updateLPS(); |
---|
[1200] | 153 | |
---|
[56] | 154 | m_bitsNeeded += numBits; |
---|
[1200] | 155 | |
---|
[56] | 156 | if ( m_bitsNeeded >= 0 ) |
---|
| 157 | { |
---|
| 158 | m_uiValue += m_pcTComBitstream->readByte() << m_bitsNeeded; |
---|
| 159 | m_bitsNeeded -= 8; |
---|
| 160 | } |
---|
[2] | 161 | } |
---|
[1200] | 162 | |
---|
| 163 | #if DEBUG_CABAC_BINS |
---|
| 164 | if ((g_debugCounter + debugCabacBinWindow) >= debugCabacBinTargetLine) |
---|
| 165 | { |
---|
| 166 | std::cout << g_debugCounter << ": coding bin value " << ruiBin << ", range = [" << startingRange << "->" << m_uiRange << "]\n"; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | if (g_debugCounter >= debugCabacBinTargetLine) |
---|
| 170 | { |
---|
| 171 | Char breakPointThis; |
---|
| 172 | breakPointThis = 7; |
---|
| 173 | } |
---|
| 174 | if (g_debugCounter >= (debugCabacBinTargetLine + debugCabacBinWindow)) |
---|
| 175 | { |
---|
| 176 | exit(0); |
---|
| 177 | } |
---|
| 178 | g_debugCounter++; |
---|
| 179 | #endif |
---|
[2] | 180 | } |
---|
| 181 | |
---|
[1200] | 182 | |
---|
| 183 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
| 184 | Void TDecBinCABAC::decodeBinEP( UInt& ruiBin, const TComCodingStatisticsClassType &whichStat ) |
---|
| 185 | #else |
---|
| 186 | Void TDecBinCABAC::decodeBinEP( UInt& ruiBin ) |
---|
| 187 | #endif |
---|
[2] | 188 | { |
---|
[1200] | 189 | if (m_uiRange == 256) |
---|
| 190 | { |
---|
| 191 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
| 192 | decodeAlignedBinsEP(ruiBin, 1, whichStat); |
---|
| 193 | #else |
---|
| 194 | decodeAlignedBinsEP(ruiBin, 1); |
---|
| 195 | #endif |
---|
| 196 | return; |
---|
| 197 | } |
---|
| 198 | |
---|
[56] | 199 | m_uiValue += m_uiValue; |
---|
[1200] | 200 | |
---|
[56] | 201 | if ( ++m_bitsNeeded >= 0 ) |
---|
[2] | 202 | { |
---|
[56] | 203 | m_bitsNeeded = -8; |
---|
| 204 | m_uiValue += m_pcTComBitstream->readByte(); |
---|
[2] | 205 | } |
---|
[1200] | 206 | |
---|
[56] | 207 | ruiBin = 0; |
---|
| 208 | UInt scaledRange = m_uiRange << 7; |
---|
| 209 | if ( m_uiValue >= scaledRange ) |
---|
[2] | 210 | { |
---|
[56] | 211 | ruiBin = 1; |
---|
| 212 | m_uiValue -= scaledRange; |
---|
[2] | 213 | } |
---|
[1200] | 214 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
| 215 | TComCodingStatistics::IncrementStatisticEP(whichStat, 1, Int(ruiBin)); |
---|
| 216 | #endif |
---|
[2] | 217 | } |
---|
| 218 | |
---|
[1200] | 219 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
| 220 | Void TDecBinCABAC::decodeBinsEP( UInt& ruiBin, Int numBins, const TComCodingStatisticsClassType &whichStat ) |
---|
| 221 | #else |
---|
[56] | 222 | Void TDecBinCABAC::decodeBinsEP( UInt& ruiBin, Int numBins ) |
---|
[1200] | 223 | #endif |
---|
[56] | 224 | { |
---|
[1200] | 225 | if (m_uiRange == 256) |
---|
| 226 | { |
---|
| 227 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
| 228 | decodeAlignedBinsEP(ruiBin, numBins, whichStat); |
---|
| 229 | #else |
---|
| 230 | decodeAlignedBinsEP(ruiBin, numBins); |
---|
| 231 | #endif |
---|
| 232 | return; |
---|
| 233 | } |
---|
| 234 | |
---|
[56] | 235 | UInt bins = 0; |
---|
[1200] | 236 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
| 237 | Int origNumBins=numBins; |
---|
| 238 | #endif |
---|
[56] | 239 | while ( numBins > 8 ) |
---|
| 240 | { |
---|
| 241 | m_uiValue = ( m_uiValue << 8 ) + ( m_pcTComBitstream->readByte() << ( 8 + m_bitsNeeded ) ); |
---|
[1200] | 242 | |
---|
[56] | 243 | UInt scaledRange = m_uiRange << 15; |
---|
| 244 | for ( Int i = 0; i < 8; i++ ) |
---|
| 245 | { |
---|
| 246 | bins += bins; |
---|
| 247 | scaledRange >>= 1; |
---|
| 248 | if ( m_uiValue >= scaledRange ) |
---|
| 249 | { |
---|
| 250 | bins++; |
---|
| 251 | m_uiValue -= scaledRange; |
---|
| 252 | } |
---|
| 253 | } |
---|
| 254 | numBins -= 8; |
---|
| 255 | } |
---|
[1200] | 256 | |
---|
[56] | 257 | m_bitsNeeded += numBins; |
---|
| 258 | m_uiValue <<= numBins; |
---|
[1200] | 259 | |
---|
[56] | 260 | if ( m_bitsNeeded >= 0 ) |
---|
| 261 | { |
---|
| 262 | m_uiValue += m_pcTComBitstream->readByte() << m_bitsNeeded; |
---|
| 263 | m_bitsNeeded -= 8; |
---|
| 264 | } |
---|
[1200] | 265 | |
---|
[56] | 266 | UInt scaledRange = m_uiRange << ( numBins + 7 ); |
---|
| 267 | for ( Int i = 0; i < numBins; i++ ) |
---|
| 268 | { |
---|
| 269 | bins += bins; |
---|
| 270 | scaledRange >>= 1; |
---|
| 271 | if ( m_uiValue >= scaledRange ) |
---|
| 272 | { |
---|
| 273 | bins++; |
---|
| 274 | m_uiValue -= scaledRange; |
---|
| 275 | } |
---|
| 276 | } |
---|
[1200] | 277 | |
---|
[56] | 278 | ruiBin = bins; |
---|
[1200] | 279 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
| 280 | TComCodingStatistics::IncrementStatisticEP(whichStat, origNumBins, Int(ruiBin)); |
---|
| 281 | #endif |
---|
[56] | 282 | } |
---|
| 283 | |
---|
[1200] | 284 | Void TDecBinCABAC::align() |
---|
| 285 | { |
---|
| 286 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
| 287 | TComCodingStatistics::UpdateCABACStat(STATS__CABAC_EP_BIT_ALIGNMENT, m_uiRange, 256, 0); |
---|
| 288 | #endif |
---|
| 289 | m_uiRange = 256; |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
| 293 | Void TDecBinCABAC::decodeAlignedBinsEP( UInt& ruiBins, Int numBins, const class TComCodingStatisticsClassType &whichStat ) |
---|
| 294 | #else |
---|
| 295 | Void TDecBinCABAC::decodeAlignedBinsEP( UInt& ruiBins, Int numBins ) |
---|
| 296 | #endif |
---|
| 297 | { |
---|
| 298 | Int binsRemaining = numBins; |
---|
| 299 | ruiBins = 0; |
---|
| 300 | |
---|
| 301 | assert(m_uiRange == 256); //aligned decode only works when range = 256 |
---|
| 302 | |
---|
| 303 | while (binsRemaining > 0) |
---|
| 304 | { |
---|
| 305 | const UInt binsToRead = std::min<UInt>(binsRemaining, 8); //read bytes if able to take advantage of the system's byte-read function |
---|
| 306 | const UInt binMask = (1 << binsToRead) - 1; |
---|
| 307 | |
---|
| 308 | //The MSB of m_uiValue is known to be 0 because range is 256. Therefore: |
---|
| 309 | // > The comparison against the symbol range of 128 is simply a test on the next-most-significant bit |
---|
| 310 | // > "Subtracting" the symbol range if the decoded bin is 1 simply involves clearing that bit. |
---|
| 311 | // |
---|
| 312 | //As a result, the required bins are simply the <binsToRead> next-most-significant bits of m_uiValue |
---|
| 313 | //(m_uiValue is stored MSB-aligned in a 16-bit buffer - hence the shift of 15) |
---|
| 314 | // |
---|
| 315 | // m_uiValue = |0|V|V|V|V|V|V|V|V|B|B|B|B|B|B|B| (V = usable bit, B = potential buffered bit (buffer refills when m_bitsNeeded >= 0)) |
---|
| 316 | // |
---|
| 317 | const UInt newBins = (m_uiValue >> (15 - binsToRead)) & binMask; |
---|
| 318 | |
---|
| 319 | ruiBins = (ruiBins << binsToRead) | newBins; |
---|
| 320 | m_uiValue = (m_uiValue << binsToRead) & 0x7FFF; |
---|
| 321 | |
---|
| 322 | binsRemaining -= binsToRead; |
---|
| 323 | m_bitsNeeded += binsToRead; |
---|
| 324 | |
---|
| 325 | if (m_bitsNeeded >= 0) |
---|
| 326 | { |
---|
| 327 | m_uiValue |= m_pcTComBitstream->readByte() << m_bitsNeeded; |
---|
| 328 | m_bitsNeeded -= 8; |
---|
| 329 | } |
---|
| 330 | } |
---|
| 331 | |
---|
| 332 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
| 333 | TComCodingStatistics::IncrementStatisticEP(whichStat, numBins, Int(ruiBins)); |
---|
| 334 | #endif |
---|
| 335 | } |
---|
| 336 | |
---|
[2] | 337 | Void |
---|
| 338 | TDecBinCABAC::decodeBinTrm( UInt& ruiBin ) |
---|
| 339 | { |
---|
| 340 | m_uiRange -= 2; |
---|
[56] | 341 | UInt scaledRange = m_uiRange << 7; |
---|
| 342 | if( m_uiValue >= scaledRange ) |
---|
[2] | 343 | { |
---|
| 344 | ruiBin = 1; |
---|
[1200] | 345 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
| 346 | TComCodingStatistics::UpdateCABACStat(STATS__CABAC_TRM_BITS, m_uiRange+2, 2, ruiBin); |
---|
| 347 | TComCodingStatistics::IncrementStatisticEP(STATS__BYTE_ALIGNMENT_BITS, -m_bitsNeeded, 0); |
---|
| 348 | #endif |
---|
[2] | 349 | } |
---|
| 350 | else |
---|
| 351 | { |
---|
| 352 | ruiBin = 0; |
---|
[1200] | 353 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
| 354 | TComCodingStatistics::UpdateCABACStat(STATS__CABAC_TRM_BITS, m_uiRange+2, m_uiRange, ruiBin); |
---|
| 355 | #endif |
---|
[56] | 356 | if ( scaledRange < ( 256 << 7 ) ) |
---|
[2] | 357 | { |
---|
[56] | 358 | m_uiRange = scaledRange >> 6; |
---|
| 359 | m_uiValue += m_uiValue; |
---|
[1200] | 360 | |
---|
[56] | 361 | if ( ++m_bitsNeeded == 0 ) |
---|
| 362 | { |
---|
| 363 | m_bitsNeeded = -8; |
---|
[1200] | 364 | m_uiValue += m_pcTComBitstream->readByte(); |
---|
[56] | 365 | } |
---|
[2] | 366 | } |
---|
| 367 | } |
---|
| 368 | } |
---|
| 369 | |
---|
[56] | 370 | /** Read a PCM code. |
---|
| 371 | * \param uiLength code bit-depth |
---|
| 372 | * \param ruiCode pointer to PCM code value |
---|
| 373 | * \returns Void |
---|
| 374 | */ |
---|
| 375 | Void TDecBinCABAC::xReadPCMCode(UInt uiLength, UInt& ruiCode) |
---|
| 376 | { |
---|
| 377 | assert ( uiLength > 0 ); |
---|
| 378 | m_pcTComBitstream->read (uiLength, ruiCode); |
---|
[1200] | 379 | #if RExt__DECODER_DEBUG_BIT_STATISTICS |
---|
| 380 | TComCodingStatistics::IncrementStatisticEP(STATS__CABAC_PCM_CODE_BITS, uiLength, ruiCode); |
---|
| 381 | #endif |
---|
[56] | 382 | } |
---|
| 383 | //! \} |
---|