diff --git a/libavcodec/mpegaudioenc.c b/libavcodec/mpegaudioenc.c index b660acab4f..005401946e 100644 --- a/libavcodec/mpegaudioenc.c +++ b/libavcodec/mpegaudioenc.c @@ -64,6 +64,16 @@ typedef struct MpegAudioContext { unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT]; int sblimit; /* number of used subbands */ const unsigned char *alloc_table; + int16_t filter_bank[512]; + int scale_factor_table[64]; + unsigned char scale_diff_table[128]; +#ifdef USE_FLOATS + float scale_factor_inv_table[64]; +#else + int8_t scale_factor_shift[64]; + unsigned short scale_factor_mult[64]; +#endif + unsigned short total_quant_bits[17]; /* total number of bits per allocation group */ } MpegAudioContext; static av_cold int MPA_encode_init(AVCodecContext *avctx) @@ -139,24 +149,24 @@ static av_cold int MPA_encode_init(AVCodecContext *avctx) #if WFRAC_BITS != 16 v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS); #endif - filter_bank[i] = v; + s->filter_bank[i] = v; if ((i & 63) != 0) v = -v; if (i != 0) - filter_bank[512 - i] = v; + s->filter_bank[512 - i] = v; } for(i=0;i<64;i++) { v = (int)(exp2((3 - i) / 3.0) * (1 << 20)); if (v <= 0) v = 1; - scale_factor_table[i] = v; + s->scale_factor_table[i] = v; #ifdef USE_FLOATS - scale_factor_inv_table[i] = exp2(-(3 - i) / 3.0) / (float)(1 << 20); + s->scale_factor_inv_table[i] = exp2(-(3 - i) / 3.0) / (float)(1 << 20); #else #define P 15 - scale_factor_shift[i] = 21 - P - (i / 3); - scale_factor_mult[i] = (1 << P) * exp2((i % 3) / 3.0); + s->scale_factor_shift[i] = 21 - P - (i / 3); + s->scale_factor_mult[i] = (1 << P) * exp2((i % 3) / 3.0); #endif } for(i=0;i<128;i++) { @@ -171,7 +181,7 @@ static av_cold int MPA_encode_init(AVCodecContext *avctx) v = 3; else v = 4; - scale_diff_table[i] = v; + s->scale_diff_table[i] = v; } for(i=0;i<17;i++) { @@ -180,7 +190,7 @@ static av_cold int MPA_encode_init(AVCodecContext *avctx) v = -v; else v = v * 3; - total_quant_bits[i] = 12 * v; + s->total_quant_bits[i] = 12 * v; } return 0; @@ -327,7 +337,7 @@ static void filter(MpegAudioContext *s, int ch, const short *samples, int incr) /* filter */ p = s->samples_buf[ch] + offset; - q = filter_bank; + q = s->filter_bank; /* maxsum = 23169 */ for(i=0;i<64;i++) { sum = p[0*64] * q[0*64]; @@ -361,7 +371,8 @@ static void filter(MpegAudioContext *s, int ch, const short *samples, int incr) s->samples_offset[ch] = offset; } -static void compute_scale_factors(unsigned char scale_code[SBLIMIT], +static void compute_scale_factors(MpegAudioContext *s, + unsigned char scale_code[SBLIMIT], unsigned char scale_factors[SBLIMIT][3], int sb_samples[3][12][SBLIMIT], int sblimit) @@ -388,7 +399,7 @@ static void compute_scale_factors(unsigned char scale_code[SBLIMIT], use at most 2 compares to find the index */ index = (21 - n) * 3 - 3; if (index >= 0) { - while (vmax <= scale_factor_table[index+1]) + while (vmax <= s->scale_factor_table[index+1]) index++; } else { index = 0; /* very unlikely case of overflow */ @@ -398,7 +409,7 @@ static void compute_scale_factors(unsigned char scale_code[SBLIMIT], } av_dlog(NULL, "%2d:%d in=%x %x %d\n", - j, i, vmax, scale_factor_table[index], index); + j, i, vmax, s->scale_factor_table[index], index); /* store the scale factor */ av_assert2(index >=0 && index <= 63); sf[i] = index; @@ -406,8 +417,8 @@ static void compute_scale_factors(unsigned char scale_code[SBLIMIT], /* compute the transmission factor : look if the scale factors are close enough to each other */ - d1 = scale_diff_table[sf[0] - sf[1] + 64]; - d2 = scale_diff_table[sf[1] - sf[2] + 64]; + d1 = s->scale_diff_table[sf[0] - sf[1] + 64]; + d2 = s->scale_diff_table[sf[1] - sf[2] + 64]; /* handle the 25 cases */ switch(d1 * 5 + d2) { @@ -557,12 +568,12 @@ static void compute_bit_allocation(MpegAudioContext *s, if (subband_status[max_ch][max_sb] == SB_NOTALLOCATED) { /* nothing was coded for this band: add the necessary bits */ incr = 2 + nb_scale_factors[s->scale_code[max_ch][max_sb]] * 6; - incr += total_quant_bits[alloc[1]]; + incr += s->total_quant_bits[alloc[1]]; } else { /* increments bit allocation */ b = bit_alloc[max_ch][max_sb]; - incr = total_quant_bits[alloc[b + 1]] - - total_quant_bits[alloc[b]]; + incr = s->total_quant_bits[alloc[b + 1]] - + s->total_quant_bits[alloc[b]]; } if (current_frame_size + incr <= max_frame_size) { @@ -676,15 +687,15 @@ static void encode_frame(MpegAudioContext *s, #ifdef USE_FLOATS { float a; - a = (float)sample * scale_factor_inv_table[s->scale_factors[ch][i][k]]; + a = (float)sample * s->scale_factor_inv_table[s->scale_factors[ch][i][k]]; q[m] = (int)((a + 1.0) * steps * 0.5); } #else { int q1, e, shift, mult; e = s->scale_factors[ch][i][k]; - shift = scale_factor_shift[e]; - mult = scale_factor_mult[e]; + shift = s->scale_factor_shift[e]; + mult = s->scale_factor_mult[e]; /* normalize to P bits */ if (shift < 0) @@ -739,7 +750,7 @@ static int MPA_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, } for(i=0;inb_channels;i++) { - compute_scale_factors(s->scale_code[i], s->scale_factors[i], + compute_scale_factors(s, s->scale_code[i], s->scale_factors[i], s->sb_samples[i], s->sblimit); } for(i=0;inb_channels;i++) { diff --git a/libavcodec/mpegaudiotab.h b/libavcodec/mpegaudiotab.h index 35129e646c..42d42d883d 100644 --- a/libavcodec/mpegaudiotab.h +++ b/libavcodec/mpegaudiotab.h @@ -79,20 +79,6 @@ static const int bitinv32[32] = { }; -static int16_t filter_bank[512]; - -static int scale_factor_table[64]; -#ifdef USE_FLOATS -static float scale_factor_inv_table[64]; -#else -static int8_t scale_factor_shift[64]; -static unsigned short scale_factor_mult[64]; -#endif -static unsigned char scale_diff_table[128]; - -/* total number of bits per allocation group */ -static unsigned short total_quant_bits[17]; - /* signal to noise ratio of each quantification step (could be computed from quant_steps[]). The values are dB multiplied by 10 */