📄 umc_base_bitstream.cpp
字号:
};
// See table 9-22 of H.264 standard
INITIALIZE_VALUES M_and_N_for_ctxIdx_277_337_inter[61][3] =
{
{{-13, 106}, {-21, 126}, {-22, 127}},
{{-16, 106}, {-23, 124}, {-25, 127}},
{{-10, 87}, {-20, 110}, {-25, 120}},
{{-21, 114}, {-26, 126}, {-27, 127}},
{{-18, 110}, {-25, 124}, {-19, 114}},
{{-14, 98}, {-17, 105}, {-23, 117}},
{{-22, 110}, {-27, 121}, {-25, 118}},
{{-21, 106}, {-27, 117}, {-26, 117}},
{{-18, 103}, {-17, 102}, {-24, 113}},
{{-21, 107}, {-26, 117}, {-28, 118}},
{{-23, 108}, {-27, 116}, {-31, 120}},
{{-26, 112}, {-33, 122}, {-37, 124}},
{{-10, 96}, {-10, 95}, {-10, 94}},
{{-12, 95}, {-14, 100}, {-15, 102}},
{{-5, 91}, {-8, 95}, {-10, 99}},
{{-9, 93}, {-17, 111}, {-13, 106}},
{{-22, 94}, {-28, 114}, {-50, 127}},
{{-5, 86}, {-6, 89}, {-5, 92}},
{{9, 67}, {-2, 80}, {17, 57}},
{{-4, 80}, {-4, 82}, {-5, 86}},
{{-10, 85}, {-9, 85}, {-13, 94}},
{{-1, 70}, {-8, 81}, {-12, 91}},
{{7, 60}, {-1, 72}, {-2, 77}},
{{9, 58}, {5, 64}, {0, 71}},
{{5, 61}, {1, 67}, {-1, 73}},
{{12, 50}, {9, 56}, {4, 64}},
{{15, 50}, {0, 69}, {-7, 81}},
{{18, 49}, {1, 69}, {5, 64}},
{{17, 54}, {7, 69}, {15, 57}},
{{10, 41}, {-7, 69}, {1, 67}},
{{7, 46}, {-6, 67}, {0, 68}},
{{-1, 51}, {-16, 77}, {-10, 67}},
{{7, 49}, {-2, 64}, {1, 68}},
{{8, 52}, {2, 61}, {0, 77}},
{{9, 41}, {-6, 67}, {2, 64}},
{{6, 47}, {-3, 64}, {0, 68}},
{{2, 55}, {2, 57}, {-5, 78}},
{{13, 41}, {-3, 65}, {7, 55}},
{{10, 44}, {-3, 66}, {5, 59}},
{{6, 50}, {0, 62}, {2, 65}},
{{5, 53}, {9, 51}, {14, 54}},
{{13, 49}, {-1, 66}, {15, 44}},
{{4, 63}, {-2, 71}, {5, 60}},
{{6, 64}, {-2, 75}, {2, 70}},
{{-2, 69}, {-1, 70}, {-2, 76}},
{{-2, 59}, {-9, 72}, {-18, 86}},
{{6, 70}, {14, 60}, {12, 70}},
{{10, 44}, {16, 37}, {5, 64}},
{{9, 31}, {0, 47}, {-12, 70}},
{{12, 43}, {18, 35}, {11, 55}},
{{3, 53}, {11, 37}, {5, 56}},
{{14, 34}, {12, 41}, {0, 69}},
{{10, 38}, {10, 41}, {2, 65}},
{{-3, 52}, {2, 48}, {-6, 74}},
{{13, 40}, {12, 41}, {5, 54}},
{{17, 32}, {13, 41}, {7, 54}},
{{7, 44}, {0, 59}, {-6, 76}},
{{7, 38}, {3, 50}, {-11, 82}},
{{13, 50}, {19, 40}, {-2, 77}},
{{10, 57}, {3, 66}, {-2, 77}},
{{26, 43}, {18, 50}, {25, 42}},
};
// See table 9-23 of H.264 standard
INITIALIZE_VALUES M_and_N_for_ctxIdx_338_398_inter[61][3] =
{
{{14, 11}, {19, -6}, {17, -13}},
{{11, 14}, {18, -6}, {16, -9}},
{{9, 11}, {14, 0}, {17, -12}},
{{18, 11}, {26, -12}, {27, -21}},
{{21, 9}, {31, -16}, {37, -30}},
{{23, -2}, {33, -25}, {41, -40}},
{{32, -15}, {33, -22}, {42, -41}},
{{32, -15}, {37, -28}, {48, -47}},
{{34, -21}, {39, -30}, {39, -32}},
{{39, -23}, {42, -30}, {46, -40}},
{{42, -33}, {47, -42}, {52, -51}},
{{41, -31}, {45, -36}, {46, -41}},
{{46, -28}, {49, -34}, {52, -39}},
{{38, -12}, {41, -17}, {43, -19}},
{{21, 29}, {32, 9}, {32, 11}},
{{45, -24}, {69, -71}, {61, -55}},
{{53, -45}, {63, -63}, {56, -46}},
{{48, -26}, {66, -64}, {62, -50}},
{{65, -43}, {77, -74}, {81, -67}},
{{43, -19}, {54, -39}, {45, -20}},
{{39, -10}, {52, -35}, {35, -2}},
{{30, 9}, {41, -10}, {28, 15}},
{{18, 26}, {36, 0}, {34, 1}},
{{20, 27}, {40, -1}, {39, 1}},
{{0, 57}, {30, 14}, {30, 17}},
{{-14, 82}, {28, 26}, {20, 38}},
{{-5, 75}, {23, 37}, {18, 45}},
{{-19, 97}, {12, 55}, {15, 54}},
{{-35, 125}, {11, 65}, {0, 79}},
{{27, 0}, {37, -33}, {36, -16}},
{{28, 0}, {39, -36}, {37, -14}},
{{31, -4}, {40, -37}, {37, -17}},
{{27, 6}, {38, -30}, {32, 1}},
{{34, 8}, {46, -33}, {34, 15}},
{{30, 10}, {42, -30}, {29, 15}},
{{24, 22}, {40, -24}, {24, 25}},
{{33, 19}, {49, -29}, {34, 22}},
{{22, 32}, {38, -12}, {31, 16}},
{{26, 31}, {40, -10}, {35, 18}},
{{21, 41}, {38, -3}, {31, 28}},
{{26, 44}, {46, -5}, {33, 41}},
{{23, 47}, {31, 20}, {36, 28}},
{{16, 65}, {29, 30}, {27, 47}},
{{14, 71}, {25, 44}, {21, 62}},
{{8, 60}, {12, 48}, {18, 31}},
{{6, 63}, {11, 49}, {19, 26}},
{{17, 65}, {26, 45}, {36, 24}},
{{21, 24}, {22, 22}, {24, 23}},
{{23, 20}, {23, 22}, {27, 16}},
{{26, 23}, {27, 21}, {24, 30}},
{{27, 32}, {33, 20}, {31, 29}},
{{28, 23}, {26, 28}, {22, 41}},
{{28, 24}, {30, 24}, {22, 42}},
{{23, 40}, {27, 34}, {16, 60}},
{{24, 32}, {18, 42}, {15, 52}},
{{28, 29}, {25, 39}, {14, 60}},
{{23, 42}, {18, 50}, {3, 78}},
{{19, 57}, {12, 70}, {-16, 123}},
{{22, 53}, {21, 54}, {21, 53}},
{{22, 61}, {14, 71}, {22, 56}},
{{11, 86}, {11, 83}, {25, 61}},
};
INITIALIZE_VALUES M_and_N_for_ctxIdx_399_401_inter[3][3] =
{
{{12, 40}, {25, 32}, {21, 33}},
{{11, 51}, {21, 49}, {19, 50}},
{{14, 59}, {21, 54}, {17, 61}},
};
INITIALIZE_VALUES M_and_N_for_ctxIdx_402_459_inter[58][3] =
{
{{-4, 79}, {-5, 85}, {-3, 78}},
{{-7, 71}, {-6, 81}, {-8, 74}},
{{-5, 69}, {-10,77}, {-9, 72}},
{{-9, 70}, {-7, 81}, {-10,72}},
{{-8 ,66}, {-17,80}, {-18,75}},
{{-10,68}, {-18,73}, {-12,71}},
{{-19,73}, {-4, 74}, {-11,63}},
{{-12,69}, {-10,83}, {-5, 70}},
{{-16,70}, {-9, 71}, {-17,75}},
{{-15,67}, {-9, 67}, {-14,72}},
{{-20,62}, {-1, 61}, {-16,67}},
{{-19,70}, {-8, 66}, {-8, 53}},
{{-16,66}, {-14,66}, {-14,59}},
{{-22,65}, {0, 59}, {-9, 52}},
{{-20,63}, {2, 59}, {-11,68}},
{{9, -2}, {17,-10}, {9, -2}},
{{26, -9}, {32,-13}, {30,-10}},
{{33, -9}, {42, -9}, {31, -4}},
{{39, -7}, {49, -5}, {33, -1}},
{{41, -2}, {53, 0}, {33, 7}},
{{45, 3}, {64, 3}, {31, 12}},
{{49, 9}, {68, 10}, {37, 23}},
{{45, 27}, {66, 27}, {31, 38}},
{{36, 59}, {47, 57}, {20, 64}},
{{-6, 66}, {-5, 71}, {-9, 71}},
{{-7, 35}, {0, 24}, {-7, 37}},
{{-7, 42}, {-1, 36}, {-8, 44}},
{{-8, 45}, {-2, 42}, {-11,49}},
{{-5, 48}, {-2, 52}, {-10,56}},
{{-12,56}, {-9, 57}, {-12,59}},
{{-6, 60}, {-6, 63}, {-8, 63}},
{{-5, 62}, {-4, 65}, {-9, 67}},
{{-8, 66}, {-4, 67}, {-6, 68}},
{{-8, 76}, {-7, 82}, {-10,79}},
{{-5, 85}, {-3, 81}, {-3, 78}},
{{-6, 81}, {-3, 76}, {-8, 74}},
{{-10,77}, {-7, 72}, {-9, 72}},
{{-7, 81}, {-6, 78}, {-10,72}},
{{-17,80}, {-12,72}, {-18,75}},
{{-18,73}, {-14,68}, {-12,71}},
{{-4, 74}, {-3, 70}, {-11,63}},
{{-10,83}, {-6, 76}, {-5, 70}},
{{-9, 71}, {-5, 66}, {-17,75}},
{{-9, 67}, {-5, 62}, {-14,72}},
{{-1, 61}, {0, 57}, {-16,67}},
{{-8, 66}, {-4, 61}, {-8, 53}},
{{-14,66}, {-9, 60}, {-14,59}},
{{0, 59}, {1, 54}, {-9, 52}},
{{2, 59}, {2, 58}, {-11,68}},
{{21,-13}, {17,-10}, {9, -2}},
{{33,-14}, {32,-13}, {30,-10}},
{{39, -7}, {42, -9}, {31, -4}},
{{46, -2}, {49, -5}, {33, -1}},
{{51, 2}, {53, 0}, {33, 7}},
{{60, 6}, {64, 3}, {31, 12}},
{{61, 17}, {68, 10}, {37, 23}},
{{55, 34}, {66, 27}, {31, 38}},
{{42, 62}, {47, 57}, {20, 64}},
};
#ifdef STORE_CABAC_BITS
FILE *cabac_bits;
Ipp32s sym_cnt;
#endif
CBaseBitstream::CBaseBitstream()
{
m_pbs = NULL;
m_pbsBase = NULL;
m_bitOffset = 0;
m_maxBsSize = 0;
}
// ---------------------------------------------------------------------------
// CBaseBitstream::~CBaseBitstream()
// Destructor.
// ---------------------------------------------------------------------------
CBaseBitstream::~CBaseBitstream()
{
} // CBaseBitstream::~CBaseBitstream()
// ---------------------------------------------------------------------------
// CBaseBitstream::CBaseBitstream()
// Constructs a new object. Sets base pointer to point to the given
// bitstream buffer. Neither the encoder or the decoder allocates
// memory for the bitstream buffer.
// pb : pointer to input bitstream
// maxsize : size of bitstream
// ---------------------------------------------------------------------------
CBaseBitstream::CBaseBitstream(Ipp8u* const pb, const Ipp32u maxsize)
{
m_pbsBase = pb;
m_pbs = pb;
m_bitOffset = 0;
m_maxBsSize = maxsize;
} // CBaseBitstream::CBaseBitstream()
// ---------------------------------------------------------------------------
// [ENC] CBaseBitstream::PutBits()
// This is a special debug version that Detects Start Code Emulations.
// Appends bits into the bitstream buffer. Supports only up to 24 bits.
//
// code : code to be inserted into the bitstream
// code_length : length of the given code in number of bits
// ---------------------------------------------------------------------------
void CBaseBitstream::PutBit(Ipp32u code)
{
if (code & 1)
m_pbs[0] = (Ipp8u)(m_pbs[0] | (Ipp8u)(0x01 << (7 - m_bitOffset)));
//else
// m_pbs[0] = (Ipp8u)(m_pbs[0] & (Ipp8u)(0xff << (8 - m_bitOffset)));
m_bitOffset ++;
if (m_bitOffset == 8) {
m_pbs ++;
m_pbs[0] = 0;
m_bitOffset = 0;
}
} // CBaseBitstream::PutBit()
void CBaseBitstream::PutBits(Ipp32u code, Ipp32u length)
{
// make sure that the number of bits given is <= 24
// clear any nonzero bits in upper part of code
VM_ASSERT( length <= 24 );
code <<= (32 - length);
// shift field so that the given code begins at the current bit
// offset in the most significant byte of the 32-bit word
length += m_bitOffset;
code >>= m_bitOffset;
// write bytes back into memory, big-endian
m_pbs[0] = (Ipp8u) ((code >> 24) | m_pbs[0]);
m_pbs[1] = (Ipp8u) (code >> 16);
//f
m_pbs[2] = 0;
if (length > 16) {
m_pbs[2] = (Ipp8u) (code >> 8);
m_pbs[3] = (Ipp8u) code;
}
// update bitstream pointer and bit offset
m_pbs += (length >> 3);
m_bitOffset = (length & 7);
} // CBaseBitstream::PutBits()
// ---------------------------------------------------------------------------
// [ENC] CBaseBitstream::PutVLCCode()
// Writes one Exp-Golomb code to the bitstream. Automatically calculates
// the required code length. Use only when this can not be implicitly
// known to the calling code, requiring length calculation anyway.
//
// code : code to be inserted into the bitstream
// ---------------------------------------------------------------------------
Ipp32u CBaseBitstream::PutVLCCode(const Ipp32u code)
{
Ipp32s i, NN;
Ipp32u code_length;
NN = code + 1;
i = -1;
while (NN) {
NN >>= 1;
i++;
}
code_length = 1+(i<<1);
//f PutVLCBits(code,code_length);
if (code_length == 1)
PutBit(1);
else {
Ipp32s info_length = (code_length - 1) >> 1;
Ipp32s bits = code + 1 - (1 << info_length);
PutBits(1, info_length + 1);
PutBits(bits, info_length);
}
return code_length;
} // CBaseBitstream::PutVLCCode()
// ---------------------------------------------------------------------------
// [ENC] CBaseBitstream::PutVLCBits()
// Writes one Exp-Golomb code to the bitstream.
// code : code to be inserted into the bitstream
// code_length : length of the given code in number of bits
// ---------------------------------------------------------------------------
void CBaseBitstream::PutVLCBits(const Ipp32u code,
const Ipp32u code_length)
{
Ipp32s info_length, bits;
if (code_length == 1)
{
PutBit(1);
return;
}
info_length = (code_length-1) >> 1;
bits = code+1-(1<<info_length);
PutBits(0,info_length);
PutBit(1);
PutBits(bits, info_length);
} // CBaseBitstream::PutVLCBits()
// ---------------------------------------------------------------------------
// [ENC] CBaseBitstream::WriteTrailingBits()
// Byte-align the buffer.
// ---------------------------------------------------------------------------
void CBaseBitstream::WriteTrailingBits()
{
// Write Stop Bit
PutBit(1);
// No action needed if already byte aligned, i.e. !m_bitOffset
if (m_bitOffset){ // note that prior write operation automatically clears the unused bits in the current byte
m_pbs++;
m_bitOffset = 0;
}
} // CBaseBitstream::WriteTrailingBits()
// ---------------------------------------------------------------------------
// [ENC] CBaseBitstream::ByteAlignWithZeros()
// Add zero bits to byte-align the buffer.
// ---------------------------------------------------------------------------
void CBaseBitstream::ByteAlignWithZeros()
{
// No action needed if already byte aligned, i.e. !m_bitOffset
if (m_bitOffset)
{
// note that prior write operation automatically clears the unused
// bits in the current byte
m_pbs ++;
m_bitOffset = 0;
}
} // CBaseBitstream::ByteAlignWithZeros()
// ---------------------------------------------------------------------------
// [ENC] CBaseBitstream::ByteAlignWithOnes()
// Add "1" bits to byte-align the buffer.
// ---------------------------------------------------------------------------
void CBaseBitstream::ByteAlignWithOnes()
{
// No action needed if already byte aligned, i.e. !m_bitOffset
while (m_bitOffset)
{
PutBits((Ipp32u) -1,8-m_bitOffset);
}
} // CBaseBitstream::ByteAlignWithOnes()
// Standalone (not a CBaseBitstream methods) functions for bitstream decoding,
// potentially with CPU-specific implementations for performance.
/************************* cabac part **************************/
template <class T>
T Clip3(T Min, T Max, T Value)
{
if (Value < Min)
return Min;
else if (Value > Max)
return Max;
else
return Value;
} //T Clip3(T Min, T Max, T Value)
void InitializeContext(CABAC_CONTEXT *lpContext, Ipp8s m, Ipp8s n, Ipp32s SliceQPy
#ifdef STORE_CABAC_BITS
#ifdef CABAC_CONTEXTS_COMP
,Ipp32s ctxIdx
#endif
#endif
)
{
Ipp32s preCtxState;
preCtxState = Clip3(1, 126, ((m * SliceQPy) >> 4) + n);
if (preCtxState <= 63)
{
lpContext->pStateIdx = static_cast<Ipp32s> (63 - preCtxState);
lpContext->valMPS = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -