Initialize Tizen 2.3
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_wb / dec / src / qpisf_2s.cpp
1 /* ------------------------------------------------------------------
2  * Copyright (C) 1998-2009 PacketVideo
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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
13  * express or implied.
14  * See the License for the specific language governing permissions
15  * and limitations under the License.
16  * -------------------------------------------------------------------
17  */
18 /****************************************************************************************
19 Portions of this file are derived from the following 3GPP standard:
20
21     3GPP TS 26.173
22     ANSI-C code for the Adaptive Multi-Rate - Wideband (AMR-WB) speech codec
23     Available from http://www.3gpp.org
24
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 ****************************************************************************************/
29 /*
30 ------------------------------------------------------------------------------
31
32
33
34  Filename: qpisf_2s.cpp
35
36 ------------------------------------------------------------------------------
37  INPUT AND OUTPUT DEFINITIONS
38
39     int16 * seed          seed for the random ng
40
41 ------------------------------------------------------------------------------
42  FUNCTION DESCRIPTION
43
44    Coding/Decoding of ISF parameters  with prediction.
45
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.
48
49 ------------------------------------------------------------------------------
50  REQUIREMENTS
51
52
53 ------------------------------------------------------------------------------
54  REFERENCES
55
56 ------------------------------------------------------------------------------
57  PSEUDO-CODE
58
59 ------------------------------------------------------------------------------
60 */
61
62
63 /*----------------------------------------------------------------------------
64 ; INCLUDES
65 ----------------------------------------------------------------------------*/
66
67 #include "pv_amr_wb_type_defs.h"
68 #include "pvamrwbdecoder_basic_op.h"
69 #include "pvamrwbdecoder_cnst.h"
70 #include "pvamrwbdecoder_acelp.h"
71
72 #include "qisf_ns.h"
73 #include "qpisf_2s.h"
74
75 /*----------------------------------------------------------------------------
76 ; MACROS
77 ; Define module specific macros here
78 ----------------------------------------------------------------------------*/
79
80
81 /*----------------------------------------------------------------------------
82 ; DEFINES
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 */
90
91 /*----------------------------------------------------------------------------
92 ; LOCAL FUNCTION DEFINITIONS
93 ; Function Prototype declaration
94 ----------------------------------------------------------------------------*/
95
96 /*----------------------------------------------------------------------------
97 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
98 ; Variable declaration - defined here and used outside this module
99 ----------------------------------------------------------------------------*/
100
101 /*----------------------------------------------------------------------------
102 ; EXTERNAL FUNCTION REFERENCES
103 ; Declare functions defined elsewhere and referenced in this module
104 ----------------------------------------------------------------------------*/
105
106 /*----------------------------------------------------------------------------
107 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
108 ; Declare variables used in this module but defined elsewhere
109 ----------------------------------------------------------------------------*/
110
111 /*----------------------------------------------------------------------------
112 ; FUNCTION CODE
113 ----------------------------------------------------------------------------*/
114
115
116 /*-------------------------------------------------------------------*
117  * routine:   Disf_2s_46b()                                          *
118  *            ~~~~~~~~~                                              *
119  * Decoding of ISF parameters                                        *
120  *-------------------------------------------------------------------*/
121
122 void Dpisf_2s_46b(
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                   */
129     int16 enc_dec
130 )
131 {
132     int16 ref_isf[M];
133     int16 i, j, tmp;
134     int32 L_tmp;
135
136
137     if (bfi == 0)                          /* Good frame */
138     {
139         for (i = 0; i < 9; i++)
140         {
141             isf_q[i] = dico1_isf[(indice[0] << 3) + indice[0] + i];
142         }
143         for (i = 0; i < 7; i++)
144         {
145             isf_q[i + 9] = dico2_isf[(indice[1] << 3) - indice[1] + i];
146         }
147
148         for (i = 0; i < 3; i++)
149         {
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];
155         }
156
157         isf_q[i + 12] += dico25_isf[(indice[6] << 2) + i];
158
159         for (i = 0; i < ORDER; i++)
160         {
161             tmp = isf_q[i];
162             isf_q[i] += mean_isf[i];
163             isf_q[i] += ((int32)MU * past_isfq[i]) >> 15;
164             past_isfq[i] = tmp;
165         }
166
167
168         if (enc_dec)
169         {
170             for (i = 0; i < M; i++)
171             {
172                 for (j = (L_MEANBUF - 1); j > 0; j--)
173                 {
174                     isf_buf[j * M + i] = isf_buf[(j - 1) * M + i];
175                 }
176                 isf_buf[i] = isf_q[i];
177             }
178         }
179     }
180     else
181     {                                      /* bad frame */
182         for (i = 0; i < M; i++)
183         {
184             L_tmp = mul_16by16_to_int32(mean_isf[i], 8192);
185             for (j = 0; j < L_MEANBUF; j++)
186             {
187                 L_tmp = mac_16by16_to_int32(L_tmp, isf_buf[j * M + i], 8192);
188             }
189             ref_isf[i] = amr_wb_round(L_tmp);
190         }
191
192         /* use the past ISFs slightly shifted towards their mean */
193         for (i = 0; i < ORDER; i++)
194         {
195             isf_q[i] = add_int16(mult_int16(ALPHA, isfold[i]), mult_int16(ONE_ALPHA, ref_isf[i]));
196         }
197
198         /* estimate past quantized residual to be used in next frame */
199
200         for (i = 0; i < ORDER; i++)
201         {
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 */
205         }
206
207     }
208
209     Reorder_isf(isf_q, ISF_GAP, ORDER);
210 }
211
212 /*
213  * routine:   Disf_2s_36b()
214  *            ~~~~~~~~~
215  * Decoding of ISF parameters
216  */
217
218 void Dpisf_2s_36b(
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                   */
225     int16 enc_dec
226 )
227 {
228     int16 ref_isf[M];
229     int16 i, j, tmp;
230     int32 L_tmp;
231
232
233     if (bfi == 0)                          /* Good frame */
234     {
235         for (i = 0; i < 9; i++)
236         {
237             isf_q[i] = dico1_isf[indice[0] * 9 + i];
238         }
239         for (i = 0; i < 7; i++)
240         {
241             isf_q[i + 9] = add_int16(dico2_isf[indice[1] * 7 + i], dico23_isf_36b[indice[4] * 7 + i]);
242         }
243
244         for (i = 0; i < 5; i++)
245         {
246             isf_q[i] = add_int16(isf_q[i], dico21_isf_36b[indice[2] * 5 + i]);
247         }
248         for (i = 0; i < 4; i++)
249         {
250             isf_q[i + 5] = add_int16(isf_q[i + 5], dico22_isf_36b[(indice[3] << 2) + i]);
251         }
252
253         for (i = 0; i < ORDER; i++)
254         {
255             tmp = isf_q[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]));
258             past_isfq[i] = tmp;
259         }
260
261
262         if (enc_dec)
263         {
264             for (i = 0; i < M; i++)
265             {
266                 for (j = (L_MEANBUF - 1); j > 0; j--)
267                 {
268                     isf_buf[j * M + i] = isf_buf[(j - 1) * M + i];
269                 }
270                 isf_buf[i] = isf_q[i];
271             }
272         }
273     }
274     else
275     {                                      /* bad frame */
276         for (i = 0; i < M; i++)
277         {
278             L_tmp = mul_16by16_to_int32(mean_isf[i], 8192);
279             for (j = 0; j < L_MEANBUF; j++)
280             {
281                 L_tmp = mac_16by16_to_int32(L_tmp, isf_buf[j * M + i], 8192);
282             }
283
284             ref_isf[i] = amr_wb_round(L_tmp);
285         }
286
287         /* use the past ISFs slightly shifted towards their mean */
288         for (i = 0; i < ORDER; i++)
289         {
290             isf_q[i] = add_int16(mult_int16(ALPHA, isfold[i]), mult_int16(ONE_ALPHA, ref_isf[i]));
291         }
292
293         /* estimate past quantized residual to be used in next frame */
294
295         for (i = 0; i < ORDER; i++)
296         {
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 */
300         }
301     }
302
303     Reorder_isf(isf_q, ISF_GAP, ORDER);
304
305     return;
306 }
307
308 /*
309  * procedure  Reorder_isf()
310  *            ~~~~~~~~~~~~~
311  * To make sure that the  isfs are properly order and to keep a certain
312  * minimum distance between consecutive isfs.
313  *
314  *    Argument         description                     in/out
315  *    ~~~~~~~~         ~~~~~~~~~~~                     ~~~~~~
316  *     isf[]           vector of isfs                    i/o
317  *     min_dist        minimum required distance         i
318  *     n               LPC order                         i
319  */
320
321 void Reorder_isf(
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                        */
325 )
326 {
327     int16 i, isf_min;
328
329     isf_min = min_dist;
330
331     for (i = 0; i < n - 1; i++)
332     {
333         if (isf[i] < isf_min)
334         {
335             isf[i] = isf_min;
336         }
337         isf_min = add_int16(isf[i], min_dist);
338     }
339
340     return;
341 }
342