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.

198 lines
5.8KB

  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. static inline int calc_lowcomp1(int a, int b0, int b1, int c)
  29. {
  30. if ((b0 + 256) == b1) {
  31. a = c;
  32. } else if (b0 > b1) {
  33. a = FFMAX(a - 64, 0);
  34. }
  35. return a;
  36. }
  37. static inline int calc_lowcomp(int a, int b0, int b1, int bin)
  38. {
  39. if (bin < 7) {
  40. return calc_lowcomp1(a, b0, b1, 384);
  41. } else if (bin < 20) {
  42. return calc_lowcomp1(a, b0, b1, 320);
  43. } else {
  44. return FFMAX(a - 128, 0);
  45. }
  46. }
  47. /* AC3 bit allocation. The algorithm is the one described in the AC3
  48. spec. */
  49. void ac3_parametric_bit_allocation(AC3BitAllocParameters *s, uint8_t *bap,
  50. int8_t *exp, int start, int end,
  51. int snroffset, int fgain, int is_lfe,
  52. int deltbae,int deltnseg,
  53. uint8_t *deltoffst, uint8_t *deltlen, uint8_t *deltba)
  54. {
  55. int bin,i,j,k,end1,v,bndstrt,bndend,lowcomp,begin;
  56. int fastleak,slowleak,address,tmp;
  57. int16_t psd[256]; /* scaled exponents */
  58. int16_t bndpsd[50]; /* interpolated exponents */
  59. int16_t excite[50]; /* excitation */
  60. int16_t mask[50]; /* masking value */
  61. /* exponent mapping to PSD */
  62. for(bin=start;bin<end;bin++) {
  63. psd[bin]=(3072 - (exp[bin] << 7));
  64. }
  65. /* PSD integration */
  66. j=start;
  67. k=masktab[start];
  68. do {
  69. v=psd[j];
  70. j++;
  71. end1 = FFMIN(bndtab[k+1], end);
  72. for(i=j;i<end1;i++) {
  73. /* logadd */
  74. int adr = FFMIN(FFABS(v - psd[j]) >> 1, 255);
  75. v = FFMAX(v, psd[j]) + latab[adr];
  76. j++;
  77. }
  78. bndpsd[k]=v;
  79. k++;
  80. } while (end > bndtab[k]);
  81. /* excitation function */
  82. bndstrt = masktab[start];
  83. bndend = masktab[end-1] + 1;
  84. if (bndstrt == 0) {
  85. lowcomp = 0;
  86. lowcomp = calc_lowcomp1(lowcomp, bndpsd[0], bndpsd[1], 384);
  87. excite[0] = bndpsd[0] - fgain - lowcomp;
  88. lowcomp = calc_lowcomp1(lowcomp, bndpsd[1], bndpsd[2], 384);
  89. excite[1] = bndpsd[1] - fgain - lowcomp;
  90. begin = 7;
  91. for (bin = 2; bin < 7; bin++) {
  92. if (!(is_lfe && bin == 6))
  93. lowcomp = calc_lowcomp1(lowcomp, bndpsd[bin], bndpsd[bin+1], 384);
  94. fastleak = bndpsd[bin] - fgain;
  95. slowleak = bndpsd[bin] - s->sgain;
  96. excite[bin] = fastleak - lowcomp;
  97. if (!(is_lfe && bin == 6)) {
  98. if (bndpsd[bin] <= bndpsd[bin+1]) {
  99. begin = bin + 1;
  100. break;
  101. }
  102. }
  103. }
  104. end1=bndend;
  105. if (end1 > 22) end1=22;
  106. for (bin = begin; bin < end1; bin++) {
  107. if (!(is_lfe && bin == 6))
  108. lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin+1], bin);
  109. fastleak = FFMAX(fastleak - s->fdecay, bndpsd[bin] - fgain);
  110. slowleak = FFMAX(slowleak - s->sdecay, bndpsd[bin] - s->sgain);
  111. excite[bin] = FFMAX(fastleak - lowcomp, slowleak);
  112. }
  113. begin = 22;
  114. } else {
  115. /* coupling channel */
  116. begin = bndstrt;
  117. fastleak = (s->cplfleak << 8) + 768;
  118. slowleak = (s->cplsleak << 8) + 768;
  119. }
  120. for (bin = begin; bin < bndend; bin++) {
  121. fastleak = FFMAX(fastleak - s->fdecay, bndpsd[bin] - fgain);
  122. slowleak = FFMAX(slowleak - s->sdecay, bndpsd[bin] - s->sgain);
  123. excite[bin] = FFMAX(fastleak, slowleak);
  124. }
  125. /* compute masking curve */
  126. for (bin = bndstrt; bin < bndend; bin++) {
  127. tmp = s->dbknee - bndpsd[bin];
  128. if (tmp > 0) {
  129. excite[bin] += tmp >> 2;
  130. }
  131. mask[bin] = FFMAX(hth[bin >> s->halfratecod][s->fscod], excite[bin]);
  132. }
  133. /* delta bit allocation */
  134. if (deltbae == 0 || deltbae == 1) {
  135. int band, seg, delta;
  136. band = 0;
  137. for (seg = 0; seg < deltnseg; seg++) {
  138. band += deltoffst[seg];
  139. if (deltba[seg] >= 4) {
  140. delta = (deltba[seg] - 3) << 7;
  141. } else {
  142. delta = (deltba[seg] - 4) << 7;
  143. }
  144. for (k = 0; k < deltlen[seg]; k++) {
  145. mask[band] += delta;
  146. band++;
  147. }
  148. }
  149. }
  150. /* compute bit allocation */
  151. i = start;
  152. j = masktab[start];
  153. do {
  154. v = (FFMAX(mask[j] - snroffset - s->floor, 0) & 0x1FE0) + s->floor;
  155. end1 = FFMIN(bndtab[j] + bndsz[j], end);
  156. for (k = i; k < end1; k++) {
  157. address = av_clip((psd[i] - v) >> 5, 0, 63);
  158. bap[i] = baptab[address];
  159. i++;
  160. }
  161. } while (end > bndtab[j++]);
  162. }
  163. void ac3_common_init(void)
  164. {
  165. int i, j, k, l, v;
  166. /* compute bndtab and masktab from bandsz */
  167. k = 0;
  168. l = 0;
  169. for(i=0;i<50;i++) {
  170. bndtab[i] = l;
  171. v = bndsz[i];
  172. for(j=0;j<v;j++) masktab[k++]=i;
  173. l += v;
  174. }
  175. bndtab[50] = l;
  176. }