1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1992-2001 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #ifndef _MULTIMEDIA_AUDIO_ENCODE_H
28 #define	_MULTIMEDIA_AUDIO_ENCODE_H
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 #include <audio_types.h>
35 #include <audio_hdr.h>
36 
37 /*
38  * audio_encode.h
39  *
40  * u-law, A-law and linear PCM conversion tables and macros.
41  */
42 
43 /* PCM linear <-> a-law conversion tables */
44 extern short		_alaw2linear[];		/* 8-bit a-law to 16-bit PCM */
45 extern unsigned char	*_linear2alaw;		/* 13-bit PCM to 8-bit a-law */
46 
47 /* PCM linear <-> u-law conversion tables */
48 extern short		_ulaw2linear[];		/* 8-bit u-law to 16-bit PCM */
49 extern unsigned char	*_linear2ulaw;		/* 14-bit PCM to 8-bit u-law */
50 
51 /* A-law <-> u-law conversion tables */
52 extern unsigned char	_alaw2ulaw[];		/* 8-bit A-law to 8-bit u-law */
53 extern unsigned char	_ulaw2alaw[];		/* 8-bit u-law to 8-bit A-law */
54 
55 /* PCM linear <-> a-law conversion macros */
56 
57 /* a-law to 8,16,32-bit linear */
58 #define	audio_a2c(X)	((char)(_alaw2linear[(unsigned char) (X)] >> 8))
59 #define	audio_a2s(X)	(_alaw2linear[(unsigned char) (X)])
60 #define	audio_a2l(X)	(((long)_alaw2linear[(unsigned char) (X)]) << 16)
61 
62 /* 8,16,32-bit linear to a-law */
63 #define	audio_c2a(X)	(_linear2alaw[((short)(X)) << 5])
64 #define	audio_s2a(X)	(_linear2alaw[((short)(X)) >> 3])
65 #define	audio_l2a(X)	(_linear2alaw[((long)(X)) >> 19])
66 
67 /* PCM linear <-> u-law conversion macros */
68 
69 /* u-law to 8,16,32-bit linear */
70 #define	audio_u2c(X)	((char)(_ulaw2linear[(unsigned char) (X)] >> 8))
71 #define	audio_u2s(X)	(_ulaw2linear[(unsigned char) (X)])
72 #define	audio_u2l(X)	(((long)_ulaw2linear[(unsigned char) (X)]) << 16)
73 
74 /* 8,16,32-bit linear to u-law */
75 #define	audio_c2u(X)	(_linear2ulaw[((short)(X)) << 6])
76 #define	audio_s2u(X)	(_linear2ulaw[((short)(X)) >> 2])
77 #define	audio_l2u(X)	(_linear2ulaw[((long)(X)) >> 18])
78 
79 /* A-law <-> u-law conversion macros */
80 
81 #define	audio_a2u(X)	(_alaw2ulaw[(unsigned char)(X)])
82 #define	audio_u2a(X)	(_ulaw2alaw[(unsigned char)(X)])
83 
84 /*
85  * external declarations, type definitions and
86  * macro definitions for use with the G.721 routines.
87  */
88 
89 /*
90  * The following is the definition of the state structure
91  * used by the G.721/G.723 encoder and decoder to preserve their internal
92  * state between successive calls.  The meanings of the majority
93  * of the state structure fields are explained in detail in the
94  * CCITT Recommendation G.721.  The field names are essentially indentical
95  * to variable names in the bit level description of the coding algorithm
96  * included in this Recommendation.
97  */
98 struct audio_g72x_state {
99 	long yl;	/* Locked or steady state step size multiplier. */
100 	short yu;	/* Unlocked or non-steady state step size multiplier. */
101 	short dms;	/* Short term energy estimate. */
102 	short dml;	/* Long term energy estimate. */
103 	short ap;	/* Linear weighting coefficient of 'yl' and 'yu'. */
104 
105 	short a[2];	/* Coefficients of pole portion of prediction filter. */
106 	short b[6];	/* Coefficients of zero portion of prediction filter. */
107 	short pk[2];
108 			/*
109 			 * Signs of previous two samples of a partially
110 			 * reconstructed signal.
111 			 */
112 	short dq[6];
113 			/*
114 			 * Previous 6 samples of the quantized difference
115 			 * signal represented in an internal floating point
116 			 * format.
117 			 */
118 	short sr[2];
119 			/*
120 			 * Previous 2 samples of the quantized difference
121 			 * signal represented in an internal floating point
122 			 * format.
123 			 */
124 	char td;	/* delayed tone detect, new in 1988 version */
125 	unsigned char leftover[8];
126 			/*
127 			 * This array is used to store the last unpackable
128 			 * code bits in the event that the number of code bits
129 			 * which must be packed into a byte stream is not a
130 			 * multiple of the sample unit size.
131 			 */
132 	char leftover_cnt;
133 			/*
134 			 * Flag indicating the number of bits stored in
135 			 * 'leftover'.  Reset to 0 upon packing of 'leftover'.
136 			 */
137 };
138 
139 /* External tables. */
140 
141 /* Look-up table for performing fast log based 2. */
142 extern unsigned char _fmultanexp[];
143 
144 /* Look-up table for perfoming fast 6bit by 6bit multiplication. */
145 extern unsigned char _fmultwanmant[];
146 
147 /*
148  * Look-up table for performing fast quantization of the step size
149  * scale factor normalized log magnitude of the difference signal.
150  */
151 extern unsigned char _quani[];
152 
153 /* External function definitions. */
154 
155 EXTERN_FUNCTION(void g721_init_state, (struct audio_g72x_state *state_ptr));
156 EXTERN_FUNCTION(int g721_encode, (
157 			void *in_buf,
158 			int data_size,
159 			Audio_hdr *in_header,
160 			unsigned char *out_buf,
161 			int *out_size,
162 			struct audio_g72x_state *state_ptr));
163 EXTERN_FUNCTION(int g721_decode, (
164 			unsigned char *in_buf,
165 			int data_size,
166 			Audio_hdr *out_header,
167 			void *out_buf,
168 			int *out_size,
169 			struct audio_g72x_state *state_ptr));
170 
171 /*
172  * Look-up table for performing fast quantization of the step size
173  * scale factor normalized log magnitude of the difference signal.
174  */
175 extern unsigned char _g723quani[];
176 
177 /* External function definitions. */
178 
179 EXTERN_FUNCTION(void g723_init_state, (struct audio_g72x_state *state_ptr));
180 EXTERN_FUNCTION(int g723_encode, (
181 			void *in_buf,
182 			int data_size,
183 			Audio_hdr *out_header,
184 			unsigned char *out_buf,
185 			int *out_size,
186 			struct audio_g72x_state *state_ptr));
187 EXTERN_FUNCTION(int g723_decode, (
188 			unsigned char *in_buf,
189 			int data_size,
190 			Audio_hdr *out_header,
191 			void *out_buf,
192 			int *out_size,
193 			struct audio_g72x_state *state_ptr));
194 
195 #ifdef __cplusplus
196 }
197 #endif
198 
199 #endif /* !_MULTIMEDIA_AUDIO_ENCODE_H */
200