1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
11 * Copyright (C) 2007 Dave Chapman
13 * All files in this archive are subject to the GNU General Public License.
14 * See the file COPYING in the source tree root for full license agreement.
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
19 ****************************************************************************/
27 #include "replaygain.h"
29 #include "rbunicode.h"
30 #include "metadata_common.h"
31 #include "metadata_parsers.h"
33 #include <codecs/libwma/asf.h>
35 /* TODO: Just read the GUIDs into a 16-byte array, and use memcmp to compare */
42 typedef struct guid_s guid_t
;
49 typedef struct asf_object_s asf_object_t
;
52 ASF_ERROR_INTERNAL
= -1, /* incorrect input to API calls */
53 ASF_ERROR_OUTOFMEM
= -2, /* some malloc inside program failed */
54 ASF_ERROR_EOF
= -3, /* unexpected end of file */
55 ASF_ERROR_IO
= -4, /* error reading or writing to file */
56 ASF_ERROR_INVALID_LENGTH
= -5, /* length value conflict in input data */
57 ASF_ERROR_INVALID_VALUE
= -6, /* other value conflict in input data */
58 ASF_ERROR_INVALID_OBJECT
= -7, /* ASF object missing or in wrong place */
59 ASF_ERROR_OBJECT_SIZE
= -8, /* invalid ASF object size (too small) */
60 ASF_ERROR_SEEKABLE
= -9, /* file not seekable */
61 ASF_ERROR_SEEK
= -10, /* file is seekable but seeking failed */
62 ASF_ERROR_ENCRYPTED
= -11 /* file is encrypted */
65 static const guid_t asf_guid_null
=
66 {0x00000000, 0x0000, 0x0000, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
68 /* top level object guids */
70 static const guid_t asf_guid_header
=
71 {0x75B22630, 0x668E, 0x11CF, {0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C}};
73 static const guid_t asf_guid_data
=
74 {0x75B22636, 0x668E, 0x11CF, {0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C}};
76 static const guid_t asf_guid_index
=
77 {0x33000890, 0xE5B1, 0x11CF, {0x89, 0xF4, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xCB}};
79 /* header level object guids */
81 static const guid_t asf_guid_file_properties
=
82 {0x8cabdca1, 0xa947, 0x11cf, {0x8E, 0xe4, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65}};
84 static const guid_t asf_guid_stream_properties
=
85 {0xB7DC0791, 0xA9B7, 0x11CF, {0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65}};
87 static const guid_t asf_guid_content_description
=
88 {0x75B22633, 0x668E, 0x11CF, {0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C}};
90 static const guid_t asf_guid_extended_content_description
=
91 {0xD2D0A440, 0xE307, 0x11D2, {0x97, 0xF0, 0x00, 0xA0, 0xC9, 0x5E, 0xA8, 0x50}};
93 static const guid_t asf_guid_content_encryption
=
94 {0x2211b3fb, 0xbd23, 0x11d2, {0xb4, 0xb7, 0x00, 0xa0, 0xc9, 0x55, 0xfc, 0x6e}};
96 static const guid_t asf_guid_extended_content_encryption
=
97 {0x298ae614, 0x2622, 0x4c17, {0xb9, 0x35, 0xda, 0xe0, 0x7e, 0xe9, 0x28, 0x9c}};
99 /* stream type guids */
101 static const guid_t asf_guid_stream_type_audio
=
102 {0xF8699E40, 0x5B4D, 0x11CF, {0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B}};
104 static int asf_guid_match(const guid_t
*guid1
, const guid_t
*guid2
)
106 if((guid1
->v1
!= guid2
->v1
) ||
107 (guid1
->v2
!= guid2
->v2
) ||
108 (guid1
->v3
!= guid2
->v3
) ||
109 (memcmp(guid1
->v4
, guid2
->v4
, 8))) {
116 /* Read the 16 byte GUID from a file */
117 static void asf_readGUID(int fd
, guid_t
* guid
)
119 read_uint32le(fd
, &guid
->v1
);
120 read_uint16le(fd
, &guid
->v2
);
121 read_uint16le(fd
, &guid
->v3
);
122 read(fd
, guid
->v4
, 8);
125 static void asf_read_object_header(asf_object_t
*obj
, int fd
)
127 asf_readGUID(fd
, &obj
->guid
);
128 read_uint64le(fd
, &obj
->size
);
132 /* Parse an integer from the extended content object - we always
133 convert to an int, regardless of native format.
135 static int asf_intdecode(int fd
, int type
, int length
)
142 read_uint32le(fd
, &tmp32
);
143 lseek(fd
,length
- 4,SEEK_CUR
);
145 } else if (type
==4) {
146 read_uint64le(fd
, &tmp64
);
147 lseek(fd
,length
- 8,SEEK_CUR
);
149 } else if (type
== 5) {
150 read_uint16le(fd
, &tmp16
);
151 lseek(fd
,length
- 2,SEEK_CUR
);
158 /* Decode a LE utf16 string from a disk buffer into a fixed-sized
162 static void asf_utf16LEdecode(int fd
,
164 unsigned char **utf8
,
170 unsigned char utf16buf
[256];
171 unsigned char* utf16
= utf16buf
;
172 unsigned char* newutf8
;
174 n
= read(fd
, utf16buf
, MIN(sizeof(utf16buf
), utf16bytes
));
178 /* Check for a surrogate pair */
179 if (utf16
[1] >= 0xD8 && utf16
[1] < 0xE0) {
181 /* Run out of utf16 bytes, read some more */
182 utf16buf
[0] = utf16
[0];
183 utf16buf
[1] = utf16
[1];
185 n
= read(fd
, utf16buf
+ 2, MIN(sizeof(utf16buf
)-2, utf16bytes
));
192 /* Truncated utf16 string, abort */
195 ucs
= 0x10000 + ((utf16
[0] << 10) | ((utf16
[1] - 0xD8) << 18)
196 | utf16
[2] | ((utf16
[3] - 0xDC) << 8));
200 ucs
= (utf16
[0] | (utf16
[1] << 8));
205 if (*utf8bytes
> 6) {
206 newutf8
= utf8encode(ucs
, *utf8
);
207 *utf8bytes
-= (newutf8
- *utf8
);
208 *utf8
+= (newutf8
- *utf8
);
211 /* We have run out of utf16 bytes, read more if available */
212 if ((n
== 0) && (utf16bytes
> 0)) {
213 n
= read(fd
, utf16buf
, MIN(sizeof(utf16buf
), utf16bytes
));
222 if (utf16bytes
> 0) {
223 /* Skip any remaining bytes */
224 lseek(fd
, utf16bytes
, SEEK_CUR
);
229 static int asf_parse_header(int fd
, struct mp3entry
* id3
,
230 asf_waveformatex_t
* wfx
)
232 asf_object_t current
;
237 uint64_t play_duration
;
240 uint8_t utf8buf
[512];
241 int id3buf_remaining
= sizeof(id3
->id3v2buf
) + sizeof(id3
->id3v1buf
);
242 unsigned char* id3buf
= (unsigned char*)id3
->id3v2buf
;
244 asf_read_object_header((asf_object_t
*) &header
, fd
);
246 //DEBUGF("header.size=%d\n",(int)header.size);
247 if (header
.size
< 30) {
248 /* invalid size for header object */
249 return ASF_ERROR_OBJECT_SIZE
;
252 read_uint32le(fd
, &subobjects
);
254 /* Two reserved bytes - do we need to read them? */
255 lseek(fd
, 2, SEEK_CUR
);
257 //DEBUGF("Read header - size=%d, subobjects=%d\n",(int)header.size, (int)subobjects);
259 if (subobjects
> 0) {
260 header
.datalen
= header
.size
- 30;
262 /* TODO: Check that we have datalen bytes left in the file */
263 datalen
= header
.datalen
;
265 for (i
=0; i
<(int)subobjects
; i
++) {
266 //DEBUGF("Parsing header object %d - datalen=%d\n",i,(int)datalen);
268 //DEBUGF("not enough data for reading object\n");
272 asf_read_object_header(¤t
, fd
);
274 if (current
.size
> datalen
|| current
.size
< 24) {
275 //DEBUGF("invalid object size - current.size=%d, datalen=%d\n",(int)current.size,(int)datalen);
279 if (asf_guid_match(¤t
.guid
, &asf_guid_file_properties
)) {
280 if (current
.size
< 104)
281 return ASF_ERROR_OBJECT_SIZE
;
284 /* multiple file properties objects not allowed */
285 return ASF_ERROR_INVALID_OBJECT
;
289 /* All we want is the play duration - uint64_t at offset 40 */
290 lseek(fd
, 40, SEEK_CUR
);
292 read_uint64le(fd
, &play_duration
);
293 id3
->length
= play_duration
/ 10000;
295 //DEBUGF("****** length = %lums\n", id3->length);
297 /* Read the packet size - uint32_t at offset 68 */
298 lseek(fd
, 20, SEEK_CUR
);
299 read_uint32le(fd
, &wfx
->packet_size
);
301 /* Skip bytes remaining in object */
302 lseek(fd
, current
.size
- 24 - 72, SEEK_CUR
);
303 } else if (asf_guid_match(¤t
.guid
, &asf_guid_stream_properties
)) {
305 uint32_t propdatalen
;
307 if (current
.size
< 78)
308 return ASF_ERROR_OBJECT_SIZE
;
311 asf_byteio_getGUID(&guid
, current
->data
);
312 datalen
= asf_byteio_getDWLE(current
->data
+ 40);
313 flags
= asf_byteio_getWLE(current
->data
+ 48);
316 asf_readGUID(fd
, &guid
);
318 lseek(fd
, 24, SEEK_CUR
);
319 read_uint32le(fd
, &propdatalen
);
320 lseek(fd
, 4, SEEK_CUR
);
321 read_uint16le(fd
, &flags
);
323 if (!asf_guid_match(&guid
, &asf_guid_stream_type_audio
)) {
324 //DEBUGF("Found stream properties for non audio stream, skipping\n");
325 lseek(fd
,current
.size
- 24 - 50,SEEK_CUR
);
326 } else if (wfx
->audiostream
== -1) {
327 lseek(fd
, 4, SEEK_CUR
);
328 //DEBUGF("Found stream properties for audio stream %d\n",flags&0x7f);
330 if (propdatalen
< 18) {
331 return ASF_ERROR_INVALID_LENGTH
;
335 if (asf_byteio_getWLE(data
+ 16) > datalen
- 16) {
336 return ASF_ERROR_INVALID_LENGTH
;
339 read_uint16le(fd
, &wfx
->codec_id
);
340 read_uint16le(fd
, &wfx
->channels
);
341 read_uint32le(fd
, &wfx
->rate
);
342 read_uint32le(fd
, &wfx
->bitrate
);
344 read_uint16le(fd
, &wfx
->blockalign
);
345 read_uint16le(fd
, &wfx
->bitspersample
);
346 read_uint16le(fd
, &wfx
->datalen
);
348 /* Round bitrate to the nearest kbit */
349 id3
->bitrate
= (wfx
->bitrate
+ 500) / 1000;
350 id3
->frequency
= wfx
->rate
;
352 if (wfx
->codec_id
== ASF_CODEC_ID_WMAV1
) {
353 read(fd
, wfx
->data
, 4);
354 lseek(fd
,current
.size
- 24 - 72 - 4,SEEK_CUR
);
355 wfx
->audiostream
= flags
&0x7f;
356 } else if (wfx
->codec_id
== ASF_CODEC_ID_WMAV2
) {
357 read(fd
, wfx
->data
, 6);
358 lseek(fd
,current
.size
- 24 - 72 - 6,SEEK_CUR
);
359 wfx
->audiostream
= flags
&0x7f;
361 DEBUGF("Unsupported WMA codec (Pro, Lossless, Voice, etc)\n");
362 lseek(fd
,current
.size
- 24 - 72,SEEK_CUR
);
366 } else if (asf_guid_match(¤t
.guid
, &asf_guid_content_description
)) {
367 /* Object contains five 16-bit string lengths, followed by the five strings:
368 title, artist, copyright, description, rating
370 uint16_t strlength
[5];
373 //DEBUGF("Found GUID_CONTENT_DESCRIPTION - size=%d\n",(int)(current.size - 24));
375 /* Read the 5 string lengths - number of bytes included trailing zero */
376 for (i
=0; i
<5; i
++) {
377 read_uint16le(fd
, &strlength
[i
]);
378 //DEBUGF("strlength = %u\n",strlength[i]);
381 if (strlength
[0] > 0) { /* 0 - Title */
383 asf_utf16LEdecode(fd
, strlength
[0], &id3buf
, &id3buf_remaining
);
386 if (strlength
[1] > 0) { /* 1 - Artist */
387 id3
->artist
= id3buf
;
388 asf_utf16LEdecode(fd
, strlength
[1], &id3buf
, &id3buf_remaining
);
391 lseek(fd
, strlength
[2], SEEK_CUR
); /* 2 - copyright */
393 if (strlength
[3] > 0) { /* 3 - description */
394 id3
->comment
= id3buf
;
395 asf_utf16LEdecode(fd
, strlength
[3], &id3buf
, &id3buf_remaining
);
398 lseek(fd
, strlength
[4], SEEK_CUR
); /* 4 - rating */
399 } else if (asf_guid_match(¤t
.guid
, &asf_guid_extended_content_description
)) {
402 int bytesleft
= current
.size
- 24;
403 //DEBUGF("Found GUID_EXTENDED_CONTENT_DESCRIPTION\n");
405 read_uint16le(fd
, &count
);
407 //DEBUGF("extended metadata count = %u\n",count);
409 for (i
=0; i
< count
; i
++) {
410 uint16_t length
, type
;
411 unsigned char* utf8
= utf8buf
;
412 int utf8length
= 512;
414 read_uint16le(fd
, &length
);
415 asf_utf16LEdecode(fd
, length
, &utf8
, &utf8length
);
416 bytesleft
-= 2 + length
;
418 read_uint16le(fd
, &type
);
419 read_uint16le(fd
, &length
);
421 if (!strcmp("WM/TrackNumber",utf8buf
)) {
423 id3
->track_string
= id3buf
;
424 asf_utf16LEdecode(fd
, length
, &id3buf
, &id3buf_remaining
);
425 id3
->tracknum
= atoi(id3
->track_string
);
426 } else if ((type
>=2) && (type
<= 5)) {
427 id3
->tracknum
= asf_intdecode(fd
, type
, length
);
429 lseek(fd
, length
, SEEK_CUR
);
431 } else if ((!strcmp("WM/Genre",utf8buf
)) && (type
== 0)) {
432 id3
->genre_string
= id3buf
;
433 asf_utf16LEdecode(fd
, length
, &id3buf
, &id3buf_remaining
);
434 } else if ((!strcmp("WM/AlbumTitle",utf8buf
)) && (type
== 0)) {
436 asf_utf16LEdecode(fd
, length
, &id3buf
, &id3buf_remaining
);
437 } else if ((!strcmp("WM/AlbumArtist",utf8buf
)) && (type
== 0)) {
438 id3
->albumartist
= id3buf
;
439 asf_utf16LEdecode(fd
, length
, &id3buf
, &id3buf_remaining
);
440 } else if ((!strcmp("WM/Composer",utf8buf
)) && (type
== 0)) {
441 id3
->composer
= id3buf
;
442 asf_utf16LEdecode(fd
, length
, &id3buf
, &id3buf_remaining
);
443 } else if (!strcmp("WM/Year",utf8buf
)) {
445 id3
->year_string
= id3buf
;
446 asf_utf16LEdecode(fd
, length
, &id3buf
, &id3buf_remaining
);
447 id3
->year
= atoi(id3
->year_string
);
448 } else if ((type
>=2) && (type
<= 5)) {
449 id3
->year
= asf_intdecode(fd
, type
, length
);
451 lseek(fd
, length
, SEEK_CUR
);
453 } else if (!strncmp("replaygain_", utf8buf
, 11)) {
454 char* value
= id3buf
;
455 int buf_len
= id3buf_remaining
;
457 asf_utf16LEdecode(fd
, length
, &id3buf
, &id3buf_remaining
);
458 len
= parse_replaygain(utf8buf
, value
, id3
,
462 /* Don't need to keep the value */
464 id3buf_remaining
= buf_len
;
467 lseek(fd
, length
, SEEK_CUR
);
469 bytesleft
-= 4 + length
;
472 lseek(fd
, bytesleft
, SEEK_CUR
);
473 } else if (asf_guid_match(¤t
.guid
, &asf_guid_content_encryption
)
474 || asf_guid_match(¤t
.guid
, &asf_guid_extended_content_encryption
)) {
475 //DEBUGF("File is encrypted\n");
476 return ASF_ERROR_ENCRYPTED
;
478 //DEBUGF("Skipping %d bytes of object\n",(int)(current.size - 24));
479 lseek(fd
,current
.size
- 24,SEEK_CUR
);
482 //DEBUGF("Parsed object - size = %d\n",(int)current.size);
483 datalen
-= current
.size
;
486 if (i
!= (int)subobjects
|| datalen
!= 0) {
487 //DEBUGF("header data doesn't match given subobject count\n");
488 return ASF_ERROR_INVALID_VALUE
;
491 //DEBUGF("%d subobjects read successfully\n", i);
495 tmp
= asf_parse_header_validate(file
, &header
);
497 /* header read ok but doesn't validate correctly */
502 //DEBUGF("header validated correctly\n");
507 bool get_asf_metadata(int fd
, struct mp3entry
* id3
)
511 asf_waveformatex_t wfx
;
513 wfx
.audiostream
= -1;
515 res
= asf_parse_header(fd
, id3
, &wfx
);
518 DEBUGF("ASF: parsing error - %d\n",res
);
522 if (wfx
.audiostream
== -1) {
523 DEBUGF("ASF: No WMA streams found\n");
527 asf_read_object_header(&obj
, fd
);
529 if (!asf_guid_match(&obj
.guid
, &asf_guid_data
)) {
530 DEBUGF("ASF: No data object found\n");
534 /* Store the current file position - no need to parse the header
535 again in the codec. The +26 skips the rest of the data object
538 id3
->first_frame_offset
= lseek(fd
, 0, SEEK_CUR
) + 26;
539 id3
->filesize
= filesize(fd
);
540 /* We copy the wfx struct to the MP3 TOC field in the id3 struct so
541 the codec doesn't need to parse the header object again */
542 memcpy(id3
->toc
, &wfx
, sizeof(wfx
));