1 /* 	$Id: g711.c,v 1.1 2007/09/19 11:49:46 toad32767 Exp $ */
2 /* 	Id: ulaw.c,v 1.10 2007/01/07 14:47:32 toad32767 Exp    */
3 /* 	Id: alaw.c,v 1.6 2007/01/07 14:47:32 toad32767 Exp     */
4 
5 /*-
6  * Copyright (c) 2006, 2007 Marco Trillo
7  *
8  * Permission is hereby granted, free of charge, to any
9  * person obtaining a copy of this software and associated
10  * documentation files (the "Software"), to deal in the
11  * Software without restriction, including without limitation
12  * the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the
14  * Software, and to permit persons to whom the Software is
15  * furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice
18  * shall be included in all copies or substantial portions of
19  * the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
22  * KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
23  * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
24  * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
25  * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
26  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
27  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
28  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29  */
30 
31 #define LIBAIFF 1
32 #include <stdlib.h>
33 #include <libaiff/libaiff.h>
34 #include "private.h"
35 
36 /*
37  * G.711 mu-Law (u-Law)
38  *
39  * G.711 mu-Law is a 8-bit floating point representation
40  * of 14-bit PCM samples, with a 4-bit mantissa and a 3-bit
41  * exponent.
42  *
43  * This gives 5-bit (4 + hidden bit) mantissa precision for all samples,
44  * so low samples (with zeros in the most-significant bits) get
45  * more precision with this encoding than with normal 8-bit LPCM.
46  *
47  * For more information on G.711 u-Law:
48  *   http://shannon.cm.nctu.edu.tw/comtheory/chap3.pdf
49  */
50 static          int16_t
ulawdec(uint8_t x)51 ulawdec(uint8_t x)
52 {
53 	int             sgn, exp, mant;
54 	int             y;
55 
56 	x = ~x;			/* bits are sent reversed */
57 	sgn = x & 0x80;
58 	x &= 0x7F;
59 	mant = (int) ((x & 0xF) << 1) | 0x21;	/* mantissa plus hidden bits */
60 	exp = (int) ((x & 0x70) >> 4);	/* exponent */
61 	mant <<= exp;
62 
63 	/*
64 	 * Subtract 33 from the 'raised output'
65 	 * to get the normal output (14-bit),
66 	 * and then convert the 14-bit output
67 	 * to 16-bit
68 	 */
69 	y = (sgn ? (-mant + 33) : (mant - 33));
70 	return (int16_t) (y << 2);
71 }
72 
73 
74 /*
75  * G.711 A-Law (A-Law)
76  *
77  * G.711 A-Law is a 8-bit floating point representation
78  * of 13-bit PCM samples, with a 4-bit mantissa and a 3-bit
79  * exponent.
80  *
81  * This gives 5-bit (4 + hidden bit) mantissa precision for all samples,
82  * so low samples (with zeros in the most-significant bits) get
83  * more precision with this encoding than with normal 8-bit LPCM.
84  *
85  * For more information on G.711 A-Law:
86  *   http://shannon.cm.nctu.edu.tw/comtheory/chap3.pdf
87  *   http://en.wikipedia.org/wiki/G.711
88  */
89 static          int16_t
alawdec(uint8_t x)90 alawdec(uint8_t x)
91 {
92 	int             sgn, exp, mant;
93 	int             y;
94 
95 	/*
96 	 * The bits at even positions are inversed.
97 	 * In addition, the sign bit is inversed too.
98 	 */
99 	x = ((~x & 0xD5) | (x & 0x2A));
100 	sgn = x & 0x80;
101 	x &= 0x7F;
102 	mant = (int) ((x & 0xF) << 1) | 0x21;
103 	exp = (int) ((x & 0x70) >> 4);
104 
105 	/*
106 	 * Denormalized numbers? then remove hidden bit
107 	 */
108 	if (exp == 0)
109 		mant &= ~0x20;
110 	else
111 		mant <<= (exp - 1);
112 
113 	y = (sgn ? -mant : mant);
114 	return (int16_t) (y << 3);	/* convert 13-bit to 16-bit */
115 }
116 
117 static int
g711_ulaw_create(AIFF_Ref r)118 g711_ulaw_create(AIFF_Ref r)
119 {
120 	int             i;
121 	int16_t        *p = malloc(256 * sizeof(int16_t));
122 	if (p != NULL) {
123 		for (i = 0; i < 256; ++i)
124 			p[i] = ulawdec(i);
125 		r->pdata = p;
126 		return 1;
127 	} else
128 		return -1;
129 }
130 
131 static int
g711_alaw_create(AIFF_Ref r)132 g711_alaw_create(AIFF_Ref r)
133 {
134 	int             i;
135 	int16_t        *p = malloc(256 * sizeof(int16_t));
136 	if (p != NULL) {
137 		for (i = 0; i < 256; ++i)
138 			p[i] = alawdec(i);
139 		r->pdata = p;
140 		return 1;
141 	} else
142 		return -1;
143 }
144 
145 static void
g711_delete(AIFF_Ref r)146 g711_delete(AIFF_Ref r)
147 {
148 	free(r->pdata);
149 }
150 
151 static          size_t
g711_read_lpcm(AIFF_Ref r,void * buffer,size_t len)152 g711_read_lpcm(AIFF_Ref r, void *buffer, size_t len)
153 {
154 	size_t          n, i, rem, bytesToRead, bytesRead;
155 	uint8_t        *bytes;
156 	int16_t        *samples, *table = r->pdata;
157 
158 	/* length must be even (16-bit samples) */
159 	if (len & 1)
160 		--len;
161 
162 	n = len >> 1;
163 	rem = (size_t) (r->soundLen) - (size_t) (r->pos);
164 	bytesToRead = MIN(n, rem);
165 	if (bytesToRead == 0)
166 		return 0;
167 
168 	if (r->buffer2 == NULL || r->buflen2 < bytesToRead) {
169 		if (r->buffer2 != NULL)
170 			free(r->buffer2);
171 		r->buffer2 = malloc(bytesToRead);
172 		if (r->buffer2 == NULL) {
173 			r->buflen2 = 0;
174 			return 0;
175 		}
176 		r->buflen2 = bytesToRead;
177 	}
178 	bytesRead = fread(r->buffer2, 1, bytesToRead, r->fd);
179 	if (bytesRead > 0) {
180 		r->pos += bytesRead;
181 	} else {
182 		return 0;
183 	}
184 
185 	bytes = (uint8_t *) (r->buffer2);
186 	samples = (int16_t *) buffer;
187 	for (i = 0; i < bytesRead; ++i) {
188 		samples[i] = table[bytes[i]];
189 	}
190 
191 	return (bytesRead << 1);/* bytesRead * 2 */
192 }
193 
194 static int
g711_seek(AIFF_Ref r,uint64_t pos)195 g711_seek(AIFF_Ref r, uint64_t pos)
196 {
197 	long            of;
198 	uint32_t        b;
199 
200 	b = (uint32_t) pos * r->nChannels;
201 	if (b >= r->soundLen)
202 		return 0;
203 	of = (long) b;
204 
205 	if (fseek(r->fd, of, SEEK_CUR) < 0) {
206 		return -1;
207 	}
208 	r->pos = b;
209 	return 1;
210 }
211 
212 static int
g711_read_float32(AIFF_Ref r,float * buffer,int nFrames)213 g711_read_float32(AIFF_Ref r, float *buffer, int nFrames)
214 {
215 	size_t          n = (size_t) nFrames, i, rem, bytesToRead, bytesRead;
216 	uint8_t        *bytes;
217 	int16_t        *table = r->pdata;
218 
219 	rem = (size_t) (r->soundLen) - (size_t) (r->pos);
220 	bytesToRead = MIN(n, rem);
221 	if (bytesToRead == 0)
222 		return 0;
223 
224 	if (r->buffer2 == NULL || r->buflen2 < bytesToRead) {
225 		if (r->buffer2 != NULL)
226 			free(r->buffer2);
227 		r->buffer2 = malloc(bytesToRead);
228 		if (r->buffer2 == NULL) {
229 			r->buflen2 = 0;
230 			return 0;
231 		}
232 		r->buflen2 = bytesToRead;
233 	}
234 	bytesRead = fread(r->buffer2, 1, bytesToRead, r->fd);
235 	if (bytesRead > 0) {
236 		r->pos += bytesRead;
237 	} else {
238 		return 0;
239 	}
240 
241 	bytes = (uint8_t *) (r->buffer2);
242 	for (i = 0; i < bytesRead; ++i) {
243 		buffer[i] = (float) (table[bytes[i]]) / 32768.0;
244 	}
245 
246 	return bytesRead;	/* bytesRead = framesRead (segmentSize = 1) */
247 }
248 
249 
250 struct decoder  ulaw = {
251 	AUDIO_FORMAT_ULAW,
252 	g711_ulaw_create,
253 	g711_read_lpcm,
254 	g711_read_float32,
255 	g711_seek,
256 	g711_delete
257 };
258 
259 struct decoder  alaw = {
260 	AUDIO_FORMAT_ALAW,
261 	g711_alaw_create,
262 	g711_read_lpcm,
263 	g711_read_float32,
264 	g711_seek,
265 	g711_delete
266 };
267 
268 
269