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.

203 lines
5.8KB

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