[324] | 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 TEncBinCoderCABAC.cpp |
---|
| 35 | \brief binary entropy encoder of CABAC |
---|
| 36 | */ |
---|
| 37 | |
---|
| 38 | #include "TEncBinCoderCABAC.h" |
---|
| 39 | #include "TLibCommon/TComRom.h" |
---|
| 40 | |
---|
| 41 | |
---|
| 42 | //! \ingroup TLibEncoder |
---|
| 43 | //! \{ |
---|
| 44 | |
---|
| 45 | |
---|
| 46 | TEncBinCABAC::TEncBinCABAC() |
---|
| 47 | : m_pcTComBitIf( 0 ) |
---|
| 48 | , m_binCountIncrement( 0 ) |
---|
| 49 | #if FAST_BIT_EST |
---|
| 50 | , m_fracBits( 0 ) |
---|
| 51 | #endif |
---|
| 52 | { |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | TEncBinCABAC::~TEncBinCABAC() |
---|
| 56 | { |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | Void TEncBinCABAC::init( TComBitIf* pcTComBitIf ) |
---|
| 60 | { |
---|
| 61 | m_pcTComBitIf = pcTComBitIf; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | Void TEncBinCABAC::uninit() |
---|
| 65 | { |
---|
| 66 | m_pcTComBitIf = 0; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | Void TEncBinCABAC::start() |
---|
| 70 | { |
---|
| 71 | m_uiLow = 0; |
---|
| 72 | m_uiRange = 510; |
---|
| 73 | m_bitsLeft = 23; |
---|
| 74 | m_numBufferedBytes = 0; |
---|
| 75 | m_bufferedByte = 0xff; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | Void TEncBinCABAC::finish() |
---|
| 79 | { |
---|
| 80 | if ( m_uiLow >> ( 32 - m_bitsLeft ) ) |
---|
| 81 | { |
---|
| 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 ); |
---|
| 91 | } |
---|
| 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 ); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | Void TEncBinCABAC::flush() |
---|
| 108 | { |
---|
| 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 | { |
---|
| 130 | finish(); |
---|
| 131 | m_pcTComBitIf->write(1, 1); |
---|
| 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 | { |
---|
| 147 | TEncBinCABAC* pcTEncBinCABAC = pcTEncBinIf->getTEncBinCABAC(); |
---|
| 148 | m_uiLow = pcTEncBinCABAC->m_uiLow; |
---|
| 149 | m_uiRange = pcTEncBinCABAC->m_uiRange; |
---|
| 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 |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | Void TEncBinCABAC::resetBits() |
---|
| 159 | { |
---|
| 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 |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | UInt TEncBinCABAC::getNumWrittenBits() |
---|
| 174 | { |
---|
| 175 | return m_pcTComBitIf->getNumberOfWrittenBits() + 8 * m_numBufferedBytes + 23 - m_bitsLeft; |
---|
| 176 | } |
---|
| 177 | |
---|
| 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 ) |
---|
| 185 | { |
---|
| 186 | { |
---|
[401] | 187 | #if !H_MV |
---|
[324] | 188 | DTRACE_CABAC_VL( g_nSymbolCounter++ ) |
---|
| 189 | DTRACE_CABAC_T( "\tstate=" ) |
---|
| 190 | DTRACE_CABAC_V( ( rcCtxModel.getState() << 1 ) + rcCtxModel.getMps() ) |
---|
| 191 | DTRACE_CABAC_T( "\tsymbol=" ) |
---|
| 192 | DTRACE_CABAC_V( binValue ) |
---|
| 193 | DTRACE_CABAC_T( "\n" ) |
---|
[401] | 194 | #endif |
---|
[324] | 195 | } |
---|
| 196 | m_uiBinsCoded += m_binCountIncrement; |
---|
| 197 | rcCtxModel.setBinsCoded( 1 ); |
---|
| 198 | |
---|
| 199 | UInt uiLPS = TComCABACTables::sm_aucLPSTable[ rcCtxModel.getState() ][ ( m_uiRange >> 6 ) & 3 ]; |
---|
| 200 | m_uiRange -= uiLPS; |
---|
| 201 | |
---|
| 202 | if( binValue != rcCtxModel.getMps() ) |
---|
| 203 | { |
---|
| 204 | Int numBits = TComCABACTables::sm_aucRenormTable[ uiLPS >> 3 ]; |
---|
| 205 | m_uiLow = ( m_uiLow + m_uiRange ) << numBits; |
---|
| 206 | m_uiRange = uiLPS << numBits; |
---|
| 207 | rcCtxModel.updateLPS(); |
---|
| 208 | |
---|
| 209 | m_bitsLeft -= numBits; |
---|
| 210 | } |
---|
| 211 | else |
---|
| 212 | { |
---|
| 213 | rcCtxModel.updateMPS(); |
---|
| 214 | if ( m_uiRange >= 256 ) |
---|
| 215 | { |
---|
| 216 | return; |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | m_uiLow <<= 1; |
---|
| 220 | m_uiRange <<= 1; |
---|
| 221 | m_bitsLeft--; |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | testAndWriteOut(); |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | /** |
---|
| 228 | * \brief Encode equiprobable bin |
---|
| 229 | * |
---|
| 230 | * \param binValue bin value |
---|
| 231 | */ |
---|
| 232 | Void TEncBinCABAC::encodeBinEP( UInt binValue ) |
---|
| 233 | { |
---|
| 234 | { |
---|
[401] | 235 | #if !H_MV |
---|
[324] | 236 | DTRACE_CABAC_VL( g_nSymbolCounter++ ) |
---|
| 237 | DTRACE_CABAC_T( "\tEPsymbol=" ) |
---|
| 238 | DTRACE_CABAC_V( binValue ) |
---|
| 239 | DTRACE_CABAC_T( "\n" ) |
---|
[401] | 240 | #endif |
---|
[324] | 241 | } |
---|
| 242 | m_uiBinsCoded += m_binCountIncrement; |
---|
| 243 | m_uiLow <<= 1; |
---|
| 244 | if( binValue ) |
---|
| 245 | { |
---|
| 246 | m_uiLow += m_uiRange; |
---|
| 247 | } |
---|
| 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++ ) |
---|
| 264 | { |
---|
[401] | 265 | #if !H_MV |
---|
[324] | 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" ) |
---|
[401] | 270 | #endif |
---|
[324] | 271 | } |
---|
| 272 | |
---|
| 273 | while ( numBins > 8 ) |
---|
| 274 | { |
---|
| 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(); |
---|
| 283 | } |
---|
| 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 | } |
---|
| 312 | else |
---|
| 313 | { |
---|
| 314 | m_uiLow <<= 1; |
---|
| 315 | m_uiRange <<= 1; |
---|
| 316 | m_bitsLeft--; |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | testAndWriteOut(); |
---|
| 320 | } |
---|
| 321 | |
---|
| 322 | Void TEncBinCABAC::testAndWriteOut() |
---|
| 323 | { |
---|
| 324 | if ( m_bitsLeft < 12 ) |
---|
| 325 | { |
---|
| 326 | writeOut(); |
---|
| 327 | } |
---|
| 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 ) |
---|
| 340 | { |
---|
| 341 | m_numBufferedBytes++; |
---|
| 342 | } |
---|
| 343 | else |
---|
| 344 | { |
---|
| 345 | if ( m_numBufferedBytes > 0 ) |
---|
| 346 | { |
---|
| 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 | } |
---|
| 358 | } |
---|
| 359 | else |
---|
| 360 | { |
---|
| 361 | m_numBufferedBytes = 1; |
---|
| 362 | m_bufferedByte = leadByte; |
---|
| 363 | } |
---|
| 364 | } |
---|
| 365 | } |
---|
| 366 | |
---|
| 367 | //! \} |
---|