Update Russian translation - FS#11770 by Alexey Polkhirev
[maemo-rb.git] / apps / metadata / mp4.c
blobb3ff348fc5a84568e7086bdd1b74d4339c714a08
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_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
77 * buffer.
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;
84 if (buffer_left == 0)
86 lseek(fd, size_left, SEEK_CUR); /* Skip everything */
88 else
90 /* Skip the data tag header - maybe we should parse it properly? */
91 lseek(fd, 16, SEEK_CUR);
92 size_left -= 16;
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;
100 else
102 read(fd, buffer, size_left);
103 bytes_read = size_left;
107 return bytes_read;
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;
118 if (bytes_read)
120 (*buffer)[bytes_read] = 0;
121 *dest = *buffer;
122 length = strlen(*buffer) + 1;
123 *buffer_left -= length;
124 *buffer += length;
126 else
128 *dest = NULL;
131 return length;
134 static unsigned int read_mp4_atom(int fd, uint32_t* size,
135 uint32_t* type, uint32_t size_left)
137 read_uint32be(fd, size);
138 read_uint32be(fd, type);
140 if (*size == 1)
142 /* FAT32 doesn't support files this big, so something seems to
143 * be wrong. (64-bit sizes should only be used when required.)
145 errno = EFBIG;
146 *type = 0;
147 return 0;
150 if (*size > 0)
152 if (*size > size_left)
154 size_left = 0;
156 else
158 size_left -= *size;
161 *size -= 8;
163 else
165 *size = size_left;
166 size_left = 0;
169 return size_left;
172 static unsigned int read_mp4_length(int fd, uint32_t* size)
174 unsigned int length = 0;
175 int bytes = 0;
176 unsigned char c;
180 read(fd, &c, 1);
181 bytes++;
182 (*size)--;
183 length = (length << 7) | (c & 0x7F);
185 while ((c & 0x80) && (bytes < 4) && (*size > 0));
187 return length;
190 static bool read_mp4_esds(int fd, struct mp3entry* id3, uint32_t* size)
192 unsigned char buf[8];
193 bool sbr = false;
195 lseek(fd, 4, SEEK_CUR); /* Version and flags. */
196 read(fd, buf, 1); /* Verify ES_DescrTag. */
197 *size -= 5;
199 if (*buf == 3)
201 /* read length */
202 if (read_mp4_length(fd, size) < 20)
204 return sbr;
207 lseek(fd, 3, SEEK_CUR);
208 *size -= 3;
210 else
212 lseek(fd, 2, SEEK_CUR);
213 *size -= 2;
216 read(fd, buf, 1); /* Verify DecoderConfigDescrTab. */
217 *size -= 1;
219 if (*buf != 4)
221 return sbr;
224 if (read_mp4_length(fd, size) < 13)
226 return sbr;
229 lseek(fd, 13, SEEK_CUR); /* Skip audio type, bit rates, etc. */
230 read(fd, buf, 1);
231 *size -= 14;
233 if (*buf != 5) /* Verify DecSpecificInfoTag. */
235 return sbr;
239 static const int sample_rates[] =
241 96000, 88200, 64000, 48000, 44100, 32000,
242 24000, 22050, 16000, 12000, 11025, 8000
244 unsigned long bits;
245 unsigned int length;
246 unsigned int index;
247 unsigned int type;
249 /* Read the (leading part of the) decoder config. */
250 length = read_mp4_length(fd, size);
251 length = MIN(length, *size);
252 length = MIN(length, sizeof(buf));
253 memset(buf, 0, sizeof(buf));
254 read(fd, buf, length);
255 *size -= length;
257 /* Maybe time to write a simple read_bits function... */
259 /* Decoder config format:
260 * Object type - 5 bits
261 * Frequency index - 4 bits
262 * Channel configuration - 4 bits
264 bits = get_long_be(buf);
265 type = bits >> 27; /* Object type - 5 bits */
266 index = (bits >> 23) & 0xf; /* Frequency index - 4 bits */
268 if (index < (sizeof(sample_rates) / sizeof(*sample_rates)))
270 id3->frequency = sample_rates[index];
273 if (type == 5)
275 DEBUGF("MP4: SBR\n");
276 unsigned int old_index = index;
278 sbr = true;
279 index = (bits >> 15) & 0xf; /* Frequency index - 4 bits */
281 if (index == 15)
283 /* 17 bits read so far... */
284 bits = get_long_be(&buf[2]);
285 id3->frequency = (bits >> 7) & 0x00ffffff;
287 else if (index < (sizeof(sample_rates) / sizeof(*sample_rates)))
289 id3->frequency = sample_rates[index];
292 if (old_index == index)
294 /* Downsampled SBR */
295 id3->frequency *= 2;
298 /* Skip 13 bits from above, plus 3 bits, then read 11 bits */
299 else if ((length >= 4) && (((bits >> 5) & 0x7ff) == 0x2b7))
301 /* extensionAudioObjectType */
302 DEBUGF("MP4: extensionAudioType\n");
303 type = bits & 0x1f; /* Object type - 5 bits*/
304 bits = get_long_be(&buf[4]);
306 if (type == 5)
308 sbr = bits >> 31;
310 if (sbr)
312 unsigned int old_index = index;
314 /* 1 bit read so far */
315 index = (bits >> 27) & 0xf; /* Frequency index - 4 bits */
317 if (index == 15)
319 /* 5 bits read so far */
320 id3->frequency = (bits >> 3) & 0x00ffffff;
322 else if (index < (sizeof(sample_rates) / sizeof(*sample_rates)))
324 id3->frequency = sample_rates[index];
327 if (old_index == index)
329 /* Downsampled SBR */
330 id3->frequency *= 2;
336 if (!sbr && (id3->frequency <= 24000) && (length <= 2))
338 /* Double the frequency for low-frequency files without a "long"
339 * DecSpecificConfig header. The file may or may not contain SBR,
340 * but here we guess it does if the header is short. This can
341 * fail on some files, but it's the best we can do, short of
342 * decoding (parts of) the file.
344 id3->frequency *= 2;
348 return sbr;
351 static bool read_mp4_tags(int fd, struct mp3entry* id3,
352 uint32_t size_left)
354 uint32_t size;
355 uint32_t type;
356 unsigned int buffer_left = sizeof(id3->id3v2buf) + sizeof(id3->id3v1buf);
357 char* buffer = id3->id3v2buf;
358 bool cwrt = false;
362 size_left = read_mp4_atom(fd, &size, &type, size_left);
364 /* DEBUGF("Tag atom: '%c%c%c%c' (%d bytes left)\n", type >> 24 & 0xff,
365 type >> 16 & 0xff, type >> 8 & 0xff, type & 0xff, size); */
367 switch (type)
369 case MP4_cnam:
370 read_mp4_tag_string(fd, size, &buffer, &buffer_left,
371 &id3->title);
372 break;
374 case MP4_cART:
375 read_mp4_tag_string(fd, size, &buffer, &buffer_left,
376 &id3->artist);
377 break;
379 case MP4_aART:
380 read_mp4_tag_string(fd, size, &buffer, &buffer_left,
381 &id3->albumartist);
382 break;
384 case MP4_cgrp:
385 read_mp4_tag_string(fd, size, &buffer, &buffer_left,
386 &id3->grouping);
387 break;
389 case MP4_calb:
390 read_mp4_tag_string(fd, size, &buffer, &buffer_left,
391 &id3->album);
392 break;
394 case MP4_cwrt:
395 read_mp4_tag_string(fd, size, &buffer, &buffer_left,
396 &id3->composer);
397 cwrt = false;
398 break;
400 case MP4_ccmt:
401 read_mp4_tag_string(fd, size, &buffer, &buffer_left,
402 &id3->comment);
403 break;
405 case MP4_cday:
406 read_mp4_tag_string(fd, size, &buffer, &buffer_left,
407 &id3->year_string);
409 /* Try to parse it as a year, for the benefit of the database.
411 if(id3->year_string)
413 id3->year = atoi(id3->year_string);
414 if (id3->year < 1900)
416 id3->year = 0;
419 else
420 id3->year = 0;
422 break;
424 case MP4_gnre:
426 unsigned short genre;
428 read_mp4_tag(fd, size, (char*) &genre, sizeof(genre));
429 id3->genre_string = id3_get_num_genre(betoh16(genre) - 1);
431 break;
433 case MP4_cgen:
434 read_mp4_tag_string(fd, size, &buffer, &buffer_left,
435 &id3->genre_string);
436 break;
438 case MP4_disk:
440 unsigned short n[2];
442 read_mp4_tag(fd, size, (char*) &n, sizeof(n));
443 id3->discnum = betoh16(n[1]);
445 break;
447 case MP4_trkn:
449 unsigned short n[2];
451 read_mp4_tag(fd, size, (char*) &n, sizeof(n));
452 id3->tracknum = betoh16(n[1]);
454 break;
456 case MP4_extra:
458 char tag_name[TAG_NAME_LENGTH];
459 uint32_t sub_size;
461 /* "mean" atom */
462 read_uint32be(fd, &sub_size);
463 size -= sub_size;
464 lseek(fd, sub_size - 4, SEEK_CUR);
465 /* "name" atom */
466 read_uint32be(fd, &sub_size);
467 size -= sub_size;
468 lseek(fd, 8, SEEK_CUR);
469 sub_size -= 12;
471 if (sub_size > sizeof(tag_name) - 1)
473 read(fd, tag_name, sizeof(tag_name) - 1);
474 lseek(fd, sub_size - (sizeof(tag_name) - 1), SEEK_CUR);
475 tag_name[sizeof(tag_name) - 1] = 0;
477 else
479 read(fd, tag_name, sub_size);
480 tag_name[sub_size] = 0;
483 if ((strcasecmp(tag_name, "composer") == 0) && !cwrt)
485 read_mp4_tag_string(fd, size, &buffer, &buffer_left,
486 &id3->composer);
488 else if (strcasecmp(tag_name, "iTunSMPB") == 0)
490 char value[TAG_VALUE_LENGTH];
491 char* value_p = value;
492 char* any;
493 unsigned int length = sizeof(value);
495 read_mp4_tag_string(fd, size, &value_p, &length, &any);
496 id3->lead_trim = get_itunes_int32(value, 1);
497 id3->tail_trim = get_itunes_int32(value, 2);
498 DEBUGF("AAC: lead_trim %d, tail_trim %d\n",
499 id3->lead_trim, id3->tail_trim);
501 else if (strcasecmp(tag_name, "musicbrainz track id") == 0)
503 read_mp4_tag_string(fd, size, &buffer, &buffer_left,
504 &id3->mb_track_id);
506 else if ((strcasecmp(tag_name, "album artist") == 0))
508 read_mp4_tag_string(fd, size, &buffer, &buffer_left,
509 &id3->albumartist);
511 else
513 char* any;
514 unsigned int length = read_mp4_tag_string(fd, size,
515 &buffer, &buffer_left, &any);
517 if (length > 0)
519 /* Re-use the read buffer as the dest buffer... */
520 buffer -= length;
521 buffer_left += length;
523 if (parse_replaygain(tag_name, buffer, id3,
524 buffer, buffer_left) > 0)
526 /* Data used, keep it. */
527 buffer += length;
528 buffer_left -= length;
533 break;
535 default:
536 lseek(fd, size, SEEK_CUR);
537 break;
540 while ((size_left > 0) && (errno == 0));
542 return true;
545 static bool read_mp4_container(int fd, struct mp3entry* id3,
546 uint32_t size_left)
548 uint32_t size;
549 uint32_t type;
550 uint32_t handler = 0;
551 bool rc = true;
552 bool done = false;
556 size_left = read_mp4_atom(fd, &size, &type, size_left);
558 /* DEBUGF("Atom: '%c%c%c%c' (0x%08lx, %lu bytes left)\n",
559 (int) ((type >> 24) & 0xff), (int) ((type >> 16) & 0xff),
560 (int) ((type >> 8) & 0xff), (int) (type & 0xff),
561 type, size); */
563 switch (type)
565 case MP4_ftyp:
567 uint32_t id;
569 read_uint32be(fd, &id);
570 size -= 4;
572 if ((id != MP4_M4A) && (id != MP4_M4B) && (id != MP4_mp42)
573 && (id != MP4_qt) && (id != MP4_3gp6) && (id != MP4_m4a)
574 && (id != MP4_isom))
576 DEBUGF("Unknown MP4 file type: '%c%c%c%c'\n",
577 (int)(id >> 24 & 0xff), (int)(id >> 16 & 0xff),
578 (int)(id >> 8 & 0xff), (int)(id & 0xff));
579 return false;
582 break;
584 case MP4_meta:
585 lseek(fd, 4, SEEK_CUR); /* Skip version */
586 size -= 4;
587 /* Fall through */
589 case MP4_moov:
590 case MP4_udta:
591 case MP4_mdia:
592 case MP4_stbl:
593 case MP4_trak:
594 rc = read_mp4_container(fd, id3, size);
595 size = 0;
596 break;
598 case MP4_ilst:
599 if (handler == MP4_mdir)
601 rc = read_mp4_tags(fd, id3, size);
602 size = 0;
604 break;
606 case MP4_minf:
607 if (handler == MP4_soun)
609 rc = read_mp4_container(fd, id3, size);
610 size = 0;
612 break;
614 case MP4_stsd:
615 lseek(fd, 8, SEEK_CUR);
616 size -= 8;
617 rc = read_mp4_container(fd, id3, size);
618 size = 0;
619 break;
621 case MP4_hdlr:
622 lseek(fd, 8, SEEK_CUR);
623 read_uint32be(fd, &handler);
624 size -= 12;
625 /* DEBUGF(" Handler '%c%c%c%c'\n", handler >> 24 & 0xff,
626 handler >> 16 & 0xff, handler >> 8 & 0xff,handler & 0xff); */
627 break;
629 case MP4_stts:
631 uint32_t entries;
632 unsigned int i;
634 lseek(fd, 4, SEEK_CUR);
635 read_uint32be(fd, &entries);
636 id3->samples = 0;
638 for (i = 0; i < entries; i++)
640 uint32_t n;
641 uint32_t l;
643 read_uint32be(fd, &n);
644 read_uint32be(fd, &l);
645 id3->samples += n * l;
648 size = 0;
650 break;
652 case MP4_mp4a:
653 case MP4_alac:
655 uint32_t frequency;
657 id3->codectype = (type == MP4_mp4a) ? AFMT_MP4_AAC : AFMT_MP4_ALAC;
658 lseek(fd, 22, SEEK_CUR);
659 read_uint32be(fd, &frequency);
660 size -= 26;
661 id3->frequency = frequency;
663 if (type == MP4_mp4a)
665 uint32_t subsize;
666 uint32_t subtype;
668 /* Get frequency from the decoder info tag, if possible. */
669 lseek(fd, 2, SEEK_CUR);
670 /* The esds atom is a part of the mp4a atom, so ignore
671 * the returned size (it's already accounted for).
673 read_mp4_atom(fd, &subsize, &subtype, size);
674 size -= 10;
676 if (subtype == MP4_esds)
678 read_mp4_esds(fd, id3, &size);
682 break;
684 case MP4_mdat:
685 /* Some AAC files appear to contain additional empty mdat chunks.
686 Ignore them. */
687 if(size == 0)
688 break;
689 id3->filesize = size;
690 if(id3->samples > 0) {
691 /* We've already seen the moov chunk. */
692 done = true;
694 break;
696 case MP4_chpl:
698 /* ADDME: add support for real chapters. Right now it's only
699 * used for Nero's gapless hack */
700 uint8_t chapters;
701 uint64_t timestamp;
703 lseek(fd, 8, SEEK_CUR);
704 read_uint8(fd, &chapters);
705 size -= 9;
707 /* the first chapter will be used as the lead_trim */
708 if (chapters > 0) {
709 read_uint64be(fd, &timestamp);
710 id3->lead_trim = (timestamp * id3->frequency) / 10000000;
711 size -= 8;
714 break;
716 default:
717 break;
720 /* Skip final seek. */
721 if (!done)
723 lseek(fd, size, SEEK_CUR);
725 } while (rc && (size_left > 0) && (errno == 0) && !done);
727 return rc;
730 bool get_mp4_metadata(int fd, struct mp3entry* id3)
732 id3->codectype = AFMT_UNKNOWN;
733 id3->filesize = 0;
734 errno = 0;
736 if (read_mp4_container(fd, id3, filesize(fd)) && (errno == 0)
737 && (id3->samples > 0) && (id3->frequency > 0)
738 && (id3->filesize > 0))
740 if (id3->codectype == AFMT_UNKNOWN)
742 logf("Not an ALAC or AAC file");
743 return false;
746 id3->length = ((int64_t) id3->samples * 1000) / id3->frequency;
748 if (id3->length <= 0)
750 logf("mp4 length invalid!");
751 return false;
754 id3->bitrate = ((int64_t) id3->filesize * 8) / id3->length;
755 DEBUGF("MP4 bitrate %d, frequency %ld Hz, length %ld ms\n",
756 id3->bitrate, id3->frequency, id3->length);
758 else
760 logf("MP4 metadata error");
761 DEBUGF("MP4 metadata error. errno %d, samples %ld, frequency %ld, "
762 "filesize %ld\n", errno, id3->samples, id3->frequency,
763 id3->filesize);
764 return false;
767 return true;