📄 takehiro.c
字号:
int r01_div[7 + 15 + 1]; int r0_tbl[7 + 15 + 1]; int r1_tbl[7 + 15 + 1]; /* SHORT BLOCK stuff fails for MPEG2 */ if (gi->block_type == SHORT_TYPE && gfc->mode_gr==1) return; memcpy(&cod_info2, gi, sizeof(gr_info)); if (gi->block_type == NORM_TYPE) { recalc_divide_init(gfc, gi, ix, r01_bits,r01_div,r0_tbl,r1_tbl); recalc_divide_sub(gfc, &cod_info2, gi, ix, r01_bits,r01_div,r0_tbl,r1_tbl); } i = cod_info2.big_values; if (i == 0 || (unsigned int)(ix[i-2] | ix[i-1]) > 1) return; i = gi->count1 + 2; if (i > 576) return; /* Determines the number of bits to encode the quadruples. */ memcpy(&cod_info2, gi, sizeof(gr_info)); cod_info2.count1 = i; a1 = a2 = 0; assert(i <= 576); for (; i > cod_info2.big_values; i -= 4) { int p = ((ix[i-4] * 2 + ix[i-3]) * 2 + ix[i-2]) * 2 + ix[i-1]; a1 += t32l[p]; a2 += t33l[p]; } cod_info2.big_values = i; cod_info2.count1table_select = 0; if (a1 > a2) { a1 = a2; cod_info2.count1table_select = 1; } cod_info2.count1bits = a1; if (cod_info2.block_type == NORM_TYPE) recalc_divide_sub(gfc, &cod_info2, gi, ix, r01_bits,r01_div,r0_tbl,r1_tbl); else { /* Count the number of bits necessary to code the bigvalues region. */ cod_info2.part2_3_length = a1; a1 = gfc->scalefac_band.l[7 + 1]; if (a1 > i) { a1 = i; } if (a1 > 0) cod_info2.table_select[0] = gfc->choose_table(ix, ix + a1, (int *)&cod_info2.part2_3_length); if (i > a1) cod_info2.table_select[1] = gfc->choose_table(ix + a1, ix + i, (int *)&cod_info2.part2_3_length); if (gi->part2_3_length > cod_info2.part2_3_length) memcpy(gi, &cod_info2, sizeof(gr_info)); }}static const int slen1_n[16] = { 1, 1, 1, 1, 8, 2, 2, 2, 4, 4, 4, 8, 8, 8,16,16 };static const int slen2_n[16] = { 1, 2, 4, 8, 1, 2, 4, 8, 2, 4, 8, 2, 4, 8, 4, 8 };const int slen1_tab [16] = { 0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4 };const int slen2_tab [16] = { 0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3 };static voidscfsi_calc(int ch, III_side_info_t *l3_side){ int i, s1, s2, c1, c2; int sfb; gr_info *gi = &l3_side->tt[1][ch]; gr_info *g0 = &l3_side->tt[0][ch]; for (i = 0; i < (sizeof(scfsi_band) / sizeof(int)) - 1; i++) { for (sfb = scfsi_band[i]; sfb < scfsi_band[i + 1]; sfb++) { if (g0->scalefac[sfb] != gi->scalefac[sfb] && gi->scalefac[sfb] >= 0) break; } if (sfb == scfsi_band[i + 1]) { for (sfb = scfsi_band[i]; sfb < scfsi_band[i + 1]; sfb++) { gi->scalefac[sfb] = -1; } l3_side->scfsi[ch][i] = 1; } } s1 = c1 = 0; for (sfb = 0; sfb < 11; sfb++) { if (gi->scalefac[sfb] == -1) continue; c1++; if (s1 < gi->scalefac[sfb]) s1 = gi->scalefac[sfb]; } s2 = c2 = 0; for (; sfb < SBPSY_l; sfb++) { if (gi->scalefac[sfb] == -1) continue; c2++; if (s2 < gi->scalefac[sfb]) s2 = gi->scalefac[sfb]; } for (i = 0; i < 16; i++) { if (s1 < slen1_n[i] && s2 < slen2_n[i]) { int c = slen1_tab[i] * c1 + slen2_tab[i] * c2; if (gi->part2_length > c) { gi->part2_length = c; gi->scalefac_compress = i; } } }}/*Find the optimal way to store the scalefactors.Only call this routine after final scalefactors have beenchosen and the channel/granule will not be re-encoded. */void best_scalefac_store( const lame_internal_flags *gfc, const int gr, const int ch, III_side_info_t * const l3_side){ /* use scalefac_scale if we can */ gr_info *gi = &l3_side->tt[gr][ch]; int sfb,i,j,l; int recalc = 0; /* remove scalefacs from bands with ix=0. This idea comes * from the AAC ISO docs. added mt 3/00 */ /* check if l3_enc=0 */ j = 0; for ( sfb = 0; sfb < gi->sfbmax; sfb++ ) { int width = gi->width[sfb]; assert( width >= 0 ); j += width; for (l = -width; l < 0; l++) { if (gi->l3_enc[l+j]!=0) break; } if (l==0) gi->scalefac[sfb] = recalc = -2; /* anything goes. */ /* only best_scalefac_store and calc_scfsi * know--and only they should know--about the magic number -2. */ } if (!gi->scalefac_scale && !gi->preflag) { int s = 0; for (sfb = 0; sfb < gi->sfbmax; sfb++) if (gi->scalefac[sfb] > 0) s |= gi->scalefac[sfb]; if (!(s & 1) && s != 0) { for (sfb = 0; sfb < gi->sfbmax; sfb++) if (gi->scalefac[sfb] > 0) gi->scalefac[sfb] >>= 1; gi->scalefac_scale = recalc = 1; } } if (!gi->preflag && gi->block_type != SHORT_TYPE && gfc->mode_gr==2) { for (sfb = 11; sfb < SBPSY_l; sfb++) if (gi->scalefac[sfb] < pretab[sfb] && gi->scalefac[sfb] != -2) break; if (sfb == SBPSY_l) { for (sfb = 11; sfb < SBPSY_l; sfb++) if (gi->scalefac[sfb] > 0) gi->scalefac[sfb] -= pretab[sfb]; gi->preflag = recalc = 1; } } for ( i = 0; i < 4; i++ ) l3_side->scfsi[ch][i] = 0; if (gfc->mode_gr==2 && gr == 1 && l3_side->tt[0][ch].block_type != SHORT_TYPE && l3_side->tt[1][ch].block_type != SHORT_TYPE) { scfsi_calc(ch, l3_side); recalc = 0; } for ( sfb = 0; sfb < gi->sfbmax; sfb++ ) { if ( gi->scalefac[sfb] == -2 ) { gi->scalefac[sfb] = 0; /* if anything goes, then 0 is a good choice */ } } if (recalc) { if (gfc->mode_gr == 2) { scale_bitcount(gi); } else { scale_bitcount_lsf(gfc, gi); } }}#ifndef NDEBUGstatic int all_scalefactors_not_negative( int const* scalefac, int n ){ int i; for ( i = 0; i < n; ++i ) { if ( scalefac[i] < 0 ) return 0; } return 1;}#endif /* number of bits used to encode scalefacs *//* 18*slen1_tab[i] + 18*slen2_tab[i] */static const int scale_short[16] = { 0, 18, 36, 54, 54, 36, 54, 72, 54, 72, 90, 72, 90, 108, 108, 126 };/* 17*slen1_tab[i] + 18*slen2_tab[i] */static const int scale_mixed[16] = { 0, 18, 36, 54, 51, 35, 53, 71, 52, 70, 88, 69, 87, 105, 104, 122 };/* 11*slen1_tab[i] + 10*slen2_tab[i] */static const int scale_long[16] = { 0, 10, 20, 30, 33, 21, 31, 41, 32, 42, 52, 43, 53, 63, 64, 74 };/*************************************************************************//* scale_bitcount *//*************************************************************************//* Also calculates the number of bits necessary to code the scalefactors. */int scale_bitcount(gr_info * const cod_info){ int k, sfb, max_slen1 = 0, max_slen2 = 0; /* maximum values */ const int *tab; int *scalefac = cod_info->scalefac; assert( all_scalefactors_not_negative( scalefac, cod_info->sfbmax ) ); if ( cod_info->block_type == SHORT_TYPE ) { tab = scale_short; if (cod_info->mixed_block_flag) tab = scale_mixed; } else { /* block_type == 1,2,or 3 */ tab = scale_long; if (!cod_info->preflag) { for ( sfb = 11; sfb < SBPSY_l; sfb++ ) if (scalefac[sfb] < pretab[sfb]) break; if (sfb == SBPSY_l) { cod_info->preflag = 1; for ( sfb = 11; sfb < SBPSY_l; sfb++ ) scalefac[sfb] -= pretab[sfb]; } } } for (sfb = 0; sfb < cod_info->sfbdivide; sfb++) if (max_slen1 < scalefac[sfb]) max_slen1 = scalefac[sfb]; for (; sfb < cod_info->sfbmax; sfb++) if (max_slen2 < scalefac[sfb]) max_slen2 = scalefac[sfb]; /* from Takehiro TOMINAGA <tominaga@isoternet.org> 10/99 * loop over *all* posible values of scalefac_compress to find the * one which uses the smallest number of bits. ISO would stop * at first valid index */ cod_info->part2_length = LARGE_BITS; for ( k = 0; k < 16; k++ ) { if (max_slen1 < slen1_n[k] && max_slen2 < slen2_n[k] && cod_info->part2_length > tab[k]) { cod_info->part2_length=tab[k]; cod_info->scalefac_compress=k; } } return cod_info->part2_length == LARGE_BITS;}/* table of largest scalefactor values for MPEG2*/static const int max_range_sfac_tab[6][4] ={ { 15, 15, 7, 7}, { 15, 15, 7, 0}, { 7, 3, 0, 0}, { 15, 31, 31, 0}, { 7, 7, 7, 0}, { 3, 3, 0, 0}};/*************************************************************************//* scale_bitcount_lsf *//*************************************************************************//* Also counts the number of bits to encode the scalefacs but for MPEG 2 */ /* Lower sampling frequencies (24, 22.05 and 16 kHz.) */ /* This is reverse-engineered from section 2.4.3.2 of the MPEG2 IS, *//* "Audio Decoding Layer III" */int scale_bitcount_lsf(const lame_internal_flags *gfc, gr_info * const cod_info){ int table_number, row_in_table, partition, nr_sfb, window, over; int i, sfb, max_sfac[ 4 ]; const int *partition_table; int *scalefac = cod_info->scalefac; /* Set partition table. Note that should try to use table one, but do not yet... */ if ( cod_info->preflag ) table_number = 2; else table_number = 0; for ( i = 0; i < 4; i++ ) max_sfac[i] = 0; if ( cod_info->block_type == SHORT_TYPE ) { row_in_table = 1; partition_table = &nr_of_sfb_block[table_number][row_in_table][0]; for ( sfb = 0, partition = 0; partition < 4; partition++ ) { nr_sfb = partition_table[ partition ] / 3; for ( i = 0; i < nr_sfb; i++, sfb++ ) for ( window = 0; window < 3; window++ ) if ( scalefac[sfb*3+window] > max_sfac[partition] ) max_sfac[partition] = scalefac[sfb*3+window]; } } else { row_in_table = 0; partition_table = &nr_of_sfb_block[table_number][row_in_table][0]; for ( sfb = 0, partition = 0; partition < 4; partition++ ) { nr_sfb = partition_table[ partition ]; for ( i = 0; i < nr_sfb; i++, sfb++ ) if ( scalefac[sfb] > max_sfac[partition] ) max_sfac[partition] = scalefac[sfb]; } } for ( over = 0, partition = 0; partition < 4; partition++ ) { if ( max_sfac[partition] > max_range_sfac_tab[table_number][partition] ) over++; } if ( !over ) { /* Since no bands have been over-amplified, we can set scalefac_compress and slen[] for the formatter */ static const int log2tab[] = { 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4 }; int slen1, slen2, slen3, slen4; cod_info->sfb_partition_table = nr_of_sfb_block[table_number][row_in_table]; for ( partition = 0; partition < 4; partition++ ) cod_info->slen[partition] = log2tab[max_sfac[partition]]; /* set scalefac_compress */ slen1 = cod_info->slen[ 0 ]; slen2 = cod_info->slen[ 1 ]; slen3 = cod_info->slen[ 2 ]; slen4 = cod_info->slen[ 3 ]; switch ( table_number ) { case 0: cod_info->scalefac_compress = (((slen1 * 5) + slen2) << 4) + (slen3 << 2) + slen4; break; case 1: cod_info->scalefac_compress = 400 + (((slen1 * 5) + slen2) << 2) + slen3; break; case 2: cod_info->scalefac_compress = 500 + (slen1 * 3) + slen2; break; default: ERRORF(gfc,"intensity stereo not implemented yet\n" ); break; } }#ifdef DEBUG if ( over ) ERRORF(gfc, "---WARNING !! Amplification of some bands over limits\n" );#endif if (!over) { assert( cod_info->sfb_partition_table ); cod_info->part2_length=0; for ( partition = 0; partition < 4; partition++ ) cod_info->part2_length += cod_info->slen[partition] * cod_info->sfb_partition_table[partition]; } return over;}void huffman_init(lame_internal_flags * const gfc){ int i; gfc->choose_table = choose_table_nonMMX; #ifdef MMX_choose_table if (gfc->CPU_features.MMX) { extern int choose_table_MMX(const int *ix, const int * const end, int * const s); gfc->choose_table = choose_table_MMX; }#endif for (i = 2; i <= 576; i += 2) { int scfb_anz = 0, index; while (gfc->scalefac_band.l[++scfb_anz] < i) ; index = subdv_table[scfb_anz].region0_count; while (gfc->scalefac_band.l[index + 1] > i) index--; if (index < 0) { /* this is an indication that everything is going to be encoded as region0: bigvalues < region0 < region1 so lets set region0, region1 to some value larger than bigvalues */ index = subdv_table[scfb_anz].region0_count; } gfc->bv_scf[i-2] = index; index = subdv_table[scfb_anz].region1_count; while (gfc->scalefac_band.l[index + gfc->bv_scf[i-2] + 2] > i) index--; if (index < 0) { index = subdv_table[scfb_anz].region1_count; } gfc->bv_scf[i-1] = index; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -