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 - Wideband (AMR-WB) speech codec
23 Available from http://www.3gpp.org
25 (C) 2007, 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: qpisf_2s.cpp
36 ------------------------------------------------------------------------------
37 INPUT AND OUTPUT DEFINITIONS
39 int16 * seed seed for the random ng
41 ------------------------------------------------------------------------------
44 Coding/Decoding of ISF parameters with prediction.
46 The ISF vector is quantized using two-stage VQ with split-by-2
47 in 1st stage and split-by-5 (or 3)in the second stage.
49 ------------------------------------------------------------------------------
53 ------------------------------------------------------------------------------
56 ------------------------------------------------------------------------------
59 ------------------------------------------------------------------------------
63 /*----------------------------------------------------------------------------
65 ----------------------------------------------------------------------------*/
67 #include "pv_amr_wb_type_defs.h"
68 #include "pvamrwbdecoder_basic_op.h"
69 #include "pvamrwbdecoder_cnst.h"
70 #include "pvamrwbdecoder_acelp.h"
75 /*----------------------------------------------------------------------------
77 ; Define module specific macros here
78 ----------------------------------------------------------------------------*/
81 /*----------------------------------------------------------------------------
83 ; Include all pre-processor statements here. Include conditional
84 ; compile variables also.
85 ----------------------------------------------------------------------------*/
86 #define MU 10923 /* Prediction factor (1.0/3.0) in Q15 */
87 #define N_SURV_MAX 4 /* 4 survivors max */
88 #define ALPHA 29491 /* 0. 9 in Q15 */
89 #define ONE_ALPHA (32768-ALPHA) /* (1.0 - ALPHA) in Q15 */
91 /*----------------------------------------------------------------------------
92 ; LOCAL FUNCTION DEFINITIONS
93 ; Function Prototype declaration
94 ----------------------------------------------------------------------------*/
96 /*----------------------------------------------------------------------------
97 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
98 ; Variable declaration - defined here and used outside this module
99 ----------------------------------------------------------------------------*/
101 /*----------------------------------------------------------------------------
102 ; EXTERNAL FUNCTION REFERENCES
103 ; Declare functions defined elsewhere and referenced in this module
104 ----------------------------------------------------------------------------*/
106 /*----------------------------------------------------------------------------
107 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
108 ; Declare variables used in this module but defined elsewhere
109 ----------------------------------------------------------------------------*/
111 /*----------------------------------------------------------------------------
113 ----------------------------------------------------------------------------*/
116 /*-------------------------------------------------------------------*
117 * routine: Disf_2s_46b() *
119 * Decoding of ISF parameters *
120 *-------------------------------------------------------------------*/
123 int16 * indice, /* input: quantization indices */
124 int16 * isf_q, /* output: quantized ISF in frequency domain (0..0.5) */
125 int16 * past_isfq,/* i/0 : past ISF quantizer */
126 int16 * isfold, /* input : past quantized ISF */
127 int16 * isf_buf, /* input : isf buffer */
128 int16 bfi, /* input : Bad frame indicator */
137 if (bfi == 0) /* Good frame */
139 for (i = 0; i < 9; i++)
141 isf_q[i] = dico1_isf[(indice[0] << 3) + indice[0] + i];
143 for (i = 0; i < 7; i++)
145 isf_q[i + 9] = dico2_isf[(indice[1] << 3) - indice[1] + i];
148 for (i = 0; i < 3; i++)
150 isf_q[i] += dico21_isf[indice[2] * 3 + i];
151 isf_q[i + 3] += dico22_isf[indice[3] * 3 + i];
152 isf_q[i + 6] += dico23_isf[indice[4] * 3 + i];
153 isf_q[i + 9] += dico24_isf[indice[5] * 3 + i];
154 isf_q[i + 12] += dico25_isf[(indice[6] << 2) + i];
157 isf_q[i + 12] += dico25_isf[(indice[6] << 2) + i];
159 for (i = 0; i < ORDER; i++)
162 isf_q[i] += mean_isf[i];
163 isf_q[i] += ((int32)MU * past_isfq[i]) >> 15;
170 for (i = 0; i < M; i++)
172 for (j = (L_MEANBUF - 1); j > 0; j--)
174 isf_buf[j * M + i] = isf_buf[(j - 1) * M + i];
176 isf_buf[i] = isf_q[i];
182 for (i = 0; i < M; i++)
184 L_tmp = mul_16by16_to_int32(mean_isf[i], 8192);
185 for (j = 0; j < L_MEANBUF; j++)
187 L_tmp = mac_16by16_to_int32(L_tmp, isf_buf[j * M + i], 8192);
189 ref_isf[i] = amr_wb_round(L_tmp);
192 /* use the past ISFs slightly shifted towards their mean */
193 for (i = 0; i < ORDER; i++)
195 isf_q[i] = add_int16(mult_int16(ALPHA, isfold[i]), mult_int16(ONE_ALPHA, ref_isf[i]));
198 /* estimate past quantized residual to be used in next frame */
200 for (i = 0; i < ORDER; i++)
202 tmp = add_int16(ref_isf[i], mult_int16(past_isfq[i], MU)); /* predicted ISF */
203 past_isfq[i] = sub_int16(isf_q[i], tmp);
204 past_isfq[i] >>= 1; /* past_isfq[i] *= 0.5 */
209 Reorder_isf(isf_q, ISF_GAP, ORDER);
213 * routine: Disf_2s_36b()
215 * Decoding of ISF parameters
219 int16 * indice, /* input: quantization indices */
220 int16 * isf_q, /* output: quantized ISF in frequency domain (0..0.5) */
221 int16 * past_isfq, /* i/0 : past ISF quantizer */
222 int16 * isfold, /* input : past quantized ISF */
223 int16 * isf_buf, /* input : isf buffer */
224 int16 bfi, /* input : Bad frame indicator */
233 if (bfi == 0) /* Good frame */
235 for (i = 0; i < 9; i++)
237 isf_q[i] = dico1_isf[indice[0] * 9 + i];
239 for (i = 0; i < 7; i++)
241 isf_q[i + 9] = add_int16(dico2_isf[indice[1] * 7 + i], dico23_isf_36b[indice[4] * 7 + i]);
244 for (i = 0; i < 5; i++)
246 isf_q[i] = add_int16(isf_q[i], dico21_isf_36b[indice[2] * 5 + i]);
248 for (i = 0; i < 4; i++)
250 isf_q[i + 5] = add_int16(isf_q[i + 5], dico22_isf_36b[(indice[3] << 2) + i]);
253 for (i = 0; i < ORDER; i++)
256 isf_q[i] = add_int16(tmp, mean_isf[i]);
257 isf_q[i] = add_int16(isf_q[i], mult_int16(MU, past_isfq[i]));
264 for (i = 0; i < M; i++)
266 for (j = (L_MEANBUF - 1); j > 0; j--)
268 isf_buf[j * M + i] = isf_buf[(j - 1) * M + i];
270 isf_buf[i] = isf_q[i];
276 for (i = 0; i < M; i++)
278 L_tmp = mul_16by16_to_int32(mean_isf[i], 8192);
279 for (j = 0; j < L_MEANBUF; j++)
281 L_tmp = mac_16by16_to_int32(L_tmp, isf_buf[j * M + i], 8192);
284 ref_isf[i] = amr_wb_round(L_tmp);
287 /* use the past ISFs slightly shifted towards their mean */
288 for (i = 0; i < ORDER; i++)
290 isf_q[i] = add_int16(mult_int16(ALPHA, isfold[i]), mult_int16(ONE_ALPHA, ref_isf[i]));
293 /* estimate past quantized residual to be used in next frame */
295 for (i = 0; i < ORDER; i++)
297 tmp = add_int16(ref_isf[i], mult_int16(past_isfq[i], MU)); /* predicted ISF */
298 past_isfq[i] = sub_int16(isf_q[i], tmp);
299 past_isfq[i] >>= 1; /* past_isfq[i] *= 0.5 */
303 Reorder_isf(isf_q, ISF_GAP, ORDER);
309 * procedure Reorder_isf()
311 * To make sure that the isfs are properly order and to keep a certain
312 * minimum distance between consecutive isfs.
314 * Argument description in/out
315 * ~~~~~~~~ ~~~~~~~~~~~ ~~~~~~
316 * isf[] vector of isfs i/o
317 * min_dist minimum required distance i
322 int16 * isf, /* (i/o) Q15: ISF in the frequency domain (0..0.5) */
323 int16 min_dist, /* (i) Q15 : minimum distance to keep */
324 int16 n /* (i) : number of ISF */
331 for (i = 0; i < n - 1; i++)
333 if (isf[i] < isf_min)
337 isf_min = add_int16(isf[i], min_dist);