summaryrefslogtreecommitdiffstats
path: root/media/libstagefright/codecs/amrnb/dec/src/d8_31pf.cpp
blob: 917c47f204d601f3b3ba1be6fcecbefc04988aae (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
/* ------------------------------------------------------------------
 * 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/d8_31pf.c
 Functions:


     Date: 01/28/2002

------------------------------------------------------------------------------
 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: Per review comments...
 (1) Removed include of "count.h" and "basic_op.h"
 (2) Added includes of mult.h, shl.h, shr.h, add.h, sub.h, negate.h,
     L_mult.h, and L_shr.h

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

 Description:

------------------------------------------------------------------------------
 MODULE DESCRIPTION
*/

/*----------------------------------------------------------------------------
; INCLUDES
----------------------------------------------------------------------------*/
#include "d8_31pf.h"
#include "typedef.h"
#include "basic_op.h"
#include "cnst.h"


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

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

/* define values/representation for output codevector and sign */
#define POS_CODE  8191
#define NEG_CODE  8191


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

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

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

 Inputs:
   MSBs -- Word16 -- MSB part of the index
   LSBs -- Word16 -- LSB part of the index
   index1 -- Word16 -- index for first pos in pos_index[]
   index2 -- Word16 -- index for second pos in pos_index[]
   index3 -- Word16 -- index for third pos in pos_index[]

 Outputs:
   pos_indx[] -- array of type Word16 -- position of 3 pulses (decompressed)

   pOverflow  Flag set when overflow occurs, pointer of type Flag *

 Returns:
    None

 Global Variables Used:
    None

 Local Variables Needed:
    None

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


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

 None

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

 d8_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 void decompress10(
    Word16 MSBs,        /* i : MSB part of the index                 */
    Word16 LSBs,        /* i : LSB part of the index                 */
    Word16 index1,      /* i : index for first pos in pos_index[]    */
    Word16 index2,      /* i : index for second pos in pos_index[]   */
    Word16 index3,      /* i : index for third pos in pos_index[]    */
    Word16 pos_indx[],  /* o : position of 3 pulses (decompressed)   */
    Flag  *pOverflow)   /* o : Flag set when overflow occurs         */
{
    Word16 ia;
    Word16 ib;
    Word16 ic;
    Word32 tempWord32;

    /*
      pos_indx[index1] = ((MSBs-25*(MSBs/25))%5)*2 + (LSBs-4*(LSBs/4))%2;
      pos_indx[index2] = ((MSBs-25*(MSBs/25))/5)*2 + (LSBs-4*(LSBs/4))/2;
      pos_indx[index3] = (MSBs/25)*2 + LSBs/4;
    */

    if (MSBs > 124)
    {
        MSBs = 124;
    }

    ia =
        mult(
            MSBs,
            1311,
            pOverflow);

    tempWord32 =
        L_mult(
            ia,
            25,
            pOverflow);


    ia = (Word16)(MSBs - (tempWord32 >> 1));
    ib =
        mult(
            ia,
            6554,
            pOverflow);

    tempWord32 =
        L_mult(
            ib,
            5,
            pOverflow);

    ib = ia - (Word16)(tempWord32 >> 1);

    ib =
        shl(
            ib,
            1,
            pOverflow);


    ic = LSBs - ((LSBs >> 2) << 2);


    pos_indx[index1] = ib + (ic & 1);


    ib =
        mult(
            ia,
            6554,
            pOverflow);

    ib =
        shl(
            ib,
            1,
            pOverflow);


    pos_indx[index2] = ib + (ic >> 1);


    ib = LSBs >> 2;

    ic =
        mult(
            MSBs,
            1311,
            pOverflow);

    ic =
        shl(
            ic,
            1,
            pOverflow);

    pos_indx[index3] =
        add(
            ib,
            ic,
            pOverflow);

    return;
}


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

 Inputs:
    indx[] -- array of type Word16 -- position and sign of
                                      8 pulses (compressed)

 Outputs:
    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 type Flag -- Flag set when overflow occurs

 Returns:
    None

 Global Variables Used:
    None

 Local Variables Needed:
    None

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

    PURPOSE: decompression 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

 d8_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 void decompress_code(
    Word16 indx[],      /* i : position and sign of 8 pulses (compressed) */
    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 ia;
    Word16 ib;
    Word16 MSBs;
    Word16 LSBs;
    Word16 MSBs0_24;
    Word32 tempWord32;

    for (i = 0; i < NB_TRACK_MR102; i++)
    {
        sign_indx[i] = indx[i];
    }

    /*
      First index: 10x10x10 -> 2x5x2x5x2x5-> 125x2x2x2 -> 7+1x3 bits
      MSBs = indx[NB_TRACK]/8;
      LSBs = indx[NB_TRACK]%8;
      */
    MSBs = indx[NB_TRACK_MR102] >> 3;

    LSBs = indx[NB_TRACK_MR102] & 0x7;

    decompress10(
        MSBs,
        LSBs,
        0,
        4,
        1,
        pos_indx,
        pOverflow);

    /*
      Second index: 10x10x10 -> 2x5x2x5x2x5-> 125x2x2x2 -> 7+1x3 bits
      MSBs = indx[NB_TRACK+1]/8;
      LSBs = indx[NB_TRACK+1]%8;
      */
    MSBs = indx[NB_TRACK_MR102+1] >> 3;

    LSBs = indx[NB_TRACK_MR102+1] & 0x7;

    decompress10(
        MSBs,
        LSBs,
        2,
        6,
        5,
        pos_indx,
        pOverflow);

    /*
      Third index: 10x10 -> 2x5x2x5-> 25x2x2 -> 5+1x2 bits
      MSBs = indx[NB_TRACK+2]/4;
      LSBs = indx[NB_TRACK+2]%4;
      MSBs0_24 = (MSBs*25+12)/32;
      if ((MSBs0_24/5)%2==1)
         pos_indx[3] = (4-(MSBs0_24%5))*2 + LSBs%2;
      else
         pos_indx[3] = (MSBs0_24%5)*2 + LSBs%2;
      pos_indx[7] = (MSBs0_24/5)*2 + LSBs/2;
      */

    MSBs = indx[NB_TRACK_MR102+2] >> 2;

    LSBs = indx[NB_TRACK_MR102+2] & 0x3;

    tempWord32 =
        L_mult(
            MSBs,
            25,
            pOverflow);

    ia =
        (Word16)
        L_shr(
            tempWord32,
            1,
            pOverflow);

    ia += 12;

    MSBs0_24 = ia >> 5;


    ia =
        mult(
            MSBs0_24,
            6554,
            pOverflow);

    ia &= 1;


    ib =
        mult(
            MSBs0_24,
            6554,
            pOverflow);

    tempWord32 =
        L_mult(
            ib,
            5,
            pOverflow);


    ib = MSBs0_24 - (Word16)(tempWord32 >> 1);

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

    }


    ib =
        shl(
            ib,
            1,
            pOverflow);

    ia = LSBs & 0x1;

    pos_indx[3] =
        add(
            ib,
            ia,
            pOverflow);

    ia =
        mult(
            MSBs0_24,
            6554,
            pOverflow);

    ia =
        shl(
            ia,
            1,
            pOverflow);

    pos_indx[7] = ia + (LSBs >> 1);

}

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

 Inputs:
    index   array of type Word16 --  index of 8 pulses (sign+position)

 Outputs:
    cod     array of type Word16 --  algebraic (fixed) codebook excitation
    pOverflow pointer to type Flag -- Flag set when overflow occurs

 Returns:
    None

 Global Variables Used:
    None

 Local Variables Needed:
    None

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

 PURPOSE:  Builds the innovative codevector from the received
           index of algebraic codebook.

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

 None

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

 d8_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 dec_8i40_31bits(
    Word16 index[],    /* i : index of 8 pulses (sign+position)         */
    Word16 cod[],      /* o : algebraic (fixed) codebook excitation     */
    Flag  *pOverflow   /* o : Flag set when overflow occurs             */
)
{
    Word16 i;
    Word16 j;
    Word16 pos1;
    Word16 pos2;
    Word16 sign;

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

    for (i = 0; i < L_CODE; i++)
    {
        cod[i] = 0;
    }

    decompress_code(
        index,
        linear_signs,
        linear_codewords,
        pOverflow);

    /* decode the positions and signs of pulses and build the codeword */
    for (j = 0; j < NB_TRACK_MR102; j++)    /* NB_TRACK_MR102 = 4 */
    {
        /* position of pulse "j" */

        pos1 = (linear_codewords[j] << 2) + j;


        if (linear_signs[j] == 0)
        {
            sign = POS_CODE; /* +1.0 */
        }
        else
        {
            sign = -NEG_CODE; /* -1.0 */
        }

        if (pos1 < L_SUBFR)
        {
            cod[pos1] = sign;    /* avoid buffer overflow */
        }

        /* compute index i */
        /* position of pulse "j+4" */

        pos2 = (linear_codewords[j + 4] << 2) + j;


        if (pos2 < pos1)
        {
            sign = negate(sign);
        }

        if (pos2 < L_SUBFR)
        {
            cod[pos2] += sign;     /* avoid buffer overflow */
        }


    } /* for (j = 0; j < NB_TRACK_MR102; j++) */

    return;
}