aacenc_tns.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * AAC encoder TNS
  3. * Copyright (C) 2015 Rostislav Pehlivanov
  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
  23. * AAC encoder temporal noise shaping
  24. * @author Rostislav Pehlivanov ( atomnuker gmail com )
  25. */
  26. #include "libavutil/libm.h"
  27. #include "aacenc.h"
  28. #include "aacenc_tns.h"
  29. #include "aactab.h"
  30. #include "aacenc_utils.h"
  31. #include "aacenc_quantization.h"
  32. /* Could be set to 3 to save an additional bit at the cost of little quality */
  33. #define TNS_Q_BITS 4
  34. /* Coefficient resolution in short windows */
  35. #define TNS_Q_BITS_IS8 4
  36. /* We really need the bits we save here elsewhere */
  37. #define TNS_ENABLE_COEF_COMPRESSION
  38. /* TNS will only be used if the LPC gain is within these margins */
  39. #define TNS_GAIN_THRESHOLD_LOW 1.4f
  40. #define TNS_GAIN_THRESHOLD_HIGH 1.16f*TNS_GAIN_THRESHOLD_LOW
  41. static inline int compress_coeffs(int *coef, int order, int c_bits)
  42. {
  43. int i;
  44. const int low_idx = c_bits ? 4 : 2;
  45. const int shift_val = c_bits ? 8 : 4;
  46. const int high_idx = c_bits ? 11 : 5;
  47. #ifndef TNS_ENABLE_COEF_COMPRESSION
  48. return 0;
  49. #endif /* TNS_ENABLE_COEF_COMPRESSION */
  50. for (i = 0; i < order; i++)
  51. if (coef[i] >= low_idx && coef[i] <= high_idx)
  52. return 0;
  53. for (i = 0; i < order; i++)
  54. coef[i] -= (coef[i] > high_idx) ? shift_val : 0;
  55. return 1;
  56. }
  57. /**
  58. * Encode TNS data.
  59. * Coefficient compression is simply not lossless as it should be
  60. * on any decoder tested and as such is not active.
  61. */
  62. void ff_aac_encode_tns_info(AACEncContext *s, SingleChannelElement *sce)
  63. {
  64. TemporalNoiseShaping *tns = &sce->tns;
  65. int i, w, filt, coef_compress = 0, coef_len;
  66. const int is8 = sce->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE;
  67. const int c_bits = is8 ? TNS_Q_BITS_IS8 == 4 : TNS_Q_BITS == 4;
  68. if (!sce->tns.present)
  69. return;
  70. for (i = 0; i < sce->ics.num_windows; i++) {
  71. put_bits(&s->pb, 2 - is8, sce->tns.n_filt[i]);
  72. if (!tns->n_filt[i])
  73. continue;
  74. put_bits(&s->pb, 1, c_bits);
  75. for (filt = 0; filt < tns->n_filt[i]; filt++) {
  76. put_bits(&s->pb, 6 - 2 * is8, tns->length[i][filt]);
  77. put_bits(&s->pb, 5 - 2 * is8, tns->order[i][filt]);
  78. if (!tns->order[i][filt])
  79. continue;
  80. put_bits(&s->pb, 1, tns->direction[i][filt]);
  81. coef_compress = compress_coeffs(tns->coef_idx[i][filt],
  82. tns->order[i][filt], c_bits);
  83. put_bits(&s->pb, 1, coef_compress);
  84. coef_len = c_bits + 3 - coef_compress;
  85. for (w = 0; w < tns->order[i][filt]; w++)
  86. put_bits(&s->pb, coef_len, tns->coef_idx[i][filt][w]);
  87. }
  88. }
  89. }
  90. /* Apply TNS filter */
  91. void ff_aac_apply_tns(AACEncContext *s, SingleChannelElement *sce)
  92. {
  93. TemporalNoiseShaping *tns = &sce->tns;
  94. IndividualChannelStream *ics = &sce->ics;
  95. int w, filt, m, i, top, order, bottom, start, end, size, inc;
  96. const int mmm = FFMIN(ics->tns_max_bands, ics->max_sfb);
  97. float lpc[TNS_MAX_ORDER];
  98. for (w = 0; w < ics->num_windows; w++) {
  99. bottom = ics->num_swb;
  100. for (filt = 0; filt < tns->n_filt[w]; filt++) {
  101. top = bottom;
  102. bottom = FFMAX(0, top - tns->length[w][filt]);
  103. order = tns->order[w][filt];
  104. if (order == 0)
  105. continue;
  106. // tns_decode_coef
  107. compute_lpc_coefs(tns->coef[w][filt], order, lpc, 0, 0, 0);
  108. start = ics->swb_offset[FFMIN(bottom, mmm)];
  109. end = ics->swb_offset[FFMIN( top, mmm)];
  110. if ((size = end - start) <= 0)
  111. continue;
  112. if (tns->direction[w][filt]) {
  113. inc = -1;
  114. start = end - 1;
  115. } else {
  116. inc = 1;
  117. }
  118. start += w * 128;
  119. /* AR filter */
  120. for (m = 0; m < size; m++, start += inc) {
  121. for (i = 1; i <= FFMIN(m, order); i++) {
  122. sce->coeffs[start] += lpc[i-1]*sce->pcoeffs[start - i*inc];
  123. }
  124. }
  125. }
  126. }
  127. }
  128. /*
  129. * c_bits - 1 if 4 bit coefficients, 0 if 3 bit coefficients
  130. */
  131. static inline void quantize_coefs(double *coef, int *idx, float *lpc, int order,
  132. int c_bits)
  133. {
  134. int i;
  135. const float *quant_arr = tns_tmp2_map[c_bits];
  136. for (i = 0; i < order; i++) {
  137. idx[i] = quant_array_idx(coef[i], quant_arr, c_bits ? 16 : 8);
  138. lpc[i] = quant_arr[idx[i]];
  139. }
  140. }
  141. /*
  142. * 3 bits per coefficient with 8 short windows
  143. */
  144. void ff_aac_search_for_tns(AACEncContext *s, SingleChannelElement *sce)
  145. {
  146. TemporalNoiseShaping *tns = &sce->tns;
  147. int w, g, count = 0;
  148. double gain, coefs[MAX_LPC_ORDER];
  149. const int mmm = FFMIN(sce->ics.tns_max_bands, sce->ics.max_sfb);
  150. const int is8 = sce->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE;
  151. const int c_bits = is8 ? TNS_Q_BITS_IS8 == 4 : TNS_Q_BITS == 4;
  152. const int sfb_start = av_clip(tns_min_sfb[is8][s->samplerate_index], 0, mmm);
  153. const int sfb_end = av_clip(sce->ics.num_swb, 0, mmm);
  154. const int order = is8 ? 7 : s->profile == FF_PROFILE_AAC_LOW ? 12 : TNS_MAX_ORDER;
  155. const int slant = sce->ics.window_sequence[0] == LONG_STOP_SEQUENCE ? 1 :
  156. sce->ics.window_sequence[0] == LONG_START_SEQUENCE ? 0 : 2;
  157. const int sfb_len = sfb_end - sfb_start;
  158. const int coef_len = sce->ics.swb_offset[sfb_end] - sce->ics.swb_offset[sfb_start];
  159. if (coef_len <= 0 || sfb_len <= 0) {
  160. sce->tns.present = 0;
  161. return;
  162. }
  163. for (w = 0; w < sce->ics.num_windows; w++) {
  164. float en[2] = {0.0f, 0.0f};
  165. int oc_start = 0, os_start = 0;
  166. int coef_start = sce->ics.swb_offset[sfb_start];
  167. for (g = sfb_start; g < sce->ics.num_swb && g <= sfb_end; g++) {
  168. FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[w*16+g];
  169. if (g > sfb_start + (sfb_len/2))
  170. en[1] += band->energy;
  171. else
  172. en[0] += band->energy;
  173. }
  174. /* LPC */
  175. gain = ff_lpc_calc_ref_coefs_f(&s->lpc, &sce->coeffs[w*128 + coef_start],
  176. coef_len, order, coefs);
  177. if (!order || !isfinite(gain) || gain < TNS_GAIN_THRESHOLD_LOW || gain > TNS_GAIN_THRESHOLD_HIGH)
  178. continue;
  179. tns->n_filt[w] = is8 ? 1 : order != TNS_MAX_ORDER ? 2 : 3;
  180. for (g = 0; g < tns->n_filt[w]; g++) {
  181. tns->direction[w][g] = slant != 2 ? slant : en[g] < en[!g];
  182. tns->order[w][g] = g < tns->n_filt[w] ? order/tns->n_filt[w] : order - oc_start;
  183. tns->length[w][g] = g < tns->n_filt[w] ? sfb_len/tns->n_filt[w] : sfb_len - os_start;
  184. quantize_coefs(&coefs[oc_start], tns->coef_idx[w][g], tns->coef[w][g],
  185. tns->order[w][g], c_bits);
  186. oc_start += tns->order[w][g];
  187. os_start += tns->length[w][g];
  188. }
  189. count++;
  190. }
  191. sce->tns.present = !!count;
  192. }