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)
1060 int ret = open_file(cb, true, &f);
1064 if (f->udta_size == 0 || f->meta_size == 0 || f->ilst_size == 0) {
1067 return -E_MP4_MISSING_ATOM;
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)
1086 /** Total length of an on-disk metadata tag. */
1087 #define TAG_LEN(_len) (24 + (_len))
1088 static void create_ilst(const struct mp4_metadata *meta, uint8_t *out)
1090 for (unsigned n = 0; n < meta->count; n++) {
1091 struct mp4_tag *tag = meta->tags + n;
1092 unsigned len = strlen(tag->value);
1093 const char *atom_name;
1095 if (!strcasecmp(tag->item, "title"))
1096 atom_name = "\xA9" "nam";
1097 else if (!strcasecmp(tag->item, "artist"))
1098 atom_name = "\xA9" "ART";
1099 else if (!strcasecmp(tag->item, "album"))
1100 atom_name = "\xA9" "alb";
1101 else if (!strcasecmp(tag->item, "date"))
1102 atom_name = "\xA9" "day";
1103 else if (!strcasecmp(tag->item, "comment"))
1104 atom_name = "\xA9" "cmt";
1107 write_u32_be(out, TAG_LEN(len));
1108 memcpy(out + 4, atom_name, 4);
1109 write_u32_be(out + 8, 8 /* data atom header */
1110 + 8 /* flags + reserved */
1112 memcpy(out + 12, "data", 4);
1113 write_u32_be(out + 16, 1); /* flags */
1114 write_u32_be(out + 20, 0); /* reserved */
1115 memcpy(out + 24, tag->value, len);
1116 out += TAG_LEN(len);
1120 static uint32_t fix_byte_order_32(uint32_t src)
1122 return read_u32_be(&src);
1125 static void *modify_moov(struct mp4 *f, uint32_t *out_size)
1128 uint64_t total_base = f->moov_offset + 8;
1129 uint32_t total_size = (uint32_t) (f->moov_size - 8);
1130 uint32_t new_ilst_size = 0;
1136 for (unsigned n = 0; n < f->meta.count; n++)
1137 new_ilst_size += TAG_LEN(strlen(f->meta.tags[n].value));
1138 size_delta = new_ilst_size - (f->ilst_size - 8);
1139 *out_size = total_size + size_delta;
1140 out_buffer = para_malloc(*out_size);
1142 set_position(f, total_base);
1143 ret = read_data(f, p_out, f->udta_offset - total_base);
1146 p_out += f->udta_offset - total_base;
1147 ret = read_int32(f, &tmp);
1150 *(uint32_t *)p_out = fix_byte_order_32(tmp + size_delta);
1152 ret = read_data(f, p_out, 4);
1156 ret = read_data(f, p_out, f->meta_offset - f->udta_offset - 8);
1159 p_out += f->meta_offset - f->udta_offset - 8;
1160 ret = read_int32(f, &tmp);
1163 *(uint32_t *)p_out = fix_byte_order_32(tmp + size_delta);
1165 ret = read_data(f, p_out, 4);
1169 ret = read_data(f, p_out, f->ilst_offset - f->meta_offset - 8);
1172 p_out += f->ilst_offset - f->meta_offset - 8;
1173 ret = read_int32(f, &tmp);
1176 *(uint32_t *)p_out = fix_byte_order_32(tmp + size_delta);
1178 ret = read_data(f, p_out, 4);
1182 create_ilst(&f->meta, p_out);
1183 p_out += new_ilst_size;
1184 set_position(f, f->ilst_offset + f->ilst_size);
1185 ret = read_data(f, p_out, total_size - (f->ilst_offset - total_base)
1192 static int32_t write_data(struct mp4 *f, void *data, uint32_t size)
1196 result = f->cb->write(f->cb->user_data, data, size);
1198 f->current_position += size;
1203 static int32_t write_int32(struct mp4 *f, uint32_t data)
1206 write_u32_be(temp, data);
1207 return write_data(f, temp, sizeof(temp));
1210 int32_t mp4_meta_update(struct mp4 *f)
1212 void *new_moov_data;
1213 uint32_t new_moov_size;
1216 new_moov_data = modify_moov(f, &new_moov_size);
1217 if (!new_moov_data ) {
1221 /* copy moov atom to end of the file */
1222 if (f->last_atom != ATOM_MOOV) {
1223 char *free_data = "free";
1225 /* rename old moov to free */
1226 set_position(f, f->moov_offset + 4);
1227 write_data(f, free_data, 4);
1229 set_position(f, f->file_size);
1230 write_int32(f, new_moov_size + 8);
1231 write_data(f, "moov", 4);
1232 write_data(f, new_moov_data, new_moov_size);
1234 set_position(f, f->moov_offset);
1235 write_int32(f, new_moov_size + 8);
1236 write_data(f, "moov", 4);
1237 write_data(f, new_moov_data, new_moov_size);
1239 free(new_moov_data);
1240 f->cb->truncate(f->cb->user_data);
1244 static char *meta_find_by_name(const struct mp4 *f, const char *item)
1248 for (i = 0; i < f->meta.count; i++)
1249 if (!strcasecmp(f->meta.tags[i].item, item))
1250 return para_strdup(f->meta.tags[i].value);
1255 * Return the value of the artist meta tag of an mp4 file.
1257 * \param f Must not be NULL.
1259 * \return If the file does not contain this metadata tag, the function returns
1260 * NULL. Otherwise, a copy of the tag value is returned. The caller should free
1261 * this memory when it is no longer needed.
1263 char *mp4_meta_get_artist(const struct mp4 *f)
1265 return meta_find_by_name(f, "artist");
1269 * Return the value of the title meta tag of an mp4 file.
1271 * \param f See \ref mp4_meta_get_artist().
1272 * \return See \ref mp4_meta_get_artist().
1274 char *mp4_meta_get_title(const struct mp4 *f)
1276 return meta_find_by_name(f, "title");
1280 * Return the value of the date meta tag of an mp4 file.
1282 * \param f See \ref mp4_meta_get_artist().
1283 * \return See \ref mp4_meta_get_artist().
1285 char *mp4_meta_get_date(const struct mp4 *f)
1287 return meta_find_by_name(f, "date");
1291 * Return the value of the album meta tag of an mp4 file.
1293 * \param f See \ref mp4_meta_get_artist().
1294 * \return See \ref mp4_meta_get_artist().
1296 char *mp4_meta_get_album(const struct mp4 *f)
1298 return meta_find_by_name(f, "album");
1302 * Return the value of the comment meta tag of an mp4 file.
1304 * \param f See \ref mp4_meta_get_artist().
1305 * \return See \ref mp4_meta_get_artist().
1307 char *mp4_meta_get_comment(const struct mp4 *f)
1309 return meta_find_by_name(f, "comment");