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)
555 if (f->total_tracks == 0)
557 t = f->track[f->total_tracks - 1];
559 for (i = 0; i < 6; i++) {
560 ret = read_int8(f, NULL); /* reserved */
564 ret = read_int16(f, NULL); /* data_reference_index */
567 ret = read_int32(f, NULL); /* reserved */
570 ret = read_int32(f, NULL); /* reserved */
573 ret = read_int16(f, &t->channelCount);
576 ret = read_int16(f, NULL);
579 ret = read_int16(f, NULL);
582 ret = read_int16(f, NULL);
585 return read_int16(f, &t->sampleRate);
588 static int read_stsd(struct mp4 *f)
591 uint32_t i, entry_count;
594 if (f->total_tracks == 0)
596 t = f->track[f->total_tracks - 1];
597 ret = read_int8(f, NULL); /* version */
600 ret = read_int24(f, NULL); /* flags */
603 ret = read_int32(f, &entry_count);
606 for (i = 0; i < entry_count; i++) {
607 uint64_t skip = get_position(f);
609 uint8_t atom_type = 0;
610 ret = atom_read_header(f, &atom_type, NULL, &size);
614 if (!f->audio_track && atom_type == ATOM_MP4A) {
618 set_position(f, skip);
623 static const char *get_metadata_name(uint8_t atom_type)
626 case ATOM_TITLE: return "title";
627 case ATOM_ARTIST: return "artist";
628 case ATOM_ALBUM: return "album";
629 case ATOM_DATE: return "date";
630 case ATOM_COMMENT: return "comment";
631 default: return "unknown";
635 static int parse_tag(struct mp4 *f, uint8_t parent, int32_t size)
638 uint64_t subsize, sumsize;
647 set_position(f, destpos), sumsize += subsize
650 uint8_t header_size = 0;
651 ret = atom_read_header(f, &atom_type, &header_size, &subsize);
654 destpos = get_position(f) + subsize - header_size;
655 if (atom_type != ATOM_DATA)
657 ret = read_int8(f, NULL); /* version */
660 ret = read_int24(f, NULL); /* flags */
663 ret = read_int32(f, NULL); /* reserved */
666 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
667 if (subsize < header_size + 8 || subsize > UINT_MAX)
669 len = subsize - (header_size + 8);
671 value = para_malloc(len + 1);
672 ret = read_data(f, value, len);
678 return -ERRNO_TO_PARA_ERROR(EINVAL);
679 f->meta.tags = para_realloc(f->meta.tags, (f->meta.count + 1)
680 * sizeof(struct mp4_tag));
681 tag = f->meta.tags + f->meta.count;
682 tag->item = para_strdup(get_metadata_name(parent));
692 static int read_mdhd(struct mp4 *f)
698 if (f->total_tracks == 0)
700 t = f->track[f->total_tracks - 1];
702 ret = read_int32(f, &version);
706 ret = read_int64(f, NULL); /* creation-time */
709 ret = read_int64(f, NULL); /* modification-time */
712 ret = read_int32(f, &t->timeScale);
715 ret = read_int64(f, &t->duration);
718 } else { //version == 0
721 ret = read_int32(f, NULL); /* creation-time */
724 ret = read_int32(f, NULL); /* modification-time */
727 ret = read_int32(f, &t->timeScale);
730 ret = read_int32(f, &temp);
733 t->duration = (temp == (uint32_t) (-1))?
734 (uint64_t) (-1) : (uint64_t) (temp);
736 ret = read_int16(f, NULL);
739 ret = read_int16(f, NULL);
745 static int32_t read_ilst(struct mp4 *f, int32_t size)
748 uint64_t sumsize = 0;
750 while (sumsize < size) {
752 uint64_t subsize, destpos;
753 uint8_t header_size = 0;
754 ret = atom_read_header(f, &atom_type, &header_size, &subsize);
757 destpos = get_position(f) + subsize - header_size;
764 ret = parse_tag(f, atom_type, subsize - header_size);
768 set_position(f, destpos);
774 static int32_t read_meta(struct mp4 *f, uint64_t size)
777 uint64_t subsize, sumsize = 0;
779 uint8_t header_size = 0;
781 ret = read_int8(f, NULL); /* version */
784 ret = read_int24(f, NULL); /* flags */
787 while (sumsize < (size - (header_size + 4))) {
788 ret = atom_read_header(f, &atom_type, &header_size, &subsize);
791 if (subsize <= header_size + 4)
793 if (atom_type == ATOM_ILST) {
794 ret = read_ilst(f, subsize - (header_size + 4));
798 set_position(f, get_position(f) + subsize - header_size);
804 static int parse_leaf_atom(struct mp4 *f, uint64_t size, uint8_t atom_type)
806 uint64_t dest_position = get_position(f) + size - 8;
807 int ret = 1; /* return success for atoms we don't care about */
810 case ATOM_STSZ: ret = read_stsz(f); break;
811 case ATOM_STTS: ret = read_stts(f); break;
812 case ATOM_STSC: ret = read_stsc(f); break;
813 case ATOM_STCO: ret = read_stco(f); break;
814 case ATOM_STSD: ret = read_stsd(f); break;
815 case ATOM_MDHD: ret = read_mdhd(f); break;
816 case ATOM_META: ret = read_meta(f, size); break;
818 set_position(f, dest_position);
822 static bool need_atom(uint8_t atom_type, bool meta_only)
824 /* these are needed in any case */
834 /* meta-only opens don't need anything else */
837 /* these are only required for regular opens */
850 /* parse atoms that are sub atoms of other atoms */
851 static int parse_sub_atoms(struct mp4 *f, uint64_t total_size, bool meta_only)
855 uint8_t atom_type = 0;
856 uint64_t counted_size = 0;
857 uint8_t header_size = 0;
859 while (counted_size < total_size) {
860 ret = atom_read_header(f, &atom_type, &header_size, &size);
865 counted_size += size;
866 if (atom_type == ATOM_TRAK) {
867 if (f->total_tracks >= MAX_TRACKS)
870 f->track[f->total_tracks - 1] = para_calloc(
871 sizeof(struct mp4_track));
873 if (!need_atom(atom_type, meta_only)) {
874 set_position(f, get_position(f) + size - header_size);
877 if (atom_type < SUBATOMIC) /* atom contains subatoms */
878 ret = parse_sub_atoms(f, size - header_size, meta_only);
880 ret = parse_leaf_atom(f, size, atom_type);
887 static int parse_root_atoms(struct mp4 *f, bool meta_only)
891 uint8_t atom_type = 0;
892 uint8_t header_size = 0;
896 while ((ret = atom_read_header(f, &atom_type, &header_size, &size)) > 0) {
897 f->file_size += size;
898 f->last_atom = atom_type;
899 if (atom_type != ATOM_MOOV || size <= header_size) { /* skip */
900 set_position(f, get_position(f) + size - header_size);
903 f->moov_offset = get_position(f) - header_size;
905 ret = parse_sub_atoms(f, size - header_size, meta_only);
916 struct mp4 *mp4_open_read(const struct mp4_callback *cb)
919 struct mp4 *f = para_calloc(sizeof(struct mp4));
922 ret = parse_root_atoms(f, false);
930 void mp4_close(struct mp4 *f)
934 for (i = 0; i < f->total_tracks; i++) {
936 free(f->track[i]->stsz_table);
937 free(f->track[i]->stts_sample_count);
938 free(f->track[i]->stsc_first_chunk);
939 free(f->track[i]->stsc_samples_per_chunk);
940 free(f->track[i]->stco_chunk_offset);
944 for (i = 0; i < f->meta.count; i++) {
945 free(f->meta.tags[i].item);
946 free(f->meta.tags[i].value);
952 static int32_t chunk_of_sample(const struct mp4 *f, int32_t sample,
955 const struct mp4_track *t = f->audio_track;
956 uint32_t *fc = t->stsc_first_chunk, *spc = t->stsc_samples_per_chunk;
957 int32_t chunk1, chunk1samples, n, total, i;
959 for (i = 1, total = 0; i < t->stsc_entry_count; i++, total += n) {
960 n = (fc[i] - fc[i - 1]) * spc[i - 1]; /* number of samples */
961 if (sample < total + n)
965 chunk1samples = spc[i - 1];
966 if (chunk1samples != 0)
967 *chunk = (sample - total) / chunk1samples + chunk1;
970 return total + (*chunk - chunk1) * chunk1samples;
974 * Return the number of milliseconds of the audio track.
976 * \param f As returned by \ref mp4_open_read(), must not be NULL.
978 uint64_t mp4_get_duration(const struct mp4 *f)
980 const struct mp4_track *t = f->audio_track;
982 if (t->timeScale == 0)
984 return t->duration * 1000 / t->timeScale;
987 int mp4_set_sample_position(struct mp4 *f, int32_t sample)
989 const struct mp4_track *t = f->audio_track;
990 int32_t offset, chunk, chunk_sample;
991 uint32_t n, srs; /* sample range size */
993 if (sample >= t->stsz_sample_count)
994 return -ERRNO_TO_PARA_ERROR(EINVAL);
995 chunk_sample = chunk_of_sample(f, sample, &chunk);
996 if (t->stsz_sample_size > 0)
997 srs = (sample - chunk_sample) * t->stsz_sample_size;
999 for (srs = 0, n = chunk_sample; n < sample; n++)
1000 srs += t->stsz_table[n];
1002 if (t->stco_entry_count > 0 && chunk > t->stco_entry_count)
1003 offset = t->stco_chunk_offset[t->stco_entry_count - 1];
1004 else if (t->stco_entry_count > 0)
1005 offset = t->stco_chunk_offset[chunk - 1];
1008 set_position(f, offset + srs);
1012 int32_t mp4_get_sample_size(const struct mp4 *f, int sample)
1014 const struct mp4_track *t = f->audio_track;
1016 if (t->stsz_sample_size != 0)
1017 return t->stsz_sample_size;
1018 return t->stsz_table[sample];
1021 uint32_t mp4_get_sample_rate(const struct mp4 *f)
1023 return f->audio_track->sampleRate;
1026 uint32_t mp4_get_channel_count(const struct mp4 *f)
1028 return f->audio_track->channelCount ;
1031 int32_t mp4_num_samples(const struct mp4 *f)
1033 const struct mp4_track *t = f->audio_track;
1037 for (i = 0; i < t->stts_entry_count; i++)
1038 total += t->stts_sample_count[i];
1042 struct mp4 *mp4_open_meta(const struct mp4_callback *cb)
1045 struct mp4 *f = para_calloc(sizeof(struct mp4));
1048 ret = parse_root_atoms(f, true);
1057 * Return the metadata of an mp4 file.
1059 * \param f As returned by either \ref mp4_open_read() or \ref mp4_open_meta().
1061 * The caller is allowed to add, delete or modify the entries of the returned
1062 * structure in order to pass the modified version to \ref mp4_meta_update().
1064 struct mp4_metadata *mp4_get_meta(struct mp4 *f)
1069 static int find_atom(struct mp4 *f, uint64_t base, uint32_t size,
1072 uint32_t remaining = size;
1073 uint64_t atom_offset = base;
1080 set_position(f, atom_offset);
1084 ret = read_int32(f, &atom_size);
1087 if (atom_size > remaining || atom_size < 8)
1089 ret = read_data(f, atom_name, 4);
1092 if (!memcmp(atom_name, name, 4)) {
1093 set_position(f, atom_offset);
1096 remaining -= atom_size;
1097 atom_offset += atom_size;
1102 * Try to find atom <name> with atom <name_inside> in it. Besides -1/0/1 for
1103 * error, EOF and success, this function may return 2 to indicate that the
1104 * desired atoms were not found.
1106 static int find_atom_v2(struct mp4 *f, uint64_t base, uint32_t size,
1107 const char *name, uint32_t extraheaders, const char *name_inside)
1109 uint64_t first_base = (uint64_t) (-1);
1114 int ret = find_atom(f, base, size, name);
1118 mybase = get_position(f);
1119 ret = read_int32(f, &mysize);
1122 if (first_base == (uint64_t) (-1))
1123 first_base = mybase;
1125 if (mysize < 8 + extraheaders)
1128 if (find_atom (f, mybase + (8 + extraheaders),
1129 mysize - (8 + extraheaders), name_inside)) {
1130 set_position(f, mybase);
1138 if (first_base != (uint64_t)(-1)) {
1139 set_position(f, first_base);
1142 /* wanted atom inside not found */
1152 static struct membuffer *membuffer_create(void)
1154 struct membuffer *buf = para_calloc(sizeof(*buf));
1156 buf->allocated = 256;
1157 buf->data = para_malloc(buf->allocated);
1161 static void membuffer_write(struct membuffer *buf, const void *ptr,
1164 unsigned dest_size = buf->written + bytes;
1166 if (dest_size > buf->allocated) {
1168 buf->allocated <<= 1;
1169 } while (dest_size > buf->allocated);
1170 buf->data = para_realloc(buf->data, buf->allocated);
1174 memcpy((char *) buf->data + buf->written, ptr, bytes);
1175 buf->written += bytes;
1178 static void membuffer_write_atom_name(struct membuffer *buf, const char *data)
1180 membuffer_write(buf, data, 4);
1183 static void membuffer_write_int32(struct membuffer *buf, uint32_t data)
1186 write_u32_be(temp, data);
1187 membuffer_write(buf, temp, 4);
1190 static void membuffer_write_std_tag(struct membuffer *buf, const char *name,
1193 uint32_t len = strlen(value);
1194 membuffer_write_int32(buf, 8 /* atom header */
1195 + 8 /* data atom header */
1196 + 8 /* flags + reserved */
1198 membuffer_write_atom_name(buf, name);
1199 membuffer_write_int32(buf, 8 /* data atom header */
1200 + 8 /* flags + reserved */
1202 membuffer_write_atom_name(buf, "data");
1203 membuffer_write_int32(buf, 1); /* flags */
1204 membuffer_write_int32(buf, 0); /* reserved */
1205 membuffer_write(buf, value, len);
1208 static unsigned membuffer_get_size(const struct membuffer *buf)
1210 return buf->written;
1213 static void *membuffer_detach(struct membuffer *buf)
1215 void *ret = para_realloc(buf->data, buf->written);
1220 struct stdmeta_entry {
1225 static const char *find_standard_meta(const char *name)
1227 const struct stdmeta_entry stdmetas[] = {
1228 {"\xA9" "nam", "title"},
1229 {"\xA9" "ART", "artist"},
1230 {"\xA9" "alb", "album"},
1231 {"\xA9" "day", "date"},
1232 {"\xA9" "cmt", "comment"},
1235 for (unsigned n = 0; n < ARRAY_SIZE(stdmetas); n++)
1236 if (!strcasecmp(name, stdmetas[n].name))
1237 return stdmetas[n].atom;
1241 static uint32_t create_ilst(const struct mp4_metadata *meta, void **out_buffer,
1242 uint32_t * out_size)
1244 struct membuffer *buf = membuffer_create();
1247 for (metaptr = 0; metaptr < meta->count; metaptr++) {
1248 struct mp4_tag *tag = meta->tags + metaptr;
1249 const char *std_meta_atom = find_standard_meta(tag->item);
1251 membuffer_write_std_tag(buf, std_meta_atom, tag->value);
1253 PARA_ERROR_LOG("invalid tag item: %s\n", tag->item);
1255 *out_size = membuffer_get_size(buf);
1256 *out_buffer = membuffer_detach(buf);
1260 static void membuffer_write_atom(struct membuffer *buf, const char *name, unsigned size,
1263 membuffer_write_int32(buf, size + 8);
1264 membuffer_write_atom_name(buf, name);
1265 membuffer_write(buf, data, size);
1268 static void *membuffer_get_ptr(const struct membuffer *buf)
1273 static bool membuffer_transfer_from_file(struct membuffer *buf, struct mp4 *src,
1276 unsigned oldsize = membuffer_get_size(buf);
1279 membuffer_write(buf, 0, bytes);
1280 bufptr = membuffer_get_ptr(buf);
1281 if (read_data(src, bufptr + oldsize, bytes) != 1) {
1289 static uint32_t create_meta(const struct mp4_metadata *meta, void **out_buffer,
1290 uint32_t * out_size)
1292 struct membuffer *buf;
1296 if (!create_ilst(meta, &ilst_buffer, &ilst_size))
1299 buf = membuffer_create();
1301 membuffer_write_int32(buf, 0);
1302 membuffer_write_atom(buf, "ilst", ilst_size, ilst_buffer);
1305 *out_size = membuffer_get_size(buf);
1306 *out_buffer = membuffer_detach(buf);
1310 static uint32_t create_udta(const struct mp4_metadata *meta, void **out_buffer,
1311 uint32_t * out_size)
1313 struct membuffer *buf;
1317 if (!create_meta(meta, &meta_buffer, &meta_size))
1320 buf = membuffer_create();
1322 membuffer_write_atom(buf, "meta", meta_size, meta_buffer);
1326 *out_size = membuffer_get_size(buf);
1327 *out_buffer = membuffer_detach(buf);
1331 static uint32_t fix_byte_order_32(uint32_t src)
1333 return read_u32_be(&src);
1336 static void *modify_moov(struct mp4 *f, uint32_t *out_size)
1339 uint64_t total_base = f->moov_offset + 8;
1340 uint32_t total_size = (uint32_t) (f->moov_size - 8);
1341 uint64_t udta_offset, meta_offset, ilst_offset;
1342 uint32_t udta_size, meta_size, ilst_size;
1343 uint32_t new_ilst_size;
1344 void *new_ilst_buffer, *out_buffer;
1349 ret = find_atom_v2(f, total_base, total_size, "udta", 0, "meta");
1353 struct membuffer *buf;
1354 void *new_udta_buffer;
1355 uint32_t new_udta_size;
1356 if (!create_udta(&f->meta, &new_udta_buffer, &new_udta_size))
1359 buf = membuffer_create();
1360 set_position(f, total_base);
1361 if (!membuffer_transfer_from_file(buf, f, total_size)) {
1362 free(new_udta_buffer);
1365 membuffer_write_atom(buf, "udta", new_udta_size,
1368 free(new_udta_buffer);
1370 *out_size = membuffer_get_size(buf);
1371 return membuffer_detach(buf);
1373 udta_offset = get_position(f);
1374 ret = read_int32(f, &udta_size);
1377 ret = find_atom_v2(f, udta_offset + 8, udta_size - 8, "meta", 4, "ilst");
1381 struct membuffer *buf;
1382 void *new_meta_buffer;
1383 uint32_t new_meta_size;
1385 if (!create_meta(&f->meta, &new_meta_buffer, &new_meta_size))
1388 buf = membuffer_create();
1389 set_position(f, total_base);
1390 if (!membuffer_transfer_from_file(buf, f,
1391 udta_offset - total_base)) {
1392 free(new_meta_buffer);
1396 membuffer_write_int32(buf, udta_size + 8 + new_meta_size);
1397 membuffer_write_atom_name(buf, "udta");
1398 if (!membuffer_transfer_from_file(buf, f, udta_size)) {
1399 free(new_meta_buffer);
1402 membuffer_write_atom(buf, "meta", new_meta_size,
1404 free(new_meta_buffer);
1406 *out_size = membuffer_get_size(buf);
1407 return membuffer_detach(buf);
1409 meta_offset = get_position(f);
1410 ret = read_int32(f, &meta_size);
1413 /* shouldn't happen, find_atom_v2 above takes care of it */
1414 if (!find_atom(f, meta_offset + 12, meta_size - 12, "ilst"))
1416 ilst_offset = get_position(f);
1417 ret = read_int32(f, &ilst_size);
1420 if (!create_ilst(&f->meta, &new_ilst_buffer, &new_ilst_size))
1422 size_delta = new_ilst_size - (ilst_size - 8);
1423 *out_size = total_size + size_delta;
1424 out_buffer = para_malloc(*out_size);
1426 set_position(f, total_base);
1427 ret = read_data(f, p_out, udta_offset - total_base);
1430 p_out += (uint32_t) (udta_offset - total_base);
1431 ret = read_int32(f, &tmp);
1434 *(uint32_t *)p_out = fix_byte_order_32(tmp + size_delta);
1436 ret = read_data(f, p_out, 4);
1440 ret = read_data(f, p_out, meta_offset - udta_offset - 8);
1443 p_out += (uint32_t) (meta_offset - udta_offset - 8);
1444 ret = read_int32(f, &tmp);
1447 *(uint32_t *)p_out = fix_byte_order_32(tmp + size_delta);
1449 ret = read_data(f, p_out, 4);
1453 ret = read_data(f, p_out, ilst_offset - meta_offset - 8);
1456 p_out += (uint32_t) (ilst_offset - meta_offset - 8);
1457 ret = read_int32(f, &tmp);
1460 *(uint32_t *)p_out = fix_byte_order_32(tmp + size_delta);
1462 ret = read_data(f, p_out, 4);
1466 memcpy(p_out, new_ilst_buffer, new_ilst_size);
1467 p_out += new_ilst_size;
1468 set_position(f, ilst_offset + ilst_size);
1469 ret = read_data(f, p_out, total_size
1470 - (ilst_offset - total_base) - ilst_size);
1473 free(new_ilst_buffer);
1477 static int32_t write_data(struct mp4 *f, void *data, uint32_t size)
1481 result = f->cb->write(f->cb->user_data, data, size);
1483 f->current_position += size;
1488 static int32_t write_int32(struct mp4 *f, uint32_t data)
1491 write_u32_be(temp, data);
1492 return write_data(f, temp, sizeof(temp));
1495 int32_t mp4_meta_update(struct mp4 *f)
1497 void *new_moov_data;
1498 uint32_t new_moov_size;
1501 new_moov_data = modify_moov(f, &new_moov_size);
1502 if (!new_moov_data ) {
1506 /* copy moov atom to end of the file */
1507 if (f->last_atom != ATOM_MOOV) {
1508 char *free_data = "free";
1510 /* rename old moov to free */
1511 set_position(f, f->moov_offset + 4);
1512 write_data(f, free_data, 4);
1514 set_position(f, f->file_size);
1515 write_int32(f, new_moov_size + 8);
1516 write_data(f, "moov", 4);
1517 write_data(f, new_moov_data, new_moov_size);
1519 set_position(f, f->moov_offset);
1520 write_int32(f, new_moov_size + 8);
1521 write_data(f, "moov", 4);
1522 write_data(f, new_moov_data, new_moov_size);
1524 free(new_moov_data);
1525 f->cb->truncate(f->cb->user_data);
1529 static char *meta_find_by_name(const struct mp4 *f, const char *item)
1533 for (i = 0; i < f->meta.count; i++)
1534 if (!strcasecmp(f->meta.tags[i].item, item))
1535 return para_strdup(f->meta.tags[i].value);
1540 * Return the value of the artist meta tag of an mp4 file.
1542 * \param f Must not be NULL.
1544 * \return If the file does not contain this metadata tag, the function returns
1545 * NULL. Otherwise, a copy of the tag value is returned. The caller should free
1546 * this memory when it is no longer needed.
1548 char *mp4_meta_get_artist(const struct mp4 *f)
1550 return meta_find_by_name(f, "artist");
1554 * Return the value of the title meta tag of an mp4 file.
1556 * \param f See \ref mp4_meta_get_artist().
1557 * \return See \ref mp4_meta_get_artist().
1559 char *mp4_meta_get_title(const struct mp4 *f)
1561 return meta_find_by_name(f, "title");
1565 * Return the value of the date meta tag of an mp4 file.
1567 * \param f See \ref mp4_meta_get_artist().
1568 * \return See \ref mp4_meta_get_artist().
1570 char *mp4_meta_get_date(const struct mp4 *f)
1572 return meta_find_by_name(f, "date");
1576 * Return the value of the album 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_album(const struct mp4 *f)
1583 return meta_find_by_name(f, "album");
1587 * Return the value of the comment 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_comment(const struct mp4 *f)
1594 return meta_find_by_name(f, "comment");