📄 l3bitstream.c
字号:
Some combinations of bitrate, Fs, and stereo make it impossible to stuff out a frame using just main_data, due to the limited number of bits to indicate main_data_length. In these situations, we put stuffing bits into the ancillary data...*/static void drain_into_ancillary_data( int lengthInBits ){ /* */ int wordsToSend = lengthInBits / 32; int remainingBits = lengthInBits % 32; int i; /* userFrameDataPH->part->nrEntries set by call to write_ancillary_data() */ for ( i = 0; i < wordsToSend; i++ ) addBits( userFrameDataPH, 0, 32 ); if ( remainingBits ) addBits( userFrameDataPH, 0, remainingBits ); }/* Note the discussion of huffmancodebits() on pages 28 and 29 of the IS, as well as the definitions of the side information on pages 26 and 27. */static void Huffmancodebits( BitHolder **pph, int *ix, gr_info *gi ){ int L3_huffman_coder_count1( BitHolder **pph, struct huffcodetab *h, int v, int w, int x, int y ); int bigv_bitcount( int ix[576], gr_info *cod_info ); int region1Start; int region2Start; int i, bigvalues, count1End; int v, w, x, y, bits, cbits, xbits, stuffingBits; unsigned int code, ext; struct huffcodetab *h; int bvbits, c1bits, tablezeros, r0, r1, r2, rt, *pr; int bitsWritten = 0; tablezeros = 0; r0 = r1 = r2 = 0; /* 1: Write the bigvalues */ bigvalues = gi->big_values * 2; if ( bigvalues ) { if ( !(gi->mixed_block_flag) && gi->window_switching_flag && (gi->block_type == 2) ) { /* Three short blocks */ /* Within each scalefactor band, data is given for successive time windows, beginning with window 0 and ending with window 2. Within each window, the quantized values are then arranged in order of increasing frequency... */ int sfb, window, line, start, end; I192_3 *ix_s; int *scalefac = &sfBandIndex[fr_ps->header->sampling_frequency].s[0]; ix_s = (I192_3 *) ix; region1Start = 12; region2Start = 576; for ( sfb = 0; sfb < 13; sfb++ ) { unsigned tableindex = 100; start = scalefac[ sfb ]; end = scalefac[ sfb+1 ]; if ( start < region1Start ) tableindex = gi->table_select[ 0 ]; else tableindex = gi->table_select[ 1 ];/* assert( tableindex < 32 ); */ for ( window = 0; window < 3; window++ ) for ( line = start; line < end; line += 2 ) { x = (*ix_s)[line][window]; y = (*ix_s)[line + 1][window];/* assert( idx < 576 ); assert( idx >= 0 ); */ bits = HuffmanCode( tableindex, x, y, &code, &ext, &cbits, &xbits ); addBits( *pph, code, cbits ); addBits( *pph, ext, xbits ); bitsWritten += bits; } } } else if ( gi->mixed_block_flag && gi->block_type == 2 ) { /* Mixed blocks long, short */ int sfb, window, line, start, end; unsigned tableindex; I192_3 *ix_s; int *scalefac = &sfBandIndex[fr_ps->header->sampling_frequency].s[0]; ix_s = (I192_3 *) ix; /* Write the long block region */ tableindex = gi->table_select[0]; if ( tableindex ) for ( i = 0; i < 36; i += 2 ) { x = ix[i]; y = ix[i + 1]; bits = HuffmanCode( tableindex, x, y, &code, &ext, &cbits, &xbits ); addBits( *pph, code, cbits ); addBits( *pph, ext, xbits ); bitsWritten += bits; } /* Write the short block region */ tableindex = gi->table_select[ 1 ];/* assert( tableindex < 32 ); */ for ( sfb = 3; sfb < 13; sfb++ ) { start = scalefac[ sfb ]; end = scalefac[ sfb+1 ]; for ( window = 0; window < 3; window++ ) for ( line = start; line < end; line += 2 ) { x = (*ix_s)[line][window]; y = (*ix_s)[line + 1][window]; bits = HuffmanCode( tableindex, x, y, &code, &ext, &cbits, &xbits ); addBits( *pph, code, cbits ); addBits( *pph, ext, xbits ); bitsWritten += bits; } } } else { /* Long blocks */ int *scalefac = &sfBandIndex[fr_ps->header->sampling_frequency].l[0]; unsigned scalefac_index = 100; if ( gi->mixed_block_flag ) { region1Start = 36; region2Start = 576; } else { scalefac_index = gi->region0_count + 1; region1Start = scalefac[ scalefac_index ]; scalefac_index += gi->region1_count + 1; region2Start = scalefac[ scalefac_index ]; } for ( i = 0; i < bigvalues; i += 2 ) { unsigned tableindex = 100; /* get table pointer */ if ( i < region1Start ) { tableindex = gi->table_select[0]; pr = &r0; } else if ( i < region2Start ) { tableindex = gi->table_select[1]; pr = &r1; } else { tableindex = gi->table_select[2]; pr = &r2; }/* assert( tableindex < 32 ); */ h = &ht[ tableindex ]; /* get huffman code */ x = ix[i]; y = ix[i + 1]; if ( tableindex ) { bits = HuffmanCode( tableindex, x, y, &code, &ext, &cbits, &xbits ); addBits( *pph, code, cbits ); addBits( *pph, ext, xbits ); bitsWritten += rt = bits; *pr += rt; } else { tablezeros += 1; *pr = 0; } } } } bvbits = bitsWritten; /* 2: Write count1 area */ h = &ht[gi->count1table_select + 32]; count1End = bigvalues + (gi->count1 * 4); for ( i = bigvalues; i < count1End; i += 4 ) { v = ix[i]; w = ix[i+1]; x = ix[i+2]; y = ix[i+3]; bitsWritten += L3_huffman_coder_count1( pph, h, v, w, x, y ); } c1bits = bitsWritten - bvbits; if ( (stuffingBits = gi->part2_3_length - gi->part2_length - bitsWritten) ) { int stuffingWords = stuffingBits / 32; int remainingBits = stuffingBits % 32;/* assert( stuffingBits > 0 ); */ /* Due to the nature of the Huffman code tables, we will pad with ones */ while ( stuffingWords-- ) addBits( *pph, ~0, 32 ); if ( remainingBits ) addBits( *pph, ~0, remainingBits ); bitsWritten += stuffingBits; }}int INLINE abs_and_sign( int *x ){ if ( *x > 0 ) return 0; *x *= -1; return 1;}int L3_huffman_coder_count1( BitHolder **pph, struct huffcodetab *h, int v, int w, int x, int y ){ HUFFBITS huffbits; unsigned int signv, signw, signx, signy, p; int len; int totalBits = 0; signv = abs_and_sign( &v ); signw = abs_and_sign( &w ); signx = abs_and_sign( &x ); signy = abs_and_sign( &y ); p = v + (w << 1) + (x << 2) + (y << 3); huffbits = h->table[p]; len = h->hlen[ p ]; addBits( *pph, huffbits, len ); totalBits += len; if ( v ) { addBits( *pph, signv, 1 ); totalBits += 1; } if ( w ) { addBits( *pph, signw, 1 ); totalBits += 1; } if ( x ) { addBits( *pph, signx, 1 ); totalBits += 1; } if ( y ) { addBits( *pph, signy, 1 ); totalBits += 1; } return totalBits;}/* Implements the pseudocode of page 98 of the IS */int HuffmanCode( int table_select, int x, int y, unsigned int *code, unsigned int *ext, int *cbits, int *xbits ){ unsigned signx, signy, linbitsx, linbitsy, linbits, xlen, ylen, idx; struct huffcodetab *h; *cbits = 0; *xbits = 0; *code = 0; *ext = 0; if ( table_select == 0 ) return 0; signx = abs_and_sign( &x ); signy = abs_and_sign( &y ); h = &(ht[table_select]); xlen = h->xlen; ylen = h->ylen; linbits = h->linbits; linbitsx = linbitsy = 0; if ( table_select > 15 ) { /* ESC-table is used */ if ( x > 14 ) { linbitsx = x - 15;/* assert( linbitsx <= h->linmax ); */ x = 15; } if ( y > 14 ) { linbitsy = y - 15;/* assert( linbitsy <= h->linmax ); */ y = 15; } idx = (x * ylen) + y; *code = h->table[idx]; *cbits = h->hlen[ idx ]; if ( x > 14 ) { *ext |= linbitsx; *xbits += linbits; } if ( x != 0 ) { *ext <<= 1; *ext |= signx; *xbits += 1; } if ( y > 14 ) { *ext <<= linbits; *ext |= linbitsy; *xbits += linbits; } if ( y != 0 ) { *ext <<= 1; *ext |= signy; *xbits += 1; } } else { /* No ESC-words */ idx = (x * ylen) + y; *code = h->table[idx]; *cbits += h->hlen[ idx ]; if ( x != 0 ) { *code <<= 1; *code |= signx; *cbits += 1; } if ( y != 0 ) { *code <<= 1; *code |= signy; *cbits += 1; } }/*assert( *cbits <= 32 ); assert( *xbits <= 32 ); */ return *cbits + *xbits;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -