?? vlcutility.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 "vlcutility.h"/* * Table for number of bits for the smallest codes */const int8 uvlcBitsUnsigned[] = { 1, 3, 3, 5, 5, 5, 5, 7, 7, 7, 7, 7, 7, 7, 7, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 11, 11,11,11,11,11,11,11,11, 11,11,11,11,11,11,11,11, 11,11,11,11,11,11,11,11, 11,11,11,11,11,11,11,13,};/* * Number of bits for motion vectors. Range goes from -511 to 511 */const int8 uvlcBitsSigned[511 + 1 + 511] = { 19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19, 19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19, 19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19, 19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19, 19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19, 19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19, 19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19, 19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19, 19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19, 19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19, 19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19, 19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19, 19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19, 19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19, 19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19, 19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19, 17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17, 17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17, 17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17, 17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17, 17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17, 17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17, 17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17, 17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17, 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15, 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15, 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15, 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15, 13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13, 13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13, 11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11, 9, 9, 9, 9, 9, 9, 9, 9, 7, 7, 7, 7, 5, 5, 3, 1, 3, 5, 5, 7, 7, 7, 7, 9, 9, 9, 9, 9, 9, 9, 9, 11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11, 13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13, 13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13, 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15, 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15, 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15, 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15, 17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17, 17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17, 17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17, 17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17, 17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17, 17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17, 17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17, 17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17, 19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19, 19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19, 19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19, 19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19, 19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19, 19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19, 19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19, 19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19, 19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19, 19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19, 19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19, 19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19, 19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19, 19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19, 19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19, 19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19};const int8 numOfRefPar[7] = { 1, 2, 2, 4, 4, 4, 4};/* * vlcuZigZagScan4x4 * * Parameters: * src Source block * dst Zig zag scanned values * * Function: * Perform zig zag scanning for a block of values. * * Returns: * - * */void vlcuZigZagScan4x4(int src[BLK_SIZE][BLK_SIZE], int *dst){ dst[0] =src[0][0], dst[1] =src[0][1], dst[2] =src[1][0], dst[3] =src[2][0]; dst[4] =src[1][1], dst[5] =src[0][2], dst[6] =src[0][3], dst[7] =src[1][2]; dst[8] =src[2][1], dst[9] =src[3][0], dst[10]=src[3][1], dst[11]=src[2][2]; dst[12]=src[1][3], dst[13]=src[2][3], dst[14]=src[3][2], dst[15]=src[3][3];}/* * vlcuUVLCSize: * * Parameters: * codeNum Code number to be sent * * Function: * Calculate the length of the Unified VLC representation of an integer. * * Returns: * Length of the codeword. */int vlcuUVLCSize(unsigned int codeNum){ int tmp; int len; int i; tmp = (codeNum + 1) >> 1; /* Find len/2 = i */ for (i = 0; tmp != 0 && i < 16; i++) tmp >>= 1; /* Compute length. Length is always odd number */ len = 2*i + 1; return len;}/* * vlcuSendUVLC: * * Parameters: * bitbuf Bitbuffer to be used * codeNum Code number to be sent * * Function: * Sends Unified VLC. * Code number is first converted to info and length. Info is the info part * of the codeword and length is the length of the final codeword * (not info part). Relationship between code number, info and length is * * codeNum = 2^(len/2) + info - 1 * * Returns: * Length of the codeword. */int vlcuSendUVLC(bitbuffer_s *bitbuf, unsigned int codeNum){ int tmp; int info; int len; int i; tmp = (codeNum+1)>>1; /* Find len/2 = i */ for (i = 0; tmp != 0 && i < 16; i++) tmp >>= 1; /* Compute length. Length is always odd number */ len = 2*i + 1; /* info = codeNum + 1 - 2^(len/2) */ info = codeNum + 1 - (1<<i); bibPutBits(bitbuf, info | (1 << (len >> 1)), len); return len;}/* * vlcuSendFLC: * * Parameters: * bitbuf Bitbuffer to be used * code Code to send * len Number of bits to send * * Function: * Send Fixed LEngth Code. * * Returns: * Length of the codeword that was sent. * */int vlcuSendFLC(bitbuffer_s *bitbuf, unsigned int codeNum, int len){ return bibPutBits(bitbuf, codeNum, len);}/* * vlcuSendNalUnitStartCodePrefix: * * Parameters: * bitbuf Bitbuffer object * zero_byte indicate there will be zero byte before the * start_code_prefix_one_3bytes * * Function: * Write the start code in a byte stream NAL unit * * Returns: * Number of bits being written */int vlcuSendNalUnitStartCodePrefix(bitbuffer_s *bitbuf, int zeroByte){ int bits = 0; // write 1 zero byte if requested if (zeroByte != 0) { bibPutByte( bitbuf, 0 ); bits += 8; } // write the start_code_prefix_one_3bytes 0x00 0x00 0x01 bibPutByte( bitbuf, 0 ); bibPutByte( bitbuf, 0 ); bibPutByte( bitbuf, 1 ); bits += 24; return bits;}/* * vlcuSendNalHead: * * Parameters: * bitbuf Bitbuffer object * nal_ref_idc nal_ref_idc * nal_type Nal type * * Function: * Write the start code in a byte stream NAL unit * * Returns: * Number of bits being written */int vlcuSendNalHead(bitbuffer_s *bitbuf, int nal_ref_idc, int nal_type){ int nalHeaderByte = 0; nalHeaderByte |= 0 << 7; // forbidden bit (1 bit) nalHeaderByte |= nal_ref_idc << 5; // nal_ref_idc (2 bits) nalHeaderByte |= nal_type; // nal_ref_idc (5 bits) bibPutByte(bitbuf, nalHeaderByte); return 8;}/* * vlcuSendEndOfSeq * * Parameters: * bitbuf Bitbuffer to be used * * Function: * Send End Of Sequence codeword. * * Returns: * Number of bits sent. */int vlcuSendEndOfSeq(bitbuffer_s *bitbuf){ return (bibByteAlign(bitbuf));}/* * vlcuGetLuma8x8cbp * * Parameters: * cbpY Coded Block Pattern for 16 4x4 blocks. * Order of the 4x4 cbp bits: * 0 1 4 5 * 2 3 6 7 * 8 9 12 13 * 10 11 14 15 * * Function: * Create Coded Block Pattern for 8x8 blocks. Each 8x8 block consists of * four neighboring 4x4 blocks. * * Returns: * Coded Block Pattern for 8x8 blocks. */int vlcuGetLuma8x8cbp(int cbpY){ int cbp; cbp = (cbpY & 0x000F) == 0 ? 0 : (1<<0); cbp |= (cbpY & 0x00F0) == 0 ? 0 : (1<<1); cbp |= (cbpY & 0x0F00) == 0 ? 0 : (1<<2); cbp |= (cbpY & 0xF000) == 0 ? 0 : (1<<3); return cbp;}/* * vlcuSendCoefLevelVLC0: * * Parameters: * bitbuf Bitbuffer object * coef Coefficient * * Function: * Send CAVLC coefficient level using VLC0 code format * * Returns: * Number of bits send */int vlcuSendCoefLevelVLC0(bitbuffer_s *bitbuf, int coef){ int len; int bits; int absLev; absLev = abs(coef); if (absLev <= 7) { bits = 1; len = 2*absLev - (coef > 0 ? 1 : 0); } else if (absLev <= 15) { bits = 16 | ((absLev-8)<<1) | (coef < 0 ? 1 : 0); len = 19; } else { bits = (1<<12) | ((absLev-16)<<1) | (coef < 0 ? 1 : 0); len = 28; } bibPutBits(bitbuf, bits, len); return len;}/* * vlcuSendCoefLevelVLCN: * * Parameters: * bitbuf Bitbuffer object * tabNum VLC table number to be used * coef Coefficient * * Function: * Send CAVLC coefficient level using VLC1-6 code format * * Returns: * Number of bits sent */int vlcuSendCoefLevelVLCN(bitbuffer_s *bitbuf, int tabNum, int coef){ int len; int bits; int absLev; absLev = abs(coef); if (absLev <= (15<<(tabNum-1))) { bits = (1<<tabNum) | (((absLev-1) & ((1<<(tabNum-1))-1))<<1) | (coef < 0 ? 1 : 0); len = ((absLev-1) >> (tabNum-1)) + 1 + tabNum; } else { bits = (1<<12) + ((absLev-(15<<(tabNum-1))-1)<<1) + (coef < 0 ? 1 : 0); len = 28; } bibPutBits(bitbuf, bits, len); return len;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -