You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1491 lines
40KB

  1. /*
  2. * The simplest AC3 encoder
  3. * Copyright (c) 2000 Gerard Lantau.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. //#define DEBUG
  20. //#define DEBUG_BITALLOC
  21. #include "avcodec.h"
  22. #include "ac3enc.h"
  23. #include "ac3tab.h"
  24. #define MDCT_NBITS 9
  25. #define N (1 << MDCT_NBITS)
  26. #define NB_BLOCKS 6 /* number of PCM blocks inside an AC3 frame */
  27. /* new exponents are sent if their Norm 1 exceed this number */
  28. #define EXP_DIFF_THRESHOLD 1000
  29. /* exponent encoding strategy */
  30. #define EXP_REUSE 0
  31. #define EXP_NEW 1
  32. #define EXP_D15 1
  33. #define EXP_D25 2
  34. #define EXP_D45 3
  35. static void fft_init(int ln);
  36. static void ac3_crc_init(void);
  37. static inline INT16 fix15(float a)
  38. {
  39. int v;
  40. v = (int)(a * (float)(1 << 15));
  41. if (v < -32767)
  42. v = -32767;
  43. else if (v > 32767)
  44. v = 32767;
  45. return v;
  46. }
  47. static inline int calc_lowcomp1(int a, int b0, int b1)
  48. {
  49. if ((b0 + 256) == b1) {
  50. a = 384 ;
  51. } else if (b0 > b1) {
  52. a = a - 64;
  53. if (a < 0) a=0;
  54. }
  55. return a;
  56. }
  57. static inline int calc_lowcomp(int a, int b0, int b1, int bin)
  58. {
  59. if (bin < 7) {
  60. if ((b0 + 256) == b1) {
  61. a = 384 ;
  62. } else if (b0 > b1) {
  63. a = a - 64;
  64. if (a < 0) a=0;
  65. }
  66. } else if (bin < 20) {
  67. if ((b0 + 256) == b1) {
  68. a = 320 ;
  69. } else if (b0 > b1) {
  70. a= a - 64;
  71. if (a < 0) a=0;
  72. }
  73. } else {
  74. a = a - 128;
  75. if (a < 0) a=0;
  76. }
  77. return a;
  78. }
  79. /* AC3 bit allocation. The algorithm is the one described in the AC3
  80. spec with some optimizations because of our simplified encoding
  81. assumptions. */
  82. void parametric_bit_allocation(AC3EncodeContext *s, UINT8 *bap,
  83. INT8 *exp, int start, int end,
  84. int snroffset, int fgain, int is_lfe)
  85. {
  86. int bin,i,j,k,end1,v,v1,bndstrt,bndend,lowcomp,begin;
  87. int fastleak,slowleak,address,tmp;
  88. INT16 psd[256]; /* scaled exponents */
  89. INT16 bndpsd[50]; /* interpolated exponents */
  90. INT16 excite[50]; /* excitation */
  91. INT16 mask[50]; /* masking value */
  92. /* exponent mapping to PSD */
  93. for(bin=start;bin<end;bin++) {
  94. psd[bin]=(3072 - (exp[bin] << 7));
  95. }
  96. /* PSD integration */
  97. j=start;
  98. k=masktab[start];
  99. do {
  100. v=psd[j];
  101. j++;
  102. end1=bndtab[k+1];
  103. if (end1 > end) end1=end;
  104. for(i=j;i<end1;i++) {
  105. int c,adr;
  106. /* logadd */
  107. v1=psd[j];
  108. c=v-v1;
  109. if (c >= 0) {
  110. adr=c >> 1;
  111. if (adr > 255) adr=255;
  112. v=v + latab[adr];
  113. } else {
  114. adr=(-c) >> 1;
  115. if (adr > 255) adr=255;
  116. v=v1 + latab[adr];
  117. }
  118. j++;
  119. }
  120. bndpsd[k]=v;
  121. k++;
  122. } while (end > bndtab[k]);
  123. /* excitation function */
  124. bndstrt = masktab[start];
  125. bndend = masktab[end-1] + 1;
  126. lowcomp = 0;
  127. lowcomp = calc_lowcomp1(lowcomp, bndpsd[0], bndpsd[1]) ;
  128. excite[0] = bndpsd[0] - fgain - lowcomp ;
  129. lowcomp = calc_lowcomp1(lowcomp, bndpsd[1], bndpsd[2]) ;
  130. excite[1] = bndpsd[1] - fgain - lowcomp ;
  131. begin = 7 ;
  132. for (bin = 2; bin < 7; bin++) {
  133. if (!(is_lfe && bin == 6))
  134. lowcomp = calc_lowcomp1(lowcomp, bndpsd[bin], bndpsd[bin+1]) ;
  135. fastleak = bndpsd[bin] - fgain ;
  136. slowleak = bndpsd[bin] - s->sgain ;
  137. excite[bin] = fastleak - lowcomp ;
  138. if (!(is_lfe && bin == 6)) {
  139. if (bndpsd[bin] <= bndpsd[bin+1]) {
  140. begin = bin + 1 ;
  141. break ;
  142. }
  143. }
  144. }
  145. end1=bndend;
  146. if (end1 > 22) end1=22;
  147. for (bin = begin; bin < end1; bin++) {
  148. if (!(is_lfe && bin == 6))
  149. lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin+1], bin) ;
  150. fastleak -= s->fdecay ;
  151. v = bndpsd[bin] - fgain;
  152. if (fastleak < v) fastleak = v;
  153. slowleak -= s->sdecay ;
  154. v = bndpsd[bin] - s->sgain;
  155. if (slowleak < v) slowleak = v;
  156. v=fastleak - lowcomp;
  157. if (slowleak > v) v=slowleak;
  158. excite[bin] = v;
  159. }
  160. for (bin = 22; bin < bndend; bin++) {
  161. fastleak -= s->fdecay ;
  162. v = bndpsd[bin] - fgain;
  163. if (fastleak < v) fastleak = v;
  164. slowleak -= s->sdecay ;
  165. v = bndpsd[bin] - s->sgain;
  166. if (slowleak < v) slowleak = v;
  167. v=fastleak;
  168. if (slowleak > v) v = slowleak;
  169. excite[bin] = v;
  170. }
  171. /* compute masking curve */
  172. for (bin = bndstrt; bin < bndend; bin++) {
  173. v1 = excite[bin];
  174. tmp = s->dbknee - bndpsd[bin];
  175. if (tmp > 0) {
  176. v1 += tmp >> 2;
  177. }
  178. v=hth[bin >> s->halfratecod][s->fscod];
  179. if (v1 > v) v=v1;
  180. mask[bin] = v;
  181. }
  182. /* compute bit allocation */
  183. i = start ;
  184. j = masktab[start] ;
  185. do {
  186. v=mask[j];
  187. v -= snroffset ;
  188. v -= s->floor ;
  189. if (v < 0) v = 0;
  190. v &= 0x1fe0 ;
  191. v += s->floor ;
  192. end1=bndtab[j] + bndsz[j];
  193. if (end1 > end) end1=end;
  194. for (k = i; k < end1; k++) {
  195. address = (psd[i] - v) >> 5 ;
  196. if (address < 0) address=0;
  197. else if (address > 63) address=63;
  198. bap[i] = baptab[address];
  199. i++;
  200. }
  201. } while (end > bndtab[j++]) ;
  202. }
  203. typedef struct IComplex {
  204. short re,im;
  205. } IComplex;
  206. static void fft_init(int ln)
  207. {
  208. int i, j, m, n;
  209. float alpha;
  210. n = 1 << ln;
  211. for(i=0;i<(n/2);i++) {
  212. alpha = 2 * M_PI * (float)i / (float)n;
  213. costab[i] = fix15(cos(alpha));
  214. sintab[i] = fix15(sin(alpha));
  215. }
  216. for(i=0;i<n;i++) {
  217. m=0;
  218. for(j=0;j<ln;j++) {
  219. m |= ((i >> j) & 1) << (ln-j-1);
  220. }
  221. fft_rev[i]=m;
  222. }
  223. }
  224. /* butter fly op */
  225. #define BF(pre, pim, qre, qim, pre1, pim1, qre1, qim1) \
  226. {\
  227. int ax, ay, bx, by;\
  228. bx=pre1;\
  229. by=pim1;\
  230. ax=qre1;\
  231. ay=qim1;\
  232. pre = (bx + ax) >> 1;\
  233. pim = (by + ay) >> 1;\
  234. qre = (bx - ax) >> 1;\
  235. qim = (by - ay) >> 1;\
  236. }
  237. #define MUL16(a,b) ((a) * (b))
  238. #define CMUL(pre, pim, are, aim, bre, bim) \
  239. {\
  240. pre = (MUL16(are, bre) - MUL16(aim, bim)) >> 15;\
  241. pim = (MUL16(are, bim) + MUL16(bre, aim)) >> 15;\
  242. }
  243. /* do a 2^n point complex fft on 2^ln points. */
  244. static void fft(IComplex *z, int ln)
  245. {
  246. int j, l, np, np2;
  247. int nblocks, nloops;
  248. register IComplex *p,*q;
  249. int tmp_re, tmp_im;
  250. np = 1 << ln;
  251. /* reverse */
  252. for(j=0;j<np;j++) {
  253. int k;
  254. IComplex tmp;
  255. k = fft_rev[j];
  256. if (k < j) {
  257. tmp = z[k];
  258. z[k] = z[j];
  259. z[j] = tmp;
  260. }
  261. }
  262. /* pass 0 */
  263. p=&z[0];
  264. j=(np >> 1);
  265. do {
  266. BF(p[0].re, p[0].im, p[1].re, p[1].im,
  267. p[0].re, p[0].im, p[1].re, p[1].im);
  268. p+=2;
  269. } while (--j != 0);
  270. /* pass 1 */
  271. p=&z[0];
  272. j=np >> 2;
  273. do {
  274. BF(p[0].re, p[0].im, p[2].re, p[2].im,
  275. p[0].re, p[0].im, p[2].re, p[2].im);
  276. BF(p[1].re, p[1].im, p[3].re, p[3].im,
  277. p[1].re, p[1].im, p[3].im, -p[3].re);
  278. p+=4;
  279. } while (--j != 0);
  280. /* pass 2 .. ln-1 */
  281. nblocks = np >> 3;
  282. nloops = 1 << 2;
  283. np2 = np >> 1;
  284. do {
  285. p = z;
  286. q = z + nloops;
  287. for (j = 0; j < nblocks; ++j) {
  288. BF(p->re, p->im, q->re, q->im,
  289. p->re, p->im, q->re, q->im);
  290. p++;
  291. q++;
  292. for(l = nblocks; l < np2; l += nblocks) {
  293. CMUL(tmp_re, tmp_im, costab[l], -sintab[l], q->re, q->im);
  294. BF(p->re, p->im, q->re, q->im,
  295. p->re, p->im, tmp_re, tmp_im);
  296. p++;
  297. q++;
  298. }
  299. p += nloops;
  300. q += nloops;
  301. }
  302. nblocks = nblocks >> 1;
  303. nloops = nloops << 1;
  304. } while (nblocks != 0);
  305. }
  306. /* do a 512 point mdct */
  307. static void mdct512(INT32 *out, INT16 *in)
  308. {
  309. int i, re, im, re1, im1;
  310. INT16 rot[N];
  311. IComplex x[N/4];
  312. /* shift to simplify computations */
  313. for(i=0;i<N/4;i++)
  314. rot[i] = -in[i + 3*N/4];
  315. for(i=N/4;i<N;i++)
  316. rot[i] = in[i - N/4];
  317. /* pre rotation */
  318. for(i=0;i<N/4;i++) {
  319. re = ((int)rot[2*i] - (int)rot[N-1-2*i]) >> 1;
  320. im = -((int)rot[N/2+2*i] - (int)rot[N/2-1-2*i]) >> 1;
  321. CMUL(x[i].re, x[i].im, re, im, -xcos1[i], xsin1[i]);
  322. }
  323. fft(x, MDCT_NBITS - 2);
  324. /* post rotation */
  325. for(i=0;i<N/4;i++) {
  326. re = x[i].re;
  327. im = x[i].im;
  328. CMUL(re1, im1, re, im, xsin1[i], xcos1[i]);
  329. out[2*i] = im1;
  330. out[N/2-1-2*i] = re1;
  331. }
  332. }
  333. /* XXX: use another norm ? */
  334. static int calc_exp_diff(UINT8 *exp1, UINT8 *exp2, int n)
  335. {
  336. int sum, i;
  337. sum = 0;
  338. for(i=0;i<n;i++) {
  339. sum += abs(exp1[i] - exp2[i]);
  340. }
  341. return sum;
  342. }
  343. static void compute_exp_strategy(UINT8 exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS],
  344. UINT8 exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
  345. int ch, int is_lfe)
  346. {
  347. int i, j;
  348. int exp_diff;
  349. /* estimate if the exponent variation & decide if they should be
  350. reused in the next frame */
  351. exp_strategy[0][ch] = EXP_NEW;
  352. for(i=1;i<NB_BLOCKS;i++) {
  353. exp_diff = calc_exp_diff(exp[i][ch], exp[i-1][ch], N/2);
  354. #ifdef DEBUG
  355. printf("exp_diff=%d\n", exp_diff);
  356. #endif
  357. if (exp_diff > EXP_DIFF_THRESHOLD)
  358. exp_strategy[i][ch] = EXP_NEW;
  359. else
  360. exp_strategy[i][ch] = EXP_REUSE;
  361. }
  362. if (is_lfe)
  363. return;
  364. /* now select the encoding strategy type : if exponents are often
  365. recoded, we use a coarse encoding */
  366. i = 0;
  367. while (i < NB_BLOCKS) {
  368. j = i + 1;
  369. while (j < NB_BLOCKS && exp_strategy[j][ch] == EXP_REUSE)
  370. j++;
  371. switch(j - i) {
  372. case 1:
  373. exp_strategy[i][ch] = EXP_D45;
  374. break;
  375. case 2:
  376. case 3:
  377. exp_strategy[i][ch] = EXP_D25;
  378. break;
  379. default:
  380. exp_strategy[i][ch] = EXP_D15;
  381. break;
  382. }
  383. i = j;
  384. }
  385. }
  386. /* set exp[i] to min(exp[i], exp1[i]) */
  387. static void exponent_min(UINT8 exp[N/2], UINT8 exp1[N/2], int n)
  388. {
  389. int i;
  390. for(i=0;i<n;i++) {
  391. if (exp1[i] < exp[i])
  392. exp[i] = exp1[i];
  393. }
  394. }
  395. /* update the exponents so that they are the ones the decoder will
  396. decode. Return the number of bits used to code the exponents */
  397. static int encode_exp(UINT8 encoded_exp[N/2],
  398. UINT8 exp[N/2],
  399. int nb_exps,
  400. int exp_strategy)
  401. {
  402. int group_size, nb_groups, i, j, k, recurse, exp_min, delta;
  403. UINT8 exp1[N/2];
  404. switch(exp_strategy) {
  405. case EXP_D15:
  406. group_size = 1;
  407. break;
  408. case EXP_D25:
  409. group_size = 2;
  410. break;
  411. default:
  412. case EXP_D45:
  413. group_size = 4;
  414. break;
  415. }
  416. nb_groups = ((nb_exps + (group_size * 3) - 4) / (3 * group_size)) * 3;
  417. /* for each group, compute the minimum exponent */
  418. exp1[0] = exp[0]; /* DC exponent is handled separately */
  419. k = 1;
  420. for(i=1;i<=nb_groups;i++) {
  421. exp_min = exp[k];
  422. assert(exp_min >= 0 && exp_min <= 24);
  423. for(j=1;j<group_size;j++) {
  424. if (exp[k+j] < exp_min)
  425. exp_min = exp[k+j];
  426. }
  427. exp1[i] = exp_min;
  428. k += group_size;
  429. }
  430. /* constraint for DC exponent */
  431. if (exp1[0] > 15)
  432. exp1[0] = 15;
  433. /* Iterate until the delta constraints between each groups are
  434. satisfyed. I'm sure it is possible to find a better algorithm,
  435. but I am lazy */
  436. do {
  437. recurse = 0;
  438. for(i=1;i<=nb_groups;i++) {
  439. delta = exp1[i] - exp1[i-1];
  440. if (delta > 2) {
  441. /* if delta too big, we encode a smaller exponent */
  442. exp1[i] = exp1[i-1] + 2;
  443. } else if (delta < -2) {
  444. /* if delta is too small, we must decrease the previous
  445. exponent, which means we must recurse */
  446. recurse = 1;
  447. exp1[i-1] = exp1[i] + 2;
  448. }
  449. }
  450. } while (recurse);
  451. /* now we have the exponent values the decoder will see */
  452. encoded_exp[0] = exp1[0];
  453. k = 1;
  454. for(i=1;i<=nb_groups;i++) {
  455. for(j=0;j<group_size;j++) {
  456. encoded_exp[k+j] = exp1[i];
  457. }
  458. k += group_size;
  459. }
  460. #if defined(DEBUG)
  461. printf("exponents: strategy=%d\n", exp_strategy);
  462. for(i=0;i<=nb_groups * group_size;i++) {
  463. printf("%d ", encoded_exp[i]);
  464. }
  465. printf("\n");
  466. #endif
  467. return 4 + (nb_groups / 3) * 7;
  468. }
  469. /* return the size in bits taken by the mantissa */
  470. int compute_mantissa_size(AC3EncodeContext *s, UINT8 *m, int nb_coefs)
  471. {
  472. int bits, mant, i;
  473. bits = 0;
  474. for(i=0;i<nb_coefs;i++) {
  475. mant = m[i];
  476. switch(mant) {
  477. case 0:
  478. /* nothing */
  479. break;
  480. case 1:
  481. /* 3 mantissa in 5 bits */
  482. if (s->mant1_cnt == 0)
  483. bits += 5;
  484. if (++s->mant1_cnt == 3)
  485. s->mant1_cnt = 0;
  486. break;
  487. case 2:
  488. /* 3 mantissa in 7 bits */
  489. if (s->mant2_cnt == 0)
  490. bits += 7;
  491. if (++s->mant2_cnt == 3)
  492. s->mant2_cnt = 0;
  493. break;
  494. case 3:
  495. bits += 3;
  496. break;
  497. case 4:
  498. /* 2 mantissa in 7 bits */
  499. if (s->mant4_cnt == 0)
  500. bits += 7;
  501. if (++s->mant4_cnt == 2)
  502. s->mant4_cnt = 0;
  503. break;
  504. case 14:
  505. bits += 14;
  506. break;
  507. case 15:
  508. bits += 16;
  509. break;
  510. default:
  511. bits += mant - 1;
  512. break;
  513. }
  514. }
  515. return bits;
  516. }
  517. static int bit_alloc(AC3EncodeContext *s,
  518. UINT8 bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
  519. UINT8 encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
  520. UINT8 exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS],
  521. int frame_bits, int csnroffst, int fsnroffst)
  522. {
  523. int i, ch;
  524. /* compute size */
  525. for(i=0;i<NB_BLOCKS;i++) {
  526. s->mant1_cnt = 0;
  527. s->mant2_cnt = 0;
  528. s->mant4_cnt = 0;
  529. for(ch=0;ch<s->nb_all_channels;ch++) {
  530. parametric_bit_allocation(s, bap[i][ch], (INT8 *)encoded_exp[i][ch],
  531. 0, s->nb_coefs[ch],
  532. (((csnroffst-15) << 4) +
  533. fsnroffst) << 2,
  534. fgaintab[s->fgaincod[ch]],
  535. ch == s->lfe_channel);
  536. frame_bits += compute_mantissa_size(s, bap[i][ch],
  537. s->nb_coefs[ch]);
  538. }
  539. }
  540. #if 0
  541. printf("csnr=%d fsnr=%d frame_bits=%d diff=%d\n",
  542. csnroffst, fsnroffst, frame_bits,
  543. 16 * s->frame_size - ((frame_bits + 7) & ~7));
  544. #endif
  545. return 16 * s->frame_size - frame_bits;
  546. }
  547. #define SNR_INC1 4
  548. static int compute_bit_allocation(AC3EncodeContext *s,
  549. UINT8 bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
  550. UINT8 encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
  551. UINT8 exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS],
  552. int frame_bits)
  553. {
  554. int i, ch;
  555. int csnroffst, fsnroffst;
  556. UINT8 bap1[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
  557. static int frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };
  558. /* init default parameters */
  559. s->sdecaycod = 2;
  560. s->fdecaycod = 1;
  561. s->sgaincod = 1;
  562. s->dbkneecod = 2;
  563. s->floorcod = 4;
  564. for(ch=0;ch<s->nb_all_channels;ch++)
  565. s->fgaincod[ch] = 4;
  566. /* compute real values */
  567. s->sdecay = sdecaytab[s->sdecaycod] >> s->halfratecod;
  568. s->fdecay = fdecaytab[s->fdecaycod] >> s->halfratecod;
  569. s->sgain = sgaintab[s->sgaincod];
  570. s->dbknee = dbkneetab[s->dbkneecod];
  571. s->floor = floortab[s->floorcod];
  572. /* header size */
  573. frame_bits += 65;
  574. // if (s->acmod == 2)
  575. // frame_bits += 2;
  576. frame_bits += frame_bits_inc[s->acmod];
  577. /* audio blocks */
  578. for(i=0;i<NB_BLOCKS;i++) {
  579. frame_bits += s->nb_channels * 2 + 2; /* blksw * c, dithflag * c, dynrnge, cplstre */
  580. if (s->acmod == 2)
  581. frame_bits++; /* rematstr */
  582. frame_bits += 2 * s->nb_channels; /* chexpstr[2] * c */
  583. if (s->lfe)
  584. frame_bits++; /* lfeexpstr */
  585. for(ch=0;ch<s->nb_channels;ch++) {
  586. if (exp_strategy[i][ch] != EXP_REUSE)
  587. frame_bits += 6 + 2; /* chbwcod[6], gainrng[2] */
  588. }
  589. frame_bits++; /* baie */
  590. frame_bits++; /* snr */
  591. frame_bits += 2; /* delta / skip */
  592. }
  593. frame_bits++; /* cplinu for block 0 */
  594. /* bit alloc info */
  595. /* sdcycod[2], fdcycod[2], sgaincod[2], dbpbcod[2], floorcod[3] */
  596. /* csnroffset[6] */
  597. /* (fsnoffset[4] + fgaincod[4]) * c */
  598. frame_bits += 2*4 + 3 + 6 + s->nb_all_channels * (4 + 3);
  599. /* CRC */
  600. frame_bits += 16;
  601. /* now the big work begins : do the bit allocation. Modify the snr
  602. offset until we can pack everything in the requested frame size */
  603. csnroffst = s->csnroffst;
  604. while (csnroffst >= 0 &&
  605. bit_alloc(s, bap, encoded_exp, exp_strategy, frame_bits, csnroffst, 0) < 0)
  606. csnroffst -= SNR_INC1;
  607. if (csnroffst < 0) {
  608. fprintf(stderr, "Yack, Error !!!\n");
  609. return -1;
  610. }
  611. while ((csnroffst + SNR_INC1) <= 63 &&
  612. bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits,
  613. csnroffst + SNR_INC1, 0) >= 0) {
  614. csnroffst += SNR_INC1;
  615. memcpy(bap, bap1, sizeof(bap1));
  616. }
  617. while ((csnroffst + 1) <= 63 &&
  618. bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits, csnroffst + 1, 0) >= 0) {
  619. csnroffst++;
  620. memcpy(bap, bap1, sizeof(bap1));
  621. }
  622. fsnroffst = 0;
  623. while ((fsnroffst + SNR_INC1) <= 15 &&
  624. bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits,
  625. csnroffst, fsnroffst + SNR_INC1) >= 0) {
  626. fsnroffst += SNR_INC1;
  627. memcpy(bap, bap1, sizeof(bap1));
  628. }
  629. while ((fsnroffst + 1) <= 15 &&
  630. bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits,
  631. csnroffst, fsnroffst + 1) >= 0) {
  632. fsnroffst++;
  633. memcpy(bap, bap1, sizeof(bap1));
  634. }
  635. s->csnroffst = csnroffst;
  636. for(ch=0;ch<s->nb_all_channels;ch++)
  637. s->fsnroffst[ch] = fsnroffst;
  638. #if defined(DEBUG_BITALLOC)
  639. {
  640. int j;
  641. for(i=0;i<6;i++) {
  642. for(ch=0;ch<s->nb_all_channels;ch++) {
  643. printf("Block #%d Ch%d:\n", i, ch);
  644. printf("bap=");
  645. for(j=0;j<s->nb_coefs[ch];j++) {
  646. printf("%d ",bap[i][ch][j]);
  647. }
  648. printf("\n");
  649. }
  650. }
  651. }
  652. #endif
  653. return 0;
  654. }
  655. static int AC3_encode_init(AVCodecContext *avctx)
  656. {
  657. int freq = avctx->sample_rate;
  658. int bitrate = avctx->bit_rate;
  659. int channels = avctx->channels;
  660. AC3EncodeContext *s = avctx->priv_data;
  661. int i, j, k, l, ch, v;
  662. float alpha;
  663. static unsigned short freqs[3] = { 48000, 44100, 32000 };
  664. static int acmod_defs[6] = {
  665. 0x01, /* C */
  666. 0x02, /* L R */
  667. 0x03, /* L C R */
  668. 0x06, /* L R SL SR */
  669. 0x07, /* L C R SL SR */
  670. 0x07, /* L C R SL SR (+LFE) */
  671. };
  672. avctx->frame_size = AC3_FRAME_SIZE;
  673. avctx->key_frame = 1; /* always key frame */
  674. /* number of channels */
  675. if (channels < 1 || channels > 6)
  676. return -1;
  677. s->acmod = acmod_defs[channels - 1];
  678. s->lfe = (channels == 6) ? 1 : 0;
  679. s->nb_all_channels = channels;
  680. s->nb_channels = channels > 5 ? 5 : channels;
  681. s->lfe_channel = s->lfe ? 5 : -1;
  682. /* frequency */
  683. for(i=0;i<3;i++) {
  684. for(j=0;j<3;j++)
  685. if ((freqs[j] >> i) == freq)
  686. goto found;
  687. }
  688. return -1;
  689. found:
  690. s->sample_rate = freq;
  691. s->halfratecod = i;
  692. s->fscod = j;
  693. s->bsid = 8 + s->halfratecod;
  694. s->bsmod = 0; /* complete main audio service */
  695. /* bitrate & frame size */
  696. bitrate /= 1000;
  697. for(i=0;i<19;i++) {
  698. if ((bitratetab[i] >> s->halfratecod) == bitrate)
  699. break;
  700. }
  701. if (i == 19)
  702. return -1;
  703. s->bit_rate = bitrate;
  704. s->frmsizecod = i << 1;
  705. s->frame_size_min = (bitrate * 1000 * AC3_FRAME_SIZE) / (freq * 16);
  706. /* for now we do not handle fractional sizes */
  707. s->frame_size = s->frame_size_min;
  708. /* bit allocation init */
  709. for(ch=0;ch<s->nb_channels;ch++) {
  710. /* bandwidth for each channel */
  711. /* XXX: should compute the bandwidth according to the frame
  712. size, so that we avoid anoying high freq artefacts */
  713. s->chbwcod[ch] = 50; /* sample bandwidth as mpeg audio layer 2 table 0 */
  714. s->nb_coefs[ch] = ((s->chbwcod[ch] + 12) * 3) + 37;
  715. }
  716. if (s->lfe) {
  717. s->nb_coefs[s->lfe_channel] = 7; /* fixed */
  718. }
  719. /* initial snr offset */
  720. s->csnroffst = 40;
  721. /* compute bndtab and masktab from bandsz */
  722. k = 0;
  723. l = 0;
  724. for(i=0;i<50;i++) {
  725. bndtab[i] = l;
  726. v = bndsz[i];
  727. for(j=0;j<v;j++) masktab[k++]=i;
  728. l += v;
  729. }
  730. bndtab[50] = 0;
  731. /* mdct init */
  732. fft_init(MDCT_NBITS - 2);
  733. for(i=0;i<N/4;i++) {
  734. alpha = 2 * M_PI * (i + 1.0 / 8.0) / (float)N;
  735. xcos1[i] = fix15(-cos(alpha));
  736. xsin1[i] = fix15(-sin(alpha));
  737. }
  738. ac3_crc_init();
  739. return 0;
  740. }
  741. /* output the AC3 frame header */
  742. static void output_frame_header(AC3EncodeContext *s, unsigned char *frame)
  743. {
  744. init_put_bits(&s->pb, frame, AC3_MAX_CODED_FRAME_SIZE, NULL, NULL);
  745. put_bits(&s->pb, 16, 0x0b77); /* frame header */
  746. put_bits(&s->pb, 16, 0); /* crc1: will be filled later */
  747. put_bits(&s->pb, 2, s->fscod);
  748. put_bits(&s->pb, 6, s->frmsizecod + (s->frame_size - s->frame_size_min));
  749. put_bits(&s->pb, 5, s->bsid);
  750. put_bits(&s->pb, 3, s->bsmod);
  751. put_bits(&s->pb, 3, s->acmod);
  752. if ((s->acmod & 0x01) && s->acmod != 0x01)
  753. put_bits(&s->pb, 2, 1); /* XXX -4.5 dB */
  754. if (s->acmod & 0x04)
  755. put_bits(&s->pb, 2, 1); /* XXX -6 dB */
  756. if (s->acmod == 0x02)
  757. put_bits(&s->pb, 2, 0); /* surround not indicated */
  758. put_bits(&s->pb, 1, s->lfe); /* LFE */
  759. put_bits(&s->pb, 5, 31); /* dialog norm: -31 db */
  760. put_bits(&s->pb, 1, 0); /* no compression control word */
  761. put_bits(&s->pb, 1, 0); /* no lang code */
  762. put_bits(&s->pb, 1, 0); /* no audio production info */
  763. put_bits(&s->pb, 1, 0); /* no copyright */
  764. put_bits(&s->pb, 1, 1); /* original bitstream */
  765. put_bits(&s->pb, 1, 0); /* no time code 1 */
  766. put_bits(&s->pb, 1, 0); /* no time code 2 */
  767. put_bits(&s->pb, 1, 0); /* no addtional bit stream info */
  768. }
  769. /* symetric quantization on 'levels' levels */
  770. static inline int sym_quant(int c, int e, int levels)
  771. {
  772. int v;
  773. if (c >= 0) {
  774. v = (levels * (c << e)) >> 24;
  775. v = (v + 1) >> 1;
  776. v = (levels >> 1) + v;
  777. } else {
  778. v = (levels * ((-c) << e)) >> 24;
  779. v = (v + 1) >> 1;
  780. v = (levels >> 1) - v;
  781. }
  782. assert (v >= 0 && v < levels);
  783. return v;
  784. }
  785. /* asymetric quantization on 2^qbits levels */
  786. static inline int asym_quant(int c, int e, int qbits)
  787. {
  788. int lshift, m, v;
  789. lshift = e + qbits - 24;
  790. if (lshift >= 0)
  791. v = c << lshift;
  792. else
  793. v = c >> (-lshift);
  794. /* rounding */
  795. v = (v + 1) >> 1;
  796. m = (1 << (qbits-1));
  797. if (v >= m)
  798. v = m - 1;
  799. assert(v >= -m);
  800. return v & ((1 << qbits)-1);
  801. }
  802. /* Output one audio block. There are NB_BLOCKS audio blocks in one AC3
  803. frame */
  804. static void output_audio_block(AC3EncodeContext *s,
  805. UINT8 exp_strategy[AC3_MAX_CHANNELS],
  806. UINT8 encoded_exp[AC3_MAX_CHANNELS][N/2],
  807. UINT8 bap[AC3_MAX_CHANNELS][N/2],
  808. INT32 mdct_coefs[AC3_MAX_CHANNELS][N/2],
  809. INT8 global_exp[AC3_MAX_CHANNELS],
  810. int block_num)
  811. {
  812. int ch, nb_groups, group_size, i, baie;
  813. UINT8 *p;
  814. UINT16 qmant[AC3_MAX_CHANNELS][N/2];
  815. int exp0, exp1;
  816. int mant1_cnt, mant2_cnt, mant4_cnt;
  817. UINT16 *qmant1_ptr, *qmant2_ptr, *qmant4_ptr;
  818. int delta0, delta1, delta2;
  819. for(ch=0;ch<s->nb_channels;ch++)
  820. put_bits(&s->pb, 1, 0); /* 512 point MDCT */
  821. for(ch=0;ch<s->nb_channels;ch++)
  822. put_bits(&s->pb, 1, 1); /* no dither */
  823. put_bits(&s->pb, 1, 0); /* no dynamic range */
  824. if (block_num == 0) {
  825. /* for block 0, even if no coupling, we must say it. This is a
  826. waste of bit :-) */
  827. put_bits(&s->pb, 1, 1); /* coupling strategy present */
  828. put_bits(&s->pb, 1, 0); /* no coupling strategy */
  829. } else {
  830. put_bits(&s->pb, 1, 0); /* no new coupling strategy */
  831. }
  832. if (s->acmod == 2) {
  833. put_bits(&s->pb, 1, 0); /* no matrixing (but should be used in the future) */
  834. }
  835. #if defined(DEBUG)
  836. {
  837. static int count = 0;
  838. printf("Block #%d (%d)\n", block_num, count++);
  839. }
  840. #endif
  841. /* exponent strategy */
  842. for(ch=0;ch<s->nb_channels;ch++) {
  843. put_bits(&s->pb, 2, exp_strategy[ch]);
  844. }
  845. if (s->lfe) {
  846. put_bits(&s->pb, 1, exp_strategy[s->lfe_channel]);
  847. }
  848. for(ch=0;ch<s->nb_channels;ch++) {
  849. if (exp_strategy[ch] != EXP_REUSE)
  850. put_bits(&s->pb, 6, s->chbwcod[ch]);
  851. }
  852. /* exponents */
  853. for (ch = 0; ch < s->nb_all_channels; ch++) {
  854. switch(exp_strategy[ch]) {
  855. case EXP_REUSE:
  856. continue;
  857. case EXP_D15:
  858. group_size = 1;
  859. break;
  860. case EXP_D25:
  861. group_size = 2;
  862. break;
  863. default:
  864. case EXP_D45:
  865. group_size = 4;
  866. break;
  867. }
  868. nb_groups = (s->nb_coefs[ch] + (group_size * 3) - 4) / (3 * group_size);
  869. p = encoded_exp[ch];
  870. /* first exponent */
  871. exp1 = *p++;
  872. put_bits(&s->pb, 4, exp1);
  873. /* next ones are delta encoded */
  874. for(i=0;i<nb_groups;i++) {
  875. /* merge three delta in one code */
  876. exp0 = exp1;
  877. exp1 = p[0];
  878. p += group_size;
  879. delta0 = exp1 - exp0 + 2;
  880. exp0 = exp1;
  881. exp1 = p[0];
  882. p += group_size;
  883. delta1 = exp1 - exp0 + 2;
  884. exp0 = exp1;
  885. exp1 = p[0];
  886. p += group_size;
  887. delta2 = exp1 - exp0 + 2;
  888. put_bits(&s->pb, 7, ((delta0 * 5 + delta1) * 5) + delta2);
  889. }
  890. if (ch != s->lfe_channel)
  891. put_bits(&s->pb, 2, 0); /* no gain range info */
  892. }
  893. /* bit allocation info */
  894. baie = (block_num == 0);
  895. put_bits(&s->pb, 1, baie);
  896. if (baie) {
  897. put_bits(&s->pb, 2, s->sdecaycod);
  898. put_bits(&s->pb, 2, s->fdecaycod);
  899. put_bits(&s->pb, 2, s->sgaincod);
  900. put_bits(&s->pb, 2, s->dbkneecod);
  901. put_bits(&s->pb, 3, s->floorcod);
  902. }
  903. /* snr offset */
  904. put_bits(&s->pb, 1, baie); /* always present with bai */
  905. if (baie) {
  906. put_bits(&s->pb, 6, s->csnroffst);
  907. for(ch=0;ch<s->nb_all_channels;ch++) {
  908. put_bits(&s->pb, 4, s->fsnroffst[ch]);
  909. put_bits(&s->pb, 3, s->fgaincod[ch]);
  910. }
  911. }
  912. put_bits(&s->pb, 1, 0); /* no delta bit allocation */
  913. put_bits(&s->pb, 1, 0); /* no data to skip */
  914. /* mantissa encoding : we use two passes to handle the grouping. A
  915. one pass method may be faster, but it would necessitate to
  916. modify the output stream. */
  917. /* first pass: quantize */
  918. mant1_cnt = mant2_cnt = mant4_cnt = 0;
  919. qmant1_ptr = qmant2_ptr = qmant4_ptr = NULL;
  920. for (ch = 0; ch < s->nb_all_channels; ch++) {
  921. int b, c, e, v;
  922. for(i=0;i<s->nb_coefs[ch];i++) {
  923. c = mdct_coefs[ch][i];
  924. e = encoded_exp[ch][i] - global_exp[ch];
  925. b = bap[ch][i];
  926. switch(b) {
  927. case 0:
  928. v = 0;
  929. break;
  930. case 1:
  931. v = sym_quant(c, e, 3);
  932. switch(mant1_cnt) {
  933. case 0:
  934. qmant1_ptr = &qmant[ch][i];
  935. v = 9 * v;
  936. mant1_cnt = 1;
  937. break;
  938. case 1:
  939. *qmant1_ptr += 3 * v;
  940. mant1_cnt = 2;
  941. v = 128;
  942. break;
  943. default:
  944. *qmant1_ptr += v;
  945. mant1_cnt = 0;
  946. v = 128;
  947. break;
  948. }
  949. break;
  950. case 2:
  951. v = sym_quant(c, e, 5);
  952. switch(mant2_cnt) {
  953. case 0:
  954. qmant2_ptr = &qmant[ch][i];
  955. v = 25 * v;
  956. mant2_cnt = 1;
  957. break;
  958. case 1:
  959. *qmant2_ptr += 5 * v;
  960. mant2_cnt = 2;
  961. v = 128;
  962. break;
  963. default:
  964. *qmant2_ptr += v;
  965. mant2_cnt = 0;
  966. v = 128;
  967. break;
  968. }
  969. break;
  970. case 3:
  971. v = sym_quant(c, e, 7);
  972. break;
  973. case 4:
  974. v = sym_quant(c, e, 11);
  975. switch(mant4_cnt) {
  976. case 0:
  977. qmant4_ptr = &qmant[ch][i];
  978. v = 11 * v;
  979. mant4_cnt = 1;
  980. break;
  981. default:
  982. *qmant4_ptr += v;
  983. mant4_cnt = 0;
  984. v = 128;
  985. break;
  986. }
  987. break;
  988. case 5:
  989. v = sym_quant(c, e, 15);
  990. break;
  991. case 14:
  992. v = asym_quant(c, e, 14);
  993. break;
  994. case 15:
  995. v = asym_quant(c, e, 16);
  996. break;
  997. default:
  998. v = asym_quant(c, e, b - 1);
  999. break;
  1000. }
  1001. qmant[ch][i] = v;
  1002. }
  1003. }
  1004. /* second pass : output the values */
  1005. for (ch = 0; ch < s->nb_all_channels; ch++) {
  1006. int b, q;
  1007. for(i=0;i<s->nb_coefs[ch];i++) {
  1008. q = qmant[ch][i];
  1009. b = bap[ch][i];
  1010. switch(b) {
  1011. case 0:
  1012. break;
  1013. case 1:
  1014. if (q != 128)
  1015. put_bits(&s->pb, 5, q);
  1016. break;
  1017. case 2:
  1018. if (q != 128)
  1019. put_bits(&s->pb, 7, q);
  1020. break;
  1021. case 3:
  1022. put_bits(&s->pb, 3, q);
  1023. break;
  1024. case 4:
  1025. if (q != 128)
  1026. put_bits(&s->pb, 7, q);
  1027. break;
  1028. case 14:
  1029. put_bits(&s->pb, 14, q);
  1030. break;
  1031. case 15:
  1032. put_bits(&s->pb, 16, q);
  1033. break;
  1034. default:
  1035. put_bits(&s->pb, b - 1, q);
  1036. break;
  1037. }
  1038. }
  1039. }
  1040. }
  1041. /* compute the ac3 crc */
  1042. #define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16))
  1043. static void ac3_crc_init(void)
  1044. {
  1045. unsigned int c, n, k;
  1046. for(n=0;n<256;n++) {
  1047. c = n << 8;
  1048. for (k = 0; k < 8; k++) {
  1049. if (c & (1 << 15))
  1050. c = ((c << 1) & 0xffff) ^ (CRC16_POLY & 0xffff);
  1051. else
  1052. c = c << 1;
  1053. }
  1054. crc_table[n] = c;
  1055. }
  1056. }
  1057. static unsigned int ac3_crc(UINT8 *data, int n, unsigned int crc)
  1058. {
  1059. int i;
  1060. for(i=0;i<n;i++) {
  1061. crc = (crc_table[data[i] ^ (crc >> 8)] ^ (crc << 8)) & 0xffff;
  1062. }
  1063. return crc;
  1064. }
  1065. static unsigned int mul_poly(unsigned int a, unsigned int b, unsigned int poly)
  1066. {
  1067. unsigned int c;
  1068. c = 0;
  1069. while (a) {
  1070. if (a & 1)
  1071. c ^= b;
  1072. a = a >> 1;
  1073. b = b << 1;
  1074. if (b & (1 << 16))
  1075. b ^= poly;
  1076. }
  1077. return c;
  1078. }
  1079. static unsigned int pow_poly(unsigned int a, unsigned int n, unsigned int poly)
  1080. {
  1081. unsigned int r;
  1082. r = 1;
  1083. while (n) {
  1084. if (n & 1)
  1085. r = mul_poly(r, a, poly);
  1086. a = mul_poly(a, a, poly);
  1087. n >>= 1;
  1088. }
  1089. return r;
  1090. }
  1091. /* compute log2(max(abs(tab[]))) */
  1092. static int log2_tab(INT16 *tab, int n)
  1093. {
  1094. int i, v;
  1095. v = 0;
  1096. for(i=0;i<n;i++) {
  1097. v |= abs(tab[i]);
  1098. }
  1099. return av_log2(v);
  1100. }
  1101. static void lshift_tab(INT16 *tab, int n, int lshift)
  1102. {
  1103. int i;
  1104. if (lshift > 0) {
  1105. for(i=0;i<n;i++) {
  1106. tab[i] <<= lshift;
  1107. }
  1108. } else if (lshift < 0) {
  1109. lshift = -lshift;
  1110. for(i=0;i<n;i++) {
  1111. tab[i] >>= lshift;
  1112. }
  1113. }
  1114. }
  1115. /* fill the end of the frame and compute the two crcs */
  1116. static int output_frame_end(AC3EncodeContext *s)
  1117. {
  1118. int frame_size, frame_size_58, n, crc1, crc2, crc_inv;
  1119. UINT8 *frame;
  1120. frame_size = s->frame_size; /* frame size in words */
  1121. /* align to 8 bits */
  1122. flush_put_bits(&s->pb);
  1123. /* add zero bytes to reach the frame size */
  1124. frame = s->pb.buf;
  1125. n = 2 * s->frame_size - (pbBufPtr(&s->pb) - frame) - 2;
  1126. assert(n >= 0);
  1127. memset(pbBufPtr(&s->pb), 0, n);
  1128. /* Now we must compute both crcs : this is not so easy for crc1
  1129. because it is at the beginning of the data... */
  1130. frame_size_58 = (frame_size >> 1) + (frame_size >> 3);
  1131. crc1 = ac3_crc(frame + 4, (2 * frame_size_58) - 4, 0);
  1132. /* XXX: could precompute crc_inv */
  1133. crc_inv = pow_poly((CRC16_POLY >> 1), (16 * frame_size_58) - 16, CRC16_POLY);
  1134. crc1 = mul_poly(crc_inv, crc1, CRC16_POLY);
  1135. frame[2] = crc1 >> 8;
  1136. frame[3] = crc1;
  1137. crc2 = ac3_crc(frame + 2 * frame_size_58, (frame_size - frame_size_58) * 2 - 2, 0);
  1138. frame[2*frame_size - 2] = crc2 >> 8;
  1139. frame[2*frame_size - 1] = crc2;
  1140. // printf("n=%d frame_size=%d\n", n, frame_size);
  1141. return frame_size * 2;
  1142. }
  1143. int AC3_encode_frame(AVCodecContext *avctx,
  1144. unsigned char *frame, int buf_size, void *data)
  1145. {
  1146. AC3EncodeContext *s = avctx->priv_data;
  1147. short *samples = data;
  1148. int i, j, k, v, ch;
  1149. INT16 input_samples[N];
  1150. INT32 mdct_coef[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
  1151. UINT8 exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
  1152. UINT8 exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS];
  1153. UINT8 encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
  1154. UINT8 bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
  1155. INT8 exp_samples[NB_BLOCKS][AC3_MAX_CHANNELS];
  1156. int frame_bits;
  1157. frame_bits = 0;
  1158. for(ch=0;ch<s->nb_all_channels;ch++) {
  1159. /* fixed mdct to the six sub blocks & exponent computation */
  1160. for(i=0;i<NB_BLOCKS;i++) {
  1161. INT16 *sptr;
  1162. int sinc;
  1163. /* compute input samples */
  1164. memcpy(input_samples, s->last_samples[ch], N/2 * sizeof(INT16));
  1165. sinc = s->nb_all_channels;
  1166. sptr = samples + (sinc * (N/2) * i) + ch;
  1167. for(j=0;j<N/2;j++) {
  1168. v = *sptr;
  1169. input_samples[j + N/2] = v;
  1170. s->last_samples[ch][j] = v;
  1171. sptr += sinc;
  1172. }
  1173. /* apply the MDCT window */
  1174. for(j=0;j<N/2;j++) {
  1175. input_samples[j] = MUL16(input_samples[j],
  1176. ac3_window[j]) >> 15;
  1177. input_samples[N-j-1] = MUL16(input_samples[N-j-1],
  1178. ac3_window[j]) >> 15;
  1179. }
  1180. /* Normalize the samples to use the maximum available
  1181. precision */
  1182. v = 14 - log2_tab(input_samples, N);
  1183. if (v < 0)
  1184. v = 0;
  1185. exp_samples[i][ch] = v - 8;
  1186. lshift_tab(input_samples, N, v);
  1187. /* do the MDCT */
  1188. mdct512(mdct_coef[i][ch], input_samples);
  1189. /* compute "exponents". We take into account the
  1190. normalization there */
  1191. for(j=0;j<N/2;j++) {
  1192. int e;
  1193. v = abs(mdct_coef[i][ch][j]);
  1194. if (v == 0)
  1195. e = 24;
  1196. else {
  1197. e = 23 - av_log2(v) + exp_samples[i][ch];
  1198. if (e >= 24) {
  1199. e = 24;
  1200. mdct_coef[i][ch][j] = 0;
  1201. }
  1202. }
  1203. exp[i][ch][j] = e;
  1204. }
  1205. }
  1206. compute_exp_strategy(exp_strategy, exp, ch, ch == s->lfe_channel);
  1207. /* compute the exponents as the decoder will see them. The
  1208. EXP_REUSE case must be handled carefully : we select the
  1209. min of the exponents */
  1210. i = 0;
  1211. while (i < NB_BLOCKS) {
  1212. j = i + 1;
  1213. while (j < NB_BLOCKS && exp_strategy[j][ch] == EXP_REUSE) {
  1214. exponent_min(exp[i][ch], exp[j][ch], s->nb_coefs[ch]);
  1215. j++;
  1216. }
  1217. frame_bits += encode_exp(encoded_exp[i][ch],
  1218. exp[i][ch], s->nb_coefs[ch],
  1219. exp_strategy[i][ch]);
  1220. /* copy encoded exponents for reuse case */
  1221. for(k=i+1;k<j;k++) {
  1222. memcpy(encoded_exp[k][ch], encoded_exp[i][ch],
  1223. s->nb_coefs[ch] * sizeof(UINT8));
  1224. }
  1225. i = j;
  1226. }
  1227. }
  1228. compute_bit_allocation(s, bap, encoded_exp, exp_strategy, frame_bits);
  1229. /* everything is known... let's output the frame */
  1230. output_frame_header(s, frame);
  1231. for(i=0;i<NB_BLOCKS;i++) {
  1232. output_audio_block(s, exp_strategy[i], encoded_exp[i],
  1233. bap[i], mdct_coef[i], exp_samples[i], i);
  1234. }
  1235. return output_frame_end(s);
  1236. }
  1237. #if 0
  1238. /*************************************************************************/
  1239. /* TEST */
  1240. #define FN (N/4)
  1241. void fft_test(void)
  1242. {
  1243. IComplex in[FN], in1[FN];
  1244. int k, n, i;
  1245. float sum_re, sum_im, a;
  1246. /* FFT test */
  1247. for(i=0;i<FN;i++) {
  1248. in[i].re = random() % 65535 - 32767;
  1249. in[i].im = random() % 65535 - 32767;
  1250. in1[i] = in[i];
  1251. }
  1252. fft(in, 7);
  1253. /* do it by hand */
  1254. for(k=0;k<FN;k++) {
  1255. sum_re = 0;
  1256. sum_im = 0;
  1257. for(n=0;n<FN;n++) {
  1258. a = -2 * M_PI * (n * k) / FN;
  1259. sum_re += in1[n].re * cos(a) - in1[n].im * sin(a);
  1260. sum_im += in1[n].re * sin(a) + in1[n].im * cos(a);
  1261. }
  1262. printf("%3d: %6d,%6d %6.0f,%6.0f\n",
  1263. k, in[k].re, in[k].im, sum_re / FN, sum_im / FN);
  1264. }
  1265. }
  1266. void mdct_test(void)
  1267. {
  1268. INT16 input[N];
  1269. INT32 output[N/2];
  1270. float input1[N];
  1271. float output1[N/2];
  1272. float s, a, err, e, emax;
  1273. int i, k, n;
  1274. for(i=0;i<N;i++) {
  1275. input[i] = (random() % 65535 - 32767) * 9 / 10;
  1276. input1[i] = input[i];
  1277. }
  1278. mdct512(output, input);
  1279. /* do it by hand */
  1280. for(k=0;k<N/2;k++) {
  1281. s = 0;
  1282. for(n=0;n<N;n++) {
  1283. a = (2*M_PI*(2*n+1+N/2)*(2*k+1) / (4 * N));
  1284. s += input1[n] * cos(a);
  1285. }
  1286. output1[k] = -2 * s / N;
  1287. }
  1288. err = 0;
  1289. emax = 0;
  1290. for(i=0;i<N/2;i++) {
  1291. printf("%3d: %7d %7.0f\n", i, output[i], output1[i]);
  1292. e = output[i] - output1[i];
  1293. if (e > emax)
  1294. emax = e;
  1295. err += e * e;
  1296. }
  1297. printf("err2=%f emax=%f\n", err / (N/2), emax);
  1298. }
  1299. void test_ac3(void)
  1300. {
  1301. AC3EncodeContext ctx;
  1302. unsigned char frame[AC3_MAX_CODED_FRAME_SIZE];
  1303. short samples[AC3_FRAME_SIZE];
  1304. int ret, i;
  1305. AC3_encode_init(&ctx, 44100, 64000, 1);
  1306. fft_test();
  1307. mdct_test();
  1308. for(i=0;i<AC3_FRAME_SIZE;i++)
  1309. samples[i] = (int)(sin(2*M_PI*i*1000.0/44100) * 10000);
  1310. ret = AC3_encode_frame(&ctx, frame, samples);
  1311. printf("ret=%d\n", ret);
  1312. }
  1313. #endif
  1314. AVCodec ac3_encoder = {
  1315. "ac3",
  1316. CODEC_TYPE_AUDIO,
  1317. CODEC_ID_AC3,
  1318. sizeof(AC3EncodeContext),
  1319. AC3_encode_init,
  1320. AC3_encode_frame,
  1321. NULL,
  1322. };