[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 |
---|
[56] | 4 | * granted under this license. |
---|
[5] | 5 | * |
---|
[608] | 6 | * Copyright (c) 2010-2013, 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 TEncBinCoderCABAC.cpp |
---|
| 35 | \brief binary entropy encoder of CABAC |
---|
| 36 | */ |
---|
| 37 | |
---|
| 38 | #include "TEncBinCoderCABAC.h" |
---|
[56] | 39 | #include "TLibCommon/TComRom.h" |
---|
[2] | 40 | |
---|
| 41 | |
---|
[56] | 42 | //! \ingroup TLibEncoder |
---|
| 43 | //! \{ |
---|
| 44 | |
---|
| 45 | |
---|
[2] | 46 | TEncBinCABAC::TEncBinCABAC() |
---|
| 47 | : m_pcTComBitIf( 0 ) |
---|
[56] | 48 | , m_binCountIncrement( 0 ) |
---|
| 49 | #if FAST_BIT_EST |
---|
| 50 | , m_fracBits( 0 ) |
---|
| 51 | #endif |
---|
[2] | 52 | { |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | TEncBinCABAC::~TEncBinCABAC() |
---|
| 56 | { |
---|
| 57 | } |
---|
| 58 | |
---|
[56] | 59 | Void TEncBinCABAC::init( TComBitIf* pcTComBitIf ) |
---|
[2] | 60 | { |
---|
| 61 | m_pcTComBitIf = pcTComBitIf; |
---|
| 62 | } |
---|
| 63 | |
---|
[56] | 64 | Void TEncBinCABAC::uninit() |
---|
[2] | 65 | { |
---|
| 66 | m_pcTComBitIf = 0; |
---|
| 67 | } |
---|
| 68 | |
---|
[56] | 69 | Void TEncBinCABAC::start() |
---|
[2] | 70 | { |
---|
[56] | 71 | m_uiLow = 0; |
---|
| 72 | m_uiRange = 510; |
---|
| 73 | m_bitsLeft = 23; |
---|
| 74 | m_numBufferedBytes = 0; |
---|
| 75 | m_bufferedByte = 0xff; |
---|
[2] | 76 | } |
---|
| 77 | |
---|
[56] | 78 | Void TEncBinCABAC::finish() |
---|
[2] | 79 | { |
---|
[56] | 80 | if ( m_uiLow >> ( 32 - m_bitsLeft ) ) |
---|
[2] | 81 | { |
---|
[56] | 82 | //assert( m_numBufferedBytes > 0 ); |
---|
| 83 | //assert( m_bufferedByte != 0xff ); |
---|
| 84 | m_pcTComBitIf->write( m_bufferedByte + 1, 8 ); |
---|
| 85 | while ( m_numBufferedBytes > 1 ) |
---|
| 86 | { |
---|
| 87 | m_pcTComBitIf->write( 0x00, 8 ); |
---|
| 88 | m_numBufferedBytes--; |
---|
| 89 | } |
---|
| 90 | m_uiLow -= 1 << ( 32 - m_bitsLeft ); |
---|
[2] | 91 | } |
---|
[56] | 92 | else |
---|
| 93 | { |
---|
| 94 | if ( m_numBufferedBytes > 0 ) |
---|
| 95 | { |
---|
| 96 | m_pcTComBitIf->write( m_bufferedByte, 8 ); |
---|
| 97 | } |
---|
| 98 | while ( m_numBufferedBytes > 1 ) |
---|
| 99 | { |
---|
| 100 | m_pcTComBitIf->write( 0xff, 8 ); |
---|
| 101 | m_numBufferedBytes--; |
---|
| 102 | } |
---|
| 103 | } |
---|
| 104 | m_pcTComBitIf->write( m_uiLow >> 8, 24 - m_bitsLeft ); |
---|
[2] | 105 | } |
---|
| 106 | |
---|
[56] | 107 | Void TEncBinCABAC::flush() |
---|
[2] | 108 | { |
---|
[56] | 109 | encodeBinTrm(1); |
---|
| 110 | finish(); |
---|
| 111 | m_pcTComBitIf->write(1, 1); |
---|
| 112 | m_pcTComBitIf->writeAlignZero(); |
---|
| 113 | |
---|
| 114 | start(); |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | /** Reset BAC register and counter values. |
---|
| 118 | * \returns Void |
---|
| 119 | */ |
---|
| 120 | Void TEncBinCABAC::resetBac() |
---|
| 121 | { |
---|
| 122 | start(); |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | /** Encode PCM alignment zero bits. |
---|
| 126 | * \returns Void |
---|
| 127 | */ |
---|
| 128 | Void TEncBinCABAC::encodePCMAlignBits() |
---|
| 129 | { |
---|
[608] | 130 | finish(); |
---|
| 131 | m_pcTComBitIf->write(1, 1); |
---|
[56] | 132 | m_pcTComBitIf->writeAlignZero(); // pcm align zero |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | /** Write a PCM code. |
---|
| 136 | * \param uiCode code value |
---|
| 137 | * \param uiLength code bit-depth |
---|
| 138 | * \returns Void |
---|
| 139 | */ |
---|
| 140 | Void TEncBinCABAC::xWritePCMCode(UInt uiCode, UInt uiLength) |
---|
| 141 | { |
---|
| 142 | m_pcTComBitIf->write(uiCode, uiLength); |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | Void TEncBinCABAC::copyState( TEncBinIf* pcTEncBinIf ) |
---|
| 146 | { |
---|
[2] | 147 | TEncBinCABAC* pcTEncBinCABAC = pcTEncBinIf->getTEncBinCABAC(); |
---|
| 148 | m_uiLow = pcTEncBinCABAC->m_uiLow; |
---|
| 149 | m_uiRange = pcTEncBinCABAC->m_uiRange; |
---|
[56] | 150 | m_bitsLeft = pcTEncBinCABAC->m_bitsLeft; |
---|
| 151 | m_bufferedByte = pcTEncBinCABAC->m_bufferedByte; |
---|
| 152 | m_numBufferedBytes = pcTEncBinCABAC->m_numBufferedBytes; |
---|
| 153 | #if FAST_BIT_EST |
---|
| 154 | m_fracBits = pcTEncBinCABAC->m_fracBits; |
---|
| 155 | #endif |
---|
[2] | 156 | } |
---|
| 157 | |
---|
[56] | 158 | Void TEncBinCABAC::resetBits() |
---|
[2] | 159 | { |
---|
[56] | 160 | m_uiLow = 0; |
---|
| 161 | m_bitsLeft = 23; |
---|
| 162 | m_numBufferedBytes = 0; |
---|
| 163 | m_bufferedByte = 0xff; |
---|
| 164 | if ( m_binCountIncrement ) |
---|
| 165 | { |
---|
| 166 | m_uiBinsCoded = 0; |
---|
| 167 | } |
---|
| 168 | #if FAST_BIT_EST |
---|
| 169 | m_fracBits &= 32767; |
---|
| 170 | #endif |
---|
[2] | 171 | } |
---|
| 172 | |
---|
[56] | 173 | UInt TEncBinCABAC::getNumWrittenBits() |
---|
[2] | 174 | { |
---|
[56] | 175 | return m_pcTComBitIf->getNumberOfWrittenBits() + 8 * m_numBufferedBytes + 23 - m_bitsLeft; |
---|
[2] | 176 | } |
---|
| 177 | |
---|
[56] | 178 | /** |
---|
| 179 | * \brief Encode bin |
---|
| 180 | * |
---|
| 181 | * \param binValue bin value |
---|
| 182 | * \param rcCtxModel context model |
---|
| 183 | */ |
---|
| 184 | Void TEncBinCABAC::encodeBin( UInt binValue, ContextModel &rcCtxModel ) |
---|
[2] | 185 | { |
---|
| 186 | { |
---|
[608] | 187 | #if !H_MV |
---|
[56] | 188 | DTRACE_CABAC_VL( g_nSymbolCounter++ ) |
---|
[2] | 189 | DTRACE_CABAC_T( "\tstate=" ) |
---|
| 190 | DTRACE_CABAC_V( ( rcCtxModel.getState() << 1 ) + rcCtxModel.getMps() ) |
---|
| 191 | DTRACE_CABAC_T( "\tsymbol=" ) |
---|
[56] | 192 | DTRACE_CABAC_V( binValue ) |
---|
[2] | 193 | DTRACE_CABAC_T( "\n" ) |
---|
[608] | 194 | #endif |
---|
[2] | 195 | } |
---|
[56] | 196 | m_uiBinsCoded += m_binCountIncrement; |
---|
| 197 | rcCtxModel.setBinsCoded( 1 ); |
---|
| 198 | |
---|
[2] | 199 | UInt uiLPS = TComCABACTables::sm_aucLPSTable[ rcCtxModel.getState() ][ ( m_uiRange >> 6 ) & 3 ]; |
---|
| 200 | m_uiRange -= uiLPS; |
---|
[56] | 201 | |
---|
| 202 | if( binValue != rcCtxModel.getMps() ) |
---|
[2] | 203 | { |
---|
[56] | 204 | Int numBits = TComCABACTables::sm_aucRenormTable[ uiLPS >> 3 ]; |
---|
| 205 | m_uiLow = ( m_uiLow + m_uiRange ) << numBits; |
---|
| 206 | m_uiRange = uiLPS << numBits; |
---|
[2] | 207 | rcCtxModel.updateLPS(); |
---|
[56] | 208 | |
---|
| 209 | m_bitsLeft -= numBits; |
---|
[2] | 210 | } |
---|
| 211 | else |
---|
| 212 | { |
---|
| 213 | rcCtxModel.updateMPS(); |
---|
[56] | 214 | if ( m_uiRange >= 256 ) |
---|
[2] | 215 | { |
---|
[56] | 216 | return; |
---|
[2] | 217 | } |
---|
[56] | 218 | |
---|
| 219 | m_uiLow <<= 1; |
---|
[2] | 220 | m_uiRange <<= 1; |
---|
[56] | 221 | m_bitsLeft--; |
---|
[2] | 222 | } |
---|
[56] | 223 | |
---|
| 224 | testAndWriteOut(); |
---|
[2] | 225 | } |
---|
| 226 | |
---|
[56] | 227 | /** |
---|
| 228 | * \brief Encode equiprobable bin |
---|
| 229 | * |
---|
| 230 | * \param binValue bin value |
---|
| 231 | */ |
---|
| 232 | Void TEncBinCABAC::encodeBinEP( UInt binValue ) |
---|
[2] | 233 | { |
---|
| 234 | { |
---|
[608] | 235 | #if !H_MV |
---|
[56] | 236 | DTRACE_CABAC_VL( g_nSymbolCounter++ ) |
---|
[2] | 237 | DTRACE_CABAC_T( "\tEPsymbol=" ) |
---|
[56] | 238 | DTRACE_CABAC_V( binValue ) |
---|
[2] | 239 | DTRACE_CABAC_T( "\n" ) |
---|
[608] | 240 | #endif |
---|
[2] | 241 | } |
---|
[56] | 242 | m_uiBinsCoded += m_binCountIncrement; |
---|
[2] | 243 | m_uiLow <<= 1; |
---|
[56] | 244 | if( binValue ) |
---|
[2] | 245 | { |
---|
| 246 | m_uiLow += m_uiRange; |
---|
| 247 | } |
---|
[56] | 248 | m_bitsLeft--; |
---|
| 249 | |
---|
| 250 | testAndWriteOut(); |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | /** |
---|
| 254 | * \brief Encode equiprobable bins |
---|
| 255 | * |
---|
| 256 | * \param binValues bin values |
---|
| 257 | * \param numBins number of bins |
---|
| 258 | */ |
---|
| 259 | Void TEncBinCABAC::encodeBinsEP( UInt binValues, Int numBins ) |
---|
| 260 | { |
---|
| 261 | m_uiBinsCoded += numBins & -m_binCountIncrement; |
---|
| 262 | |
---|
| 263 | for ( Int i = 0; i < numBins; i++ ) |
---|
[2] | 264 | { |
---|
[608] | 265 | #if !H_MV |
---|
[56] | 266 | DTRACE_CABAC_VL( g_nSymbolCounter++ ) |
---|
| 267 | DTRACE_CABAC_T( "\tEPsymbol=" ) |
---|
| 268 | DTRACE_CABAC_V( ( binValues >> ( numBins - 1 - i ) ) & 1 ) |
---|
| 269 | DTRACE_CABAC_T( "\n" ) |
---|
[608] | 270 | #endif |
---|
[2] | 271 | } |
---|
[56] | 272 | |
---|
| 273 | while ( numBins > 8 ) |
---|
[2] | 274 | { |
---|
[56] | 275 | numBins -= 8; |
---|
| 276 | UInt pattern = binValues >> numBins; |
---|
| 277 | m_uiLow <<= 8; |
---|
| 278 | m_uiLow += m_uiRange * pattern; |
---|
| 279 | binValues -= pattern << numBins; |
---|
| 280 | m_bitsLeft -= 8; |
---|
| 281 | |
---|
| 282 | testAndWriteOut(); |
---|
[2] | 283 | } |
---|
[56] | 284 | |
---|
| 285 | m_uiLow <<= numBins; |
---|
| 286 | m_uiLow += m_uiRange * binValues; |
---|
| 287 | m_bitsLeft -= numBins; |
---|
| 288 | |
---|
| 289 | testAndWriteOut(); |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | /** |
---|
| 293 | * \brief Encode terminating bin |
---|
| 294 | * |
---|
| 295 | * \param binValue bin value |
---|
| 296 | */ |
---|
| 297 | Void TEncBinCABAC::encodeBinTrm( UInt binValue ) |
---|
| 298 | { |
---|
| 299 | m_uiBinsCoded += m_binCountIncrement; |
---|
| 300 | m_uiRange -= 2; |
---|
| 301 | if( binValue ) |
---|
| 302 | { |
---|
| 303 | m_uiLow += m_uiRange; |
---|
| 304 | m_uiLow <<= 7; |
---|
| 305 | m_uiRange = 2 << 7; |
---|
| 306 | m_bitsLeft -= 7; |
---|
| 307 | } |
---|
| 308 | else if ( m_uiRange >= 256 ) |
---|
| 309 | { |
---|
| 310 | return; |
---|
| 311 | } |
---|
[2] | 312 | else |
---|
| 313 | { |
---|
[56] | 314 | m_uiLow <<= 1; |
---|
| 315 | m_uiRange <<= 1; |
---|
| 316 | m_bitsLeft--; |
---|
[2] | 317 | } |
---|
[56] | 318 | |
---|
| 319 | testAndWriteOut(); |
---|
[2] | 320 | } |
---|
| 321 | |
---|
[56] | 322 | Void TEncBinCABAC::testAndWriteOut() |
---|
[2] | 323 | { |
---|
[56] | 324 | if ( m_bitsLeft < 12 ) |
---|
[2] | 325 | { |
---|
[56] | 326 | writeOut(); |
---|
[2] | 327 | } |
---|
[56] | 328 | } |
---|
| 329 | |
---|
| 330 | /** |
---|
| 331 | * \brief Move bits from register into bitstream |
---|
| 332 | */ |
---|
| 333 | Void TEncBinCABAC::writeOut() |
---|
| 334 | { |
---|
| 335 | UInt leadByte = m_uiLow >> (24 - m_bitsLeft); |
---|
| 336 | m_bitsLeft += 8; |
---|
| 337 | m_uiLow &= 0xffffffffu >> m_bitsLeft; |
---|
| 338 | |
---|
| 339 | if ( leadByte == 0xff ) |
---|
[2] | 340 | { |
---|
[56] | 341 | m_numBufferedBytes++; |
---|
[2] | 342 | } |
---|
[56] | 343 | else |
---|
[2] | 344 | { |
---|
[56] | 345 | if ( m_numBufferedBytes > 0 ) |
---|
[2] | 346 | { |
---|
[56] | 347 | UInt carry = leadByte >> 8; |
---|
| 348 | UInt byte = m_bufferedByte + carry; |
---|
| 349 | m_bufferedByte = leadByte & 0xff; |
---|
| 350 | m_pcTComBitIf->write( byte, 8 ); |
---|
| 351 | |
---|
| 352 | byte = ( 0xff + carry ) & 0xff; |
---|
| 353 | while ( m_numBufferedBytes > 1 ) |
---|
| 354 | { |
---|
| 355 | m_pcTComBitIf->write( byte, 8 ); |
---|
| 356 | m_numBufferedBytes--; |
---|
| 357 | } |
---|
[2] | 358 | } |
---|
| 359 | else |
---|
| 360 | { |
---|
[56] | 361 | m_numBufferedBytes = 1; |
---|
| 362 | m_bufferedByte = leadByte; |
---|
| 363 | } |
---|
| 364 | } |
---|
[2] | 365 | } |
---|
| 366 | |
---|
[56] | 367 | //! \} |
---|