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.

238 lines
7.1KB

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