~ubuntu-branches/ubuntu/raring/sflphone/raring

« back to all changes in this revision

Viewing changes to daemon/libs/pjproject/pjmedia/src/pjmedia/alaw_ulaw.c

  • Committer: Package Import Robot
  • Author(s): Francois Marier
  • Date: 2011-11-25 13:24:12 UTC
  • mfrom: (4.1.10 sid)
  • Revision ID: package-import@ubuntu.com-20111125132412-dc4qvhyosk74cd42
Tags: 1.0.1-4
Don't assume that arch:all packages will get built (closes: #649726)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This source code is a product of Sun Microsystems, Inc. and is provided
 
3
 * for unrestricted use.  Users may copy or modify this source code without
 
4
 * charge.
 
5
 *
 
6
 * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
 
7
 * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
 
8
 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
 
9
 *
 
10
 * Sun source code is provided with no support and without any obligation on
 
11
 * the part of Sun Microsystems, Inc. to assist in its use, correction,
 
12
 * modification or enhancement.
 
13
 *
 
14
 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
 
15
 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE
 
16
 * OR ANY PART THEREOF.
 
17
 *
 
18
 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
 
19
 * or profits or other special, indirect and consequential damages, even if
 
20
 * Sun has been advised of the possibility of such damages.
 
21
 *
 
22
 * Sun Microsystems, Inc.
 
23
 * 2550 Garcia Avenue
 
24
 * Mountain View, California  94043
 
25
 */
 
26
#include <pjmedia/alaw_ulaw.h>
 
27
 
 
28
#if !defined(PJMEDIA_HAS_ALAW_ULAW_TABLE) || PJMEDIA_HAS_ALAW_ULAW_TABLE==0
 
29
 
 
30
#ifdef _MSC_VER
 
31
#  pragma warning ( disable: 4244 ) /* Conversion from int to char etc */
 
32
#endif
 
33
 
 
34
/*
 
35
 * g711.c
 
36
 *
 
37
 * u-law, A-law and linear PCM conversions.
 
38
 */
 
39
#define SIGN_BIT        (0x80)          /* Sign bit for a A-law byte. */
 
40
#define QUANT_MASK      (0xf)           /* Quantization field mask. */
 
41
#define NSEGS           (8)             /* Number of A-law segments. */
 
42
#define SEG_SHIFT       (4)             /* Left shift for segment number. */
 
43
#define SEG_MASK        (0x70)          /* Segment field mask. */
 
44
 
 
45
static short seg_end[8] = {0xFF, 0x1FF, 0x3FF, 0x7FF,
 
46
                            0xFFF, 0x1FFF, 0x3FFF, 0x7FFF};
 
47
 
 
48
/* copy from CCITT G.711 specifications */
 
49
static unsigned char _u2a[128] = {              /* u- to A-law conversions */
 
50
        1,      1,      2,      2,      3,      3,      4,      4,
 
51
        5,      5,      6,      6,      7,      7,      8,      8,
 
52
        9,      10,     11,     12,     13,     14,     15,     16,
 
53
        17,     18,     19,     20,     21,     22,     23,     24,
 
54
        25,     27,     29,     31,     33,     34,     35,     36,
 
55
        37,     38,     39,     40,     41,     42,     43,     44,
 
56
        46,     48,     49,     50,     51,     52,     53,     54,
 
57
        55,     56,     57,     58,     59,     60,     61,     62,
 
58
        64,     65,     66,     67,     68,     69,     70,     71,
 
59
        72,     73,     74,     75,     76,     77,     78,     79,
 
60
        81,     82,     83,     84,     85,     86,     87,     88,
 
61
        89,     90,     91,     92,     93,     94,     95,     96,
 
62
        97,     98,     99,     100,    101,    102,    103,    104,
 
63
        105,    106,    107,    108,    109,    110,    111,    112,
 
64
        113,    114,    115,    116,    117,    118,    119,    120,
 
65
        121,    122,    123,    124,    125,    126,    127,    128};
 
66
 
 
67
static unsigned char _a2u[128] = {              /* A- to u-law conversions */
 
68
        1,      3,      5,      7,      9,      11,     13,     15,
 
69
        16,     17,     18,     19,     20,     21,     22,     23,
 
70
        24,     25,     26,     27,     28,     29,     30,     31,
 
71
        32,     32,     33,     33,     34,     34,     35,     35,
 
72
        36,     37,     38,     39,     40,     41,     42,     43,
 
73
        44,     45,     46,     47,     48,     48,     49,     49,
 
74
        50,     51,     52,     53,     54,     55,     56,     57,
 
75
        58,     59,     60,     61,     62,     63,     64,     64,
 
76
        65,     66,     67,     68,     69,     70,     71,     72,
 
77
        73,     74,     75,     76,     77,     78,     79,     79,
 
78
        80,     81,     82,     83,     84,     85,     86,     87,
 
79
        88,     89,     90,     91,     92,     93,     94,     95,
 
80
        96,     97,     98,     99,     100,    101,    102,    103,
 
81
        104,    105,    106,    107,    108,    109,    110,    111,
 
82
        112,    113,    114,    115,    116,    117,    118,    119,
 
83
        120,    121,    122,    123,    124,    125,    126,    127};
 
84
 
 
85
static int
 
86
search(
 
87
        int             val,
 
88
        short           *table,
 
89
        int             size)
 
90
{
 
91
        int             i;
 
92
 
 
93
        for (i = 0; i < size; i++) {
 
94
                if (val <= *table++)
 
95
                        return (i);
 
96
        }
 
97
        return (size);
 
98
}
 
99
 
 
100
/*
 
101
 * linear2alaw() - Convert a 16-bit linear PCM value to 8-bit A-law
 
102
 *
 
103
 * linear2alaw() accepts an 16-bit integer and encodes it as A-law data.
 
104
 *
 
105
 *              Linear Input Code       Compressed Code
 
106
 *      ------------------------        ---------------
 
107
 *      0000000wxyza                    000wxyz
 
108
 *      0000001wxyza                    001wxyz
 
109
 *      000001wxyzab                    010wxyz
 
110
 *      00001wxyzabc                    011wxyz
 
111
 *      0001wxyzabcd                    100wxyz
 
112
 *      001wxyzabcde                    101wxyz
 
113
 *      01wxyzabcdef                    110wxyz
 
114
 *      1wxyzabcdefg                    111wxyz
 
115
 *
 
116
 * For further information see John C. Bellamy's Digital Telephony, 1982,
 
117
 * John Wiley & Sons, pps 98-111 and 472-476.
 
118
 */
 
119
PJ_DEF(pj_uint8_t) pjmedia_linear2alaw(
 
120
        int             pcm_val)        /* 2's complement (16-bit range) */
 
121
{
 
122
        int             mask;
 
123
        int             seg;
 
124
        unsigned char   aval;
 
125
 
 
126
        if (pcm_val >= 0) {
 
127
                mask = 0xD5;            /* sign (7th) bit = 1 */
 
128
        } else {
 
129
                mask = 0x55;            /* sign bit = 0 */
 
130
                pcm_val = -pcm_val - 8;
 
131
        }
 
132
 
 
133
        /* Convert the scaled magnitude to segment number. */
 
134
        seg = search(pcm_val, seg_end, 8);
 
135
 
 
136
        /* Combine the sign, segment, and quantization bits. */
 
137
 
 
138
        if (seg >= 8)           /* out of range, return maximum value. */
 
139
                return (0x7F ^ mask);
 
140
        else {
 
141
                aval = seg << SEG_SHIFT;
 
142
                if (seg < 2)
 
143
                        aval |= (pcm_val >> 4) & QUANT_MASK;
 
144
                else
 
145
                        aval |= (pcm_val >> (seg + 3)) & QUANT_MASK;
 
146
                return (aval ^ mask);
 
147
        }
 
148
}
 
149
 
 
150
/*
 
151
 * alaw2linear() - Convert an A-law value to 16-bit linear PCM
 
152
 *
 
153
 */
 
154
PJ_DEF(int) pjmedia_alaw2linear(
 
155
        unsigned a_val)
 
156
{
 
157
        int             t;
 
158
        int             seg;
 
159
 
 
160
        a_val ^= 0x55;
 
161
 
 
162
        t = (a_val & QUANT_MASK) << 4;
 
163
        seg = ((unsigned)a_val & SEG_MASK) >> SEG_SHIFT;
 
164
        switch (seg) {
 
165
        case 0:
 
166
                t += 8;
 
167
                break;
 
168
        case 1:
 
169
                t += 0x108;
 
170
                break;
 
171
        default:
 
172
                t += 0x108;
 
173
                t <<= seg - 1;
 
174
        }
 
175
        return ((a_val & SIGN_BIT) ? t : -t);
 
176
}
 
177
 
 
178
#define BIAS            (0x84)          /* Bias for linear code. */
 
179
 
 
180
/*
 
181
 * linear2ulaw() - Convert a linear PCM value to u-law
 
182
 *
 
183
 * In order to simplify the encoding process, the original linear magnitude
 
184
 * is biased by adding 33 which shifts the encoding range from (0 - 8158) to
 
185
 * (33 - 8191). The result can be seen in the following encoding table:
 
186
 *
 
187
 *      Biased Linear Input Code        Compressed Code
 
188
 *      ------------------------        ---------------
 
189
 *      00000001wxyza                   000wxyz
 
190
 *      0000001wxyzab                   001wxyz
 
191
 *      000001wxyzabc                   010wxyz
 
192
 *      00001wxyzabcd                   011wxyz
 
193
 *      0001wxyzabcde                   100wxyz
 
194
 *      001wxyzabcdef                   101wxyz
 
195
 *      01wxyzabcdefg                   110wxyz
 
196
 *      1wxyzabcdefgh                   111wxyz
 
197
 *
 
198
 * Each biased linear code has a leading 1 which identifies the segment
 
199
 * number. The value of the segment number is equal to 7 minus the number
 
200
 * of leading 0's. The quantization interval is directly available as the
 
201
 * four bits wxyz.  * The trailing bits (a - h) are ignored.
 
202
 *
 
203
 * Ordinarily the complement of the resulting code word is used for
 
204
 * transmission, and so the code word is complemented before it is returned.
 
205
 *
 
206
 * For further information see John C. Bellamy's Digital Telephony, 1982,
 
207
 * John Wiley & Sons, pps 98-111 and 472-476.
 
208
 */
 
209
PJ_DEF(unsigned char) pjmedia_linear2ulaw(
 
210
        int             pcm_val)        /* 2's complement (16-bit range) */
 
211
{
 
212
        int             mask;
 
213
        int             seg;
 
214
        unsigned char   uval;
 
215
 
 
216
        /* Get the sign and the magnitude of the value. */
 
217
        if (pcm_val < 0) {
 
218
                pcm_val = BIAS - pcm_val;
 
219
                mask = 0x7F;
 
220
        } else {
 
221
                pcm_val += BIAS;
 
222
                mask = 0xFF;
 
223
        }
 
224
 
 
225
        /* Convert the scaled magnitude to segment number. */
 
226
        seg = search(pcm_val, seg_end, 8);
 
227
 
 
228
        /*
 
229
         * Combine the sign, segment, quantization bits;
 
230
         * and complement the code word.
 
231
         */
 
232
        if (seg >= 8)           /* out of range, return maximum value. */
 
233
                return (0x7F ^ mask);
 
234
        else {
 
235
                uval = (seg << 4) | ((pcm_val >> (seg + 3)) & 0xF);
 
236
                return (uval ^ mask);
 
237
        }
 
238
 
 
239
}
 
240
 
 
241
/*
 
242
 * ulaw2linear() - Convert a u-law value to 16-bit linear PCM
 
243
 *
 
244
 * First, a biased linear code is derived from the code word. An unbiased
 
245
 * output can then be obtained by subtracting 33 from the biased code.
 
246
 *
 
247
 * Note that this function expects to be passed the complement of the
 
248
 * original code word. This is in keeping with ISDN conventions.
 
249
 */
 
250
PJ_DEF(int) pjmedia_ulaw2linear(
 
251
        unsigned char   u_val)
 
252
{
 
253
        int             t;
 
254
 
 
255
        /* Shortcut: when input is zero, output is zero 
 
256
         * This will also make the VAD works harder.
 
257
         *  -bennylp
 
258
         */
 
259
        if (u_val == 0) return 0;
 
260
 
 
261
        /* Complement to obtain normal u-law value. */
 
262
        u_val = ~u_val;
 
263
 
 
264
        /*
 
265
         * Extract and bias the quantization bits. Then
 
266
         * shift up by the segment number and subtract out the bias.
 
267
         */
 
268
        t = ((u_val & QUANT_MASK) << 3) + BIAS;
 
269
        t <<= ((unsigned)u_val & SEG_MASK) >> SEG_SHIFT;
 
270
 
 
271
        return ((u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS));
 
272
}
 
273
 
 
274
/* A-law to u-law conversion */
 
275
PJ_DEF(unsigned char) pjmedia_alaw2ulaw(
 
276
        unsigned char   aval)
 
277
{
 
278
        aval &= 0xff;
 
279
        return ((aval & 0x80) ? (0xFF ^ _a2u[aval ^ 0xD5]) :
 
280
            (0x7F ^ _a2u[aval ^ 0x55]));
 
281
}
 
282
 
 
283
/* u-law to A-law conversion */
 
284
PJ_DEF(unsigned char) pjmedia_ulaw2alaw(
 
285
        unsigned char   uval)
 
286
{
 
287
        uval &= 0xff;
 
288
        return ((uval & 0x80) ? (0xD5 ^ (_u2a[0xFF ^ uval] - 1)) :
 
289
            (0x55 ^ (_u2a[0x7F ^ uval] - 1)));
 
290
}
 
291
 
 
292
 
 
293
#endif  /* PJMEDIA_HAS_ALAW_ULAW_TABLE */
 
294