xref: /third_party/libsnd/src/wavlike.c (revision b815c7f3)
1/*
2** Copyright (C) 1999-2020 Erik de Castro Lopo <erikd@mega-nerd.com>
3** Copyright (C) 2004-2005 David Viens <davidv@plogue.com>
4**
5** This program is free software; you can redistribute it and/or modify
6** it under the terms of the GNU Lesser General Public License as published by
7** the Free Software Foundation; either version 2.1 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13** GNU Lesser General Public License for more details.
14**
15** You should have received a copy of the GNU Lesser General Public License
16** along with this program; if not, write to the Free Software
17** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18*/
19
20#include	"sfconfig.h"
21
22#include	<stdio.h>
23#include	<string.h>
24#include	<ctype.h>
25#include	<time.h>
26
27#include	"sndfile.h"
28#include	"sfendian.h"
29#include	"common.h"
30#include	"wavlike.h"
31
32
33#define WAV_BEXT_MIN_CHUNK_SIZE		602
34#define WAV_BEXT_MAX_CHUNK_SIZE		(10 * 1024)
35
36#define WAV_CART_MIN_CHUNK_SIZE		2048
37#define WAV_CART_MAX_CHUNK_SIZE		0xffffffff
38
39
40static int 	exif_subchunk_parse	(SF_PRIVATE *psf, uint32_t length) ;
41
42
43/*  Known WAVEFORMATEXTENSIBLE GUIDS.  */
44static const EXT_SUBFORMAT MSGUID_SUBTYPE_PCM =
45{	0x00000001, 0x0000, 0x0010, {	0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
46} ;
47
48static const EXT_SUBFORMAT MSGUID_SUBTYPE_MS_ADPCM =
49{	0x00000002, 0x0000, 0x0010, {	0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
50} ;
51
52static const EXT_SUBFORMAT MSGUID_SUBTYPE_IEEE_FLOAT =
53{	0x00000003, 0x0000, 0x0010, {	0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
54} ;
55
56static const EXT_SUBFORMAT MSGUID_SUBTYPE_ALAW =
57{	0x00000006, 0x0000, 0x0010, {	0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
58} ;
59
60static const EXT_SUBFORMAT MSGUID_SUBTYPE_MULAW =
61{	0x00000007, 0x0000, 0x0010, {	0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
62} ;
63
64/*
65** the next two are from
66** http://dream.cs.bath.ac.uk/researchdev/wave-ex/bformat.html
67*/
68
69static const EXT_SUBFORMAT MSGUID_SUBTYPE_AMBISONIC_B_FORMAT_PCM =
70{	0x00000001, 0x0721, 0x11d3, {	0x86, 0x44, 0xc8, 0xc1, 0xca, 0x00, 0x00, 0x00 }
71} ;
72
73static const EXT_SUBFORMAT MSGUID_SUBTYPE_AMBISONIC_B_FORMAT_IEEE_FLOAT =
74{	0x00000003, 0x0721, 0x11d3, {	0x86, 0x44, 0xc8, 0xc1, 0xca, 0x00, 0x00, 0x00 }
75} ;
76
77
78#if 0
79/* maybe interesting one day to read the following through sf_read_raw */
80/* http://www.bath.ac.uk/~masrwd/pvocex/pvocex.html */
81static const EXT_SUBFORMAT MSGUID_SUBTYPE_PVOCEX =
82{	0x8312b9c2, 0x2e6e, 0x11d4, {	0xa8, 0x24, 0xde, 0x5b, 0x96, 0xc3, 0xab, 0x21 }
83} ;
84#endif
85
86/* This stores which bit in dwChannelMask maps to which channel */
87static const struct chanmap_s
88{	int id ;
89	const char * name ;
90} channel_mask_bits [] =
91{	/* WAVEFORMATEXTENSIBLE doesn't distuingish FRONT_LEFT from LEFT */
92	{	SF_CHANNEL_MAP_LEFT, "L" },
93	{	SF_CHANNEL_MAP_RIGHT, "R" },
94	{	SF_CHANNEL_MAP_CENTER, "C" },
95	{	SF_CHANNEL_MAP_LFE, "LFE" },
96	{	SF_CHANNEL_MAP_REAR_LEFT, "Ls" },
97	{	SF_CHANNEL_MAP_REAR_RIGHT, "Rs" },
98	{	SF_CHANNEL_MAP_FRONT_LEFT_OF_CENTER, "Lc" },
99	{	SF_CHANNEL_MAP_FRONT_RIGHT_OF_CENTER, "Rc" },
100	{	SF_CHANNEL_MAP_REAR_CENTER, "Cs" },
101	{	SF_CHANNEL_MAP_SIDE_LEFT, "Sl" },
102	{	SF_CHANNEL_MAP_SIDE_RIGHT, "Sr" },
103	{	SF_CHANNEL_MAP_TOP_CENTER, "Tc" },
104	{	SF_CHANNEL_MAP_TOP_FRONT_LEFT, "Tfl" },
105	{	SF_CHANNEL_MAP_TOP_FRONT_CENTER, "Tfc" },
106	{	SF_CHANNEL_MAP_TOP_FRONT_RIGHT, "Tfr" },
107	{	SF_CHANNEL_MAP_TOP_REAR_LEFT, "Trl" },
108	{	SF_CHANNEL_MAP_TOP_REAR_CENTER, "Trc" },
109	{	SF_CHANNEL_MAP_TOP_REAR_RIGHT, "Trr" },
110} ;
111
112/*------------------------------------------------------------------------------
113 * Private static functions.
114 */
115
116static int
117wavex_guid_equal (const EXT_SUBFORMAT * first, const EXT_SUBFORMAT * second)
118{	return !memcmp (first, second, sizeof (EXT_SUBFORMAT)) ;
119} /* wavex_guid_equal */
120
121
122
123int
124wavlike_read_fmt_chunk (SF_PRIVATE *psf, int fmtsize)
125{	WAVLIKE_PRIVATE * wpriv ;
126	WAV_FMT *wav_fmt ;
127	int	bytesread, k, bytespersec = 0 ;
128
129	if ((wpriv = psf->container_data) == NULL)
130		return SFE_INTERNAL ;
131	wav_fmt = &wpriv->wav_fmt ;
132
133	memset (wav_fmt, 0, sizeof (WAV_FMT)) ;
134
135	if (fmtsize < 16)
136		return SFE_WAV_FMT_SHORT ;
137
138	/* assume psf->rwf_endian is already properly set */
139
140	/* Read the minimal WAV file header here. */
141	bytesread = psf_binheader_readf (psf, "224422",
142					&(wav_fmt->format), &(wav_fmt->min.channels),
143					&(wav_fmt->min.samplerate), &(wav_fmt->min.bytespersec),
144					&(wav_fmt->min.blockalign), &(wav_fmt->min.bitwidth)) ;
145
146	psf_log_printf (psf, "  Format        : 0x%X => %s\n", wav_fmt->format, wavlike_format_str (wav_fmt->format)) ;
147	psf_log_printf (psf, "  Channels      : %d\n", wav_fmt->min.channels) ;
148	psf_log_printf (psf, "  Sample Rate   : %d\n", wav_fmt->min.samplerate) ;
149
150	if (wav_fmt->format == WAVE_FORMAT_PCM && wav_fmt->min.blockalign == 0
151		&& wav_fmt->min.bitwidth > 0 && wav_fmt->min.channels > 0)
152	{	wav_fmt->min.blockalign = wav_fmt->min.bitwidth / 8 + (wav_fmt->min.bitwidth % 8 > 0 ? 1 : 0) ;
153		wav_fmt->min.blockalign *= wav_fmt->min.channels ;
154		psf_log_printf (psf, "  Block Align   : 0 (should be %d)\n", wav_fmt->min.blockalign) ;
155		}
156	else
157		psf_log_printf (psf, "  Block Align   : %d\n", wav_fmt->min.blockalign) ;
158
159	if (wav_fmt->format == WAVE_FORMAT_PCM && wav_fmt->min.bitwidth == 24 &&
160			wav_fmt->min.blockalign == 4 * wav_fmt->min.channels)
161	{	psf_log_printf (psf, "  Bit Width     : 24\n") ;
162
163		psf_log_printf (psf, "\n"
164			"  Ambiguous information in 'fmt ' chunk. Possibile file types:\n"
165			"    0) Invalid IEEE float file generated by Syntrillium's Cooledit!\n"
166			"    1) File generated by ALSA's arecord containing 24 bit samples in 32 bit containers.\n"
167			"    2) 24 bit file with incorrect Block Align value.\n"
168			"\n") ;
169
170		wpriv->fmt_is_broken = 1 ;
171		}
172	else if (wav_fmt->min.bitwidth == 0)
173	{	switch (wav_fmt->format)
174		{	case WAVE_FORMAT_GSM610 :
175			case WAVE_FORMAT_IPP_ITU_G_723_1 :
176			case WAVE_FORMAT_MPEGLAYER3 :
177					psf_log_printf (psf, "  Bit Width     : %d\n", wav_fmt->min.bitwidth) ;
178					break ;
179			default :
180					psf_log_printf (psf, "  Bit Width     : %d (should not be 0)\n", wav_fmt->min.bitwidth) ;
181			}
182		}
183	else
184	{	switch (wav_fmt->format)
185		{	case WAVE_FORMAT_GSM610 :
186			case WAVE_FORMAT_IPP_ITU_G_723_1 :
187			case WAVE_FORMAT_MPEGLAYER3 :
188					psf_log_printf (psf, "  Bit Width     : %d (should be 0)\n", wav_fmt->min.bitwidth) ;
189					break ;
190			default :
191					psf_log_printf (psf, "  Bit Width     : %d\n", wav_fmt->min.bitwidth) ;
192			}
193		} ;
194
195	psf->sf.samplerate	= wav_fmt->min.samplerate ;
196	psf->sf.frames 		= 0 ;					/* Correct this when reading data chunk. */
197	psf->sf.channels	= wav_fmt->min.channels ;
198
199	switch (wav_fmt->format)
200	{	case WAVE_FORMAT_PCM :
201		case WAVE_FORMAT_IEEE_FLOAT :
202				bytespersec = wav_fmt->min.samplerate * wav_fmt->min.blockalign ;
203				if (wav_fmt->min.bytespersec != (unsigned) bytespersec)
204					psf_log_printf (psf, "  Bytes/sec     : %d (should be %d)\n", wav_fmt->min.bytespersec, bytespersec) ;
205				else
206					psf_log_printf (psf, "  Bytes/sec     : %d\n", wav_fmt->min.bytespersec) ;
207
208				psf->bytewidth = BITWIDTH2BYTES (wav_fmt->min.bitwidth) ;
209				break ;
210
211		case WAVE_FORMAT_ALAW :
212		case WAVE_FORMAT_MULAW :
213				if (wav_fmt->min.bytespersec != wav_fmt->min.samplerate * wav_fmt->min.blockalign)
214					psf_log_printf (psf, "  Bytes/sec     : %d (should be %d)\n", wav_fmt->min.bytespersec, wav_fmt->min.samplerate * wav_fmt->min.blockalign) ;
215				else
216					psf_log_printf (psf, "  Bytes/sec     : %d\n", wav_fmt->min.bytespersec) ;
217
218				psf->bytewidth = 1 ;
219				if (fmtsize >= 18)
220				{	bytesread += psf_binheader_readf (psf, "2", &(wav_fmt->size20.extrabytes)) ;
221					psf_log_printf (psf, "  Extra Bytes   : %d\n", wav_fmt->size20.extrabytes) ;
222					} ;
223				break ;
224
225		case WAVE_FORMAT_IMA_ADPCM :
226				if (wav_fmt->min.bitwidth != 4)
227					return SFE_WAV_ADPCM_NOT4BIT ;
228				if (wav_fmt->min.channels < 1 || wav_fmt->min.channels > 2)
229					return SFE_WAV_ADPCM_CHANNELS ;
230
231				bytesread += psf_binheader_readf (psf, "22", &(wav_fmt->ima.extrabytes), &(wav_fmt->ima.samplesperblock)) ;
232				psf_log_printf (psf, "  Extra Bytes   : %d\n", wav_fmt->ima.extrabytes) ;
233				if (wav_fmt->ima.samplesperblock < 1)
234				{	psf_log_printf (psf, "  Samples/Block : %d (should be > 0)\n", wav_fmt->ima.samplesperblock) ;
235					return SFE_WAV_ADPCM_SAMPLES ;
236					}
237				else
238					psf_log_printf (psf, "  Samples/Block : %d\n", wav_fmt->ima.samplesperblock) ;
239
240				bytespersec = (wav_fmt->ima.samplerate * wav_fmt->ima.blockalign) / wav_fmt->ima.samplesperblock ;
241				if (wav_fmt->ima.bytespersec != (unsigned) bytespersec)
242					psf_log_printf (psf, "  Bytes/sec     : %d (should be %d)\n", wav_fmt->ima.bytespersec, bytespersec) ;
243				else
244					psf_log_printf (psf, "  Bytes/sec     : %d\n", wav_fmt->ima.bytespersec) ;
245
246				break ;
247
248		case WAVE_FORMAT_MS_ADPCM :
249				if (wav_fmt->msadpcm.bitwidth != 4)
250					return SFE_WAV_ADPCM_NOT4BIT ;
251				if (wav_fmt->msadpcm.channels < 1 || wav_fmt->msadpcm.channels > 2)
252					return SFE_WAV_ADPCM_CHANNELS ;
253
254				bytesread += psf_binheader_readf (psf, "222", &(wav_fmt->msadpcm.extrabytes),
255								&(wav_fmt->msadpcm.samplesperblock), &(wav_fmt->msadpcm.numcoeffs)) ;
256
257				psf_log_printf (psf, "  Extra Bytes   : %d\n", wav_fmt->msadpcm.extrabytes) ;
258				if (wav_fmt->ima.samplesperblock < 1)
259				{	psf_log_printf (psf, "  Samples/Block : %d (should be > 0)\n", wav_fmt->ima.samplesperblock) ;
260					return SFE_WAV_ADPCM_SAMPLES ;
261					}
262				else
263					psf_log_printf (psf, "  Samples/Block : %d\n", wav_fmt->ima.samplesperblock) ;
264
265				bytespersec = (wav_fmt->min.samplerate * wav_fmt->min.blockalign) / wav_fmt->msadpcm.samplesperblock ;
266				if (wav_fmt->min.bytespersec == (unsigned) bytespersec)
267					psf_log_printf (psf, "  Bytes/sec     : %d\n", wav_fmt->min.bytespersec) ;
268				else if (wav_fmt->min.bytespersec == (wav_fmt->min.samplerate / wav_fmt->msadpcm.samplesperblock) * wav_fmt->min.blockalign)
269					psf_log_printf (psf, "  Bytes/sec     : %d (should be %d (MS BUG!))\n", wav_fmt->min.bytespersec, bytespersec) ;
270				else
271					psf_log_printf (psf, "  Bytes/sec     : %d (should be %d)\n", wav_fmt->min.bytespersec, bytespersec) ;
272
273				if (wav_fmt->msadpcm.numcoeffs > ARRAY_LEN (wav_fmt->msadpcm.coeffs))
274				{	psf_log_printf (psf, "  No. of Coeffs : %d (should be <= %d)\n", wav_fmt->msadpcm.numcoeffs, ARRAY_LEN (wav_fmt->msadpcm.coeffs)) ;
275					wav_fmt->msadpcm.numcoeffs = ARRAY_LEN (wav_fmt->msadpcm.coeffs) ;
276					}
277				else
278					psf_log_printf (psf, "  No. of Coeffs : %d\n", wav_fmt->msadpcm.numcoeffs) ;
279
280				psf_log_printf (psf, "    Index   Coeffs1   Coeffs2\n") ;
281				for (k = 0 ; k < wav_fmt->msadpcm.numcoeffs ; k++)
282				{	char buffer [128] ;
283
284					bytesread +=
285						psf_binheader_readf (psf, "22", &(wav_fmt->msadpcm.coeffs [k].coeff1), &(wav_fmt->msadpcm.coeffs [k].coeff2)) ;
286					snprintf (buffer, sizeof (buffer), "     %2d     %7d   %7d\n", k, wav_fmt->msadpcm.coeffs [k].coeff1, wav_fmt->msadpcm.coeffs [k].coeff2) ;
287					psf_log_printf (psf, buffer) ;
288					} ;
289				break ;
290
291		case WAVE_FORMAT_GSM610 :
292				if (wav_fmt->gsm610.channels != 1 || wav_fmt->gsm610.blockalign != 65)
293					return SFE_WAV_GSM610_FORMAT ;
294
295				bytesread +=
296				psf_binheader_readf (psf, "22", &(wav_fmt->gsm610.extrabytes), &(wav_fmt->gsm610.samplesperblock)) ;
297
298				if (wav_fmt->gsm610.samplesperblock != 320)
299					return SFE_WAV_GSM610_FORMAT ;
300
301				bytespersec = (wav_fmt->gsm610.samplerate * wav_fmt->gsm610.blockalign) / wav_fmt->gsm610.samplesperblock ;
302				if (wav_fmt->gsm610.bytespersec != (unsigned) bytespersec)
303					psf_log_printf (psf, "  Bytes/sec     : %d (should be %d)\n", wav_fmt->gsm610.bytespersec, bytespersec) ;
304				else
305					psf_log_printf (psf, "  Bytes/sec     : %d\n", wav_fmt->gsm610.bytespersec) ;
306
307				psf_log_printf (psf, "  Extra Bytes   : %d\n", wav_fmt->gsm610.extrabytes) ;
308				psf_log_printf (psf, "  Samples/Block : %d\n", wav_fmt->gsm610.samplesperblock) ;
309				break ;
310
311		case WAVE_FORMAT_MPEGLAYER3 :
312				bytesread += psf_binheader_readf (psf, "24222", &(wav_fmt->mpeg3.extrabytes),
313					&(wav_fmt->mpeg3.id), &(wav_fmt->mpeg3.flags), &(wav_fmt->mpeg3.blocksize),
314					&(wav_fmt->mpeg3.samplesperblock), &(wav_fmt->mpeg3.codecdelay)) ;
315
316				psf_log_printf (psf, "  Bytes/sec     : %d\n", wav_fmt->mpeg3.bytespersec) ;
317				psf_log_printf (psf, "  Extra Bytes   : %d\n", wav_fmt->mpeg3.extrabytes) ;
318				if (wav_fmt->mpeg3.id != 1)
319					psf_log_printf (psf, "  ID            : %d (unknown, should be 1)\n", wav_fmt->mpeg3.id) ;
320				else
321					psf_log_printf (psf, "  ID            : MPEGLAYER3_ID_MPEG\n") ;
322				psf_log_printf (psf, "  Flags         : 0x%08x\n", wav_fmt->mpeg3.flags) ;
323				psf_log_printf (psf, "  Block Size    : %d\n", wav_fmt->mpeg3.blocksize) ;
324				psf_log_printf (psf, "  Samples/Block : %d\n", wav_fmt->mpeg3.samplesperblock) ;
325				psf_log_printf (psf, "  Codec Delay   : %d samples\n", wav_fmt->mpeg3.codecdelay) ;
326				break ;
327
328		case WAVE_FORMAT_EXTENSIBLE :
329				if (wav_fmt->ext.bytespersec != wav_fmt->ext.samplerate * wav_fmt->ext.blockalign)
330					psf_log_printf (psf, "  Bytes/sec     : %d (should be %d)\n", wav_fmt->ext.bytespersec, wav_fmt->ext.samplerate * wav_fmt->ext.blockalign) ;
331				else
332					psf_log_printf (psf, "  Bytes/sec     : %d\n", wav_fmt->ext.bytespersec) ;
333
334				bytesread +=
335				psf_binheader_readf (psf, "224", &(wav_fmt->ext.extrabytes), &(wav_fmt->ext.validbits),
336						&(wav_fmt->ext.channelmask)) ;
337
338				psf_log_printf (psf, "  Valid Bits    : %d\n", wav_fmt->ext.validbits) ;
339
340				if (wav_fmt->ext.channelmask == 0)
341					psf_log_printf (psf, "  Channel Mask  : 0x0 (should not be zero)\n") ;
342				else
343				{	char buffer [512] ;
344					unsigned bit ;
345
346					wpriv->wavex_channelmask = wav_fmt->ext.channelmask ;
347
348					/* It's probably wise to ignore the channel mask if it is all zero */
349					free (psf->channel_map) ;
350
351					if ((psf->channel_map = calloc (psf->sf.channels, sizeof (psf->channel_map [0]))) == NULL)
352						return SFE_MALLOC_FAILED ;
353
354					/* Terminate the buffer we're going to append_snprintf into. */
355					buffer [0] = 0 ;
356
357					for (bit = k = 0 ; bit < ARRAY_LEN (channel_mask_bits) && k < psf->sf.channels ; bit++)
358					{
359						if (wav_fmt->ext.channelmask & (1 << bit))
360						{	if (k > psf->sf.channels)
361							{	psf_log_printf (psf, "*** More channel map bits than there are channels.\n") ;
362								break ;
363								} ;
364
365							psf->channel_map [k++] = channel_mask_bits [bit].id ;
366							append_snprintf (buffer, sizeof (buffer), "%s, ", channel_mask_bits [bit].name) ;
367							} ;
368						} ;
369
370					/* Remove trailing ", ". */
371					bit = strlen (buffer) ;
372					if (bit >= 2)
373					{	buffer [--bit] = 0 ;
374						buffer [--bit] = 0 ;
375						} ;
376
377					if (k != psf->sf.channels)
378					{	psf_log_printf (psf, "  Channel Mask  : 0x%X\n", wav_fmt->ext.channelmask) ;
379						psf_log_printf (psf, "*** Less channel map bits than there are channels.\n") ;
380						}
381					else
382						psf_log_printf (psf, "  Channel Mask  : 0x%X (%s)\n", wav_fmt->ext.channelmask, buffer) ;
383					} ;
384
385				bytesread += psf_binheader_readf (psf, "422", &(wav_fmt->ext.esf.esf_field1), &(wav_fmt->ext.esf.esf_field2), &(wav_fmt->ext.esf.esf_field3)) ;
386
387				/* compare the esf_fields with each known GUID? and print? */
388				psf_log_printf (psf, "  Subformat\n") ;
389				psf_log_printf (psf, "    esf_field1 : 0x%X\n", wav_fmt->ext.esf.esf_field1) ;
390				psf_log_printf (psf, "    esf_field2 : 0x%X\n", wav_fmt->ext.esf.esf_field2) ;
391				psf_log_printf (psf, "    esf_field3 : 0x%X\n", wav_fmt->ext.esf.esf_field3) ;
392				psf_log_printf (psf, "    esf_field4 : ") ;
393				for (k = 0 ; k < 8 ; k++)
394				{	bytesread += psf_binheader_readf (psf, "1", &(wav_fmt->ext.esf.esf_field4 [k])) ;
395					psf_log_printf (psf, "0x%X ", wav_fmt->ext.esf.esf_field4 [k] & 0xFF) ;
396					} ;
397				psf_log_printf (psf, "\n") ;
398				psf->bytewidth = BITWIDTH2BYTES (wav_fmt->ext.bitwidth) ;
399
400				/* Compare GUIDs for known ones. */
401				if (wavex_guid_equal (&wav_fmt->ext.esf, &MSGUID_SUBTYPE_PCM))
402				{	psf->sf.format = SF_FORMAT_WAVEX | u_bitwidth_to_subformat (psf->bytewidth * 8) ;
403					psf_log_printf (psf, "    format : pcm\n") ;
404					}
405				else if (wavex_guid_equal (&wav_fmt->ext.esf, &MSGUID_SUBTYPE_MS_ADPCM))
406				{	psf->sf.format = (SF_FORMAT_WAVEX | SF_FORMAT_MS_ADPCM) ;
407					psf_log_printf (psf, "    format : ms adpcm\n") ;
408					}
409				else if (wavex_guid_equal (&wav_fmt->ext.esf, &MSGUID_SUBTYPE_IEEE_FLOAT))
410				{	psf->sf.format = SF_FORMAT_WAVEX | ((psf->bytewidth == 8) ? SF_FORMAT_DOUBLE : SF_FORMAT_FLOAT) ;
411					psf_log_printf (psf, "    format : IEEE float\n") ;
412					}
413				else if (wavex_guid_equal (&wav_fmt->ext.esf, &MSGUID_SUBTYPE_ALAW))
414				{	psf->sf.format = (SF_FORMAT_WAVEX | SF_FORMAT_ALAW) ;
415					psf_log_printf (psf, "    format : A-law\n") ;
416					}
417				else if (wavex_guid_equal (&wav_fmt->ext.esf, &MSGUID_SUBTYPE_MULAW))
418				{	psf->sf.format = (SF_FORMAT_WAVEX | SF_FORMAT_ULAW) ;
419					psf_log_printf (psf, "    format : u-law\n") ;
420					}
421				else if (wavex_guid_equal (&wav_fmt->ext.esf, &MSGUID_SUBTYPE_AMBISONIC_B_FORMAT_PCM))
422				{	psf->sf.format = SF_FORMAT_WAVEX | u_bitwidth_to_subformat (psf->bytewidth * 8) ;
423					psf_log_printf (psf, "    format : pcm (Ambisonic B)\n") ;
424					wpriv->wavex_ambisonic = SF_AMBISONIC_B_FORMAT ;
425					}
426				else if (wavex_guid_equal (&wav_fmt->ext.esf, &MSGUID_SUBTYPE_AMBISONIC_B_FORMAT_IEEE_FLOAT))
427				{	psf->sf.format = SF_FORMAT_WAVEX | ((psf->bytewidth == 8) ? SF_FORMAT_DOUBLE : SF_FORMAT_FLOAT) ;
428					psf_log_printf (psf, "    format : IEEE float (Ambisonic B)\n") ;
429					wpriv->wavex_ambisonic = SF_AMBISONIC_B_FORMAT ;
430					}
431				else
432					return SFE_UNIMPLEMENTED ;
433
434				break ;
435
436		case WAVE_FORMAT_G721_ADPCM :
437				psf_log_printf (psf, "  Bytes/sec     : %d\n", wav_fmt->g72x.bytespersec) ;
438				if (fmtsize >= 20)
439				{	bytesread += psf_binheader_readf (psf, "22", &(wav_fmt->g72x.extrabytes), &(wav_fmt->g72x.auxblocksize)) ;
440					if (wav_fmt->g72x.extrabytes == 0)
441						psf_log_printf (psf, "  Extra Bytes   : %d (should be 2)\n", wav_fmt->g72x.extrabytes) ;
442					else
443						psf_log_printf (psf, "  Extra Bytes   : %d\n", wav_fmt->g72x.extrabytes) ;
444					psf_log_printf (psf, "  Aux Blk Size  : %d\n", wav_fmt->g72x.auxblocksize) ;
445					}
446				else if (fmtsize == 18)
447				{	bytesread += psf_binheader_readf (psf, "2", &(wav_fmt->g72x.extrabytes)) ;
448					psf_log_printf (psf, "  Extra Bytes   : %d%s\n", wav_fmt->g72x.extrabytes, wav_fmt->g72x.extrabytes != 0 ? " (should be 0)" : "") ;
449					}
450				else
451					psf_log_printf (psf, "*** 'fmt ' chunk should be bigger than this!\n") ;
452				break ;
453
454		case WAVE_FORMAT_NMS_VBXADPCM :
455				if (wav_fmt->min.channels != 1 || wav_fmt->min.bitwidth < 2 || wav_fmt->min.bitwidth * 20 + 2 != wav_fmt->min.blockalign)
456					return SFE_WAV_NMS_FORMAT ;
457
458				bytespersec = (wav_fmt->min.samplerate * wav_fmt->min.blockalign) / 160 ;
459				if (wav_fmt->min.bytespersec == (unsigned) bytespersec)
460					psf_log_printf (psf, "  Bytes/sec     : %d\n", wav_fmt->min.bytespersec) ;
461				else
462					psf_log_printf (psf, "  Bytes/sec     : %d (should be %d)\n", wav_fmt->min.bytespersec, bytespersec) ;
463				if (fmtsize >= 18)
464				{	bytesread += psf_binheader_readf (psf, "2", &(wav_fmt->size20.extrabytes)) ;
465					psf_log_printf (psf, "  Extra Bytes   : %d\n", wav_fmt->size20.extrabytes) ;
466					} ;
467				break ;
468
469		default :
470				psf_log_printf (psf, "*** No 'fmt ' chunk dumper for this format!\n") ;
471				return SFE_WAV_BAD_FMT ;
472		} ;
473
474	if (bytesread > fmtsize)
475	{	psf_log_printf (psf, "*** wavlike_read_fmt_chunk (bytesread > fmtsize)\n") ;
476		return SFE_WAV_BAD_FMT ;
477		}
478	else
479		psf_binheader_readf (psf, "j", fmtsize - bytesread) ;
480
481	psf->blockwidth = wav_fmt->min.channels * psf->bytewidth ;
482
483	return 0 ;
484} /* wavlike_read_fmt_chunk */
485
486void
487wavlike_write_guid (SF_PRIVATE *psf, const EXT_SUBFORMAT * subformat)
488{
489	psf_binheader_writef (psf, "422b", BHW4 (subformat->esf_field1),
490					BHW2 (subformat->esf_field2), BHW2 (subformat->esf_field3),
491					BHWv (subformat->esf_field4), BHWz (8)) ;
492} /* wavlike_write_guid */
493
494
495int
496wavlike_gen_channel_mask (const int *chan_map, int channels)
497{	int chan, mask = 0, bit = -1, last_bit = -1 ;
498
499	if (chan_map == NULL)
500		return 0 ;
501
502	for (chan = 0 ; chan < channels ; chan ++)
503	{	int k ;
504
505		for (k = bit + 1 ; k < ARRAY_LEN (channel_mask_bits) ; k++)
506			if (chan_map [chan] == channel_mask_bits [k].id)
507			{	bit = k ;
508				break ;
509				} ;
510
511		/* Check for bad sequence. */
512		if (bit <= last_bit)
513			return 0 ;
514
515		mask += 1 << bit ;
516		last_bit = bit ;
517		} ;
518
519	return mask ;
520} /* wavlike_gen_channel_mask */
521
522void
523wavlike_analyze (SF_PRIVATE *psf)
524{	unsigned char buffer [4096] ;
525	AUDIO_DETECT ad ;
526	int format = 0 ;
527
528	if (psf->is_pipe)
529	{	psf_log_printf (psf, "*** Error : Reading from a pipe. Can't analyze data section to figure out real data format.\n\n") ;
530		return ;
531		} ;
532
533	psf_log_printf (psf, "---------------------------------------------------\n"
534						"Format is known to be broken. Using detection code.\n") ;
535
536	/* Code goes here. */
537	ad.endianness = SF_ENDIAN_LITTLE ;
538	ad.channels = psf->sf.channels ;
539
540	psf_fseek (psf, 3 * 4 * 50, SEEK_SET) ;
541
542	while (psf_fread (buffer, 1, sizeof (buffer), psf) == sizeof (buffer))
543	{	format = audio_detect (psf, &ad, buffer, sizeof (buffer)) ;
544		if (format != 0)
545			break ;
546		} ;
547
548	/* Seek to start of DATA section. */
549	psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
550
551	if (format == 0)
552	{	psf_log_printf (psf, "wavlike_analyze : detection failed.\n") ;
553		return ;
554		} ;
555
556	switch (format)
557	{	case SF_FORMAT_PCM_32 :
558		case SF_FORMAT_FLOAT :
559			psf_log_printf (psf, "wavlike_analyze : found format : 0x%X\n", format) ;
560			psf->sf.format = (psf->sf.format & ~SF_FORMAT_SUBMASK) + format ;
561			psf->bytewidth = 4 ;
562			psf->blockwidth = psf->sf.channels * psf->bytewidth ;
563			break ;
564
565		case SF_FORMAT_PCM_24 :
566			psf_log_printf (psf, "wavlike_analyze : found format : 0x%X\n", format) ;
567			psf->sf.format = (psf->sf.format & ~SF_FORMAT_SUBMASK) + format ;
568			psf->bytewidth = 3 ;
569			psf->blockwidth = psf->sf.channels * psf->bytewidth ;
570			break ;
571
572		default :
573			psf_log_printf (psf, "wavlike_analyze : unhandled format : 0x%X\n", format) ;
574			break ;
575		} ;
576
577	return ;
578} /* wavlike_analyze */
579
580/*==============================================================================
581*/
582
583typedef struct
584{	int			ID ;
585	const char	*name ;
586} WAV_FORMAT_DESC ;
587
588#define STR(x)			#x
589#define FORMAT_TYPE(x)	{ x, STR (x) }
590
591static WAV_FORMAT_DESC wave_descs [] =
592{	FORMAT_TYPE	(WAVE_FORMAT_PCM),
593	FORMAT_TYPE (WAVE_FORMAT_MS_ADPCM),
594	FORMAT_TYPE (WAVE_FORMAT_IEEE_FLOAT),
595	FORMAT_TYPE (WAVE_FORMAT_VSELP),
596	FORMAT_TYPE (WAVE_FORMAT_IBM_CVSD),
597	FORMAT_TYPE (WAVE_FORMAT_ALAW),
598	FORMAT_TYPE (WAVE_FORMAT_MULAW),
599	FORMAT_TYPE (WAVE_FORMAT_OKI_ADPCM),
600	FORMAT_TYPE (WAVE_FORMAT_IMA_ADPCM),
601	FORMAT_TYPE (WAVE_FORMAT_MEDIASPACE_ADPCM),
602	FORMAT_TYPE (WAVE_FORMAT_SIERRA_ADPCM),
603	FORMAT_TYPE (WAVE_FORMAT_G723_ADPCM),
604	FORMAT_TYPE (WAVE_FORMAT_DIGISTD),
605	FORMAT_TYPE (WAVE_FORMAT_DIGIFIX),
606	FORMAT_TYPE (WAVE_FORMAT_DIALOGIC_OKI_ADPCM),
607	FORMAT_TYPE (WAVE_FORMAT_MEDIAVISION_ADPCM),
608	FORMAT_TYPE (WAVE_FORMAT_CU_CODEC),
609	FORMAT_TYPE (WAVE_FORMAT_YAMAHA_ADPCM),
610	FORMAT_TYPE (WAVE_FORMAT_SONARC),
611	FORMAT_TYPE (WAVE_FORMAT_DSPGROUP_TRUESPEECH),
612	FORMAT_TYPE (WAVE_FORMAT_ECHOSC1),
613	FORMAT_TYPE (WAVE_FORMAT_AUDIOFILE_AF36),
614	FORMAT_TYPE (WAVE_FORMAT_APTX),
615	FORMAT_TYPE (WAVE_FORMAT_AUDIOFILE_AF10),
616	FORMAT_TYPE (WAVE_FORMAT_PROSODY_1612),
617	FORMAT_TYPE (WAVE_FORMAT_LRC),
618	FORMAT_TYPE (WAVE_FORMAT_DOLBY_AC2),
619	FORMAT_TYPE (WAVE_FORMAT_GSM610),
620	FORMAT_TYPE (WAVE_FORMAT_MSNAUDIO),
621	FORMAT_TYPE (WAVE_FORMAT_ANTEX_ADPCME),
622	FORMAT_TYPE (WAVE_FORMAT_CONTROL_RES_VQLPC),
623	FORMAT_TYPE (WAVE_FORMAT_DIGIREAL),
624	FORMAT_TYPE (WAVE_FORMAT_DIGIADPCM),
625	FORMAT_TYPE (WAVE_FORMAT_CONTROL_RES_CR10),
626	FORMAT_TYPE (WAVE_FORMAT_NMS_VBXADPCM),
627	FORMAT_TYPE (WAVE_FORMAT_ROLAND_RDAC),
628	FORMAT_TYPE (WAVE_FORMAT_ECHOSC3),
629	FORMAT_TYPE (WAVE_FORMAT_ROCKWELL_ADPCM),
630	FORMAT_TYPE (WAVE_FORMAT_ROCKWELL_DIGITALK),
631	FORMAT_TYPE (WAVE_FORMAT_XEBEC),
632	FORMAT_TYPE (WAVE_FORMAT_G721_ADPCM),
633	FORMAT_TYPE (WAVE_FORMAT_G728_CELP),
634	FORMAT_TYPE (WAVE_FORMAT_MSG723),
635	FORMAT_TYPE (WAVE_FORMAT_MPEG),
636	FORMAT_TYPE (WAVE_FORMAT_RT24),
637	FORMAT_TYPE (WAVE_FORMAT_PAC),
638	FORMAT_TYPE (WAVE_FORMAT_MPEGLAYER3),
639	FORMAT_TYPE (WAVE_FORMAT_LUCENT_G723),
640	FORMAT_TYPE (WAVE_FORMAT_CIRRUS),
641	FORMAT_TYPE (WAVE_FORMAT_ESPCM),
642	FORMAT_TYPE (WAVE_FORMAT_VOXWARE),
643	FORMAT_TYPE (WAVE_FORMAT_CANOPUS_ATRAC),
644	FORMAT_TYPE (WAVE_FORMAT_G726_ADPCM),
645	FORMAT_TYPE (WAVE_FORMAT_G722_ADPCM),
646	FORMAT_TYPE (WAVE_FORMAT_DSAT),
647	FORMAT_TYPE (WAVE_FORMAT_DSAT_DISPLAY),
648	FORMAT_TYPE (WAVE_FORMAT_VOXWARE_BYTE_ALIGNED),
649	FORMAT_TYPE (WAVE_FORMAT_VOXWARE_AC8),
650	FORMAT_TYPE (WAVE_FORMAT_VOXWARE_AC10),
651	FORMAT_TYPE (WAVE_FORMAT_VOXWARE_AC16),
652	FORMAT_TYPE (WAVE_FORMAT_VOXWARE_AC20),
653	FORMAT_TYPE (WAVE_FORMAT_VOXWARE_RT24),
654	FORMAT_TYPE (WAVE_FORMAT_VOXWARE_RT29),
655	FORMAT_TYPE (WAVE_FORMAT_VOXWARE_RT29HW),
656	FORMAT_TYPE (WAVE_FORMAT_VOXWARE_VR12),
657	FORMAT_TYPE (WAVE_FORMAT_VOXWARE_VR18),
658	FORMAT_TYPE (WAVE_FORMAT_VOXWARE_TQ40),
659	FORMAT_TYPE (WAVE_FORMAT_SOFTSOUND),
660	FORMAT_TYPE (WAVE_FORMAT_VOXARE_TQ60),
661	FORMAT_TYPE (WAVE_FORMAT_MSRT24),
662	FORMAT_TYPE (WAVE_FORMAT_G729A),
663	FORMAT_TYPE (WAVE_FORMAT_MVI_MV12),
664	FORMAT_TYPE (WAVE_FORMAT_DF_G726),
665	FORMAT_TYPE (WAVE_FORMAT_DF_GSM610),
666	FORMAT_TYPE (WAVE_FORMAT_ONLIVE),
667	FORMAT_TYPE (WAVE_FORMAT_SBC24),
668	FORMAT_TYPE (WAVE_FORMAT_DOLBY_AC3_SPDIF),
669	FORMAT_TYPE (WAVE_FORMAT_ZYXEL_ADPCM),
670	FORMAT_TYPE (WAVE_FORMAT_PHILIPS_LPCBB),
671	FORMAT_TYPE (WAVE_FORMAT_PACKED),
672	FORMAT_TYPE (WAVE_FORMAT_RHETOREX_ADPCM),
673	FORMAT_TYPE (IBM_FORMAT_MULAW),
674	FORMAT_TYPE (IBM_FORMAT_ALAW),
675	FORMAT_TYPE (IBM_FORMAT_ADPCM),
676	FORMAT_TYPE (WAVE_FORMAT_VIVO_G723),
677	FORMAT_TYPE (WAVE_FORMAT_VIVO_SIREN),
678	FORMAT_TYPE (WAVE_FORMAT_DIGITAL_G723),
679	FORMAT_TYPE (WAVE_FORMAT_CREATIVE_ADPCM),
680	FORMAT_TYPE (WAVE_FORMAT_CREATIVE_FASTSPEECH8),
681	FORMAT_TYPE (WAVE_FORMAT_CREATIVE_FASTSPEECH10),
682	FORMAT_TYPE (WAVE_FORMAT_QUARTERDECK),
683	FORMAT_TYPE (WAVE_FORMAT_FM_TOWNS_SND),
684	FORMAT_TYPE (WAVE_FORMAT_BZV_DIGITAL),
685	FORMAT_TYPE (WAVE_FORMAT_VME_VMPCM),
686	FORMAT_TYPE (WAVE_FORMAT_OLIGSM),
687	FORMAT_TYPE (WAVE_FORMAT_OLIADPCM),
688	FORMAT_TYPE (WAVE_FORMAT_OLICELP),
689	FORMAT_TYPE (WAVE_FORMAT_OLISBC),
690	FORMAT_TYPE (WAVE_FORMAT_OLIOPR),
691	FORMAT_TYPE (WAVE_FORMAT_LH_CODEC),
692	FORMAT_TYPE (WAVE_FORMAT_NORRIS),
693	FORMAT_TYPE (WAVE_FORMAT_SOUNDSPACE_MUSICOMPRESS),
694	FORMAT_TYPE (WAVE_FORMAT_DVM),
695	FORMAT_TYPE (WAVE_FORMAT_INTERWAV_VSC112),
696	FORMAT_TYPE (WAVE_FORMAT_IPP_ITU_G_723_1),
697	FORMAT_TYPE (WAVE_FORMAT_EXTENSIBLE),
698} ;
699
700char const*
701wavlike_format_str (int k)
702{	int lower, upper, mid ;
703
704	lower = -1 ;
705	upper = sizeof (wave_descs) / sizeof (WAV_FORMAT_DESC) ;
706
707	/* binary search */
708	if ((wave_descs [0].ID <= k) && (k <= wave_descs [upper - 1].ID))
709	{
710		while (lower + 1 < upper)
711		{	mid = (upper + lower) / 2 ;
712
713			if (k == wave_descs [mid].ID)
714				return wave_descs [mid].name ;
715			if (k < wave_descs [mid].ID)
716				upper = mid ;
717			else
718				lower = mid ;
719			} ;
720		} ;
721
722	return "Unknown format" ;
723} /* wavlike_format_str */
724
725int
726wavlike_srate2blocksize (int srate_chan_product)
727{	if (srate_chan_product < 12000)
728		return 256 ;
729	if (srate_chan_product < 23000)
730		return 512 ;
731	if (srate_chan_product < 44000)
732		return 1024 ;
733	return 2048 ;
734} /* srate2blocksize */
735
736int
737wavlike_read_bext_chunk (SF_PRIVATE *psf, uint32_t chunksize)
738{
739	SF_BROADCAST_INFO_16K * b ;
740	uint32_t bytes = 0 ;
741
742	if (chunksize < WAV_BEXT_MIN_CHUNK_SIZE)
743	{	psf_log_printf (psf, "bext : %u (should be >= %d)\n", chunksize, WAV_BEXT_MIN_CHUNK_SIZE) ;
744		psf_binheader_readf (psf, "j", chunksize) ;
745		return 0 ;
746		} ;
747
748	if (chunksize > WAV_BEXT_MAX_CHUNK_SIZE)
749	{	psf_log_printf (psf, "bext : %u (should be < %d)\n", chunksize, WAV_BEXT_MAX_CHUNK_SIZE) ;
750		psf_binheader_readf (psf, "j", chunksize) ;
751		return 0 ;
752		} ;
753
754	if (chunksize >= sizeof (SF_BROADCAST_INFO_16K))
755	{	psf_log_printf (psf, "bext : %u too big to be handled\n", chunksize) ;
756		psf_binheader_readf (psf, "j", chunksize) ;
757		return 0 ;
758		} ;
759
760	psf_log_printf (psf, "bext : %u\n", chunksize) ;
761
762	if (!psf->broadcast_16k)
763	{	psf->broadcast_16k = broadcast_var_alloc () ;
764		if (!psf->broadcast_16k)
765		{	psf->error = SFE_MALLOC_FAILED ;
766			return psf->error ;
767			}
768		}
769	else
770	{	psf_log_printf (psf, "bext : found more than one bext chunk, using last one.\n") ;
771		memset (psf->broadcast_16k, 0, sizeof (SF_BROADCAST_INFO_16K)) ;
772		}
773
774	b = psf->broadcast_16k ;
775
776	bytes += psf_binheader_readf (psf, "b", b->description, sizeof (b->description)) ;
777	bytes += psf_binheader_readf (psf, "b", b->originator, sizeof (b->originator)) ;
778	bytes += psf_binheader_readf (psf, "b", b->originator_reference, sizeof (b->originator_reference)) ;
779	bytes += psf_binheader_readf (psf, "b", b->origination_date, sizeof (b->origination_date)) ;
780	bytes += psf_binheader_readf (psf, "b", b->origination_time, sizeof (b->origination_time)) ;
781	bytes += psf_binheader_readf (psf, "442", &b->time_reference_low, &b->time_reference_high, &b->version) ;
782	bytes += psf_binheader_readf (psf, "b", &b->umid, sizeof (b->umid)) ;
783	bytes += psf_binheader_readf (psf, "22", &b->loudness_value, &b->loudness_range) ;
784	bytes += psf_binheader_readf (psf, "222", &b->max_true_peak_level, &b->max_momentary_loudness, &b->max_shortterm_loudness) ;
785	bytes += psf_binheader_readf (psf, "j", 180) ;
786
787	if (chunksize > WAV_BEXT_MIN_CHUNK_SIZE)
788	{	/* File has coding history data. */
789
790		b->coding_history_size = chunksize - WAV_BEXT_MIN_CHUNK_SIZE ;
791
792		/* We do not parse the coding history */
793		bytes += psf_binheader_readf (psf, "b", BHWv (b->coding_history), BHWz (b->coding_history_size)) ;
794		} ;
795
796	if (bytes < chunksize)
797		psf_binheader_readf (psf, "j", BHWj (chunksize - bytes)) ;
798
799	return 0 ;
800} /* wavlike_read_bext_chunk */
801
802int
803wavlike_write_bext_chunk (SF_PRIVATE *psf)
804{	SF_BROADCAST_INFO_16K *b ;
805
806	if (psf->broadcast_16k == NULL)
807		return -1 ;
808
809	b = psf->broadcast_16k ;
810
811	psf_binheader_writef (psf, "m4", BHWm (bext_MARKER), BHW4 (WAV_BEXT_MIN_CHUNK_SIZE + b->coding_history_size)) ;
812
813	/*
814	**	Note that it is very important that the field widths of the SF_BROADCAST_INFO
815	**	struct match those of the bext chunk fields.
816	*/
817
818	psf_binheader_writef (psf, "b", BHWv (b->description), BHWz (sizeof (b->description))) ;
819	psf_binheader_writef (psf, "b", BHWv (b->originator), BHWz (sizeof (b->originator))) ;
820	psf_binheader_writef (psf, "b", BHWv (b->originator_reference), BHWz (sizeof (b->originator_reference))) ;
821	psf_binheader_writef (psf, "b", BHWv (b->origination_date), BHWz (sizeof (b->origination_date))) ;
822	psf_binheader_writef (psf, "b", BHWv (b->origination_time), BHWz (sizeof (b->origination_time))) ;
823	psf_binheader_writef (psf, "442", BHW4 (b->time_reference_low), BHW4 (b->time_reference_high), BHW2 (b->version)) ;
824	psf_binheader_writef (psf, "b", BHWv (b->umid), BHWz (sizeof (b->umid))) ;
825	psf_binheader_writef (psf, "22", BHW2 (b->loudness_value), BHW2 (b->loudness_range)) ;
826	psf_binheader_writef (psf, "222", BHW2 (b->max_true_peak_level), BHW2 (b->max_momentary_loudness), BHW2 (b->max_shortterm_loudness)) ;
827	psf_binheader_writef (psf, "z", BHWz (180)) ;
828
829	if (b->coding_history_size > 0)
830		psf_binheader_writef (psf, "b", BHWv (b->coding_history), BHWz (b->coding_history_size)) ;
831
832	return 0 ;
833} /* wavlike_write_bext_chunk */
834
835int
836wavlike_read_cart_chunk (SF_PRIVATE *psf, uint32_t chunksize)
837{	SF_CART_INFO_16K *c ;
838	uint32_t bytes = 0 ;
839	int k ;
840
841	if (chunksize < WAV_CART_MIN_CHUNK_SIZE)
842	{	psf_log_printf (psf, "cart : %u (should be >= %d)\n", chunksize, WAV_CART_MIN_CHUNK_SIZE) ;
843		psf_binheader_readf (psf, "j", chunksize) ;
844		return 0 ;
845		} ;
846	if (chunksize > WAV_CART_MAX_CHUNK_SIZE)
847	{	psf_log_printf (psf, "cart : %u (should be < %d)\n", chunksize, WAV_CART_MAX_CHUNK_SIZE) ;
848		psf_binheader_readf (psf, "j", chunksize) ;
849		return 0 ;
850		} ;
851
852	/*
853	**	SF_CART_INFO_16K has an extra field 'tag_text_size' that isn't part
854	**	of the chunk, so don't include it in the size check.
855	*/
856	if (chunksize >= sizeof (SF_CART_INFO_16K) - 4)
857	{	psf_log_printf (psf, "cart : %u too big to be handled\n", chunksize) ;
858		psf_binheader_readf (psf, "j", chunksize) ;
859		return 0 ;
860		} ;
861
862	psf_log_printf (psf, "cart : %u\n", chunksize) ;
863
864	if (psf->cart_16k)
865	{	psf_log_printf (psf, "  Found more than one cart chunk, using last one.\n") ;
866		free (psf->cart_16k) ;
867		psf->cart_16k = NULL ;
868		} ;
869
870	if ((psf->cart_16k = cart_var_alloc ()) == NULL)
871	{	psf->error = SFE_MALLOC_FAILED ;
872		return psf->error ;
873		} ;
874
875	c = psf->cart_16k ;
876	bytes += psf_binheader_readf (psf, "b", c->version, sizeof (c->version)) ;
877	bytes += psf_binheader_readf (psf, "b", c->title, sizeof (c->title)) ;
878	bytes += psf_binheader_readf (psf, "b", c->artist, sizeof (c->artist)) ;
879	bytes += psf_binheader_readf (psf, "b", c->cut_id, sizeof (c->cut_id)) ;
880	bytes += psf_binheader_readf (psf, "b", c->client_id, sizeof (c->client_id)) ;
881	bytes += psf_binheader_readf (psf, "b", c->category, sizeof (c->category)) ;
882	bytes += psf_binheader_readf (psf, "b", c->classification, sizeof (c->classification)) ;
883	bytes += psf_binheader_readf (psf, "b", c->out_cue, sizeof (c->out_cue)) ;
884	bytes += psf_binheader_readf (psf, "b", c->start_date, sizeof (c->start_date)) ;
885	bytes += psf_binheader_readf (psf, "b", c->start_time, sizeof (c->start_time)) ;
886	bytes += psf_binheader_readf (psf, "b", c->end_date, sizeof (c->end_date)) ;
887	bytes += psf_binheader_readf (psf, "b", c->end_time, sizeof (c->end_time)) ;
888	bytes += psf_binheader_readf (psf, "b", c->producer_app_id, sizeof (c->producer_app_id)) ;
889	bytes += psf_binheader_readf (psf, "b", c->producer_app_version, sizeof (c->producer_app_version)) ;
890	bytes += psf_binheader_readf (psf, "b", c->user_def, sizeof (c->user_def)) ;
891	bytes += psf_binheader_readf (psf, "e4", &c->level_reference, sizeof (c->level_reference)) ;
892
893	for (k = 0 ; k < ARRAY_LEN (c->post_timers) ; k++)
894		bytes += psf_binheader_readf (psf, "b4", &c->post_timers [k].usage, make_size_t (4), &c->post_timers [k].value) ;
895
896	bytes += psf_binheader_readf (psf, "b", c->reserved, sizeof (c->reserved)) ;
897	bytes += psf_binheader_readf (psf, "b", c->url, sizeof (c->url)) ;
898
899	if (chunksize > WAV_CART_MIN_CHUNK_SIZE)
900	{	/* File has tag text. */
901		c->tag_text_size = chunksize - WAV_CART_MIN_CHUNK_SIZE ;
902		bytes += psf_binheader_readf (psf, "b", c->tag_text, make_size_t (c->tag_text_size)) ;
903		} ;
904
905	if (bytes < chunksize)
906		psf_log_printf (psf, "  %d trailing bytes in cart chunk.\n", chunksize - bytes) ;
907
908	return 0 ;
909} /* wavlike_read_cart_chunk */
910
911int
912wavlike_write_cart_chunk (SF_PRIVATE *psf)
913{	SF_CART_INFO_16K *c ;
914	int k ;
915
916	if (psf->cart_16k == NULL)
917		return -1 ;
918
919	c = psf->cart_16k ;
920	psf_binheader_writef (psf, "m4", BHWm (cart_MARKER), BHW4 (WAV_CART_MIN_CHUNK_SIZE + c->tag_text_size)) ;
921	/*
922	**	Note that it is very important that the field widths of the SF_CART_INFO
923	**	struct match those of the cart chunk fields.
924	*/
925	psf_binheader_writef (psf, "b", BHWv (c->version), BHWz (sizeof (c->version))) ;
926	psf_binheader_writef (psf, "b", BHWv (c->title), BHWz (sizeof (c->title))) ;
927	psf_binheader_writef (psf, "b", BHWv (c->artist), BHWz (sizeof (c->artist))) ;
928	psf_binheader_writef (psf, "b", BHWv (c->cut_id), BHWz (sizeof (c->cut_id))) ;
929	psf_binheader_writef (psf, "b", BHWv (c->client_id), BHWz (sizeof (c->client_id))) ;
930	psf_binheader_writef (psf, "b", BHWv (c->category), BHWz (sizeof (c->category))) ;
931	psf_binheader_writef (psf, "b", BHWv (c->classification), BHWz (sizeof (c->classification))) ;
932	psf_binheader_writef (psf, "b", BHWv (c->out_cue), BHWz (sizeof (c->out_cue))) ;
933	psf_binheader_writef (psf, "b", BHWv (c->start_date), BHWz (sizeof (c->start_date))) ;
934	psf_binheader_writef (psf, "b", BHWv (c->start_time), BHWz (sizeof (c->start_time))) ;
935	psf_binheader_writef (psf, "b", BHWv (c->end_date), BHWz (sizeof (c->end_date))) ;
936	psf_binheader_writef (psf, "b", BHWv (c->end_time), BHWz (sizeof (c->end_time))) ;
937	psf_binheader_writef (psf, "b", BHWv (c->producer_app_id), BHWz (sizeof (c->producer_app_id))) ;
938	psf_binheader_writef (psf, "b", BHWv (c->producer_app_version), BHWz (sizeof (c->producer_app_version))) ;
939	psf_binheader_writef (psf, "b", BHWv (c->user_def), BHWz (sizeof (c->user_def))) ;
940	psf_binheader_writef (psf, "e4", BHW4 (c->level_reference)) ;
941
942	for (k = 0 ; k < ARRAY_LEN (c->post_timers) ; k++)
943		psf_binheader_writef (psf, "b4", BHWv (c->post_timers [k].usage), BHWz (4), BHW4 (c->post_timers [k].value)) ;
944
945	psf_binheader_writef (psf, "z", BHWz (sizeof (c->reserved))) ;	// just write zeros, we don't have any other use for it
946	psf_binheader_writef (psf, "b", BHWv (c->url), BHWz (sizeof (c->url))) ;
947
948	if (c->tag_text_size > 0)
949		psf_binheader_writef (psf, "b", BHWv (c->tag_text), BHWz (c->tag_text_size)) ;
950
951	return 0 ;
952} /* wavlike_write_cart_chunk */
953
954int
955wavlike_subchunk_parse (SF_PRIVATE *psf, int chunk, uint32_t chunk_length)
956{	sf_count_t	current_pos ;
957	char		buffer [2048] ;
958	uint32_t 	chunk_size, bytesread = 0 ;
959
960	current_pos = psf_fseek (psf, 0, SEEK_CUR) ;
961
962	if (chunk_length <= 8)
963	{	/* This case is for broken files generated by PEAK. */
964		psf_log_printf (psf, "%M : %u (weird length)\n", chunk, chunk_length) ;
965		psf_binheader_readf (psf, "mj", &chunk, chunk_length - 4) ;
966		psf_log_printf (psf, "  %M\n", chunk) ;
967		return 0 ;
968		} ;
969
970	if (current_pos + chunk_length > psf->filelength)
971	{	psf_log_printf (psf, "%M : %u (should be %d)\n", chunk, chunk_length, (int) (psf->filelength - current_pos)) ;
972		chunk_length = psf->filelength - current_pos ;
973		}
974	else
975		psf_log_printf (psf, "%M : %u\n", chunk, chunk_length) ;
976
977	while (bytesread < chunk_length)
978	{	uint32_t thisread ;
979
980		if ((thisread = psf_binheader_readf (psf, "m", &chunk)) == 0)
981			break ;
982		bytesread += thisread ;
983
984		switch (chunk)
985		{	case adtl_MARKER :
986			case INFO_MARKER :
987					/* These markers don't contain anything, not even a chunk length. */
988					psf_log_printf (psf, "  %M\n", chunk) ;
989					continue ;
990
991			case exif_MARKER :
992					psf_log_printf (psf, "  %M\n", chunk) ;
993					if (chunk_length > bytesread)
994						bytesread += exif_subchunk_parse (psf, chunk_length - bytesread) ;
995					continue ;
996
997			case data_MARKER :
998					psf_log_printf (psf, "  %M inside a LIST block??? Backing out.\n", chunk) ;
999					/* Jump back four bytes and return to caller. */
1000					psf_binheader_readf (psf, "j", -4) ;
1001					return 0 ;
1002
1003			case 0 :
1004					/*
1005					**	Four zero bytes where a marker was expected. Assume this means
1006					**	the rest of the chunk is garbage.
1007					*/
1008					psf_log_printf (psf, "    *** Found weird-ass zero marker. Jumping to end of chunk.\n") ;
1009					goto cleanup_subchunk_parse ;
1010
1011			default :
1012					break ;
1013			} ;
1014
1015		switch (chunk)
1016		{	case ISFT_MARKER :
1017			case ICOP_MARKER :
1018			case IARL_MARKER :
1019			case IART_MARKER :
1020			case ICMT_MARKER :
1021			case ICRD_MARKER :
1022			case IENG_MARKER :
1023			case IGNR_MARKER :
1024			case INAM_MARKER :
1025			case IPRD_MARKER :
1026			case ISBJ_MARKER :
1027			case ISRC_MARKER :
1028			case IAUT_MARKER :
1029			case ITRK_MARKER :
1030					bytesread += psf_binheader_readf (psf, "4", &chunk_size) ;
1031					chunk_size += (chunk_size & 1) ;
1032					if (chunk_size >= SIGNED_SIZEOF (buffer) || bytesread + chunk_size > chunk_length)
1033					{	psf_log_printf (psf, "  *** %M : %u (too big)\n", chunk, chunk_size) ;
1034						goto cleanup_subchunk_parse ;
1035						} ;
1036
1037					bytesread += psf_binheader_readf (psf, "b", buffer, chunk_size) ;
1038					buffer [chunk_size] = 0 ;
1039					psf_log_printf (psf, "    %M : %s\n", chunk, buffer) ;
1040					break ;
1041
1042			case labl_MARKER :
1043					{	int mark_id ;
1044
1045						bytesread += psf_binheader_readf (psf, "44", &chunk_size, &mark_id) ;
1046						chunk_size -= 4 ;
1047						chunk_size += (chunk_size & 1) ;
1048						if (chunk_size < 1 || chunk_size >= SIGNED_SIZEOF (buffer) || bytesread + chunk_size > chunk_length)
1049						{	psf_log_printf (psf, "  *** %M : %u (too big)\n", chunk, chunk_size) ;
1050							goto cleanup_subchunk_parse ;
1051							} ;
1052
1053						bytesread += psf_binheader_readf (psf, "b", buffer, chunk_size) ;
1054						buffer [chunk_size] = 0 ;
1055
1056						if (mark_id < 10) /* avoid swamping log buffer with labels */
1057							psf_log_printf (psf, "    %M : %u : %s\n", chunk, mark_id, buffer) ;
1058						else if (mark_id == 10)
1059							psf_log_printf (psf, "    (Skipping)\n") ;
1060
1061						if (psf->cues)
1062 						{	unsigned int i = 0 ;
1063
1064							/* find id to store label */
1065							while (i < psf->cues->cue_count && psf->cues->cue_points [i].indx != mark_id)
1066								i++ ;
1067
1068							if (i < psf->cues->cue_count)
1069								memcpy (psf->cues->cue_points [i].name, buffer, sizeof (psf->cues->cue_points [i].name)) ;
1070							} ;
1071						} ;
1072					break ;
1073
1074			case DISP_MARKER :
1075			case ltxt_MARKER :
1076			case note_MARKER :
1077					bytesread += psf_binheader_readf (psf, "4", &chunk_size) ;
1078					chunk_size += (chunk_size & 1) ;
1079					if (chunk_size >= SIGNED_SIZEOF (buffer) || bytesread + chunk_size > chunk_length)
1080					{	psf_log_printf (psf, "  *** %M : %u (too big)\n", chunk, chunk_size) ;
1081						goto cleanup_subchunk_parse ;
1082						} ;
1083
1084					psf_log_printf (psf, "    %M : %u\n", chunk, chunk_size) ;
1085					goto cleanup_subchunk_parse ;
1086
1087			default :
1088					bytesread += psf_binheader_readf (psf, "4", &chunk_size) ;
1089					chunk_size += (chunk_size & 1) ;
1090					if (bytesread + chunk_size > chunk_length)
1091					{	psf_log_printf (psf, "  *** %M : %u (too big)\n", chunk, chunk_size) ;
1092						goto cleanup_subchunk_parse ;
1093						}
1094					else
1095					{	psf_log_printf (psf, "    %M : %u\n", chunk, chunk_size) ;
1096						bytesread += psf_binheader_readf (psf, "j", chunk_size) ;
1097						} ;
1098					break ;
1099			} ;
1100
1101		switch (chunk)
1102		{	case ISFT_MARKER :
1103					psf_store_string (psf, SF_STR_SOFTWARE, buffer) ;
1104					break ;
1105			case ICOP_MARKER :
1106					psf_store_string (psf, SF_STR_COPYRIGHT, buffer) ;
1107					break ;
1108			case INAM_MARKER :
1109					psf_store_string (psf, SF_STR_TITLE, buffer) ;
1110					break ;
1111			case IART_MARKER :
1112					psf_store_string (psf, SF_STR_ARTIST, buffer) ;
1113					break ;
1114			case ICMT_MARKER :
1115					psf_store_string (psf, SF_STR_COMMENT, buffer) ;
1116					break ;
1117			case ICRD_MARKER :
1118					psf_store_string (psf, SF_STR_DATE, buffer) ;
1119					break ;
1120			case IGNR_MARKER :
1121					psf_store_string (psf, SF_STR_GENRE, buffer) ;
1122					break ;
1123			case IPRD_MARKER :
1124					psf_store_string (psf, SF_STR_ALBUM, buffer) ;
1125					break ;
1126			case ITRK_MARKER :
1127					psf_store_string (psf, SF_STR_TRACKNUMBER, buffer) ;
1128					break ;
1129			} ;
1130		} ;
1131
1132cleanup_subchunk_parse :
1133
1134	if (chunk_length > bytesread)
1135		bytesread += psf_binheader_readf (psf, "j", chunk_length - bytesread) ;
1136
1137	return 0 ;
1138} /* wavlike_subchunk_parse */
1139
1140void
1141wavlike_write_strings (SF_PRIVATE *psf, int location)
1142{	int	k, prev_head_index, saved_head_index ;
1143
1144	if (psf_location_string_count (psf, location) == 0)
1145		return ;
1146
1147	prev_head_index = psf->header.indx + 4 ;
1148
1149	psf_binheader_writef (psf, "m4m", BHWm (LIST_MARKER), BHW4 (0xBADBAD), BHWm (INFO_MARKER)) ;
1150
1151	for (k = 0 ; k < SF_MAX_STRINGS ; k++)
1152	{	if (psf->strings.data [k].type == 0)
1153			break ;
1154		if (psf->strings.data [k].type < 0 || psf->strings.data [k].flags != location)
1155			continue ;
1156
1157		switch (psf->strings.data [k].type)
1158		{	case SF_STR_SOFTWARE :
1159				psf_binheader_writef (psf, "ms", BHWm (ISFT_MARKER), BHWs (psf->strings.storage + psf->strings.data [k].offset)) ;
1160				break ;
1161
1162			case SF_STR_TITLE :
1163				psf_binheader_writef (psf, "ms", BHWm (INAM_MARKER), BHWs (psf->strings.storage + psf->strings.data [k].offset)) ;
1164				break ;
1165
1166			case SF_STR_COPYRIGHT :
1167				psf_binheader_writef (psf, "ms", BHWm (ICOP_MARKER), BHWs (psf->strings.storage + psf->strings.data [k].offset)) ;
1168				break ;
1169
1170			case SF_STR_ARTIST :
1171				psf_binheader_writef (psf, "ms", BHWm (IART_MARKER), BHWs (psf->strings.storage + psf->strings.data [k].offset)) ;
1172				break ;
1173
1174			case SF_STR_COMMENT :
1175				psf_binheader_writef (psf, "ms", BHWm (ICMT_MARKER), BHWs (psf->strings.storage + psf->strings.data [k].offset)) ;
1176				break ;
1177
1178			case SF_STR_DATE :
1179				psf_binheader_writef (psf, "ms", BHWm (ICRD_MARKER), BHWs (psf->strings.storage + psf->strings.data [k].offset)) ;
1180				break ;
1181
1182			case SF_STR_GENRE :
1183				psf_binheader_writef (psf, "ms", BHWm (IGNR_MARKER), BHWs (psf->strings.storage + psf->strings.data [k].offset)) ;
1184				break ;
1185
1186			case SF_STR_ALBUM :
1187				psf_binheader_writef (psf, "ms", BHWm (IPRD_MARKER), BHWs (psf->strings.storage + psf->strings.data [k].offset)) ;
1188				break ;
1189
1190			case SF_STR_TRACKNUMBER :
1191				psf_binheader_writef (psf, "ms", BHWm (ITRK_MARKER), BHWs (psf->strings.storage + psf->strings.data [k].offset)) ;
1192				break ;
1193
1194			default :
1195				break ;
1196			} ;
1197		} ;
1198
1199	saved_head_index = psf->header.indx ;
1200	psf->header.indx = prev_head_index ;
1201	psf_binheader_writef (psf, "4", BHW4 (saved_head_index - prev_head_index - 4)) ;
1202	psf->header.indx = saved_head_index ;
1203
1204} /* wavlike_write_strings */
1205
1206int
1207wavlike_read_peak_chunk (SF_PRIVATE * psf, size_t chunk_size)
1208{	char		buffer [256] ;
1209	uint32_t uk ;
1210
1211	if (chunk_size != WAVLIKE_PEAK_CHUNK_SIZE (psf->sf.channels))
1212	{	psf_binheader_readf (psf, "j", chunk_size) ;
1213		psf_log_printf (psf, "*** File PEAK chunk size doesn't fit with number of channels (%d).\n", psf->sf.channels) ;
1214		return SFE_WAV_BAD_PEAK ;
1215		} ;
1216
1217	if (psf->peak_info)
1218	{	psf_log_printf (psf, "*** Found existing peak info, using last one.\n") ;
1219		free (psf->peak_info) ;
1220		psf->peak_info = NULL ;
1221		} ;
1222	if ((psf->peak_info = peak_info_calloc (psf->sf.channels)) == NULL)
1223		return SFE_MALLOC_FAILED ;
1224
1225	/* read in rest of PEAK chunk. */
1226	psf_binheader_readf (psf, "44", & (psf->peak_info->version), & (psf->peak_info->timestamp)) ;
1227
1228	if (psf->peak_info->version != 1)
1229		psf_log_printf (psf, "  version    : %d *** (should be version 1)\n", psf->peak_info->version) ;
1230	else
1231		psf_log_printf (psf, "  version    : %d\n", psf->peak_info->version) ;
1232
1233	psf_log_printf (psf, "  time stamp : %d\n", psf->peak_info->timestamp) ;
1234	psf_log_printf (psf, "    Ch   Position       Value\n") ;
1235
1236	for (uk = 0 ; uk < (uint32_t) psf->sf.channels ; uk++)
1237	{	float value ;
1238		uint32_t position ;
1239
1240		psf_binheader_readf (psf, "f4", &value, &position) ;
1241		psf->peak_info->peaks [uk].value = value ;
1242		psf->peak_info->peaks [uk].position = position ;
1243
1244		snprintf (buffer, sizeof (buffer), "    %2d   %-12" PRId64 "   %g\n",
1245				uk, psf->peak_info->peaks [uk].position, psf->peak_info->peaks [uk].value) ;
1246		buffer [sizeof (buffer) - 1] = 0 ;
1247		psf_log_printf (psf, "%s", buffer) ;
1248		} ;
1249
1250	return 0 ;
1251} /* wavlike_read_peak_chunk */
1252
1253void
1254wavlike_write_peak_chunk (SF_PRIVATE * psf)
1255{	int k ;
1256
1257	if (psf->peak_info == NULL)
1258		return ;
1259
1260	psf_binheader_writef (psf, "m4", BHWm (PEAK_MARKER), BHW4 (WAVLIKE_PEAK_CHUNK_SIZE (psf->sf.channels))) ;
1261	psf_binheader_writef (psf, "44", BHW4 (1), BHW4 (time (NULL))) ;
1262	for (k = 0 ; k < psf->sf.channels ; k++)
1263		psf_binheader_writef (psf, "ft8", BHWf (psf->peak_info->peaks [k].value), BHW8 (psf->peak_info->peaks [k].position)) ;
1264} /* wavlike_write_peak_chunk */
1265
1266/*==============================================================================
1267*/
1268
1269static int
1270exif_fill_and_sink (SF_PRIVATE *psf, char* buf, size_t bufsz, size_t toread)
1271{
1272	size_t bytesread = 0 ;
1273
1274	buf [0] = 0 ;
1275	bufsz -= 1 ;
1276	if (toread < bufsz)
1277		bufsz = toread ;
1278	bytesread = psf_binheader_readf (psf, "b", buf, bufsz) ;
1279	buf [bufsz] = 0 ;
1280
1281	if (bytesread == bufsz && toread > bufsz)
1282		bytesread += psf_binheader_readf (psf, "j", toread - bufsz) ;
1283
1284	return bytesread ;
1285} /* exif_fill_and_sink */
1286
1287/*
1288** Exif specification for audio files, at JEITA CP-3451 Exif 2.2 section 5
1289** (Exif Audio File Specification) http://www.exif.org/Exif2-2.PDF
1290*/
1291static int
1292exif_subchunk_parse (SF_PRIVATE *psf, uint32_t length)
1293{	uint32_t marker, dword = 0, vmajor = -1, vminor = -1, bytesread = 0 ;
1294	char buf [4096] ;
1295	int thisread ;
1296
1297	while (bytesread < length)
1298	{
1299		if ((thisread = psf_binheader_readf (psf, "m", &marker)) == 0)
1300			break ;
1301		bytesread += thisread ;
1302
1303		switch (marker)
1304		{
1305			case 0 : /* camera padding? */
1306				break ;
1307
1308			case ever_MARKER :
1309				bytesread += psf_binheader_readf (psf, "j4", 4, &dword) ;
1310				vmajor = 10 * (((dword >> 24) & 0xff) - '0') + (((dword >> 16) & 0xff) - '0') ;
1311				vminor = 10 * (((dword >> 8) & 0xff) - '0') + ((dword & 0xff) - '0') ;
1312				psf_log_printf (psf, "    EXIF Version : %u.%02u\n", vmajor, vminor) ;
1313				break ;
1314
1315			case olym_MARKER :
1316				bytesread += psf_binheader_readf (psf, "4", &dword) ;
1317				psf_log_printf (psf, "%M : %u\n", marker, dword) ;
1318				if (dword > length || bytesread + dword > length)
1319					break ;
1320				dword += (dword & 1) ;
1321				bytesread += psf_binheader_readf (psf, "j", dword) ;
1322				break ;
1323
1324			case emnt_MARKER : /* design information: null-terminated string */
1325			case emdl_MARKER : /* model name ; null-terminated string */
1326			case ecor_MARKER : /* manufacturer: null-terminated string */
1327			case etim_MARKER : /* creation time: null-terminated string in the format "hour:minute:second.subsecond" */
1328			case erel_MARKER : /* relation info: null-terminated string (filename) */
1329			case eucm_MARKER : /* user comment: 4-byte size follows, then possibly unicode data */
1330				bytesread += psf_binheader_readf (psf, "4", &dword) ;
1331				bytesread += sizeof (dword) ;
1332				dword += (dword & 1) ;
1333
1334				if (dword >= sizeof (buf))
1335				{	psf_log_printf (psf, "*** Marker '%M' is too big %u\n\n", marker, dword) ;
1336					return bytesread ;
1337					} ;
1338
1339				bytesread += exif_fill_and_sink (psf, buf, sizeof (buf), dword) ;
1340
1341				/* BAD - don't know what's going on here -- maybe a bug in the camera */
1342				/* field should be NULL-terminated but there's no room for it with the reported number */
1343				/*  example output:     emdl : 8 (EX-Z1050) */
1344				if (marker == emdl_MARKER && dword == strlen (buf) /* should be >= strlen+1*/)
1345				{	psf_log_printf (psf, "    *** field size too small for string (sinking 2 bytes)\n") ;
1346					bytesread += psf_binheader_readf (psf, "j", 2) ;
1347					} ;
1348
1349				psf_log_printf (psf, "    %M : %u (%s)\n", marker, dword, buf) ;
1350				if (dword > length)
1351					return bytesread ;
1352				break ;
1353
1354			default :
1355				psf_log_printf (psf, "    *** %M (%u): -- ignored --\n", marker, marker) ;
1356				break ;
1357			} ;
1358		} ;
1359
1360	return bytesread ;
1361} /* exif_subchunk_parse */
1362
1363void
1364wavlike_write_custom_chunks (SF_PRIVATE * psf)
1365{	uint32_t k ;
1366
1367	for (k = 0 ; k < psf->wchunks.used ; k++)
1368		psf_binheader_writef (psf, "m4b", BHWm (psf->wchunks.chunks [k].mark32), BHW4 (psf->wchunks.chunks [k].len),
1369							BHWv (psf->wchunks.chunks [k].data), BHWz (psf->wchunks.chunks [k].len)) ;
1370
1371} /* wavlike_write_custom_chunks */
1372