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.

1603 lines
44KB

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