aacps_tablegen.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Header file for hardcoded Parametric Stereo tables
  3. *
  4. * Copyright (c) 2010 Alex Converse <alex.converse@gmail.com>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #ifndef AVCODEC_AACPS_TABLEGEN_H
  23. #define AVCODEC_AACPS_TABLEGEN_H
  24. #include <math.h>
  25. #include <stdint.h>
  26. #if CONFIG_HARDCODED_TABLES
  27. #define ps_tableinit()
  28. #define TABLE_CONST const
  29. #include "libavcodec/aacps_tables.h"
  30. #else
  31. #include "libavutil/common.h"
  32. #include "libavutil/libm.h"
  33. #include "libavutil/mathematics.h"
  34. #include "libavutil/mem.h"
  35. #define NR_ALLPASS_BANDS20 30
  36. #define NR_ALLPASS_BANDS34 50
  37. #define PS_AP_LINKS 3
  38. #define TABLE_CONST
  39. static float pd_re_smooth[8*8*8];
  40. static float pd_im_smooth[8*8*8];
  41. static float HA[46][8][4];
  42. static float HB[46][8][4];
  43. static DECLARE_ALIGNED(16, float, f20_0_8) [ 8][8][2];
  44. static DECLARE_ALIGNED(16, float, f34_0_12)[12][8][2];
  45. static DECLARE_ALIGNED(16, float, f34_1_8) [ 8][8][2];
  46. static DECLARE_ALIGNED(16, float, f34_2_4) [ 4][8][2];
  47. static TABLE_CONST DECLARE_ALIGNED(16, float, Q_fract_allpass)[2][50][3][2];
  48. static DECLARE_ALIGNED(16, float, phi_fract)[2][50][2];
  49. static const float g0_Q8[] = {
  50. 0.00746082949812f, 0.02270420949825f, 0.04546865930473f, 0.07266113929591f,
  51. 0.09885108575264f, 0.11793710567217f, 0.125f
  52. };
  53. static const float g0_Q12[] = {
  54. 0.04081179924692f, 0.03812810994926f, 0.05144908135699f, 0.06399831151592f,
  55. 0.07428313801106f, 0.08100347892914f, 0.08333333333333f
  56. };
  57. static const float g1_Q8[] = {
  58. 0.01565675600122f, 0.03752716391991f, 0.05417891378782f, 0.08417044116767f,
  59. 0.10307344158036f, 0.12222452249753f, 0.125f
  60. };
  61. static const float g2_Q4[] = {
  62. -0.05908211155639f, -0.04871498374946f, 0.0f, 0.07778723915851f,
  63. 0.16486303567403f, 0.23279856662996f, 0.25f
  64. };
  65. static av_cold void make_filters_from_proto(float (*filter)[8][2], const float *proto, int bands)
  66. {
  67. int q, n;
  68. for (q = 0; q < bands; q++) {
  69. for (n = 0; n < 7; n++) {
  70. double theta = 2 * M_PI * (q + 0.5) * (n - 6) / bands;
  71. filter[q][n][0] = proto[n] * cos(theta);
  72. filter[q][n][1] = proto[n] * -sin(theta);
  73. }
  74. }
  75. }
  76. static av_cold void ps_tableinit(void)
  77. {
  78. static const float ipdopd_sin[] = { 0, M_SQRT1_2, 1, M_SQRT1_2, 0, -M_SQRT1_2, -1, -M_SQRT1_2 };
  79. static const float ipdopd_cos[] = { 1, M_SQRT1_2, 0, -M_SQRT1_2, -1, -M_SQRT1_2, 0, M_SQRT1_2 };
  80. int pd0, pd1, pd2;
  81. static const float iid_par_dequant[] = {
  82. //iid_par_dequant_default
  83. 0.05623413251903, 0.12589254117942, 0.19952623149689, 0.31622776601684,
  84. 0.44668359215096, 0.63095734448019, 0.79432823472428, 1,
  85. 1.25892541179417, 1.58489319246111, 2.23872113856834, 3.16227766016838,
  86. 5.01187233627272, 7.94328234724282, 17.7827941003892,
  87. //iid_par_dequant_fine
  88. 0.00316227766017, 0.00562341325190, 0.01, 0.01778279410039,
  89. 0.03162277660168, 0.05623413251903, 0.07943282347243, 0.11220184543020,
  90. 0.15848931924611, 0.22387211385683, 0.31622776601684, 0.39810717055350,
  91. 0.50118723362727, 0.63095734448019, 0.79432823472428, 1,
  92. 1.25892541179417, 1.58489319246111, 1.99526231496888, 2.51188643150958,
  93. 3.16227766016838, 4.46683592150963, 6.30957344480193, 8.91250938133745,
  94. 12.5892541179417, 17.7827941003892, 31.6227766016838, 56.2341325190349,
  95. 100, 177.827941003892, 316.227766016837,
  96. };
  97. static const float icc_invq[] = {
  98. 1, 0.937, 0.84118, 0.60092, 0.36764, 0, -0.589, -1
  99. };
  100. static const float acos_icc_invq[] = {
  101. 0, 0.35685527, 0.57133466, 0.92614472, 1.1943263, M_PI/2, 2.2006171, M_PI
  102. };
  103. int iid, icc;
  104. int k, m;
  105. static const int8_t f_center_20[] = {
  106. -3, -1, 1, 3, 5, 7, 10, 14, 18, 22,
  107. };
  108. static const int8_t f_center_34[] = {
  109. 2, 6, 10, 14, 18, 22, 26, 30,
  110. 34,-10, -6, -2, 51, 57, 15, 21,
  111. 27, 33, 39, 45, 54, 66, 78, 42,
  112. 102, 66, 78, 90,102,114,126, 90,
  113. };
  114. static const float fractional_delay_links[] = { 0.43f, 0.75f, 0.347f };
  115. const float fractional_delay_gain = 0.39f;
  116. for (pd0 = 0; pd0 < 8; pd0++) {
  117. float pd0_re = ipdopd_cos[pd0];
  118. float pd0_im = ipdopd_sin[pd0];
  119. for (pd1 = 0; pd1 < 8; pd1++) {
  120. float pd1_re = ipdopd_cos[pd1];
  121. float pd1_im = ipdopd_sin[pd1];
  122. for (pd2 = 0; pd2 < 8; pd2++) {
  123. float pd2_re = ipdopd_cos[pd2];
  124. float pd2_im = ipdopd_sin[pd2];
  125. float re_smooth = 0.25f * pd0_re + 0.5f * pd1_re + pd2_re;
  126. float im_smooth = 0.25f * pd0_im + 0.5f * pd1_im + pd2_im;
  127. float pd_mag = 1 / hypot(im_smooth, re_smooth);
  128. pd_re_smooth[pd0*64+pd1*8+pd2] = re_smooth * pd_mag;
  129. pd_im_smooth[pd0*64+pd1*8+pd2] = im_smooth * pd_mag;
  130. }
  131. }
  132. }
  133. for (iid = 0; iid < 46; iid++) {
  134. float c = iid_par_dequant[iid]; ///< Linear Inter-channel Intensity Difference
  135. float c1 = (float)M_SQRT2 / sqrtf(1.0f + c*c);
  136. float c2 = c * c1;
  137. for (icc = 0; icc < 8; icc++) {
  138. /*if (PS_BASELINE || ps->icc_mode < 3)*/ {
  139. float alpha = 0.5f * acos_icc_invq[icc];
  140. float beta = alpha * (c1 - c2) * (float)M_SQRT1_2;
  141. HA[iid][icc][0] = c2 * cosf(beta + alpha);
  142. HA[iid][icc][1] = c1 * cosf(beta - alpha);
  143. HA[iid][icc][2] = c2 * sinf(beta + alpha);
  144. HA[iid][icc][3] = c1 * sinf(beta - alpha);
  145. } /* else */ {
  146. float alpha, gamma, mu, rho;
  147. float alpha_c, alpha_s, gamma_c, gamma_s;
  148. rho = FFMAX(icc_invq[icc], 0.05f);
  149. alpha = 0.5f * atan2f(2.0f * c * rho, c*c - 1.0f);
  150. mu = c + 1.0f / c;
  151. mu = sqrtf(1 + (4 * rho * rho - 4)/(mu * mu));
  152. gamma = atanf(sqrtf((1.0f - mu)/(1.0f + mu)));
  153. if (alpha < 0) alpha += M_PI/2;
  154. alpha_c = cosf(alpha);
  155. alpha_s = sinf(alpha);
  156. gamma_c = cosf(gamma);
  157. gamma_s = sinf(gamma);
  158. HB[iid][icc][0] = M_SQRT2 * alpha_c * gamma_c;
  159. HB[iid][icc][1] = M_SQRT2 * alpha_s * gamma_c;
  160. HB[iid][icc][2] = -M_SQRT2 * alpha_s * gamma_s;
  161. HB[iid][icc][3] = M_SQRT2 * alpha_c * gamma_s;
  162. }
  163. }
  164. }
  165. for (k = 0; k < NR_ALLPASS_BANDS20; k++) {
  166. double f_center, theta;
  167. if (k < FF_ARRAY_ELEMS(f_center_20))
  168. f_center = f_center_20[k] * 0.125;
  169. else
  170. f_center = k - 6.5f;
  171. for (m = 0; m < PS_AP_LINKS; m++) {
  172. theta = -M_PI * fractional_delay_links[m] * f_center;
  173. Q_fract_allpass[0][k][m][0] = cos(theta);
  174. Q_fract_allpass[0][k][m][1] = sin(theta);
  175. }
  176. theta = -M_PI*fractional_delay_gain*f_center;
  177. phi_fract[0][k][0] = cos(theta);
  178. phi_fract[0][k][1] = sin(theta);
  179. }
  180. for (k = 0; k < NR_ALLPASS_BANDS34; k++) {
  181. double f_center, theta;
  182. if (k < FF_ARRAY_ELEMS(f_center_34))
  183. f_center = f_center_34[k] / 24.0;
  184. else
  185. f_center = k - 26.5f;
  186. for (m = 0; m < PS_AP_LINKS; m++) {
  187. theta = -M_PI * fractional_delay_links[m] * f_center;
  188. Q_fract_allpass[1][k][m][0] = cos(theta);
  189. Q_fract_allpass[1][k][m][1] = sin(theta);
  190. }
  191. theta = -M_PI*fractional_delay_gain*f_center;
  192. phi_fract[1][k][0] = cos(theta);
  193. phi_fract[1][k][1] = sin(theta);
  194. }
  195. make_filters_from_proto(f20_0_8, g0_Q8, 8);
  196. make_filters_from_proto(f34_0_12, g0_Q12, 12);
  197. make_filters_from_proto(f34_1_8, g1_Q8, 8);
  198. make_filters_from_proto(f34_2_4, g2_Q4, 4);
  199. }
  200. #endif /* CONFIG_HARDCODED_TABLES */
  201. #endif /* AVCODEC_AACPS_TABLEGEN_H */