Remove executable property that somehow got set
[kugel-rb.git] / apps / metadata / asf.c
blob611cc2aca3c4a053b0a94b4f58c8287b60241f99
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * $Id$
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 ****************************************************************************/
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <ctype.h>
26 #include <inttypes.h>
28 #include "metadata.h"
29 #include "replaygain.h"
30 #include "debug.h"
31 #include "rbunicode.h"
32 #include "metadata_common.h"
33 #include "metadata_parsers.h"
34 #include "system.h"
35 #include <codecs/libwma/asf.h>
37 /* TODO: Just read the GUIDs into a 16-byte array, and use memcmp to compare */
38 struct guid_s {
39 uint32_t v1;
40 uint16_t v2;
41 uint16_t v3;
42 uint8_t v4[8];
44 typedef struct guid_s guid_t;
46 struct asf_object_s {
47 guid_t guid;
48 uint64_t size;
49 uint64_t datalen;
51 typedef struct asf_object_s asf_object_t;
53 enum asf_error_e {
54 ASF_ERROR_INTERNAL = -1, /* incorrect input to API calls */
55 ASF_ERROR_OUTOFMEM = -2, /* some malloc inside program failed */
56 ASF_ERROR_EOF = -3, /* unexpected end of file */
57 ASF_ERROR_IO = -4, /* error reading or writing to file */
58 ASF_ERROR_INVALID_LENGTH = -5, /* length value conflict in input data */
59 ASF_ERROR_INVALID_VALUE = -6, /* other value conflict in input data */
60 ASF_ERROR_INVALID_OBJECT = -7, /* ASF object missing or in wrong place */
61 ASF_ERROR_OBJECT_SIZE = -8, /* invalid ASF object size (too small) */
62 ASF_ERROR_SEEKABLE = -9, /* file not seekable */
63 ASF_ERROR_SEEK = -10, /* file is seekable but seeking failed */
64 ASF_ERROR_ENCRYPTED = -11 /* file is encrypted */
67 static const guid_t asf_guid_null =
68 {0x00000000, 0x0000, 0x0000, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
70 /* top level object guids */
72 static const guid_t asf_guid_header =
73 {0x75B22630, 0x668E, 0x11CF, {0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C}};
75 static const guid_t asf_guid_data =
76 {0x75B22636, 0x668E, 0x11CF, {0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C}};
78 static const guid_t asf_guid_index =
79 {0x33000890, 0xE5B1, 0x11CF, {0x89, 0xF4, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xCB}};
81 /* header level object guids */
83 static const guid_t asf_guid_file_properties =
84 {0x8cabdca1, 0xa947, 0x11cf, {0x8E, 0xe4, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65}};
86 static const guid_t asf_guid_stream_properties =
87 {0xB7DC0791, 0xA9B7, 0x11CF, {0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65}};
89 static const guid_t asf_guid_content_description =
90 {0x75B22633, 0x668E, 0x11CF, {0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C}};
92 static const guid_t asf_guid_extended_content_description =
93 {0xD2D0A440, 0xE307, 0x11D2, {0x97, 0xF0, 0x00, 0xA0, 0xC9, 0x5E, 0xA8, 0x50}};
95 static const guid_t asf_guid_content_encryption =
96 {0x2211b3fb, 0xbd23, 0x11d2, {0xb4, 0xb7, 0x00, 0xa0, 0xc9, 0x55, 0xfc, 0x6e}};
98 static const guid_t asf_guid_extended_content_encryption =
99 {0x298ae614, 0x2622, 0x4c17, {0xb9, 0x35, 0xda, 0xe0, 0x7e, 0xe9, 0x28, 0x9c}};
101 /* stream type guids */
103 static const guid_t asf_guid_stream_type_audio =
104 {0xF8699E40, 0x5B4D, 0x11CF, {0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B}};
106 static int asf_guid_match(const guid_t *guid1, const guid_t *guid2)
108 if((guid1->v1 != guid2->v1) ||
109 (guid1->v2 != guid2->v2) ||
110 (guid1->v3 != guid2->v3) ||
111 (memcmp(guid1->v4, guid2->v4, 8))) {
112 return 0;
115 return 1;
118 /* Read the 16 byte GUID from a file */
119 static void asf_readGUID(int fd, guid_t* guid)
121 read_uint32le(fd, &guid->v1);
122 read_uint16le(fd, &guid->v2);
123 read_uint16le(fd, &guid->v3);
124 read(fd, guid->v4, 8);
127 static void asf_read_object_header(asf_object_t *obj, int fd)
129 asf_readGUID(fd, &obj->guid);
130 read_uint64le(fd, &obj->size);
131 obj->datalen = 0;
134 /* Parse an integer from the extended content object - we always
135 convert to an int, regardless of native format.
137 static int asf_intdecode(int fd, int type, int length)
139 uint16_t tmp16;
140 uint32_t tmp32;
141 uint64_t tmp64;
143 if (type==3) {
144 read_uint32le(fd, &tmp32);
145 lseek(fd,length - 4,SEEK_CUR);
146 return (int)tmp32;
147 } else if (type==4) {
148 read_uint64le(fd, &tmp64);
149 lseek(fd,length - 8,SEEK_CUR);
150 return (int)tmp64;
151 } else if (type == 5) {
152 read_uint16le(fd, &tmp16);
153 lseek(fd,length - 2,SEEK_CUR);
154 return (int)tmp16;
157 return 0;
160 /* Decode a LE utf16 string from a disk buffer into a fixed-sized
161 utf8 buffer.
164 static void asf_utf16LEdecode(int fd,
165 uint16_t utf16bytes,
166 unsigned char **utf8,
167 int* utf8bytes
170 unsigned long ucs;
171 int n;
172 unsigned char utf16buf[256];
173 unsigned char* utf16 = utf16buf;
174 unsigned char* newutf8;
176 n = read(fd, utf16buf, MIN(sizeof(utf16buf), utf16bytes));
177 utf16bytes -= n;
179 while (n > 0) {
180 /* Check for a surrogate pair */
181 if (utf16[1] >= 0xD8 && utf16[1] < 0xE0) {
182 if (n < 4) {
183 /* Run out of utf16 bytes, read some more */
184 utf16buf[0] = utf16[0];
185 utf16buf[1] = utf16[1];
187 n = read(fd, utf16buf + 2, MIN(sizeof(utf16buf)-2, utf16bytes));
188 utf16 = utf16buf;
189 utf16bytes -= n;
190 n += 2;
193 if (n < 4) {
194 /* Truncated utf16 string, abort */
195 break;
197 ucs = 0x10000 + ((utf16[0] << 10) | ((utf16[1] - 0xD8) << 18)
198 | utf16[2] | ((utf16[3] - 0xDC) << 8));
199 utf16 += 4;
200 n -= 4;
201 } else {
202 ucs = (utf16[0] | (utf16[1] << 8));
203 utf16 += 2;
204 n -= 2;
207 if (*utf8bytes > 6) {
208 newutf8 = utf8encode(ucs, *utf8);
209 *utf8bytes -= (newutf8 - *utf8);
210 *utf8 += (newutf8 - *utf8);
213 /* We have run out of utf16 bytes, read more if available */
214 if ((n == 0) && (utf16bytes > 0)) {
215 n = read(fd, utf16buf, MIN(sizeof(utf16buf), utf16bytes));
216 utf16 = utf16buf;
217 utf16bytes -= n;
221 *utf8[0] = 0;
222 --*utf8bytes;
224 if (utf16bytes > 0) {
225 /* Skip any remaining bytes */
226 lseek(fd, utf16bytes, SEEK_CUR);
228 return;
231 static int asf_parse_header(int fd, struct mp3entry* id3,
232 asf_waveformatex_t* wfx)
234 asf_object_t current;
235 asf_object_t header;
236 uint64_t datalen;
237 int i;
238 int fileprop = 0;
239 uint64_t play_duration;
240 uint16_t flags;
241 uint32_t subobjects;
242 uint8_t utf8buf[512];
243 int id3buf_remaining = sizeof(id3->id3v2buf) + sizeof(id3->id3v1buf);
244 unsigned char* id3buf = (unsigned char*)id3->id3v2buf;
246 asf_read_object_header((asf_object_t *) &header, fd);
248 //DEBUGF("header.size=%d\n",(int)header.size);
249 if (header.size < 30) {
250 /* invalid size for header object */
251 return ASF_ERROR_OBJECT_SIZE;
254 read_uint32le(fd, &subobjects);
256 /* Two reserved bytes - do we need to read them? */
257 lseek(fd, 2, SEEK_CUR);
259 //DEBUGF("Read header - size=%d, subobjects=%d\n",(int)header.size, (int)subobjects);
261 if (subobjects > 0) {
262 header.datalen = header.size - 30;
264 /* TODO: Check that we have datalen bytes left in the file */
265 datalen = header.datalen;
267 for (i=0; i<(int)subobjects; i++) {
268 //DEBUGF("Parsing header object %d - datalen=%d\n",i,(int)datalen);
269 if (datalen < 24) {
270 //DEBUGF("not enough data for reading object\n");
271 break;
274 asf_read_object_header(&current, fd);
276 if (current.size > datalen || current.size < 24) {
277 //DEBUGF("invalid object size - current.size=%d, datalen=%d\n",(int)current.size,(int)datalen);
278 break;
281 if (asf_guid_match(&current.guid, &asf_guid_file_properties)) {
282 if (current.size < 104)
283 return ASF_ERROR_OBJECT_SIZE;
285 if (fileprop) {
286 /* multiple file properties objects not allowed */
287 return ASF_ERROR_INVALID_OBJECT;
290 fileprop = 1;
291 /* All we want is the play duration - uint64_t at offset 40 */
292 lseek(fd, 40, SEEK_CUR);
294 read_uint64le(fd, &play_duration);
295 id3->length = play_duration / 10000;
297 //DEBUGF("****** length = %lums\n", id3->length);
299 /* Read the packet size - uint32_t at offset 68 */
300 lseek(fd, 20, SEEK_CUR);
301 read_uint32le(fd, &wfx->packet_size);
303 /* Skip bytes remaining in object */
304 lseek(fd, current.size - 24 - 72, SEEK_CUR);
305 } else if (asf_guid_match(&current.guid, &asf_guid_stream_properties)) {
306 guid_t guid;
307 uint32_t propdatalen;
309 if (current.size < 78)
310 return ASF_ERROR_OBJECT_SIZE;
312 #if 0
313 asf_byteio_getGUID(&guid, current->data);
314 datalen = asf_byteio_getDWLE(current->data + 40);
315 flags = asf_byteio_getWLE(current->data + 48);
316 #endif
318 asf_readGUID(fd, &guid);
320 lseek(fd, 24, SEEK_CUR);
321 read_uint32le(fd, &propdatalen);
322 lseek(fd, 4, SEEK_CUR);
323 read_uint16le(fd, &flags);
325 if (!asf_guid_match(&guid, &asf_guid_stream_type_audio)) {
326 //DEBUGF("Found stream properties for non audio stream, skipping\n");
327 lseek(fd,current.size - 24 - 50,SEEK_CUR);
328 } else if (wfx->audiostream == -1) {
329 lseek(fd, 4, SEEK_CUR);
330 //DEBUGF("Found stream properties for audio stream %d\n",flags&0x7f);
332 if (propdatalen < 18) {
333 return ASF_ERROR_INVALID_LENGTH;
336 #if 0
337 if (asf_byteio_getWLE(data + 16) > datalen - 16) {
338 return ASF_ERROR_INVALID_LENGTH;
340 #endif
341 read_uint16le(fd, &wfx->codec_id);
342 read_uint16le(fd, &wfx->channels);
343 read_uint32le(fd, &wfx->rate);
344 read_uint32le(fd, &wfx->bitrate);
345 wfx->bitrate *= 8;
346 read_uint16le(fd, &wfx->blockalign);
347 read_uint16le(fd, &wfx->bitspersample);
348 read_uint16le(fd, &wfx->datalen);
350 /* Round bitrate to the nearest kbit */
351 id3->bitrate = (wfx->bitrate + 500) / 1000;
352 id3->frequency = wfx->rate;
354 if (wfx->codec_id == ASF_CODEC_ID_WMAV1) {
355 read(fd, wfx->data, 4);
356 lseek(fd,current.size - 24 - 72 - 4,SEEK_CUR);
357 wfx->audiostream = flags&0x7f;
358 } else if (wfx->codec_id == ASF_CODEC_ID_WMAV2) {
359 read(fd, wfx->data, 6);
360 lseek(fd,current.size - 24 - 72 - 6,SEEK_CUR);
361 wfx->audiostream = flags&0x7f;
362 } else {
363 DEBUGF("Unsupported WMA codec (Pro, Lossless, Voice, etc)\n");
364 lseek(fd,current.size - 24 - 72,SEEK_CUR);
368 } else if (asf_guid_match(&current.guid, &asf_guid_content_description)) {
369 /* Object contains five 16-bit string lengths, followed by the five strings:
370 title, artist, copyright, description, rating
372 uint16_t strlength[5];
373 int i;
375 //DEBUGF("Found GUID_CONTENT_DESCRIPTION - size=%d\n",(int)(current.size - 24));
377 /* Read the 5 string lengths - number of bytes included trailing zero */
378 for (i=0; i<5; i++) {
379 read_uint16le(fd, &strlength[i]);
380 //DEBUGF("strlength = %u\n",strlength[i]);
383 if (strlength[0] > 0) { /* 0 - Title */
384 id3->title = id3buf;
385 asf_utf16LEdecode(fd, strlength[0], &id3buf, &id3buf_remaining);
388 if (strlength[1] > 0) { /* 1 - Artist */
389 id3->artist = id3buf;
390 asf_utf16LEdecode(fd, strlength[1], &id3buf, &id3buf_remaining);
393 lseek(fd, strlength[2], SEEK_CUR); /* 2 - copyright */
395 if (strlength[3] > 0) { /* 3 - description */
396 id3->comment = id3buf;
397 asf_utf16LEdecode(fd, strlength[3], &id3buf, &id3buf_remaining);
400 lseek(fd, strlength[4], SEEK_CUR); /* 4 - rating */
401 } else if (asf_guid_match(&current.guid, &asf_guid_extended_content_description)) {
402 uint16_t count;
403 int i;
404 int bytesleft = current.size - 24;
405 //DEBUGF("Found GUID_EXTENDED_CONTENT_DESCRIPTION\n");
407 read_uint16le(fd, &count);
408 bytesleft -= 2;
409 //DEBUGF("extended metadata count = %u\n",count);
411 for (i=0; i < count; i++) {
412 uint16_t length, type;
413 unsigned char* utf8 = utf8buf;
414 int utf8length = 512;
416 read_uint16le(fd, &length);
417 asf_utf16LEdecode(fd, length, &utf8, &utf8length);
418 bytesleft -= 2 + length;
420 read_uint16le(fd, &type);
421 read_uint16le(fd, &length);
423 if (!strcmp("WM/TrackNumber",utf8buf)) {
424 if (type == 0) {
425 id3->track_string = id3buf;
426 asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining);
427 id3->tracknum = atoi(id3->track_string);
428 } else if ((type >=2) && (type <= 5)) {
429 id3->tracknum = asf_intdecode(fd, type, length);
430 } else {
431 lseek(fd, length, SEEK_CUR);
433 } else if ((!strcmp("WM/Genre",utf8buf)) && (type == 0)) {
434 id3->genre_string = id3buf;
435 asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining);
436 } else if ((!strcmp("WM/AlbumTitle",utf8buf)) && (type == 0)) {
437 id3->album = id3buf;
438 asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining);
439 } else if ((!strcmp("WM/AlbumArtist",utf8buf)) && (type == 0)) {
440 id3->albumartist = id3buf;
441 asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining);
442 } else if ((!strcmp("WM/Composer",utf8buf)) && (type == 0)) {
443 id3->composer = id3buf;
444 asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining);
445 } else if (!strcmp("WM/Year",utf8buf)) {
446 if (type == 0) {
447 id3->year_string = id3buf;
448 asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining);
449 id3->year = atoi(id3->year_string);
450 } else if ((type >=2) && (type <= 5)) {
451 id3->year = asf_intdecode(fd, type, length);
452 } else {
453 lseek(fd, length, SEEK_CUR);
455 } else if (!strncmp("replaygain_", utf8buf, 11)) {
456 char* value = id3buf;
457 int buf_len = id3buf_remaining;
458 int len;
459 asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining);
460 len = parse_replaygain(utf8buf, value, id3,
461 value, buf_len);
463 if (len == 0) {
464 /* Don't need to keep the value */
465 id3buf = value;
466 id3buf_remaining = buf_len;
468 } else if (!strcmp("MusicBrainz/Track Id", utf8buf)) {
469 id3->mb_track_id = id3buf;
470 asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining);
471 } else {
472 lseek(fd, length, SEEK_CUR);
474 bytesleft -= 4 + length;
477 lseek(fd, bytesleft, SEEK_CUR);
478 } else if (asf_guid_match(&current.guid, &asf_guid_content_encryption)
479 || asf_guid_match(&current.guid, &asf_guid_extended_content_encryption)) {
480 //DEBUGF("File is encrypted\n");
481 return ASF_ERROR_ENCRYPTED;
482 } else {
483 //DEBUGF("Skipping %d bytes of object\n",(int)(current.size - 24));
484 lseek(fd,current.size - 24,SEEK_CUR);
487 //DEBUGF("Parsed object - size = %d\n",(int)current.size);
488 datalen -= current.size;
491 if (i != (int)subobjects || datalen != 0) {
492 //DEBUGF("header data doesn't match given subobject count\n");
493 return ASF_ERROR_INVALID_VALUE;
496 //DEBUGF("%d subobjects read successfully\n", i);
499 #if 0
500 tmp = asf_parse_header_validate(file, &header);
501 if (tmp < 0) {
502 /* header read ok but doesn't validate correctly */
503 return tmp;
505 #endif
507 //DEBUGF("header validated correctly\n");
509 return 0;
512 bool get_asf_metadata(int fd, struct mp3entry* id3)
514 int res;
515 asf_object_t obj;
516 asf_waveformatex_t wfx;
518 wfx.audiostream = -1;
520 res = asf_parse_header(fd, id3, &wfx);
522 if (res < 0) {
523 DEBUGF("ASF: parsing error - %d\n",res);
524 return false;
527 if (wfx.audiostream == -1) {
528 DEBUGF("ASF: No WMA streams found\n");
529 return false;
532 asf_read_object_header(&obj, fd);
534 if (!asf_guid_match(&obj.guid, &asf_guid_data)) {
535 DEBUGF("ASF: No data object found\n");
536 return false;
539 /* Store the current file position - no need to parse the header
540 again in the codec. The +26 skips the rest of the data object
541 header.
543 id3->first_frame_offset = lseek(fd, 0, SEEK_CUR) + 26;
544 id3->filesize = filesize(fd);
545 /* We copy the wfx struct to the MP3 TOC field in the id3 struct so
546 the codec doesn't need to parse the header object again */
547 memcpy(id3->toc, &wfx, sizeof(wfx));
549 return true;