Initialize Tizen 2.3
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / enc / src / c1035pf.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: c1035pf.cpp
35  Functions: q_p
36             build_code
37             code_10i40_35bits
38
39 ------------------------------------------------------------------------------
40  MODULE DESCRIPTION
41
42  This file contains the function that searches a 35 bit algebraic codebook
43  containing 10 pulses in a frame of 40 samples.
44
45 ------------------------------------------------------------------------------
46 */
47
48 /*----------------------------------------------------------------------------
49 ; INCLUDES
50 ----------------------------------------------------------------------------*/
51 #include "c1035pf.h"
52 #include "cnst.h"
53 #include "basic_op.h"
54 #include "inv_sqrt.h"
55 #include "set_sign.h"
56 #include "cor_h.h"
57 #include "cor_h_x.h"
58 #include "s10_8pf.h"
59 #include "oscl_mem.h"
60
61 /*----------------------------------------------------------------------------
62 ; MACROS
63 ; [Define module specific macros here]
64 ----------------------------------------------------------------------------*/
65
66
67 /*----------------------------------------------------------------------------
68 ; DEFINES
69 ; [Include all pre-processor statements here. Include conditional
70 ; compile variables also.]
71 ----------------------------------------------------------------------------*/
72 #define NB_PULSE  10
73
74 /*----------------------------------------------------------------------------
75 ; LOCAL FUNCTION DEFINITIONS
76 ; [List function prototypes here]
77 ----------------------------------------------------------------------------*/
78
79 /*----------------------------------------------------------------------------
80 ; LOCAL VARIABLE DEFINITIONS
81 ; [Variable declaration - defined here and used outside this module]
82 ----------------------------------------------------------------------------*/
83
84 /*
85 ------------------------------------------------------------------------------
86  FUNCTION NAME: q_p
87 ------------------------------------------------------------------------------
88  INPUT AND OUTPUT DEFINITIONS
89
90  Inputs:
91     pShift_reg = pointer to Old CN generator shift register state (Word32)
92     no_bits = Number of bits (Word16)
93
94  Outputs:
95     pShift_reg -> Updated CN generator shift register state
96
97  Returns:
98     noise_bits = Generated random integer value (Word16)
99
100  Global Variables Used:
101     None
102
103  Local Variables Needed:
104     None
105
106 ------------------------------------------------------------------------------
107  FUNCTION DESCRIPTION
108
109  This is a local function that determnes the index of the pulses by looking up
110  the gray encoder table
111
112 ------------------------------------------------------------------------------
113  REQUIREMENTS
114
115  None
116
117 ------------------------------------------------------------------------------
118  REFERENCES
119
120  c1035pf.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
121
122 ------------------------------------------------------------------------------
123  PSEUDO-CODE
124
125 void q_p (
126     Word16 *ind,        // Pulse position
127     Word16 n            // Pulse number
128 )
129 {
130     Word16 tmp;
131
132     tmp = *ind;
133
134     if (sub (n, 5) < 0)
135     {
136         *ind = (tmp & 0x8) | gray[tmp & 0x7];
137     }
138     else
139     {
140         *ind = gray[tmp & 0x7];
141     }
142 }
143
144 ------------------------------------------------------------------------------
145  CAUTION [optional]
146  [State any special notes, constraints or cautions for users of this function]
147
148 ------------------------------------------------------------------------------
149 */
150
151 /*----------------------------------------------------------------------------
152 ; FUNCTION CODE
153 ----------------------------------------------------------------------------*/
154
155 void q_p(
156     Word16 *pInd,       /* Pulse position */
157     Word16 n,            /* Pulse number   */
158     const Word16* gray_ptr
159 )
160 {
161     Word16 tmp;
162
163     tmp = *pInd;
164
165     if (n < 5)
166     {
167         *pInd = (tmp & 0x8) | gray_ptr[tmp & 0x7];
168     }
169     else
170     {
171         *pInd = gray_ptr[tmp & 0x7];
172     }
173 }
174
175 /*
176 ------------------------------------------------------------------------------
177  FUNCTION NAME: build_code
178 ------------------------------------------------------------------------------
179  INPUT AND OUTPUT DEFINITIONS
180
181  Inputs:
182     pSeed = pointer to the Old CN generator shift register state (Word32)
183     n_param = Number of parameters to randomize (Word16)
184     param_size_table = table holding paameter sizes (Word16)
185     param[] = array to hold CN generated paramters (Word16)
186     pOverflow = pointer to overflow flag (Flag)
187
188  Outputs:
189     param[] = CN generated parameters (Word16)
190     pSeed = Updated CN generator shift register state (Word16)
191     pOverflow -> 1 if overflow occured
192
193  Returns:
194     None
195
196  Global Variables Used:
197     None
198
199  Local Variables Needed:
200     None
201
202 ------------------------------------------------------------------------------
203  FUNCTION DESCRIPTION
204
205  This function builds the codeword, the filtered codeword and index of the
206  codevector, based on the signs and positions of 10 pulses.
207 ------------------------------------------------------------------------------
208  REQUIREMENTS
209
210  None
211
212 ------------------------------------------------------------------------------
213  REFERENCES
214
215  c1035pf.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
216
217 ------------------------------------------------------------------------------
218  PSEUDO-CODE
219 static void build_code (
220     Word16 codvec[],    // (i)  : position of pulses
221     Word16 sign[],      // (i)  : sign of d[n]
222     Word16 cod[],       // (o)  : innovative code vector
223     Word16 h[],         // (i)  : impulse response of weighted synthesis filter
224     Word16 y[],         // (o)  : filtered innovative code
225     Word16 indx[]       // (o)  : index of 10 pulses (sign+position)
226 )
227 {
228     Word16 i, j, k, track, index, _sign[NB_PULSE];
229     Word16 *p0, *p1, *p2, *p3, *p4, *p5, *p6, *p7, *p8, *p9;
230     Word32 s;
231
232     for (i = 0; i < L_CODE; i++)
233     {
234         cod[i] = 0;
235     }
236     for (i = 0; i < NB_TRACK; i++)
237     {
238         indx[i] = -1;
239     }
240
241     for (k = 0; k < NB_PULSE; k++)
242     {
243         // read pulse position
244         i = codvec[k];
245         // read sign
246         j = sign[i];
247
248         index = mult (i, 6554);                  // index = pos/5
249         // track = pos%5
250         track = sub (i, extract_l (L_shr (L_mult (index, 5), 1)));
251
252         if (j > 0)
253         {
254             cod[i] = add (cod[i], 4096);
255             _sign[k] = 8192;
256
257         }
258         else
259         {
260             cod[i] = sub (cod[i], 4096);
261             _sign[k] = -8192;
262             index = add (index, 8);
263         }
264
265         if (indx[track] < 0)
266         {
267             indx[track] = index;
268         }
269         else
270         {
271             if (((index ^ indx[track]) & 8) == 0)
272             {
273                 // sign of 1st pulse == sign of 2nd pulse
274
275                 if (sub (indx[track], index) <= 0)
276                 {
277                     indx[track + 5] = index;
278                 }
279                 else
280                 {
281                     indx[track + 5] = indx[track];
282                     indx[track] = index;
283                 }
284             }
285             else
286             {
287                 // sign of 1st pulse != sign of 2nd pulse
288
289                 if (sub ((Word16)(indx[track] & 7), (Word16)(index & 7)) <= 0)
290                 {
291                     indx[track + 5] = indx[track];
292                     indx[track] = index;
293                 }
294                 else
295                 {
296                     indx[track + 5] = index;
297                 }
298             }
299         }
300     }
301
302     p0 = h - codvec[0];
303     p1 = h - codvec[1];
304     p2 = h - codvec[2];
305     p3 = h - codvec[3];
306     p4 = h - codvec[4];
307     p5 = h - codvec[5];
308     p6 = h - codvec[6];
309     p7 = h - codvec[7];
310     p8 = h - codvec[8];
311     p9 = h - codvec[9];
312
313     for (i = 0; i < L_CODE; i++)
314     {
315         s = 0;
316         s = L_mac (s, *p0++, _sign[0]);
317         s = L_mac (s, *p1++, _sign[1]);
318         s = L_mac (s, *p2++, _sign[2]);
319         s = L_mac (s, *p3++, _sign[3]);
320         s = L_mac (s, *p4++, _sign[4]);
321         s = L_mac (s, *p5++, _sign[5]);
322         s = L_mac (s, *p6++, _sign[6]);
323         s = L_mac (s, *p7++, _sign[7]);
324         s = L_mac (s, *p8++, _sign[8]);
325         s = L_mac (s, *p9++, _sign[9]);
326         y[i] = pv_round (s);
327     }
328 }
329
330 ------------------------------------------------------------------------------
331  CAUTION [optional]
332  [State any special notes, constraints or cautions for users of this function]
333
334 ------------------------------------------------------------------------------
335 */
336
337 /*----------------------------------------------------------------------------
338 ; FUNCTION CODE
339 ----------------------------------------------------------------------------*/
340 static void build_code(
341     Word16 codvec[],    /* (i)  : position of pulses                        */
342     Word16 sign[],      /* (i)  : sign of d[n]                              */
343     Word16 cod[],       /* (o)  : innovative code vector                    */
344     Word16 h[],         /* (i)  : impulse response of weighted synthesis filter*/
345     Word16 y[],         /* (o)  : filtered innovative code                  */
346     Word16 indx[],      /* (o)  : index of 10 pulses (sign+position)        */
347     Flag   *pOverflow   /* i/o  : overflow Flag                             */
348 )
349 {
350     Word16 i, k, track, index, _sign[NB_PULSE];
351     Word16 *p0, *p1, *p2, *p3, *p4, *p5, *p6, *p7, *p8, *p9;
352     Word32 s;
353     Word16 temp;
354     Word16 *p__sign;
355     Word16 *p_y;
356     Word16 *p_codvec;
357
358     OSCL_UNUSED_ARG(pOverflow);
359
360     oscl_memset(cod, 0, L_CODE*sizeof(*cod));
361     oscl_memset(indx, 0xFF, NB_TRACK*sizeof(*indx));
362
363     p__sign = _sign;
364
365     p0 = &codvec[0];
366
367     for (k = 0; k < NB_PULSE; k++)
368     {
369         /* read pulse position */
370         i = *(p0++);
371         /* read sign           */
372
373         index = ((Word32)i * 6554) >> 15;       /* index = pos/5    */
374
375         /* track = pos%5 */
376         /* track = sub (i, extract_l (L_shr (L_mult (index, 5), 1))); */
377         track = i - (index + (index << 2));
378
379         if (sign[i] > 0)
380         {
381             cod[i] +=  4096;
382             *(p__sign++) = 8192;
383
384         }
385         else
386         {
387             cod[i] -=  4096;
388             *(p__sign++) = -8192;
389             /* index = add (index, 8); */
390             index += 8;
391         }
392
393         p1 = &indx[track];
394
395         temp = *p1;
396
397         if (temp < 0)
398         {
399             *p1 = index;
400         }
401         else
402         {
403             if (((index ^ temp) & 8) == 0)
404             {
405                 /* sign of 1st pulse == sign of 2nd pulse */
406
407                 /* if (sub (indx[track], index) <= 0) */
408                 if (temp <= index)
409                 {
410                     *(p1 + 5) = index;
411                 }
412                 else
413                 {
414                     *(p1 + 5) = temp;
415                     *p1 = index;
416                 }
417             }
418             else
419             {
420                 /* sign of 1st pulse != sign of 2nd pulse */
421
422                 /* if (sub ((Word16)(indx[track] & 7), (Word16)(index & 7)) <= 0) */
423                 if ((temp & 7) <= (index & 7))
424                 {
425                     *(p1 + 5) = temp;
426                     *p1 = index;
427                 }
428                 else
429                 {
430                     *(p1 + 5) = index;
431                 }
432             }
433         }
434     }
435
436     p_codvec = &codvec[0];
437
438     p0 = h - *(p_codvec++);
439     p1 = h - *(p_codvec++);
440     p2 = h - *(p_codvec++);
441     p3 = h - *(p_codvec++);
442     p4 = h - *(p_codvec++);
443     p5 = h - *(p_codvec++);
444     p6 = h - *(p_codvec++);
445     p7 = h - *(p_codvec++);
446     p8 = h - *(p_codvec++);
447     p9 = h - *(p_codvec++);
448
449     p_y = y;
450
451     for (i = L_CODE; i != 0; i--)
452     {
453         p__sign = _sign;
454
455         s  = (*p0++ * *(p__sign++)) >> 7;
456         s += (*p1++ * *(p__sign++)) >> 7;
457         s += (*p2++ * *(p__sign++)) >> 7;
458         s += (*p3++ * *(p__sign++)) >> 7;
459         s += (*p4++ * *(p__sign++)) >> 7;
460         s += (*p5++ * *(p__sign++)) >> 7;
461         s += (*p6++ * *(p__sign++)) >> 7;
462         s += (*p7++ * *(p__sign++)) >> 7;
463         s += (*p8++ * *(p__sign++)) >> 7;
464         s += (*p9++ * *(p__sign++)) >> 7;
465
466         *(p_y++) = (s + 0x080) >> 8;
467     }
468
469 }
470
471 /*
472 ------------------------------------------------------------------------------
473  FUNCTION NAME: code_10i40_35bits
474 ------------------------------------------------------------------------------
475  INPUT AND OUTPUT DEFINITIONS
476
477  Inputs:
478     pSeed = pointer to the Old CN generator shift register state (Word32)
479     n_param = Number of parameters to randomize (Word16)
480     param_size_table = table holding paameter sizes (Word16)
481     param[] = array to hold CN generated paramters (Word16)
482     pOverflow = pointer to overflow flag (Flag)
483
484  Outputs:
485     param[] = CN generated parameters (Word16)
486     pSeed = Updated CN generator shift register state (Word16)
487     pOverflow -> 1 if overflow occured
488
489  Returns:
490     None
491
492  Global Variables Used:
493     None
494
495  Local Variables Needed:
496     None
497
498 ------------------------------------------------------------------------------
499  FUNCTION DESCRIPTION
500
501  This function searches a 35 bit algebraic codebook containing 10 pulses in a
502  frame of 40 samples.
503
504  The code contains 10 nonzero pulses: i0...i9.
505  All pulses can have two possible amplitudes: +1 or -1.
506  The 40 positions in a subframe are divided into 5 tracks of
507  interleaved positions. Each track contains two pulses.
508  The pulses can have the following possible positions:
509
510     i0, i5 :  0, 5, 10, 15, 20, 25, 30, 35.
511     i1, i6 :  1, 6, 11, 16, 21, 26, 31, 36.
512     i2, i7 :  2, 7, 12, 17, 22, 27, 32, 37.
513     i3, i8 :  3, 8, 13, 18, 23, 28, 33, 38.
514     i4, i9 :  4, 9, 14, 19, 24, 29, 34, 39.
515
516  Each pair of pulses require 1 bit for their signs and 6 bits for their
517  positions (3 bits + 3 bits). This results in a 35 bit codebook.
518  The function determines the optimal pulse signs and positions, builds
519  the codevector, and computes the filtered codevector.
520
521 ------------------------------------------------------------------------------
522  REQUIREMENTS
523
524  None
525
526 ------------------------------------------------------------------------------
527  REFERENCES
528
529  c1035pf.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
530
531 ------------------------------------------------------------------------------
532  PSEUDO-CODE
533 void code_10i40_35bits (
534     Word16 x[],   // (i)   : target vector
535     Word16 cn[],  // (i)   : residual after long term prediction
536     Word16 h[],   // (i)   : impulse response of weighted synthesis filter
537                            // h[-L_subfr..-1] must be set to zero
538     Word16 cod[], // (o)   : algebraic (fixed) codebook excitation
539     Word16 y[],   // (o)   : filtered fixed codebook excitation
540     Word16 indx[] // (o)   : index of 10 pulses (sign + position)
541 )
542 {
543     Word16 ipos[NB_PULSE], pos_max[NB_TRACK], codvec[NB_PULSE];
544     Word16 dn[L_CODE], sign[L_CODE];
545     Word16 rr[L_CODE][L_CODE], i;
546
547     cor_h_x (h, x, dn, 2);
548     set_sign12k2 (dn, cn, sign, pos_max, NB_TRACK, ipos, STEP);
549     cor_h (h, sign, rr);
550
551     search_10and8i40 (NB_PULSE, STEP, NB_TRACK,
552                       dn, rr, ipos, pos_max, codvec);
553
554     build_code (codvec, sign, cod, h, y, indx);
555     for (i = 0; i < 10; i++)
556     {
557         q_p (&indx[i], i);
558     }
559     return;
560 }
561
562 ------------------------------------------------------------------------------
563  CAUTION [optional]
564  [State any special notes, constraints or cautions for users of this function]
565
566 ------------------------------------------------------------------------------
567 */
568
569 /*----------------------------------------------------------------------------
570 ; FUNCTION CODE
571 ----------------------------------------------------------------------------*/
572 void code_10i40_35bits(
573     Word16 x[],     /* (i)   : target vector                                */
574     Word16 cn[],    /* (i)   : residual after long term prediction          */
575     Word16 h[],     /* (i)   : impulse response of weighted synthesis filter
576                              h[-L_subfr..-1] must be set to zero            */
577     Word16 cod[],   /* (o)   : algebraic (fixed) codebook excitation        */
578     Word16 y[],     /* (o)   : filtered fixed codebook excitation           */
579     Word16 indx[],  /* (o)   : index of 10 pulses (sign + position)         */
580     const Word16* gray_ptr, /* (i) : ptr to read-only table                 */
581     Flag *pOverflow /* (i/o) : overflow Flag                                */
582 )
583 {
584     Word16 ipos[NB_PULSE], pos_max[NB_TRACK], codvec[NB_PULSE];
585     Word16 dn[L_CODE], sign[L_CODE];
586     Word16 rr[L_CODE][L_CODE], i;
587
588     cor_h_x(h, x, dn, 2, pOverflow);
589     set_sign12k2(dn, cn, sign, pos_max, NB_TRACK, ipos, STEP, pOverflow);
590     cor_h(h, sign, rr, pOverflow);
591
592     search_10and8i40(NB_PULSE, STEP, NB_TRACK,
593                      dn, rr, ipos, pos_max, codvec, pOverflow);
594
595     build_code(codvec, sign, cod, h, y, indx, pOverflow);
596     for (i = 0; i < 10; i++)
597     {
598         q_p(&indx[i], i, gray_ptr);
599     }
600     return;
601 }
602