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/****************************************************************************************
19Portions 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)
26Permission to distribute, modify and use this file under the standard license
27terms listed above has been obtained from the copyright holder.
28****************************************************************************************/
29/*
30------------------------------------------------------------------------------
31
32
33
34 Filename: qpisf_2s.cpp
35
36     Date: 05/08/2007
37
38------------------------------------------------------------------------------
39 REVISION HISTORY
40
41
42 Description:
43
44------------------------------------------------------------------------------
45 INPUT AND OUTPUT DEFINITIONS
46
47    int16 * seed          seed for the random ng
48
49------------------------------------------------------------------------------
50 FUNCTION DESCRIPTION
51
52   Coding/Decoding of ISF parameters  with prediction.
53
54   The ISF vector is quantized using two-stage VQ with split-by-2
55   in 1st stage and split-by-5 (or 3)in the second stage.
56
57------------------------------------------------------------------------------
58 REQUIREMENTS
59
60
61------------------------------------------------------------------------------
62 REFERENCES
63
64------------------------------------------------------------------------------
65 PSEUDO-CODE
66
67------------------------------------------------------------------------------
68*/
69
70
71/*----------------------------------------------------------------------------
72; INCLUDES
73----------------------------------------------------------------------------*/
74
75#include "pv_amr_wb_type_defs.h"
76#include "pvamrwbdecoder_basic_op.h"
77#include "pvamrwbdecoder_cnst.h"
78#include "pvamrwbdecoder_acelp.h"
79
80#include "qisf_ns.h"
81#include "qpisf_2s.h"
82
83/*----------------------------------------------------------------------------
84; MACROS
85; Define module specific macros here
86----------------------------------------------------------------------------*/
87
88
89/*----------------------------------------------------------------------------
90; DEFINES
91; Include all pre-processor statements here. Include conditional
92; compile variables also.
93----------------------------------------------------------------------------*/
94#define MU         10923           /* Prediction factor   (1.0/3.0) in Q15 */
95#define N_SURV_MAX 4               /* 4 survivors max */
96#define ALPHA      29491           /* 0. 9 in Q15     */
97#define ONE_ALPHA (32768-ALPHA)    /* (1.0 - ALPHA) in Q15 */
98
99/*----------------------------------------------------------------------------
100; LOCAL FUNCTION DEFINITIONS
101; Function Prototype declaration
102----------------------------------------------------------------------------*/
103
104/*----------------------------------------------------------------------------
105; LOCAL STORE/BUFFER/POINTER DEFINITIONS
106; Variable declaration - defined here and used outside this module
107----------------------------------------------------------------------------*/
108
109/*----------------------------------------------------------------------------
110; EXTERNAL FUNCTION REFERENCES
111; Declare functions defined elsewhere and referenced in this module
112----------------------------------------------------------------------------*/
113
114/*----------------------------------------------------------------------------
115; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
116; Declare variables used in this module but defined elsewhere
117----------------------------------------------------------------------------*/
118
119/*----------------------------------------------------------------------------
120; FUNCTION CODE
121----------------------------------------------------------------------------*/
122
123
124/*-------------------------------------------------------------------*
125 * routine:   Disf_2s_46b()                                          *
126 *            ~~~~~~~~~                                              *
127 * Decoding of ISF parameters                                        *
128 *-------------------------------------------------------------------*/
129
130void Dpisf_2s_46b(
131    int16 * indice,   /* input:  quantization indices                       */
132    int16 * isf_q,    /* output: quantized ISF in frequency domain (0..0.5) */
133    int16 * past_isfq,/* i/0   : past ISF quantizer                    */
134    int16 * isfold,   /* input : past quantized ISF                    */
135    int16 * isf_buf,  /* input : isf buffer                            */
136    int16 bfi,        /* input : Bad frame indicator                   */
137    int16 enc_dec
138)
139{
140    int16 ref_isf[M];
141    int16 i, j, tmp;
142    int32 L_tmp;
143
144
145    if (bfi == 0)                          /* Good frame */
146    {
147        for (i = 0; i < 9; i++)
148        {
149            isf_q[i] = dico1_isf[(indice[0] << 3) + indice[0] + i];
150        }
151        for (i = 0; i < 7; i++)
152        {
153            isf_q[i + 9] = dico2_isf[(indice[1] << 3) - indice[1] + i];
154        }
155
156        for (i = 0; i < 3; i++)
157        {
158            isf_q[i]      += dico21_isf[indice[2] * 3 + i];
159            isf_q[i + 3]  += dico22_isf[indice[3] * 3 + i];
160            isf_q[i + 6]  += dico23_isf[indice[4] * 3 + i];
161            isf_q[i + 9]  += dico24_isf[indice[5] * 3 + i];
162            isf_q[i + 12] += dico25_isf[(indice[6] << 2) + i];
163        }
164
165        isf_q[i + 12] += dico25_isf[(indice[6] << 2) + i];
166
167        for (i = 0; i < ORDER; i++)
168        {
169            tmp = isf_q[i];
170            isf_q[i] += mean_isf[i];
171            isf_q[i] += ((int32)MU * past_isfq[i]) >> 15;
172            past_isfq[i] = tmp;
173        }
174
175
176        if (enc_dec)
177        {
178            for (i = 0; i < M; i++)
179            {
180                for (j = (L_MEANBUF - 1); j > 0; j--)
181                {
182                    isf_buf[j * M + i] = isf_buf[(j - 1) * M + i];
183                }
184                isf_buf[i] = isf_q[i];
185            }
186        }
187    }
188    else
189    {                                      /* bad frame */
190        for (i = 0; i < M; i++)
191        {
192            L_tmp = mul_16by16_to_int32(mean_isf[i], 8192);
193            for (j = 0; j < L_MEANBUF; j++)
194            {
195                L_tmp = mac_16by16_to_int32(L_tmp, isf_buf[j * M + i], 8192);
196            }
197            ref_isf[i] = amr_wb_round(L_tmp);
198        }
199
200        /* use the past ISFs slightly shifted towards their mean */
201        for (i = 0; i < ORDER; i++)
202        {
203            isf_q[i] = add_int16(mult_int16(ALPHA, isfold[i]), mult_int16(ONE_ALPHA, ref_isf[i]));
204        }
205
206        /* estimate past quantized residual to be used in next frame */
207
208        for (i = 0; i < ORDER; i++)
209        {
210            tmp = add_int16(ref_isf[i], mult_int16(past_isfq[i], MU));      /* predicted ISF */
211            past_isfq[i] = sub_int16(isf_q[i], tmp);
212            past_isfq[i] >>= 1;           /* past_isfq[i] *= 0.5 */
213        }
214
215    }
216
217    Reorder_isf(isf_q, ISF_GAP, ORDER);
218}
219
220/*
221 * routine:   Disf_2s_36b()
222 *            ~~~~~~~~~
223 * Decoding of ISF parameters
224 */
225
226void Dpisf_2s_36b(
227    int16 * indice,    /* input:  quantization indices                       */
228    int16 * isf_q,     /* output: quantized ISF in frequency domain (0..0.5) */
229    int16 * past_isfq, /* i/0   : past ISF quantizer                    */
230    int16 * isfold,    /* input : past quantized ISF                    */
231    int16 * isf_buf,   /* input : isf buffer                            */
232    int16 bfi,         /* input : Bad frame indicator                   */
233    int16 enc_dec
234)
235{
236    int16 ref_isf[M];
237    int16 i, j, tmp;
238    int32 L_tmp;
239
240
241    if (bfi == 0)                          /* Good frame */
242    {
243        for (i = 0; i < 9; i++)
244        {
245            isf_q[i] = dico1_isf[indice[0] * 9 + i];
246        }
247        for (i = 0; i < 7; i++)
248        {
249            isf_q[i + 9] = add_int16(dico2_isf[indice[1] * 7 + i], dico23_isf_36b[indice[4] * 7 + i]);
250        }
251
252        for (i = 0; i < 5; i++)
253        {
254            isf_q[i] = add_int16(isf_q[i], dico21_isf_36b[indice[2] * 5 + i]);
255        }
256        for (i = 0; i < 4; i++)
257        {
258            isf_q[i + 5] = add_int16(isf_q[i + 5], dico22_isf_36b[(indice[3] << 2) + i]);
259        }
260
261        for (i = 0; i < ORDER; i++)
262        {
263            tmp = isf_q[i];
264            isf_q[i] = add_int16(tmp, mean_isf[i]);
265            isf_q[i] = add_int16(isf_q[i], mult_int16(MU, past_isfq[i]));
266            past_isfq[i] = tmp;
267        }
268
269
270        if (enc_dec)
271        {
272            for (i = 0; i < M; i++)
273            {
274                for (j = (L_MEANBUF - 1); j > 0; j--)
275                {
276                    isf_buf[j * M + i] = isf_buf[(j - 1) * M + i];
277                }
278                isf_buf[i] = isf_q[i];
279            }
280        }
281    }
282    else
283    {                                      /* bad frame */
284        for (i = 0; i < M; i++)
285        {
286            L_tmp = mul_16by16_to_int32(mean_isf[i], 8192);
287            for (j = 0; j < L_MEANBUF; j++)
288            {
289                L_tmp = mac_16by16_to_int32(L_tmp, isf_buf[j * M + i], 8192);
290            }
291
292            ref_isf[i] = amr_wb_round(L_tmp);
293        }
294
295        /* use the past ISFs slightly shifted towards their mean */
296        for (i = 0; i < ORDER; i++)
297        {
298            isf_q[i] = add_int16(mult_int16(ALPHA, isfold[i]), mult_int16(ONE_ALPHA, ref_isf[i]));
299        }
300
301        /* estimate past quantized residual to be used in next frame */
302
303        for (i = 0; i < ORDER; i++)
304        {
305            tmp = add_int16(ref_isf[i], mult_int16(past_isfq[i], MU));      /* predicted ISF */
306            past_isfq[i] = sub_int16(isf_q[i], tmp);
307            past_isfq[i] >>=  1;           /* past_isfq[i] *= 0.5 */
308        }
309    }
310
311    Reorder_isf(isf_q, ISF_GAP, ORDER);
312
313    return;
314}
315
316/*
317 * procedure  Reorder_isf()
318 *            ~~~~~~~~~~~~~
319 * To make sure that the  isfs are properly order and to keep a certain
320 * minimum distance between consecutive isfs.
321 *
322 *    Argument         description                     in/out
323 *    ~~~~~~~~         ~~~~~~~~~~~                     ~~~~~~
324 *     isf[]           vector of isfs                    i/o
325 *     min_dist        minimum required distance         i
326 *     n               LPC order                         i
327 */
328
329void Reorder_isf(
330    int16 * isf,                         /* (i/o) Q15: ISF in the frequency domain (0..0.5) */
331    int16 min_dist,                      /* (i) Q15  : minimum distance to keep             */
332    int16 n                              /* (i)      : number of ISF                        */
333)
334{
335    int16 i, isf_min;
336
337    isf_min = min_dist;
338
339    for (i = 0; i < n - 1; i++)
340    {
341        if (isf[i] < isf_min)
342        {
343            isf[i] = isf_min;
344        }
345        isf_min = add_int16(isf[i], min_dist);
346    }
347
348    return;
349}
350
351