📄 mpeglayer3.cc
字号:
for (k = i = 0; i < 4; i++) for (j = 0; j < si[i]; j++, k++) if (slen[i] == 0) sb[k] = 0; else sb[k] = wgetbits(slen[i]); } } { int sfb, window; int k = 0; if (gi->window_switching_flag && (gi->block_type == 2)) { if (gi->mixed_block_flag) { for (sfb = 0; sfb < 8; sfb++) sf->l[sfb] = sb[k++]; sfb = 3; } else sfb = 0; for (; sfb < 12; sfb++) for (window = 0; window < 3; window++) sf->s[window][sfb] = sb[k++]; sf->s[0][12] = sf->s[1][12] = sf->s[2][12] = 0; } else { for (sfb = 0; sfb < 21; sfb++) sf->l[sfb] = sb[k++]; sf->l[21] = sf->l[22] = 0; } }}typedef unsigned int HUFFBITS;#define MXOFF 250/* do the huffman-decoding *//* note! for counta,countb -the 4 bit value is returned in y, discard x */// Huffman decoder for tablename<32inline voidMpegtoraw::huffmandecoder_1(const HUFFMANCODETABLE * h, int *x, int *y){ HUFFBITS level = (1 << (sizeof(HUFFBITS) * 8 - 1)); int point = 0; /* Lookup in Huffman table. */ for (;;) { if (h->val[point][0] == 0) { /*end of tree */ int xx, yy; xx = h->val[point][1] >> 4; yy = h->val[point][1] & 0xf; if (h->linbits) { if ((h->xlen) == (unsigned) xx) xx += wgetbits(h->linbits); if (xx) if (wgetbit()) xx = -xx; if ((h->ylen) == (unsigned) yy) yy += wgetbits(h->linbits); if (yy) if (wgetbit()) yy = -yy; } else { if (xx) if (wgetbit()) xx = -xx; if (yy) if (wgetbit()) yy = -yy; } *x = xx; *y = yy; break; } point += h->val[point][wgetbit()]; level >>= 1; if (!(level || ((unsigned) point < ht->treelen))) { register int xx, yy; xx = (h->xlen << 1); // set x and y to a medium value as a simple concealment yy = (h->ylen << 1); // h->xlen and h->ylen can't be 1 under tablename 32 // if(xx) if (wgetbit()) xx = -xx; // if(yy) if (wgetbit()) yy = -yy; *x = xx; *y = yy; break; } }}// Huffman decoder tablenumber>=32inline voidMpegtoraw::huffmandecoder_2(const HUFFMANCODETABLE * h, int *x, int *y, int *v, int *w){ HUFFBITS level = (1 << (sizeof(HUFFBITS) * 8 - 1)); int point = 0; /* Lookup in Huffman table. */ for (;;) { if (h->val[point][0] == 0) { /*end of tree */ register int t = h->val[point][1]; if (t & 8) *v = 1 - (wgetbit() << 1); else *v = 0; if (t & 4) *w = 1 - (wgetbit() << 1); else *w = 0; if (t & 2) *x = 1 - (wgetbit() << 1); else *x = 0; if (t & 1) *y = 1 - (wgetbit() << 1); else *y = 0; break; } point += h->val[point][wgetbit()]; level >>= 1; if (!(level || ((unsigned) point < ht->treelen))) { *v = 1 - (wgetbit() << 1); *w = 1 - (wgetbit() << 1); *x = 1 - (wgetbit() << 1); *y = 1 - (wgetbit() << 1); break; } }}typedef struct{ int l[23]; int s[14];}SFBANDINDEX;static SFBANDINDEX sfBandIndextable[2][3] = { // MPEG 1 {{{0, 4, 8, 12, 16, 20, 24, 30, 36, 44, 52, 62, 74, 90, 110, 134, 162, 196, 238, 288, 342, 418, 576}, {0, 4, 8, 12, 16, 22, 30, 40, 52, 66, 84, 106, 136, 192}}, {{0, 4, 8, 12, 16, 20, 24, 30, 36, 42, 50, 60, 72, 88, 106, 128, 156, 190, 230, 276, 330, 384, 576}, {0, 4, 8, 12, 16, 22, 28, 38, 50, 64, 80, 100, 126, 192}}, {{0, 4, 8, 12, 16, 20, 24, 30, 36, 44, 54, 66, 82, 102, 126, 156, 194, 240, 296, 364, 448, 550, 576}, {0, 4, 8, 12, 16, 22, 30, 42, 58, 78, 104, 138, 180, 192}}}, // MPEG 2 {{{0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464, 522, 576}, {0, 4, 8, 12, 18, 24, 32, 42, 56, 74, 100, 132, 174, 192}}, {{0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 114, 136, 162, 194, 232, 278, 330, 394, 464, 540, 576}, {0, 4, 8, 12, 18, 26, 36, 48, 62, 80, 104, 136, 180, 192}}, {{0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464, 522, 576}, {0, 4, 8, 12, 18, 26, 36, 48, 62, 80, 104, 134, 174, 192}}}};voidMpegtoraw::layer3huffmandecode(int ch, int gr, int out[SBLIMIT][SSLIMIT]){ layer3grinfo *gi = &(sideinfo.ch[ch].gr[gr]); int part2_3_end = layer3part2start + (gi->part2_3_length); int region1Start, region2Start; int i, e = gi->big_values << 1; /* Find region boundary for short block case. */ if (gi->generalflag) { /* Region2. */ region1Start = 36; /* sfb[9/3]*3=36 */ region2Start = 576; /* No Region2 for short block case. */ } else { /* Find region boundary for long block case. */ region1Start = sfBandIndextable[version][frequency].l[gi->region0_count + 1]; region2Start = sfBandIndextable[version][frequency].l[gi->region0_count + gi->region1_count + 2]; } /* Read bigvalues area. */ for (i = 0; i < e;) { const HUFFMANCODETABLE *h; register int end; if (i < region1Start) { h = &ht[gi->table_select[0]]; if (region1Start > e) end = e; else end = region1Start; } else if (i < region2Start) { h = &ht[gi->table_select[1]]; if (region2Start > e) end = e; else end = region2Start; } else { h = &ht[gi->table_select[2]]; end = e; } if (h->treelen) while (i < end) { huffmandecoder_1(h, &out[0][i], &out[0][i + 1]); i += 2; } else for (; i < end; i += 2) out[0][i] = out[0][i + 1] = 0; } /* Read count1 area. */ const HUFFMANCODETABLE *h = &ht[gi->count1table_select + 32]; while (bitwindow.gettotalbit() < part2_3_end) { huffmandecoder_2(h, &out[0][i + 2], &out[0][i + 3], &out[0][i], &out[0][i + 1]); i += 4; if (i >= ARRAYSIZE) { bitwindow.rewind(bitwindow.gettotalbit() - part2_3_end); return; } } for (; i < ARRAYSIZE; i++) out[0][i] = 0; bitwindow.rewind(bitwindow.gettotalbit() - part2_3_end);}static int pretab[22] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 2, 0 };inline REALMpegtoraw::layer3twopow2(int scale, int preflag, int pretab_offset, int l){ int index = l; if (preflag) index += pretab_offset; return (two_to_negative_half_pow[index << scale]);}inline REALMpegtoraw::layer3twopow2_1(int a, int b, int c){ return POW2_1[a][b][c];}voidMpegtoraw::layer3dequantizesample(int ch, int gr, int in[SBLIMIT][SSLIMIT], REAL out[SBLIMIT][SSLIMIT]){ layer3grinfo *gi = &(sideinfo.ch[ch].gr[gr]); SFBANDINDEX *sfBandIndex = &(sfBandIndextable[version][frequency]); REAL19 globalgain = POW2[gi->global_gain]; /* choose correct scalefactor band per block type, initialize boundary */ /* and apply formula per block type */ if (!gi->generalflag) { /* LONG blocks: 0,1,3 */ int next_cb_boundary; int cb = -1, index = 0; REAL factor; do { next_cb_boundary = sfBandIndex->l[(++cb) + 1]; factor = globalgain * layer3twopow2(gi->scalefac_scale, gi->preflag, pretab[cb], scalefactors[ch].l[cb]); for (; index < next_cb_boundary;) { out[0][index] = factor * TO_FOUR_THIRDS(in[0][index]); index++; out[0][index] = factor * TO_FOUR_THIRDS(in[0][index]); index++; } } while (index < ARRAYSIZE); } else if (!gi->mixed_block_flag) { int cb = 0, index = 0; int cb_width; do { cb_width = (sfBandIndex->s[cb + 1] - sfBandIndex->s[cb]) >> 1; for (register int k = 0; k < 3; k++) { REAL factor; register int count = cb_width; factor = globalgain * layer3twopow2_1(gi->subblock_gain[k], gi->scalefac_scale, scalefactors[ch].s[k][cb]); do { out[0][index] = factor * TO_FOUR_THIRDS(in[0][index]); index++; out[0][index] = factor * TO_FOUR_THIRDS(in[0][index]); index++; } while (--count); } cb++; } while (index < ARRAYSIZE); } else { int cb_begin = 0, cb_width = 0; int cb = 0; int next_cb_boundary = sfBandIndex->l[1]; /* LONG blocks: 0,1,3 */ int index; /* Compute overall (global) scaling. */ { for (int sb = 0; sb < SBLIMIT; sb++) { int *i = in[sb]; REAL *o = out[sb]; o[0] = globalgain * TO_FOUR_THIRDS(i[0]); o[1] = globalgain * TO_FOUR_THIRDS(i[1]); o[2] = globalgain * TO_FOUR_THIRDS(i[2]); o[3] = globalgain * TO_FOUR_THIRDS(i[3]); o[4] = globalgain * TO_FOUR_THIRDS(i[4]); o[5] = globalgain * TO_FOUR_THIRDS(i[5]); o[6] = globalgain * TO_FOUR_THIRDS(i[6]); o[7] = globalgain * TO_FOUR_THIRDS(i[7]); o[8] = globalgain * TO_FOUR_THIRDS(i[8]); o[9] = globalgain * TO_FOUR_THIRDS(i[9]); o[10] = globalgain * TO_FOUR_THIRDS(i[10]); o[11] = globalgain * TO_FOUR_THIRDS(i[11]); o[12] = globalgain * TO_FOUR_THIRDS(i[12]); o[13] = globalgain * TO_FOUR_THIRDS(i[13]); o[14] = globalgain * TO_FOUR_THIRDS(i[14]); o[15] = globalgain * TO_FOUR_THIRDS(i[15]); o[16] = globalgain * TO_FOUR_THIRDS(i[16]); o[17] = globalgain * TO_FOUR_THIRDS(i[17]); } } for (index = 0; index < SSLIMIT * 2; index++) { if (index == next_cb_boundary) { if (index == sfBandIndex->l[8]) { next_cb_boundary = sfBandIndex->s[4]; next_cb_boundary = MUL3(next_cb_boundary); cb = 3; cb_width = sfBandIndex->s[4] - sfBandIndex->s[3]; cb_begin = sfBandIndex->s[3]; cb_begin = MUL3(cb_begin); } else if (index < sfBandIndex->l[8]) next_cb_boundary = sfBandIndex->l[(++cb) + 1]; else { next_cb_boundary = sfBandIndex->s[(++cb) + 1]; next_cb_boundary = MUL3(next_cb_boundary); cb_begin = sfBandIndex->s[cb]; cb_width = sfBandIndex->s[cb + 1] - cb_begin; cb_begin = MUL3(cb_begin); } } /* LONG block types 0,1,3 & 1st 2 subbands of switched blocks */ out[0][index] *= layer3twopow2(gi->scalefac_scale, gi->preflag, pretab[cb], scalefactors[ch].l[cb]); } for (; index < ARRAYSIZE; index++) { if (index == next_cb_boundary) { if (index == sfBandIndex->l[8]) { next_cb_boundary = sfBandIndex->s[4]; next_cb_boundary = MUL3(next_cb_boundary); cb = 3; cb_width = sfBandIndex->s[4] - sfBandIndex->s[3]; cb_begin = sfBandIndex->s[3]; cb_begin = (cb_begin << 2) - cb_begin; } else if (index < sfBandIndex->l[8]) next_cb_boundary = sfBandIndex->l[(++cb) + 1]; else { next_cb_boundary = sfBandIndex->s[(++cb) + 1]; next_cb_boundary = MUL3(next_cb_boundary); cb_begin = sfBandIndex->s[cb]; cb_width = sfBandIndex->s[cb + 1] - cb_begin; cb_begin = MUL3(cb_begin); } } { int t_index = (index - cb_begin) / cb_width; out[0][index] *= layer3twopow2_1(gi->subblock_gain[t_index], gi->scalefac_scale, scalefactors[ch]. s[t_index][cb]); } } }}inline voidMpegtoraw::layer3fixtostereo(int gr, REAL in[2][SBLIMIT][SSLIMIT]){ layer3grinfo *gi = &(sideinfo.ch[0].gr[gr]); SFBANDINDEX *sfBandIndex = &(sfBandIndextable[version][frequency]); int ms_stereo = (mode == joint) && (extendedmode & 0x2); int i_stereo = (mode == joint) && (extendedmode & 0x1); if (!inputstereo) { /* mono , bypass xr[0][][] to lr[0][][] */ // memcpy(out[0][0],in[0][0],ARRAYSIZE*REALSIZE); return; } if (i_stereo) { int i; int is_pos[ARRAYSIZE]; RATIOS is_ratio[ARRAYSIZE]; const RATIOS *ratios; if (version) ratios = rat_2[gi->scalefac_compress % 2]; else ratios = rat_1; /* initialization */ for (i = 0; i < ARRAYSIZE; i += 2) is_pos[i] = is_pos[i + 1] = 7; if (gi->generalflag) { if (gi->mixed_block_flag) // Part I { int max_sfb = 0; for (int j = 0; j < 3; j++) { int sfb, sfbcnt = 2; for (sfb = 12; sfb >= 3; sfb--) { int lines; i = sfBandIndex->s[sfb]; lines = sfBandIndex->s[sfb + 1] - i; i = MUL3(i) + (j + 1) * lines - 1; for (; lines > 0; lines--, i--) if (in[1][0][i] != 0.0f) { sfbcnt = sfb; sfb = 0; break; // quit loop } } sfb = sfbcnt + 1; if (sfb > max_sfb) max_sfb = sfb; for (; sfb < 12; sfb++) { int k, t; t = sfBandIndex->s[sfb]; k = sfBandIndex->s[sfb + 1] - t; i = MUL3(t) + j * k; t = scalefactors[1].s[j][sfb]; if (t != 7) { RATIOS r = ratios[t]; for (; k > 0; k--, i++) { is_pos[i] = t; is_ratio[i] = r; } } else for (; k > 0; k--, i++) is_pos[i] = t; } sfb = sfBandIndex->s[10]; sfb = MUL3(sfb) + j * (sfBandIndex->s[11] - sfb); { int k, t; t = sfBandIndex->s[11]; k = sfBandIndex->s[12] - t; i = MUL3(t) + j * k; t = is_pos[sfb]; if (t != 7) { RATIOS r = is_ratio[sfb]; for (; k > 0; k--, i++) { is_pos[i] = t; is_ratio[i] = r; } } else for (; k > 0; k--, i++) is_pos[i] = t; } } if (max_sfb <= 3) { { REAL temp; int k; temp = in[1][0][0];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -