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.

240 lines
7.2KB

  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 "bitstream.h"
  28. static uint8_t bndtab[51];
  29. static uint8_t masktab[253];
  30. static inline int calc_lowcomp1(int a, int b0, int b1, int c)
  31. {
  32. if ((b0 + 256) == b1) {
  33. a = c;
  34. } else if (b0 > b1) {
  35. a = FFMAX(a - 64, 0);
  36. }
  37. return a;
  38. }
  39. static inline int calc_lowcomp(int a, int b0, int b1, int bin)
  40. {
  41. if (bin < 7) {
  42. return calc_lowcomp1(a, b0, b1, 384);
  43. } else if (bin < 20) {
  44. return calc_lowcomp1(a, b0, b1, 320);
  45. } else {
  46. return FFMAX(a - 128, 0);
  47. }
  48. }
  49. void ff_ac3_bit_alloc_calc_psd(int8_t *exp, int start, int end, int16_t *psd,
  50. int16_t *bndpsd)
  51. {
  52. int bin, i, j, k, end1, v;
  53. /* exponent mapping to PSD */
  54. for(bin=start;bin<end;bin++) {
  55. psd[bin]=(3072 - (exp[bin] << 7));
  56. }
  57. /* PSD integration */
  58. j=start;
  59. k=masktab[start];
  60. do {
  61. v=psd[j];
  62. j++;
  63. end1 = FFMIN(bndtab[k+1], end);
  64. for(i=j;i<end1;i++) {
  65. /* logadd */
  66. int adr = FFMIN(FFABS(v - psd[j]) >> 1, 255);
  67. v = FFMAX(v, psd[j]) + ff_ac3_latab[adr];
  68. j++;
  69. }
  70. bndpsd[k]=v;
  71. k++;
  72. } while (end > bndtab[k]);
  73. }
  74. void ff_ac3_bit_alloc_calc_mask(AC3BitAllocParameters *s, int16_t *bndpsd,
  75. int start, int end, int fgain, int is_lfe,
  76. int deltbae, int deltnseg, uint8_t *deltoffst,
  77. uint8_t *deltlen, uint8_t *deltba,
  78. int16_t *mask)
  79. {
  80. int16_t excite[50]; /* excitation */
  81. int bin, k;
  82. int bndstrt, bndend, begin, end1, tmp;
  83. int lowcomp, fastleak, slowleak;
  84. /* excitation function */
  85. bndstrt = masktab[start];
  86. bndend = masktab[end-1] + 1;
  87. if (bndstrt == 0) {
  88. lowcomp = 0;
  89. lowcomp = calc_lowcomp1(lowcomp, bndpsd[0], bndpsd[1], 384);
  90. excite[0] = bndpsd[0] - fgain - lowcomp;
  91. lowcomp = calc_lowcomp1(lowcomp, bndpsd[1], bndpsd[2], 384);
  92. excite[1] = bndpsd[1] - fgain - lowcomp;
  93. begin = 7;
  94. for (bin = 2; bin < 7; bin++) {
  95. if (!(is_lfe && bin == 6))
  96. lowcomp = calc_lowcomp1(lowcomp, bndpsd[bin], bndpsd[bin+1], 384);
  97. fastleak = bndpsd[bin] - fgain;
  98. slowleak = bndpsd[bin] - s->sgain;
  99. excite[bin] = fastleak - lowcomp;
  100. if (!(is_lfe && bin == 6)) {
  101. if (bndpsd[bin] <= bndpsd[bin+1]) {
  102. begin = bin + 1;
  103. break;
  104. }
  105. }
  106. }
  107. end1=bndend;
  108. if (end1 > 22) end1=22;
  109. for (bin = begin; bin < end1; bin++) {
  110. if (!(is_lfe && bin == 6))
  111. lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin+1], bin);
  112. fastleak = FFMAX(fastleak - s->fdecay, bndpsd[bin] - fgain);
  113. slowleak = FFMAX(slowleak - s->sdecay, bndpsd[bin] - s->sgain);
  114. excite[bin] = FFMAX(fastleak - lowcomp, slowleak);
  115. }
  116. begin = 22;
  117. } else {
  118. /* coupling channel */
  119. begin = bndstrt;
  120. fastleak = (s->cplfleak << 8) + 768;
  121. slowleak = (s->cplsleak << 8) + 768;
  122. }
  123. for (bin = begin; bin < bndend; bin++) {
  124. fastleak = FFMAX(fastleak - s->fdecay, bndpsd[bin] - fgain);
  125. slowleak = FFMAX(slowleak - s->sdecay, bndpsd[bin] - s->sgain);
  126. excite[bin] = FFMAX(fastleak, slowleak);
  127. }
  128. /* compute masking curve */
  129. for (bin = bndstrt; bin < bndend; bin++) {
  130. tmp = s->dbknee - bndpsd[bin];
  131. if (tmp > 0) {
  132. excite[bin] += tmp >> 2;
  133. }
  134. mask[bin] = FFMAX(ff_ac3_hth[bin >> s->halfratecod][s->fscod], excite[bin]);
  135. }
  136. /* delta bit allocation */
  137. if (deltbae == DBA_REUSE || deltbae == DBA_NEW) {
  138. int band, seg, delta;
  139. band = 0;
  140. for (seg = 0; seg < deltnseg; seg++) {
  141. band += deltoffst[seg];
  142. if (deltba[seg] >= 4) {
  143. delta = (deltba[seg] - 3) << 7;
  144. } else {
  145. delta = (deltba[seg] - 4) << 7;
  146. }
  147. for (k = 0; k < deltlen[seg]; k++) {
  148. mask[band] += delta;
  149. band++;
  150. }
  151. }
  152. }
  153. }
  154. void ff_ac3_bit_alloc_calc_bap(int16_t *mask, int16_t *psd, int start, int end,
  155. int snroffset, int floor, uint8_t *bap)
  156. {
  157. int i, j, k, end1, v, address;
  158. /* special case, if snroffset is -960, set all bap's to zero */
  159. if(snroffset == -960) {
  160. memset(bap, 0, 256);
  161. return;
  162. }
  163. i = start;
  164. j = masktab[start];
  165. do {
  166. v = (FFMAX(mask[j] - snroffset - floor, 0) & 0x1FE0) + floor;
  167. end1 = FFMIN(bndtab[j] + ff_ac3_bndsz[j], end);
  168. for (k = i; k < end1; k++) {
  169. address = av_clip((psd[i] - v) >> 5, 0, 63);
  170. bap[i] = ff_ac3_baptab[address];
  171. i++;
  172. }
  173. } while (end > bndtab[j++]);
  174. }
  175. /* AC3 bit allocation. The algorithm is the one described in the AC3
  176. spec. */
  177. void ac3_parametric_bit_allocation(AC3BitAllocParameters *s, uint8_t *bap,
  178. int8_t *exp, int start, int end,
  179. int snroffset, int fgain, int is_lfe,
  180. int deltbae,int deltnseg,
  181. uint8_t *deltoffst, uint8_t *deltlen,
  182. uint8_t *deltba)
  183. {
  184. int16_t psd[256]; /* scaled exponents */
  185. int16_t bndpsd[50]; /* interpolated exponents */
  186. int16_t mask[50]; /* masking value */
  187. ff_ac3_bit_alloc_calc_psd(exp, start, end, psd, bndpsd);
  188. ff_ac3_bit_alloc_calc_mask(s, bndpsd, start, end, fgain, is_lfe,
  189. deltbae, deltnseg, deltoffst, deltlen, deltba,
  190. mask);
  191. ff_ac3_bit_alloc_calc_bap(mask, psd, start, end, snroffset, s->floor, bap);
  192. }
  193. /**
  194. * Initializes some tables.
  195. * note: This function must remain thread safe because it is called by the
  196. * AVParser init code.
  197. */
  198. void ac3_common_init(void)
  199. {
  200. int i, j, k, l, v;
  201. /* compute bndtab and masktab from bandsz */
  202. k = 0;
  203. l = 0;
  204. for(i=0;i<50;i++) {
  205. bndtab[i] = l;
  206. v = ff_ac3_bndsz[i];
  207. for(j=0;j<v;j++) masktab[k++]=i;
  208. l += v;
  209. }
  210. bndtab[50] = l;
  211. }