📄 mpegaudiodec.c
字号:
ch_bitrate = bitrate / nb_channels; if (!lsf) { if ((freq == 48000 && ch_bitrate >= 56) || (ch_bitrate >= 56 && ch_bitrate <= 80)) table = 0; else if (freq != 48000 && ch_bitrate >= 96) table = 1; else if (freq != 32000 && ch_bitrate <= 48) table = 2; else table = 3; } else { table = 4; } return table;}static int mp_decode_layer2(MPADecodeContext *s){ int sblimit; /* number of used subbands */ const unsigned char *alloc_table; int table, bit_alloc_bits, i, j, ch, bound, v; unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT]; unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT]; unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf; int scale, qindex, bits, steps, k, l, m, b; /* select decoding table */ table = l2_select_table(s->bit_rate / 1000, s->nb_channels, s->sample_rate, s->lsf); sblimit = sblimit_table[table]; alloc_table = alloc_tables[table]; if (s->mode == MPA_JSTEREO) bound = (s->mode_ext + 1) * 4; else bound = sblimit; dprintf("bound=%d sblimit=%d\n", bound, sblimit); /* sanity check */ if( bound > sblimit ) bound = sblimit; /* parse bit allocation */ j = 0; for(i=0;i<bound;i++) { bit_alloc_bits = alloc_table[j]; for(ch=0;ch<s->nb_channels;ch++) { bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits); } j += 1 << bit_alloc_bits; } for(i=bound;i<sblimit;i++) { bit_alloc_bits = alloc_table[j]; v = get_bits(&s->gb, bit_alloc_bits); bit_alloc[0][i] = v; bit_alloc[1][i] = v; j += 1 << bit_alloc_bits; }#ifdef DEBUG { for(ch=0;ch<s->nb_channels;ch++) { for(i=0;i<sblimit;i++) printf(" %d", bit_alloc[ch][i]); printf("\n"); } }#endif /* scale codes */ for(i=0;i<sblimit;i++) { for(ch=0;ch<s->nb_channels;ch++) { if (bit_alloc[ch][i]) scale_code[ch][i] = get_bits(&s->gb, 2); } } /* scale factors */ for(i=0;i<sblimit;i++) { for(ch=0;ch<s->nb_channels;ch++) { if (bit_alloc[ch][i]) { sf = scale_factors[ch][i]; switch(scale_code[ch][i]) { default: case 0: sf[0] = get_bits(&s->gb, 6); sf[1] = get_bits(&s->gb, 6); sf[2] = get_bits(&s->gb, 6); break; case 2: sf[0] = get_bits(&s->gb, 6); sf[1] = sf[0]; sf[2] = sf[0]; break; case 1: sf[0] = get_bits(&s->gb, 6); sf[2] = get_bits(&s->gb, 6); sf[1] = sf[0]; break; case 3: sf[0] = get_bits(&s->gb, 6); sf[2] = get_bits(&s->gb, 6); sf[1] = sf[2]; break; } } } }#ifdef DEBUG for(ch=0;ch<s->nb_channels;ch++) { for(i=0;i<sblimit;i++) { if (bit_alloc[ch][i]) { sf = scale_factors[ch][i]; printf(" %d %d %d", sf[0], sf[1], sf[2]); } else { printf(" -"); } } printf("\n"); }#endif /* samples */ for(k=0;k<3;k++) { for(l=0;l<12;l+=3) { j = 0; for(i=0;i<bound;i++) { bit_alloc_bits = alloc_table[j]; for(ch=0;ch<s->nb_channels;ch++) { b = bit_alloc[ch][i]; if (b) { scale = scale_factors[ch][i][k]; qindex = alloc_table[j+b]; bits = quant_bits[qindex]; if (bits < 0) { /* 3 values at the same time */ v = get_bits(&s->gb, -bits); steps = quant_steps[qindex]; s->sb_samples[ch][k * 12 + l + 0][i] = l2_unscale_group(steps, v % steps, scale); v = v / steps; s->sb_samples[ch][k * 12 + l + 1][i] = l2_unscale_group(steps, v % steps, scale); v = v / steps; s->sb_samples[ch][k * 12 + l + 2][i] = l2_unscale_group(steps, v, scale); } else { for(m=0;m<3;m++) { v = get_bits(&s->gb, bits); v = l1_unscale(bits - 1, v, scale); s->sb_samples[ch][k * 12 + l + m][i] = v; } } } else { s->sb_samples[ch][k * 12 + l + 0][i] = 0; s->sb_samples[ch][k * 12 + l + 1][i] = 0; s->sb_samples[ch][k * 12 + l + 2][i] = 0; } } /* next subband in alloc table */ j += 1 << bit_alloc_bits; } /* XXX: find a way to avoid this duplication of code */ for(i=bound;i<sblimit;i++) { bit_alloc_bits = alloc_table[j]; b = bit_alloc[0][i]; if (b) { int mant, scale0, scale1; scale0 = scale_factors[0][i][k]; scale1 = scale_factors[1][i][k]; qindex = alloc_table[j+b]; bits = quant_bits[qindex]; if (bits < 0) { /* 3 values at the same time */ v = get_bits(&s->gb, -bits); steps = quant_steps[qindex]; mant = v % steps; v = v / steps; s->sb_samples[0][k * 12 + l + 0][i] = l2_unscale_group(steps, mant, scale0); s->sb_samples[1][k * 12 + l + 0][i] = l2_unscale_group(steps, mant, scale1); mant = v % steps; v = v / steps; s->sb_samples[0][k * 12 + l + 1][i] = l2_unscale_group(steps, mant, scale0); s->sb_samples[1][k * 12 + l + 1][i] = l2_unscale_group(steps, mant, scale1); s->sb_samples[0][k * 12 + l + 2][i] = l2_unscale_group(steps, v, scale0); s->sb_samples[1][k * 12 + l + 2][i] = l2_unscale_group(steps, v, scale1); } else { for(m=0;m<3;m++) { mant = get_bits(&s->gb, bits); s->sb_samples[0][k * 12 + l + m][i] = l1_unscale(bits - 1, mant, scale0); s->sb_samples[1][k * 12 + l + m][i] = l1_unscale(bits - 1, mant, scale1); } } } else { s->sb_samples[0][k * 12 + l + 0][i] = 0; s->sb_samples[0][k * 12 + l + 1][i] = 0; s->sb_samples[0][k * 12 + l + 2][i] = 0; s->sb_samples[1][k * 12 + l + 0][i] = 0; s->sb_samples[1][k * 12 + l + 1][i] = 0; s->sb_samples[1][k * 12 + l + 2][i] = 0; } /* next subband in alloc table */ j += 1 << bit_alloc_bits; } /* fill remaining samples to zero */ for(i=sblimit;i<SBLIMIT;i++) { for(ch=0;ch<s->nb_channels;ch++) { s->sb_samples[ch][k * 12 + l + 0][i] = 0; s->sb_samples[ch][k * 12 + l + 1][i] = 0; s->sb_samples[ch][k * 12 + l + 2][i] = 0; } } } } return 3 * 12;}/* * Seek back in the stream for backstep bytes (at most 511 bytes) */static void seek_to_maindata(MPADecodeContext *s, unsigned int backstep){ uint8_t *ptr; /* compute current position in stream */ ptr = (uint8_t *)(s->gb.buffer + (get_bits_count(&s->gb)>>3)); /* copy old data before current one */ ptr -= backstep; memcpy(ptr, s->inbuf1[s->inbuf_index ^ 1] + BACKSTEP_SIZE + s->old_frame_size - backstep, backstep); /* init get bits again */ init_get_bits(&s->gb, ptr, (s->frame_size + backstep)*8); /* prepare next buffer */ s->inbuf_index ^= 1; s->inbuf = &s->inbuf1[s->inbuf_index][BACKSTEP_SIZE]; s->old_frame_size = s->frame_size;}static inline void lsf_sf_expand(int *slen, int sf, int n1, int n2, int n3){ if (n3) { slen[3] = sf % n3; sf /= n3; } else { slen[3] = 0; } if (n2) { slen[2] = sf % n2; sf /= n2; } else { slen[2] = 0; } slen[1] = sf % n1; sf /= n1; slen[0] = sf;}static void exponents_from_scale_factors(MPADecodeContext *s, GranuleDef *g, int16_t *exponents){ const uint8_t *bstab, *pretab; int len, i, j, k, l, v0, shift, gain, gains[3]; int16_t *exp_ptr; exp_ptr = exponents; gain = g->global_gain - 210; shift = g->scalefac_scale + 1; bstab = band_size_long[s->sample_rate_index]; pretab = mpa_pretab[g->preflag]; for(i=0;i<g->long_end;i++) { v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift); len = bstab[i]; for(j=len;j>0;j--) *exp_ptr++ = v0; } if (g->short_start < 13) { bstab = band_size_short[s->sample_rate_index]; gains[0] = gain - (g->subblock_gain[0] << 3); gains[1] = gain - (g->subblock_gain[1] << 3); gains[2] = gain - (g->subblock_gain[2] << 3); k = g->long_end; for(i=g->short_start;i<13;i++) { len = bstab[i]; for(l=0;l<3;l++) { v0 = gains[l] - (g->scale_factors[k++] << shift); for(j=len;j>0;j--) *exp_ptr++ = v0; } } }}/* handle n = 0 too */static inline int get_bitsz(GetBitContext *s, int n){ if (n == 0) return 0; else return get_bits(s, n);}static int huffman_decode(MPADecodeContext *s, GranuleDef *g, int16_t *exponents, int end_pos){ int s_index; int linbits, code, x, y, l, v, i, j, k, pos; GetBitContext last_gb; VLC *vlc; uint8_t *code_table; /* low frequencies (called big values) */ s_index = 0; for(i=0;i<3;i++) { j = g->region_size[i]; if (j == 0) continue; /* select vlc table */ k = g->table_select[i]; l = mpa_huff_data[k][0]; linbits = mpa_huff_data[k][1]; vlc = &huff_vlc[l]; code_table = huff_code_table[l]; /* read huffcode and compute each couple */ for(;j>0;j--) { if (get_bits_count(&s->gb) >= end_pos) break; if (code_table) { code = get_vlc(&s->gb, vlc); if (code < 0) return -1; y = code_table[code]; x = y >> 4; y = y & 0x0f; } else { x = 0; y = 0; } dprintf("region=%d n=%d x=%d y=%d exp=%d\n", i, g->region_size[i] - j, x, y, exponents[s_index]); if (x) { if (x == 15) x += get_bitsz(&s->gb, linbits); v = l3_unscale(x, exponents[s_index]); if (get_bits1(&s->gb)) v = -v; } else { v = 0; } g->sb_hybrid[s_index++] = v; if (y) { if (y == 15) y += get_bitsz(&s->gb, linbits); v = l3_unscale(y, exponents[s_index]); if (get_bits1(&s->gb)) v = -v; } else { v = 0; } g->sb_hybrid[s_index++] = v; } } /* high frequencies */ vlc = &huff_quad_vlc[g->count1table_select]; last_gb.buffer = NULL; while (s_index <= 572) { pos = get_bits_count(&s->gb); if (pos >= end_pos) { if (pos > end_pos && last_gb.buffer != NULL) { /* some encoders generate an incorrect size for this part. We must go back into the data */ s_index -= 4; s->gb = last_gb; } break; } last_gb= s->gb; code = get_vlc(&s->gb, vlc); dprintf("t=%d code=%d\n", g->count1table_select, code); if (code < 0) return -1; for(i=0;i<4;i++) { if (code & (8 >> i)) { /* non zero value. Could use a hand coded function for 'one' value */ v = l3_unscale(1, exponents[s_index]); if(get_bits1(&s->gb)) v = -v; } else { v = 0; } g->sb_hybrid[s_index++] = v; } } while (s_index < 576) g->sb_hybrid[s_index++] = 0; return 0;}/* Reorder short blocks from bitstream order to interleaved order. It would be faster to do it in parsing, but the code would be far more complicated */static void reorder_block(MPADecodeContext *s, GranuleDef *g){ int i, j, k, len; int32_t *ptr, *dst, *ptr1; int32_t tmp[576]; if (g->block_type != 2) return; if (g->switch_point) { if (s->sample_rate_index != 8) { ptr = g->sb_hybrid + 36; } else { ptr = g->sb_hybrid + 48; } } else { ptr = g->sb_hybrid; } for(i=g->short_start;i<13;i++) { len = band_size_short[s->sample_rate_index][i]; ptr1 = ptr; for(k=0;k<3;k++) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -