2 * Copyright (C) 2003-2005 M. Bakker, Nero AG, http://www.nero.com
3 * FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
11 #include "portable_io.h"
21 int32_t stsz_sample_size;
22 int32_t stsz_sample_count;
26 int32_t stts_entry_count;
27 int32_t *stts_sample_count;
28 int32_t *stts_sample_delta;
31 int32_t stsc_entry_count;
32 int32_t *stsc_first_chunk;
33 int32_t *stsc_samples_per_chunk;
34 int32_t *stsc_sample_desc_index;
37 int32_t stco_entry_count;
38 int32_t *stco_chunk_offset;
44 #define MAX_TRACKS 1024
47 const struct mp4_callback *cb;
48 int64_t current_position;
58 /* incremental track index while reading the file */
62 struct mp4_track *track[MAX_TRACKS];
65 struct mp4_metadata meta;
68 int32_t mp4_total_tracks(const struct mp4 *f)
70 return f->total_tracks;
73 static int32_t read_data(struct mp4 *f, void *data, uint32_t size)
77 result = f->cb->read(f->cb->user_data, data, size);
82 f->current_position += size;
87 static uint64_t read_int64(struct mp4 *f)
91 read_data(f, data, 8);
92 return read_u64_be(data);
95 static bool atom_compare(int8_t a1, int8_t b1, int8_t c1, int8_t d1,
96 int8_t a2, int8_t b2, int8_t c2, int8_t d2)
98 return a1 == a2 && b1 == b2 && c1 == c2 && d1 == d2;
101 /* atoms with subatoms */
109 ATOM_ILST = 8, /* iTunes Metadata list */
120 ATOM_COMPILATION = 19,
130 /* atoms without subatoms */
150 ATOM_META = 148, /* iTunes Metadata box */
151 ATOM_NAME = 149, /* iTunes Metadata name box */
152 ATOM_DATA = 150, /* iTunes Metadata data box */
159 ATOM_ALBUM_ARTIST = 157,
160 ATOM_CONTENTGROUP = 158,
162 ATOM_DESCRIPTION = 160,
165 ATOM_EPISODENAME = 163,
166 ATOM_SORTTITLE = 164,
167 ATOM_SORTALBUM = 165,
168 ATOM_SORTARTIST = 166,
169 ATOM_SORTALBUMARTIST = 167,
170 ATOM_SORTWRITER = 168,
179 #define ATOM_FREE ATOM_UNKNOWN
180 #define ATOM_SKIP ATOM_UNKNOWN
182 #define COPYRIGHT_SYMBOL ((int8_t)0xA9)
184 static uint8_t atom_name_to_type(int8_t a, int8_t b, int8_t c, int8_t d)
187 if (atom_compare(a, b, c, d, 'm', 'o', 'o', 'v'))
189 else if (atom_compare(a, b, c, d, 'm', 'i', 'n', 'f'))
191 else if (atom_compare(a, b, c, d, 'm', 'd', 'i', 'a'))
193 else if (atom_compare(a, b, c, d, 'm', 'd', 'a', 't'))
195 else if (atom_compare(a, b, c, d, 'm', 'd', 'h', 'd'))
197 else if (atom_compare(a, b, c, d, 'm', 'v', 'h', 'd'))
199 else if (atom_compare(a, b, c, d, 'm', 'p', '4', 'a'))
201 else if (atom_compare(a, b, c, d, 'm', 'p', '4', 'v'))
203 else if (atom_compare(a, b, c, d, 'm', 'p', '4', 's'))
205 else if (atom_compare(a, b, c, d, 'm', 'e', 't', 'a'))
207 } else if (a == 't') {
208 if (atom_compare(a, b, c, d, 't', 'r', 'a', 'k'))
210 else if (atom_compare(a, b, c, d, 't', 'k', 'h', 'd'))
212 else if (atom_compare(a, b, c, d, 't', 'r', 'e', 'f'))
214 else if (atom_compare(a, b, c, d, 't', 'r', 'k', 'n'))
216 else if (atom_compare(a, b, c, d, 't', 'm', 'p', 'o'))
218 else if (atom_compare(a, b, c, d, 't', 'v', 'n', 'n'))
220 else if (atom_compare(a, b, c, d, 't', 'v', 's', 'h'))
222 else if (atom_compare(a, b, c, d, 't', 'v', 'e', 'n'))
223 return ATOM_EPISODENAME;
224 else if (atom_compare(a, b, c, d, 't', 'v', 's', 'n'))
226 else if (atom_compare(a, b, c, d, 't', 'v', 'e', 's'))
228 } else if (a == 's') {
229 if (atom_compare(a, b, c, d, 's', 't', 'b', 'l'))
231 else if (atom_compare(a, b, c, d, 's', 'm', 'h', 'd'))
233 else if (atom_compare(a, b, c, d, 's', 't', 's', 'd'))
235 else if (atom_compare(a, b, c, d, 's', 't', 't', 's'))
237 else if (atom_compare(a, b, c, d, 's', 't', 'c', 'o'))
239 else if (atom_compare(a, b, c, d, 's', 't', 's', 'c'))
241 else if (atom_compare(a, b, c, d, 's', 't', 's', 'z'))
243 else if (atom_compare(a, b, c, d, 's', 't', 'z', '2'))
245 else if (atom_compare(a, b, c, d, 's', 'k', 'i', 'p'))
247 else if (atom_compare(a, b, c, d, 's', 'i', 'n', 'f'))
249 else if (atom_compare(a, b, c, d, 's', 'c', 'h', 'i'))
251 else if (atom_compare(a, b, c, d, 's', 'o', 'n', 'm'))
252 return ATOM_SORTTITLE;
253 else if (atom_compare(a, b, c, d, 's', 'o', 'a', 'l'))
254 return ATOM_SORTALBUM;
255 else if (atom_compare(a, b, c, d, 's', 'o', 'a', 'r'))
256 return ATOM_SORTARTIST;
257 else if (atom_compare(a, b, c, d, 's', 'o', 'a', 'a'))
258 return ATOM_SORTALBUMARTIST;
259 else if (atom_compare(a, b, c, d, 's', 'o', 'c', 'o'))
260 return ATOM_SORTWRITER;
261 else if (atom_compare(a, b, c, d, 's', 'o', 's', 'n'))
262 return ATOM_SORTSHOW;
263 } else if (a == COPYRIGHT_SYMBOL) {
264 if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'n', 'a', 'm'))
266 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'A', 'R', 'T'))
268 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'w', 'r', 't'))
270 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'a', 'l', 'b'))
272 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'd', 'a', 'y'))
274 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 't', 'o', 'o'))
276 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'c', 'm', 't'))
278 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'g', 'e', 'n'))
280 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'g', 'r', 'p'))
281 return ATOM_CONTENTGROUP;
282 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'l', 'y', 'r'))
286 if (atom_compare(a, b, c, d, 'e', 'd', 't', 's'))
288 else if (atom_compare(a, b, c, d, 'e', 's', 'd', 's'))
290 else if (atom_compare(a, b, c, d, 'f', 't', 'y', 'p'))
292 else if (atom_compare(a, b, c, d, 'f', 'r', 'e', 'e'))
294 else if (atom_compare(a, b, c, d, 'h', 'm', 'h', 'd'))
296 else if (atom_compare(a, b, c, d, 'v', 'm', 'h', 'd'))
298 else if (atom_compare(a, b, c, d, 'u', 'd', 't', 'a'))
300 else if (atom_compare(a, b, c, d, 'i', 'l', 's', 't'))
302 else if (atom_compare(a, b, c, d, 'n', 'a', 'm', 'e'))
304 else if (atom_compare(a, b, c, d, 'd', 'a', 't', 'a'))
306 else if (atom_compare(a, b, c, d, 'd', 'i', 's', 'k'))
308 else if (atom_compare(a, b, c, d, 'g', 'n', 'r', 'e'))
310 else if (atom_compare(a, b, c, d, 'c', 'o', 'v', 'r'))
312 else if (atom_compare(a, b, c, d, 'c', 'p', 'i', 'l'))
313 return ATOM_COMPILATION;
314 else if (atom_compare(a, b, c, d, 'c', 't', 't', 's'))
316 else if (atom_compare(a, b, c, d, 'd', 'r', 'm', 's'))
318 else if (atom_compare(a, b, c, d, 'f', 'r', 'm', 'a'))
320 else if (atom_compare(a, b, c, d, 'p', 'r', 'i', 'v'))
322 else if (atom_compare(a, b, c, d, 'i', 'v', 'i', 'v'))
324 else if (atom_compare(a, b, c, d, 'u', 's', 'e', 'r'))
326 else if (atom_compare(a, b, c, d, 'k', 'e', 'y', ' '))
328 else if (atom_compare(a, b, c, d, 'a', 'A', 'R', 'T'))
329 return ATOM_ALBUM_ARTIST;
330 else if (atom_compare(a, b, c, d, 'd', 'e', 's', 'c'))
331 return ATOM_DESCRIPTION;
332 else if (atom_compare(a, b, c, d, 'p', 'c', 's', 't'))
338 /* read atom header, return atom size, atom size is with header included */
339 static uint64_t atom_read_header(struct mp4 *f, uint8_t * atom_type,
340 uint8_t * header_size)
344 int8_t atom_header[8];
346 ret = read_data(f, atom_header, 8);
350 size = read_u32_be(atom_header);
353 /* check for 64 bit atom size */
356 size = read_int64(f);
358 *atom_type = atom_name_to_type(atom_header[4], atom_header[5],
359 atom_header[6], atom_header[7]);
363 static int64_t get_position(const struct mp4 *f)
365 return f->current_position;
368 static int need_parse_when_meta_only(uint8_t atom_type)
389 static int32_t set_position(struct mp4 *f, int64_t position)
391 f->cb->seek(f->cb->user_data, position);
392 f->current_position = position;
397 static void track_add(struct mp4 *f)
401 if (f->total_tracks > MAX_TRACKS) {
406 f->track[f->total_tracks - 1] = para_calloc(sizeof(struct mp4_track));
409 static uint8_t read_char(struct mp4 *f)
412 read_data(f, &output, 1);
416 static uint32_t read_int24(struct mp4 *f)
420 read_data(f, data, 3);
421 return read_u24_be(data);
424 static uint32_t read_int32(struct mp4 *f)
428 read_data(f, data, 4);
429 return read_u32_be(data);
432 static int32_t read_stsz(struct mp4 *f)
437 if (f->total_tracks == 0)
439 t = f->track[f->total_tracks - 1];
440 read_char(f); /* version */
441 read_int24(f); /* flags */
442 t->stsz_sample_size = read_int32(f);
443 t->stsz_sample_count = read_int32(f);
444 if (t->stsz_sample_size != 0)
446 t->stsz_table = para_malloc(t->stsz_sample_count * sizeof(int32_t));
447 for (i = 0; i < t->stsz_sample_count && !f->read_error; i++)
448 t->stsz_table[i] = read_int32(f);
452 static int32_t read_stts(struct mp4 *f)
458 if (f->total_tracks == 0)
460 t = f->track[f->total_tracks - 1];
461 if (t->stts_entry_count)
463 read_char(f); /* version */
464 read_int24(f); /* flags */
465 t->stts_entry_count = read_int32(f);
467 t->stts_sample_count = para_malloc(t->stts_entry_count
469 t->stts_sample_delta = para_malloc(t->stts_entry_count
472 for (i = 0; i < t->stts_entry_count && !f->read_error; i++) {
473 t->stts_sample_count[i] = read_int32(f);
474 t->stts_sample_delta[i] = read_int32(f);
479 static int32_t read_stsc(struct mp4 *f)
484 if (f->total_tracks == 0)
486 t = f->track[f->total_tracks - 1];
488 read_char(f); /* version */
489 read_int24(f); /* flags */
490 t->stsc_entry_count = read_int32(f);
491 t->stsc_first_chunk = para_malloc(t->stsc_entry_count * sizeof(int32_t));
492 t->stsc_samples_per_chunk = para_malloc(t->stsc_entry_count
494 t->stsc_sample_desc_index = para_malloc(t->stsc_entry_count *
498 for (i = 0; i < t->stsc_entry_count && !f->read_error; i++) {
499 t->stsc_first_chunk[i] = read_int32(f);
500 t->stsc_samples_per_chunk[i] = read_int32(f);
501 t->stsc_sample_desc_index[i] = read_int32(f);
506 static int32_t read_stco(struct mp4 *f)
511 if (f->total_tracks == 0)
513 t = f->track[f->total_tracks - 1];
515 read_char(f); /* version */
516 read_int24(f); /* flags */
517 t->stco_entry_count = read_int32(f);
518 t->stco_chunk_offset = para_malloc(t->stco_entry_count
521 for (i = 0; i < t->stco_entry_count && !f->read_error; i++)
522 t->stco_chunk_offset[i] = read_int32(f);
526 static uint16_t read_int16(struct mp4 *f)
530 read_data(f, data, 2);
531 return read_u16_be(data);
534 static int32_t read_mp4a(struct mp4 *f)
537 uint8_t atom_type = 0;
538 uint8_t header_size = 0;
541 if (f->total_tracks == 0)
543 t = f->track[f->total_tracks - 1];
545 for (i = 0; i < 6; i++) {
546 read_char(f); /* reserved */
548 /* data_reference_index */ read_int16(f);
550 read_int32(f); /* reserved */
551 read_int32(f); /* reserved */
553 t->channelCount = read_int16(f);
559 t->sampleRate = read_int16(f);
563 atom_read_header(f, &atom_type, &header_size);
567 static int32_t read_stsd(struct mp4 *f)
569 int32_t i, entry_count;
570 uint8_t header_size = 0;
574 if (f->total_tracks == 0)
576 t = f->track[f->total_tracks - 1];
578 read_char(f); /* version */
579 read_int24(f); /* flags */
581 entry_count = read_int32(f);
584 for (i = 0; i < entry_count && !f->read_error; i++) {
585 uint64_t skip = get_position(f);
587 uint8_t atom_type = 0;
588 size = atom_read_header(f, &atom_type, &header_size);
590 t->is_audio = atom_type == ATOM_MP4A;
593 set_position(f, skip);
599 static int32_t tag_add_field(struct mp4_metadata *meta, const char *item,
600 const char *value, int32_t len)
602 meta->tags = para_realloc(meta->tags,
603 (meta->count + 1) * sizeof(struct mp4_tag));
604 meta->tags[meta->count].item = para_strdup(item);
605 meta->tags[meta->count].len = len;
607 meta->tags[meta->count].value = para_malloc(len + 1);
608 memcpy(meta->tags[meta->count].value, value, len);
609 meta->tags[meta->count].value[len] = 0;
611 meta->tags[meta->count].value = para_strdup(value);
617 static char *read_string(struct mp4 *f, uint32_t length)
619 char *str = para_malloc(length + 1);
620 if ((uint32_t)read_data(f, str, length) != length) {
628 static const char *get_metadata_name(uint8_t atom_type)
631 case ATOM_TITLE: return "title";
632 case ATOM_ARTIST: return "artist";
633 case ATOM_ALBUM: return "album";
634 case ATOM_DATE: return "date";
635 case ATOM_COMMENT: return "comment";
636 default: return "unknown";
640 static void parse_tag(struct mp4 *f, uint8_t parent, int32_t size)
642 uint64_t subsize, sumsize;
649 sumsize < size && !f->read_error; /* CVE-2017-9222 */
650 set_position(f, destpos), sumsize += subsize
653 uint8_t header_size = 0;
654 subsize = atom_read_header(f, &atom_type, &header_size);
655 destpos = get_position(f) + subsize - header_size;
656 if (atom_type != ATOM_DATA)
658 read_char(f); /* version */
659 read_int24(f); /* flags */
660 read_int32(f); /* reserved */
662 data = read_string(f, subsize - (header_size + 8));
663 len = subsize - (header_size + 8);
667 tag_add_field(&f->meta, get_metadata_name(parent), data, len);
671 static int32_t read_mdhd(struct mp4 *f)
677 if (f->total_tracks == 0)
679 t = f->track[f->total_tracks - 1];
681 version = read_int32(f);
683 read_int64(f); //creation-time
684 read_int64(f); //modification-time
685 t->timeScale = read_int32(f); //timescale
686 t->duration = read_int64(f); //duration
687 } else { //version == 0
690 read_int32(f); //creation-time
691 read_int32(f); //modification-time
692 t->timeScale = read_int32(f); //timescale
693 temp = read_int32(f);
694 t->duration = (temp == (uint32_t) (-1))?
695 (uint64_t) (-1) : (uint64_t) (temp);
702 static int32_t read_ilst(struct mp4 *f, int32_t size)
704 uint64_t sumsize = 0;
706 while (sumsize < size) {
708 uint64_t subsize, destpos;
709 uint8_t header_size = 0;
710 subsize = atom_read_header(f, &atom_type, &header_size);
713 destpos = get_position(f) + subsize - header_size;
720 parse_tag(f, atom_type, subsize - header_size);
722 set_position(f, destpos);
729 static int32_t read_meta(struct mp4 *f, uint64_t size)
731 uint64_t subsize, sumsize = 0;
733 uint8_t header_size = 0;
735 read_char(f); /* version */
736 read_int24(f); /* flags */
738 while (sumsize < (size - (header_size + 4))) {
739 subsize = atom_read_header(f, &atom_type, &header_size);
740 if (subsize <= header_size + 4)
742 if (atom_type == ATOM_ILST)
743 read_ilst(f, subsize - (header_size + 4));
745 set_position(f, get_position(f) + subsize - header_size);
752 static int32_t atom_read(struct mp4 *f, int32_t size, uint8_t atom_type)
754 uint64_t dest_position = get_position(f) + size - 8;
755 if (atom_type == ATOM_STSZ) {
756 /* sample size box */
758 } else if (atom_type == ATOM_STTS) {
759 /* time to sample box */
761 } else if (atom_type == ATOM_STSC) {
762 /* sample to chunk box */
764 } else if (atom_type == ATOM_STCO) {
765 /* chunk offset box */
767 } else if (atom_type == ATOM_STSD) {
768 /* sample description box */
770 } else if (atom_type == ATOM_MDHD) {
773 } else if (atom_type == ATOM_META) {
774 /* iTunes Metadata box */
778 set_position(f, dest_position);
782 /* parse atoms that are sub atoms of other atoms */
783 static int32_t parse_sub_atoms(struct mp4 *f, uint64_t total_size, int meta_only)
786 uint8_t atom_type = 0;
787 uint64_t counted_size = 0;
788 uint8_t header_size = 0;
790 while (counted_size < total_size) {
791 size = atom_read_header(f, &atom_type, &header_size);
792 counted_size += size;
794 /* check for end of file */
798 /* we're starting to read a new track, update index,
799 * so that all data and tables get written in the right place
801 if (atom_type == ATOM_TRAK)
804 if (meta_only && !need_parse_when_meta_only(atom_type)) {
805 set_position(f, get_position(f) + size - header_size);
806 } else if (atom_type < SUBATOMIC) {
807 parse_sub_atoms(f, size - header_size, meta_only);
809 atom_read(f, (uint32_t) size, atom_type);
816 /* parse root atoms */
817 static int32_t parse_atoms(struct mp4 *f, int meta_only)
820 uint8_t atom_type = 0;
821 uint8_t header_size = 0;
827 atom_read_header(f, &atom_type, &header_size)) != 0) {
828 f->file_size += size;
829 f->last_atom = atom_type;
831 if (atom_type == ATOM_MOOV && size > header_size) {
832 f->moov_offset = get_position(f) - header_size;
837 if (meta_only && !need_parse_when_meta_only(atom_type)) {
838 set_position(f, get_position(f) + size - header_size);
839 } else if (atom_type < SUBATOMIC) {
840 parse_sub_atoms(f, size - header_size, meta_only);
843 set_position(f, get_position(f) + size - header_size);
850 struct mp4 *mp4_open_read(const struct mp4_callback *cb)
852 struct mp4 *f = para_calloc(sizeof(struct mp4));
863 void mp4_close(struct mp4 *f)
867 for (i = 0; i < f->total_tracks; i++) {
869 free(f->track[i]->stsz_table);
870 free(f->track[i]->stts_sample_count);
871 free(f->track[i]->stts_sample_delta);
872 free(f->track[i]->stsc_first_chunk);
873 free(f->track[i]->stsc_samples_per_chunk);
874 free(f->track[i]->stsc_sample_desc_index);
875 free(f->track[i]->stco_chunk_offset);
879 for (i = 0; i < f->meta.count; i++) {
880 free(f->meta.tags[i].item);
881 free(f->meta.tags[i].value);
887 static int32_t chunk_of_sample(const struct mp4 *f, int32_t track,
888 int32_t sample, int32_t *chunk_sample, int32_t *chunk)
890 int32_t total_entries = 0;
892 int32_t chunk1, chunk2, chunk1samples, range_samples, total = 0;
896 if (f->track[track] == NULL) {
900 total_entries = f->track[track]->stsc_entry_count;
907 chunk2 = f->track[track]->stsc_first_chunk[chunk2entry];
908 *chunk = chunk2 - chunk1;
909 range_samples = *chunk * chunk1samples;
911 if (sample < total + range_samples)
914 chunk1samples = f->track[track]->stsc_samples_per_chunk[chunk2entry];
917 if (chunk2entry < total_entries) {
919 total += range_samples;
921 } while (chunk2entry < total_entries);
924 *chunk = (sample - total) / chunk1samples + chunk1;
928 *chunk_sample = total + (*chunk - chunk1) * chunk1samples;
933 static int32_t chunk_to_offset(const struct mp4 *f, int32_t track,
936 const struct mp4_track *p_track = f->track[track];
938 if (p_track->stco_entry_count && (chunk > p_track->stco_entry_count)) {
939 return p_track->stco_chunk_offset[p_track->stco_entry_count -
941 } else if (p_track->stco_entry_count) {
942 return p_track->stco_chunk_offset[chunk - 1];
950 static int32_t sample_range_size(const struct mp4 *f, int32_t track,
951 int32_t chunk_sample, int32_t sample)
954 const struct mp4_track *p_track = f->track[track];
956 if (p_track->stsz_sample_size) {
957 return (sample - chunk_sample) * p_track->stsz_sample_size;
959 if (sample >= p_track->stsz_sample_count)
962 for (i = chunk_sample, total = 0; i < sample; i++) {
963 total += p_track->stsz_table[i];
970 static int32_t sample_to_offset(const struct mp4 *f, int32_t track,
973 int32_t chunk, chunk_sample, chunk_offset1, chunk_offset2;
975 chunk_of_sample(f, track, sample, &chunk_sample, &chunk);
977 chunk_offset1 = chunk_to_offset(f, track, chunk);
978 chunk_offset2 = chunk_offset1 + sample_range_size(f,
979 track, chunk_sample, sample);
980 return chunk_offset2;
984 * Return the number of milliseconds of the given track.
986 * \param f As returned by \ref mp4_open_read(), must not be NULL.
987 * \param track Between zero and the value returned by \ref mp4_total_tracks().
989 * The function returns zero if the audio file is of zero length or contains a
990 * corrupt track header.
992 uint64_t mp4_get_duration(const struct mp4 *f, int32_t track)
994 const struct mp4_track *t = f->track[track];
996 if (t->timeScale == 0)
998 return t->duration * 1000 / t->timeScale;
1002 * Check whether the given track number corresponds to an audio track.
1004 * \param f See \ref mp4_get_duration().
1005 * \param track See \ref mp4_get_duration().
1007 * Besides audio tracks, an mp4 file may contain video and system tracks. For
1008 * those the function returns false.
1010 bool mp4_is_audio_track(const struct mp4 *f, int32_t track)
1012 return f->track[track]->is_audio;
1015 void mp4_set_sample_position(struct mp4 *f, int32_t track, int32_t sample)
1017 int32_t offset = sample_to_offset(f, track, sample);
1018 set_position(f, offset);
1021 int32_t mp4_get_sample_size(const struct mp4 *f, int track, int sample)
1023 const struct mp4_track *t = f->track[track];
1025 if (t->stsz_sample_size != 0)
1026 return t->stsz_sample_size;
1027 return t->stsz_table[sample];
1030 uint32_t mp4_get_sample_rate(const struct mp4 *f, int32_t track)
1032 return f->track[track]->sampleRate;
1035 uint32_t mp4_get_channel_count(const struct mp4 *f, int32_t track)
1037 return f->track[track]->channelCount;
1040 int32_t mp4_num_samples(const struct mp4 *f, int32_t track)
1045 for (i = 0; i < f->track[track]->stts_entry_count; i++) {
1046 total += f->track[track]->stts_sample_count[i];
1051 struct mp4 *mp4_open_meta(const struct mp4_callback *cb)
1053 struct mp4 *f = para_calloc(sizeof(struct mp4));
1065 * Return the metadata of an mp4 file.
1067 * \param f As returned by either \ref mp4_open_read() or \ref mp4_open_meta().
1069 * The caller is allowed to add, delete or modify the entries of the returned
1070 * structure in order to pass the modified version to \ref mp4_meta_update().
1072 struct mp4_metadata *mp4_get_meta(struct mp4 *f)
1077 static uint32_t find_atom(struct mp4 *f, uint64_t base, uint32_t size,
1080 uint32_t remaining = size;
1081 uint64_t atom_offset = base;
1086 set_position(f, atom_offset);
1090 atom_size = read_int32(f);
1091 if (atom_size > remaining || atom_size < 8)
1093 read_data(f, atom_name, 4);
1095 if (!memcmp(atom_name, name, 4)) {
1096 set_position(f, atom_offset);
1100 remaining -= atom_size;
1101 atom_offset += atom_size;
1106 static uint32_t 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);
1110 while (find_atom(f, base, size, name)) //try to find atom <name> with atom <name_inside> in it
1112 uint64_t mybase = get_position(f);
1113 uint32_t mysize = read_int32(f);
1115 if (first_base == (uint64_t) (-1))
1116 first_base = mybase;
1118 if (mysize < 8 + extraheaders)
1121 if (find_atom (f, mybase + (8 + extraheaders),
1122 mysize - (8 + extraheaders), name_inside)) {
1123 set_position(f, mybase);
1127 if (size <= mysize) {
1134 if (first_base != (uint64_t) (-1)) //wanted atom inside not found
1136 set_position(f, first_base);
1149 static struct membuffer *membuffer_create(void)
1151 struct membuffer *buf = para_calloc(sizeof(*buf));
1153 buf->allocated = 256;
1154 buf->data = para_malloc(buf->allocated);
1158 static unsigned membuffer_write(struct membuffer *buf, const void *ptr, unsigned bytes)
1160 unsigned dest_size = buf->written + bytes;
1164 if (dest_size > buf->allocated) {
1166 buf->allocated <<= 1;
1167 } while (dest_size > buf->allocated);
1168 buf->data = para_realloc(buf->data, buf->allocated);
1172 memcpy((char *) buf->data + buf->written, ptr, bytes);
1173 buf->written += bytes;
1177 static unsigned membuffer_write_atom_name(struct membuffer *buf, const char *data)
1179 return membuffer_write(buf, data, 4) == 4 ? 1 : 0;
1182 static unsigned membuffer_write_int32(struct membuffer *buf, uint32_t data)
1185 write_u32_be(temp, data);
1186 return membuffer_write(buf, temp, 4);
1189 static void membuffer_write_std_tag(struct membuffer *buf, const char *name,
1192 uint32_t len = strlen(value);
1193 membuffer_write_int32(buf, 8 /* atom header */
1194 + 8 /* data atom header */
1195 + 8 /* flags + reserved */
1197 membuffer_write_atom_name(buf, name);
1198 membuffer_write_int32(buf, 8 /* data atom header */
1199 + 8 /* flags + reserved */
1201 membuffer_write_atom_name(buf, "data");
1202 membuffer_write_int32(buf, 1); /* flags */
1203 membuffer_write_int32(buf, 0); /* reserved */
1204 membuffer_write(buf, value, len);
1207 static unsigned membuffer_error(const struct membuffer *buf)
1212 static void membuffer_free(struct membuffer *buf)
1218 static unsigned membuffer_get_size(const struct membuffer *buf)
1220 return buf->written;
1223 static void *membuffer_detach(struct membuffer *buf)
1229 ret = para_realloc(buf->data, buf->written);
1235 struct stdmeta_entry {
1240 static const char *find_standard_meta(const char *name)
1242 const struct stdmeta_entry stdmetas[] = {
1243 {"\xA9" "nam", "title"},
1244 {"\xA9" "ART", "artist"},
1245 {"\xA9" "alb", "album"},
1246 {"\xA9" "day", "date"},
1247 {"\xA9" "cmt", "comment"},
1250 for (unsigned n = 0; n < ARRAY_SIZE(stdmetas); n++)
1251 if (!strcasecmp(name, stdmetas[n].name))
1252 return stdmetas[n].atom;
1256 static uint32_t create_ilst(const struct mp4_metadata *meta, void **out_buffer,
1257 uint32_t * out_size)
1259 struct membuffer *buf = membuffer_create();
1262 for (metaptr = 0; metaptr < meta->count; metaptr++) {
1263 struct mp4_tag *tag = meta->tags + metaptr;
1264 const char *std_meta_atom = find_standard_meta(tag->item);
1266 membuffer_write_std_tag(buf, std_meta_atom, tag->value);
1268 PARA_ERROR_LOG("invalid tag item: %s\n", tag->item);
1270 if (membuffer_error(buf)) {
1271 membuffer_free(buf);
1275 *out_size = membuffer_get_size(buf);
1276 *out_buffer = membuffer_detach(buf);
1277 membuffer_free(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,
1301 oldsize = membuffer_get_size(buf);
1302 if (membuffer_write(buf, 0, bytes) != bytes)
1305 bufptr = membuffer_get_ptr(buf);
1309 if ((unsigned)read_data(src, (char *) bufptr + oldsize, bytes) !=
1311 membuffer_free(buf);
1317 static uint32_t create_meta(const struct mp4_metadata *meta, void **out_buffer,
1318 uint32_t * out_size)
1320 struct membuffer *buf;
1324 if (!create_ilst(meta, &ilst_buffer, &ilst_size))
1327 buf = membuffer_create();
1329 membuffer_write_int32(buf, 0);
1330 membuffer_write_atom(buf, "ilst", ilst_size, ilst_buffer);
1333 *out_size = membuffer_get_size(buf);
1334 *out_buffer = membuffer_detach(buf);
1335 membuffer_free(buf);
1339 static uint32_t create_udta(const struct mp4_metadata *meta, void **out_buffer,
1340 uint32_t * out_size)
1342 struct membuffer *buf;
1346 if (!create_meta(meta, &meta_buffer, &meta_size))
1349 buf = membuffer_create();
1351 membuffer_write_atom(buf, "meta", meta_size, meta_buffer);
1355 *out_size = membuffer_get_size(buf);
1356 *out_buffer = membuffer_detach(buf);
1357 membuffer_free(buf);
1361 static uint32_t fix_byte_order_32(uint32_t src)
1363 return read_u32_be(&src);
1366 static void *modify_moov(struct mp4 *f, uint32_t *out_size)
1368 uint64_t total_base = f->moov_offset + 8;
1369 uint32_t total_size = (uint32_t) (f->moov_size - 8);
1371 uint64_t udta_offset, meta_offset, ilst_offset;
1372 uint32_t udta_size, meta_size, ilst_size;
1374 uint32_t new_ilst_size;
1375 void *new_ilst_buffer, *out_buffer;
1380 if (!find_atom_v2(f, total_base, total_size, "udta", 0, "meta")) {
1381 struct membuffer *buf;
1382 void *new_udta_buffer;
1383 uint32_t new_udta_size;
1384 if (!create_udta(&f->meta, &new_udta_buffer, &new_udta_size))
1387 buf = membuffer_create();
1388 set_position(f, total_base);
1389 if (!membuffer_transfer_from_file(buf, f, total_size)) {
1390 free(new_udta_buffer);
1393 membuffer_write_atom(buf, "udta", new_udta_size,
1396 free(new_udta_buffer);
1398 *out_size = membuffer_get_size(buf);
1399 out_buffer = membuffer_detach(buf);
1400 membuffer_free(buf);
1403 udta_offset = get_position(f);
1404 udta_size = read_int32(f);
1405 if (!find_atom_v2 (f, udta_offset + 8, udta_size - 8, "meta", 4, "ilst")) {
1406 struct membuffer *buf;
1407 void *new_meta_buffer;
1408 uint32_t new_meta_size;
1410 if (!create_meta(&f->meta, &new_meta_buffer, &new_meta_size))
1413 buf = membuffer_create();
1414 set_position(f, total_base);
1415 if (!membuffer_transfer_from_file(buf, f,
1416 udta_offset - total_base)) {
1417 free(new_meta_buffer);
1421 membuffer_write_int32(buf, udta_size + 8 + new_meta_size);
1422 membuffer_write_atom_name(buf, "udta");
1423 if (!membuffer_transfer_from_file(buf, f, udta_size)) {
1424 free(new_meta_buffer);
1428 membuffer_write_atom(buf, "meta", new_meta_size,
1430 free(new_meta_buffer);
1432 *out_size = membuffer_get_size(buf);
1433 out_buffer = membuffer_detach(buf);
1434 membuffer_free(buf);
1437 meta_offset = get_position(f);
1438 meta_size = read_int32(f);
1439 /* shouldn't happen, find_atom_v2 above takes care of it */
1440 if (!find_atom(f, meta_offset + 12, meta_size - 12, "ilst"))
1442 ilst_offset = get_position(f);
1443 ilst_size = read_int32(f);
1444 if (!create_ilst(&f->meta, &new_ilst_buffer, &new_ilst_size))
1446 size_delta = new_ilst_size - (ilst_size - 8);
1447 *out_size = total_size + size_delta;
1448 out_buffer = para_malloc(*out_size);
1450 set_position(f, total_base);
1451 read_data(f, p_out, (uint32_t) (udta_offset - total_base));
1452 p_out += (uint32_t) (udta_offset - total_base);
1453 *(uint32_t *)p_out = fix_byte_order_32(read_int32(f) + size_delta);
1455 read_data(f, p_out, 4);
1457 read_data(f, p_out, (uint32_t) (meta_offset - udta_offset - 8));
1458 p_out += (uint32_t) (meta_offset - udta_offset - 8);
1459 *(uint32_t *)p_out = fix_byte_order_32(read_int32(f) + size_delta);
1461 read_data(f, p_out, 4);
1463 read_data(f, p_out, (uint32_t) (ilst_offset - meta_offset - 8));
1464 p_out += (uint32_t) (ilst_offset - meta_offset - 8);
1465 *(uint32_t *)p_out = fix_byte_order_32(read_int32(f) + size_delta);
1467 read_data(f, p_out, 4);
1469 memcpy(p_out, new_ilst_buffer, new_ilst_size);
1470 p_out += new_ilst_size;
1471 set_position(f, ilst_offset + ilst_size);
1472 read_data(f, p_out, (uint32_t) (total_size
1473 - (ilst_offset - total_base) - ilst_size));
1474 free(new_ilst_buffer);
1478 static int32_t write_data(struct mp4 *f, void *data, uint32_t size)
1482 result = f->cb->write(f->cb->user_data, data, size);
1484 f->current_position += size;
1489 static int32_t write_int32(struct mp4 *f, uint32_t data)
1492 write_u32_be(temp, data);
1493 return write_data(f, temp, sizeof(temp));
1496 int32_t mp4_meta_update(struct mp4 *f)
1498 void *new_moov_data;
1499 uint32_t new_moov_size;
1502 new_moov_data = modify_moov(f, &new_moov_size);
1503 if (!new_moov_data ) {
1507 /* copy moov atom to end of the file */
1508 if (f->last_atom != ATOM_MOOV) {
1509 char *free_data = "free";
1511 /* rename old moov to free */
1512 set_position(f, f->moov_offset + 4);
1513 write_data(f, free_data, 4);
1515 set_position(f, f->file_size);
1516 write_int32(f, new_moov_size + 8);
1517 write_data(f, "moov", 4);
1518 write_data(f, new_moov_data, new_moov_size);
1520 set_position(f, f->moov_offset);
1521 write_int32(f, new_moov_size + 8);
1522 write_data(f, "moov", 4);
1523 write_data(f, new_moov_data, new_moov_size);
1525 free(new_moov_data);
1526 f->cb->truncate(f->cb->user_data);
1530 static char *meta_find_by_name(const struct mp4 *f, const char *item)
1534 for (i = 0; i < f->meta.count; i++)
1535 if (!strcasecmp(f->meta.tags[i].item, item))
1536 return para_strdup(f->meta.tags[i].value);
1541 * Return the value of the artist meta tag of an mp4 file.
1543 * \param f Must not be NULL.
1545 * \return If the file does not contain this metadata tag, the function returns
1546 * NULL. Otherwise, a copy of the tag value is returned. The caller should free
1547 * this memory when it is no longer needed.
1549 char *mp4_meta_get_artist(const struct mp4 *f)
1551 return meta_find_by_name(f, "artist");
1555 * Return the value of the title meta tag of an mp4 file.
1557 * \param f See \ref mp4_meta_get_artist().
1558 * \return See \ref mp4_meta_get_artist().
1560 char *mp4_meta_get_title(const struct mp4 *f)
1562 return meta_find_by_name(f, "title");
1566 * Return the value of the date meta tag of an mp4 file.
1568 * \param f See \ref mp4_meta_get_artist().
1569 * \return See \ref mp4_meta_get_artist().
1571 char *mp4_meta_get_date(const struct mp4 *f)
1573 return meta_find_by_name(f, "date");
1577 * Return the value of the album meta tag of an mp4 file.
1579 * \param f See \ref mp4_meta_get_artist().
1580 * \return See \ref mp4_meta_get_artist().
1582 char *mp4_meta_get_album(const struct mp4 *f)
1584 return meta_find_by_name(f, "album");
1588 * Return the value of the comment meta tag of an mp4 file.
1590 * \param f See \ref mp4_meta_get_artist().
1591 * \return See \ref mp4_meta_get_artist().
1593 char *mp4_meta_get_comment(const struct mp4 *f)
1595 return meta_find_by_name(f, "comment");