📄 h263_vld_c.c
字号:
/* ======================================================================== *//* TEXAS INSTRUMENTS, INC. *//* *//* NAME *//* h263_vld *//* *//* USAGE *//* This routine is C-callable and can be called as: *//* *//* int h263_vld *//* ( *//* unsigned int *restrict *restrict bufPtr, *//* int *restrict bitPtr, *//* const unsigned short *restrict tab, *//* const unsigned char *restrict len, *//* short Q, *//* int dq, *//* const unsigned char *restrict zz, *//* short *restrict idct, *//* int inter *//* ) *//* *//* *//* bufPtr = pointer to VLD buffer word pointer. *//* bitPtr = pointer to the number of valid bits.n VLD_buffer *//* tab = pointer to VLC Coeff/Run table. *//* len = pointer to VLC Length table. *//* Q = Quantization value. *//* dq = Quant/Mismatch control value. *//* zz = Ptr to zig-zag scan table. *//* idct = 8x8 IDCT Coeff block. (Output) *//* inter = inter/intra 1/0 *//* *//* The function places decoded data in the idct[] array. Also, *//* it returns a two-bit error code as its return value: Bit 0 *//* of the return value denotes whether the zig-zag array was *//* overrun, and bit 1 denotes whether an invalid VLC symbol was *//* encountered. A return value of 0 denotes no error. *//* *//* DESCRIPTION *//* This code performs the variable length decode of a H.263 bit *//* stream using the Table 16 and Table 17 of the H.263 standard. *//* The function accepts a packed Variable length encoded buffer *//* and returns the idct array for one block. This function also *//* returns state variables that allow chaining for calling it *//* multiple times to perform VLD for a macro-block. The function *//* returns 0 if a valid VLD decode of a block was performed and a *//* 1 if the block had an error. *//* *//* The following is the implementation: *//* *//* This routine performs VLD on the AC terms in a Huffman coded 8x8 *//* block for H.263 decode. Inverse Quantization, Run-Length Decode *//* and Zig-Zag reordering are performed on each coefficient before *//* it is written to the IDCT array. The IDCT array is presumed to *//* prezeroed by the calling function. *//* *//* Error detection is performed by detecting invalid Huffman codes *//* and for run-values which send the run-length decode outside the *//* current 8x8 block. No test is made, however, to determine if the *//* bitstream input underflows. The user is advised to place a *//* sentinel bitstream at the end of the VLD buffer to prevent buffer *//* underflow in the case of an error. *//* *//* The bitstream is processed in word-sized chunks, using word-wide *//* loads from the bitstream. Therefore, it is necessary to store the *//* bitstream in "word order". For example, consider the following *//* bitstream: (leftmost bits are earliest in time.) *//* *//* +--------------------------------+---------------------- *//* |ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef|ghijklmnopqrstuvwxyz . . . *//* +--------------------------------+---------------------- *//* Word 0 Word 1 *//* *//* The VLD routine will read the bitstream a word at a time, as *//* labeled above. On a little endian CPU, this implies that the *//* bit-stream will be stored as follows in memory: *//* *//* +--------+--------+--------+--------+----- *//* |YZabcdef|QRSTUVWX|IJKLMNOP|ABCDEFGH| . . . *//* +--------+--------+--------+--------+----- *//* | Byte 0 Byte 1 Byte 2 Byte 3 | *//* |<------------ Word 0 ------------->| *//* *//* The VLC codes fall into several different classes. The VLD *//* attempts to isolate what VLC code it has by looking at various *//* ranges of bits. The following diagram illustrates the various *//* regions of the bitstream that are extracted into different *//* variables. *//* *//* 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 *//* 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 *//* +------------------------------------------------- *//* |A B C D E F G H I J K L M N O P Q R S T U V W . . . *//* +------------------------------------------------- *//* |<--f5--->| | | | | *//* |<----f7----->| | | | *//* |<------f9------->| | | *//* |<-------idx2------>| | *//* |<---------idx1-------->| *//* *//* *//* The Len/Run/Last lookup for VLD then proceeds as follows: *//* *//* -- If 'f5' is less than 4, then we have a code in the range *//* 00000xxxxx to 00011xxxxx. *//* *//* -- For 00000xxxxxxx codes, lookup tab[idx1]. Since f5 *//* is zero (and therefore bits A...E are zero), idx1 has *//* 7 significant bits. Therefore, the lookup occurs over *//* over the range [0,127]. *//* *//* -- For 00001xxxxx thru 00011xxxxx codes, lookup *//* tab[64 + idx0]. For this range of codes, idx0 >= 64, *//* and has 7 significant bits. Therefore, the lookup *//* occurs over the range [128,191]. *//* *//* -- If 'f5' is not less than 4, then we need to look at the *//* first 7 bits to determine what code we have. *//* *//* -- For 001xxxx through 1xxxxxx codes, lookup *//* tab[176 + f7]. We know that f7 >= 32, so therefore, *//* the lookup will occur over the range [198,303]. *//* *//* -- If 'f7' equals 3, we have an escape code of the form *//* 0000011Lrrrrrrllllllll, where 'L' is the LAST flag, *//* 'rrrrrr' is the Run, and 'llllllll' is the signed level. *//* *//* *//* The length of the VLC code is determined with one pair of *//* lookups. *//* *//* -- If 'f5' is less than 4, then we have a code in the range *//* 00000xxxxx to 00011xxxxx. For this case, we look up the *//* length at len[f9]. This lookup occurs over the range *//* [0,63]. *//* *//* -- If 'f5' is >= 4, we use the first 5 bits to look in the *//* length table. We look at len[60 + f5]. Since f5 >= 4, *//* this lookup occurs over the range [64, 91]. *//* *//* The actual code table contains 304 entires to get multiple of 8. *//* Each code word is stored in the table in the following format: *//* *//* Last(1/0) RUNVALUE LEVEL *//* 14 13....8 7....0 *//* *//* The code word used in this context refers to the run, level *//* associated with each Huffman code word that is decoded and not *//* to the Huffman code word itself. *//* *//* Each code word thus has a pad of 2 bits and is stored as a half *//* word *//* *//* Example: *//* *//* 1) Code: 0000 0000 110s Run: 0 Level: 11 *//* *//* Clearly for this code f5 the first 5 bits from the left are zero. *//* first 12 bits from the left are extracted in idx1. If the upper *//* 5 bits are zero then idx1 can have a maximum value of upto 127. *//* In this case this value will be either 13 or 14 depending on the *//* sign bit. *//* 0 000000 001011 -------------> 0x0b *//* *//* *//* The length table is a set of values constructed as follows. The *//* code words are taken and divided into two categories for this *//* purpose: *//* Class 1: Codes 00000xxxxx...00011xxxxx *//* Class 2: Codes 001xxxxx...1xxxxx *//* Therfore it can be seen that under class 2 there can be a set of *//* 32 codes and under class 1 there are 60 enties that the given *//* code table maps into. Therfore there are a total of 32 entries. *//* This can be seen by way of example: *//* *//* Code at Index: 50 of the Table 16 in H.263 spec *//* *//* Run: 19 Level: 1 Code: 0000 1110 1s and falls under Class 1 *//* When the 9 left most bits are examined this gives us 29. The *//* 29th entry in the length table gives us the length of the code *//* word as 10. *//* *//* *//* Similarly a code at Index 40 of the Table 16 in H.263 spec */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -