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 Filename: q_plsf_5.cpp
32 ------------------------------------------------------------------------------
36 ------------------------------------------------------------------------------
39 /*----------------------------------------------------------------------------
41 ----------------------------------------------------------------------------*/
49 /*--------------------------------------------------------------------------*/
55 /*----------------------------------------------------------------------------
57 ; Define module specific macros here
58 ----------------------------------------------------------------------------*/
60 /*----------------------------------------------------------------------------
62 ; Include all pre-processor statements here. Include conditional
63 ; compile variables also.
64 ----------------------------------------------------------------------------*/
66 /*----------------------------------------------------------------------------
67 ; LOCAL FUNCTION DEFINITIONS
68 ; Function Prototype declaration
69 ----------------------------------------------------------------------------*/
71 /*----------------------------------------------------------------------------
72 ; LOCAL VARIABLE DEFINITIONS
73 ; Variable declaration - defined here and used outside this module
74 ----------------------------------------------------------------------------*/
76 /*----------------------------------------------------------------------------
77 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
78 ; Declare variables used in this module but defined elsewhere
79 ----------------------------------------------------------------------------*/
80 /* Codebooks of LSF prediction residual */
81 extern const Word16 mean_lsf_5[];
83 extern const Word16 dico1_lsf_5[];
84 extern const Word16 dico2_lsf_5[];
85 extern const Word16 dico3_lsf_5[];
86 extern const Word16 dico4_lsf_5[];
87 extern const Word16 dico5_lsf_5[];
89 /*--------------------------------------------------------------------------*/
95 ------------------------------------------------------------------------------
96 FUNCTION NAME: Vq_subvec
97 ------------------------------------------------------------------------------
98 INPUT AND OUTPUT DEFINITIONS
101 lsf_r1 -- array of type Word16 -- 1st LSF residual vector, Q15
102 lsf_r2 -- array of type Word16 -- 2nd LSF residual vector, Q15
103 dico -- pointer to const Word16 -- quantization codebook, Q15
104 wf1 -- array of type Word16 -- 1st LSF weighting factors, Q13
105 wf2 -- array of type Word16 -- 2nd LSF weighting factors, Q13
106 dico_size -- Word16 -- size of quantization codebook, Q0
108 pOverflow -- pointer to type Flag -- overflow indicator
111 Word16 -- quantization index, Q0
113 Global Variables Used:
116 Local Variables Needed:
119 ------------------------------------------------------------------------------
122 This function performs the quantization of a 4-dimensional subvector.
124 ------------------------------------------------------------------------------
129 ------------------------------------------------------------------------------
132 q_plsf_5.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
134 ------------------------------------------------------------------------------
138 ------------------------------------------------------------------------------
140 [State any special notes, constraints or cautions for users of this function]
142 ------------------------------------------------------------------------------
144 /* Quantization of a 4 dimensional subvector */
146 static Word16 Vq_subvec( /* o : quantization index, Q0 */
147 Word16 *lsf_r1, /* i : 1st LSF residual vector, Q15 */
148 Word16 *lsf_r2, /* i : 2nd LSF residual vector, Q15 */
149 const Word16 *dico, /* i : quantization codebook, Q15 */
150 Word16 *wf1, /* i : 1st LSF weighting factors Q13 */
151 Word16 *wf2, /* i : 2nd LSF weighting factors Q13 */
152 Word16 dico_size, /* i : size of quantization codebook, Q0 */
153 Flag *pOverflow /* o : overflow indicator */
156 Word16 index = 0; /* initialization only needed to keep gcc silent */
159 const Word16 *p_dico;
171 OSCL_UNUSED_ARG(pOverflow);
181 aux1 = ((Word32) lsf_r1[0] * wf1_0);
182 aux2 = ((Word32) lsf_r1[1] * wf1_1);
183 aux3 = ((Word32) lsf_r2[0] * wf2_0);
184 aux4 = ((Word32) lsf_r2[1] * wf2_1);
186 for (i = 0; i < dico_size; i++)
188 temp = (aux1 - ((Word32)wf1_0 * *(p_dico++))) >> 15;
189 dist = ((Word32)temp * temp);
191 if (dist >= dist_min)
197 temp = (aux2 - ((Word32)wf1_1 * *(p_dico++))) >> 15;
198 dist += ((Word32)temp * temp);
200 if (dist >= dist_min)
206 temp = (aux3 - ((Word32)wf2_0 * *(p_dico++))) >> 15;
207 dist += ((Word32)temp * temp);
209 if (dist >= dist_min)
215 temp = (aux4 - ((Word32)wf2_1 * *(p_dico++))) >> 15;
216 dist += ((Word32)temp * temp);
228 /* Reading the selected vector */
230 p_dico = &dico[ index<<2];
231 lsf_r1[0] = *p_dico++;
232 lsf_r1[1] = *p_dico++;
233 lsf_r2[0] = *p_dico++;
242 ------------------------------------------------------------------------------
243 FUNCTION NAME: Vq_subvec_s
244 ------------------------------------------------------------------------------
245 INPUT AND OUTPUT DEFINITIONS
248 lsf_r1 -- array of type Word16 -- 1st LSF residual vector, Q15
249 lsf_r2 -- array of type Word16 -- 2nd LSF residual vector, Q15
250 dico -- pointer to const Word16 -- quantization codebook, Q15
251 wf1 -- array of type Word16 -- 1st LSF weighting factors, Q13
252 wf2 -- array of type Word16 -- 2nd LSF weighting factors, Q13
253 dico_size -- Word16 -- size of quantization codebook, Q0
256 pOverflow -- pointer to type Flag -- overflow indicator
259 Word16 -- quantization index, Q0
261 Global Variables Used:
264 Local Variables Needed:
267 ------------------------------------------------------------------------------
270 This function performs the quantization of a 4-dimensional subvector.
272 ------------------------------------------------------------------------------
277 ------------------------------------------------------------------------------
280 q_plsf_5.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
282 ------------------------------------------------------------------------------
286 ------------------------------------------------------------------------------
288 [State any special notes, constraints or cautions for users of this function]
290 ------------------------------------------------------------------------------
294 /* Quantization of a 4 dimensional subvector with a signed codebook */
296 static Word16 Vq_subvec_s( /* o : quantization index Q0 */
297 Word16 *lsf_r1, /* i : 1st LSF residual vector Q15 */
298 Word16 *lsf_r2, /* i : and LSF residual vector Q15 */
299 const Word16 *dico, /* i : quantization codebook Q15 */
300 Word16 *wf1, /* i : 1st LSF weighting factors Q13 */
301 Word16 *wf2, /* i : 2nd LSF weighting factors Q13 */
302 Word16 dico_size, /* i : size of quantization codebook Q0 */
303 Flag *pOverflow) /* o : overflow indicator */
305 Word16 index = 0; /* initialization only needed to keep gcc silent */
306 Word16 sign = 0; /* initialization only needed to keep gcc silent */
311 const Word16 *p_dico;
326 OSCL_UNUSED_ARG(pOverflow);
332 lsf_r1_0 = lsf_r1[0];
333 lsf_r1_1 = lsf_r1[1];
334 lsf_r2_0 = lsf_r2[0];
335 lsf_r2_1 = lsf_r2[1];
342 for (i = 0; i < dico_size; i++)
346 temp1 = lsf_r1_0 - temp;
347 temp2 = lsf_r1_0 + temp;
348 temp1 = ((Word32)wf1_0 * temp1) >> 15;
349 temp2 = ((Word32)wf1_0 * temp2) >> 15;
350 dist1 = ((Word32)temp1 * temp1);
351 dist2 = ((Word32)temp2 * temp2);
354 temp1 = lsf_r1_1 - temp;
355 temp2 = lsf_r1_1 + temp;
356 temp1 = ((Word32)wf1_1 * temp1) >> 15;
357 temp2 = ((Word32)wf1_1 * temp2) >> 15;
358 dist1 += ((Word32)temp1 * temp1);
359 dist2 += ((Word32)temp2 * temp2);
361 if ((dist1 >= dist_min) && (dist2 >= dist_min))
368 temp1 = lsf_r2_0 - temp;
369 temp2 = lsf_r2_0 + temp;
370 temp1 = ((Word32)wf2_0 * temp1) >> 15;
371 temp2 = ((Word32)wf2_0 * temp2) >> 15;
372 dist1 += ((Word32)temp1 * temp1);
373 dist2 += ((Word32)temp2 * temp2);
376 temp1 = lsf_r2_1 - temp;
377 temp2 = lsf_r2_1 + temp;
378 temp1 = ((Word32)wf2_1 * temp1) >> 15;
379 temp2 = ((Word32)wf2_1 * temp2) >> 15;
380 dist1 += ((Word32)temp1 * temp1);
381 dist2 += ((Word32)temp2 * temp2);
383 if (dist1 < dist_min)
392 if (dist2 < dist_min)
400 /* Reading the selected vector */
402 p_dico = &dico[index<<2];
406 lsf_r1[0] = - (*p_dico++);
407 lsf_r1[1] = - (*p_dico++);
408 lsf_r2[0] = - (*p_dico++);
409 lsf_r2[1] = - (*p_dico);
414 lsf_r1[0] = *p_dico++;
415 lsf_r1[1] = *p_dico++;
416 lsf_r2[0] = *p_dico++;
425 ------------------------------------------------------------------------------
426 FUNCTION NAME: Q_plsf_5
427 ------------------------------------------------------------------------------
428 INPUT AND OUTPUT DEFINITIONS
432 st -- pointer to Q_plsfState -- state information
433 lsp1 -- array of type Word16 -- 1st LSP vector, Q15
434 lsp2 -- array of type Word16 -- 2nd LSP vector, Q15
437 lps1_q -- array of type Word16 -- quantized 1st LSP vector, Q15
438 lps2_q -- array of type Word16 -- quantized 2nd LSP vector, Q15
439 indices -- array of type Word16 -- quantization indices of 5 matrics, Q0
440 pOverflow -- pointer to type Flag -- overflow indicator
445 Global Variables Used:
454 Local Variables Needed:
457 ------------------------------------------------------------------------------
460 PURPOSE: Quantization of 2 sets of LSF parameters using 1st order MA
461 prediction and split by 5 matrix quantization (split-MQ)
465 p[i] = pred_factor*past_rq[i]; i=0,...,m-1
466 r1[i]= lsf1[i] - p[i]; i=0,...,m-1
467 r2[i]= lsf2[i] - p[i]; i=0,...,m-1
469 lsf1[i] 1st mean-removed LSF vector.
470 lsf2[i] 2nd mean-removed LSF vector.
471 r1[i] 1st residual prediction vector.
472 r2[i] 2nd residual prediction vector.
473 past_r2q[i] Past quantized residual (2nd vector).
475 The residual vectors r1[i] and r2[i] are jointly quantized using
476 split-MQ with 5 codebooks. Each 4th dimension submatrix contains 2
477 elements from each residual vector. The 5 submatrices are as follows:
478 {r1[0], r1[1], r2[0], r2[1]}; {r1[2], r1[3], r2[2], r2[3]};
479 {r1[4], r1[5], r2[4], r2[5]}; {r1[6], r1[7], r2[6], r2[7]};
480 {r1[8], r1[9], r2[8], r2[9]}
482 ------------------------------------------------------------------------------
487 ------------------------------------------------------------------------------
490 q_plsf_5.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
492 ------------------------------------------------------------------------------
496 ------------------------------------------------------------------------------
498 [State any special notes, constraints or cautions for users of this function]
500 ------------------------------------------------------------------------------
503 OSCL_EXPORT_REF void Q_plsf_5(
505 Word16 *lsp1, /* i : 1st LSP vector, Q15 */
506 Word16 *lsp2, /* i : 2nd LSP vector, Q15 */
507 Word16 *lsp1_q, /* o : quantized 1st LSP vector, Q15 */
508 Word16 *lsp2_q, /* o : quantized 2nd LSP vector, Q15 */
509 Word16 *indice, /* o : quantization indices of 5 matrices, Q0 */
510 Flag *pOverflow /* o : overflow indicator */
530 /* convert LSFs to normalize frequency domain 0..16384 */
532 Lsp_lsf(lsp1, lsf1, M, pOverflow);
533 Lsp_lsf(lsp2, lsf2, M, pOverflow);
535 /* Compute LSF weighting factors (Q13) */
537 Lsf_wt(lsf1, wf1, pOverflow);
538 Lsf_wt(lsf2, wf2, pOverflow);
540 /* Compute predicted LSF and prediction error */
545 p_lsf_r1 = &lsf_r1[0];
546 p_lsf_r2 = &lsf_r2[0];
548 for (i = 0; i < M; i++)
550 *(p_lsf_p) = mean_lsf_5[i] +
551 (((Word32)st->past_rq[i] * LSP_PRED_FAC_MR122) >> 15);
553 *(p_lsf_r1++) = *(p_lsf1++) - *(p_lsf_p);
554 *(p_lsf_r2++) = *(p_lsf2++) - *(p_lsf_p++);
557 /*---- Split-MQ of prediction error ----*/
559 indice[0] = Vq_subvec(&lsf_r1[0], &lsf_r2[0], dico1_lsf_5,
560 &wf1[0], &wf2[0], DICO1_5_SIZE, pOverflow);
562 indice[1] = Vq_subvec(&lsf_r1[2], &lsf_r2[2], dico2_lsf_5,
563 &wf1[2], &wf2[2], DICO2_5_SIZE, pOverflow);
565 indice[2] = Vq_subvec_s(&lsf_r1[4], &lsf_r2[4], dico3_lsf_5,
566 &wf1[4], &wf2[4], DICO3_5_SIZE, pOverflow);
568 indice[3] = Vq_subvec(&lsf_r1[6], &lsf_r2[6], dico4_lsf_5,
569 &wf1[6], &wf2[6], DICO4_5_SIZE, pOverflow);
571 indice[4] = Vq_subvec(&lsf_r1[8], &lsf_r2[8], dico5_lsf_5,
572 &wf1[8], &wf2[8], DICO5_5_SIZE, pOverflow);
574 /* Compute quantized LSFs and update the past quantized residual */
576 p_lsf_r1 = &lsf_r1[0];
577 p_lsf_r2 = &lsf_r2[0];
583 for (i = 0; i < M; i++)
585 *(p_lsf1++) = *(p_lsf_r1++) + *(p_lsf_p);
586 *(p_lsf2++) = *(p_lsf_r2) + *(p_lsf_p++);
587 st->past_rq[i] = *(p_lsf_r2++);
590 /* verification that LSFs has minimum distance of LSF_GAP */
592 Reorder_lsf(lsf1_q, LSF_GAP, M, pOverflow);
593 Reorder_lsf(lsf2_q, LSF_GAP, M, pOverflow);
595 /* convert LSFs to the cosine domain */
597 Lsf_lsp(lsf1_q, lsp1_q, M, pOverflow);
598 Lsf_lsp(lsf2_q, lsp2_q, M, pOverflow);