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;
48 int64_t current_position;
62 /* incremental track index while reading the file */
65 struct mp4_track *track[MAX_TRACKS];
66 /* the first audio track found */
67 struct mp4_track *audio_track;
70 struct mp4_metadata meta;
74 * Returns -1, 0, or 1 on errors/EOF/success. Partial reads followed by EOF or
75 * read errors are treated as errors.
77 static int read_data(struct mp4 *f, void *data, size_t size)
80 ssize_t ret = f->cb->read(f->cb->user_data, data, size);
81 if (ret < 0 && errno == EINTR)
83 /* regard EAGAIN as an error as reads should be blocking. */
85 return ret < 0? -1 : 0;
86 f->current_position += ret;
92 static int read_int64(struct mp4 *f, uint64_t *result)
95 int ret = read_data(f, data, 8);
97 if (ret > 0 && result)
98 *result = read_u64_be(data);
102 static int read_int32(struct mp4 *f, uint32_t *result)
105 int ret = read_data(f, data, 4);
107 if (ret > 0 && result)
108 *result = read_u32_be(data);
112 static int read_int24(struct mp4 *f, uint32_t *result)
115 int ret = read_data(f, data, 3);
117 if (ret > 0 && result)
118 *result = read_u24_be(data);
122 static int read_int16(struct mp4 *f, uint16_t *result)
125 int ret = read_data(f, data, 2);
127 if (ret > 0 && result)
128 *result = read_u16_be(data);
132 static uint8_t read_int8(struct mp4 *f, uint8_t *result)
135 int ret = read_data(f, data, 1);
137 if (ret > 0 && result)
143 ATOM_ITEM(MOOV, 'm', 'o', 'o', 'v') \
144 ATOM_ITEM(TRAK, 't', 'r', 'a', 'k') \
145 ATOM_ITEM(MDIA, 'm', 'd', 'i', 'a') \
146 ATOM_ITEM(MINF, 'm', 'i', 'n', 'f') \
147 ATOM_ITEM(STBL, 's', 't', 'b', 'l') \
148 ATOM_ITEM(UDTA, 'u', 'd', 't', 'a') \
149 ATOM_ITEM(ILST, 'i', 'l', 's', 't') /* iTunes Metadata list */ \
150 ATOM_ITEM(ARTIST, 0xa9, 'A', 'R', 'T') \
151 ATOM_ITEM(TITLE, 0xa9, 'n', 'a', 'm') \
152 ATOM_ITEM(ALBUM, 0xa9, 'a', 'l', 'b') \
153 ATOM_ITEM(DATE, 0xa9, 'd', 'a', 'y') \
154 ATOM_ITEM(COMMENT, 0xa9, 'c', 'm', 't') \
155 ATOM_ITEM(MDHD, 'm', 'd', 'h', 'd') /* track header */ \
156 ATOM_ITEM(STSD, 's', 't', 's', 'd') /* sample description box */ \
157 ATOM_ITEM(STTS, 's', 't', 't', 's') /* time to sample box */ \
158 ATOM_ITEM(STSZ, 's', 't', 's', 'z') /* sample size box */ \
159 ATOM_ITEM(STCO, 's', 't', 'c', 'o') /* chunk offset box */ \
160 ATOM_ITEM(STSC, 's', 't', 's', 'c') /* sample to chunk box */ \
161 ATOM_ITEM(MP4A, 'm', 'p', '4', 'a') \
162 ATOM_ITEM(META, 'm', 'e', 't', 'a') /* iTunes Metadata box */ \
163 ATOM_ITEM(DATA, 'd', 'a', 't', 'a') /* iTunes Metadata data box */ \
165 #define ATOM_ITEM(_name, a, b, c, d) ATOM_ ## _name,
166 enum atom {ATOM_ITEMS};
169 static uint8_t atom_name_to_type(uint8_t *p)
171 #define ATOM_VALUE(a, b, c, d) ((a << 24) + (b << 16) + (c << 8) + d)
172 #define ATOM_ITEM(_name, a, b, c, d) \
173 {.name = # _name, .val = ATOM_VALUE(a, b, c, d)},
174 static const struct {
177 } atom_table[] = {ATOM_ITEMS};
179 uint32_t val = read_u32_be(p);
181 for (uint8_t n = 0; n < ARRAY_SIZE(atom_table); n++)
182 if (val == atom_table[n].val)
187 /* read atom header, atom size is returned with header included. */
188 static int atom_read_header(struct mp4 *f, uint8_t *atom_type,
189 uint8_t *header_size, uint64_t *atom_size)
193 uint8_t atom_header[8];
195 ret = read_data(f, atom_header, 8);
198 size = read_u32_be(atom_header);
199 if (size == 1) { /* 64 bit atom size */
202 ret = read_int64(f, atom_size);
211 *atom_type = atom_name_to_type(atom_header + 4);
215 static int64_t get_position(const struct mp4 *f)
217 return f->current_position;
220 static int32_t set_position(struct mp4 *f, int64_t position)
222 f->cb->seek(f->cb->user_data, position);
223 f->current_position = position;
228 static int read_stsz(struct mp4 *f)
234 if (f->total_tracks == 0)
236 t = f->track[f->total_tracks - 1];
237 ret = read_int8(f, NULL); /* version */
240 ret = read_int24(f, NULL); /* flags */
243 ret = read_int32(f, &t->stsz_sample_size);
246 ret = read_int32(f, &t->stsz_sample_count);
249 if (t->stsz_sample_size != 0)
251 t->stsz_table = para_malloc(t->stsz_sample_count * sizeof(int32_t));
252 for (i = 0; i < t->stsz_sample_count; i++) {
253 ret = read_int32(f, &t->stsz_table[i]);
260 static int read_stts(struct mp4 *f)
266 if (f->total_tracks == 0)
268 t = f->track[f->total_tracks - 1];
269 if (t->stts_entry_count)
271 ret = read_int8(f, NULL); /* version */
274 ret = read_int24(f, NULL); /* flags */
277 ret = read_int32(f, &t->stts_entry_count);
280 t->stts_sample_count = para_malloc(t->stts_entry_count
282 for (i = 0; i < t->stts_entry_count; i++) {
283 ret = read_int32(f, &t->stts_sample_count[i]);
286 ret = read_int32(f, NULL); /* sample delta */
293 static int read_stsc(struct mp4 *f)
299 if (f->total_tracks == 0)
301 t = f->track[f->total_tracks - 1];
303 ret = read_int8(f, NULL); /* version */
306 ret = read_int24(f, NULL); /* flags */
309 ret = read_int32(f, &t->stsc_entry_count);
312 t->stsc_first_chunk = para_malloc(t->stsc_entry_count * sizeof(int32_t));
313 t->stsc_samples_per_chunk = para_malloc(t->stsc_entry_count
315 for (i = 0; i < t->stsc_entry_count; i++) {
316 ret = read_int32(f, &t->stsc_first_chunk[i]);
319 ret = read_int32(f, &t->stsc_samples_per_chunk[i]);
322 ret = read_int32(f, NULL); /* sample desc index */
329 static int read_stco(struct mp4 *f)
335 if (f->total_tracks == 0)
337 t = f->track[f->total_tracks - 1];
339 ret = read_int8(f, NULL); /* version */
342 ret = read_int24(f, NULL); /* flags */
345 ret = read_int32(f, &t->stco_entry_count);
348 t->stco_chunk_offset = para_malloc(t->stco_entry_count
350 for (i = 0; i < t->stco_entry_count; i++) {
351 ret = read_int32(f, &t->stco_chunk_offset[i]);
358 static int read_mp4a(struct mp4 *f)
364 if (f->total_tracks == 0)
366 t = f->track[f->total_tracks - 1];
368 for (i = 0; i < 6; i++) {
369 ret = read_int8(f, NULL); /* reserved */
373 ret = read_int16(f, NULL); /* data_reference_index */
376 ret = read_int32(f, NULL); /* reserved */
379 ret = read_int32(f, NULL); /* reserved */
382 ret = read_int16(f, &t->channel_count);
385 ret = read_int16(f, NULL);
388 ret = read_int16(f, NULL);
391 ret = read_int16(f, NULL);
394 return read_int16(f, &t->sample_rate);
397 static int read_stsd(struct mp4 *f)
400 uint32_t i, entry_count;
403 if (f->total_tracks == 0)
405 t = f->track[f->total_tracks - 1];
406 ret = read_int8(f, NULL); /* version */
409 ret = read_int24(f, NULL); /* flags */
412 ret = read_int32(f, &entry_count);
415 for (i = 0; i < entry_count; i++) {
416 uint64_t skip = get_position(f);
418 uint8_t atom_type = 0;
419 ret = atom_read_header(f, &atom_type, NULL, &size);
423 if (!f->audio_track && atom_type == ATOM_MP4A) {
427 set_position(f, skip);
432 static const char *get_metadata_name(uint8_t atom_type)
435 case ATOM_TITLE: return "title";
436 case ATOM_ARTIST: return "artist";
437 case ATOM_ALBUM: return "album";
438 case ATOM_DATE: return "date";
439 case ATOM_COMMENT: return "comment";
440 default: return "unknown";
444 static int parse_tag(struct mp4 *f, uint8_t parent, int32_t size)
447 uint64_t subsize, sumsize;
456 set_position(f, destpos), sumsize += subsize
459 uint8_t header_size = 0;
460 ret = atom_read_header(f, &atom_type, &header_size, &subsize);
463 destpos = get_position(f) + subsize - header_size;
464 if (atom_type != ATOM_DATA)
466 ret = read_int8(f, NULL); /* version */
469 ret = read_int24(f, NULL); /* flags */
472 ret = read_int32(f, NULL); /* reserved */
475 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
476 if (subsize < header_size + 8 || subsize > UINT_MAX)
478 len = subsize - (header_size + 8);
480 value = para_malloc(len + 1);
481 ret = read_data(f, value, len);
487 return -ERRNO_TO_PARA_ERROR(EINVAL);
488 f->meta.tags = para_realloc(f->meta.tags, (f->meta.count + 1)
489 * sizeof(struct mp4_tag));
490 tag = f->meta.tags + f->meta.count;
491 tag->item = para_strdup(get_metadata_name(parent));
501 static int read_mdhd(struct mp4 *f)
507 if (f->total_tracks == 0)
509 t = f->track[f->total_tracks - 1];
511 ret = read_int32(f, &version);
515 ret = read_int64(f, NULL); /* creation-time */
518 ret = read_int64(f, NULL); /* modification-time */
521 ret = read_int32(f, &t->time_scale);
524 ret = read_int64(f, &t->duration);
527 } else { //version == 0
530 ret = read_int32(f, NULL); /* creation-time */
533 ret = read_int32(f, NULL); /* modification-time */
536 ret = read_int32(f, &t->time_scale);
539 ret = read_int32(f, &temp);
542 t->duration = (temp == (uint32_t) (-1))?
543 (uint64_t) (-1) : (uint64_t) (temp);
545 ret = read_int16(f, NULL);
548 ret = read_int16(f, NULL);
554 static int32_t read_ilst(struct mp4 *f, int32_t size)
557 uint64_t sumsize = 0;
559 while (sumsize < size) {
561 uint64_t subsize, destpos;
562 uint8_t header_size = 0;
563 ret = atom_read_header(f, &atom_type, &header_size, &subsize);
566 destpos = get_position(f) + subsize - header_size;
573 ret = parse_tag(f, atom_type, subsize - header_size);
577 set_position(f, destpos);
583 static int32_t read_meta(struct mp4 *f, uint64_t size)
586 uint64_t subsize, sumsize = 0;
588 uint8_t header_size = 0;
590 ret = read_int8(f, NULL); /* version */
593 ret = read_int24(f, NULL); /* flags */
596 while (sumsize < (size - (header_size + 4))) {
597 ret = atom_read_header(f, &atom_type, &header_size, &subsize);
600 if (subsize <= header_size + 4)
602 if (atom_type == ATOM_ILST) {
603 f->ilst_offset = get_position(f) - header_size;
604 f->ilst_size = subsize;
605 ret = read_ilst(f, subsize - (header_size + 4));
609 set_position(f, get_position(f) + subsize - header_size);
615 static bool need_atom(uint8_t atom_type, bool meta_only)
617 /* these are needed in any case */
628 /* meta-only opens don't need anything else */
631 /* these are only required for regular opens */
643 /* parse atoms that are sub atoms of other atoms */
644 static int parse_sub_atoms(struct mp4 *f, uint64_t total_size, bool meta_only)
647 uint64_t dest, size, end = get_position(f) + total_size;
649 for (dest = get_position(f); dest < end; set_position(f, dest)) {
650 uint8_t header_size, atom_type;
651 ret = atom_read_header(f, &atom_type, &header_size, &size);
656 dest = get_position(f) + size - header_size;
657 if (atom_type == ATOM_TRAK) {
658 if (f->total_tracks >= MAX_TRACKS)
661 f->track[f->total_tracks - 1] = para_calloc(
662 sizeof(struct mp4_track));
663 } else if (atom_type == ATOM_UDTA) {
664 f->udta_offset = get_position(f) - header_size;
667 if (!need_atom(atom_type, meta_only))
670 case ATOM_STSZ: ret = read_stsz(f); break;
671 case ATOM_STTS: ret = read_stts(f); break;
672 case ATOM_STSC: ret = read_stsc(f); break;
673 case ATOM_STCO: ret = read_stco(f); break;
674 case ATOM_STSD: ret = read_stsd(f); break;
675 case ATOM_MDHD: ret = read_mdhd(f); break;
677 f->meta_offset = get_position(f) - header_size;
679 ret = read_meta(f, size);
682 ret = parse_sub_atoms(f, size - header_size, meta_only);
690 static int open_file(const struct mp4_callback *cb, bool meta_only, struct mp4 **result)
694 uint8_t atom_type, header_size;
695 struct mp4 *f = para_calloc(sizeof(*f));
698 while ((ret = atom_read_header(f, &atom_type, &header_size, &size)) > 0) {
699 f->file_size += size;
700 f->last_atom = atom_type;
701 if (atom_type != ATOM_MOOV || size <= header_size) { /* skip */
702 set_position(f, get_position(f) + size - header_size);
705 f->moov_offset = get_position(f) - header_size;
707 ret = parse_sub_atoms(f, size - header_size, meta_only);
726 int mp4_open_read(const struct mp4_callback *cb, struct mp4 **result)
728 return open_file(cb, false, result);
731 void mp4_close(struct mp4 *f)
735 for (i = 0; i < f->total_tracks; i++) {
737 free(f->track[i]->stsz_table);
738 free(f->track[i]->stts_sample_count);
739 free(f->track[i]->stsc_first_chunk);
740 free(f->track[i]->stsc_samples_per_chunk);
741 free(f->track[i]->stco_chunk_offset);
745 for (i = 0; i < f->meta.count; i++) {
746 free(f->meta.tags[i].item);
747 free(f->meta.tags[i].value);
753 static int32_t chunk_of_sample(const struct mp4 *f, int32_t sample,
756 const struct mp4_track *t = f->audio_track;
757 uint32_t *fc = t->stsc_first_chunk, *spc = t->stsc_samples_per_chunk;
758 int32_t chunk1, chunk1samples, n, total, i;
760 for (i = 1, total = 0; i < t->stsc_entry_count; i++, total += n) {
761 n = (fc[i] - fc[i - 1]) * spc[i - 1]; /* number of samples */
762 if (sample < total + n)
766 chunk1samples = spc[i - 1];
767 if (chunk1samples != 0)
768 *chunk = (sample - total) / chunk1samples + chunk1;
771 return total + (*chunk - chunk1) * chunk1samples;
775 * Return the number of milliseconds of the audio track.
777 * \param f As returned by \ref mp4_open_read(), must not be NULL.
779 uint64_t mp4_get_duration(const struct mp4 *f)
781 const struct mp4_track *t = f->audio_track;
783 if (t->time_scale == 0)
785 return t->duration * 1000 / t->time_scale;
788 int mp4_set_sample_position(struct mp4 *f, int32_t sample)
790 const struct mp4_track *t = f->audio_track;
791 int32_t offset, chunk, chunk_sample;
792 uint32_t n, srs; /* sample range size */
794 if (sample >= t->stsz_sample_count)
795 return -ERRNO_TO_PARA_ERROR(EINVAL);
796 chunk_sample = chunk_of_sample(f, sample, &chunk);
797 if (t->stsz_sample_size > 0)
798 srs = (sample - chunk_sample) * t->stsz_sample_size;
800 for (srs = 0, n = chunk_sample; n < sample; n++)
801 srs += t->stsz_table[n];
803 if (t->stco_entry_count > 0 && chunk > t->stco_entry_count)
804 offset = t->stco_chunk_offset[t->stco_entry_count - 1];
805 else if (t->stco_entry_count > 0)
806 offset = t->stco_chunk_offset[chunk - 1];
809 set_position(f, offset + srs);
813 int32_t mp4_get_sample_size(const struct mp4 *f, int sample)
815 const struct mp4_track *t = f->audio_track;
817 if (t->stsz_sample_size != 0)
818 return t->stsz_sample_size;
819 return t->stsz_table[sample];
822 uint32_t mp4_get_sample_rate(const struct mp4 *f)
824 return f->audio_track->sample_rate;
827 uint32_t mp4_get_channel_count(const struct mp4 *f)
829 return f->audio_track->channel_count;
832 int32_t mp4_num_samples(const struct mp4 *f)
834 const struct mp4_track *t = f->audio_track;
838 for (i = 0; i < t->stts_entry_count; i++)
839 total += t->stts_sample_count[i];
843 int mp4_open_meta(const struct mp4_callback *cb, struct mp4 **result)
846 int ret = open_file(cb, true, &f);
850 if (f->udta_size == 0 || f->meta_size == 0 || f->ilst_size == 0) {
853 return -E_MP4_MISSING_ATOM;
860 * Return the metadata of an mp4 file.
862 * \param f As returned by either \ref mp4_open_read() or \ref mp4_open_meta().
864 * The caller is allowed to add, delete or modify the entries of the returned
865 * structure in order to pass the modified version to \ref mp4_meta_update().
867 struct mp4_metadata *mp4_get_meta(struct mp4 *f)
872 /** Total length of an on-disk metadata tag. */
873 #define TAG_LEN(_len) (24 + (_len))
874 static void create_ilst(const struct mp4_metadata *meta, uint8_t *out)
876 for (unsigned n = 0; n < meta->count; n++) {
877 struct mp4_tag *tag = meta->tags + n;
878 unsigned len = strlen(tag->value);
879 const char *atom_name;
881 if (!strcasecmp(tag->item, "title"))
882 atom_name = "\xA9" "nam";
883 else if (!strcasecmp(tag->item, "artist"))
884 atom_name = "\xA9" "ART";
885 else if (!strcasecmp(tag->item, "album"))
886 atom_name = "\xA9" "alb";
887 else if (!strcasecmp(tag->item, "date"))
888 atom_name = "\xA9" "day";
889 else if (!strcasecmp(tag->item, "comment"))
890 atom_name = "\xA9" "cmt";
893 write_u32_be(out, TAG_LEN(len));
894 memcpy(out + 4, atom_name, 4);
895 write_u32_be(out + 8, 8 /* data atom header */
896 + 8 /* flags + reserved */
898 memcpy(out + 12, "data", 4);
899 write_u32_be(out + 16, 1); /* flags */
900 write_u32_be(out + 20, 0); /* reserved */
901 memcpy(out + 24, tag->value, len);
906 static void *modify_moov(struct mp4 *f, uint32_t *out_size)
909 uint64_t total_base = f->moov_offset + 8;
910 uint32_t total_size = (uint32_t) (f->moov_size - 8);
911 uint32_t new_ilst_size = 0;
917 for (unsigned n = 0; n < f->meta.count; n++)
918 new_ilst_size += TAG_LEN(strlen(f->meta.tags[n].value));
919 size_delta = new_ilst_size - (f->ilst_size - 8);
920 *out_size = total_size + size_delta;
921 out_buffer = para_malloc(*out_size);
923 set_position(f, total_base);
924 ret = read_data(f, p_out, f->udta_offset - total_base);
927 p_out += f->udta_offset - total_base;
928 ret = read_int32(f, &tmp);
931 write_u32_be(p_out, tmp + size_delta);
933 ret = read_data(f, p_out, 4);
937 ret = read_data(f, p_out, f->meta_offset - f->udta_offset - 8);
940 p_out += f->meta_offset - f->udta_offset - 8;
941 ret = read_int32(f, &tmp);
944 write_u32_be(p_out, tmp + size_delta);
946 ret = read_data(f, p_out, 4);
950 ret = read_data(f, p_out, f->ilst_offset - f->meta_offset - 8);
953 p_out += f->ilst_offset - f->meta_offset - 8;
954 ret = read_int32(f, &tmp);
957 write_u32_be(p_out, tmp + size_delta);
959 ret = read_data(f, p_out, 4);
963 create_ilst(&f->meta, p_out);
964 p_out += new_ilst_size;
965 set_position(f, f->ilst_offset + f->ilst_size);
966 ret = read_data(f, p_out, total_size - (f->ilst_offset - total_base)
973 static int32_t write_data(struct mp4 *f, void *data, uint32_t size)
977 result = f->cb->write(f->cb->user_data, data, size);
979 f->current_position += size;
984 static int32_t write_int32(struct mp4 *f, uint32_t data)
987 write_u32_be(temp, data);
988 return write_data(f, temp, sizeof(temp));
991 int32_t mp4_meta_update(struct mp4 *f)
994 uint32_t new_moov_size;
997 new_moov_data = modify_moov(f, &new_moov_size);
998 if (!new_moov_data ) {
1002 /* copy moov atom to end of the file */
1003 if (f->last_atom != ATOM_MOOV) {
1004 char *free_data = "free";
1006 /* rename old moov to free */
1007 set_position(f, f->moov_offset + 4);
1008 write_data(f, free_data, 4);
1010 set_position(f, f->file_size);
1011 write_int32(f, new_moov_size + 8);
1012 write_data(f, "moov", 4);
1013 write_data(f, new_moov_data, new_moov_size);
1015 set_position(f, f->moov_offset);
1016 write_int32(f, new_moov_size + 8);
1017 write_data(f, "moov", 4);
1018 write_data(f, new_moov_data, new_moov_size);
1020 free(new_moov_data);
1021 f->cb->truncate(f->cb->user_data);
1025 static char *meta_find_by_name(const struct mp4 *f, const char *item)
1029 for (i = 0; i < f->meta.count; i++)
1030 if (!strcasecmp(f->meta.tags[i].item, item))
1031 return para_strdup(f->meta.tags[i].value);
1036 * Return the value of the artist meta tag of an mp4 file.
1038 * \param f Must not be NULL.
1040 * \return If the file does not contain this metadata tag, the function returns
1041 * NULL. Otherwise, a copy of the tag value is returned. The caller should free
1042 * this memory when it is no longer needed.
1044 char *mp4_meta_get_artist(const struct mp4 *f)
1046 return meta_find_by_name(f, "artist");
1050 * Return the value of the title meta tag of an mp4 file.
1052 * \param f See \ref mp4_meta_get_artist().
1053 * \return See \ref mp4_meta_get_artist().
1055 char *mp4_meta_get_title(const struct mp4 *f)
1057 return meta_find_by_name(f, "title");
1061 * Return the value of the date meta tag of an mp4 file.
1063 * \param f See \ref mp4_meta_get_artist().
1064 * \return See \ref mp4_meta_get_artist().
1066 char *mp4_meta_get_date(const struct mp4 *f)
1068 return meta_find_by_name(f, "date");
1072 * Return the value of the album meta tag of an mp4 file.
1074 * \param f See \ref mp4_meta_get_artist().
1075 * \return See \ref mp4_meta_get_artist().
1077 char *mp4_meta_get_album(const struct mp4 *f)
1079 return meta_find_by_name(f, "album");
1083 * Return the value of the comment meta tag of an mp4 file.
1085 * \param f See \ref mp4_meta_get_artist().
1086 * \return See \ref mp4_meta_get_artist().
1088 char *mp4_meta_get_comment(const struct mp4 *f)
1090 return meta_find_by_name(f, "comment");