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;
53 /* incremental track index while reading the file */
56 struct mp4_track *track[MAX_TRACKS];
57 /* the first audio track found */
58 struct mp4_track *audio_track;
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 int32_t set_position(struct mp4 *f, int64_t position)
413 f->cb->seek(f->cb->user_data, position);
414 f->current_position = position;
419 static int read_stsz(struct mp4 *f)
425 if (f->total_tracks == 0)
427 t = f->track[f->total_tracks - 1];
428 ret = read_int8(f, NULL); /* version */
431 ret = read_int24(f, NULL); /* flags */
434 ret = read_int32(f, &t->stsz_sample_size);
437 ret = read_int32(f, &t->stsz_sample_count);
440 if (t->stsz_sample_size != 0)
442 t->stsz_table = para_malloc(t->stsz_sample_count * sizeof(int32_t));
443 for (i = 0; i < t->stsz_sample_count; i++) {
444 ret = read_int32(f, &t->stsz_table[i]);
451 static int read_stts(struct mp4 *f)
457 if (f->total_tracks == 0)
459 t = f->track[f->total_tracks - 1];
460 if (t->stts_entry_count)
462 ret = read_int8(f, NULL); /* version */
465 ret = read_int24(f, NULL); /* flags */
468 ret = read_int32(f, &t->stts_entry_count);
471 t->stts_sample_count = para_malloc(t->stts_entry_count
473 for (i = 0; i < t->stts_entry_count; i++) {
474 ret = read_int32(f, &t->stts_sample_count[i]);
477 ret = read_int32(f, NULL); /* sample delta */
484 static int read_stsc(struct mp4 *f)
490 if (f->total_tracks == 0)
492 t = f->track[f->total_tracks - 1];
494 ret = read_int8(f, NULL); /* version */
497 ret = read_int24(f, NULL); /* flags */
500 ret = read_int32(f, &t->stsc_entry_count);
503 t->stsc_first_chunk = para_malloc(t->stsc_entry_count * sizeof(int32_t));
504 t->stsc_samples_per_chunk = para_malloc(t->stsc_entry_count
506 for (i = 0; i < t->stsc_entry_count; i++) {
507 ret = read_int32(f, &t->stsc_first_chunk[i]);
510 ret = read_int32(f, &t->stsc_samples_per_chunk[i]);
513 ret = read_int32(f, NULL); /* sample desc index */
520 static int read_stco(struct mp4 *f)
526 if (f->total_tracks == 0)
528 t = f->track[f->total_tracks - 1];
530 ret = read_int8(f, NULL); /* version */
533 ret = read_int24(f, NULL); /* flags */
536 ret = read_int32(f, &t->stco_entry_count);
539 t->stco_chunk_offset = para_malloc(t->stco_entry_count
541 for (i = 0; i < t->stco_entry_count; i++) {
542 ret = read_int32(f, &t->stco_chunk_offset[i]);
549 static int read_mp4a(struct mp4 *f)
553 uint8_t atom_type = 0;
554 uint8_t header_size = 0;
557 if (f->total_tracks == 0)
559 t = f->track[f->total_tracks - 1];
561 for (i = 0; i < 6; i++) {
562 ret = read_int8(f, NULL); /* reserved */
566 ret = read_int16(f, NULL); /* data_reference_index */
569 ret = read_int32(f, NULL); /* reserved */
572 ret = read_int32(f, NULL); /* reserved */
575 ret = read_int16(f, &t->channelCount);
578 ret = read_int16(f, NULL);
581 ret = read_int16(f, NULL);
584 ret = read_int16(f, NULL);
587 ret = read_int16(f, &t->sampleRate);
590 ret = read_int16(f, NULL);
593 return atom_read_header(f, &atom_type, &header_size, NULL);
596 static int read_stsd(struct mp4 *f)
599 uint32_t i, entry_count;
600 uint8_t header_size = 0;
603 if (f->total_tracks == 0)
605 t = f->track[f->total_tracks - 1];
606 ret = read_int8(f, NULL); /* version */
609 ret = read_int24(f, NULL); /* flags */
612 ret = read_int32(f, &entry_count);
615 for (i = 0; i < entry_count; i++) {
616 uint64_t skip = get_position(f);
618 uint8_t atom_type = 0;
619 ret = atom_read_header(f, &atom_type, &header_size, &size);
623 if (!f->audio_track && atom_type == ATOM_MP4A) {
627 set_position(f, skip);
632 static int32_t tag_add_field(struct mp4_metadata *meta, const char *item,
633 const char *value, int32_t len)
635 meta->tags = para_realloc(meta->tags,
636 (meta->count + 1) * sizeof(struct mp4_tag));
637 meta->tags[meta->count].item = para_strdup(item);
638 meta->tags[meta->count].len = len;
640 meta->tags[meta->count].value = para_malloc(len + 1);
641 memcpy(meta->tags[meta->count].value, value, len);
642 meta->tags[meta->count].value[len] = 0;
644 meta->tags[meta->count].value = para_strdup(value);
650 static int read_string(struct mp4 *f, uint32_t length, char **result)
652 char *str = para_malloc(length + 1);
653 int ret = read_data(f, str, length);
665 static const char *get_metadata_name(uint8_t atom_type)
668 case ATOM_TITLE: return "title";
669 case ATOM_ARTIST: return "artist";
670 case ATOM_ALBUM: return "album";
671 case ATOM_DATE: return "date";
672 case ATOM_COMMENT: return "comment";
673 default: return "unknown";
677 static int parse_tag(struct mp4 *f, uint8_t parent, int32_t size)
680 uint64_t subsize, sumsize;
688 set_position(f, destpos), sumsize += subsize
691 uint8_t header_size = 0;
692 ret = atom_read_header(f, &atom_type, &header_size, &subsize);
695 destpos = get_position(f) + subsize - header_size;
696 if (atom_type != ATOM_DATA)
698 ret = read_int8(f, NULL); /* version */
701 ret = read_int24(f, NULL); /* flags */
704 ret = read_int32(f, NULL); /* reserved */
708 ret = read_string(f, subsize - (header_size + 8), &data);
711 len = subsize - (header_size + 8);
715 tag_add_field(&f->meta, get_metadata_name(parent), data, len);
720 static int read_mdhd(struct mp4 *f)
726 if (f->total_tracks == 0)
728 t = f->track[f->total_tracks - 1];
730 ret = read_int32(f, &version);
734 ret = read_int64(f, NULL); /* creation-time */
737 ret = read_int64(f, NULL); /* modification-time */
740 ret = read_int32(f, &t->timeScale);
743 ret = read_int64(f, &t->duration);
746 } else { //version == 0
749 ret = read_int32(f, NULL); /* creation-time */
752 ret = read_int32(f, NULL); /* modification-time */
755 ret = read_int32(f, &t->timeScale);
758 ret = read_int32(f, &temp);
761 t->duration = (temp == (uint32_t) (-1))?
762 (uint64_t) (-1) : (uint64_t) (temp);
764 ret = read_int16(f, NULL);
767 ret = read_int16(f, NULL);
773 static int32_t read_ilst(struct mp4 *f, int32_t size)
776 uint64_t sumsize = 0;
778 while (sumsize < size) {
780 uint64_t subsize, destpos;
781 uint8_t header_size = 0;
782 ret = atom_read_header(f, &atom_type, &header_size, &subsize);
785 destpos = get_position(f) + subsize - header_size;
792 parse_tag(f, atom_type, subsize - header_size);
794 set_position(f, destpos);
800 static int32_t read_meta(struct mp4 *f, uint64_t size)
803 uint64_t subsize, sumsize = 0;
805 uint8_t header_size = 0;
807 ret = read_int8(f, NULL); /* version */
810 ret = read_int24(f, NULL); /* flags */
813 while (sumsize < (size - (header_size + 4))) {
814 ret = atom_read_header(f, &atom_type, &header_size, &subsize);
817 if (subsize <= header_size + 4)
819 if (atom_type == ATOM_ILST)
820 read_ilst(f, subsize - (header_size + 4));
822 set_position(f, get_position(f) + subsize - header_size);
828 static int atom_read(struct mp4 *f, uint64_t size, uint8_t atom_type)
830 uint64_t dest_position = get_position(f) + size - 8;
831 int ret = 1; /* return success for atoms we don't care about */
834 case ATOM_STSZ: ret = read_stsz(f); break;
835 case ATOM_STTS: ret = read_stts(f); break;
836 case ATOM_STSC: ret = read_stsc(f); break;
837 case ATOM_STCO: ret = read_stco(f); break;
838 case ATOM_STSD: ret = read_stsd(f); break;
839 case ATOM_MDHD: ret = read_mdhd(f); break;
840 case ATOM_META: ret = read_meta(f, size); break;
842 set_position(f, dest_position);
846 static bool need_atom(uint8_t atom_type, bool meta_only)
848 /* these are needed in any case */
858 /* meta-only opens don't need anything else */
861 /* these are only required for regular opens */
874 /* parse atoms that are sub atoms of other atoms */
875 static int parse_sub_atoms(struct mp4 *f, uint64_t total_size, bool meta_only)
879 uint8_t atom_type = 0;
880 uint64_t counted_size = 0;
881 uint8_t header_size = 0;
883 while (counted_size < total_size) {
884 ret = atom_read_header(f, &atom_type, &header_size, &size);
889 counted_size += size;
890 if (atom_type == ATOM_TRAK) {
891 if (f->total_tracks >= MAX_TRACKS)
894 f->track[f->total_tracks - 1] = para_calloc(
895 sizeof(struct mp4_track));
897 if (!need_atom(atom_type, meta_only)) {
898 set_position(f, get_position(f) + size - header_size);
901 if (atom_type < SUBATOMIC)
902 ret = parse_sub_atoms(f, size - header_size, meta_only);
904 ret = atom_read(f, size, atom_type);
911 static int parse_root_atoms(struct mp4 *f, bool 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;
923 if (atom_type != ATOM_MOOV || size <= header_size) { /* skip */
924 set_position(f, get_position(f) + size - header_size);
927 f->moov_offset = get_position(f) - header_size;
929 ret = parse_sub_atoms(f, size - header_size, meta_only);
940 struct mp4 *mp4_open_read(const struct mp4_callback *cb)
943 struct mp4 *f = para_calloc(sizeof(struct mp4));
946 ret = parse_root_atoms(f, false);
954 void mp4_close(struct mp4 *f)
958 for (i = 0; i < f->total_tracks; i++) {
960 free(f->track[i]->stsz_table);
961 free(f->track[i]->stts_sample_count);
962 free(f->track[i]->stsc_first_chunk);
963 free(f->track[i]->stsc_samples_per_chunk);
964 free(f->track[i]->stco_chunk_offset);
968 for (i = 0; i < f->meta.count; i++) {
969 free(f->meta.tags[i].item);
970 free(f->meta.tags[i].value);
976 static int32_t chunk_of_sample(const struct mp4 *f, int32_t sample,
979 const struct mp4_track *t = f->audio_track;
980 uint32_t *fc = t->stsc_first_chunk, *spc = t->stsc_samples_per_chunk;
981 int32_t chunk1, chunk1samples, n, total, i;
983 for (i = 1, total = 0; i < t->stsc_entry_count; i++, total += n) {
984 n = (fc[i] - fc[i - 1]) * spc[i - 1]; /* number of samples */
985 if (sample < total + n)
989 chunk1samples = spc[i - 1];
990 if (chunk1samples != 0)
991 *chunk = (sample - total) / chunk1samples + chunk1;
994 return total + (*chunk - chunk1) * chunk1samples;
998 * Return the number of milliseconds of the audio track.
1000 * \param f As returned by \ref mp4_open_read(), must not be NULL.
1002 uint64_t mp4_get_duration(const struct mp4 *f)
1004 const struct mp4_track *t = f->audio_track;
1006 if (t->timeScale == 0)
1008 return t->duration * 1000 / t->timeScale;
1011 int mp4_set_sample_position(struct mp4 *f, int32_t sample)
1013 const struct mp4_track *t = f->audio_track;
1014 int32_t offset, chunk, chunk_sample;
1015 uint32_t n, srs; /* sample range size */
1017 if (sample >= t->stsz_sample_count)
1018 return -ERRNO_TO_PARA_ERROR(EINVAL);
1019 chunk_sample = chunk_of_sample(f, sample, &chunk);
1020 if (t->stsz_sample_size > 0)
1021 srs = (sample - chunk_sample) * t->stsz_sample_size;
1023 for (srs = 0, n = chunk_sample; n < sample; n++)
1024 srs += t->stsz_table[n];
1026 if (t->stco_entry_count > 0 && chunk > t->stco_entry_count)
1027 offset = t->stco_chunk_offset[t->stco_entry_count - 1];
1028 else if (t->stco_entry_count > 0)
1029 offset = t->stco_chunk_offset[chunk - 1];
1032 set_position(f, offset + srs);
1036 int32_t mp4_get_sample_size(const struct mp4 *f, int sample)
1038 const struct mp4_track *t = f->audio_track;
1040 if (t->stsz_sample_size != 0)
1041 return t->stsz_sample_size;
1042 return t->stsz_table[sample];
1045 uint32_t mp4_get_sample_rate(const struct mp4 *f)
1047 return f->audio_track->sampleRate;
1050 uint32_t mp4_get_channel_count(const struct mp4 *f)
1052 return f->audio_track->channelCount ;
1055 int32_t mp4_num_samples(const struct mp4 *f)
1057 const struct mp4_track *t = f->audio_track;
1061 for (i = 0; i < t->stts_entry_count; i++)
1062 total += t->stts_sample_count[i];
1066 struct mp4 *mp4_open_meta(const struct mp4_callback *cb)
1069 struct mp4 *f = para_calloc(sizeof(struct mp4));
1072 ret = parse_root_atoms(f, true);
1081 * Return the metadata of an mp4 file.
1083 * \param f As returned by either \ref mp4_open_read() or \ref mp4_open_meta().
1085 * The caller is allowed to add, delete or modify the entries of the returned
1086 * structure in order to pass the modified version to \ref mp4_meta_update().
1088 struct mp4_metadata *mp4_get_meta(struct mp4 *f)
1093 static int find_atom(struct mp4 *f, uint64_t base, uint32_t size,
1096 uint32_t remaining = size;
1097 uint64_t atom_offset = base;
1104 set_position(f, atom_offset);
1108 ret = read_int32(f, &atom_size);
1111 if (atom_size > remaining || atom_size < 8)
1113 ret = read_data(f, atom_name, 4);
1116 if (!memcmp(atom_name, name, 4)) {
1117 set_position(f, atom_offset);
1120 remaining -= atom_size;
1121 atom_offset += atom_size;
1126 * Try to find atom <name> with atom <name_inside> in it. Besides -1/0/1 for
1127 * error, EOF and success, this function may return 2 to indicate that the
1128 * desired atoms were not found.
1130 static int find_atom_v2(struct mp4 *f, uint64_t base, uint32_t size,
1131 const char *name, uint32_t extraheaders, const char *name_inside)
1133 uint64_t first_base = (uint64_t) (-1);
1138 int ret = find_atom(f, base, size, name);
1142 mybase = get_position(f);
1143 ret = read_int32(f, &mysize);
1146 if (first_base == (uint64_t) (-1))
1147 first_base = mybase;
1149 if (mysize < 8 + extraheaders)
1152 if (find_atom (f, mybase + (8 + extraheaders),
1153 mysize - (8 + extraheaders), name_inside)) {
1154 set_position(f, mybase);
1162 if (first_base != (uint64_t)(-1)) {
1163 set_position(f, first_base);
1166 /* wanted atom inside not found */
1176 static struct membuffer *membuffer_create(void)
1178 struct membuffer *buf = para_calloc(sizeof(*buf));
1180 buf->allocated = 256;
1181 buf->data = para_malloc(buf->allocated);
1185 static void membuffer_write(struct membuffer *buf, const void *ptr,
1188 unsigned dest_size = buf->written + bytes;
1190 if (dest_size > buf->allocated) {
1192 buf->allocated <<= 1;
1193 } while (dest_size > buf->allocated);
1194 buf->data = para_realloc(buf->data, buf->allocated);
1198 memcpy((char *) buf->data + buf->written, ptr, bytes);
1199 buf->written += bytes;
1202 static void membuffer_write_atom_name(struct membuffer *buf, const char *data)
1204 membuffer_write(buf, data, 4);
1207 static void membuffer_write_int32(struct membuffer *buf, uint32_t data)
1210 write_u32_be(temp, data);
1211 membuffer_write(buf, temp, 4);
1214 static void membuffer_write_std_tag(struct membuffer *buf, const char *name,
1217 uint32_t len = strlen(value);
1218 membuffer_write_int32(buf, 8 /* atom header */
1219 + 8 /* data atom header */
1220 + 8 /* flags + reserved */
1222 membuffer_write_atom_name(buf, name);
1223 membuffer_write_int32(buf, 8 /* data atom header */
1224 + 8 /* flags + reserved */
1226 membuffer_write_atom_name(buf, "data");
1227 membuffer_write_int32(buf, 1); /* flags */
1228 membuffer_write_int32(buf, 0); /* reserved */
1229 membuffer_write(buf, value, len);
1232 static unsigned membuffer_get_size(const struct membuffer *buf)
1234 return buf->written;
1237 static void *membuffer_detach(struct membuffer *buf)
1239 void *ret = para_realloc(buf->data, buf->written);
1244 struct stdmeta_entry {
1249 static const char *find_standard_meta(const char *name)
1251 const struct stdmeta_entry stdmetas[] = {
1252 {"\xA9" "nam", "title"},
1253 {"\xA9" "ART", "artist"},
1254 {"\xA9" "alb", "album"},
1255 {"\xA9" "day", "date"},
1256 {"\xA9" "cmt", "comment"},
1259 for (unsigned n = 0; n < ARRAY_SIZE(stdmetas); n++)
1260 if (!strcasecmp(name, stdmetas[n].name))
1261 return stdmetas[n].atom;
1265 static uint32_t create_ilst(const struct mp4_metadata *meta, void **out_buffer,
1266 uint32_t * out_size)
1268 struct membuffer *buf = membuffer_create();
1271 for (metaptr = 0; metaptr < meta->count; metaptr++) {
1272 struct mp4_tag *tag = meta->tags + metaptr;
1273 const char *std_meta_atom = find_standard_meta(tag->item);
1275 membuffer_write_std_tag(buf, std_meta_atom, tag->value);
1277 PARA_ERROR_LOG("invalid tag item: %s\n", tag->item);
1279 *out_size = membuffer_get_size(buf);
1280 *out_buffer = membuffer_detach(buf);
1284 static void membuffer_write_atom(struct membuffer *buf, const char *name, unsigned size,
1287 membuffer_write_int32(buf, size + 8);
1288 membuffer_write_atom_name(buf, name);
1289 membuffer_write(buf, data, size);
1292 static void *membuffer_get_ptr(const struct membuffer *buf)
1297 static bool membuffer_transfer_from_file(struct membuffer *buf, struct mp4 *src,
1300 unsigned oldsize = membuffer_get_size(buf);
1303 membuffer_write(buf, 0, bytes);
1304 bufptr = membuffer_get_ptr(buf);
1305 if (read_data(src, bufptr + oldsize, bytes) != 1) {
1313 static uint32_t create_meta(const struct mp4_metadata *meta, void **out_buffer,
1314 uint32_t * out_size)
1316 struct membuffer *buf;
1320 if (!create_ilst(meta, &ilst_buffer, &ilst_size))
1323 buf = membuffer_create();
1325 membuffer_write_int32(buf, 0);
1326 membuffer_write_atom(buf, "ilst", ilst_size, ilst_buffer);
1329 *out_size = membuffer_get_size(buf);
1330 *out_buffer = membuffer_detach(buf);
1334 static uint32_t create_udta(const struct mp4_metadata *meta, void **out_buffer,
1335 uint32_t * out_size)
1337 struct membuffer *buf;
1341 if (!create_meta(meta, &meta_buffer, &meta_size))
1344 buf = membuffer_create();
1346 membuffer_write_atom(buf, "meta", meta_size, meta_buffer);
1350 *out_size = membuffer_get_size(buf);
1351 *out_buffer = membuffer_detach(buf);
1355 static uint32_t fix_byte_order_32(uint32_t src)
1357 return read_u32_be(&src);
1360 static void *modify_moov(struct mp4 *f, uint32_t *out_size)
1363 uint64_t total_base = f->moov_offset + 8;
1364 uint32_t total_size = (uint32_t) (f->moov_size - 8);
1365 uint64_t udta_offset, meta_offset, ilst_offset;
1366 uint32_t udta_size, meta_size, ilst_size;
1367 uint32_t new_ilst_size;
1368 void *new_ilst_buffer, *out_buffer;
1373 ret = find_atom_v2(f, total_base, total_size, "udta", 0, "meta");
1377 struct membuffer *buf;
1378 void *new_udta_buffer;
1379 uint32_t new_udta_size;
1380 if (!create_udta(&f->meta, &new_udta_buffer, &new_udta_size))
1383 buf = membuffer_create();
1384 set_position(f, total_base);
1385 if (!membuffer_transfer_from_file(buf, f, total_size)) {
1386 free(new_udta_buffer);
1389 membuffer_write_atom(buf, "udta", new_udta_size,
1392 free(new_udta_buffer);
1394 *out_size = membuffer_get_size(buf);
1395 return membuffer_detach(buf);
1397 udta_offset = get_position(f);
1398 ret = read_int32(f, &udta_size);
1401 ret = find_atom_v2(f, udta_offset + 8, udta_size - 8, "meta", 4, "ilst");
1405 struct membuffer *buf;
1406 void *new_meta_buffer;
1407 uint32_t new_meta_size;
1409 if (!create_meta(&f->meta, &new_meta_buffer, &new_meta_size))
1412 buf = membuffer_create();
1413 set_position(f, total_base);
1414 if (!membuffer_transfer_from_file(buf, f,
1415 udta_offset - total_base)) {
1416 free(new_meta_buffer);
1420 membuffer_write_int32(buf, udta_size + 8 + new_meta_size);
1421 membuffer_write_atom_name(buf, "udta");
1422 if (!membuffer_transfer_from_file(buf, f, udta_size)) {
1423 free(new_meta_buffer);
1426 membuffer_write_atom(buf, "meta", new_meta_size,
1428 free(new_meta_buffer);
1430 *out_size = membuffer_get_size(buf);
1431 return membuffer_detach(buf);
1433 meta_offset = get_position(f);
1434 ret = read_int32(f, &meta_size);
1437 /* shouldn't happen, find_atom_v2 above takes care of it */
1438 if (!find_atom(f, meta_offset + 12, meta_size - 12, "ilst"))
1440 ilst_offset = get_position(f);
1441 ret = read_int32(f, &ilst_size);
1444 if (!create_ilst(&f->meta, &new_ilst_buffer, &new_ilst_size))
1446 size_delta = new_ilst_size - (ilst_size - 8);
1447 *out_size = total_size + size_delta;
1448 out_buffer = para_malloc(*out_size);
1450 set_position(f, total_base);
1451 ret = read_data(f, p_out, udta_offset - total_base);
1454 p_out += (uint32_t) (udta_offset - total_base);
1455 ret = read_int32(f, &tmp);
1458 *(uint32_t *)p_out = fix_byte_order_32(tmp + size_delta);
1460 ret = read_data(f, p_out, 4);
1464 ret = read_data(f, p_out, meta_offset - udta_offset - 8);
1467 p_out += (uint32_t) (meta_offset - udta_offset - 8);
1468 ret = read_int32(f, &tmp);
1471 *(uint32_t *)p_out = fix_byte_order_32(tmp + size_delta);
1473 ret = read_data(f, p_out, 4);
1477 ret = read_data(f, p_out, ilst_offset - meta_offset - 8);
1480 p_out += (uint32_t) (ilst_offset - meta_offset - 8);
1481 ret = read_int32(f, &tmp);
1484 *(uint32_t *)p_out = fix_byte_order_32(tmp + size_delta);
1486 ret = read_data(f, p_out, 4);
1490 memcpy(p_out, new_ilst_buffer, new_ilst_size);
1491 p_out += new_ilst_size;
1492 set_position(f, ilst_offset + ilst_size);
1493 ret = read_data(f, p_out, total_size
1494 - (ilst_offset - total_base) - ilst_size);
1497 free(new_ilst_buffer);
1501 static int32_t write_data(struct mp4 *f, void *data, uint32_t size)
1505 result = f->cb->write(f->cb->user_data, data, size);
1507 f->current_position += size;
1512 static int32_t write_int32(struct mp4 *f, uint32_t data)
1515 write_u32_be(temp, data);
1516 return write_data(f, temp, sizeof(temp));
1519 int32_t mp4_meta_update(struct mp4 *f)
1521 void *new_moov_data;
1522 uint32_t new_moov_size;
1525 new_moov_data = modify_moov(f, &new_moov_size);
1526 if (!new_moov_data ) {
1530 /* copy moov atom to end of the file */
1531 if (f->last_atom != ATOM_MOOV) {
1532 char *free_data = "free";
1534 /* rename old moov to free */
1535 set_position(f, f->moov_offset + 4);
1536 write_data(f, free_data, 4);
1538 set_position(f, f->file_size);
1539 write_int32(f, new_moov_size + 8);
1540 write_data(f, "moov", 4);
1541 write_data(f, new_moov_data, new_moov_size);
1543 set_position(f, f->moov_offset);
1544 write_int32(f, new_moov_size + 8);
1545 write_data(f, "moov", 4);
1546 write_data(f, new_moov_data, new_moov_size);
1548 free(new_moov_data);
1549 f->cb->truncate(f->cb->user_data);
1553 static char *meta_find_by_name(const struct mp4 *f, const char *item)
1557 for (i = 0; i < f->meta.count; i++)
1558 if (!strcasecmp(f->meta.tags[i].item, item))
1559 return para_strdup(f->meta.tags[i].value);
1564 * Return the value of the artist meta tag of an mp4 file.
1566 * \param f Must not be NULL.
1568 * \return If the file does not contain this metadata tag, the function returns
1569 * NULL. Otherwise, a copy of the tag value is returned. The caller should free
1570 * this memory when it is no longer needed.
1572 char *mp4_meta_get_artist(const struct mp4 *f)
1574 return meta_find_by_name(f, "artist");
1578 * Return the value of the title meta tag of an mp4 file.
1580 * \param f See \ref mp4_meta_get_artist().
1581 * \return See \ref mp4_meta_get_artist().
1583 char *mp4_meta_get_title(const struct mp4 *f)
1585 return meta_find_by_name(f, "title");
1589 * Return the value of the date meta tag of an mp4 file.
1591 * \param f See \ref mp4_meta_get_artist().
1592 * \return See \ref mp4_meta_get_artist().
1594 char *mp4_meta_get_date(const struct mp4 *f)
1596 return meta_find_by_name(f, "date");
1600 * Return the value of the album meta tag of an mp4 file.
1602 * \param f See \ref mp4_meta_get_artist().
1603 * \return See \ref mp4_meta_get_artist().
1605 char *mp4_meta_get_album(const struct mp4 *f)
1607 return meta_find_by_name(f, "album");
1611 * Return the value of the comment meta tag of an mp4 file.
1613 * \param f See \ref mp4_meta_get_artist().
1614 * \return See \ref mp4_meta_get_artist().
1616 char *mp4_meta_get_comment(const struct mp4 *f)
1618 return meta_find_by_name(f, "comment");