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.

202 lines
5.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. #include "lpc.h"
  24. /**
  25. * Levinson-Durbin recursion.
  26. * Produces LPC coefficients from autocorrelation data.
  27. */
  28. static void compute_lpc_coefs(const double *autoc, int max_order,
  29. double lpc[][MAX_LPC_ORDER], double *ref)
  30. {
  31. int i, j;
  32. double err = autoc[0];
  33. double lpc_tmp[MAX_LPC_ORDER];
  34. for(i=0; i<max_order; i++) {
  35. double r = -autoc[i+1];
  36. for(j=0; j<i; j++)
  37. r -= lpc_tmp[j] * autoc[i-j];
  38. r /= err;
  39. ref[i] = fabs(r);
  40. err *= 1.0 - (r * r);
  41. lpc_tmp[i] = r;
  42. for(j=0; j < i>>1; j++) {
  43. double tmp = lpc_tmp[j];
  44. lpc_tmp[j] += r * lpc_tmp[i-1-j];
  45. lpc_tmp[i-1-j] += r * tmp;
  46. }
  47. if(i & 1)
  48. lpc_tmp[j] += lpc_tmp[j] * r;
  49. for(j=0; j<=i; j++)
  50. lpc[i][j] = -lpc_tmp[j];
  51. }
  52. }
  53. /**
  54. * Quantize LPC coefficients
  55. */
  56. static void quantize_lpc_coefs(double *lpc_in, int order, int precision,
  57. int32_t *lpc_out, int *shift, int max_shift, int zero_shift)
  58. {
  59. int i;
  60. double cmax, error;
  61. int32_t qmax;
  62. int sh;
  63. /* define maximum levels */
  64. qmax = (1 << (precision - 1)) - 1;
  65. /* find maximum coefficient value */
  66. cmax = 0.0;
  67. for(i=0; i<order; i++) {
  68. cmax= FFMAX(cmax, fabs(lpc_in[i]));
  69. }
  70. /* if maximum value quantizes to zero, return all zeros */
  71. if(cmax * (1 << max_shift) < 1.0) {
  72. *shift = zero_shift;
  73. memset(lpc_out, 0, sizeof(int32_t) * order);
  74. return;
  75. }
  76. /* calculate level shift which scales max coeff to available bits */
  77. sh = max_shift;
  78. while((cmax * (1 << sh) > qmax) && (sh > 0)) {
  79. sh--;
  80. }
  81. /* since negative shift values are unsupported in decoder, scale down
  82. coefficients instead */
  83. if(sh == 0 && cmax > qmax) {
  84. double scale = ((double)qmax) / cmax;
  85. for(i=0; i<order; i++) {
  86. lpc_in[i] *= scale;
  87. }
  88. }
  89. /* output quantized coefficients and level shift */
  90. error=0;
  91. for(i=0; i<order; i++) {
  92. error += lpc_in[i] * (1 << sh);
  93. lpc_out[i] = av_clip(lrintf(error), -qmax, qmax);
  94. error -= lpc_out[i];
  95. }
  96. *shift = sh;
  97. }
  98. static int estimate_best_order(double *ref, int min_order, int max_order)
  99. {
  100. int i, est;
  101. est = min_order;
  102. for(i=max_order-1; i>=min_order-1; i--) {
  103. if(ref[i] > 0.10) {
  104. est = i+1;
  105. break;
  106. }
  107. }
  108. return est;
  109. }
  110. /**
  111. * Calculate LPC coefficients for multiple orders
  112. */
  113. int ff_lpc_calc_coefs(DSPContext *s,
  114. const int32_t *samples, int blocksize, int min_order,
  115. int max_order, int precision,
  116. int32_t coefs[][MAX_LPC_ORDER], int *shift, int use_lpc,
  117. int omethod, int max_shift, int zero_shift)
  118. {
  119. double autoc[MAX_LPC_ORDER+1];
  120. double ref[MAX_LPC_ORDER];
  121. double lpc[MAX_LPC_ORDER][MAX_LPC_ORDER];
  122. int i, j, pass;
  123. int opt_order;
  124. assert(max_order >= MIN_LPC_ORDER && max_order <= MAX_LPC_ORDER);
  125. if(use_lpc == 1){
  126. s->flac_compute_autocorr(samples, blocksize, max_order, autoc);
  127. compute_lpc_coefs(autoc, max_order, lpc, ref);
  128. }else{
  129. LLSModel m[2];
  130. double var[MAX_LPC_ORDER+1], weight;
  131. for(pass=0; pass<use_lpc-1; pass++){
  132. av_init_lls(&m[pass&1], max_order);
  133. weight=0;
  134. for(i=max_order; i<blocksize; i++){
  135. for(j=0; j<=max_order; j++)
  136. var[j]= samples[i-j];
  137. if(pass){
  138. double eval, inv, rinv;
  139. eval= av_evaluate_lls(&m[(pass-1)&1], var+1, max_order-1);
  140. eval= (512>>pass) + fabs(eval - var[0]);
  141. inv = 1/eval;
  142. rinv = sqrt(inv);
  143. for(j=0; j<=max_order; j++)
  144. var[j] *= rinv;
  145. weight += inv;
  146. }else
  147. weight++;
  148. av_update_lls(&m[pass&1], var, 1.0);
  149. }
  150. av_solve_lls(&m[pass&1], 0.001, 0);
  151. }
  152. for(i=0; i<max_order; i++){
  153. for(j=0; j<max_order; j++)
  154. lpc[i][j]= m[(pass-1)&1].coeff[i][j];
  155. ref[i]= sqrt(m[(pass-1)&1].variance[i] / weight) * (blocksize - max_order) / 4000;
  156. }
  157. for(i=max_order-1; i>0; i--)
  158. ref[i] = ref[i-1] - ref[i];
  159. }
  160. opt_order = max_order;
  161. if(omethod == ORDER_METHOD_EST) {
  162. opt_order = estimate_best_order(ref, min_order, max_order);
  163. i = opt_order-1;
  164. quantize_lpc_coefs(lpc[i], i+1, precision, coefs[i], &shift[i], max_shift, zero_shift);
  165. } else {
  166. for(i=min_order-1; i<max_order; i++) {
  167. quantize_lpc_coefs(lpc[i], i+1, precision, coefs[i], &shift[i], max_shift, zero_shift);
  168. }
  169. }
  170. return opt_order;
  171. }