1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2005 Dave Chapman
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 ****************************************************************************/
29 #include "metadata_common.h"
30 #include "metadata_parsers.h"
33 # define AV_WL32(p, d) do { \
34 ((uint8_t*)(p))[0] = (d); \
35 ((uint8_t*)(p))[1] = (d)>>8; \
36 ((uint8_t*)(p))[2] = (d)>>16; \
37 ((uint8_t*)(p))[3] = (d)>>24; \
39 # define AV_WL16(p, d) do { \
40 ((uint8_t*)(p))[0] = (d); \
41 ((uint8_t*)(p))[1] = (d)>>8; \
46 WAVE_FORMAT_PCM
= 0x0001, /* Microsoft PCM Format */
47 WAVE_FORMAT_ADPCM
= 0x0002, /* Microsoft ADPCM Format */
48 WAVE_FORMAT_IEEE_FLOAT
= 0x0003, /* IEEE Float */
49 WAVE_FORMAT_ALAW
= 0x0006, /* Microsoft ALAW */
50 WAVE_FORMAT_MULAW
= 0x0007, /* Microsoft MULAW */
51 WAVE_FORMAT_DVI_ADPCM
= 0x0011, /* Intel's DVI ADPCM */
52 WAVE_FORMAT_DIALOGIC_OKI_ADPCM
= 0x0017, /* Dialogic OKI ADPCM */
53 WAVE_FORMAT_YAMAHA_ADPCM
= 0x0020, /* Yamaha ADPCM */
54 WAVE_FORMAT_XBOX_ADPCM
= 0x0069, /* XBOX ADPCM */
55 IBM_FORMAT_MULAW
= 0x0101, /* same as WAVE_FORMAT_MULAW */
56 IBM_FORMAT_ALAW
= 0x0102, /* same as WAVE_FORMAT_ALAW */
57 WAVE_FORMAT_ATRAC3
= 0x0270, /* Atrac3 stream */
58 WAVE_FORMAT_SWF_ADPCM
= 0x5346, /* Adobe SWF ADPCM */
62 unsigned int formattag
;
63 unsigned long channels
;
64 unsigned int blockalign
;
65 unsigned long bitspersample
;
66 unsigned int samplesperblock
;
67 unsigned long numbytes
;
70 static unsigned long get_totalsamples(struct wave_fmt
*fmt
, struct mp3entry
* id3
)
72 unsigned long totalsamples
= 0;
74 switch (fmt
->formattag
)
77 case WAVE_FORMAT_IEEE_FLOAT
:
78 case WAVE_FORMAT_ALAW
:
79 case WAVE_FORMAT_MULAW
:
81 case IBM_FORMAT_MULAW
:
83 fmt
->numbytes
/ ((((fmt
->bitspersample
- 1) / 8) + 1) * fmt
->channels
);
85 case WAVE_FORMAT_ADPCM
:
86 case WAVE_FORMAT_DVI_ADPCM
:
87 case WAVE_FORMAT_XBOX_ADPCM
:
88 totalsamples
= (fmt
->numbytes
/ fmt
->blockalign
) * fmt
->samplesperblock
;
90 case WAVE_FORMAT_YAMAHA_ADPCM
:
91 if (fmt
->samplesperblock
== 0)
93 if (fmt
->blockalign
== ((id3
->frequency
/ 60) + 4) * fmt
->channels
)
94 fmt
->samplesperblock
= id3
->frequency
/ 30;
96 fmt
->samplesperblock
= fmt
->blockalign
* 2 / fmt
->channels
;
98 totalsamples
= (fmt
->numbytes
/ fmt
->blockalign
) * fmt
->samplesperblock
;
100 case WAVE_FORMAT_DIALOGIC_OKI_ADPCM
:
101 totalsamples
= 2 * fmt
->numbytes
;
103 case WAVE_FORMAT_SWF_ADPCM
:
104 if (fmt
->samplesperblock
== 0)
105 fmt
->samplesperblock
= (((fmt
->blockalign
<< 3) - 2) / fmt
->channels
- 22)
106 / fmt
->bitspersample
;
108 totalsamples
= (fmt
->numbytes
/ fmt
->blockalign
) * fmt
->samplesperblock
;
117 bool get_wave_metadata(int fd
, struct mp3entry
* id3
)
119 /* Use the trackname part of the id3 structure as a temporary buffer */
120 unsigned char* buf
= (unsigned char *)id3
->path
;
122 unsigned long totalsamples
= 0;
123 unsigned long offset
= 0;
127 memset(&fmt
, 0, sizeof(struct wave_fmt
));
129 /* get RIFF chunk header */
130 if ((lseek(fd
, 0, SEEK_SET
) < 0) || (read(fd
, buf
, 12) < 12))
136 if ((memcmp(buf
, "RIFF", 4) != 0) || (memcmp(&buf
[8], "WAVE", 4) != 0))
138 DEBUGF("metadata error: missing riff header.\n");
142 /* iterate over WAVE chunks until 'data' chunk */
145 /* get chunk header */
146 if (read(fd
, buf
, 8) < 8)
151 i
= get_long_le(&buf
[4]);
153 if (memcmp(buf
, "fmt ", 4) == 0)
155 /* get rest of chunk */
163 if (read(fd
, buf
, read_bytes
) != read_bytes
)
166 offset
+= read_bytes
;
170 fmt
.formattag
= buf
[0] | (buf
[1] << 8);
172 fmt
.channels
= buf
[2] | (buf
[3] << 8);
173 /* dwSamplesPerSec */
174 id3
->frequency
= get_long_le(&buf
[4]);
175 /* dwAvgBytesPerSec */
176 id3
->bitrate
= (get_long_le(&buf
[8]) * 8) / 1000;
178 fmt
.blockalign
= buf
[12] | (buf
[13] << 8);
179 id3
->bytesperframe
= fmt
.blockalign
;
181 fmt
.bitspersample
= buf
[14] | (buf
[15] << 8);
184 /* wSamplesPerBlock */
185 fmt
.samplesperblock
= buf
[18] | (buf
[19] << 8);
188 /* Check for ATRAC3 stream */
189 if (fmt
.formattag
== WAVE_FORMAT_ATRAC3
)
192 if(id3
->bitrate
== 66 || id3
->bitrate
== 94)
195 id3
->extradata_size
= 14;
197 id3
->codectype
= AFMT_OMA_ATRAC3
;
198 /* Store the extradata for the codec */
199 AV_WL16(&id3
->id3v2buf
[0], 1); // always 1
200 AV_WL32(&id3
->id3v2buf
[2], id3
->frequency
); // samples rate
201 AV_WL16(&id3
->id3v2buf
[6], jsflag
); // coding mode
202 AV_WL16(&id3
->id3v2buf
[8], jsflag
); // coding mode
203 AV_WL16(&id3
->id3v2buf
[10], 1); // always 1
204 AV_WL16(&id3
->id3v2buf
[12], 0); // always 0
207 else if (memcmp(buf
, "data", 4) == 0)
210 if (fmt
.formattag
== WAVE_FORMAT_ATRAC3
)
211 id3
->first_frame_offset
= offset
;
214 else if (memcmp(buf
, "fact", 4) == 0)
219 /* get rest of chunk */
220 if (read(fd
, buf
, 4) < 4)
224 totalsamples
= get_long_le(buf
);
228 /* seek to next chunk (even chunk sizes must be padded) */
232 if(lseek(fd
, i
, SEEK_CUR
) < 0)
237 if ((fmt
.numbytes
== 0) || (fmt
.channels
== 0) || (fmt
.blockalign
== 0))
239 DEBUGF("metadata error: numbytes, channels, or blockalign is 0.\n");
243 if (totalsamples
== 0)
245 totalsamples
= get_totalsamples(&fmt
, id3
);
248 id3
->vbr
= false; /* All WAV files are CBR */
249 id3
->filesize
= filesize(fd
);
251 /* Calculate track length (in ms) and estimate the bitrate (in kbit/s) */
252 if(id3
->codectype
!= AFMT_OMA_ATRAC3
)
253 id3
->length
= ((int64_t) totalsamples
* 1000) / id3
->frequency
;
255 id3
->length
= ((id3
->filesize
- id3
->first_frame_offset
) * 8) / id3
->bitrate
;