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.

1560 lines
43KB

  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. unsigned int sample_rate;
  30. unsigned int bsid;
  31. unsigned int frame_size_min; /* minimum frame size in case rounding is necessary */
  32. unsigned int frame_size; /* current frame size in words */
  33. int halfratecod;
  34. unsigned int frmsizecod;
  35. unsigned int fscod; /* frequency */
  36. unsigned int acmod;
  37. int lfe;
  38. unsigned int bsmod;
  39. short last_samples[AC3_MAX_CHANNELS][256];
  40. unsigned 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_t 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_t *bap,
  103. int8_t *exp, int start, int end,
  104. int snroffset, int fgain, int is_lfe,
  105. int deltbae,int deltnseg,
  106. uint8_t *deltoffst, uint8_t *deltlen, uint8_t *deltba)
  107. {
  108. int bin,i,j,k,end1,v,v1,bndstrt,bndend,lowcomp,begin;
  109. int fastleak,slowleak,address,tmp;
  110. int16_t psd[256]; /* scaled exponents */
  111. int16_t bndpsd[50]; /* interpolated exponents */
  112. int16_t excite[50]; /* excitation */
  113. int16_t 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_t *out, int16_t *in)
  355. {
  356. int i, re, im, re1, im1;
  357. int16_t 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_t *exp1, uint8_t *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_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS],
  391. uint8_t 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_t exp[N/2], uint8_t 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_t encoded_exp[N/2],
  445. uint8_t 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_t 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. static int compute_mantissa_size(AC3EncodeContext *s, uint8_t *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_t bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
  566. uint8_t encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
  567. uint8_t 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_t *)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_t bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
  599. uint8_t encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
  600. uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS],
  601. int frame_bits)
  602. {
  603. int i, ch;
  604. int csnroffst, fsnroffst;
  605. uint8_t 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_t 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. /* number of channels */
  738. if (channels < 1 || channels > 6)
  739. return -1;
  740. s->acmod = acmod_defs[channels - 1];
  741. s->lfe = (channels == 6) ? 1 : 0;
  742. s->nb_all_channels = channels;
  743. s->nb_channels = channels > 5 ? 5 : channels;
  744. s->lfe_channel = s->lfe ? 5 : -1;
  745. /* frequency */
  746. for(i=0;i<3;i++) {
  747. for(j=0;j<3;j++)
  748. if ((ac3_freqs[j] >> i) == freq)
  749. goto found;
  750. }
  751. return -1;
  752. found:
  753. s->sample_rate = freq;
  754. s->halfratecod = i;
  755. s->fscod = j;
  756. s->bsid = 8 + s->halfratecod;
  757. s->bsmod = 0; /* complete main audio service */
  758. /* bitrate & frame size */
  759. bitrate /= 1000;
  760. for(i=0;i<19;i++) {
  761. if ((ac3_bitratetab[i] >> s->halfratecod) == bitrate)
  762. break;
  763. }
  764. if (i == 19)
  765. return -1;
  766. s->bit_rate = bitrate;
  767. s->frmsizecod = i << 1;
  768. s->frame_size_min = (bitrate * 1000 * AC3_FRAME_SIZE) / (freq * 16);
  769. /* for now we do not handle fractional sizes */
  770. s->frame_size = s->frame_size_min;
  771. /* bit allocation init */
  772. for(ch=0;ch<s->nb_channels;ch++) {
  773. /* bandwidth for each channel */
  774. /* XXX: should compute the bandwidth according to the frame
  775. size, so that we avoid anoying high freq artefacts */
  776. s->chbwcod[ch] = 50; /* sample bandwidth as mpeg audio layer 2 table 0 */
  777. s->nb_coefs[ch] = ((s->chbwcod[ch] + 12) * 3) + 37;
  778. }
  779. if (s->lfe) {
  780. s->nb_coefs[s->lfe_channel] = 7; /* fixed */
  781. }
  782. /* initial snr offset */
  783. s->csnroffst = 40;
  784. ac3_common_init();
  785. /* mdct init */
  786. fft_init(MDCT_NBITS - 2);
  787. for(i=0;i<N/4;i++) {
  788. alpha = 2 * M_PI * (i + 1.0 / 8.0) / (float)N;
  789. xcos1[i] = fix15(-cos(alpha));
  790. xsin1[i] = fix15(-sin(alpha));
  791. }
  792. ac3_crc_init();
  793. avctx->coded_frame= avcodec_alloc_frame();
  794. avctx->coded_frame->key_frame= 1;
  795. return 0;
  796. }
  797. /* output the AC3 frame header */
  798. static void output_frame_header(AC3EncodeContext *s, unsigned char *frame)
  799. {
  800. init_put_bits(&s->pb, frame, AC3_MAX_CODED_FRAME_SIZE, NULL, NULL);
  801. put_bits(&s->pb, 16, 0x0b77); /* frame header */
  802. put_bits(&s->pb, 16, 0); /* crc1: will be filled later */
  803. put_bits(&s->pb, 2, s->fscod);
  804. put_bits(&s->pb, 6, s->frmsizecod + (s->frame_size - s->frame_size_min));
  805. put_bits(&s->pb, 5, s->bsid);
  806. put_bits(&s->pb, 3, s->bsmod);
  807. put_bits(&s->pb, 3, s->acmod);
  808. if ((s->acmod & 0x01) && s->acmod != 0x01)
  809. put_bits(&s->pb, 2, 1); /* XXX -4.5 dB */
  810. if (s->acmod & 0x04)
  811. put_bits(&s->pb, 2, 1); /* XXX -6 dB */
  812. if (s->acmod == 0x02)
  813. put_bits(&s->pb, 2, 0); /* surround not indicated */
  814. put_bits(&s->pb, 1, s->lfe); /* LFE */
  815. put_bits(&s->pb, 5, 31); /* dialog norm: -31 db */
  816. put_bits(&s->pb, 1, 0); /* no compression control word */
  817. put_bits(&s->pb, 1, 0); /* no lang code */
  818. put_bits(&s->pb, 1, 0); /* no audio production info */
  819. put_bits(&s->pb, 1, 0); /* no copyright */
  820. put_bits(&s->pb, 1, 1); /* original bitstream */
  821. put_bits(&s->pb, 1, 0); /* no time code 1 */
  822. put_bits(&s->pb, 1, 0); /* no time code 2 */
  823. put_bits(&s->pb, 1, 0); /* no addtional bit stream info */
  824. }
  825. /* symetric quantization on 'levels' levels */
  826. static inline int sym_quant(int c, int e, int levels)
  827. {
  828. int v;
  829. if (c >= 0) {
  830. v = (levels * (c << e)) >> 24;
  831. v = (v + 1) >> 1;
  832. v = (levels >> 1) + v;
  833. } else {
  834. v = (levels * ((-c) << e)) >> 24;
  835. v = (v + 1) >> 1;
  836. v = (levels >> 1) - v;
  837. }
  838. assert (v >= 0 && v < levels);
  839. return v;
  840. }
  841. /* asymetric quantization on 2^qbits levels */
  842. static inline int asym_quant(int c, int e, int qbits)
  843. {
  844. int lshift, m, v;
  845. lshift = e + qbits - 24;
  846. if (lshift >= 0)
  847. v = c << lshift;
  848. else
  849. v = c >> (-lshift);
  850. /* rounding */
  851. v = (v + 1) >> 1;
  852. m = (1 << (qbits-1));
  853. if (v >= m)
  854. v = m - 1;
  855. assert(v >= -m);
  856. return v & ((1 << qbits)-1);
  857. }
  858. /* Output one audio block. There are NB_BLOCKS audio blocks in one AC3
  859. frame */
  860. static void output_audio_block(AC3EncodeContext *s,
  861. uint8_t exp_strategy[AC3_MAX_CHANNELS],
  862. uint8_t encoded_exp[AC3_MAX_CHANNELS][N/2],
  863. uint8_t bap[AC3_MAX_CHANNELS][N/2],
  864. int32_t mdct_coefs[AC3_MAX_CHANNELS][N/2],
  865. int8_t global_exp[AC3_MAX_CHANNELS],
  866. int block_num)
  867. {
  868. int ch, nb_groups, group_size, i, baie;
  869. uint8_t *p;
  870. uint16_t qmant[AC3_MAX_CHANNELS][N/2];
  871. int exp0, exp1;
  872. int mant1_cnt, mant2_cnt, mant4_cnt;
  873. uint16_t *qmant1_ptr, *qmant2_ptr, *qmant4_ptr;
  874. int delta0, delta1, delta2;
  875. for(ch=0;ch<s->nb_channels;ch++)
  876. put_bits(&s->pb, 1, 0); /* 512 point MDCT */
  877. for(ch=0;ch<s->nb_channels;ch++)
  878. put_bits(&s->pb, 1, 1); /* no dither */
  879. put_bits(&s->pb, 1, 0); /* no dynamic range */
  880. if (block_num == 0) {
  881. /* for block 0, even if no coupling, we must say it. This is a
  882. waste of bit :-) */
  883. put_bits(&s->pb, 1, 1); /* coupling strategy present */
  884. put_bits(&s->pb, 1, 0); /* no coupling strategy */
  885. } else {
  886. put_bits(&s->pb, 1, 0); /* no new coupling strategy */
  887. }
  888. if (s->acmod == 2) {
  889. put_bits(&s->pb, 1, 0); /* no matrixing (but should be used in the future) */
  890. }
  891. #if defined(DEBUG)
  892. {
  893. static int count = 0;
  894. printf("Block #%d (%d)\n", block_num, count++);
  895. }
  896. #endif
  897. /* exponent strategy */
  898. for(ch=0;ch<s->nb_channels;ch++) {
  899. put_bits(&s->pb, 2, exp_strategy[ch]);
  900. }
  901. if (s->lfe) {
  902. put_bits(&s->pb, 1, exp_strategy[s->lfe_channel]);
  903. }
  904. for(ch=0;ch<s->nb_channels;ch++) {
  905. if (exp_strategy[ch] != EXP_REUSE)
  906. put_bits(&s->pb, 6, s->chbwcod[ch]);
  907. }
  908. /* exponents */
  909. for (ch = 0; ch < s->nb_all_channels; ch++) {
  910. switch(exp_strategy[ch]) {
  911. case EXP_REUSE:
  912. continue;
  913. case EXP_D15:
  914. group_size = 1;
  915. break;
  916. case EXP_D25:
  917. group_size = 2;
  918. break;
  919. default:
  920. case EXP_D45:
  921. group_size = 4;
  922. break;
  923. }
  924. nb_groups = (s->nb_coefs[ch] + (group_size * 3) - 4) / (3 * group_size);
  925. p = encoded_exp[ch];
  926. /* first exponent */
  927. exp1 = *p++;
  928. put_bits(&s->pb, 4, exp1);
  929. /* next ones are delta encoded */
  930. for(i=0;i<nb_groups;i++) {
  931. /* merge three delta in one code */
  932. exp0 = exp1;
  933. exp1 = p[0];
  934. p += group_size;
  935. delta0 = exp1 - exp0 + 2;
  936. exp0 = exp1;
  937. exp1 = p[0];
  938. p += group_size;
  939. delta1 = exp1 - exp0 + 2;
  940. exp0 = exp1;
  941. exp1 = p[0];
  942. p += group_size;
  943. delta2 = exp1 - exp0 + 2;
  944. put_bits(&s->pb, 7, ((delta0 * 5 + delta1) * 5) + delta2);
  945. }
  946. if (ch != s->lfe_channel)
  947. put_bits(&s->pb, 2, 0); /* no gain range info */
  948. }
  949. /* bit allocation info */
  950. baie = (block_num == 0);
  951. put_bits(&s->pb, 1, baie);
  952. if (baie) {
  953. put_bits(&s->pb, 2, s->sdecaycod);
  954. put_bits(&s->pb, 2, s->fdecaycod);
  955. put_bits(&s->pb, 2, s->sgaincod);
  956. put_bits(&s->pb, 2, s->dbkneecod);
  957. put_bits(&s->pb, 3, s->floorcod);
  958. }
  959. /* snr offset */
  960. put_bits(&s->pb, 1, baie); /* always present with bai */
  961. if (baie) {
  962. put_bits(&s->pb, 6, s->csnroffst);
  963. for(ch=0;ch<s->nb_all_channels;ch++) {
  964. put_bits(&s->pb, 4, s->fsnroffst[ch]);
  965. put_bits(&s->pb, 3, s->fgaincod[ch]);
  966. }
  967. }
  968. put_bits(&s->pb, 1, 0); /* no delta bit allocation */
  969. put_bits(&s->pb, 1, 0); /* no data to skip */
  970. /* mantissa encoding : we use two passes to handle the grouping. A
  971. one pass method may be faster, but it would necessitate to
  972. modify the output stream. */
  973. /* first pass: quantize */
  974. mant1_cnt = mant2_cnt = mant4_cnt = 0;
  975. qmant1_ptr = qmant2_ptr = qmant4_ptr = NULL;
  976. for (ch = 0; ch < s->nb_all_channels; ch++) {
  977. int b, c, e, v;
  978. for(i=0;i<s->nb_coefs[ch];i++) {
  979. c = mdct_coefs[ch][i];
  980. e = encoded_exp[ch][i] - global_exp[ch];
  981. b = bap[ch][i];
  982. switch(b) {
  983. case 0:
  984. v = 0;
  985. break;
  986. case 1:
  987. v = sym_quant(c, e, 3);
  988. switch(mant1_cnt) {
  989. case 0:
  990. qmant1_ptr = &qmant[ch][i];
  991. v = 9 * v;
  992. mant1_cnt = 1;
  993. break;
  994. case 1:
  995. *qmant1_ptr += 3 * v;
  996. mant1_cnt = 2;
  997. v = 128;
  998. break;
  999. default:
  1000. *qmant1_ptr += v;
  1001. mant1_cnt = 0;
  1002. v = 128;
  1003. break;
  1004. }
  1005. break;
  1006. case 2:
  1007. v = sym_quant(c, e, 5);
  1008. switch(mant2_cnt) {
  1009. case 0:
  1010. qmant2_ptr = &qmant[ch][i];
  1011. v = 25 * v;
  1012. mant2_cnt = 1;
  1013. break;
  1014. case 1:
  1015. *qmant2_ptr += 5 * v;
  1016. mant2_cnt = 2;
  1017. v = 128;
  1018. break;
  1019. default:
  1020. *qmant2_ptr += v;
  1021. mant2_cnt = 0;
  1022. v = 128;
  1023. break;
  1024. }
  1025. break;
  1026. case 3:
  1027. v = sym_quant(c, e, 7);
  1028. break;
  1029. case 4:
  1030. v = sym_quant(c, e, 11);
  1031. switch(mant4_cnt) {
  1032. case 0:
  1033. qmant4_ptr = &qmant[ch][i];
  1034. v = 11 * v;
  1035. mant4_cnt = 1;
  1036. break;
  1037. default:
  1038. *qmant4_ptr += v;
  1039. mant4_cnt = 0;
  1040. v = 128;
  1041. break;
  1042. }
  1043. break;
  1044. case 5:
  1045. v = sym_quant(c, e, 15);
  1046. break;
  1047. case 14:
  1048. v = asym_quant(c, e, 14);
  1049. break;
  1050. case 15:
  1051. v = asym_quant(c, e, 16);
  1052. break;
  1053. default:
  1054. v = asym_quant(c, e, b - 1);
  1055. break;
  1056. }
  1057. qmant[ch][i] = v;
  1058. }
  1059. }
  1060. /* second pass : output the values */
  1061. for (ch = 0; ch < s->nb_all_channels; ch++) {
  1062. int b, q;
  1063. for(i=0;i<s->nb_coefs[ch];i++) {
  1064. q = qmant[ch][i];
  1065. b = bap[ch][i];
  1066. switch(b) {
  1067. case 0:
  1068. break;
  1069. case 1:
  1070. if (q != 128)
  1071. put_bits(&s->pb, 5, q);
  1072. break;
  1073. case 2:
  1074. if (q != 128)
  1075. put_bits(&s->pb, 7, q);
  1076. break;
  1077. case 3:
  1078. put_bits(&s->pb, 3, q);
  1079. break;
  1080. case 4:
  1081. if (q != 128)
  1082. put_bits(&s->pb, 7, q);
  1083. break;
  1084. case 14:
  1085. put_bits(&s->pb, 14, q);
  1086. break;
  1087. case 15:
  1088. put_bits(&s->pb, 16, q);
  1089. break;
  1090. default:
  1091. put_bits(&s->pb, b - 1, q);
  1092. break;
  1093. }
  1094. }
  1095. }
  1096. }
  1097. /* compute the ac3 crc */
  1098. #define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16))
  1099. static void ac3_crc_init(void)
  1100. {
  1101. unsigned int c, n, k;
  1102. for(n=0;n<256;n++) {
  1103. c = n << 8;
  1104. for (k = 0; k < 8; k++) {
  1105. if (c & (1 << 15))
  1106. c = ((c << 1) & 0xffff) ^ (CRC16_POLY & 0xffff);
  1107. else
  1108. c = c << 1;
  1109. }
  1110. crc_table[n] = c;
  1111. }
  1112. }
  1113. static unsigned int ac3_crc(uint8_t *data, int n, unsigned int crc)
  1114. {
  1115. int i;
  1116. for(i=0;i<n;i++) {
  1117. crc = (crc_table[data[i] ^ (crc >> 8)] ^ (crc << 8)) & 0xffff;
  1118. }
  1119. return crc;
  1120. }
  1121. static unsigned int mul_poly(unsigned int a, unsigned int b, unsigned int poly)
  1122. {
  1123. unsigned int c;
  1124. c = 0;
  1125. while (a) {
  1126. if (a & 1)
  1127. c ^= b;
  1128. a = a >> 1;
  1129. b = b << 1;
  1130. if (b & (1 << 16))
  1131. b ^= poly;
  1132. }
  1133. return c;
  1134. }
  1135. static unsigned int pow_poly(unsigned int a, unsigned int n, unsigned int poly)
  1136. {
  1137. unsigned int r;
  1138. r = 1;
  1139. while (n) {
  1140. if (n & 1)
  1141. r = mul_poly(r, a, poly);
  1142. a = mul_poly(a, a, poly);
  1143. n >>= 1;
  1144. }
  1145. return r;
  1146. }
  1147. /* compute log2(max(abs(tab[]))) */
  1148. static int log2_tab(int16_t *tab, int n)
  1149. {
  1150. int i, v;
  1151. v = 0;
  1152. for(i=0;i<n;i++) {
  1153. v |= abs(tab[i]);
  1154. }
  1155. return av_log2(v);
  1156. }
  1157. static void lshift_tab(int16_t *tab, int n, int lshift)
  1158. {
  1159. int i;
  1160. if (lshift > 0) {
  1161. for(i=0;i<n;i++) {
  1162. tab[i] <<= lshift;
  1163. }
  1164. } else if (lshift < 0) {
  1165. lshift = -lshift;
  1166. for(i=0;i<n;i++) {
  1167. tab[i] >>= lshift;
  1168. }
  1169. }
  1170. }
  1171. /* fill the end of the frame and compute the two crcs */
  1172. static int output_frame_end(AC3EncodeContext *s)
  1173. {
  1174. int frame_size, frame_size_58, n, crc1, crc2, crc_inv;
  1175. uint8_t *frame;
  1176. frame_size = s->frame_size; /* frame size in words */
  1177. /* align to 8 bits */
  1178. flush_put_bits(&s->pb);
  1179. /* add zero bytes to reach the frame size */
  1180. frame = s->pb.buf;
  1181. n = 2 * s->frame_size - (pbBufPtr(&s->pb) - frame) - 2;
  1182. assert(n >= 0);
  1183. memset(pbBufPtr(&s->pb), 0, n);
  1184. /* Now we must compute both crcs : this is not so easy for crc1
  1185. because it is at the beginning of the data... */
  1186. frame_size_58 = (frame_size >> 1) + (frame_size >> 3);
  1187. crc1 = ac3_crc(frame + 4, (2 * frame_size_58) - 4, 0);
  1188. /* XXX: could precompute crc_inv */
  1189. crc_inv = pow_poly((CRC16_POLY >> 1), (16 * frame_size_58) - 16, CRC16_POLY);
  1190. crc1 = mul_poly(crc_inv, crc1, CRC16_POLY);
  1191. frame[2] = crc1 >> 8;
  1192. frame[3] = crc1;
  1193. crc2 = ac3_crc(frame + 2 * frame_size_58, (frame_size - frame_size_58) * 2 - 2, 0);
  1194. frame[2*frame_size - 2] = crc2 >> 8;
  1195. frame[2*frame_size - 1] = crc2;
  1196. // printf("n=%d frame_size=%d\n", n, frame_size);
  1197. return frame_size * 2;
  1198. }
  1199. static int AC3_encode_frame(AVCodecContext *avctx,
  1200. unsigned char *frame, int buf_size, void *data)
  1201. {
  1202. AC3EncodeContext *s = avctx->priv_data;
  1203. short *samples = data;
  1204. int i, j, k, v, ch;
  1205. int16_t input_samples[N];
  1206. int32_t mdct_coef[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
  1207. uint8_t exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
  1208. uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS];
  1209. uint8_t encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
  1210. uint8_t bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
  1211. int8_t exp_samples[NB_BLOCKS][AC3_MAX_CHANNELS];
  1212. int frame_bits;
  1213. frame_bits = 0;
  1214. for(ch=0;ch<s->nb_all_channels;ch++) {
  1215. /* fixed mdct to the six sub blocks & exponent computation */
  1216. for(i=0;i<NB_BLOCKS;i++) {
  1217. int16_t *sptr;
  1218. int sinc;
  1219. /* compute input samples */
  1220. memcpy(input_samples, s->last_samples[ch], N/2 * sizeof(int16_t));
  1221. sinc = s->nb_all_channels;
  1222. sptr = samples + (sinc * (N/2) * i) + ch;
  1223. for(j=0;j<N/2;j++) {
  1224. v = *sptr;
  1225. input_samples[j + N/2] = v;
  1226. s->last_samples[ch][j] = v;
  1227. sptr += sinc;
  1228. }
  1229. /* apply the MDCT window */
  1230. for(j=0;j<N/2;j++) {
  1231. input_samples[j] = MUL16(input_samples[j],
  1232. ac3_window[j]) >> 15;
  1233. input_samples[N-j-1] = MUL16(input_samples[N-j-1],
  1234. ac3_window[j]) >> 15;
  1235. }
  1236. /* Normalize the samples to use the maximum available
  1237. precision */
  1238. v = 14 - log2_tab(input_samples, N);
  1239. if (v < 0)
  1240. v = 0;
  1241. exp_samples[i][ch] = v - 8;
  1242. lshift_tab(input_samples, N, v);
  1243. /* do the MDCT */
  1244. mdct512(mdct_coef[i][ch], input_samples);
  1245. /* compute "exponents". We take into account the
  1246. normalization there */
  1247. for(j=0;j<N/2;j++) {
  1248. int e;
  1249. v = abs(mdct_coef[i][ch][j]);
  1250. if (v == 0)
  1251. e = 24;
  1252. else {
  1253. e = 23 - av_log2(v) + exp_samples[i][ch];
  1254. if (e >= 24) {
  1255. e = 24;
  1256. mdct_coef[i][ch][j] = 0;
  1257. }
  1258. }
  1259. exp[i][ch][j] = e;
  1260. }
  1261. }
  1262. compute_exp_strategy(exp_strategy, exp, ch, ch == s->lfe_channel);
  1263. /* compute the exponents as the decoder will see them. The
  1264. EXP_REUSE case must be handled carefully : we select the
  1265. min of the exponents */
  1266. i = 0;
  1267. while (i < NB_BLOCKS) {
  1268. j = i + 1;
  1269. while (j < NB_BLOCKS && exp_strategy[j][ch] == EXP_REUSE) {
  1270. exponent_min(exp[i][ch], exp[j][ch], s->nb_coefs[ch]);
  1271. j++;
  1272. }
  1273. frame_bits += encode_exp(encoded_exp[i][ch],
  1274. exp[i][ch], s->nb_coefs[ch],
  1275. exp_strategy[i][ch]);
  1276. /* copy encoded exponents for reuse case */
  1277. for(k=i+1;k<j;k++) {
  1278. memcpy(encoded_exp[k][ch], encoded_exp[i][ch],
  1279. s->nb_coefs[ch] * sizeof(uint8_t));
  1280. }
  1281. i = j;
  1282. }
  1283. }
  1284. compute_bit_allocation(s, bap, encoded_exp, exp_strategy, frame_bits);
  1285. /* everything is known... let's output the frame */
  1286. output_frame_header(s, frame);
  1287. for(i=0;i<NB_BLOCKS;i++) {
  1288. output_audio_block(s, exp_strategy[i], encoded_exp[i],
  1289. bap[i], mdct_coef[i], exp_samples[i], i);
  1290. }
  1291. return output_frame_end(s);
  1292. }
  1293. static int AC3_encode_close(AVCodecContext *avctx)
  1294. {
  1295. av_freep(&avctx->coded_frame);
  1296. return 0;
  1297. }
  1298. #if 0
  1299. /*************************************************************************/
  1300. /* TEST */
  1301. #define FN (N/4)
  1302. void fft_test(void)
  1303. {
  1304. IComplex in[FN], in1[FN];
  1305. int k, n, i;
  1306. float sum_re, sum_im, a;
  1307. /* FFT test */
  1308. for(i=0;i<FN;i++) {
  1309. in[i].re = random() % 65535 - 32767;
  1310. in[i].im = random() % 65535 - 32767;
  1311. in1[i] = in[i];
  1312. }
  1313. fft(in, 7);
  1314. /* do it by hand */
  1315. for(k=0;k<FN;k++) {
  1316. sum_re = 0;
  1317. sum_im = 0;
  1318. for(n=0;n<FN;n++) {
  1319. a = -2 * M_PI * (n * k) / FN;
  1320. sum_re += in1[n].re * cos(a) - in1[n].im * sin(a);
  1321. sum_im += in1[n].re * sin(a) + in1[n].im * cos(a);
  1322. }
  1323. printf("%3d: %6d,%6d %6.0f,%6.0f\n",
  1324. k, in[k].re, in[k].im, sum_re / FN, sum_im / FN);
  1325. }
  1326. }
  1327. void mdct_test(void)
  1328. {
  1329. int16_t input[N];
  1330. int32_t output[N/2];
  1331. float input1[N];
  1332. float output1[N/2];
  1333. float s, a, err, e, emax;
  1334. int i, k, n;
  1335. for(i=0;i<N;i++) {
  1336. input[i] = (random() % 65535 - 32767) * 9 / 10;
  1337. input1[i] = input[i];
  1338. }
  1339. mdct512(output, input);
  1340. /* do it by hand */
  1341. for(k=0;k<N/2;k++) {
  1342. s = 0;
  1343. for(n=0;n<N;n++) {
  1344. a = (2*M_PI*(2*n+1+N/2)*(2*k+1) / (4 * N));
  1345. s += input1[n] * cos(a);
  1346. }
  1347. output1[k] = -2 * s / N;
  1348. }
  1349. err = 0;
  1350. emax = 0;
  1351. for(i=0;i<N/2;i++) {
  1352. printf("%3d: %7d %7.0f\n", i, output[i], output1[i]);
  1353. e = output[i] - output1[i];
  1354. if (e > emax)
  1355. emax = e;
  1356. err += e * e;
  1357. }
  1358. printf("err2=%f emax=%f\n", err / (N/2), emax);
  1359. }
  1360. void test_ac3(void)
  1361. {
  1362. AC3EncodeContext ctx;
  1363. unsigned char frame[AC3_MAX_CODED_FRAME_SIZE];
  1364. short samples[AC3_FRAME_SIZE];
  1365. int ret, i;
  1366. AC3_encode_init(&ctx, 44100, 64000, 1);
  1367. fft_test();
  1368. mdct_test();
  1369. for(i=0;i<AC3_FRAME_SIZE;i++)
  1370. samples[i] = (int)(sin(2*M_PI*i*1000.0/44100) * 10000);
  1371. ret = AC3_encode_frame(&ctx, frame, samples);
  1372. printf("ret=%d\n", ret);
  1373. }
  1374. #endif
  1375. AVCodec ac3_encoder = {
  1376. "ac3",
  1377. CODEC_TYPE_AUDIO,
  1378. CODEC_ID_AC3,
  1379. sizeof(AC3EncodeContext),
  1380. AC3_encode_init,
  1381. AC3_encode_frame,
  1382. AC3_encode_close,
  1383. NULL,
  1384. };