Mercurial > libavcodec.hg
comparison lsp.c @ 6770:e6338dd4a6e3 libavcodec
Replace hardcoded LP filter order with parameter.
This will allow to reuse the code in the SIPR decoder.
author | voroshil |
---|---|
date | Sun, 11 May 2008 03:18:18 +0000 |
parents | df4f7cf1ee5f |
children | ca449dc51bd7 |
comparison
equal
deleted
inserted
replaced
6769:c834f8d9e854 | 6770:e6338dd4a6e3 |
---|---|
26 #define FRAC_BITS 14 | 26 #define FRAC_BITS 14 |
27 #include "mathops.h" | 27 #include "mathops.h" |
28 #include "lsp.h" | 28 #include "lsp.h" |
29 #include "acelp_math.h" | 29 #include "acelp_math.h" |
30 | 30 |
31 void ff_acelp_reorder_lsf(int16_t* lsfq, int lsfq_min_distance, int lsfq_min, int lsfq_max) | 31 void ff_acelp_reorder_lsf(int16_t* lsfq, int lsfq_min_distance, int lsfq_min, int lsfq_max, int lp_order) |
32 { | 32 { |
33 int i, j; | 33 int i, j; |
34 | 34 |
35 /* sort lsfq in ascending order. float bubble agorithm, | 35 /* sort lsfq in ascending order. float bubble agorithm, |
36 O(n) if data already sorted, O(n^2) - otherwise */ | 36 O(n) if data already sorted, O(n^2) - otherwise */ |
37 for(i=0; i<9; i++) | 37 for(i=0; i<lp_order-1; i++) |
38 for(j=i; j>=0 && lsfq[j] > lsfq[j+1]; j--) | 38 for(j=i; j>=0 && lsfq[j] > lsfq[j+1]; j--) |
39 FFSWAP(int16_t, lsfq[j], lsfq[j+1]); | 39 FFSWAP(int16_t, lsfq[j], lsfq[j+1]); |
40 | 40 |
41 for(i=0;i<10; i++) | 41 for(i=0; i<lp_order; i++) |
42 { | 42 { |
43 lsfq[i] = FFMAX(lsfq[i], lsfq_min); | 43 lsfq[i] = FFMAX(lsfq[i], lsfq_min); |
44 lsfq_min = lsfq[i] + lsfq_min_distance; | 44 lsfq_min = lsfq[i] + lsfq_min_distance; |
45 } | 45 } |
46 lsfq[9] = FFMIN(lsfq[9], lsfq_max);//Is warning required ? | 46 lsfq[lp_order-1] = FFMIN(lsfq[lp_order-1], lsfq_max);//Is warning required ? |
47 } | 47 } |
48 | 48 |
49 void ff_acelp_lsf2lsp(int16_t *lsp, const int16_t *lsf) | 49 void ff_acelp_lsf2lsp(int16_t *lsp, const int16_t *lsf, int lp_order) |
50 { | 50 { |
51 int i; | 51 int i; |
52 | 52 |
53 /* Convert LSF to LSP, lsp=cos(lsf) */ | 53 /* Convert LSF to LSP, lsp=cos(lsf) */ |
54 for(i=0;i<10; i++) | 54 for(i=0; i<lp_order; i++) |
55 // 20861 = 2.0 / PI in (0.15) | 55 // 20861 = 2.0 / PI in (0.15) |
56 lsp[i] = ff_cos(lsf[i] * 20861 >> 15); // divide by PI and (0,13) -> (0,14) | 56 lsp[i] = ff_cos(lsf[i] * 20861 >> 15); // divide by PI and (0,13) -> (0,14) |
57 } | 57 } |
58 | 58 |
59 /** | 59 /** |
60 * \brief decodes polynomial coefficients from LSP | 60 * \brief decodes polynomial coefficients from LSP |
61 * \param f [out] decoded polynomial coefficients (-0x20000000 <= (3.22) <= 0x1fffffff) | 61 * \param f [out] decoded polynomial coefficients (-0x20000000 <= (3.22) <= 0x1fffffff) |
62 * \param lsp LSP coefficients (-0x8000 <= (0.15) <= 0x7fff) | 62 * \param lsp LSP coefficients (-0x8000 <= (0.15) <= 0x7fff) |
63 */ | 63 */ |
64 static void lsp2poly(int* f, const int16_t* lsp) | 64 static void lsp2poly(int* f, const int16_t* lsp, int lp_half_order) |
65 { | 65 { |
66 int i, j; | 66 int i, j; |
67 | 67 |
68 f[0] = 0x400000; // 1.0 in (3.22) | 68 f[0] = 0x400000; // 1.0 in (3.22) |
69 f[1] = -lsp[0] << 8; // *2 and (0.15) -> (3.22) | 69 f[1] = -lsp[0] << 8; // *2 and (0.15) -> (3.22) |
70 | 70 |
71 for(i=2; i<=5; i++) | 71 for(i=2; i<=lp_half_order; i++) |
72 { | 72 { |
73 f[i] = f[i-2]; | 73 f[i] = f[i-2]; |
74 for(j=i; j>1; j--) | 74 for(j=i; j>1; j--) |
75 f[j] -= MULL(f[j-1], lsp[2*i-2]) - f[j-2]; // (3.22) * (0.15) * 2 -> (3.22) | 75 f[j] -= MULL(f[j-1], lsp[2*i-2]) - f[j-2]; // (3.22) * (0.15) * 2 -> (3.22) |
76 | 76 |
77 f[1] -= lsp[2*i-2] << 8; | 77 f[1] -= lsp[2*i-2] << 8; |
78 } | 78 } |
79 } | 79 } |
80 | 80 |
81 void ff_acelp_lsp2lpc(int16_t* lp, const int16_t* lsp) | 81 void ff_acelp_lsp2lpc(int16_t* lp, const int16_t* lsp, int lp_half_order) |
82 { | 82 { |
83 int i; | 83 int i; |
84 int f1[6]; // (3.22) | 84 int f1[lp_half_order+1]; // (3.22) |
85 int f2[6]; // (3.22) | 85 int f2[lp_half_order+1]; // (3.22) |
86 | 86 |
87 lsp2poly(f1, lsp ); | 87 lsp2poly(f1, lsp , lp_half_order); |
88 lsp2poly(f2, lsp+1); | 88 lsp2poly(f2, lsp+1, lp_half_order); |
89 | 89 |
90 /* 3.2.6 of G.729, Equations 25 and 26*/ | 90 /* 3.2.6 of G.729, Equations 25 and 26*/ |
91 lp[0] = 4096; | 91 lp[0] = 4096; |
92 for(i=1; i<6; i++) | 92 for(i=1; i<lp_half_order+1; i++) |
93 { | 93 { |
94 int ff1 = f1[i] + f1[i-1]; // (3.22) | 94 int ff1 = f1[i] + f1[i-1]; // (3.22) |
95 int ff2 = f2[i] - f2[i-1]; // (3.22) | 95 int ff2 = f2[i] - f2[i-1]; // (3.22) |
96 | 96 |
97 ff1 += 1 << 10; // for rounding | 97 ff1 += 1 << 10; // for rounding |
98 lp[i] = (ff1 + ff2) >> 11; // divide by 2 and (3.22) -> (3.12) | 98 lp[i] = (ff1 + ff2) >> 11; // divide by 2 and (3.22) -> (3.12) |
99 lp[11-i] = (ff1 - ff2) >> 11; // divide by 2 and (3.22) -> (3.12) | 99 lp[lp_order + 1 - i] = (ff1 - ff2) >> 11; // divide by 2 and (3.22) -> (3.12) |
100 } | 100 } |
101 } | 101 } |
102 | 102 |
103 void ff_acelp_lp_decode(int16_t* lp_1st, int16_t* lp_2nd, const int16_t* lsp_2nd, const int16_t* lsp_prev) | 103 void ff_acelp_lp_decode(int16_t* lp_1st, int16_t* lp_2nd, const int16_t* lsp_2nd, const int16_t* lsp_prev, int lp_order) |
104 { | 104 { |
105 int16_t lsp_1st[10]; // (0.15) | 105 int16_t lsp_1st[lp_order]; // (0.15) |
106 int i; | 106 int i; |
107 | 107 |
108 /* LSP values for first subframe (3.2.5 of G.729, Equation 24)*/ | 108 /* LSP values for first subframe (3.2.5 of G.729, Equation 24)*/ |
109 for(i=0;i<10;i++) | 109 for(i=0; i<lp_order; i++) |
110 #ifdef G729_BITEXACT | 110 #ifdef G729_BITEXACT |
111 lsp_1st[i] = (lsp_2nd[i] >> 1) + (lsp_prev[i] >> 1); | 111 lsp_1st[i] = (lsp_2nd[i] >> 1) + (lsp_prev[i] >> 1); |
112 #else | 112 #else |
113 lsp_1st[i] = (lsp_2nd[i] + lsp_prev[i]) >> 1; | 113 lsp_1st[i] = (lsp_2nd[i] + lsp_prev[i]) >> 1; |
114 #endif | 114 #endif |
115 | 115 |
116 ff_acelp_lsp2lpc(lp_1st, lsp_1st); | 116 ff_acelp_lsp2lpc(lp_1st, lsp_1st, lp_order >> 1); |
117 | 117 |
118 /* LSP values for second subframe (3.2.5 of G.729)*/ | 118 /* LSP values for second subframe (3.2.5 of G.729)*/ |
119 ff_acelp_lsp2lpc(lp_2nd, lsp_2nd); | 119 ff_acelp_lsp2lpc(lp_2nd, lsp_2nd, lp_order >> 1); |
120 } | 120 } |