-
Notifications
You must be signed in to change notification settings - Fork 5
/
g711.c
316 lines (264 loc) · 7.58 KB
/
g711.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/* $Id: g711.c,v 1.3 2009/09/11 16:51:07 toad32767 Exp $ */
/* Id: ulaw.c,v 1.10 2007/01/07 14:47:32 toad32767 Exp */
/* Id: alaw.c,v 1.6 2007/01/07 14:47:32 toad32767 Exp */
/*-
* Copyright (c) 2006, 2007, 2009 Marco Trillo.
*
* Permission is hereby granted, free of charge, to any
* person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the
* Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice
* shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
* KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
* OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#define LIBAIFF 1
#include <stdlib.h>
#include <libaiff/libaiff.h>
#include <math.h>
#include "private.h"
static int8_t expt[128] = {0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7};
/*
* G.711 mu-Law
*/
static int16_t
ulawdec(uint8_t x)
{
int sgn, exp, mant;
int y;
x = ~x; /* bits are sent reversed */
sgn = x & 0x80; /* sign bit */
mant = ((x & 0xF) << 1) | 0x21; /* mantissa plus hidden bits */
exp = (x & 0x70) >> 4; /* exponent */
mant = (mant << exp) - 0x21; /* get mantissa, unraise */
y = (sgn ? -mant : mant);
return (y << 2);
}
static uint8_t
ulawenc(int16_t x)
{
int sgn, exp;
uint8_t out;
x >>= 2;
sgn = x < 0; /* get sgn */
if (0 != sgn)
x = -x; /* get 13-bit magnitude */
if (x > 0x1FDE)
x = 0x1FDE; /* clip for raising... */
x = (x + 0x21) >> 1; /* raise output -> 5 bits */
exp = expt[x >> 5]; /* get exponent */
x >>= exp; /* get mantissa */
out = (sgn << 7) | (exp << 4) | (x & 0xF); /* pack */
return (~out); /* reverse */
}
/*
* G.711 A-Law
*/
static int16_t
alawdec(uint8_t x)
{
int sgn, exp, mant;
int y;
x = (~x & 0xD5) | (x & 0x2A); /* even bits (ex. sign) are reversed */
sgn = x & 0x80;
mant = ((x & 0xF) << 1) | 0x21; /* mantissa plus hidden bits */
exp = (x & 0x70) >> 4; /* exponent */
if (0 == exp)
mant &= ~0x20; /* denormalized */
else
mant <<= exp - 1;
y = (sgn ? -mant : mant);
return (y << 3);
}
static uint8_t
alawenc(int16_t y)
{
int x = y;
int sgn, exp;
uint8_t out;
if (x == -32768) x = -32767;
sgn = x < 0; /* get sgn */
if (0 != sgn)
x = -x; /* get 11-bit magnitude */
x >>= 4;
exp = expt[x >> 4]; /* get exponent */
if (0 != exp)
x >>= exp - 1; /* hidden bit */
out = (sgn << 7) | (exp << 4) | (x & 0xF); /* pack */
return ((~out & 0xD5) | (out & 0x2A)); /* reverse */
}
static int
g711_ulaw_create(AIFF_Ref r)
{
int i;
int16_t *p = malloc(256 * sizeof(int16_t));
if (p != NULL) {
for (i = 0; i < 256; ++i)
p[i] = ulawdec(i);
r->pdata = p;
return 1;
} else
return -1;
}
static int
g711_alaw_create(AIFF_Ref r)
{
int i;
int16_t *p = malloc(256 * sizeof(int16_t));
if (p != NULL) {
for (i = 0; i < 256; ++i)
p[i] = alawdec(i);
r->pdata = p;
return 1;
} else
return -1;
}
static void
g711_delete(AIFF_Ref r)
{
free(r->pdata);
}
static size_t
g711_read_lpcm(AIFF_Ref r, void *buffer, size_t len)
{
size_t n, i, rem, bytesToRead, bytesRead;
uint8_t *bytes;
int16_t *samples, *table = r->pdata;
void *buf;
/* length must be even */
len &= ~1;
n = len >> 1;
rem = r->soundLen - r->pos;
bytesToRead = MIN(n, rem);
if (bytesToRead == 0)
return 0;
buf = AIFFBufAllocate(r, kAIFFBufConv, bytesToRead);
if (NULL == buf)
return 0;
bytesRead = fread(buf, 1, bytesToRead, r->fd);
if (bytesRead > 0) {
r->pos += bytesRead;
} else {
return 0;
}
bytes = buf;
samples = buffer;
for (i = 0; i < bytesRead; ++i) {
samples[i] = table[bytes[i]];
}
return (bytesRead << 1);
}
static int
g711_seek(AIFF_Ref r, uint64_t pos)
{
uint64_t b;
OFF_T of;
b = pos * r->nChannels;
if (b >= r->soundLen)
return 0;
of = b;
if (FSEEKO(r->fd, of, SEEK_CUR) < 0) {
return -1;
}
r->pos = b;
return 1;
}
static int
g711_read_float32(AIFF_Ref r, float *buffer, int nFrames)
{
size_t n = nFrames, i, rem, bytesToRead, bytesRead;
uint8_t *bytes;
int16_t *table = r->pdata;
void *buf;
rem = r->soundLen - r->pos;
bytesToRead = MIN(n, rem);
if (bytesToRead == 0)
return 0;
buf = AIFFBufAllocate(r, kAIFFBufConv, bytesToRead);
if (NULL == buf)
return 0;
bytesRead = fread(buf, 1, bytesToRead, r->fd);
if (bytesRead > 0) {
r->pos += bytesRead;
} else {
return 0;
}
bytes = buf;
for (i = 0; i < bytesRead; ++i) {
buffer[i] = ldexp(table[bytes[i]], -15);
}
return bytesRead; /* = framesRead */
}
static int
g711_write_lpcm(AIFF_Ref w, void *inptr, size_t ilen, int readOnlyBuf)
{
uint8_t *outb;
int16_t *inb;
uint8_t (*f)(int16_t);
int i, n;
if (2 != w->segmentSize)
return (-1);
n = ilen >> 1;
if (readOnlyBuf) {
if ((outb = AIFFBufAllocate(w, kAIFFBufExt, n)) == NULL)
return -1;
} else {
outb = inptr;
}
inb = inptr;
switch (w->audioFormat) {
case AUDIO_FORMAT_ULAW: f = ulawenc; break;
case AUDIO_FORMAT_ALAW: f = alawenc; break;
default:
ASSERT(0);
}
for (i = 0; i < n; i++) {
outb[i] = (*f)(inb[i]);
}
if (fwrite(outb, 1, n, w->fd) != (size_t)n) {
return (-1);
}
w->nSamples += n;
w->sampleBytes += n;
w->len += n;
return (1);
}
struct codec ulaw = {
AUDIO_FORMAT_ULAW,
g711_ulaw_create,
g711_read_lpcm,
g711_read_float32,
g711_write_lpcm,
g711_seek,
g711_delete
};
struct codec alaw = {
AUDIO_FORMAT_ALAW,
g711_alaw_create,
g711_read_lpcm,
g711_read_float32,
g711_write_lpcm,
g711_seek,
g711_delete
};