?? uvlcreader.cpp
字號:
/*
********************************************************************************
NOTE - One of the two copyright statements below may be chosen
that applies for the software.
********************************************************************************
This software module was originally developed by
Heiko Schwarz (Fraunhofer HHI),
Tobias Hinz (Fraunhofer HHI),
Karsten Suehring (Fraunhofer HHI)
in the course of development of the ISO/IEC 14496-10:2005 Amd.1 (Scalable Video
Coding) for reference purposes and its performance may not have been optimized.
This software module is an implementation of one or more tools as specified by
the ISO/IEC 14496-10:2005 Amd.1 (Scalable Video Coding).
Those intending to use this software module in products are advised that its
use may infringe existing patents. ISO/IEC have no liability for use of this
software module or modifications thereof.
Assurance that the originally developed software module can be used
(1) in the ISO/IEC 14496-10:2005 Amd.1 (Scalable Video Coding) once the
ISO/IEC 14496-10:2005 Amd.1 (Scalable Video Coding) has been adopted; and
(2) to develop the ISO/IEC 14496-10:2005 Amd.1 (Scalable Video Coding):
To the extent that Fraunhofer HHI owns patent rights that would be required to
make, use, or sell the originally developed software module or portions thereof
included in the ISO/IEC 14496-10:2005 Amd.1 (Scalable Video Coding) in a
conforming product, Fraunhofer HHI will assure the ISO/IEC that it is willing
to negotiate licenses under reasonable and non-discriminatory terms and
conditions with applicants throughout the world.
Fraunhofer HHI retains full right to modify and use the code for its own
purpose, assign or donate the code to a third party and to inhibit third
parties from using the code for products that do not conform to MPEG-related
ITU Recommendations and/or ISO/IEC International Standards.
This copyright notice must be included in all copies or derivative works.
Copyright (c) ISO/IEC 2005.
********************************************************************************
COPYRIGHT AND WARRANTY INFORMATION
Copyright 2005, International Telecommunications Union, Geneva
The Fraunhofer HHI hereby donate this source code to the ITU, with the following
understanding:
1. Fraunhofer HHI retain the right to do whatever they wish with the
contributed source code, without limit.
2. Fraunhofer HHI retain full patent rights (if any exist) in the technical
content of techniques and algorithms herein.
3. The ITU shall make this code available to anyone, free of license or
royalty fees.
DISCLAIMER OF WARRANTY
These software programs are available to the user without any license fee or
royalty on an "as is" basis. The ITU disclaims any and all warranties, whether
express, implied, or statutory, including any implied warranties of
merchantability or of fitness for a particular purpose. In no event shall the
contributor or the ITU be liable for any incidental, punitive, or consequential
damages of any kind whatsoever arising from the use of these programs.
This disclaimer of warranty extends to the user of these programs and user's
customers, employees, agents, transferees, successors, and assigns.
The ITU does not represent or warrant that the programs furnished hereunder are
free of infringement of any third-party patents. Commercial implementations of
ITU-T Recommendations, including shareware, may be subject to royalty fees to
patent holders. Information regarding the ITU-T patent policy is available from
the ITU Web site at http://www.itu.int.
THIS IS NOT A GRANT OF PATENT RIGHTS - SEE THE ITU-T PATENT POLICY.
********************************************************************************
*/
#include "H264AVCDecoderLib.h"
#include "H264AVCCommonLib/TraceFile.h"
#include "H264AVCCommonLib/Tables.h"
#include "UvlcReader.h"
#include "BitReadBuffer.h"
#include "DecError.h"
H264AVC_NAMESPACE_BEGIN
#define RUNBEFORE_NUM 7
#define MAX_VALUE 0xdead
#define TOTRUN_NUM 15
const UInt g_auiIncVlc[] = {0,3,6,12,24,48,32768}; // maximum vlc = 6
const UChar COEFF_COST[16] =
{
3, 2,2,1, 1,1,0,0,0,0,0,0,0,0,0,0
};
const UChar g_aucLenTableTO4[4][5] =
{
{ 2, 6, 6, 6, 6,},
{ 0, 1, 6, 7, 8,},
{ 0, 0, 3, 7, 8,},
{ 0, 0, 0, 6, 7,},
};
const UChar g_aucCodeTableTO4[4][5] =
{
{1,7,4,3,2},
{0,1,6,3,3},
{0,0,1,2,2},
{0,0,0,5,0},
};
const UChar g_aucLenTableTZ4[3][4] =
{
{ 1, 2, 3, 3,},
{ 1, 2, 2, 0,},
{ 1, 1, 0, 0,},
};
const UChar g_aucCodeTableTZ4[3][4] =
{
{ 1, 1, 1, 0,},
{ 1, 1, 0, 0,},
{ 1, 0, 0, 0,},
};
const UChar g_aucLenTableTZ16[TOTRUN_NUM][16] =
{
{ 1,3,3,4,4,5,5,6,6,7,7,8,8,9,9,9},
{ 3,3,3,3,3,4,4,4,4,5,5,6,6,6,6},
{ 4,3,3,3,4,4,3,3,4,5,5,6,5,6},
{ 5,3,4,4,3,3,3,4,3,4,5,5,5},
{ 4,4,4,3,3,3,3,3,4,5,4,5},
{ 6,5,3,3,3,3,3,3,4,3,6},
{ 6,5,3,3,3,2,3,4,3,6},
{ 6,4,5,3,2,2,3,3,6},
{ 6,6,4,2,2,3,2,5},
{ 5,5,3,2,2,2,4},
{ 4,4,3,3,1,3},
{ 4,4,2,1,3},
{ 3,3,1,2},
{ 2,2,1},
{ 1,1},
};
const UChar g_aucCodeTableTZ16[TOTRUN_NUM][16] =
{
{1,3,2,3,2,3,2,3,2,3,2,3,2,3,2,1},
{7,6,5,4,3,5,4,3,2,3,2,3,2,1,0},
{5,7,6,5,4,3,4,3,2,3,2,1,1,0},
{3,7,5,4,6,5,4,3,3,2,2,1,0},
{5,4,3,7,6,5,4,3,2,1,1,0},
{1,1,7,6,5,4,3,2,1,1,0},
{1,1,5,4,3,3,2,1,1,0},
{1,1,1,3,3,2,2,1,0},
{1,0,1,3,2,1,1,1,},
{1,0,1,3,2,1,1,},
{0,1,1,2,1,3},
{0,1,1,1,1},
{0,1,1,1},
{0,1,1},
{0,1},
};
const UChar g_aucCbpIntra[48] =
{
47, 31, 15, 0,
23, 27, 29, 30,
7, 11, 13, 14,
39, 43, 45, 46,
16, 3, 5, 10,
12, 19, 21, 26,
28, 35, 37, 42,
44, 1, 2, 4,
8, 17, 18, 20,
24, 6, 9, 22,
25, 32, 33, 34,
36, 40, 38, 41
};
const UChar g_aucCbpInter[48] =
{
0, 16, 1, 2,
4, 8, 32, 3,
5, 10, 12, 15,
47, 7, 11, 13,
14, 6, 9, 31,
35, 37, 42, 44,
33, 34, 36, 40,
39, 43, 45, 46,
17, 18, 20, 24,
19, 21, 26, 28,
23, 27, 29, 30,
22, 25, 38, 41
};
const UChar g_aucLenTableTO16[3][4][17] =
{
{ // 0702
{ 1, 6, 8, 9,10,11,13,13,13,14,14,15,15,16,16,16,16},
{ 0, 2, 6, 8, 9,10,11,13,13,14,14,15,15,15,16,16,16},
{ 0, 0, 3, 7, 8, 9,10,11,13,13,14,14,15,15,16,16,16},
{ 0, 0, 0, 5, 6, 7, 8, 9,10,11,13,14,14,15,15,16,16},
},
{
{ 2, 6, 6, 7, 8, 8, 9,11,11,12,12,12,13,13,13,14,14},
{ 0, 2, 5, 6, 6, 7, 8, 9,11,11,12,12,13,13,14,14,14},
{ 0, 0, 3, 6, 6, 7, 8, 9,11,11,12,12,13,13,13,14,14},
{ 0, 0, 0, 4, 4, 5, 6, 6, 7, 9,11,11,12,13,13,13,14},
},
{
{ 4, 6, 6, 6, 7, 7, 7, 7, 8, 8, 9, 9, 9,10,10,10,10},
{ 0, 4, 5, 5, 5, 5, 6, 6, 7, 8, 8, 9, 9, 9,10,10,10},
{ 0, 0, 4, 5, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9,10,10,10},
{ 0, 0, 0, 4, 4, 4, 4, 4, 5, 6, 7, 8, 8, 9,10,10,10},
},
};
const UChar g_aucCodeTableTO16[3][4][17] =
{
{
{ 1, 5, 7, 7, 7, 7,15,11, 8,15,11,15,11,15,11, 7,4},
{ 0, 1, 4, 6, 6, 6, 6,14,10,14,10,14,10, 1,14,10,6},
{ 0, 0, 1, 5, 5, 5, 5, 5,13, 9,13, 9,13, 9,13, 9,5},
{ 0, 0, 0, 3, 3, 4, 4, 4, 4, 4,12,12, 8,12, 8,12,8},
},
{
{ 3,11, 7, 7, 7, 4, 7,15,11,15,11, 8,15,11, 7, 9,7},
{ 0, 2, 7,10, 6, 6, 6, 6,14,10,14,10,14,10,11, 8,6},
{ 0, 0, 3, 9, 5, 5, 5, 5,13, 9,13, 9,13, 9, 6,10,5},
{ 0, 0, 0, 5, 4, 6, 8, 4, 4, 4,12, 8,12,12, 8, 1,4},
},
{
{15,15,11, 8,15,11, 9, 8,15,11,15,11, 8,13, 9, 5,1},
{ 0,14,15,12,10, 8,14,10,14,14,10,14,10, 7,12, 8,4},
{ 0, 0,13,14,11, 9,13, 9,13,10,13, 9,13, 9,11, 7,3},
{ 0, 0, 0,12,11,10, 9, 8,13,12,12,12, 8,12,10, 6,2},
},
};
const UChar g_aucLenTable3[7][15] =
{
{1,1},
{1,2,2},
{2,2,2,2},
{2,2,2,3,3},
{2,2,3,3,3,3},
{2,3,3,3,3,3,3},
{3,3,3,3,3,3,3,4,5,6,7,8,9,10,11},
};
const UChar g_aucCodeTable3[7][15] =
{
{1,0},
{1,1,0},
{3,2,1,0},
{3,2,1,1,0},
{3,2,3,2,1,0},
{3,0,1,3,2,5,4},
{7,6,5,4,3,2,1,1,1,1,1,1,1,1,1},
};
const UInt g_auiISymCode[3][16] =
{ { 0, 1 },
{ 0, 4, 5, 28, 6, 29, 30, 31 },
{ 0, 4, 5, 28, 12, 29, 60,252, 13, 61,124,253,125,254,510,511}
};
const UInt g_auiISymLen[3][16] =
{ { 1, 1 },
{ 1, 3, 3, 5, 3, 5, 5, 5 },
{ 1, 3, 3, 5, 4, 5, 6, 8, 4, 6, 7, 8, 7, 8, 9, 9}
};
const UInt g_auiRefSymCode[2][27] =
{
{
0x1, 0x3, 0x5, 0x3, 0x5, 0x5, 0x4, 0x5, 0x5,
0x2, 0x4, 0x4, 0x3, 0x4, 0x3, 0x4, 0x2, 0x3,
0x3, 0x3, 0x3, 0x3, 0x1, 0x2, 0x2, 0x1, 0x0
},
{
0x1, 0x7, 0x6, 0x7, 0x9, 0x8, 0x6, 0x7, 0x9,
0x5, 0x6, 0x5, 0x8, 0x7, 0x6, 0x7, 0x5, 0x4,
0x4, 0x4, 0x6, 0x5, 0x3, 0x2, 0x5, 0x1, 0x0
}
};
const UInt g_auiRefSymLen[2][27] =
{
{
1, 4, 5, 3, 6, 8, 5, 7, 9,
3, 6, 8, 6, 9,10, 7,10,12,
5, 7, 9, 8,10,12, 9,12,12
},
{
1, 5, 5, 4, 7, 7, 4, 7, 6,
4, 7, 7, 6, 8, 8, 6, 8, 8,
4, 7, 6, 6, 8, 8, 5, 8, 8
}
};
UvlcReader::UvlcReader() :
m_pcBitReadBuffer( NULL ),
m_uiBitCounter( 0 ),
m_uiPosCounter( 0 ),
m_bRunLengthCoding( false ),
m_uiRun( 0 )
{
m_pSymGrp = new UcSymGrpReader( this );
m_uiRefSymCounter = CAVLC_SYMGRP_SIZE;
}
UvlcReader::~UvlcReader()
{
delete m_pSymGrp;
}
__inline ErrVal UvlcReader::xGetCode( UInt& ruiCode, UInt uiLength )
{
DTRACE_TY( " u(v)" );
ErrVal retVal = m_pcBitReadBuffer->get( ruiCode, uiLength );
DTRACE_POS;
DTRACE_CODE (ruiCode);
DTRACE_BITS (ruiCode, uiLength );
DTRACE_COUNT(uiLength);
return retVal;
}
__inline ErrVal UvlcReader::xGetFlag( UInt& ruiCode )
{
DTRACE_TY( " u(1)" );
ErrVal retVal = m_pcBitReadBuffer->get( ruiCode );
DTRACE_POS;
DTRACE_CODE (ruiCode);
DTRACE_BITS (ruiCode, 1 );
DTRACE_COUNT(1);
return retVal;
}
Bool UvlcReader::moreRBSPData()
{
return m_pcBitReadBuffer->isValid();
}
ErrVal UvlcReader::create( UvlcReader*& rpcUvlcReader )
{
rpcUvlcReader = new UvlcReader;
ROT( NULL == rpcUvlcReader );
return Err::m_nOK;
}
ErrVal UvlcReader::destroy()
{
delete this;
return Err::m_nOK;
}
ErrVal UvlcReader::init( BitReadBuffer* pcBitReadBuffer )
{
ROT( NULL == pcBitReadBuffer );
m_pcBitReadBuffer = pcBitReadBuffer;
m_bRunLengthCoding = false;
m_uiRun = 0;
return Err::m_nOK;
}
ErrVal UvlcReader::uninit()
{
m_pcBitReadBuffer = NULL;
return Err::m_nOK;
}
ErrVal UvlcReader::xGetUvlcCode( UInt& ruiVal)
{
UInt uiVal = 0;
UInt uiCode = 0;
UInt uiLength;
DTRACE_DO( m_uiBitCounter = 1 );
DTRACE_TY( "ue(v)" );
DECRNOK( m_pcBitReadBuffer->get( uiCode, 1 ) );
DTRACE_BITS(uiCode, 1);
if( 0 == uiCode )
{
uiLength = 0;
while( ! ( uiCode & 1 ))
{
DECRNOK( m_pcBitReadBuffer->get( uiCode, 1 ) );
DTRACE_BITS(uiCode, 1);
uiLength++;
}
DTRACE_DO( m_uiBitCounter += 2*uiLength );
DECRNOK( m_pcBitReadBuffer->get( uiVal, uiLength ) );
DTRACE_BITS(uiVal, uiLength);
uiVal += (1 << uiLength)-1;
}
ruiVal = uiVal;
DTRACE_POS;
DTRACE_CODE(uiVal);
DTRACE_COUNT(m_uiBitCounter);
return Err::m_nOK;
}
ErrVal UvlcReader::xGetSvlcCode( Int& riVal)
{
UInt uiBits = 0;
DTRACE_DO( m_uiBitCounter = 1 );
DTRACE_TY( "se(v)" );
DECRNOK( m_pcBitReadBuffer->get( uiBits, 1 ) );
DTRACE_BITS(uiBits, 1);
if( 0 == uiBits )
{
UInt uiLength = 0;
while( ! ( uiBits & 1 ))
{
DECRNOK( m_pcBitReadBuffer->get( uiBits, 1 ) );
DTRACE_BITS(uiBits, 1);
uiLength++;
}
DTRACE_DO( m_uiBitCounter += 2*uiLength );
DECRNOK( m_pcBitReadBuffer->get( uiBits, uiLength ) );
DTRACE_BITS(uiBits, uiLength);
uiBits += (1 << uiLength);
riVal = ( uiBits & 1) ? -(Int)(uiBits>>1) : (Int)(uiBits>>1);
}
else
{
riVal = 0;
}
DTRACE_POS;
DTRACE_CODE(riVal);
DTRACE_COUNT(m_uiBitCounter);
return Err::m_nOK;
}
ErrVal UvlcReader::getFlag( Bool& rbFlag, Char* pcTraceString )
{
DTRACE_TH( pcTraceString );
UInt uiCode;
DECRNOK( xGetFlag( uiCode ) );
rbFlag = (1 == uiCode);
DTRACE_N;
return Err::m_nOK;
}
ErrVal UvlcReader::getCode( UInt& ruiCode, UInt uiLength, Char* pcTraceString )
{
DTRACE_TH( pcTraceString );
DTRACE_TY( " u(v)" );
DECRNOK( m_pcBitReadBuffer->get( ruiCode, uiLength ) );
DTRACE_POS;
DTRACE_CODE (ruiCode);
DTRACE_BITS (ruiCode, uiLength );
DTRACE_COUNT(uiLength);
DTRACE_N;
return Err::m_nOK;
}
ErrVal UvlcReader::getUvlc( UInt& ruiCode, Char* pcTraceString )
{
DTRACE_TH( pcTraceString );
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -