GSM: Fix encoding problems with GSM
[asterisk/asterisk.git] / codecs / gsm / src / code.c
1 /*
2  * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische
3  * Universitaet Berlin.  See the accompanying file "COPYRIGHT" for
4  * details.  THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
5  */
6
7 /* $Header$ */
8
9 #include        "config.h"
10
11 #ifdef  HAS_STRING_H
12 #include        <string.h>
13 #else
14 #       include "proto.h"
15         extern char     * memcpy P((char *, char *, int));
16 #endif
17
18 #include        "private.h"
19 #include        "gsm.h"
20 #include        "proto.h"
21
22 /* 
23  *  4.2 FIXED POINT IMPLEMENTATION OF THE RPE-LTP CODER 
24  */
25
26 void Gsm_Coder P8((S,s,LARc,Nc,bc,Mc,xmaxc,xMc),
27
28         struct gsm_state        * S,
29
30         word    * s,    /* [0..159] samples                     IN      */
31
32 /*
33  * The RPE-LTD coder works on a frame by frame basis.  The length of
34  * the frame is equal to 160 samples.  Some computations are done
35  * once per frame to produce at the output of the coder the
36  * LARc[1..8] parameters which are the coded LAR coefficients and 
37  * also to realize the inverse filtering operation for the entire
38  * frame (160 samples of signal d[0..159]).  These parts produce at
39  * the output of the coder:
40  */
41
42         word    * LARc, /* [0..7] LAR coefficients              OUT     */
43
44 /*
45  * Procedure 4.2.11 to 4.2.18 are to be executed four times per
46  * frame.  That means once for each sub-segment RPE-LTP analysis of
47  * 40 samples.  These parts produce at the output of the coder:
48  */
49
50         word    * Nc,   /* [0..3] LTP lag                       OUT     */
51         word    * bc,   /* [0..3] coded LTP gain                OUT     */
52         word    * Mc,   /* [0..3] RPE grid selection            OUT     */
53         word    * xmaxc,/* [0..3] Coded maximum amplitude       OUT     */
54         word    * xMc   /* [13*4] normalized RPE samples        OUT     */
55 )
56 {
57         int     k;
58         word    * dp  = S->dp0 + 120;   /* [ -120...-1 ] */
59         word    * dpp = dp;             /* [ 0...39 ]    */
60
61         word    e[50];
62
63         word    so[160];
64
65         memset(e,0,sizeof e);
66         Gsm_Preprocess                  (S, s, so);
67         Gsm_LPC_Analysis                (S, so, LARc);
68         Gsm_Short_Term_Analysis_Filter  (S, LARc, so);
69
70         for (k = 0; k <= 3; k++, xMc += 13) {
71
72                 Gsm_Long_Term_Predictor ( S,
73                                          so+k*40, /* d      [0..39] IN  */
74                                          dp,      /* dp  [-120..-1] IN  */
75                                         e + 5,    /* e      [0..39] OUT */
76                                         dpp,      /* dpp    [0..39] OUT */
77                                          Nc++,
78                                          bc++);
79
80                 Gsm_RPE_Encoding        ( S,
81                                         e + 5,  /* e      ][0..39][ IN/OUT */
82                                           xmaxc++, Mc++, xMc );
83                 /*
84                  * Gsm_Update_of_reconstructed_short_time_residual_signal
85                  *                      ( dpp, e + 5, dp );
86                  */
87
88                 { register int i;
89                   for (i = 0; i <= 39; i++)
90                         dp[ i ] = GSM_ADD( e[5 + i], dpp[i] );
91                 }
92                 dp  += 40;
93                 dpp += 40;
94
95         }
96         (void)memcpy( (char *)S->dp0, (char *)(S->dp0 + 160),
97                 120 * sizeof(*S->dp0) );
98 }