You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

237 lines
6.7KB

  1. /**
  2. * LPC utility code
  3. * Copyright (c) 2006 Justin Ruggles <justin.ruggles@gmail.com>
  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. #include "libavutil/lls.h"
  22. #include "dsputil.h"
  23. #define LPC_USE_DOUBLE
  24. #include "lpc.h"
  25. /**
  26. * Apply Welch window function to audio block
  27. */
  28. static void apply_welch_window(const int32_t *data, int len, double *w_data)
  29. {
  30. int i, n2;
  31. double w;
  32. double c;
  33. assert(!(len&1)); //the optimization in r11881 does not support odd len
  34. //if someone wants odd len extend the change in r11881
  35. n2 = (len >> 1);
  36. c = 2.0 / (len - 1.0);
  37. w_data+=n2;
  38. data+=n2;
  39. for(i=0; i<n2; i++) {
  40. w = c - n2 + i;
  41. w = 1.0 - (w * w);
  42. w_data[-i-1] = data[-i-1] * w;
  43. w_data[+i ] = data[+i ] * w;
  44. }
  45. }
  46. /**
  47. * Calculates autocorrelation data from audio samples
  48. * A Welch window function is applied before calculation.
  49. */
  50. void ff_lpc_compute_autocorr(const int32_t *data, int len, int lag,
  51. double *autoc)
  52. {
  53. int i, j;
  54. double tmp[len + lag + 1];
  55. double *data1= tmp + lag;
  56. apply_welch_window(data, len, data1);
  57. for(j=0; j<lag; j++)
  58. data1[j-lag]= 0.0;
  59. data1[len] = 0.0;
  60. for(j=0; j<lag; j+=2){
  61. double sum0 = 1.0, sum1 = 1.0;
  62. for(i=j; i<len; i++){
  63. sum0 += data1[i] * data1[i-j];
  64. sum1 += data1[i] * data1[i-j-1];
  65. }
  66. autoc[j ] = sum0;
  67. autoc[j+1] = sum1;
  68. }
  69. if(j==lag){
  70. double sum = 1.0;
  71. for(i=j-1; i<len; i+=2){
  72. sum += data1[i ] * data1[i-j ]
  73. + data1[i+1] * data1[i-j+1];
  74. }
  75. autoc[j] = sum;
  76. }
  77. }
  78. /**
  79. * Quantize LPC coefficients
  80. */
  81. static void quantize_lpc_coefs(double *lpc_in, int order, int precision,
  82. int32_t *lpc_out, int *shift, int max_shift, int zero_shift)
  83. {
  84. int i;
  85. double cmax, error;
  86. int32_t qmax;
  87. int sh;
  88. /* define maximum levels */
  89. qmax = (1 << (precision - 1)) - 1;
  90. /* find maximum coefficient value */
  91. cmax = 0.0;
  92. for(i=0; i<order; i++) {
  93. cmax= FFMAX(cmax, fabs(lpc_in[i]));
  94. }
  95. /* if maximum value quantizes to zero, return all zeros */
  96. if(cmax * (1 << max_shift) < 1.0) {
  97. *shift = zero_shift;
  98. memset(lpc_out, 0, sizeof(int32_t) * order);
  99. return;
  100. }
  101. /* calculate level shift which scales max coeff to available bits */
  102. sh = max_shift;
  103. while((cmax * (1 << sh) > qmax) && (sh > 0)) {
  104. sh--;
  105. }
  106. /* since negative shift values are unsupported in decoder, scale down
  107. coefficients instead */
  108. if(sh == 0 && cmax > qmax) {
  109. double scale = ((double)qmax) / cmax;
  110. for(i=0; i<order; i++) {
  111. lpc_in[i] *= scale;
  112. }
  113. }
  114. /* output quantized coefficients and level shift */
  115. error=0;
  116. for(i=0; i<order; i++) {
  117. error -= lpc_in[i] * (1 << sh);
  118. lpc_out[i] = av_clip(lrintf(error), -qmax, qmax);
  119. error -= lpc_out[i];
  120. }
  121. *shift = sh;
  122. }
  123. static int estimate_best_order(double *ref, int min_order, int max_order)
  124. {
  125. int i, est;
  126. est = min_order;
  127. for(i=max_order-1; i>=min_order-1; i--) {
  128. if(ref[i] > 0.10) {
  129. est = i+1;
  130. break;
  131. }
  132. }
  133. return est;
  134. }
  135. /**
  136. * Calculate LPC coefficients for multiple orders
  137. *
  138. * @param use_lpc LPC method for determining coefficients
  139. * 0 = LPC with fixed pre-defined coeffs
  140. * 1 = LPC with coeffs determined by Levinson-Durbin recursion
  141. * 2+ = LPC with coeffs determined by Cholesky factorization using (use_lpc-1) passes.
  142. */
  143. int ff_lpc_calc_coefs(DSPContext *s,
  144. const int32_t *samples, int blocksize, int min_order,
  145. int max_order, int precision,
  146. int32_t coefs[][MAX_LPC_ORDER], int *shift, int use_lpc,
  147. int omethod, int max_shift, int zero_shift)
  148. {
  149. double autoc[MAX_LPC_ORDER+1];
  150. double ref[MAX_LPC_ORDER];
  151. double lpc[MAX_LPC_ORDER][MAX_LPC_ORDER];
  152. int i, j, pass;
  153. int opt_order;
  154. assert(max_order >= MIN_LPC_ORDER && max_order <= MAX_LPC_ORDER && use_lpc > 0);
  155. if(use_lpc == 1){
  156. s->lpc_compute_autocorr(samples, blocksize, max_order, autoc);
  157. compute_lpc_coefs(autoc, max_order, &lpc[0][0], MAX_LPC_ORDER, 0, 1);
  158. for(i=0; i<max_order; i++)
  159. ref[i] = fabs(lpc[i][i]);
  160. }else{
  161. LLSModel m[2];
  162. double var[MAX_LPC_ORDER+1], av_uninit(weight);
  163. for(pass=0; pass<use_lpc-1; pass++){
  164. av_init_lls(&m[pass&1], max_order);
  165. weight=0;
  166. for(i=max_order; i<blocksize; i++){
  167. for(j=0; j<=max_order; j++)
  168. var[j]= samples[i-j];
  169. if(pass){
  170. double eval, inv, rinv;
  171. eval= av_evaluate_lls(&m[(pass-1)&1], var+1, max_order-1);
  172. eval= (512>>pass) + fabs(eval - var[0]);
  173. inv = 1/eval;
  174. rinv = sqrt(inv);
  175. for(j=0; j<=max_order; j++)
  176. var[j] *= rinv;
  177. weight += inv;
  178. }else
  179. weight++;
  180. av_update_lls(&m[pass&1], var, 1.0);
  181. }
  182. av_solve_lls(&m[pass&1], 0.001, 0);
  183. }
  184. for(i=0; i<max_order; i++){
  185. for(j=0; j<max_order; j++)
  186. lpc[i][j]=-m[(pass-1)&1].coeff[i][j];
  187. ref[i]= sqrt(m[(pass-1)&1].variance[i] / weight) * (blocksize - max_order) / 4000;
  188. }
  189. for(i=max_order-1; i>0; i--)
  190. ref[i] = ref[i-1] - ref[i];
  191. }
  192. opt_order = max_order;
  193. if(omethod == ORDER_METHOD_EST) {
  194. opt_order = estimate_best_order(ref, min_order, max_order);
  195. i = opt_order-1;
  196. quantize_lpc_coefs(lpc[i], i+1, precision, coefs[i], &shift[i], max_shift, zero_shift);
  197. } else {
  198. for(i=min_order-1; i<max_order; i++) {
  199. quantize_lpc_coefs(lpc[i], i+1, precision, coefs[i], &shift[i], max_shift, zero_shift);
  200. }
  201. }
  202. return opt_order;
  203. }