lpc.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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/common.h"
  22. #include "libavutil/lls.h"
  23. #define LPC_USE_DOUBLE
  24. #include "lpc.h"
  25. #include "libavutil/avassert.h"
  26. /**
  27. * Apply Welch window function to audio block
  28. */
  29. static void lpc_apply_welch_window_c(const int32_t *data, int len,
  30. double *w_data)
  31. {
  32. int i, n2;
  33. double w;
  34. double c;
  35. n2 = (len >> 1);
  36. c = 2.0 / (len - 1.0);
  37. if (len & 1) {
  38. for(i=0; i<n2; i++) {
  39. w = c - i - 1.0;
  40. w = 1.0 - (w * w);
  41. w_data[i] = data[i] * w;
  42. w_data[len-1-i] = data[len-1-i] * w;
  43. }
  44. return;
  45. }
  46. w_data+=n2;
  47. data+=n2;
  48. for(i=0; i<n2; i++) {
  49. w = c - n2 + i;
  50. w = 1.0 - (w * w);
  51. w_data[-i-1] = data[-i-1] * w;
  52. w_data[+i ] = data[+i ] * w;
  53. }
  54. }
  55. /**
  56. * Calculate autocorrelation data from audio samples
  57. * A Welch window function is applied before calculation.
  58. */
  59. static void lpc_compute_autocorr_c(const double *data, int len, int lag,
  60. double *autoc)
  61. {
  62. int i, j;
  63. for(j=0; j<lag; j+=2){
  64. double sum0 = 1.0, sum1 = 1.0;
  65. for(i=j; i<len; i++){
  66. sum0 += data[i] * data[i-j];
  67. sum1 += data[i] * data[i-j-1];
  68. }
  69. autoc[j ] = sum0;
  70. autoc[j+1] = sum1;
  71. }
  72. if(j==lag){
  73. double sum = 1.0;
  74. for(i=j-1; i<len; i+=2){
  75. sum += data[i ] * data[i-j ]
  76. + data[i+1] * data[i-j+1];
  77. }
  78. autoc[j] = sum;
  79. }
  80. }
  81. /**
  82. * Quantize LPC coefficients
  83. */
  84. static void quantize_lpc_coefs(double *lpc_in, int order, int precision,
  85. int32_t *lpc_out, int *shift, int min_shift,
  86. int max_shift, int zero_shift)
  87. {
  88. int i;
  89. double cmax, error;
  90. int32_t qmax;
  91. int sh;
  92. /* define maximum levels */
  93. qmax = (1 << (precision - 1)) - 1;
  94. /* find maximum coefficient value */
  95. cmax = 0.0;
  96. for(i=0; i<order; i++) {
  97. cmax= FFMAX(cmax, fabs(lpc_in[i]));
  98. }
  99. /* if maximum value quantizes to zero, return all zeros */
  100. if(cmax * (1 << max_shift) < 1.0) {
  101. *shift = zero_shift;
  102. memset(lpc_out, 0, sizeof(int32_t) * order);
  103. return;
  104. }
  105. /* calculate level shift which scales max coeff to available bits */
  106. sh = max_shift;
  107. while((cmax * (1 << sh) > qmax) && (sh > min_shift)) {
  108. sh--;
  109. }
  110. /* since negative shift values are unsupported in decoder, scale down
  111. coefficients instead */
  112. if(sh == 0 && cmax > qmax) {
  113. double scale = ((double)qmax) / cmax;
  114. for(i=0; i<order; i++) {
  115. lpc_in[i] *= scale;
  116. }
  117. }
  118. /* output quantized coefficients and level shift */
  119. error=0;
  120. for(i=0; i<order; i++) {
  121. error -= lpc_in[i] * (1 << sh);
  122. lpc_out[i] = av_clip(lrintf(error), -qmax, qmax);
  123. error -= lpc_out[i];
  124. }
  125. *shift = sh;
  126. }
  127. static int estimate_best_order(double *ref, int min_order, int max_order)
  128. {
  129. int i, est;
  130. est = min_order;
  131. for(i=max_order-1; i>=min_order-1; i--) {
  132. if(ref[i] > 0.10) {
  133. est = i+1;
  134. break;
  135. }
  136. }
  137. return est;
  138. }
  139. int ff_lpc_calc_ref_coefs(LPCContext *s,
  140. const int32_t *samples, int order, double *ref)
  141. {
  142. double autoc[MAX_LPC_ORDER + 1];
  143. s->lpc_apply_welch_window(samples, s->blocksize, s->windowed_samples);
  144. s->lpc_compute_autocorr(s->windowed_samples, s->blocksize, order, autoc);
  145. compute_ref_coefs(autoc, order, ref, NULL);
  146. return order;
  147. }
  148. double ff_lpc_calc_ref_coefs_f(LPCContext *s, const float *samples, int len,
  149. int order, double *ref)
  150. {
  151. int i;
  152. double signal = 0.0f, avg_err = 0.0f;
  153. double autoc[MAX_LPC_ORDER+1] = {0}, error[MAX_LPC_ORDER+1] = {0};
  154. const double a = 0.5f, b = 1.0f - a;
  155. /* Apply windowing */
  156. for (i = 0; i <= len / 2; i++) {
  157. double weight = a - b*cos((2*M_PI*i)/(len - 1));
  158. s->windowed_samples[i] = weight*samples[i];
  159. s->windowed_samples[len-1-i] = weight*samples[len-1-i];
  160. }
  161. s->lpc_compute_autocorr(s->windowed_samples, len, order, autoc);
  162. signal = autoc[0];
  163. compute_ref_coefs(autoc, order, ref, error);
  164. for (i = 0; i < order; i++)
  165. avg_err = (avg_err + error[i])/2.0f;
  166. return signal/avg_err;
  167. }
  168. /**
  169. * Calculate LPC coefficients for multiple orders
  170. *
  171. * @param lpc_type LPC method for determining coefficients,
  172. * see #FFLPCType for details
  173. */
  174. int ff_lpc_calc_coefs(LPCContext *s,
  175. const int32_t *samples, int blocksize, int min_order,
  176. int max_order, int precision,
  177. int32_t coefs[][MAX_LPC_ORDER], int *shift,
  178. enum FFLPCType lpc_type, int lpc_passes,
  179. int omethod, int min_shift, int max_shift, int zero_shift)
  180. {
  181. double autoc[MAX_LPC_ORDER+1];
  182. double ref[MAX_LPC_ORDER] = { 0 };
  183. double lpc[MAX_LPC_ORDER][MAX_LPC_ORDER];
  184. int i, j, pass = 0;
  185. int opt_order;
  186. av_assert2(max_order >= MIN_LPC_ORDER && max_order <= MAX_LPC_ORDER &&
  187. lpc_type > FF_LPC_TYPE_FIXED);
  188. av_assert0(lpc_type == FF_LPC_TYPE_CHOLESKY || lpc_type == FF_LPC_TYPE_LEVINSON);
  189. /* reinit LPC context if parameters have changed */
  190. if (blocksize != s->blocksize || max_order != s->max_order ||
  191. lpc_type != s->lpc_type) {
  192. ff_lpc_end(s);
  193. ff_lpc_init(s, blocksize, max_order, lpc_type);
  194. }
  195. if(lpc_passes <= 0)
  196. lpc_passes = 2;
  197. if (lpc_type == FF_LPC_TYPE_LEVINSON || (lpc_type == FF_LPC_TYPE_CHOLESKY && lpc_passes > 1)) {
  198. s->lpc_apply_welch_window(samples, blocksize, s->windowed_samples);
  199. s->lpc_compute_autocorr(s->windowed_samples, blocksize, max_order, autoc);
  200. compute_lpc_coefs(autoc, max_order, &lpc[0][0], MAX_LPC_ORDER, 0, 1);
  201. for(i=0; i<max_order; i++)
  202. ref[i] = fabs(lpc[i][i]);
  203. pass++;
  204. }
  205. if (lpc_type == FF_LPC_TYPE_CHOLESKY) {
  206. LLSModel *m = s->lls_models;
  207. LOCAL_ALIGNED(32, double, var, [FFALIGN(MAX_LPC_ORDER+1,4)]);
  208. double av_uninit(weight);
  209. memset(var, 0, FFALIGN(MAX_LPC_ORDER+1,4)*sizeof(*var));
  210. for(j=0; j<max_order; j++)
  211. m[0].coeff[max_order-1][j] = -lpc[max_order-1][j];
  212. for(; pass<lpc_passes; pass++){
  213. avpriv_init_lls(&m[pass&1], max_order);
  214. weight=0;
  215. for(i=max_order; i<blocksize; i++){
  216. for(j=0; j<=max_order; j++)
  217. var[j]= samples[i-j];
  218. if(pass){
  219. double eval, inv, rinv;
  220. eval= m[pass&1].evaluate_lls(&m[(pass-1)&1], var+1, max_order-1);
  221. eval= (512>>pass) + fabs(eval - var[0]);
  222. inv = 1/eval;
  223. rinv = sqrt(inv);
  224. for(j=0; j<=max_order; j++)
  225. var[j] *= rinv;
  226. weight += inv;
  227. }else
  228. weight++;
  229. m[pass&1].update_lls(&m[pass&1], var);
  230. }
  231. avpriv_solve_lls(&m[pass&1], 0.001, 0);
  232. }
  233. for(i=0; i<max_order; i++){
  234. for(j=0; j<max_order; j++)
  235. lpc[i][j]=-m[(pass-1)&1].coeff[i][j];
  236. ref[i]= sqrt(m[(pass-1)&1].variance[i] / weight) * (blocksize - max_order) / 4000;
  237. }
  238. for(i=max_order-1; i>0; i--)
  239. ref[i] = ref[i-1] - ref[i];
  240. }
  241. opt_order = max_order;
  242. if(omethod == ORDER_METHOD_EST) {
  243. opt_order = estimate_best_order(ref, min_order, max_order);
  244. i = opt_order-1;
  245. quantize_lpc_coefs(lpc[i], i+1, precision, coefs[i], &shift[i],
  246. min_shift, max_shift, zero_shift);
  247. } else {
  248. for(i=min_order-1; i<max_order; i++) {
  249. quantize_lpc_coefs(lpc[i], i+1, precision, coefs[i], &shift[i],
  250. min_shift, max_shift, zero_shift);
  251. }
  252. }
  253. return opt_order;
  254. }
  255. av_cold int ff_lpc_init(LPCContext *s, int blocksize, int max_order,
  256. enum FFLPCType lpc_type)
  257. {
  258. s->blocksize = blocksize;
  259. s->max_order = max_order;
  260. s->lpc_type = lpc_type;
  261. s->windowed_buffer = av_mallocz((blocksize + 2 + FFALIGN(max_order, 4)) *
  262. sizeof(*s->windowed_samples));
  263. if (!s->windowed_buffer)
  264. return AVERROR(ENOMEM);
  265. s->windowed_samples = s->windowed_buffer + FFALIGN(max_order, 4);
  266. s->lpc_apply_welch_window = lpc_apply_welch_window_c;
  267. s->lpc_compute_autocorr = lpc_compute_autocorr_c;
  268. if (ARCH_X86)
  269. ff_lpc_init_x86(s);
  270. return 0;
  271. }
  272. av_cold void ff_lpc_end(LPCContext *s)
  273. {
  274. av_freep(&s->windowed_buffer);
  275. }