Merge remote-tracking branch 'origin/master'
[unleashed/lotheac.git] / usr / src / uts / common / io / audio / impl / audio_grc3.h
blob0003dc15744fb6a540d7e8d4b8650446c5288918
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright (C) 4Front Technologies 1996-2008.
24 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
29 * Purpose: GRC library version 3.1 internal definitions
31 * GRC3 is a high quality sample rate conversion module that uses fixed point
32 * arithmetic.
35 #ifndef AUDIO_GRC3_H
36 #define AUDIO_GRC3_H
38 #define GRC3_MAXHISTORY 4096
40 #ifdef __cplusplus
41 extern "C"
43 #endif
45 typedef struct grc3state {
46 uint32_t srcrate;
47 uint32_t dstrate;
48 uint32_t ptr;
49 uint32_t ptr_incv;
51 uint32_t sat;
52 uint32_t filtfactor;
53 int32_t *historyptr;
54 int32_t dummy_pad1;
56 int32_t history[GRC3_MAXHISTORY * 2];
58 uint32_t outsz;
59 } grc3state_t;
62 /* BEGIN CSTYLED */
63 /*****************************************************************************
65 Tutorial on how to use GRC3 rate conversion
67 1. First, you create an instance of grc3state_t for each channel. If you
68 are working with stereo files - you will need 2 of such instances,
69 for quadro - 4.
71 The instances may be allocated in either static or dynamic memory - that
72 makes no difference to the convertor. So, if your program has to process
73 one stereo stream, there's no reason why should you use malloc/free to
74 allocate/deallocate structures. Also, in device drivers, you can
75 use static variables as well:
77 static grc3state_t grc[2]; // for two channels
80 2. Before starting any conversion, grc3state_t instances should be initialized
81 properly, and you do this with grc3_setup function. Function itself does
82 not allocate additional memory or change anything except grc3state_t
83 structure, so this is thread safe, and you don't have to do additional
84 "deinitialization".
86 If you are doing interleaved audio (stereo/quadro/whatever) conversion,
87 you should do setup on each of the channels, and should have separate
88 instance of grc3state_t for each channel. As you will understand further,
89 such conversion is done separately. And now, the setup function:
91 int grc3_setup( grc3state_t *grc,
92 uint32_t fromRate,
93 uint32_t toRate );
95 grc - pointer to grc3state_t instance
96 fromRate - source sample rate
97 toRate - destination sample rate
99 Note, that sample rates itself are not important - the important thing
100 is ratio between those sample rates. So, for example, if you have to
101 convert from 24000Hz to 48000Hz, it's ok to write:
103 grc3_setup( &grc[0], 240, 480 );
105 Sometimes (in MIDI synths) it would be desired to use fractional sample
106 rates. For example, setup for conversion from 33100.78 to 48000 may look
107 like this:
109 grc3_setup( &grc[0], 3310078, 4800000);
111 Note, that on stereo, GRC3 setup will look like this:
113 static grc3state_t grc[2];
115 // ...
117 grc3_setup( &grc[0], 3310078, 4800000)
118 grc3_setup( &grc[1], 3310078, 4800000);
121 Note, that you should not rely on grc3_setup's fast execution or any
122 execution timing. It may contain some massive arithmetic and even huge
123 loops, so avoid putting grc3_setup to inner loops and calling in
124 latency-dependent code.
127 3. Next, before running a stream through grc3_convert function, you should
128 reset each of grc3state_t instance used:
130 void grc3_reset(grc3state_t *grc);
133 grc - pointer to GRC3 instance variable
135 So, for stereo, this appears to be:
137 static grc3state_t grc[2];
139 // ...
141 grc3_reset( &grc[0] );
142 grc3_reset( &grc[1] );
145 4. Finally, doing conversion is easy:
147 void grc3_convert( grc3state_t *grc,
148 int domain,
149 int quality,
150 const void *src,
151 void *dst,
152 int maxInSize,
153 int maxOutSize,
154 int interleave,
155 int offset );
158 grc - pointer to initialized grc3state_t instance; you
159 can specify NULL to check whether a particular
160 domain/quality pair is supported, check return value
162 quality - quality to use for conversion, supported values are:
164 0 - D lowest quality (normally equals to low quality)
165 1 - L low quality (spline interpolation)
166 2 - M medium quality (lagrange interpolation)
167 3 - H high quality
168 4 - HX high quality (high quality with extra precision)
169 5 - P production quality
171 6 - PX production quality (prod quality with extra precision)
172 (PX is currently disabled because it causes a crash)
174 src - source audio buffer
176 dst - destination audio buffer;
178 maxInSize - size of input buffer (in samples per channel!)
180 maxOutSize - size of output buffer (in samples per channel!)
181 (will never overrun this size)
183 interleave - interleave factor; for MONO or non-interleaved data
184 it should be equal to 1;
186 2 - STEREO interleaved audio
187 4 - QUADRO interleaved audio
189 So, basically, this parameter should be equal to number
190 of interleaved channels
192 offset - number of interleaved channel currently processing,
193 starting from 0; for MONO or non-interleaved data
194 it should be equal to 0
197 For unsupported quality values, it will fall back to
198 "D" quality (the lowest one)
200 also on return it sets:
202 grc->outsz == number of output samples
204 Note, that if quality is not supported,
205 calling the function with real data will fall back
206 to the worst quality available.
208 Note that this version of GRC3 only supports 24-bit
209 native endian. (Modified by Sun for performance.)
213 5. Interleaved processing of N channels is done like this:
216 static grc3state_t grc[N];
217 int t;
219 //...
222 for(t=0; t<N; t++)
224 grc3_setup( &grc[t], 22050, 48000 );
226 grc3_reset( &grc[t] );
230 //...
232 while (...) {
234 for(t = 0; t < N; t++) {
235 grc3_convert(&grc[t], // instance pointer
236 4, // quality
237 in_buffer, // input buffer
238 out_buffer, // input buffer
239 in_samples_count, // number of samples
240 // in in_buffer
241 2048, // size of out_buffer
242 N, t // num of channels, channel#
247 // Normally, for interleaved data, ->outsz of all instances will
248 // be the same for the same stream
250 put_sound_somewhere(out_buffer,
251 grc[0]->outsz * N * sizeof(out_buffer[0]) );
255 6. If you use the same storage and the same setup for processing few separate
256 non-related sounds, to prevent the feedback of sound1's tail to sound2's
257 beginning - do grc3_reset on the state instances before calling
258 grc_convert.
260 *****************************************************************************
262 /* END CSTYLED */
264 void grc3_setup(grc3state_t *, uint32_t fromRate, uint32_t toRate);
266 void grc3_reset(grc3state_t *);
268 void grc3_convert(grc3state_t *, int quality,
269 const void *src, void *dst, int sz, int bufsz, int inc, int offset);
271 #ifdef __cplusplus
273 #endif
275 #endif /* AUDIO_GRC3_H */