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