1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
18 /****************************************************************************************
19 Portions of this file are derived from the following 3GPP standard:
22 ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec
23 Available from http://www.3gpp.org
25 (C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC)
26 Permission to distribute, modify and use this file under the standard license
27 terms listed above has been obtained from the copyright holder.
28 ****************************************************************************************/
30 ------------------------------------------------------------------------------
34 Filename: d_plsf_5.cpp
36 ------------------------------------------------------------------------------
39 /*----------------------------------------------------------------------------
41 ----------------------------------------------------------------------------*/
50 /*--------------------------------------------------------------------------*/
56 /*----------------------------------------------------------------------------
58 ; Define module specific macros here
59 ----------------------------------------------------------------------------*/
62 /*----------------------------------------------------------------------------
64 ; Include all pre-processor statements here. Include conditional
65 ; compile variables also.
66 ----------------------------------------------------------------------------*/
68 /* ONE_ALPHA-> (1.0-ALPHA) */
70 #define ONE_ALPHA 1639
72 /*----------------------------------------------------------------------------
73 ; LOCAL FUNCTION DEFINITIONS
74 ; Function Prototype declaration
75 ----------------------------------------------------------------------------*/
77 /*----------------------------------------------------------------------------
78 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
79 ; Variable declaration - defined here and used outside this module
80 ----------------------------------------------------------------------------*/
82 /*--------------------------------------------------------------------------*/
88 ------------------------------------------------------------------------------
89 FUNCTION NAME: D_plsf_5
90 ------------------------------------------------------------------------------
91 INPUT AND OUTPUT DEFINITIONS
94 st = pointer to a structure of type D_plsfState
95 bfi = bad frame indicator; set to 1 if a bad frame is received (Word16)
96 indice = pointer to quantization indices of 5 submatrices (Word16)
97 lsp1_q = pointer to the quantized 1st LSP vector (Word16)
98 lsp2_q = pointer to the quantized 2nd LSP vector (Word16)
101 lsp1_q points to the updated quantized 1st LSP vector
102 lsp2_q points to the updated quantized 2nd LSP vector
103 Flag *pOverflow -- Flag set when overflow occurs.
106 return_value = 0 (int)
108 Global Variables Used:
111 Local Variables Needed:
114 ------------------------------------------------------------------------------
117 This function decodes the 2 sets of LSP parameters in a frame using the
118 received quantization indices.
120 ------------------------------------------------------------------------------
125 ------------------------------------------------------------------------------
128 d_plsf_5.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
130 ------------------------------------------------------------------------------
134 D_plsfState *st, // i/o: State variables
135 Word16 bfi, // i : bad frame indicator (set to 1 if a bad
137 Word16 *indice, // i : quantization indices of 5 submatrices, Q0
138 Word16 *lsp1_q, // o : quantized 1st LSP vector (M), Q15
139 Word16 *lsp2_q // o : quantized 2nd LSP vector (M), Q15
143 const Word16 *p_dico;
145 Word16 lsf1_r[M], lsf2_r[M];
146 Word16 lsf1_q[M], lsf2_q[M];
148 if (bfi != 0) // if bad frame
150 // use the past LSFs slightly shifted towards their mean
152 for (i = 0; i < M; i++)
154 // lsfi_q[i] = ALPHA*st->past_lsf_q[i] + ONE_ALPHA*mean_lsf[i];
156 lsf1_q[i] = add (mult (st->past_lsf_q[i], ALPHA),
157 mult (mean_lsf[i], ONE_ALPHA));
159 lsf2_q[i] = lsf1_q[i];
162 // estimate past quantized residual to be used in next frame
164 for (i = 0; i < M; i++)
166 // temp = mean_lsf[i] + st->past_r_q[i] * LSP_PRED_FAC_MR122;
168 temp = add (mean_lsf[i], mult (st->past_r_q[i],
169 LSP_PRED_FAC_MR122));
171 st->past_r_q[i] = sub (lsf2_q[i], temp);
175 // if good LSFs received
177 // decode prediction residuals from 5 received indices
179 p_dico = &dico1_lsf[shl (indice[0], 2)];
180 lsf1_r[0] = *p_dico++;
181 lsf1_r[1] = *p_dico++;
182 lsf2_r[0] = *p_dico++;
183 lsf2_r[1] = *p_dico++;
185 p_dico = &dico2_lsf[shl (indice[1], 2)];
186 lsf1_r[2] = *p_dico++;
187 lsf1_r[3] = *p_dico++;
188 lsf2_r[2] = *p_dico++;
189 lsf2_r[3] = *p_dico++;
191 sign = indice[2] & 1;
192 i = shr (indice[2], 1);
193 p_dico = &dico3_lsf[shl (i, 2)];
197 lsf1_r[4] = *p_dico++;
198 lsf1_r[5] = *p_dico++;
199 lsf2_r[4] = *p_dico++;
200 lsf2_r[5] = *p_dico++;
204 lsf1_r[4] = negate (*p_dico++);
205 lsf1_r[5] = negate (*p_dico++);
206 lsf2_r[4] = negate (*p_dico++);
207 lsf2_r[5] = negate (*p_dico++);
210 p_dico = &dico4_lsf[shl (indice[3], 2)];
211 lsf1_r[6] = *p_dico++;
212 lsf1_r[7] = *p_dico++;
213 lsf2_r[6] = *p_dico++;
214 lsf2_r[7] = *p_dico++;
216 p_dico = &dico5_lsf[shl (indice[4], 2)];
217 lsf1_r[8] = *p_dico++;
218 lsf1_r[9] = *p_dico++;
219 lsf2_r[8] = *p_dico++;
220 lsf2_r[9] = *p_dico++;
222 // Compute quantized LSFs and update the past quantized residual
223 for (i = 0; i < M; i++)
225 temp = add (mean_lsf[i], mult (st->past_r_q[i],
226 LSP_PRED_FAC_MR122));
227 lsf1_q[i] = add (lsf1_r[i], temp);
228 lsf2_q[i] = add (lsf2_r[i], temp);
229 st->past_r_q[i] = lsf2_r[i];
233 // verification that LSFs have minimum distance of LSF_GAP Hz
235 Reorder_lsf (lsf1_q, LSF_GAP, M);
236 Reorder_lsf (lsf2_q, LSF_GAP, M);
238 Copy (lsf2_q, st->past_lsf_q, M);
240 // convert LSFs to the cosine domain
242 Lsf_lsp (lsf1_q, lsp1_q, M);
243 Lsf_lsp (lsf2_q, lsp2_q, M);
248 ------------------------------------------------------------------------------
250 [State any special notes, constraints or cautions for users of this function]
252 ------------------------------------------------------------------------------
256 D_plsfState *st, /* i/o: State variables */
257 Word16 bfi, /* i : bad frame indicator (set to 1 if a bad
258 frame is received) */
259 Word16 *indice, /* i : quantization indices of 5 submatrices, Q0 */
260 CommonAmrTbls* common_amr_tbls, /* i : structure containing ptrs to read-only tables */
261 Word16 *lsp1_q, /* o : quantized 1st LSP vector (M), Q15 */
262 Word16 *lsp2_q, /* o : quantized 2nd LSP vector (M), Q15 */
263 Flag *pOverflow /* o : Flag set when overflow occurs */
270 const Word16 *p_dico;
277 /* These tables are defined in q_plsf_5_tbl.c */
278 const Word16* mean_lsf_5_ptr = common_amr_tbls->mean_lsf_5_ptr;
279 const Word16* dico1_lsf_5_ptr = common_amr_tbls->dico1_lsf_5_ptr;
280 const Word16* dico2_lsf_5_ptr = common_amr_tbls->dico2_lsf_5_ptr;
281 const Word16* dico3_lsf_5_ptr = common_amr_tbls->dico3_lsf_5_ptr;
282 const Word16* dico4_lsf_5_ptr = common_amr_tbls->dico4_lsf_5_ptr;
283 const Word16* dico5_lsf_5_ptr = common_amr_tbls->dico5_lsf_5_ptr;
285 if (bfi != 0) /* if bad frame */
287 /* use the past LSFs slightly shifted towards their mean */
289 for (i = 0; i < M; i++)
292 * lsfi_q[i] = ALPHA*st->past_lsf_q[i] +
293 * ONE_ALPHA*mean_lsf[i];
296 temp = (Word16)(((Word32) st->past_lsf_q[i] * ALPHA) >> 15);
298 sign = (Word16)(((Word32) * (mean_lsf_5_ptr + i) * ONE_ALPHA) >> 15);
300 *(lsf1_q + i) = add_16(sign, temp, pOverflow);
302 *(lsf2_q + i) = *(lsf1_q + i);
305 * estimate past quantized residual to be used in
310 * temp = mean_lsf[i] +
311 * st->past_r_q[i] * LSP_PRED_FAC_MR122;
314 temp = (Word16)(((Word32) st->past_r_q[i] * LSP_PRED_FAC_MR122) >> 15);
316 temp = add_16(*(mean_lsf_5_ptr + i), temp, pOverflow);
318 st->past_r_q[i] = sub(*(lsf2_q + i), temp, pOverflow);
322 /* if good LSFs received */
324 /* decode prediction residuals from 5 received indices */
332 p_dico = &dico1_lsf_5_ptr[temp];
334 *(lsf1_r + 0) = *p_dico++;
335 *(lsf1_r + 1) = *p_dico++;
336 *(lsf2_r + 0) = *p_dico++;
337 *(lsf2_r + 1) = *p_dico++;
339 temp = shl(*(indice + 1), 2, pOverflow);
341 p_dico = &dico2_lsf_5_ptr[temp];
343 *(lsf1_r + 2) = *p_dico++;
344 *(lsf1_r + 3) = *p_dico++;
345 *(lsf2_r + 2) = *p_dico++;
346 *(lsf2_r + 3) = *p_dico++;
348 sign = *(indice + 2) & 1;
350 if (*(indice + 2) < 0)
352 i = ~(~(*(indice + 2)) >> 1);
356 i = *(indice + 2) >> 1;
359 temp = shl(i, 2, pOverflow);
361 p_dico = &dico3_lsf_5_ptr[temp];
365 *(lsf1_r + 4) = *p_dico++;
366 *(lsf1_r + 5) = *p_dico++;
367 *(lsf2_r + 4) = *p_dico++;
368 *(lsf2_r + 5) = *p_dico++;
372 *(lsf1_r + 4) = negate(*p_dico++);
373 *(lsf1_r + 5) = negate(*p_dico++);
374 *(lsf2_r + 4) = negate(*p_dico++);
375 *(lsf2_r + 5) = negate(*p_dico++);
378 temp = shl(*(indice + 3), 2, pOverflow);
380 p_dico = &dico4_lsf_5_ptr[temp];
382 *(lsf1_r + 6) = *p_dico++;
383 *(lsf1_r + 7) = *p_dico++;
384 *(lsf2_r + 6) = *p_dico++;
385 *(lsf2_r + 7) = *p_dico++;
387 temp = shl(*(indice + 4), 2, pOverflow);
389 p_dico = &dico5_lsf_5_ptr[temp];
391 *(lsf1_r + 8) = *p_dico++;
392 *(lsf1_r + 9) = *p_dico++;
393 *(lsf2_r + 8) = *p_dico++;
394 *(lsf2_r + 9) = *p_dico++;
396 /* Compute quantized LSFs and update the past quantized
398 for (i = 0; i < M; i++)
408 *(mean_lsf_5_ptr + i),
424 st->past_r_q[i] = *(lsf2_r + i);
428 /* verification that LSFs have minimum distance of LSF_GAP Hz */
442 oscl_memmove((void *)st->past_lsf_q, lsf2_q, M*sizeof(*lsf2_q));
444 /* convert LSFs to the cosine domain */