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"
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;
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 uint32_t read_int32(struct mp4 *f)
99 read_data(f, data, 4);
100 return read_u32_be(data);
103 static uint32_t read_int24(struct mp4 *f)
107 read_data(f, data, 3);
108 return read_u24_be(data);
111 static uint16_t read_int16(struct mp4 *f)
115 read_data(f, data, 2);
116 return read_u16_be(data);
119 static uint8_t read_int8(struct mp4 *f)
122 read_data(f, &output, 1);
126 static bool atom_compare(int8_t a1, int8_t b1, int8_t c1, int8_t d1,
127 int8_t a2, int8_t b2, int8_t c2, int8_t d2)
129 return a1 == a2 && b1 == b2 && c1 == c2 && d1 == d2;
132 /* atoms with subatoms */
140 ATOM_ILST = 8, /* iTunes Metadata list */
151 ATOM_COMPILATION = 19,
161 /* atoms without subatoms */
181 ATOM_META = 148, /* iTunes Metadata box */
182 ATOM_NAME = 149, /* iTunes Metadata name box */
183 ATOM_DATA = 150, /* iTunes Metadata data box */
190 ATOM_ALBUM_ARTIST = 157,
191 ATOM_CONTENTGROUP = 158,
193 ATOM_DESCRIPTION = 160,
196 ATOM_EPISODENAME = 163,
197 ATOM_SORTTITLE = 164,
198 ATOM_SORTALBUM = 165,
199 ATOM_SORTARTIST = 166,
200 ATOM_SORTALBUMARTIST = 167,
201 ATOM_SORTWRITER = 168,
210 #define ATOM_FREE ATOM_UNKNOWN
211 #define ATOM_SKIP ATOM_UNKNOWN
213 #define COPYRIGHT_SYMBOL ((int8_t)0xA9)
215 static uint8_t atom_name_to_type(int8_t a, int8_t b, int8_t c, int8_t d)
218 if (atom_compare(a, b, c, d, 'm', 'o', 'o', 'v'))
220 else if (atom_compare(a, b, c, d, 'm', 'i', 'n', 'f'))
222 else if (atom_compare(a, b, c, d, 'm', 'd', 'i', 'a'))
224 else if (atom_compare(a, b, c, d, 'm', 'd', 'a', 't'))
226 else if (atom_compare(a, b, c, d, 'm', 'd', 'h', 'd'))
228 else if (atom_compare(a, b, c, d, 'm', 'v', 'h', 'd'))
230 else if (atom_compare(a, b, c, d, 'm', 'p', '4', 'a'))
232 else if (atom_compare(a, b, c, d, 'm', 'p', '4', 'v'))
234 else if (atom_compare(a, b, c, d, 'm', 'p', '4', 's'))
236 else if (atom_compare(a, b, c, d, 'm', 'e', 't', 'a'))
238 } else if (a == 't') {
239 if (atom_compare(a, b, c, d, 't', 'r', 'a', 'k'))
241 else if (atom_compare(a, b, c, d, 't', 'k', 'h', 'd'))
243 else if (atom_compare(a, b, c, d, 't', 'r', 'e', 'f'))
245 else if (atom_compare(a, b, c, d, 't', 'r', 'k', 'n'))
247 else if (atom_compare(a, b, c, d, 't', 'm', 'p', 'o'))
249 else if (atom_compare(a, b, c, d, 't', 'v', 'n', 'n'))
251 else if (atom_compare(a, b, c, d, 't', 'v', 's', 'h'))
253 else if (atom_compare(a, b, c, d, 't', 'v', 'e', 'n'))
254 return ATOM_EPISODENAME;
255 else if (atom_compare(a, b, c, d, 't', 'v', 's', 'n'))
257 else if (atom_compare(a, b, c, d, 't', 'v', 'e', 's'))
259 } else if (a == 's') {
260 if (atom_compare(a, b, c, d, 's', 't', 'b', 'l'))
262 else if (atom_compare(a, b, c, d, 's', 'm', 'h', 'd'))
264 else if (atom_compare(a, b, c, d, 's', 't', 's', 'd'))
266 else if (atom_compare(a, b, c, d, 's', 't', 't', 's'))
268 else if (atom_compare(a, b, c, d, 's', 't', 'c', 'o'))
270 else if (atom_compare(a, b, c, d, 's', 't', 's', 'c'))
272 else if (atom_compare(a, b, c, d, 's', 't', 's', 'z'))
274 else if (atom_compare(a, b, c, d, 's', 't', 'z', '2'))
276 else if (atom_compare(a, b, c, d, 's', 'k', 'i', 'p'))
278 else if (atom_compare(a, b, c, d, 's', 'i', 'n', 'f'))
280 else if (atom_compare(a, b, c, d, 's', 'c', 'h', 'i'))
282 else if (atom_compare(a, b, c, d, 's', 'o', 'n', 'm'))
283 return ATOM_SORTTITLE;
284 else if (atom_compare(a, b, c, d, 's', 'o', 'a', 'l'))
285 return ATOM_SORTALBUM;
286 else if (atom_compare(a, b, c, d, 's', 'o', 'a', 'r'))
287 return ATOM_SORTARTIST;
288 else if (atom_compare(a, b, c, d, 's', 'o', 'a', 'a'))
289 return ATOM_SORTALBUMARTIST;
290 else if (atom_compare(a, b, c, d, 's', 'o', 'c', 'o'))
291 return ATOM_SORTWRITER;
292 else if (atom_compare(a, b, c, d, 's', 'o', 's', 'n'))
293 return ATOM_SORTSHOW;
294 } else if (a == COPYRIGHT_SYMBOL) {
295 if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'n', 'a', 'm'))
297 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'A', 'R', 'T'))
299 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'w', 'r', 't'))
301 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'a', 'l', 'b'))
303 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'd', 'a', 'y'))
305 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 't', 'o', 'o'))
307 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'c', 'm', 't'))
309 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'g', 'e', 'n'))
311 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'g', 'r', 'p'))
312 return ATOM_CONTENTGROUP;
313 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'l', 'y', 'r'))
317 if (atom_compare(a, b, c, d, 'e', 'd', 't', 's'))
319 else if (atom_compare(a, b, c, d, 'e', 's', 'd', 's'))
321 else if (atom_compare(a, b, c, d, 'f', 't', 'y', 'p'))
323 else if (atom_compare(a, b, c, d, 'f', 'r', 'e', 'e'))
325 else if (atom_compare(a, b, c, d, 'h', 'm', 'h', 'd'))
327 else if (atom_compare(a, b, c, d, 'v', 'm', 'h', 'd'))
329 else if (atom_compare(a, b, c, d, 'u', 'd', 't', 'a'))
331 else if (atom_compare(a, b, c, d, 'i', 'l', 's', 't'))
333 else if (atom_compare(a, b, c, d, 'n', 'a', 'm', 'e'))
335 else if (atom_compare(a, b, c, d, 'd', 'a', 't', 'a'))
337 else if (atom_compare(a, b, c, d, 'd', 'i', 's', 'k'))
339 else if (atom_compare(a, b, c, d, 'g', 'n', 'r', 'e'))
341 else if (atom_compare(a, b, c, d, 'c', 'o', 'v', 'r'))
343 else if (atom_compare(a, b, c, d, 'c', 'p', 'i', 'l'))
344 return ATOM_COMPILATION;
345 else if (atom_compare(a, b, c, d, 'c', 't', 't', 's'))
347 else if (atom_compare(a, b, c, d, 'd', 'r', 'm', 's'))
349 else if (atom_compare(a, b, c, d, 'f', 'r', 'm', 'a'))
351 else if (atom_compare(a, b, c, d, 'p', 'r', 'i', 'v'))
353 else if (atom_compare(a, b, c, d, 'i', 'v', 'i', 'v'))
355 else if (atom_compare(a, b, c, d, 'u', 's', 'e', 'r'))
357 else if (atom_compare(a, b, c, d, 'k', 'e', 'y', ' '))
359 else if (atom_compare(a, b, c, d, 'a', 'A', 'R', 'T'))
360 return ATOM_ALBUM_ARTIST;
361 else if (atom_compare(a, b, c, d, 'd', 'e', 's', 'c'))
362 return ATOM_DESCRIPTION;
363 else if (atom_compare(a, b, c, d, 'p', 'c', 's', 't'))
369 /* read atom header, return atom size, atom size is with header included */
370 static uint64_t atom_read_header(struct mp4 *f, uint8_t * atom_type,
371 uint8_t * header_size)
375 int8_t atom_header[8];
377 ret = read_data(f, atom_header, 8);
381 size = read_u32_be(atom_header);
384 /* check for 64 bit atom size */
387 size = read_int64(f);
389 *atom_type = atom_name_to_type(atom_header[4], atom_header[5],
390 atom_header[6], atom_header[7]);
394 static int64_t get_position(const struct mp4 *f)
396 return f->current_position;
399 static int need_parse_when_meta_only(uint8_t atom_type)
420 static int32_t set_position(struct mp4 *f, int64_t position)
422 f->cb->seek(f->cb->user_data, position);
423 f->current_position = position;
428 static void track_add(struct mp4 *f)
432 if (f->total_tracks > MAX_TRACKS) {
437 f->track[f->total_tracks - 1] = para_calloc(sizeof(struct mp4_track));
440 static int32_t read_stsz(struct mp4 *f)
445 if (f->total_tracks == 0)
447 t = f->track[f->total_tracks - 1];
448 read_int8(f); /* version */
449 read_int24(f); /* flags */
450 t->stsz_sample_size = read_int32(f);
451 t->stsz_sample_count = read_int32(f);
452 if (t->stsz_sample_size != 0)
454 t->stsz_table = para_malloc(t->stsz_sample_count * sizeof(int32_t));
455 for (i = 0; i < t->stsz_sample_count && !f->read_error; i++)
456 t->stsz_table[i] = read_int32(f);
460 static int32_t read_stts(struct mp4 *f)
466 if (f->total_tracks == 0)
468 t = f->track[f->total_tracks - 1];
469 if (t->stts_entry_count)
471 read_int8(f); /* version */
472 read_int24(f); /* flags */
473 t->stts_entry_count = read_int32(f);
475 t->stts_sample_count = para_malloc(t->stts_entry_count
477 t->stts_sample_delta = para_malloc(t->stts_entry_count
480 for (i = 0; i < t->stts_entry_count && !f->read_error; i++) {
481 t->stts_sample_count[i] = read_int32(f);
482 t->stts_sample_delta[i] = read_int32(f);
487 static int32_t read_stsc(struct mp4 *f)
492 if (f->total_tracks == 0)
494 t = f->track[f->total_tracks - 1];
496 read_int8(f); /* version */
497 read_int24(f); /* flags */
498 t->stsc_entry_count = read_int32(f);
499 t->stsc_first_chunk = para_malloc(t->stsc_entry_count * sizeof(int32_t));
500 t->stsc_samples_per_chunk = para_malloc(t->stsc_entry_count
502 t->stsc_sample_desc_index = para_malloc(t->stsc_entry_count *
506 for (i = 0; i < t->stsc_entry_count && !f->read_error; i++) {
507 t->stsc_first_chunk[i] = read_int32(f);
508 t->stsc_samples_per_chunk[i] = read_int32(f);
509 t->stsc_sample_desc_index[i] = read_int32(f);
514 static int32_t read_stco(struct mp4 *f)
519 if (f->total_tracks == 0)
521 t = f->track[f->total_tracks - 1];
523 read_int8(f); /* version */
524 read_int24(f); /* flags */
525 t->stco_entry_count = read_int32(f);
526 t->stco_chunk_offset = para_malloc(t->stco_entry_count
529 for (i = 0; i < t->stco_entry_count && !f->read_error; i++)
530 t->stco_chunk_offset[i] = read_int32(f);
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_int8(f); /* reserved */
547 /* data_reference_index */ read_int16(f);
549 read_int32(f); /* reserved */
550 read_int32(f); /* reserved */
552 t->channelCount = read_int16(f);
558 t->sampleRate = read_int16(f);
562 atom_read_header(f, &atom_type, &header_size);
566 static int32_t read_stsd(struct mp4 *f)
568 int32_t i, entry_count;
569 uint8_t header_size = 0;
573 if (f->total_tracks == 0)
575 t = f->track[f->total_tracks - 1];
577 read_int8(f); /* version */
578 read_int24(f); /* flags */
580 entry_count = read_int32(f);
583 for (i = 0; i < entry_count && !f->read_error; i++) {
584 uint64_t skip = get_position(f);
586 uint8_t atom_type = 0;
587 size = atom_read_header(f, &atom_type, &header_size);
589 t->is_audio = atom_type == ATOM_MP4A;
592 set_position(f, skip);
598 static int32_t tag_add_field(struct mp4_metadata *meta, const char *item,
599 const char *value, int32_t len)
601 meta->tags = para_realloc(meta->tags,
602 (meta->count + 1) * sizeof(struct mp4_tag));
603 meta->tags[meta->count].item = para_strdup(item);
604 meta->tags[meta->count].len = len;
606 meta->tags[meta->count].value = para_malloc(len + 1);
607 memcpy(meta->tags[meta->count].value, value, len);
608 meta->tags[meta->count].value[len] = 0;
610 meta->tags[meta->count].value = para_strdup(value);
616 static char *read_string(struct mp4 *f, uint32_t length)
618 char *str = para_malloc(length + 1);
619 if ((uint32_t)read_data(f, str, length) != length) {
627 static const char *get_metadata_name(uint8_t atom_type)
630 case ATOM_TITLE: return "title";
631 case ATOM_ARTIST: return "artist";
632 case ATOM_ALBUM: return "album";
633 case ATOM_DATE: return "date";
634 case ATOM_COMMENT: return "comment";
635 default: return "unknown";
639 static void parse_tag(struct mp4 *f, uint8_t parent, int32_t size)
641 uint64_t subsize, sumsize;
648 sumsize < size && !f->read_error; /* CVE-2017-9222 */
649 set_position(f, destpos), sumsize += subsize
652 uint8_t header_size = 0;
653 subsize = atom_read_header(f, &atom_type, &header_size);
654 destpos = get_position(f) + subsize - header_size;
655 if (atom_type != ATOM_DATA)
657 read_int8(f); /* version */
658 read_int24(f); /* flags */
659 read_int32(f); /* reserved */
661 data = read_string(f, subsize - (header_size + 8));
662 len = subsize - (header_size + 8);
666 tag_add_field(&f->meta, get_metadata_name(parent), data, len);
670 static int32_t read_mdhd(struct mp4 *f)
676 if (f->total_tracks == 0)
678 t = f->track[f->total_tracks - 1];
680 version = read_int32(f);
682 read_int64(f); //creation-time
683 read_int64(f); //modification-time
684 t->timeScale = read_int32(f); //timescale
685 t->duration = read_int64(f); //duration
686 } else { //version == 0
689 read_int32(f); //creation-time
690 read_int32(f); //modification-time
691 t->timeScale = read_int32(f); //timescale
692 temp = read_int32(f);
693 t->duration = (temp == (uint32_t) (-1))?
694 (uint64_t) (-1) : (uint64_t) (temp);
701 static int32_t read_ilst(struct mp4 *f, int32_t size)
703 uint64_t sumsize = 0;
705 while (sumsize < size) {
707 uint64_t subsize, destpos;
708 uint8_t header_size = 0;
709 subsize = atom_read_header(f, &atom_type, &header_size);
712 destpos = get_position(f) + subsize - header_size;
719 parse_tag(f, atom_type, subsize - header_size);
721 set_position(f, destpos);
728 static int32_t read_meta(struct mp4 *f, uint64_t size)
730 uint64_t subsize, sumsize = 0;
732 uint8_t header_size = 0;
734 read_int8(f); /* version */
735 read_int24(f); /* flags */
737 while (sumsize < (size - (header_size + 4))) {
738 subsize = atom_read_header(f, &atom_type, &header_size);
739 if (subsize <= header_size + 4)
741 if (atom_type == ATOM_ILST)
742 read_ilst(f, subsize - (header_size + 4));
744 set_position(f, get_position(f) + subsize - header_size);
751 static int32_t atom_read(struct mp4 *f, int32_t size, uint8_t atom_type)
753 uint64_t dest_position = get_position(f) + size - 8;
754 if (atom_type == ATOM_STSZ) {
755 /* sample size box */
757 } else if (atom_type == ATOM_STTS) {
758 /* time to sample box */
760 } else if (atom_type == ATOM_STSC) {
761 /* sample to chunk box */
763 } else if (atom_type == ATOM_STCO) {
764 /* chunk offset box */
766 } else if (atom_type == ATOM_STSD) {
767 /* sample description box */
769 } else if (atom_type == ATOM_MDHD) {
772 } else if (atom_type == ATOM_META) {
773 /* iTunes Metadata box */
777 set_position(f, dest_position);
781 /* parse atoms that are sub atoms of other atoms */
782 static int32_t parse_sub_atoms(struct mp4 *f, uint64_t total_size, int meta_only)
785 uint8_t atom_type = 0;
786 uint64_t counted_size = 0;
787 uint8_t header_size = 0;
789 while (counted_size < total_size) {
790 size = atom_read_header(f, &atom_type, &header_size);
791 counted_size += size;
793 /* check for end of file */
797 /* we're starting to read a new track, update index,
798 * so that all data and tables get written in the right place
800 if (atom_type == ATOM_TRAK)
803 if (meta_only && !need_parse_when_meta_only(atom_type)) {
804 set_position(f, get_position(f) + size - header_size);
805 } else if (atom_type < SUBATOMIC) {
806 parse_sub_atoms(f, size - header_size, meta_only);
808 atom_read(f, (uint32_t) size, atom_type);
815 /* parse root atoms */
816 static int32_t parse_atoms(struct mp4 *f, int meta_only)
819 uint8_t atom_type = 0;
820 uint8_t header_size = 0;
826 atom_read_header(f, &atom_type, &header_size)) != 0) {
827 f->file_size += size;
828 f->last_atom = atom_type;
830 if (atom_type == ATOM_MOOV && size > header_size) {
831 f->moov_offset = get_position(f) - header_size;
836 if (meta_only && !need_parse_when_meta_only(atom_type)) {
837 set_position(f, get_position(f) + size - header_size);
838 } else if (atom_type < SUBATOMIC) {
839 parse_sub_atoms(f, size - header_size, meta_only);
842 set_position(f, get_position(f) + size - header_size);
849 struct mp4 *mp4_open_read(const struct mp4_callback *cb)
851 struct mp4 *f = para_calloc(sizeof(struct mp4));
862 void mp4_close(struct mp4 *f)
866 for (i = 0; i < f->total_tracks; i++) {
868 free(f->track[i]->stsz_table);
869 free(f->track[i]->stts_sample_count);
870 free(f->track[i]->stts_sample_delta);
871 free(f->track[i]->stsc_first_chunk);
872 free(f->track[i]->stsc_samples_per_chunk);
873 free(f->track[i]->stsc_sample_desc_index);
874 free(f->track[i]->stco_chunk_offset);
878 for (i = 0; i < f->meta.count; i++) {
879 free(f->meta.tags[i].item);
880 free(f->meta.tags[i].value);
886 static int32_t chunk_of_sample(const struct mp4 *f, int32_t track,
887 int32_t sample, int32_t *chunk_sample, int32_t *chunk)
889 int32_t total_entries = 0;
891 int32_t chunk1, chunk2, chunk1samples, range_samples, total = 0;
895 if (f->track[track] == NULL) {
899 total_entries = f->track[track]->stsc_entry_count;
906 chunk2 = f->track[track]->stsc_first_chunk[chunk2entry];
907 *chunk = chunk2 - chunk1;
908 range_samples = *chunk * chunk1samples;
910 if (sample < total + range_samples)
913 chunk1samples = f->track[track]->stsc_samples_per_chunk[chunk2entry];
916 if (chunk2entry < total_entries) {
918 total += range_samples;
920 } while (chunk2entry < total_entries);
923 *chunk = (sample - total) / chunk1samples + chunk1;
927 *chunk_sample = total + (*chunk - chunk1) * chunk1samples;
932 static int32_t chunk_to_offset(const struct mp4 *f, int32_t track,
935 const struct mp4_track *p_track = f->track[track];
937 if (p_track->stco_entry_count && (chunk > p_track->stco_entry_count)) {
938 return p_track->stco_chunk_offset[p_track->stco_entry_count -
940 } else if (p_track->stco_entry_count) {
941 return p_track->stco_chunk_offset[chunk - 1];
949 static int32_t sample_range_size(const struct mp4 *f, int32_t track,
950 int32_t chunk_sample, int32_t sample)
953 const struct mp4_track *p_track = f->track[track];
955 if (p_track->stsz_sample_size) {
956 return (sample - chunk_sample) * p_track->stsz_sample_size;
958 if (sample >= p_track->stsz_sample_count)
961 for (i = chunk_sample, total = 0; i < sample; i++) {
962 total += p_track->stsz_table[i];
969 static int32_t sample_to_offset(const struct mp4 *f, int32_t track,
972 int32_t chunk, chunk_sample, chunk_offset1, chunk_offset2;
974 chunk_of_sample(f, track, sample, &chunk_sample, &chunk);
976 chunk_offset1 = chunk_to_offset(f, track, chunk);
977 chunk_offset2 = chunk_offset1 + sample_range_size(f,
978 track, chunk_sample, sample);
979 return chunk_offset2;
983 * Return the number of milliseconds of the given track.
985 * \param f As returned by \ref mp4_open_read(), must not be NULL.
986 * \param track Between zero and the value returned by \ref mp4_total_tracks().
988 * The function returns zero if the audio file is of zero length or contains a
989 * corrupt track header.
991 uint64_t mp4_get_duration(const struct mp4 *f, int32_t track)
993 const struct mp4_track *t = f->track[track];
995 if (t->timeScale == 0)
997 return t->duration * 1000 / t->timeScale;
1001 * Check whether the given track number corresponds to an audio track.
1003 * \param f See \ref mp4_get_duration().
1004 * \param track See \ref mp4_get_duration().
1006 * Besides audio tracks, an mp4 file may contain video and system tracks. For
1007 * those the function returns false.
1009 bool mp4_is_audio_track(const struct mp4 *f, int32_t track)
1011 return f->track[track]->is_audio;
1014 void mp4_set_sample_position(struct mp4 *f, int32_t track, int32_t sample)
1016 int32_t offset = sample_to_offset(f, track, sample);
1017 set_position(f, offset);
1020 int32_t mp4_get_sample_size(const struct mp4 *f, int track, int sample)
1022 const struct mp4_track *t = f->track[track];
1024 if (t->stsz_sample_size != 0)
1025 return t->stsz_sample_size;
1026 return t->stsz_table[sample];
1029 uint32_t mp4_get_sample_rate(const struct mp4 *f, int32_t track)
1031 return f->track[track]->sampleRate;
1034 uint32_t mp4_get_channel_count(const struct mp4 *f, int32_t track)
1036 return f->track[track]->channelCount;
1039 int32_t mp4_num_samples(const struct mp4 *f, int32_t track)
1044 for (i = 0; i < f->track[track]->stts_entry_count; i++) {
1045 total += f->track[track]->stts_sample_count[i];
1050 struct mp4 *mp4_open_meta(const struct mp4_callback *cb)
1052 struct mp4 *f = para_calloc(sizeof(struct mp4));
1064 * Return the metadata of an mp4 file.
1066 * \param f As returned by either \ref mp4_open_read() or \ref mp4_open_meta().
1068 * The caller is allowed to add, delete or modify the entries of the returned
1069 * structure in order to pass the modified version to \ref mp4_meta_update().
1071 struct mp4_metadata *mp4_get_meta(struct mp4 *f)
1076 static uint32_t find_atom(struct mp4 *f, uint64_t base, uint32_t size,
1079 uint32_t remaining = size;
1080 uint64_t atom_offset = base;
1085 set_position(f, atom_offset);
1089 atom_size = read_int32(f);
1090 if (atom_size > remaining || atom_size < 8)
1092 read_data(f, atom_name, 4);
1094 if (!memcmp(atom_name, name, 4)) {
1095 set_position(f, atom_offset);
1099 remaining -= atom_size;
1100 atom_offset += atom_size;
1105 static uint32_t find_atom_v2(struct mp4 *f, uint64_t base, uint32_t size,
1106 const char *name, uint32_t extraheaders, const char *name_inside)
1108 uint64_t first_base = (uint64_t) (-1);
1109 while (find_atom(f, base, size, name)) //try to find atom <name> with atom <name_inside> in it
1111 uint64_t mybase = get_position(f);
1112 uint32_t mysize = read_int32(f);
1114 if (first_base == (uint64_t) (-1))
1115 first_base = mybase;
1117 if (mysize < 8 + extraheaders)
1120 if (find_atom (f, mybase + (8 + extraheaders),
1121 mysize - (8 + extraheaders), name_inside)) {
1122 set_position(f, mybase);
1126 if (size <= mysize) {
1133 if (first_base != (uint64_t) (-1)) //wanted atom inside not found
1135 set_position(f, first_base);
1147 static struct membuffer *membuffer_create(void)
1149 struct membuffer *buf = para_calloc(sizeof(*buf));
1151 buf->allocated = 256;
1152 buf->data = para_malloc(buf->allocated);
1156 static void membuffer_write(struct membuffer *buf, const void *ptr,
1159 unsigned dest_size = buf->written + bytes;
1161 if (dest_size > buf->allocated) {
1163 buf->allocated <<= 1;
1164 } while (dest_size > buf->allocated);
1165 buf->data = para_realloc(buf->data, buf->allocated);
1169 memcpy((char *) buf->data + buf->written, ptr, bytes);
1170 buf->written += bytes;
1173 static void membuffer_write_atom_name(struct membuffer *buf, const char *data)
1175 membuffer_write(buf, data, 4);
1178 static void membuffer_write_int32(struct membuffer *buf, uint32_t data)
1181 write_u32_be(temp, data);
1182 membuffer_write(buf, temp, 4);
1185 static void membuffer_write_std_tag(struct membuffer *buf, const char *name,
1188 uint32_t len = strlen(value);
1189 membuffer_write_int32(buf, 8 /* atom header */
1190 + 8 /* data atom header */
1191 + 8 /* flags + reserved */
1193 membuffer_write_atom_name(buf, name);
1194 membuffer_write_int32(buf, 8 /* data atom header */
1195 + 8 /* flags + reserved */
1197 membuffer_write_atom_name(buf, "data");
1198 membuffer_write_int32(buf, 1); /* flags */
1199 membuffer_write_int32(buf, 0); /* reserved */
1200 membuffer_write(buf, value, len);
1203 static unsigned membuffer_get_size(const struct membuffer *buf)
1205 return buf->written;
1208 static void *membuffer_detach(struct membuffer *buf)
1210 void *ret = para_realloc(buf->data, buf->written);
1215 struct stdmeta_entry {
1220 static const char *find_standard_meta(const char *name)
1222 const struct stdmeta_entry stdmetas[] = {
1223 {"\xA9" "nam", "title"},
1224 {"\xA9" "ART", "artist"},
1225 {"\xA9" "alb", "album"},
1226 {"\xA9" "day", "date"},
1227 {"\xA9" "cmt", "comment"},
1230 for (unsigned n = 0; n < ARRAY_SIZE(stdmetas); n++)
1231 if (!strcasecmp(name, stdmetas[n].name))
1232 return stdmetas[n].atom;
1236 static uint32_t create_ilst(const struct mp4_metadata *meta, void **out_buffer,
1237 uint32_t * out_size)
1239 struct membuffer *buf = membuffer_create();
1242 for (metaptr = 0; metaptr < meta->count; metaptr++) {
1243 struct mp4_tag *tag = meta->tags + metaptr;
1244 const char *std_meta_atom = find_standard_meta(tag->item);
1246 membuffer_write_std_tag(buf, std_meta_atom, tag->value);
1248 PARA_ERROR_LOG("invalid tag item: %s\n", tag->item);
1250 *out_size = membuffer_get_size(buf);
1251 *out_buffer = membuffer_detach(buf);
1255 static void membuffer_write_atom(struct membuffer *buf, const char *name, unsigned size,
1258 membuffer_write_int32(buf, size + 8);
1259 membuffer_write_atom_name(buf, name);
1260 membuffer_write(buf, data, size);
1263 static void *membuffer_get_ptr(const struct membuffer *buf)
1268 static bool membuffer_transfer_from_file(struct membuffer *buf, struct mp4 *src,
1271 unsigned oldsize = membuffer_get_size(buf);
1274 membuffer_write(buf, 0, bytes);
1275 bufptr = membuffer_get_ptr(buf);
1276 if (read_data(src, bufptr + oldsize, bytes) != bytes) {
1284 static uint32_t create_meta(const struct mp4_metadata *meta, void **out_buffer,
1285 uint32_t * out_size)
1287 struct membuffer *buf;
1291 if (!create_ilst(meta, &ilst_buffer, &ilst_size))
1294 buf = membuffer_create();
1296 membuffer_write_int32(buf, 0);
1297 membuffer_write_atom(buf, "ilst", ilst_size, ilst_buffer);
1300 *out_size = membuffer_get_size(buf);
1301 *out_buffer = membuffer_detach(buf);
1305 static uint32_t create_udta(const struct mp4_metadata *meta, void **out_buffer,
1306 uint32_t * out_size)
1308 struct membuffer *buf;
1312 if (!create_meta(meta, &meta_buffer, &meta_size))
1315 buf = membuffer_create();
1317 membuffer_write_atom(buf, "meta", meta_size, meta_buffer);
1321 *out_size = membuffer_get_size(buf);
1322 *out_buffer = membuffer_detach(buf);
1326 static uint32_t fix_byte_order_32(uint32_t src)
1328 return read_u32_be(&src);
1331 static void *modify_moov(struct mp4 *f, uint32_t *out_size)
1333 uint64_t total_base = f->moov_offset + 8;
1334 uint32_t total_size = (uint32_t) (f->moov_size - 8);
1336 uint64_t udta_offset, meta_offset, ilst_offset;
1337 uint32_t udta_size, meta_size, ilst_size;
1339 uint32_t new_ilst_size;
1340 void *new_ilst_buffer, *out_buffer;
1345 if (!find_atom_v2(f, total_base, total_size, "udta", 0, "meta")) {
1346 struct membuffer *buf;
1347 void *new_udta_buffer;
1348 uint32_t new_udta_size;
1349 if (!create_udta(&f->meta, &new_udta_buffer, &new_udta_size))
1352 buf = membuffer_create();
1353 set_position(f, total_base);
1354 if (!membuffer_transfer_from_file(buf, f, total_size)) {
1355 free(new_udta_buffer);
1358 membuffer_write_atom(buf, "udta", new_udta_size,
1361 free(new_udta_buffer);
1363 *out_size = membuffer_get_size(buf);
1364 return membuffer_detach(buf);
1366 udta_offset = get_position(f);
1367 udta_size = read_int32(f);
1368 if (!find_atom_v2 (f, udta_offset + 8, udta_size - 8, "meta", 4, "ilst")) {
1369 struct membuffer *buf;
1370 void *new_meta_buffer;
1371 uint32_t new_meta_size;
1373 if (!create_meta(&f->meta, &new_meta_buffer, &new_meta_size))
1376 buf = membuffer_create();
1377 set_position(f, total_base);
1378 if (!membuffer_transfer_from_file(buf, f,
1379 udta_offset - total_base)) {
1380 free(new_meta_buffer);
1384 membuffer_write_int32(buf, udta_size + 8 + new_meta_size);
1385 membuffer_write_atom_name(buf, "udta");
1386 if (!membuffer_transfer_from_file(buf, f, udta_size)) {
1387 free(new_meta_buffer);
1391 membuffer_write_atom(buf, "meta", new_meta_size,
1393 free(new_meta_buffer);
1395 *out_size = membuffer_get_size(buf);
1396 return membuffer_detach(buf);
1398 meta_offset = get_position(f);
1399 meta_size = read_int32(f);
1400 /* shouldn't happen, find_atom_v2 above takes care of it */
1401 if (!find_atom(f, meta_offset + 12, meta_size - 12, "ilst"))
1403 ilst_offset = get_position(f);
1404 ilst_size = read_int32(f);
1405 if (!create_ilst(&f->meta, &new_ilst_buffer, &new_ilst_size))
1407 size_delta = new_ilst_size - (ilst_size - 8);
1408 *out_size = total_size + size_delta;
1409 out_buffer = para_malloc(*out_size);
1411 set_position(f, total_base);
1412 read_data(f, p_out, (uint32_t) (udta_offset - total_base));
1413 p_out += (uint32_t) (udta_offset - total_base);
1414 *(uint32_t *)p_out = fix_byte_order_32(read_int32(f) + size_delta);
1416 read_data(f, p_out, 4);
1418 read_data(f, p_out, (uint32_t) (meta_offset - udta_offset - 8));
1419 p_out += (uint32_t) (meta_offset - udta_offset - 8);
1420 *(uint32_t *)p_out = fix_byte_order_32(read_int32(f) + size_delta);
1422 read_data(f, p_out, 4);
1424 read_data(f, p_out, (uint32_t) (ilst_offset - meta_offset - 8));
1425 p_out += (uint32_t) (ilst_offset - meta_offset - 8);
1426 *(uint32_t *)p_out = fix_byte_order_32(read_int32(f) + size_delta);
1428 read_data(f, p_out, 4);
1430 memcpy(p_out, new_ilst_buffer, new_ilst_size);
1431 p_out += new_ilst_size;
1432 set_position(f, ilst_offset + ilst_size);
1433 read_data(f, p_out, (uint32_t) (total_size
1434 - (ilst_offset - total_base) - ilst_size));
1435 free(new_ilst_buffer);
1439 static int32_t write_data(struct mp4 *f, void *data, uint32_t size)
1443 result = f->cb->write(f->cb->user_data, data, size);
1445 f->current_position += size;
1450 static int32_t write_int32(struct mp4 *f, uint32_t data)
1453 write_u32_be(temp, data);
1454 return write_data(f, temp, sizeof(temp));
1457 int32_t mp4_meta_update(struct mp4 *f)
1459 void *new_moov_data;
1460 uint32_t new_moov_size;
1463 new_moov_data = modify_moov(f, &new_moov_size);
1464 if (!new_moov_data ) {
1468 /* copy moov atom to end of the file */
1469 if (f->last_atom != ATOM_MOOV) {
1470 char *free_data = "free";
1472 /* rename old moov to free */
1473 set_position(f, f->moov_offset + 4);
1474 write_data(f, free_data, 4);
1476 set_position(f, f->file_size);
1477 write_int32(f, new_moov_size + 8);
1478 write_data(f, "moov", 4);
1479 write_data(f, new_moov_data, new_moov_size);
1481 set_position(f, f->moov_offset);
1482 write_int32(f, new_moov_size + 8);
1483 write_data(f, "moov", 4);
1484 write_data(f, new_moov_data, new_moov_size);
1486 free(new_moov_data);
1487 f->cb->truncate(f->cb->user_data);
1491 static char *meta_find_by_name(const struct mp4 *f, const char *item)
1495 for (i = 0; i < f->meta.count; i++)
1496 if (!strcasecmp(f->meta.tags[i].item, item))
1497 return para_strdup(f->meta.tags[i].value);
1502 * Return the value of the artist meta tag of an mp4 file.
1504 * \param f Must not be NULL.
1506 * \return If the file does not contain this metadata tag, the function returns
1507 * NULL. Otherwise, a copy of the tag value is returned. The caller should free
1508 * this memory when it is no longer needed.
1510 char *mp4_meta_get_artist(const struct mp4 *f)
1512 return meta_find_by_name(f, "artist");
1516 * Return the value of the title meta tag of an mp4 file.
1518 * \param f See \ref mp4_meta_get_artist().
1519 * \return See \ref mp4_meta_get_artist().
1521 char *mp4_meta_get_title(const struct mp4 *f)
1523 return meta_find_by_name(f, "title");
1527 * Return the value of the date meta tag of an mp4 file.
1529 * \param f See \ref mp4_meta_get_artist().
1530 * \return See \ref mp4_meta_get_artist().
1532 char *mp4_meta_get_date(const struct mp4 *f)
1534 return meta_find_by_name(f, "date");
1538 * Return the value of the album meta tag of an mp4 file.
1540 * \param f See \ref mp4_meta_get_artist().
1541 * \return See \ref mp4_meta_get_artist().
1543 char *mp4_meta_get_album(const struct mp4 *f)
1545 return meta_find_by_name(f, "album");
1549 * Return the value of the comment meta tag of an mp4 file.
1551 * \param f See \ref mp4_meta_get_artist().
1552 * \return See \ref mp4_meta_get_artist().
1554 char *mp4_meta_get_comment(const struct mp4 *f)
1556 return meta_find_by_name(f, "comment");