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"
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;
30 uint32_t stsc_entry_count;
31 uint32_t *stsc_first_chunk;
32 uint32_t *stsc_samples_per_chunk;
35 uint32_t stco_entry_count;
36 uint32_t *stco_chunk_offset;
42 #define MAX_TRACKS 1024
45 const struct mp4_callback *cb;
46 int64_t current_position;
60 /* incremental track index while reading the file */
63 struct mp4_track *track[MAX_TRACKS];
64 /* the first audio track found */
65 struct mp4_track *audio_track;
68 struct mp4_metadata meta;
72 * Returns -1, 0, or 1 on errors/EOF/success. Partial reads followed by EOF or
73 * read errors are treated as errors.
75 static int read_data(struct mp4 *f, void *data, size_t size)
78 ssize_t ret = f->cb->read(f->cb->user_data, data, size);
79 if (ret < 0 && errno == EINTR)
81 /* regard EAGAIN as an error as reads should be blocking. */
83 return ret < 0? -1 : 0;
84 f->current_position += ret;
90 static int read_int64(struct mp4 *f, uint64_t *result)
93 int ret = read_data(f, data, 8);
95 if (ret > 0 && result)
96 *result = read_u64_be(data);
100 static int read_int32(struct mp4 *f, uint32_t *result)
103 int ret = read_data(f, data, 4);
105 if (ret > 0 && result)
106 *result = read_u32_be(data);
110 static int read_int24(struct mp4 *f, uint32_t *result)
113 int ret = read_data(f, data, 3);
115 if (ret > 0 && result)
116 *result = read_u24_be(data);
120 static int read_int16(struct mp4 *f, uint16_t *result)
123 int ret = read_data(f, data, 2);
125 if (ret > 0 && result)
126 *result = read_u16_be(data);
130 static uint8_t read_int8(struct mp4 *f, uint8_t *result)
133 int ret = read_data(f, data, 1);
135 if (ret > 0 && result)
140 static bool atom_compare(int8_t a1, int8_t b1, int8_t c1, int8_t d1,
141 int8_t a2, int8_t b2, int8_t c2, int8_t d2)
143 return a1 == a2 && b1 == b2 && c1 == c2 && d1 == d2;
147 /* atoms with subatoms */
155 ATOM_ILST = 8, /* iTunes Metadata list */
166 ATOM_COMPILATION = 19,
176 /* atoms without subatoms */
182 ATOM_MDHD = 134, /* track header */
186 ATOM_STSD = 138, /* sample description box */
187 ATOM_STTS = 139, /* time to sample box */
188 ATOM_STSZ = 140, /* sample size box */
190 ATOM_STCO = 142, /* chunk offset box */
191 ATOM_STSC = 143, /* sample to chunk box */
196 ATOM_META = 148, /* iTunes Metadata box */
197 ATOM_NAME = 149, /* iTunes Metadata name box */
198 ATOM_DATA = 150, /* iTunes Metadata data box */
205 ATOM_ALBUM_ARTIST = 157,
206 ATOM_CONTENTGROUP = 158,
208 ATOM_DESCRIPTION = 160,
211 ATOM_EPISODENAME = 163,
212 ATOM_SORTTITLE = 164,
213 ATOM_SORTALBUM = 165,
214 ATOM_SORTARTIST = 166,
215 ATOM_SORTALBUMARTIST = 167,
216 ATOM_SORTWRITER = 168,
225 #define ATOM_FREE ATOM_UNKNOWN
226 #define ATOM_SKIP ATOM_UNKNOWN
228 #define COPYRIGHT_SYMBOL ((int8_t)0xA9)
230 static uint8_t atom_name_to_type(int8_t a, int8_t b, int8_t c, int8_t d)
233 if (atom_compare(a, b, c, d, 'm', 'o', 'o', 'v'))
235 else if (atom_compare(a, b, c, d, 'm', 'i', 'n', 'f'))
237 else if (atom_compare(a, b, c, d, 'm', 'd', 'i', 'a'))
239 else if (atom_compare(a, b, c, d, 'm', 'd', 'a', 't'))
241 else if (atom_compare(a, b, c, d, 'm', 'd', 'h', 'd'))
243 else if (atom_compare(a, b, c, d, 'm', 'v', 'h', 'd'))
245 else if (atom_compare(a, b, c, d, 'm', 'p', '4', 'a'))
247 else if (atom_compare(a, b, c, d, 'm', 'p', '4', 'v'))
249 else if (atom_compare(a, b, c, d, 'm', 'p', '4', 's'))
251 else if (atom_compare(a, b, c, d, 'm', 'e', 't', 'a'))
253 } else if (a == 't') {
254 if (atom_compare(a, b, c, d, 't', 'r', 'a', 'k'))
256 else if (atom_compare(a, b, c, d, 't', 'k', 'h', 'd'))
258 else if (atom_compare(a, b, c, d, 't', 'r', 'e', 'f'))
260 else if (atom_compare(a, b, c, d, 't', 'r', 'k', 'n'))
262 else if (atom_compare(a, b, c, d, 't', 'm', 'p', 'o'))
264 else if (atom_compare(a, b, c, d, 't', 'v', 'n', 'n'))
266 else if (atom_compare(a, b, c, d, 't', 'v', 's', 'h'))
268 else if (atom_compare(a, b, c, d, 't', 'v', 'e', 'n'))
269 return ATOM_EPISODENAME;
270 else if (atom_compare(a, b, c, d, 't', 'v', 's', 'n'))
272 else if (atom_compare(a, b, c, d, 't', 'v', 'e', 's'))
274 } else if (a == 's') {
275 if (atom_compare(a, b, c, d, 's', 't', 'b', 'l'))
277 else if (atom_compare(a, b, c, d, 's', 'm', 'h', 'd'))
279 else if (atom_compare(a, b, c, d, 's', 't', 's', 'd'))
281 else if (atom_compare(a, b, c, d, 's', 't', 't', 's'))
283 else if (atom_compare(a, b, c, d, 's', 't', 'c', 'o'))
285 else if (atom_compare(a, b, c, d, 's', 't', 's', 'c'))
287 else if (atom_compare(a, b, c, d, 's', 't', 's', 'z'))
289 else if (atom_compare(a, b, c, d, 's', 't', 'z', '2'))
291 else if (atom_compare(a, b, c, d, 's', 'k', 'i', 'p'))
293 else if (atom_compare(a, b, c, d, 's', 'i', 'n', 'f'))
295 else if (atom_compare(a, b, c, d, 's', 'c', 'h', 'i'))
297 else if (atom_compare(a, b, c, d, 's', 'o', 'n', 'm'))
298 return ATOM_SORTTITLE;
299 else if (atom_compare(a, b, c, d, 's', 'o', 'a', 'l'))
300 return ATOM_SORTALBUM;
301 else if (atom_compare(a, b, c, d, 's', 'o', 'a', 'r'))
302 return ATOM_SORTARTIST;
303 else if (atom_compare(a, b, c, d, 's', 'o', 'a', 'a'))
304 return ATOM_SORTALBUMARTIST;
305 else if (atom_compare(a, b, c, d, 's', 'o', 'c', 'o'))
306 return ATOM_SORTWRITER;
307 else if (atom_compare(a, b, c, d, 's', 'o', 's', 'n'))
308 return ATOM_SORTSHOW;
309 } else if (a == COPYRIGHT_SYMBOL) {
310 if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'n', 'a', 'm'))
312 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'A', 'R', 'T'))
314 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'w', 'r', 't'))
316 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'a', 'l', 'b'))
318 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'd', 'a', 'y'))
320 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 't', 'o', 'o'))
322 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'c', 'm', 't'))
324 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'g', 'e', 'n'))
326 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'g', 'r', 'p'))
327 return ATOM_CONTENTGROUP;
328 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'l', 'y', 'r'))
332 if (atom_compare(a, b, c, d, 'e', 'd', 't', 's'))
334 else if (atom_compare(a, b, c, d, 'e', 's', 'd', 's'))
336 else if (atom_compare(a, b, c, d, 'f', 't', 'y', 'p'))
338 else if (atom_compare(a, b, c, d, 'f', 'r', 'e', 'e'))
340 else if (atom_compare(a, b, c, d, 'h', 'm', 'h', 'd'))
342 else if (atom_compare(a, b, c, d, 'v', 'm', 'h', 'd'))
344 else if (atom_compare(a, b, c, d, 'u', 'd', 't', 'a'))
346 else if (atom_compare(a, b, c, d, 'i', 'l', 's', 't'))
348 else if (atom_compare(a, b, c, d, 'n', 'a', 'm', 'e'))
350 else if (atom_compare(a, b, c, d, 'd', 'a', 't', 'a'))
352 else if (atom_compare(a, b, c, d, 'd', 'i', 's', 'k'))
354 else if (atom_compare(a, b, c, d, 'g', 'n', 'r', 'e'))
356 else if (atom_compare(a, b, c, d, 'c', 'o', 'v', 'r'))
358 else if (atom_compare(a, b, c, d, 'c', 'p', 'i', 'l'))
359 return ATOM_COMPILATION;
360 else if (atom_compare(a, b, c, d, 'c', 't', 't', 's'))
362 else if (atom_compare(a, b, c, d, 'd', 'r', 'm', 's'))
364 else if (atom_compare(a, b, c, d, 'f', 'r', 'm', 'a'))
366 else if (atom_compare(a, b, c, d, 'p', 'r', 'i', 'v'))
368 else if (atom_compare(a, b, c, d, 'i', 'v', 'i', 'v'))
370 else if (atom_compare(a, b, c, d, 'u', 's', 'e', 'r'))
372 else if (atom_compare(a, b, c, d, 'k', 'e', 'y', ' '))
374 else if (atom_compare(a, b, c, d, 'a', 'A', 'R', 'T'))
375 return ATOM_ALBUM_ARTIST;
376 else if (atom_compare(a, b, c, d, 'd', 'e', 's', 'c'))
377 return ATOM_DESCRIPTION;
378 else if (atom_compare(a, b, c, d, 'p', 'c', 's', 't'))
384 /* read atom header, atom size is returned with header included. */
385 static int atom_read_header(struct mp4 *f, uint8_t *atom_type,
386 uint8_t *header_size, uint64_t *atom_size)
390 int8_t atom_header[8];
392 ret = read_data(f, atom_header, 8);
395 size = read_u32_be(atom_header);
396 if (size == 1) { /* 64 bit atom size */
399 ret = read_int64(f, atom_size);
408 *atom_type = atom_name_to_type(atom_header[4], atom_header[5],
409 atom_header[6], atom_header[7]);
413 static int64_t get_position(const struct mp4 *f)
415 return f->current_position;
418 static int32_t set_position(struct mp4 *f, int64_t position)
420 f->cb->seek(f->cb->user_data, position);
421 f->current_position = position;
426 static int read_stsz(struct mp4 *f)
432 if (f->total_tracks == 0)
434 t = f->track[f->total_tracks - 1];
435 ret = read_int8(f, NULL); /* version */
438 ret = read_int24(f, NULL); /* flags */
441 ret = read_int32(f, &t->stsz_sample_size);
444 ret = read_int32(f, &t->stsz_sample_count);
447 if (t->stsz_sample_size != 0)
449 t->stsz_table = para_malloc(t->stsz_sample_count * sizeof(int32_t));
450 for (i = 0; i < t->stsz_sample_count; i++) {
451 ret = read_int32(f, &t->stsz_table[i]);
458 static int read_stts(struct mp4 *f)
464 if (f->total_tracks == 0)
466 t = f->track[f->total_tracks - 1];
467 if (t->stts_entry_count)
469 ret = read_int8(f, NULL); /* version */
472 ret = read_int24(f, NULL); /* flags */
475 ret = read_int32(f, &t->stts_entry_count);
478 t->stts_sample_count = para_malloc(t->stts_entry_count
480 for (i = 0; i < t->stts_entry_count; i++) {
481 ret = read_int32(f, &t->stts_sample_count[i]);
484 ret = read_int32(f, NULL); /* sample delta */
491 static int read_stsc(struct mp4 *f)
497 if (f->total_tracks == 0)
499 t = f->track[f->total_tracks - 1];
501 ret = read_int8(f, NULL); /* version */
504 ret = read_int24(f, NULL); /* flags */
507 ret = read_int32(f, &t->stsc_entry_count);
510 t->stsc_first_chunk = para_malloc(t->stsc_entry_count * sizeof(int32_t));
511 t->stsc_samples_per_chunk = para_malloc(t->stsc_entry_count
513 for (i = 0; i < t->stsc_entry_count; i++) {
514 ret = read_int32(f, &t->stsc_first_chunk[i]);
517 ret = read_int32(f, &t->stsc_samples_per_chunk[i]);
520 ret = read_int32(f, NULL); /* sample desc index */
527 static int read_stco(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->stco_entry_count);
546 t->stco_chunk_offset = para_malloc(t->stco_entry_count
548 for (i = 0; i < t->stco_entry_count; i++) {
549 ret = read_int32(f, &t->stco_chunk_offset[i]);
556 static int read_mp4a(struct mp4 *f)
562 if (f->total_tracks == 0)
564 t = f->track[f->total_tracks - 1];
566 for (i = 0; i < 6; i++) {
567 ret = read_int8(f, NULL); /* reserved */
571 ret = read_int16(f, NULL); /* data_reference_index */
574 ret = read_int32(f, NULL); /* reserved */
577 ret = read_int32(f, NULL); /* reserved */
580 ret = read_int16(f, &t->channelCount);
583 ret = read_int16(f, NULL);
586 ret = read_int16(f, NULL);
589 ret = read_int16(f, NULL);
592 return read_int16(f, &t->sampleRate);
595 static int read_stsd(struct mp4 *f)
598 uint32_t i, entry_count;
601 if (f->total_tracks == 0)
603 t = f->track[f->total_tracks - 1];
604 ret = read_int8(f, NULL); /* version */
607 ret = read_int24(f, NULL); /* flags */
610 ret = read_int32(f, &entry_count);
613 for (i = 0; i < entry_count; i++) {
614 uint64_t skip = get_position(f);
616 uint8_t atom_type = 0;
617 ret = atom_read_header(f, &atom_type, NULL, &size);
621 if (!f->audio_track && atom_type == ATOM_MP4A) {
625 set_position(f, skip);
630 static const char *get_metadata_name(uint8_t atom_type)
633 case ATOM_TITLE: return "title";
634 case ATOM_ARTIST: return "artist";
635 case ATOM_ALBUM: return "album";
636 case ATOM_DATE: return "date";
637 case ATOM_COMMENT: return "comment";
638 default: return "unknown";
642 static int parse_tag(struct mp4 *f, uint8_t parent, int32_t size)
645 uint64_t subsize, sumsize;
654 set_position(f, destpos), sumsize += subsize
657 uint8_t header_size = 0;
658 ret = atom_read_header(f, &atom_type, &header_size, &subsize);
661 destpos = get_position(f) + subsize - header_size;
662 if (atom_type != ATOM_DATA)
664 ret = read_int8(f, NULL); /* version */
667 ret = read_int24(f, NULL); /* flags */
670 ret = read_int32(f, NULL); /* reserved */
673 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
674 if (subsize < header_size + 8 || subsize > UINT_MAX)
676 len = subsize - (header_size + 8);
678 value = para_malloc(len + 1);
679 ret = read_data(f, value, len);
685 return -ERRNO_TO_PARA_ERROR(EINVAL);
686 f->meta.tags = para_realloc(f->meta.tags, (f->meta.count + 1)
687 * sizeof(struct mp4_tag));
688 tag = f->meta.tags + f->meta.count;
689 tag->item = para_strdup(get_metadata_name(parent));
699 static int read_mdhd(struct mp4 *f)
705 if (f->total_tracks == 0)
707 t = f->track[f->total_tracks - 1];
709 ret = read_int32(f, &version);
713 ret = read_int64(f, NULL); /* creation-time */
716 ret = read_int64(f, NULL); /* modification-time */
719 ret = read_int32(f, &t->timeScale);
722 ret = read_int64(f, &t->duration);
725 } else { //version == 0
728 ret = read_int32(f, NULL); /* creation-time */
731 ret = read_int32(f, NULL); /* modification-time */
734 ret = read_int32(f, &t->timeScale);
737 ret = read_int32(f, &temp);
740 t->duration = (temp == (uint32_t) (-1))?
741 (uint64_t) (-1) : (uint64_t) (temp);
743 ret = read_int16(f, NULL);
746 ret = read_int16(f, NULL);
752 static int32_t read_ilst(struct mp4 *f, int32_t size)
755 uint64_t sumsize = 0;
757 while (sumsize < size) {
759 uint64_t subsize, destpos;
760 uint8_t header_size = 0;
761 ret = atom_read_header(f, &atom_type, &header_size, &subsize);
764 destpos = get_position(f) + subsize - header_size;
771 ret = parse_tag(f, atom_type, subsize - header_size);
775 set_position(f, destpos);
781 static int32_t read_meta(struct mp4 *f, uint64_t size)
784 uint64_t subsize, sumsize = 0;
786 uint8_t header_size = 0;
788 ret = read_int8(f, NULL); /* version */
791 ret = read_int24(f, NULL); /* flags */
794 while (sumsize < (size - (header_size + 4))) {
795 ret = atom_read_header(f, &atom_type, &header_size, &subsize);
798 if (subsize <= header_size + 4)
800 if (atom_type == ATOM_ILST) {
801 f->ilst_offset = get_position(f) - header_size;
802 f->ilst_size = subsize;
803 ret = read_ilst(f, subsize - (header_size + 4));
807 set_position(f, get_position(f) + subsize - header_size);
813 static int parse_leaf_atom(struct mp4 *f, uint64_t size, uint8_t header_size,
816 uint64_t dest_position = get_position(f) + size - 8;
817 int ret = 1; /* return success for atoms we don't care about */
820 case ATOM_STSZ: ret = read_stsz(f); break;
821 case ATOM_STTS: ret = read_stts(f); break;
822 case ATOM_STSC: ret = read_stsc(f); break;
823 case ATOM_STCO: ret = read_stco(f); break;
824 case ATOM_STSD: ret = read_stsd(f); break;
825 case ATOM_MDHD: ret = read_mdhd(f); break;
827 f->meta_offset = get_position(f) - header_size;
829 ret = read_meta(f, size);
832 set_position(f, dest_position);
836 static bool need_atom(uint8_t atom_type, bool meta_only)
838 /* these are needed in any case */
849 /* meta-only opens don't need anything else */
852 /* these are only required for regular opens */
864 /* parse atoms that are sub atoms of other atoms */
865 static int parse_sub_atoms(struct mp4 *f, uint64_t total_size, bool meta_only)
869 uint8_t atom_type = 0;
870 uint64_t counted_size = 0;
871 uint8_t header_size = 0;
873 while (counted_size < total_size) {
874 ret = atom_read_header(f, &atom_type, &header_size, &size);
879 counted_size += size;
880 if (atom_type == ATOM_TRAK) {
881 if (f->total_tracks >= MAX_TRACKS)
884 f->track[f->total_tracks - 1] = para_calloc(
885 sizeof(struct mp4_track));
886 } else if (atom_type == ATOM_UDTA) {
887 f->udta_offset = get_position(f) - header_size;
890 if (!need_atom(atom_type, meta_only)) {
891 set_position(f, get_position(f) + size - header_size);
894 if (atom_type < SUBATOMIC) /* atom contains subatoms */
895 ret = parse_sub_atoms(f, size - header_size, meta_only);
897 ret = parse_leaf_atom(f, size, header_size, atom_type);
904 static int parse_root_atoms(struct mp4 *f, bool meta_only)
908 uint8_t atom_type = 0;
909 uint8_t header_size = 0;
913 while ((ret = atom_read_header(f, &atom_type, &header_size, &size)) > 0) {
914 f->file_size += size;
915 f->last_atom = atom_type;
916 if (atom_type != ATOM_MOOV || size <= header_size) { /* skip */
917 set_position(f, get_position(f) + size - header_size);
920 f->moov_offset = get_position(f) - header_size;
922 ret = parse_sub_atoms(f, size - header_size, meta_only);
933 struct mp4 *mp4_open_read(const struct mp4_callback *cb)
936 struct mp4 *f = para_calloc(sizeof(struct mp4));
939 ret = parse_root_atoms(f, false);
947 void mp4_close(struct mp4 *f)
951 for (i = 0; i < f->total_tracks; i++) {
953 free(f->track[i]->stsz_table);
954 free(f->track[i]->stts_sample_count);
955 free(f->track[i]->stsc_first_chunk);
956 free(f->track[i]->stsc_samples_per_chunk);
957 free(f->track[i]->stco_chunk_offset);
961 for (i = 0; i < f->meta.count; i++) {
962 free(f->meta.tags[i].item);
963 free(f->meta.tags[i].value);
969 static int32_t chunk_of_sample(const struct mp4 *f, int32_t sample,
972 const struct mp4_track *t = f->audio_track;
973 uint32_t *fc = t->stsc_first_chunk, *spc = t->stsc_samples_per_chunk;
974 int32_t chunk1, chunk1samples, n, total, i;
976 for (i = 1, total = 0; i < t->stsc_entry_count; i++, total += n) {
977 n = (fc[i] - fc[i - 1]) * spc[i - 1]; /* number of samples */
978 if (sample < total + n)
982 chunk1samples = spc[i - 1];
983 if (chunk1samples != 0)
984 *chunk = (sample - total) / chunk1samples + chunk1;
987 return total + (*chunk - chunk1) * chunk1samples;
991 * Return the number of milliseconds of the audio track.
993 * \param f As returned by \ref mp4_open_read(), must not be NULL.
995 uint64_t mp4_get_duration(const struct mp4 *f)
997 const struct mp4_track *t = f->audio_track;
999 if (t->timeScale == 0)
1001 return t->duration * 1000 / t->timeScale;
1004 int mp4_set_sample_position(struct mp4 *f, int32_t sample)
1006 const struct mp4_track *t = f->audio_track;
1007 int32_t offset, chunk, chunk_sample;
1008 uint32_t n, srs; /* sample range size */
1010 if (sample >= t->stsz_sample_count)
1011 return -ERRNO_TO_PARA_ERROR(EINVAL);
1012 chunk_sample = chunk_of_sample(f, sample, &chunk);
1013 if (t->stsz_sample_size > 0)
1014 srs = (sample - chunk_sample) * t->stsz_sample_size;
1016 for (srs = 0, n = chunk_sample; n < sample; n++)
1017 srs += t->stsz_table[n];
1019 if (t->stco_entry_count > 0 && chunk > t->stco_entry_count)
1020 offset = t->stco_chunk_offset[t->stco_entry_count - 1];
1021 else if (t->stco_entry_count > 0)
1022 offset = t->stco_chunk_offset[chunk - 1];
1025 set_position(f, offset + srs);
1029 int32_t mp4_get_sample_size(const struct mp4 *f, int sample)
1031 const struct mp4_track *t = f->audio_track;
1033 if (t->stsz_sample_size != 0)
1034 return t->stsz_sample_size;
1035 return t->stsz_table[sample];
1038 uint32_t mp4_get_sample_rate(const struct mp4 *f)
1040 return f->audio_track->sampleRate;
1043 uint32_t mp4_get_channel_count(const struct mp4 *f)
1045 return f->audio_track->channelCount ;
1048 int32_t mp4_num_samples(const struct mp4 *f)
1050 const struct mp4_track *t = f->audio_track;
1054 for (i = 0; i < t->stts_entry_count; i++)
1055 total += t->stts_sample_count[i];
1059 struct mp4 *mp4_open_meta(const struct mp4_callback *cb)
1062 struct mp4 *f = para_calloc(sizeof(struct mp4));
1065 ret = parse_root_atoms(f, true);
1074 * Return the metadata of an mp4 file.
1076 * \param f As returned by either \ref mp4_open_read() or \ref mp4_open_meta().
1078 * The caller is allowed to add, delete or modify the entries of the returned
1079 * structure in order to pass the modified version to \ref mp4_meta_update().
1081 struct mp4_metadata *mp4_get_meta(struct mp4 *f)
1092 static struct membuffer *membuffer_create(void)
1094 struct membuffer *buf = para_calloc(sizeof(*buf));
1096 buf->allocated = 256;
1097 buf->data = para_malloc(buf->allocated);
1101 static void membuffer_write(struct membuffer *buf, const void *ptr,
1104 unsigned dest_size = buf->written + bytes;
1106 if (dest_size > buf->allocated) {
1108 buf->allocated <<= 1;
1109 } while (dest_size > buf->allocated);
1110 buf->data = para_realloc(buf->data, buf->allocated);
1114 memcpy((char *) buf->data + buf->written, ptr, bytes);
1115 buf->written += bytes;
1118 static void membuffer_write_atom_name(struct membuffer *buf, const char *data)
1120 membuffer_write(buf, data, 4);
1123 static void membuffer_write_int32(struct membuffer *buf, uint32_t data)
1126 write_u32_be(temp, data);
1127 membuffer_write(buf, temp, 4);
1130 static void membuffer_write_std_tag(struct membuffer *buf, const char *name,
1133 uint32_t len = strlen(value);
1134 membuffer_write_int32(buf, 8 /* atom header */
1135 + 8 /* data atom header */
1136 + 8 /* flags + reserved */
1138 membuffer_write_atom_name(buf, name);
1139 membuffer_write_int32(buf, 8 /* data atom header */
1140 + 8 /* flags + reserved */
1142 membuffer_write_atom_name(buf, "data");
1143 membuffer_write_int32(buf, 1); /* flags */
1144 membuffer_write_int32(buf, 0); /* reserved */
1145 membuffer_write(buf, value, len);
1148 static unsigned membuffer_get_size(const struct membuffer *buf)
1150 return buf->written;
1153 static void *membuffer_detach(struct membuffer *buf)
1155 void *ret = para_realloc(buf->data, buf->written);
1160 static void *create_ilst(const struct mp4_metadata *meta, uint32_t *out_size)
1162 struct membuffer *buf = membuffer_create();
1165 for (n = 0; n < meta->count; n++) {
1166 struct mp4_tag *tag = meta->tags + n;
1167 const char *atom_name;
1168 if (!strcasecmp(tag->item, "title"))
1169 atom_name = "\xA9" "nam";
1170 else if (!strcasecmp(tag->item, "artist"))
1171 atom_name = "\xA9" "ART";
1172 else if (!strcasecmp(tag->item, "album"))
1173 atom_name = "\xA9" "alb";
1174 else if (!strcasecmp(tag->item, "date"))
1175 atom_name = "\xA9" "day";
1176 else if (!strcasecmp(tag->item, "comment"))
1177 atom_name = "\xA9" "cmt";
1180 membuffer_write_std_tag(buf, atom_name, tag->value);
1182 *out_size = membuffer_get_size(buf);
1183 return membuffer_detach(buf);
1186 static void membuffer_write_atom(struct membuffer *buf, const char *name, unsigned size,
1189 membuffer_write_int32(buf, size + 8);
1190 membuffer_write_atom_name(buf, name);
1191 membuffer_write(buf, data, size);
1194 static void *membuffer_get_ptr(const struct membuffer *buf)
1199 static bool membuffer_transfer_from_file(struct membuffer *buf, struct mp4 *src,
1202 unsigned oldsize = membuffer_get_size(buf);
1205 membuffer_write(buf, 0, bytes);
1206 bufptr = membuffer_get_ptr(buf);
1207 if (read_data(src, bufptr + oldsize, bytes) != 1) {
1215 static uint32_t create_meta(const struct mp4_metadata *meta, void **out_buffer,
1216 uint32_t * out_size)
1218 struct membuffer *buf;
1222 ilst_buffer = create_ilst(meta, &ilst_size);
1224 buf = membuffer_create();
1225 membuffer_write_int32(buf, 0);
1226 membuffer_write_atom(buf, "ilst", ilst_size, ilst_buffer);
1229 *out_size = membuffer_get_size(buf);
1230 *out_buffer = membuffer_detach(buf);
1234 static uint32_t create_udta(const struct mp4_metadata *meta, void **out_buffer,
1235 uint32_t * out_size)
1237 struct membuffer *buf;
1241 if (!create_meta(meta, &meta_buffer, &meta_size))
1244 buf = membuffer_create();
1246 membuffer_write_atom(buf, "meta", meta_size, meta_buffer);
1250 *out_size = membuffer_get_size(buf);
1251 *out_buffer = membuffer_detach(buf);
1255 static uint32_t fix_byte_order_32(uint32_t src)
1257 return read_u32_be(&src);
1260 static void *modify_moov(struct mp4 *f, uint32_t *out_size)
1263 uint64_t total_base = f->moov_offset + 8;
1264 uint32_t total_size = (uint32_t) (f->moov_size - 8);
1265 uint32_t new_ilst_size;
1266 void *new_ilst_buffer, *out_buffer;
1271 if (f->udta_size == 0) {
1272 struct membuffer *buf;
1273 void *new_udta_buffer;
1274 uint32_t new_udta_size;
1275 if (!create_udta(&f->meta, &new_udta_buffer, &new_udta_size))
1278 buf = membuffer_create();
1279 set_position(f, total_base);
1280 if (!membuffer_transfer_from_file(buf, f, total_size)) {
1281 free(new_udta_buffer);
1284 membuffer_write_atom(buf, "udta", new_udta_size,
1287 free(new_udta_buffer);
1289 *out_size = membuffer_get_size(buf);
1290 return membuffer_detach(buf);
1292 if (f->meta_size == 0 || f->ilst_size == 0) {
1293 struct membuffer *buf;
1294 void *new_meta_buffer;
1295 uint32_t new_meta_size;
1297 if (!create_meta(&f->meta, &new_meta_buffer, &new_meta_size))
1300 buf = membuffer_create();
1301 set_position(f, total_base);
1302 if (!membuffer_transfer_from_file(buf, f,
1303 f->udta_offset - total_base)) {
1304 free(new_meta_buffer);
1308 membuffer_write_int32(buf, f->udta_size + 8 + new_meta_size);
1309 membuffer_write_atom_name(buf, "udta");
1310 if (!membuffer_transfer_from_file(buf, f, f->udta_size)) {
1311 free(new_meta_buffer);
1314 membuffer_write_atom(buf, "meta", new_meta_size,
1316 free(new_meta_buffer);
1318 *out_size = membuffer_get_size(buf);
1319 return membuffer_detach(buf);
1321 new_ilst_buffer = create_ilst(&f->meta, &new_ilst_size);
1322 size_delta = new_ilst_size - (f->ilst_size - 8);
1323 *out_size = total_size + size_delta;
1324 out_buffer = para_malloc(*out_size);
1326 set_position(f, total_base);
1327 ret = read_data(f, p_out, f->udta_offset - total_base);
1330 p_out += f->udta_offset - total_base;
1331 ret = read_int32(f, &tmp);
1334 *(uint32_t *)p_out = fix_byte_order_32(tmp + size_delta);
1336 ret = read_data(f, p_out, 4);
1340 ret = read_data(f, p_out, f->meta_offset - f->udta_offset - 8);
1343 p_out += f->meta_offset - f->udta_offset - 8;
1344 ret = read_int32(f, &tmp);
1347 *(uint32_t *)p_out = fix_byte_order_32(tmp + size_delta);
1349 ret = read_data(f, p_out, 4);
1353 ret = read_data(f, p_out, f->ilst_offset - f->meta_offset - 8);
1356 p_out += f->ilst_offset - f->meta_offset - 8;
1357 ret = read_int32(f, &tmp);
1360 *(uint32_t *)p_out = fix_byte_order_32(tmp + size_delta);
1362 ret = read_data(f, p_out, 4);
1366 memcpy(p_out, new_ilst_buffer, new_ilst_size);
1367 p_out += new_ilst_size;
1368 set_position(f, f->ilst_offset + f->ilst_size);
1369 ret = read_data(f, p_out, total_size - (f->ilst_offset - total_base)
1373 free(new_ilst_buffer);
1377 static int32_t write_data(struct mp4 *f, void *data, uint32_t size)
1381 result = f->cb->write(f->cb->user_data, data, size);
1383 f->current_position += size;
1388 static int32_t write_int32(struct mp4 *f, uint32_t data)
1391 write_u32_be(temp, data);
1392 return write_data(f, temp, sizeof(temp));
1395 int32_t mp4_meta_update(struct mp4 *f)
1397 void *new_moov_data;
1398 uint32_t new_moov_size;
1401 new_moov_data = modify_moov(f, &new_moov_size);
1402 if (!new_moov_data ) {
1406 /* copy moov atom to end of the file */
1407 if (f->last_atom != ATOM_MOOV) {
1408 char *free_data = "free";
1410 /* rename old moov to free */
1411 set_position(f, f->moov_offset + 4);
1412 write_data(f, free_data, 4);
1414 set_position(f, f->file_size);
1415 write_int32(f, new_moov_size + 8);
1416 write_data(f, "moov", 4);
1417 write_data(f, new_moov_data, new_moov_size);
1419 set_position(f, f->moov_offset);
1420 write_int32(f, new_moov_size + 8);
1421 write_data(f, "moov", 4);
1422 write_data(f, new_moov_data, new_moov_size);
1424 free(new_moov_data);
1425 f->cb->truncate(f->cb->user_data);
1429 static char *meta_find_by_name(const struct mp4 *f, const char *item)
1433 for (i = 0; i < f->meta.count; i++)
1434 if (!strcasecmp(f->meta.tags[i].item, item))
1435 return para_strdup(f->meta.tags[i].value);
1440 * Return the value of the artist meta tag of an mp4 file.
1442 * \param f Must not be NULL.
1444 * \return If the file does not contain this metadata tag, the function returns
1445 * NULL. Otherwise, a copy of the tag value is returned. The caller should free
1446 * this memory when it is no longer needed.
1448 char *mp4_meta_get_artist(const struct mp4 *f)
1450 return meta_find_by_name(f, "artist");
1454 * Return the value of the title meta tag of an mp4 file.
1456 * \param f See \ref mp4_meta_get_artist().
1457 * \return See \ref mp4_meta_get_artist().
1459 char *mp4_meta_get_title(const struct mp4 *f)
1461 return meta_find_by_name(f, "title");
1465 * Return the value of the date meta tag of an mp4 file.
1467 * \param f See \ref mp4_meta_get_artist().
1468 * \return See \ref mp4_meta_get_artist().
1470 char *mp4_meta_get_date(const struct mp4 *f)
1472 return meta_find_by_name(f, "date");
1476 * Return the value of the album meta tag of an mp4 file.
1478 * \param f See \ref mp4_meta_get_artist().
1479 * \return See \ref mp4_meta_get_artist().
1481 char *mp4_meta_get_album(const struct mp4 *f)
1483 return meta_find_by_name(f, "album");
1487 * Return the value of the comment meta tag of an mp4 file.
1489 * \param f See \ref mp4_meta_get_artist().
1490 * \return See \ref mp4_meta_get_artist().
1492 char *mp4_meta_get_comment(const struct mp4 *f)
1494 return meta_find_by_name(f, "comment");