46 #define MUL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS)
48 #define SAMPLES_BUF_SIZE 4096
73 int8_t scale_factor_shift[64];
88 if (channels <= 0 || channels > 2){
89 av_log(avctx,
AV_LOG_ERROR,
"encoding %d channel(s) is not allowed in mp2\n", channels);
92 bitrate = bitrate / 1000;
95 avctx->
delay = 512 - 32 + 1;
140 av_dlog(avctx,
"%d kb/s, %d Hz, frame_size=%d bits, table=%d, padincr=%x\n",
160 v = (int)(
exp2((3 - i) / 3.0) * (1 << 20));
168 s->scale_factor_shift[i] = 21 - P - (i / 3);
169 s->scale_factor_mult[i] = (1 << P) *
exp2((i % 3) / 3.0);
206 for(j=31;j>=3;j-=2) tab[j] += tab[j - 2];
250 x1 =
MUL((t[8] - x2), xp[0]);
251 x2 =
MUL((t[8] + x2), xp[1]);
264 xr =
MUL(t[28],xp[0]);
268 xr =
MUL(t[4],xp[1]);
269 t[ 4] = (t[24] - xr);
270 t[24] = (t[24] + xr);
272 xr =
MUL(t[20],xp[2]);
276 xr =
MUL(t[12],xp[3]);
277 t[12] = (t[16] - xr);
278 t[16] = (t[16] + xr);
283 for (i = 0; i < 4; i++) {
284 xr =
MUL(tab[30-i*4],xp[0]);
285 tab[30-i*4] = (tab[i*4] - xr);
286 tab[ i*4] = (tab[i*4] + xr);
288 xr =
MUL(tab[ 2+i*4],xp[1]);
289 tab[ 2+i*4] = (tab[28-i*4] - xr);
290 tab[28-i*4] = (tab[28-i*4] + xr);
292 xr =
MUL(tab[31-i*4],xp[0]);
293 tab[31-i*4] = (tab[1+i*4] - xr);
294 tab[ 1+i*4] = (tab[1+i*4] + xr);
296 xr =
MUL(tab[ 3+i*4],xp[1]);
297 tab[ 3+i*4] = (tab[29-i*4] - xr);
298 tab[29-i*4] = (tab[29-i*4] + xr);
306 xr =
MUL(t1[0], *xp);
319 #define WSHIFT (WFRAC_BITS + 15 - FRAC_BITS)
334 s->
samples_buf[ch][offset + (31 - i)] = samples[0];
343 sum = p[0*64] * q[0*64];
344 sum += p[1*64] * q[1*64];
345 sum += p[2*64] * q[2*64];
346 sum += p[3*64] * q[3*64];
347 sum += p[4*64] * q[4*64];
348 sum += p[5*64] * q[5*64];
349 sum += p[6*64] * q[6*64];
350 sum += p[7*64] * q[7*64];
355 tmp1[0] = tmp[16] >>
WSHIFT;
356 for( i=1; i<=16; i++ ) tmp1[i] = (tmp[i+16]+tmp[16-i]) >>
WSHIFT;
357 for( i=17; i<=31; i++ ) tmp1[i] = (tmp[i+16]-tmp[80-i]) >>
WSHIFT;
375 unsigned char scale_code[
SBLIMIT],
376 unsigned char scale_factors[SBLIMIT][3],
377 int sb_samples[3][12][SBLIMIT],
380 int *p, vmax,
v,
n, i, j, k, code;
382 unsigned char *sf = &scale_factors[0][0];
384 for(j=0;j<sblimit;j++) {
387 p = &sb_samples[i][0][j];
400 index = (21 -
n) * 3 - 3;
402 while (vmax <= s->scale_factor_table[index+1])
411 av_dlog(NULL,
"%2d:%d in=%x %x %d\n",
424 switch(d1 * 5 + d2) {
456 sf[1] = sf[2] = sf[0];
461 sf[0] = sf[1] = sf[2];
467 sf[0] = sf[2] = sf[1];
473 sf[1] = sf[2] = sf[0];
480 av_dlog(NULL,
"%d: %2d %2d %2d %d %d -> %d\n", j,
481 sf[0], sf[1], sf[2], d1, d2, code);
482 scale_code[j] = code;
500 #define SB_NOTALLOCATED 0
501 #define SB_ALLOCATED 1
512 int i, ch,
b, max_smr, max_ch, max_sb, current_frame_size, max_frame_size;
516 const unsigned char *alloc;
518 memcpy(smr, smr1, s->
nb_channels *
sizeof(
short) * SBLIMIT);
534 current_frame_size = 32;
548 if (smr[ch][i] > max_smr && subband_status[ch][i] !=
SB_NOMORE) {
549 max_smr = smr[ch][i];
557 av_dlog(NULL,
"current=%d max=%d max_sb=%d max_ch=%d alloc=%d\n",
558 current_frame_size, max_frame_size, max_sb, max_ch,
564 for(i=0;i<max_sb;i++) {
565 alloc += 1 << alloc[0];
579 if (current_frame_size + incr <= max_frame_size) {
582 current_frame_size += incr;
584 smr[max_ch][max_sb] = smr1[max_ch][max_sb] -
quant_snr[alloc[
b]];
586 if (b == ((1 << alloc[0]) - 1))
587 subband_status[max_ch][max_sb] =
SB_NOMORE;
592 subband_status[max_ch][max_sb] =
SB_NOMORE;
595 *padding = max_frame_size - current_frame_size;
607 int i, j, k, l, bit_alloc_bits,
b, ch;
635 j += 1 << bit_alloc_bits;
691 q[m] = (
int)((a + 1.0) * steps * 0.5);
697 shift = s->scale_factor_shift[e];
698 mult = s->scale_factor_mult[e];
702 q1 = sample << (-
shift);
704 q1 = sample >>
shift;
705 q1 = (q1 *
mult) >> P;
706 q[
m] = ((q1 + (1 << P)) * steps) >> (P + 1);
719 q[0] + steps * (q[1] + steps * q[2]));
728 j += 1 << bit_alloc_bits;
734 for(i=0;i<padding;i++)
745 const int16_t *samples = (
const int16_t *)frame->
data[0];
793 .supported_samplerates = (
const int[]){
794 44100, 48000, 32000, 22050, 24000, 16000, 0