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.

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