aacpsy.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * AAC encoder psychoacoustic model
  3. * Copyright (C) 2008 Konstantin Shishkov
  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/aacpsy.c
  23. * AAC encoder psychoacoustic model
  24. */
  25. #include "avcodec.h"
  26. #include "aacpsy.h"
  27. #include "aactab.h"
  28. /***********************************
  29. * TODOs:
  30. * General:
  31. * better audio preprocessing (add DC highpass filter?)
  32. * more psy models
  33. * maybe improve coefficient quantization function in some way
  34. *
  35. * 3GPP-based psy model:
  36. * thresholds linearization after their modifications for attaining given bitrate
  37. * try other bitrate controlling mechanism (maybe use ratecontrol.c?)
  38. * control quality for quality-based output
  39. **********************************/
  40. /**
  41. * Quantize one coefficient.
  42. * @return absolute value of the quantized coefficient
  43. * @see 3GPP TS26.403 5.6.2 "Scalefactor determination"
  44. */
  45. static av_always_inline int quant(float coef, const float Q)
  46. {
  47. return av_clip((int)(pow(fabsf(coef) * Q, 0.75) + 0.4054), 0, 8191);
  48. }
  49. static inline float get_approximate_quant_error(float *c, int size, int scale_idx)
  50. {
  51. int i;
  52. int q;
  53. float coef, unquant, sum = 0.0f;
  54. const float Q = ff_aac_pow2sf_tab[200 - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
  55. const float IQ = ff_aac_pow2sf_tab[200 + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
  56. for(i = 0; i < size; i++){
  57. coef = fabs(c[i]);
  58. q = quant(c[i], Q);
  59. unquant = (q * cbrt(q)) * IQ;
  60. sum += (coef - unquant) * (coef - unquant);
  61. }
  62. return sum;
  63. }
  64. /**
  65. * constants for 3GPP AAC psychoacoustic model
  66. * @{
  67. */
  68. #define PSY_3GPP_SPREAD_LOW 1.5f // spreading factor for ascending threshold spreading (15 dB/Bark)
  69. #define PSY_3GPP_SPREAD_HI 3.0f // spreading factor for descending threshold spreading (30 dB/Bark)
  70. /**
  71. * @}
  72. */
  73. /**
  74. * information for single band used by 3GPP TS26.403-inspired psychoacoustic model
  75. */
  76. typedef struct Psy3gppBand{
  77. float energy; ///< band energy
  78. float ffac; ///< form factor
  79. }Psy3gppBand;
  80. /**
  81. * psychoacoustic model frame type-dependent coefficients
  82. */
  83. typedef struct Psy3gppCoeffs{
  84. float ath [64]; ///< absolute threshold of hearing per bands
  85. float barks [64]; ///< Bark value for each spectral band in long frame
  86. float spread_low[64]; ///< spreading factor for low-to-high threshold spreading in long frame
  87. float spread_hi [64]; ///< spreading factor for high-to-low threshold spreading in long frame
  88. }Psy3gppCoeffs;
  89. /**
  90. * Calculate Bark value for given line.
  91. */
  92. static inline float calc_bark(float f)
  93. {
  94. return 13.3f * atanf(0.00076f * f) + 3.5f * atanf((f / 7500.0f) * (f / 7500.0f));
  95. }