summaryrefslogtreecommitdiffstats
path: root/media/libstagefright/codecs/amrnb/common/src/q_plsf_3.cpp
blob: c70847e21fc3825e25b1f5d29427409a0d407f15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
/* ------------------------------------------------------------------
 * Copyright (C) 1998-2009 PacketVideo
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 * -------------------------------------------------------------------
 */
/****************************************************************************************
Portions of this file are derived from the following 3GPP standard:

    3GPP TS 26.073
    ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec
    Available from http://www.3gpp.org

(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC)
Permission to distribute, modify and use this file under the standard license
terms listed above has been obtained from the copyright holder.
****************************************************************************************/
/*

 Pathname: ./audio/gsm-amr/c/src/q_plsf_3.c
 Funtions: Vq_subvec4
           Test_Vq_subvec4
           Vq_subvec3
           Test_Vq_subvec3
           Q_plsf_3

------------------------------------------------------------------------------
 REVISION HISTORY

 Description: Updated template used to PV coding template. First attempt at
          optimizing C code.

 Description: Updated modules per Phase 2/3 review comments. Updated
          Vq_subvec3 pseudo-code to reflect the new restructured code.

 Description: Added setting of Overflow flag in inlined code.

 Description: Synchronized file with UMTS version 3.2.0. Updated coding
              template. Removed unnecessary include files.

 Description: Replaced basic_op.h with the header file of the math functions
              used in the file.

 Description: Made the following changes per comments from Phase 2/3 review:
              1. Fixed typecasting issue with TI C compiler.
              2. Optimized IF stament in Vq_subvec3() function.
              3. Updated copyright year.

 Description: Removed redundancy in the Vq_subvec4 function.

 Description: Updated to accept new parameter, Flag *pOverflow.

 Description: Per review comments, added pOverflow flag description
 to the input/outputs section.

 Description: Corrected missed Overflow global variables -- changed to
 proper pOverflow.

 Description: Optimized all functions to further reduce clock cycle usage.
              Updated copyright year.

 Description: Added left shift by 1 in line 1050 of Q_plsf_3().

 Description:  Replaced OSCL mem type functions and eliminated include
               files that now are chosen by OSCL definitions

 Description:  Replaced "int" and/or "char" with OSCL defined types.

 Description: Added #ifdef __cplusplus around extern'ed table.

 Who:                           Date:
 Description:

------------------------------------------------------------------------------
 MODULE DESCRIPTION

 This file contains the functions that perform the quantization of LSF
 parameters with first order MA prediction and split by 3 vector
 quantization (split-VQ).

------------------------------------------------------------------------------
*/

/*----------------------------------------------------------------------------
; INCLUDES
----------------------------------------------------------------------------*/

#include <string.h>

#include "q_plsf.h"
#include "typedef.h"
#include "lsp_lsf.h"
#include "reorder.h"
#include "lsfwt.h"

/*--------------------------------------------------------------------------*/
#ifdef __cplusplus
extern "C"
{
#endif

    /*----------------------------------------------------------------------------
    ; MACROS
    ; Define module specific macros here
    ----------------------------------------------------------------------------*/

    /*----------------------------------------------------------------------------
    ; DEFINES
    ; Include all pre-processor statements here. Include conditional
    ; compile variables also.
    ----------------------------------------------------------------------------*/
#define PAST_RQ_INIT_SIZE 8

    /*----------------------------------------------------------------------------
    ; LOCAL FUNCTION DEFINITIONS
    ; Function Prototype declaration
    ----------------------------------------------------------------------------*/

    /*----------------------------------------------------------------------------
    ; LOCAL VARIABLE DEFINITIONS
    ; Variable declaration - defined here and used outside this module
    ----------------------------------------------------------------------------*/

    /*----------------------------------------------------------------------------
    ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
    ; Declare variables used in this module but defined elsewhere
    ----------------------------------------------------------------------------*/
    /* Codebooks of LSF prediction residual */
    extern const Word16 mean_lsf_3[];

    extern const Word16 pred_fac_3[];

    extern const Word16 dico1_lsf_3[];
    extern const Word16 dico2_lsf_3[];
    extern const Word16 dico3_lsf_3[];

    extern const Word16 mr515_3_lsf[];
    extern const Word16 mr795_1_lsf[];

    extern const Word16 past_rq_init[];

    /*--------------------------------------------------------------------------*/
#ifdef __cplusplus
}
#endif

/*
------------------------------------------------------------------------------
 FUNCTION NAME: Vq_subvec4
------------------------------------------------------------------------------
 INPUT AND OUTPUT DEFINITIONS

 Inputs:
    lsf_r1 = pointer to the first LSF residual vector (Q15) (Word16)
    dico = pointer to the quantization codebook (Q15) (const Word16)
    wf1 = pointer to the first LSF weighting factor (Q13) (Word16)
    dico_size = size of quantization codebook (Q0) (Word16)

 Outputs:
    buffer pointed to by lsf_r1 contains the selected vector
    pOverflow -- pointer to Flag -- Flag set when overflow occurs

 Returns:
    index = quantization index (Q0) (Word16)

 Global Variables Used:
    None

 Local Variables Needed:
    None

------------------------------------------------------------------------------
 FUNCTION DESCRIPTION

 This function performs the quantization of a 4-dimensional subvector.

------------------------------------------------------------------------------
 REQUIREMENTS

 None

------------------------------------------------------------------------------
 REFERENCES

 q_plsf_3.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001

------------------------------------------------------------------------------
 PSEUDO-CODE

static Word16
Vq_subvec4(             // o: quantization index,            Q0
    Word16 * lsf_r1,    // i: 1st LSF residual vector,       Q15
    Word16 * dico,      // i: quantization codebook,         Q15
    Word16 * wf1,       // i: 1st LSF weighting factors,     Q13
    Word16 dico_size)   // i: size of quantization codebook, Q0
{
    Word16 i, index = 0;
    Word16 *p_dico, temp;
    Word32 dist_min, dist;

    dist_min = MAX_32;
    p_dico = dico;

    for (i = 0; i < dico_size; i++)
    {
        temp = sub (lsf_r1[0], *p_dico++);
        temp = mult (wf1[0], temp);
        dist = L_mult (temp, temp);

        temp = sub (lsf_r1[1], *p_dico++);
        temp = mult (wf1[1], temp);
        dist = L_mac (dist, temp, temp);

        temp = sub (lsf_r1[2], *p_dico++);
        temp = mult (wf1[2], temp);
        dist = L_mac (dist, temp, temp);

        temp = sub (lsf_r1[3], *p_dico++);
        temp = mult (wf1[3], temp);
        dist = L_mac (dist, temp, temp);


        if (L_sub (dist, dist_min) < (Word32) 0)
        {
            dist_min = dist;
            index = i;
        }
    }

    // Reading the selected vector

    p_dico = &dico[shl (index, 2)];
    lsf_r1[0] = *p_dico++;
    lsf_r1[1] = *p_dico++;
    lsf_r1[2] = *p_dico++;
    lsf_r1[3] = *p_dico;

    return index;

}

------------------------------------------------------------------------------
 RESOURCES USED [optional]

 When the code is written for a specific target processor the
 the resources used should be documented below.

 HEAP MEMORY USED: x bytes

 STACK MEMORY USED: x bytes

 CLOCK CYCLES: (cycle count equation for this function) + (variable
                used to represent cycle count for each subroutine
                called)
     where: (cycle count variable) = cycle count for [subroutine
                                     name]

------------------------------------------------------------------------------
 CAUTION [optional]
 [State any special notes, constraints or cautions for users of this function]

------------------------------------------------------------------------------
*/

static Word16 Vq_subvec4( /* o: quantization index,            Q0  */
    Word16 * lsf_r1,      /* i: 1st LSF residual vector,       Q15 */
    const Word16 * dico,  /* i: quantization codebook,         Q15 */
    Word16 * wf1,         /* i: 1st LSF weighting factors,     Q13 */
    Word16 dico_size,     /* i: size of quantization codebook, Q0  */
    Flag  *pOverflow      /* o : Flag set when overflow occurs     */
)
{
    Word16 i;
    Word16 temp;
    const Word16 *p_dico;
    Word16 index = 0;
    Word32 dist_min;
    Word32 dist;

    Word16 lsf_r1_0;
    Word16 lsf_r1_1;
    Word16 lsf_r1_2;
    Word16 lsf_r1_3;

    Word16 wf1_0;
    Word16 wf1_1;
    Word16 wf1_2;
    Word16 wf1_3;

    OSCL_UNUSED_ARG(pOverflow);

    dist_min = MAX_32;
    p_dico = dico;

    lsf_r1_0 = lsf_r1[0];
    lsf_r1_1 = lsf_r1[1];
    lsf_r1_2 = lsf_r1[2];
    lsf_r1_3 = lsf_r1[3];

    wf1_0 = wf1[0];
    wf1_1 = wf1[1];
    wf1_2 = wf1[2];
    wf1_3 = wf1[3];

    for (i = 0; i < dico_size; i++)
    {
        temp = lsf_r1_0 - (*p_dico++);
        temp = (Word16)((((Word32) wf1_0) * temp) >> 15);
        dist = ((Word32) temp) * temp;

        temp = lsf_r1_1 - (*p_dico++);
        temp = (Word16)((((Word32) wf1_1) * temp) >> 15);
        dist += ((Word32) temp) * temp;

        temp = lsf_r1_2 - (*p_dico++);
        temp = (Word16)((((Word32) wf1_2) * temp) >> 15);
        dist += ((Word32) temp) * temp;

        temp = lsf_r1_3 - (*p_dico++);
        temp = (Word16)((((Word32) wf1_3) * temp) >> 15);
        dist += ((Word32) temp) * temp;

        if (dist < dist_min)
        {
            dist_min = dist;
            index = i;
        }
    }

    /* Reading the selected vector */

    p_dico = dico + (index << 2);
    *lsf_r1++ = *p_dico++;
    *lsf_r1++ = *p_dico++;
    *lsf_r1++ = *p_dico++;
    *lsf_r1 = *p_dico;

    return(index);

}

/****************************************************************************/


/*
------------------------------------------------------------------------------
 FUNCTION NAME: Test_Vq_subvec4
------------------------------------------------------------------------------
 INPUT AND OUTPUT DEFINITIONS

 Inputs:
    lsf_r1 = pointer to the first LSF residual vector (Q15) (Word16)
    dico = pointer to the quantization codebook (Q15) (const Word16)
    wf1 = pointer to the first LSF weighting factor (Q13) (Word16)
    dico_size = size of quantization codebook (Q0) (Word16)

 Outputs:
    buffer pointed to by lsf_r1 contains the selected vector
    pOverflow -- pointer to Flag -- Flag set when overflow occurs

 Returns:
    index = quantization index (Q0) (Word16)

 Global Variables Used:
    None

 Local Variables Needed:
    None

------------------------------------------------------------------------------
 FUNCTION DESCRIPTION

 This function calls the static function Vq_subvec4. It is used for testing
 purposes only

------------------------------------------------------------------------------
 REQUIREMENTS

 None

------------------------------------------------------------------------------
 REFERENCES

 None

------------------------------------------------------------------------------
 PSEUDO-CODE


 CALL Vq_subvec4(lsf_r1 = lsf_r1
                 dico = dico
                 wf1 = wf1
                 dico_size = dico_size)
   MODIFYING(nothing)
   RETURNING(index = tst_index4)

------------------------------------------------------------------------------
 RESOURCES USED [optional]

 When the code is written for a specific target processor the
 the resources used should be documented below.

 HEAP MEMORY USED: x bytes

 STACK MEMORY USED: x bytes

 CLOCK CYCLES: (cycle count equation for this function) + (variable
                used to represent cycle count for each subroutine
                called)
     where: (cycle count variable) = cycle count for [subroutine
                                     name]

------------------------------------------------------------------------------
 CAUTION [optional]
 [State any special notes, constraints or cautions for users of this function]

------------------------------------------------------------------------------
*/

Word16 Test_Vq_subvec4(
    Word16 * lsf_r1,
    const Word16 * dico,
    Word16 * wf1,
    Word16 dico_size,
    Flag   *pOverflow)
{
    Word16  tst_index4 = 0;

    /*------------------------------------------------------------------------
     CALL Vq_subvec4(lsf_r1 = lsf_r1
                     dico = dico
                     wf1 = wf1
                     dico_size = dico_size)
       MODIFYING(nothing)
       RETURNING(index = index)
    ------------------------------------------------------------------------*/
    tst_index4 =
        Vq_subvec4(
            lsf_r1,
            dico,
            wf1,
            dico_size,
            pOverflow);

    return(tst_index4);

}

/****************************************************************************/

/*
------------------------------------------------------------------------------
 FUNCTION NAME: Vq_subvec3
------------------------------------------------------------------------------
 INPUT AND OUTPUT DEFINITIONS

 Inputs:
    lsf_r1 = pointer to the first LSF residual vector (Q15) (Word16)
    dico = pointer to the quantization codebook (Q15) (const Word16)
    wf1 = pointer to the first LSF weighting factor (Q13) (Word16)
    dico_size = size of quantization codebook (Q0) (Word16)
    use_half = flag to indicate use of every second entry in the
               codebook (Flag)

 Outputs:
    buffer pointed to by lsf_r1 contains the selected vector
    pOverflow -- pointer to Flag -- Flag set when overflow occurs

 Returns:
    index = quantization index (Q0) (Word16)

 Global Variables Used:
    None

 Local Variables Needed:
    None

------------------------------------------------------------------------------
 FUNCTION DESCRIPTION

 This function performs the quantization of a 3 dimensional subvector.

------------------------------------------------------------------------------
 REQUIREMENTS

 None

------------------------------------------------------------------------------
 REFERENCES

 q_plsf_3.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001

------------------------------------------------------------------------------
 PSEUDO-CODE

static Word16
Vq_subvec3(             // o: quantization index,            Q0
    Word16 * lsf_r1,    // i: 1st LSF residual vector,       Q15
    Word16 * dico,      // i: quantization codebook,         Q15
    Word16 * wf1,       // i: 1st LSF weighting factors,     Q13
    Word16 dico_size,   // i: size of quantization codebook, Q0
    Flag use_half)      // i: use every second entry in codebook
{
    Word16 i, index = 0;
    Word16 *p_dico, temp;
    Word32 dist_min, dist;

    dist_min = MAX_32;
    p_dico = dico;

    if (use_half == 0) {
       for (i = 0; i < dico_size; i++)
       {
          temp = sub(lsf_r1[0], *p_dico++);
          temp = mult(wf1[0], temp);
          dist = L_mult(temp, temp);

          temp = sub(lsf_r1[1], *p_dico++);
          temp = mult(wf1[1], temp);
          dist = L_mac(dist, temp, temp);

          temp = sub(lsf_r1[2], *p_dico++);
          temp = mult(wf1[2], temp);
          dist = L_mac(dist, temp, temp);

          if (L_sub(dist, dist_min) < (Word32) 0) {
             dist_min = dist;
             index = i;
          }
       }
       p_dico = &dico[add(index, add(index, index))];
    }
    else
    {
       for (i = 0; i < dico_size; i++)
       {
          temp = sub(lsf_r1[0], *p_dico++);
          temp = mult(wf1[0], temp);
          dist = L_mult(temp, temp);

          temp = sub(lsf_r1[1], *p_dico++);
          temp = mult(wf1[1], temp);
          dist = L_mac(dist, temp, temp);

          temp = sub(lsf_r1[2], *p_dico++);
          temp = mult(wf1[2], temp);
          dist = L_mac(dist, temp, temp);

          if (L_sub(dist, dist_min) < (Word32) 0)
          {
             dist_min = dist;
             index = i;
          }
          p_dico = p_dico + 3; add(0,0);
       }
       p_dico = &dico[shl(add(index, add(index, index)),1)];
    }


    // Reading the selected vector
    lsf_r1[0] = *p_dico++;
    lsf_r1[1] = *p_dico++;
    lsf_r1[2] = *p_dico++;

    return index;
}

------------------------------------------------------------------------------
 RESOURCES USED [optional]

 When the code is written for a specific target processor the
 the resources used should be documented below.

 HEAP MEMORY USED: x bytes

 STACK MEMORY USED: x bytes

 CLOCK CYCLES: (cycle count equation for this function) + (variable
                used to represent cycle count for each subroutine
                called)
     where: (cycle count variable) = cycle count for [subroutine
                                     name]

------------------------------------------------------------------------------
 CAUTION [optional]
 [State any special notes, constraints or cautions for users of this function]

------------------------------------------------------------------------------
*/

static Word16 Vq_subvec3( /* o: quantization index,            Q0  */
    Word16 * lsf_r1,      /* i: 1st LSF residual vector,       Q15 */
    const Word16 * dico,  /* i: quantization codebook,         Q15 */
    Word16 * wf1,         /* i: 1st LSF weighting factors,     Q13 */
    Word16 dico_size,     /* i: size of quantization codebook, Q0  */
    Flag use_half,        /* i: use every second entry in codebook */
    Flag  *pOverflow)     /* o : Flag set when overflow occurs     */
{
    Word16 i;
    Word16 temp;

    const Word16 *p_dico;

    Word16 p_dico_index = 0;
    Word16 index = 0;

    Word32 dist_min;
    Word32 dist;

    Word16 lsf_r1_0;
    Word16 lsf_r1_1;
    Word16 lsf_r1_2;

    Word16 wf1_0;
    Word16 wf1_1;
    Word16 wf1_2;

    OSCL_UNUSED_ARG(pOverflow);

    dist_min = MAX_32;
    p_dico = dico;

    lsf_r1_0 = lsf_r1[0];
    lsf_r1_1 = lsf_r1[1];
    lsf_r1_2 = lsf_r1[2];

    wf1_0 = wf1[0];
    wf1_1 = wf1[1];
    wf1_2 = wf1[2];

    if (use_half != 0)
    {
        p_dico_index = 3;
    }

    for (i = 0; i < dico_size; i++)
    {
        temp = lsf_r1_0 - (*p_dico++);
        temp = (Word16)((((Word32) wf1_0) * temp) >> 15);
        dist = ((Word32) temp) * temp;

        temp = lsf_r1_1 - (*p_dico++);
        temp = (Word16)((((Word32) wf1_1) * temp) >> 15);
        dist += ((Word32) temp) * temp;

        temp = lsf_r1_2 - (*p_dico++);
        temp = (Word16)((((Word32) wf1_2) * temp) >> 15);
        dist += ((Word32) temp) * temp;

        if (dist < dist_min)
        {
            dist_min = dist;
            index = i;
        }

        p_dico = p_dico + p_dico_index;
    }

    p_dico = dico + (3 * index);

    if (use_half != 0)
    {
        p_dico += (3 * index);
    }

    /* Reading the selected vector */
    *lsf_r1++ = *p_dico++;
    *lsf_r1++ = *p_dico++;
    *lsf_r1 = *p_dico;

    return(index);
}

/****************************************************************************/


/*
------------------------------------------------------------------------------
 FUNCTION NAME: Test_Vq_subvec3
------------------------------------------------------------------------------
 INPUT AND OUTPUT DEFINITIONS

 Inputs:
    lsf_r1 = pointer to the first LSF residual vector (Q15) (Word16)
    dico = pointer to the quantization codebook (Q15) (const Word16)
    wf1 = pointer to the first LSF weighting factor (Q13) (Word16)
    dico_size = size of quantization codebook (Q0) (Word16)
    use_half = flag to indicate use of every second entry in the
               codebook (Flag)

 Outputs:
    buffer pointed to by lsf_r1 contains the selected vector
    pOverflow -- pointer to Flag -- Flag set when overflow occurs

 Returns:
    index = quantization index (Q0) (Word16)

 Global Variables Used:
    None

 Local Variables Needed:
    None

------------------------------------------------------------------------------
 FUNCTION DESCRIPTION

 This function calls the static function Vq_subvec3. It is used for testing
 purposes only

------------------------------------------------------------------------------
 REQUIREMENTS

 None

------------------------------------------------------------------------------
 REFERENCES

 None

------------------------------------------------------------------------------
 PSEUDO-CODE

 CALL Vq_subvec3(lsf_r1 = lsf_r1
                 dico = dico
                 wf1 = wf1
                 dico_size = dico_size
                 use_half = use_half)
   MODIFYING(nothing)
   RETURNING(index = tst_index3)

------------------------------------------------------------------------------
 RESOURCES USED [optional]

 When the code is written for a specific target processor the
 the resources used should be documented below.

 HEAP MEMORY USED: x bytes

 STACK MEMORY USED: x bytes

 CLOCK CYCLES: (cycle count equation for this function) + (variable
                used to represent cycle count for each subroutine
                called)
     where: (cycle count variable) = cycle count for [subroutine
                                     name]

------------------------------------------------------------------------------
 CAUTION [optional]
 [State any special notes, constraints or cautions for users of this function]

------------------------------------------------------------------------------
*/

Word16 Test_Vq_subvec3(
    Word16 * lsf_r1,
    const Word16 * dico,
    Word16 * wf1,
    Word16 dico_size,
    Flag use_half,
    Flag *pOverflow)
{
    Word16  tst_index3 = 0;

    /*------------------------------------------------------------------------
     CALL Vq_subvec3(lsf_r1 = lsf_r1
                     dico = dico
                     wf1 = wf1
                     dico_size = dico_size
                     use_half = use_half)
       MODIFYING(nothing)
       RETURNING(index = index)
    ------------------------------------------------------------------------*/
    tst_index3 =
        Vq_subvec3(
            lsf_r1,
            dico,
            wf1,
            dico_size,
            use_half,
            pOverflow);

    return(tst_index3);

}

/****************************************************************************/


/*
------------------------------------------------------------------------------
 FUNCTION NAME: Q_plsf_3
------------------------------------------------------------------------------
 INPUT AND OUTPUT DEFINITIONS

 Inputs:
    st = pointer to structures of type Q_plsfState (Q_plsfState)
    mode = coder mode (enum)
    lsp1 = pointer to the first LSP vector (Word16)
    lsp1_q = pointer to the quantized first LSP vector (Word16)
    indice = pointer to the quantization indices of 3 vectors (Word16)
    pred_init_i = pointer to the index of the initial value for
                  MA prediction in DTX mode (Word16)

 Outputs:
    lsp1_q points to a vector containing the new quantized LSPs
    indice points to the new quantization indices of 3 vectors
    pred_init_i points to the new initial index for MA prediction
      in DTX mode
    past_rq field of structure pointed to by st contains the current
      quantized LSF parameters
    pOverflow -- pointer to Flag -- Flag set when overflow occurs

 Returns:
    None

 Global Variables Used:
    pred_fac = table containing prediction factors (const Word16)
    dico1_lsf = quantization table for split_MQ of 2 sets of LSFs
                in a 20 ms frame (const Word16)
    dico2_lsf = quantization table for split_MQ of 2 sets of LSFs
                in a 20 ms frame (const Word16)
    dico3_lsf = quantization table for split_MQ of 2 sets of LSFs
                in a 20 ms frame (const Word16)
    mr515_3_lsf = third codebook for MR475 and MR515 modes (const Word16)
    mr795_1_lsf = first codebook for MR795 mode (const Word16)
    mean_lsf = table of mean LSFs (const Word16)
    past_rq_init = initalization table for MA predictor in DTX mode
                   (const Word16)


 Local Variables Needed:
    None

------------------------------------------------------------------------------
 FUNCTION DESCRIPTION

 This function performs quantization of LSF parameters with 1st order MA
 prediction and split by 3 vector quantization (split-VQ)

------------------------------------------------------------------------------
 REQUIREMENTS

 None

------------------------------------------------------------------------------
 REFERENCES

 q_plsf_3.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001

------------------------------------------------------------------------------
 PSEUDO-CODE

void Q_plsf_3(
    Q_plsfState *st,    // i/o: state struct
    enum Mode mode,     // i  : coder mode
    Word16 *lsp1,       // i  : 1st LSP vector                      Q15
    Word16 *lsp1_q,     // o  : quantized 1st LSP vector            Q15
    Word16 *indice,     // o  : quantization indices of 3 vectors   Q0
    Word16 *pred_init_i // o  : init index for MA prediction in DTX mode
)
{
    Word16 i, j;
    Word16 lsf1[M], wf1[M], lsf_p[M], lsf_r1[M];
    Word16 lsf1_q[M];

    Word32 L_pred_init_err;
    Word32 L_min_pred_init_err;
    Word16 temp_r1[M];
    Word16 temp_p[M];

    // convert LSFs to normalize frequency domain 0..16384

    Lsp_lsf(lsp1, lsf1, M);

    // compute LSF weighting factors (Q13)

    Lsf_wt(lsf1, wf1);

    // Compute predicted LSF and prediction error
    if (test(), sub(mode, MRDTX) != 0)
    {
       for (i = 0; i < M; i++)
       {
          lsf_p[i] = add(mean_lsf[i],
                         mult(st->past_rq[i],
                              pred_fac[i]));
          lsf_r1[i] = sub(lsf1[i], lsf_p[i]);
      }
    }
    else
    {
       // DTX mode, search the init vector that yields
       // lowest prediction resuidual energy
       *pred_init_i = 0;
       L_min_pred_init_err = 0x7fffffff; // 2^31 - 1
       for (j = 0; j < PAST_RQ_INIT_SIZE; j++)
       {
          L_pred_init_err = 0;
          for (i = 0; i < M; i++)
          {
             temp_p[i] = add(mean_lsf[i], past_rq_init[j*M+i]);
             temp_r1[i] = sub(lsf1[i],temp_p[i]);
             L_pred_init_err = L_mac(L_pred_init_err, temp_r1[i], temp_r1[i]);
          }  // next i


          if (L_sub(L_pred_init_err, L_min_pred_init_err) < (Word32) 0)
          {
             L_min_pred_init_err = L_pred_init_err;
             Copy(temp_r1, lsf_r1, M);
             Copy(temp_p, lsf_p, M);
             // Set zerom
             Copy(&past_rq_init[j*M], st->past_rq, M);
             *pred_init_i = j;
          } // endif
       } // next j
    } // endif MRDTX

    //---- Split-VQ of prediction error ----
    if (sub (mode, MR475) == 0 || sub (mode, MR515) == 0)
    {   // MR475, MR515


      indice[0] = Vq_subvec3(&lsf_r1[0], dico1_lsf, &wf1[0], DICO1_SIZE, 0);

      indice[1] = Vq_subvec3(&lsf_r1[3], dico2_lsf, &wf1[3], DICO2_SIZE/2, 1);

      indice[2] = Vq_subvec4(&lsf_r1[6], mr515_3_lsf, &wf1[6], MR515_3_SIZE);

    }
    else if (sub (mode, MR795) == 0)
    {   // MR795


      indice[0] = Vq_subvec3(&lsf_r1[0], mr795_1_lsf, &wf1[0], MR795_1_SIZE, 0);

      indice[1] = Vq_subvec3(&lsf_r1[3], dico2_lsf, &wf1[3], DICO2_SIZE, 0);

      indice[2] = Vq_subvec4(&lsf_r1[6], dico3_lsf, &wf1[6], DICO3_SIZE);

    }
    else
    {   // MR59, MR67, MR74, MR102 , MRDTX


      indice[0] = Vq_subvec3(&lsf_r1[0], dico1_lsf, &wf1[0], DICO1_SIZE, 0);

      indice[1] = Vq_subvec3(&lsf_r1[3], dico2_lsf, &wf1[3], DICO2_SIZE, 0);

      indice[2] = Vq_subvec4(&lsf_r1[6], dico3_lsf, &wf1[6], DICO3_SIZE);

    }


    // Compute quantized LSFs and update the past quantized residual

    for (i = 0; i < M; i++)
    {
        lsf1_q[i] = add(lsf_r1[i], lsf_p[i]);
        st->past_rq[i] = lsf_r1[i];
    }

    // verification that LSFs has mimimum distance of LSF_GAP Hz

    Reorder_lsf(lsf1_q, LSF_GAP, M);

    //  convert LSFs to the cosine domain

    Lsf_lsp(lsf1_q, lsp1_q, M);
}

------------------------------------------------------------------------------
 RESOURCES USED [optional]

 When the code is written for a specific target processor the
 the resources used should be documented below.

 HEAP MEMORY USED: x bytes

 STACK MEMORY USED: x bytes

 CLOCK CYCLES: (cycle count equation for this function) + (variable
                used to represent cycle count for each subroutine
                called)
     where: (cycle count variable) = cycle count for [subroutine
                                     name]

------------------------------------------------------------------------------
 CAUTION [optional]
 [State any special notes, constraints or cautions for users of this function]

------------------------------------------------------------------------------
*/

void Q_plsf_3(
    Q_plsfState *st,    /* i/o: state struct                             */
    enum Mode mode,     /* i  : coder mode                               */
    Word16 *lsp1,       /* i  : 1st LSP vector                      Q15  */
    Word16 *lsp1_q,     /* o  : quantized 1st LSP vector            Q15  */
    Word16 *indice,     /* o  : quantization indices of 3 vectors   Q0   */
    Word16 *pred_init_i,/* o  : init index for MA prediction in DTX mode */
    Flag  *pOverflow    /* o : Flag set when overflow occurs             */
)
{
    Word16 i, j;
    Word16 lsf1[M];
    Word16 wf1[M];
    Word16 lsf_p[M];
    Word16 lsf_r1[M];
    Word16 lsf1_q[M];

    Word32 L_pred_init_err;
    Word32 L_min_pred_init_err;
    Word32 L_temp;
    Word16 temp_r1[M];
    Word16 temp_p[M];
    Word16 temp;

    /* convert LSFs to normalize frequency domain 0..16384 */

    Lsp_lsf(
        lsp1,
        lsf1,
        M,
        pOverflow);

    /* compute LSF weighting factors (Q13) */

    Lsf_wt(
        lsf1,
        wf1,
        pOverflow);

    /* Compute predicted LSF and prediction error */
    if (mode != MRDTX)
    {
        for (i = 0; i < M; i++)
        {
            temp = (Word16)((((Word32) st->past_rq[i]) *
                             (*(pred_fac_3 + i))) >> 15);

            *(lsf_p + i) = *(mean_lsf_3 + i) + temp;

            *(lsf_r1 + i) = *(lsf1 + i) - *(lsf_p + i);
        }
    }
    else
    {
        /* DTX mode, search the init vector that yields */
        /* lowest prediction resuidual energy           */
        *pred_init_i = 0;
        L_min_pred_init_err = 0x7fffffff; /* 2^31 - 1 */

        for (j = 0; j < PAST_RQ_INIT_SIZE; j++)
        {
            L_pred_init_err = 0;
            for (i = 0; i < M; i++)
            {
                *(temp_p + i) = *(mean_lsf_3 + i) + *(past_rq_init + j * M + i);

                *(temp_r1 + i) = *(lsf1 + i) - *(temp_p + i);

                L_temp = ((Word32) * (temp_r1 + i)) * *(temp_r1 + i);

                L_pred_init_err = L_pred_init_err + (L_temp << 1);

            }  /* next i */


            if (L_pred_init_err < L_min_pred_init_err)
            {
                L_min_pred_init_err = L_pred_init_err;

                memcpy(
                    lsf_r1,
                    temp_r1,
                    M*sizeof(Word16));

                memcpy(
                    lsf_p,
                    temp_p,
                    M*sizeof(Word16));

                /* Set zerom */
                memcpy(
                    st->past_rq,
                    &past_rq_init[j*M],
                    M*sizeof(Word16));

                *pred_init_i = j;

            } /* endif */
        } /* next j */
    } /* endif MRDTX */

    /*---- Split-VQ of prediction error ----*/
    if ((mode == MR475) || (mode == MR515))
    {   /* MR475, MR515 */

        *indice =
            Vq_subvec3(
                lsf_r1,
                dico1_lsf_3,
                wf1,
                DICO1_SIZE,
                0,
                pOverflow);

        *(indice + 1) =
            Vq_subvec3(
                lsf_r1 + 3,
                dico2_lsf_3,
                wf1 + 3,
                DICO2_SIZE / 2,
                1,
                pOverflow);

        *(indice + 2) =
            Vq_subvec4(
                lsf_r1 + 6,
                mr515_3_lsf,
                wf1 + 6,
                MR515_3_SIZE,
                pOverflow);

    }
    else if (mode == MR795)
    {   /* MR795 */

        *indice =
            Vq_subvec3(
                lsf_r1,
                mr795_1_lsf,
                wf1,
                MR795_1_SIZE,
                0,
                pOverflow);

        *(indice + 1) =
            Vq_subvec3(
                lsf_r1 + 3,
                dico2_lsf_3,
                wf1 + 3,
                DICO2_SIZE,
                0,
                pOverflow);

        *(indice + 2) =
            Vq_subvec4(
                lsf_r1 + 6,
                dico3_lsf_3,
                wf1 + 6,
                DICO3_SIZE,
                pOverflow);

    }
    else
    {   /* MR59, MR67, MR74, MR102 , MRDTX */

        *indice =
            Vq_subvec3(
                lsf_r1,
                dico1_lsf_3,
                wf1,
                DICO1_SIZE,
                0,
                pOverflow);

        *(indice + 1) =
            Vq_subvec3(
                lsf_r1 + 3,
                dico2_lsf_3,
                wf1 + 3,
                DICO2_SIZE,
                0,
                pOverflow);

        *(indice + 2) =
            Vq_subvec4(
                lsf_r1 + 6,
                dico3_lsf_3,
                wf1 + 6,
                DICO3_SIZE,
                pOverflow);

    }


    /* Compute quantized LSFs and update the past quantized residual */

    for (i = 0; i < M; i++)
    {
        *(lsf1_q + i) = *(lsf_r1 + i) + *(lsf_p + i);
        st->past_rq[i] = *(lsf_r1 + i);
    }

    /* verification that LSFs has mimimum distance of LSF_GAP Hz */

    Reorder_lsf(
        lsf1_q,
        LSF_GAP,
        M,
        pOverflow);

    /*  convert LSFs to the cosine domain */

    Lsf_lsp(
        lsf1_q,
        lsp1_q,
        M,
        pOverflow);

    return;

}