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.

1551 lines
42KB

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