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 open_file(const struct mp4_callback *cb, bool meta_only, struct mp4 **result)
908 uint8_t atom_type, header_size;
909 struct mp4 *f = para_calloc(sizeof(*f));
912 while ((ret = atom_read_header(f, &atom_type, &header_size, &size)) > 0) {
913 f->file_size += size;
914 f->last_atom = atom_type;
915 if (atom_type != ATOM_MOOV || size <= header_size) { /* skip */
916 set_position(f, get_position(f) + size - header_size);
919 f->moov_offset = get_position(f) - header_size;
921 ret = parse_sub_atoms(f, size - header_size, meta_only);
940 int mp4_open_read(const struct mp4_callback *cb, struct mp4 **result)
942 return open_file(cb, false, result);
945 void mp4_close(struct mp4 *f)
949 for (i = 0; i < f->total_tracks; i++) {
951 free(f->track[i]->stsz_table);
952 free(f->track[i]->stts_sample_count);
953 free(f->track[i]->stsc_first_chunk);
954 free(f->track[i]->stsc_samples_per_chunk);
955 free(f->track[i]->stco_chunk_offset);
959 for (i = 0; i < f->meta.count; i++) {
960 free(f->meta.tags[i].item);
961 free(f->meta.tags[i].value);
967 static int32_t chunk_of_sample(const struct mp4 *f, int32_t sample,
970 const struct mp4_track *t = f->audio_track;
971 uint32_t *fc = t->stsc_first_chunk, *spc = t->stsc_samples_per_chunk;
972 int32_t chunk1, chunk1samples, n, total, i;
974 for (i = 1, total = 0; i < t->stsc_entry_count; i++, total += n) {
975 n = (fc[i] - fc[i - 1]) * spc[i - 1]; /* number of samples */
976 if (sample < total + n)
980 chunk1samples = spc[i - 1];
981 if (chunk1samples != 0)
982 *chunk = (sample - total) / chunk1samples + chunk1;
985 return total + (*chunk - chunk1) * chunk1samples;
989 * Return the number of milliseconds of the audio track.
991 * \param f As returned by \ref mp4_open_read(), must not be NULL.
993 uint64_t mp4_get_duration(const struct mp4 *f)
995 const struct mp4_track *t = f->audio_track;
997 if (t->timeScale == 0)
999 return t->duration * 1000 / t->timeScale;
1002 int mp4_set_sample_position(struct mp4 *f, int32_t sample)
1004 const struct mp4_track *t = f->audio_track;
1005 int32_t offset, chunk, chunk_sample;
1006 uint32_t n, srs; /* sample range size */
1008 if (sample >= t->stsz_sample_count)
1009 return -ERRNO_TO_PARA_ERROR(EINVAL);
1010 chunk_sample = chunk_of_sample(f, sample, &chunk);
1011 if (t->stsz_sample_size > 0)
1012 srs = (sample - chunk_sample) * t->stsz_sample_size;
1014 for (srs = 0, n = chunk_sample; n < sample; n++)
1015 srs += t->stsz_table[n];
1017 if (t->stco_entry_count > 0 && chunk > t->stco_entry_count)
1018 offset = t->stco_chunk_offset[t->stco_entry_count - 1];
1019 else if (t->stco_entry_count > 0)
1020 offset = t->stco_chunk_offset[chunk - 1];
1023 set_position(f, offset + srs);
1027 int32_t mp4_get_sample_size(const struct mp4 *f, int sample)
1029 const struct mp4_track *t = f->audio_track;
1031 if (t->stsz_sample_size != 0)
1032 return t->stsz_sample_size;
1033 return t->stsz_table[sample];
1036 uint32_t mp4_get_sample_rate(const struct mp4 *f)
1038 return f->audio_track->sampleRate;
1041 uint32_t mp4_get_channel_count(const struct mp4 *f)
1043 return f->audio_track->channelCount ;
1046 int32_t mp4_num_samples(const struct mp4 *f)
1048 const struct mp4_track *t = f->audio_track;
1052 for (i = 0; i < t->stts_entry_count; i++)
1053 total += t->stts_sample_count[i];
1057 int mp4_open_meta(const struct mp4_callback *cb, struct mp4 **result)
1059 return open_file(cb, true, result);
1063 * Return the metadata of an mp4 file.
1065 * \param f As returned by either \ref mp4_open_read() or \ref mp4_open_meta().
1067 * The caller is allowed to add, delete or modify the entries of the returned
1068 * structure in order to pass the modified version to \ref mp4_meta_update().
1070 struct mp4_metadata *mp4_get_meta(struct mp4 *f)
1081 static struct membuffer *membuffer_create(void)
1083 struct membuffer *buf = para_calloc(sizeof(*buf));
1085 buf->allocated = 256;
1086 buf->data = para_malloc(buf->allocated);
1090 static void membuffer_write(struct membuffer *buf, const void *ptr,
1093 unsigned dest_size = buf->written + bytes;
1095 if (dest_size > buf->allocated) {
1097 buf->allocated <<= 1;
1098 } while (dest_size > buf->allocated);
1099 buf->data = para_realloc(buf->data, buf->allocated);
1103 memcpy((char *) buf->data + buf->written, ptr, bytes);
1104 buf->written += bytes;
1107 static void membuffer_write_atom_name(struct membuffer *buf, const char *data)
1109 membuffer_write(buf, data, 4);
1112 static void membuffer_write_int32(struct membuffer *buf, uint32_t data)
1115 write_u32_be(temp, data);
1116 membuffer_write(buf, temp, 4);
1119 static void membuffer_write_std_tag(struct membuffer *buf, const char *name,
1122 uint32_t len = strlen(value);
1123 membuffer_write_int32(buf, 8 /* atom header */
1124 + 8 /* data atom header */
1125 + 8 /* flags + reserved */
1127 membuffer_write_atom_name(buf, name);
1128 membuffer_write_int32(buf, 8 /* data atom header */
1129 + 8 /* flags + reserved */
1131 membuffer_write_atom_name(buf, "data");
1132 membuffer_write_int32(buf, 1); /* flags */
1133 membuffer_write_int32(buf, 0); /* reserved */
1134 membuffer_write(buf, value, len);
1137 static unsigned membuffer_get_size(const struct membuffer *buf)
1139 return buf->written;
1142 static void *membuffer_detach(struct membuffer *buf)
1144 void *ret = para_realloc(buf->data, buf->written);
1149 static void *create_ilst(const struct mp4_metadata *meta, uint32_t *out_size)
1151 struct membuffer *buf = membuffer_create();
1154 for (n = 0; n < meta->count; n++) {
1155 struct mp4_tag *tag = meta->tags + n;
1156 const char *atom_name;
1157 if (!strcasecmp(tag->item, "title"))
1158 atom_name = "\xA9" "nam";
1159 else if (!strcasecmp(tag->item, "artist"))
1160 atom_name = "\xA9" "ART";
1161 else if (!strcasecmp(tag->item, "album"))
1162 atom_name = "\xA9" "alb";
1163 else if (!strcasecmp(tag->item, "date"))
1164 atom_name = "\xA9" "day";
1165 else if (!strcasecmp(tag->item, "comment"))
1166 atom_name = "\xA9" "cmt";
1169 membuffer_write_std_tag(buf, atom_name, tag->value);
1171 *out_size = membuffer_get_size(buf);
1172 return membuffer_detach(buf);
1175 static void membuffer_write_atom(struct membuffer *buf, const char *name, unsigned size,
1178 membuffer_write_int32(buf, size + 8);
1179 membuffer_write_atom_name(buf, name);
1180 membuffer_write(buf, data, size);
1183 static void *membuffer_get_ptr(const struct membuffer *buf)
1188 static bool membuffer_transfer_from_file(struct membuffer *buf, struct mp4 *src,
1191 unsigned oldsize = membuffer_get_size(buf);
1194 membuffer_write(buf, 0, bytes);
1195 bufptr = membuffer_get_ptr(buf);
1196 if (read_data(src, bufptr + oldsize, bytes) != 1) {
1204 static uint32_t create_meta(const struct mp4_metadata *meta, void **out_buffer,
1205 uint32_t * out_size)
1207 struct membuffer *buf;
1211 ilst_buffer = create_ilst(meta, &ilst_size);
1213 buf = membuffer_create();
1214 membuffer_write_int32(buf, 0);
1215 membuffer_write_atom(buf, "ilst", ilst_size, ilst_buffer);
1218 *out_size = membuffer_get_size(buf);
1219 *out_buffer = membuffer_detach(buf);
1223 static uint32_t create_udta(const struct mp4_metadata *meta, void **out_buffer,
1224 uint32_t * out_size)
1226 struct membuffer *buf;
1230 if (!create_meta(meta, &meta_buffer, &meta_size))
1233 buf = membuffer_create();
1235 membuffer_write_atom(buf, "meta", meta_size, meta_buffer);
1239 *out_size = membuffer_get_size(buf);
1240 *out_buffer = membuffer_detach(buf);
1244 static uint32_t fix_byte_order_32(uint32_t src)
1246 return read_u32_be(&src);
1249 static void *modify_moov(struct mp4 *f, uint32_t *out_size)
1252 uint64_t total_base = f->moov_offset + 8;
1253 uint32_t total_size = (uint32_t) (f->moov_size - 8);
1254 uint32_t new_ilst_size;
1255 void *new_ilst_buffer, *out_buffer;
1260 if (f->udta_size == 0) {
1261 struct membuffer *buf;
1262 void *new_udta_buffer;
1263 uint32_t new_udta_size;
1264 if (!create_udta(&f->meta, &new_udta_buffer, &new_udta_size))
1267 buf = membuffer_create();
1268 set_position(f, total_base);
1269 if (!membuffer_transfer_from_file(buf, f, total_size)) {
1270 free(new_udta_buffer);
1273 membuffer_write_atom(buf, "udta", new_udta_size,
1276 free(new_udta_buffer);
1278 *out_size = membuffer_get_size(buf);
1279 return membuffer_detach(buf);
1281 if (f->meta_size == 0 || f->ilst_size == 0) {
1282 struct membuffer *buf;
1283 void *new_meta_buffer;
1284 uint32_t new_meta_size;
1286 if (!create_meta(&f->meta, &new_meta_buffer, &new_meta_size))
1289 buf = membuffer_create();
1290 set_position(f, total_base);
1291 if (!membuffer_transfer_from_file(buf, f,
1292 f->udta_offset - total_base)) {
1293 free(new_meta_buffer);
1297 membuffer_write_int32(buf, f->udta_size + 8 + new_meta_size);
1298 membuffer_write_atom_name(buf, "udta");
1299 if (!membuffer_transfer_from_file(buf, f, f->udta_size)) {
1300 free(new_meta_buffer);
1303 membuffer_write_atom(buf, "meta", new_meta_size,
1305 free(new_meta_buffer);
1307 *out_size = membuffer_get_size(buf);
1308 return membuffer_detach(buf);
1310 new_ilst_buffer = create_ilst(&f->meta, &new_ilst_size);
1311 size_delta = new_ilst_size - (f->ilst_size - 8);
1312 *out_size = total_size + size_delta;
1313 out_buffer = para_malloc(*out_size);
1315 set_position(f, total_base);
1316 ret = read_data(f, p_out, f->udta_offset - total_base);
1319 p_out += f->udta_offset - total_base;
1320 ret = read_int32(f, &tmp);
1323 *(uint32_t *)p_out = fix_byte_order_32(tmp + size_delta);
1325 ret = read_data(f, p_out, 4);
1329 ret = read_data(f, p_out, f->meta_offset - f->udta_offset - 8);
1332 p_out += f->meta_offset - f->udta_offset - 8;
1333 ret = read_int32(f, &tmp);
1336 *(uint32_t *)p_out = fix_byte_order_32(tmp + size_delta);
1338 ret = read_data(f, p_out, 4);
1342 ret = read_data(f, p_out, f->ilst_offset - f->meta_offset - 8);
1345 p_out += f->ilst_offset - f->meta_offset - 8;
1346 ret = read_int32(f, &tmp);
1349 *(uint32_t *)p_out = fix_byte_order_32(tmp + size_delta);
1351 ret = read_data(f, p_out, 4);
1355 memcpy(p_out, new_ilst_buffer, new_ilst_size);
1356 p_out += new_ilst_size;
1357 set_position(f, f->ilst_offset + f->ilst_size);
1358 ret = read_data(f, p_out, total_size - (f->ilst_offset - total_base)
1362 free(new_ilst_buffer);
1366 static int32_t write_data(struct mp4 *f, void *data, uint32_t size)
1370 result = f->cb->write(f->cb->user_data, data, size);
1372 f->current_position += size;
1377 static int32_t write_int32(struct mp4 *f, uint32_t data)
1380 write_u32_be(temp, data);
1381 return write_data(f, temp, sizeof(temp));
1384 int32_t mp4_meta_update(struct mp4 *f)
1386 void *new_moov_data;
1387 uint32_t new_moov_size;
1390 new_moov_data = modify_moov(f, &new_moov_size);
1391 if (!new_moov_data ) {
1395 /* copy moov atom to end of the file */
1396 if (f->last_atom != ATOM_MOOV) {
1397 char *free_data = "free";
1399 /* rename old moov to free */
1400 set_position(f, f->moov_offset + 4);
1401 write_data(f, free_data, 4);
1403 set_position(f, f->file_size);
1404 write_int32(f, new_moov_size + 8);
1405 write_data(f, "moov", 4);
1406 write_data(f, new_moov_data, new_moov_size);
1408 set_position(f, f->moov_offset);
1409 write_int32(f, new_moov_size + 8);
1410 write_data(f, "moov", 4);
1411 write_data(f, new_moov_data, new_moov_size);
1413 free(new_moov_data);
1414 f->cb->truncate(f->cb->user_data);
1418 static char *meta_find_by_name(const struct mp4 *f, const char *item)
1422 for (i = 0; i < f->meta.count; i++)
1423 if (!strcasecmp(f->meta.tags[i].item, item))
1424 return para_strdup(f->meta.tags[i].value);
1429 * Return the value of the artist meta tag of an mp4 file.
1431 * \param f Must not be NULL.
1433 * \return If the file does not contain this metadata tag, the function returns
1434 * NULL. Otherwise, a copy of the tag value is returned. The caller should free
1435 * this memory when it is no longer needed.
1437 char *mp4_meta_get_artist(const struct mp4 *f)
1439 return meta_find_by_name(f, "artist");
1443 * Return the value of the title meta tag of an mp4 file.
1445 * \param f See \ref mp4_meta_get_artist().
1446 * \return See \ref mp4_meta_get_artist().
1448 char *mp4_meta_get_title(const struct mp4 *f)
1450 return meta_find_by_name(f, "title");
1454 * Return the value of the date 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_date(const struct mp4 *f)
1461 return meta_find_by_name(f, "date");
1465 * Return the value of the album 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_album(const struct mp4 *f)
1472 return meta_find_by_name(f, "album");
1476 * Return the value of the comment 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_comment(const struct mp4 *f)
1483 return meta_find_by_name(f, "comment");