add missing command line to ipod 3g installation instructions
[Rockbox.git] / apps / metadata.c
blobee0100ecf7a93bb59a6017fc4cab25ced4dff6d3
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Dave Chapman
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include <stdio.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <ctype.h>
23 #include <inttypes.h>
25 #include "errno.h"
26 #include "metadata.h"
27 #include "mp3_playback.h"
28 #include "logf.h"
29 #include "atoi.h"
30 #include "replaygain.h"
31 #include "debug.h"
32 #include "system.h"
34 enum tagtype { TAGTYPE_APE = 1, TAGTYPE_VORBIS };
36 #define APETAG_HEADER_LENGTH 32
37 #define APETAG_HEADER_FORMAT "8LLLL"
38 #define APETAG_ITEM_HEADER_FORMAT "LL"
39 #define APETAG_ITEM_TYPE_MASK 3
41 #define TAG_NAME_LENGTH 32
42 #define TAG_VALUE_LENGTH 128
44 #define MP4_ID(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
46 #define MP4_3gp6 MP4_ID('3', 'g', 'p', '6')
47 #define MP4_alac MP4_ID('a', 'l', 'a', 'c')
48 #define MP4_calb MP4_ID(0xa9, 'a', 'l', 'b')
49 #define MP4_cART MP4_ID(0xa9, 'A', 'R', 'T')
50 #define MP4_cnam MP4_ID(0xa9, 'n', 'a', 'm')
51 #define MP4_cwrt MP4_ID(0xa9, 'w', 'r', 't')
52 #define MP4_ftyp MP4_ID('f', 't', 'y', 'p')
53 #define MP4_gnre MP4_ID('g', 'n', 'r', 'e')
54 #define MP4_hdlr MP4_ID('h', 'd', 'l', 'r')
55 #define MP4_ilst MP4_ID('i', 'l', 's', 't')
56 #define MP4_M4A MP4_ID('M', '4', 'A', ' ')
57 #define MP4_mdat MP4_ID('m', 'd', 'a', 't')
58 #define MP4_mdia MP4_ID('m', 'd', 'i', 'a')
59 #define MP4_mdir MP4_ID('m', 'd', 'i', 'r')
60 #define MP4_meta MP4_ID('m', 'e', 't', 'a')
61 #define MP4_minf MP4_ID('m', 'i', 'n', 'f')
62 #define MP4_moov MP4_ID('m', 'o', 'o', 'v')
63 #define MP4_mp4a MP4_ID('m', 'p', '4', 'a')
64 #define MP4_mp42 MP4_ID('m', 'p', '4', '2')
65 #define MP4_qt MP4_ID('q', 't', ' ', ' ')
66 #define MP4_soun MP4_ID('s', 'o', 'u', 'n')
67 #define MP4_stbl MP4_ID('s', 't', 'b', 'l')
68 #define MP4_stsd MP4_ID('s', 't', 's', 'd')
69 #define MP4_stts MP4_ID('s', 't', 't', 's')
70 #define MP4_trak MP4_ID('t', 'r', 'a', 'k')
71 #define MP4_trkn MP4_ID('t', 'r', 'k', 'n')
72 #define MP4_udta MP4_ID('u', 'd', 't', 'a')
73 #define MP4_extra MP4_ID('-', '-', '-', '-')
75 struct apetag_header
77 char id[8];
78 long version;
79 long length;
80 long item_count;
81 long flags;
82 char reserved[8];
85 struct apetag_item_header
87 long length;
88 long flags;
91 struct format_list
93 char format;
94 char extension[5];
97 static const struct format_list formats[] =
99 { AFMT_MPA_L1, "mp1" },
100 { AFMT_MPA_L2, "mp2" },
101 { AFMT_MPA_L2, "mpa" },
102 { AFMT_MPA_L3, "mp3" },
103 #if CONFIG_CODEC == SWCODEC
104 { AFMT_OGG_VORBIS, "ogg" },
105 { AFMT_PCM_WAV, "wav" },
106 { AFMT_FLAC, "flac" },
107 { AFMT_MPC, "mpc" },
108 { AFMT_A52, "a52" },
109 { AFMT_A52, "ac3" },
110 { AFMT_WAVPACK, "wv" },
111 { AFMT_ALAC, "m4a" },
112 { AFMT_AAC, "mp4" },
113 { AFMT_SHN, "shn" },
114 { AFMT_AIFF, "aif" },
115 { AFMT_AIFF, "aiff" },
116 { AFMT_SID, "sid" },
117 { AFMT_ADX, "adx" },
118 #endif
121 #if CONFIG_CODEC == SWCODEC
122 static const unsigned short a52_bitrates[] =
124 32, 40, 48, 56, 64, 80, 96, 112, 128, 160,
125 192, 224, 256, 320, 384, 448, 512, 576, 640
128 /* Only store frame sizes for 44.1KHz - others are simply multiples
129 of the bitrate */
130 static const unsigned short a52_441framesizes[] =
132 69 * 2, 70 * 2, 87 * 2, 88 * 2, 104 * 2, 105 * 2, 121 * 2,
133 122 * 2, 139 * 2, 140 * 2, 174 * 2, 175 * 2, 208 * 2, 209 * 2,
134 243 * 2, 244 * 2, 278 * 2, 279 * 2, 348 * 2, 349 * 2, 417 * 2,
135 418 * 2, 487 * 2, 488 * 2, 557 * 2, 558 * 2, 696 * 2, 697 * 2,
136 835 * 2, 836 * 2, 975 * 2, 976 * 2, 1114 * 2, 1115 * 2, 1253 * 2,
137 1254 * 2, 1393 * 2, 1394 * 2
140 static const long wavpack_sample_rates [] =
142 6000, 8000, 9600, 11025, 12000, 16000, 22050, 24000,
143 32000, 44100, 48000, 64000, 88200, 96000, 192000
146 /* Read a string from the file. Read up to size bytes, or, if eos != -1,
147 * until the eos character is found (eos is not stored in buf, unless it is
148 * nil). Writes up to buf_size chars to buf, always terminating with a nil.
149 * Returns number of chars read or -1 on read error.
151 static long read_string(int fd, char* buf, long buf_size, int eos, long size)
153 long read_bytes = 0;
154 char c;
156 while (size != 0)
158 if (read(fd, &c, 1) != 1)
160 read_bytes = -1;
161 break;
164 read_bytes++;
165 size--;
167 if ((eos != -1) && (eos == (unsigned char) c))
169 break;
172 if (buf_size > 1)
174 *buf++ = c;
175 buf_size--;
179 *buf = 0;
180 return read_bytes;
183 /* Convert a little-endian structure to native format using a format string.
184 * Does nothing on a little-endian machine.
186 static void convert_endian(void *data, const char *format)
188 while (*format)
190 switch (*format)
192 case 'L':
194 long* d = (long*) data;
196 *d = letoh32(*d);
197 data = d + 1;
200 break;
202 case 'S':
204 short* d = (short*) data;
206 *d = letoh16(*d);
207 data = d + 1;
210 break;
212 default:
213 if (isdigit(*format))
215 data = ((char*) data) + *format - '0';
218 break;
221 format++;
225 /* Read an unsigned 16-bit integer from a big-endian file. */
226 #ifdef ROCKBOX_BIG_ENDIAN
227 #define read_uint16be(fd, buf) read((fd), (buf), 2)
228 #else
229 int read_uint16be(int fd, unsigned short* buf)
231 size_t n;
233 n = read(fd, (char*) buf, 2);
234 *buf = betoh16(*buf);
235 return n;
237 #endif
239 /* Read an unsigned 32-bit integer from a big-endian file. */
240 #ifdef ROCKBOX_BIG_ENDIAN
241 #define read_uint32be(fd,buf) read((fd), (buf), 4)
242 #else
243 int read_uint32be(int fd, unsigned int* buf)
245 size_t n;
247 n = read(fd, (char*) buf, 4);
248 *buf = betoh32(*buf);
249 return n;
251 #endif
253 /* Read an unaligned 32-bit little endian long from buffer. */
254 static unsigned long get_long(void* buf)
256 unsigned char* p = (unsigned char*) buf;
258 return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
261 /* Read a string tag from an M4A file */
262 void read_m4a_tag_string(int fd, int len,char** bufptr,size_t* bytes_remaining, char** dest)
264 int data_length;
266 if (bytes_remaining==0) {
267 lseek(fd,len,SEEK_CUR); /* Skip everything */
268 } else {
269 /* Skip the data tag header - maybe we should parse it properly? */
270 lseek(fd,16,SEEK_CUR);
271 len-=16;
273 *dest=*bufptr;
274 if ((size_t)len+1 > *bytes_remaining) {
275 read(fd,*bufptr,*bytes_remaining-1);
276 lseek(fd,len-(*bytes_remaining-1),SEEK_CUR);
277 *bufptr+=(*bytes_remaining-1);
278 } else {
279 read(fd,*bufptr,len);
280 *bufptr+=len;
282 **bufptr=(char)0;
284 data_length = strlen(*dest)+1;
285 *bufptr=(*dest)+data_length;
286 *bytes_remaining-=data_length;
290 /* Parse the tag (the name-value pair) and fill id3 and buffer accordingly.
291 * String values to keep are written to buf. Returns number of bytes written
292 * to buf (including end nil).
294 static long parse_tag(const char* name, char* value, struct mp3entry* id3,
295 char* buf, long buf_remaining, enum tagtype type)
297 long len = 0;
298 char** p;
300 if ((((strcasecmp(name, "track") == 0) && (type == TAGTYPE_APE)))
301 || ((strcasecmp(name, "tracknumber") == 0) && (type == TAGTYPE_VORBIS)))
303 id3->tracknum = atoi(value);
304 p = &(id3->track_string);
306 else if (((strcasecmp(name, "year") == 0) && (type == TAGTYPE_APE))
307 || ((strcasecmp(name, "date") == 0) && (type == TAGTYPE_VORBIS)))
309 /* Date can be in more any format in a Vorbis tag, so don't try to
310 * parse it.
312 if (type != TAGTYPE_VORBIS)
314 id3->year = atoi(value);
317 p = &(id3->year_string);
319 else if (strcasecmp(name, "title") == 0)
321 p = &(id3->title);
323 else if (strcasecmp(name, "artist") == 0)
325 p = &(id3->artist);
327 else if (strcasecmp(name, "album") == 0)
329 p = &(id3->album);
331 else if (strcasecmp(name, "genre") == 0)
333 p = &(id3->genre_string);
335 else if (strcasecmp(name, "composer") == 0)
337 p = &(id3->composer);
339 else
341 len = parse_replaygain(name, value, id3, buf, buf_remaining);
342 p = NULL;
345 if (p)
347 len = strlen(value);
348 len = MIN(len, buf_remaining - 1);
350 if (len > 0)
352 strncpy(buf, value, len);
353 buf[len] = 0;
354 *p = buf;
355 len++;
357 else
359 len = 0;
363 return len;
366 /* Read the items in an APEV2 tag. Only looks for a tag at the end of a
367 * file. Returns true if a tag was found and fully read, false otherwise.
369 static bool read_ape_tags(int fd, struct mp3entry* id3)
371 struct apetag_header header;
373 if ((lseek(fd, -APETAG_HEADER_LENGTH, SEEK_END) < 0)
374 || (read(fd, &header, APETAG_HEADER_LENGTH) != APETAG_HEADER_LENGTH)
375 || (memcmp(header.id, "APETAGEX", sizeof(header.id))))
377 return false;
380 convert_endian(&header, APETAG_HEADER_FORMAT);
381 id3->genre = 0xff;
383 if ((header.version == 2000) && (header.item_count > 0)
384 && (header.length > APETAG_HEADER_LENGTH))
386 char *buf = id3->id3v2buf;
387 unsigned int buf_remaining = sizeof(id3->id3v2buf)
388 + sizeof(id3->id3v1buf);
389 unsigned int tag_remaining = header.length - APETAG_HEADER_LENGTH;
390 int i;
392 if (lseek(fd, -header.length, SEEK_END) < 0)
394 return false;
397 for (i = 0; i < header.item_count; i++)
399 struct apetag_item_header item;
400 char name[TAG_NAME_LENGTH];
401 char value[TAG_VALUE_LENGTH];
402 long r;
404 if (tag_remaining < sizeof(item))
406 break;
409 if (read(fd, &item, sizeof(item)) < (long) sizeof(item))
411 return false;
414 convert_endian(&item, APETAG_ITEM_HEADER_FORMAT);
415 tag_remaining -= sizeof(item);
416 r = read_string(fd, name, sizeof(name), 0, tag_remaining);
418 if (r == -1)
420 return false;
423 tag_remaining -= r + item.length;
425 if ((item.flags & APETAG_ITEM_TYPE_MASK) == 0)
427 long len;
429 if (read_string(fd, value, sizeof(value), -1, item.length)
430 != item.length)
432 return false;
435 len = parse_tag(name, value, id3, buf, buf_remaining,
436 TAGTYPE_APE);
437 buf += len;
438 buf_remaining -= len;
440 else
442 if (lseek(fd, item.length, SEEK_CUR) < 0)
444 return false;
450 return true;
453 /* Read the items in a Vorbis comment packet. Returns true the items were
454 * fully read, false otherwise.
456 static bool read_vorbis_tags(int fd, struct mp3entry *id3,
457 long tag_remaining)
459 char *buf = id3->id3v2buf;
460 long comment_count;
461 long len;
462 int buf_remaining = sizeof(id3->id3v2buf) + sizeof(id3->id3v1buf);
463 int i;
465 id3->genre = 255;
467 if (read(fd, &len, sizeof(len)) < (long) sizeof(len))
469 return false;
472 convert_endian(&len, "L");
474 if ((lseek(fd, len, SEEK_CUR) < 0)
475 || (read(fd, &comment_count, sizeof(comment_count))
476 < (long) sizeof(comment_count)))
478 return false;
481 convert_endian(&comment_count, "L");
482 tag_remaining -= len + sizeof(len) + sizeof(comment_count);
484 if (tag_remaining <= 0)
486 return true;
489 for (i = 0; i < comment_count; i++)
491 char name[TAG_NAME_LENGTH];
492 char value[TAG_VALUE_LENGTH];
493 long read_len;
495 if (tag_remaining < 4)
497 break;
500 if (read(fd, &len, sizeof(len)) < (long) sizeof(len))
502 return false;
505 convert_endian(&len, "L");
506 tag_remaining -= 4;
508 /* Quit if we've passed the end of the page */
509 if (tag_remaining < len)
511 break;
514 tag_remaining -= len;
515 read_len = read_string(fd, name, sizeof(name), '=', len);
517 if (read_len < 0)
519 return false;
522 len -= read_len;
524 if (read_string(fd, value, sizeof(value), -1, len) < 0)
526 return false;
529 len = parse_tag(name, value, id3, buf, buf_remaining,
530 TAGTYPE_VORBIS);
531 buf += len;
532 buf_remaining -= len;
535 /* Skip to the end of the block */
536 if (tag_remaining)
538 if (lseek(fd, tag_remaining, SEEK_CUR) < 0)
540 return false;
544 return true;
547 /* Skip an ID3v2 tag if it can be found. We assume the tag is located at the
548 * start of the file, which should be true in all cases where we need to skip it.
549 * Returns true if successfully skipped or not skipped, and false if
550 * something went wrong while skipping.
552 static bool skip_id3v2(int fd, struct mp3entry *id3)
554 char buf[4];
556 read(fd, buf, 4);
557 if (memcmp(buf, "ID3", 3) == 0)
559 /* We have found an ID3v2 tag at the start of the file - find its
560 length and then skip it. */
561 if ((id3->first_frame_offset = getid3v2len(fd)) == 0)
562 return false;
564 if ((lseek(fd, id3->first_frame_offset, SEEK_SET) < 0))
565 return false;
567 return true;
568 } else {
569 lseek(fd, 0, SEEK_SET);
570 id3->first_frame_offset = 0;
571 return true;
575 /* A simple parser to read vital metadata from an Ogg Vorbis file. Returns
576 * false if metadata needed by the Vorbis codec couldn't be read.
578 static bool get_vorbis_metadata(int fd, struct mp3entry* id3)
580 /* An Ogg File is split into pages, each starting with the string
581 * "OggS". Each page has a timestamp (in PCM samples) referred to as
582 * the "granule position".
584 * An Ogg Vorbis has the following structure:
585 * 1) Identification header (containing samplerate, numchannels, etc)
586 * 2) Comment header - containing the Vorbis Comments
587 * 3) Setup header - containing codec setup information
588 * 4) Many audio packets...
591 /* Use the path name of the id3 structure as a temporary buffer. */
592 unsigned char* buf = id3->path;
593 long comment_size;
594 long remaining = 0;
595 long last_serial = 0;
596 long serial, r;
597 int segments;
598 int i;
599 bool eof = false;
601 if ((lseek(fd, 0, SEEK_SET) < 0) || (read(fd, buf, 58) < 4))
603 return false;
606 if ((memcmp(buf, "OggS", 4) != 0) || (memcmp(&buf[29], "vorbis", 6) != 0))
608 return false;
611 /* We need to ensure the serial number from this page is the same as the
612 * one from the last page (since we only support a single bitstream).
614 serial = get_long(&buf[14]);
615 id3->frequency = get_long(&buf[40]);
616 id3->filesize = filesize(fd);
618 /* Comments are in second Ogg page */
619 if (lseek(fd, 58, SEEK_SET) < 0)
621 return false;
624 /* Minimum header length for Ogg pages is 27. */
625 if (read(fd, buf, 27) < 27)
627 return false;
630 if (memcmp(buf, "OggS", 4) !=0 )
632 return false;
635 segments = buf[26];
637 /* read in segment table */
638 if (read(fd, buf, segments) < segments)
640 return false;
643 /* The second packet in a vorbis stream is the comment packet. It *may*
644 * extend beyond the second page, but usually does not. Here we find the
645 * length of the comment packet (or the rest of the page if the comment
646 * packet extends to the third page).
648 for (i = 0; i < segments; i++)
650 remaining += buf[i];
652 /* The last segment of a packet is always < 255 bytes */
653 if (buf[i] < 255)
655 break;
659 /* Now read in packet header (type and id string) */
660 if (read(fd, buf, 7) < 7)
662 return false;
665 comment_size = remaining;
666 remaining -= 7;
668 /* The first byte of a packet is the packet type; comment packets are
669 * type 3.
671 if ((buf[0] != 3) || (memcmp(buf + 1, "vorbis", 6) !=0))
673 return false;
676 /* Failure to read the tags isn't fatal. */
677 read_vorbis_tags(fd, id3, remaining);
679 /* We now need to search for the last page in the file - identified by
680 * by ('O','g','g','S',0) and retrieve totalsamples.
683 /* A page is always < 64 kB */
684 if (lseek(fd, -(MIN(64 * 1024, id3->filesize)), SEEK_END) < 0)
686 return false;
689 remaining = 0;
691 while (!eof)
693 r = read(fd, &buf[remaining], MAX_PATH - remaining);
695 if (r <= 0)
697 eof = true;
699 else
701 remaining += r;
704 /* Inefficient (but simple) search */
705 i = 0;
707 while (i < (remaining - 3))
709 if ((buf[i] == 'O') && (memcmp(&buf[i], "OggS", 4) == 0))
711 if (i < (remaining - 17))
713 /* Note that this only reads the low 32 bits of a
714 * 64 bit value.
716 id3->samples = get_long(&buf[i + 6]);
717 last_serial = get_long(&buf[i + 14]);
719 /* If this page is very small the beginning of the next
720 * header could be in buffer. Jump near end of this header
721 * and continue */
722 i += 27;
724 else
726 break;
729 else
731 i++;
735 if (i < remaining)
737 /* Move the remaining bytes to start of buffer.
738 * Reuse var 'segments' as it is no longer needed */
739 segments = 0;
740 while (i < remaining)
742 buf[segments++] = buf[i++];
744 remaining = segments;
746 else
748 /* Discard the rest of the buffer */
749 remaining = 0;
753 /* This file has mutiple vorbis bitstreams (or is corrupt). */
754 /* FIXME we should display an error here. */
755 if (serial != last_serial)
757 logf("serialno mismatch");
758 logf("%ld", serial);
759 logf("%ld", last_serial);
760 return false;
763 id3->length = ((int64_t) id3->samples * 1000) / id3->frequency;
765 if (id3->length <= 0)
767 logf("ogg length invalid!");
768 return false;
771 id3->bitrate = (((int64_t) id3->filesize - comment_size) * 8) / id3->length;
772 id3->vbr = true;
774 return true;
777 static bool get_flac_metadata(int fd, struct mp3entry* id3)
779 /* A simple parser to read vital metadata from a FLAC file - length,
780 * frequency, bitrate etc. This code should either be moved to a
781 * seperate file, or discarded in favour of the libFLAC code.
782 * The FLAC stream specification can be found at
783 * http://flac.sourceforge.net/format.html#stream
786 /* Use the trackname part of the id3 structure as a temporary buffer */
787 unsigned char* buf = id3->path;
788 bool rc = false;
790 if (!skip_id3v2(fd, id3) || (read(fd, buf, 4) < 4))
792 return rc;
795 if (memcmp(buf, "fLaC", 4) != 0)
797 return rc;
800 while (true)
802 long i;
804 if (read(fd, buf, 4) < 0)
806 return rc;
809 /* The length of the block */
810 i = (buf[1] << 16) | (buf[2] << 8) | buf[3];
812 if ((buf[0] & 0x7f) == 0) /* 0 is the STREAMINFO block */
814 unsigned long totalsamples;
816 /* FIXME: Don't trust the value of i */
817 if (read(fd, buf, i) < 0)
819 return rc;
822 id3->vbr = true; /* All FLAC files are VBR */
823 id3->filesize = filesize(fd);
824 id3->frequency = (buf[10] << 12) | (buf[11] << 4)
825 | ((buf[12] & 0xf0) >> 4);
826 rc = true; /* Got vital metadata */
828 /* totalsamples is a 36-bit field, but we assume <= 32 bits are used */
829 totalsamples = (buf[14] << 24) | (buf[15] << 16)
830 | (buf[16] << 8) | buf[17];
832 /* Calculate track length (in ms) and estimate the bitrate (in kbit/s) */
833 id3->length = ((int64_t) totalsamples * 1000) / id3->frequency;
835 if (id3->length <= 0)
837 logf("flac length invalid!");
838 return false;
841 id3->bitrate = (id3->filesize * 8) / id3->length;
843 else if ((buf[0] & 0x7f) == 4) /* 4 is the VORBIS_COMMENT block */
845 /* The next i bytes of the file contain the VORBIS COMMENTS. */
846 if (!read_vorbis_tags(fd, id3, i))
848 return rc;
851 else
853 if (buf[0] & 0x80)
855 /* If we have reached the last metadata block, abort. */
856 break;
858 else
860 /* Skip to next metadata block */
861 if (lseek(fd, i, SEEK_CUR) < 0)
863 return rc;
869 return true;
872 static bool get_wave_metadata(int fd, struct mp3entry* id3)
874 /* Use the trackname part of the id3 structure as a temporary buffer */
875 unsigned char* buf = id3->path;
876 unsigned long totalsamples = 0;
877 unsigned long channels = 0;
878 unsigned long bitspersample = 0;
879 unsigned long numbytes = 0;
880 int read_bytes;
881 int i;
883 /* get RIFF chunk header */
884 if ((lseek(fd, 0, SEEK_SET) < 0)
885 || ((read_bytes = read(fd, buf, 12)) < 12))
887 return false;
890 if ((memcmp(buf, "RIFF",4) != 0)
891 || (memcmp(&buf[8], "WAVE", 4) !=0 ))
893 return false;
896 /* iterate over WAVE chunks until 'data' chunk */
897 while (true)
899 /* get chunk header */
900 if ((read_bytes = read(fd, buf, 8)) < 8)
901 return false;
903 /* chunkSize */
904 i = get_long(&buf[4]);
906 if (memcmp(buf, "fmt ", 4) == 0)
908 /* get rest of chunk */
909 if ((read_bytes = read(fd, buf, 16)) < 16)
910 return false;
912 i -= 16;
914 /* skipping wFormatTag */
915 /* wChannels */
916 channels = buf[2] | (buf[3] << 8);
917 /* dwSamplesPerSec */
918 id3->frequency = get_long(&buf[4]);
919 /* dwAvgBytesPerSec */
920 id3->bitrate = (get_long(&buf[8]) * 8) / 1000;
921 /* skipping wBlockAlign */
922 /* wBitsPerSample */
923 bitspersample = buf[14] | (buf[15] << 8);
925 else if (memcmp(buf, "data", 4) == 0)
927 numbytes = i;
928 break;
930 else if (memcmp(buf, "fact", 4) == 0)
932 /* dwSampleLength */
933 if (i >= 4)
935 /* get rest of chunk */
936 if ((read_bytes = read(fd, buf, 2)) < 2)
937 return false;
939 i -= 2;
940 totalsamples = get_long(buf);
944 /* seek to next chunk (even chunk sizes must be padded) */
945 if (i & 0x01)
946 i++;
948 if(lseek(fd, i, SEEK_CUR) < 0)
949 return false;
952 if ((numbytes == 0) || (channels == 0))
954 return false;
957 if (totalsamples == 0)
959 /* for PCM only */
960 totalsamples = numbytes
961 / ((((bitspersample - 1) / 8) + 1) * channels);
964 id3->vbr = false; /* All WAV files are CBR */
965 id3->filesize = filesize(fd);
967 /* Calculate track length (in ms) and estimate the bitrate (in kbit/s) */
968 id3->length = ((int64_t) totalsamples * 1000) / id3->frequency;
970 return true;
973 /* Read the tag data from an MP4 file, storing up to buffer_size bytes in
974 * buffer.
976 unsigned long read_mp4_tag(int fd, unsigned int size_left, char* buffer,
977 unsigned int buffer_left)
979 unsigned int bytes_read = 0;
981 if (buffer_left == 0)
983 lseek(fd, size_left, SEEK_CUR); /* Skip everything */
985 else
987 /* Skip the data tag header - maybe we should parse it properly? */
988 lseek(fd, 16, SEEK_CUR);
989 size_left -= 16;
991 if (size_left > buffer_left)
993 read(fd, buffer, buffer_left);
994 lseek(fd, size_left - buffer_left, SEEK_CUR);
995 bytes_read = buffer_left;
997 else
999 read(fd, buffer, size_left);
1000 bytes_read = size_left;
1004 return bytes_read;
1007 /* Read a string tag from an MP4 file */
1008 unsigned int read_mp4_tag_string(int fd, int size_left, char** buffer,
1009 unsigned int* buffer_left, char** dest)
1011 unsigned int bytes_read = read_mp4_tag(fd, size_left, *buffer,
1012 *buffer_left - 1);
1013 unsigned int length = 0;
1015 if (bytes_read)
1017 (*buffer)[bytes_read] = 0;
1018 *dest = *buffer;
1019 length = strlen(*buffer) + 1;
1020 *buffer_left -= length;
1021 *buffer += length;
1023 else
1025 *dest = NULL;
1028 return length;
1031 static unsigned int read_mp4_atom(int fd, unsigned int* size,
1032 unsigned int* type, unsigned int size_left)
1034 read_uint32be(fd, size);
1035 read_uint32be(fd, type);
1037 if (*size == 1)
1039 /* FAT32 doesn't support files this big, so something seems to
1040 * be wrong. (64-bit sizes should only be used when required.)
1042 errno = EFBIG;
1043 *type = 0;
1044 return 0;
1047 if (*size > 0)
1049 if (*size > size_left)
1051 size_left = 0;
1053 else
1055 size_left -= *size;
1058 *size -= 8;
1060 else
1062 *size = size_left;
1063 size_left = 0;
1066 return size_left;
1069 static bool read_mp4_tags(int fd, struct mp3entry* id3,
1070 unsigned int size_left)
1072 unsigned int size;
1073 unsigned int type;
1074 unsigned int buffer_left = sizeof(id3->id3v2buf) + sizeof(id3->id3v1buf);
1075 char* buffer = id3->id3v2buf;
1076 bool cwrt = false;
1080 size_left = read_mp4_atom(fd, &size, &type, size_left);
1082 /* DEBUGF("Tag atom: '%c%c%c%c' (%d bytes left)\n", type >> 24 & 0xff,
1083 type >> 16 & 0xff, type >> 8 & 0xff, type & 0xff, size); */
1085 switch (type)
1087 case MP4_cnam:
1088 read_mp4_tag_string(fd, size, &buffer, &buffer_left,
1089 &id3->title);
1090 break;
1092 case MP4_cART:
1093 read_mp4_tag_string(fd, size, &buffer, &buffer_left,
1094 &id3->artist);
1095 break;
1097 case MP4_calb:
1098 read_mp4_tag_string(fd, size, &buffer, &buffer_left,
1099 &id3->album);
1100 break;
1102 case MP4_cwrt:
1103 read_mp4_tag_string(fd, size, &buffer, &buffer_left,
1104 &id3->composer);
1105 cwrt = false;
1106 break;
1108 case MP4_gnre:
1110 unsigned short genre;
1112 read_mp4_tag(fd, size, (char*) &genre, sizeof(genre));
1113 id3->genre = betoh16(genre);
1115 break;
1117 case MP4_trkn:
1119 unsigned short n[2];
1121 read_mp4_tag(fd, size, (char*) &n, sizeof(n));
1122 id3->tracknum = betoh16(n[1]);
1124 break;
1126 case MP4_extra:
1128 char tag_name[TAG_NAME_LENGTH];
1129 unsigned int sub_size;
1131 /* "mean" atom */
1132 read_uint32be(fd, &sub_size);
1133 size -= sub_size;
1134 lseek(fd, sub_size - 4, SEEK_CUR);
1135 /* "name" atom */
1136 read_uint32be(fd, &sub_size);
1137 size -= sub_size;
1138 lseek(fd, 8, SEEK_CUR);
1139 sub_size -= 12;
1141 if (sub_size > sizeof(tag_name) - 1)
1143 read(fd, tag_name, sizeof(tag_name) - 1);
1144 lseek(fd, sub_size - sizeof(tag_name) - 1, SEEK_CUR);
1145 tag_name[sizeof(tag_name) - 1] = 0;
1147 else
1149 read(fd, tag_name, sub_size);
1150 tag_name[sub_size] = 0;
1153 if ((strcasecmp(tag_name, "composer") == 0) && !cwrt)
1155 read_mp4_tag_string(fd, size, &buffer, &buffer_left,
1156 &id3->composer);
1158 else
1160 char* any;
1161 unsigned int length = read_mp4_tag_string(fd, size,
1162 &buffer, &buffer_left, &any);
1164 if (length > 0)
1166 /* Re-use the read buffer as the dest buffer... */
1167 buffer -= length;
1168 buffer_left += length;
1170 parse_replaygain(tag_name, buffer, id3, buffer,
1171 buffer_left);
1175 break;
1177 default:
1178 lseek(fd, size, SEEK_CUR);
1179 break;
1182 while ((size_left > 0) && (errno == 0));
1184 return true;
1187 static bool read_mp4_container(int fd, struct mp3entry* id3,
1188 unsigned int size_left)
1190 unsigned int size;
1191 unsigned int type;
1192 unsigned int handler = 0;
1193 bool rc = true;
1197 size_left = read_mp4_atom(fd, &size, &type, size_left);
1199 /* DEBUGF("Atom: '%c%c%c%c' (0x%08x, %d bytes left)\n",
1200 (type >> 24) & 0xff, (type >> 16) & 0xff, (type >> 8) & 0xff,
1201 type & 0xff, type, size); */
1203 switch (type)
1205 case MP4_ftyp:
1207 unsigned int id;
1209 read_uint32be(fd, &id);
1210 size -= 4;
1212 if ((id != MP4_M4A) && (id != MP4_mp42) && (id != MP4_qt)
1213 && (id != MP4_3gp6))
1215 DEBUGF("Unknown MP4 file type: '%c%c%c%c'\n",
1216 id >> 24 & 0xff, id >> 16 & 0xff, id >> 8 & 0xff,
1217 id & 0xff);
1218 return false;
1221 break;
1223 case MP4_meta:
1224 lseek(fd, 4, SEEK_CUR); /* Skip version */
1225 size -= 4;
1226 /* Fall through */
1228 case MP4_moov:
1229 case MP4_udta:
1230 case MP4_mdia:
1231 case MP4_stbl:
1232 case MP4_trak:
1233 rc = read_mp4_container(fd, id3, size);
1234 size = 0;
1235 break;
1237 case MP4_ilst:
1238 if (handler == MP4_mdir)
1240 rc = read_mp4_tags(fd, id3, size);
1241 size = 0;
1243 break;
1245 case MP4_minf:
1246 if (handler == MP4_soun)
1248 rc = read_mp4_container(fd, id3, size);
1249 size = 0;
1251 break;
1253 case MP4_stsd:
1254 lseek(fd, 8, SEEK_CUR);
1255 size -= 8;
1256 rc = read_mp4_container(fd, id3, size);
1257 size = 0;
1258 break;
1260 case MP4_hdlr:
1261 lseek(fd, 8, SEEK_CUR);
1262 read_uint32be(fd, &handler);
1263 size -= 12;
1264 /* DEBUGF(" Handler '%c%c%c%c'\n", handler >> 24 & 0xff,
1265 handler >> 16 & 0xff, handler >> 8 & 0xff,handler & 0xff); */
1266 break;
1268 case MP4_stts:
1270 unsigned int entries;
1271 unsigned int i;
1273 lseek(fd, 4, SEEK_CUR);
1274 read_uint32be(fd, &entries);
1275 id3->samples = 0;
1277 for (i = 0; i < entries; i++)
1279 unsigned int n;
1280 unsigned int l;
1282 read_uint32be(fd, &n);
1283 read_uint32be(fd, &l);
1284 id3->samples += n * l;
1287 size = 0;
1289 break;
1291 case MP4_mp4a:
1292 case MP4_alac:
1294 unsigned int frequency;
1296 id3->codectype = (type == MP4_mp4a) ? AFMT_AAC : AFMT_ALAC;
1297 lseek(fd, 22, SEEK_CUR);
1298 read_uint32be(fd, &frequency);
1299 size -= 26;
1300 id3->frequency = frequency;
1301 /* There're some child atoms here, but we don't need them */
1303 break;
1305 case MP4_mdat:
1306 id3->filesize = size;
1307 break;
1309 default:
1310 break;
1313 lseek(fd, size, SEEK_CUR);
1315 while (rc && (size_left > 0) && (errno == 0) && (id3->filesize == 0));
1316 /* Break on non-zero filesize, since Rockbox currently doesn't support
1317 * metadata after the mdat atom (which sets the filesize field).
1320 return rc;
1323 static bool get_mp4_metadata(int fd, struct mp3entry* id3)
1325 id3->codectype = AFMT_UNKNOWN;
1326 id3->genre = 255;
1327 id3->filesize = 0;
1328 errno = 0;
1330 if (read_mp4_container(fd, id3, filesize(fd)) && (errno == 0)
1331 && (id3->samples > 0) && (id3->frequency > 0)
1332 && (id3->filesize > 0))
1334 if (id3->codectype == AFMT_UNKNOWN)
1336 logf("Not an ALAC or AAC file");
1337 return false;
1340 id3->length = ((int64_t) id3->samples * 1000) / id3->frequency;
1342 if (id3->length <= 0)
1344 logf("mp4 length invalid!");
1345 return false;
1348 id3->bitrate = ((int64_t) id3->filesize * 8) / id3->length;
1349 DEBUGF("MP4 bitrate %d, frequency %d Hz, length %d ms\n",
1350 id3->bitrate, id3->frequency, id3->length);
1352 else
1354 logf("MP4 metadata error");
1355 DEBUGF("MP4 metadata error. errno %d, length %d, frequency %d, filesize %d\n",
1356 errno, id3->length, id3->frequency, id3->filesize);
1357 return false;
1360 return true;
1363 static bool get_musepack_metadata(int fd, struct mp3entry *id3)
1365 const int32_t sfreqs_sv7[4] = { 44100, 48000, 37800, 32000 };
1366 uint32_t header[8];
1367 uint64_t samples = 0;
1368 int i;
1370 if (!skip_id3v2(fd, id3))
1371 return false;
1372 if (read(fd, header, 4*8) != 4*8) return false;
1373 /* Musepack files are little endian, might need swapping */
1374 for (i = 1; i < 8; i++)
1375 header[i] = letoh32(header[i]);
1376 if (!memcmp(header, "MP+", 3)) { /* Compare to sig "MP+" */
1377 unsigned int streamversion;
1379 header[0] = letoh32(header[0]);
1380 streamversion = (header[0] >> 24) & 15;
1381 if (streamversion >= 8) {
1382 return false; /* SV8 or higher don't exist yet, so no support */
1383 } else if (streamversion == 7) {
1384 unsigned int gapless = (header[5] >> 31) & 0x0001;
1385 unsigned int last_frame_samples = (header[5] >> 20) & 0x07ff;
1386 int track_gain, album_gain;
1387 unsigned int bufused;
1389 id3->frequency = sfreqs_sv7[(header[2] >> 16) & 0x0003];
1390 samples = (uint64_t)header[1]*1152; /* 1152 is mpc frame size */
1391 if (gapless)
1392 samples -= 1152 - last_frame_samples;
1393 else
1394 samples -= 481; /* Musepack subband synth filter delay */
1396 /* Extract ReplayGain data from header */
1397 track_gain = (int16_t)((header[3] >> 16) & 0xffff);
1398 id3->track_gain = get_replaygain_int(track_gain);
1399 id3->track_peak = ((uint16_t)(header[3] & 0xffff)) << 9;
1401 album_gain = (int16_t)((header[4] >> 16) & 0xffff);
1402 id3->album_gain = get_replaygain_int(album_gain);
1403 id3->album_peak = ((uint16_t)(header[4] & 0xffff)) << 9;
1405 /* Write replaygain values to strings for use in id3 screen. We use
1406 the XING header as buffer space since Musepack files shouldn't
1407 need to use it in any other way */
1408 id3->track_gain_string = id3->toc;
1409 bufused = snprintf(id3->track_gain_string, 45,
1410 "%d.%d dB", track_gain/100, abs(track_gain)%100);
1411 id3->album_gain_string = id3->toc + bufused + 1;
1412 bufused = snprintf(id3->album_gain_string, 45,
1413 "%d.%d dB", album_gain/100, abs(album_gain)%100);
1415 } else {
1416 header[0] = letoh32(header[0]);
1417 unsigned int streamversion = (header[0] >> 11) & 0x03FF;
1418 if (streamversion != 4 && streamversion != 5 && streamversion != 6)
1419 return false;
1420 id3->frequency = 44100;
1421 id3->track_gain = 0;
1422 id3->track_peak = 0;
1423 id3->album_gain = 0;
1424 id3->album_peak = 0;
1426 if (streamversion >= 5)
1427 samples = (uint64_t)header[1]*1152; // 32 bit
1428 else
1429 samples = (uint64_t)(header[1] >> 16)*1152; // 16 bit
1431 samples -= 576;
1432 if (streamversion < 6)
1433 samples -= 1152;
1436 id3->vbr = true;
1437 /* Estimate bitrate, we should probably subtract the various header sizes
1438 here for super-accurate results */
1439 id3->length = ((int64_t) samples * 1000) / id3->frequency;
1441 if (id3->length <= 0)
1443 logf("mpc length invalid!");
1444 return false;
1447 id3->filesize = filesize(fd);
1448 id3->bitrate = id3->filesize * 8 / id3->length;
1449 return true;
1452 static bool get_sid_metadata(int fd, struct mp3entry* id3)
1454 /* Use the trackname part of the id3 structure as a temporary buffer */
1455 unsigned char* buf = id3->path;
1456 int read_bytes;
1457 char *p;
1460 if ((lseek(fd, 0, SEEK_SET) < 0)
1461 || ((read_bytes = read(fd, buf, sizeof(id3->path))) < 44))
1463 return false;
1466 if ((memcmp(buf, "PSID",4) != 0))
1468 return false;
1471 p = id3->id3v2buf;
1473 /* Copy Title */
1474 strcpy(p, &buf[0x16]);
1475 id3->title = p;
1476 p += strlen(p)+1;
1478 /* Copy Artist */
1479 strcpy(p, &buf[0x36]);
1480 id3->artist = p;
1481 p += strlen(p)+1;
1483 id3->bitrate = 706;
1484 id3->frequency = 44100;
1485 /* New idea as posted by Marco Alanen (ravon):
1486 * Set the songlength in seconds to the number of subsongs
1487 * so every second represents a subsong.
1488 * Users can then skip the current subsong by seeking */
1489 id3->length = (buf[0xf]-1)*1000;
1490 id3->vbr = false;
1491 id3->filesize = filesize(fd);
1493 return true;
1496 static bool get_adx_metadata(int fd, struct mp3entry* id3)
1498 /* Use the trackname part of the id3 structure as a temporary buffer */
1499 unsigned char * buf = id3->path;
1500 int chanstart, channels, read_bytes;
1501 int looping = 0, start_adr = 0, end_adr = 0;
1503 /* try to get the basic header */
1504 if ((lseek(fd, 0, SEEK_SET) < 0)
1505 || ((read_bytes = read(fd, buf, 0x38)) < 0x38))
1507 DEBUGF("lseek or read failed\n");
1508 return false;
1511 /* ADX starts with 0x80 */
1512 if (buf[0] != 0x80) {
1513 DEBUGF("get_adx_metadata: wrong first byte %c\n",buf[0]);
1514 return false;
1517 /* check for a reasonable offset */
1518 chanstart = ((buf[2] << 8) | buf[3]) + 4;
1519 if (chanstart > 4096) {
1520 DEBUGF("get_adx_metadata: bad chanstart %i\n", chanstart);
1521 return false;
1524 /* check for a workable number of channels */
1525 channels = buf[7];
1526 if (channels != 1 && channels != 2) {
1527 DEBUGF("get_adx_metadata: bad channel count %i\n",channels);
1528 return false;
1531 id3->frequency = (buf[8] << 24) | (buf[9] << 16) | (buf[10] << 8) | buf[11];
1532 /* 32 samples per 18 bytes */
1533 id3->bitrate = id3->frequency * channels * 18 * 8 / 32 / 1000;
1534 id3->length = ((unsigned long)
1535 (buf[12] << 24) | (buf[13] << 16) | (buf[14] << 8) | buf[15]) /
1536 id3->frequency * 1000;
1537 id3->vbr = false;
1538 id3->filesize = filesize(fd);
1540 /* get loop info */
1541 if (!memcmp(buf+0x10,"\x01\xF4\x03\x00",4)) {
1542 /* Soul Calibur 2 style (type 03) */
1543 DEBUGF("get_adx_metadata: type 03 found\n");
1544 /* check if header is too small for loop data */
1545 if (chanstart-6 < 0x2c) looping=0;
1546 else {
1547 looping = (buf[0x18]) ||
1548 (buf[0x19]) ||
1549 (buf[0x1a]) ||
1550 (buf[0x1b]);
1551 end_adr = (buf[0x28]<<24) |
1552 (buf[0x29]<<16) |
1553 (buf[0x2a]<<8) |
1554 (buf[0x2b]);
1556 start_adr = (
1557 (buf[0x1c]<<24) |
1558 (buf[0x1d]<<16) |
1559 (buf[0x1e]<<8) |
1560 (buf[0x1f])
1561 )/32*channels*18+chanstart;
1563 } else if (!memcmp(buf+0x10,"\x01\xF4\x04\x00",4)) {
1564 /* Standard (type 04) */
1565 DEBUGF("get_adx_metadata: type 04 found\n");
1566 /* check if header is too small for loop data */
1567 if (chanstart-6 < 0x38) looping=0;
1568 else {
1569 looping = (buf[0x24]) ||
1570 (buf[0x25]) ||
1571 (buf[0x26]) ||
1572 (buf[0x27]);
1573 end_adr = (buf[0x34]<<24) |
1574 (buf[0x35]<<16) |
1575 (buf[0x36]<<8) |
1576 buf[0x37];
1577 start_adr = (
1578 (buf[0x28]<<24) |
1579 (buf[0x29]<<16) |
1580 (buf[0x2a]<<8) |
1581 (buf[0x2b])
1582 )/32*channels*18+chanstart;
1584 } else {
1585 DEBUGF("get_adx_metadata: error, couldn't determine ADX type\n");
1586 return false;
1589 if (looping) {
1590 /* 2 loops, 10 second fade */
1591 id3->length = (start_adr-chanstart + 2*(end_adr-start_adr))
1592 *8 / id3->bitrate + 10000;
1595 /* try to get the channel header */
1596 if ((lseek(fd, chanstart-6, SEEK_SET) < 0)
1597 || ((read_bytes = read(fd, buf, 6)) < 6))
1599 return false;
1602 /* check channel header */
1603 if (memcmp(buf, "(c)CRI", 6) != 0) return false;
1605 return true;
1608 static bool get_aiff_metadata(int fd, struct mp3entry* id3)
1610 /* Use the trackname part of the id3 structure as a temporary buffer */
1611 unsigned char* buf = id3->path;
1612 unsigned long numChannels = 0;
1613 unsigned long numSampleFrames = 0;
1614 unsigned long sampleSize = 0;
1615 unsigned long sampleRate = 0;
1616 unsigned long numbytes = 0;
1617 int read_bytes;
1618 int i;
1620 if ((lseek(fd, 0, SEEK_SET) < 0)
1621 || ((read_bytes = read(fd, buf, sizeof(id3->path))) < 44))
1623 return false;
1626 if ((memcmp(buf, "FORM",4) != 0)
1627 || (memcmp(&buf[8], "AIFF", 4) !=0 ))
1629 return false;
1632 buf += 12;
1633 read_bytes -= 12;
1635 while ((numbytes == 0) && (read_bytes >= 8))
1637 /* chunkSize */
1638 i = ((buf[4]<<24)|(buf[5]<<16)|(buf[6]<<8)|buf[7]);
1640 if (memcmp(buf, "COMM", 4) == 0)
1642 /* numChannels */
1643 numChannels = ((buf[8]<<8)|buf[9]);
1644 /* numSampleFrames */
1645 numSampleFrames =((buf[10]<<24)|(buf[11]<<16)|(buf[12]<<8)|buf[13]);
1646 /* sampleSize */
1647 sampleSize = ((buf[14]<<8)|buf[15]);
1648 /* sampleRate */
1649 sampleRate = ((buf[18]<<24)|(buf[19]<<16)|(buf[20]<<8)|buf[21]);
1650 sampleRate = sampleRate >> (16+14-buf[17]);
1651 /* save format infos */
1652 id3->bitrate = (sampleSize * numChannels * sampleRate) / 1000;
1653 id3->frequency = sampleRate;
1654 id3->length = ((int64_t) numSampleFrames * 1000) / id3->frequency;
1656 id3->vbr = false; /* AIFF files are CBR */
1657 id3->filesize = filesize(fd);
1659 else if (memcmp(buf, "SSND", 4) == 0)
1661 numbytes = i - 8;
1664 if (i & 0x01)
1666 i++; /* odd chunk sizes must be padded */
1668 buf += i + 8;
1669 read_bytes -= i + 8;
1672 if ((numbytes == 0) || (numChannels == 0))
1674 return false;
1676 return true;
1678 #endif /* CONFIG_CODEC == SWCODEC */
1681 /* Simple file type probing by looking at the filename extension. */
1682 unsigned int probe_file_format(const char *filename)
1684 char *suffix;
1685 unsigned int i;
1687 suffix = strrchr(filename, '.');
1689 if (suffix == NULL)
1691 return AFMT_UNKNOWN;
1694 suffix += 1;
1696 for (i = 0; i < sizeof(formats) / sizeof(formats[0]); i++)
1698 if (strcasecmp(suffix, formats[i].extension) == 0)
1700 return formats[i].format;
1704 return AFMT_UNKNOWN;
1707 /* Get metadata for track - return false if parsing showed problems with the
1708 * file that would prevent playback.
1710 bool get_metadata(struct track_info* track, int fd, const char* trackname,
1711 bool v1first)
1713 #if CONFIG_CODEC == SWCODEC
1714 unsigned char* buf;
1715 unsigned long totalsamples;
1716 int i;
1717 #endif
1719 /* Take our best guess at the codec type based on file extension */
1720 track->id3.codectype = probe_file_format(trackname);
1722 /* Load codec specific track tag information and confirm the codec type. */
1723 switch (track->id3.codectype)
1725 case AFMT_MPA_L1:
1726 case AFMT_MPA_L2:
1727 case AFMT_MPA_L3:
1728 if (!get_mp3_metadata(fd, &track->id3, trackname, v1first))
1730 return false;
1733 break;
1735 #if CONFIG_CODEC == SWCODEC
1736 case AFMT_FLAC:
1737 if (!get_flac_metadata(fd, &(track->id3)))
1739 return false;
1742 break;
1744 case AFMT_MPC:
1745 if (!get_musepack_metadata(fd, &(track->id3)))
1746 return false;
1747 read_ape_tags(fd, &(track->id3));
1748 break;
1750 case AFMT_OGG_VORBIS:
1751 if (!get_vorbis_metadata(fd, &(track->id3)))
1753 return false;
1756 break;
1758 case AFMT_PCM_WAV:
1759 if (!get_wave_metadata(fd, &(track->id3)))
1761 return false;
1764 break;
1766 case AFMT_WAVPACK:
1767 /* A simple parser to read basic information from a WavPack file. This
1768 * now works with self-extrating WavPack files and also will fail on
1769 * WavPack files containing floating-point audio data (although these
1770 * should be possible to play in theory).
1773 /* Use the trackname part of the id3 structure as a temporary buffer */
1774 buf = track->id3.path;
1776 for (i = 0; i < 256; ++i) {
1778 /* at every 256 bytes into file, try to read a WavPack header */
1780 if ((lseek(fd, i * 256, SEEK_SET) < 0) || (read(fd, buf, 32) < 32))
1782 return false;
1785 /* if valid WavPack 4 header version & not floating data, break */
1787 if (memcmp (buf, "wvpk", 4) == 0 && buf [9] == 4 &&
1788 (buf [8] >= 2 && buf [8] <= 0x10) && !(buf [24] & 0x80))
1790 break;
1794 if (i == 256) {
1795 logf ("%s is not a WavPack file\n", trackname);
1796 return false;
1799 track->id3.vbr = true; /* All WavPack files are VBR */
1800 track->id3.filesize = filesize (fd);
1802 if ((buf [20] | buf [21] | buf [22] | buf [23]) &&
1803 (buf [12] & buf [13] & buf [14] & buf [15]) != 0xff)
1805 int srindx = ((buf [26] >> 7) & 1) + ((buf [27] << 1) & 14);
1807 if (srindx == 15)
1809 track->id3.frequency = 44100;
1811 else
1813 track->id3.frequency = wavpack_sample_rates[srindx];
1816 totalsamples = get_long(&buf[12]);
1817 track->id3.length = totalsamples / (track->id3.frequency / 100) * 10;
1818 track->id3.bitrate = filesize (fd) / (track->id3.length / 8);
1821 read_ape_tags(fd, &track->id3); /* use any apetag info we find */
1822 break;
1824 case AFMT_A52:
1825 /* Use the trackname part of the id3 structure as a temporary buffer */
1826 buf = track->id3.path;
1828 if ((lseek(fd, 0, SEEK_SET) < 0) || (read(fd, buf, 5) < 5))
1830 return false;
1833 if ((buf[0] != 0x0b) || (buf[1] != 0x77))
1835 logf("%s is not an A52/AC3 file\n",trackname);
1836 return false;
1839 i = buf[4] & 0x3e;
1841 if (i > 36)
1843 logf("A52: Invalid frmsizecod: %d\n",i);
1844 return false;
1847 track->id3.bitrate = a52_bitrates[i >> 1];
1848 track->id3.vbr = false;
1849 track->id3.filesize = filesize(fd);
1851 switch (buf[4] & 0xc0)
1853 case 0x00:
1854 track->id3.frequency = 48000;
1855 track->id3.bytesperframe=track->id3.bitrate * 2 * 2;
1856 break;
1858 case 0x40:
1859 track->id3.frequency = 44100;
1860 track->id3.bytesperframe = a52_441framesizes[i];
1861 break;
1863 case 0x80:
1864 track->id3.frequency = 32000;
1865 track->id3.bytesperframe = track->id3.bitrate * 3 * 2;
1866 break;
1868 default:
1869 logf("A52: Invalid samplerate code: 0x%02x\n", buf[4] & 0xc0);
1870 return false;
1871 break;
1874 /* One A52 frame contains 6 blocks, each containing 256 samples */
1875 totalsamples = track->id3.filesize / track->id3.bytesperframe * 6 * 256;
1876 track->id3.length = totalsamples / track->id3.frequency * 1000;
1877 break;
1879 case AFMT_ALAC:
1880 case AFMT_AAC:
1881 if (!get_mp4_metadata(fd, &(track->id3)))
1883 return false;
1886 break;
1888 case AFMT_SHN:
1889 track->id3.vbr = true;
1890 track->id3.filesize = filesize(fd);
1891 if (!skip_id3v2(fd, &(track->id3)))
1893 return false;
1895 /* TODO: read the id3v2 header if it exists */
1896 break;
1898 case AFMT_SID:
1899 if (!get_sid_metadata(fd, &(track->id3)))
1901 return false;
1903 break;
1905 case AFMT_ADX:
1906 if (!get_adx_metadata(fd, &(track->id3)))
1908 DEBUGF("get_adx_metadata error\n");
1909 return false;
1912 break;
1914 case AFMT_AIFF:
1915 if (!get_aiff_metadata(fd, &(track->id3)))
1917 return false;
1920 break;
1922 #endif /* CONFIG_CODEC == SWCODEC */
1924 default:
1925 /* If we don't know how to read the metadata, assume we can't play
1926 the file */
1927 return false;
1928 break;
1931 /* We have successfully read the metadata from the file */
1933 lseek(fd, 0, SEEK_SET);
1934 strncpy(track->id3.path, trackname, sizeof(track->id3.path));
1935 track->taginfo_ready = true;
1937 return true;