comparison ac3enc.c @ 2967:ef2149182f1c libavcodec

COSMETICS: Remove all trailing whitespace.
author diego
date Sat, 17 Dec 2005 18:14:38 +0000
parents 6ff5dc0dbaf0
children bfabfdf9ce55
comparison
equal deleted inserted replaced
2966:564788471dd4 2967:ef2149182f1c
44 int lfe; 44 int lfe;
45 unsigned int bsmod; 45 unsigned int bsmod;
46 short last_samples[AC3_MAX_CHANNELS][256]; 46 short last_samples[AC3_MAX_CHANNELS][256];
47 unsigned int chbwcod[AC3_MAX_CHANNELS]; 47 unsigned int chbwcod[AC3_MAX_CHANNELS];
48 int nb_coefs[AC3_MAX_CHANNELS]; 48 int nb_coefs[AC3_MAX_CHANNELS];
49 49
50 /* bitrate allocation control */ 50 /* bitrate allocation control */
51 int sgaincod, sdecaycod, fdecaycod, dbkneecod, floorcod; 51 int sgaincod, sdecaycod, fdecaycod, dbkneecod, floorcod;
52 AC3BitAllocParameters bit_alloc; 52 AC3BitAllocParameters bit_alloc;
53 int csnroffst; 53 int csnroffst;
54 int fgaincod[AC3_MAX_CHANNELS]; 54 int fgaincod[AC3_MAX_CHANNELS];
55 int fsnroffst[AC3_MAX_CHANNELS]; 55 int fsnroffst[AC3_MAX_CHANNELS];
56 /* mantissa encoding */ 56 /* mantissa encoding */
72 { 72 {
73 int v; 73 int v;
74 v = (int)(a * (float)(1 << 15)); 74 v = (int)(a * (float)(1 << 15));
75 if (v < -32767) 75 if (v < -32767)
76 v = -32767; 76 v = -32767;
77 else if (v > 32767) 77 else if (v > 32767)
78 v = 32767; 78 v = 32767;
79 return v; 79 return v;
80 } 80 }
81 81
82 static inline int calc_lowcomp1(int a, int b0, int b1) 82 static inline int calc_lowcomp1(int a, int b0, int b1)
83 { 83 {
84 if ((b0 + 256) == b1) { 84 if ((b0 + 256) == b1) {
85 a = 384 ; 85 a = 384 ;
86 } else if (b0 > b1) { 86 } else if (b0 > b1) {
87 a = a - 64; 87 a = a - 64;
88 if (a < 0) a=0; 88 if (a < 0) a=0;
89 } 89 }
90 return a; 90 return a;
91 } 91 }
93 static inline int calc_lowcomp(int a, int b0, int b1, int bin) 93 static inline int calc_lowcomp(int a, int b0, int b1, int bin)
94 { 94 {
95 if (bin < 7) { 95 if (bin < 7) {
96 if ((b0 + 256) == b1) { 96 if ((b0 + 256) == b1) {
97 a = 384 ; 97 a = 384 ;
98 } else if (b0 > b1) { 98 } else if (b0 > b1) {
99 a = a - 64; 99 a = a - 64;
100 if (a < 0) a=0; 100 if (a < 0) a=0;
101 } 101 }
102 } else if (bin < 20) { 102 } else if (bin < 20) {
103 if ((b0 + 256) == b1) { 103 if ((b0 + 256) == b1) {
116 /* AC3 bit allocation. The algorithm is the one described in the AC3 116 /* AC3 bit allocation. The algorithm is the one described in the AC3
117 spec. */ 117 spec. */
118 void ac3_parametric_bit_allocation(AC3BitAllocParameters *s, uint8_t *bap, 118 void ac3_parametric_bit_allocation(AC3BitAllocParameters *s, uint8_t *bap,
119 int8_t *exp, int start, int end, 119 int8_t *exp, int start, int end,
120 int snroffset, int fgain, int is_lfe, 120 int snroffset, int fgain, int is_lfe,
121 int deltbae,int deltnseg, 121 int deltbae,int deltnseg,
122 uint8_t *deltoffst, uint8_t *deltlen, uint8_t *deltba) 122 uint8_t *deltoffst, uint8_t *deltlen, uint8_t *deltba)
123 { 123 {
124 int bin,i,j,k,end1,v,v1,bndstrt,bndend,lowcomp,begin; 124 int bin,i,j,k,end1,v,v1,bndstrt,bndend,lowcomp,begin;
125 int fastleak,slowleak,address,tmp; 125 int fastleak,slowleak,address,tmp;
126 int16_t psd[256]; /* scaled exponents */ 126 int16_t psd[256]; /* scaled exponents */
162 } while (end > bndtab[k]); 162 } while (end > bndtab[k]);
163 163
164 /* excitation function */ 164 /* excitation function */
165 bndstrt = masktab[start]; 165 bndstrt = masktab[start];
166 bndend = masktab[end-1] + 1; 166 bndend = masktab[end-1] + 1;
167 167
168 if (bndstrt == 0) { 168 if (bndstrt == 0) {
169 lowcomp = 0; 169 lowcomp = 0;
170 lowcomp = calc_lowcomp1(lowcomp, bndpsd[0], bndpsd[1]) ; 170 lowcomp = calc_lowcomp1(lowcomp, bndpsd[0], bndpsd[1]) ;
171 excite[0] = bndpsd[0] - fgain - lowcomp ; 171 excite[0] = bndpsd[0] - fgain - lowcomp ;
172 lowcomp = calc_lowcomp1(lowcomp, bndpsd[1], bndpsd[2]) ; 172 lowcomp = calc_lowcomp1(lowcomp, bndpsd[1], bndpsd[2]) ;
183 begin = bin + 1 ; 183 begin = bin + 1 ;
184 break ; 184 break ;
185 } 185 }
186 } 186 }
187 } 187 }
188 188
189 end1=bndend; 189 end1=bndend;
190 if (end1 > 22) end1=22; 190 if (end1 > 22) end1=22;
191 191
192 for (bin = begin; bin < end1; bin++) { 192 for (bin = begin; bin < end1; bin++) {
193 if (!(is_lfe && bin == 6)) 193 if (!(is_lfe && bin == 6))
194 lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin+1], bin) ; 194 lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin+1], bin) ;
195 195
196 fastleak -= s->fdecay ; 196 fastleak -= s->fdecay ;
197 v = bndpsd[bin] - fgain; 197 v = bndpsd[bin] - fgain;
198 if (fastleak < v) fastleak = v; 198 if (fastleak < v) fastleak = v;
199 199
200 slowleak -= s->sdecay ; 200 slowleak -= s->sdecay ;
201 v = bndpsd[bin] - s->sgain; 201 v = bndpsd[bin] - s->sgain;
202 if (slowleak < v) slowleak = v; 202 if (slowleak < v) slowleak = v;
203 203
204 v=fastleak - lowcomp; 204 v=fastleak - lowcomp;
205 if (slowleak > v) v=slowleak; 205 if (slowleak > v) v=slowleak;
206 206
207 excite[bin] = v; 207 excite[bin] = v;
208 } 208 }
209 begin = 22; 209 begin = 22;
210 } else { 210 } else {
211 /* coupling channel */ 211 /* coupling channel */
212 begin = bndstrt; 212 begin = bndstrt;
213 213
214 fastleak = (s->cplfleak << 8) + 768; 214 fastleak = (s->cplfleak << 8) + 768;
215 slowleak = (s->cplsleak << 8) + 768; 215 slowleak = (s->cplsleak << 8) + 768;
216 } 216 }
217 217
218 for (bin = begin; bin < bndend; bin++) { 218 for (bin = begin; bin < bndend; bin++) {
259 } 259 }
260 } 260 }
261 } 261 }
262 262
263 /* compute bit allocation */ 263 /* compute bit allocation */
264 264
265 i = start ; 265 i = start ;
266 j = masktab[start] ; 266 j = masktab[start] ;
267 do { 267 do {
268 v=mask[j]; 268 v=mask[j];
269 v -= snroffset ; 269 v -= snroffset ;
359 /* pass 0 */ 359 /* pass 0 */
360 360
361 p=&z[0]; 361 p=&z[0];
362 j=(np >> 1); 362 j=(np >> 1);
363 do { 363 do {
364 BF(p[0].re, p[0].im, p[1].re, p[1].im, 364 BF(p[0].re, p[0].im, p[1].re, p[1].im,
365 p[0].re, p[0].im, p[1].re, p[1].im); 365 p[0].re, p[0].im, p[1].re, p[1].im);
366 p+=2; 366 p+=2;
367 } while (--j != 0); 367 } while (--j != 0);
368 368
369 /* pass 1 */ 369 /* pass 1 */
370 370
371 p=&z[0]; 371 p=&z[0];
372 j=np >> 2; 372 j=np >> 2;
373 do { 373 do {
374 BF(p[0].re, p[0].im, p[2].re, p[2].im, 374 BF(p[0].re, p[0].im, p[2].re, p[2].im,
375 p[0].re, p[0].im, p[2].re, p[2].im); 375 p[0].re, p[0].im, p[2].re, p[2].im);
376 BF(p[1].re, p[1].im, p[3].re, p[3].im, 376 BF(p[1].re, p[1].im, p[3].re, p[3].im,
377 p[1].re, p[1].im, p[3].im, -p[3].re); 377 p[1].re, p[1].im, p[3].im, -p[3].re);
378 p+=4; 378 p+=4;
379 } while (--j != 0); 379 } while (--j != 0);
380 380
381 /* pass 2 .. ln-1 */ 381 /* pass 2 .. ln-1 */
388 q = z + nloops; 388 q = z + nloops;
389 for (j = 0; j < nblocks; ++j) { 389 for (j = 0; j < nblocks; ++j) {
390 390
391 BF(p->re, p->im, q->re, q->im, 391 BF(p->re, p->im, q->re, q->im,
392 p->re, p->im, q->re, q->im); 392 p->re, p->im, q->re, q->im);
393 393
394 p++; 394 p++;
395 q++; 395 q++;
396 for(l = nblocks; l < np2; l += nblocks) { 396 for(l = nblocks; l < np2; l += nblocks) {
397 CMUL(tmp_re, tmp_im, costab[l], -sintab[l], q->re, q->im); 397 CMUL(tmp_re, tmp_im, costab[l], -sintab[l], q->re, q->im);
398 BF(p->re, p->im, q->re, q->im, 398 BF(p->re, p->im, q->re, q->im,
410 410
411 /* do a 512 point mdct */ 411 /* do a 512 point mdct */
412 static void mdct512(int32_t *out, int16_t *in) 412 static void mdct512(int32_t *out, int16_t *in)
413 { 413 {
414 int i, re, im, re1, im1; 414 int i, re, im, re1, im1;
415 int16_t rot[N]; 415 int16_t rot[N];
416 IComplex x[N/4]; 416 IComplex x[N/4];
417 417
418 /* shift to simplify computations */ 418 /* shift to simplify computations */
419 for(i=0;i<N/4;i++) 419 for(i=0;i<N/4;i++)
420 rot[i] = -in[i + 3*N/4]; 420 rot[i] = -in[i + 3*N/4];
421 for(i=N/4;i<N;i++) 421 for(i=N/4;i<N;i++)
422 rot[i] = in[i - N/4]; 422 rot[i] = in[i - N/4];
423 423
424 /* pre rotation */ 424 /* pre rotation */
425 for(i=0;i<N/4;i++) { 425 for(i=0;i<N/4;i++) {
426 re = ((int)rot[2*i] - (int)rot[N-1-2*i]) >> 1; 426 re = ((int)rot[2*i] - (int)rot[N-1-2*i]) >> 1;
427 im = -((int)rot[N/2+2*i] - (int)rot[N/2-1-2*i]) >> 1; 427 im = -((int)rot[N/2+2*i] - (int)rot[N/2-1-2*i]) >> 1;
428 CMUL(x[i].re, x[i].im, re, im, -xcos1[i], xsin1[i]); 428 CMUL(x[i].re, x[i].im, re, im, -xcos1[i], xsin1[i]);
429 } 429 }
430 430
431 fft(x, MDCT_NBITS - 2); 431 fft(x, MDCT_NBITS - 2);
432 432
433 /* post rotation */ 433 /* post rotation */
434 for(i=0;i<N/4;i++) { 434 for(i=0;i<N/4;i++) {
435 re = x[i].re; 435 re = x[i].re;
436 im = x[i].im; 436 im = x[i].im;
437 CMUL(re1, im1, re, im, xsin1[i], xcos1[i]); 437 CMUL(re1, im1, re, im, xsin1[i], xcos1[i]);
455 uint8_t exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], 455 uint8_t exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
456 int ch, int is_lfe) 456 int ch, int is_lfe)
457 { 457 {
458 int i, j; 458 int i, j;
459 int exp_diff; 459 int exp_diff;
460 460
461 /* estimate if the exponent variation & decide if they should be 461 /* estimate if the exponent variation & decide if they should be
462 reused in the next frame */ 462 reused in the next frame */
463 exp_strategy[0][ch] = EXP_NEW; 463 exp_strategy[0][ch] = EXP_NEW;
464 for(i=1;i<NB_BLOCKS;i++) { 464 for(i=1;i<NB_BLOCKS;i++) {
465 exp_diff = calc_exp_diff(exp[i][ch], exp[i-1][ch], N/2); 465 exp_diff = calc_exp_diff(exp[i][ch], exp[i-1][ch], N/2);
466 #ifdef DEBUG 466 #ifdef DEBUG
467 av_log(NULL, AV_LOG_DEBUG, "exp_diff=%d\n", exp_diff); 467 av_log(NULL, AV_LOG_DEBUG, "exp_diff=%d\n", exp_diff);
468 #endif 468 #endif
469 if (exp_diff > EXP_DIFF_THRESHOLD) 469 if (exp_diff > EXP_DIFF_THRESHOLD)
470 exp_strategy[i][ch] = EXP_NEW; 470 exp_strategy[i][ch] = EXP_NEW;
471 else 471 else
505 for(i=0;i<n;i++) { 505 for(i=0;i<n;i++) {
506 if (exp1[i] < exp[i]) 506 if (exp1[i] < exp[i])
507 exp[i] = exp1[i]; 507 exp[i] = exp1[i];
508 } 508 }
509 } 509 }
510 510
511 /* update the exponents so that they are the ones the decoder will 511 /* update the exponents so that they are the ones the decoder will
512 decode. Return the number of bits used to code the exponents */ 512 decode. Return the number of bits used to code the exponents */
513 static int encode_exp(uint8_t encoded_exp[N/2], 513 static int encode_exp(uint8_t encoded_exp[N/2],
514 uint8_t exp[N/2], 514 uint8_t exp[N/2],
515 int nb_exps, 515 int nb_exps,
516 int exp_strategy) 516 int exp_strategy)
517 { 517 {
518 int group_size, nb_groups, i, j, k, exp_min; 518 int group_size, nb_groups, i, j, k, exp_min;
519 uint8_t exp1[N/2]; 519 uint8_t exp1[N/2];
564 for(j=0;j<group_size;j++) { 564 for(j=0;j<group_size;j++) {
565 encoded_exp[k+j] = exp1[i]; 565 encoded_exp[k+j] = exp1[i];
566 } 566 }
567 k += group_size; 567 k += group_size;
568 } 568 }
569 569
570 #if defined(DEBUG) 570 #if defined(DEBUG)
571 av_log(NULL, AV_LOG_DEBUG, "exponents: strategy=%d\n", exp_strategy); 571 av_log(NULL, AV_LOG_DEBUG, "exponents: strategy=%d\n", exp_strategy);
572 for(i=0;i<=nb_groups * group_size;i++) { 572 for(i=0;i<=nb_groups * group_size;i++) {
573 av_log(NULL, AV_LOG_DEBUG, "%d ", encoded_exp[i]); 573 av_log(NULL, AV_LOG_DEBUG, "%d ", encoded_exp[i]);
574 } 574 }
590 case 0: 590 case 0:
591 /* nothing */ 591 /* nothing */
592 break; 592 break;
593 case 1: 593 case 1:
594 /* 3 mantissa in 5 bits */ 594 /* 3 mantissa in 5 bits */
595 if (s->mant1_cnt == 0) 595 if (s->mant1_cnt == 0)
596 bits += 5; 596 bits += 5;
597 if (++s->mant1_cnt == 3) 597 if (++s->mant1_cnt == 3)
598 s->mant1_cnt = 0; 598 s->mant1_cnt = 0;
599 break; 599 break;
600 case 2: 600 case 2:
601 /* 3 mantissa in 7 bits */ 601 /* 3 mantissa in 7 bits */
602 if (s->mant2_cnt == 0) 602 if (s->mant2_cnt == 0)
603 bits += 7; 603 bits += 7;
604 if (++s->mant2_cnt == 3) 604 if (++s->mant2_cnt == 3)
605 s->mant2_cnt = 0; 605 s->mant2_cnt = 0;
606 break; 606 break;
607 case 3: 607 case 3:
609 break; 609 break;
610 case 4: 610 case 4:
611 /* 2 mantissa in 7 bits */ 611 /* 2 mantissa in 7 bits */
612 if (s->mant4_cnt == 0) 612 if (s->mant4_cnt == 0)
613 bits += 7; 613 bits += 7;
614 if (++s->mant4_cnt == 2) 614 if (++s->mant4_cnt == 2)
615 s->mant4_cnt = 0; 615 s->mant4_cnt = 0;
616 break; 616 break;
617 case 14: 617 case 14:
618 bits += 14; 618 bits += 14;
619 break; 619 break;
641 for(i=0;i<NB_BLOCKS;i++) { 641 for(i=0;i<NB_BLOCKS;i++) {
642 s->mant1_cnt = 0; 642 s->mant1_cnt = 0;
643 s->mant2_cnt = 0; 643 s->mant2_cnt = 0;
644 s->mant4_cnt = 0; 644 s->mant4_cnt = 0;
645 for(ch=0;ch<s->nb_all_channels;ch++) { 645 for(ch=0;ch<s->nb_all_channels;ch++) {
646 ac3_parametric_bit_allocation(&s->bit_alloc, 646 ac3_parametric_bit_allocation(&s->bit_alloc,
647 bap[i][ch], (int8_t *)encoded_exp[i][ch], 647 bap[i][ch], (int8_t *)encoded_exp[i][ch],
648 0, s->nb_coefs[ch], 648 0, s->nb_coefs[ch],
649 (((csnroffst-15) << 4) + 649 (((csnroffst-15) << 4) +
650 fsnroffst) << 2, 650 fsnroffst) << 2,
651 fgaintab[s->fgaincod[ch]], 651 fgaintab[s->fgaincod[ch]],
652 ch == s->lfe_channel, 652 ch == s->lfe_channel,
653 2, 0, NULL, NULL, NULL); 653 2, 0, NULL, NULL, NULL);
654 frame_bits += compute_mantissa_size(s, bap[i][ch], 654 frame_bits += compute_mantissa_size(s, bap[i][ch],
655 s->nb_coefs[ch]); 655 s->nb_coefs[ch]);
656 } 656 }
657 } 657 }
658 #if 0 658 #if 0
659 printf("csnr=%d fsnr=%d frame_bits=%d diff=%d\n", 659 printf("csnr=%d fsnr=%d frame_bits=%d diff=%d\n",
660 csnroffst, fsnroffst, frame_bits, 660 csnroffst, fsnroffst, frame_bits,
661 16 * s->frame_size - ((frame_bits + 7) & ~7)); 661 16 * s->frame_size - ((frame_bits + 7) & ~7));
662 #endif 662 #endif
663 return 16 * s->frame_size - frame_bits; 663 return 16 * s->frame_size - frame_bits;
664 } 664 }
665 665
680 s->sdecaycod = 2; 680 s->sdecaycod = 2;
681 s->fdecaycod = 1; 681 s->fdecaycod = 1;
682 s->sgaincod = 1; 682 s->sgaincod = 1;
683 s->dbkneecod = 2; 683 s->dbkneecod = 2;
684 s->floorcod = 4; 684 s->floorcod = 4;
685 for(ch=0;ch<s->nb_all_channels;ch++) 685 for(ch=0;ch<s->nb_all_channels;ch++)
686 s->fgaincod[ch] = 4; 686 s->fgaincod[ch] = 4;
687 687
688 /* compute real values */ 688 /* compute real values */
689 s->bit_alloc.fscod = s->fscod; 689 s->bit_alloc.fscod = s->fscod;
690 s->bit_alloc.halfratecod = s->halfratecod; 690 s->bit_alloc.halfratecod = s->halfratecod;
691 s->bit_alloc.sdecay = sdecaytab[s->sdecaycod] >> s->halfratecod; 691 s->bit_alloc.sdecay = sdecaytab[s->sdecaycod] >> s->halfratecod;
692 s->bit_alloc.fdecay = fdecaytab[s->fdecaycod] >> s->halfratecod; 692 s->bit_alloc.fdecay = fdecaytab[s->fdecaycod] >> s->halfratecod;
693 s->bit_alloc.sgain = sgaintab[s->sgaincod]; 693 s->bit_alloc.sgain = sgaintab[s->sgaincod];
694 s->bit_alloc.dbknee = dbkneetab[s->dbkneecod]; 694 s->bit_alloc.dbknee = dbkneetab[s->dbkneecod];
695 s->bit_alloc.floor = floortab[s->floorcod]; 695 s->bit_alloc.floor = floortab[s->floorcod];
696 696
697 /* header size */ 697 /* header size */
698 frame_bits += 65; 698 frame_bits += 65;
699 // if (s->acmod == 2) 699 // if (s->acmod == 2)
700 // frame_bits += 2; 700 // frame_bits += 2;
701 frame_bits += frame_bits_inc[s->acmod]; 701 frame_bits += frame_bits_inc[s->acmod];
733 733
734 /* now the big work begins : do the bit allocation. Modify the snr 734 /* now the big work begins : do the bit allocation. Modify the snr
735 offset until we can pack everything in the requested frame size */ 735 offset until we can pack everything in the requested frame size */
736 736
737 csnroffst = s->csnroffst; 737 csnroffst = s->csnroffst;
738 while (csnroffst >= 0 && 738 while (csnroffst >= 0 &&
739 bit_alloc(s, bap, encoded_exp, exp_strategy, frame_bits, csnroffst, 0) < 0) 739 bit_alloc(s, bap, encoded_exp, exp_strategy, frame_bits, csnroffst, 0) < 0)
740 csnroffst -= SNR_INC1; 740 csnroffst -= SNR_INC1;
741 if (csnroffst < 0) { 741 if (csnroffst < 0) {
742 av_log(NULL, AV_LOG_ERROR, "Yack, Error !!!\n"); 742 av_log(NULL, AV_LOG_ERROR, "Yack, Error !!!\n");
743 return -1; 743 return -1;
744 } 744 }
745 while ((csnroffst + SNR_INC1) <= 63 && 745 while ((csnroffst + SNR_INC1) <= 63 &&
746 bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits, 746 bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits,
747 csnroffst + SNR_INC1, 0) >= 0) { 747 csnroffst + SNR_INC1, 0) >= 0) {
748 csnroffst += SNR_INC1; 748 csnroffst += SNR_INC1;
749 memcpy(bap, bap1, sizeof(bap1)); 749 memcpy(bap, bap1, sizeof(bap1));
750 } 750 }
751 while ((csnroffst + 1) <= 63 && 751 while ((csnroffst + 1) <= 63 &&
752 bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits, csnroffst + 1, 0) >= 0) { 752 bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits, csnroffst + 1, 0) >= 0) {
753 csnroffst++; 753 csnroffst++;
754 memcpy(bap, bap1, sizeof(bap1)); 754 memcpy(bap, bap1, sizeof(bap1));
755 } 755 }
756 756
757 fsnroffst = 0; 757 fsnroffst = 0;
758 while ((fsnroffst + SNR_INC1) <= 15 && 758 while ((fsnroffst + SNR_INC1) <= 15 &&
759 bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits, 759 bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits,
760 csnroffst, fsnroffst + SNR_INC1) >= 0) { 760 csnroffst, fsnroffst + SNR_INC1) >= 0) {
761 fsnroffst += SNR_INC1; 761 fsnroffst += SNR_INC1;
762 memcpy(bap, bap1, sizeof(bap1)); 762 memcpy(bap, bap1, sizeof(bap1));
763 } 763 }
764 while ((fsnroffst + 1) <= 15 && 764 while ((fsnroffst + 1) <= 15 &&
765 bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits, 765 bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits,
766 csnroffst, fsnroffst + 1) >= 0) { 766 csnroffst, fsnroffst + 1) >= 0) {
767 fsnroffst++; 767 fsnroffst++;
768 memcpy(bap, bap1, sizeof(bap1)); 768 memcpy(bap, bap1, sizeof(bap1));
769 } 769 }
770 770
771 s->csnroffst = csnroffst; 771 s->csnroffst = csnroffst;
772 for(ch=0;ch<s->nb_all_channels;ch++) 772 for(ch=0;ch<s->nb_all_channels;ch++)
773 s->fsnroffst[ch] = fsnroffst; 773 s->fsnroffst[ch] = fsnroffst;
774 #if defined(DEBUG_BITALLOC) 774 #if defined(DEBUG_BITALLOC)
775 { 775 {
822 0x07, /* L C R SL SR */ 822 0x07, /* L C R SL SR */
823 0x07, /* L C R SL SR (+LFE) */ 823 0x07, /* L C R SL SR (+LFE) */
824 }; 824 };
825 825
826 avctx->frame_size = AC3_FRAME_SIZE; 826 avctx->frame_size = AC3_FRAME_SIZE;
827 827
828 /* number of channels */ 828 /* number of channels */
829 if (channels < 1 || channels > 6) 829 if (channels < 1 || channels > 6)
830 return -1; 830 return -1;
831 s->acmod = acmod_defs[channels - 1]; 831 s->acmod = acmod_defs[channels - 1];
832 s->lfe = (channels == 6) ? 1 : 0; 832 s->lfe = (channels == 6) ? 1 : 0;
834 s->nb_channels = channels > 5 ? 5 : channels; 834 s->nb_channels = channels > 5 ? 5 : channels;
835 s->lfe_channel = s->lfe ? 5 : -1; 835 s->lfe_channel = s->lfe ? 5 : -1;
836 836
837 /* frequency */ 837 /* frequency */
838 for(i=0;i<3;i++) { 838 for(i=0;i<3;i++) {
839 for(j=0;j<3;j++) 839 for(j=0;j<3;j++)
840 if ((ac3_freqs[j] >> i) == freq) 840 if ((ac3_freqs[j] >> i) == freq)
841 goto found; 841 goto found;
842 } 842 }
843 return -1; 843 return -1;
844 found: 844 found:
845 s->sample_rate = freq; 845 s->sample_rate = freq;
846 s->halfratecod = i; 846 s->halfratecod = i;
847 s->fscod = j; 847 s->fscod = j;
848 s->bsid = 8 + s->halfratecod; 848 s->bsid = 8 + s->halfratecod;
849 s->bsmod = 0; /* complete main audio service */ 849 s->bsmod = 0; /* complete main audio service */
859 s->bit_rate = bitrate; 859 s->bit_rate = bitrate;
860 s->frmsizecod = i << 1; 860 s->frmsizecod = i << 1;
861 s->frame_size_min = (bitrate * 1000 * AC3_FRAME_SIZE) / (freq * 16); 861 s->frame_size_min = (bitrate * 1000 * AC3_FRAME_SIZE) / (freq * 16);
862 /* for now we do not handle fractional sizes */ 862 /* for now we do not handle fractional sizes */
863 s->frame_size = s->frame_size_min; 863 s->frame_size = s->frame_size_min;
864 864
865 /* bit allocation init */ 865 /* bit allocation init */
866 for(ch=0;ch<s->nb_channels;ch++) { 866 for(ch=0;ch<s->nb_channels;ch++) {
867 /* bandwidth for each channel */ 867 /* bandwidth for each channel */
868 /* XXX: should compute the bandwidth according to the frame 868 /* XXX: should compute the bandwidth according to the frame
869 size, so that we avoid anoying high freq artefacts */ 869 size, so that we avoid anoying high freq artefacts */
885 xcos1[i] = fix15(-cos(alpha)); 885 xcos1[i] = fix15(-cos(alpha));
886 xsin1[i] = fix15(-sin(alpha)); 886 xsin1[i] = fix15(-sin(alpha));
887 } 887 }
888 888
889 ac3_crc_init(); 889 ac3_crc_init();
890 890
891 avctx->coded_frame= avcodec_alloc_frame(); 891 avctx->coded_frame= avcodec_alloc_frame();
892 avctx->coded_frame->key_frame= 1; 892 avctx->coded_frame->key_frame= 1;
893 893
894 return 0; 894 return 0;
895 } 895 }
977 int exp0, exp1; 977 int exp0, exp1;
978 int mant1_cnt, mant2_cnt, mant4_cnt; 978 int mant1_cnt, mant2_cnt, mant4_cnt;
979 uint16_t *qmant1_ptr, *qmant2_ptr, *qmant4_ptr; 979 uint16_t *qmant1_ptr, *qmant2_ptr, *qmant4_ptr;
980 int delta0, delta1, delta2; 980 int delta0, delta1, delta2;
981 981
982 for(ch=0;ch<s->nb_channels;ch++) 982 for(ch=0;ch<s->nb_channels;ch++)
983 put_bits(&s->pb, 1, 0); /* 512 point MDCT */ 983 put_bits(&s->pb, 1, 0); /* 512 point MDCT */
984 for(ch=0;ch<s->nb_channels;ch++) 984 for(ch=0;ch<s->nb_channels;ch++)
985 put_bits(&s->pb, 1, 1); /* no dither */ 985 put_bits(&s->pb, 1, 1); /* no dither */
986 put_bits(&s->pb, 1, 0); /* no dynamic range */ 986 put_bits(&s->pb, 1, 0); /* no dynamic range */
987 if (block_num == 0) { 987 if (block_num == 0) {
988 /* for block 0, even if no coupling, we must say it. This is a 988 /* for block 0, even if no coupling, we must say it. This is a
989 waste of bit :-) */ 989 waste of bit :-) */
996 if (s->acmod == 2) 996 if (s->acmod == 2)
997 { 997 {
998 if(block_num==0) 998 if(block_num==0)
999 { 999 {
1000 /* first block must define rematrixing (rematstr) */ 1000 /* first block must define rematrixing (rematstr) */
1001 put_bits(&s->pb, 1, 1); 1001 put_bits(&s->pb, 1, 1);
1002 1002
1003 /* dummy rematrixing rematflg(1:4)=0 */ 1003 /* dummy rematrixing rematflg(1:4)=0 */
1004 for (rbnd=0;rbnd<4;rbnd++) 1004 for (rbnd=0;rbnd<4;rbnd++)
1005 put_bits(&s->pb, 1, 0); 1005 put_bits(&s->pb, 1, 0);
1006 } 1006 }
1007 else 1007 else
1008 { 1008 {
1009 /* no matrixing (but should be used in the future) */ 1009 /* no matrixing (but should be used in the future) */
1010 put_bits(&s->pb, 1, 0); 1010 put_bits(&s->pb, 1, 0);
1011 } 1011 }
1012 } 1012 }
1013 1013
1014 #if defined(DEBUG) 1014 #if defined(DEBUG)
1015 { 1015 {
1016 static int count = 0; 1016 static int count = 0;
1017 av_log(NULL, AV_LOG_DEBUG, "Block #%d (%d)\n", block_num, count++); 1017 av_log(NULL, AV_LOG_DEBUG, "Block #%d (%d)\n", block_num, count++);
1018 } 1018 }
1019 #endif 1019 #endif
1020 /* exponent strategy */ 1020 /* exponent strategy */
1021 for(ch=0;ch<s->nb_channels;ch++) { 1021 for(ch=0;ch<s->nb_channels;ch++) {
1022 put_bits(&s->pb, 2, exp_strategy[ch]); 1022 put_bits(&s->pb, 2, exp_strategy[ch]);
1023 } 1023 }
1024 1024
1025 if (s->lfe) { 1025 if (s->lfe) {
1026 put_bits(&s->pb, 1, exp_strategy[s->lfe_channel]); 1026 put_bits(&s->pb, 1, exp_strategy[s->lfe_channel]);
1027 } 1027 }
1028 1028
1029 for(ch=0;ch<s->nb_channels;ch++) { 1029 for(ch=0;ch<s->nb_channels;ch++) {
1030 if (exp_strategy[ch] != EXP_REUSE) 1030 if (exp_strategy[ch] != EXP_REUSE)
1031 put_bits(&s->pb, 6, s->chbwcod[ch]); 1031 put_bits(&s->pb, 6, s->chbwcod[ch]);
1032 } 1032 }
1033 1033
1034 /* exponents */ 1034 /* exponents */
1035 for (ch = 0; ch < s->nb_all_channels; ch++) { 1035 for (ch = 0; ch < s->nb_all_channels; ch++) {
1036 switch(exp_strategy[ch]) { 1036 switch(exp_strategy[ch]) {
1037 case EXP_REUSE: 1037 case EXP_REUSE:
1038 continue; 1038 continue;
1097 for(ch=0;ch<s->nb_all_channels;ch++) { 1097 for(ch=0;ch<s->nb_all_channels;ch++) {
1098 put_bits(&s->pb, 4, s->fsnroffst[ch]); 1098 put_bits(&s->pb, 4, s->fsnroffst[ch]);
1099 put_bits(&s->pb, 3, s->fgaincod[ch]); 1099 put_bits(&s->pb, 3, s->fgaincod[ch]);
1100 } 1100 }
1101 } 1101 }
1102 1102
1103 put_bits(&s->pb, 1, 0); /* no delta bit allocation */ 1103 put_bits(&s->pb, 1, 0); /* no delta bit allocation */
1104 put_bits(&s->pb, 1, 0); /* no data to skip */ 1104 put_bits(&s->pb, 1, 0); /* no data to skip */
1105 1105
1106 /* mantissa encoding : we use two passes to handle the grouping. A 1106 /* mantissa encoding : we use two passes to handle the grouping. A
1107 one pass method may be faster, but it would necessitate to 1107 one pass method may be faster, but it would necessitate to
1198 } 1198 }
1199 1199
1200 /* second pass : output the values */ 1200 /* second pass : output the values */
1201 for (ch = 0; ch < s->nb_all_channels; ch++) { 1201 for (ch = 0; ch < s->nb_all_channels; ch++) {
1202 int b, q; 1202 int b, q;
1203 1203
1204 for(i=0;i<s->nb_coefs[ch];i++) { 1204 for(i=0;i<s->nb_coefs[ch];i++) {
1205 q = qmant[ch][i]; 1205 q = qmant[ch][i];
1206 b = bap[ch][i]; 1206 b = bap[ch][i];
1207 switch(b) { 1207 switch(b) {
1208 case 0: 1208 case 0:
1209 break; 1209 break;
1210 case 1: 1210 case 1:
1211 if (q != 128) 1211 if (q != 128)
1212 put_bits(&s->pb, 5, q); 1212 put_bits(&s->pb, 5, q);
1213 break; 1213 break;
1214 case 2: 1214 case 2:
1215 if (q != 128) 1215 if (q != 128)
1216 put_bits(&s->pb, 7, q); 1216 put_bits(&s->pb, 7, q);
1217 break; 1217 break;
1218 case 3: 1218 case 3:
1219 put_bits(&s->pb, 3, q); 1219 put_bits(&s->pb, 3, q);
1220 break; 1220 break;
1245 unsigned int c, n, k; 1245 unsigned int c, n, k;
1246 1246
1247 for(n=0;n<256;n++) { 1247 for(n=0;n<256;n++) {
1248 c = n << 8; 1248 c = n << 8;
1249 for (k = 0; k < 8; k++) { 1249 for (k = 0; k < 8; k++) {
1250 if (c & (1 << 15)) 1250 if (c & (1 << 15))
1251 c = ((c << 1) & 0xffff) ^ (CRC16_POLY & 0xffff); 1251 c = ((c << 1) & 0xffff) ^ (CRC16_POLY & 0xffff);
1252 else 1252 else
1253 c = c << 1; 1253 c = c << 1;
1254 } 1254 }
1255 crc_table[n] = c; 1255 crc_table[n] = c;
1336 frame = s->pb.buf; 1336 frame = s->pb.buf;
1337 n = 2 * s->frame_size - (pbBufPtr(&s->pb) - frame) - 2; 1337 n = 2 * s->frame_size - (pbBufPtr(&s->pb) - frame) - 2;
1338 assert(n >= 0); 1338 assert(n >= 0);
1339 if(n>0) 1339 if(n>0)
1340 memset(pbBufPtr(&s->pb), 0, n); 1340 memset(pbBufPtr(&s->pb), 0, n);
1341 1341
1342 /* Now we must compute both crcs : this is not so easy for crc1 1342 /* Now we must compute both crcs : this is not so easy for crc1
1343 because it is at the beginning of the data... */ 1343 because it is at the beginning of the data... */
1344 frame_size_58 = (frame_size >> 1) + (frame_size >> 3); 1344 frame_size_58 = (frame_size >> 1) + (frame_size >> 3);
1345 crc1 = ac3_crc(frame + 4, (2 * frame_size_58) - 4, 0); 1345 crc1 = ac3_crc(frame + 4, (2 * frame_size_58) - 4, 0);
1346 /* XXX: could precompute crc_inv */ 1346 /* XXX: could precompute crc_inv */
1347 crc_inv = pow_poly((CRC16_POLY >> 1), (16 * frame_size_58) - 16, CRC16_POLY); 1347 crc_inv = pow_poly((CRC16_POLY >> 1), (16 * frame_size_58) - 16, CRC16_POLY);
1348 crc1 = mul_poly(crc_inv, crc1, CRC16_POLY); 1348 crc1 = mul_poly(crc_inv, crc1, CRC16_POLY);
1349 frame[2] = crc1 >> 8; 1349 frame[2] = crc1 >> 8;
1350 frame[3] = crc1; 1350 frame[3] = crc1;
1351 1351
1352 crc2 = ac3_crc(frame + 2 * frame_size_58, (frame_size - frame_size_58) * 2 - 2, 0); 1352 crc2 = ac3_crc(frame + 2 * frame_size_58, (frame_size - frame_size_58) * 2 - 2, 0);
1353 frame[2*frame_size - 2] = crc2 >> 8; 1353 frame[2*frame_size - 2] = crc2 >> 8;
1354 frame[2*frame_size - 1] = crc2; 1354 frame[2*frame_size - 1] = crc2;
1355 1355
1356 // printf("n=%d frame_size=%d\n", n, frame_size); 1356 // printf("n=%d frame_size=%d\n", n, frame_size);
1384 sinc = s->nb_all_channels; 1384 sinc = s->nb_all_channels;
1385 sptr = samples + (sinc * (N/2) * i) + ch; 1385 sptr = samples + (sinc * (N/2) * i) + ch;
1386 for(j=0;j<N/2;j++) { 1386 for(j=0;j<N/2;j++) {
1387 v = *sptr; 1387 v = *sptr;
1388 input_samples[j + N/2] = v; 1388 input_samples[j + N/2] = v;
1389 s->last_samples[ch][j] = v; 1389 s->last_samples[ch][j] = v;
1390 sptr += sinc; 1390 sptr += sinc;
1391 } 1391 }
1392 1392
1393 /* apply the MDCT window */ 1393 /* apply the MDCT window */
1394 for(j=0;j<N/2;j++) { 1394 for(j=0;j<N/2;j++) {
1395 input_samples[j] = MUL16(input_samples[j], 1395 input_samples[j] = MUL16(input_samples[j],
1396 ac3_window[j]) >> 15; 1396 ac3_window[j]) >> 15;
1397 input_samples[N-j-1] = MUL16(input_samples[N-j-1], 1397 input_samples[N-j-1] = MUL16(input_samples[N-j-1],
1398 ac3_window[j]) >> 15; 1398 ac3_window[j]) >> 15;
1399 } 1399 }
1400 1400
1401 /* Normalize the samples to use the maximum available 1401 /* Normalize the samples to use the maximum available
1402 precision */ 1402 precision */
1403 v = 14 - log2_tab(input_samples, N); 1403 v = 14 - log2_tab(input_samples, N);
1404 if (v < 0) 1404 if (v < 0)
1405 v = 0; 1405 v = 0;
1406 exp_samples[i][ch] = v - 8; 1406 exp_samples[i][ch] = v - 8;
1407 lshift_tab(input_samples, N, v); 1407 lshift_tab(input_samples, N, v);
1408 1408
1409 /* do the MDCT */ 1409 /* do the MDCT */
1410 mdct512(mdct_coef[i][ch], input_samples); 1410 mdct512(mdct_coef[i][ch], input_samples);
1411 1411
1412 /* compute "exponents". We take into account the 1412 /* compute "exponents". We take into account the
1413 normalization there */ 1413 normalization there */
1414 for(j=0;j<N/2;j++) { 1414 for(j=0;j<N/2;j++) {
1415 int e; 1415 int e;
1416 v = abs(mdct_coef[i][ch][j]); 1416 v = abs(mdct_coef[i][ch][j]);
1424 } 1424 }
1425 } 1425 }
1426 exp[i][ch][j] = e; 1426 exp[i][ch][j] = e;
1427 } 1427 }
1428 } 1428 }
1429 1429
1430 compute_exp_strategy(exp_strategy, exp, ch, ch == s->lfe_channel); 1430 compute_exp_strategy(exp_strategy, exp, ch, ch == s->lfe_channel);
1431 1431
1432 /* compute the exponents as the decoder will see them. The 1432 /* compute the exponents as the decoder will see them. The
1433 EXP_REUSE case must be handled carefully : we select the 1433 EXP_REUSE case must be handled carefully : we select the
1434 min of the exponents */ 1434 min of the exponents */
1438 while (j < NB_BLOCKS && exp_strategy[j][ch] == EXP_REUSE) { 1438 while (j < NB_BLOCKS && exp_strategy[j][ch] == EXP_REUSE) {
1439 exponent_min(exp[i][ch], exp[j][ch], s->nb_coefs[ch]); 1439 exponent_min(exp[i][ch], exp[j][ch], s->nb_coefs[ch]);
1440 j++; 1440 j++;
1441 } 1441 }
1442 frame_bits += encode_exp(encoded_exp[i][ch], 1442 frame_bits += encode_exp(encoded_exp[i][ch],
1443 exp[i][ch], s->nb_coefs[ch], 1443 exp[i][ch], s->nb_coefs[ch],
1444 exp_strategy[i][ch]); 1444 exp_strategy[i][ch]);
1445 /* copy encoded exponents for reuse case */ 1445 /* copy encoded exponents for reuse case */
1446 for(k=i+1;k<j;k++) { 1446 for(k=i+1;k<j;k++) {
1447 memcpy(encoded_exp[k][ch], encoded_exp[i][ch], 1447 memcpy(encoded_exp[k][ch], encoded_exp[i][ch],
1448 s->nb_coefs[ch] * sizeof(uint8_t)); 1448 s->nb_coefs[ch] * sizeof(uint8_t));
1449 } 1449 }
1450 i = j; 1450 i = j;
1451 } 1451 }
1452 } 1452 }
1453 1453
1454 compute_bit_allocation(s, bap, encoded_exp, exp_strategy, frame_bits); 1454 compute_bit_allocation(s, bap, encoded_exp, exp_strategy, frame_bits);
1455 /* everything is known... let's output the frame */ 1455 /* everything is known... let's output the frame */
1456 output_frame_header(s, frame); 1456 output_frame_header(s, frame);
1457 1457
1458 for(i=0;i<NB_BLOCKS;i++) { 1458 for(i=0;i<NB_BLOCKS;i++) {
1459 output_audio_block(s, exp_strategy[i], encoded_exp[i], 1459 output_audio_block(s, exp_strategy[i], encoded_exp[i],
1460 bap[i], mdct_coef[i], exp_samples[i], i); 1460 bap[i], mdct_coef[i], exp_samples[i], i);
1461 } 1461 }
1462 return output_frame_end(s); 1462 return output_frame_end(s);
1463 } 1463 }
1464 1464
1496 for(n=0;n<FN;n++) { 1496 for(n=0;n<FN;n++) {
1497 a = -2 * M_PI * (n * k) / FN; 1497 a = -2 * M_PI * (n * k) / FN;
1498 sum_re += in1[n].re * cos(a) - in1[n].im * sin(a); 1498 sum_re += in1[n].re * cos(a) - in1[n].im * sin(a);
1499 sum_im += in1[n].re * sin(a) + in1[n].im * cos(a); 1499 sum_im += in1[n].re * sin(a) + in1[n].im * cos(a);
1500 } 1500 }
1501 printf("%3d: %6d,%6d %6.0f,%6.0f\n", 1501 printf("%3d: %6d,%6d %6.0f,%6.0f\n",
1502 k, in[k].re, in[k].im, sum_re / FN, sum_im / FN); 1502 k, in[k].re, in[k].im, sum_re / FN, sum_im / FN);
1503 } 1503 }
1504 } 1504 }
1505 1505
1506 void mdct_test(void) 1506 void mdct_test(void)
1507 { 1507 {
1516 input[i] = (random() % 65535 - 32767) * 9 / 10; 1516 input[i] = (random() % 65535 - 32767) * 9 / 10;
1517 input1[i] = input[i]; 1517 input1[i] = input[i];
1518 } 1518 }
1519 1519
1520 mdct512(output, input); 1520 mdct512(output, input);
1521 1521
1522 /* do it by hand */ 1522 /* do it by hand */
1523 for(k=0;k<N/2;k++) { 1523 for(k=0;k<N/2;k++) {
1524 s = 0; 1524 s = 0;
1525 for(n=0;n<N;n++) { 1525 for(n=0;n<N;n++) {
1526 a = (2*M_PI*(2*n+1+N/2)*(2*k+1) / (4 * N)); 1526 a = (2*M_PI*(2*n+1+N/2)*(2*k+1) / (4 * N));
1527 s += input1[n] * cos(a); 1527 s += input1[n] * cos(a);
1528 } 1528 }
1529 output1[k] = -2 * s / N; 1529 output1[k] = -2 * s / N;
1530 } 1530 }
1531 1531
1532 err = 0; 1532 err = 0;
1533 emax = 0; 1533 emax = 0;
1534 for(i=0;i<N/2;i++) { 1534 for(i=0;i<N/2;i++) {
1535 printf("%3d: %7d %7.0f\n", i, output[i], output1[i]); 1535 printf("%3d: %7d %7.0f\n", i, output[i], output1[i]);
1536 e = output[i] - output1[i]; 1536 e = output[i] - output1[i];
1545 { 1545 {
1546 AC3EncodeContext ctx; 1546 AC3EncodeContext ctx;
1547 unsigned char frame[AC3_MAX_CODED_FRAME_SIZE]; 1547 unsigned char frame[AC3_MAX_CODED_FRAME_SIZE];
1548 short samples[AC3_FRAME_SIZE]; 1548 short samples[AC3_FRAME_SIZE];
1549 int ret, i; 1549 int ret, i;
1550 1550
1551 AC3_encode_init(&ctx, 44100, 64000, 1); 1551 AC3_encode_init(&ctx, 44100, 64000, 1);
1552 1552
1553 fft_test(); 1553 fft_test();
1554 mdct_test(); 1554 mdct_test();
1555 1555