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.

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