1 /* $Id$ */
2 /*
3  * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4  * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 #ifndef __PJMEDIA_ALAW_ULAW_H__
21 #define __PJMEDIA_ALAW_ULAW_H__
22 
23 #include <pjmedia/types.h>
24 
25 PJ_BEGIN_DECL
26 
27 #if defined(PJMEDIA_HAS_ALAW_ULAW_TABLE) && PJMEDIA_HAS_ALAW_ULAW_TABLE!=0
28 
29 extern const pj_uint8_t pjmedia_linear2ulaw_tab[16384];
30 extern const pj_uint8_t pjmedia_linear2alaw_tab[16384];
31 extern const pj_int16_t pjmedia_ulaw2linear_tab[256];
32 extern const pj_int16_t pjmedia_alaw2linear_tab[256];
33 
34 
35 /**
36  * Convert 16-bit linear PCM value to 8-bit A-Law.
37  *
38  * @param pcm_val   16-bit linear PCM value.
39  * @return	    8-bit A-Law value.
40  */
41 #define pjmedia_linear2alaw(pcm_val)	\
42 	    pjmedia_linear2alaw_tab[(((pj_int16_t)pcm_val) >> 2) & 0x3fff]
43 
44 /**
45  * Convert 8-bit A-Law value to 16-bit linear PCM value.
46  *
47  * @param chara_val 8-bit A-Law value.
48  * @return	    16-bit linear PCM value.
49  */
50 #define pjmedia_alaw2linear(chara_val)	\
51 	    pjmedia_alaw2linear_tab[chara_val]
52 
53 /**
54  * Convert 16-bit linear PCM value to 8-bit U-Law.
55  *
56  * @param pcm_val   16-bit linear PCM value.
57  * @return	    U-bit A-Law value.
58  */
59 #define pjmedia_linear2ulaw(pcm_val)	\
60 	    pjmedia_linear2ulaw_tab[(((pj_int16_t)pcm_val) >> 2) & 0x3fff]
61 
62 /**
63  * Convert 8-bit U-Law value to 16-bit linear PCM value.
64  *
65  * @param u_val	    8-bit U-Law value.
66  * @return	    16-bit linear PCM value.
67  */
68 #define pjmedia_ulaw2linear(u_val)	\
69 	    pjmedia_ulaw2linear_tab[u_val]
70 
71 /**
72  * Convert 8-bit A-Law value to 8-bit U-Law value.
73  *
74  * @param aval	    8-bit A-Law value.
75  * @return	    8-bit U-Law value.
76  */
77 #define pjmedia_alaw2ulaw(aval)		\
78 	    pjmedia_linear2ulaw(pjmedia_alaw2linear(aval))
79 
80 /**
81  * Convert 8-bit U-Law value to 8-bit A-Law value.
82  *
83  * @param uval	    8-bit U-Law value.
84  * @return	    8-bit A-Law value.
85  */
86 #define pjmedia_ulaw2alaw(uval)		\
87 	    pjmedia_linear2alaw(pjmedia_ulaw2linear(uval))
88 
89 
90 #else
91 
92 /**
93  * Convert 16-bit linear PCM value to 8-bit A-Law.
94  *
95  * @param pcm_val   16-bit linear PCM value.
96  * @return	    8-bit A-Law value.
97  */
98 PJ_DECL(pj_uint8_t) pjmedia_linear2alaw(int pcm_val);
99 
100 /**
101  * Convert 8-bit A-Law value to 16-bit linear PCM value.
102  *
103  * @param chara_val 8-bit A-Law value.
104  * @return	    16-bit linear PCM value.
105  */
106 PJ_DECL(int) pjmedia_alaw2linear(unsigned chara_val);
107 
108 /**
109  * Convert 16-bit linear PCM value to 8-bit U-Law.
110  *
111  * @param pcm_val   16-bit linear PCM value.
112  * @return	    U-bit A-Law value.
113  */
114 PJ_DECL(unsigned char) pjmedia_linear2ulaw(int pcm_val);
115 
116 /**
117  * Convert 8-bit U-Law value to 16-bit linear PCM value.
118  *
119  * @param u_val	    8-bit U-Law value.
120  * @return	    16-bit linear PCM value.
121  */
122 PJ_DECL(int) pjmedia_ulaw2linear(unsigned char u_val);
123 
124 /**
125  * Convert 8-bit A-Law value to 8-bit U-Law value.
126  *
127  * @param aval	    8-bit A-Law value.
128  * @return	    8-bit U-Law value.
129  */
130 PJ_DECL(unsigned char) pjmedia_alaw2ulaw(unsigned char aval);
131 
132 /**
133  * Convert 8-bit U-Law value to 8-bit A-Law value.
134  *
135  * @param uval	    8-bit U-Law value.
136  * @return	    8-bit A-Law value.
137  */
138 PJ_DECL(unsigned char) pjmedia_ulaw2alaw(unsigned char uval);
139 
140 #endif
141 
142 /**
143  * Encode 16-bit linear PCM data to 8-bit U-Law data.
144  *
145  * @param dst	    Destination buffer for 8-bit U-Law data.
146  * @param src	    Source, 16-bit linear PCM data.
147  * @param count	    Number of samples.
148  */
pjmedia_ulaw_encode(pj_uint8_t * dst,const pj_int16_t * src,pj_size_t count)149 PJ_INLINE(void) pjmedia_ulaw_encode(pj_uint8_t *dst, const pj_int16_t *src,
150 				    pj_size_t count)
151 {
152     const pj_int16_t *end = src + count;
153 
154     while (src < end) {
155 	*dst++ = pjmedia_linear2ulaw(*src++);
156     }
157 }
158 
159 /**
160  * Encode 16-bit linear PCM data to 8-bit A-Law data.
161  *
162  * @param dst	    Destination buffer for 8-bit A-Law data.
163  * @param src	    Source, 16-bit linear PCM data.
164  * @param count	    Number of samples.
165  */
pjmedia_alaw_encode(pj_uint8_t * dst,const pj_int16_t * src,pj_size_t count)166 PJ_INLINE(void) pjmedia_alaw_encode(pj_uint8_t *dst, const pj_int16_t *src,
167 				    pj_size_t count)
168 {
169     const pj_int16_t *end = src + count;
170 
171     while (src < end) {
172 	*dst++ = pjmedia_linear2alaw(*src++);
173     }
174 }
175 
176 /**
177  * Decode 8-bit U-Law data to 16-bit linear PCM data.
178  *
179  * @param dst	    Destination buffer for 16-bit PCM data.
180  * @param src	    Source, 8-bit U-Law data.
181  * @param len	    Encoded frame/source length in bytes.
182  */
pjmedia_ulaw_decode(pj_int16_t * dst,const pj_uint8_t * src,pj_size_t len)183 PJ_INLINE(void) pjmedia_ulaw_decode(pj_int16_t *dst, const pj_uint8_t *src,
184 				    pj_size_t len)
185 {
186     const pj_uint8_t *end = src + len;
187 
188     while (src < end) {
189 	*dst++ = pjmedia_ulaw2linear(*src++);
190     }
191 }
192 
193 /**
194  * Decode 8-bit A-Law data to 16-bit linear PCM data.
195  *
196  * @param dst	    Destination buffer for 16-bit PCM data.
197  * @param src	    Source, 8-bit A-Law data.
198  * @param len	    Encoded frame/source length in bytes.
199  */
pjmedia_alaw_decode(pj_int16_t * dst,const pj_uint8_t * src,pj_size_t len)200 PJ_INLINE(void) pjmedia_alaw_decode(pj_int16_t *dst, const pj_uint8_t *src,
201 				    pj_size_t len)
202 {
203     const pj_uint8_t *end = src + len;
204 
205     while (src < end) {
206 	*dst++ = pjmedia_alaw2linear(*src++);
207     }
208 }
209 
210 PJ_END_DECL
211 
212 #endif	/* __PJMEDIA_ALAW_ULAW_H__ */
213 
214