~ubuntu-branches/ubuntu/trusty/linphone/trusty

« back to all changes in this revision

Viewing changes to mediastreamer2/src/utils/g711common.h

  • Committer: Package Import Robot
  • Author(s): Luk Claes
  • Date: 2013-09-11 19:08:43 UTC
  • mfrom: (1.1.19) (16.1.12 sid)
  • Revision ID: package-import@ubuntu.com-20130911190843-fkydjxsdvy1fmx24
Tags: 3.6.1-2.1
* Non-maintainer upload.
* Apply Sebastian Ramacher's patch to fix FTBFS (Closes: #720668).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  PCM - A-Law conversion
 
3
 *  Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
 
4
 *
 
5
 *  Wrapper for linphone Codec class by Simon Morlat <simon.morlat@linphone.org>
 
6
 */
 
7
 
 
8
static inline int val_seg(int val)
 
9
{
 
10
        int r = 0;
 
11
        val >>= 7; /*7 = 4 + 3*/
 
12
        if (val & 0xf0) {
 
13
                val >>= 4;
 
14
                r += 4;
 
15
        }
 
16
        if (val & 0x0c) {
 
17
                val >>= 2;
 
18
                r += 2;
 
19
        }
 
20
        if (val & 0x02)
 
21
                r += 1;
 
22
        return r;
 
23
}
 
24
 
 
25
/*
 
26
 * s16_to_alaw() - Convert a 16-bit linear PCM value to 8-bit A-law
 
27
 *
 
28
 * s16_to_alaw() accepts an 16-bit integer and encodes it as A-law data.
 
29
 *
 
30
 *              Linear Input Code       Compressed Code
 
31
 *      ------------------------        ---------------
 
32
 *      0000000wxyza                    000wxyz
 
33
 *      0000001wxyza                    001wxyz
 
34
 *      000001wxyzab                    010wxyz
 
35
 *      00001wxyzabc                    011wxyz
 
36
 *      0001wxyzabcd                    100wxyz
 
37
 *      001wxyzabcde                    101wxyz
 
38
 *      01wxyzabcdef                    110wxyz
 
39
 *      1wxyzabcdefg                    111wxyz
 
40
 *
 
41
 * For further information see John C. Bellamy's Digital Telephony, 1982,
 
42
 * John Wiley & Sons, pps 98-111 and 472-476.
 
43
 * G711 is designed for 13 bits input signal, this function add extra shifting to take this into account.
 
44
 */
 
45
 
 
46
static inline unsigned char s16_to_alaw(int pcm_val)
 
47
{
 
48
        int             mask;
 
49
        int             seg;
 
50
        unsigned char   aval;
 
51
 
 
52
        if (pcm_val >= 0) {
 
53
                mask = 0xD5;
 
54
        } else {
 
55
                mask = 0x55;
 
56
                pcm_val = -pcm_val;
 
57
                if (pcm_val > 0x7fff)
 
58
                        pcm_val = 0x7fff;
 
59
        }
 
60
 
 
61
        if (pcm_val < 256) /*256 = 32 << 3*/
 
62
                aval = pcm_val >> 4; /*4 = 1 + 3*/
 
63
        else {
 
64
                /* Convert the scaled magnitude to segment number. */
 
65
                seg = val_seg(pcm_val);
 
66
                aval = (seg << 4) | ((pcm_val >> (seg + 3)) & 0x0f);
 
67
        }
 
68
        return aval ^ mask;
 
69
}
 
70
 
 
71
/*
 
72
 * alaw_to_s16() - Convert an A-law value to 16-bit linear PCM
 
73
 *
 
74
 */
 
75
static inline int alaw_to_s16(unsigned char a_val)
 
76
{
 
77
        int             t;
 
78
        int             seg;
 
79
 
 
80
        a_val ^= 0x55;
 
81
        t = a_val & 0x7f;
 
82
        if (t < 16)
 
83
                t = (t << 4) + 8;
 
84
        else {
 
85
                seg = (t >> 4) & 0x07;
 
86
                t = ((t & 0x0f) << 4) + 0x108;
 
87
                t <<= seg -1;
 
88
        }
 
89
        return ((a_val & 0x80) ? t : -t);
 
90
}
 
91
/*
 
92
 * s16_to_ulaw() - Convert a linear PCM value to u-law
 
93
 *
 
94
 * In order to simplify the encoding process, the original linear magnitude
 
95
 * is biased by adding 33 which shifts the encoding range from (0 - 8158) to
 
96
 * (33 - 8191). The result can be seen in the following encoding table:
 
97
 *
 
98
 *      Biased Linear Input Code        Compressed Code
 
99
 *      ------------------------        ---------------
 
100
 *      00000001wxyza                   000wxyz
 
101
 *      0000001wxyzab                   001wxyz
 
102
 *      000001wxyzabc                   010wxyz
 
103
 *      00001wxyzabcd                   011wxyz
 
104
 *      0001wxyzabcde                   100wxyz
 
105
 *      001wxyzabcdef                   101wxyz
 
106
 *      01wxyzabcdefg                   110wxyz
 
107
 *      1wxyzabcdefgh                   111wxyz
 
108
 *
 
109
 * Each biased linear code has a leading 1 which identifies the segment
 
110
 * number. The value of the segment number is equal to 7 minus the number
 
111
 * of leading 0's. The quantization interval is directly available as the
 
112
 * four bits wxyz.  * The trailing bits (a - h) are ignored.
 
113
 *
 
114
 * Ordinarily the complement of the resulting code word is used for
 
115
 * transmission, and so the code word is complemented before it is returned.
 
116
 *
 
117
 * For further information see John C. Bellamy's Digital Telephony, 1982,
 
118
 * John Wiley & Sons, pps 98-111 and 472-476.
 
119
 */
 
120
 
 
121
static inline unsigned char s16_to_ulaw(int pcm_val)    /* 2's complement (16-bit range) */
 
122
{
 
123
        int mask;
 
124
        int seg;
 
125
        unsigned char uval;
 
126
 
 
127
        if (pcm_val < 0) {
 
128
                pcm_val = 0x84 - pcm_val;
 
129
                mask = 0x7f;
 
130
        } else {
 
131
                pcm_val += 0x84;
 
132
                mask = 0xff;
 
133
        }
 
134
        if (pcm_val > 0x7fff)
 
135
                pcm_val = 0x7fff;
 
136
 
 
137
        /* Convert the scaled magnitude to segment number. */
 
138
        seg = val_seg(pcm_val);
 
139
 
 
140
        /*
 
141
         * Combine the sign, segment, quantization bits;
 
142
         * and complement the code word.
 
143
         */
 
144
        uval = (seg << 4) | ((pcm_val >> (seg + 3)) & 0x0f);
 
145
        return uval ^ mask;
 
146
}
 
147
 
 
148
/*
 
149
 * ulaw_to_s16() - Convert a u-law value to 16-bit linear PCM
 
150
 *
 
151
 * First, a biased linear code is derived from the code word. An unbiased
 
152
 * output can then be obtained by subtracting 33 from the biased code.
 
153
 *
 
154
 * Note that this function expects to be passed the complement of the
 
155
 * original code word. This is in keeping with ISDN conventions.
 
156
 */
 
157
static inline int ulaw_to_s16(unsigned char u_val)
 
158
{
 
159
        int t;
 
160
 
 
161
        /* Complement to obtain normal u-law value. */
 
162
        u_val = ~u_val;
 
163
 
 
164
        /*
 
165
         * Extract and bias the quantization bits. Then
 
166
         * shift up by the segment number and subtract out the bias.
 
167
         */
 
168
        t = ((u_val & 0x0f) << 3) + 0x84;
 
169
        t <<= (u_val & 0x70) >> 4;
 
170
 
 
171
        return ((u_val & 0x80) ? (0x84 - t) : (t - 0x84));
 
172
}