- v = v * 3;
- total_quant_bits[i] = 12 * v;
- }
-
- avctx->coded_frame= avcodec_alloc_frame();
- avctx->coded_frame->key_frame= 1;
-
- return 0;
-}
-
-/* 32 point floating point IDCT without 1/sqrt(2) coef zero scaling */
-static void idct32(int *out, int *tab)
-{
- int i, j;
- int *t, *t1, xr;
- const int *xp = costab32;
-
- for(j=31;j>=3;j-=2) tab[j] += tab[j - 2];
-
- t = tab + 30;
- t1 = tab + 2;
- do {
- t[0] += t[-4];
- t[1] += t[1 - 4];
- t -= 4;
- } while (t != t1);
-
- t = tab + 28;
- t1 = tab + 4;
- do {
- t[0] += t[-8];
- t[1] += t[1-8];
- t[2] += t[2-8];
- t[3] += t[3-8];
- t -= 8;
- } while (t != t1);
-
- t = tab;
- t1 = tab + 32;
- do {
- t[ 3] = -t[ 3];
- t[ 6] = -t[ 6];
-
- t[11] = -t[11];
- t[12] = -t[12];
- t[13] = -t[13];
- t[15] = -t[15];
- t += 16;
- } while (t != t1);
-
-
- t = tab;
- t1 = tab + 8;
- do {
- int x1, x2, x3, x4;
-
- x3 = MUL(t[16], FIX(SQRT2*0.5));
- x4 = t[0] - x3;
- x3 = t[0] + x3;
-
- x2 = MUL(-(t[24] + t[8]), FIX(SQRT2*0.5));
- x1 = MUL((t[8] - x2), xp[0]);
- x2 = MUL((t[8] + x2), xp[1]);
-
- t[ 0] = x3 + x1;
- t[ 8] = x4 - x2;
- t[16] = x4 + x2;
- t[24] = x3 - x1;
- t++;
- } while (t != t1);
-
- xp += 2;
- t = tab;
- t1 = tab + 4;
- do {
- xr = MUL(t[28],xp[0]);
- t[28] = (t[0] - xr);
- t[0] = (t[0] + xr);
-
- xr = MUL(t[4],xp[1]);
- t[ 4] = (t[24] - xr);
- t[24] = (t[24] + xr);
-
- xr = MUL(t[20],xp[2]);
- t[20] = (t[8] - xr);
- t[ 8] = (t[8] + xr);
-
- xr = MUL(t[12],xp[3]);
- t[12] = (t[16] - xr);
- t[16] = (t[16] + xr);
- t++;
- } while (t != t1);
- xp += 4;
-
- for (i = 0; i < 4; i++) {
- xr = MUL(tab[30-i*4],xp[0]);
- tab[30-i*4] = (tab[i*4] - xr);
- tab[ i*4] = (tab[i*4] + xr);
-
- xr = MUL(tab[ 2+i*4],xp[1]);
- tab[ 2+i*4] = (tab[28-i*4] - xr);
- tab[28-i*4] = (tab[28-i*4] + xr);
-
- xr = MUL(tab[31-i*4],xp[0]);
- tab[31-i*4] = (tab[1+i*4] - xr);
- tab[ 1+i*4] = (tab[1+i*4] + xr);
-
- xr = MUL(tab[ 3+i*4],xp[1]);
- tab[ 3+i*4] = (tab[29-i*4] - xr);
- tab[29-i*4] = (tab[29-i*4] + xr);
-
- xp += 2;
- }
-
- t = tab + 30;
- t1 = tab + 1;
- do {
- xr = MUL(t1[0], *xp);
- t1[0] = (t[0] - xr);
- t[0] = (t[0] + xr);
- t -= 2;
- t1 += 2;
- xp++;
- } while (t >= tab);
-
- for(i=0;i<32;i++) {
- out[i] = tab[bitinv32[i]];
- }
-}
-
-#define WSHIFT (WFRAC_BITS + 15 - FRAC_BITS)
-
-static void filter(MpegAudioContext *s, int ch, short *samples, int incr)
-{
- short *p, *q;
- int sum, offset, i, j;
- int tmp[64];
- int tmp1[32];
- int *out;
-
- // print_pow1(samples, 1152);
-
- offset = s->samples_offset[ch];
- out = &s->sb_samples[ch][0][0][0];
- for(j=0;j<36;j++) {
- /* 32 samples at once */
- for(i=0;i<32;i++) {
- s->samples_buf[ch][offset + (31 - i)] = samples[0];
- samples += incr;
- }
-
- /* filter */
- p = s->samples_buf[ch] + offset;
- q = filter_bank;
- /* maxsum = 23169 */
- for(i=0;i<64;i++) {
- sum = p[0*64] * q[0*64];
- sum += p[1*64] * q[1*64];
- sum += p[2*64] * q[2*64];
- sum += p[3*64] * q[3*64];
- sum += p[4*64] * q[4*64];
- sum += p[5*64] * q[5*64];
- sum += p[6*64] * q[6*64];
- sum += p[7*64] * q[7*64];
- tmp[i] = sum;
- p++;
- q++;
- }
- tmp1[0] = tmp[16] >> WSHIFT;
- for( i=1; i<=16; i++ ) tmp1[i] = (tmp[i+16]+tmp[16-i]) >> WSHIFT;
- for( i=17; i<=31; i++ ) tmp1[i] = (tmp[i+16]-tmp[80-i]) >> WSHIFT;
-
- idct32(out, tmp1);
-
- /* advance of 32 samples */
- offset -= 32;
- out += 32;
- /* handle the wrap around */
- if (offset < 0) {
- memmove(s->samples_buf[ch] + SAMPLES_BUF_SIZE - (512 - 32),
- s->samples_buf[ch], (512 - 32) * 2);
- offset = SAMPLES_BUF_SIZE - 512;
- }
- }
- s->samples_offset[ch] = offset;
-
- // print_pow(s->sb_samples, 1152);
-}
-
-static void compute_scale_factors(unsigned char scale_code[SBLIMIT],
- unsigned char scale_factors[SBLIMIT][3],
- int sb_samples[3][12][SBLIMIT],
- int sblimit)
-{
- int *p, vmax, v, n, i, j, k, code;
- int index, d1, d2;
- unsigned char *sf = &scale_factors[0][0];
-
- for(j=0;j<sblimit;j++) {
- for(i=0;i<3;i++) {
- /* find the max absolute value */
- p = &sb_samples[i][0][j];
- vmax = abs(*p);
- for(k=1;k<12;k++) {
- p += SBLIMIT;
- v = abs(*p);
- if (v > vmax)
- vmax = v;
- }
- /* compute the scale factor index using log 2 computations */
- if (vmax > 0) {
- n = av_log2(vmax);
- /* n is the position of the MSB of vmax. now
- use at most 2 compares to find the index */
- index = (21 - n) * 3 - 3;
- if (index >= 0) {
- while (vmax <= scale_factor_table[index+1])
- index++;
- } else {
- index = 0; /* very unlikely case of overflow */
- }
- } else {
- index = 62; /* value 63 is not allowed */
- }
-
-#if 0
- printf("%2d:%d in=%x %x %d\n",
- j, i, vmax, scale_factor_table[index], index);
-#endif
- /* store the scale factor */
- assert(index >=0 && index <= 63);
- sf[i] = index;
- }
-
- /* 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];
-
- /* handle the 25 cases */
- switch(d1 * 5 + d2) {
- case 0*5+0:
- case 0*5+4:
- case 3*5+4:
- case 4*5+0:
- case 4*5+4:
- code = 0;
- break;
- case 0*5+1:
- case 0*5+2:
- case 4*5+1:
- case 4*5+2:
- code = 3;
- sf[2] = sf[1];
- break;
- case 0*5+3:
- case 4*5+3:
- code = 3;
- sf[1] = sf[2];
- break;
- case 1*5+0:
- case 1*5+4:
- case 2*5+4:
- code = 1;
- sf[1] = sf[0];
- break;
- case 1*5+1:
- case 1*5+2:
- case 2*5+0:
- case 2*5+1:
- case 2*5+2:
- code = 2;
- sf[1] = sf[2] = sf[0];
- break;
- case 2*5+3:
- case 3*5+3:
- code = 2;
- sf[0] = sf[1] = sf[2];
- break;
- case 3*5+0:
- case 3*5+1:
- case 3*5+2:
- code = 2;
- sf[0] = sf[2] = sf[1];
- break;
- case 1*5+3:
- code = 2;
- if (sf[0] > sf[2])
- sf[0] = sf[2];
- sf[1] = sf[2] = sf[0];
- break;
- default:
- assert(0); //cant happen
- }
-
-#if 0
- printf("%d: %2d %2d %2d %d %d -> %d\n", j,
- sf[0], sf[1], sf[2], d1, d2, code);
-#endif
- scale_code[j] = code;
- sf += 3;
- }
-}
-
-/* The most important function : psycho acoustic module. In this
- encoder there is basically none, so this is the worst you can do,
- but also this is the simpler. */
-static void psycho_acoustic_model(MpegAudioContext *s, short smr[SBLIMIT])
-{
- int i;
-
- for(i=0;i<s->sblimit;i++) {
- smr[i] = (int)(fixed_smr[i] * 10);
- }
-}
-
-
-#define SB_NOTALLOCATED 0
-#define SB_ALLOCATED 1
-#define SB_NOMORE 2
-
-/* Try to maximize the smr while using a number of bits inferior to
- the frame size. I tried to make the code simpler, faster and
- smaller than other encoders :-) */
-static void compute_bit_allocation(MpegAudioContext *s,
- short smr1[MPA_MAX_CHANNELS][SBLIMIT],
- unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT],
- int *padding)
-{
- int i, ch, b, max_smr, max_ch, max_sb, current_frame_size, max_frame_size;
- int incr;
- short smr[MPA_MAX_CHANNELS][SBLIMIT];
- unsigned char subband_status[MPA_MAX_CHANNELS][SBLIMIT];
- const unsigned char *alloc;
-
- memcpy(smr, smr1, s->nb_channels * sizeof(short) * SBLIMIT);
- memset(subband_status, SB_NOTALLOCATED, s->nb_channels * SBLIMIT);
- memset(bit_alloc, 0, s->nb_channels * SBLIMIT);
-
- /* compute frame size and padding */
- max_frame_size = s->frame_size;
- s->frame_frac += s->frame_frac_incr;
- if (s->frame_frac >= 65536) {
- s->frame_frac -= 65536;
- s->do_padding = 1;
- max_frame_size += 8;