1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
11 * Copyright (C) 2007 Dave Chapman
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
29 #include "replaygain.h"
31 #include "rbunicode.h"
32 #include "metadata_common.h"
33 #include "metadata_parsers.h"
35 #include <codecs/libasf/asf.h>
37 /* TODO: Just read the GUIDs into a 16-byte array, and use memcmp to compare */
44 typedef struct guid_s guid_t
;
51 typedef struct asf_object_s asf_object_t
;
53 static const guid_t asf_guid_null
=
54 {0x00000000, 0x0000, 0x0000, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
56 /* top level object guids */
58 static const guid_t asf_guid_header
=
59 {0x75B22630, 0x668E, 0x11CF, {0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C}};
61 static const guid_t asf_guid_data
=
62 {0x75B22636, 0x668E, 0x11CF, {0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C}};
64 static const guid_t asf_guid_index
=
65 {0x33000890, 0xE5B1, 0x11CF, {0x89, 0xF4, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xCB}};
67 /* header level object guids */
69 static const guid_t asf_guid_file_properties
=
70 {0x8cabdca1, 0xa947, 0x11cf, {0x8E, 0xe4, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65}};
72 static const guid_t asf_guid_stream_properties
=
73 {0xB7DC0791, 0xA9B7, 0x11CF, {0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65}};
75 static const guid_t asf_guid_content_description
=
76 {0x75B22633, 0x668E, 0x11CF, {0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C}};
78 static const guid_t asf_guid_extended_content_description
=
79 {0xD2D0A440, 0xE307, 0x11D2, {0x97, 0xF0, 0x00, 0xA0, 0xC9, 0x5E, 0xA8, 0x50}};
81 static const guid_t asf_guid_content_encryption
=
82 {0x2211b3fb, 0xbd23, 0x11d2, {0xb4, 0xb7, 0x00, 0xa0, 0xc9, 0x55, 0xfc, 0x6e}};
84 static const guid_t asf_guid_extended_content_encryption
=
85 {0x298ae614, 0x2622, 0x4c17, {0xb9, 0x35, 0xda, 0xe0, 0x7e, 0xe9, 0x28, 0x9c}};
87 /* stream type guids */
89 static const guid_t asf_guid_stream_type_audio
=
90 {0xF8699E40, 0x5B4D, 0x11CF, {0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B}};
92 static int asf_guid_match(const guid_t
*guid1
, const guid_t
*guid2
)
94 if((guid1
->v1
!= guid2
->v1
) ||
95 (guid1
->v2
!= guid2
->v2
) ||
96 (guid1
->v3
!= guid2
->v3
) ||
97 (memcmp(guid1
->v4
, guid2
->v4
, 8))) {
104 /* Read the 16 byte GUID from a file */
105 static void asf_readGUID(int fd
, guid_t
* guid
)
107 read_uint32le(fd
, &guid
->v1
);
108 read_uint16le(fd
, &guid
->v2
);
109 read_uint16le(fd
, &guid
->v3
);
110 read(fd
, guid
->v4
, 8);
113 static void asf_read_object_header(asf_object_t
*obj
, int fd
)
115 asf_readGUID(fd
, &obj
->guid
);
116 read_uint64le(fd
, &obj
->size
);
120 /* Parse an integer from the extended content object - we always
121 convert to an int, regardless of native format.
123 static int asf_intdecode(int fd
, int type
, int length
)
130 read_uint32le(fd
, &tmp32
);
131 lseek(fd
,length
- 4,SEEK_CUR
);
133 } else if (type
== 4) {
134 read_uint64le(fd
, &tmp64
);
135 lseek(fd
,length
- 8,SEEK_CUR
);
137 } else if (type
== 5) {
138 read_uint16le(fd
, &tmp16
);
139 lseek(fd
,length
- 2,SEEK_CUR
);
146 /* Decode a LE utf16 string from a disk buffer into a fixed-sized
150 static void asf_utf16LEdecode(int fd
,
152 unsigned char **utf8
,
158 unsigned char utf16buf
[256];
159 unsigned char* utf16
= utf16buf
;
160 unsigned char* newutf8
;
162 n
= read(fd
, utf16buf
, MIN(sizeof(utf16buf
), utf16bytes
));
166 /* Check for a surrogate pair */
167 if (utf16
[1] >= 0xD8 && utf16
[1] < 0xE0) {
169 /* Run out of utf16 bytes, read some more */
170 utf16buf
[0] = utf16
[0];
171 utf16buf
[1] = utf16
[1];
173 n
= read(fd
, utf16buf
+ 2, MIN(sizeof(utf16buf
)-2, utf16bytes
));
180 /* Truncated utf16 string, abort */
183 ucs
= 0x10000 + ((utf16
[0] << 10) | ((utf16
[1] - 0xD8) << 18)
184 | utf16
[2] | ((utf16
[3] - 0xDC) << 8));
188 ucs
= (utf16
[0] | (utf16
[1] << 8));
193 if (*utf8bytes
> 6) {
194 newutf8
= utf8encode(ucs
, *utf8
);
195 *utf8bytes
-= (newutf8
- *utf8
);
196 *utf8
+= (newutf8
- *utf8
);
199 /* We have run out of utf16 bytes, read more if available */
200 if ((n
== 0) && (utf16bytes
> 0)) {
201 n
= read(fd
, utf16buf
, MIN(sizeof(utf16buf
), utf16bytes
));
210 if (utf16bytes
> 0) {
211 /* Skip any remaining bytes */
212 lseek(fd
, utf16bytes
, SEEK_CUR
);
217 static int asf_parse_header(int fd
, struct mp3entry
* id3
,
218 asf_waveformatex_t
* wfx
)
220 asf_object_t current
;
225 uint64_t play_duration
;
228 uint8_t utf8buf
[512];
229 int id3buf_remaining
= sizeof(id3
->id3v2buf
) + sizeof(id3
->id3v1buf
);
230 unsigned char* id3buf
= (unsigned char*)id3
->id3v2buf
;
232 asf_read_object_header((asf_object_t
*) &header
, fd
);
234 //DEBUGF("header.size=%d\n",(int)header.size);
235 if (header
.size
< 30) {
236 /* invalid size for header object */
237 return ASF_ERROR_OBJECT_SIZE
;
240 read_uint32le(fd
, &subobjects
);
242 /* Two reserved bytes - do we need to read them? */
243 lseek(fd
, 2, SEEK_CUR
);
245 //DEBUGF("Read header - size=%d, subobjects=%d\n",(int)header.size, (int)subobjects);
247 if (subobjects
> 0) {
248 header
.datalen
= header
.size
- 30;
250 /* TODO: Check that we have datalen bytes left in the file */
251 datalen
= header
.datalen
;
253 for (i
=0; i
<(int)subobjects
; i
++) {
254 //DEBUGF("Parsing header object %d - datalen=%d\n",i,(int)datalen);
256 //DEBUGF("not enough data for reading object\n");
260 asf_read_object_header(¤t
, fd
);
262 if (current
.size
> datalen
|| current
.size
< 24) {
263 //DEBUGF("invalid object size - current.size=%d, datalen=%d\n",(int)current.size,(int)datalen);
267 if (asf_guid_match(¤t
.guid
, &asf_guid_file_properties
)) {
268 if (current
.size
< 104)
269 return ASF_ERROR_OBJECT_SIZE
;
272 /* multiple file properties objects not allowed */
273 return ASF_ERROR_INVALID_OBJECT
;
277 /* All we want is the play duration - uint64_t at offset 40 */
278 lseek(fd
, 40, SEEK_CUR
);
280 read_uint64le(fd
, &play_duration
);
281 id3
->length
= play_duration
/ 10000;
283 //DEBUGF("****** length = %lums\n", id3->length);
285 /* Read the packet size - uint32_t at offset 68 */
286 lseek(fd
, 20, SEEK_CUR
);
287 read_uint32le(fd
, &wfx
->packet_size
);
289 /* Skip bytes remaining in object */
290 lseek(fd
, current
.size
- 24 - 72, SEEK_CUR
);
291 } else if (asf_guid_match(¤t
.guid
, &asf_guid_stream_properties
)) {
293 uint32_t propdatalen
;
295 if (current
.size
< 78)
296 return ASF_ERROR_OBJECT_SIZE
;
299 asf_byteio_getGUID(&guid
, current
->data
);
300 datalen
= asf_byteio_getDWLE(current
->data
+ 40);
301 flags
= asf_byteio_getWLE(current
->data
+ 48);
304 asf_readGUID(fd
, &guid
);
306 lseek(fd
, 24, SEEK_CUR
);
307 read_uint32le(fd
, &propdatalen
);
308 lseek(fd
, 4, SEEK_CUR
);
309 read_uint16le(fd
, &flags
);
311 if (!asf_guid_match(&guid
, &asf_guid_stream_type_audio
)) {
312 //DEBUGF("Found stream properties for non audio stream, skipping\n");
313 lseek(fd
,current
.size
- 24 - 50,SEEK_CUR
);
314 } else if (wfx
->audiostream
== -1) {
315 lseek(fd
, 4, SEEK_CUR
);
316 //DEBUGF("Found stream properties for audio stream %d\n",flags&0x7f);
318 if (propdatalen
< 18) {
319 return ASF_ERROR_INVALID_LENGTH
;
323 if (asf_byteio_getWLE(data
+ 16) > datalen
- 16) {
324 return ASF_ERROR_INVALID_LENGTH
;
327 read_uint16le(fd
, &wfx
->codec_id
);
328 read_uint16le(fd
, &wfx
->channels
);
329 read_uint32le(fd
, &wfx
->rate
);
330 read_uint32le(fd
, &wfx
->bitrate
);
332 read_uint16le(fd
, &wfx
->blockalign
);
333 read_uint16le(fd
, &wfx
->bitspersample
);
334 read_uint16le(fd
, &wfx
->datalen
);
336 /* Round bitrate to the nearest kbit */
337 id3
->bitrate
= (wfx
->bitrate
+ 500) / 1000;
338 id3
->frequency
= wfx
->rate
;
340 if (wfx
->codec_id
== ASF_CODEC_ID_WMAV1
) {
341 read(fd
, wfx
->data
, 4);
342 lseek(fd
,current
.size
- 24 - 72 - 4,SEEK_CUR
);
343 wfx
->audiostream
= flags
&0x7f;
344 } else if (wfx
->codec_id
== ASF_CODEC_ID_WMAV2
) {
345 read(fd
, wfx
->data
, 6);
346 lseek(fd
,current
.size
- 24 - 72 - 6,SEEK_CUR
);
347 wfx
->audiostream
= flags
&0x7f;
349 DEBUGF("Unsupported WMA codec (Pro, Lossless, Voice, etc)\n");
350 lseek(fd
,current
.size
- 24 - 72,SEEK_CUR
);
354 } else if (asf_guid_match(¤t
.guid
, &asf_guid_content_description
)) {
355 /* Object contains five 16-bit string lengths, followed by the five strings:
356 title, artist, copyright, description, rating
358 uint16_t strlength
[5];
361 //DEBUGF("Found GUID_CONTENT_DESCRIPTION - size=%d\n",(int)(current.size - 24));
363 /* Read the 5 string lengths - number of bytes included trailing zero */
364 for (i
=0; i
<5; i
++) {
365 read_uint16le(fd
, &strlength
[i
]);
366 //DEBUGF("strlength = %u\n",strlength[i]);
369 if (strlength
[0] > 0) { /* 0 - Title */
371 asf_utf16LEdecode(fd
, strlength
[0], &id3buf
, &id3buf_remaining
);
374 if (strlength
[1] > 0) { /* 1 - Artist */
375 id3
->artist
= id3buf
;
376 asf_utf16LEdecode(fd
, strlength
[1], &id3buf
, &id3buf_remaining
);
379 lseek(fd
, strlength
[2], SEEK_CUR
); /* 2 - copyright */
381 if (strlength
[3] > 0) { /* 3 - description */
382 id3
->comment
= id3buf
;
383 asf_utf16LEdecode(fd
, strlength
[3], &id3buf
, &id3buf_remaining
);
386 lseek(fd
, strlength
[4], SEEK_CUR
); /* 4 - rating */
387 } else if (asf_guid_match(¤t
.guid
, &asf_guid_extended_content_description
)) {
390 int bytesleft
= current
.size
- 24;
391 //DEBUGF("Found GUID_EXTENDED_CONTENT_DESCRIPTION\n");
393 read_uint16le(fd
, &count
);
395 //DEBUGF("extended metadata count = %u\n",count);
397 for (i
=0; i
< count
; i
++) {
398 uint16_t length
, type
;
399 unsigned char* utf8
= utf8buf
;
400 int utf8length
= 512;
402 read_uint16le(fd
, &length
);
403 asf_utf16LEdecode(fd
, length
, &utf8
, &utf8length
);
404 bytesleft
-= 2 + length
;
406 read_uint16le(fd
, &type
);
407 read_uint16le(fd
, &length
);
409 if (!strcmp("WM/TrackNumber",utf8buf
)) {
411 id3
->track_string
= id3buf
;
412 asf_utf16LEdecode(fd
, length
, &id3buf
, &id3buf_remaining
);
413 id3
->tracknum
= atoi(id3
->track_string
);
414 } else if ((type
>=2) && (type
<= 5)) {
415 id3
->tracknum
= asf_intdecode(fd
, type
, length
);
417 lseek(fd
, length
, SEEK_CUR
);
419 } else if ((!strcmp("WM/Genre", utf8buf
)) && (type
== 0)) {
420 id3
->genre_string
= id3buf
;
421 asf_utf16LEdecode(fd
, length
, &id3buf
, &id3buf_remaining
);
422 } else if ((!strcmp("WM/AlbumTitle", utf8buf
)) && (type
== 0)) {
424 asf_utf16LEdecode(fd
, length
, &id3buf
, &id3buf_remaining
);
425 } else if ((!strcmp("WM/AlbumArtist", utf8buf
)) && (type
== 0)) {
426 id3
->albumartist
= id3buf
;
427 asf_utf16LEdecode(fd
, length
, &id3buf
, &id3buf_remaining
);
428 } else if ((!strcmp("WM/Composer", utf8buf
)) && (type
== 0)) {
429 id3
->composer
= id3buf
;
430 asf_utf16LEdecode(fd
, length
, &id3buf
, &id3buf_remaining
);
431 } else if (!strcmp("WM/Year", utf8buf
)) {
433 id3
->year_string
= id3buf
;
434 asf_utf16LEdecode(fd
, length
, &id3buf
, &id3buf_remaining
);
435 id3
->year
= atoi(id3
->year_string
);
436 } else if ((type
>=2) && (type
<= 5)) {
437 id3
->year
= asf_intdecode(fd
, type
, length
);
439 lseek(fd
, length
, SEEK_CUR
);
441 } else if (!strncmp("replaygain_", utf8buf
, 11)) {
442 char* value
= id3buf
;
443 int buf_len
= id3buf_remaining
;
445 asf_utf16LEdecode(fd
, length
, &id3buf
, &id3buf_remaining
);
446 len
= parse_replaygain(utf8buf
, value
, id3
,
450 /* Don't need to keep the value */
452 id3buf_remaining
= buf_len
;
454 } else if (!strcmp("MusicBrainz/Track Id", utf8buf
)) {
455 id3
->mb_track_id
= id3buf
;
456 asf_utf16LEdecode(fd
, length
, &id3buf
, &id3buf_remaining
);
458 lseek(fd
, length
, SEEK_CUR
);
460 bytesleft
-= 4 + length
;
463 lseek(fd
, bytesleft
, SEEK_CUR
);
464 } else if (asf_guid_match(¤t
.guid
, &asf_guid_content_encryption
)
465 || asf_guid_match(¤t
.guid
, &asf_guid_extended_content_encryption
)) {
466 //DEBUGF("File is encrypted\n");
467 return ASF_ERROR_ENCRYPTED
;
469 //DEBUGF("Skipping %d bytes of object\n",(int)(current.size - 24));
470 lseek(fd
,current
.size
- 24,SEEK_CUR
);
473 //DEBUGF("Parsed object - size = %d\n",(int)current.size);
474 datalen
-= current
.size
;
477 if (i
!= (int)subobjects
|| datalen
!= 0) {
478 //DEBUGF("header data doesn't match given subobject count\n");
479 return ASF_ERROR_INVALID_VALUE
;
482 //DEBUGF("%d subobjects read successfully\n", i);
486 tmp
= asf_parse_header_validate(file
, &header
);
488 /* header read ok but doesn't validate correctly */
493 //DEBUGF("header validated correctly\n");
498 bool get_asf_metadata(int fd
, struct mp3entry
* id3
)
502 asf_waveformatex_t wfx
;
504 wfx
.audiostream
= -1;
506 res
= asf_parse_header(fd
, id3
, &wfx
);
509 DEBUGF("ASF: parsing error - %d\n",res
);
513 if (wfx
.audiostream
== -1) {
514 DEBUGF("ASF: No WMA streams found\n");
518 asf_read_object_header(&obj
, fd
);
520 if (!asf_guid_match(&obj
.guid
, &asf_guid_data
)) {
521 DEBUGF("ASF: No data object found\n");
525 /* Store the current file position - no need to parse the header
526 again in the codec. The +26 skips the rest of the data object
529 id3
->first_frame_offset
= lseek(fd
, 0, SEEK_CUR
) + 26;
530 id3
->filesize
= filesize(fd
);
531 /* We copy the wfx struct to the MP3 TOC field in the id3 struct so
532 the codec doesn't need to parse the header object again */
533 memcpy(id3
->toc
, &wfx
, sizeof(wfx
));