[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 TEncBinCoderCABAC.cpp |
---|
| 35 | \brief binary entropy encoder of CABAC |
---|
| 36 | */ |
---|
| 37 | |
---|
| 38 | #include "TEncBinCoderCABAC.h" |
---|
[56] | 39 | #include "TLibCommon/TComRom.h" |
---|
[1200] | 40 | #include "TLibCommon/Debug.h" |
---|
[2] | 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; |
---|
[1200] | 76 | #if FAST_BIT_EST |
---|
| 77 | m_fracBits = 0; |
---|
| 78 | #endif |
---|
[2] | 79 | } |
---|
| 80 | |
---|
[56] | 81 | Void TEncBinCABAC::finish() |
---|
[2] | 82 | { |
---|
[56] | 83 | if ( m_uiLow >> ( 32 - m_bitsLeft ) ) |
---|
[2] | 84 | { |
---|
[56] | 85 | //assert( m_numBufferedBytes > 0 ); |
---|
| 86 | //assert( m_bufferedByte != 0xff ); |
---|
| 87 | m_pcTComBitIf->write( m_bufferedByte + 1, 8 ); |
---|
| 88 | while ( m_numBufferedBytes > 1 ) |
---|
| 89 | { |
---|
| 90 | m_pcTComBitIf->write( 0x00, 8 ); |
---|
| 91 | m_numBufferedBytes--; |
---|
| 92 | } |
---|
| 93 | m_uiLow -= 1 << ( 32 - m_bitsLeft ); |
---|
[2] | 94 | } |
---|
[56] | 95 | else |
---|
| 96 | { |
---|
| 97 | if ( m_numBufferedBytes > 0 ) |
---|
| 98 | { |
---|
| 99 | m_pcTComBitIf->write( m_bufferedByte, 8 ); |
---|
| 100 | } |
---|
| 101 | while ( m_numBufferedBytes > 1 ) |
---|
| 102 | { |
---|
| 103 | m_pcTComBitIf->write( 0xff, 8 ); |
---|
| 104 | m_numBufferedBytes--; |
---|
[1200] | 105 | } |
---|
[56] | 106 | } |
---|
| 107 | m_pcTComBitIf->write( m_uiLow >> 8, 24 - m_bitsLeft ); |
---|
[2] | 108 | } |
---|
| 109 | |
---|
[56] | 110 | Void TEncBinCABAC::flush() |
---|
[2] | 111 | { |
---|
[56] | 112 | encodeBinTrm(1); |
---|
| 113 | finish(); |
---|
| 114 | m_pcTComBitIf->write(1, 1); |
---|
| 115 | m_pcTComBitIf->writeAlignZero(); |
---|
| 116 | |
---|
| 117 | start(); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | /** Reset BAC register and counter values. |
---|
| 121 | * \returns Void |
---|
| 122 | */ |
---|
| 123 | Void TEncBinCABAC::resetBac() |
---|
| 124 | { |
---|
| 125 | start(); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | /** Encode PCM alignment zero bits. |
---|
| 129 | * \returns Void |
---|
| 130 | */ |
---|
| 131 | Void TEncBinCABAC::encodePCMAlignBits() |
---|
| 132 | { |
---|
[608] | 133 | finish(); |
---|
| 134 | m_pcTComBitIf->write(1, 1); |
---|
[56] | 135 | m_pcTComBitIf->writeAlignZero(); // pcm align zero |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | /** Write a PCM code. |
---|
| 139 | * \param uiCode code value |
---|
| 140 | * \param uiLength code bit-depth |
---|
| 141 | * \returns Void |
---|
| 142 | */ |
---|
| 143 | Void TEncBinCABAC::xWritePCMCode(UInt uiCode, UInt uiLength) |
---|
| 144 | { |
---|
| 145 | m_pcTComBitIf->write(uiCode, uiLength); |
---|
| 146 | } |
---|
| 147 | |
---|
[1200] | 148 | Void TEncBinCABAC::copyState( const TEncBinIf* pcTEncBinIf ) |
---|
[56] | 149 | { |
---|
[1200] | 150 | const TEncBinCABAC* pcTEncBinCABAC = pcTEncBinIf->getTEncBinCABAC(); |
---|
[2] | 151 | m_uiLow = pcTEncBinCABAC->m_uiLow; |
---|
| 152 | m_uiRange = pcTEncBinCABAC->m_uiRange; |
---|
[56] | 153 | m_bitsLeft = pcTEncBinCABAC->m_bitsLeft; |
---|
| 154 | m_bufferedByte = pcTEncBinCABAC->m_bufferedByte; |
---|
| 155 | m_numBufferedBytes = pcTEncBinCABAC->m_numBufferedBytes; |
---|
| 156 | #if FAST_BIT_EST |
---|
| 157 | m_fracBits = pcTEncBinCABAC->m_fracBits; |
---|
| 158 | #endif |
---|
[2] | 159 | } |
---|
| 160 | |
---|
[56] | 161 | Void TEncBinCABAC::resetBits() |
---|
[2] | 162 | { |
---|
[56] | 163 | m_uiLow = 0; |
---|
| 164 | m_bitsLeft = 23; |
---|
| 165 | m_numBufferedBytes = 0; |
---|
| 166 | m_bufferedByte = 0xff; |
---|
| 167 | if ( m_binCountIncrement ) |
---|
| 168 | { |
---|
| 169 | m_uiBinsCoded = 0; |
---|
| 170 | } |
---|
| 171 | #if FAST_BIT_EST |
---|
| 172 | m_fracBits &= 32767; |
---|
| 173 | #endif |
---|
[2] | 174 | } |
---|
| 175 | |
---|
[56] | 176 | UInt TEncBinCABAC::getNumWrittenBits() |
---|
[2] | 177 | { |
---|
[56] | 178 | return m_pcTComBitIf->getNumberOfWrittenBits() + 8 * m_numBufferedBytes + 23 - m_bitsLeft; |
---|
[2] | 179 | } |
---|
| 180 | |
---|
[56] | 181 | /** |
---|
| 182 | * \brief Encode bin |
---|
| 183 | * |
---|
| 184 | * \param binValue bin value |
---|
| 185 | * \param rcCtxModel context model |
---|
| 186 | */ |
---|
| 187 | Void TEncBinCABAC::encodeBin( UInt binValue, ContextModel &rcCtxModel ) |
---|
[2] | 188 | { |
---|
[1200] | 189 | #if !NH_MV |
---|
| 190 | //{ |
---|
| 191 | // DTRACE_CABAC_VL( g_nSymbolCounter++ ) |
---|
| 192 | // DTRACE_CABAC_T( "\tstate=" ) |
---|
| 193 | // DTRACE_CABAC_V( ( rcCtxModel.getState() << 1 ) + rcCtxModel.getMps() ) |
---|
| 194 | // DTRACE_CABAC_T( "\tsymbol=" ) |
---|
| 195 | // DTRACE_CABAC_V( binValue ) |
---|
| 196 | // DTRACE_CABAC_T( "\n" ) |
---|
| 197 | //} |
---|
[608] | 198 | #endif |
---|
[1200] | 199 | #if DEBUG_CABAC_BINS |
---|
| 200 | const UInt startingRange = m_uiRange; |
---|
| 201 | #endif |
---|
| 202 | |
---|
[56] | 203 | m_uiBinsCoded += m_binCountIncrement; |
---|
| 204 | rcCtxModel.setBinsCoded( 1 ); |
---|
[1200] | 205 | |
---|
[2] | 206 | UInt uiLPS = TComCABACTables::sm_aucLPSTable[ rcCtxModel.getState() ][ ( m_uiRange >> 6 ) & 3 ]; |
---|
| 207 | m_uiRange -= uiLPS; |
---|
[1200] | 208 | |
---|
[56] | 209 | if( binValue != rcCtxModel.getMps() ) |
---|
[2] | 210 | { |
---|
[56] | 211 | Int numBits = TComCABACTables::sm_aucRenormTable[ uiLPS >> 3 ]; |
---|
| 212 | m_uiLow = ( m_uiLow + m_uiRange ) << numBits; |
---|
| 213 | m_uiRange = uiLPS << numBits; |
---|
[2] | 214 | rcCtxModel.updateLPS(); |
---|
[56] | 215 | m_bitsLeft -= numBits; |
---|
[1200] | 216 | testAndWriteOut(); |
---|
[2] | 217 | } |
---|
| 218 | else |
---|
| 219 | { |
---|
| 220 | rcCtxModel.updateMPS(); |
---|
[1200] | 221 | |
---|
| 222 | if ( m_uiRange < 256 ) |
---|
[2] | 223 | { |
---|
[1200] | 224 | m_uiLow <<= 1; |
---|
| 225 | m_uiRange <<= 1; |
---|
| 226 | m_bitsLeft--; |
---|
| 227 | testAndWriteOut(); |
---|
[2] | 228 | } |
---|
| 229 | } |
---|
[1200] | 230 | |
---|
| 231 | #if DEBUG_CABAC_BINS |
---|
| 232 | if ((g_debugCounter + debugCabacBinWindow) >= debugCabacBinTargetLine) |
---|
| 233 | { |
---|
| 234 | std::cout << g_debugCounter << ": coding bin value " << binValue << ", range = [" << startingRange << "->" << m_uiRange << "]\n"; |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | if (g_debugCounter >= debugCabacBinTargetLine) |
---|
| 238 | { |
---|
| 239 | Char breakPointThis; |
---|
| 240 | breakPointThis = 7; |
---|
| 241 | } |
---|
| 242 | if (g_debugCounter >= (debugCabacBinTargetLine + debugCabacBinWindow)) |
---|
| 243 | { |
---|
| 244 | exit(0); |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | g_debugCounter++; |
---|
| 248 | #endif |
---|
[2] | 249 | } |
---|
| 250 | |
---|
[56] | 251 | /** |
---|
| 252 | * \brief Encode equiprobable bin |
---|
| 253 | * |
---|
| 254 | * \param binValue bin value |
---|
| 255 | */ |
---|
| 256 | Void TEncBinCABAC::encodeBinEP( UInt binValue ) |
---|
[2] | 257 | { |
---|
[1200] | 258 | if (false) |
---|
[2] | 259 | { |
---|
[1200] | 260 | #if !NH_MV |
---|
[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" ) |
---|
[608] | 265 | #endif |
---|
[2] | 266 | } |
---|
[1200] | 267 | |
---|
[56] | 268 | m_uiBinsCoded += m_binCountIncrement; |
---|
[1200] | 269 | |
---|
| 270 | if (m_uiRange == 256) |
---|
| 271 | { |
---|
| 272 | encodeAlignedBinsEP(binValue, 1); |
---|
| 273 | return; |
---|
| 274 | } |
---|
| 275 | |
---|
[2] | 276 | m_uiLow <<= 1; |
---|
[56] | 277 | if( binValue ) |
---|
[2] | 278 | { |
---|
| 279 | m_uiLow += m_uiRange; |
---|
| 280 | } |
---|
[56] | 281 | m_bitsLeft--; |
---|
[1200] | 282 | |
---|
[56] | 283 | testAndWriteOut(); |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | /** |
---|
| 287 | * \brief Encode equiprobable bins |
---|
| 288 | * |
---|
| 289 | * \param binValues bin values |
---|
| 290 | * \param numBins number of bins |
---|
| 291 | */ |
---|
| 292 | Void TEncBinCABAC::encodeBinsEP( UInt binValues, Int numBins ) |
---|
| 293 | { |
---|
| 294 | m_uiBinsCoded += numBins & -m_binCountIncrement; |
---|
[1200] | 295 | |
---|
| 296 | if (false) |
---|
[2] | 297 | { |
---|
[1200] | 298 | for ( Int i = 0; i < numBins; i++ ) |
---|
| 299 | { |
---|
| 300 | #if !NH_MV |
---|
| 301 | DTRACE_CABAC_VL( g_nSymbolCounter++ ) |
---|
| 302 | DTRACE_CABAC_T( "\tEPsymbol=" ) |
---|
| 303 | DTRACE_CABAC_V( ( binValues >> ( numBins - 1 - i ) ) & 1 ) |
---|
| 304 | DTRACE_CABAC_T( "\n" ) |
---|
[608] | 305 | #endif |
---|
[1200] | 306 | } |
---|
[2] | 307 | } |
---|
[1200] | 308 | |
---|
| 309 | if (m_uiRange == 256) |
---|
| 310 | { |
---|
| 311 | encodeAlignedBinsEP(binValues, numBins); |
---|
| 312 | return; |
---|
| 313 | } |
---|
| 314 | |
---|
[56] | 315 | while ( numBins > 8 ) |
---|
[2] | 316 | { |
---|
[56] | 317 | numBins -= 8; |
---|
[1200] | 318 | UInt pattern = binValues >> numBins; |
---|
[56] | 319 | m_uiLow <<= 8; |
---|
| 320 | m_uiLow += m_uiRange * pattern; |
---|
| 321 | binValues -= pattern << numBins; |
---|
| 322 | m_bitsLeft -= 8; |
---|
[1200] | 323 | |
---|
[56] | 324 | testAndWriteOut(); |
---|
[2] | 325 | } |
---|
[1200] | 326 | |
---|
[56] | 327 | m_uiLow <<= numBins; |
---|
| 328 | m_uiLow += m_uiRange * binValues; |
---|
| 329 | m_bitsLeft -= numBins; |
---|
[1200] | 330 | |
---|
[56] | 331 | testAndWriteOut(); |
---|
| 332 | } |
---|
| 333 | |
---|
[1200] | 334 | Void TEncBinCABAC::align() |
---|
| 335 | { |
---|
| 336 | m_uiRange = 256; |
---|
| 337 | } |
---|
| 338 | |
---|
| 339 | Void TEncBinCABAC::encodeAlignedBinsEP( UInt binValues, Int numBins ) |
---|
| 340 | { |
---|
| 341 | Int binsRemaining = numBins; |
---|
| 342 | |
---|
| 343 | assert(m_uiRange == 256); //aligned encode only works when range = 256 |
---|
| 344 | |
---|
| 345 | while (binsRemaining > 0) |
---|
| 346 | { |
---|
| 347 | const UInt binsToCode = std::min<UInt>(binsRemaining, 8); //code bytes if able to take advantage of the system's byte-write function |
---|
| 348 | const UInt binMask = (1 << binsToCode) - 1; |
---|
| 349 | |
---|
| 350 | const UInt newBins = (binValues >> (binsRemaining - binsToCode)) & binMask; |
---|
| 351 | |
---|
| 352 | //The process of encoding an EP bin is the same as that of coding a normal |
---|
| 353 | //bin where the symbol ranges for 1 and 0 are both half the range: |
---|
| 354 | // |
---|
| 355 | // low = (low + range/2) << 1 (to encode a 1) |
---|
| 356 | // low = low << 1 (to encode a 0) |
---|
| 357 | // |
---|
| 358 | // i.e. |
---|
| 359 | // low = (low + (bin * range/2)) << 1 |
---|
| 360 | // |
---|
| 361 | // which is equivalent to: |
---|
| 362 | // |
---|
| 363 | // low = (low << 1) + (bin * range) |
---|
| 364 | // |
---|
| 365 | // this can be generalised for multiple bins, producing the following expression: |
---|
| 366 | // |
---|
| 367 | m_uiLow = (m_uiLow << binsToCode) + (newBins << 8); //range is known to be 256 |
---|
| 368 | |
---|
| 369 | binsRemaining -= binsToCode; |
---|
| 370 | m_bitsLeft -= binsToCode; |
---|
| 371 | |
---|
| 372 | testAndWriteOut(); |
---|
| 373 | } |
---|
| 374 | } |
---|
| 375 | |
---|
[56] | 376 | /** |
---|
| 377 | * \brief Encode terminating bin |
---|
| 378 | * |
---|
| 379 | * \param binValue bin value |
---|
| 380 | */ |
---|
| 381 | Void TEncBinCABAC::encodeBinTrm( UInt binValue ) |
---|
| 382 | { |
---|
| 383 | m_uiBinsCoded += m_binCountIncrement; |
---|
| 384 | m_uiRange -= 2; |
---|
| 385 | if( binValue ) |
---|
| 386 | { |
---|
| 387 | m_uiLow += m_uiRange; |
---|
| 388 | m_uiLow <<= 7; |
---|
| 389 | m_uiRange = 2 << 7; |
---|
| 390 | m_bitsLeft -= 7; |
---|
| 391 | } |
---|
| 392 | else if ( m_uiRange >= 256 ) |
---|
| 393 | { |
---|
| 394 | return; |
---|
| 395 | } |
---|
[2] | 396 | else |
---|
| 397 | { |
---|
[56] | 398 | m_uiLow <<= 1; |
---|
| 399 | m_uiRange <<= 1; |
---|
[1200] | 400 | m_bitsLeft--; |
---|
[2] | 401 | } |
---|
[1200] | 402 | |
---|
[56] | 403 | testAndWriteOut(); |
---|
[2] | 404 | } |
---|
| 405 | |
---|
[56] | 406 | Void TEncBinCABAC::testAndWriteOut() |
---|
[2] | 407 | { |
---|
[56] | 408 | if ( m_bitsLeft < 12 ) |
---|
[2] | 409 | { |
---|
[56] | 410 | writeOut(); |
---|
[2] | 411 | } |
---|
[56] | 412 | } |
---|
| 413 | |
---|
| 414 | /** |
---|
| 415 | * \brief Move bits from register into bitstream |
---|
| 416 | */ |
---|
| 417 | Void TEncBinCABAC::writeOut() |
---|
| 418 | { |
---|
| 419 | UInt leadByte = m_uiLow >> (24 - m_bitsLeft); |
---|
| 420 | m_bitsLeft += 8; |
---|
| 421 | m_uiLow &= 0xffffffffu >> m_bitsLeft; |
---|
[1200] | 422 | |
---|
[56] | 423 | if ( leadByte == 0xff ) |
---|
[2] | 424 | { |
---|
[56] | 425 | m_numBufferedBytes++; |
---|
[2] | 426 | } |
---|
[56] | 427 | else |
---|
[2] | 428 | { |
---|
[56] | 429 | if ( m_numBufferedBytes > 0 ) |
---|
[2] | 430 | { |
---|
[56] | 431 | UInt carry = leadByte >> 8; |
---|
| 432 | UInt byte = m_bufferedByte + carry; |
---|
| 433 | m_bufferedByte = leadByte & 0xff; |
---|
| 434 | m_pcTComBitIf->write( byte, 8 ); |
---|
[1200] | 435 | |
---|
[56] | 436 | byte = ( 0xff + carry ) & 0xff; |
---|
| 437 | while ( m_numBufferedBytes > 1 ) |
---|
| 438 | { |
---|
| 439 | m_pcTComBitIf->write( byte, 8 ); |
---|
| 440 | m_numBufferedBytes--; |
---|
| 441 | } |
---|
[2] | 442 | } |
---|
| 443 | else |
---|
| 444 | { |
---|
[56] | 445 | m_numBufferedBytes = 1; |
---|
| 446 | m_bufferedByte = leadByte; |
---|
[1200] | 447 | } |
---|
| 448 | } |
---|
[2] | 449 | } |
---|
| 450 | |
---|
[56] | 451 | //! \} |
---|