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.

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