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