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