Git init
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / enc / src / qgain475.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.073
22     ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec
23     Available from http://www.3gpp.org
24
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 ****************************************************************************************/
29 /*
30 ------------------------------------------------------------------------------
31
32
33
34  Filename: qgain475.cpp
35  Funtions: MR475_quant_store_results
36            MR475_update_unq_pred
37            MR475_gain_quant
38
39 ------------------------------------------------------------------------------
40  MODULE DESCRIPTION
41
42  These modules handle the quantization of pitch and codebook gains for MR475.
43
44 ------------------------------------------------------------------------------
45 */
46
47
48 /*----------------------------------------------------------------------------
49 ; INCLUDES
50 ----------------------------------------------------------------------------*/
51 #include "qgain475.h"
52 #include "typedef.h"
53 #include "basic_op.h"
54 #include "mode.h"
55 #include "cnst.h"
56 #include "pow2.h"
57 #include "log2.h"
58
59 /*----------------------------------------------------------------------------
60 ; MACROS
61 ; Define module specific macros here
62 ----------------------------------------------------------------------------*/
63
64
65 /*----------------------------------------------------------------------------
66 ; DEFINES
67 ; Include all pre-processor statements here. Include conditional
68 ; compile variables also.
69 ----------------------------------------------------------------------------*/
70 #define MR475_VQ_SIZE 256
71
72 /*----------------------------------------------------------------------------
73 ; LOCAL FUNCTION DEFINITIONS
74 ; Function Prototype declaration
75 ----------------------------------------------------------------------------*/
76
77 /*----------------------------------------------------------------------------
78 ; LOCAL VARIABLE DEFINITIONS
79 ; Variable declaration - defined here and used outside this module
80 ----------------------------------------------------------------------------*/
81
82 /* The table contains the following data:
83  *
84  *    g_pitch(0)        (Q14) // for sub-
85  *    g_fac(0)          (Q12) // frame 0 and 2
86  *    g_pitch(1)        (Q14) // for sub-
87  *    g_fac(2)          (Q12) // frame 1 and 3
88  *
89  */
90 static const Word16 table_gain_MR475[MR475_VQ_SIZE*4] =
91 {
92     /*g_pit(0), g_fac(0),      g_pit(1), g_fac(1) */
93     812,          128,           542,      140,
94     2873,         1135,          2266,     3402,
95     2067,          563,         12677,      647,
96     4132,         1798,          5601,     5285,
97     7689,          374,          3735,      441,
98     10912,         2638,         11807,     2494,
99     20490,          797,          5218,      675,
100     6724,         8354,          5282,     1696,
101     1488,          428,          5882,      452,
102     5332,         4072,          3583,     1268,
103     2469,          901,         15894,     1005,
104     14982,         3271,         10331,     4858,
105     3635,         2021,          2596,      835,
106     12360,         4892,         12206,     1704,
107     13432,         1604,          9118,     2341,
108     3968,         1538,          5479,     9936,
109     3795,          417,          1359,      414,
110     3640,         1569,          7995,     3541,
111     11405,          645,          8552,      635,
112     4056,         1377,         16608,     6124,
113     11420,          700,          2007,      607,
114     12415,         1578,         11119,     4654,
115     13680,         1708,         11990,     1229,
116     7996,         7297,         13231,     5715,
117     2428,         1159,          2073,     1941,
118     6218,         6121,          3546,     1804,
119     8925,         1802,          8679,     1580,
120     13935,         3576,         13313,     6237,
121     6142,         1130,          5994,     1734,
122     14141,         4662,         11271,     3321,
123     12226,         1551,         13931,     3015,
124     5081,        10464,          9444,     6706,
125     1689,          683,          1436,     1306,
126     7212,         3933,          4082,     2713,
127     7793,          704,         15070,      802,
128     6299,         5212,          4337,     5357,
129     6676,          541,          6062,      626,
130     13651,         3700,         11498,     2408,
131     16156,          716,         12177,      751,
132     8065,        11489,          6314,     2256,
133     4466,          496,          7293,      523,
134     10213,         3833,          8394,     3037,
135     8403,          966,         14228,     1880,
136     8703,         5409,         16395,     4863,
137     7420,         1979,          6089,     1230,
138     9371,         4398,         14558,     3363,
139     13559,         2873,         13163,     1465,
140     5534,         1678,         13138,    14771,
141     7338,          600,          1318,      548,
142     4252,         3539,         10044,     2364,
143     10587,          622,         13088,      669,
144     14126,         3526,          5039,     9784,
145     15338,          619,          3115,      590,
146     16442,         3013,         15542,     4168,
147     15537,         1611,         15405,     1228,
148     16023,         9299,          7534,     4976,
149     1990,         1213,         11447,     1157,
150     12512,         5519,          9475,     2644,
151     7716,         2034,         13280,     2239,
152     16011,         5093,          8066,     6761,
153     10083,         1413,          5002,     2347,
154     12523,         5975,         15126,     2899,
155     18264,         2289,         15827,     2527,
156     16265,        10254,         14651,    11319,
157     1797,          337,          3115,      397,
158     3510,         2928,          4592,     2670,
159     7519,          628,         11415,      656,
160     5946,         2435,          6544,     7367,
161     8238,          829,          4000,      863,
162     10032,         2492,         16057,     3551,
163     18204,         1054,          6103,     1454,
164     5884,         7900,         18752,     3468,
165     1864,          544,          9198,      683,
166     11623,         4160,          4594,     1644,
167     3158,         1157,         15953,     2560,
168     12349,         3733,         17420,     5260,
169     6106,         2004,          2917,     1742,
170     16467,         5257,         16787,     1680,
171     17205,         1759,          4773,     3231,
172     7386,         6035,         14342,    10012,
173     4035,          442,          4194,      458,
174     9214,         2242,          7427,     4217,
175     12860,          801,         11186,      825,
176     12648,         2084,         12956,     6554,
177     9505,          996,          6629,      985,
178     10537,         2502,         15289,     5006,
179     12602,         2055,         15484,     1653,
180     16194,         6921,         14231,     5790,
181     2626,          828,          5615,     1686,
182     13663,         5778,          3668,     1554,
183     11313,         2633,          9770,     1459,
184     14003,         4733,         15897,     6291,
185     6278,         1870,          7910,     2285,
186     16978,         4571,         16576,     3849,
187     15248,         2311,         16023,     3244,
188     14459,        17808,         11847,     2763,
189     1981,         1407,          1400,      876,
190     4335,         3547,          4391,     4210,
191     5405,          680,         17461,      781,
192     6501,         5118,          8091,     7677,
193     7355,          794,          8333,     1182,
194     15041,         3160,         14928,     3039,
195     20421,          880,         14545,      852,
196     12337,        14708,          6904,     1920,
197     4225,          933,          8218,     1087,
198     10659,         4084,         10082,     4533,
199     2735,          840,         20657,     1081,
200     16711,         5966,         15873,     4578,
201     10871,         2574,          3773,     1166,
202     14519,         4044,         20699,     2627,
203     15219,         2734,         15274,     2186,
204     6257,         3226,         13125,    19480,
205     7196,          930,          2462,     1618,
206     4515,         3092,         13852,     4277,
207     10460,          833,         17339,      810,
208     16891,         2289,         15546,     8217,
209     13603,         1684,          3197,     1834,
210     15948,         2820,         15812,     5327,
211     17006,         2438,         16788,     1326,
212     15671,         8156,         11726,     8556,
213     3762,         2053,          9563,     1317,
214     13561,         6790,         12227,     1936,
215     8180,         3550,         13287,     1778,
216     16299,         6599,         16291,     7758,
217     8521,         2551,          7225,     2645,
218     18269,         7489,         16885,     2248,
219     17882,         2884,         17265,     3328,
220     9417,        20162,         11042,     8320,
221     1286,          620,          1431,      583,
222     5993,         2289,          3978,     3626,
223     5144,          752,         13409,      830,
224     5553,         2860,         11764,     5908,
225     10737,          560,          5446,      564,
226     13321,         3008,         11946,     3683,
227     19887,          798,          9825,      728,
228     13663,         8748,          7391,     3053,
229     2515,          778,          6050,      833,
230     6469,         5074,          8305,     2463,
231     6141,         1865,         15308,     1262,
232     14408,         4547,         13663,     4515,
233     3137,         2983,          2479,     1259,
234     15088,         4647,         15382,     2607,
235     14492,         2392,         12462,     2537,
236     7539,         2949,         12909,    12060,
237     5468,          684,          3141,      722,
238     5081,         1274,         12732,     4200,
239     15302,          681,          7819,      592,
240     6534,         2021,         16478,     8737,
241     13364,          882,          5397,      899,
242     14656,         2178,         14741,     4227,
243     14270,         1298,         13929,     2029,
244     15477,         7482,         15815,     4572,
245     2521,         2013,          5062,     1804,
246     5159,         6582,          7130,     3597,
247     10920,         1611,         11729,     1708,
248     16903,         3455,         16268,     6640,
249     9306,         1007,          9369,     2106,
250     19182,         5037,         12441,     4269,
251     15919,         1332,         15357,     3512,
252     11898,        14141,         16101,     6854,
253     2010,          737,          3779,      861,
254     11454,         2880,          3564,     3540,
255     9057,         1241,         12391,      896,
256     8546,         4629,         11561,     5776,
257     8129,          589,          8218,      588,
258     18728,         3755,         12973,     3149,
259     15729,          758,         16634,      754,
260     15222,        11138,         15871,     2208,
261     4673,          610,         10218,      678,
262     15257,         4146,          5729,     3327,
263     8377,         1670,         19862,     2321,
264     15450,         5511,         14054,     5481,
265     5728,         2888,          7580,     1346,
266     14384,         5325,         16236,     3950,
267     15118,         3744,         15306,     1435,
268     14597,         4070,         12301,    15696,
269     7617,         1699,          2170,      884,
270     4459,         4567,         18094,     3306,
271     12742,          815,         14926,      907,
272     15016,         4281,         15518,     8368,
273     17994,         1087,          2358,      865,
274     16281,         3787,         15679,     4596,
275     16356,         1534,         16584,     2210,
276     16833,         9697,         15929,     4513,
277     3277,         1085,          9643,     2187,
278     11973,         6068,          9199,     4462,
279     8955,         1629,         10289,     3062,
280     16481,         5155,         15466,     7066,
281     13678,         2543,          5273,     2277,
282     16746,         6213,         16655,     3408,
283     20304,         3363,         18688,     1985,
284     14172,        12867,         15154,    15703,
285     4473,         1020,          1681,      886,
286     4311,         4301,          8952,     3657,
287     5893,         1147,         11647,     1452,
288     15886,         2227,          4582,     6644,
289     6929,         1205,          6220,      799,
290     12415,         3409,         15968,     3877,
291     19859,         2109,          9689,     2141,
292     14742,         8830,         14480,     2599,
293     1817,         1238,          7771,      813,
294     19079,         4410,          5554,     2064,
295     3687,         2844,         17435,     2256,
296     16697,         4486,         16199,     5388,
297     8028,         2763,          3405,     2119,
298     17426,         5477,         13698,     2786,
299     19879,         2720,          9098,     3880,
300     18172,         4833,         17336,    12207,
301     5116,          996,          4935,      988,
302     9888,         3081,          6014,     5371,
303     15881,         1667,          8405,     1183,
304     15087,         2366,         19777,     7002,
305     11963,         1562,          7279,     1128,
306     16859,         1532,         15762,     5381,
307     14708,         2065,         20105,     2155,
308     17158,         8245,         17911,     6318,
309     5467,         1504,          4100,     2574,
310     17421,         6810,          5673,     2888,
311     16636,         3382,          8975,     1831,
312     20159,         4737,         19550,     7294,
313     6658,         2781,         11472,     3321,
314     19397,         5054,         18878,     4722,
315     16439,         2373,         20430,     4386,
316     11353,        26526,         11593,     3068,
317     2866,         1566,          5108,     1070,
318     9614,         4915,          4939,     3536,
319     7541,          878,         20717,      851,
320     6938,         4395,         16799,     7733,
321     10137,         1019,          9845,      964,
322     15494,         3955,         15459,     3430,
323     18863,          982,         20120,      963,
324     16876,        12887,         14334,     4200,
325     6599,         1220,          9222,      814,
326     16942,         5134,          5661,     4898,
327     5488,         1798,         20258,     3962,
328     17005,         6178,         17929,     5929,
329     9365,         3420,          7474,     1971,
330     19537,         5177,         19003,     3006,
331     16454,         3788,         16070,     2367,
332     8664,         2743,          9445,    26358,
333     10856,         1287,          3555,     1009,
334     5606,         3622,         19453,     5512,
335     12453,          797,         20634,      911,
336     15427,         3066,         17037,    10275,
337     18883,         2633,          3913,     1268,
338     19519,         3371,         18052,     5230,
339     19291,         1678,         19508,     3172,
340     18072,        10754,         16625,     6845,
341     3134,         2298,         10869,     2437,
342     15580,         6913,         12597,     3381,
343     11116,         3297,         16762,     2424,
344     18853,         6715,         17171,     9887,
345     12743,         2605,          8937,     3140,
346     19033,         7764,         18347,     3880,
347     20475,         3682,         19602,     3380,
348     13044,        19373,         10526,    23124
349 };
350
351 /*
352 ------------------------------------------------------------------------------
353  FUNCTION NAME: MR475_quant_store_results
354 ------------------------------------------------------------------------------
355  INPUT AND OUTPUT DEFINITIONS
356
357  Inputs:
358     pred_st = pointer to structure of type gc_predState
359     p = pointer to selected quantizer table entry (const Word16)
360     gcode0 = predicted CB gain (Word16)
361     exp_gcode0 = exponent of predicted CB gain (Word16)
362     gain_pit = pointer to Pitch gain (Word16)
363     gain_cod = pointer to Code gain (Word16)
364
365  Outputs:
366     pred_st points to the updated structure of type gc_predState
367     gain_pit points to Pitch gain
368     gain_cod points to Code gain
369     pOverflow points to overflow indicator (Flag)
370
371  Returns:
372     None.
373
374  Global Variables Used:
375     None.
376
377  Local Variables Needed:
378     None.
379
380 ------------------------------------------------------------------------------
381  FUNCTION DESCRIPTION
382
383  This function calculates the final fixed codebook gain and the predictor
384  update values, and updates the gain predictor.
385
386 ------------------------------------------------------------------------------
387  REQUIREMENTS
388
389  None.
390
391 ------------------------------------------------------------------------------
392  REFERENCES
393
394  qgain475.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
395
396 ------------------------------------------------------------------------------
397  PSEUDO-CODE
398
399 static void MR475_quant_store_results(
400
401     gc_predState *pred_st, // i/o: gain predictor state struct
402     const Word16 *p,       // i  : pointer to selected quantizer table entry
403     Word16 gcode0,         // i  : predicted CB gain,     Q(14 - exp_gcode0)
404     Word16 exp_gcode0,     // i  : exponent of predicted CB gain,        Q0
405     Word16 *gain_pit,      // o  : Pitch gain,                           Q14
406     Word16 *gain_cod       // o  : Code gain,                            Q1
407 )
408 {
409
410     Word16 g_code, exp, frac, tmp;
411     Word32 L_tmp;
412
413     Word16 qua_ener_MR122; // o  : quantized energy error, MR122 version Q10
414     Word16 qua_ener;       // o  : quantized energy error,               Q10
415
416     // Read the quantized gains
417     *gain_pit = *p++;
418     g_code = *p++;
419
420     //------------------------------------------------------------------*
421      *  calculate final fixed codebook gain:                            *
422      *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                            *
423      *                                                                  *
424      *   gc = gc0 * g                                                   *
425      *------------------------------------------------------------------
426
427     L_tmp = L_mult(g_code, gcode0);
428     L_tmp = L_shr(L_tmp, sub(10, exp_gcode0));
429     *gain_cod = extract_h(L_tmp);
430
431     //------------------------------------------------------------------*
432      *  calculate predictor update values and update gain predictor:    *
433      *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    *
434      *                                                                  *
435      *   qua_ener       = log2(g)                                       *
436      *   qua_ener_MR122 = 20*log10(g)                                   *
437      *------------------------------------------------------------------
438
439     Log2 (L_deposit_l (g_code), &exp, &frac); // Log2(x Q12) = log2(x) + 12
440     exp = sub(exp, 12);
441
442     tmp = shr_r (frac, 5);
443     qua_ener_MR122 = add (tmp, shl (exp, 10));
444
445     L_tmp = Mpy_32_16(exp, frac, 24660); // 24660 Q12 ~= 6.0206 = 20*log10(2)
446     qua_ener = pv_round (L_shl (L_tmp, 13)); // Q12 * Q0 = Q13 -> Q10
447
448     gc_pred_update(pred_st, qua_ener_MR122, qua_ener);
449 }
450
451 ------------------------------------------------------------------------------
452  CAUTION [optional]
453  [State any special notes, constraints or cautions for users of this function]
454
455 ------------------------------------------------------------------------------
456 */
457
458 static void MR475_quant_store_results(
459     gc_predState *pred_st, /* i/o: gain predictor state struct               */
460     const Word16 *p,       /* i  : pointer to selected quantizer table entry */
461     Word16 gcode0,         /* i  : predicted CB gain,     Q(14 - exp_gcode0) */
462     Word16 exp_gcode0,     /* i  : exponent of predicted CB gain,        Q0  */
463     Word16 *gain_pit,      /* o  : Pitch gain,                           Q14 */
464     Word16 *gain_cod,      /* o  : Code gain,                            Q1  */
465     Flag   *pOverflow      /* o  : overflow indicator                        */
466 )
467 {
468     Word16 g_code;
469     Word16 exp;
470     Word16 frac;
471     Word16 tmp;
472     Word32 L_tmp;
473
474     Word16 qua_ener_MR122; /* o  : quantized energy error, MR122 version Q10 */
475     Word16 qua_ener;       /* o  : quantized energy error,               Q10 */
476
477
478     /* Read the quantized gains */
479     *gain_pit = *p++;
480     g_code = *p++;
481
482     /*------------------------------------------------------------------*
483      *  calculate final fixed codebook gain:                            *
484      *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                            *
485      *                                                                  *
486      *   gc = gc0 * g                                                   *
487      *------------------------------------------------------------------*/
488
489     L_tmp = ((Word32) g_code * gcode0) << 1;
490     tmp   = 10 - exp_gcode0;
491     L_tmp = L_shr(L_tmp, tmp, pOverflow);
492     *gain_cod = (Word16)(L_tmp >> 16);
493
494     /*------------------------------------------------------------------*
495      *  calculate predictor update values and update gain predictor:    *
496      *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    *
497      *                                                                  *
498      *   qua_ener       = log2(g)                                       *
499      *   qua_ener_MR122 = 20*log10(g)                                   *
500      *------------------------------------------------------------------*/
501
502     /* Log2(x Q12) = log2(x) + 12 */
503     Log2((Word32) g_code, &exp, &frac, pOverflow);
504     exp -= 12;
505
506     tmp = shr_r(frac, 5, pOverflow);
507     qua_ener_MR122 = exp << 10;
508     qua_ener_MR122 = tmp + qua_ener_MR122;
509
510     /* 24660 Q12 ~= 6.0206 = 20*log10(2) */
511     L_tmp = Mpy_32_16(exp, frac, 24660, pOverflow);
512     L_tmp = L_tmp << 13;
513
514     /* Q12 * Q0 = Q13 -> Q10 */
515     qua_ener = (Word16)((L_tmp + (Word32) 0x00008000L) >> 16);
516
517     gc_pred_update(pred_st, qua_ener_MR122, qua_ener);
518
519     return;
520 }
521
522 /****************************************************************************/
523
524
525 /*
526 ------------------------------------------------------------------------------
527  FUNCTION NAME: MR475_update_unq_pred
528 ------------------------------------------------------------------------------
529  INPUT AND OUTPUT DEFINITIONS
530
531  Inputs:
532     pred_st = pointer to structure of type gc_predState
533     exp_gcode0 = predicted CB gain (exponent MSW) (Word16)
534     frac_gcode0 = predicted CB gain (exponent LSW) (Word16)
535     cod_gain_exp = optimum codebook gain (exponent)(Word16)
536     cod_gain_frac = optimum codebook gain (fraction) (Word16)
537
538  Outputs:
539     pred_st points to the updated structure of type gc_predState
540     pOverflow points to overflow indicator (Flag)
541
542  Returns:
543     None.
544
545  Global Variables Used:
546     None.
547
548  Local Variables Needed:
549     None.
550
551 ------------------------------------------------------------------------------
552  FUNCTION DESCRIPTION
553
554  This module uses the optimum codebook gain and updates the "unquantized"
555  gain predictor with the (bounded) prediction error.
556
557 ------------------------------------------------------------------------------
558  REQUIREMENTS
559
560  None.
561
562 ------------------------------------------------------------------------------
563  REFERENCES
564
565  qgain475.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
566
567 ------------------------------------------------------------------------------
568  PSEUDO-CODE
569
570 void
571 MR475_update_unq_pred(
572     gc_predState *pred_st, // i/o: gain predictor state struct
573     Word16 exp_gcode0,     // i  : predicted CB gain (exponent MSW),  Q0
574     Word16 frac_gcode0,    // i  : predicted CB gain (exponent LSW),  Q15
575     Word16 cod_gain_exp,   // i  : optimum codebook gain (exponent),  Q0
576     Word16 cod_gain_frac   // i  : optimum codebook gain (fraction),  Q15
577 )
578 {
579     Word16 tmp, exp, frac;
580     Word16 qua_ener, qua_ener_MR122;
581     Word32 L_tmp;
582
583     // calculate prediction error factor (given optimum CB gain gcu):
584     //   predErrFact = gcu / gcode0
585     //   (limit to MIN_PRED_ERR_FACT <= predErrFact <= MAX_PRED_ERR_FACT
586     //    -> limit qua_ener*)
587     //
588     // calculate prediction error (log):
589     //
590     //   qua_ener_MR122 = log2(predErrFact)
591     //   qua_ener       = 20*log10(predErrFact)
592
593     if (cod_gain_frac <= 0)
594     {
595         // if gcu <= 0 -> predErrFact = 0 < MIN_PRED_ERR_FACT
596         // -> set qua_ener(_MR122) directly
597         qua_ener = MIN_QUA_ENER;
598         qua_ener_MR122 = MIN_QUA_ENER_MR122;
599     }
600     else
601     {
602         // convert gcode0 from DPF to standard fraction/exponent format
603         // with normalized frac, i.e. 16384 <= frac <= 32767
604         // Note: exponent correction (exp=exp-14) is done after div_s
605         frac_gcode0 = extract_l (Pow2 (14, frac_gcode0));
606
607         // make sure cod_gain_frac < frac_gcode0  for div_s
608         if (sub(cod_gain_frac, frac_gcode0) >= 0)
609         {
610             cod_gain_frac = shr (cod_gain_frac, 1);
611             cod_gain_exp = add (cod_gain_exp, 1);
612         }
613
614         // predErrFact
615         //   = gcu / gcode0
616         //   = cod_gain_frac/frac_gcode0 * 2^(cod_gain_exp-(exp_gcode0-14))
617         //   = div_s (c_g_f, frac_gcode0)*2^-15 * 2^(c_g_e-exp_gcode0+14)
618         //   = div_s * 2^(cod_gain_exp-exp_gcode0 - 1)
619
620         frac = div_s (cod_gain_frac, frac_gcode0);
621         tmp = sub (sub (cod_gain_exp, exp_gcode0), 1);
622
623         Log2 (L_deposit_l (frac), &exp, &frac);
624         exp = add (exp, tmp);
625
626         // calculate prediction error (log2, Q10)
627         qua_ener_MR122 = shr_r (frac, 5);
628         qua_ener_MR122 = add (qua_ener_MR122, shl (exp, 10));
629
630         if (sub(qua_ener_MR122, MIN_QUA_ENER_MR122) < 0)
631         {
632             qua_ener = MIN_QUA_ENER;
633             qua_ener_MR122 = MIN_QUA_ENER_MR122;
634         }
635         else if (sub(qua_ener_MR122, MAX_QUA_ENER_MR122) > 0)
636         {
637             qua_ener = MAX_QUA_ENER;
638             qua_ener_MR122 = MAX_QUA_ENER_MR122;
639         }
640         else
641         {
642             // calculate prediction error (20*log10, Q10)
643             L_tmp = Mpy_32_16(exp, frac, 24660);
644             // 24660 Q12 ~= 6.0206 = 20*log10(2)
645             qua_ener = pv_round (L_shl (L_tmp, 13));
646             // Q12 * Q0 = Q13 -> Q26 -> Q10
647         }
648     }
649
650     // update MA predictor memory
651     gc_pred_update(pred_st, qua_ener_MR122, qua_ener);
652 }
653
654 ------------------------------------------------------------------------------
655  CAUTION [optional]
656  [State any special notes, constraints or cautions for users of this function]
657
658 ------------------------------------------------------------------------------
659 */
660
661 void MR475_update_unq_pred(
662     gc_predState *pred_st, /* i/o: gain predictor state struct            */
663     Word16 exp_gcode0,     /* i  : predicted CB gain (exponent MSW),  Q0  */
664     Word16 frac_gcode0,    /* i  : predicted CB gain (exponent LSW),  Q15 */
665     Word16 cod_gain_exp,   /* i  : optimum codebook gain (exponent),  Q0  */
666     Word16 cod_gain_frac,  /* i  : optimum codebook gain (fraction),  Q15 */
667     Flag   *pOverflow      /* o  : overflow indicator                     */
668 )
669 {
670     Word16 tmp;
671     Word16 exp;
672     Word16 frac;
673     Word16 qua_ener;
674     Word16 qua_ener_MR122;
675     Word32 L_tmp;
676
677     /* calculate prediction error factor (given optimum CB gain gcu):
678      *
679      *   predErrFact = gcu / gcode0
680      *   (limit to MIN_PRED_ERR_FACT <= predErrFact <= MAX_PRED_ERR_FACT
681      *    -> limit qua_ener*)
682      *
683      * calculate prediction error (log):
684      *
685      *   qua_ener_MR122 = log2(predErrFact)
686      *   qua_ener       = 20*log10(predErrFact)
687      *
688      */
689
690     if (cod_gain_frac <= 0)
691     {
692         /* if gcu <= 0 -> predErrFact = 0 < MIN_PRED_ERR_FACT */
693         /* -> set qua_ener(_MR122) directly                   */
694         qua_ener = MIN_QUA_ENER;
695         qua_ener_MR122 = MIN_QUA_ENER_MR122;
696     }
697     else
698     {
699         /* convert gcode0 from DPF to standard fraction/exponent format */
700         /* with normalized frac, i.e. 16384 <= frac <= 32767            */
701         /* Note: exponent correction (exp=exp-14) is done after div_s   */
702         frac_gcode0 = (Word16)(Pow2(14, frac_gcode0, pOverflow));
703
704         /* make sure cod_gain_frac < frac_gcode0  for div_s */
705         if (cod_gain_frac >= frac_gcode0)
706         {
707             cod_gain_frac >>= 1;
708             cod_gain_exp += 1;
709         }
710
711         /*
712           predErrFact
713              = gcu / gcode0
714              = cod_gain_frac/frac_gcode0 * 2^(cod_gain_exp-(exp_gcode0-14))
715              = div_s (c_g_f, frac_gcode0)*2^-15 * 2^(c_g_e-exp_gcode0+14)
716              = div_s * 2^(cod_gain_exp-exp_gcode0 - 1)
717         */
718         frac = div_s(cod_gain_frac, frac_gcode0);
719         tmp = cod_gain_exp - exp_gcode0;
720         tmp -= 1;
721
722         Log2((Word32) frac, &exp, &frac, pOverflow);
723         exp += tmp;
724
725         /* calculate prediction error (log2, Q10) */
726         qua_ener_MR122 = shr_r(frac, 5, pOverflow);
727         tmp = exp << 10;
728         qua_ener_MR122 += tmp;
729
730         if (qua_ener_MR122 > MAX_QUA_ENER_MR122)
731         {
732             qua_ener = MAX_QUA_ENER;
733             qua_ener_MR122 = MAX_QUA_ENER_MR122;
734         }
735         else
736         {
737             /* calculate prediction error (20*log10, Q10) */
738             L_tmp = Mpy_32_16(exp, frac, 24660, pOverflow);
739             /* 24660 Q12 ~= 6.0206 = 20*log10(2) */
740             L_tmp =  L_shl(L_tmp, 13, pOverflow);
741             qua_ener = pv_round(L_tmp, pOverflow);
742
743             /* Q12 * Q0 = Q13 -> Q26 -> Q10     */
744         }
745     }
746
747     /* update MA predictor memory */
748     gc_pred_update(pred_st, qua_ener_MR122, qua_ener);
749
750
751     return;
752 }
753
754 /****************************************************************************/
755
756
757 /*
758 ------------------------------------------------------------------------------
759  FUNCTION NAME: MR475_gain_quant
760 ------------------------------------------------------------------------------
761  INPUT AND OUTPUT DEFINITIONS
762
763  Inputs:
764     pred_st = pointer to structure of type gc_predState
765     sf0_exp_gcode0 = predicted CB gain (exponent) (Word16)
766     f0_frac_gcode0 = predicted CB gain (fraction) (Word16)
767     sf0_exp_coeff = energy coeff. (exponent part) (Word16)
768     sf0_frac_coeff = energy coeff. ((fraction part) (Word16)
769     sf0_exp_target_en = exponent of target energy (Word16)
770     sf0_frac_target_en = fraction of target energy (Word16)
771     sf1_code_nosharp = innovative codebook vector  (Word16)
772     sf1_exp_gcode0 = predicted CB gain (exponent) (Word16)
773     sf1_frac_gcode0 = predicted CB gain (fraction) (Word16)
774     sf1_exp_coeff = energy coeff. (exponent part) (Word16)
775     sf1_frac_coeff = energy coeff. (fraction part) (Word16)
776     sf1_exp_target_en = exponent of target energy (Word16)
777     sf1_frac_target_en = fraction of target energy (Word16)
778     gp_limit = pitch gain limit (Word16)
779     sf0_gain_pit = pointer to Pitch gain (Word16)
780     sf0_gain_cod = pointer to Code gain (Word16)
781     sf1_gain_pit = pointer to Pitch gain (Word16)
782     sf1_gain_cod = pointer to Code gain (Word16)
783
784  Outputs:
785     pred_st points to the updated structure of type gc_predState
786     sf0_gain_pit points to Pitch gain
787     sf0_gain_cod points to Code gain
788     sf1_gain_pit points to Pitch gain
789     sf1_gain_cod points to Code gain
790
791  Returns:
792     index = index of quantization
793
794  Global Variables Used:
795     None.
796
797  Local Variables Needed:
798     None.
799
800 ------------------------------------------------------------------------------
801  FUNCTION DESCRIPTION
802
803  This module provides quantization of pitch and codebook gains for two
804  subframes using the predicted codebook gain.
805
806 ------------------------------------------------------------------------------
807  REQUIREMENTS
808
809  None.
810
811 ------------------------------------------------------------------------------
812  REFERENCES
813
814  qgain475.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
815
816 ------------------------------------------------------------------------------
817  PSEUDO-CODE
818
819 Word16
820 MR475_gain_quant(              // o  : index of quantization.
821     gc_predState *pred_st,     // i/o: gain predictor state struct
822
823                                // data from subframe 0 (or 2)
824     Word16 sf0_exp_gcode0,     // i  : predicted CB gain (exponent),      Q0
825     Word16 sf0_frac_gcode0,    // i  : predicted CB gain (fraction),      Q15
826     Word16 sf0_exp_coeff[],    // i  : energy coeff. (5), exponent part,  Q0
827     Word16 sf0_frac_coeff[],   // i  : energy coeff. (5), fraction part,  Q15
828                                //      (frac_coeff and exp_coeff computed in
829                                //       calc_filt_energies())
830     Word16 sf0_exp_target_en,  // i  : exponent of target energy,         Q0
831     Word16 sf0_frac_target_en, // i  : fraction of target energy,         Q15
832
833                                // data from subframe 1 (or 3)
834     Word16 sf1_code_nosharp[], // i  : innovative codebook vector (L_SUBFR)
835                                //      (whithout pitch sharpening)
836     Word16 sf1_exp_gcode0,     // i  : predicted CB gain (exponent),      Q0
837     Word16 sf1_frac_gcode0,    // i  : predicted CB gain (fraction),      Q15
838     Word16 sf1_exp_coeff[],    // i  : energy coeff. (5), exponent part,  Q0
839     Word16 sf1_frac_coeff[],   // i  : energy coeff. (5), fraction part,  Q15
840                                //      (frac_coeff and exp_coeff computed in
841                                //       calc_filt_energies())
842     Word16 sf1_exp_target_en,  // i  : exponent of target energy,         Q0
843     Word16 sf1_frac_target_en, // i  : fraction of target energy,         Q15
844
845     Word16 gp_limit,           // i  : pitch gain limit
846
847     Word16 *sf0_gain_pit,      // o  : Pitch gain,                        Q14
848     Word16 *sf0_gain_cod,      // o  : Code gain,                         Q1
849
850     Word16 *sf1_gain_pit,      // o  : Pitch gain,                        Q14
851     Word16 *sf1_gain_cod       // o  : Code gain,                         Q1
852 )
853 {
854     const Word16 *p;
855     Word16 i, index = 0;
856     Word16 tmp;
857     Word16 exp;
858     Word16 sf0_gcode0, sf1_gcode0;
859     Word16 g_pitch, g2_pitch, g_code, g2_code, g_pit_cod;
860     Word16 coeff[10], coeff_lo[10], exp_max[10];  // 0..4: sf0; 5..9: sf1
861     Word32 L_tmp, dist_min;
862
863      *-------------------------------------------------------------------*
864      *  predicted codebook gain                                          *
865      *  ~~~~~~~~~~~~~~~~~~~~~~~                                          *
866      *  gc0     = 2^exp_gcode0 + 2^frac_gcode0                           *
867      *                                                                   *
868      *  gcode0 (Q14) = 2^14*2^frac_gcode0 = gc0 * 2^(14-exp_gcode0)      *
869      *-------------------------------------------------------------------*
870
871     sf0_gcode0 = extract_l(Pow2(14, sf0_frac_gcode0));
872     sf1_gcode0 = extract_l(Pow2(14, sf1_frac_gcode0));
873
874      * For each subframe, the error energy (sum) to be minimized consists
875      * of five terms, t[0..4].
876      *
877      *                      t[0] =    gp^2  * <y1 y1>
878      *                      t[1] = -2*gp    * <xn y1>
879      *                      t[2] =    gc^2  * <y2 y2>
880      *                      t[3] = -2*gc    * <xn y2>
881      *                      t[4] =  2*gp*gc * <y1 y2>
882      *
883
884     // sf 0
885     // determine the scaling exponent for g_code: ec = ec0 - 11
886     exp = sub(sf0_exp_gcode0, 11);
887
888     // calculate exp_max[i] = s[i]-1
889     exp_max[0] = sub(sf0_exp_coeff[0], 13);
890     exp_max[1] = sub(sf0_exp_coeff[1], 14);
891     exp_max[2] = add(sf0_exp_coeff[2], add(15, shl(exp, 1)));
892     exp_max[3] = add(sf0_exp_coeff[3], exp);
893     exp_max[4] = add(sf0_exp_coeff[4], add(1, exp));
894
895     // sf 1
896     // determine the scaling exponent for g_code: ec = ec0 - 11
897     exp = sub(sf1_exp_gcode0, 11);
898
899     // calculate exp_max[i] = s[i]-1
900     exp_max[5] = sub(sf1_exp_coeff[0], 13);
901     exp_max[6] = sub(sf1_exp_coeff[1], 14);
902     exp_max[7] = add(sf1_exp_coeff[2], add(15, shl(exp, 1)));
903     exp_max[8] = add(sf1_exp_coeff[3], exp);
904     exp_max[9] = add(sf1_exp_coeff[4], add(1, exp));
905
906      *-------------------------------------------------------------------*
907      *  Gain search equalisation:                                        *
908      *  ~~~~~~~~~~~~~~~~~~~~~~~~~                                        *
909      *  The MSE for the two subframes is weighted differently if there   *
910      *  is a big difference in the corresponding target energies         *
911      *-------------------------------------------------------------------*
912
913     // make the target energy exponents the same by de-normalizing the
914     // fraction of the smaller one. This is necessary to be able to compare
915     // them
916
917     exp = sf0_exp_target_en - sf1_exp_target_en;
918     if (exp > 0)
919     {
920         sf1_frac_target_en = shr (sf1_frac_target_en, exp);
921     }
922     else
923     {
924         sf0_frac_target_en = shl (sf0_frac_target_en, exp);
925     }
926
927     // assume no change of exponents
928     exp = 0;
929
930     // test for target energy difference; set exp to +1 or -1 to scale
931     // up/down coefficients for sf 1
932
933     tmp = shr_r (sf1_frac_target_en, 1);   // tmp = ceil(0.5*en(sf1))
934     if (sub (tmp, sf0_frac_target_en) > 0) // tmp > en(sf0)?
935     {
936         // target_energy(sf1) > 2*target_energy(sf0)
937         //   -> scale up MSE(sf0) by 2 by adding 1 to exponents 0..4
938         exp = 1;
939     }
940     else
941     {
942         tmp = shr (add (sf0_frac_target_en, 3), 2); // tmp=ceil(0.25*en(sf0))
943         if (sub (tmp, sf1_frac_target_en) > 0)      // tmp > en(sf1)?
944         {
945             // target_energy(sf1) < 0.25*target_energy(sf0)
946             //   -> scale down MSE(sf0) by 0.5 by subtracting 1 from
947             //      coefficients 0..4
948             exp = -1;
949         }
950     }
951
952     for (i = 0; i < 5; i++)
953     {
954         exp_max[i] = add (exp_max[i], exp);
955     }
956
957      *-------------------------------------------------------------------*
958      *  Find maximum exponent:                                           *
959      *  ~~~~~~~~~~~~~~~~~~~~~~                                           *
960      *                                                                   *
961      *  For the sum operation, all terms must have the same scaling;     *
962      *  that scaling should be low enough to prevent overflow. There-    *
963      *  fore, the maximum scale is determined and all coefficients are   *
964      *  re-scaled:                                                       *
965      *                                                                   *
966      *    exp = max(exp_max[i]) + 1;                                     *
967      *    e = exp_max[i]-exp;         e <= 0!                            *
968      *    c[i] = c[i]*2^e                                                *
969      *-------------------------------------------------------------------*
970
971     exp = exp_max[0];
972     for (i = 1; i < 10; i++)
973     {
974         if (sub(exp_max[i], exp) > 0)
975         {
976             exp = exp_max[i];
977         }
978     }
979     exp = add(exp, 1);      // To avoid overflow
980
981     p = &sf0_frac_coeff[0];
982     for (i = 0; i < 5; i++) {
983         tmp = sub(exp, exp_max[i]);
984         L_tmp = L_deposit_h(*p++);
985         L_tmp = L_shr(L_tmp, tmp);
986         L_Extract(L_tmp, &coeff[i], &coeff_lo[i]);
987     }
988     p = &sf1_frac_coeff[0];
989     for (; i < 10; i++) {
990         tmp = sub(exp, exp_max[i]);
991         L_tmp = L_deposit_h(*p++);
992         L_tmp = L_shr(L_tmp, tmp);
993         L_Extract(L_tmp, &coeff[i], &coeff_lo[i]);
994     }
995
996     //-------------------------------------------------------------------*
997      *  Codebook search:                                                 *
998      *  ~~~~~~~~~~~~~~~~                                                 *
999      *                                                                   *
1000      *  For each pair (g_pitch, g_fac) in the table calculate the        *
1001      *  terms t[0..4] and sum them up; the result is the mean squared    *
1002      *  error for the quantized gains from the table. The index for the  *
1003      *  minimum MSE is stored and finally used to retrieve the quantized *
1004      *  gains                                                            *
1005      *-------------------------------------------------------------------
1006
1007     // start with "infinite" MSE
1008     dist_min = MAX_32;
1009
1010     p = &table_gain_MR475[0];
1011
1012     for (i = 0; i < MR475_VQ_SIZE; i++)
1013     {
1014         // subframe 0 (and 2) calculations
1015         g_pitch = *p++;
1016         g_code = *p++;
1017
1018         g_code = mult(g_code, sf0_gcode0);
1019         g2_pitch = mult(g_pitch, g_pitch);
1020         g2_code = mult(g_code, g_code);
1021         g_pit_cod = mult(g_code, g_pitch);
1022
1023         L_tmp = Mpy_32_16(       coeff[0], coeff_lo[0], g2_pitch);
1024         L_tmp = Mac_32_16(L_tmp, coeff[1], coeff_lo[1], g_pitch);
1025         L_tmp = Mac_32_16(L_tmp, coeff[2], coeff_lo[2], g2_code);
1026         L_tmp = Mac_32_16(L_tmp, coeff[3], coeff_lo[3], g_code);
1027         L_tmp = Mac_32_16(L_tmp, coeff[4], coeff_lo[4], g_pit_cod);
1028
1029         tmp = sub (g_pitch, gp_limit);
1030
1031         // subframe 1 (and 3) calculations
1032         g_pitch = *p++;
1033         g_code = *p++;
1034
1035         if (tmp <= 0 && sub(g_pitch, gp_limit) <= 0)
1036         {
1037             g_code = mult(g_code, sf1_gcode0);
1038             g2_pitch = mult(g_pitch, g_pitch);
1039             g2_code = mult(g_code, g_code);
1040             g_pit_cod = mult(g_code, g_pitch);
1041
1042             L_tmp = Mac_32_16(L_tmp, coeff[5], coeff_lo[5], g2_pitch);
1043             L_tmp = Mac_32_16(L_tmp, coeff[6], coeff_lo[6], g_pitch);
1044             L_tmp = Mac_32_16(L_tmp, coeff[7], coeff_lo[7], g2_code);
1045             L_tmp = Mac_32_16(L_tmp, coeff[8], coeff_lo[8], g_code);
1046             L_tmp = Mac_32_16(L_tmp, coeff[9], coeff_lo[9], g_pit_cod);
1047
1048             // store table index if MSE for this index is lower
1049                than the minimum MSE seen so far
1050             if (L_sub(L_tmp, dist_min) < (Word32) 0)
1051             {
1052                 dist_min = L_tmp;
1053                 index = i;
1054             }
1055         }
1056     }
1057
1058      *------------------------------------------------------------------*
1059      *  read quantized gains and update MA predictor memories           *
1060      *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~           *
1061      *------------------------------------------------------------------*
1062
1063     // for subframe 0, the pre-calculated gcode0/exp_gcode0 are the same
1064     // as those calculated from the "real" predictor using quantized gains
1065     tmp = shl(index, 2);
1066     MR475_quant_store_results(pred_st,
1067                               &table_gain_MR475[tmp],
1068                               sf0_gcode0,
1069                               sf0_exp_gcode0,
1070                               sf0_gain_pit,
1071                               sf0_gain_cod);
1072
1073     // calculate new predicted gain for subframe 1 (this time using
1074     // the real, quantized gains)
1075     gc_pred(pred_st, MR475, sf1_code_nosharp,
1076             &sf1_exp_gcode0, &sf1_frac_gcode0,
1077             &sf0_exp_gcode0, &sf0_gcode0); // last two args are dummy
1078     sf1_gcode0 = extract_l(Pow2(14, sf1_frac_gcode0));
1079
1080     tmp = add (tmp, 2);
1081     MR475_quant_store_results(pred_st,
1082                               &table_gain_MR475[tmp],
1083                               sf1_gcode0,
1084                               sf1_exp_gcode0,
1085                               sf1_gain_pit,
1086                               sf1_gain_cod);
1087
1088     return index;
1089 }
1090
1091 ------------------------------------------------------------------------------
1092  CAUTION [optional]
1093  [State any special notes, constraints or cautions for users of this function]
1094
1095 ------------------------------------------------------------------------------
1096 */
1097
1098 Word16 MR475_gain_quant(       /* o  : index of quantization.                 */
1099     gc_predState *pred_st,     /* i/o: gain predictor state struct            */
1100
1101     /* data from subframe 0 (or 2) */
1102     Word16 sf0_exp_gcode0,     /* i  : predicted CB gain (exponent),      Q0  */
1103     Word16 sf0_frac_gcode0,    /* i  : predicted CB gain (fraction),      Q15 */
1104     Word16 sf0_exp_coeff[],    /* i  : energy coeff. (5), exponent part,  Q0  */
1105     Word16 sf0_frac_coeff[],   /* i  : energy coeff. (5), fraction part,  Q15 */
1106     /*      (frac_coeff and exp_coeff computed in  */
1107     /*       calc_filt_energies())                 */
1108     Word16 sf0_exp_target_en,  /* i  : exponent of target energy,         Q0  */
1109     Word16 sf0_frac_target_en, /* i  : fraction of target energy,         Q15 */
1110
1111     /* data from subframe 1 (or 3) */
1112     Word16 sf1_code_nosharp[], /* i  : innovative codebook vector (L_SUBFR)   */
1113     /*      (whithout pitch sharpening)            */
1114     Word16 sf1_exp_gcode0,     /* i  : predicted CB gain (exponent),      Q0  */
1115     Word16 sf1_frac_gcode0,    /* i  : predicted CB gain (fraction),      Q15 */
1116     Word16 sf1_exp_coeff[],    /* i  : energy coeff. (5), exponent part,  Q0  */
1117     Word16 sf1_frac_coeff[],   /* i  : energy coeff. (5), fraction part,  Q15 */
1118     /*      (frac_coeff and exp_coeff computed in  */
1119     /*       calc_filt_energies())                 */
1120     Word16 sf1_exp_target_en,  /* i  : exponent of target energy,         Q0  */
1121     Word16 sf1_frac_target_en, /* i  : fraction of target energy,         Q15 */
1122
1123     Word16 gp_limit,           /* i  : pitch gain limit                       */
1124
1125     Word16 *sf0_gain_pit,      /* o  : Pitch gain,                        Q14 */
1126     Word16 *sf0_gain_cod,      /* o  : Code gain,                         Q1  */
1127
1128     Word16 *sf1_gain_pit,      /* o  : Pitch gain,                        Q14 */
1129     Word16 *sf1_gain_cod,      /* o  : Code gain,                         Q1  */
1130     Flag   *pOverflow          /* o  : overflow indicator                     */
1131 )
1132 {
1133     const Word16 *p;
1134     Word16 i;
1135     Word16 index = 0;
1136     Word16 tmp;
1137     Word16 exp;
1138     Word16 sf0_gcode0;
1139     Word16 sf1_gcode0;
1140     Word16 g_pitch;
1141     Word16 g2_pitch;
1142     Word16 g_code;
1143     Word16 g2_code;
1144     Word16 g_pit_cod;
1145     Word16 coeff[10];
1146     Word16 coeff_lo[10];
1147     Word16 exp_max[10];  /* 0..4: sf0; 5..9: sf1 */
1148     Word32 L_tmp;
1149     Word32 dist_min;
1150
1151     /*-------------------------------------------------------------------*
1152      *  predicted codebook gain                                          *
1153      *  ~~~~~~~~~~~~~~~~~~~~~~~                                          *
1154      *  gc0     = 2^exp_gcode0 + 2^frac_gcode0                           *
1155      *                                                                   *
1156      *  gcode0 (Q14) = 2^14*2^frac_gcode0 = gc0 * 2^(14-exp_gcode0)      *
1157      *-------------------------------------------------------------------*/
1158
1159     sf0_gcode0 = (Word16)(Pow2(14, sf0_frac_gcode0, pOverflow));
1160     sf1_gcode0 = (Word16)(Pow2(14, sf1_frac_gcode0, pOverflow));
1161
1162     /*
1163      * For each subframe, the error energy (sum) to be minimized consists
1164      * of five terms, t[0..4].
1165      *
1166      *                      t[0] =    gp^2  * <y1 y1>
1167      *                      t[1] = -2*gp    * <xn y1>
1168      *                      t[2] =    gc^2  * <y2 y2>
1169      *                      t[3] = -2*gc    * <xn y2>
1170      *                      t[4] =  2*gp*gc * <y1 y2>
1171      *
1172      */
1173
1174     /* sf 0 */
1175     /* determine the scaling exponent for g_code: ec = ec0 - 11 */
1176     exp = sf0_exp_gcode0 - 11;
1177
1178     /* calculate exp_max[i] = s[i]-1 */
1179     exp_max[0] = (sf0_exp_coeff[0] - 13);
1180     exp_max[1] = (sf0_exp_coeff[1] - 14);
1181     exp_max[2] = (sf0_exp_coeff[2] + (15 + (exp << 1)));
1182     exp_max[3] = (sf0_exp_coeff[3] + exp);
1183     exp_max[4] = (sf0_exp_coeff[4] + (1 + exp));
1184
1185     /* sf 1 */
1186     /* determine the scaling exponent for g_code: ec = ec0 - 11 */
1187     exp = sf1_exp_gcode0 - 11;
1188
1189     /* calculate exp_max[i] = s[i]-1 */
1190     exp_max[5] = (sf1_exp_coeff[0] - 13);
1191     exp_max[6] = (sf1_exp_coeff[1] - 14);
1192     exp_max[7] = (sf1_exp_coeff[2] + (15 + (exp << 1)));
1193     exp_max[8] = (sf1_exp_coeff[3] + exp);
1194     exp_max[9] = (sf1_exp_coeff[4] + (1 + exp));
1195
1196     /*-------------------------------------------------------------------*
1197      *  Gain search equalisation:                                        *
1198      *  ~~~~~~~~~~~~~~~~~~~~~~~~~                                        *
1199      *  The MSE for the two subframes is weighted differently if there   *
1200      *  is a big difference in the corresponding target energies         *
1201      *-------------------------------------------------------------------*/
1202
1203     /* make the target energy exponents the same by de-normalizing the
1204        fraction of the smaller one. This is necessary to be able to compare
1205        them
1206      */
1207     exp = sf0_exp_target_en - sf1_exp_target_en;
1208     if (exp > 0)
1209     {
1210         sf1_frac_target_en >>= exp;
1211     }
1212     else
1213     {
1214         sf0_frac_target_en >>= (-exp);
1215     }
1216
1217     /* assume no change of exponents */
1218     exp = 0;
1219
1220     /* test for target energy difference; set exp to +1 or -1 to scale
1221      * up/down coefficients for sf 1
1222      */
1223     tmp = shr_r(sf1_frac_target_en, 1, pOverflow);  /* tmp = ceil(0.5*en(sf1)) */
1224
1225     if (tmp > sf0_frac_target_en)          /* tmp > en(sf0)? */
1226     {
1227         /*
1228          * target_energy(sf1) > 2*target_energy(sf0)
1229          *   -> scale up MSE(sf0) by 2 by adding 1 to exponents 0..4
1230          */
1231         exp = 1;
1232     }
1233     else
1234     {
1235         tmp = ((sf0_frac_target_en + 3) >> 2); /* tmp=ceil(0.25*en(sf0)) */
1236
1237         if (tmp > sf1_frac_target_en)      /* tmp > en(sf1)? */
1238         {
1239             /*
1240              * target_energy(sf1) < 0.25*target_energy(sf0)
1241              *   -> scale down MSE(sf0) by 0.5 by subtracting 1 from
1242              *      coefficients 0..4
1243              */
1244             exp = -1;
1245         }
1246     }
1247
1248     for (i = 0; i < 5; i++)
1249     {
1250         exp_max[i] += exp;
1251     }
1252
1253     /*-------------------------------------------------------------------*
1254      *  Find maximum exponent:                                           *
1255      *  ~~~~~~~~~~~~~~~~~~~~~~                                           *
1256      *                                                                   *
1257      *  For the sum operation, all terms must have the same scaling;     *
1258      *  that scaling should be low enough to prevent overflow. There-    *
1259      *  fore, the maximum scale is determined and all coefficients are   *
1260      *  re-scaled:                                                       *
1261      *                                                                   *
1262      *    exp = max(exp_max[i]) + 1;                                     *
1263      *    e = exp_max[i]-exp;         e <= 0!                            *
1264      *    c[i] = c[i]*2^e                                                *
1265      *-------------------------------------------------------------------*/
1266
1267     exp = exp_max[0];
1268     for (i = 9; i > 0; i--)
1269     {
1270         if (exp_max[i] > exp)
1271         {
1272             exp = exp_max[i];
1273         }
1274     }
1275     exp++;      /* To avoid overflow */
1276
1277     p = &sf0_frac_coeff[0];
1278     for (i = 0; i < 5; i++)
1279     {
1280         tmp = (exp - exp_max[i]);
1281         L_tmp = ((Word32)(*p++) << 16);
1282         L_tmp = L_shr(L_tmp, tmp, pOverflow);
1283         coeff[i] = (Word16)(L_tmp >> 16);
1284         coeff_lo[i] = (Word16)((L_tmp >> 1) - ((L_tmp >> 16) << 15));
1285     }
1286     p = &sf1_frac_coeff[0];
1287     for (; i < 10; i++)
1288     {
1289         tmp = exp - exp_max[i];
1290         L_tmp = ((Word32)(*p++) << 16);
1291         L_tmp = L_shr(L_tmp, tmp, pOverflow);
1292         coeff[i] = (Word16)(L_tmp >> 16);
1293         coeff_lo[i] = (Word16)((L_tmp >> 1) - ((L_tmp >> 16) << 15));
1294     }
1295
1296
1297     /*-------------------------------------------------------------------*
1298      *  Codebook search:                                                 *
1299      *  ~~~~~~~~~~~~~~~~                                                 *
1300      *                                                                   *
1301      *  For each pair (g_pitch, g_fac) in the table calculate the        *
1302      *  terms t[0..4] and sum them up; the result is the mean squared    *
1303      *  error for the quantized gains from the table. The index for the  *
1304      *  minimum MSE is stored and finally used to retrieve the quantized *
1305      *  gains                                                            *
1306      *-------------------------------------------------------------------*/
1307
1308     /* start with "infinite" MSE */
1309     dist_min = MAX_32;
1310
1311     p = &table_gain_MR475[0];
1312
1313     for (i = 0; i < MR475_VQ_SIZE; i++)
1314     {
1315         /* subframe 0 (and 2) calculations */
1316         g_pitch = *p++;
1317         g_code = *p++;
1318
1319         /* Need to be there OKA */
1320         g_code    = (Word16)(((Word32) g_code * sf0_gcode0) >> 15);
1321         g2_pitch  = (Word16)(((Word32) g_pitch * g_pitch) >> 15);
1322         g2_code   = (Word16)(((Word32) g_code * g_code) >> 15);
1323         g_pit_cod = (Word16)(((Word32) g_code * g_pitch) >> 15);
1324
1325
1326         L_tmp = Mpy_32_16(coeff[0], coeff_lo[0], g2_pitch, pOverflow) +
1327                 Mpy_32_16(coeff[1], coeff_lo[1], g_pitch, pOverflow) +
1328                 Mpy_32_16(coeff[2], coeff_lo[2], g2_code, pOverflow) +
1329                 Mpy_32_16(coeff[3], coeff_lo[3], g_code, pOverflow) +
1330                 Mpy_32_16(coeff[4], coeff_lo[4], g_pit_cod, pOverflow);
1331
1332         tmp = (g_pitch - gp_limit);
1333
1334         /* subframe 1 (and 3) calculations */
1335         g_pitch = *p++;
1336         g_code = *p++;
1337
1338         if ((tmp <= 0) && (g_pitch <= gp_limit))
1339         {
1340             g_code = (Word16)(((Word32) g_code * sf1_gcode0) >> 15);
1341             g2_pitch  = (Word16)(((Word32) g_pitch * g_pitch) >> 15);
1342             g2_code   = (Word16)(((Word32) g_code * g_code) >> 15);
1343             g_pit_cod = (Word16)(((Word32) g_code * g_pitch) >> 15);
1344
1345             L_tmp += (Mpy_32_16(coeff[5], coeff_lo[5], g2_pitch, pOverflow) +
1346                       Mpy_32_16(coeff[6], coeff_lo[6], g_pitch, pOverflow) +
1347                       Mpy_32_16(coeff[7], coeff_lo[7], g2_code, pOverflow) +
1348                       Mpy_32_16(coeff[8], coeff_lo[8], g_code, pOverflow) +
1349                       Mpy_32_16(coeff[9], coeff_lo[9], g_pit_cod, pOverflow));
1350
1351             /* store table index if MSE for this index is lower
1352                than the minimum MSE seen so far */
1353             if (L_tmp < dist_min)
1354             {
1355                 dist_min = L_tmp;
1356                 index = i;
1357             }
1358         }
1359     }
1360
1361     /*------------------------------------------------------------------*
1362      *  read quantized gains and update MA predictor memories           *
1363      *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~           *
1364      *------------------------------------------------------------------*/
1365
1366     /* for subframe 0, the pre-calculated gcode0/exp_gcode0 are the same
1367        as those calculated from the "real" predictor using quantized gains */
1368     tmp = index << 2;
1369     MR475_quant_store_results(pred_st,
1370                               &table_gain_MR475[tmp],
1371                               sf0_gcode0,
1372                               sf0_exp_gcode0,
1373                               sf0_gain_pit,
1374                               sf0_gain_cod,
1375                               pOverflow);
1376
1377     /* calculate new predicted gain for subframe 1 (this time using
1378        the real, quantized gains)                                   */
1379     gc_pred(pred_st, MR475, sf1_code_nosharp,
1380             &sf1_exp_gcode0, &sf1_frac_gcode0,
1381             &sf0_exp_gcode0, &sf0_gcode0, /* dummy args */
1382             pOverflow);
1383
1384     sf1_gcode0 = (Word16)(Pow2(14, sf1_frac_gcode0, pOverflow));
1385
1386     tmp += 2;
1387     MR475_quant_store_results(
1388         pred_st,
1389         &table_gain_MR475[tmp],
1390         sf1_gcode0,
1391         sf1_exp_gcode0,
1392         sf1_gain_pit,
1393         sf1_gain_cod,
1394         pOverflow);
1395
1396     return(index);
1397 }