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.

262 lines
7.6KB

  1. /*
  2. * Common code between AC3 encoder and decoder
  3. * Copyright (c) 2000 Fabrice Bellard.
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file ac3.c
  23. * Common code between AC3 encoder and decoder.
  24. */
  25. #include "avcodec.h"
  26. #include "ac3.h"
  27. #include "ac3tab.h"
  28. #include "bitstream.h"
  29. static inline int calc_lowcomp1(int a, int b0, int b1, int c)
  30. {
  31. if ((b0 + 256) == b1) {
  32. a = c;
  33. } else if (b0 > b1) {
  34. a = FFMAX(a - 64, 0);
  35. }
  36. return a;
  37. }
  38. static inline int calc_lowcomp(int a, int b0, int b1, int bin)
  39. {
  40. if (bin < 7) {
  41. return calc_lowcomp1(a, b0, b1, 384);
  42. } else if (bin < 20) {
  43. return calc_lowcomp1(a, b0, b1, 320);
  44. } else {
  45. return FFMAX(a - 128, 0);
  46. }
  47. }
  48. /* AC3 bit allocation. The algorithm is the one described in the AC3
  49. spec. */
  50. void ac3_parametric_bit_allocation(AC3BitAllocParameters *s, uint8_t *bap,
  51. int8_t *exp, int start, int end,
  52. int snroffset, int fgain, int is_lfe,
  53. int deltbae,int deltnseg,
  54. uint8_t *deltoffst, uint8_t *deltlen, uint8_t *deltba)
  55. {
  56. int bin,i,j,k,end1,v,bndstrt,bndend,lowcomp,begin;
  57. int fastleak,slowleak,address,tmp;
  58. int16_t psd[256]; /* scaled exponents */
  59. int16_t bndpsd[50]; /* interpolated exponents */
  60. int16_t excite[50]; /* excitation */
  61. int16_t mask[50]; /* masking value */
  62. /* exponent mapping to PSD */
  63. for(bin=start;bin<end;bin++) {
  64. psd[bin]=(3072 - (exp[bin] << 7));
  65. }
  66. /* PSD integration */
  67. j=start;
  68. k=masktab[start];
  69. do {
  70. v=psd[j];
  71. j++;
  72. end1 = FFMIN(bndtab[k+1], end);
  73. for(i=j;i<end1;i++) {
  74. /* logadd */
  75. int adr = FFMIN(FFABS(v - psd[j]) >> 1, 255);
  76. v = FFMAX(v, psd[j]) + latab[adr];
  77. j++;
  78. }
  79. bndpsd[k]=v;
  80. k++;
  81. } while (end > bndtab[k]);
  82. /* excitation function */
  83. bndstrt = masktab[start];
  84. bndend = masktab[end-1] + 1;
  85. if (bndstrt == 0) {
  86. lowcomp = 0;
  87. lowcomp = calc_lowcomp1(lowcomp, bndpsd[0], bndpsd[1], 384);
  88. excite[0] = bndpsd[0] - fgain - lowcomp;
  89. lowcomp = calc_lowcomp1(lowcomp, bndpsd[1], bndpsd[2], 384);
  90. excite[1] = bndpsd[1] - fgain - lowcomp;
  91. begin = 7;
  92. for (bin = 2; bin < 7; bin++) {
  93. if (!(is_lfe && bin == 6))
  94. lowcomp = calc_lowcomp1(lowcomp, bndpsd[bin], bndpsd[bin+1], 384);
  95. fastleak = bndpsd[bin] - fgain;
  96. slowleak = bndpsd[bin] - s->sgain;
  97. excite[bin] = fastleak - lowcomp;
  98. if (!(is_lfe && bin == 6)) {
  99. if (bndpsd[bin] <= bndpsd[bin+1]) {
  100. begin = bin + 1;
  101. break;
  102. }
  103. }
  104. }
  105. end1=bndend;
  106. if (end1 > 22) end1=22;
  107. for (bin = begin; bin < end1; bin++) {
  108. if (!(is_lfe && bin == 6))
  109. lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin+1], bin);
  110. fastleak = FFMAX(fastleak - s->fdecay, bndpsd[bin] - fgain);
  111. slowleak = FFMAX(slowleak - s->sdecay, bndpsd[bin] - s->sgain);
  112. excite[bin] = FFMAX(fastleak - lowcomp, slowleak);
  113. }
  114. begin = 22;
  115. } else {
  116. /* coupling channel */
  117. begin = bndstrt;
  118. fastleak = (s->cplfleak << 8) + 768;
  119. slowleak = (s->cplsleak << 8) + 768;
  120. }
  121. for (bin = begin; bin < bndend; bin++) {
  122. fastleak = FFMAX(fastleak - s->fdecay, bndpsd[bin] - fgain);
  123. slowleak = FFMAX(slowleak - s->sdecay, bndpsd[bin] - s->sgain);
  124. excite[bin] = FFMAX(fastleak, slowleak);
  125. }
  126. /* compute masking curve */
  127. for (bin = bndstrt; bin < bndend; bin++) {
  128. tmp = s->dbknee - bndpsd[bin];
  129. if (tmp > 0) {
  130. excite[bin] += tmp >> 2;
  131. }
  132. mask[bin] = FFMAX(hth[bin >> s->halfratecod][s->fscod], excite[bin]);
  133. }
  134. /* delta bit allocation */
  135. if (deltbae == 0 || deltbae == 1) {
  136. int band, seg, delta;
  137. band = 0;
  138. for (seg = 0; seg < deltnseg; seg++) {
  139. band += deltoffst[seg];
  140. if (deltba[seg] >= 4) {
  141. delta = (deltba[seg] - 3) << 7;
  142. } else {
  143. delta = (deltba[seg] - 4) << 7;
  144. }
  145. for (k = 0; k < deltlen[seg]; k++) {
  146. mask[band] += delta;
  147. band++;
  148. }
  149. }
  150. }
  151. /* compute bit allocation */
  152. i = start;
  153. j = masktab[start];
  154. do {
  155. v = (FFMAX(mask[j] - snroffset - s->floor, 0) & 0x1FE0) + s->floor;
  156. end1 = FFMIN(bndtab[j] + bndsz[j], end);
  157. for (k = i; k < end1; k++) {
  158. address = av_clip((psd[i] - v) >> 5, 0, 63);
  159. bap[i] = baptab[address];
  160. i++;
  161. }
  162. } while (end > bndtab[j++]);
  163. }
  164. /**
  165. * Initializes some tables.
  166. * note: This function must remain thread safe because it is called by the
  167. * AVParser init code.
  168. */
  169. void ac3_common_init(void)
  170. {
  171. int i, j, k, l, v;
  172. /* compute bndtab and masktab from bandsz */
  173. k = 0;
  174. l = 0;
  175. for(i=0;i<50;i++) {
  176. bndtab[i] = l;
  177. v = bndsz[i];
  178. for(j=0;j<v;j++) masktab[k++]=i;
  179. l += v;
  180. }
  181. bndtab[50] = l;
  182. /* generate ff_ac3_frame_sizes table */
  183. for(i=0; i<38; i++) {
  184. int br = ff_ac3_bitratetab[i >> 1];
  185. ff_ac3_frame_sizes[i][0] = ( 2*br );
  186. ff_ac3_frame_sizes[i][1] = (320*br / 147) + (i & 1);
  187. ff_ac3_frame_sizes[i][2] = ( 3*br );
  188. }
  189. }
  190. int ff_ac3_parse_header(const uint8_t buf[7], AC3HeaderInfo *hdr)
  191. {
  192. GetBitContext gbc;
  193. memset(hdr, 0, sizeof(*hdr));
  194. init_get_bits(&gbc, buf, 54);
  195. hdr->sync_word = get_bits(&gbc, 16);
  196. if(hdr->sync_word != 0x0B77)
  197. return -1;
  198. /* read ahead to bsid to make sure this is AC-3, not E-AC-3 */
  199. hdr->bsid = show_bits_long(&gbc, 29) & 0x1F;
  200. if(hdr->bsid > 10)
  201. return -2;
  202. hdr->crc1 = get_bits(&gbc, 16);
  203. hdr->fscod = get_bits(&gbc, 2);
  204. if(hdr->fscod == 3)
  205. return -3;
  206. hdr->frmsizecod = get_bits(&gbc, 6);
  207. if(hdr->frmsizecod > 37)
  208. return -4;
  209. skip_bits(&gbc, 5); // skip bsid, already got it
  210. hdr->bsmod = get_bits(&gbc, 3);
  211. hdr->acmod = get_bits(&gbc, 3);
  212. if((hdr->acmod & 1) && hdr->acmod != 1) {
  213. hdr->cmixlev = get_bits(&gbc, 2);
  214. }
  215. if(hdr->acmod & 4) {
  216. hdr->surmixlev = get_bits(&gbc, 2);
  217. }
  218. if(hdr->acmod == 2) {
  219. hdr->dsurmod = get_bits(&gbc, 2);
  220. }
  221. hdr->lfeon = get_bits1(&gbc);
  222. hdr->halfratecod = FFMAX(hdr->bsid, 8) - 8;
  223. hdr->sample_rate = ff_ac3_freqs[hdr->fscod] >> hdr->halfratecod;
  224. hdr->bit_rate = (ff_ac3_bitratetab[hdr->frmsizecod>>1] * 1000) >> hdr->halfratecod;
  225. hdr->channels = ff_ac3_channels[hdr->acmod] + hdr->lfeon;
  226. hdr->frame_size = ff_ac3_frame_sizes[hdr->frmsizecod][hdr->fscod] * 2;
  227. return 0;
  228. }