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.

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