mpegplayer:
[kugel-rb.git] / apps / metadata / mp4.c
blobcd6eda428f2be622583a41f009d69069330d8750
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <ctype.h>
25 #include <inttypes.h>
27 #include "system.h"
28 #include "errno.h"
29 #include "metadata.h"
30 #include "metadata_common.h"
31 #include "metadata_parsers.h"
32 #include "logf.h"
33 #include "debug.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_M4B FOURCC('M', '4', 'B', ' ')
57 #define MP4_mdat FOURCC('m', 'd', 'a', 't')
58 #define MP4_mdia FOURCC('m', 'd', 'i', 'a')
59 #define MP4_mdir FOURCC('m', 'd', 'i', 'r')
60 #define MP4_meta FOURCC('m', 'e', 't', 'a')
61 #define MP4_minf FOURCC('m', 'i', 'n', 'f')
62 #define MP4_moov FOURCC('m', 'o', 'o', 'v')
63 #define MP4_mp4a FOURCC('m', 'p', '4', 'a')
64 #define MP4_mp42 FOURCC('m', 'p', '4', '2')
65 #define MP4_qt FOURCC('q', 't', ' ', ' ')
66 #define MP4_soun FOURCC('s', 'o', 'u', 'n')
67 #define MP4_stbl FOURCC('s', 't', 'b', 'l')
68 #define MP4_stsd FOURCC('s', 't', 's', 'd')
69 #define MP4_stts FOURCC('s', 't', 't', 's')
70 #define MP4_trak FOURCC('t', 'r', 'a', 'k')
71 #define MP4_trkn FOURCC('t', 'r', 'k', 'n')
72 #define MP4_udta FOURCC('u', 'd', 't', 'a')
73 #define MP4_extra FOURCC('-', '-', '-', '-')
75 /* Read the tag data from an MP4 file, storing up to buffer_size bytes in
76 * buffer.
78 static unsigned long read_mp4_tag(int fd, unsigned int size_left, char* buffer,
79 unsigned int buffer_left)
81 unsigned int bytes_read = 0;
83 if (buffer_left == 0)
85 lseek(fd, size_left, SEEK_CUR); /* Skip everything */
87 else
89 /* Skip the data tag header - maybe we should parse it properly? */
90 lseek(fd, 16, SEEK_CUR);
91 size_left -= 16;
93 if (size_left > buffer_left)
95 read(fd, buffer, buffer_left);
96 lseek(fd, size_left - buffer_left, SEEK_CUR);
97 bytes_read = buffer_left;
99 else
101 read(fd, buffer, size_left);
102 bytes_read = size_left;
106 return bytes_read;
109 /* Read a string tag from an MP4 file */
110 static unsigned int read_mp4_tag_string(int fd, int size_left, char** buffer,
111 unsigned int* buffer_left, char** dest)
113 unsigned int bytes_read = read_mp4_tag(fd, size_left, *buffer,
114 *buffer_left > 0 ? *buffer_left - 1 : 0);
115 unsigned int length = 0;
117 if (bytes_read)
119 (*buffer)[bytes_read] = 0;
120 *dest = *buffer;
121 length = strlen(*buffer) + 1;
122 *buffer_left -= length;
123 *buffer += length;
125 else
127 *dest = NULL;
130 return length;
133 static unsigned int read_mp4_atom(int fd, uint32_t* size,
134 uint32_t* type, uint32_t size_left)
136 read_uint32be(fd, size);
137 read_uint32be(fd, type);
139 if (*size == 1)
141 /* FAT32 doesn't support files this big, so something seems to
142 * be wrong. (64-bit sizes should only be used when required.)
144 errno = EFBIG;
145 *type = 0;
146 return 0;
149 if (*size > 0)
151 if (*size > size_left)
153 size_left = 0;
155 else
157 size_left -= *size;
160 *size -= 8;
162 else
164 *size = size_left;
165 size_left = 0;
168 return size_left;
171 static unsigned int read_mp4_length(int fd, uint32_t* size)
173 unsigned int length = 0;
174 int bytes = 0;
175 unsigned char c;
179 read(fd, &c, 1);
180 bytes++;
181 (*size)--;
182 length = (length << 7) | (c & 0x7F);
184 while ((c & 0x80) && (bytes < 4) && (*size > 0));
186 return length;
189 static bool read_mp4_esds(int fd, struct mp3entry* id3, uint32_t* size)
191 unsigned char buf[8];
192 bool sbr = false;
194 lseek(fd, 4, SEEK_CUR); /* Version and flags. */
195 read(fd, buf, 1); /* Verify ES_DescrTag. */
196 *size -= 5;
198 if (*buf == 3)
200 /* read length */
201 if (read_mp4_length(fd, size) < 20)
203 return sbr;
206 lseek(fd, 3, SEEK_CUR);
207 *size -= 3;
209 else
211 lseek(fd, 2, SEEK_CUR);
212 *size -= 2;
215 read(fd, buf, 1); /* Verify DecoderConfigDescrTab. */
216 *size -= 1;
218 if (*buf != 4)
220 return sbr;
223 if (read_mp4_length(fd, size) < 13)
225 return sbr;
228 lseek(fd, 13, SEEK_CUR); /* Skip audio type, bit rates, etc. */
229 read(fd, buf, 1);
230 *size -= 14;
232 if (*buf != 5) /* Verify DecSpecificInfoTag. */
234 return sbr;
238 static const int sample_rates[] =
240 96000, 88200, 64000, 48000, 44100, 32000,
241 24000, 22050, 16000, 12000, 11025, 8000
243 unsigned long bits;
244 unsigned int length;
245 unsigned int index;
246 unsigned int type;
248 /* Read the (leading part of the) decoder config. */
249 length = read_mp4_length(fd, size);
250 length = MIN(length, *size);
251 length = MIN(length, sizeof(buf));
252 memset(buf, 0, sizeof(buf));
253 read(fd, buf, length);
254 *size -= length;
256 /* Maybe time to write a simple read_bits function... */
258 /* Decoder config format:
259 * Object type - 5 bits
260 * Frequency index - 4 bits
261 * Channel configuration - 4 bits
263 bits = get_long_be(buf);
264 type = bits >> 27; /* Object type - 5 bits */
265 index = (bits >> 23) & 0xf; /* Frequency index - 4 bits */
267 if (index < (sizeof(sample_rates) / sizeof(*sample_rates)))
269 id3->frequency = sample_rates[index];
272 if (type == 5)
274 DEBUGF("MP4: SBR\n");
275 unsigned int old_index = index;
277 sbr = true;
278 index = (bits >> 15) & 0xf; /* Frequency index - 4 bits */
280 if (index == 15)
282 /* 17 bits read so far... */
283 bits = get_long_be(&buf[2]);
284 id3->frequency = (bits >> 7) & 0x00ffffff;
286 else if (index < (sizeof(sample_rates) / sizeof(*sample_rates)))
288 id3->frequency = sample_rates[index];
291 if (old_index == index)
293 /* Downsampled SBR */
294 id3->frequency *= 2;
297 /* Skip 13 bits from above, plus 3 bits, then read 11 bits */
298 else if ((length >= 4) && (((bits >> 5) & 0x7ff) == 0x2b7))
300 /* extensionAudioObjectType */
301 DEBUGF("MP4: extensionAudioType\n");
302 type = bits & 0x1f; /* Object type - 5 bits*/
303 bits = get_long_be(&buf[4]);
305 if (type == 5)
307 sbr = bits >> 31;
309 if (sbr)
311 unsigned int old_index = index;
313 /* 1 bit read so far */
314 index = (bits >> 27) & 0xf; /* Frequency index - 4 bits */
316 if (index == 15)
318 /* 5 bits read so far */
319 id3->frequency = (bits >> 3) & 0x00ffffff;
321 else if (index < (sizeof(sample_rates) / sizeof(*sample_rates)))
323 id3->frequency = sample_rates[index];
326 if (old_index == index)
328 /* Downsampled SBR */
329 id3->frequency *= 2;
335 if (!sbr && (id3->frequency <= 24000) && (length <= 2))
337 /* Double the frequency for low-frequency files without a "long"
338 * DecSpecificConfig header. The file may or may not contain SBR,
339 * but here we guess it does if the header is short. This can
340 * fail on some files, but it's the best we can do, short of
341 * decoding (parts of) the file.
343 id3->frequency *= 2;
347 return sbr;
350 static bool read_mp4_tags(int fd, struct mp3entry* id3,
351 uint32_t size_left)
353 uint32_t size;
354 uint32_t type;
355 unsigned int buffer_left = sizeof(id3->id3v2buf) + sizeof(id3->id3v1buf);
356 char* buffer = id3->id3v2buf;
357 bool cwrt = false;
361 size_left = read_mp4_atom(fd, &size, &type, size_left);
363 /* DEBUGF("Tag atom: '%c%c%c%c' (%d bytes left)\n", type >> 24 & 0xff,
364 type >> 16 & 0xff, type >> 8 & 0xff, type & 0xff, size); */
366 switch (type)
368 case MP4_cnam:
369 read_mp4_tag_string(fd, size, &buffer, &buffer_left,
370 &id3->title);
371 break;
373 case MP4_cART:
374 read_mp4_tag_string(fd, size, &buffer, &buffer_left,
375 &id3->artist);
376 break;
378 case MP4_aART:
379 read_mp4_tag_string(fd, size, &buffer, &buffer_left,
380 &id3->albumartist);
381 break;
383 case MP4_cgrp:
384 read_mp4_tag_string(fd, size, &buffer, &buffer_left,
385 &id3->grouping);
386 break;
388 case MP4_calb:
389 read_mp4_tag_string(fd, size, &buffer, &buffer_left,
390 &id3->album);
391 break;
393 case MP4_cwrt:
394 read_mp4_tag_string(fd, size, &buffer, &buffer_left,
395 &id3->composer);
396 cwrt = false;
397 break;
399 case MP4_ccmt:
400 read_mp4_tag_string(fd, size, &buffer, &buffer_left,
401 &id3->comment);
402 break;
404 case MP4_cday:
405 read_mp4_tag_string(fd, size, &buffer, &buffer_left,
406 &id3->year_string);
408 /* Try to parse it as a year, for the benefit of the database.
410 if(id3->year_string)
412 id3->year = atoi(id3->year_string);
413 if (id3->year < 1900)
415 id3->year = 0;
418 else
419 id3->year = 0;
421 break;
423 case MP4_gnre:
425 unsigned short genre;
427 read_mp4_tag(fd, size, (char*) &genre, sizeof(genre));
428 id3->genre_string = id3_get_num_genre(betoh16(genre) - 1);
430 break;
432 case MP4_cgen:
433 read_mp4_tag_string(fd, size, &buffer, &buffer_left,
434 &id3->genre_string);
435 break;
437 case MP4_disk:
439 unsigned short n[2];
441 read_mp4_tag(fd, size, (char*) &n, sizeof(n));
442 id3->discnum = betoh16(n[1]);
444 break;
446 case MP4_trkn:
448 unsigned short n[2];
450 read_mp4_tag(fd, size, (char*) &n, sizeof(n));
451 id3->tracknum = betoh16(n[1]);
453 break;
455 case MP4_extra:
457 char tag_name[TAG_NAME_LENGTH];
458 uint32_t sub_size;
460 /* "mean" atom */
461 read_uint32be(fd, &sub_size);
462 size -= sub_size;
463 lseek(fd, sub_size - 4, SEEK_CUR);
464 /* "name" atom */
465 read_uint32be(fd, &sub_size);
466 size -= sub_size;
467 lseek(fd, 8, SEEK_CUR);
468 sub_size -= 12;
470 if (sub_size > sizeof(tag_name) - 1)
472 read(fd, tag_name, sizeof(tag_name) - 1);
473 lseek(fd, sub_size - (sizeof(tag_name) - 1), SEEK_CUR);
474 tag_name[sizeof(tag_name) - 1] = 0;
476 else
478 read(fd, tag_name, sub_size);
479 tag_name[sub_size] = 0;
482 if ((strcasecmp(tag_name, "composer") == 0) && !cwrt)
484 read_mp4_tag_string(fd, size, &buffer, &buffer_left,
485 &id3->composer);
487 else if (strcasecmp(tag_name, "iTunSMPB") == 0)
489 char value[TAG_VALUE_LENGTH];
490 char* value_p = value;
491 char* any;
492 unsigned int length = sizeof(value);
494 read_mp4_tag_string(fd, size, &value_p, &length, &any);
495 id3->lead_trim = get_itunes_int32(value, 1);
496 id3->tail_trim = get_itunes_int32(value, 2);
497 DEBUGF("AAC: lead_trim %d, tail_trim %d\n",
498 id3->lead_trim, id3->tail_trim);
500 else if (strcasecmp(tag_name, "musicbrainz track id") == 0)
502 read_mp4_tag_string(fd, size, &buffer, &buffer_left,
503 &id3->mb_track_id);
505 else if ((strcasecmp(tag_name, "album artist") == 0))
507 read_mp4_tag_string(fd, size, &buffer, &buffer_left,
508 &id3->albumartist);
510 else
512 char* any;
513 unsigned int length = read_mp4_tag_string(fd, size,
514 &buffer, &buffer_left, &any);
516 if (length > 0)
518 /* Re-use the read buffer as the dest buffer... */
519 buffer -= length;
520 buffer_left += length;
522 if (parse_replaygain(tag_name, buffer, id3,
523 buffer, buffer_left) > 0)
525 /* Data used, keep it. */
526 buffer += length;
527 buffer_left -= length;
532 break;
534 default:
535 lseek(fd, size, SEEK_CUR);
536 break;
539 while ((size_left > 0) && (errno == 0));
541 return true;
544 static bool read_mp4_container(int fd, struct mp3entry* id3,
545 uint32_t size_left)
547 uint32_t size;
548 uint32_t type;
549 uint32_t handler = 0;
550 bool rc = true;
551 bool done = false;
555 size_left = read_mp4_atom(fd, &size, &type, size_left);
557 /* DEBUGF("Atom: '%c%c%c%c' (0x%08lx, %lu bytes left)\n",
558 (int) ((type >> 24) & 0xff), (int) ((type >> 16) & 0xff),
559 (int) ((type >> 8) & 0xff), (int) (type & 0xff),
560 type, size); */
562 switch (type)
564 case MP4_ftyp:
566 uint32_t id;
568 read_uint32be(fd, &id);
569 size -= 4;
571 if ((id != MP4_M4A) && (id != MP4_M4B) && (id != MP4_mp42)
572 && (id != MP4_qt) && (id != MP4_3gp6)
573 && (id != MP4_isom))
575 DEBUGF("Unknown MP4 file type: '%c%c%c%c'\n",
576 (int)(id >> 24 & 0xff), (int)(id >> 16 & 0xff),
577 (int)(id >> 8 & 0xff), (int)(id & 0xff));
578 return false;
581 break;
583 case MP4_meta:
584 lseek(fd, 4, SEEK_CUR); /* Skip version */
585 size -= 4;
586 /* Fall through */
588 case MP4_moov:
589 case MP4_udta:
590 case MP4_mdia:
591 case MP4_stbl:
592 case MP4_trak:
593 rc = read_mp4_container(fd, id3, size);
594 size = 0;
595 break;
597 case MP4_ilst:
598 if (handler == MP4_mdir)
600 rc = read_mp4_tags(fd, id3, size);
601 size = 0;
603 break;
605 case MP4_minf:
606 if (handler == MP4_soun)
608 rc = read_mp4_container(fd, id3, size);
609 size = 0;
611 break;
613 case MP4_stsd:
614 lseek(fd, 8, SEEK_CUR);
615 size -= 8;
616 rc = read_mp4_container(fd, id3, size);
617 size = 0;
618 break;
620 case MP4_hdlr:
621 lseek(fd, 8, SEEK_CUR);
622 read_uint32be(fd, &handler);
623 size -= 12;
624 /* DEBUGF(" Handler '%c%c%c%c'\n", handler >> 24 & 0xff,
625 handler >> 16 & 0xff, handler >> 8 & 0xff,handler & 0xff); */
626 break;
628 case MP4_stts:
630 uint32_t entries;
631 unsigned int i;
633 lseek(fd, 4, SEEK_CUR);
634 read_uint32be(fd, &entries);
635 id3->samples = 0;
637 for (i = 0; i < entries; i++)
639 uint32_t n;
640 uint32_t l;
642 read_uint32be(fd, &n);
643 read_uint32be(fd, &l);
644 id3->samples += n * l;
647 size = 0;
649 break;
651 case MP4_mp4a:
652 case MP4_alac:
654 uint32_t frequency;
656 id3->codectype = (type == MP4_mp4a) ? AFMT_MP4_AAC : AFMT_MP4_ALAC;
657 lseek(fd, 22, SEEK_CUR);
658 read_uint32be(fd, &frequency);
659 size -= 26;
660 id3->frequency = frequency;
662 if (type == MP4_mp4a)
664 uint32_t subsize;
665 uint32_t subtype;
667 /* Get frequency from the decoder info tag, if possible. */
668 lseek(fd, 2, SEEK_CUR);
669 /* The esds atom is a part of the mp4a atom, so ignore
670 * the returned size (it's already accounted for).
672 read_mp4_atom(fd, &subsize, &subtype, size);
673 size -= 10;
675 if (subtype == MP4_esds)
677 read_mp4_esds(fd, id3, &size);
681 break;
683 case MP4_mdat:
684 /* Some AAC files appear to contain additional empty mdat chunks.
685 Ignore them. */
686 if(size == 0)
687 break;
688 id3->filesize = size;
689 if(id3->samples > 0) {
690 /* We've already seen the moov chunk. */
691 done = true;
693 break;
695 case MP4_chpl:
697 /* ADDME: add support for real chapters. Right now it's only
698 * used for Nero's gapless hack */
699 uint8_t chapters;
700 uint64_t timestamp;
702 lseek(fd, 8, SEEK_CUR);
703 read_uint8(fd, &chapters);
704 size -= 9;
706 /* the first chapter will be used as the lead_trim */
707 if (chapters > 0) {
708 read_uint64be(fd, &timestamp);
709 id3->lead_trim = (timestamp * id3->frequency) / 10000000;
710 size -= 8;
713 break;
715 default:
716 break;
719 /* Skip final seek. */
720 if (!done)
722 lseek(fd, size, SEEK_CUR);
724 } while (rc && (size_left > 0) && (errno == 0) && !done);
726 return rc;
729 bool get_mp4_metadata(int fd, struct mp3entry* id3)
731 id3->codectype = AFMT_UNKNOWN;
732 id3->filesize = 0;
733 errno = 0;
735 if (read_mp4_container(fd, id3, filesize(fd)) && (errno == 0)
736 && (id3->samples > 0) && (id3->frequency > 0)
737 && (id3->filesize > 0))
739 if (id3->codectype == AFMT_UNKNOWN)
741 logf("Not an ALAC or AAC file");
742 return false;
745 id3->length = ((int64_t) id3->samples * 1000) / id3->frequency;
747 if (id3->length <= 0)
749 logf("mp4 length invalid!");
750 return false;
753 id3->bitrate = ((int64_t) id3->filesize * 8) / id3->length;
754 DEBUGF("MP4 bitrate %d, frequency %ld Hz, length %ld ms\n",
755 id3->bitrate, id3->frequency, id3->length);
757 else
759 logf("MP4 metadata error");
760 DEBUGF("MP4 metadata error. errno %d, samples %ld, frequency %ld, "
761 "filesize %ld\n", errno, id3->samples, id3->frequency,
762 id3->filesize);
763 return false;
766 return true;