| 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-2013, 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 TDecBinCoderCABAC.cpp |
|---|
| 35 | \brief binary entropy decoder of CABAC |
|---|
| 36 | */ |
|---|
| 37 | |
|---|
| 38 | #include "TDecBinCoderCABAC.h" |
|---|
| 39 | #include "../TLibCommon/TComRom.h" |
|---|
| 40 | |
|---|
| 41 | //! \ingroup TLibDecoder |
|---|
| 42 | //! \{ |
|---|
| 43 | |
|---|
| 44 | TDecBinCABAC::TDecBinCABAC() |
|---|
| 45 | : m_pcTComBitstream( 0 ) |
|---|
| 46 | { |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | TDecBinCABAC::~TDecBinCABAC() |
|---|
| 50 | { |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | Void |
|---|
| 54 | TDecBinCABAC::init( TComInputBitstream* pcTComBitstream ) |
|---|
| 55 | { |
|---|
| 56 | m_pcTComBitstream = pcTComBitstream; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | Void |
|---|
| 60 | TDecBinCABAC::uninit() |
|---|
| 61 | { |
|---|
| 62 | m_pcTComBitstream = 0; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | Void |
|---|
| 66 | TDecBinCABAC::start() |
|---|
| 67 | { |
|---|
| 68 | assert( m_pcTComBitstream->getNumBitsUntilByteAligned() == 0 ); |
|---|
| 69 | m_uiRange = 510; |
|---|
| 70 | m_bitsNeeded = -8; |
|---|
| 71 | m_uiValue = (m_pcTComBitstream->readByte() << 8); |
|---|
| 72 | m_uiValue |= m_pcTComBitstream->readByte(); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | Void |
|---|
| 76 | TDecBinCABAC::finish() |
|---|
| 77 | { |
|---|
| 78 | UInt lastByte; |
|---|
| 79 | |
|---|
| 80 | m_pcTComBitstream->peekPreviousByte( lastByte ); |
|---|
| 81 | // Check for proper stop/alignment pattern |
|---|
| 82 | assert( ((lastByte << (8 + m_bitsNeeded)) & 0xff) == 0x80 ); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | /** |
|---|
| 86 | - Copy CABAC state. |
|---|
| 87 | . |
|---|
| 88 | \param pcTDecBinIf The source CABAC engine. |
|---|
| 89 | */ |
|---|
| 90 | Void |
|---|
| 91 | TDecBinCABAC::copyState( TDecBinIf* pcTDecBinIf ) |
|---|
| 92 | { |
|---|
| 93 | TDecBinCABAC* pcTDecBinCABAC = pcTDecBinIf->getTDecBinCABAC(); |
|---|
| 94 | m_uiRange = pcTDecBinCABAC->m_uiRange; |
|---|
| 95 | m_uiValue = pcTDecBinCABAC->m_uiValue; |
|---|
| 96 | m_bitsNeeded= pcTDecBinCABAC->m_bitsNeeded; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | Void |
|---|
| 101 | TDecBinCABAC::decodeBin( UInt& ruiBin, ContextModel &rcCtxModel ) |
|---|
| 102 | { |
|---|
| 103 | UInt uiLPS = TComCABACTables::sm_aucLPSTable[ rcCtxModel.getState() ][ ( m_uiRange >> 6 ) - 4 ]; |
|---|
| 104 | m_uiRange -= uiLPS; |
|---|
| 105 | UInt scaledRange = m_uiRange << 7; |
|---|
| 106 | |
|---|
| 107 | if( m_uiValue < scaledRange ) |
|---|
| 108 | { |
|---|
| 109 | // MPS path |
|---|
| 110 | ruiBin = rcCtxModel.getMps(); |
|---|
| 111 | rcCtxModel.updateMPS(); |
|---|
| 112 | |
|---|
| 113 | if ( scaledRange >= ( 256 << 7 ) ) |
|---|
| 114 | { |
|---|
| 115 | return; |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | m_uiRange = scaledRange >> 6; |
|---|
| 119 | m_uiValue += m_uiValue; |
|---|
| 120 | |
|---|
| 121 | if ( ++m_bitsNeeded == 0 ) |
|---|
| 122 | { |
|---|
| 123 | m_bitsNeeded = -8; |
|---|
| 124 | m_uiValue += m_pcTComBitstream->readByte(); |
|---|
| 125 | } |
|---|
| 126 | } |
|---|
| 127 | else |
|---|
| 128 | { |
|---|
| 129 | // LPS path |
|---|
| 130 | Int numBits = TComCABACTables::sm_aucRenormTable[ uiLPS >> 3 ]; |
|---|
| 131 | m_uiValue = ( m_uiValue - scaledRange ) << numBits; |
|---|
| 132 | m_uiRange = uiLPS << numBits; |
|---|
| 133 | ruiBin = 1 - rcCtxModel.getMps(); |
|---|
| 134 | rcCtxModel.updateLPS(); |
|---|
| 135 | |
|---|
| 136 | m_bitsNeeded += numBits; |
|---|
| 137 | |
|---|
| 138 | if ( m_bitsNeeded >= 0 ) |
|---|
| 139 | { |
|---|
| 140 | m_uiValue += m_pcTComBitstream->readByte() << m_bitsNeeded; |
|---|
| 141 | m_bitsNeeded -= 8; |
|---|
| 142 | } |
|---|
| 143 | } |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | Void |
|---|
| 147 | TDecBinCABAC::decodeBinEP( UInt& ruiBin ) |
|---|
| 148 | { |
|---|
| 149 | m_uiValue += m_uiValue; |
|---|
| 150 | |
|---|
| 151 | if ( ++m_bitsNeeded >= 0 ) |
|---|
| 152 | { |
|---|
| 153 | m_bitsNeeded = -8; |
|---|
| 154 | m_uiValue += m_pcTComBitstream->readByte(); |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | ruiBin = 0; |
|---|
| 158 | UInt scaledRange = m_uiRange << 7; |
|---|
| 159 | if ( m_uiValue >= scaledRange ) |
|---|
| 160 | { |
|---|
| 161 | ruiBin = 1; |
|---|
| 162 | m_uiValue -= scaledRange; |
|---|
| 163 | } |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | Void TDecBinCABAC::decodeBinsEP( UInt& ruiBin, Int numBins ) |
|---|
| 167 | { |
|---|
| 168 | UInt bins = 0; |
|---|
| 169 | |
|---|
| 170 | while ( numBins > 8 ) |
|---|
| 171 | { |
|---|
| 172 | m_uiValue = ( m_uiValue << 8 ) + ( m_pcTComBitstream->readByte() << ( 8 + m_bitsNeeded ) ); |
|---|
| 173 | |
|---|
| 174 | UInt scaledRange = m_uiRange << 15; |
|---|
| 175 | for ( Int i = 0; i < 8; i++ ) |
|---|
| 176 | { |
|---|
| 177 | bins += bins; |
|---|
| 178 | scaledRange >>= 1; |
|---|
| 179 | if ( m_uiValue >= scaledRange ) |
|---|
| 180 | { |
|---|
| 181 | bins++; |
|---|
| 182 | m_uiValue -= scaledRange; |
|---|
| 183 | } |
|---|
| 184 | } |
|---|
| 185 | numBins -= 8; |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | m_bitsNeeded += numBins; |
|---|
| 189 | m_uiValue <<= numBins; |
|---|
| 190 | |
|---|
| 191 | if ( m_bitsNeeded >= 0 ) |
|---|
| 192 | { |
|---|
| 193 | m_uiValue += m_pcTComBitstream->readByte() << m_bitsNeeded; |
|---|
| 194 | m_bitsNeeded -= 8; |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | UInt scaledRange = m_uiRange << ( numBins + 7 ); |
|---|
| 198 | for ( Int i = 0; i < numBins; i++ ) |
|---|
| 199 | { |
|---|
| 200 | bins += bins; |
|---|
| 201 | scaledRange >>= 1; |
|---|
| 202 | if ( m_uiValue >= scaledRange ) |
|---|
| 203 | { |
|---|
| 204 | bins++; |
|---|
| 205 | m_uiValue -= scaledRange; |
|---|
| 206 | } |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | ruiBin = bins; |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | Void |
|---|
| 213 | TDecBinCABAC::decodeBinTrm( UInt& ruiBin ) |
|---|
| 214 | { |
|---|
| 215 | m_uiRange -= 2; |
|---|
| 216 | UInt scaledRange = m_uiRange << 7; |
|---|
| 217 | if( m_uiValue >= scaledRange ) |
|---|
| 218 | { |
|---|
| 219 | ruiBin = 1; |
|---|
| 220 | } |
|---|
| 221 | else |
|---|
| 222 | { |
|---|
| 223 | ruiBin = 0; |
|---|
| 224 | if ( scaledRange < ( 256 << 7 ) ) |
|---|
| 225 | { |
|---|
| 226 | m_uiRange = scaledRange >> 6; |
|---|
| 227 | m_uiValue += m_uiValue; |
|---|
| 228 | |
|---|
| 229 | if ( ++m_bitsNeeded == 0 ) |
|---|
| 230 | { |
|---|
| 231 | m_bitsNeeded = -8; |
|---|
| 232 | m_uiValue += m_pcTComBitstream->readByte(); |
|---|
| 233 | } |
|---|
| 234 | } |
|---|
| 235 | } |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | /** Read a PCM code. |
|---|
| 239 | * \param uiLength code bit-depth |
|---|
| 240 | * \param ruiCode pointer to PCM code value |
|---|
| 241 | * \returns Void |
|---|
| 242 | */ |
|---|
| 243 | Void TDecBinCABAC::xReadPCMCode(UInt uiLength, UInt& ruiCode) |
|---|
| 244 | { |
|---|
| 245 | assert ( uiLength > 0 ); |
|---|
| 246 | m_pcTComBitstream->read (uiLength, ruiCode); |
|---|
| 247 | } |
|---|
| 248 | //! \} |
|---|