2 * Copyright (C) 2003-2005 M. Bakker, Nero AG, http://www.nero.com
3 * FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
12 #include "portable_io.h"
18 uint16_t channel_count;
22 uint32_t stsz_sample_size;
23 uint32_t stsz_sample_count;
27 uint32_t stts_entry_count;
28 uint32_t *stts_sample_count;
31 uint32_t stsc_entry_count;
32 uint32_t *stsc_first_chunk;
33 uint32_t *stsc_samples_per_chunk;
36 uint32_t stco_entry_count;
37 uint32_t *stco_chunk_offset;
44 #define MAX_TRACKS 1024
47 const struct mp4_callback *cb;
59 /* incremental track index while reading the file */
62 struct mp4_track *track[MAX_TRACKS];
63 /* the first audio track found */
64 struct mp4_track *audio_track;
67 struct mp4_metadata meta;
71 * Returns -1, 0, or 1 on errors/EOF/success. Partial reads followed by EOF or
72 * read errors are treated as errors.
74 static int read_data(struct mp4 *f, void *data, size_t size)
77 ssize_t ret = f->cb->read(f->cb->user_data, data, size);
78 if (ret < 0 && errno == EINTR)
80 /* regard EAGAIN as an error as reads should be blocking. */
82 return ret < 0? -1 : 0;
88 static int read_int64(struct mp4 *f, uint64_t *result)
91 int ret = read_data(f, data, 8);
93 if (ret > 0 && result)
94 *result = read_u64_be(data);
98 static int read_int32(struct mp4 *f, uint32_t *result)
101 int ret = read_data(f, data, 4);
103 if (ret > 0 && result)
104 *result = read_u32_be(data);
108 static int read_int24(struct mp4 *f, uint32_t *result)
111 int ret = read_data(f, data, 3);
113 if (ret > 0 && result)
114 *result = read_u24_be(data);
118 static int read_int16(struct mp4 *f, uint16_t *result)
121 int ret = read_data(f, data, 2);
123 if (ret > 0 && result)
124 *result = read_u16_be(data);
128 static uint8_t read_int8(struct mp4 *f, uint8_t *result)
131 int ret = read_data(f, data, 1);
133 if (ret > 0 && result)
139 ATOM_ITEM(MOOV, 'm', 'o', 'o', 'v') \
140 ATOM_ITEM(TRAK, 't', 'r', 'a', 'k') \
141 ATOM_ITEM(MDIA, 'm', 'd', 'i', 'a') \
142 ATOM_ITEM(MINF, 'm', 'i', 'n', 'f') \
143 ATOM_ITEM(STBL, 's', 't', 'b', 'l') \
144 ATOM_ITEM(UDTA, 'u', 'd', 't', 'a') \
145 ATOM_ITEM(ILST, 'i', 'l', 's', 't') /* iTunes Metadata list */ \
146 ATOM_ITEM(ARTIST, 0xa9, 'A', 'R', 'T') \
147 ATOM_ITEM(TITLE, 0xa9, 'n', 'a', 'm') \
148 ATOM_ITEM(ALBUM, 0xa9, 'a', 'l', 'b') \
149 ATOM_ITEM(DATE, 0xa9, 'd', 'a', 'y') \
150 ATOM_ITEM(COMMENT, 0xa9, 'c', 'm', 't') \
151 ATOM_ITEM(MDHD, 'm', 'd', 'h', 'd') /* track header */ \
152 ATOM_ITEM(STSD, 's', 't', 's', 'd') /* sample description box */ \
153 ATOM_ITEM(STTS, 's', 't', 't', 's') /* time to sample box */ \
154 ATOM_ITEM(STSZ, 's', 't', 's', 'z') /* sample size box */ \
155 ATOM_ITEM(STCO, 's', 't', 'c', 'o') /* chunk offset box */ \
156 ATOM_ITEM(STSC, 's', 't', 's', 'c') /* sample to chunk box */ \
157 ATOM_ITEM(MP4A, 'm', 'p', '4', 'a') \
158 ATOM_ITEM(META, 'm', 'e', 't', 'a') /* iTunes Metadata box */ \
159 ATOM_ITEM(DATA, 'd', 'a', 't', 'a') /* iTunes Metadata data box */ \
161 #define ATOM_ITEM(_name, a, b, c, d) ATOM_ ## _name,
162 enum atom {ATOM_ITEMS};
165 static uint8_t atom_name_to_type(uint8_t *p)
167 #define ATOM_VALUE(a, b, c, d) ((a << 24) + (b << 16) + (c << 8) + d)
168 #define ATOM_ITEM(_name, a, b, c, d) \
169 {.name = # _name, .val = ATOM_VALUE(a, b, c, d)},
170 static const struct {
173 } atom_table[] = {ATOM_ITEMS};
175 uint32_t val = read_u32_be(p);
177 for (uint8_t n = 0; n < ARRAY_SIZE(atom_table); n++)
178 if (val == atom_table[n].val)
183 /* read atom header, atom size is returned with header included. */
184 static int atom_read_header(struct mp4 *f, uint8_t *atom_type,
185 uint8_t *header_size, uint64_t *atom_size)
189 uint8_t atom_header[8];
191 ret = read_data(f, atom_header, 8);
194 size = read_u32_be(atom_header);
195 if (size == 1) { /* 64 bit atom size */
198 ret = read_int64(f, atom_size);
207 *atom_type = atom_name_to_type(atom_header + 4);
211 static off_t get_position(const struct mp4 *f)
213 return f->cb->seek(f->cb->user_data, 0, SEEK_CUR);
216 static void set_position(struct mp4 *f, off_t position)
218 f->cb->seek(f->cb->user_data, position, SEEK_SET);
221 static int read_stsz(struct mp4 *f)
227 if (f->total_tracks == 0)
229 t = f->track[f->total_tracks - 1];
230 ret = read_int8(f, NULL); /* version */
233 ret = read_int24(f, NULL); /* flags */
236 ret = read_int32(f, &t->stsz_sample_size);
239 ret = read_int32(f, &t->stsz_sample_count);
242 if (t->stsz_sample_size != 0)
244 t->stsz_table = para_malloc(t->stsz_sample_count * sizeof(int32_t));
245 for (i = 0; i < t->stsz_sample_count; i++) {
246 ret = read_int32(f, &t->stsz_table[i]);
253 static int read_stts(struct mp4 *f)
259 if (f->total_tracks == 0)
261 t = f->track[f->total_tracks - 1];
262 if (t->stts_entry_count)
264 ret = read_int8(f, NULL); /* version */
267 ret = read_int24(f, NULL); /* flags */
270 ret = read_int32(f, &t->stts_entry_count);
273 t->stts_sample_count = para_malloc(t->stts_entry_count
275 for (i = 0; i < t->stts_entry_count; i++) {
276 ret = read_int32(f, &t->stts_sample_count[i]);
279 ret = read_int32(f, NULL); /* sample delta */
286 static int read_stsc(struct mp4 *f)
292 if (f->total_tracks == 0)
294 t = f->track[f->total_tracks - 1];
296 ret = read_int8(f, NULL); /* version */
299 ret = read_int24(f, NULL); /* flags */
302 ret = read_int32(f, &t->stsc_entry_count);
305 t->stsc_first_chunk = para_malloc(t->stsc_entry_count * sizeof(int32_t));
306 t->stsc_samples_per_chunk = para_malloc(t->stsc_entry_count
308 for (i = 0; i < t->stsc_entry_count; i++) {
309 ret = read_int32(f, &t->stsc_first_chunk[i]);
312 ret = read_int32(f, &t->stsc_samples_per_chunk[i]);
315 ret = read_int32(f, NULL); /* sample desc index */
322 static int read_stco(struct mp4 *f)
328 if (f->total_tracks == 0)
330 t = f->track[f->total_tracks - 1];
332 ret = read_int8(f, NULL); /* version */
335 ret = read_int24(f, NULL); /* flags */
338 ret = read_int32(f, &t->stco_entry_count);
341 t->stco_chunk_offset = para_malloc(t->stco_entry_count
343 for (i = 0; i < t->stco_entry_count; i++) {
344 ret = read_int32(f, &t->stco_chunk_offset[i]);
351 static int read_mp4a(struct mp4 *f)
357 if (f->total_tracks == 0)
359 t = f->track[f->total_tracks - 1];
361 for (i = 0; i < 6; i++) {
362 ret = read_int8(f, NULL); /* reserved */
366 ret = read_int16(f, NULL); /* data_reference_index */
369 ret = read_int32(f, NULL); /* reserved */
372 ret = read_int32(f, NULL); /* reserved */
375 ret = read_int16(f, &t->channel_count);
378 ret = read_int16(f, NULL);
381 ret = read_int16(f, NULL);
384 ret = read_int16(f, NULL);
387 return read_int16(f, &t->sample_rate);
390 static int read_stsd(struct mp4 *f)
393 uint32_t i, entry_count;
396 if (f->total_tracks == 0)
398 t = f->track[f->total_tracks - 1];
399 ret = read_int8(f, NULL); /* version */
402 ret = read_int24(f, NULL); /* flags */
405 ret = read_int32(f, &entry_count);
408 for (i = 0; i < entry_count; i++) {
409 uint64_t skip = get_position(f);
411 uint8_t atom_type = 0;
412 ret = atom_read_header(f, &atom_type, NULL, &size);
416 if (!f->audio_track && atom_type == ATOM_MP4A) {
420 set_position(f, skip);
425 static const char *get_metadata_name(uint8_t atom_type)
428 case ATOM_TITLE: return "title";
429 case ATOM_ARTIST: return "artist";
430 case ATOM_ALBUM: return "album";
431 case ATOM_DATE: return "date";
432 case ATOM_COMMENT: return "comment";
433 default: return "unknown";
437 static int parse_tag(struct mp4 *f, uint8_t parent, int32_t size)
440 uint64_t subsize, sumsize;
449 set_position(f, destpos), sumsize += subsize
452 uint8_t header_size = 0;
453 ret = atom_read_header(f, &atom_type, &header_size, &subsize);
456 destpos = get_position(f) + subsize - header_size;
457 if (atom_type != ATOM_DATA)
459 ret = read_int8(f, NULL); /* version */
462 ret = read_int24(f, NULL); /* flags */
465 ret = read_int32(f, NULL); /* reserved */
468 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
469 if (subsize < header_size + 8 || subsize > UINT_MAX)
471 len = subsize - (header_size + 8);
473 value = para_malloc(len + 1);
474 ret = read_data(f, value, len);
480 return -ERRNO_TO_PARA_ERROR(EINVAL);
481 f->meta.tags = para_realloc(f->meta.tags, (f->meta.count + 1)
482 * sizeof(struct mp4_tag));
483 tag = f->meta.tags + f->meta.count;
484 tag->item = para_strdup(get_metadata_name(parent));
494 static int read_mdhd(struct mp4 *f)
500 if (f->total_tracks == 0)
502 t = f->track[f->total_tracks - 1];
504 ret = read_int32(f, &version);
508 ret = read_int64(f, NULL); /* creation-time */
511 ret = read_int64(f, NULL); /* modification-time */
514 ret = read_int32(f, &t->time_scale);
517 ret = read_int64(f, &t->duration);
520 } else { //version == 0
523 ret = read_int32(f, NULL); /* creation-time */
526 ret = read_int32(f, NULL); /* modification-time */
529 ret = read_int32(f, &t->time_scale);
532 ret = read_int32(f, &temp);
535 t->duration = (temp == (uint32_t) (-1))?
536 (uint64_t) (-1) : (uint64_t) (temp);
538 ret = read_int16(f, NULL);
541 ret = read_int16(f, NULL);
547 static int32_t read_ilst(struct mp4 *f, int32_t size)
550 uint64_t sumsize = 0;
552 while (sumsize < size) {
554 uint64_t subsize, destpos;
555 uint8_t header_size = 0;
556 ret = atom_read_header(f, &atom_type, &header_size, &subsize);
559 destpos = get_position(f) + subsize - header_size;
566 ret = parse_tag(f, atom_type, subsize - header_size);
570 set_position(f, destpos);
576 static int32_t read_meta(struct mp4 *f, uint64_t size)
579 uint64_t subsize, sumsize = 0;
581 uint8_t header_size = 0;
583 ret = read_int8(f, NULL); /* version */
586 ret = read_int24(f, NULL); /* flags */
589 while (sumsize < (size - (header_size + 4))) {
590 ret = atom_read_header(f, &atom_type, &header_size, &subsize);
593 if (subsize <= header_size + 4)
595 if (atom_type == ATOM_ILST) {
596 f->ilst_offset = get_position(f) - header_size;
597 f->ilst_size = subsize;
598 ret = read_ilst(f, subsize - (header_size + 4));
602 set_position(f, get_position(f) + subsize - header_size);
608 static bool need_atom(uint8_t atom_type, bool meta_only)
610 /* these are needed in any case */
621 /* meta-only opens don't need anything else */
624 /* these are only required for regular opens */
636 /* parse atoms that are sub atoms of other atoms */
637 static int parse_sub_atoms(struct mp4 *f, uint64_t total_size, bool meta_only)
640 uint64_t dest, size, end = get_position(f) + total_size;
642 for (dest = get_position(f); dest < end; set_position(f, dest)) {
643 uint8_t header_size, atom_type;
644 ret = atom_read_header(f, &atom_type, &header_size, &size);
649 dest = get_position(f) + size - header_size;
650 if (atom_type == ATOM_TRAK) {
651 if (f->total_tracks >= MAX_TRACKS)
654 f->track[f->total_tracks - 1] = para_calloc(
655 sizeof(struct mp4_track));
656 } else if (atom_type == ATOM_UDTA) {
657 f->udta_offset = get_position(f) - header_size;
660 if (!need_atom(atom_type, meta_only))
663 case ATOM_STSZ: ret = read_stsz(f); break;
664 case ATOM_STTS: ret = read_stts(f); break;
665 case ATOM_STSC: ret = read_stsc(f); break;
666 case ATOM_STCO: ret = read_stco(f); break;
667 case ATOM_STSD: ret = read_stsd(f); break;
668 case ATOM_MDHD: ret = read_mdhd(f); break;
670 f->meta_offset = get_position(f) - header_size;
672 ret = read_meta(f, size);
675 ret = parse_sub_atoms(f, size - header_size, meta_only);
683 static int open_file(const struct mp4_callback *cb, bool meta_only, struct mp4 **result)
687 uint8_t atom_type, header_size;
688 struct mp4 *f = para_calloc(sizeof(*f));
691 while ((ret = atom_read_header(f, &atom_type, &header_size, &size)) > 0) {
692 f->last_atom = atom_type;
693 if (atom_type != ATOM_MOOV || size <= header_size) { /* skip */
694 set_position(f, get_position(f) + size - header_size);
697 f->moov_offset = get_position(f) - header_size;
699 ret = parse_sub_atoms(f, size - header_size, meta_only);
718 int mp4_open_read(const struct mp4_callback *cb, struct mp4 **result)
720 return open_file(cb, false, result);
723 void mp4_close(struct mp4 *f)
727 for (i = 0; i < f->total_tracks; i++) {
729 free(f->track[i]->stsz_table);
730 free(f->track[i]->stts_sample_count);
731 free(f->track[i]->stsc_first_chunk);
732 free(f->track[i]->stsc_samples_per_chunk);
733 free(f->track[i]->stco_chunk_offset);
737 for (i = 0; i < f->meta.count; i++) {
738 free(f->meta.tags[i].item);
739 free(f->meta.tags[i].value);
745 static int32_t chunk_of_sample(const struct mp4 *f, int32_t sample,
748 const struct mp4_track *t = f->audio_track;
749 uint32_t *fc = t->stsc_first_chunk, *spc = t->stsc_samples_per_chunk;
750 int32_t chunk1, chunk1samples, n, total, i;
752 for (i = 1, total = 0; i < t->stsc_entry_count; i++, total += n) {
753 n = (fc[i] - fc[i - 1]) * spc[i - 1]; /* number of samples */
754 if (sample < total + n)
758 chunk1samples = spc[i - 1];
759 if (chunk1samples != 0)
760 *chunk = (sample - total) / chunk1samples + chunk1;
763 return total + (*chunk - chunk1) * chunk1samples;
767 * Return the number of milliseconds of the audio track.
769 * \param f As returned by \ref mp4_open_read(), must not be NULL.
771 uint64_t mp4_get_duration(const struct mp4 *f)
773 const struct mp4_track *t = f->audio_track;
775 if (t->time_scale == 0)
777 return t->duration * 1000 / t->time_scale;
780 int mp4_set_sample_position(struct mp4 *f, int32_t sample)
782 const struct mp4_track *t = f->audio_track;
783 int32_t offset, chunk, chunk_sample;
784 uint32_t n, srs; /* sample range size */
786 if (sample >= t->stsz_sample_count)
787 return -ERRNO_TO_PARA_ERROR(EINVAL);
788 chunk_sample = chunk_of_sample(f, sample, &chunk);
789 if (t->stsz_sample_size > 0)
790 srs = (sample - chunk_sample) * t->stsz_sample_size;
792 for (srs = 0, n = chunk_sample; n < sample; n++)
793 srs += t->stsz_table[n];
795 if (t->stco_entry_count > 0 && chunk > t->stco_entry_count)
796 offset = t->stco_chunk_offset[t->stco_entry_count - 1];
797 else if (t->stco_entry_count > 0)
798 offset = t->stco_chunk_offset[chunk - 1];
801 set_position(f, offset + srs);
805 int32_t mp4_get_sample_size(const struct mp4 *f, int sample)
807 const struct mp4_track *t = f->audio_track;
809 if (t->stsz_sample_size != 0)
810 return t->stsz_sample_size;
811 return t->stsz_table[sample];
814 uint32_t mp4_get_sample_rate(const struct mp4 *f)
816 return f->audio_track->sample_rate;
819 uint32_t mp4_get_channel_count(const struct mp4 *f)
821 return f->audio_track->channel_count;
824 int32_t mp4_num_samples(const struct mp4 *f)
826 const struct mp4_track *t = f->audio_track;
830 for (i = 0; i < t->stts_entry_count; i++)
831 total += t->stts_sample_count[i];
835 int mp4_open_meta(const struct mp4_callback *cb, struct mp4 **result)
838 int ret = open_file(cb, true, &f);
842 if (f->udta_size == 0 || f->meta_size == 0 || f->ilst_size == 0) {
845 return -E_MP4_MISSING_ATOM;
852 * Return the metadata of an mp4 file.
854 * \param f As returned by either \ref mp4_open_read() or \ref mp4_open_meta().
856 * The caller is allowed to add, delete or modify the entries of the returned
857 * structure in order to pass the modified version to \ref mp4_meta_update().
859 struct mp4_metadata *mp4_get_meta(struct mp4 *f)
864 /** Total length of an on-disk metadata tag. */
865 #define TAG_LEN(_len) (24 + (_len))
866 static void create_ilst(const struct mp4_metadata *meta, uint8_t *out)
868 for (unsigned n = 0; n < meta->count; n++) {
869 struct mp4_tag *tag = meta->tags + n;
870 unsigned len = strlen(tag->value);
871 const char *atom_name;
873 if (!strcasecmp(tag->item, "title"))
874 atom_name = "\xA9" "nam";
875 else if (!strcasecmp(tag->item, "artist"))
876 atom_name = "\xA9" "ART";
877 else if (!strcasecmp(tag->item, "album"))
878 atom_name = "\xA9" "alb";
879 else if (!strcasecmp(tag->item, "date"))
880 atom_name = "\xA9" "day";
881 else if (!strcasecmp(tag->item, "comment"))
882 atom_name = "\xA9" "cmt";
885 write_u32_be(out, TAG_LEN(len));
886 memcpy(out + 4, atom_name, 4);
887 write_u32_be(out + 8, 8 /* data atom header */
888 + 8 /* flags + reserved */
890 memcpy(out + 12, "data", 4);
891 write_u32_be(out + 16, 1); /* flags */
892 write_u32_be(out + 20, 0); /* reserved */
893 memcpy(out + 24, tag->value, len);
898 static void *modify_moov(struct mp4 *f, uint32_t *out_size)
901 uint64_t total_base = f->moov_offset + 8;
902 uint32_t total_size = (uint32_t) (f->moov_size - 8);
903 uint32_t new_ilst_size = 0;
909 for (unsigned n = 0; n < f->meta.count; n++)
910 new_ilst_size += TAG_LEN(strlen(f->meta.tags[n].value));
911 size_delta = new_ilst_size - (f->ilst_size - 8);
912 *out_size = total_size + size_delta;
913 out_buffer = para_malloc(*out_size);
915 set_position(f, total_base);
916 ret = read_data(f, p_out, f->udta_offset - total_base);
919 p_out += f->udta_offset - total_base;
920 ret = read_int32(f, &tmp);
923 write_u32_be(p_out, tmp + size_delta);
925 ret = read_data(f, p_out, 4);
929 ret = read_data(f, p_out, f->meta_offset - f->udta_offset - 8);
932 p_out += f->meta_offset - f->udta_offset - 8;
933 ret = read_int32(f, &tmp);
936 write_u32_be(p_out, tmp + size_delta);
938 ret = read_data(f, p_out, 4);
942 ret = read_data(f, p_out, f->ilst_offset - f->meta_offset - 8);
945 p_out += f->ilst_offset - f->meta_offset - 8;
946 ret = read_int32(f, &tmp);
949 write_u32_be(p_out, tmp + size_delta);
951 ret = read_data(f, p_out, 4);
955 create_ilst(&f->meta, p_out);
956 p_out += new_ilst_size;
957 set_position(f, f->ilst_offset + f->ilst_size);
958 ret = read_data(f, p_out, total_size - (f->ilst_offset - total_base)
965 static int write_data(struct mp4 *f, void *data, size_t size)
968 ssize_t ret = f->cb->write(f->cb->user_data, data, size);
972 return -ERRNO_TO_PARA_ERROR(errno);
979 int mp4_meta_update(struct mp4 *f)
982 uint32_t new_moov_size;
983 uint8_t buf[8] = "----moov";
987 new_moov_data = modify_moov(f, &new_moov_size);
988 if (!new_moov_data ) {
992 if (f->last_atom != ATOM_MOOV) {
993 set_position(f, f->moov_offset + 4);
994 ret = write_data(f, "free", 4); /* rename old moov to free */
997 /* write new moov atom at EOF */
998 f->cb->seek(f->cb->user_data, 0, SEEK_END);
999 } else /* overwrite old moov atom */
1000 set_position(f, f->moov_offset);
1001 write_u32_be(buf, new_moov_size + 8);
1002 ret = write_data(f, buf, sizeof(buf));
1005 ret = write_data(f, new_moov_data, new_moov_size);
1008 f->cb->truncate(f->cb->user_data);
1011 free(new_moov_data);
1015 static char *meta_find_by_name(const struct mp4 *f, const char *item)
1019 for (i = 0; i < f->meta.count; i++)
1020 if (!strcasecmp(f->meta.tags[i].item, item))
1021 return para_strdup(f->meta.tags[i].value);
1026 * Return the value of the artist meta tag of an mp4 file.
1028 * \param f Must not be NULL.
1030 * \return If the file does not contain this metadata tag, the function returns
1031 * NULL. Otherwise, a copy of the tag value is returned. The caller should free
1032 * this memory when it is no longer needed.
1034 char *mp4_meta_get_artist(const struct mp4 *f)
1036 return meta_find_by_name(f, "artist");
1040 * Return the value of the title meta tag of an mp4 file.
1042 * \param f See \ref mp4_meta_get_artist().
1043 * \return See \ref mp4_meta_get_artist().
1045 char *mp4_meta_get_title(const struct mp4 *f)
1047 return meta_find_by_name(f, "title");
1051 * Return the value of the date meta tag of an mp4 file.
1053 * \param f See \ref mp4_meta_get_artist().
1054 * \return See \ref mp4_meta_get_artist().
1056 char *mp4_meta_get_date(const struct mp4 *f)
1058 return meta_find_by_name(f, "date");
1062 * Return the value of the album meta tag of an mp4 file.
1064 * \param f See \ref mp4_meta_get_artist().
1065 * \return See \ref mp4_meta_get_artist().
1067 char *mp4_meta_get_album(const struct mp4 *f)
1069 return meta_find_by_name(f, "album");
1073 * Return the value of the comment meta tag of an mp4 file.
1075 * \param f See \ref mp4_meta_get_artist().
1076 * \return See \ref mp4_meta_get_artist().
1078 char *mp4_meta_get_comment(const struct mp4 *f)
1080 return meta_find_by_name(f, "comment");