summaryrefslogtreecommitdiffstats
path: root/media/libstagefright/codecs/amrnb/enc/src/c8_31pf.cpp
blob: 07c2efd77cb99ab219ebe8143d3efd279d67ce99 (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
/* ------------------------------------------------------------------
 * 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/c8_31pf.c
 Functions:

     Date: 05/26/2000

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

 Description: Modified to pass overflow flag through to basic math function.
 The flag is passed back to the calling function by pointer reference.

 Description: Optimized file to reduce clock cycle usage. Updated copyright
              year. Removed unnecessary include files and unused #defines.

 Description: Changed round function name to pv_round to avoid conflict with
              round function in C standard library.

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

 Description:

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

 Purpose          : Searches a 31 bit algebraic codebook containing
                  : 8 pulses in a frame of 40 samples.
                  : in the same manner as GSM-EFR
------------------------------------------------------------------------------
*/

/*----------------------------------------------------------------------------
; INCLUDES
----------------------------------------------------------------------------*/
#include "c8_31pf.h"
#include "typedef.h"
#include "cnst.h"
#include "inv_sqrt.h"
#include "cor_h.h"
#include "cor_h_x2.h"
#include "set_sign.h"
#include "s10_8pf.h"
#include "basic_op.h"

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

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

/* define values/representation for output codevector and sign */
#define POS_CODE  8191
#define NEG_CODE  8191
#define POS_SIGN  32767
#define NEG_SIGN  (Word16) (-32768L)

/*----------------------------------------------------------------------------
; 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
----------------------------------------------------------------------------*/

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

 Inputs:
    codvec[]   Array of type Word16 -- position of pulses
    sign[]     Array of type Word16 -- sign of pulses
    h[]        Array of type Word16 -- impulse response of
                                       weighted synthesis filter
 Outputs:
    cod[]       Array of type Word16 -- innovative code vector
    y[]         Array of type Word16 -- filtered innovative code
    sign_indx[] Array of type Word16 -- signs of 4 pulses (signs only)
    pos_indx[]  Array of type Word16 --
                             position index of 8 pulses(position only)

    pOverflow  Pointer to Flag  -- set when overflow occurs

 Returns:
    indx

 Global Variables Used:
    None

 Local Variables Needed:

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


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

 None

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

 [1] c8_31pf.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001

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

------------------------------------------------------------------------------
 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]

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

/*************************************************************************
 *
 *  FUNCTION:  build_code()
 *
 *  PURPOSE: Builds the codeword, the filtered codeword and a
 *   linear uncombined version of  the index of the
 *           codevector, based on the signs and positions of 8  pulses.
 *
 *************************************************************************/

static void build_code(
    Word16 codvec[],    /* i : position of pulses                           */
    Word16 sign[],      /* i : sign of d[n]                                 */
    Word16 cod[],       /* o : innovative code vector                       */
    Word16 h[],         /* i : impulse response of weighted synthesis filter*/
    Word16 y[],         /* o : filtered innovative code                     */
    Word16 sign_indx[], /* o : signs of 4  pulses (signs only)              */
    Word16 pos_indx[],  /* o : position index of 8 pulses(position only)    */
    Flag   * pOverflow  /* o : Flag set when overflow occurs                */
)
{
    Word16 i;
    Word16 j;
    Word16 k;
    Word16 track;
    Word16 sign_index;
    Word16 pos_index;
    Word16 _sign[NB_PULSE];

    Word16 *p0;
    Word16 *p1;
    Word16 *p2;
    Word16 *p3;
    Word16 *p4;
    Word16 *p5;
    Word16 *p6;
    Word16 *p7;

    Word16 *p_cod = &cod[0];
    Word16 *p_codvec = &codvec[0];

    Word32 s;

    for (i = 0; i < L_CODE; i++)
    {
        *(p_cod++) = 0;
    }

    for (i = 0; i < NB_TRACK_MR102; i++)
    {
        pos_indx[i] = -1;
        sign_indx[i] = -1;
    }

    for (k = 0; k < NB_PULSE; k++)
    {
        /* read pulse position */
        i = codvec[k];
        /* read sign           */
        j = sign[i];

        pos_index = i >> 2; /* index = pos/4 */

        track = i & 3;     /* track = pos%4 */

        if (j > 0)
        {
            cod[i] = (Word16)((Word32) cod[i] + POS_CODE);

            _sign[k] = POS_SIGN;
            sign_index = 0;  /* bit=0 -> positive pulse */
        }
        else
        {
            cod[i] = (Word16)((Word32) cod[i] - NEG_CODE);

            _sign[k] = NEG_SIGN;
            sign_index = 1; /* bit=1 => negative pulse */
            /* index = add (index, 8); 1 = negative  old code */
        }

        if (pos_indx[track] < 0)
        {   /* first set first NB_TRACK pulses  */
            pos_indx[track] = pos_index;
            sign_indx[track] = sign_index;
        }
        else
        {   /* 2nd row of pulses , test if positions needs to be switched */
            if (((sign_index ^ sign_indx[track]) & 1) == 0)
            {
                /* sign of 1st pulse == sign of 2nd pulse */

                if (pos_indx[track] <= pos_index)
                {   /* no swap */
                    pos_indx[track + NB_TRACK_MR102] = pos_index;
                }
                else
                {   /* swap*/
                    pos_indx[track + NB_TRACK_MR102] = pos_indx[track];

                    pos_indx[track] = pos_index;
                    sign_indx[track] = sign_index;
                }
            }
            else
            {
                /* sign of 1st pulse != sign of 2nd pulse */

                if (pos_indx[track] <= pos_index)
                {  /*swap*/
                    pos_indx[track + NB_TRACK_MR102] = pos_indx[track];

                    pos_indx[track] = pos_index;
                    sign_indx[track] = sign_index;
                }
                else
                {   /*no swap */
                    pos_indx[track + NB_TRACK_MR102] = pos_index;
                }
            }
        }
    }

    p0 = h - *(p_codvec++);
    p1 = h - *(p_codvec++);
    p2 = h - *(p_codvec++);
    p3 = h - *(p_codvec++);
    p4 = h - *(p_codvec++);
    p5 = h - *(p_codvec++);
    p6 = h - *(p_codvec++);
    p7 = h - *(p_codvec);

    for (i = 0; i < L_CODE; i++)
    {
        s = 0;

        s =
            L_mac(
                s,
                *p0++,
                _sign[0],
                pOverflow);
        s =
            L_mac(
                s,
                *p1++,
                _sign[1],
                pOverflow);
        s =
            L_mac(
                s,
                *p2++,
                _sign[2],
                pOverflow);
        s =
            L_mac(
                s,
                *p3++,
                _sign[3],
                pOverflow);
        s =
            L_mac(
                s,
                *p4++,
                _sign[4],
                pOverflow);
        s =
            L_mac(
                s,
                *p5++,
                _sign[5],
                pOverflow);
        s =
            L_mac(
                s,
                *p6++,
                _sign[6],
                pOverflow);
        s =
            L_mac(
                s,
                *p7++,
                _sign[7],
                pOverflow);

        y[i] =
            pv_round(
                s,
                pOverflow);

    } /* for (i = 0; i < L_CODE; i++) */

} /* build_code */

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

/*
------------------------------------------------------------------------------
 FUNCTION NAME: compress_code()
------------------------------------------------------------------------------
 INPUT AND OUTPUT DEFINITIONS

 Inputs:

 Outputs:

 Returns:
    None

 Global Variables Used:
    None

 Local Variables Needed:

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

 FUNCTION:

 PURPOSE: compression of three indeces [0..9] to one 10 bit index
          minimizing the phase shift of a bit error.

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

 None

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

 [1] c8_31pf.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001

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

------------------------------------------------------------------------------
 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 compress10(
    Word16 pos_indxA,  /* i : signs of 4 pulses (signs only)             */
    Word16 pos_indxB,  /* i : position index of 8 pulses (pos only)      */
    Word16 pos_indxC,  /* i : position and sign of 8 pulses (compressed) */
    Flag  *pOverflow)  /* o : Flag set when overflow occurs              */
{
    Word16 indx;
    Word16 ia;
    Word16 ib;
    Word16 ic;

    Word32 tempWord32;

    OSCL_UNUSED_ARG(pOverflow);

    ia = pos_indxA >> 1;

    ib = pos_indxB >> 1;

    tempWord32 = ((Word32) ib * 5) << 1;

    tempWord32 = tempWord32 >> 1;

    ib = (Word16) tempWord32;

    ic = pos_indxC >> 1;

    tempWord32 = ((Word32) ic * 25) << 1;

    tempWord32 = tempWord32 >> 1;

    ic = (Word16) tempWord32;

    ib += ic;

    ib += ia;

    indx = ib << 3;

    ia = pos_indxA & 1;

    ib = ((Word16)(pos_indxB & 1)) << 1;

    ic = ((Word16)(pos_indxC & 1)) << 2;

    ib += ic;

    ib += ia;

    indx += ib;

    return indx;

}

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

/*
------------------------------------------------------------------------------
 FUNCTION NAME: compress_code()
------------------------------------------------------------------------------
 INPUT AND OUTPUT DEFINITIONS

 Inputs:
    sign_indx   Array of type Word16 -- signs of 4 pulses (signs only)
    pos_indx    Array of type Word16 -- position index of 8 pulses
                                            (position only)

 Outputs:
    indx         Array of type Word16 -- position and sign of 8 pulses
                                            (compressed)
    pOverflow    Pointer to Flag      -- set when overflow occurs

 Returns:
    None

 Global Variables Used:
    None

 Local Variables Needed:
    None

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

 PURPOSE: compression of the linear codewords to 4+three indeces
          one bit from each pulse is made robust to errors by
          minimizing the phase shift of a bit error.
          4 signs (one for each track)
          i0,i4,i1 => one index (7+3) bits, 3   LSBs more robust
          i2,i6,i5 => one index (7+3) bits, 3   LSBs more robust
          i3,i7    => one index (5+2) bits, 2-3 LSbs more robust

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

 None

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

 [1] c3_14pf.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001

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

------------------------------------------------------------------------------
 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 void compress_code(
    Word16 sign_indx[], /* i : signs of 4 pulses (signs only)             */
    Word16 pos_indx[],  /* i : position index of 8 pulses (position only) */
    Word16 indx[],      /* o : position and sign of 8 pulses (compressed) */
    Flag  *pOverflow)   /* o : Flag set when overflow occurs              */
{
    Word16 i;
    Word16 ia;
    Word16 ib;
    Word16 ic;

    Word16 *p_indx = &indx[0];
    Word16 *p_sign_indx = &sign_indx[0];

    Word32 tempWord32;

    for (i = 0; i < NB_TRACK_MR102; i++)
    {
        *(p_indx++) = *(p_sign_indx++);
    }

    /* First index
      indx[NB_TRACK] = (ia/2+(ib/2)*5 +(ic/2)*25)*8 + ia%2 + (ib%2)*2 + (ic%2)*4; */

    indx[NB_TRACK_MR102] =
        compress10(
            pos_indx[0],
            pos_indx[4],
            pos_indx[1],
            pOverflow);

    /* Second index
      indx[NB_TRACK+1] = (ia/2+(ib/2)*5 +(ic/2)*25)*8 + ia%2 + (ib%2)*2 + (ic%2)*4; */

    indx[NB_TRACK_MR102+1] =
        compress10(
            pos_indx[2],
            pos_indx[6],
            pos_indx[5],
            pOverflow);

    /*
      Third index
      if ((ib/2)%2 == 1)
        indx[NB_TRACK+2] = ((((4-ia/2) + (ib/2)*5)*32+12)/25)*4 + ia%2 + (ib%2)*2;
      else
        indx[NB_TRACK+2] = ((((ia/2) +   (ib/2)*5)*32+12)/25)*4 + ia%2 + (ib%2)*2;
        */

    ib = pos_indx[7] >> 1;

    ib &= 1;

    ia = pos_indx[3] >> 1;

    if (ib == 1)
    {
        ia = 4 - ia;
    }

    ib = pos_indx[7] >> 1;

    tempWord32 = ((Word32) ib * 5) << 1;

    tempWord32 = tempWord32 >> 1;

    ib = (Word16) tempWord32;

    ib += ia;

    ib <<= 5;

    ib += 12;

    ic = (Word16)(((Word32) ib * 1311) >> 15);

    ic <<= 2;

    ia = pos_indx[3] & 1;

    ib = ((Word16)(pos_indx[7] & 1)) << 1;

    ib += ic;

    ib += ia;

    indx[NB_TRACK_MR102+2] = ib;

} /* compress_code */


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

/*
------------------------------------------------------------------------------
 FUNCTION NAME: code_8i40_31bits()
------------------------------------------------------------------------------
 INPUT AND OUTPUT DEFINITIONS

 Inputs:
    x   Array of type Word16 -- target vector
    cn  Array of type Word16 -- residual after long term prediction
    h   Array of type Word16 -- impulse response of weighted synthesis filter


 Outputs:
    cod Array of type Word16 -- algebraic (fixed) codebook excitation
    y   Array of type Word16 -- filtered fixed codebook excitation
    indx Array of type Word16 -- index of 8 pulses (signs+positions)
    pOverflow    Pointer to Flag      -- set when overflow occurs

 Returns:
    None

 Global Variables Used:
    None

 Local Variables Needed:
    None

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

 FUNCTION:

 PURPOSE:  Searches a 31 bit algebraic codebook containing 8 pulses
           in a frame of 40 samples.

 DESCRIPTION:
   The code contains 8 nonzero pulses: i0...i7.
   All pulses can have two possible amplitudes: +1 or -1.
   The 40 positions in a subframe are divided into 4 tracks of
   interleaved positions. Each track contains two pulses.
   The pulses can have the following possible positions:

      i0, i4 :  0, 4, 8,  12, 16, 20, 24, 28, 32, 36
      i1, i5 :  1, 5, 9,  13, 17, 21, 25, 29, 33, 37
      i2, i6 :  2, 6, 10, 14, 18, 22, 26, 30, 34, 38
      i3, i7 :  3, 7, 11, 15, 19, 23, 27, 31, 35, 39

   Each pair of pulses require 1 bit for their signs. The positions
   are encoded together 3,3 and 2 resulting in
   (7+3) + (7+3) + (5+2) bits for their
   positions. This results in a 31 (4 sign and 27 pos) bit codebook.
   The function determines the optimal pulse signs and positions, builds
   the codevector, and computes the filtered codevector.

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

 None

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

 [1] c8_31pf.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001

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

------------------------------------------------------------------------------
 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 code_8i40_31bits(
    Word16 x[],        /* i : target vector                                  */
    Word16 cn[],       /* i : residual after long term prediction            */
    Word16 h[],        /* i : impulse response of weighted synthesis
                             filter                                         */
    Word16 cod[],      /* o : algebraic (fixed) codebook excitation          */
    Word16 y[],        /* o : filtered fixed codebook excitation             */
    Word16 indx[],     /* o : 7 Word16, index of 8 pulses (signs+positions)  */
    Flag  *pOverflow   /* o : Flag set when overflow occurs                  */
)
{
    Word16 ipos[NB_PULSE];
    Word16 pos_max[NB_TRACK_MR102];
    Word16 codvec[NB_PULSE];

    Word16 dn[L_CODE];
    Word16 sign[L_CODE];

    Word16 rr[L_CODE][L_CODE];
    Word16 linear_signs[NB_TRACK_MR102];
    Word16 linear_codewords[NB_PULSE];

    cor_h_x2(
        h,
        x,
        dn,
        2,
        NB_TRACK_MR102,
        STEP_MR102,
        pOverflow);

    /* 2 = use GSMEFR scaling */

    set_sign12k2(
        dn,
        cn,
        sign,
        pos_max,
        NB_TRACK_MR102,
        ipos,
        STEP_MR102,
        pOverflow);

    /* same setsign alg as GSM-EFR new constants though*/

    cor_h(
        h,
        sign,
        rr,
        pOverflow);

    search_10and8i40(
        NB_PULSE,
        STEP_MR102,
        NB_TRACK_MR102,
        dn,
        rr,
        ipos,
        pos_max,
        codvec,
        pOverflow);

    build_code(
        codvec,
        sign,
        cod,
        h,
        y,
        linear_signs,
        linear_codewords,
        pOverflow);

    compress_code(
        linear_signs,
        linear_codewords,
        indx,
        pOverflow);

} /* code_8i40_31bits */