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;
57 /* incremental track index while reading the file */
61 struct mp4_track *track[MAX_TRACKS];
64 struct mp4_metadata meta;
67 int32_t mp4_total_tracks(const struct mp4 *f)
69 return f->total_tracks;
73 * Returns -1, 0, or 1 on errors/EOF/success. Partial reads followed by EOF or
74 * read errors are treated as errors.
76 static int read_data(struct mp4 *f, void *data, size_t size)
79 ssize_t ret = f->cb->read(f->cb->user_data, data, size);
80 if (ret < 0 && errno == EINTR)
82 /* regard EAGAIN as an error as reads should be blocking. */
84 return ret < 0? -1 : 0;
85 f->current_position += ret;
91 static int read_int64(struct mp4 *f, uint64_t *result)
94 int ret = read_data(f, data, 8);
96 if (ret > 0 && result)
97 *result = read_u64_be(data);
101 static int read_int32(struct mp4 *f, uint32_t *result)
104 int ret = read_data(f, data, 4);
106 if (ret > 0 && result)
107 *result = read_u32_be(data);
111 static int read_int24(struct mp4 *f, uint32_t *result)
114 int ret = read_data(f, data, 3);
116 if (ret > 0 && result)
117 *result = read_u24_be(data);
121 static int read_int16(struct mp4 *f, uint16_t *result)
124 int ret = read_data(f, data, 2);
126 if (ret > 0 && result)
127 *result = read_u16_be(data);
131 static uint8_t read_int8(struct mp4 *f, uint8_t *result)
134 int ret = read_data(f, data, 1);
136 if (ret > 0 && result)
141 static bool atom_compare(int8_t a1, int8_t b1, int8_t c1, int8_t d1,
142 int8_t a2, int8_t b2, int8_t c2, int8_t d2)
144 return a1 == a2 && b1 == b2 && c1 == c2 && d1 == d2;
148 /* atoms with subatoms */
156 ATOM_ILST = 8, /* iTunes Metadata list */
167 ATOM_COMPILATION = 19,
177 /* atoms without subatoms */
183 ATOM_MDHD = 134, /* track header */
187 ATOM_STSD = 138, /* sample description box */
188 ATOM_STTS = 139, /* time to sample box */
189 ATOM_STSZ = 140, /* sample size box */
191 ATOM_STCO = 142, /* chunk offset box */
192 ATOM_STSC = 143, /* sample to chunk box */
197 ATOM_META = 148, /* iTunes Metadata box */
198 ATOM_NAME = 149, /* iTunes Metadata name box */
199 ATOM_DATA = 150, /* iTunes Metadata data box */
206 ATOM_ALBUM_ARTIST = 157,
207 ATOM_CONTENTGROUP = 158,
209 ATOM_DESCRIPTION = 160,
212 ATOM_EPISODENAME = 163,
213 ATOM_SORTTITLE = 164,
214 ATOM_SORTALBUM = 165,
215 ATOM_SORTARTIST = 166,
216 ATOM_SORTALBUMARTIST = 167,
217 ATOM_SORTWRITER = 168,
226 #define ATOM_FREE ATOM_UNKNOWN
227 #define ATOM_SKIP ATOM_UNKNOWN
229 #define COPYRIGHT_SYMBOL ((int8_t)0xA9)
231 static uint8_t atom_name_to_type(int8_t a, int8_t b, int8_t c, int8_t d)
234 if (atom_compare(a, b, c, d, 'm', 'o', 'o', 'v'))
236 else if (atom_compare(a, b, c, d, 'm', 'i', 'n', 'f'))
238 else if (atom_compare(a, b, c, d, 'm', 'd', 'i', 'a'))
240 else if (atom_compare(a, b, c, d, 'm', 'd', 'a', 't'))
242 else if (atom_compare(a, b, c, d, 'm', 'd', 'h', 'd'))
244 else if (atom_compare(a, b, c, d, 'm', 'v', 'h', 'd'))
246 else if (atom_compare(a, b, c, d, 'm', 'p', '4', 'a'))
248 else if (atom_compare(a, b, c, d, 'm', 'p', '4', 'v'))
250 else if (atom_compare(a, b, c, d, 'm', 'p', '4', 's'))
252 else if (atom_compare(a, b, c, d, 'm', 'e', 't', 'a'))
254 } else if (a == 't') {
255 if (atom_compare(a, b, c, d, 't', 'r', 'a', 'k'))
257 else if (atom_compare(a, b, c, d, 't', 'k', 'h', 'd'))
259 else if (atom_compare(a, b, c, d, 't', 'r', 'e', 'f'))
261 else if (atom_compare(a, b, c, d, 't', 'r', 'k', 'n'))
263 else if (atom_compare(a, b, c, d, 't', 'm', 'p', 'o'))
265 else if (atom_compare(a, b, c, d, 't', 'v', 'n', 'n'))
267 else if (atom_compare(a, b, c, d, 't', 'v', 's', 'h'))
269 else if (atom_compare(a, b, c, d, 't', 'v', 'e', 'n'))
270 return ATOM_EPISODENAME;
271 else if (atom_compare(a, b, c, d, 't', 'v', 's', 'n'))
273 else if (atom_compare(a, b, c, d, 't', 'v', 'e', 's'))
275 } else if (a == 's') {
276 if (atom_compare(a, b, c, d, 's', 't', 'b', 'l'))
278 else if (atom_compare(a, b, c, d, 's', 'm', 'h', 'd'))
280 else if (atom_compare(a, b, c, d, 's', 't', 's', 'd'))
282 else if (atom_compare(a, b, c, d, 's', 't', 't', 's'))
284 else if (atom_compare(a, b, c, d, 's', 't', 'c', 'o'))
286 else if (atom_compare(a, b, c, d, 's', 't', 's', 'c'))
288 else if (atom_compare(a, b, c, d, 's', 't', 's', 'z'))
290 else if (atom_compare(a, b, c, d, 's', 't', 'z', '2'))
292 else if (atom_compare(a, b, c, d, 's', 'k', 'i', 'p'))
294 else if (atom_compare(a, b, c, d, 's', 'i', 'n', 'f'))
296 else if (atom_compare(a, b, c, d, 's', 'c', 'h', 'i'))
298 else if (atom_compare(a, b, c, d, 's', 'o', 'n', 'm'))
299 return ATOM_SORTTITLE;
300 else if (atom_compare(a, b, c, d, 's', 'o', 'a', 'l'))
301 return ATOM_SORTALBUM;
302 else if (atom_compare(a, b, c, d, 's', 'o', 'a', 'r'))
303 return ATOM_SORTARTIST;
304 else if (atom_compare(a, b, c, d, 's', 'o', 'a', 'a'))
305 return ATOM_SORTALBUMARTIST;
306 else if (atom_compare(a, b, c, d, 's', 'o', 'c', 'o'))
307 return ATOM_SORTWRITER;
308 else if (atom_compare(a, b, c, d, 's', 'o', 's', 'n'))
309 return ATOM_SORTSHOW;
310 } else if (a == COPYRIGHT_SYMBOL) {
311 if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'n', 'a', 'm'))
313 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'A', 'R', 'T'))
315 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'w', 'r', 't'))
317 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'a', 'l', 'b'))
319 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'd', 'a', 'y'))
321 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 't', 'o', 'o'))
323 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'c', 'm', 't'))
325 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'g', 'e', 'n'))
327 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'g', 'r', 'p'))
328 return ATOM_CONTENTGROUP;
329 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'l', 'y', 'r'))
333 if (atom_compare(a, b, c, d, 'e', 'd', 't', 's'))
335 else if (atom_compare(a, b, c, d, 'e', 's', 'd', 's'))
337 else if (atom_compare(a, b, c, d, 'f', 't', 'y', 'p'))
339 else if (atom_compare(a, b, c, d, 'f', 'r', 'e', 'e'))
341 else if (atom_compare(a, b, c, d, 'h', 'm', 'h', 'd'))
343 else if (atom_compare(a, b, c, d, 'v', 'm', 'h', 'd'))
345 else if (atom_compare(a, b, c, d, 'u', 'd', 't', 'a'))
347 else if (atom_compare(a, b, c, d, 'i', 'l', 's', 't'))
349 else if (atom_compare(a, b, c, d, 'n', 'a', 'm', 'e'))
351 else if (atom_compare(a, b, c, d, 'd', 'a', 't', 'a'))
353 else if (atom_compare(a, b, c, d, 'd', 'i', 's', 'k'))
355 else if (atom_compare(a, b, c, d, 'g', 'n', 'r', 'e'))
357 else if (atom_compare(a, b, c, d, 'c', 'o', 'v', 'r'))
359 else if (atom_compare(a, b, c, d, 'c', 'p', 'i', 'l'))
360 return ATOM_COMPILATION;
361 else if (atom_compare(a, b, c, d, 'c', 't', 't', 's'))
363 else if (atom_compare(a, b, c, d, 'd', 'r', 'm', 's'))
365 else if (atom_compare(a, b, c, d, 'f', 'r', 'm', 'a'))
367 else if (atom_compare(a, b, c, d, 'p', 'r', 'i', 'v'))
369 else if (atom_compare(a, b, c, d, 'i', 'v', 'i', 'v'))
371 else if (atom_compare(a, b, c, d, 'u', 's', 'e', 'r'))
373 else if (atom_compare(a, b, c, d, 'k', 'e', 'y', ' '))
375 else if (atom_compare(a, b, c, d, 'a', 'A', 'R', 'T'))
376 return ATOM_ALBUM_ARTIST;
377 else if (atom_compare(a, b, c, d, 'd', 'e', 's', 'c'))
378 return ATOM_DESCRIPTION;
379 else if (atom_compare(a, b, c, d, 'p', 'c', 's', 't'))
385 /* read atom header, atom size is returned with header included. */
386 static int atom_read_header(struct mp4 *f, uint8_t *atom_type,
387 uint8_t *header_size, uint64_t *atom_size)
391 int8_t atom_header[8];
393 ret = read_data(f, atom_header, 8);
396 size = read_u32_be(atom_header);
397 if (size == 1) { /* 64 bit atom size */
400 ret = read_int64(f, atom_size);
409 *atom_type = atom_name_to_type(atom_header[4], atom_header[5],
410 atom_header[6], atom_header[7]);
414 static int64_t get_position(const struct mp4 *f)
416 return f->current_position;
419 static int need_parse_when_meta_only(uint8_t atom_type)
440 static int32_t set_position(struct mp4 *f, int64_t position)
442 f->cb->seek(f->cb->user_data, position);
443 f->current_position = position;
448 static void track_add(struct mp4 *f)
452 if (f->total_tracks > MAX_TRACKS) {
457 f->track[f->total_tracks - 1] = para_calloc(sizeof(struct mp4_track));
460 static int read_stsz(struct mp4 *f)
466 if (f->total_tracks == 0)
468 t = f->track[f->total_tracks - 1];
469 ret = read_int8(f, NULL); /* version */
472 ret = read_int24(f, NULL); /* flags */
475 ret = read_int32(f, &t->stsz_sample_size);
478 ret = read_int32(f, &t->stsz_sample_count);
481 if (t->stsz_sample_size != 0)
483 t->stsz_table = para_malloc(t->stsz_sample_count * sizeof(int32_t));
484 for (i = 0; i < t->stsz_sample_count; i++) {
485 ret = read_int32(f, &t->stsz_table[i]);
492 static int read_stts(struct mp4 *f)
498 if (f->total_tracks == 0)
500 t = f->track[f->total_tracks - 1];
501 if (t->stts_entry_count)
503 ret = read_int8(f, NULL); /* version */
506 ret = read_int24(f, NULL); /* flags */
509 ret = read_int32(f, &t->stts_entry_count);
512 t->stts_sample_count = para_malloc(t->stts_entry_count
514 t->stts_sample_delta = para_malloc(t->stts_entry_count
516 for (i = 0; i < t->stts_entry_count; i++) {
517 ret = read_int32(f, &t->stts_sample_count[i]);
520 ret = read_int32(f, &t->stts_sample_delta[i]);
527 static int read_stsc(struct mp4 *f)
533 if (f->total_tracks == 0)
535 t = f->track[f->total_tracks - 1];
537 ret = read_int8(f, NULL); /* version */
540 ret = read_int24(f, NULL); /* flags */
543 ret = read_int32(f, &t->stsc_entry_count);
546 t->stsc_first_chunk = para_malloc(t->stsc_entry_count * sizeof(int32_t));
547 t->stsc_samples_per_chunk = para_malloc(t->stsc_entry_count
549 t->stsc_sample_desc_index = para_malloc(t->stsc_entry_count *
552 for (i = 0; i < t->stsc_entry_count; i++) {
553 ret = read_int32(f, &t->stsc_first_chunk[i]);
556 ret = read_int32(f, &t->stsc_samples_per_chunk[i]);
559 ret = read_int32(f, &t->stsc_sample_desc_index[i]);
566 static int read_stco(struct mp4 *f)
572 if (f->total_tracks == 0)
574 t = f->track[f->total_tracks - 1];
576 ret = read_int8(f, NULL); /* version */
579 ret = read_int24(f, NULL); /* flags */
582 ret = read_int32(f, &t->stco_entry_count);
585 t->stco_chunk_offset = para_malloc(t->stco_entry_count
587 for (i = 0; i < t->stco_entry_count; i++) {
588 ret = read_int32(f, &t->stco_chunk_offset[i]);
595 static int read_mp4a(struct mp4 *f)
599 uint8_t atom_type = 0;
600 uint8_t header_size = 0;
603 if (f->total_tracks == 0)
605 t = f->track[f->total_tracks - 1];
607 for (i = 0; i < 6; i++) {
608 ret = read_int8(f, NULL); /* reserved */
612 ret = read_int16(f, NULL); /* data_reference_index */
615 ret = read_int32(f, NULL); /* reserved */
618 ret = read_int32(f, NULL); /* reserved */
621 ret = read_int16(f, &t->channelCount);
624 ret = read_int16(f, NULL);
627 ret = read_int16(f, NULL);
630 ret = read_int16(f, NULL);
633 ret = read_int16(f, &t->sampleRate);
636 ret = read_int16(f, NULL);
639 return atom_read_header(f, &atom_type, &header_size, NULL);
642 static int read_stsd(struct mp4 *f)
645 uint32_t i, entry_count;
646 uint8_t header_size = 0;
649 if (f->total_tracks == 0)
651 t = f->track[f->total_tracks - 1];
652 ret = read_int8(f, NULL); /* version */
655 ret = read_int24(f, NULL); /* flags */
658 ret = read_int32(f, &entry_count);
661 for (i = 0; i < entry_count; i++) {
662 uint64_t skip = get_position(f);
664 uint8_t atom_type = 0;
665 ret = atom_read_header(f, &atom_type, &header_size, &size);
669 t->is_audio = atom_type == ATOM_MP4A;
672 set_position(f, skip);
677 static int32_t tag_add_field(struct mp4_metadata *meta, const char *item,
678 const char *value, int32_t len)
680 meta->tags = para_realloc(meta->tags,
681 (meta->count + 1) * sizeof(struct mp4_tag));
682 meta->tags[meta->count].item = para_strdup(item);
683 meta->tags[meta->count].len = len;
685 meta->tags[meta->count].value = para_malloc(len + 1);
686 memcpy(meta->tags[meta->count].value, value, len);
687 meta->tags[meta->count].value[len] = 0;
689 meta->tags[meta->count].value = para_strdup(value);
695 static int read_string(struct mp4 *f, uint32_t length, char **result)
697 char *str = para_malloc(length + 1);
698 int ret = read_data(f, str, length);
710 static const char *get_metadata_name(uint8_t atom_type)
713 case ATOM_TITLE: return "title";
714 case ATOM_ARTIST: return "artist";
715 case ATOM_ALBUM: return "album";
716 case ATOM_DATE: return "date";
717 case ATOM_COMMENT: return "comment";
718 default: return "unknown";
722 static int parse_tag(struct mp4 *f, uint8_t parent, int32_t size)
725 uint64_t subsize, sumsize;
733 set_position(f, destpos), sumsize += subsize
736 uint8_t header_size = 0;
737 ret = atom_read_header(f, &atom_type, &header_size, &subsize);
740 destpos = get_position(f) + subsize - header_size;
741 if (atom_type != ATOM_DATA)
743 ret = read_int8(f, NULL); /* version */
746 ret = read_int24(f, NULL); /* flags */
749 ret = read_int32(f, NULL); /* reserved */
753 ret = read_string(f, subsize - (header_size + 8), &data);
756 len = subsize - (header_size + 8);
760 tag_add_field(&f->meta, get_metadata_name(parent), data, len);
765 static int read_mdhd(struct mp4 *f)
771 if (f->total_tracks == 0)
773 t = f->track[f->total_tracks - 1];
775 ret = read_int32(f, &version);
779 ret = read_int64(f, NULL); /* creation-time */
782 ret = read_int64(f, NULL); /* modification-time */
785 ret = read_int32(f, &t->timeScale);
788 ret = read_int64(f, &t->duration);
791 } else { //version == 0
794 ret = read_int32(f, NULL); /* creation-time */
797 ret = read_int32(f, NULL); /* modification-time */
800 ret = read_int32(f, &t->timeScale);
803 ret = read_int32(f, &temp);
806 t->duration = (temp == (uint32_t) (-1))?
807 (uint64_t) (-1) : (uint64_t) (temp);
809 ret = read_int16(f, NULL);
812 ret = read_int16(f, NULL);
818 static int32_t read_ilst(struct mp4 *f, int32_t size)
821 uint64_t sumsize = 0;
823 while (sumsize < size) {
825 uint64_t subsize, destpos;
826 uint8_t header_size = 0;
827 ret = atom_read_header(f, &atom_type, &header_size, &subsize);
830 destpos = get_position(f) + subsize - header_size;
837 parse_tag(f, atom_type, subsize - header_size);
839 set_position(f, destpos);
845 static int32_t read_meta(struct mp4 *f, uint64_t size)
848 uint64_t subsize, sumsize = 0;
850 uint8_t header_size = 0;
852 ret = read_int8(f, NULL); /* version */
855 ret = read_int24(f, NULL); /* flags */
858 while (sumsize < (size - (header_size + 4))) {
859 ret = atom_read_header(f, &atom_type, &header_size, &subsize);
862 if (subsize <= header_size + 4)
864 if (atom_type == ATOM_ILST)
865 read_ilst(f, subsize - (header_size + 4));
867 set_position(f, get_position(f) + subsize - header_size);
873 static int atom_read(struct mp4 *f, uint64_t size, uint8_t atom_type)
875 uint64_t dest_position = get_position(f) + size - 8;
876 int ret = 1; /* return success for atoms we don't care about */
879 case ATOM_STSZ: ret = read_stsz(f); break;
880 case ATOM_STTS: ret = read_stts(f); break;
881 case ATOM_STSC: ret = read_stsc(f); break;
882 case ATOM_STCO: ret = read_stco(f); break;
883 case ATOM_STSD: ret = read_stsd(f); break;
884 case ATOM_MDHD: ret = read_mdhd(f); break;
885 case ATOM_META: ret = read_meta(f, size); break;
887 set_position(f, dest_position);
891 /* parse atoms that are sub atoms of other atoms */
892 static int parse_sub_atoms(struct mp4 *f, uint64_t total_size, int meta_only)
896 uint8_t atom_type = 0;
897 uint64_t counted_size = 0;
898 uint8_t header_size = 0;
900 while (counted_size < total_size) {
901 ret = atom_read_header(f, &atom_type, &header_size, &size);
906 counted_size += size;
907 /* we're starting to read a new track, update index,
908 * so that all data and tables get written in the right place
910 if (atom_type == ATOM_TRAK)
913 if (meta_only && !need_parse_when_meta_only(atom_type)) {
914 set_position(f, get_position(f) + size - header_size);
915 } else if (atom_type < SUBATOMIC) {
916 parse_sub_atoms(f, size - header_size, meta_only);
918 ret = atom_read(f, size, atom_type);
926 /* parse root atoms */
927 static int32_t parse_atoms(struct mp4 *f, int meta_only)
931 uint8_t atom_type = 0;
932 uint8_t header_size = 0;
936 while ((ret = atom_read_header(f, &atom_type, &header_size, &size)) > 0) {
937 f->file_size += size;
938 f->last_atom = atom_type;
940 if (atom_type == ATOM_MOOV && size > header_size) {
941 f->moov_offset = get_position(f) - header_size;
946 if (meta_only && !need_parse_when_meta_only(atom_type)) {
947 set_position(f, get_position(f) + size - header_size);
948 } else if (atom_type < SUBATOMIC) {
949 parse_sub_atoms(f, size - header_size, meta_only);
952 set_position(f, get_position(f) + size - header_size);
958 struct mp4 *mp4_open_read(const struct mp4_callback *cb)
960 struct mp4 *f = para_calloc(sizeof(struct mp4));
971 void mp4_close(struct mp4 *f)
975 for (i = 0; i < f->total_tracks; i++) {
977 free(f->track[i]->stsz_table);
978 free(f->track[i]->stts_sample_count);
979 free(f->track[i]->stts_sample_delta);
980 free(f->track[i]->stsc_first_chunk);
981 free(f->track[i]->stsc_samples_per_chunk);
982 free(f->track[i]->stsc_sample_desc_index);
983 free(f->track[i]->stco_chunk_offset);
987 for (i = 0; i < f->meta.count; i++) {
988 free(f->meta.tags[i].item);
989 free(f->meta.tags[i].value);
995 static int32_t chunk_of_sample(const struct mp4 *f, int32_t track,
996 int32_t sample, int32_t *chunk_sample, int32_t *chunk)
998 int32_t total_entries = 0;
1000 int32_t chunk1, chunk2, chunk1samples, range_samples, total = 0;
1004 if (f->track[track] == NULL) {
1008 total_entries = f->track[track]->stsc_entry_count;
1015 chunk2 = f->track[track]->stsc_first_chunk[chunk2entry];
1016 *chunk = chunk2 - chunk1;
1017 range_samples = *chunk * chunk1samples;
1019 if (sample < total + range_samples)
1022 chunk1samples = f->track[track]->stsc_samples_per_chunk[chunk2entry];
1025 if (chunk2entry < total_entries) {
1027 total += range_samples;
1029 } while (chunk2entry < total_entries);
1032 *chunk = (sample - total) / chunk1samples + chunk1;
1036 *chunk_sample = total + (*chunk - chunk1) * chunk1samples;
1041 static int32_t chunk_to_offset(const struct mp4 *f, int32_t track,
1044 const struct mp4_track *p_track = f->track[track];
1046 if (p_track->stco_entry_count && (chunk > p_track->stco_entry_count)) {
1047 return p_track->stco_chunk_offset[p_track->stco_entry_count -
1049 } else if (p_track->stco_entry_count) {
1050 return p_track->stco_chunk_offset[chunk - 1];
1058 static int32_t sample_range_size(const struct mp4 *f, int32_t track,
1059 int32_t chunk_sample, int32_t sample)
1062 const struct mp4_track *p_track = f->track[track];
1064 if (p_track->stsz_sample_size) {
1065 return (sample - chunk_sample) * p_track->stsz_sample_size;
1067 if (sample >= p_track->stsz_sample_count)
1070 for (i = chunk_sample, total = 0; i < sample; i++) {
1071 total += p_track->stsz_table[i];
1078 static int32_t sample_to_offset(const struct mp4 *f, int32_t track,
1081 int32_t chunk, chunk_sample, chunk_offset1, chunk_offset2;
1083 chunk_of_sample(f, track, sample, &chunk_sample, &chunk);
1085 chunk_offset1 = chunk_to_offset(f, track, chunk);
1086 chunk_offset2 = chunk_offset1 + sample_range_size(f,
1087 track, chunk_sample, sample);
1088 return chunk_offset2;
1092 * Return the number of milliseconds of the given track.
1094 * \param f As returned by \ref mp4_open_read(), must not be NULL.
1095 * \param track Between zero and the value returned by \ref mp4_total_tracks().
1097 * The function returns zero if the audio file is of zero length or contains a
1098 * corrupt track header.
1100 uint64_t mp4_get_duration(const struct mp4 *f, int32_t track)
1102 const struct mp4_track *t = f->track[track];
1104 if (t->timeScale == 0)
1106 return t->duration * 1000 / t->timeScale;
1110 * Check whether the given track number corresponds to an audio track.
1112 * \param f See \ref mp4_get_duration().
1113 * \param track See \ref mp4_get_duration().
1115 * Besides audio tracks, an mp4 file may contain video and system tracks. For
1116 * those the function returns false.
1118 bool mp4_is_audio_track(const struct mp4 *f, int32_t track)
1120 return f->track[track]->is_audio;
1123 void mp4_set_sample_position(struct mp4 *f, int32_t track, int32_t sample)
1125 int32_t offset = sample_to_offset(f, track, sample);
1126 set_position(f, offset);
1129 int32_t mp4_get_sample_size(const struct mp4 *f, int track, int sample)
1131 const struct mp4_track *t = f->track[track];
1133 if (t->stsz_sample_size != 0)
1134 return t->stsz_sample_size;
1135 return t->stsz_table[sample];
1138 uint32_t mp4_get_sample_rate(const struct mp4 *f, int32_t track)
1140 return f->track[track]->sampleRate;
1143 uint32_t mp4_get_channel_count(const struct mp4 *f, int32_t track)
1145 return f->track[track]->channelCount;
1148 int32_t mp4_num_samples(const struct mp4 *f, int32_t track)
1153 for (i = 0; i < f->track[track]->stts_entry_count; i++) {
1154 total += f->track[track]->stts_sample_count[i];
1159 struct mp4 *mp4_open_meta(const struct mp4_callback *cb)
1161 struct mp4 *f = para_calloc(sizeof(struct mp4));
1173 * Return the metadata of an mp4 file.
1175 * \param f As returned by either \ref mp4_open_read() or \ref mp4_open_meta().
1177 * The caller is allowed to add, delete or modify the entries of the returned
1178 * structure in order to pass the modified version to \ref mp4_meta_update().
1180 struct mp4_metadata *mp4_get_meta(struct mp4 *f)
1185 static int find_atom(struct mp4 *f, uint64_t base, uint32_t size,
1188 uint32_t remaining = size;
1189 uint64_t atom_offset = base;
1196 set_position(f, atom_offset);
1200 ret = read_int32(f, &atom_size);
1203 if (atom_size > remaining || atom_size < 8)
1205 ret = read_data(f, atom_name, 4);
1208 if (!memcmp(atom_name, name, 4)) {
1209 set_position(f, atom_offset);
1212 remaining -= atom_size;
1213 atom_offset += atom_size;
1218 * Try to find atom <name> with atom <name_inside> in it. Besides -1/0/1 for
1219 * error, EOF and success, this function may return 2 to indicate that the
1220 * desired atoms were not found.
1222 static int find_atom_v2(struct mp4 *f, uint64_t base, uint32_t size,
1223 const char *name, uint32_t extraheaders, const char *name_inside)
1225 uint64_t first_base = (uint64_t) (-1);
1230 int ret = find_atom(f, base, size, name);
1234 mybase = get_position(f);
1235 ret = read_int32(f, &mysize);
1238 if (first_base == (uint64_t) (-1))
1239 first_base = mybase;
1241 if (mysize < 8 + extraheaders)
1244 if (find_atom (f, mybase + (8 + extraheaders),
1245 mysize - (8 + extraheaders), name_inside)) {
1246 set_position(f, mybase);
1254 if (first_base != (uint64_t)(-1)) {
1255 set_position(f, first_base);
1258 /* wanted atom inside not found */
1268 static struct membuffer *membuffer_create(void)
1270 struct membuffer *buf = para_calloc(sizeof(*buf));
1272 buf->allocated = 256;
1273 buf->data = para_malloc(buf->allocated);
1277 static void membuffer_write(struct membuffer *buf, const void *ptr,
1280 unsigned dest_size = buf->written + bytes;
1282 if (dest_size > buf->allocated) {
1284 buf->allocated <<= 1;
1285 } while (dest_size > buf->allocated);
1286 buf->data = para_realloc(buf->data, buf->allocated);
1290 memcpy((char *) buf->data + buf->written, ptr, bytes);
1291 buf->written += bytes;
1294 static void membuffer_write_atom_name(struct membuffer *buf, const char *data)
1296 membuffer_write(buf, data, 4);
1299 static void membuffer_write_int32(struct membuffer *buf, uint32_t data)
1302 write_u32_be(temp, data);
1303 membuffer_write(buf, temp, 4);
1306 static void membuffer_write_std_tag(struct membuffer *buf, const char *name,
1309 uint32_t len = strlen(value);
1310 membuffer_write_int32(buf, 8 /* atom header */
1311 + 8 /* data atom header */
1312 + 8 /* flags + reserved */
1314 membuffer_write_atom_name(buf, name);
1315 membuffer_write_int32(buf, 8 /* data atom header */
1316 + 8 /* flags + reserved */
1318 membuffer_write_atom_name(buf, "data");
1319 membuffer_write_int32(buf, 1); /* flags */
1320 membuffer_write_int32(buf, 0); /* reserved */
1321 membuffer_write(buf, value, len);
1324 static unsigned membuffer_get_size(const struct membuffer *buf)
1326 return buf->written;
1329 static void *membuffer_detach(struct membuffer *buf)
1331 void *ret = para_realloc(buf->data, buf->written);
1336 struct stdmeta_entry {
1341 static const char *find_standard_meta(const char *name)
1343 const struct stdmeta_entry stdmetas[] = {
1344 {"\xA9" "nam", "title"},
1345 {"\xA9" "ART", "artist"},
1346 {"\xA9" "alb", "album"},
1347 {"\xA9" "day", "date"},
1348 {"\xA9" "cmt", "comment"},
1351 for (unsigned n = 0; n < ARRAY_SIZE(stdmetas); n++)
1352 if (!strcasecmp(name, stdmetas[n].name))
1353 return stdmetas[n].atom;
1357 static uint32_t create_ilst(const struct mp4_metadata *meta, void **out_buffer,
1358 uint32_t * out_size)
1360 struct membuffer *buf = membuffer_create();
1363 for (metaptr = 0; metaptr < meta->count; metaptr++) {
1364 struct mp4_tag *tag = meta->tags + metaptr;
1365 const char *std_meta_atom = find_standard_meta(tag->item);
1367 membuffer_write_std_tag(buf, std_meta_atom, tag->value);
1369 PARA_ERROR_LOG("invalid tag item: %s\n", tag->item);
1371 *out_size = membuffer_get_size(buf);
1372 *out_buffer = membuffer_detach(buf);
1376 static void membuffer_write_atom(struct membuffer *buf, const char *name, unsigned size,
1379 membuffer_write_int32(buf, size + 8);
1380 membuffer_write_atom_name(buf, name);
1381 membuffer_write(buf, data, size);
1384 static void *membuffer_get_ptr(const struct membuffer *buf)
1389 static bool membuffer_transfer_from_file(struct membuffer *buf, struct mp4 *src,
1392 unsigned oldsize = membuffer_get_size(buf);
1395 membuffer_write(buf, 0, bytes);
1396 bufptr = membuffer_get_ptr(buf);
1397 if (read_data(src, bufptr + oldsize, bytes) != 1) {
1405 static uint32_t create_meta(const struct mp4_metadata *meta, void **out_buffer,
1406 uint32_t * out_size)
1408 struct membuffer *buf;
1412 if (!create_ilst(meta, &ilst_buffer, &ilst_size))
1415 buf = membuffer_create();
1417 membuffer_write_int32(buf, 0);
1418 membuffer_write_atom(buf, "ilst", ilst_size, ilst_buffer);
1421 *out_size = membuffer_get_size(buf);
1422 *out_buffer = membuffer_detach(buf);
1426 static uint32_t create_udta(const struct mp4_metadata *meta, void **out_buffer,
1427 uint32_t * out_size)
1429 struct membuffer *buf;
1433 if (!create_meta(meta, &meta_buffer, &meta_size))
1436 buf = membuffer_create();
1438 membuffer_write_atom(buf, "meta", meta_size, meta_buffer);
1442 *out_size = membuffer_get_size(buf);
1443 *out_buffer = membuffer_detach(buf);
1447 static uint32_t fix_byte_order_32(uint32_t src)
1449 return read_u32_be(&src);
1452 static void *modify_moov(struct mp4 *f, uint32_t *out_size)
1455 uint64_t total_base = f->moov_offset + 8;
1456 uint32_t total_size = (uint32_t) (f->moov_size - 8);
1457 uint64_t udta_offset, meta_offset, ilst_offset;
1458 uint32_t udta_size, meta_size, ilst_size;
1459 uint32_t new_ilst_size;
1460 void *new_ilst_buffer, *out_buffer;
1465 ret = find_atom_v2(f, total_base, total_size, "udta", 0, "meta");
1469 struct membuffer *buf;
1470 void *new_udta_buffer;
1471 uint32_t new_udta_size;
1472 if (!create_udta(&f->meta, &new_udta_buffer, &new_udta_size))
1475 buf = membuffer_create();
1476 set_position(f, total_base);
1477 if (!membuffer_transfer_from_file(buf, f, total_size)) {
1478 free(new_udta_buffer);
1481 membuffer_write_atom(buf, "udta", new_udta_size,
1484 free(new_udta_buffer);
1486 *out_size = membuffer_get_size(buf);
1487 return membuffer_detach(buf);
1489 udta_offset = get_position(f);
1490 ret = read_int32(f, &udta_size);
1493 ret = find_atom_v2(f, udta_offset + 8, udta_size - 8, "meta", 4, "ilst");
1497 struct membuffer *buf;
1498 void *new_meta_buffer;
1499 uint32_t new_meta_size;
1501 if (!create_meta(&f->meta, &new_meta_buffer, &new_meta_size))
1504 buf = membuffer_create();
1505 set_position(f, total_base);
1506 if (!membuffer_transfer_from_file(buf, f,
1507 udta_offset - total_base)) {
1508 free(new_meta_buffer);
1512 membuffer_write_int32(buf, udta_size + 8 + new_meta_size);
1513 membuffer_write_atom_name(buf, "udta");
1514 if (!membuffer_transfer_from_file(buf, f, udta_size)) {
1515 free(new_meta_buffer);
1518 membuffer_write_atom(buf, "meta", new_meta_size,
1520 free(new_meta_buffer);
1522 *out_size = membuffer_get_size(buf);
1523 return membuffer_detach(buf);
1525 meta_offset = get_position(f);
1526 ret = read_int32(f, &meta_size);
1529 /* shouldn't happen, find_atom_v2 above takes care of it */
1530 if (!find_atom(f, meta_offset + 12, meta_size - 12, "ilst"))
1532 ilst_offset = get_position(f);
1533 ret = read_int32(f, &ilst_size);
1536 if (!create_ilst(&f->meta, &new_ilst_buffer, &new_ilst_size))
1538 size_delta = new_ilst_size - (ilst_size - 8);
1539 *out_size = total_size + size_delta;
1540 out_buffer = para_malloc(*out_size);
1542 set_position(f, total_base);
1543 ret = read_data(f, p_out, udta_offset - total_base);
1546 p_out += (uint32_t) (udta_offset - total_base);
1547 ret = read_int32(f, &tmp);
1550 *(uint32_t *)p_out = fix_byte_order_32(tmp + size_delta);
1552 ret = read_data(f, p_out, 4);
1556 ret = read_data(f, p_out, meta_offset - udta_offset - 8);
1559 p_out += (uint32_t) (meta_offset - udta_offset - 8);
1560 ret = read_int32(f, &tmp);
1563 *(uint32_t *)p_out = fix_byte_order_32(tmp + size_delta);
1565 ret = read_data(f, p_out, 4);
1569 ret = read_data(f, p_out, ilst_offset - meta_offset - 8);
1572 p_out += (uint32_t) (ilst_offset - meta_offset - 8);
1573 ret = read_int32(f, &tmp);
1576 *(uint32_t *)p_out = fix_byte_order_32(tmp + size_delta);
1578 ret = read_data(f, p_out, 4);
1582 memcpy(p_out, new_ilst_buffer, new_ilst_size);
1583 p_out += new_ilst_size;
1584 set_position(f, ilst_offset + ilst_size);
1585 ret = read_data(f, p_out, total_size
1586 - (ilst_offset - total_base) - ilst_size);
1589 free(new_ilst_buffer);
1593 static int32_t write_data(struct mp4 *f, void *data, uint32_t size)
1597 result = f->cb->write(f->cb->user_data, data, size);
1599 f->current_position += size;
1604 static int32_t write_int32(struct mp4 *f, uint32_t data)
1607 write_u32_be(temp, data);
1608 return write_data(f, temp, sizeof(temp));
1611 int32_t mp4_meta_update(struct mp4 *f)
1613 void *new_moov_data;
1614 uint32_t new_moov_size;
1617 new_moov_data = modify_moov(f, &new_moov_size);
1618 if (!new_moov_data ) {
1622 /* copy moov atom to end of the file */
1623 if (f->last_atom != ATOM_MOOV) {
1624 char *free_data = "free";
1626 /* rename old moov to free */
1627 set_position(f, f->moov_offset + 4);
1628 write_data(f, free_data, 4);
1630 set_position(f, f->file_size);
1631 write_int32(f, new_moov_size + 8);
1632 write_data(f, "moov", 4);
1633 write_data(f, new_moov_data, new_moov_size);
1635 set_position(f, f->moov_offset);
1636 write_int32(f, new_moov_size + 8);
1637 write_data(f, "moov", 4);
1638 write_data(f, new_moov_data, new_moov_size);
1640 free(new_moov_data);
1641 f->cb->truncate(f->cb->user_data);
1645 static char *meta_find_by_name(const struct mp4 *f, const char *item)
1649 for (i = 0; i < f->meta.count; i++)
1650 if (!strcasecmp(f->meta.tags[i].item, item))
1651 return para_strdup(f->meta.tags[i].value);
1656 * Return the value of the artist meta tag of an mp4 file.
1658 * \param f Must not be NULL.
1660 * \return If the file does not contain this metadata tag, the function returns
1661 * NULL. Otherwise, a copy of the tag value is returned. The caller should free
1662 * this memory when it is no longer needed.
1664 char *mp4_meta_get_artist(const struct mp4 *f)
1666 return meta_find_by_name(f, "artist");
1670 * Return the value of the title meta tag of an mp4 file.
1672 * \param f See \ref mp4_meta_get_artist().
1673 * \return See \ref mp4_meta_get_artist().
1675 char *mp4_meta_get_title(const struct mp4 *f)
1677 return meta_find_by_name(f, "title");
1681 * Return the value of the date meta tag of an mp4 file.
1683 * \param f See \ref mp4_meta_get_artist().
1684 * \return See \ref mp4_meta_get_artist().
1686 char *mp4_meta_get_date(const struct mp4 *f)
1688 return meta_find_by_name(f, "date");
1692 * Return the value of the album meta tag of an mp4 file.
1694 * \param f See \ref mp4_meta_get_artist().
1695 * \return See \ref mp4_meta_get_artist().
1697 char *mp4_meta_get_album(const struct mp4 *f)
1699 return meta_find_by_name(f, "album");
1703 * Return the value of the comment meta tag of an mp4 file.
1705 * \param f See \ref mp4_meta_get_artist().
1706 * \return See \ref mp4_meta_get_artist().
1708 char *mp4_meta_get_comment(const struct mp4 *f)
1710 return meta_find_by_name(f, "comment");