📄 ac_vld_decode_c.c
字号:
/* ======================================================================== *//* TEXAS INSTRUMENTS, INC. *//* *//* NAME *//* ac_vld_decode *//* *//* USAGE *//* This routine is C-callable and can be called as: *//* *//* unsigned char ac_vld_decode *//* ( *//* const unsigned char **data_ptr, *//* unsigned int *data_word, *//* short *run_level, *//* unsigned char *num_of_rles, *//* unsigned char *bit_position, *//* unsigned int *used_up_bytes, *//* const AC_TBL_str *ac_table *//* ) *//* *//* data_ptr: Points to the next byte of VLD buffer. *//* data_word: Pointer to word containing the first 32 bits of *//* bitstream. *//* run_level: Buffer where run/level pairs will be written *//* num_of_rles: Location to write number of run/level pairs *//* bit_position: Count of bits in "data_word" that are earlier *//* in the bitstream than the bits pointed to by *//* "data_ptr" *//* used_up_bytes: Location to write number of bytes consumed. *//* ac_table: Structure containing pointers to the run, level, *//* and size tables, as well as the "eob_flag". *//* (The "eob_flag" is ignored.) *//* *//*C #ifndef AC_TBL_STRUCT_ C*//*C #define AC_TBL_STRUCT_ 1 C*//*C C*//*C typedef struct AC_TBL_str C*//*C { C*//*C unsigned char eob_flag; C*//*C const unsigned char *run_ptr; C*//*C const unsigned char *sze_ptr; C*//*C const unsigned char *shf_ptr; C*//*C C*//*C } AC_TBL_str; C*//*C C*//*C #endif C*//* *//* The pointer "data_ptr" points to the first byte in the data *//* stream that is not completely contained within the state word. *//* *//* The variable "bit_position" contains the count of bits that *//* are in the "data_word" but which are before the "data_ptr" in *//* the bitstream. *//* *//* The following diagram attempts to illustrate the relationship *//* between the "data_word", "data_ptr", and "bit_position" *//* arguments to ac_vld_decode(). (Each box is 1 byte / 8 bits.) *//* In this example, we've already consumed 11 bits of the bitstream *//* and are calling ac_vld_decode() to decode the remaining bits. *//* (The 'x' denote bits that have already been consumed from the *//* bitstream.) *//* *//* data_ptr *//* points here *//* | *//* 1 2 3 4 __|__ 4 5 *//* 0 8 6 4 2 0/ \8 6 *//* +-------+-------+-------+-------+-------+-------+-------+ *//* xxxxxxxxxxx | | | | | | *//* +-------+--|----+-------+-------+-------+-|-----+-------+ *//* | | *//* |<-- contained in data_word -->| *//* *//* *//* In this example, "data_word" contains bits 11 through 42 of the *//* bitstream, and "data_ptr" points to the byte containing bits *//* 40 through 47 of the bitstream. In this setting, "bit_position" *//* would contain "29", since three of the bits in "data_word" *//* overlap bits in the byte pointed to by "data_ptr". *//* *//* In all circumstances, "bit_position" must be between 25 and 32 on *//* entry to ac_vld_decode, and all 32 bits of "data_word" must be *//* valid bits from the bitstream. *//* *//* On exit from ac_vld_decode(), the "bit_position", "data_ptr", and *//* "data_word" values are updated to reflect the current position in *//* the bitstream, to allow chaining to other VLD routines that share *//* this interface. *//* *//* DESCRIPTION *//* The encoded AC terms in a JPEG stream are in the following *//* generalized format: *//* *//* 1 1 1 1 0 c c c c c l l l l l *//* *//* That is, zero or more leading 1's, a zero, some additional *//* code-bits (which complete the Huffman code), followed by the *//* non-sign bit portion of the "level" which this symbol encodes. *//* *//* The JPEG VLD lookup tables are ordered according to Huffman *//* codewords so that the left-most zero value can be used effectively *//* as an index into the table. Commonly occuring tables (such as *//* tables K.5 and K.6 in the JPEG standard) have groups of codes *//* which cluster naturally into four separate left-most zero ranges, *//* as shown in the following table: *//* *//* Left-Most Zero Codewords *//* -------------- -------------------------------------- *//* 0 x x x x x *//* 0 thru 2 1 0 x x x x *//* 1 1 0 x x x *//* |<---ofs--->| *//* *//* 1 1 1 0 x x x x x *//* 3 thru 5 1 1 1 1 0 x x x x *//* 1 1 1 1 1 0 x x x *//* |<---ofs--->| *//* *//* 1 1 1 1 1 1 0 x x x x x *//* 6 thru 8 1 1 1 1 1 1 1 0 x x x x *//* 1 1 1 1 1 1 1 1 0 x x x *//* |<----ofs---->| *//* *//* 9 thru 15 1 1 1 1 1 1 1 1 1 x x x x x x x *//* |<-----ofs----->| *//* *//* This grouping lends itself to a lookup table which contains four *//* subtables -- three using a 6-bit offset, and a fourth using a *//* 7-bit offset. Additionally, the 6-bit subtables each have 8 *//* unused entries due to the slight overlap between left-most zero *//* ranges. The ranges marked "ofs" above denote the bits that are *//* extracted for use as a table offset into the lookup table. *//* (An extra leading '1' is extracted for two of the sub-tables, in *//* order to simplify table-offset calculations.) *//* *//* As a result, the VLD lookup tables are laid out like so, for *//* efficient lookups. *//* *//* 0 +-----------------------------------+ *//* | | *//* | 0 <= left-most zero <= 2 | *//* | | *//* 56 +-----------------------------------+ *//* | | *//* | 3 <= left-most zero <= 5 | *//* | | *//* 112 +-----------------------------------+ *//* | -- hole -- | *//* 120 +-----------------------------------+ *//* | | *//* | 6 <= left-most zero <= 8 | *//* | | *//* 176 +-----------------------------------+ *//* | -- hole -- | *//* 184 +-----------------------------------+ *//* | | *//* | | *//* | | *//* | | *//* | 9 <= left-most zero <= 15 | *//* | | *//* | | *//* | | *//* | | *//* 312 +---+-------------------------------+ *//* |pad| *//* 314 +---+ *//* *//* There are three such tables packed together (run, size, and *//* shift) into one super-table. A 2-byte "pad" is placed between *//* these three tables to allow three parallel lookups to proceed *//* without bank conflicts. The total super-table size is 940 bytes. *//* (That is, 312 * 3 + 4 bytes for padding.) *//* *//* To save time/space, the code corresponding to EOB is stored *//* in one of the holes in the table. Specifically, offsets *//* 112 and 113 in the super-table hold the 16-bit EOB code, and *//* offset 116 holds the actual length of the EOB code in bits. *//* (Although 32 bits of space are actually reserved to hold the *//* EOB code, valid JPEG Huffman codes are no longer than 16 bits.) *//* *//* When decoding, the sub-table within the run/size/shift tables *//* is selected based on the left-most zero near the front of the *//* bitstream. (This is found using the _lmbd() instrinsic on the *//* "data_word".) The lmbd value is calculated from the bitstream, *//* and the table offset is computed from the significant bits *//* to the right of the leftmost zero (as shown in the table above). *//* *//* Once calculated, the same offset is used to look up the run, *//* size, and shift values for the Huffman code in parallel. (The *//* shift value is essentially another way of describing the code *//* length). *//* *//* The size and code-length values are used for extracting the *//* encoded "level" from the bitstream. Once extracted, "level" and *//* "run" are stored in an interleaved array of runs/levels for *//* later run-length decoding. *//* *//* IMPLEMENTATION *//* *//* a) Three table pointers are used. They are run_tbl,sze_tbl,shf_tbl *//* run_tbl: points to the end of the run table and will use negative *//* offsets. sze_tbl and shf_tbl point to the start of the size and *//* shift tables and use positive offsets */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -