1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2005 Magnus Holmgren
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 ****************************************************************************/
30 #include "metadata_common.h"
31 #include "metadata_parsers.h"
34 #include "replaygain.h"
36 #define MP4_3gp6 FOURCC('3', 'g', 'p', '6')
37 #define MP4_aART FOURCC('a', 'A', 'R', 'T')
38 #define MP4_alac FOURCC('a', 'l', 'a', 'c')
39 #define MP4_calb FOURCC(0xa9, 'a', 'l', 'b')
40 #define MP4_cART FOURCC(0xa9, 'A', 'R', 'T')
41 #define MP4_cgrp FOURCC(0xa9, 'g', 'r', 'p')
42 #define MP4_cgen FOURCC(0xa9, 'g', 'e', 'n')
43 #define MP4_chpl FOURCC('c', 'h', 'p', 'l')
44 #define MP4_cnam FOURCC(0xa9, 'n', 'a', 'm')
45 #define MP4_cwrt FOURCC(0xa9, 'w', 'r', 't')
46 #define MP4_ccmt FOURCC(0xa9, 'c', 'm', 't')
47 #define MP4_cday FOURCC(0xa9, 'd', 'a', 'y')
48 #define MP4_disk FOURCC('d', 'i', 's', 'k')
49 #define MP4_esds FOURCC('e', 's', 'd', 's')
50 #define MP4_ftyp FOURCC('f', 't', 'y', 'p')
51 #define MP4_gnre FOURCC('g', 'n', 'r', 'e')
52 #define MP4_hdlr FOURCC('h', 'd', 'l', 'r')
53 #define MP4_ilst FOURCC('i', 'l', 's', 't')
54 #define MP4_isom FOURCC('i', 's', 'o', 'm')
55 #define MP4_M4A FOURCC('M', '4', 'A', ' ')
56 #define MP4_m4a FOURCC('m', '4', 'a', ' ') /*technically its "M4A "*/
57 #define MP4_M4B FOURCC('M', '4', 'B', ' ') /*but files exist with lower case*/
58 #define MP4_mdat FOURCC('m', 'd', 'a', 't')
59 #define MP4_mdia FOURCC('m', 'd', 'i', 'a')
60 #define MP4_mdir FOURCC('m', 'd', 'i', 'r')
61 #define MP4_meta FOURCC('m', 'e', 't', 'a')
62 #define MP4_minf FOURCC('m', 'i', 'n', 'f')
63 #define MP4_moov FOURCC('m', 'o', 'o', 'v')
64 #define MP4_mp4a FOURCC('m', 'p', '4', 'a')
65 #define MP4_mp42 FOURCC('m', 'p', '4', '2')
66 #define MP4_qt FOURCC('q', 't', ' ', ' ')
67 #define MP4_soun FOURCC('s', 'o', 'u', 'n')
68 #define MP4_stbl FOURCC('s', 't', 'b', 'l')
69 #define MP4_stsd FOURCC('s', 't', 's', 'd')
70 #define MP4_stts FOURCC('s', 't', 't', 's')
71 #define MP4_trak FOURCC('t', 'r', 'a', 'k')
72 #define MP4_trkn FOURCC('t', 'r', 'k', 'n')
73 #define MP4_udta FOURCC('u', 'd', 't', 'a')
74 #define MP4_extra FOURCC('-', '-', '-', '-')
76 /* Read the tag data from an MP4 file, storing up to buffer_size bytes in
79 static unsigned long read_mp4_tag(int fd
, unsigned int size_left
, char* buffer
,
80 unsigned int buffer_left
)
82 unsigned int bytes_read
= 0;
86 lseek(fd
, size_left
, SEEK_CUR
); /* Skip everything */
90 /* Skip the data tag header - maybe we should parse it properly? */
91 lseek(fd
, 16, SEEK_CUR
);
94 if (size_left
> buffer_left
)
96 read(fd
, buffer
, buffer_left
);
97 lseek(fd
, size_left
- buffer_left
, SEEK_CUR
);
98 bytes_read
= buffer_left
;
102 read(fd
, buffer
, size_left
);
103 bytes_read
= size_left
;
110 /* Read a string tag from an MP4 file */
111 static unsigned int read_mp4_tag_string(int fd
, int size_left
, char** buffer
,
112 unsigned int* buffer_left
, char** dest
)
114 unsigned int bytes_read
= read_mp4_tag(fd
, size_left
, *buffer
,
115 *buffer_left
> 0 ? *buffer_left
- 1 : 0);
116 unsigned int length
= 0;
120 /* Do not overwrite already available metadata. Especially when reading
121 * tags with e.g. multiple genres / artists. This way only the first
122 * of multiple entries is used, all following are dropped. */
125 (*buffer
)[bytes_read
] = 0; /* zero-terminate for correct strlen().*/
126 length
= strlen(*buffer
) + 1;
127 length
= MIN(length
, ID3V2_MAX_ITEM_SIZE
); /* Limit item size. */
130 (*buffer
)[length
-1] = 0; /* zero-terminate buffer. */
131 *buffer_left
-= length
;
143 static unsigned int read_mp4_atom(int fd
, uint32_t* size
,
144 uint32_t* type
, uint32_t size_left
)
146 read_uint32be(fd
, size
);
147 read_uint32be(fd
, type
);
151 /* FAT32 doesn't support files this big, so something seems to
152 * be wrong. (64-bit sizes should only be used when required.)
161 if (*size
> size_left
)
181 static unsigned int read_mp4_length(int fd
, uint32_t* size
)
183 unsigned int length
= 0;
192 length
= (length
<< 7) | (c
& 0x7F);
194 while ((c
& 0x80) && (bytes
< 4) && (*size
> 0));
199 static bool read_mp4_esds(int fd
, struct mp3entry
* id3
, uint32_t* size
)
201 unsigned char buf
[8];
204 lseek(fd
, 4, SEEK_CUR
); /* Version and flags. */
205 read(fd
, buf
, 1); /* Verify ES_DescrTag. */
211 if (read_mp4_length(fd
, size
) < 20)
216 lseek(fd
, 3, SEEK_CUR
);
221 lseek(fd
, 2, SEEK_CUR
);
225 read(fd
, buf
, 1); /* Verify DecoderConfigDescrTab. */
233 if (read_mp4_length(fd
, size
) < 13)
238 lseek(fd
, 13, SEEK_CUR
); /* Skip audio type, bit rates, etc. */
242 if (*buf
!= 5) /* Verify DecSpecificInfoTag. */
248 static const int sample_rates
[] =
250 96000, 88200, 64000, 48000, 44100, 32000,
251 24000, 22050, 16000, 12000, 11025, 8000
258 /* Read the (leading part of the) decoder config. */
259 length
= read_mp4_length(fd
, size
);
260 length
= MIN(length
, *size
);
261 length
= MIN(length
, sizeof(buf
));
262 memset(buf
, 0, sizeof(buf
));
263 read(fd
, buf
, length
);
266 /* Maybe time to write a simple read_bits function... */
268 /* Decoder config format:
269 * Object type - 5 bits
270 * Frequency index - 4 bits
271 * Channel configuration - 4 bits
273 bits
= get_long_be(buf
);
274 type
= bits
>> 27; /* Object type - 5 bits */
275 index
= (bits
>> 23) & 0xf; /* Frequency index - 4 bits */
277 if (index
< (sizeof(sample_rates
) / sizeof(*sample_rates
)))
279 id3
->frequency
= sample_rates
[index
];
284 unsigned int old_index
= index
;
287 index
= (bits
>> 15) & 0xf; /* Frequency index - 4 bits */
291 /* 17 bits read so far... */
292 bits
= get_long_be(&buf
[2]);
293 id3
->frequency
= (bits
>> 7) & 0x00ffffff;
295 else if (index
< (sizeof(sample_rates
) / sizeof(*sample_rates
)))
297 id3
->frequency
= sample_rates
[index
];
300 if (old_index
== index
)
302 /* Downsampled SBR */
306 /* Skip 13 bits from above, plus 3 bits, then read 11 bits */
307 else if ((length
>= 4) && (((bits
>> 5) & 0x7ff) == 0x2b7))
309 /* We found an extensionAudioObjectType */
310 type
= bits
& 0x1f; /* Object type - 5 bits*/
311 bits
= get_long_be(&buf
[4]);
319 unsigned int old_index
= index
;
321 /* 1 bit read so far */
322 index
= (bits
>> 27) & 0xf; /* Frequency index - 4 bits */
326 /* 5 bits read so far */
327 id3
->frequency
= (bits
>> 3) & 0x00ffffff;
329 else if (index
< (sizeof(sample_rates
) / sizeof(*sample_rates
)))
331 id3
->frequency
= sample_rates
[index
];
334 if (old_index
== index
)
336 /* Downsampled SBR */
343 if (!sbr
&& (id3
->frequency
<= 24000) && (length
<= 2))
345 /* Double the frequency for low-frequency files without a "long"
346 * DecSpecificConfig header. The file may or may not contain SBR,
347 * but here we guess it does if the header is short. This can
348 * fail on some files, but it's the best we can do, short of
349 * decoding (parts of) the file.
359 static bool read_mp4_tags(int fd
, struct mp3entry
* id3
,
364 unsigned int buffer_left
= sizeof(id3
->id3v2buf
) + sizeof(id3
->id3v1buf
);
365 char* buffer
= id3
->id3v2buf
;
370 size_left
= read_mp4_atom(fd
, &size
, &type
, size_left
);
372 /* DEBUGF("Tag atom: '%c%c%c%c' (%d bytes left)\n", type >> 24 & 0xff,
373 type >> 16 & 0xff, type >> 8 & 0xff, type & 0xff, size); */
378 read_mp4_tag_string(fd
, size
, &buffer
, &buffer_left
,
383 read_mp4_tag_string(fd
, size
, &buffer
, &buffer_left
,
388 read_mp4_tag_string(fd
, size
, &buffer
, &buffer_left
,
393 read_mp4_tag_string(fd
, size
, &buffer
, &buffer_left
,
398 read_mp4_tag_string(fd
, size
, &buffer
, &buffer_left
,
403 read_mp4_tag_string(fd
, size
, &buffer
, &buffer_left
,
409 read_mp4_tag_string(fd
, size
, &buffer
, &buffer_left
,
414 read_mp4_tag_string(fd
, size
, &buffer
, &buffer_left
,
417 /* Try to parse it as a year, for the benefit of the database.
421 id3
->year
= atoi(id3
->year_string
);
422 if (id3
->year
< 1900)
434 unsigned short genre
;
436 read_mp4_tag(fd
, size
, (char*) &genre
, sizeof(genre
));
437 id3
->genre_string
= id3_get_num_genre(betoh16(genre
) - 1);
442 read_mp4_tag_string(fd
, size
, &buffer
, &buffer_left
,
450 read_mp4_tag(fd
, size
, (char*) &n
, sizeof(n
));
451 id3
->discnum
= betoh16(n
[1]);
459 read_mp4_tag(fd
, size
, (char*) &n
, sizeof(n
));
460 id3
->tracknum
= betoh16(n
[1]);
466 char tag_name
[TAG_NAME_LENGTH
];
470 read_uint32be(fd
, &sub_size
);
472 lseek(fd
, sub_size
- 4, SEEK_CUR
);
474 read_uint32be(fd
, &sub_size
);
476 lseek(fd
, 8, SEEK_CUR
);
479 if (sub_size
> sizeof(tag_name
) - 1)
481 read(fd
, tag_name
, sizeof(tag_name
) - 1);
482 lseek(fd
, sub_size
- (sizeof(tag_name
) - 1), SEEK_CUR
);
483 tag_name
[sizeof(tag_name
) - 1] = 0;
487 read(fd
, tag_name
, sub_size
);
488 tag_name
[sub_size
] = 0;
491 if ((strcasecmp(tag_name
, "composer") == 0) && !cwrt
)
493 read_mp4_tag_string(fd
, size
, &buffer
, &buffer_left
,
496 else if (strcasecmp(tag_name
, "iTunSMPB") == 0)
498 char value
[TAG_VALUE_LENGTH
];
499 char* value_p
= value
;
501 unsigned int length
= sizeof(value
);
503 read_mp4_tag_string(fd
, size
, &value_p
, &length
, &any
);
504 id3
->lead_trim
= get_itunes_int32(value
, 1);
505 id3
->tail_trim
= get_itunes_int32(value
, 2);
506 DEBUGF("AAC: lead_trim %d, tail_trim %d\n",
507 id3
->lead_trim
, id3
->tail_trim
);
509 else if (strcasecmp(tag_name
, "musicbrainz track id") == 0)
511 read_mp4_tag_string(fd
, size
, &buffer
, &buffer_left
,
514 else if ((strcasecmp(tag_name
, "album artist") == 0))
516 read_mp4_tag_string(fd
, size
, &buffer
, &buffer_left
,
522 unsigned int length
= read_mp4_tag_string(fd
, size
,
523 &buffer
, &buffer_left
, &any
);
527 /* Re-use the read buffer as the dest buffer... */
529 buffer_left
+= length
;
531 parse_replaygain(tag_name
, buffer
, id3
);
538 lseek(fd
, size
, SEEK_CUR
);
542 while ((size_left
> 0) && (errno
== 0));
547 static bool read_mp4_container(int fd
, struct mp3entry
* id3
,
552 uint32_t handler
= 0;
558 size_left
= read_mp4_atom(fd
, &size
, &type
, size_left
);
560 /* DEBUGF("Atom: '%c%c%c%c' (0x%08lx, %lu bytes left)\n",
561 (int) ((type >> 24) & 0xff), (int) ((type >> 16) & 0xff),
562 (int) ((type >> 8) & 0xff), (int) (type & 0xff),
571 read_uint32be(fd
, &id
);
574 if ((id
!= MP4_M4A
) && (id
!= MP4_M4B
) && (id
!= MP4_mp42
)
575 && (id
!= MP4_qt
) && (id
!= MP4_3gp6
) && (id
!= MP4_m4a
)
578 DEBUGF("Unknown MP4 file type: '%c%c%c%c'\n",
579 (int)(id
>> 24 & 0xff), (int)(id
>> 16 & 0xff),
580 (int)(id
>> 8 & 0xff), (int)(id
& 0xff));
587 lseek(fd
, 4, SEEK_CUR
); /* Skip version */
596 rc
= read_mp4_container(fd
, id3
, size
);
601 /* We need at least a size of 8 to read the next atom. */
602 if (handler
== MP4_mdir
&& size
>8)
604 rc
= read_mp4_tags(fd
, id3
, size
);
610 if (handler
== MP4_soun
)
612 rc
= read_mp4_container(fd
, id3
, size
);
618 lseek(fd
, 8, SEEK_CUR
);
620 rc
= read_mp4_container(fd
, id3
, size
);
625 lseek(fd
, 8, SEEK_CUR
);
626 read_uint32be(fd
, &handler
);
628 /* DEBUGF(" Handler '%c%c%c%c'\n", handler >> 24 & 0xff,
629 handler >> 16 & 0xff, handler >> 8 & 0xff,handler & 0xff); */
637 /* Reset to false. */
638 id3
->needs_upsampling_correction
= false;
640 lseek(fd
, 4, SEEK_CUR
);
641 read_uint32be(fd
, &entries
);
644 for (i
= 0; i
< entries
; i
++)
649 read_uint32be(fd
, &n
);
650 read_uint32be(fd
, &l
);
652 /* Some AAC file use HE profile. In this case the number
653 * of output samples is doubled to a maximum of 2048
654 * samples per frame. This means that files which already
655 * report a frame size of 2048 in their header will not
656 * need any further special handling. */
657 if (id3
->codectype
==AFMT_MP4_AAC_HE
&& l
<=1024)
659 id3
->samples
+= n
* l
* 2;
660 id3
->needs_upsampling_correction
= true;
664 id3
->samples
+= n
* l
;
677 /* Move to the next expected mp4 atom. */
678 lseek(fd
, 28, SEEK_CUR
);
679 read_mp4_atom(fd
, &subsize
, &subtype
, size
);
682 if (subtype
== MP4_esds
)
684 /* Read esds metadata and return if AAC-HE/SBR is used. */
685 if (read_mp4_esds(fd
, id3
, &size
))
686 id3
->codectype
= AFMT_MP4_AAC_HE
;
688 id3
->codectype
= AFMT_MP4_AAC
;
699 /* Move to the next expected mp4 atom. */
700 lseek(fd
, 28, SEEK_CUR
);
701 read_mp4_atom(fd
, &subsize
, &subtype
, size
);
704 /* We might need to parse for the alac metadata atom. */
705 while (!((subsize
==28) && (subtype
==MP4_alac
)) && (size
>0))
707 lseek(fd
, -7, SEEK_CUR
);
708 read_mp4_atom(fd
, &subsize
, &subtype
, size
);
710 errno
= 0; /* will most likely be set while parsing */
713 if (subtype
== MP4_alac
)
715 lseek(fd
, 24, SEEK_CUR
);
716 read_uint32be(fd
, &frequency
);
718 id3
->frequency
= frequency
;
719 id3
->codectype
= AFMT_MP4_ALAC
;
725 /* Some AAC files appear to contain additional empty mdat chunks.
729 id3
->filesize
= size
;
730 if(id3
->samples
> 0) {
731 /* We've already seen the moov chunk. */
738 /* ADDME: add support for real chapters. Right now it's only
739 * used for Nero's gapless hack */
743 lseek(fd
, 8, SEEK_CUR
);
744 read_uint8(fd
, &chapters
);
747 /* the first chapter will be used as the lead_trim */
749 read_uint64be(fd
, ×tamp
);
750 id3
->lead_trim
= (timestamp
* id3
->frequency
) / 10000000;
760 /* Skip final seek. */
763 lseek(fd
, size
, SEEK_CUR
);
765 } while (rc
&& (size_left
> 0) && (errno
== 0) && !done
);
770 bool get_mp4_metadata(int fd
, struct mp3entry
* id3
)
772 id3
->codectype
= AFMT_UNKNOWN
;
776 if (read_mp4_container(fd
, id3
, filesize(fd
)) && (errno
== 0)
777 && (id3
->samples
> 0) && (id3
->frequency
> 0)
778 && (id3
->filesize
> 0))
780 if (id3
->codectype
== AFMT_UNKNOWN
)
782 logf("Not an ALAC or AAC file");
786 id3
->length
= ((int64_t) id3
->samples
* 1000) / id3
->frequency
;
788 id3
->vbr
= true; /* ALAC is native VBR, AAC very unlikely is CBR. */
790 if (id3
->length
<= 0)
792 logf("mp4 length invalid!");
796 id3
->bitrate
= ((int64_t) id3
->filesize
* 8) / id3
->length
;
797 DEBUGF("MP4 bitrate %d, frequency %ld Hz, length %ld ms\n",
798 id3
->bitrate
, id3
->frequency
, id3
->length
);
802 logf("MP4 metadata error");
803 DEBUGF("MP4 metadata error. errno %d, samples %ld, frequency %ld, "
804 "filesize %ld\n", errno
, id3
->samples
, id3
->frequency
,