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;
28 uint32_t *stts_sample_delta;
31 uint32_t stsc_entry_count;
32 uint32_t *stsc_first_chunk;
33 uint32_t *stsc_samples_per_chunk;
34 uint32_t *stsc_sample_desc_index;
37 uint32_t stco_entry_count;
38 uint32_t *stco_chunk_offset;
44 #define MAX_TRACKS 1024
47 const struct mp4_callback *cb;
48 int64_t current_position;
55 /* incremental track index while reading the file */
58 struct mp4_track *track[MAX_TRACKS];
59 /* the first audio track found */
60 struct mp4_track *audio_track;
63 struct mp4_metadata meta;
67 * Returns -1, 0, or 1 on errors/EOF/success. Partial reads followed by EOF or
68 * read errors are treated as errors.
70 static int read_data(struct mp4 *f, void *data, size_t size)
73 ssize_t ret = f->cb->read(f->cb->user_data, data, size);
74 if (ret < 0 && errno == EINTR)
76 /* regard EAGAIN as an error as reads should be blocking. */
78 return ret < 0? -1 : 0;
79 f->current_position += ret;
85 static int read_int64(struct mp4 *f, uint64_t *result)
88 int ret = read_data(f, data, 8);
90 if (ret > 0 && result)
91 *result = read_u64_be(data);
95 static int read_int32(struct mp4 *f, uint32_t *result)
98 int ret = read_data(f, data, 4);
100 if (ret > 0 && result)
101 *result = read_u32_be(data);
105 static int read_int24(struct mp4 *f, uint32_t *result)
108 int ret = read_data(f, data, 3);
110 if (ret > 0 && result)
111 *result = read_u24_be(data);
115 static int read_int16(struct mp4 *f, uint16_t *result)
118 int ret = read_data(f, data, 2);
120 if (ret > 0 && result)
121 *result = read_u16_be(data);
125 static uint8_t read_int8(struct mp4 *f, uint8_t *result)
128 int ret = read_data(f, data, 1);
130 if (ret > 0 && result)
135 static bool atom_compare(int8_t a1, int8_t b1, int8_t c1, int8_t d1,
136 int8_t a2, int8_t b2, int8_t c2, int8_t d2)
138 return a1 == a2 && b1 == b2 && c1 == c2 && d1 == d2;
142 /* atoms with subatoms */
150 ATOM_ILST = 8, /* iTunes Metadata list */
161 ATOM_COMPILATION = 19,
171 /* atoms without subatoms */
177 ATOM_MDHD = 134, /* track header */
181 ATOM_STSD = 138, /* sample description box */
182 ATOM_STTS = 139, /* time to sample box */
183 ATOM_STSZ = 140, /* sample size box */
185 ATOM_STCO = 142, /* chunk offset box */
186 ATOM_STSC = 143, /* sample to chunk box */
191 ATOM_META = 148, /* iTunes Metadata box */
192 ATOM_NAME = 149, /* iTunes Metadata name box */
193 ATOM_DATA = 150, /* iTunes Metadata data box */
200 ATOM_ALBUM_ARTIST = 157,
201 ATOM_CONTENTGROUP = 158,
203 ATOM_DESCRIPTION = 160,
206 ATOM_EPISODENAME = 163,
207 ATOM_SORTTITLE = 164,
208 ATOM_SORTALBUM = 165,
209 ATOM_SORTARTIST = 166,
210 ATOM_SORTALBUMARTIST = 167,
211 ATOM_SORTWRITER = 168,
220 #define ATOM_FREE ATOM_UNKNOWN
221 #define ATOM_SKIP ATOM_UNKNOWN
223 #define COPYRIGHT_SYMBOL ((int8_t)0xA9)
225 static uint8_t atom_name_to_type(int8_t a, int8_t b, int8_t c, int8_t d)
228 if (atom_compare(a, b, c, d, 'm', 'o', 'o', 'v'))
230 else if (atom_compare(a, b, c, d, 'm', 'i', 'n', 'f'))
232 else if (atom_compare(a, b, c, d, 'm', 'd', 'i', 'a'))
234 else if (atom_compare(a, b, c, d, 'm', 'd', 'a', 't'))
236 else if (atom_compare(a, b, c, d, 'm', 'd', 'h', 'd'))
238 else if (atom_compare(a, b, c, d, 'm', 'v', 'h', 'd'))
240 else if (atom_compare(a, b, c, d, 'm', 'p', '4', 'a'))
242 else if (atom_compare(a, b, c, d, 'm', 'p', '4', 'v'))
244 else if (atom_compare(a, b, c, d, 'm', 'p', '4', 's'))
246 else if (atom_compare(a, b, c, d, 'm', 'e', 't', 'a'))
248 } else if (a == 't') {
249 if (atom_compare(a, b, c, d, 't', 'r', 'a', 'k'))
251 else if (atom_compare(a, b, c, d, 't', 'k', 'h', 'd'))
253 else if (atom_compare(a, b, c, d, 't', 'r', 'e', 'f'))
255 else if (atom_compare(a, b, c, d, 't', 'r', 'k', 'n'))
257 else if (atom_compare(a, b, c, d, 't', 'm', 'p', 'o'))
259 else if (atom_compare(a, b, c, d, 't', 'v', 'n', 'n'))
261 else if (atom_compare(a, b, c, d, 't', 'v', 's', 'h'))
263 else if (atom_compare(a, b, c, d, 't', 'v', 'e', 'n'))
264 return ATOM_EPISODENAME;
265 else if (atom_compare(a, b, c, d, 't', 'v', 's', 'n'))
267 else if (atom_compare(a, b, c, d, 't', 'v', 'e', 's'))
269 } else if (a == 's') {
270 if (atom_compare(a, b, c, d, 's', 't', 'b', 'l'))
272 else if (atom_compare(a, b, c, d, 's', 'm', 'h', 'd'))
274 else if (atom_compare(a, b, c, d, 's', 't', 's', 'd'))
276 else if (atom_compare(a, b, c, d, 's', 't', 't', 's'))
278 else if (atom_compare(a, b, c, d, 's', 't', 'c', 'o'))
280 else if (atom_compare(a, b, c, d, 's', 't', 's', 'c'))
282 else if (atom_compare(a, b, c, d, 's', 't', 's', 'z'))
284 else if (atom_compare(a, b, c, d, 's', 't', 'z', '2'))
286 else if (atom_compare(a, b, c, d, 's', 'k', 'i', 'p'))
288 else if (atom_compare(a, b, c, d, 's', 'i', 'n', 'f'))
290 else if (atom_compare(a, b, c, d, 's', 'c', 'h', 'i'))
292 else if (atom_compare(a, b, c, d, 's', 'o', 'n', 'm'))
293 return ATOM_SORTTITLE;
294 else if (atom_compare(a, b, c, d, 's', 'o', 'a', 'l'))
295 return ATOM_SORTALBUM;
296 else if (atom_compare(a, b, c, d, 's', 'o', 'a', 'r'))
297 return ATOM_SORTARTIST;
298 else if (atom_compare(a, b, c, d, 's', 'o', 'a', 'a'))
299 return ATOM_SORTALBUMARTIST;
300 else if (atom_compare(a, b, c, d, 's', 'o', 'c', 'o'))
301 return ATOM_SORTWRITER;
302 else if (atom_compare(a, b, c, d, 's', 'o', 's', 'n'))
303 return ATOM_SORTSHOW;
304 } else if (a == COPYRIGHT_SYMBOL) {
305 if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'n', 'a', 'm'))
307 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'A', 'R', 'T'))
309 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'w', 'r', 't'))
311 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'a', 'l', 'b'))
313 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'd', 'a', 'y'))
315 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 't', 'o', 'o'))
317 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'c', 'm', 't'))
319 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'g', 'e', 'n'))
321 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'g', 'r', 'p'))
322 return ATOM_CONTENTGROUP;
323 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'l', 'y', 'r'))
327 if (atom_compare(a, b, c, d, 'e', 'd', 't', 's'))
329 else if (atom_compare(a, b, c, d, 'e', 's', 'd', 's'))
331 else if (atom_compare(a, b, c, d, 'f', 't', 'y', 'p'))
333 else if (atom_compare(a, b, c, d, 'f', 'r', 'e', 'e'))
335 else if (atom_compare(a, b, c, d, 'h', 'm', 'h', 'd'))
337 else if (atom_compare(a, b, c, d, 'v', 'm', 'h', 'd'))
339 else if (atom_compare(a, b, c, d, 'u', 'd', 't', 'a'))
341 else if (atom_compare(a, b, c, d, 'i', 'l', 's', 't'))
343 else if (atom_compare(a, b, c, d, 'n', 'a', 'm', 'e'))
345 else if (atom_compare(a, b, c, d, 'd', 'a', 't', 'a'))
347 else if (atom_compare(a, b, c, d, 'd', 'i', 's', 'k'))
349 else if (atom_compare(a, b, c, d, 'g', 'n', 'r', 'e'))
351 else if (atom_compare(a, b, c, d, 'c', 'o', 'v', 'r'))
353 else if (atom_compare(a, b, c, d, 'c', 'p', 'i', 'l'))
354 return ATOM_COMPILATION;
355 else if (atom_compare(a, b, c, d, 'c', 't', 't', 's'))
357 else if (atom_compare(a, b, c, d, 'd', 'r', 'm', 's'))
359 else if (atom_compare(a, b, c, d, 'f', 'r', 'm', 'a'))
361 else if (atom_compare(a, b, c, d, 'p', 'r', 'i', 'v'))
363 else if (atom_compare(a, b, c, d, 'i', 'v', 'i', 'v'))
365 else if (atom_compare(a, b, c, d, 'u', 's', 'e', 'r'))
367 else if (atom_compare(a, b, c, d, 'k', 'e', 'y', ' '))
369 else if (atom_compare(a, b, c, d, 'a', 'A', 'R', 'T'))
370 return ATOM_ALBUM_ARTIST;
371 else if (atom_compare(a, b, c, d, 'd', 'e', 's', 'c'))
372 return ATOM_DESCRIPTION;
373 else if (atom_compare(a, b, c, d, 'p', 'c', 's', 't'))
379 /* read atom header, atom size is returned with header included. */
380 static int atom_read_header(struct mp4 *f, uint8_t *atom_type,
381 uint8_t *header_size, uint64_t *atom_size)
385 int8_t atom_header[8];
387 ret = read_data(f, atom_header, 8);
390 size = read_u32_be(atom_header);
391 if (size == 1) { /* 64 bit atom size */
394 ret = read_int64(f, atom_size);
403 *atom_type = atom_name_to_type(atom_header[4], atom_header[5],
404 atom_header[6], atom_header[7]);
408 static int64_t get_position(const struct mp4 *f)
410 return f->current_position;
413 static int need_parse_when_meta_only(uint8_t atom_type)
434 static int32_t set_position(struct mp4 *f, int64_t position)
436 f->cb->seek(f->cb->user_data, position);
437 f->current_position = position;
442 static int read_stsz(struct mp4 *f)
448 if (f->total_tracks == 0)
450 t = f->track[f->total_tracks - 1];
451 ret = read_int8(f, NULL); /* version */
454 ret = read_int24(f, NULL); /* flags */
457 ret = read_int32(f, &t->stsz_sample_size);
460 ret = read_int32(f, &t->stsz_sample_count);
463 if (t->stsz_sample_size != 0)
465 t->stsz_table = para_malloc(t->stsz_sample_count * sizeof(int32_t));
466 for (i = 0; i < t->stsz_sample_count; i++) {
467 ret = read_int32(f, &t->stsz_table[i]);
474 static int read_stts(struct mp4 *f)
480 if (f->total_tracks == 0)
482 t = f->track[f->total_tracks - 1];
483 if (t->stts_entry_count)
485 ret = read_int8(f, NULL); /* version */
488 ret = read_int24(f, NULL); /* flags */
491 ret = read_int32(f, &t->stts_entry_count);
494 t->stts_sample_count = para_malloc(t->stts_entry_count
496 t->stts_sample_delta = para_malloc(t->stts_entry_count
498 for (i = 0; i < t->stts_entry_count; i++) {
499 ret = read_int32(f, &t->stts_sample_count[i]);
502 ret = read_int32(f, &t->stts_sample_delta[i]);
509 static int read_stsc(struct mp4 *f)
515 if (f->total_tracks == 0)
517 t = f->track[f->total_tracks - 1];
519 ret = read_int8(f, NULL); /* version */
522 ret = read_int24(f, NULL); /* flags */
525 ret = read_int32(f, &t->stsc_entry_count);
528 t->stsc_first_chunk = para_malloc(t->stsc_entry_count * sizeof(int32_t));
529 t->stsc_samples_per_chunk = para_malloc(t->stsc_entry_count
531 t->stsc_sample_desc_index = para_malloc(t->stsc_entry_count *
534 for (i = 0; i < t->stsc_entry_count; i++) {
535 ret = read_int32(f, &t->stsc_first_chunk[i]);
538 ret = read_int32(f, &t->stsc_samples_per_chunk[i]);
541 ret = read_int32(f, &t->stsc_sample_desc_index[i]);
548 static int read_stco(struct mp4 *f)
554 if (f->total_tracks == 0)
556 t = f->track[f->total_tracks - 1];
558 ret = read_int8(f, NULL); /* version */
561 ret = read_int24(f, NULL); /* flags */
564 ret = read_int32(f, &t->stco_entry_count);
567 t->stco_chunk_offset = para_malloc(t->stco_entry_count
569 for (i = 0; i < t->stco_entry_count; i++) {
570 ret = read_int32(f, &t->stco_chunk_offset[i]);
577 static int read_mp4a(struct mp4 *f)
581 uint8_t atom_type = 0;
582 uint8_t header_size = 0;
585 if (f->total_tracks == 0)
587 t = f->track[f->total_tracks - 1];
589 for (i = 0; i < 6; i++) {
590 ret = read_int8(f, NULL); /* reserved */
594 ret = read_int16(f, NULL); /* data_reference_index */
597 ret = read_int32(f, NULL); /* reserved */
600 ret = read_int32(f, NULL); /* reserved */
603 ret = read_int16(f, &t->channelCount);
606 ret = read_int16(f, NULL);
609 ret = read_int16(f, NULL);
612 ret = read_int16(f, NULL);
615 ret = read_int16(f, &t->sampleRate);
618 ret = read_int16(f, NULL);
621 return atom_read_header(f, &atom_type, &header_size, NULL);
624 static int read_stsd(struct mp4 *f)
627 uint32_t i, entry_count;
628 uint8_t header_size = 0;
631 if (f->total_tracks == 0)
633 t = f->track[f->total_tracks - 1];
634 ret = read_int8(f, NULL); /* version */
637 ret = read_int24(f, NULL); /* flags */
640 ret = read_int32(f, &entry_count);
643 for (i = 0; i < entry_count; i++) {
644 uint64_t skip = get_position(f);
646 uint8_t atom_type = 0;
647 ret = atom_read_header(f, &atom_type, &header_size, &size);
651 if (!f->audio_track && atom_type == ATOM_MP4A) {
655 set_position(f, skip);
660 static int32_t tag_add_field(struct mp4_metadata *meta, const char *item,
661 const char *value, int32_t len)
663 meta->tags = para_realloc(meta->tags,
664 (meta->count + 1) * sizeof(struct mp4_tag));
665 meta->tags[meta->count].item = para_strdup(item);
666 meta->tags[meta->count].len = len;
668 meta->tags[meta->count].value = para_malloc(len + 1);
669 memcpy(meta->tags[meta->count].value, value, len);
670 meta->tags[meta->count].value[len] = 0;
672 meta->tags[meta->count].value = para_strdup(value);
678 static int read_string(struct mp4 *f, uint32_t length, char **result)
680 char *str = para_malloc(length + 1);
681 int ret = read_data(f, str, length);
693 static const char *get_metadata_name(uint8_t atom_type)
696 case ATOM_TITLE: return "title";
697 case ATOM_ARTIST: return "artist";
698 case ATOM_ALBUM: return "album";
699 case ATOM_DATE: return "date";
700 case ATOM_COMMENT: return "comment";
701 default: return "unknown";
705 static int parse_tag(struct mp4 *f, uint8_t parent, int32_t size)
708 uint64_t subsize, sumsize;
716 set_position(f, destpos), sumsize += subsize
719 uint8_t header_size = 0;
720 ret = atom_read_header(f, &atom_type, &header_size, &subsize);
723 destpos = get_position(f) + subsize - header_size;
724 if (atom_type != ATOM_DATA)
726 ret = read_int8(f, NULL); /* version */
729 ret = read_int24(f, NULL); /* flags */
732 ret = read_int32(f, NULL); /* reserved */
736 ret = read_string(f, subsize - (header_size + 8), &data);
739 len = subsize - (header_size + 8);
743 tag_add_field(&f->meta, get_metadata_name(parent), data, len);
748 static int read_mdhd(struct mp4 *f)
754 if (f->total_tracks == 0)
756 t = f->track[f->total_tracks - 1];
758 ret = read_int32(f, &version);
762 ret = read_int64(f, NULL); /* creation-time */
765 ret = read_int64(f, NULL); /* modification-time */
768 ret = read_int32(f, &t->timeScale);
771 ret = read_int64(f, &t->duration);
774 } else { //version == 0
777 ret = read_int32(f, NULL); /* creation-time */
780 ret = read_int32(f, NULL); /* modification-time */
783 ret = read_int32(f, &t->timeScale);
786 ret = read_int32(f, &temp);
789 t->duration = (temp == (uint32_t) (-1))?
790 (uint64_t) (-1) : (uint64_t) (temp);
792 ret = read_int16(f, NULL);
795 ret = read_int16(f, NULL);
801 static int32_t read_ilst(struct mp4 *f, int32_t size)
804 uint64_t sumsize = 0;
806 while (sumsize < size) {
808 uint64_t subsize, destpos;
809 uint8_t header_size = 0;
810 ret = atom_read_header(f, &atom_type, &header_size, &subsize);
813 destpos = get_position(f) + subsize - header_size;
820 parse_tag(f, atom_type, subsize - header_size);
822 set_position(f, destpos);
828 static int32_t read_meta(struct mp4 *f, uint64_t size)
831 uint64_t subsize, sumsize = 0;
833 uint8_t header_size = 0;
835 ret = read_int8(f, NULL); /* version */
838 ret = read_int24(f, NULL); /* flags */
841 while (sumsize < (size - (header_size + 4))) {
842 ret = atom_read_header(f, &atom_type, &header_size, &subsize);
845 if (subsize <= header_size + 4)
847 if (atom_type == ATOM_ILST)
848 read_ilst(f, subsize - (header_size + 4));
850 set_position(f, get_position(f) + subsize - header_size);
856 static int atom_read(struct mp4 *f, uint64_t size, uint8_t atom_type)
858 uint64_t dest_position = get_position(f) + size - 8;
859 int ret = 1; /* return success for atoms we don't care about */
862 case ATOM_STSZ: ret = read_stsz(f); break;
863 case ATOM_STTS: ret = read_stts(f); break;
864 case ATOM_STSC: ret = read_stsc(f); break;
865 case ATOM_STCO: ret = read_stco(f); break;
866 case ATOM_STSD: ret = read_stsd(f); break;
867 case ATOM_MDHD: ret = read_mdhd(f); break;
868 case ATOM_META: ret = read_meta(f, size); break;
870 set_position(f, dest_position);
874 /* parse atoms that are sub atoms of other atoms */
875 static int parse_sub_atoms(struct mp4 *f, uint64_t total_size, int meta_only)
879 uint8_t atom_type = 0;
880 uint64_t counted_size = 0;
881 uint8_t header_size = 0;
883 while (counted_size < total_size) {
884 ret = atom_read_header(f, &atom_type, &header_size, &size);
889 counted_size += size;
890 if (atom_type == ATOM_TRAK) {
891 if (f->total_tracks >= MAX_TRACKS)
894 f->track[f->total_tracks - 1] = para_calloc(
895 sizeof(struct mp4_track));
898 if (meta_only && !need_parse_when_meta_only(atom_type)) {
899 set_position(f, get_position(f) + size - header_size);
900 } else if (atom_type < SUBATOMIC) {
901 ret = parse_sub_atoms(f, size - header_size, meta_only);
905 ret = atom_read(f, size, atom_type);
913 /* parse root atoms */
914 static int parse_atoms(struct mp4 *f, int meta_only)
918 uint8_t atom_type = 0;
919 uint8_t header_size = 0;
923 while ((ret = atom_read_header(f, &atom_type, &header_size, &size)) > 0) {
924 f->file_size += size;
925 f->last_atom = atom_type;
927 if (atom_type == ATOM_MOOV && size > header_size) {
928 f->moov_offset = get_position(f) - header_size;
933 if (meta_only && !need_parse_when_meta_only(atom_type)) {
934 set_position(f, get_position(f) + size - header_size);
935 } else if (atom_type < SUBATOMIC) {
936 ret = parse_sub_atoms(f, size - header_size, meta_only);
941 set_position(f, get_position(f) + size - header_size);
951 struct mp4 *mp4_open_read(const struct mp4_callback *cb)
954 struct mp4 *f = para_calloc(sizeof(struct mp4));
957 ret = parse_atoms(f, 0);
965 void mp4_close(struct mp4 *f)
969 for (i = 0; i < f->total_tracks; i++) {
971 free(f->track[i]->stsz_table);
972 free(f->track[i]->stts_sample_count);
973 free(f->track[i]->stts_sample_delta);
974 free(f->track[i]->stsc_first_chunk);
975 free(f->track[i]->stsc_samples_per_chunk);
976 free(f->track[i]->stsc_sample_desc_index);
977 free(f->track[i]->stco_chunk_offset);
981 for (i = 0; i < f->meta.count; i++) {
982 free(f->meta.tags[i].item);
983 free(f->meta.tags[i].value);
989 static int32_t chunk_of_sample(const struct mp4 *f, int32_t sample,
992 const struct mp4_track *t = f->audio_track;
993 uint32_t *fc = t->stsc_first_chunk, *spc = t->stsc_samples_per_chunk;
994 int32_t chunk1, chunk1samples, n, total, i;
996 for (i = 1, total = 0; i < t->stsc_entry_count; i++, total += n) {
997 n = (fc[i] - fc[i - 1]) * spc[i - 1]; /* number of samples */
998 if (sample < total + n)
1002 chunk1samples = spc[i - 1];
1003 if (chunk1samples != 0)
1004 *chunk = (sample - total) / chunk1samples + chunk1;
1007 return total + (*chunk - chunk1) * chunk1samples;
1011 * Return the number of milliseconds of the audio track.
1013 * \param f As returned by \ref mp4_open_read(), must not be NULL.
1015 uint64_t mp4_get_duration(const struct mp4 *f)
1017 const struct mp4_track *t = f->audio_track;
1019 if (t->timeScale == 0)
1021 return t->duration * 1000 / t->timeScale;
1024 int mp4_set_sample_position(struct mp4 *f, int32_t sample)
1026 const struct mp4_track *t = f->audio_track;
1027 int32_t offset, chunk, chunk_sample;
1028 uint32_t n, srs; /* sample range size */
1030 if (sample >= t->stsz_sample_count)
1031 return -ERRNO_TO_PARA_ERROR(EINVAL);
1032 chunk_sample = chunk_of_sample(f, sample, &chunk);
1033 if (t->stsz_sample_size > 0)
1034 srs = (sample - chunk_sample) * t->stsz_sample_size;
1036 for (srs = 0, n = chunk_sample; n < sample; n++)
1037 srs += t->stsz_table[n];
1039 if (t->stco_entry_count > 0 && chunk > t->stco_entry_count)
1040 offset = t->stco_chunk_offset[t->stco_entry_count - 1];
1041 else if (t->stco_entry_count > 0)
1042 offset = t->stco_chunk_offset[chunk - 1];
1045 set_position(f, offset + srs);
1049 int32_t mp4_get_sample_size(const struct mp4 *f, int sample)
1051 const struct mp4_track *t = f->audio_track;
1053 if (t->stsz_sample_size != 0)
1054 return t->stsz_sample_size;
1055 return t->stsz_table[sample];
1058 uint32_t mp4_get_sample_rate(const struct mp4 *f)
1060 return f->audio_track->sampleRate;
1063 uint32_t mp4_get_channel_count(const struct mp4 *f)
1065 return f->audio_track->channelCount ;
1068 int32_t mp4_num_samples(const struct mp4 *f)
1070 const struct mp4_track *t = f->audio_track;
1074 for (i = 0; i < t->stts_entry_count; i++)
1075 total += t->stts_sample_count[i];
1079 struct mp4 *mp4_open_meta(const struct mp4_callback *cb)
1082 struct mp4 *f = para_calloc(sizeof(struct mp4));
1085 ret = parse_atoms(f, 1);
1094 * Return the metadata of an mp4 file.
1096 * \param f As returned by either \ref mp4_open_read() or \ref mp4_open_meta().
1098 * The caller is allowed to add, delete or modify the entries of the returned
1099 * structure in order to pass the modified version to \ref mp4_meta_update().
1101 struct mp4_metadata *mp4_get_meta(struct mp4 *f)
1106 static int find_atom(struct mp4 *f, uint64_t base, uint32_t size,
1109 uint32_t remaining = size;
1110 uint64_t atom_offset = base;
1117 set_position(f, atom_offset);
1121 ret = read_int32(f, &atom_size);
1124 if (atom_size > remaining || atom_size < 8)
1126 ret = read_data(f, atom_name, 4);
1129 if (!memcmp(atom_name, name, 4)) {
1130 set_position(f, atom_offset);
1133 remaining -= atom_size;
1134 atom_offset += atom_size;
1139 * Try to find atom <name> with atom <name_inside> in it. Besides -1/0/1 for
1140 * error, EOF and success, this function may return 2 to indicate that the
1141 * desired atoms were not found.
1143 static int find_atom_v2(struct mp4 *f, uint64_t base, uint32_t size,
1144 const char *name, uint32_t extraheaders, const char *name_inside)
1146 uint64_t first_base = (uint64_t) (-1);
1151 int ret = find_atom(f, base, size, name);
1155 mybase = get_position(f);
1156 ret = read_int32(f, &mysize);
1159 if (first_base == (uint64_t) (-1))
1160 first_base = mybase;
1162 if (mysize < 8 + extraheaders)
1165 if (find_atom (f, mybase + (8 + extraheaders),
1166 mysize - (8 + extraheaders), name_inside)) {
1167 set_position(f, mybase);
1175 if (first_base != (uint64_t)(-1)) {
1176 set_position(f, first_base);
1179 /* wanted atom inside not found */
1189 static struct membuffer *membuffer_create(void)
1191 struct membuffer *buf = para_calloc(sizeof(*buf));
1193 buf->allocated = 256;
1194 buf->data = para_malloc(buf->allocated);
1198 static void membuffer_write(struct membuffer *buf, const void *ptr,
1201 unsigned dest_size = buf->written + bytes;
1203 if (dest_size > buf->allocated) {
1205 buf->allocated <<= 1;
1206 } while (dest_size > buf->allocated);
1207 buf->data = para_realloc(buf->data, buf->allocated);
1211 memcpy((char *) buf->data + buf->written, ptr, bytes);
1212 buf->written += bytes;
1215 static void membuffer_write_atom_name(struct membuffer *buf, const char *data)
1217 membuffer_write(buf, data, 4);
1220 static void membuffer_write_int32(struct membuffer *buf, uint32_t data)
1223 write_u32_be(temp, data);
1224 membuffer_write(buf, temp, 4);
1227 static void membuffer_write_std_tag(struct membuffer *buf, const char *name,
1230 uint32_t len = strlen(value);
1231 membuffer_write_int32(buf, 8 /* atom header */
1232 + 8 /* data atom header */
1233 + 8 /* flags + reserved */
1235 membuffer_write_atom_name(buf, name);
1236 membuffer_write_int32(buf, 8 /* data atom header */
1237 + 8 /* flags + reserved */
1239 membuffer_write_atom_name(buf, "data");
1240 membuffer_write_int32(buf, 1); /* flags */
1241 membuffer_write_int32(buf, 0); /* reserved */
1242 membuffer_write(buf, value, len);
1245 static unsigned membuffer_get_size(const struct membuffer *buf)
1247 return buf->written;
1250 static void *membuffer_detach(struct membuffer *buf)
1252 void *ret = para_realloc(buf->data, buf->written);
1257 struct stdmeta_entry {
1262 static const char *find_standard_meta(const char *name)
1264 const struct stdmeta_entry stdmetas[] = {
1265 {"\xA9" "nam", "title"},
1266 {"\xA9" "ART", "artist"},
1267 {"\xA9" "alb", "album"},
1268 {"\xA9" "day", "date"},
1269 {"\xA9" "cmt", "comment"},
1272 for (unsigned n = 0; n < ARRAY_SIZE(stdmetas); n++)
1273 if (!strcasecmp(name, stdmetas[n].name))
1274 return stdmetas[n].atom;
1278 static uint32_t create_ilst(const struct mp4_metadata *meta, void **out_buffer,
1279 uint32_t * out_size)
1281 struct membuffer *buf = membuffer_create();
1284 for (metaptr = 0; metaptr < meta->count; metaptr++) {
1285 struct mp4_tag *tag = meta->tags + metaptr;
1286 const char *std_meta_atom = find_standard_meta(tag->item);
1288 membuffer_write_std_tag(buf, std_meta_atom, tag->value);
1290 PARA_ERROR_LOG("invalid tag item: %s\n", tag->item);
1292 *out_size = membuffer_get_size(buf);
1293 *out_buffer = membuffer_detach(buf);
1297 static void membuffer_write_atom(struct membuffer *buf, const char *name, unsigned size,
1300 membuffer_write_int32(buf, size + 8);
1301 membuffer_write_atom_name(buf, name);
1302 membuffer_write(buf, data, size);
1305 static void *membuffer_get_ptr(const struct membuffer *buf)
1310 static bool membuffer_transfer_from_file(struct membuffer *buf, struct mp4 *src,
1313 unsigned oldsize = membuffer_get_size(buf);
1316 membuffer_write(buf, 0, bytes);
1317 bufptr = membuffer_get_ptr(buf);
1318 if (read_data(src, bufptr + oldsize, bytes) != 1) {
1326 static uint32_t create_meta(const struct mp4_metadata *meta, void **out_buffer,
1327 uint32_t * out_size)
1329 struct membuffer *buf;
1333 if (!create_ilst(meta, &ilst_buffer, &ilst_size))
1336 buf = membuffer_create();
1338 membuffer_write_int32(buf, 0);
1339 membuffer_write_atom(buf, "ilst", ilst_size, ilst_buffer);
1342 *out_size = membuffer_get_size(buf);
1343 *out_buffer = membuffer_detach(buf);
1347 static uint32_t create_udta(const struct mp4_metadata *meta, void **out_buffer,
1348 uint32_t * out_size)
1350 struct membuffer *buf;
1354 if (!create_meta(meta, &meta_buffer, &meta_size))
1357 buf = membuffer_create();
1359 membuffer_write_atom(buf, "meta", meta_size, meta_buffer);
1363 *out_size = membuffer_get_size(buf);
1364 *out_buffer = membuffer_detach(buf);
1368 static uint32_t fix_byte_order_32(uint32_t src)
1370 return read_u32_be(&src);
1373 static void *modify_moov(struct mp4 *f, uint32_t *out_size)
1376 uint64_t total_base = f->moov_offset + 8;
1377 uint32_t total_size = (uint32_t) (f->moov_size - 8);
1378 uint64_t udta_offset, meta_offset, ilst_offset;
1379 uint32_t udta_size, meta_size, ilst_size;
1380 uint32_t new_ilst_size;
1381 void *new_ilst_buffer, *out_buffer;
1386 ret = find_atom_v2(f, total_base, total_size, "udta", 0, "meta");
1390 struct membuffer *buf;
1391 void *new_udta_buffer;
1392 uint32_t new_udta_size;
1393 if (!create_udta(&f->meta, &new_udta_buffer, &new_udta_size))
1396 buf = membuffer_create();
1397 set_position(f, total_base);
1398 if (!membuffer_transfer_from_file(buf, f, total_size)) {
1399 free(new_udta_buffer);
1402 membuffer_write_atom(buf, "udta", new_udta_size,
1405 free(new_udta_buffer);
1407 *out_size = membuffer_get_size(buf);
1408 return membuffer_detach(buf);
1410 udta_offset = get_position(f);
1411 ret = read_int32(f, &udta_size);
1414 ret = find_atom_v2(f, udta_offset + 8, udta_size - 8, "meta", 4, "ilst");
1418 struct membuffer *buf;
1419 void *new_meta_buffer;
1420 uint32_t new_meta_size;
1422 if (!create_meta(&f->meta, &new_meta_buffer, &new_meta_size))
1425 buf = membuffer_create();
1426 set_position(f, total_base);
1427 if (!membuffer_transfer_from_file(buf, f,
1428 udta_offset - total_base)) {
1429 free(new_meta_buffer);
1433 membuffer_write_int32(buf, udta_size + 8 + new_meta_size);
1434 membuffer_write_atom_name(buf, "udta");
1435 if (!membuffer_transfer_from_file(buf, f, udta_size)) {
1436 free(new_meta_buffer);
1439 membuffer_write_atom(buf, "meta", new_meta_size,
1441 free(new_meta_buffer);
1443 *out_size = membuffer_get_size(buf);
1444 return membuffer_detach(buf);
1446 meta_offset = get_position(f);
1447 ret = read_int32(f, &meta_size);
1450 /* shouldn't happen, find_atom_v2 above takes care of it */
1451 if (!find_atom(f, meta_offset + 12, meta_size - 12, "ilst"))
1453 ilst_offset = get_position(f);
1454 ret = read_int32(f, &ilst_size);
1457 if (!create_ilst(&f->meta, &new_ilst_buffer, &new_ilst_size))
1459 size_delta = new_ilst_size - (ilst_size - 8);
1460 *out_size = total_size + size_delta;
1461 out_buffer = para_malloc(*out_size);
1463 set_position(f, total_base);
1464 ret = read_data(f, p_out, udta_offset - total_base);
1467 p_out += (uint32_t) (udta_offset - total_base);
1468 ret = read_int32(f, &tmp);
1471 *(uint32_t *)p_out = fix_byte_order_32(tmp + size_delta);
1473 ret = read_data(f, p_out, 4);
1477 ret = read_data(f, p_out, meta_offset - udta_offset - 8);
1480 p_out += (uint32_t) (meta_offset - udta_offset - 8);
1481 ret = read_int32(f, &tmp);
1484 *(uint32_t *)p_out = fix_byte_order_32(tmp + size_delta);
1486 ret = read_data(f, p_out, 4);
1490 ret = read_data(f, p_out, ilst_offset - meta_offset - 8);
1493 p_out += (uint32_t) (ilst_offset - meta_offset - 8);
1494 ret = read_int32(f, &tmp);
1497 *(uint32_t *)p_out = fix_byte_order_32(tmp + size_delta);
1499 ret = read_data(f, p_out, 4);
1503 memcpy(p_out, new_ilst_buffer, new_ilst_size);
1504 p_out += new_ilst_size;
1505 set_position(f, ilst_offset + ilst_size);
1506 ret = read_data(f, p_out, total_size
1507 - (ilst_offset - total_base) - ilst_size);
1510 free(new_ilst_buffer);
1514 static int32_t write_data(struct mp4 *f, void *data, uint32_t size)
1518 result = f->cb->write(f->cb->user_data, data, size);
1520 f->current_position += size;
1525 static int32_t write_int32(struct mp4 *f, uint32_t data)
1528 write_u32_be(temp, data);
1529 return write_data(f, temp, sizeof(temp));
1532 int32_t mp4_meta_update(struct mp4 *f)
1534 void *new_moov_data;
1535 uint32_t new_moov_size;
1538 new_moov_data = modify_moov(f, &new_moov_size);
1539 if (!new_moov_data ) {
1543 /* copy moov atom to end of the file */
1544 if (f->last_atom != ATOM_MOOV) {
1545 char *free_data = "free";
1547 /* rename old moov to free */
1548 set_position(f, f->moov_offset + 4);
1549 write_data(f, free_data, 4);
1551 set_position(f, f->file_size);
1552 write_int32(f, new_moov_size + 8);
1553 write_data(f, "moov", 4);
1554 write_data(f, new_moov_data, new_moov_size);
1556 set_position(f, f->moov_offset);
1557 write_int32(f, new_moov_size + 8);
1558 write_data(f, "moov", 4);
1559 write_data(f, new_moov_data, new_moov_size);
1561 free(new_moov_data);
1562 f->cb->truncate(f->cb->user_data);
1566 static char *meta_find_by_name(const struct mp4 *f, const char *item)
1570 for (i = 0; i < f->meta.count; i++)
1571 if (!strcasecmp(f->meta.tags[i].item, item))
1572 return para_strdup(f->meta.tags[i].value);
1577 * Return the value of the artist meta tag of an mp4 file.
1579 * \param f Must not be NULL.
1581 * \return If the file does not contain this metadata tag, the function returns
1582 * NULL. Otherwise, a copy of the tag value is returned. The caller should free
1583 * this memory when it is no longer needed.
1585 char *mp4_meta_get_artist(const struct mp4 *f)
1587 return meta_find_by_name(f, "artist");
1591 * Return the value of the title meta tag of an mp4 file.
1593 * \param f See \ref mp4_meta_get_artist().
1594 * \return See \ref mp4_meta_get_artist().
1596 char *mp4_meta_get_title(const struct mp4 *f)
1598 return meta_find_by_name(f, "title");
1602 * Return the value of the date meta tag of an mp4 file.
1604 * \param f See \ref mp4_meta_get_artist().
1605 * \return See \ref mp4_meta_get_artist().
1607 char *mp4_meta_get_date(const struct mp4 *f)
1609 return meta_find_by_name(f, "date");
1613 * Return the value of the album meta tag of an mp4 file.
1615 * \param f See \ref mp4_meta_get_artist().
1616 * \return See \ref mp4_meta_get_artist().
1618 char *mp4_meta_get_album(const struct mp4 *f)
1620 return meta_find_by_name(f, "album");
1624 * Return the value of the comment meta tag of an mp4 file.
1626 * \param f See \ref mp4_meta_get_artist().
1627 * \return See \ref mp4_meta_get_artist().
1629 char *mp4_meta_get_comment(const struct mp4 *f)
1631 return meta_find_by_name(f, "comment");