2 * Copyright (C) 2003-2005 M. Bakker, Nero AG, http://www.nero.com
3 * FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
11 #include "portable_io.h"
17 uint16_t channelCount;
21 uint32_t stsz_sample_size;
22 uint32_t stsz_sample_count;
26 uint32_t stts_entry_count;
27 uint32_t *stts_sample_count;
28 uint32_t *stts_sample_delta;
31 uint32_t stsc_entry_count;
32 uint32_t *stsc_first_chunk;
33 uint32_t *stsc_samples_per_chunk;
34 uint32_t *stsc_sample_desc_index;
37 uint32_t stco_entry_count;
38 uint32_t *stco_chunk_offset;
44 #define MAX_TRACKS 1024
47 const struct mp4_callback *cb;
48 int64_t current_position;
55 /* incremental track index while reading the file */
58 struct mp4_track *track[MAX_TRACKS];
61 struct mp4_metadata meta;
65 * Returns -1, 0, or 1 on errors/EOF/success. Partial reads followed by EOF or
66 * read errors are treated as errors.
68 static int read_data(struct mp4 *f, void *data, size_t size)
71 ssize_t ret = f->cb->read(f->cb->user_data, data, size);
72 if (ret < 0 && errno == EINTR)
74 /* regard EAGAIN as an error as reads should be blocking. */
76 return ret < 0? -1 : 0;
77 f->current_position += ret;
83 static int read_int64(struct mp4 *f, uint64_t *result)
86 int ret = read_data(f, data, 8);
88 if (ret > 0 && result)
89 *result = read_u64_be(data);
93 static int read_int32(struct mp4 *f, uint32_t *result)
96 int ret = read_data(f, data, 4);
98 if (ret > 0 && result)
99 *result = read_u32_be(data);
103 static int read_int24(struct mp4 *f, uint32_t *result)
106 int ret = read_data(f, data, 3);
108 if (ret > 0 && result)
109 *result = read_u24_be(data);
113 static int read_int16(struct mp4 *f, uint16_t *result)
116 int ret = read_data(f, data, 2);
118 if (ret > 0 && result)
119 *result = read_u16_be(data);
123 static uint8_t read_int8(struct mp4 *f, uint8_t *result)
126 int ret = read_data(f, data, 1);
128 if (ret > 0 && result)
133 static bool atom_compare(int8_t a1, int8_t b1, int8_t c1, int8_t d1,
134 int8_t a2, int8_t b2, int8_t c2, int8_t d2)
136 return a1 == a2 && b1 == b2 && c1 == c2 && d1 == d2;
140 /* atoms with subatoms */
148 ATOM_ILST = 8, /* iTunes Metadata list */
159 ATOM_COMPILATION = 19,
169 /* atoms without subatoms */
175 ATOM_MDHD = 134, /* track header */
179 ATOM_STSD = 138, /* sample description box */
180 ATOM_STTS = 139, /* time to sample box */
181 ATOM_STSZ = 140, /* sample size box */
183 ATOM_STCO = 142, /* chunk offset box */
184 ATOM_STSC = 143, /* sample to chunk box */
189 ATOM_META = 148, /* iTunes Metadata box */
190 ATOM_NAME = 149, /* iTunes Metadata name box */
191 ATOM_DATA = 150, /* iTunes Metadata data box */
198 ATOM_ALBUM_ARTIST = 157,
199 ATOM_CONTENTGROUP = 158,
201 ATOM_DESCRIPTION = 160,
204 ATOM_EPISODENAME = 163,
205 ATOM_SORTTITLE = 164,
206 ATOM_SORTALBUM = 165,
207 ATOM_SORTARTIST = 166,
208 ATOM_SORTALBUMARTIST = 167,
209 ATOM_SORTWRITER = 168,
218 #define ATOM_FREE ATOM_UNKNOWN
219 #define ATOM_SKIP ATOM_UNKNOWN
221 #define COPYRIGHT_SYMBOL ((int8_t)0xA9)
223 static uint8_t atom_name_to_type(int8_t a, int8_t b, int8_t c, int8_t d)
226 if (atom_compare(a, b, c, d, 'm', 'o', 'o', 'v'))
228 else if (atom_compare(a, b, c, d, 'm', 'i', 'n', 'f'))
230 else if (atom_compare(a, b, c, d, 'm', 'd', 'i', 'a'))
232 else if (atom_compare(a, b, c, d, 'm', 'd', 'a', 't'))
234 else if (atom_compare(a, b, c, d, 'm', 'd', 'h', 'd'))
236 else if (atom_compare(a, b, c, d, 'm', 'v', 'h', 'd'))
238 else if (atom_compare(a, b, c, d, 'm', 'p', '4', 'a'))
240 else if (atom_compare(a, b, c, d, 'm', 'p', '4', 'v'))
242 else if (atom_compare(a, b, c, d, 'm', 'p', '4', 's'))
244 else if (atom_compare(a, b, c, d, 'm', 'e', 't', 'a'))
246 } else if (a == 't') {
247 if (atom_compare(a, b, c, d, 't', 'r', 'a', 'k'))
249 else if (atom_compare(a, b, c, d, 't', 'k', 'h', 'd'))
251 else if (atom_compare(a, b, c, d, 't', 'r', 'e', 'f'))
253 else if (atom_compare(a, b, c, d, 't', 'r', 'k', 'n'))
255 else if (atom_compare(a, b, c, d, 't', 'm', 'p', 'o'))
257 else if (atom_compare(a, b, c, d, 't', 'v', 'n', 'n'))
259 else if (atom_compare(a, b, c, d, 't', 'v', 's', 'h'))
261 else if (atom_compare(a, b, c, d, 't', 'v', 'e', 'n'))
262 return ATOM_EPISODENAME;
263 else if (atom_compare(a, b, c, d, 't', 'v', 's', 'n'))
265 else if (atom_compare(a, b, c, d, 't', 'v', 'e', 's'))
267 } else if (a == 's') {
268 if (atom_compare(a, b, c, d, 's', 't', 'b', 'l'))
270 else if (atom_compare(a, b, c, d, 's', 'm', 'h', 'd'))
272 else if (atom_compare(a, b, c, d, 's', 't', 's', 'd'))
274 else if (atom_compare(a, b, c, d, 's', 't', 't', 's'))
276 else if (atom_compare(a, b, c, d, 's', 't', 'c', 'o'))
278 else if (atom_compare(a, b, c, d, 's', 't', 's', 'c'))
280 else if (atom_compare(a, b, c, d, 's', 't', 's', 'z'))
282 else if (atom_compare(a, b, c, d, 's', 't', 'z', '2'))
284 else if (atom_compare(a, b, c, d, 's', 'k', 'i', 'p'))
286 else if (atom_compare(a, b, c, d, 's', 'i', 'n', 'f'))
288 else if (atom_compare(a, b, c, d, 's', 'c', 'h', 'i'))
290 else if (atom_compare(a, b, c, d, 's', 'o', 'n', 'm'))
291 return ATOM_SORTTITLE;
292 else if (atom_compare(a, b, c, d, 's', 'o', 'a', 'l'))
293 return ATOM_SORTALBUM;
294 else if (atom_compare(a, b, c, d, 's', 'o', 'a', 'r'))
295 return ATOM_SORTARTIST;
296 else if (atom_compare(a, b, c, d, 's', 'o', 'a', 'a'))
297 return ATOM_SORTALBUMARTIST;
298 else if (atom_compare(a, b, c, d, 's', 'o', 'c', 'o'))
299 return ATOM_SORTWRITER;
300 else if (atom_compare(a, b, c, d, 's', 'o', 's', 'n'))
301 return ATOM_SORTSHOW;
302 } else if (a == COPYRIGHT_SYMBOL) {
303 if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'n', 'a', 'm'))
305 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'A', 'R', 'T'))
307 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'w', 'r', 't'))
309 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'a', 'l', 'b'))
311 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'd', 'a', 'y'))
313 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 't', 'o', 'o'))
315 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'c', 'm', 't'))
317 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'g', 'e', 'n'))
319 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'g', 'r', 'p'))
320 return ATOM_CONTENTGROUP;
321 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'l', 'y', 'r'))
325 if (atom_compare(a, b, c, d, 'e', 'd', 't', 's'))
327 else if (atom_compare(a, b, c, d, 'e', 's', 'd', 's'))
329 else if (atom_compare(a, b, c, d, 'f', 't', 'y', 'p'))
331 else if (atom_compare(a, b, c, d, 'f', 'r', 'e', 'e'))
333 else if (atom_compare(a, b, c, d, 'h', 'm', 'h', 'd'))
335 else if (atom_compare(a, b, c, d, 'v', 'm', 'h', 'd'))
337 else if (atom_compare(a, b, c, d, 'u', 'd', 't', 'a'))
339 else if (atom_compare(a, b, c, d, 'i', 'l', 's', 't'))
341 else if (atom_compare(a, b, c, d, 'n', 'a', 'm', 'e'))
343 else if (atom_compare(a, b, c, d, 'd', 'a', 't', 'a'))
345 else if (atom_compare(a, b, c, d, 'd', 'i', 's', 'k'))
347 else if (atom_compare(a, b, c, d, 'g', 'n', 'r', 'e'))
349 else if (atom_compare(a, b, c, d, 'c', 'o', 'v', 'r'))
351 else if (atom_compare(a, b, c, d, 'c', 'p', 'i', 'l'))
352 return ATOM_COMPILATION;
353 else if (atom_compare(a, b, c, d, 'c', 't', 't', 's'))
355 else if (atom_compare(a, b, c, d, 'd', 'r', 'm', 's'))
357 else if (atom_compare(a, b, c, d, 'f', 'r', 'm', 'a'))
359 else if (atom_compare(a, b, c, d, 'p', 'r', 'i', 'v'))
361 else if (atom_compare(a, b, c, d, 'i', 'v', 'i', 'v'))
363 else if (atom_compare(a, b, c, d, 'u', 's', 'e', 'r'))
365 else if (atom_compare(a, b, c, d, 'k', 'e', 'y', ' '))
367 else if (atom_compare(a, b, c, d, 'a', 'A', 'R', 'T'))
368 return ATOM_ALBUM_ARTIST;
369 else if (atom_compare(a, b, c, d, 'd', 'e', 's', 'c'))
370 return ATOM_DESCRIPTION;
371 else if (atom_compare(a, b, c, d, 'p', 'c', 's', 't'))
377 /* read atom header, atom size is returned with header included. */
378 static int atom_read_header(struct mp4 *f, uint8_t *atom_type,
379 uint8_t *header_size, uint64_t *atom_size)
383 int8_t atom_header[8];
385 ret = read_data(f, atom_header, 8);
388 size = read_u32_be(atom_header);
389 if (size == 1) { /* 64 bit atom size */
392 ret = read_int64(f, atom_size);
401 *atom_type = atom_name_to_type(atom_header[4], atom_header[5],
402 atom_header[6], atom_header[7]);
406 static int64_t get_position(const struct mp4 *f)
408 return f->current_position;
411 static int need_parse_when_meta_only(uint8_t atom_type)
432 static int32_t set_position(struct mp4 *f, int64_t position)
434 f->cb->seek(f->cb->user_data, position);
435 f->current_position = position;
440 static int read_stsz(struct mp4 *f)
446 if (f->total_tracks == 0)
448 t = f->track[f->total_tracks - 1];
449 ret = read_int8(f, NULL); /* version */
452 ret = read_int24(f, NULL); /* flags */
455 ret = read_int32(f, &t->stsz_sample_size);
458 ret = read_int32(f, &t->stsz_sample_count);
461 if (t->stsz_sample_size != 0)
463 t->stsz_table = para_malloc(t->stsz_sample_count * sizeof(int32_t));
464 for (i = 0; i < t->stsz_sample_count; i++) {
465 ret = read_int32(f, &t->stsz_table[i]);
472 static int read_stts(struct mp4 *f)
478 if (f->total_tracks == 0)
480 t = f->track[f->total_tracks - 1];
481 if (t->stts_entry_count)
483 ret = read_int8(f, NULL); /* version */
486 ret = read_int24(f, NULL); /* flags */
489 ret = read_int32(f, &t->stts_entry_count);
492 t->stts_sample_count = para_malloc(t->stts_entry_count
494 t->stts_sample_delta = para_malloc(t->stts_entry_count
496 for (i = 0; i < t->stts_entry_count; i++) {
497 ret = read_int32(f, &t->stts_sample_count[i]);
500 ret = read_int32(f, &t->stts_sample_delta[i]);
507 static int read_stsc(struct mp4 *f)
513 if (f->total_tracks == 0)
515 t = f->track[f->total_tracks - 1];
517 ret = read_int8(f, NULL); /* version */
520 ret = read_int24(f, NULL); /* flags */
523 ret = read_int32(f, &t->stsc_entry_count);
526 t->stsc_first_chunk = para_malloc(t->stsc_entry_count * sizeof(int32_t));
527 t->stsc_samples_per_chunk = para_malloc(t->stsc_entry_count
529 t->stsc_sample_desc_index = para_malloc(t->stsc_entry_count *
532 for (i = 0; i < t->stsc_entry_count; i++) {
533 ret = read_int32(f, &t->stsc_first_chunk[i]);
536 ret = read_int32(f, &t->stsc_samples_per_chunk[i]);
539 ret = read_int32(f, &t->stsc_sample_desc_index[i]);
546 static int read_stco(struct mp4 *f)
552 if (f->total_tracks == 0)
554 t = f->track[f->total_tracks - 1];
556 ret = read_int8(f, NULL); /* version */
559 ret = read_int24(f, NULL); /* flags */
562 ret = read_int32(f, &t->stco_entry_count);
565 t->stco_chunk_offset = para_malloc(t->stco_entry_count
567 for (i = 0; i < t->stco_entry_count; i++) {
568 ret = read_int32(f, &t->stco_chunk_offset[i]);
575 static int read_mp4a(struct mp4 *f)
579 uint8_t atom_type = 0;
580 uint8_t header_size = 0;
583 if (f->total_tracks == 0)
585 t = f->track[f->total_tracks - 1];
587 for (i = 0; i < 6; i++) {
588 ret = read_int8(f, NULL); /* reserved */
592 ret = read_int16(f, NULL); /* data_reference_index */
595 ret = read_int32(f, NULL); /* reserved */
598 ret = read_int32(f, NULL); /* reserved */
601 ret = read_int16(f, &t->channelCount);
604 ret = read_int16(f, NULL);
607 ret = read_int16(f, NULL);
610 ret = read_int16(f, NULL);
613 ret = read_int16(f, &t->sampleRate);
616 ret = read_int16(f, NULL);
619 return atom_read_header(f, &atom_type, &header_size, NULL);
622 static int read_stsd(struct mp4 *f)
625 uint32_t i, entry_count;
626 uint8_t header_size = 0;
629 if (f->total_tracks == 0)
631 t = f->track[f->total_tracks - 1];
632 ret = read_int8(f, NULL); /* version */
635 ret = read_int24(f, NULL); /* flags */
638 ret = read_int32(f, &entry_count);
641 for (i = 0; i < entry_count; i++) {
642 uint64_t skip = get_position(f);
644 uint8_t atom_type = 0;
645 ret = atom_read_header(f, &atom_type, &header_size, &size);
649 t->is_audio = atom_type == ATOM_MP4A;
652 set_position(f, skip);
657 static int32_t tag_add_field(struct mp4_metadata *meta, const char *item,
658 const char *value, int32_t len)
660 meta->tags = para_realloc(meta->tags,
661 (meta->count + 1) * sizeof(struct mp4_tag));
662 meta->tags[meta->count].item = para_strdup(item);
663 meta->tags[meta->count].len = len;
665 meta->tags[meta->count].value = para_malloc(len + 1);
666 memcpy(meta->tags[meta->count].value, value, len);
667 meta->tags[meta->count].value[len] = 0;
669 meta->tags[meta->count].value = para_strdup(value);
675 static int read_string(struct mp4 *f, uint32_t length, char **result)
677 char *str = para_malloc(length + 1);
678 int ret = read_data(f, str, length);
690 static const char *get_metadata_name(uint8_t atom_type)
693 case ATOM_TITLE: return "title";
694 case ATOM_ARTIST: return "artist";
695 case ATOM_ALBUM: return "album";
696 case ATOM_DATE: return "date";
697 case ATOM_COMMENT: return "comment";
698 default: return "unknown";
702 static int parse_tag(struct mp4 *f, uint8_t parent, int32_t size)
705 uint64_t subsize, sumsize;
713 set_position(f, destpos), sumsize += subsize
716 uint8_t header_size = 0;
717 ret = atom_read_header(f, &atom_type, &header_size, &subsize);
720 destpos = get_position(f) + subsize - header_size;
721 if (atom_type != ATOM_DATA)
723 ret = read_int8(f, NULL); /* version */
726 ret = read_int24(f, NULL); /* flags */
729 ret = read_int32(f, NULL); /* reserved */
733 ret = read_string(f, subsize - (header_size + 8), &data);
736 len = subsize - (header_size + 8);
740 tag_add_field(&f->meta, get_metadata_name(parent), data, len);
745 static int read_mdhd(struct mp4 *f)
751 if (f->total_tracks == 0)
753 t = f->track[f->total_tracks - 1];
755 ret = read_int32(f, &version);
759 ret = read_int64(f, NULL); /* creation-time */
762 ret = read_int64(f, NULL); /* modification-time */
765 ret = read_int32(f, &t->timeScale);
768 ret = read_int64(f, &t->duration);
771 } else { //version == 0
774 ret = read_int32(f, NULL); /* creation-time */
777 ret = read_int32(f, NULL); /* modification-time */
780 ret = read_int32(f, &t->timeScale);
783 ret = read_int32(f, &temp);
786 t->duration = (temp == (uint32_t) (-1))?
787 (uint64_t) (-1) : (uint64_t) (temp);
789 ret = read_int16(f, NULL);
792 ret = read_int16(f, NULL);
798 static int32_t read_ilst(struct mp4 *f, int32_t size)
801 uint64_t sumsize = 0;
803 while (sumsize < size) {
805 uint64_t subsize, destpos;
806 uint8_t header_size = 0;
807 ret = atom_read_header(f, &atom_type, &header_size, &subsize);
810 destpos = get_position(f) + subsize - header_size;
817 parse_tag(f, atom_type, subsize - header_size);
819 set_position(f, destpos);
825 static int32_t read_meta(struct mp4 *f, uint64_t size)
828 uint64_t subsize, sumsize = 0;
830 uint8_t header_size = 0;
832 ret = read_int8(f, NULL); /* version */
835 ret = read_int24(f, NULL); /* flags */
838 while (sumsize < (size - (header_size + 4))) {
839 ret = atom_read_header(f, &atom_type, &header_size, &subsize);
842 if (subsize <= header_size + 4)
844 if (atom_type == ATOM_ILST)
845 read_ilst(f, subsize - (header_size + 4));
847 set_position(f, get_position(f) + subsize - header_size);
853 static int atom_read(struct mp4 *f, uint64_t size, uint8_t atom_type)
855 uint64_t dest_position = get_position(f) + size - 8;
856 int ret = 1; /* return success for atoms we don't care about */
859 case ATOM_STSZ: ret = read_stsz(f); break;
860 case ATOM_STTS: ret = read_stts(f); break;
861 case ATOM_STSC: ret = read_stsc(f); break;
862 case ATOM_STCO: ret = read_stco(f); break;
863 case ATOM_STSD: ret = read_stsd(f); break;
864 case ATOM_MDHD: ret = read_mdhd(f); break;
865 case ATOM_META: ret = read_meta(f, size); break;
867 set_position(f, dest_position);
871 /* parse atoms that are sub atoms of other atoms */
872 static int parse_sub_atoms(struct mp4 *f, uint64_t total_size, int meta_only)
876 uint8_t atom_type = 0;
877 uint64_t counted_size = 0;
878 uint8_t header_size = 0;
880 while (counted_size < total_size) {
881 ret = atom_read_header(f, &atom_type, &header_size, &size);
886 counted_size += size;
887 if (atom_type == ATOM_TRAK) {
888 if (f->total_tracks >= MAX_TRACKS)
891 f->track[f->total_tracks - 1] = para_calloc(
892 sizeof(struct mp4_track));
895 if (meta_only && !need_parse_when_meta_only(atom_type)) {
896 set_position(f, get_position(f) + size - header_size);
897 } else if (atom_type < SUBATOMIC) {
898 ret = parse_sub_atoms(f, size - header_size, meta_only);
902 ret = atom_read(f, size, atom_type);
910 /* parse root atoms */
911 static int parse_atoms(struct mp4 *f, int meta_only)
915 uint8_t atom_type = 0;
916 uint8_t header_size = 0;
920 while ((ret = atom_read_header(f, &atom_type, &header_size, &size)) > 0) {
921 f->file_size += size;
922 f->last_atom = atom_type;
924 if (atom_type == ATOM_MOOV && size > header_size) {
925 f->moov_offset = get_position(f) - header_size;
930 if (meta_only && !need_parse_when_meta_only(atom_type)) {
931 set_position(f, get_position(f) + size - header_size);
932 } else if (atom_type < SUBATOMIC) {
933 ret = parse_sub_atoms(f, size - header_size, meta_only);
938 set_position(f, get_position(f) + size - header_size);
944 struct mp4 *mp4_open_read(const struct mp4_callback *cb)
947 struct mp4 *f = para_calloc(sizeof(struct mp4));
950 ret = parse_atoms(f, 0);
958 void mp4_close(struct mp4 *f)
962 for (i = 0; i < f->total_tracks; i++) {
964 free(f->track[i]->stsz_table);
965 free(f->track[i]->stts_sample_count);
966 free(f->track[i]->stts_sample_delta);
967 free(f->track[i]->stsc_first_chunk);
968 free(f->track[i]->stsc_samples_per_chunk);
969 free(f->track[i]->stsc_sample_desc_index);
970 free(f->track[i]->stco_chunk_offset);
974 for (i = 0; i < f->meta.count; i++) {
975 free(f->meta.tags[i].item);
976 free(f->meta.tags[i].value);
982 static int32_t chunk_of_sample(const struct mp4 *f, int32_t track,
983 int32_t sample, int32_t *chunk_sample, int32_t *chunk)
985 int32_t total_entries = 0;
987 int32_t chunk1, chunk2, chunk1samples, range_samples, total = 0;
991 if (f->track[track] == NULL) {
995 total_entries = f->track[track]->stsc_entry_count;
1002 chunk2 = f->track[track]->stsc_first_chunk[chunk2entry];
1003 *chunk = chunk2 - chunk1;
1004 range_samples = *chunk * chunk1samples;
1006 if (sample < total + range_samples)
1009 chunk1samples = f->track[track]->stsc_samples_per_chunk[chunk2entry];
1012 if (chunk2entry < total_entries) {
1014 total += range_samples;
1016 } while (chunk2entry < total_entries);
1019 *chunk = (sample - total) / chunk1samples + chunk1;
1023 *chunk_sample = total + (*chunk - chunk1) * chunk1samples;
1028 static int32_t chunk_to_offset(const struct mp4 *f, int32_t track,
1031 const struct mp4_track *p_track = f->track[track];
1033 if (p_track->stco_entry_count && (chunk > p_track->stco_entry_count)) {
1034 return p_track->stco_chunk_offset[p_track->stco_entry_count -
1036 } else if (p_track->stco_entry_count) {
1037 return p_track->stco_chunk_offset[chunk - 1];
1045 static int32_t sample_range_size(const struct mp4 *f, int32_t track,
1046 int32_t chunk_sample, int32_t sample)
1049 const struct mp4_track *p_track = f->track[track];
1051 if (p_track->stsz_sample_size) {
1052 return (sample - chunk_sample) * p_track->stsz_sample_size;
1054 if (sample >= p_track->stsz_sample_count)
1057 for (i = chunk_sample, total = 0; i < sample; i++) {
1058 total += p_track->stsz_table[i];
1066 * Return the number of milliseconds of the given track.
1068 * \param f As returned by \ref mp4_open_read(), must not be NULL.
1069 * \param track Between zero and the value returned by \ref mp4_get_total_tracks().
1071 * The function returns zero if the audio file is of zero length or contains a
1072 * corrupt track header.
1074 uint64_t mp4_get_duration(const struct mp4 *f, int32_t track)
1076 const struct mp4_track *t = f->track[track];
1078 if (t->timeScale == 0)
1080 return t->duration * 1000 / t->timeScale;
1083 int32_t mp4_get_total_tracks(const struct mp4 *f)
1085 return f->total_tracks;
1089 * Check whether the given track number corresponds to an audio track.
1091 * \param f See \ref mp4_get_duration().
1092 * \param track See \ref mp4_get_duration().
1094 * Besides audio tracks, an mp4 file may contain video and system tracks. For
1095 * those the function returns false.
1097 bool mp4_is_audio_track(const struct mp4 *f, int32_t track)
1099 return f->track[track]->is_audio;
1102 void mp4_set_sample_position(struct mp4 *f, int32_t track, int32_t sample)
1104 int32_t offset, chunk, chunk_sample;
1106 chunk_of_sample(f, track, sample, &chunk_sample, &chunk);
1107 offset = chunk_to_offset(f, track, chunk)
1108 + sample_range_size(f, track, chunk_sample, sample);
1109 set_position(f, offset);
1112 int32_t mp4_get_sample_size(const struct mp4 *f, int track, int sample)
1114 const struct mp4_track *t = f->track[track];
1116 if (t->stsz_sample_size != 0)
1117 return t->stsz_sample_size;
1118 return t->stsz_table[sample];
1121 uint32_t mp4_get_sample_rate(const struct mp4 *f, int32_t track)
1123 return f->track[track]->sampleRate;
1126 uint32_t mp4_get_channel_count(const struct mp4 *f, int32_t track)
1128 return f->track[track]->channelCount;
1131 int32_t mp4_num_samples(const struct mp4 *f, int32_t track)
1136 for (i = 0; i < f->track[track]->stts_entry_count; i++) {
1137 total += f->track[track]->stts_sample_count[i];
1142 struct mp4 *mp4_open_meta(const struct mp4_callback *cb)
1145 struct mp4 *f = para_calloc(sizeof(struct mp4));
1148 ret = parse_atoms(f, 1);
1157 * Return the metadata of an mp4 file.
1159 * \param f As returned by either \ref mp4_open_read() or \ref mp4_open_meta().
1161 * The caller is allowed to add, delete or modify the entries of the returned
1162 * structure in order to pass the modified version to \ref mp4_meta_update().
1164 struct mp4_metadata *mp4_get_meta(struct mp4 *f)
1169 static int find_atom(struct mp4 *f, uint64_t base, uint32_t size,
1172 uint32_t remaining = size;
1173 uint64_t atom_offset = base;
1180 set_position(f, atom_offset);
1184 ret = read_int32(f, &atom_size);
1187 if (atom_size > remaining || atom_size < 8)
1189 ret = read_data(f, atom_name, 4);
1192 if (!memcmp(atom_name, name, 4)) {
1193 set_position(f, atom_offset);
1196 remaining -= atom_size;
1197 atom_offset += atom_size;
1202 * Try to find atom <name> with atom <name_inside> in it. Besides -1/0/1 for
1203 * error, EOF and success, this function may return 2 to indicate that the
1204 * desired atoms were not found.
1206 static int find_atom_v2(struct mp4 *f, uint64_t base, uint32_t size,
1207 const char *name, uint32_t extraheaders, const char *name_inside)
1209 uint64_t first_base = (uint64_t) (-1);
1214 int ret = find_atom(f, base, size, name);
1218 mybase = get_position(f);
1219 ret = read_int32(f, &mysize);
1222 if (first_base == (uint64_t) (-1))
1223 first_base = mybase;
1225 if (mysize < 8 + extraheaders)
1228 if (find_atom (f, mybase + (8 + extraheaders),
1229 mysize - (8 + extraheaders), name_inside)) {
1230 set_position(f, mybase);
1238 if (first_base != (uint64_t)(-1)) {
1239 set_position(f, first_base);
1242 /* wanted atom inside not found */
1252 static struct membuffer *membuffer_create(void)
1254 struct membuffer *buf = para_calloc(sizeof(*buf));
1256 buf->allocated = 256;
1257 buf->data = para_malloc(buf->allocated);
1261 static void membuffer_write(struct membuffer *buf, const void *ptr,
1264 unsigned dest_size = buf->written + bytes;
1266 if (dest_size > buf->allocated) {
1268 buf->allocated <<= 1;
1269 } while (dest_size > buf->allocated);
1270 buf->data = para_realloc(buf->data, buf->allocated);
1274 memcpy((char *) buf->data + buf->written, ptr, bytes);
1275 buf->written += bytes;
1278 static void membuffer_write_atom_name(struct membuffer *buf, const char *data)
1280 membuffer_write(buf, data, 4);
1283 static void membuffer_write_int32(struct membuffer *buf, uint32_t data)
1286 write_u32_be(temp, data);
1287 membuffer_write(buf, temp, 4);
1290 static void membuffer_write_std_tag(struct membuffer *buf, const char *name,
1293 uint32_t len = strlen(value);
1294 membuffer_write_int32(buf, 8 /* atom header */
1295 + 8 /* data atom header */
1296 + 8 /* flags + reserved */
1298 membuffer_write_atom_name(buf, name);
1299 membuffer_write_int32(buf, 8 /* data atom header */
1300 + 8 /* flags + reserved */
1302 membuffer_write_atom_name(buf, "data");
1303 membuffer_write_int32(buf, 1); /* flags */
1304 membuffer_write_int32(buf, 0); /* reserved */
1305 membuffer_write(buf, value, len);
1308 static unsigned membuffer_get_size(const struct membuffer *buf)
1310 return buf->written;
1313 static void *membuffer_detach(struct membuffer *buf)
1315 void *ret = para_realloc(buf->data, buf->written);
1320 struct stdmeta_entry {
1325 static const char *find_standard_meta(const char *name)
1327 const struct stdmeta_entry stdmetas[] = {
1328 {"\xA9" "nam", "title"},
1329 {"\xA9" "ART", "artist"},
1330 {"\xA9" "alb", "album"},
1331 {"\xA9" "day", "date"},
1332 {"\xA9" "cmt", "comment"},
1335 for (unsigned n = 0; n < ARRAY_SIZE(stdmetas); n++)
1336 if (!strcasecmp(name, stdmetas[n].name))
1337 return stdmetas[n].atom;
1341 static uint32_t create_ilst(const struct mp4_metadata *meta, void **out_buffer,
1342 uint32_t * out_size)
1344 struct membuffer *buf = membuffer_create();
1347 for (metaptr = 0; metaptr < meta->count; metaptr++) {
1348 struct mp4_tag *tag = meta->tags + metaptr;
1349 const char *std_meta_atom = find_standard_meta(tag->item);
1351 membuffer_write_std_tag(buf, std_meta_atom, tag->value);
1353 PARA_ERROR_LOG("invalid tag item: %s\n", tag->item);
1355 *out_size = membuffer_get_size(buf);
1356 *out_buffer = membuffer_detach(buf);
1360 static void membuffer_write_atom(struct membuffer *buf, const char *name, unsigned size,
1363 membuffer_write_int32(buf, size + 8);
1364 membuffer_write_atom_name(buf, name);
1365 membuffer_write(buf, data, size);
1368 static void *membuffer_get_ptr(const struct membuffer *buf)
1373 static bool membuffer_transfer_from_file(struct membuffer *buf, struct mp4 *src,
1376 unsigned oldsize = membuffer_get_size(buf);
1379 membuffer_write(buf, 0, bytes);
1380 bufptr = membuffer_get_ptr(buf);
1381 if (read_data(src, bufptr + oldsize, bytes) != 1) {
1389 static uint32_t create_meta(const struct mp4_metadata *meta, void **out_buffer,
1390 uint32_t * out_size)
1392 struct membuffer *buf;
1396 if (!create_ilst(meta, &ilst_buffer, &ilst_size))
1399 buf = membuffer_create();
1401 membuffer_write_int32(buf, 0);
1402 membuffer_write_atom(buf, "ilst", ilst_size, ilst_buffer);
1405 *out_size = membuffer_get_size(buf);
1406 *out_buffer = membuffer_detach(buf);
1410 static uint32_t create_udta(const struct mp4_metadata *meta, void **out_buffer,
1411 uint32_t * out_size)
1413 struct membuffer *buf;
1417 if (!create_meta(meta, &meta_buffer, &meta_size))
1420 buf = membuffer_create();
1422 membuffer_write_atom(buf, "meta", meta_size, meta_buffer);
1426 *out_size = membuffer_get_size(buf);
1427 *out_buffer = membuffer_detach(buf);
1431 static uint32_t fix_byte_order_32(uint32_t src)
1433 return read_u32_be(&src);
1436 static void *modify_moov(struct mp4 *f, uint32_t *out_size)
1439 uint64_t total_base = f->moov_offset + 8;
1440 uint32_t total_size = (uint32_t) (f->moov_size - 8);
1441 uint64_t udta_offset, meta_offset, ilst_offset;
1442 uint32_t udta_size, meta_size, ilst_size;
1443 uint32_t new_ilst_size;
1444 void *new_ilst_buffer, *out_buffer;
1449 ret = find_atom_v2(f, total_base, total_size, "udta", 0, "meta");
1453 struct membuffer *buf;
1454 void *new_udta_buffer;
1455 uint32_t new_udta_size;
1456 if (!create_udta(&f->meta, &new_udta_buffer, &new_udta_size))
1459 buf = membuffer_create();
1460 set_position(f, total_base);
1461 if (!membuffer_transfer_from_file(buf, f, total_size)) {
1462 free(new_udta_buffer);
1465 membuffer_write_atom(buf, "udta", new_udta_size,
1468 free(new_udta_buffer);
1470 *out_size = membuffer_get_size(buf);
1471 return membuffer_detach(buf);
1473 udta_offset = get_position(f);
1474 ret = read_int32(f, &udta_size);
1477 ret = find_atom_v2(f, udta_offset + 8, udta_size - 8, "meta", 4, "ilst");
1481 struct membuffer *buf;
1482 void *new_meta_buffer;
1483 uint32_t new_meta_size;
1485 if (!create_meta(&f->meta, &new_meta_buffer, &new_meta_size))
1488 buf = membuffer_create();
1489 set_position(f, total_base);
1490 if (!membuffer_transfer_from_file(buf, f,
1491 udta_offset - total_base)) {
1492 free(new_meta_buffer);
1496 membuffer_write_int32(buf, udta_size + 8 + new_meta_size);
1497 membuffer_write_atom_name(buf, "udta");
1498 if (!membuffer_transfer_from_file(buf, f, udta_size)) {
1499 free(new_meta_buffer);
1502 membuffer_write_atom(buf, "meta", new_meta_size,
1504 free(new_meta_buffer);
1506 *out_size = membuffer_get_size(buf);
1507 return membuffer_detach(buf);
1509 meta_offset = get_position(f);
1510 ret = read_int32(f, &meta_size);
1513 /* shouldn't happen, find_atom_v2 above takes care of it */
1514 if (!find_atom(f, meta_offset + 12, meta_size - 12, "ilst"))
1516 ilst_offset = get_position(f);
1517 ret = read_int32(f, &ilst_size);
1520 if (!create_ilst(&f->meta, &new_ilst_buffer, &new_ilst_size))
1522 size_delta = new_ilst_size - (ilst_size - 8);
1523 *out_size = total_size + size_delta;
1524 out_buffer = para_malloc(*out_size);
1526 set_position(f, total_base);
1527 ret = read_data(f, p_out, udta_offset - total_base);
1530 p_out += (uint32_t) (udta_offset - total_base);
1531 ret = read_int32(f, &tmp);
1534 *(uint32_t *)p_out = fix_byte_order_32(tmp + size_delta);
1536 ret = read_data(f, p_out, 4);
1540 ret = read_data(f, p_out, meta_offset - udta_offset - 8);
1543 p_out += (uint32_t) (meta_offset - udta_offset - 8);
1544 ret = read_int32(f, &tmp);
1547 *(uint32_t *)p_out = fix_byte_order_32(tmp + size_delta);
1549 ret = read_data(f, p_out, 4);
1553 ret = read_data(f, p_out, ilst_offset - meta_offset - 8);
1556 p_out += (uint32_t) (ilst_offset - meta_offset - 8);
1557 ret = read_int32(f, &tmp);
1560 *(uint32_t *)p_out = fix_byte_order_32(tmp + size_delta);
1562 ret = read_data(f, p_out, 4);
1566 memcpy(p_out, new_ilst_buffer, new_ilst_size);
1567 p_out += new_ilst_size;
1568 set_position(f, ilst_offset + ilst_size);
1569 ret = read_data(f, p_out, total_size
1570 - (ilst_offset - total_base) - ilst_size);
1573 free(new_ilst_buffer);
1577 static int32_t write_data(struct mp4 *f, void *data, uint32_t size)
1581 result = f->cb->write(f->cb->user_data, data, size);
1583 f->current_position += size;
1588 static int32_t write_int32(struct mp4 *f, uint32_t data)
1591 write_u32_be(temp, data);
1592 return write_data(f, temp, sizeof(temp));
1595 int32_t mp4_meta_update(struct mp4 *f)
1597 void *new_moov_data;
1598 uint32_t new_moov_size;
1601 new_moov_data = modify_moov(f, &new_moov_size);
1602 if (!new_moov_data ) {
1606 /* copy moov atom to end of the file */
1607 if (f->last_atom != ATOM_MOOV) {
1608 char *free_data = "free";
1610 /* rename old moov to free */
1611 set_position(f, f->moov_offset + 4);
1612 write_data(f, free_data, 4);
1614 set_position(f, f->file_size);
1615 write_int32(f, new_moov_size + 8);
1616 write_data(f, "moov", 4);
1617 write_data(f, new_moov_data, new_moov_size);
1619 set_position(f, f->moov_offset);
1620 write_int32(f, new_moov_size + 8);
1621 write_data(f, "moov", 4);
1622 write_data(f, new_moov_data, new_moov_size);
1624 free(new_moov_data);
1625 f->cb->truncate(f->cb->user_data);
1629 static char *meta_find_by_name(const struct mp4 *f, const char *item)
1633 for (i = 0; i < f->meta.count; i++)
1634 if (!strcasecmp(f->meta.tags[i].item, item))
1635 return para_strdup(f->meta.tags[i].value);
1640 * Return the value of the artist meta tag of an mp4 file.
1642 * \param f Must not be NULL.
1644 * \return If the file does not contain this metadata tag, the function returns
1645 * NULL. Otherwise, a copy of the tag value is returned. The caller should free
1646 * this memory when it is no longer needed.
1648 char *mp4_meta_get_artist(const struct mp4 *f)
1650 return meta_find_by_name(f, "artist");
1654 * Return the value of the title meta tag of an mp4 file.
1656 * \param f See \ref mp4_meta_get_artist().
1657 * \return See \ref mp4_meta_get_artist().
1659 char *mp4_meta_get_title(const struct mp4 *f)
1661 return meta_find_by_name(f, "title");
1665 * Return the value of the date meta tag of an mp4 file.
1667 * \param f See \ref mp4_meta_get_artist().
1668 * \return See \ref mp4_meta_get_artist().
1670 char *mp4_meta_get_date(const struct mp4 *f)
1672 return meta_find_by_name(f, "date");
1676 * Return the value of the album meta tag of an mp4 file.
1678 * \param f See \ref mp4_meta_get_artist().
1679 * \return See \ref mp4_meta_get_artist().
1681 char *mp4_meta_get_album(const struct mp4 *f)
1683 return meta_find_by_name(f, "album");
1687 * Return the value of the comment meta tag of an mp4 file.
1689 * \param f See \ref mp4_meta_get_artist().
1690 * \return See \ref mp4_meta_get_artist().
1692 char *mp4_meta_get_comment(const struct mp4 *f)
1694 return meta_find_by_name(f, "comment");