[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 | * |
---|
[56] | 6 | * Copyright (c) 2010-2012, 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 | #if OL_FLUSH_ALIGN |
---|
| 113 | m_pcTComBitIf->writeAlignZero(); |
---|
| 114 | #endif |
---|
| 115 | |
---|
| 116 | start(); |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | /** Reset BAC register and counter values. |
---|
| 120 | * \returns Void |
---|
| 121 | */ |
---|
| 122 | Void TEncBinCABAC::resetBac() |
---|
| 123 | { |
---|
| 124 | start(); |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | /** Encode # of subsequent IPCM blocks. |
---|
| 128 | * \param numSubseqIPCM |
---|
| 129 | * \returns Void |
---|
| 130 | */ |
---|
| 131 | Void TEncBinCABAC::encodeNumSubseqIPCM( Int numSubseqIPCM ) |
---|
| 132 | { |
---|
| 133 | finish(); |
---|
| 134 | m_pcTComBitIf->write( 1, 1 ); // stop bit |
---|
| 135 | |
---|
| 136 | m_pcTComBitIf->write( numSubseqIPCM ? 1 : 0, 1); |
---|
| 137 | |
---|
| 138 | if ( numSubseqIPCM > 0) |
---|
| 139 | { |
---|
| 140 | Bool bCodeLast = ( 3 > numSubseqIPCM ); |
---|
| 141 | |
---|
| 142 | while( --numSubseqIPCM ) |
---|
| 143 | { |
---|
| 144 | m_pcTComBitIf->write( 1, 1 ); |
---|
| 145 | } |
---|
| 146 | if( bCodeLast ) |
---|
| 147 | { |
---|
| 148 | m_pcTComBitIf->write( 0, 1 ); |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | /** Encode PCM alignment zero bits. |
---|
| 154 | * \returns Void |
---|
| 155 | */ |
---|
| 156 | Void TEncBinCABAC::encodePCMAlignBits() |
---|
| 157 | { |
---|
| 158 | m_pcTComBitIf->writeAlignZero(); // pcm align zero |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | /** Write a PCM code. |
---|
| 162 | * \param uiCode code value |
---|
| 163 | * \param uiLength code bit-depth |
---|
| 164 | * \returns Void |
---|
| 165 | */ |
---|
| 166 | Void TEncBinCABAC::xWritePCMCode(UInt uiCode, UInt uiLength) |
---|
| 167 | { |
---|
| 168 | m_pcTComBitIf->write(uiCode, uiLength); |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | Void TEncBinCABAC::copyState( TEncBinIf* pcTEncBinIf ) |
---|
| 172 | { |
---|
[2] | 173 | TEncBinCABAC* pcTEncBinCABAC = pcTEncBinIf->getTEncBinCABAC(); |
---|
| 174 | m_uiLow = pcTEncBinCABAC->m_uiLow; |
---|
| 175 | m_uiRange = pcTEncBinCABAC->m_uiRange; |
---|
[56] | 176 | m_bitsLeft = pcTEncBinCABAC->m_bitsLeft; |
---|
| 177 | m_bufferedByte = pcTEncBinCABAC->m_bufferedByte; |
---|
| 178 | m_numBufferedBytes = pcTEncBinCABAC->m_numBufferedBytes; |
---|
| 179 | #if FAST_BIT_EST |
---|
| 180 | m_fracBits = pcTEncBinCABAC->m_fracBits; |
---|
| 181 | #endif |
---|
[2] | 182 | } |
---|
| 183 | |
---|
[56] | 184 | Void TEncBinCABAC::resetBits() |
---|
[2] | 185 | { |
---|
[56] | 186 | m_uiLow = 0; |
---|
| 187 | m_bitsLeft = 23; |
---|
| 188 | m_numBufferedBytes = 0; |
---|
| 189 | m_bufferedByte = 0xff; |
---|
| 190 | if ( m_binCountIncrement ) |
---|
| 191 | { |
---|
| 192 | m_uiBinsCoded = 0; |
---|
| 193 | } |
---|
| 194 | #if FAST_BIT_EST |
---|
| 195 | m_fracBits &= 32767; |
---|
| 196 | #endif |
---|
[2] | 197 | } |
---|
| 198 | |
---|
[56] | 199 | UInt TEncBinCABAC::getNumWrittenBits() |
---|
[2] | 200 | { |
---|
[56] | 201 | return m_pcTComBitIf->getNumberOfWrittenBits() + 8 * m_numBufferedBytes + 23 - m_bitsLeft; |
---|
[2] | 202 | } |
---|
| 203 | |
---|
[56] | 204 | /** |
---|
| 205 | * \brief Encode bin |
---|
| 206 | * |
---|
| 207 | * \param binValue bin value |
---|
| 208 | * \param rcCtxModel context model |
---|
| 209 | */ |
---|
| 210 | Void TEncBinCABAC::encodeBin( UInt binValue, ContextModel &rcCtxModel ) |
---|
[2] | 211 | { |
---|
| 212 | { |
---|
[56] | 213 | DTRACE_CABAC_VL( g_nSymbolCounter++ ) |
---|
[2] | 214 | DTRACE_CABAC_T( "\tstate=" ) |
---|
| 215 | DTRACE_CABAC_V( ( rcCtxModel.getState() << 1 ) + rcCtxModel.getMps() ) |
---|
| 216 | DTRACE_CABAC_T( "\tsymbol=" ) |
---|
[56] | 217 | DTRACE_CABAC_V( binValue ) |
---|
[2] | 218 | DTRACE_CABAC_T( "\n" ) |
---|
| 219 | } |
---|
[56] | 220 | m_uiBinsCoded += m_binCountIncrement; |
---|
| 221 | #if CABAC_INIT_FLAG |
---|
| 222 | rcCtxModel.setBinsCoded( 1 ); |
---|
| 223 | #endif |
---|
| 224 | |
---|
[2] | 225 | UInt uiLPS = TComCABACTables::sm_aucLPSTable[ rcCtxModel.getState() ][ ( m_uiRange >> 6 ) & 3 ]; |
---|
| 226 | m_uiRange -= uiLPS; |
---|
[56] | 227 | |
---|
| 228 | if( binValue != rcCtxModel.getMps() ) |
---|
[2] | 229 | { |
---|
[56] | 230 | Int numBits = TComCABACTables::sm_aucRenormTable[ uiLPS >> 3 ]; |
---|
| 231 | m_uiLow = ( m_uiLow + m_uiRange ) << numBits; |
---|
| 232 | m_uiRange = uiLPS << numBits; |
---|
[2] | 233 | rcCtxModel.updateLPS(); |
---|
[56] | 234 | |
---|
| 235 | m_bitsLeft -= numBits; |
---|
[2] | 236 | } |
---|
| 237 | else |
---|
| 238 | { |
---|
| 239 | rcCtxModel.updateMPS(); |
---|
[56] | 240 | if ( m_uiRange >= 256 ) |
---|
[2] | 241 | { |
---|
[56] | 242 | return; |
---|
[2] | 243 | } |
---|
[56] | 244 | |
---|
| 245 | m_uiLow <<= 1; |
---|
[2] | 246 | m_uiRange <<= 1; |
---|
[56] | 247 | m_bitsLeft--; |
---|
[2] | 248 | } |
---|
[56] | 249 | |
---|
| 250 | testAndWriteOut(); |
---|
[2] | 251 | } |
---|
| 252 | |
---|
[56] | 253 | /** |
---|
| 254 | * \brief Encode equiprobable bin |
---|
| 255 | * |
---|
| 256 | * \param binValue bin value |
---|
| 257 | */ |
---|
| 258 | Void TEncBinCABAC::encodeBinEP( UInt binValue ) |
---|
[2] | 259 | { |
---|
| 260 | { |
---|
[56] | 261 | DTRACE_CABAC_VL( g_nSymbolCounter++ ) |
---|
[2] | 262 | DTRACE_CABAC_T( "\tEPsymbol=" ) |
---|
[56] | 263 | DTRACE_CABAC_V( binValue ) |
---|
[2] | 264 | DTRACE_CABAC_T( "\n" ) |
---|
| 265 | } |
---|
[56] | 266 | m_uiBinsCoded += m_binCountIncrement; |
---|
[2] | 267 | m_uiLow <<= 1; |
---|
[56] | 268 | if( binValue ) |
---|
[2] | 269 | { |
---|
| 270 | m_uiLow += m_uiRange; |
---|
| 271 | } |
---|
[56] | 272 | m_bitsLeft--; |
---|
| 273 | |
---|
| 274 | testAndWriteOut(); |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | /** |
---|
| 278 | * \brief Encode equiprobable bins |
---|
| 279 | * |
---|
| 280 | * \param binValues bin values |
---|
| 281 | * \param numBins number of bins |
---|
| 282 | */ |
---|
| 283 | Void TEncBinCABAC::encodeBinsEP( UInt binValues, Int numBins ) |
---|
| 284 | { |
---|
| 285 | m_uiBinsCoded += numBins & -m_binCountIncrement; |
---|
| 286 | |
---|
| 287 | for ( Int i = 0; i < numBins; i++ ) |
---|
[2] | 288 | { |
---|
[56] | 289 | DTRACE_CABAC_VL( g_nSymbolCounter++ ) |
---|
| 290 | DTRACE_CABAC_T( "\tEPsymbol=" ) |
---|
| 291 | DTRACE_CABAC_V( ( binValues >> ( numBins - 1 - i ) ) & 1 ) |
---|
| 292 | DTRACE_CABAC_T( "\n" ) |
---|
[2] | 293 | } |
---|
[56] | 294 | |
---|
| 295 | while ( numBins > 8 ) |
---|
[2] | 296 | { |
---|
[56] | 297 | numBins -= 8; |
---|
| 298 | UInt pattern = binValues >> numBins; |
---|
| 299 | m_uiLow <<= 8; |
---|
| 300 | m_uiLow += m_uiRange * pattern; |
---|
| 301 | binValues -= pattern << numBins; |
---|
| 302 | m_bitsLeft -= 8; |
---|
| 303 | |
---|
| 304 | testAndWriteOut(); |
---|
[2] | 305 | } |
---|
[56] | 306 | |
---|
| 307 | m_uiLow <<= numBins; |
---|
| 308 | m_uiLow += m_uiRange * binValues; |
---|
| 309 | m_bitsLeft -= numBins; |
---|
| 310 | |
---|
| 311 | testAndWriteOut(); |
---|
| 312 | } |
---|
| 313 | |
---|
| 314 | /** |
---|
| 315 | * \brief Encode terminating bin |
---|
| 316 | * |
---|
| 317 | * \param binValue bin value |
---|
| 318 | */ |
---|
| 319 | Void TEncBinCABAC::encodeBinTrm( UInt binValue ) |
---|
| 320 | { |
---|
| 321 | m_uiBinsCoded += m_binCountIncrement; |
---|
| 322 | m_uiRange -= 2; |
---|
| 323 | if( binValue ) |
---|
| 324 | { |
---|
| 325 | m_uiLow += m_uiRange; |
---|
| 326 | m_uiLow <<= 7; |
---|
| 327 | m_uiRange = 2 << 7; |
---|
| 328 | m_bitsLeft -= 7; |
---|
| 329 | } |
---|
| 330 | else if ( m_uiRange >= 256 ) |
---|
| 331 | { |
---|
| 332 | return; |
---|
| 333 | } |
---|
[2] | 334 | else |
---|
| 335 | { |
---|
[56] | 336 | m_uiLow <<= 1; |
---|
| 337 | m_uiRange <<= 1; |
---|
| 338 | m_bitsLeft--; |
---|
[2] | 339 | } |
---|
[56] | 340 | |
---|
| 341 | testAndWriteOut(); |
---|
[2] | 342 | } |
---|
| 343 | |
---|
[56] | 344 | Void TEncBinCABAC::testAndWriteOut() |
---|
[2] | 345 | { |
---|
[56] | 346 | if ( m_bitsLeft < 12 ) |
---|
[2] | 347 | { |
---|
[56] | 348 | writeOut(); |
---|
[2] | 349 | } |
---|
[56] | 350 | } |
---|
| 351 | |
---|
| 352 | /** |
---|
| 353 | * \brief Move bits from register into bitstream |
---|
| 354 | */ |
---|
| 355 | Void TEncBinCABAC::writeOut() |
---|
| 356 | { |
---|
| 357 | UInt leadByte = m_uiLow >> (24 - m_bitsLeft); |
---|
| 358 | m_bitsLeft += 8; |
---|
| 359 | m_uiLow &= 0xffffffffu >> m_bitsLeft; |
---|
| 360 | |
---|
| 361 | if ( leadByte == 0xff ) |
---|
[2] | 362 | { |
---|
[56] | 363 | m_numBufferedBytes++; |
---|
[2] | 364 | } |
---|
[56] | 365 | else |
---|
[2] | 366 | { |
---|
[56] | 367 | if ( m_numBufferedBytes > 0 ) |
---|
[2] | 368 | { |
---|
[56] | 369 | UInt carry = leadByte >> 8; |
---|
| 370 | UInt byte = m_bufferedByte + carry; |
---|
| 371 | m_bufferedByte = leadByte & 0xff; |
---|
| 372 | m_pcTComBitIf->write( byte, 8 ); |
---|
| 373 | |
---|
| 374 | byte = ( 0xff + carry ) & 0xff; |
---|
| 375 | while ( m_numBufferedBytes > 1 ) |
---|
| 376 | { |
---|
| 377 | m_pcTComBitIf->write( byte, 8 ); |
---|
| 378 | m_numBufferedBytes--; |
---|
| 379 | } |
---|
[2] | 380 | } |
---|
| 381 | else |
---|
| 382 | { |
---|
[56] | 383 | m_numBufferedBytes = 1; |
---|
| 384 | m_bufferedByte = leadByte; |
---|
| 385 | } |
---|
| 386 | } |
---|
[2] | 387 | } |
---|
| 388 | |
---|
[56] | 389 | /** flush bits when CABAC termination |
---|
| 390 | * \param [in] bEnd true means this flushing happens at the end of RBSP. No need to encode stop bit |
---|
| 391 | */ |
---|
| 392 | Void TEncBinCABAC::encodeFlush(Bool bEnd) |
---|
[2] | 393 | { |
---|
[56] | 394 | m_uiRange = 2; |
---|
| 395 | |
---|
| 396 | m_uiLow += 2; |
---|
| 397 | m_uiLow <<= 7; |
---|
| 398 | m_uiRange = 2 << 7; |
---|
| 399 | m_bitsLeft -= 7; |
---|
| 400 | testAndWriteOut(); |
---|
| 401 | finish(); |
---|
| 402 | |
---|
| 403 | if(!bEnd) |
---|
[2] | 404 | { |
---|
[56] | 405 | m_pcTComBitIf->write( 1, 1 ); // stop bit |
---|
[2] | 406 | } |
---|
| 407 | } |
---|
| 408 | |
---|
[56] | 409 | //! \} |
---|