patch-libavcodec_aacenc_tns_c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. $OpenBSD: patch-libavcodec_aacenc_tns_c,v 1.7 2016/01/22 07:05:01 ajacoutot Exp $
  2. aacenc_tns: redo coefficient quantization and decision making
  3. aacenc_tns: encode coefficients directly and reenable compression
  4. aacenc_tns: fix coefficient compression condition
  5. aacenc_tns: add moving average filter for LTP
  6. aacenc_tns: disable coefficient compression by default
  7. aacenc_tns: simplify encoding function
  8. aacenc_tns: rework TNS descision logic
  9. aacenc_ltp: use an AR filter for LTP encoding as well
  10. aacenc_tns: tune and reduce artifacts
  11. aacenc_tns: use 4 bits for short windows
  12. AAC encoder: TNS fixes on short windows
  13. libavcodec/aacenc_tnc.c: remove unused variable w2
  14. AAC encoder: check for NaNs/inf in TNS gain
  15. --- libavcodec/aacenc_tns.c.orig Wed Jan 13 15:27:48 2016
  16. +++ libavcodec/aacenc_tns.c Thu Jan 21 05:58:54 2016
  17. @@ -25,62 +25,79 @@
  18. * @author Rostislav Pehlivanov ( atomnuker gmail com )
  19. */
  20. +#include "libavutil/libm.h"
  21. #include "aacenc.h"
  22. #include "aacenc_tns.h"
  23. #include "aactab.h"
  24. #include "aacenc_utils.h"
  25. #include "aacenc_quantization.h"
  26. +/* Could be set to 3 to save an additional bit at the cost of little quality */
  27. +#define TNS_Q_BITS 4
  28. +
  29. +/* Coefficient resolution in short windows */
  30. +#define TNS_Q_BITS_IS8 4
  31. +
  32. +/* We really need the bits we save here elsewhere */
  33. +#define TNS_ENABLE_COEF_COMPRESSION
  34. +
  35. +/* TNS will only be used if the LPC gain is within these margins */
  36. +#define TNS_GAIN_THRESHOLD_LOW 1.4f
  37. +#define TNS_GAIN_THRESHOLD_HIGH 1.16f*TNS_GAIN_THRESHOLD_LOW
  38. +
  39. +static inline int compress_coeffs(int *coef, int order, int c_bits)
  40. +{
  41. + int i;
  42. + const int low_idx = c_bits ? 4 : 2;
  43. + const int shift_val = c_bits ? 8 : 4;
  44. + const int high_idx = c_bits ? 11 : 5;
  45. +#ifndef TNS_ENABLE_COEF_COMPRESSION
  46. + return 0;
  47. +#endif /* TNS_ENABLE_COEF_COMPRESSION */
  48. + for (i = 0; i < order; i++)
  49. + if (coef[i] >= low_idx && coef[i] <= high_idx)
  50. + return 0;
  51. + for (i = 0; i < order; i++)
  52. + coef[i] -= (coef[i] > high_idx) ? shift_val : 0;
  53. + return 1;
  54. +}
  55. +
  56. /**
  57. * Encode TNS data.
  58. - * Coefficient compression saves a single bit per coefficient.
  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. - uint8_t u_coef;
  65. - const uint8_t coef_res = TNS_Q_BITS == 4;
  66. - int i, w, filt, coef_len, coef_compress = 0;
  67. - const int is8 = sce->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE;
  68. TemporalNoiseShaping *tns = &sce->tns;
  69. + int i, w, filt, coef_compress = 0, coef_len;
  70. + const int is8 = sce->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE;
  71. + const int c_bits = is8 ? TNS_Q_BITS_IS8 == 4 : TNS_Q_BITS == 4;
  72. if (!sce->tns.present)
  73. return;
  74. for (i = 0; i < sce->ics.num_windows; i++) {
  75. put_bits(&s->pb, 2 - is8, sce->tns.n_filt[i]);
  76. - if (tns->n_filt[i]) {
  77. - put_bits(&s->pb, 1, coef_res);
  78. - for (filt = 0; filt < tns->n_filt[i]; filt++) {
  79. - put_bits(&s->pb, 6 - 2 * is8, tns->length[i][filt]);
  80. - put_bits(&s->pb, 5 - 2 * is8, tns->order[i][filt]);
  81. - if (tns->order[i][filt]) {
  82. - put_bits(&s->pb, 1, !!tns->direction[i][filt]);
  83. - put_bits(&s->pb, 1, !!coef_compress);
  84. - coef_len = coef_res + 3 - coef_compress;
  85. - for (w = 0; w < tns->order[i][filt]; w++) {
  86. - u_coef = (tns->coef_idx[i][filt][w])&(~(~0<<coef_len));
  87. - put_bits(&s->pb, coef_len, u_coef);
  88. - }
  89. - }
  90. - }
  91. + if (!tns->n_filt[i])
  92. + continue;
  93. + put_bits(&s->pb, 1, c_bits);
  94. + for (filt = 0; filt < tns->n_filt[i]; filt++) {
  95. + put_bits(&s->pb, 6 - 2 * is8, tns->length[i][filt]);
  96. + put_bits(&s->pb, 5 - 2 * is8, tns->order[i][filt]);
  97. + if (!tns->order[i][filt])
  98. + continue;
  99. + put_bits(&s->pb, 1, tns->direction[i][filt]);
  100. + coef_compress = compress_coeffs(tns->coef_idx[i][filt],
  101. + tns->order[i][filt], c_bits);
  102. + put_bits(&s->pb, 1, coef_compress);
  103. + coef_len = c_bits + 3 - coef_compress;
  104. + for (w = 0; w < tns->order[i][filt]; w++)
  105. + put_bits(&s->pb, coef_len, tns->coef_idx[i][filt][w]);
  106. }
  107. }
  108. }
  109. -static inline void quantize_coefs(double *coef, int *idx, float *lpc, int order)
  110. -{
  111. - int i;
  112. - uint8_t u_coef;
  113. - const float *quant_arr = tns_tmp2_map[TNS_Q_BITS == 4];
  114. - const double iqfac_p = ((1 << (TNS_Q_BITS-1)) - 0.5)/(M_PI/2.0);
  115. - const double iqfac_m = ((1 << (TNS_Q_BITS-1)) + 0.5)/(M_PI/2.0);
  116. - for (i = 0; i < order; i++) {
  117. - idx[i] = ceilf(asin(coef[i])*((coef[i] >= 0) ? iqfac_p : iqfac_m));
  118. - u_coef = (idx[i])&(~(~0<<TNS_Q_BITS));
  119. - lpc[i] = quant_arr[u_coef];
  120. - }
  121. -}
  122. -
  123. /* Apply TNS filter */
  124. void ff_aac_apply_tns(AACEncContext *s, SingleChannelElement *sce)
  125. {
  126. @@ -114,81 +131,85 @@ void ff_aac_apply_tns(AACEncContext *s, SingleChannelE
  127. }
  128. start += w * 128;
  129. - // ar filter
  130. - for (m = 0; m < size; m++, start += inc)
  131. - for (i = 1; i <= FFMIN(m, order); i++)
  132. + /* AR filter */
  133. + for (m = 0; m < size; m++, start += inc) {
  134. + for (i = 1; i <= FFMIN(m, order); i++) {
  135. sce->coeffs[start] += lpc[i-1]*sce->pcoeffs[start - i*inc];
  136. + }
  137. + }
  138. }
  139. }
  140. }
  141. +/*
  142. + * c_bits - 1 if 4 bit coefficients, 0 if 3 bit coefficients
  143. + */
  144. +static inline void quantize_coefs(double *coef, int *idx, float *lpc, int order,
  145. + int c_bits)
  146. +{
  147. + int i;
  148. + const float *quant_arr = tns_tmp2_map[c_bits];
  149. + for (i = 0; i < order; i++) {
  150. + idx[i] = quant_array_idx(coef[i], quant_arr, c_bits ? 16 : 8);
  151. + lpc[i] = quant_arr[idx[i]];
  152. + }
  153. +}
  154. +
  155. +/*
  156. + * 3 bits per coefficient with 8 short windows
  157. + */
  158. void ff_aac_search_for_tns(AACEncContext *s, SingleChannelElement *sce)
  159. {
  160. TemporalNoiseShaping *tns = &sce->tns;
  161. - int w, w2, g, count = 0;
  162. + int w, g, count = 0;
  163. + double gain, coefs[MAX_LPC_ORDER];
  164. const int mmm = FFMIN(sce->ics.tns_max_bands, sce->ics.max_sfb);
  165. const int is8 = sce->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE;
  166. + const int c_bits = is8 ? TNS_Q_BITS_IS8 == 4 : TNS_Q_BITS == 4;
  167. + const int sfb_start = av_clip(tns_min_sfb[is8][s->samplerate_index], 0, mmm);
  168. + const int sfb_end = av_clip(sce->ics.num_swb, 0, mmm);
  169. const int order = is8 ? 7 : s->profile == FF_PROFILE_AAC_LOW ? 12 : TNS_MAX_ORDER;
  170. + const int slant = sce->ics.window_sequence[0] == LONG_STOP_SEQUENCE ? 1 :
  171. + sce->ics.window_sequence[0] == LONG_START_SEQUENCE ? 0 : 2;
  172. + const int sfb_len = sfb_end - sfb_start;
  173. + const int coef_len = sce->ics.swb_offset[sfb_end] - sce->ics.swb_offset[sfb_start];
  174. - int sfb_start = av_clip(tns_min_sfb[is8][s->samplerate_index], 0, mmm);
  175. - int sfb_end = av_clip(sce->ics.num_swb, 0, mmm);
  176. + if (coef_len <= 0 || sfb_len <= 0) {
  177. + sce->tns.present = 0;
  178. + return;
  179. + }
  180. for (w = 0; w < sce->ics.num_windows; w++) {
  181. - float e_ratio = 0.0f, threshold = 0.0f, spread = 0.0f, en[2] = {0.0, 0.0f};
  182. - double gain = 0.0f, coefs[MAX_LPC_ORDER] = {0};
  183. - int coef_start = w*sce->ics.num_swb + sce->ics.swb_offset[sfb_start];
  184. - int coef_len = sce->ics.swb_offset[sfb_end] - sce->ics.swb_offset[sfb_start];
  185. + float en[2] = {0.0f, 0.0f};
  186. + int oc_start = 0, os_start = 0;
  187. + int coef_start = sce->ics.swb_offset[sfb_start];
  188. - for (g = 0; g < sce->ics.num_swb; g++) {
  189. - if (w*16+g < sfb_start || w*16+g > sfb_end)
  190. - continue;
  191. - for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
  192. - FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
  193. - if ((w+w2)*16+g > sfb_start + ((sfb_end - sfb_start)/2))
  194. - en[1] += band->energy;
  195. - else
  196. - en[0] += band->energy;
  197. - threshold += band->threshold;
  198. - spread += band->spread;
  199. - }
  200. + for (g = sfb_start; g < sce->ics.num_swb && g <= sfb_end; g++) {
  201. + FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[w*16+g];
  202. + if (g > sfb_start + (sfb_len/2))
  203. + en[1] += band->energy;
  204. + else
  205. + en[0] += band->energy;
  206. }
  207. - if (coef_len <= 0 || (sfb_end - sfb_start) <= 0)
  208. - continue;
  209. - else
  210. - e_ratio = en[0]/en[1];
  211. -
  212. /* LPC */
  213. - gain = ff_lpc_calc_ref_coefs_f(&s->lpc, &sce->coeffs[coef_start],
  214. + gain = ff_lpc_calc_ref_coefs_f(&s->lpc, &sce->coeffs[w*128 + coef_start],
  215. coef_len, order, coefs);
  216. - if (gain > TNS_GAIN_THRESHOLD_LOW && gain < TNS_GAIN_THRESHOLD_HIGH &&
  217. - (en[0]+en[1]) > TNS_GAIN_THRESHOLD_LOW*threshold &&
  218. - spread < TNS_SPREAD_THRESHOLD && order) {
  219. - if (is8 || order < 2 || (e_ratio > TNS_E_RATIO_LOW && e_ratio < TNS_E_RATIO_HIGH)) {
  220. - tns->n_filt[w] = 1;
  221. - for (g = 0; g < tns->n_filt[w]; g++) {
  222. - tns->length[w][g] = sfb_end - sfb_start;
  223. - tns->direction[w][g] = en[0] < en[1];
  224. - tns->order[w][g] = order;
  225. - quantize_coefs(coefs, tns->coef_idx[w][g], tns->coef[w][g],
  226. - order);
  227. - }
  228. - } else { /* 2 filters due to energy disbalance */
  229. - tns->n_filt[w] = 2;
  230. - for (g = 0; g < tns->n_filt[w]; g++) {
  231. - tns->direction[w][g] = en[g] < en[!g];
  232. - tns->order[w][g] = !g ? order/2 : order - tns->order[w][g-1];
  233. - tns->length[w][g] = !g ? (sfb_end - sfb_start)/2 : \
  234. - (sfb_end - sfb_start) - tns->length[w][g-1];
  235. - quantize_coefs(&coefs[!g ? 0 : order - tns->order[w][g-1]],
  236. - tns->coef_idx[w][g], tns->coef[w][g],
  237. - tns->order[w][g]);
  238. - }
  239. - }
  240. - count++;
  241. + if (!order || !isfinite(gain) || gain < TNS_GAIN_THRESHOLD_LOW || gain > TNS_GAIN_THRESHOLD_HIGH)
  242. + continue;
  243. +
  244. + tns->n_filt[w] = is8 ? 1 : order != TNS_MAX_ORDER ? 2 : 3;
  245. + for (g = 0; g < tns->n_filt[w]; g++) {
  246. + tns->direction[w][g] = slant != 2 ? slant : en[g] < en[!g];
  247. + tns->order[w][g] = g < tns->n_filt[w] ? order/tns->n_filt[w] : order - oc_start;
  248. + tns->length[w][g] = g < tns->n_filt[w] ? sfb_len/tns->n_filt[w] : sfb_len - os_start;
  249. + quantize_coefs(&coefs[oc_start], tns->coef_idx[w][g], tns->coef[w][g],
  250. + tns->order[w][g], c_bits);
  251. + oc_start += tns->order[w][g];
  252. + os_start += tns->length[w][g];
  253. }
  254. + count++;
  255. }
  256. -
  257. sce->tns.present = !!count;
  258. }