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;
556 if (f->total_tracks == 0)
558 t = f->track[f->total_tracks - 1];
560 for (i = 0; i < 6; i++) {
561 ret = read_int8(f, NULL); /* reserved */
565 ret = read_int16(f, NULL); /* data_reference_index */
568 ret = read_int32(f, NULL); /* reserved */
571 ret = read_int32(f, NULL); /* reserved */
574 ret = read_int16(f, &t->channelCount);
577 ret = read_int16(f, NULL);
580 ret = read_int16(f, NULL);
583 ret = read_int16(f, NULL);
586 ret = read_int16(f, &t->sampleRate);
589 ret = read_int16(f, NULL);
592 return atom_read_header(f, &atom_type, NULL, NULL);
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 int32_t tag_add_field(struct mp4_metadata *meta, const char *item,
631 const char *value, int32_t len)
633 meta->tags = para_realloc(meta->tags,
634 (meta->count + 1) * sizeof(struct mp4_tag));
635 meta->tags[meta->count].item = para_strdup(item);
636 meta->tags[meta->count].len = len;
638 meta->tags[meta->count].value = para_malloc(len + 1);
639 memcpy(meta->tags[meta->count].value, value, len);
640 meta->tags[meta->count].value[len] = 0;
642 meta->tags[meta->count].value = para_strdup(value);
648 static int read_string(struct mp4 *f, uint32_t length, char **result)
650 char *str = para_malloc(length + 1);
651 int ret = read_data(f, str, length);
663 static const char *get_metadata_name(uint8_t atom_type)
666 case ATOM_TITLE: return "title";
667 case ATOM_ARTIST: return "artist";
668 case ATOM_ALBUM: return "album";
669 case ATOM_DATE: return "date";
670 case ATOM_COMMENT: return "comment";
671 default: return "unknown";
675 static int parse_tag(struct mp4 *f, uint8_t parent, int32_t size)
678 uint64_t subsize, sumsize;
686 set_position(f, destpos), sumsize += subsize
689 uint8_t header_size = 0;
690 ret = atom_read_header(f, &atom_type, &header_size, &subsize);
693 destpos = get_position(f) + subsize - header_size;
694 if (atom_type != ATOM_DATA)
696 ret = read_int8(f, NULL); /* version */
699 ret = read_int24(f, NULL); /* flags */
702 ret = read_int32(f, NULL); /* reserved */
706 ret = read_string(f, subsize - (header_size + 8), &data);
709 len = subsize - (header_size + 8);
713 tag_add_field(&f->meta, get_metadata_name(parent), data, len);
718 static int read_mdhd(struct mp4 *f)
724 if (f->total_tracks == 0)
726 t = f->track[f->total_tracks - 1];
728 ret = read_int32(f, &version);
732 ret = read_int64(f, NULL); /* creation-time */
735 ret = read_int64(f, NULL); /* modification-time */
738 ret = read_int32(f, &t->timeScale);
741 ret = read_int64(f, &t->duration);
744 } else { //version == 0
747 ret = read_int32(f, NULL); /* creation-time */
750 ret = read_int32(f, NULL); /* modification-time */
753 ret = read_int32(f, &t->timeScale);
756 ret = read_int32(f, &temp);
759 t->duration = (temp == (uint32_t) (-1))?
760 (uint64_t) (-1) : (uint64_t) (temp);
762 ret = read_int16(f, NULL);
765 ret = read_int16(f, NULL);
771 static int32_t read_ilst(struct mp4 *f, int32_t size)
774 uint64_t sumsize = 0;
776 while (sumsize < size) {
778 uint64_t subsize, destpos;
779 uint8_t header_size = 0;
780 ret = atom_read_header(f, &atom_type, &header_size, &subsize);
783 destpos = get_position(f) + subsize - header_size;
790 parse_tag(f, atom_type, subsize - header_size);
792 set_position(f, destpos);
798 static int32_t read_meta(struct mp4 *f, uint64_t size)
801 uint64_t subsize, sumsize = 0;
803 uint8_t header_size = 0;
805 ret = read_int8(f, NULL); /* version */
808 ret = read_int24(f, NULL); /* flags */
811 while (sumsize < (size - (header_size + 4))) {
812 ret = atom_read_header(f, &atom_type, &header_size, &subsize);
815 if (subsize <= header_size + 4)
817 if (atom_type == ATOM_ILST)
818 read_ilst(f, subsize - (header_size + 4));
820 set_position(f, get_position(f) + subsize - header_size);
826 static int parse_leaf_atom(struct mp4 *f, uint64_t size, uint8_t atom_type)
828 uint64_t dest_position = get_position(f) + size - 8;
829 int ret = 1; /* return success for atoms we don't care about */
832 case ATOM_STSZ: ret = read_stsz(f); break;
833 case ATOM_STTS: ret = read_stts(f); break;
834 case ATOM_STSC: ret = read_stsc(f); break;
835 case ATOM_STCO: ret = read_stco(f); break;
836 case ATOM_STSD: ret = read_stsd(f); break;
837 case ATOM_MDHD: ret = read_mdhd(f); break;
838 case ATOM_META: ret = read_meta(f, size); break;
840 set_position(f, dest_position);
844 static bool need_atom(uint8_t atom_type, bool meta_only)
846 /* these are needed in any case */
856 /* meta-only opens don't need anything else */
859 /* these are only required for regular opens */
872 /* parse atoms that are sub atoms of other atoms */
873 static int parse_sub_atoms(struct mp4 *f, uint64_t total_size, bool meta_only)
877 uint8_t atom_type = 0;
878 uint64_t counted_size = 0;
879 uint8_t header_size = 0;
881 while (counted_size < total_size) {
882 ret = atom_read_header(f, &atom_type, &header_size, &size);
887 counted_size += size;
888 if (atom_type == ATOM_TRAK) {
889 if (f->total_tracks >= MAX_TRACKS)
892 f->track[f->total_tracks - 1] = para_calloc(
893 sizeof(struct mp4_track));
895 if (!need_atom(atom_type, meta_only)) {
896 set_position(f, get_position(f) + size - header_size);
899 if (atom_type < SUBATOMIC) /* atom contains subatoms */
900 ret = parse_sub_atoms(f, size - header_size, meta_only);
902 ret = parse_leaf_atom(f, size, atom_type);
909 static int parse_root_atoms(struct mp4 *f, bool meta_only)
913 uint8_t atom_type = 0;
914 uint8_t header_size = 0;
918 while ((ret = atom_read_header(f, &atom_type, &header_size, &size)) > 0) {
919 f->file_size += size;
920 f->last_atom = atom_type;
921 if (atom_type != ATOM_MOOV || size <= header_size) { /* skip */
922 set_position(f, get_position(f) + size - header_size);
925 f->moov_offset = get_position(f) - header_size;
927 ret = parse_sub_atoms(f, size - header_size, meta_only);
938 struct mp4 *mp4_open_read(const struct mp4_callback *cb)
941 struct mp4 *f = para_calloc(sizeof(struct mp4));
944 ret = parse_root_atoms(f, false);
952 void mp4_close(struct mp4 *f)
956 for (i = 0; i < f->total_tracks; i++) {
958 free(f->track[i]->stsz_table);
959 free(f->track[i]->stts_sample_count);
960 free(f->track[i]->stsc_first_chunk);
961 free(f->track[i]->stsc_samples_per_chunk);
962 free(f->track[i]->stco_chunk_offset);
966 for (i = 0; i < f->meta.count; i++) {
967 free(f->meta.tags[i].item);
968 free(f->meta.tags[i].value);
974 static int32_t chunk_of_sample(const struct mp4 *f, int32_t sample,
977 const struct mp4_track *t = f->audio_track;
978 uint32_t *fc = t->stsc_first_chunk, *spc = t->stsc_samples_per_chunk;
979 int32_t chunk1, chunk1samples, n, total, i;
981 for (i = 1, total = 0; i < t->stsc_entry_count; i++, total += n) {
982 n = (fc[i] - fc[i - 1]) * spc[i - 1]; /* number of samples */
983 if (sample < total + n)
987 chunk1samples = spc[i - 1];
988 if (chunk1samples != 0)
989 *chunk = (sample - total) / chunk1samples + chunk1;
992 return total + (*chunk - chunk1) * chunk1samples;
996 * Return the number of milliseconds of the audio track.
998 * \param f As returned by \ref mp4_open_read(), must not be NULL.
1000 uint64_t mp4_get_duration(const struct mp4 *f)
1002 const struct mp4_track *t = f->audio_track;
1004 if (t->timeScale == 0)
1006 return t->duration * 1000 / t->timeScale;
1009 int mp4_set_sample_position(struct mp4 *f, int32_t sample)
1011 const struct mp4_track *t = f->audio_track;
1012 int32_t offset, chunk, chunk_sample;
1013 uint32_t n, srs; /* sample range size */
1015 if (sample >= t->stsz_sample_count)
1016 return -ERRNO_TO_PARA_ERROR(EINVAL);
1017 chunk_sample = chunk_of_sample(f, sample, &chunk);
1018 if (t->stsz_sample_size > 0)
1019 srs = (sample - chunk_sample) * t->stsz_sample_size;
1021 for (srs = 0, n = chunk_sample; n < sample; n++)
1022 srs += t->stsz_table[n];
1024 if (t->stco_entry_count > 0 && chunk > t->stco_entry_count)
1025 offset = t->stco_chunk_offset[t->stco_entry_count - 1];
1026 else if (t->stco_entry_count > 0)
1027 offset = t->stco_chunk_offset[chunk - 1];
1030 set_position(f, offset + srs);
1034 int32_t mp4_get_sample_size(const struct mp4 *f, int sample)
1036 const struct mp4_track *t = f->audio_track;
1038 if (t->stsz_sample_size != 0)
1039 return t->stsz_sample_size;
1040 return t->stsz_table[sample];
1043 uint32_t mp4_get_sample_rate(const struct mp4 *f)
1045 return f->audio_track->sampleRate;
1048 uint32_t mp4_get_channel_count(const struct mp4 *f)
1050 return f->audio_track->channelCount ;
1053 int32_t mp4_num_samples(const struct mp4 *f)
1055 const struct mp4_track *t = f->audio_track;
1059 for (i = 0; i < t->stts_entry_count; i++)
1060 total += t->stts_sample_count[i];
1064 struct mp4 *mp4_open_meta(const struct mp4_callback *cb)
1067 struct mp4 *f = para_calloc(sizeof(struct mp4));
1070 ret = parse_root_atoms(f, true);
1079 * Return the metadata of an mp4 file.
1081 * \param f As returned by either \ref mp4_open_read() or \ref mp4_open_meta().
1083 * The caller is allowed to add, delete or modify the entries of the returned
1084 * structure in order to pass the modified version to \ref mp4_meta_update().
1086 struct mp4_metadata *mp4_get_meta(struct mp4 *f)
1091 static int find_atom(struct mp4 *f, uint64_t base, uint32_t size,
1094 uint32_t remaining = size;
1095 uint64_t atom_offset = base;
1102 set_position(f, atom_offset);
1106 ret = read_int32(f, &atom_size);
1109 if (atom_size > remaining || atom_size < 8)
1111 ret = read_data(f, atom_name, 4);
1114 if (!memcmp(atom_name, name, 4)) {
1115 set_position(f, atom_offset);
1118 remaining -= atom_size;
1119 atom_offset += atom_size;
1124 * Try to find atom <name> with atom <name_inside> in it. Besides -1/0/1 for
1125 * error, EOF and success, this function may return 2 to indicate that the
1126 * desired atoms were not found.
1128 static int find_atom_v2(struct mp4 *f, uint64_t base, uint32_t size,
1129 const char *name, uint32_t extraheaders, const char *name_inside)
1131 uint64_t first_base = (uint64_t) (-1);
1136 int ret = find_atom(f, base, size, name);
1140 mybase = get_position(f);
1141 ret = read_int32(f, &mysize);
1144 if (first_base == (uint64_t) (-1))
1145 first_base = mybase;
1147 if (mysize < 8 + extraheaders)
1150 if (find_atom (f, mybase + (8 + extraheaders),
1151 mysize - (8 + extraheaders), name_inside)) {
1152 set_position(f, mybase);
1160 if (first_base != (uint64_t)(-1)) {
1161 set_position(f, first_base);
1164 /* wanted atom inside not found */
1174 static struct membuffer *membuffer_create(void)
1176 struct membuffer *buf = para_calloc(sizeof(*buf));
1178 buf->allocated = 256;
1179 buf->data = para_malloc(buf->allocated);
1183 static void membuffer_write(struct membuffer *buf, const void *ptr,
1186 unsigned dest_size = buf->written + bytes;
1188 if (dest_size > buf->allocated) {
1190 buf->allocated <<= 1;
1191 } while (dest_size > buf->allocated);
1192 buf->data = para_realloc(buf->data, buf->allocated);
1196 memcpy((char *) buf->data + buf->written, ptr, bytes);
1197 buf->written += bytes;
1200 static void membuffer_write_atom_name(struct membuffer *buf, const char *data)
1202 membuffer_write(buf, data, 4);
1205 static void membuffer_write_int32(struct membuffer *buf, uint32_t data)
1208 write_u32_be(temp, data);
1209 membuffer_write(buf, temp, 4);
1212 static void membuffer_write_std_tag(struct membuffer *buf, const char *name,
1215 uint32_t len = strlen(value);
1216 membuffer_write_int32(buf, 8 /* atom header */
1217 + 8 /* data atom header */
1218 + 8 /* flags + reserved */
1220 membuffer_write_atom_name(buf, name);
1221 membuffer_write_int32(buf, 8 /* data atom header */
1222 + 8 /* flags + reserved */
1224 membuffer_write_atom_name(buf, "data");
1225 membuffer_write_int32(buf, 1); /* flags */
1226 membuffer_write_int32(buf, 0); /* reserved */
1227 membuffer_write(buf, value, len);
1230 static unsigned membuffer_get_size(const struct membuffer *buf)
1232 return buf->written;
1235 static void *membuffer_detach(struct membuffer *buf)
1237 void *ret = para_realloc(buf->data, buf->written);
1242 struct stdmeta_entry {
1247 static const char *find_standard_meta(const char *name)
1249 const struct stdmeta_entry stdmetas[] = {
1250 {"\xA9" "nam", "title"},
1251 {"\xA9" "ART", "artist"},
1252 {"\xA9" "alb", "album"},
1253 {"\xA9" "day", "date"},
1254 {"\xA9" "cmt", "comment"},
1257 for (unsigned n = 0; n < ARRAY_SIZE(stdmetas); n++)
1258 if (!strcasecmp(name, stdmetas[n].name))
1259 return stdmetas[n].atom;
1263 static uint32_t create_ilst(const struct mp4_metadata *meta, void **out_buffer,
1264 uint32_t * out_size)
1266 struct membuffer *buf = membuffer_create();
1269 for (metaptr = 0; metaptr < meta->count; metaptr++) {
1270 struct mp4_tag *tag = meta->tags + metaptr;
1271 const char *std_meta_atom = find_standard_meta(tag->item);
1273 membuffer_write_std_tag(buf, std_meta_atom, tag->value);
1275 PARA_ERROR_LOG("invalid tag item: %s\n", tag->item);
1277 *out_size = membuffer_get_size(buf);
1278 *out_buffer = membuffer_detach(buf);
1282 static void membuffer_write_atom(struct membuffer *buf, const char *name, unsigned size,
1285 membuffer_write_int32(buf, size + 8);
1286 membuffer_write_atom_name(buf, name);
1287 membuffer_write(buf, data, size);
1290 static void *membuffer_get_ptr(const struct membuffer *buf)
1295 static bool membuffer_transfer_from_file(struct membuffer *buf, struct mp4 *src,
1298 unsigned oldsize = membuffer_get_size(buf);
1301 membuffer_write(buf, 0, bytes);
1302 bufptr = membuffer_get_ptr(buf);
1303 if (read_data(src, bufptr + oldsize, bytes) != 1) {
1311 static uint32_t create_meta(const struct mp4_metadata *meta, void **out_buffer,
1312 uint32_t * out_size)
1314 struct membuffer *buf;
1318 if (!create_ilst(meta, &ilst_buffer, &ilst_size))
1321 buf = membuffer_create();
1323 membuffer_write_int32(buf, 0);
1324 membuffer_write_atom(buf, "ilst", ilst_size, ilst_buffer);
1327 *out_size = membuffer_get_size(buf);
1328 *out_buffer = membuffer_detach(buf);
1332 static uint32_t create_udta(const struct mp4_metadata *meta, void **out_buffer,
1333 uint32_t * out_size)
1335 struct membuffer *buf;
1339 if (!create_meta(meta, &meta_buffer, &meta_size))
1342 buf = membuffer_create();
1344 membuffer_write_atom(buf, "meta", meta_size, meta_buffer);
1348 *out_size = membuffer_get_size(buf);
1349 *out_buffer = membuffer_detach(buf);
1353 static uint32_t fix_byte_order_32(uint32_t src)
1355 return read_u32_be(&src);
1358 static void *modify_moov(struct mp4 *f, uint32_t *out_size)
1361 uint64_t total_base = f->moov_offset + 8;
1362 uint32_t total_size = (uint32_t) (f->moov_size - 8);
1363 uint64_t udta_offset, meta_offset, ilst_offset;
1364 uint32_t udta_size, meta_size, ilst_size;
1365 uint32_t new_ilst_size;
1366 void *new_ilst_buffer, *out_buffer;
1371 ret = find_atom_v2(f, total_base, total_size, "udta", 0, "meta");
1375 struct membuffer *buf;
1376 void *new_udta_buffer;
1377 uint32_t new_udta_size;
1378 if (!create_udta(&f->meta, &new_udta_buffer, &new_udta_size))
1381 buf = membuffer_create();
1382 set_position(f, total_base);
1383 if (!membuffer_transfer_from_file(buf, f, total_size)) {
1384 free(new_udta_buffer);
1387 membuffer_write_atom(buf, "udta", new_udta_size,
1390 free(new_udta_buffer);
1392 *out_size = membuffer_get_size(buf);
1393 return membuffer_detach(buf);
1395 udta_offset = get_position(f);
1396 ret = read_int32(f, &udta_size);
1399 ret = find_atom_v2(f, udta_offset + 8, udta_size - 8, "meta", 4, "ilst");
1403 struct membuffer *buf;
1404 void *new_meta_buffer;
1405 uint32_t new_meta_size;
1407 if (!create_meta(&f->meta, &new_meta_buffer, &new_meta_size))
1410 buf = membuffer_create();
1411 set_position(f, total_base);
1412 if (!membuffer_transfer_from_file(buf, f,
1413 udta_offset - total_base)) {
1414 free(new_meta_buffer);
1418 membuffer_write_int32(buf, udta_size + 8 + new_meta_size);
1419 membuffer_write_atom_name(buf, "udta");
1420 if (!membuffer_transfer_from_file(buf, f, udta_size)) {
1421 free(new_meta_buffer);
1424 membuffer_write_atom(buf, "meta", new_meta_size,
1426 free(new_meta_buffer);
1428 *out_size = membuffer_get_size(buf);
1429 return membuffer_detach(buf);
1431 meta_offset = get_position(f);
1432 ret = read_int32(f, &meta_size);
1435 /* shouldn't happen, find_atom_v2 above takes care of it */
1436 if (!find_atom(f, meta_offset + 12, meta_size - 12, "ilst"))
1438 ilst_offset = get_position(f);
1439 ret = read_int32(f, &ilst_size);
1442 if (!create_ilst(&f->meta, &new_ilst_buffer, &new_ilst_size))
1444 size_delta = new_ilst_size - (ilst_size - 8);
1445 *out_size = total_size + size_delta;
1446 out_buffer = para_malloc(*out_size);
1448 set_position(f, total_base);
1449 ret = read_data(f, p_out, udta_offset - total_base);
1452 p_out += (uint32_t) (udta_offset - total_base);
1453 ret = read_int32(f, &tmp);
1456 *(uint32_t *)p_out = fix_byte_order_32(tmp + size_delta);
1458 ret = read_data(f, p_out, 4);
1462 ret = read_data(f, p_out, meta_offset - udta_offset - 8);
1465 p_out += (uint32_t) (meta_offset - udta_offset - 8);
1466 ret = read_int32(f, &tmp);
1469 *(uint32_t *)p_out = fix_byte_order_32(tmp + size_delta);
1471 ret = read_data(f, p_out, 4);
1475 ret = read_data(f, p_out, ilst_offset - meta_offset - 8);
1478 p_out += (uint32_t) (ilst_offset - meta_offset - 8);
1479 ret = read_int32(f, &tmp);
1482 *(uint32_t *)p_out = fix_byte_order_32(tmp + size_delta);
1484 ret = read_data(f, p_out, 4);
1488 memcpy(p_out, new_ilst_buffer, new_ilst_size);
1489 p_out += new_ilst_size;
1490 set_position(f, ilst_offset + ilst_size);
1491 ret = read_data(f, p_out, total_size
1492 - (ilst_offset - total_base) - ilst_size);
1495 free(new_ilst_buffer);
1499 static int32_t write_data(struct mp4 *f, void *data, uint32_t size)
1503 result = f->cb->write(f->cb->user_data, data, size);
1505 f->current_position += size;
1510 static int32_t write_int32(struct mp4 *f, uint32_t data)
1513 write_u32_be(temp, data);
1514 return write_data(f, temp, sizeof(temp));
1517 int32_t mp4_meta_update(struct mp4 *f)
1519 void *new_moov_data;
1520 uint32_t new_moov_size;
1523 new_moov_data = modify_moov(f, &new_moov_size);
1524 if (!new_moov_data ) {
1528 /* copy moov atom to end of the file */
1529 if (f->last_atom != ATOM_MOOV) {
1530 char *free_data = "free";
1532 /* rename old moov to free */
1533 set_position(f, f->moov_offset + 4);
1534 write_data(f, free_data, 4);
1536 set_position(f, f->file_size);
1537 write_int32(f, new_moov_size + 8);
1538 write_data(f, "moov", 4);
1539 write_data(f, new_moov_data, new_moov_size);
1541 set_position(f, f->moov_offset);
1542 write_int32(f, new_moov_size + 8);
1543 write_data(f, "moov", 4);
1544 write_data(f, new_moov_data, new_moov_size);
1546 free(new_moov_data);
1547 f->cb->truncate(f->cb->user_data);
1551 static char *meta_find_by_name(const struct mp4 *f, const char *item)
1555 for (i = 0; i < f->meta.count; i++)
1556 if (!strcasecmp(f->meta.tags[i].item, item))
1557 return para_strdup(f->meta.tags[i].value);
1562 * Return the value of the artist meta tag of an mp4 file.
1564 * \param f Must not be NULL.
1566 * \return If the file does not contain this metadata tag, the function returns
1567 * NULL. Otherwise, a copy of the tag value is returned. The caller should free
1568 * this memory when it is no longer needed.
1570 char *mp4_meta_get_artist(const struct mp4 *f)
1572 return meta_find_by_name(f, "artist");
1576 * Return the value of the title meta tag of an mp4 file.
1578 * \param f See \ref mp4_meta_get_artist().
1579 * \return See \ref mp4_meta_get_artist().
1581 char *mp4_meta_get_title(const struct mp4 *f)
1583 return meta_find_by_name(f, "title");
1587 * Return the value of the date meta tag of an mp4 file.
1589 * \param f See \ref mp4_meta_get_artist().
1590 * \return See \ref mp4_meta_get_artist().
1592 char *mp4_meta_get_date(const struct mp4 *f)
1594 return meta_find_by_name(f, "date");
1598 * Return the value of the album meta tag of an mp4 file.
1600 * \param f See \ref mp4_meta_get_artist().
1601 * \return See \ref mp4_meta_get_artist().
1603 char *mp4_meta_get_album(const struct mp4 *f)
1605 return meta_find_by_name(f, "album");
1609 * Return the value of the comment meta tag of an mp4 file.
1611 * \param f See \ref mp4_meta_get_artist().
1612 * \return See \ref mp4_meta_get_artist().
1614 char *mp4_meta_get_comment(const struct mp4 *f)
1616 return meta_find_by_name(f, "comment");