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.

1458 lines
39KB

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