📄 vld.c
字号:
* * Function: * Get macroblock type. If MB is 16x16 intra then form cbp. Subblock * mode are also fetched if main mode is 8x8 inter. * * Returns: * VLD_OK for no error and VLD_ERROR for error. * */int vldGetMBtype(bitbuffer_s *bitbuf, vldMBtype_s *hdr, int picType){ unsigned int code; int ac, nc; int i; /* Get macroblock mode */ code = vldGetUVLC(bitbuf); if (IS_SLICE_I(picType)) { /* INTRA macroblock in INTRA slice */ if (code >= VLD_NUM_MB_CODES_INTRA) return VLD_ERROR; hdr->type = MBK_INTRA; } else { if (code >= VLD_NUM_MB_CODES_INTER) return VLD_ERROR; if (code >= 5) { /* INTRA macroblock in INTER slice */ hdr->type = MBK_INTRA; code -= 5; } else { /* INTER macroblock in INTER slice */ hdr->type = MBK_INTER; hdr->interMode = code+1; if (hdr->interMode >= 4) { /* * Get sub-macroblock mode modes */ for (i = 0; i < 4; i++) { code = vldGetUVLC(bitbuf); if (code > VLD_MAX_SUB_MB_MODE) return VLD_ERROR; hdr->inter8x8modes[i] = code; } } if (bibGetStatus(bitbuf) < 0) return VLD_ERROR; return VLD_OK; } } if (bibGetStatus(bitbuf) < 0) return VLD_ERROR; /* * This is INTRA macroblock, find out INTRA type */ if (code == 0) hdr->intraType = MBK_INTRA_TYPE1; else if (code != VLD_NUM_MB_CODES_INTRA-1) { /* 16x16 INTRA - compose cbp value */ hdr->intraType = MBK_INTRA_TYPE2; code -= 1; hdr->intraMode = code % 4; code /= 4; nc = code % 3; ac = code / 3; hdr->cbpY = ac ? 0xffff : 0; setChromaCbp(nc, &hdr->cbpChromaDC, &hdr->cbpC); } else { hdr->intraType = MBK_INTRA_TYPE_PCM; hdr->cbpY = 0xffff; hdr->cbpC = 0xff; hdr->cbpChromaDC = 0x3; } return VLD_OK;}/* * * vldGetIntraPred: * * Parameters: * bitbuf Bitbuffer object * ipTab Return pointer for intra pred. modes * * Function: * Get 4x4 intra prediction modes. First a one bit flag is read. If flag * is 1, mode is predicted from neighborin blocks. If flag is 0, * additional 3 bit mode code is read. Process is repeated for 16 blocks. * * Returns: * VLD_OK for no error and VLD_ERROR for error. * */int vldGetIntraPred(bitbuffer_s *bitbuf, int8 *ipTab){ int i, j, k, l; int bit; int modeCode; for (j = 0; j < BLK_PER_MB; j+=2) { for (i = 0; i < BLK_PER_MB; i+=2) { for (k = 0; k < 2; k++) { for (l = 0; l < 2; l++) { bibGetBit(bitbuf, &bit); /* Read flag */ if (bit == 1) /* If 1, predict from neighbors */ ipTab[(j+k)*4+i+l] = -1; else { /* If 0, read mode code */ bibGetMax8bits(bitbuf, 3, &modeCode); ipTab[(j+k)*4+i+l] = (int8)modeCode; } } } } } /* Check if error occured */ if (bibGetStatus(bitbuf) < 0) return VLD_ERROR; else return VLD_OK;}/* * * vldGetChromaIntraPred: * * Parameters: * bitbuf Bitbuffer object * * Function: * Get chroma intra prediction mode. * * Returns: * Prediction mode. * */int vldGetChromaIntraPred(bitbuffer_s *bitbuf){ unsigned int mode; mode = vldGetUVLC(bitbuf); if (bibGetStatus(bitbuf) < 0 || mode > VLD_MAX_IPR_CHROMA_MODE) return VLD_ERROR; return (int)mode;}/* * * vldGetMotVecs: * * Parameters: * bitbuf Bitbuffer object * interMode Motion mode (16x16, 8x16, ...) * hasRef Flag for multiple ref. frames * refNum Return pointer for ref. indices * predVecs Return pointer for delta vectors * numVecs Number of vectors to read * * Function: * Get reference indices and delta motion vectors for MB * * Returns: * VLD_OK for no error and VLD_ERROR for error * */int vldGetMotVecs(bitbuffer_s *bitbuf, int interMode, int numRefFrames, int *refNum, int predVecs[][2], int numVecs){ int i, j; int code; int refIdx; if (numRefFrames > 1 && interMode < 5) { if (numRefFrames == 2) { for (i = 0; i < numRefIndices[interMode]; i++) { bibGetBit(bitbuf, &refIdx); refNum[i] = 1 - refIdx; } } else { for (i = 0; i < numRefIndices[interMode]; i++) { if ((refNum[i] = (int)vldGetUVLC(bitbuf)) >= numRefFrames) return VLD_ERROR; } } } else refNum[0] = 0; for(j = 0; j < numVecs; j++) { for(i = 0; i < 2; i++) { code = vldGetUVLC(bitbuf); predVecs[j][i] = (code+1)>>1; if ((code & 1) == 0) predVecs[j][i] = -predVecs[j][i]; } } if (bibGetStatus(bitbuf) < 0) return VLD_ERROR; else return VLD_OK;}/* * * vldGetCBP: * * Parameters: * bitbuf Bitbuffer object * mbType Macroblock type (intra/inter) * cbpY Return pointer for luma CBP * cbpChromaDC Return pointer for chroma DC CBP * cbpC Return pointer for chroma CBP * * Function: * Get Coded Block Patterns for both luma and chroma * * Returns: * VLD_OK for no error and VLD_ERROR for error * */int vldGetCBP(bitbuffer_s *bitbuf, int mbType, int *cbpY, int *cbpChromaDC, int *cbpC){ unsigned int code; int cbp; code = vldGetUVLC(bitbuf); if (bibGetStatus(bitbuf) < 0 || code > VLD_MAX_CBP_CODE) return VLD_ERROR; if (mbType == MBK_INTRA) cbp = code2cbp[code][0]; else cbp = code2cbp[code][1]; *cbpY = getLumaBlkCbp(cbp%16); setChromaCbp(cbp>>4, cbpChromaDC, cbpC); return VLD_OK;}/* * * vldGetDeltaqp: * * Parameters: * bitbuf Bitbuffer object * delta_qp Return pointer for delta QP * * Function: * Get macroblock delta QP * * Returns: * VLD_OK for no error and VLD_ERROR for error * */int vldGetDeltaqp(bitbuffer_s *bitbuf, int *delta_qp){ unsigned int code; code = vldGetUVLC(bitbuf); if (bibGetStatus(bitbuf) < 0 || code > VLD_MAX_DELTA_QP_CODE) return VLD_ERROR; *delta_qp = (code+1)>>1; if ((code & 1) == 0) *delta_qp = -(*delta_qp); return VLD_OK;}/* * * getCoefLevelVLC0: * * Parameters: * bitbuf Bitbuffer object * * Function: * Get CAVLC coefficient level for VLC0 code format * * Returns: * Coefficient level * */static int getCoefLevelVLC0(bitbuffer_s *bitbuf){ int numLeadingZeroBits; u_int32 bits; int len; int coef; bibShowMax16bits(bitbuf, 16, &bits); if (bits > 0x0003) { if (bits > 0x00ff) numLeadingZeroBits = numLeadZerosTab[(int)(bits>>(16-7))]; else numLeadingZeroBits = 7 + numLeadZerosTab[(int)(bits>>(16-7-7))]; coef = (numLeadingZeroBits >> 1) + 1; if (numLeadingZeroBits & 1) coef = -coef; len = numLeadingZeroBits + 1; bibSkipBits(bitbuf, len); } else if (bits > 0x0001) { bibGetBits(bitbuf, 19, &bits); coef = 8 + (((int)bits & 15) >> 1); if (bits & 1) coef = -coef; } else if (bits == 0x0001) { bibSkipBits(bitbuf, 16); bibGetMax16bits(bitbuf, 12, &bits); coef = 16 + ((int)bits >> 1); if (bits & 1) coef = -coef; } else { bibRaiseError(bitbuf, BIB_ERR_BIT_ERROR); coef = 0; } return coef;}/* * * getCoefLevelVLCN: * * Parameters: * bitbuf Bitbuffer object * tabNum VLC table number to be used * * Function: * Get CAVLC coefficient level for VLC1-6 code format * * Returns: * Coefficient level * */static int getCoefLevelVLCN(bitbuffer_s *bitbuf, int tabNum){ int numLeadingZeroBits; u_int32 bits; int len; int coef; bibShowBits(bitbuf, 21, &bits); if (bits > 0x00003f) { if (bits > 0x001fff) numLeadingZeroBits = numLeadZerosTab[(int)(bits>>(21-7))]; else numLeadingZeroBits = 7 + numLeadZerosTab[(int)(bits>>(21-7-7))]; len = numLeadingZeroBits + 1 + tabNum; bits = (bits >> (21 - len)) & ((1 << tabNum) - 1); coef = (int)((numLeadingZeroBits << (tabNum - 1)) + (bits >> 1) + 1); if (bits & 1) coef = -coef; bibSkipBits(bitbuf, len); } else if (bits > 0x00001f) { bibSkipBits(bitbuf, 16); bibGetMax16bits(bitbuf, 12, &bits); coef = (15 << (tabNum - 1)) + ((int)bits >> 1) + 1; if (bits & 1) coef = -coef; } else { bibRaiseError(bitbuf, BIB_ERR_BIT_ERROR); coef = 0; } return coef;}/* * * get4x4coefs: * * Parameters: * bitbuf Bitbuffer object * coef Return pointer for 4x4 coefficients * blkIdxX Horizontal block pos. within MB * blkIdxY Vertical block pos. within MB * numCoefUpPred Block coefficients counts of upper block * numCoefLeftPred Block coefficients counts of left block * mbAvailBits Macroblock availability flags * dcSkip if 1, there's no DC coefficient * * Function: * Decode coefficients for 4x4 block. * * Returns: * VLD_OK for no error, VLD_ERROR for error * */static int get4x4coefs(bitbuffer_s *bitbuf, int coef[4][4], int blkIdxX, int blkIdxY, int8 *numCoefUpPred, int8 *numCoefLeftPred, int mbAvailBits, int dcSkip){ int numCoefPred; int tabNum; u_int32 bits; int code; int numTrailingOnes; int numLeadingZeroBits; int totalCoef; int numBits; u_int32 flc; int tmpLevel[16]; int tmpCoef[16]; int level; int numBigCoef; u_int32 signs; int s; int i; const u_int8 *totalZerosPtr; int offset; int totalZeros; int zerosLeft; int run; int coefPos; int coefNum; static const int vlcNumTab[8] = { 0, 0, 1, 1, 2, 2, 2, 2 }; static const unsigned int incVlc[7] = { 0,3,6,12,24,48,32768 /* maximum vlc = 6 */ }; /* * Decode number of coefficients and number of trailing ones */ /* Predict number of coefficients from neighboring blocks */ if (blkIdxX || (mbAvailBits & 1)) { numCoefPred = numCoefLeftPred[0]; if (blkIdxY || (mbAvailBits & 2)) numCoefPred = (numCoefPred + numCoefUpPred[0] + 1) >> 1; } else numCoefPred = (blkIdxY || (mbAvailBits & 2)) ? numCoefUpPred[0] : 0; /* Select variable length code or fixed length decoding */ if (numCoefPred < 8) { /* Use variable length code */ /* Select table number based on the prediction */ tabNum = vlcNumTab[numCoefPred]; bibShowBits(bitbuf, 18, &bits); /* Compute number of leading zeros using look-up table */ if (bits >= 0x00400) numLeadingZeroBits = numLeadZerosTab[(int)(bits>>(18-7))]; else numLeadingZeroBits = 7 + numLeadZerosTab[(int)(bits>>(18-7-7))]; /* Shift excess bits away */ bits >>= 18-4-numLeadingZeroBits; /* numTrailingOnes, totalCoef and length of the codeword are codec in one code */ code = numCoefTrailTab[tabNum][numLeadingZeroBits][(int)bits&7]; /* Total number of bits in codeword is sum of the number of leading zero bits, */ /* 1 one bit and the number of bits in postfix. We know the number of leading zero */ /* bits. Postfix length is extracted from 2 highest bits of code. */ numBits = numLeadingZeroBits + 1 + (code >> 6); bibSkipBits(bitbuf, numBits); if ((code & 0x3f) == 2) { /* All coefficients are zero */ numCoefUpPred[0] = numCoefLeftPred[0] = 0; return VLD_OK; } else { /* 2 lowest bits contains number of trailing ones */ numTrailingOnes = code & 3; /* 4 middle bits contain total number of coefficients minus 1 */ totalCoef = ((code >> 2) & 15) + 1; } } else { /* Use fixed length code */ bibGetMax8bits(bitbuf, 6, &flc); if (flc != 3) { /* 2 lowest bits contains number of trailing ones */ numTrailingOnes = (int)flc & 0x03; /* high bits contain total number of coefficients minus 1 */ totalCoef = ((int)flc >> 2) + 1; } else { /* All coefficients are zero */ numCoefUpPred[0] = numCoefLeftPred[0] = 0; return VLD_OK; } } numCoefUpPred[0] = numCoefLeftPred[0] = (int8) totalCoef; /* * Decode signs for trailing ones and store trailing ones in tmpLevel. */ numBigCoef = totalCoef - numTrailingOnes; if (numTrailingOnes != 0) { /* Get signs for trailing ones. There can be maximum of 3 of them */ bibGetMax8bits(bitbuf, numTrailingOnes, &signs); for (i = numTrailingOnes-1; i >= 0; i--) { s = ((int)signs >> i) & 1; tmpLevel[numBigCoef+i] = 1 - 2*s; } } if (numBigCoef != 0) { /* * Decode first "big" level */ if (totalCoef > 10 && numTrailingOnes < 3) level = getCoefLevelVLCN(bitbuf, 1); else level = getCoefLevelVLC0(bitbuf); if (numTrailingOnes < 3) level += (level > 0) ? 1 : -1; tmpLevel[numBigCoef-1] = level; tabNum = (labs((int32)level) > 3) ? 2 : 1; /* * Decode rest of the "big" levels */ for (i = numBigCoef - 2; i >= 0; i--) { level = getCoefLevelVLCN(bitbuf, tabNum); tmpLevel[i] = level; /* update VLC table number */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -