📄 vld.c
字号:
/*COPYRIGHT, LICENSE AND WARRANTY INFORMATIONThis software module has been originally developed by Nokia Corporation. Provided that a person, entity or a company willing to use the Software (hereinafter Licensee) comply with all the terms and conditions of this Statement and subject to the limitations set forth in this Statement Nokia grants to such Licensee a non-exclusive, sub-licensable, worldwide, limited license under copyrights owned by Nokia to use the Software for the sole purpose of creating, manufacturing, selling, marketing, or distributing (including the right to make modifications to the Software) a fully compliant decoder implementation (hereinafter "Decoder") of ITU-T Recommendation H.264 / ISO/IEC International Standard 14496-10 and an encoder implementation producing output that is decodable with the Decoder.Nokia retains the ownership of copyrights to the Software. There is no patent nor other intellectual property right of Nokia licensed under this Statement (except the copyright license above). Licensee hereby assumes sole responsibility to secure any other intellectual property rights needed, if any. For example, if patent licenses are required, it is their responsibility to acquire the license before utilizing the Software.The license by Nokia is subject to that the Licensee grants to Nokia the non-exclusive, worldwide, royalty-free, perpetual and irrevocable covenant that the Licensee(s) shall not bring a suit before any court or administrative agency or otherwise assert a claim for infringement under the Licensee intellectual property rights that, but for a license, would be infringed by the Software against (a) Nokia or Nokia's Affiliate; or (b) other recipient of a license and covenant not to sue with respect to the Software from Nokia; or (c) contractor, customer or distributor of a party listed above in a or b, which suit or claim is related to the Software or use thereof.The Licensee(s) further agrees to grant a reciprocal license to Nokia (as granted by Nokia to the Licensee(s) on the modifications made by Licensee(s) to the Software. THE SOFTWARE IS PROVIDED "AS IS" AND THE ORIGINAL DEVELOPER DISCLAIMS ANY AND ALL WARRANTIES WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. THOSE INTENDING TO USE THE SOFTWARE ARE EXPRESSLY ADVISED THAT ITS USE MAY INFRINGE EXISTING PATENTS AND BE SUBJECT TO ROYALTY PAYMENTS TO PATENT OWNERS. ANYONE USING THE SOFTWARE ON THE BASIS OF THIS LICENSE AGREES TO OBTAIN THE NECESSARY PERMISSIONS FROM ANY AND ALL APPLICABLE PATENT OWNERS FOR SUCH USE.IN NO EVENT SHALL THE ORIGINAL DEVELOPER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.This copyright, license and warranty information notice must be retained in all copies and derivative works of the Software or substantial portions thereof.*/#include "nccglob.h"#include "globals.h"#include "debug.h"#include "bitbuffer.h"#include "vld.h"#define VLD_MAX_CW_LEN 33#define VLD_MAX_LONG_CW_LEN 65#define VLD_MAX_UVLC_CODE_NUM 65535 /* 2^16-1 */#define VLD_MAX_LONG_UVLC_CODE_NUM 4294967295 /* 2^32-1 */#define VLD_NUM_MB_CODES_INTRA (1+4*3*2+1) /* 1 4x4, 24 16x16, 1 PCM */#define VLD_NUM_MB_CODES_INTER (5+1+4*3*2+1) /* 5 inter, 1 4x4, 24 16x16, 1 PCM */#define VLD_MAX_SUB_MB_MODE 3#define VLD_MAX_IPR_CHROMA_MODE 3#define VLD_MAX_CBP_CODE 47#define VLD_MAX_DELTA_QP_CODE 52/* * Static tables for VLD decoder *//* gives CBP value from codeword number, both for intra and inter */static const int8 code2cbp[VLD_MAX_CBP_CODE+1][2] = { {47, 0},{31,16},{15, 1},{ 0, 2},{23, 4},{27, 8},{29,32},{30, 3},{ 7, 5},{11,10},{13,12},{14,15}, {39,47},{43, 7},{45,11},{46,13},{16,14},{ 3, 6},{ 5, 9},{10,31},{12,35},{19,37},{21,42},{26,44}, {28,33},{35,34},{37,36},{42,40},{44,39},{ 1,43},{ 2,45},{ 4,46},{ 8,17},{17,18},{18,20},{20,24}, {24,19},{ 6,21},{ 9,26},{22,28},{25,23},{32,27},{33,29},{34,30},{36,22},{40,25},{38,38},{41,41} };static const int numRefIndices[5] = { 0, 1, 2, 2, 4};/* Look-up table for determining the number of leading zero bits in a 7-bit bumber */static const int8 numLeadZerosTab[128] = { 7,6,5,5,4,4,4,4,3,3,3,3,3,3,3,3,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};/* * Structure of the code is XXYYYYZZ, where XX is the length of the code * minus (number of leading zero bits plus 1), YYYY is totalCoef-1 and * ZZ is numTrailingOnes. Code xx000010 means that there are no coefficients. * 0 is illegal. */static const u_int8 numCoefTrailTab[3][16][8] = { { { 2, 2, 2, 2, 2, 2, 2, 2}, { 1, 1, 1, 1, 1, 1, 1, 1}, { 6, 6, 6, 6, 6, 6, 6, 6}, {133,133,128,128, 75, 75, 75, 75}, {147,147,138,138, 79, 79, 79, 79}, {151,151,142,142,137,137,132,132}, {155,155,146,146,141,141,136,136}, {159,159,150,150,145,145,140,140}, {163,163,154,154,149,149,144,144}, {220,226,221,216,231,222,217,212}, {239,234,229,228,235,230,225,224}, {247,242,237,236,243,238,233,232}, {255,250,249,244,251,246,245,240}, {188,188,190,190,189,189,184,184}, { 49, 49, 49, 49, 49, 49, 49, 49}, { 0, 0, 0, 0, 0, 0, 0, 0} }, { { 65, 65, 65, 65, 66, 66, 66, 66}, {143,143,139,139, 70, 70, 70, 70}, {215,202,201,192,147,147,133,133}, {155,155,142,142,141,141,132,132}, {159,159,146,146,145,145,136,136}, {144,144,150,150,149,149,140,140}, {163,163,154,154,153,153,148,148}, {235,226,225,220,231,222,221,216}, {232,234,233,228,239,230,229,224}, {247,242,241,240,243,238,237,236}, {249,248,250,245,182,182,180,180}, {191,191,190,190,189,189,188,188}, { 59, 59, 59, 59, 59, 59, 59, 59}, { 0, 0, 0, 0, 0, 0, 0, 0}, { 0, 0, 0, 0, 0, 0, 0, 0}, { 0, 0, 0, 0, 0, 0, 0, 0} }, { {219,215,211,207,203,198,193,194}, {209,210,205,206,201,223,202,197}, {200,218,217,196,227,214,213,192}, {216,212,226,208,231,222,221,204}, {239,234,229,224,235,230,225,220}, {236,242,237,232,243,238,233,228}, {249,244,247,246,245,240,177,177}, {189,189,184,184,187,187,186,186}, {127,127,127,127,126,126,126,126}, { 60, 60, 60, 60, 60, 60, 60, 60}, { 0, 0, 0, 0, 0, 0, 0, 0}, { 0, 0, 0, 0, 0, 0, 0, 0}, { 0, 0, 0, 0, 0, 0, 0, 0}, { 0, 0, 0, 0, 0, 0, 0, 0}, { 0, 0, 0, 0, 0, 0, 0, 0}, { 0, 0, 0, 0, 0, 0, 0, 0} }};/* * The structure of the code is XXXYYYXX where XXX is the length of the code * minus 1, YYY is total number of non-zero coefficients and XX is * the number of trailing ones. */static const u_int8 numCoefTrailTabChroma[8][4] = { { 5, 5, 5, 5}, { 32, 32, 32, 32}, { 74, 74, 74, 74}, {168,175,169,164}, {176,176,172,172}, {206,206,205,205}, {242,242,241,241}, {211,211,211,211}};/* Offsets for 15 Huffman tables in totalZerosTab */static const int totalZerosTabOffset[15] = { 0, 40, 72, 104, 136, 168, 184, 200, 216, 232, 248, 264, 280, 288, 296};/* * The meaning of the code in the table is the following: * If (code > 0xc0) then (code - 0xc0) is an offset to the next position * in the Huffman tree * Otherwise, code structure is XXXXYYYY where XXXX is the length of the code * and YYYY is the number of zero coefficients. */static const u_int8 totalZerosTab[304] = { 0xC8, 0xD0, 0x32, 0x31, 0x10, 0x10, 0x10, 0x10, /* totalCoef==1 */ 0xD8, 0xE0, 0x68, 0x67, 0x56, 0x56, 0x55, 0x55, /* prefix 000 */ 0x44, 0x44, 0x44, 0x44, 0x43, 0x43, 0x43, 0x43, /* prefix 001 */ 0x00, 0x9f, 0x9e, 0x9d, 0x8c, 0x8c, 0x8b, 0x8b, /* prefix 000000 */ 0x7a, 0x7a, 0x7a, 0x7a, 0x79, 0x79, 0x79, 0x79, /* prefix 000001 */ 0xC8, 0xD0, 0xD8, 0x34, 0x33, 0x32, 0x31, 0x30, /* totalCoef==2 */ 0x6e, 0x6d, 0x6c, 0x6b, 0x5a, 0x5a, 0x59, 0x59, /* prefix 000 */ 0x48, 0x48, 0x48, 0x48, 0x47, 0x47, 0x47, 0x47, /* prefix 001 */ 0x46, 0x46, 0x46, 0x46, 0x45, 0x45, 0x45, 0x45, /* prefix 010 */ 0xC8, 0xD0, 0xD8, 0x37, 0x36, 0x33, 0x32, 0x31, /* totalCoef==3 */ 0x6d, 0x6b, 0x5c, 0x5c, 0x5a, 0x5a, 0x59, 0x59, /* prefix 000 */ 0x48, 0x48, 0x48, 0x48, 0x45, 0x45, 0x45, 0x45, /* prefix 001 */ 0x44, 0x44, 0x44, 0x44, 0x40, 0x40, 0x40, 0x40, /* prefix 010 */ 0xC8, 0xD0, 0xD8, 0x38, 0x36, 0x35, 0x34, 0x31, /* totalCoef==4 */ 0x5c, 0x5c, 0x5b, 0x5b, 0x5a, 0x5a, 0x50, 0x50, /* prefix 000 */ 0x49, 0x49, 0x49, 0x49, 0x47, 0x47, 0x47, 0x47, /* prefix 001 */ 0x43, 0x43, 0x43, 0x43, 0x42, 0x42, 0x42, 0x42, /* prefix 010 */ 0xC8, 0xD0, 0xD8, 0x37, 0x36, 0x35, 0x34, 0x33, /* totalCoef==5 */ 0x5b, 0x5b, 0x59, 0x59, 0x4a, 0x4a, 0x4a, 0x4a, /* prefix 000 */ 0x48, 0x48, 0x48, 0x48, 0x42, 0x42, 0x42, 0x42, /* prefix 001 */ 0x41, 0x41, 0x41, 0x41, 0x40, 0x40, 0x40, 0x40, /* prefix 010 */ 0xC8, 0x39, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, /* totalCoef==6 */ 0x6a, 0x60, 0x51, 0x51, 0x48, 0x48, 0x48, 0x48, /* prefix 000 */ 0xC8, 0x38, 0x36, 0x34, 0x33, 0x32, 0x25, 0x25, /* totalCoef==7 */ 0x69, 0x60, 0x51, 0x51, 0x47, 0x47, 0x47, 0x47, /* prefix 000 */ 0xC8, 0x37, 0x36, 0x33, 0x25, 0x25, 0x24, 0x24, /* totalCoef==8 */ 0x68, 0x60, 0x52, 0x52, 0x41, 0x41, 0x41, 0x41, /* prefix 000 */ 0xC8, 0x35, 0x26, 0x26, 0x24, 0x24, 0x23, 0x23, /* totalCoef==9 */ 0x61, 0x60, 0x57, 0x57, 0x42, 0x42, 0x42, 0x42, /* prefix 000 */ 0xC8, 0x32, 0x25, 0x25, 0x24, 0x24, 0x23, 0x23, /* totalCoef==10 */ 0x51, 0x51, 0x50, 0x50, 0x46, 0x46, 0x46, 0x46, /* prefix 000 */ 0xC8, 0x32, 0x33, 0x35, 0x14, 0x14, 0x14, 0x14, /* totalCoef==11 */ 0x40, 0x40, 0x40, 0x40, 0x41, 0x41, 0x41, 0x41, /* prefix 000 */ 0xC8, 0x34, 0x22, 0x22, 0x13, 0x13, 0x13, 0x13, /* totalCoef==12 */ 0x40, 0x40, 0x40, 0x40, 0x41, 0x41, 0x41, 0x41, /* prefix 000 */ 0x30, 0x31, 0x23, 0x23, 0x12, 0x12, 0x12, 0x12, /* totalCoef==13 */ 0x20, 0x20, 0x21, 0x21, 0x12, 0x12, 0x12, 0x12, /* totalCoef==14 */ 0x10, 0x10, 0x10, 0x10, 0x11, 0x11, 0x11, 0x11, /* totalCoef==15 */};/* * The structure of the code is XXXXYYYY where XXXX is the length of the code * and YYYY is the number of zero coefficients. */static const u_int8 totalZerosTabChroma[3][8] = { {0x33, 0x32, 0x21, 0x21, 0x10, 0x10, 0x10, 0x10}, {0x22, 0x22, 0x21, 0x21, 0x10, 0x10, 0x10, 0x10}, {0x11, 0x11, 0x11, 0x11, 0x10, 0x10, 0x10, 0x10}};/* * The structure of the code is XXXXYYYY where XXXX is the length of the code * and YYYY is run length. */static const u_int8 runBeforeTab[7][8] = { {0x11, 0x11, 0x11, 0x11, 0x10, 0x10, 0x10, 0x10}, {0x22, 0x22, 0x21, 0x21, 0x10, 0x10, 0x10, 0x10}, {0x23, 0x23, 0x22, 0x22, 0x21, 0x21, 0x20, 0x20}, {0x34, 0x33, 0x22, 0x22, 0x21, 0x21, 0x20, 0x20}, {0x35, 0x34, 0x33, 0x32, 0x21, 0x21, 0x20, 0x20}, {0x31, 0x32, 0x34, 0x33, 0x36, 0x35, 0x20, 0x20}, {0x00, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0x30},};/* * Static function prototypes */static int getCoefLevelVLC0(bitbuffer_s *bitbuf);static int getCoefLevelVLCN(bitbuffer_s *bitbuf, int tabNum);static int get4x4coefs(bitbuffer_s *bitbuf, int coef[4][4], int blkIdxX, int blkIdxY, int8 *numCoefUpPred, int8 *numCoefLeftPred, int mbAvailbits, int dcSkip);static int get2x2coefsCDC(bitbuffer_s *bitbuf, int coef[4], int *numCoef);/* * Functions begin here *//* * * vldInvZigZagScan4x4: * * Parameters: * src Zigzag scanned coefficients * dst Coefficients non-zigzag scanned order * * Function: * Put coefficients in non-zigzag scanned order. * * Returns: * - * */void vldInvZigZagScan4x4(int *src, int dst[BLK_SIZE][BLK_SIZE]){ dst[0][0]=src[0], dst[0][1]=src[1], dst[1][0]=src[2], dst[2][0]=src[3]; dst[1][1]=src[4], dst[0][2]=src[5], dst[0][3]=src[6], dst[1][2]=src[7]; dst[2][1]=src[8], dst[3][0]=src[9], dst[3][1]=src[10], dst[2][2]=src[11]; dst[1][3]=src[12], dst[2][3]=src[13], dst[3][2]=src[14], dst[3][3]=src[15];}/* * * vldGetUVLC: * * Parameters: * bitbuf Bitbuffer object * * Function: * Decode an UVLC codeword, UVLC codeword is of the form: * code codeword * 0 1 * 1 010 * 2 011 * 3 00100 * 4 00101 * 5 00110 * 6 00111 * 7 0001000 * 8 0001001 * ... ... * * Returns: * Codenumber in the range [0, 65535] * */unsigned int vldGetUVLC(bitbuffer_s *bitbuf){ u_int32 c; int prefixLen; int bit; bibGetBit(bitbuf, &bit); if (bit == 1) return 0; prefixLen = 0; do { bibGetBit(bitbuf, &bit); prefixLen += 1; /* Maximum prefix length is VLD_MAX_CW_LEN/2 */ if (prefixLen == VLD_MAX_CW_LEN/2) { bibGetMax16bits(bitbuf, VLD_MAX_CW_LEN/2, &c); if (bit != 1 || c != 0) { /* We encoutered overlong codeword or we encoutered too big code */ bibRaiseError(bitbuf, BIB_ERR_BIT_ERROR); return 0; } else return VLD_MAX_UVLC_CODE_NUM; /* Otherwise, return maximum 16-bit code number */ } } while (bit == 0); bibGetMax16bits(bitbuf, prefixLen, &c); return (unsigned int)(c + (1<<prefixLen)-1);}/* * * vldGetUVLClong: * * Parameters: * bitbuf Bitbuffer object * code Return pointer for code * * Function: * Decode long UVLC codeword. * * Returns: * Codenumber in the range [0, 2^32-1] * */u_int32 vldGetUVLClong(bitbuffer_s *bitbuf){ u_int32 c, cLo; int prefixLen, len0; int bits; prefixLen = -1; do { bibGetBit(bitbuf, &bits); prefixLen += 1; /* Is codeword too long? */ if (prefixLen > VLD_MAX_LONG_CW_LEN/2) { bibRaiseError(bitbuf, BIB_ERR_BIT_ERROR); return 0; } } while (bits == 0); len0 = min(24, prefixLen); bibGetBits(bitbuf, len0, &c); if (prefixLen > 24) { /* We have to read bits in two pieces because bibGetBits can only fetch */ /* max. 24 bits */ bibGetMax16bits(bitbuf, prefixLen-24, &cLo); c = (c << (prefixLen-24)) | cLo; /* Combine two pieces */ if (prefixLen == VLD_MAX_LONG_CW_LEN/2) { /* Is codeword too big? */ if (c != 0) { bibRaiseError(bitbuf, BIB_ERR_BIT_ERROR); return 0; } else return VLD_MAX_LONG_UVLC_CODE_NUM; /* Otherwise, return maximum 32-bit code number */ } } return (c + (1<<prefixLen)-1);}/* * * vldGetSignedUVLC: * * Parameters: * bitbuf Bitbuffer object * * Function: * Decode an UVLC codeword and produce signed integer. * * Returns: * Code is in the range [-32768,32767]. * */int vldGetSignedUVLC(bitbuffer_s *bitbuf){ unsigned int codeNum; int code; codeNum = vldGetUVLC(bitbuf); /* Decode magnitude */ code = (int)(((int32)codeNum+1)>>1); /* Decode sign */ if ((codeNum & 1) == 0) code = -code; return code;}/* * * vldGetSignedUVLClong: * * Parameters: * bitbuf Bitbuffer object * * Function: * Decode an UVLC codeword and produce signed 32-bit integer * * Returns: * Code in the range [-2^31, 2^31-1] * */int32 vldGetSignedUVLClong(bitbuffer_s *bitbuf){ u_int32 codeNum; int32 code; codeNum = vldGetUVLClong(bitbuf); /* Decode magnitude */ code = (int32)((codeNum >> 1) + (codeNum & 1)); /* Decode sign */ if ((codeNum & 1) == 0) code = -code; return code;}/* * * vldGetFLC: * * Parameters: * bitbuf Bitbuffer object * len Length of the code * * Function: * Get Fixed Length Code (max length 16 bits). * * Returns: * Code * */unsigned int vldGetFLC(bitbuffer_s *bitbuf, int len){ u_int32 code; bibGetMax16bits(bitbuf, len, &code); return (unsigned int)code;}/* * * setChromaCbp: * * Parameters: * bitbuf Bitbuffer object * picType Return pointer for picture type * * Function: * Set chroma DC and AC CBP values * nc = 0: No coefficients * nc = 1: Only nonzero DC coefficients * nc = 2: Nonzero AC and DC coefficients * * Returns: * - */void setChromaCbp(int nc, int *cbpDC, int *cbp){ if (nc == 0) *cbpDC = *cbp = 0; else if (nc == 1) *cbpDC = 3, *cbp = 0; else *cbpDC = 3, *cbp = (1<<2*BLK_PER_MB/2*BLK_PER_MB/2)-1;}/* * * getLumaBlkCbp: * * Parameters: * cbpY CBP for 8x8 blocks * * Function: * Convert Codec Block Pattern of 8x8 blocks to Codec Block Pattern for * 4x4 blocks. If 8x8 block has nonzero coefficients then all 4x4 blocks * within that 8x8 block are marked nonzero. * * Returns: * CBP for 4x4 blocks * */int getLumaBlkCbp(int cbpY){ int cbp; cbp = (cbpY & (1<<0)) == 0 ? 0 : 0x0033; cbp |= (cbpY & (1<<1)) == 0 ? 0 : 0x00cc; cbp |= (cbpY & (1<<2)) == 0 ? 0 : 0x3300; cbp |= (cbpY & (1<<3)) == 0 ? 0 : 0xcc00; return cbp;}/* * * vldGetRunIndicator: * * Parameters: * bitbuf Bitbuffer object * * Function: * Get the amount of copy macroblocks. * * Returns: * The number of copy MBs * */unsigned int vldGetRunIndicator(bitbuffer_s *bitbuf){ return vldGetUVLC(bitbuf);}/* * * vldGetMBtype: * * Parameters: * bitbuf Bitbuffer object * hdr Return pointer for MB params * picType Type of current picture (intra/inter)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -