1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate */
26*7c478bd9Sstevel@tonic-gate
27*7c478bd9Sstevel@tonic-gate #include <stdio.h>
28*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
29*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
30*7c478bd9Sstevel@tonic-gate #include <string.h>
31*7c478bd9Sstevel@tonic-gate #include <unistd.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/file.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
36*7c478bd9Sstevel@tonic-gate #include <Audio.h>
37*7c478bd9Sstevel@tonic-gate #include <AudioFile.h>
38*7c478bd9Sstevel@tonic-gate #include <AudioPipe.h>
39*7c478bd9Sstevel@tonic-gate #include <AudioRawPipe.h>
40*7c478bd9Sstevel@tonic-gate #include <AudioLib.h>
41*7c478bd9Sstevel@tonic-gate #include <AudioTypePcm.h>
42*7c478bd9Sstevel@tonic-gate #include <AudioTypeG72X.h>
43*7c478bd9Sstevel@tonic-gate #include <AudioTypeChannel.h>
44*7c478bd9Sstevel@tonic-gate #include <AudioTypeMux.h>
45*7c478bd9Sstevel@tonic-gate #include <AudioTypeSampleRate.h>
46*7c478bd9Sstevel@tonic-gate
47*7c478bd9Sstevel@tonic-gate #include <convert.h>
48*7c478bd9Sstevel@tonic-gate
49*7c478bd9Sstevel@tonic-gate
50*7c478bd9Sstevel@tonic-gate // Maximum sizes of buffer to convert, in seconds and bytes
51*7c478bd9Sstevel@tonic-gate #define CVTMAXTIME ((double)5.0)
52*7c478bd9Sstevel@tonic-gate #define CVTMAXBUF (64 * 1024)
53*7c478bd9Sstevel@tonic-gate
54*7c478bd9Sstevel@tonic-gate // maintain a list of conversions
55*7c478bd9Sstevel@tonic-gate struct conv_list {
56*7c478bd9Sstevel@tonic-gate struct conv_list *next; // next conversion in chain
57*7c478bd9Sstevel@tonic-gate unsigned bufcnt; // number of buffers to process
58*7c478bd9Sstevel@tonic-gate AudioTypeConvert* conv; // conversion class
59*7c478bd9Sstevel@tonic-gate AudioHdr hdr; // what to convert to
60*7c478bd9Sstevel@tonic-gate char *desc; // describe conversion (for errs)
61*7c478bd9Sstevel@tonic-gate };
62*7c478bd9Sstevel@tonic-gate
63*7c478bd9Sstevel@tonic-gate
64*7c478bd9Sstevel@tonic-gate // check if this is a valid conversion. return -1 if not, 0 if OK.
65*7c478bd9Sstevel@tonic-gate int
verify_conversion(AudioHdr ihdr,AudioHdr ohdr)66*7c478bd9Sstevel@tonic-gate verify_conversion(
67*7c478bd9Sstevel@tonic-gate AudioHdr ihdr,
68*7c478bd9Sstevel@tonic-gate AudioHdr ohdr)
69*7c478bd9Sstevel@tonic-gate {
70*7c478bd9Sstevel@tonic-gate char *enc1;
71*7c478bd9Sstevel@tonic-gate char *enc2;
72*7c478bd9Sstevel@tonic-gate
73*7c478bd9Sstevel@tonic-gate if (((ihdr.encoding != ULAW) &&
74*7c478bd9Sstevel@tonic-gate (ihdr.encoding != ALAW) &&
75*7c478bd9Sstevel@tonic-gate (ihdr.encoding != LINEAR) &&
76*7c478bd9Sstevel@tonic-gate (ihdr.encoding != FLOAT) &&
77*7c478bd9Sstevel@tonic-gate (ihdr.encoding != G721) &&
78*7c478bd9Sstevel@tonic-gate (ihdr.encoding != G723)) ||
79*7c478bd9Sstevel@tonic-gate ((ohdr.encoding != ULAW) &&
80*7c478bd9Sstevel@tonic-gate (ohdr.encoding != ALAW) &&
81*7c478bd9Sstevel@tonic-gate (ohdr.encoding != LINEAR) &&
82*7c478bd9Sstevel@tonic-gate (ohdr.encoding != FLOAT) &&
83*7c478bd9Sstevel@tonic-gate (ohdr.encoding != G721) &&
84*7c478bd9Sstevel@tonic-gate (ohdr.encoding != G723))) {
85*7c478bd9Sstevel@tonic-gate enc1 = ihdr.EncodingString();
86*7c478bd9Sstevel@tonic-gate enc2 = ohdr.EncodingString();
87*7c478bd9Sstevel@tonic-gate Err(MGET("can't convert from %s to %s\n"), enc1, enc2);
88*7c478bd9Sstevel@tonic-gate delete enc1;
89*7c478bd9Sstevel@tonic-gate delete enc2;
90*7c478bd9Sstevel@tonic-gate return (-1);
91*7c478bd9Sstevel@tonic-gate }
92*7c478bd9Sstevel@tonic-gate return (0);
93*7c478bd9Sstevel@tonic-gate }
94*7c478bd9Sstevel@tonic-gate
95*7c478bd9Sstevel@tonic-gate // check if this conversion is a no-op
96*7c478bd9Sstevel@tonic-gate int
noop_conversion(AudioHdr ihdr,AudioHdr ohdr,format_type i_fmt,format_type o_fmt,off_t i_offset,off_t)97*7c478bd9Sstevel@tonic-gate noop_conversion(
98*7c478bd9Sstevel@tonic-gate AudioHdr ihdr,
99*7c478bd9Sstevel@tonic-gate AudioHdr ohdr,
100*7c478bd9Sstevel@tonic-gate format_type i_fmt,
101*7c478bd9Sstevel@tonic-gate format_type o_fmt,
102*7c478bd9Sstevel@tonic-gate off_t i_offset,
103*7c478bd9Sstevel@tonic-gate off_t /* o_offset */)
104*7c478bd9Sstevel@tonic-gate {
105*7c478bd9Sstevel@tonic-gate if ((ihdr == ohdr) &&
106*7c478bd9Sstevel@tonic-gate (i_fmt == o_fmt) &&
107*7c478bd9Sstevel@tonic-gate (i_offset == 0)) {
108*7c478bd9Sstevel@tonic-gate return (1);
109*7c478bd9Sstevel@tonic-gate }
110*7c478bd9Sstevel@tonic-gate return (0);
111*7c478bd9Sstevel@tonic-gate }
112*7c478bd9Sstevel@tonic-gate
113*7c478bd9Sstevel@tonic-gate
114*7c478bd9Sstevel@tonic-gate // Conversion list maintenance routines
115*7c478bd9Sstevel@tonic-gate
116*7c478bd9Sstevel@tonic-gate // Return a pointer to the last conversion entry in the list
117*7c478bd9Sstevel@tonic-gate struct conv_list
get_last_conv(struct conv_list * list)118*7c478bd9Sstevel@tonic-gate *get_last_conv(
119*7c478bd9Sstevel@tonic-gate struct conv_list *list)
120*7c478bd9Sstevel@tonic-gate {
121*7c478bd9Sstevel@tonic-gate struct conv_list *lp;
122*7c478bd9Sstevel@tonic-gate
123*7c478bd9Sstevel@tonic-gate for (lp = list; lp != NULL; lp = lp->next) {
124*7c478bd9Sstevel@tonic-gate if (lp->next == NULL)
125*7c478bd9Sstevel@tonic-gate break;
126*7c478bd9Sstevel@tonic-gate }
127*7c478bd9Sstevel@tonic-gate return (lp);
128*7c478bd9Sstevel@tonic-gate }
129*7c478bd9Sstevel@tonic-gate
130*7c478bd9Sstevel@tonic-gate // Release the conversion list
131*7c478bd9Sstevel@tonic-gate void
free_conv_list(struct conv_list * & list)132*7c478bd9Sstevel@tonic-gate free_conv_list(
133*7c478bd9Sstevel@tonic-gate struct conv_list *&list)
134*7c478bd9Sstevel@tonic-gate {
135*7c478bd9Sstevel@tonic-gate unsigned int i;
136*7c478bd9Sstevel@tonic-gate unsigned int bufs;
137*7c478bd9Sstevel@tonic-gate struct conv_list *tlp;
138*7c478bd9Sstevel@tonic-gate AudioTypeConvert* conv;
139*7c478bd9Sstevel@tonic-gate
140*7c478bd9Sstevel@tonic-gate while (list != NULL) {
141*7c478bd9Sstevel@tonic-gate bufs = list->bufcnt;
142*7c478bd9Sstevel@tonic-gate conv = list->conv;
143*7c478bd9Sstevel@tonic-gate for (i = 0; i < bufs; i++) {
144*7c478bd9Sstevel@tonic-gate // Delete the conversion string
145*7c478bd9Sstevel@tonic-gate if (list[i].desc != NULL)
146*7c478bd9Sstevel@tonic-gate free(list[i].desc);
147*7c478bd9Sstevel@tonic-gate
148*7c478bd9Sstevel@tonic-gate // Delete the conversion class if unique
149*7c478bd9Sstevel@tonic-gate if ((list[i].conv != NULL) &&
150*7c478bd9Sstevel@tonic-gate ((i == 0) || (list[i].conv != conv)))
151*7c478bd9Sstevel@tonic-gate delete(list[i].conv);
152*7c478bd9Sstevel@tonic-gate }
153*7c478bd9Sstevel@tonic-gate tlp = list->next;
154*7c478bd9Sstevel@tonic-gate free((char *)list);
155*7c478bd9Sstevel@tonic-gate list = tlp;
156*7c478bd9Sstevel@tonic-gate }
157*7c478bd9Sstevel@tonic-gate }
158*7c478bd9Sstevel@tonic-gate
159*7c478bd9Sstevel@tonic-gate // Append a new entry on the end of the conversion list
160*7c478bd9Sstevel@tonic-gate void
append_conv_list(struct conv_list * & list,AudioHdr tohdr,unsigned int bufs,AudioTypeConvert * conv,char * desc)161*7c478bd9Sstevel@tonic-gate append_conv_list(
162*7c478bd9Sstevel@tonic-gate struct conv_list *&list, // list to modify
163*7c478bd9Sstevel@tonic-gate AudioHdr tohdr, // target format
164*7c478bd9Sstevel@tonic-gate unsigned int bufs, // number of buffers involved
165*7c478bd9Sstevel@tonic-gate AudioTypeConvert* conv, // NULL, if multiple buffers
166*7c478bd9Sstevel@tonic-gate char *desc) // string describing the transform
167*7c478bd9Sstevel@tonic-gate {
168*7c478bd9Sstevel@tonic-gate unsigned int i;
169*7c478bd9Sstevel@tonic-gate struct conv_list *lp;
170*7c478bd9Sstevel@tonic-gate struct conv_list *nlp;
171*7c478bd9Sstevel@tonic-gate Boolean B;
172*7c478bd9Sstevel@tonic-gate
173*7c478bd9Sstevel@tonic-gate nlp = new struct conv_list[bufs];
174*7c478bd9Sstevel@tonic-gate if (nlp == NULL) {
175*7c478bd9Sstevel@tonic-gate Err(MGET("out of memory\n"));
176*7c478bd9Sstevel@tonic-gate exit(1);
177*7c478bd9Sstevel@tonic-gate }
178*7c478bd9Sstevel@tonic-gate B = tohdr.Validate();
179*7c478bd9Sstevel@tonic-gate // Initialize a conversion entry for each expected buffer
180*7c478bd9Sstevel@tonic-gate for (i = 0; i < bufs; i++) {
181*7c478bd9Sstevel@tonic-gate nlp[i].next = NULL;
182*7c478bd9Sstevel@tonic-gate nlp[i].hdr = tohdr;
183*7c478bd9Sstevel@tonic-gate B = nlp[i].hdr.Validate();
184*7c478bd9Sstevel@tonic-gate nlp[i].bufcnt = bufs;
185*7c478bd9Sstevel@tonic-gate nlp[i].conv = conv;
186*7c478bd9Sstevel@tonic-gate if (desc && *desc) {
187*7c478bd9Sstevel@tonic-gate nlp[i].desc = strdup(desc);
188*7c478bd9Sstevel@tonic-gate } else {
189*7c478bd9Sstevel@tonic-gate nlp[i].desc = NULL;
190*7c478bd9Sstevel@tonic-gate }
191*7c478bd9Sstevel@tonic-gate }
192*7c478bd9Sstevel@tonic-gate
193*7c478bd9Sstevel@tonic-gate // Link in the new entry
194*7c478bd9Sstevel@tonic-gate if (list == NULL) {
195*7c478bd9Sstevel@tonic-gate list = nlp;
196*7c478bd9Sstevel@tonic-gate } else {
197*7c478bd9Sstevel@tonic-gate lp = get_last_conv(list);
198*7c478bd9Sstevel@tonic-gate lp->next = nlp;
199*7c478bd9Sstevel@tonic-gate }
200*7c478bd9Sstevel@tonic-gate }
201*7c478bd9Sstevel@tonic-gate
202*7c478bd9Sstevel@tonic-gate
203*7c478bd9Sstevel@tonic-gate // Routines to establish specific conversions.
204*7c478bd9Sstevel@tonic-gate // These routines append the proper conversion to the list, and update
205*7c478bd9Sstevel@tonic-gate // the audio header structure to reflect the resulting data format.
206*7c478bd9Sstevel@tonic-gate
207*7c478bd9Sstevel@tonic-gate // Multiplex/Demultiplex interleaved data
208*7c478bd9Sstevel@tonic-gate // If the data is multi-channel, demultiplex into multiple buffer streams.
209*7c478bd9Sstevel@tonic-gate // If there are multiple buffers, multiplex back into one interleaved stream.
210*7c478bd9Sstevel@tonic-gate AudioError
add_mux_convert(struct conv_list * & list,AudioHdr & ihdr,unsigned int & bufs)211*7c478bd9Sstevel@tonic-gate add_mux_convert(
212*7c478bd9Sstevel@tonic-gate struct conv_list *&list,
213*7c478bd9Sstevel@tonic-gate AudioHdr& ihdr,
214*7c478bd9Sstevel@tonic-gate unsigned int& bufs)
215*7c478bd9Sstevel@tonic-gate {
216*7c478bd9Sstevel@tonic-gate AudioTypeConvert* conv;
217*7c478bd9Sstevel@tonic-gate unsigned int n;
218*7c478bd9Sstevel@tonic-gate char *msg;
219*7c478bd9Sstevel@tonic-gate
220*7c478bd9Sstevel@tonic-gate conv = new AudioTypeMux;
221*7c478bd9Sstevel@tonic-gate
222*7c478bd9Sstevel@tonic-gate // Verify conversion
223*7c478bd9Sstevel@tonic-gate if (!conv->CanConvert(ihdr)) {
224*7c478bd9Sstevel@tonic-gate error: delete conv;
225*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_FORMATLOCK);
226*7c478bd9Sstevel@tonic-gate }
227*7c478bd9Sstevel@tonic-gate
228*7c478bd9Sstevel@tonic-gate if (bufs == 1) {
229*7c478bd9Sstevel@tonic-gate // Demultiplex multi-channel data
230*7c478bd9Sstevel@tonic-gate n = ihdr.channels; // save the target number of buffers
231*7c478bd9Sstevel@tonic-gate ihdr.channels = 1; // each output buffer will be mono
232*7c478bd9Sstevel@tonic-gate msg = MGET("Split multi-channel data");
233*7c478bd9Sstevel@tonic-gate } else {
234*7c478bd9Sstevel@tonic-gate // Multiplex multiple buffers
235*7c478bd9Sstevel@tonic-gate ihdr.channels = bufs; // set the target interleave
236*7c478bd9Sstevel@tonic-gate n = 1;
237*7c478bd9Sstevel@tonic-gate bufs = 1; // just one conversion necessary
238*7c478bd9Sstevel@tonic-gate msg = MGET("Interleave multi-channel data");
239*7c478bd9Sstevel@tonic-gate }
240*7c478bd9Sstevel@tonic-gate if (!conv->CanConvert(ihdr))
241*7c478bd9Sstevel@tonic-gate goto error;
242*7c478bd9Sstevel@tonic-gate
243*7c478bd9Sstevel@tonic-gate append_conv_list(list, ihdr, bufs, conv, msg);
244*7c478bd9Sstevel@tonic-gate bufs = n;
245*7c478bd9Sstevel@tonic-gate return (AUDIO_SUCCESS);
246*7c478bd9Sstevel@tonic-gate }
247*7c478bd9Sstevel@tonic-gate
248*7c478bd9Sstevel@tonic-gate // Convert to PCM (linear, ulaw, alaw)
249*7c478bd9Sstevel@tonic-gate AudioError
add_pcm_convert(struct conv_list * & list,AudioHdr & ihdr,AudioEncoding tofmt,unsigned int unitsz,unsigned int & bufs)250*7c478bd9Sstevel@tonic-gate add_pcm_convert(
251*7c478bd9Sstevel@tonic-gate struct conv_list *&list,
252*7c478bd9Sstevel@tonic-gate AudioHdr& ihdr,
253*7c478bd9Sstevel@tonic-gate AudioEncoding tofmt,
254*7c478bd9Sstevel@tonic-gate unsigned int unitsz,
255*7c478bd9Sstevel@tonic-gate unsigned int& bufs)
256*7c478bd9Sstevel@tonic-gate {
257*7c478bd9Sstevel@tonic-gate AudioTypeConvert* conv;
258*7c478bd9Sstevel@tonic-gate char msg[BUFSIZ];
259*7c478bd9Sstevel@tonic-gate char *infmt;
260*7c478bd9Sstevel@tonic-gate char *outfmt;
261*7c478bd9Sstevel@tonic-gate AudioError err;
262*7c478bd9Sstevel@tonic-gate
263*7c478bd9Sstevel@tonic-gate conv = new AudioTypePcm;
264*7c478bd9Sstevel@tonic-gate
265*7c478bd9Sstevel@tonic-gate // Verify conversion
266*7c478bd9Sstevel@tonic-gate if (!conv->CanConvert(ihdr)) {
267*7c478bd9Sstevel@tonic-gate error: delete conv;
268*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_FORMATLOCK);
269*7c478bd9Sstevel@tonic-gate }
270*7c478bd9Sstevel@tonic-gate
271*7c478bd9Sstevel@tonic-gate // Set up conversion, get encoding strings
272*7c478bd9Sstevel@tonic-gate infmt = ihdr.EncodingString();
273*7c478bd9Sstevel@tonic-gate ihdr.encoding = tofmt;
274*7c478bd9Sstevel@tonic-gate ihdr.bytes_per_unit = unitsz;
275*7c478bd9Sstevel@tonic-gate ihdr.samples_per_unit = 1;
276*7c478bd9Sstevel@tonic-gate if (!conv->CanConvert(ihdr))
277*7c478bd9Sstevel@tonic-gate goto error;
278*7c478bd9Sstevel@tonic-gate outfmt = ihdr.EncodingString();
279*7c478bd9Sstevel@tonic-gate
280*7c478bd9Sstevel@tonic-gate sprintf(msg, MGET("Convert %s to %s"), infmt, outfmt);
281*7c478bd9Sstevel@tonic-gate delete infmt;
282*7c478bd9Sstevel@tonic-gate delete outfmt;
283*7c478bd9Sstevel@tonic-gate
284*7c478bd9Sstevel@tonic-gate append_conv_list(list, ihdr, bufs, conv, msg);
285*7c478bd9Sstevel@tonic-gate return (AUDIO_SUCCESS);
286*7c478bd9Sstevel@tonic-gate }
287*7c478bd9Sstevel@tonic-gate
288*7c478bd9Sstevel@tonic-gate // Convert multi-channel data to mono, or vice versa
289*7c478bd9Sstevel@tonic-gate AudioError
add_channel_convert(struct conv_list * & list,AudioHdr & ihdr,unsigned int tochans,unsigned int & bufs)290*7c478bd9Sstevel@tonic-gate add_channel_convert(
291*7c478bd9Sstevel@tonic-gate struct conv_list *&list,
292*7c478bd9Sstevel@tonic-gate AudioHdr& ihdr,
293*7c478bd9Sstevel@tonic-gate unsigned int tochans,
294*7c478bd9Sstevel@tonic-gate unsigned int& bufs)
295*7c478bd9Sstevel@tonic-gate {
296*7c478bd9Sstevel@tonic-gate AudioTypeConvert* conv;
297*7c478bd9Sstevel@tonic-gate char msg[BUFSIZ];
298*7c478bd9Sstevel@tonic-gate char *inchans;
299*7c478bd9Sstevel@tonic-gate char *outchans;
300*7c478bd9Sstevel@tonic-gate AudioError err;
301*7c478bd9Sstevel@tonic-gate
302*7c478bd9Sstevel@tonic-gate // Make sure we're converting to/from mono with an interleaved buffer
303*7c478bd9Sstevel@tonic-gate if (((ihdr.channels != 1) && (tochans != 1)) || (bufs != 1))
304*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_FORMATLOCK);
305*7c478bd9Sstevel@tonic-gate
306*7c478bd9Sstevel@tonic-gate conv = new AudioTypeChannel;
307*7c478bd9Sstevel@tonic-gate
308*7c478bd9Sstevel@tonic-gate // Verify conversion; if no good, try converting to 16-bit pcm first
309*7c478bd9Sstevel@tonic-gate if (!conv->CanConvert(ihdr) || (ihdr.channels != 1)) {
310*7c478bd9Sstevel@tonic-gate if (err = add_pcm_convert(list, ihdr, LINEAR, 2, bufs)) {
311*7c478bd9Sstevel@tonic-gate delete conv;
312*7c478bd9Sstevel@tonic-gate return (err);
313*7c478bd9Sstevel@tonic-gate }
314*7c478bd9Sstevel@tonic-gate if (!conv->CanConvert(ihdr)) {
315*7c478bd9Sstevel@tonic-gate error: delete conv;
316*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_FORMATLOCK);
317*7c478bd9Sstevel@tonic-gate }
318*7c478bd9Sstevel@tonic-gate }
319*7c478bd9Sstevel@tonic-gate
320*7c478bd9Sstevel@tonic-gate // Set up conversion, get channel strings
321*7c478bd9Sstevel@tonic-gate inchans = ihdr.ChannelString();
322*7c478bd9Sstevel@tonic-gate ihdr.channels = tochans;
323*7c478bd9Sstevel@tonic-gate if (!conv->CanConvert(ihdr))
324*7c478bd9Sstevel@tonic-gate goto error;
325*7c478bd9Sstevel@tonic-gate outchans = ihdr.ChannelString();
326*7c478bd9Sstevel@tonic-gate
327*7c478bd9Sstevel@tonic-gate sprintf(msg, MGET("Convert %s to %s"), inchans, outchans);
328*7c478bd9Sstevel@tonic-gate delete inchans;
329*7c478bd9Sstevel@tonic-gate delete outchans;
330*7c478bd9Sstevel@tonic-gate
331*7c478bd9Sstevel@tonic-gate append_conv_list(list, ihdr, bufs, conv, msg);
332*7c478bd9Sstevel@tonic-gate return (AUDIO_SUCCESS);
333*7c478bd9Sstevel@tonic-gate }
334*7c478bd9Sstevel@tonic-gate
335*7c478bd9Sstevel@tonic-gate // Compress data
336*7c478bd9Sstevel@tonic-gate AudioError
add_compress(struct conv_list * & list,AudioHdr & ihdr,AudioEncoding tofmt,unsigned int unitsz,unsigned int & bufs)337*7c478bd9Sstevel@tonic-gate add_compress(
338*7c478bd9Sstevel@tonic-gate struct conv_list *&list,
339*7c478bd9Sstevel@tonic-gate AudioHdr& ihdr,
340*7c478bd9Sstevel@tonic-gate AudioEncoding tofmt,
341*7c478bd9Sstevel@tonic-gate unsigned int unitsz,
342*7c478bd9Sstevel@tonic-gate unsigned int& bufs)
343*7c478bd9Sstevel@tonic-gate {
344*7c478bd9Sstevel@tonic-gate AudioTypeConvert* conv;
345*7c478bd9Sstevel@tonic-gate char msg[BUFSIZ];
346*7c478bd9Sstevel@tonic-gate char *infmt;
347*7c478bd9Sstevel@tonic-gate char *outfmt;
348*7c478bd9Sstevel@tonic-gate struct conv_list *lp;
349*7c478bd9Sstevel@tonic-gate int i;
350*7c478bd9Sstevel@tonic-gate AudioError err;
351*7c478bd9Sstevel@tonic-gate
352*7c478bd9Sstevel@tonic-gate // Make sure we're converting something we understand
353*7c478bd9Sstevel@tonic-gate if ((tofmt != G721) && (tofmt != G723))
354*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_FORMATLOCK);
355*7c478bd9Sstevel@tonic-gate
356*7c478bd9Sstevel@tonic-gate conv = new AudioTypeG72X;
357*7c478bd9Sstevel@tonic-gate
358*7c478bd9Sstevel@tonic-gate // Verify conversion; if no good, try converting to 16-bit pcm first
359*7c478bd9Sstevel@tonic-gate if (!conv->CanConvert(ihdr)) {
360*7c478bd9Sstevel@tonic-gate if (err = add_pcm_convert(list, ihdr, LINEAR, 2, bufs)) {
361*7c478bd9Sstevel@tonic-gate delete conv;
362*7c478bd9Sstevel@tonic-gate return (err);
363*7c478bd9Sstevel@tonic-gate }
364*7c478bd9Sstevel@tonic-gate if (!conv->CanConvert(ihdr)) {
365*7c478bd9Sstevel@tonic-gate error: delete conv;
366*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_FORMATLOCK);
367*7c478bd9Sstevel@tonic-gate }
368*7c478bd9Sstevel@tonic-gate }
369*7c478bd9Sstevel@tonic-gate
370*7c478bd9Sstevel@tonic-gate // Set up conversion, get encoding strings
371*7c478bd9Sstevel@tonic-gate infmt = ihdr.EncodingString();
372*7c478bd9Sstevel@tonic-gate ihdr.encoding = tofmt;
373*7c478bd9Sstevel@tonic-gate switch (tofmt) {
374*7c478bd9Sstevel@tonic-gate case G721:
375*7c478bd9Sstevel@tonic-gate ihdr.bytes_per_unit = unitsz;
376*7c478bd9Sstevel@tonic-gate ihdr.samples_per_unit = 2;
377*7c478bd9Sstevel@tonic-gate break;
378*7c478bd9Sstevel@tonic-gate case G723:
379*7c478bd9Sstevel@tonic-gate ihdr.bytes_per_unit = unitsz;
380*7c478bd9Sstevel@tonic-gate ihdr.samples_per_unit = 8;
381*7c478bd9Sstevel@tonic-gate break;
382*7c478bd9Sstevel@tonic-gate }
383*7c478bd9Sstevel@tonic-gate if (!conv->CanConvert(ihdr))
384*7c478bd9Sstevel@tonic-gate goto error;
385*7c478bd9Sstevel@tonic-gate outfmt = ihdr.EncodingString();
386*7c478bd9Sstevel@tonic-gate
387*7c478bd9Sstevel@tonic-gate sprintf(msg, MGET("Convert %s to %s"), infmt, outfmt);
388*7c478bd9Sstevel@tonic-gate delete infmt;
389*7c478bd9Sstevel@tonic-gate delete outfmt;
390*7c478bd9Sstevel@tonic-gate
391*7c478bd9Sstevel@tonic-gate append_conv_list(list, ihdr, bufs, NULL, msg);
392*7c478bd9Sstevel@tonic-gate
393*7c478bd9Sstevel@tonic-gate // Need a separate converter instantiation for each channel
394*7c478bd9Sstevel@tonic-gate lp = get_last_conv(list);
395*7c478bd9Sstevel@tonic-gate for (i = 0; i < bufs; i++) {
396*7c478bd9Sstevel@tonic-gate if (i == 0)
397*7c478bd9Sstevel@tonic-gate lp[i].conv = conv;
398*7c478bd9Sstevel@tonic-gate else
399*7c478bd9Sstevel@tonic-gate lp[i].conv = new AudioTypeG72X;
400*7c478bd9Sstevel@tonic-gate }
401*7c478bd9Sstevel@tonic-gate return (AUDIO_SUCCESS);
402*7c478bd9Sstevel@tonic-gate }
403*7c478bd9Sstevel@tonic-gate
404*7c478bd9Sstevel@tonic-gate // Decompress data
405*7c478bd9Sstevel@tonic-gate AudioError
add_decompress(struct conv_list * & list,AudioHdr & ihdr,AudioEncoding tofmt,unsigned int unitsz,unsigned int & bufs)406*7c478bd9Sstevel@tonic-gate add_decompress(
407*7c478bd9Sstevel@tonic-gate struct conv_list *&list,
408*7c478bd9Sstevel@tonic-gate AudioHdr& ihdr,
409*7c478bd9Sstevel@tonic-gate AudioEncoding tofmt,
410*7c478bd9Sstevel@tonic-gate unsigned int unitsz,
411*7c478bd9Sstevel@tonic-gate unsigned int& bufs)
412*7c478bd9Sstevel@tonic-gate {
413*7c478bd9Sstevel@tonic-gate AudioTypeConvert* conv;
414*7c478bd9Sstevel@tonic-gate char msg[BUFSIZ];
415*7c478bd9Sstevel@tonic-gate char *infmt;
416*7c478bd9Sstevel@tonic-gate char *outfmt;
417*7c478bd9Sstevel@tonic-gate struct conv_list *lp;
418*7c478bd9Sstevel@tonic-gate int i;
419*7c478bd9Sstevel@tonic-gate AudioError err;
420*7c478bd9Sstevel@tonic-gate
421*7c478bd9Sstevel@tonic-gate // Make sure we're converting something we understand
422*7c478bd9Sstevel@tonic-gate if ((ihdr.encoding != G721) && (ihdr.encoding != G723))
423*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_FORMATLOCK);
424*7c478bd9Sstevel@tonic-gate
425*7c478bd9Sstevel@tonic-gate conv = new AudioTypeG72X;
426*7c478bd9Sstevel@tonic-gate
427*7c478bd9Sstevel@tonic-gate // Verify conversion
428*7c478bd9Sstevel@tonic-gate if (!conv->CanConvert(ihdr)) {
429*7c478bd9Sstevel@tonic-gate error: delete conv;
430*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_FORMATLOCK);
431*7c478bd9Sstevel@tonic-gate }
432*7c478bd9Sstevel@tonic-gate
433*7c478bd9Sstevel@tonic-gate // Set up conversion, get encoding strings
434*7c478bd9Sstevel@tonic-gate infmt = ihdr.EncodingString();
435*7c478bd9Sstevel@tonic-gate ihdr.encoding = tofmt;
436*7c478bd9Sstevel@tonic-gate ihdr.bytes_per_unit = unitsz;
437*7c478bd9Sstevel@tonic-gate ihdr.samples_per_unit = 1;
438*7c478bd9Sstevel@tonic-gate if (!conv->CanConvert(ihdr)) {
439*7c478bd9Sstevel@tonic-gate // Try converting to 16-bit linear
440*7c478bd9Sstevel@tonic-gate ihdr.encoding = LINEAR;
441*7c478bd9Sstevel@tonic-gate ihdr.bytes_per_unit = 2;
442*7c478bd9Sstevel@tonic-gate if (!conv->CanConvert(ihdr))
443*7c478bd9Sstevel@tonic-gate goto error;
444*7c478bd9Sstevel@tonic-gate }
445*7c478bd9Sstevel@tonic-gate outfmt = ihdr.EncodingString();
446*7c478bd9Sstevel@tonic-gate
447*7c478bd9Sstevel@tonic-gate sprintf(msg, MGET("Convert %s to %s"), infmt, outfmt);
448*7c478bd9Sstevel@tonic-gate delete infmt;
449*7c478bd9Sstevel@tonic-gate delete outfmt;
450*7c478bd9Sstevel@tonic-gate
451*7c478bd9Sstevel@tonic-gate append_conv_list(list, ihdr, bufs, NULL, msg);
452*7c478bd9Sstevel@tonic-gate
453*7c478bd9Sstevel@tonic-gate // Need a separate converter instantiation for each channel
454*7c478bd9Sstevel@tonic-gate lp = get_last_conv(list);
455*7c478bd9Sstevel@tonic-gate for (i = 0; i < bufs; i++) {
456*7c478bd9Sstevel@tonic-gate if (i == 0)
457*7c478bd9Sstevel@tonic-gate lp[i].conv = conv;
458*7c478bd9Sstevel@tonic-gate else
459*7c478bd9Sstevel@tonic-gate lp[i].conv = new AudioTypeG72X;
460*7c478bd9Sstevel@tonic-gate }
461*7c478bd9Sstevel@tonic-gate return (AUDIO_SUCCESS);
462*7c478bd9Sstevel@tonic-gate }
463*7c478bd9Sstevel@tonic-gate
464*7c478bd9Sstevel@tonic-gate // Sample rate conversion
465*7c478bd9Sstevel@tonic-gate AudioError
add_rate_convert(struct conv_list * & list,AudioHdr & ihdr,unsigned int torate,unsigned int & bufs)466*7c478bd9Sstevel@tonic-gate add_rate_convert(
467*7c478bd9Sstevel@tonic-gate struct conv_list *&list,
468*7c478bd9Sstevel@tonic-gate AudioHdr& ihdr,
469*7c478bd9Sstevel@tonic-gate unsigned int torate,
470*7c478bd9Sstevel@tonic-gate unsigned int& bufs)
471*7c478bd9Sstevel@tonic-gate {
472*7c478bd9Sstevel@tonic-gate AudioTypeConvert* conv;
473*7c478bd9Sstevel@tonic-gate unsigned int fromrate;
474*7c478bd9Sstevel@tonic-gate char msg[BUFSIZ];
475*7c478bd9Sstevel@tonic-gate char *inrate;
476*7c478bd9Sstevel@tonic-gate char *outrate;
477*7c478bd9Sstevel@tonic-gate struct conv_list *lp;
478*7c478bd9Sstevel@tonic-gate int i;
479*7c478bd9Sstevel@tonic-gate AudioError err;
480*7c478bd9Sstevel@tonic-gate
481*7c478bd9Sstevel@tonic-gate fromrate = ihdr.sample_rate;
482*7c478bd9Sstevel@tonic-gate conv = new AudioTypeSampleRate(fromrate, torate);
483*7c478bd9Sstevel@tonic-gate
484*7c478bd9Sstevel@tonic-gate // Verify conversion; if no good, try converting to 16-bit pcm first
485*7c478bd9Sstevel@tonic-gate if (!conv->CanConvert(ihdr)) {
486*7c478bd9Sstevel@tonic-gate if (err = add_pcm_convert(list, ihdr, LINEAR, 2, bufs)) {
487*7c478bd9Sstevel@tonic-gate delete conv;
488*7c478bd9Sstevel@tonic-gate return (err);
489*7c478bd9Sstevel@tonic-gate }
490*7c478bd9Sstevel@tonic-gate if (!conv->CanConvert(ihdr)) {
491*7c478bd9Sstevel@tonic-gate error: delete conv;
492*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_FORMATLOCK);
493*7c478bd9Sstevel@tonic-gate }
494*7c478bd9Sstevel@tonic-gate }
495*7c478bd9Sstevel@tonic-gate
496*7c478bd9Sstevel@tonic-gate // Set up conversion, get encoding strings
497*7c478bd9Sstevel@tonic-gate inrate = ihdr.RateString();
498*7c478bd9Sstevel@tonic-gate ihdr.sample_rate = torate;
499*7c478bd9Sstevel@tonic-gate if (!conv->CanConvert(ihdr))
500*7c478bd9Sstevel@tonic-gate goto error;
501*7c478bd9Sstevel@tonic-gate outrate = ihdr.RateString();
502*7c478bd9Sstevel@tonic-gate
503*7c478bd9Sstevel@tonic-gate sprintf(msg, MGET("Convert %s to %s"), inrate, outrate);
504*7c478bd9Sstevel@tonic-gate delete inrate;
505*7c478bd9Sstevel@tonic-gate delete outrate;
506*7c478bd9Sstevel@tonic-gate
507*7c478bd9Sstevel@tonic-gate append_conv_list(list, ihdr, bufs, NULL, msg);
508*7c478bd9Sstevel@tonic-gate
509*7c478bd9Sstevel@tonic-gate // Need a separate converter instantiation for each channel
510*7c478bd9Sstevel@tonic-gate lp = get_last_conv(list);
511*7c478bd9Sstevel@tonic-gate for (i = 0; i < bufs; i++) {
512*7c478bd9Sstevel@tonic-gate if (i == 0)
513*7c478bd9Sstevel@tonic-gate lp[i].conv = conv;
514*7c478bd9Sstevel@tonic-gate else
515*7c478bd9Sstevel@tonic-gate lp[i].conv = new AudioTypeSampleRate(fromrate, torate);
516*7c478bd9Sstevel@tonic-gate }
517*7c478bd9Sstevel@tonic-gate return (AUDIO_SUCCESS);
518*7c478bd9Sstevel@tonic-gate }
519*7c478bd9Sstevel@tonic-gate
520*7c478bd9Sstevel@tonic-gate // Returns TRUE if the specified header has a pcm type encoding
521*7c478bd9Sstevel@tonic-gate Boolean
pcmtype(AudioHdr & hdr)522*7c478bd9Sstevel@tonic-gate pcmtype(
523*7c478bd9Sstevel@tonic-gate AudioHdr& hdr)
524*7c478bd9Sstevel@tonic-gate {
525*7c478bd9Sstevel@tonic-gate if (hdr.samples_per_unit != 1)
526*7c478bd9Sstevel@tonic-gate return (FALSE);
527*7c478bd9Sstevel@tonic-gate switch (hdr.encoding) {
528*7c478bd9Sstevel@tonic-gate case LINEAR:
529*7c478bd9Sstevel@tonic-gate case FLOAT:
530*7c478bd9Sstevel@tonic-gate case ULAW:
531*7c478bd9Sstevel@tonic-gate case ALAW:
532*7c478bd9Sstevel@tonic-gate return (TRUE);
533*7c478bd9Sstevel@tonic-gate }
534*7c478bd9Sstevel@tonic-gate return (FALSE);
535*7c478bd9Sstevel@tonic-gate }
536*7c478bd9Sstevel@tonic-gate
537*7c478bd9Sstevel@tonic-gate #define IS_PCM(ihp) (pcmtype(ihp))
538*7c478bd9Sstevel@tonic-gate #define IS_MONO(ihp) (ihp.channels == 1)
539*7c478bd9Sstevel@tonic-gate #define RATE_CONV(ihp, ohp) (ihp.sample_rate != ohp.sample_rate)
540*7c478bd9Sstevel@tonic-gate #define ENC_CONV(ihp, ohp) ((ihp.encoding != ohp.encoding) || \
541*7c478bd9Sstevel@tonic-gate (ihp.samples_per_unit != \
542*7c478bd9Sstevel@tonic-gate ohp.samples_per_unit) || \
543*7c478bd9Sstevel@tonic-gate (ihp.bytes_per_unit != ohp.bytes_per_unit))
544*7c478bd9Sstevel@tonic-gate #define CHAN_CONV(ihp, ohp) (ihp.channels != ohp.channels)
545*7c478bd9Sstevel@tonic-gate
546*7c478bd9Sstevel@tonic-gate
547*7c478bd9Sstevel@tonic-gate // Build the conversion list to get from input to output format
548*7c478bd9Sstevel@tonic-gate AudioError
build_conversion_list(struct conv_list * & list,AudioStream * ifp,AudioStream * ofp)549*7c478bd9Sstevel@tonic-gate build_conversion_list(
550*7c478bd9Sstevel@tonic-gate struct conv_list *&list,
551*7c478bd9Sstevel@tonic-gate AudioStream* ifp,
552*7c478bd9Sstevel@tonic-gate AudioStream* ofp)
553*7c478bd9Sstevel@tonic-gate {
554*7c478bd9Sstevel@tonic-gate AudioHdr ihdr;
555*7c478bd9Sstevel@tonic-gate AudioHdr ohdr;
556*7c478bd9Sstevel@tonic-gate unsigned int bufs;
557*7c478bd9Sstevel@tonic-gate AudioError err;
558*7c478bd9Sstevel@tonic-gate
559*7c478bd9Sstevel@tonic-gate ihdr = ifp->GetHeader();
560*7c478bd9Sstevel@tonic-gate ohdr = ofp->GetHeader();
561*7c478bd9Sstevel@tonic-gate bufs = 1;
562*7c478bd9Sstevel@tonic-gate
563*7c478bd9Sstevel@tonic-gate // Each pass, add another conversion, until there's no more to do
564*7c478bd9Sstevel@tonic-gate while (((ihdr != ohdr) || (bufs != 1)) && !err) {
565*7c478bd9Sstevel@tonic-gate
566*7c478bd9Sstevel@tonic-gate // First off, if the target is mono, convert the source to mono
567*7c478bd9Sstevel@tonic-gate // before doing harder stuff, like sample rate conversion.
568*7c478bd9Sstevel@tonic-gate if (IS_MONO(ohdr)) {
569*7c478bd9Sstevel@tonic-gate if (!IS_MONO(ihdr)) {
570*7c478bd9Sstevel@tonic-gate if (IS_PCM(ihdr)) {
571*7c478bd9Sstevel@tonic-gate // If multi-channel pcm,
572*7c478bd9Sstevel@tonic-gate // mix the channels down to one
573*7c478bd9Sstevel@tonic-gate err = add_channel_convert(list,
574*7c478bd9Sstevel@tonic-gate ihdr, 1, bufs);
575*7c478bd9Sstevel@tonic-gate } else {
576*7c478bd9Sstevel@tonic-gate // If not pcm, demultiplex in order
577*7c478bd9Sstevel@tonic-gate // to decompress
578*7c478bd9Sstevel@tonic-gate err = add_mux_convert(list, ihdr, bufs);
579*7c478bd9Sstevel@tonic-gate }
580*7c478bd9Sstevel@tonic-gate continue;
581*7c478bd9Sstevel@tonic-gate } else if (bufs != 1) {
582*7c478bd9Sstevel@tonic-gate // Multi-channel data was demultiplexed
583*7c478bd9Sstevel@tonic-gate if (IS_PCM(ihdr)) {
584*7c478bd9Sstevel@tonic-gate // If multi-channel pcm, recombine them
585*7c478bd9Sstevel@tonic-gate // for mixing down to one
586*7c478bd9Sstevel@tonic-gate err = add_mux_convert(list, ihdr, bufs);
587*7c478bd9Sstevel@tonic-gate } else {
588*7c478bd9Sstevel@tonic-gate // If not pcm, decompress it
589*7c478bd9Sstevel@tonic-gate err = add_decompress(list, ihdr,
590*7c478bd9Sstevel@tonic-gate ohdr.encoding, ohdr.bytes_per_unit,
591*7c478bd9Sstevel@tonic-gate bufs);
592*7c478bd9Sstevel@tonic-gate }
593*7c478bd9Sstevel@tonic-gate continue;
594*7c478bd9Sstevel@tonic-gate }
595*7c478bd9Sstevel@tonic-gate // At this point, input and output are both mono
596*7c478bd9Sstevel@tonic-gate
597*7c478bd9Sstevel@tonic-gate } else if (ihdr.channels != 1) {
598*7c478bd9Sstevel@tonic-gate // Here if input and output are both multi-channel.
599*7c478bd9Sstevel@tonic-gate // If sample rate conversion or compression,
600*7c478bd9Sstevel@tonic-gate // split into multiple streams
601*7c478bd9Sstevel@tonic-gate if (RATE_CONV(ihdr, ohdr) ||
602*7c478bd9Sstevel@tonic-gate (ENC_CONV(ihdr, ohdr) &&
603*7c478bd9Sstevel@tonic-gate (!IS_PCM(ihdr) || !IS_PCM(ohdr)))) {
604*7c478bd9Sstevel@tonic-gate err = add_mux_convert(list, ihdr, bufs);
605*7c478bd9Sstevel@tonic-gate continue;
606*7c478bd9Sstevel@tonic-gate }
607*7c478bd9Sstevel@tonic-gate }
608*7c478bd9Sstevel@tonic-gate
609*7c478bd9Sstevel@tonic-gate // Input is either mono, split into multiple buffers, or
610*7c478bd9Sstevel@tonic-gate // this is a conversion that can be handled multi-channel.
611*7c478bd9Sstevel@tonic-gate if (RATE_CONV(ihdr, ohdr)) {
612*7c478bd9Sstevel@tonic-gate // Decompress before sample-rate conversion
613*7c478bd9Sstevel@tonic-gate if (!IS_PCM(ihdr)) {
614*7c478bd9Sstevel@tonic-gate err = add_decompress(list, ihdr,
615*7c478bd9Sstevel@tonic-gate ohdr.encoding, ohdr.bytes_per_unit,
616*7c478bd9Sstevel@tonic-gate bufs);
617*7c478bd9Sstevel@tonic-gate } else {
618*7c478bd9Sstevel@tonic-gate err = add_rate_convert(list, ihdr,
619*7c478bd9Sstevel@tonic-gate ohdr.sample_rate, bufs);
620*7c478bd9Sstevel@tonic-gate }
621*7c478bd9Sstevel@tonic-gate continue;
622*7c478bd9Sstevel@tonic-gate }
623*7c478bd9Sstevel@tonic-gate
624*7c478bd9Sstevel@tonic-gate if (ENC_CONV(ihdr, ohdr)) {
625*7c478bd9Sstevel@tonic-gate // Encoding is changing:
626*7c478bd9Sstevel@tonic-gate if (!IS_PCM(ihdr)) {
627*7c478bd9Sstevel@tonic-gate // if we start compressed, decompress
628*7c478bd9Sstevel@tonic-gate err = add_decompress(list, ihdr,
629*7c478bd9Sstevel@tonic-gate ohdr.encoding, ohdr.bytes_per_unit,
630*7c478bd9Sstevel@tonic-gate bufs);
631*7c478bd9Sstevel@tonic-gate } else if (IS_PCM(ohdr)) {
632*7c478bd9Sstevel@tonic-gate // we should be able to convert to PCM now
633*7c478bd9Sstevel@tonic-gate err = add_pcm_convert(list, ihdr,
634*7c478bd9Sstevel@tonic-gate ohdr.encoding, ohdr.bytes_per_unit,
635*7c478bd9Sstevel@tonic-gate bufs);
636*7c478bd9Sstevel@tonic-gate } else {
637*7c478bd9Sstevel@tonic-gate // we should be able to compress now
638*7c478bd9Sstevel@tonic-gate err = add_compress(list, ihdr,
639*7c478bd9Sstevel@tonic-gate ohdr.encoding, ohdr.bytes_per_unit,
640*7c478bd9Sstevel@tonic-gate bufs);
641*7c478bd9Sstevel@tonic-gate }
642*7c478bd9Sstevel@tonic-gate continue;
643*7c478bd9Sstevel@tonic-gate }
644*7c478bd9Sstevel@tonic-gate
645*7c478bd9Sstevel@tonic-gate // The sample rate and encoding match.
646*7c478bd9Sstevel@tonic-gate // All that's left to do is get the channels right
647*7c478bd9Sstevel@tonic-gate if (bufs > 1) {
648*7c478bd9Sstevel@tonic-gate // Combine channels back into an interleaved stream
649*7c478bd9Sstevel@tonic-gate err = add_mux_convert(list, ihdr, bufs);
650*7c478bd9Sstevel@tonic-gate continue;
651*7c478bd9Sstevel@tonic-gate }
652*7c478bd9Sstevel@tonic-gate if (!IS_MONO(ohdr)) {
653*7c478bd9Sstevel@tonic-gate // If multi-channel output, try to accomodate
654*7c478bd9Sstevel@tonic-gate err = add_channel_convert(list,
655*7c478bd9Sstevel@tonic-gate ihdr, ohdr.channels, bufs);
656*7c478bd9Sstevel@tonic-gate continue;
657*7c478bd9Sstevel@tonic-gate }
658*7c478bd9Sstevel@tonic-gate
659*7c478bd9Sstevel@tonic-gate // Everything should be done at this point.
660*7c478bd9Sstevel@tonic-gate // XXX - this should never be reached
661*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_FORMATLOCK);
662*7c478bd9Sstevel@tonic-gate }
663*7c478bd9Sstevel@tonic-gate return (err);
664*7c478bd9Sstevel@tonic-gate }
665*7c478bd9Sstevel@tonic-gate
666*7c478bd9Sstevel@tonic-gate // Set up the conversion list and execute it
667*7c478bd9Sstevel@tonic-gate int
do_convert(AudioStream * ifp,AudioStream * ofp)668*7c478bd9Sstevel@tonic-gate do_convert(
669*7c478bd9Sstevel@tonic-gate AudioStream* ifp,
670*7c478bd9Sstevel@tonic-gate AudioStream* ofp)
671*7c478bd9Sstevel@tonic-gate {
672*7c478bd9Sstevel@tonic-gate struct conv_list *list = NULL;
673*7c478bd9Sstevel@tonic-gate struct conv_list *lp;
674*7c478bd9Sstevel@tonic-gate AudioBuffer* obuf;
675*7c478bd9Sstevel@tonic-gate AudioBuffer** multibuf;
676*7c478bd9Sstevel@tonic-gate AudioError err;
677*7c478bd9Sstevel@tonic-gate AudioHdr ihdr;
678*7c478bd9Sstevel@tonic-gate AudioHdr ohdr;
679*7c478bd9Sstevel@tonic-gate Double pos = 0.0;
680*7c478bd9Sstevel@tonic-gate size_t len;
681*7c478bd9Sstevel@tonic-gate unsigned int i;
682*7c478bd9Sstevel@tonic-gate Double cvtlen;
683*7c478bd9Sstevel@tonic-gate char *msg1;
684*7c478bd9Sstevel@tonic-gate char *msg2;
685*7c478bd9Sstevel@tonic-gate
686*7c478bd9Sstevel@tonic-gate ihdr = ifp->GetHeader();
687*7c478bd9Sstevel@tonic-gate ohdr = ofp->GetHeader();
688*7c478bd9Sstevel@tonic-gate
689*7c478bd9Sstevel@tonic-gate // create conversion list
690*7c478bd9Sstevel@tonic-gate if ((err = build_conversion_list(list, ifp, ofp)) != AUDIO_SUCCESS) {
691*7c478bd9Sstevel@tonic-gate free_conv_list(list);
692*7c478bd9Sstevel@tonic-gate msg1 = ohdr.FormatString();
693*7c478bd9Sstevel@tonic-gate Err(MGET("Cannot convert %s to %s\n"), ifp->GetName(), msg1);
694*7c478bd9Sstevel@tonic-gate delete msg1;
695*7c478bd9Sstevel@tonic-gate return (-1);
696*7c478bd9Sstevel@tonic-gate }
697*7c478bd9Sstevel@tonic-gate
698*7c478bd9Sstevel@tonic-gate // Print warnings for exceptional conditions
699*7c478bd9Sstevel@tonic-gate if ((ohdr.sample_rate < 8000) || (ohdr.sample_rate > 48000)) {
700*7c478bd9Sstevel@tonic-gate msg1 = ohdr.RateString();
701*7c478bd9Sstevel@tonic-gate Err(MGET("Warning: converting %s to %s\n"),
702*7c478bd9Sstevel@tonic-gate ifp->GetName(), msg1);
703*7c478bd9Sstevel@tonic-gate delete msg1;
704*7c478bd9Sstevel@tonic-gate }
705*7c478bd9Sstevel@tonic-gate if (ohdr.channels > 2) {
706*7c478bd9Sstevel@tonic-gate msg1 = ohdr.ChannelString();
707*7c478bd9Sstevel@tonic-gate Err(MGET("Warning: converting %s to %s\n"),
708*7c478bd9Sstevel@tonic-gate ifp->GetName(), msg1);
709*7c478bd9Sstevel@tonic-gate delete msg1;
710*7c478bd9Sstevel@tonic-gate }
711*7c478bd9Sstevel@tonic-gate
712*7c478bd9Sstevel@tonic-gate if (Debug) {
713*7c478bd9Sstevel@tonic-gate msg1 = ihdr.FormatString();
714*7c478bd9Sstevel@tonic-gate msg2 = ohdr.FormatString();
715*7c478bd9Sstevel@tonic-gate Err(MGET("Converting %s:\n\t\tfrom: %s\n\t\tto: %s\n"),
716*7c478bd9Sstevel@tonic-gate ifp->GetName(), msg1, msg2);
717*7c478bd9Sstevel@tonic-gate delete msg1;
718*7c478bd9Sstevel@tonic-gate delete msg2;
719*7c478bd9Sstevel@tonic-gate
720*7c478bd9Sstevel@tonic-gate // Print each entry in the conversion list
721*7c478bd9Sstevel@tonic-gate for (lp = list; lp; lp = lp->next) {
722*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, MGET("\t%s %s\n"), lp->desc,
723*7c478bd9Sstevel@tonic-gate (lp->bufcnt == 1) ? "" : MGET("(multi-channel)"));
724*7c478bd9Sstevel@tonic-gate }
725*7c478bd9Sstevel@tonic-gate }
726*7c478bd9Sstevel@tonic-gate
727*7c478bd9Sstevel@tonic-gate // Calculate buffer size, obeying maximums
728*7c478bd9Sstevel@tonic-gate cvtlen = ihdr.Bytes_to_Time(CVTMAXBUF);
729*7c478bd9Sstevel@tonic-gate if (cvtlen > CVTMAXTIME)
730*7c478bd9Sstevel@tonic-gate cvtlen = CVTMAXTIME;
731*7c478bd9Sstevel@tonic-gate if (cvtlen > ohdr.Bytes_to_Time(CVTMAXBUF * 4))
732*7c478bd9Sstevel@tonic-gate cvtlen = ohdr.Bytes_to_Time(CVTMAXBUF * 4);
733*7c478bd9Sstevel@tonic-gate
734*7c478bd9Sstevel@tonic-gate // create output buf
735*7c478bd9Sstevel@tonic-gate if (!(obuf = new AudioBuffer(cvtlen, MGET("Audio Convert Buffer")))) {
736*7c478bd9Sstevel@tonic-gate Err(MGET("Can't create conversion buffer\n"));
737*7c478bd9Sstevel@tonic-gate exit(1);
738*7c478bd9Sstevel@tonic-gate }
739*7c478bd9Sstevel@tonic-gate
740*7c478bd9Sstevel@tonic-gate while (1) {
741*7c478bd9Sstevel@tonic-gate // Reset length
742*7c478bd9Sstevel@tonic-gate len = (size_t)ihdr.Time_to_Bytes(cvtlen);
743*7c478bd9Sstevel@tonic-gate if ((err = obuf->SetHeader(ihdr)) != AUDIO_SUCCESS) {
744*7c478bd9Sstevel@tonic-gate Err(MGET("Can't set buffer header: %s\n"), err.msg());
745*7c478bd9Sstevel@tonic-gate return (-1);
746*7c478bd9Sstevel@tonic-gate }
747*7c478bd9Sstevel@tonic-gate // If growing buffer, free the old one rather than copy data
748*7c478bd9Sstevel@tonic-gate if (obuf->GetSize() < cvtlen)
749*7c478bd9Sstevel@tonic-gate obuf->SetSize(0.);
750*7c478bd9Sstevel@tonic-gate obuf->SetSize(cvtlen);
751*7c478bd9Sstevel@tonic-gate
752*7c478bd9Sstevel@tonic-gate // Read a chunk of input and set the real length of buffer
753*7c478bd9Sstevel@tonic-gate // XXX - Use Copy() method?? Check for errors?
754*7c478bd9Sstevel@tonic-gate if (err = ifp->ReadData(obuf->GetAddress(), len, pos))
755*7c478bd9Sstevel@tonic-gate break;
756*7c478bd9Sstevel@tonic-gate obuf->SetLength(ihdr.Bytes_to_Time(len));
757*7c478bd9Sstevel@tonic-gate
758*7c478bd9Sstevel@tonic-gate // Process each entry in the conversion list
759*7c478bd9Sstevel@tonic-gate for (lp = list; lp; lp = lp->next) {
760*7c478bd9Sstevel@tonic-gate if (lp->conv) {
761*7c478bd9Sstevel@tonic-gate // If multiple buffers, make multiple calls
762*7c478bd9Sstevel@tonic-gate if (lp->bufcnt == 1) {
763*7c478bd9Sstevel@tonic-gate err = lp->conv->Convert(obuf, lp->hdr);
764*7c478bd9Sstevel@tonic-gate } else {
765*7c478bd9Sstevel@tonic-gate multibuf = (AudioBuffer**)obuf;
766*7c478bd9Sstevel@tonic-gate for (i = 0; i < lp->bufcnt; i++) {
767*7c478bd9Sstevel@tonic-gate err = lp[i].conv->Convert(
768*7c478bd9Sstevel@tonic-gate multibuf[i], lp[i].hdr);
769*7c478bd9Sstevel@tonic-gate if (err)
770*7c478bd9Sstevel@tonic-gate break;
771*7c478bd9Sstevel@tonic-gate }
772*7c478bd9Sstevel@tonic-gate }
773*7c478bd9Sstevel@tonic-gate if (err) {
774*7c478bd9Sstevel@tonic-gate Err(MGET(
775*7c478bd9Sstevel@tonic-gate "Conversion failed: %s (%s)\n"),
776*7c478bd9Sstevel@tonic-gate lp->desc ? lp->desc : MGET("???"),
777*7c478bd9Sstevel@tonic-gate err.msg());
778*7c478bd9Sstevel@tonic-gate return (-1);
779*7c478bd9Sstevel@tonic-gate }
780*7c478bd9Sstevel@tonic-gate }
781*7c478bd9Sstevel@tonic-gate }
782*7c478bd9Sstevel@tonic-gate
783*7c478bd9Sstevel@tonic-gate if ((err = write_output(obuf, ofp)) != AUDIO_SUCCESS) {
784*7c478bd9Sstevel@tonic-gate Err(MGET("Error writing to output file %s (%s)\n"),
785*7c478bd9Sstevel@tonic-gate ofp->GetName(), err.msg());
786*7c478bd9Sstevel@tonic-gate return (-1);
787*7c478bd9Sstevel@tonic-gate }
788*7c478bd9Sstevel@tonic-gate }
789*7c478bd9Sstevel@tonic-gate
790*7c478bd9Sstevel@tonic-gate // Now flush any left overs from conversions w/state
791*7c478bd9Sstevel@tonic-gate obuf->SetLength(0.0);
792*7c478bd9Sstevel@tonic-gate for (lp = list; lp; lp = lp->next) {
793*7c478bd9Sstevel@tonic-gate if (lp->conv) {
794*7c478bd9Sstevel@tonic-gate // First check if there's any residual to convert.
795*7c478bd9Sstevel@tonic-gate // If not, just set the header to this type.
796*7c478bd9Sstevel@tonic-gate // If multiple buffers, make multiple calls
797*7c478bd9Sstevel@tonic-gate if (lp->bufcnt == 1) {
798*7c478bd9Sstevel@tonic-gate err = lp->conv->Convert(obuf, lp->hdr);
799*7c478bd9Sstevel@tonic-gate if (!err)
800*7c478bd9Sstevel@tonic-gate err = lp->conv->Flush(obuf);
801*7c478bd9Sstevel@tonic-gate } else {
802*7c478bd9Sstevel@tonic-gate multibuf = (AudioBuffer**)obuf;
803*7c478bd9Sstevel@tonic-gate for (i = 0; i < lp->bufcnt; i++) {
804*7c478bd9Sstevel@tonic-gate err = lp[i].conv->Convert(
805*7c478bd9Sstevel@tonic-gate multibuf[i], lp[i].hdr);
806*7c478bd9Sstevel@tonic-gate if (!err) {
807*7c478bd9Sstevel@tonic-gate err = lp[i].conv->Flush(
808*7c478bd9Sstevel@tonic-gate multibuf[i]);
809*7c478bd9Sstevel@tonic-gate }
810*7c478bd9Sstevel@tonic-gate if (err)
811*7c478bd9Sstevel@tonic-gate break;
812*7c478bd9Sstevel@tonic-gate }
813*7c478bd9Sstevel@tonic-gate }
814*7c478bd9Sstevel@tonic-gate if (err) {
815*7c478bd9Sstevel@tonic-gate Err(MGET(
816*7c478bd9Sstevel@tonic-gate "Warning: Flush of final bytes failed: "
817*7c478bd9Sstevel@tonic-gate "%s (%s)\n"),
818*7c478bd9Sstevel@tonic-gate lp->desc ? lp->desc : MGET("???"),
819*7c478bd9Sstevel@tonic-gate err.msg());
820*7c478bd9Sstevel@tonic-gate
821*7c478bd9Sstevel@tonic-gate /* return (-1); ignore errors for now */
822*7c478bd9Sstevel@tonic-gate break;
823*7c478bd9Sstevel@tonic-gate }
824*7c478bd9Sstevel@tonic-gate }
825*7c478bd9Sstevel@tonic-gate }
826*7c478bd9Sstevel@tonic-gate
827*7c478bd9Sstevel@tonic-gate if (obuf->GetLength() > 0.0) {
828*7c478bd9Sstevel@tonic-gate if ((err = write_output(obuf, ofp)) != AUDIO_SUCCESS) {
829*7c478bd9Sstevel@tonic-gate Err(MGET("Warning: Final write to %s failed (%s)\n"),
830*7c478bd9Sstevel@tonic-gate ofp->GetName(), err.msg());
831*7c478bd9Sstevel@tonic-gate /* return (-1); ignore errors for now */
832*7c478bd9Sstevel@tonic-gate }
833*7c478bd9Sstevel@tonic-gate }
834*7c478bd9Sstevel@tonic-gate
835*7c478bd9Sstevel@tonic-gate delete obuf;
836*7c478bd9Sstevel@tonic-gate free_conv_list(list);
837*7c478bd9Sstevel@tonic-gate return (0);
838*7c478bd9Sstevel@tonic-gate }
839