ac3.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Common code between the AC-3 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 libavcodec/ac3.c
  23. * Common code between the AC-3 encoder and decoder.
  24. */
  25. #include "avcodec.h"
  26. #include "ac3.h"
  27. #include "bitstream.h"
  28. static uint8_t band_start_tab[51];
  29. static uint8_t bin_to_band_tab[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 *band_psd)
  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=bin_to_band_tab[start];
  60. do {
  61. v=psd[j];
  62. j++;
  63. end1 = FFMIN(band_start_tab[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_log_add_tab[adr];
  68. j++;
  69. }
  70. band_psd[k]=v;
  71. k++;
  72. } while (end > band_start_tab[k]);
  73. }
  74. int ff_ac3_bit_alloc_calc_mask(AC3BitAllocParameters *s, int16_t *band_psd,
  75. int start, int end, int fast_gain, int is_lfe,
  76. int dba_mode, int dba_nsegs, uint8_t *dba_offsets,
  77. uint8_t *dba_lengths, uint8_t *dba_values,
  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 = bin_to_band_tab[start];
  86. bndend = bin_to_band_tab[end-1] + 1;
  87. if (bndstrt == 0) {
  88. lowcomp = 0;
  89. lowcomp = calc_lowcomp1(lowcomp, band_psd[0], band_psd[1], 384);
  90. excite[0] = band_psd[0] - fast_gain - lowcomp;
  91. lowcomp = calc_lowcomp1(lowcomp, band_psd[1], band_psd[2], 384);
  92. excite[1] = band_psd[1] - fast_gain - lowcomp;
  93. begin = 7;
  94. for (bin = 2; bin < 7; bin++) {
  95. if (!(is_lfe && bin == 6))
  96. lowcomp = calc_lowcomp1(lowcomp, band_psd[bin], band_psd[bin+1], 384);
  97. fastleak = band_psd[bin] - fast_gain;
  98. slowleak = band_psd[bin] - s->slow_gain;
  99. excite[bin] = fastleak - lowcomp;
  100. if (!(is_lfe && bin == 6)) {
  101. if (band_psd[bin] <= band_psd[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, band_psd[bin], band_psd[bin+1], bin);
  112. fastleak = FFMAX(fastleak - s->fast_decay, band_psd[bin] - fast_gain);
  113. slowleak = FFMAX(slowleak - s->slow_decay, band_psd[bin] - s->slow_gain);
  114. excite[bin] = FFMAX(fastleak - lowcomp, slowleak);
  115. }
  116. begin = 22;
  117. } else {
  118. /* coupling channel */
  119. begin = bndstrt;
  120. fastleak = (s->cpl_fast_leak << 8) + 768;
  121. slowleak = (s->cpl_slow_leak << 8) + 768;
  122. }
  123. for (bin = begin; bin < bndend; bin++) {
  124. fastleak = FFMAX(fastleak - s->fast_decay, band_psd[bin] - fast_gain);
  125. slowleak = FFMAX(slowleak - s->slow_decay, band_psd[bin] - s->slow_gain);
  126. excite[bin] = FFMAX(fastleak, slowleak);
  127. }
  128. /* compute masking curve */
  129. for (bin = bndstrt; bin < bndend; bin++) {
  130. tmp = s->db_per_bit - band_psd[bin];
  131. if (tmp > 0) {
  132. excite[bin] += tmp >> 2;
  133. }
  134. mask[bin] = FFMAX(ff_ac3_hearing_threshold_tab[bin >> s->sr_shift][s->sr_code], excite[bin]);
  135. }
  136. /* delta bit allocation */
  137. if (dba_mode == DBA_REUSE || dba_mode == DBA_NEW) {
  138. int band, seg, delta;
  139. if (dba_nsegs >= 8)
  140. return -1;
  141. band = 0;
  142. for (seg = 0; seg < dba_nsegs; seg++) {
  143. band += dba_offsets[seg];
  144. if (band >= 50 || dba_lengths[seg] > 50-band)
  145. return -1;
  146. if (dba_values[seg] >= 4) {
  147. delta = (dba_values[seg] - 3) << 7;
  148. } else {
  149. delta = (dba_values[seg] - 4) << 7;
  150. }
  151. for (k = 0; k < dba_lengths[seg]; k++) {
  152. mask[band] += delta;
  153. band++;
  154. }
  155. }
  156. }
  157. return 0;
  158. }
  159. void ff_ac3_bit_alloc_calc_bap(int16_t *mask, int16_t *psd, int start, int end,
  160. int snr_offset, int floor,
  161. const uint8_t *bap_tab, uint8_t *bap)
  162. {
  163. int i, j, k, end1, v, address;
  164. /* special case, if snr offset is -960, set all bap's to zero */
  165. if(snr_offset == -960) {
  166. memset(bap, 0, 256);
  167. return;
  168. }
  169. i = start;
  170. j = bin_to_band_tab[start];
  171. do {
  172. v = (FFMAX(mask[j] - snr_offset - floor, 0) & 0x1FE0) + floor;
  173. end1 = FFMIN(band_start_tab[j] + ff_ac3_critical_band_size_tab[j], end);
  174. for (k = i; k < end1; k++) {
  175. address = av_clip((psd[i] - v) >> 5, 0, 63);
  176. bap[i] = bap_tab[address];
  177. i++;
  178. }
  179. } while (end > band_start_tab[j++]);
  180. }
  181. /* AC-3 bit allocation. The algorithm is the one described in the AC-3
  182. spec. */
  183. void ac3_parametric_bit_allocation(AC3BitAllocParameters *s, uint8_t *bap,
  184. int8_t *exp, int start, int end,
  185. int snr_offset, int fast_gain, int is_lfe,
  186. int dba_mode, int dba_nsegs,
  187. uint8_t *dba_offsets, uint8_t *dba_lengths,
  188. uint8_t *dba_values)
  189. {
  190. int16_t psd[256]; /* scaled exponents */
  191. int16_t band_psd[50]; /* interpolated exponents */
  192. int16_t mask[50]; /* masking value */
  193. ff_ac3_bit_alloc_calc_psd(exp, start, end, psd, band_psd);
  194. ff_ac3_bit_alloc_calc_mask(s, band_psd, start, end, fast_gain, is_lfe,
  195. dba_mode, dba_nsegs, dba_offsets, dba_lengths, dba_values,
  196. mask);
  197. ff_ac3_bit_alloc_calc_bap(mask, psd, start, end, snr_offset, s->floor,
  198. ff_ac3_bap_tab, bap);
  199. }
  200. /**
  201. * Initializes some tables.
  202. * note: This function must remain thread safe because it is called by the
  203. * AVParser init code.
  204. */
  205. av_cold void ac3_common_init(void)
  206. {
  207. int i, j, k, l, v;
  208. /* compute bndtab and masktab from bandsz */
  209. k = 0;
  210. l = 0;
  211. for(i=0;i<50;i++) {
  212. band_start_tab[i] = l;
  213. v = ff_ac3_critical_band_size_tab[i];
  214. for(j=0;j<v;j++) bin_to_band_tab[k++]=i;
  215. l += v;
  216. }
  217. band_start_tab[50] = l;
  218. }