qcelp_lsp.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * QCELP decoder
  3. * Copyright (c) 2007 Reynaldo H. Verdejo Pinochet
  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/qcelp_lsp.c
  23. * QCELP decoder
  24. * @author Reynaldo H. Verdejo Pinochet
  25. * @remark FFmpeg merging spearheaded by Kenan Gillet
  26. * @remark Development mentored by Benjamin Larson
  27. */
  28. #include "libavutil/mathematics.h"
  29. /**
  30. * initial coefficient to perform bandwidth expansion on LPC
  31. *
  32. * @note: 0.9883 looks like an approximation of 253/256.
  33. *
  34. * TIA/EIA/IS-733 2.4.3.3.6 6
  35. */
  36. #define QCELP_BANDWITH_EXPANSION_COEFF 0.9883
  37. /**
  38. * Computes the Pa / (1 + z(-1)) or Qa / (1 - z(-1)) coefficients
  39. * needed for LSP to LPC conversion.
  40. * We only need to calculate the 6 first elements of the polynomial.
  41. *
  42. * @param lspf line spectral pair frequencies
  43. * @param f [out] polynomial input/output as a vector
  44. *
  45. * TIA/EIA/IS-733 2.4.3.3.5-1/2
  46. */
  47. static void lsp2polyf(const float *lspf, double *f, int lp_half_order)
  48. {
  49. int i, j;
  50. f[0] = 1.0;
  51. f[1] = -2 * cos(M_PI * lspf[0]);
  52. lspf -= 2;
  53. for(i=2; i<=lp_half_order; i++)
  54. {
  55. double val = -2 * cos(M_PI * lspf[2*i]);
  56. f[i] = val * f[i-1] + 2*f[i-2];
  57. for(j=i-1; j>1; j--)
  58. f[j] += f[j-1] * val + f[j-2];
  59. f[1] += val;
  60. }
  61. }
  62. /**
  63. * Reconstructs LPC coefficients from the line spectral pair frequencies
  64. * and performs bandwidth expansion.
  65. *
  66. * @param lspf line spectral pair frequencies
  67. * @param lpc linear predictive coding coefficients
  68. *
  69. * @note: bandwith_expansion_coeff could be precalculated into a table
  70. * but it seems to be slower on x86
  71. *
  72. * TIA/EIA/IS-733 2.4.3.3.5
  73. */
  74. void ff_qcelp_lspf2lpc(const float *lspf, float *lpc)
  75. {
  76. double pa[6], qa[6];
  77. int i;
  78. double bandwith_expansion_coeff = QCELP_BANDWITH_EXPANSION_COEFF * 0.5;
  79. lsp2polyf(lspf, pa, 5);
  80. lsp2polyf(lspf + 1, qa, 5);
  81. for (i=4; i>=0; i--)
  82. {
  83. double paf = pa[i+1] + pa[i];
  84. double qaf = qa[i+1] - qa[i];
  85. lpc[i ] = paf + qaf;
  86. lpc[9-i] = paf - qaf;
  87. }
  88. for (i=0; i<10; i++)
  89. {
  90. lpc[i] *= bandwith_expansion_coeff;
  91. bandwith_expansion_coeff *= QCELP_BANDWITH_EXPANSION_COEFF;
  92. }
  93. }