1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2010 Yoshihisa Uchida
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
23 #include "codecs/libpcm/support_formats.h"
30 * [1] VCS Aktiengesellschaft, Sony Wave64, Informations_about_Sony_Wave64.pdf
33 #define PCM_SAMPLE_SIZE (4096*2)
35 static int32_t samples
[PCM_SAMPLE_SIZE
] IBSS_ATTR
;
38 #define WAVE64_GUID_RIFF "riff\x2e\x91\xcf\x11\xa5\xd6\x28\xdb\x04\xc1\x00\x00"
39 #define WAVE64_GUID_WAVE "wave\xf3\xac\xd3\x11\x8c\xd1\x00\xc0\x4f\x8e\xdb\x8a"
40 #define WAVE64_GUID_FMT "fmt \xf3\xac\xd3\x11\x8c\xd1\x00\xc0\x4f\x8e\xdb\x8a"
41 #define WAVE64_GUID_FACT "fact\xf3\xac\xd3\x11\x8c\xd1\x00\xc0\x4f\x8e\xdb\x8a"
42 #define WAVE64_GUID_DATA "data\xf3\xac\xd3\x11\x8c\xd1\x00\xc0\x4f\x8e\xdb\x8a"
44 /* This codec support WAVE files with the following formats: */
47 WAVE_FORMAT_UNKNOWN
= 0x0000, /* Microsoft Unknown Wave Format */
48 WAVE_FORMAT_PCM
= 0x0001, /* Microsoft PCM Format */
49 WAVE_FORMAT_ADPCM
= 0x0002, /* Microsoft ADPCM Format */
50 WAVE_FORMAT_IEEE_FLOAT
= 0x0003, /* IEEE Float */
51 WAVE_FORMAT_ALAW
= 0x0006, /* Microsoft ALAW */
52 WAVE_FORMAT_MULAW
= 0x0007, /* Microsoft MULAW */
53 WAVE_FORMAT_DVI_ADPCM
= 0x0011, /* Intel's DVI ADPCM */
54 WAVE_FORMAT_DIALOGIC_OKI_ADPCM
= 0x0017, /* Dialogic OKI ADPCM */
55 WAVE_FORMAT_YAMAHA_ADPCM
= 0x0020, /* Yamaha ADPCM */
56 WAVE_FORMAT_XBOX_ADPCM
= 0x0069, /* XBOX ADPCM */
57 IBM_FORMAT_MULAW
= 0x0101, /* same as WAVE_FORMAT_MULAW */
58 IBM_FORMAT_ALAW
= 0x0102, /* same as WAVE_FORMAT_ALAW */
59 WAVE_FORMAT_SWF_ADPCM
= 0x5346, /* Adobe SWF ADPCM */
60 WAVE_FORMAT_EXTENSIBLE
= 0xFFFE
63 const struct pcm_entry wave_codecs
[] = {
64 { WAVE_FORMAT_UNKNOWN
, 0 },
65 { WAVE_FORMAT_PCM
, get_linear_pcm_codec
},
66 { WAVE_FORMAT_ADPCM
, get_ms_adpcm_codec
},
67 { WAVE_FORMAT_IEEE_FLOAT
, get_ieee_float_codec
},
68 { WAVE_FORMAT_ALAW
, get_itut_g711_alaw_codec
},
69 { WAVE_FORMAT_MULAW
, get_itut_g711_mulaw_codec
},
70 { WAVE_FORMAT_DVI_ADPCM
, get_dvi_adpcm_codec
},
71 { WAVE_FORMAT_DIALOGIC_OKI_ADPCM
, get_dialogic_oki_adpcm_codec
},
72 { WAVE_FORMAT_YAMAHA_ADPCM
, get_yamaha_adpcm_codec
},
73 { WAVE_FORMAT_XBOX_ADPCM
, get_dvi_adpcm_codec
},
74 { IBM_FORMAT_MULAW
, get_itut_g711_mulaw_codec
},
75 { IBM_FORMAT_ALAW
, get_itut_g711_alaw_codec
},
76 { WAVE_FORMAT_SWF_ADPCM
, get_swf_adpcm_codec
},
79 #define NUM_FORMATS 13
81 static const struct pcm_codec
*get_wave_codec(uint32_t formattag
)
85 for (i
= 0; i
< NUM_FORMATS
; i
++)
87 if (wave_codecs
[i
].format_tag
== formattag
)
89 if (wave_codecs
[i
].get_codec
)
90 return wave_codecs
[i
].get_codec();
97 static struct pcm_format format
;
98 static uint32_t bytesdone
;
100 /* Read an unaligned 64-bit little endian unsigned integer from buffer. */
101 static uint64_t get_uint64_le(void* buf
)
103 unsigned char* p
= (unsigned char*) buf
;
105 return p
[0] | (p
[1] << 8) | (p
[2] << 16) | (p
[3] << 24) | ((uint64_t)p
[4] << 32) |
106 ((uint64_t)p
[5] << 40) | ((uint64_t)p
[6] << 48) | ((uint64_t)p
[7] << 56);
109 static bool set_msadpcm_coeffs(unsigned char *buf
)
115 buf
+= 16; /* skip 'fmt ' GUID */
116 size
= get_uint64_le(buf
);
119 DEBUGF("CODEC_ERROR: microsoft adpcm 'fmt ' chunk size=%d < 50\n", (int)size
);
125 num
= buf
[0] | (buf
[1] << 8);
128 * In many case, nNumCoef is 7.
129 * Depending upon the encoder, as for this value there is a possibility of
131 * If you found the file where this value exceeds 7, please report.
133 if (num
!= MSADPCM_NUM_COEFF
)
135 DEBUGF("CODEC_ERROR: microsoft adpcm nNumCoef=%d != 7\n", num
);
141 for (i
= 0; i
< MSADPCM_NUM_COEFF
; i
++)
143 format
.coeffs
[i
][0] = buf
[0] | (SE(buf
[1]) << 8);
144 format
.coeffs
[i
][1] = buf
[2] | (SE(buf
[3]) << 8);
151 static uint8_t *read_buffer(size_t *realsize
)
153 uint8_t *buffer
= (uint8_t *)ci
->request_buffer(realsize
, format
.chunksize
);
154 if (bytesdone
+ (*realsize
) > format
.numbytes
)
155 *realsize
= format
.numbytes
- bytesdone
;
156 bytesdone
+= *realsize
;
157 ci
->advance_buffer(*realsize
);
161 /* this is the codec entry point */
162 enum codec_status
codec_main(void)
164 int status
= CODEC_OK
;
165 uint32_t decodedsamples
;
172 off_t firstblockposn
; /* position of the first block in file */
173 const struct pcm_codec
*codec
;
176 /* Generic codec initialisation */
177 ci
->configure(DSP_SET_SAMPLE_DEPTH
, 28);
181 DEBUGF("codec_init() error\n");
182 status
= CODEC_ERROR
;
186 while (!*ci
->taginfo_ready
&& !ci
->stop_codec
)
189 codec_set_replaygain(ci
->id3
);
191 /* Need to save offset for later use (cleared indirectly by advance_buffer) */
192 bytesdone
= ci
->id3
->offset
;
194 /* get RIFF chunk header */
195 buf
= ci
->request_buffer(&n
, 40);
197 DEBUGF("request_buffer error\n");
198 status
= CODEC_ERROR
;
201 if ((memcmp(buf
, WAVE64_GUID_RIFF
, 16) != 0) ||
202 (memcmp(buf
+24, WAVE64_GUID_WAVE
, 16) != 0))
204 status
= CODEC_ERROR
;
208 /* advance to first WAVE chunk */
209 ci
->advance_buffer(40);
212 ci
->memset(&format
, 0, sizeof(struct pcm_format
));
213 format
.is_signed
= true;
214 format
.is_little_endian
= true;
219 /* iterate over WAVE chunks until the 'data' chunk, which should be after the 'fmt ' chunk */
221 /* get WAVE chunk header */
222 buf
= ci
->request_buffer(&n
, 1024);
224 DEBUGF("data chunk request_buffer error\n");
225 /* no more chunks, 'data' chunk must not have been found */
226 status
= CODEC_ERROR
;
231 size
= get_uint64_le(buf
+16) - 24;
232 if (memcmp(buf
, WAVE64_GUID_FMT
, 16) == 0) {
234 DEBUGF("CODEC_ERROR: 'fmt ' chunk size=%d < 16\n", (int)size
);
235 status
= CODEC_ERROR
;
239 format
.formattag
=buf
[24]|(buf
[25]<<8);
241 format
.channels
=buf
[26]|(buf
[27]<<8);
242 /* skipping dwSamplesPerSec */
243 /* skipping dwAvgBytesPerSec */
245 format
.blockalign
=buf
[36]|(buf
[37]<<8);
247 format
.bitspersample
=buf
[38]|(buf
[39]<<8);
248 if (format
.formattag
!= WAVE_FORMAT_PCM
) {
250 /* this is not a fatal error with some formats,
251 * we'll see later if we can't decode it */
252 DEBUGF("CODEC_WARNING: non-PCM WAVE (formattag=0x%x) "
253 "doesn't have ext. fmt descr (chunksize=%d<18).\n",
254 (unsigned int)format
.formattag
, (int)size
);
258 format
.size
= buf
[40]|(buf
[41]<<8);
259 if (format
.formattag
!= WAVE_FORMAT_EXTENSIBLE
)
260 format
.samplesperblock
= buf
[42]|(buf
[43]<<8);
262 if (format
.size
< 22) {
263 DEBUGF("CODEC_ERROR: WAVE_FORMAT_EXTENSIBLE is "
264 "missing extension\n");
265 status
= CODEC_ERROR
;
268 /* wValidBitsPerSample */
269 format
.bitspersample
= buf
[42]|(buf
[43]<<8);
270 /* skipping dwChannelMask (4bytes) */
271 /* SubFormat (only get the first two bytes) */
272 format
.formattag
= buf
[48]|(buf
[49]<<8);
277 /* msadpcm specific */
278 if (format
.formattag
== WAVE_FORMAT_ADPCM
)
280 if (!set_msadpcm_coeffs(buf
))
282 status
= CODEC_ERROR
;
288 codec
= get_wave_codec(format
.formattag
);
291 DEBUGF("CODEC_ERROR: unsupported wave format 0x%x\n",
292 (unsigned int) format
.formattag
);
293 status
= CODEC_ERROR
;
297 /* riff 8bit linear pcm is unsigned */
298 if (format
.formattag
== WAVE_FORMAT_PCM
&& format
.bitspersample
== 8)
299 format
.is_signed
= false;
301 /* check format, and calculate chunk size */
302 if (!codec
->set_format(&format
))
304 status
= CODEC_ERROR
;
307 } else if (memcmp(buf
, WAVE64_GUID_DATA
, 16) == 0) {
308 format
.numbytes
= size
;
309 /* advance to start of data */
310 ci
->advance_buffer(24);
311 firstblockposn
+= 24;
313 } else if (memcmp(buf
, WAVE64_GUID_FACT
, 16) == 0) {
314 /* skip 'fact' chunk */
316 DEBUGF("unknown Wave64 chunk: "
317 "'%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x'\n",
318 buf
[0], buf
[1], buf
[ 2], buf
[ 3], buf
[ 4], buf
[ 5], buf
[ 6], buf
[ 7],
319 buf
[8], buf
[9], buf
[10], buf
[11], buf
[12], buf
[13], buf
[14], buf
[15]);
322 /* go to next chunk (8byte bound) */
324 size
+= 8 - (size
& 0x7);
325 ci
->advance_buffer(size
+ 24);
326 firstblockposn
+= size
+ 24;
331 DEBUGF("CODEC_ERROR: 'fmt ' chunk not found\n");
332 status
= CODEC_ERROR
;
336 /* common format check */
337 if (format
.channels
== 0) {
338 DEBUGF("CODEC_ERROR: 'fmt ' chunk not found or 0-channels file\n");
339 status
= CODEC_ERROR
;
342 if (format
.samplesperblock
== 0) {
343 DEBUGF("CODEC_ERROR: 'fmt ' chunk not found or 0-wSamplesPerBlock file\n");
344 status
= CODEC_ERROR
;
347 if (format
.blockalign
== 0)
349 DEBUGF("CODEC_ERROR: 'fmt ' chunk not found or 0-blockalign file\n");
353 if (format
.numbytes
== 0) {
354 DEBUGF("CODEC_ERROR: 'data' chunk not found or has zero-length\n");
355 status
= CODEC_ERROR
;
359 /* check chunksize */
360 if ((format
.chunksize
/ format
.blockalign
) * format
.samplesperblock
* format
.channels
362 format
.chunksize
= (PCM_SAMPLE_SIZE
/ format
.blockalign
) * format
.blockalign
;
363 if (format
.chunksize
== 0)
365 DEBUGF("CODEC_ERROR: chunksize is 0\n");
370 ci
->configure(DSP_SWITCH_FREQUENCY
, ci
->id3
->frequency
);
371 if (format
.channels
== 2) {
372 ci
->configure(DSP_SET_STEREO_MODE
, STEREO_INTERLEAVED
);
373 } else if (format
.channels
== 1) {
374 ci
->configure(DSP_SET_STEREO_MODE
, STEREO_MONO
);
376 DEBUGF("CODEC_ERROR: more than 2 channels\n");
377 status
= CODEC_ERROR
;
381 /* make sure we're at the correct offset */
382 if (bytesdone
> (uint32_t) firstblockposn
) {
383 /* Round down to previous block */
384 uint32_t offset
= bytesdone
- bytesdone
% format
.blockalign
;
386 ci
->advance_buffer(offset
-firstblockposn
);
387 bytesdone
= offset
- firstblockposn
;
389 /* already where we need to be */
393 /* The main decoder loop */
396 while (!endofstream
) {
398 if (ci
->stop_codec
|| ci
->new_track
) {
403 struct pcm_pos
*newpos
= codec
->get_seek_pos(ci
->seek_time
, &read_buffer
);
405 decodedsamples
= newpos
->samples
;
406 if (newpos
->pos
> format
.numbytes
)
408 if (ci
->seek_buffer(firstblockposn
+ newpos
->pos
))
410 bytesdone
= newpos
->pos
;
415 wavbuf
= (uint8_t *)ci
->request_buffer(&n
, format
.chunksize
);
417 break; /* End of stream */
418 if (bytesdone
+ n
> format
.numbytes
) {
419 n
= format
.numbytes
- bytesdone
;
423 status
= codec
->decode(wavbuf
, n
, samples
, &bufcount
);
424 if (status
== CODEC_ERROR
)
426 DEBUGF("codec error\n");
430 ci
->pcmbuf_insert(samples
, NULL
, bufcount
);
431 ci
->advance_buffer(n
);
433 decodedsamples
+= bufcount
;
435 if (bytesdone
>= format
.numbytes
)
437 ci
->set_elapsed(decodedsamples
*1000LL/ci
->id3
->frequency
);
442 if (ci
->request_next_track())