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.

1544 lines
42KB

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