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"
18 uint16_t channelCount;
22 uint32_t stsz_sample_size;
23 uint32_t stsz_sample_count;
27 uint32_t stts_entry_count;
28 uint32_t *stts_sample_count;
29 uint32_t *stts_sample_delta;
32 uint32_t stsc_entry_count;
33 uint32_t *stsc_first_chunk;
34 uint32_t *stsc_samples_per_chunk;
35 uint32_t *stsc_sample_desc_index;
38 uint32_t stco_entry_count;
39 uint32_t *stco_chunk_offset;
45 #define MAX_TRACKS 1024
48 const struct mp4_callback *cb;
49 int64_t current_position;
56 /* incremental track index while reading the file */
59 struct mp4_track *track[MAX_TRACKS];
62 struct mp4_metadata meta;
66 * Returns -1, 0, or 1 on errors/EOF/success. Partial reads followed by EOF or
67 * read errors are treated as errors.
69 static int read_data(struct mp4 *f, void *data, size_t size)
72 ssize_t ret = f->cb->read(f->cb->user_data, data, size);
73 if (ret < 0 && errno == EINTR)
75 /* regard EAGAIN as an error as reads should be blocking. */
77 return ret < 0? -1 : 0;
78 f->current_position += ret;
84 static int read_int64(struct mp4 *f, uint64_t *result)
87 int ret = read_data(f, data, 8);
89 if (ret > 0 && result)
90 *result = read_u64_be(data);
94 static int read_int32(struct mp4 *f, uint32_t *result)
97 int ret = read_data(f, data, 4);
99 if (ret > 0 && result)
100 *result = read_u32_be(data);
104 static int read_int24(struct mp4 *f, uint32_t *result)
107 int ret = read_data(f, data, 3);
109 if (ret > 0 && result)
110 *result = read_u24_be(data);
114 static int read_int16(struct mp4 *f, uint16_t *result)
117 int ret = read_data(f, data, 2);
119 if (ret > 0 && result)
120 *result = read_u16_be(data);
124 static uint8_t read_int8(struct mp4 *f, uint8_t *result)
127 int ret = read_data(f, data, 1);
129 if (ret > 0 && result)
134 static bool atom_compare(int8_t a1, int8_t b1, int8_t c1, int8_t d1,
135 int8_t a2, int8_t b2, int8_t c2, int8_t d2)
137 return a1 == a2 && b1 == b2 && c1 == c2 && d1 == d2;
141 /* atoms with subatoms */
149 ATOM_ILST = 8, /* iTunes Metadata list */
160 ATOM_COMPILATION = 19,
170 /* atoms without subatoms */
176 ATOM_MDHD = 134, /* track header */
180 ATOM_STSD = 138, /* sample description box */
181 ATOM_STTS = 139, /* time to sample box */
182 ATOM_STSZ = 140, /* sample size box */
184 ATOM_STCO = 142, /* chunk offset box */
185 ATOM_STSC = 143, /* sample to chunk box */
190 ATOM_META = 148, /* iTunes Metadata box */
191 ATOM_NAME = 149, /* iTunes Metadata name box */
192 ATOM_DATA = 150, /* iTunes Metadata data box */
199 ATOM_ALBUM_ARTIST = 157,
200 ATOM_CONTENTGROUP = 158,
202 ATOM_DESCRIPTION = 160,
205 ATOM_EPISODENAME = 163,
206 ATOM_SORTTITLE = 164,
207 ATOM_SORTALBUM = 165,
208 ATOM_SORTARTIST = 166,
209 ATOM_SORTALBUMARTIST = 167,
210 ATOM_SORTWRITER = 168,
219 #define ATOM_FREE ATOM_UNKNOWN
220 #define ATOM_SKIP ATOM_UNKNOWN
222 #define COPYRIGHT_SYMBOL ((int8_t)0xA9)
224 static uint8_t atom_name_to_type(int8_t a, int8_t b, int8_t c, int8_t d)
227 if (atom_compare(a, b, c, d, 'm', 'o', 'o', 'v'))
229 else if (atom_compare(a, b, c, d, 'm', 'i', 'n', 'f'))
231 else if (atom_compare(a, b, c, d, 'm', 'd', 'i', 'a'))
233 else if (atom_compare(a, b, c, d, 'm', 'd', 'a', 't'))
235 else if (atom_compare(a, b, c, d, 'm', 'd', 'h', 'd'))
237 else if (atom_compare(a, b, c, d, 'm', 'v', 'h', 'd'))
239 else if (atom_compare(a, b, c, d, 'm', 'p', '4', 'a'))
241 else if (atom_compare(a, b, c, d, 'm', 'p', '4', 'v'))
243 else if (atom_compare(a, b, c, d, 'm', 'p', '4', 's'))
245 else if (atom_compare(a, b, c, d, 'm', 'e', 't', 'a'))
247 } else if (a == 't') {
248 if (atom_compare(a, b, c, d, 't', 'r', 'a', 'k'))
250 else if (atom_compare(a, b, c, d, 't', 'k', 'h', 'd'))
252 else if (atom_compare(a, b, c, d, 't', 'r', 'e', 'f'))
254 else if (atom_compare(a, b, c, d, 't', 'r', 'k', 'n'))
256 else if (atom_compare(a, b, c, d, 't', 'm', 'p', 'o'))
258 else if (atom_compare(a, b, c, d, 't', 'v', 'n', 'n'))
260 else if (atom_compare(a, b, c, d, 't', 'v', 's', 'h'))
262 else if (atom_compare(a, b, c, d, 't', 'v', 'e', 'n'))
263 return ATOM_EPISODENAME;
264 else if (atom_compare(a, b, c, d, 't', 'v', 's', 'n'))
266 else if (atom_compare(a, b, c, d, 't', 'v', 'e', 's'))
268 } else if (a == 's') {
269 if (atom_compare(a, b, c, d, 's', 't', 'b', 'l'))
271 else if (atom_compare(a, b, c, d, 's', 'm', 'h', 'd'))
273 else if (atom_compare(a, b, c, d, 's', 't', 's', 'd'))
275 else if (atom_compare(a, b, c, d, 's', 't', 't', 's'))
277 else if (atom_compare(a, b, c, d, 's', 't', 'c', 'o'))
279 else if (atom_compare(a, b, c, d, 's', 't', 's', 'c'))
281 else if (atom_compare(a, b, c, d, 's', 't', 's', 'z'))
283 else if (atom_compare(a, b, c, d, 's', 't', 'z', '2'))
285 else if (atom_compare(a, b, c, d, 's', 'k', 'i', 'p'))
287 else if (atom_compare(a, b, c, d, 's', 'i', 'n', 'f'))
289 else if (atom_compare(a, b, c, d, 's', 'c', 'h', 'i'))
291 else if (atom_compare(a, b, c, d, 's', 'o', 'n', 'm'))
292 return ATOM_SORTTITLE;
293 else if (atom_compare(a, b, c, d, 's', 'o', 'a', 'l'))
294 return ATOM_SORTALBUM;
295 else if (atom_compare(a, b, c, d, 's', 'o', 'a', 'r'))
296 return ATOM_SORTARTIST;
297 else if (atom_compare(a, b, c, d, 's', 'o', 'a', 'a'))
298 return ATOM_SORTALBUMARTIST;
299 else if (atom_compare(a, b, c, d, 's', 'o', 'c', 'o'))
300 return ATOM_SORTWRITER;
301 else if (atom_compare(a, b, c, d, 's', 'o', 's', 'n'))
302 return ATOM_SORTSHOW;
303 } else if (a == COPYRIGHT_SYMBOL) {
304 if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'n', 'a', 'm'))
306 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'A', 'R', 'T'))
308 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'w', 'r', 't'))
310 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'a', 'l', 'b'))
312 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'd', 'a', 'y'))
314 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 't', 'o', 'o'))
316 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'c', 'm', 't'))
318 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'g', 'e', 'n'))
320 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'g', 'r', 'p'))
321 return ATOM_CONTENTGROUP;
322 else if (atom_compare(a, b, c, d, COPYRIGHT_SYMBOL, 'l', 'y', 'r'))
326 if (atom_compare(a, b, c, d, 'e', 'd', 't', 's'))
328 else if (atom_compare(a, b, c, d, 'e', 's', 'd', 's'))
330 else if (atom_compare(a, b, c, d, 'f', 't', 'y', 'p'))
332 else if (atom_compare(a, b, c, d, 'f', 'r', 'e', 'e'))
334 else if (atom_compare(a, b, c, d, 'h', 'm', 'h', 'd'))
336 else if (atom_compare(a, b, c, d, 'v', 'm', 'h', 'd'))
338 else if (atom_compare(a, b, c, d, 'u', 'd', 't', 'a'))
340 else if (atom_compare(a, b, c, d, 'i', 'l', 's', 't'))
342 else if (atom_compare(a, b, c, d, 'n', 'a', 'm', 'e'))
344 else if (atom_compare(a, b, c, d, 'd', 'a', 't', 'a'))
346 else if (atom_compare(a, b, c, d, 'd', 'i', 's', 'k'))
348 else if (atom_compare(a, b, c, d, 'g', 'n', 'r', 'e'))
350 else if (atom_compare(a, b, c, d, 'c', 'o', 'v', 'r'))
352 else if (atom_compare(a, b, c, d, 'c', 'p', 'i', 'l'))
353 return ATOM_COMPILATION;
354 else if (atom_compare(a, b, c, d, 'c', 't', 't', 's'))
356 else if (atom_compare(a, b, c, d, 'd', 'r', 'm', 's'))
358 else if (atom_compare(a, b, c, d, 'f', 'r', 'm', 'a'))
360 else if (atom_compare(a, b, c, d, 'p', 'r', 'i', 'v'))
362 else if (atom_compare(a, b, c, d, 'i', 'v', 'i', 'v'))
364 else if (atom_compare(a, b, c, d, 'u', 's', 'e', 'r'))
366 else if (atom_compare(a, b, c, d, 'k', 'e', 'y', ' '))
368 else if (atom_compare(a, b, c, d, 'a', 'A', 'R', 'T'))
369 return ATOM_ALBUM_ARTIST;
370 else if (atom_compare(a, b, c, d, 'd', 'e', 's', 'c'))
371 return ATOM_DESCRIPTION;
372 else if (atom_compare(a, b, c, d, 'p', 'c', 's', 't'))
378 /* read atom header, atom size is returned with header included. */
379 static int atom_read_header(struct mp4 *f, uint8_t *atom_type,
380 uint8_t *header_size, uint64_t *atom_size)
384 int8_t atom_header[8];
386 ret = read_data(f, atom_header, 8);
389 size = read_u32_be(atom_header);
390 if (size == 1) { /* 64 bit atom size */
393 ret = read_int64(f, atom_size);
402 *atom_type = atom_name_to_type(atom_header[4], atom_header[5],
403 atom_header[6], atom_header[7]);
407 static int64_t get_position(const struct mp4 *f)
409 return f->current_position;
412 static int need_parse_when_meta_only(uint8_t atom_type)
433 static int32_t set_position(struct mp4 *f, int64_t position)
435 f->cb->seek(f->cb->user_data, position);
436 f->current_position = position;
441 static int read_stsz(struct mp4 *f)
447 if (f->total_tracks == 0)
449 t = f->track[f->total_tracks - 1];
450 ret = read_int8(f, NULL); /* version */
453 ret = read_int24(f, NULL); /* flags */
456 ret = read_int32(f, &t->stsz_sample_size);
459 ret = read_int32(f, &t->stsz_sample_count);
462 if (t->stsz_sample_size != 0)
464 t->stsz_table = para_malloc(t->stsz_sample_count * sizeof(int32_t));
465 for (i = 0; i < t->stsz_sample_count; i++) {
466 ret = read_int32(f, &t->stsz_table[i]);
473 static int read_stts(struct mp4 *f)
479 if (f->total_tracks == 0)
481 t = f->track[f->total_tracks - 1];
482 if (t->stts_entry_count)
484 ret = read_int8(f, NULL); /* version */
487 ret = read_int24(f, NULL); /* flags */
490 ret = read_int32(f, &t->stts_entry_count);
493 t->stts_sample_count = para_malloc(t->stts_entry_count
495 t->stts_sample_delta = para_malloc(t->stts_entry_count
497 for (i = 0; i < t->stts_entry_count; i++) {
498 ret = read_int32(f, &t->stts_sample_count[i]);
501 ret = read_int32(f, &t->stts_sample_delta[i]);
508 static int read_stsc(struct mp4 *f)
514 if (f->total_tracks == 0)
516 t = f->track[f->total_tracks - 1];
518 ret = read_int8(f, NULL); /* version */
521 ret = read_int24(f, NULL); /* flags */
524 ret = read_int32(f, &t->stsc_entry_count);
527 t->stsc_first_chunk = para_malloc(t->stsc_entry_count * sizeof(int32_t));
528 t->stsc_samples_per_chunk = para_malloc(t->stsc_entry_count
530 t->stsc_sample_desc_index = para_malloc(t->stsc_entry_count *
533 for (i = 0; i < t->stsc_entry_count; i++) {
534 ret = read_int32(f, &t->stsc_first_chunk[i]);
537 ret = read_int32(f, &t->stsc_samples_per_chunk[i]);
540 ret = read_int32(f, &t->stsc_sample_desc_index[i]);
547 static int read_stco(struct mp4 *f)
553 if (f->total_tracks == 0)
555 t = f->track[f->total_tracks - 1];
557 ret = read_int8(f, NULL); /* version */
560 ret = read_int24(f, NULL); /* flags */
563 ret = read_int32(f, &t->stco_entry_count);
566 t->stco_chunk_offset = para_malloc(t->stco_entry_count
568 for (i = 0; i < t->stco_entry_count; i++) {
569 ret = read_int32(f, &t->stco_chunk_offset[i]);
576 static int read_mp4a(struct mp4 *f)
580 uint8_t atom_type = 0;
581 uint8_t header_size = 0;
584 if (f->total_tracks == 0)
586 t = f->track[f->total_tracks - 1];
588 for (i = 0; i < 6; i++) {
589 ret = read_int8(f, NULL); /* reserved */
593 ret = read_int16(f, NULL); /* data_reference_index */
596 ret = read_int32(f, NULL); /* reserved */
599 ret = read_int32(f, NULL); /* reserved */
602 ret = read_int16(f, &t->channelCount);
605 ret = read_int16(f, NULL);
608 ret = read_int16(f, NULL);
611 ret = read_int16(f, NULL);
614 ret = read_int16(f, &t->sampleRate);
617 ret = read_int16(f, NULL);
620 return atom_read_header(f, &atom_type, &header_size, NULL);
623 static int read_stsd(struct mp4 *f)
626 uint32_t i, entry_count;
627 uint8_t header_size = 0;
630 if (f->total_tracks == 0)
632 t = f->track[f->total_tracks - 1];
633 ret = read_int8(f, NULL); /* version */
636 ret = read_int24(f, NULL); /* flags */
639 ret = read_int32(f, &entry_count);
642 for (i = 0; i < entry_count; i++) {
643 uint64_t skip = get_position(f);
645 uint8_t atom_type = 0;
646 ret = atom_read_header(f, &atom_type, &header_size, &size);
650 t->is_audio = atom_type == ATOM_MP4A;
653 set_position(f, skip);
658 static int32_t tag_add_field(struct mp4_metadata *meta, const char *item,
659 const char *value, int32_t len)
661 meta->tags = para_realloc(meta->tags,
662 (meta->count + 1) * sizeof(struct mp4_tag));
663 meta->tags[meta->count].item = para_strdup(item);
664 meta->tags[meta->count].len = len;
666 meta->tags[meta->count].value = para_malloc(len + 1);
667 memcpy(meta->tags[meta->count].value, value, len);
668 meta->tags[meta->count].value[len] = 0;
670 meta->tags[meta->count].value = para_strdup(value);
676 static int read_string(struct mp4 *f, uint32_t length, char **result)
678 char *str = para_malloc(length + 1);
679 int ret = read_data(f, str, length);
691 static const char *get_metadata_name(uint8_t atom_type)
694 case ATOM_TITLE: return "title";
695 case ATOM_ARTIST: return "artist";
696 case ATOM_ALBUM: return "album";
697 case ATOM_DATE: return "date";
698 case ATOM_COMMENT: return "comment";
699 default: return "unknown";
703 static int parse_tag(struct mp4 *f, uint8_t parent, int32_t size)
706 uint64_t subsize, sumsize;
714 set_position(f, destpos), sumsize += subsize
717 uint8_t header_size = 0;
718 ret = atom_read_header(f, &atom_type, &header_size, &subsize);
721 destpos = get_position(f) + subsize - header_size;
722 if (atom_type != ATOM_DATA)
724 ret = read_int8(f, NULL); /* version */
727 ret = read_int24(f, NULL); /* flags */
730 ret = read_int32(f, NULL); /* reserved */
734 ret = read_string(f, subsize - (header_size + 8), &data);
737 len = subsize - (header_size + 8);
741 tag_add_field(&f->meta, get_metadata_name(parent), data, len);
746 static int read_mdhd(struct mp4 *f)
752 if (f->total_tracks == 0)
754 t = f->track[f->total_tracks - 1];
756 ret = read_int32(f, &version);
760 ret = read_int64(f, NULL); /* creation-time */
763 ret = read_int64(f, NULL); /* modification-time */
766 ret = read_int32(f, &t->timeScale);
769 ret = read_int64(f, &t->duration);
772 } else { //version == 0
775 ret = read_int32(f, NULL); /* creation-time */
778 ret = read_int32(f, NULL); /* modification-time */
781 ret = read_int32(f, &t->timeScale);
784 ret = read_int32(f, &temp);
787 t->duration = (temp == (uint32_t) (-1))?
788 (uint64_t) (-1) : (uint64_t) (temp);
790 ret = read_int16(f, NULL);
793 ret = read_int16(f, NULL);
799 static int32_t read_ilst(struct mp4 *f, int32_t size)
802 uint64_t sumsize = 0;
804 while (sumsize < size) {
806 uint64_t subsize, destpos;
807 uint8_t header_size = 0;
808 ret = atom_read_header(f, &atom_type, &header_size, &subsize);
811 destpos = get_position(f) + subsize - header_size;
818 parse_tag(f, atom_type, subsize - header_size);
820 set_position(f, destpos);
826 static int32_t read_meta(struct mp4 *f, uint64_t size)
829 uint64_t subsize, sumsize = 0;
831 uint8_t header_size = 0;
833 ret = read_int8(f, NULL); /* version */
836 ret = read_int24(f, NULL); /* flags */
839 while (sumsize < (size - (header_size + 4))) {
840 ret = atom_read_header(f, &atom_type, &header_size, &subsize);
843 if (subsize <= header_size + 4)
845 if (atom_type == ATOM_ILST)
846 read_ilst(f, subsize - (header_size + 4));
848 set_position(f, get_position(f) + subsize - header_size);
854 static int atom_read(struct mp4 *f, uint64_t size, uint8_t atom_type)
856 uint64_t dest_position = get_position(f) + size - 8;
857 int ret = 1; /* return success for atoms we don't care about */
860 case ATOM_STSZ: ret = read_stsz(f); break;
861 case ATOM_STTS: ret = read_stts(f); break;
862 case ATOM_STSC: ret = read_stsc(f); break;
863 case ATOM_STCO: ret = read_stco(f); break;
864 case ATOM_STSD: ret = read_stsd(f); break;
865 case ATOM_MDHD: ret = read_mdhd(f); break;
866 case ATOM_META: ret = read_meta(f, size); break;
868 set_position(f, dest_position);
872 /* parse atoms that are sub atoms of other atoms */
873 static int parse_sub_atoms(struct mp4 *f, uint64_t total_size, int meta_only)
877 uint8_t atom_type = 0;
878 uint64_t counted_size = 0;
879 uint8_t header_size = 0;
881 while (counted_size < total_size) {
882 ret = atom_read_header(f, &atom_type, &header_size, &size);
887 counted_size += size;
888 if (atom_type == ATOM_TRAK) {
889 if (f->total_tracks >= MAX_TRACKS)
892 f->track[f->total_tracks - 1] = para_calloc(
893 sizeof(struct mp4_track));
896 if (meta_only && !need_parse_when_meta_only(atom_type)) {
897 set_position(f, get_position(f) + size - header_size);
898 } else if (atom_type < SUBATOMIC) {
899 ret = parse_sub_atoms(f, size - header_size, meta_only);
903 ret = atom_read(f, size, atom_type);
911 /* parse root atoms */
912 static int parse_atoms(struct mp4 *f, int meta_only)
916 uint8_t atom_type = 0;
917 uint8_t header_size = 0;
921 while ((ret = atom_read_header(f, &atom_type, &header_size, &size)) > 0) {
922 f->file_size += size;
923 f->last_atom = atom_type;
925 if (atom_type == ATOM_MOOV && size > header_size) {
926 f->moov_offset = get_position(f) - header_size;
931 if (meta_only && !need_parse_when_meta_only(atom_type)) {
932 set_position(f, get_position(f) + size - header_size);
933 } else if (atom_type < SUBATOMIC) {
934 ret = parse_sub_atoms(f, size - header_size, meta_only);
939 set_position(f, get_position(f) + size - header_size);
945 struct mp4 *mp4_open_read(const struct mp4_callback *cb)
948 struct mp4 *f = para_calloc(sizeof(struct mp4));
951 ret = parse_atoms(f, 0);
959 void mp4_close(struct mp4 *f)
963 for (i = 0; i < f->total_tracks; i++) {
965 free(f->track[i]->stsz_table);
966 free(f->track[i]->stts_sample_count);
967 free(f->track[i]->stts_sample_delta);
968 free(f->track[i]->stsc_first_chunk);
969 free(f->track[i]->stsc_samples_per_chunk);
970 free(f->track[i]->stsc_sample_desc_index);
971 free(f->track[i]->stco_chunk_offset);
975 for (i = 0; i < f->meta.count; i++) {
976 free(f->meta.tags[i].item);
977 free(f->meta.tags[i].value);
983 static int32_t chunk_of_sample(const struct mp4 *f, int32_t track,
984 int32_t sample, int32_t *chunk_sample, int32_t *chunk)
986 int32_t total_entries = 0;
988 int32_t chunk1, chunk2, chunk1samples, range_samples, total = 0;
993 total_entries = f->track[track]->stsc_entry_count;
1000 chunk2 = f->track[track]->stsc_first_chunk[chunk2entry];
1001 *chunk = chunk2 - chunk1;
1002 range_samples = *chunk * chunk1samples;
1004 if (sample < total + range_samples)
1007 chunk1samples = f->track[track]->stsc_samples_per_chunk[chunk2entry];
1010 if (chunk2entry < total_entries) {
1012 total += range_samples;
1014 } while (chunk2entry < total_entries);
1017 *chunk = (sample - total) / chunk1samples + chunk1;
1021 *chunk_sample = total + (*chunk - chunk1) * chunk1samples;
1027 * Return the number of milliseconds of the given track.
1029 * \param f As returned by \ref mp4_open_read(), must not be NULL.
1030 * \param track Between zero and the value returned by \ref mp4_get_total_tracks().
1032 * The function returns zero if the audio file is of zero length or contains a
1033 * corrupt track header.
1035 uint64_t mp4_get_duration(const struct mp4 *f, int32_t track)
1037 const struct mp4_track *t = f->track[track];
1039 if (t->timeScale == 0)
1041 return t->duration * 1000 / t->timeScale;
1044 int32_t mp4_get_total_tracks(const struct mp4 *f)
1046 return f->total_tracks;
1050 * Check whether the given track number corresponds to an audio track.
1052 * \param f See \ref mp4_get_duration().
1053 * \param track See \ref mp4_get_duration().
1055 * Besides audio tracks, an mp4 file may contain video and system tracks. For
1056 * those the function returns false.
1058 bool mp4_is_audio_track(const struct mp4 *f, int32_t track)
1060 return f->track[track]->is_audio;
1063 int mp4_set_sample_position(struct mp4 *f, uint32_t track, int32_t sample)
1065 const struct mp4_track *t = f->track[track];
1066 int32_t offset, chunk, chunk_sample;
1067 uint32_t n, srs; /* sample range size */
1069 if (sample >= t->stsz_sample_count || track >= f->total_tracks)
1070 return -ERRNO_TO_PARA_ERROR(EINVAL);
1071 chunk_of_sample(f, track, sample, &chunk_sample, &chunk);
1072 if (t->stsz_sample_size > 0)
1073 srs = (sample - chunk_sample) * t->stsz_sample_size;
1075 for (srs = 0, n = chunk_sample; n < sample; n++)
1076 srs += t->stsz_table[n];
1078 if (t->stco_entry_count > 0 && chunk > t->stco_entry_count)
1079 offset = t->stco_chunk_offset[t->stco_entry_count - 1];
1080 else if (t->stco_entry_count > 0)
1081 offset = t->stco_chunk_offset[chunk - 1];
1084 set_position(f, offset + srs);
1088 int32_t mp4_get_sample_size(const struct mp4 *f, int track, int sample)
1090 const struct mp4_track *t = f->track[track];
1092 if (t->stsz_sample_size != 0)
1093 return t->stsz_sample_size;
1094 return t->stsz_table[sample];
1097 uint32_t mp4_get_sample_rate(const struct mp4 *f, int32_t track)
1099 return f->track[track]->sampleRate;
1102 uint32_t mp4_get_channel_count(const struct mp4 *f, int32_t track)
1104 return f->track[track]->channelCount;
1107 int32_t mp4_num_samples(const struct mp4 *f, int32_t track)
1112 for (i = 0; i < f->track[track]->stts_entry_count; i++) {
1113 total += f->track[track]->stts_sample_count[i];
1118 struct mp4 *mp4_open_meta(const struct mp4_callback *cb)
1121 struct mp4 *f = para_calloc(sizeof(struct mp4));
1124 ret = parse_atoms(f, 1);
1133 * Return the metadata of an mp4 file.
1135 * \param f As returned by either \ref mp4_open_read() or \ref mp4_open_meta().
1137 * The caller is allowed to add, delete or modify the entries of the returned
1138 * structure in order to pass the modified version to \ref mp4_meta_update().
1140 struct mp4_metadata *mp4_get_meta(struct mp4 *f)
1145 static int find_atom(struct mp4 *f, uint64_t base, uint32_t size,
1148 uint32_t remaining = size;
1149 uint64_t atom_offset = base;
1156 set_position(f, atom_offset);
1160 ret = read_int32(f, &atom_size);
1163 if (atom_size > remaining || atom_size < 8)
1165 ret = read_data(f, atom_name, 4);
1168 if (!memcmp(atom_name, name, 4)) {
1169 set_position(f, atom_offset);
1172 remaining -= atom_size;
1173 atom_offset += atom_size;
1178 * Try to find atom <name> with atom <name_inside> in it. Besides -1/0/1 for
1179 * error, EOF and success, this function may return 2 to indicate that the
1180 * desired atoms were not found.
1182 static int find_atom_v2(struct mp4 *f, uint64_t base, uint32_t size,
1183 const char *name, uint32_t extraheaders, const char *name_inside)
1185 uint64_t first_base = (uint64_t) (-1);
1190 int ret = find_atom(f, base, size, name);
1194 mybase = get_position(f);
1195 ret = read_int32(f, &mysize);
1198 if (first_base == (uint64_t) (-1))
1199 first_base = mybase;
1201 if (mysize < 8 + extraheaders)
1204 if (find_atom (f, mybase + (8 + extraheaders),
1205 mysize - (8 + extraheaders), name_inside)) {
1206 set_position(f, mybase);
1214 if (first_base != (uint64_t)(-1)) {
1215 set_position(f, first_base);
1218 /* wanted atom inside not found */
1228 static struct membuffer *membuffer_create(void)
1230 struct membuffer *buf = para_calloc(sizeof(*buf));
1232 buf->allocated = 256;
1233 buf->data = para_malloc(buf->allocated);
1237 static void membuffer_write(struct membuffer *buf, const void *ptr,
1240 unsigned dest_size = buf->written + bytes;
1242 if (dest_size > buf->allocated) {
1244 buf->allocated <<= 1;
1245 } while (dest_size > buf->allocated);
1246 buf->data = para_realloc(buf->data, buf->allocated);
1250 memcpy((char *) buf->data + buf->written, ptr, bytes);
1251 buf->written += bytes;
1254 static void membuffer_write_atom_name(struct membuffer *buf, const char *data)
1256 membuffer_write(buf, data, 4);
1259 static void membuffer_write_int32(struct membuffer *buf, uint32_t data)
1262 write_u32_be(temp, data);
1263 membuffer_write(buf, temp, 4);
1266 static void membuffer_write_std_tag(struct membuffer *buf, const char *name,
1269 uint32_t len = strlen(value);
1270 membuffer_write_int32(buf, 8 /* atom header */
1271 + 8 /* data atom header */
1272 + 8 /* flags + reserved */
1274 membuffer_write_atom_name(buf, name);
1275 membuffer_write_int32(buf, 8 /* data atom header */
1276 + 8 /* flags + reserved */
1278 membuffer_write_atom_name(buf, "data");
1279 membuffer_write_int32(buf, 1); /* flags */
1280 membuffer_write_int32(buf, 0); /* reserved */
1281 membuffer_write(buf, value, len);
1284 static unsigned membuffer_get_size(const struct membuffer *buf)
1286 return buf->written;
1289 static void *membuffer_detach(struct membuffer *buf)
1291 void *ret = para_realloc(buf->data, buf->written);
1296 struct stdmeta_entry {
1301 static const char *find_standard_meta(const char *name)
1303 const struct stdmeta_entry stdmetas[] = {
1304 {"\xA9" "nam", "title"},
1305 {"\xA9" "ART", "artist"},
1306 {"\xA9" "alb", "album"},
1307 {"\xA9" "day", "date"},
1308 {"\xA9" "cmt", "comment"},
1311 for (unsigned n = 0; n < ARRAY_SIZE(stdmetas); n++)
1312 if (!strcasecmp(name, stdmetas[n].name))
1313 return stdmetas[n].atom;
1317 static uint32_t create_ilst(const struct mp4_metadata *meta, void **out_buffer,
1318 uint32_t * out_size)
1320 struct membuffer *buf = membuffer_create();
1323 for (metaptr = 0; metaptr < meta->count; metaptr++) {
1324 struct mp4_tag *tag = meta->tags + metaptr;
1325 const char *std_meta_atom = find_standard_meta(tag->item);
1327 membuffer_write_std_tag(buf, std_meta_atom, tag->value);
1329 PARA_ERROR_LOG("invalid tag item: %s\n", tag->item);
1331 *out_size = membuffer_get_size(buf);
1332 *out_buffer = membuffer_detach(buf);
1336 static void membuffer_write_atom(struct membuffer *buf, const char *name, unsigned size,
1339 membuffer_write_int32(buf, size + 8);
1340 membuffer_write_atom_name(buf, name);
1341 membuffer_write(buf, data, size);
1344 static void *membuffer_get_ptr(const struct membuffer *buf)
1349 static bool membuffer_transfer_from_file(struct membuffer *buf, struct mp4 *src,
1352 unsigned oldsize = membuffer_get_size(buf);
1355 membuffer_write(buf, 0, bytes);
1356 bufptr = membuffer_get_ptr(buf);
1357 if (read_data(src, bufptr + oldsize, bytes) != 1) {
1365 static uint32_t create_meta(const struct mp4_metadata *meta, void **out_buffer,
1366 uint32_t * out_size)
1368 struct membuffer *buf;
1372 if (!create_ilst(meta, &ilst_buffer, &ilst_size))
1375 buf = membuffer_create();
1377 membuffer_write_int32(buf, 0);
1378 membuffer_write_atom(buf, "ilst", ilst_size, ilst_buffer);
1381 *out_size = membuffer_get_size(buf);
1382 *out_buffer = membuffer_detach(buf);
1386 static uint32_t create_udta(const struct mp4_metadata *meta, void **out_buffer,
1387 uint32_t * out_size)
1389 struct membuffer *buf;
1393 if (!create_meta(meta, &meta_buffer, &meta_size))
1396 buf = membuffer_create();
1398 membuffer_write_atom(buf, "meta", meta_size, meta_buffer);
1402 *out_size = membuffer_get_size(buf);
1403 *out_buffer = membuffer_detach(buf);
1407 static uint32_t fix_byte_order_32(uint32_t src)
1409 return read_u32_be(&src);
1412 static void *modify_moov(struct mp4 *f, uint32_t *out_size)
1415 uint64_t total_base = f->moov_offset + 8;
1416 uint32_t total_size = (uint32_t) (f->moov_size - 8);
1417 uint64_t udta_offset, meta_offset, ilst_offset;
1418 uint32_t udta_size, meta_size, ilst_size;
1419 uint32_t new_ilst_size;
1420 void *new_ilst_buffer, *out_buffer;
1425 ret = find_atom_v2(f, total_base, total_size, "udta", 0, "meta");
1429 struct membuffer *buf;
1430 void *new_udta_buffer;
1431 uint32_t new_udta_size;
1432 if (!create_udta(&f->meta, &new_udta_buffer, &new_udta_size))
1435 buf = membuffer_create();
1436 set_position(f, total_base);
1437 if (!membuffer_transfer_from_file(buf, f, total_size)) {
1438 free(new_udta_buffer);
1441 membuffer_write_atom(buf, "udta", new_udta_size,
1444 free(new_udta_buffer);
1446 *out_size = membuffer_get_size(buf);
1447 return membuffer_detach(buf);
1449 udta_offset = get_position(f);
1450 ret = read_int32(f, &udta_size);
1453 ret = find_atom_v2(f, udta_offset + 8, udta_size - 8, "meta", 4, "ilst");
1457 struct membuffer *buf;
1458 void *new_meta_buffer;
1459 uint32_t new_meta_size;
1461 if (!create_meta(&f->meta, &new_meta_buffer, &new_meta_size))
1464 buf = membuffer_create();
1465 set_position(f, total_base);
1466 if (!membuffer_transfer_from_file(buf, f,
1467 udta_offset - total_base)) {
1468 free(new_meta_buffer);
1472 membuffer_write_int32(buf, udta_size + 8 + new_meta_size);
1473 membuffer_write_atom_name(buf, "udta");
1474 if (!membuffer_transfer_from_file(buf, f, udta_size)) {
1475 free(new_meta_buffer);
1478 membuffer_write_atom(buf, "meta", new_meta_size,
1480 free(new_meta_buffer);
1482 *out_size = membuffer_get_size(buf);
1483 return membuffer_detach(buf);
1485 meta_offset = get_position(f);
1486 ret = read_int32(f, &meta_size);
1489 /* shouldn't happen, find_atom_v2 above takes care of it */
1490 if (!find_atom(f, meta_offset + 12, meta_size - 12, "ilst"))
1492 ilst_offset = get_position(f);
1493 ret = read_int32(f, &ilst_size);
1496 if (!create_ilst(&f->meta, &new_ilst_buffer, &new_ilst_size))
1498 size_delta = new_ilst_size - (ilst_size - 8);
1499 *out_size = total_size + size_delta;
1500 out_buffer = para_malloc(*out_size);
1502 set_position(f, total_base);
1503 ret = read_data(f, p_out, udta_offset - total_base);
1506 p_out += (uint32_t) (udta_offset - total_base);
1507 ret = read_int32(f, &tmp);
1510 *(uint32_t *)p_out = fix_byte_order_32(tmp + size_delta);
1512 ret = read_data(f, p_out, 4);
1516 ret = read_data(f, p_out, meta_offset - udta_offset - 8);
1519 p_out += (uint32_t) (meta_offset - udta_offset - 8);
1520 ret = read_int32(f, &tmp);
1523 *(uint32_t *)p_out = fix_byte_order_32(tmp + size_delta);
1525 ret = read_data(f, p_out, 4);
1529 ret = read_data(f, p_out, ilst_offset - meta_offset - 8);
1532 p_out += (uint32_t) (ilst_offset - meta_offset - 8);
1533 ret = read_int32(f, &tmp);
1536 *(uint32_t *)p_out = fix_byte_order_32(tmp + size_delta);
1538 ret = read_data(f, p_out, 4);
1542 memcpy(p_out, new_ilst_buffer, new_ilst_size);
1543 p_out += new_ilst_size;
1544 set_position(f, ilst_offset + ilst_size);
1545 ret = read_data(f, p_out, total_size
1546 - (ilst_offset - total_base) - ilst_size);
1549 free(new_ilst_buffer);
1553 static int32_t write_data(struct mp4 *f, void *data, uint32_t size)
1557 result = f->cb->write(f->cb->user_data, data, size);
1559 f->current_position += size;
1564 static int32_t write_int32(struct mp4 *f, uint32_t data)
1567 write_u32_be(temp, data);
1568 return write_data(f, temp, sizeof(temp));
1571 int32_t mp4_meta_update(struct mp4 *f)
1573 void *new_moov_data;
1574 uint32_t new_moov_size;
1577 new_moov_data = modify_moov(f, &new_moov_size);
1578 if (!new_moov_data ) {
1582 /* copy moov atom to end of the file */
1583 if (f->last_atom != ATOM_MOOV) {
1584 char *free_data = "free";
1586 /* rename old moov to free */
1587 set_position(f, f->moov_offset + 4);
1588 write_data(f, free_data, 4);
1590 set_position(f, f->file_size);
1591 write_int32(f, new_moov_size + 8);
1592 write_data(f, "moov", 4);
1593 write_data(f, new_moov_data, new_moov_size);
1595 set_position(f, f->moov_offset);
1596 write_int32(f, new_moov_size + 8);
1597 write_data(f, "moov", 4);
1598 write_data(f, new_moov_data, new_moov_size);
1600 free(new_moov_data);
1601 f->cb->truncate(f->cb->user_data);
1605 static char *meta_find_by_name(const struct mp4 *f, const char *item)
1609 for (i = 0; i < f->meta.count; i++)
1610 if (!strcasecmp(f->meta.tags[i].item, item))
1611 return para_strdup(f->meta.tags[i].value);
1616 * Return the value of the artist meta tag of an mp4 file.
1618 * \param f Must not be NULL.
1620 * \return If the file does not contain this metadata tag, the function returns
1621 * NULL. Otherwise, a copy of the tag value is returned. The caller should free
1622 * this memory when it is no longer needed.
1624 char *mp4_meta_get_artist(const struct mp4 *f)
1626 return meta_find_by_name(f, "artist");
1630 * Return the value of the title meta tag of an mp4 file.
1632 * \param f See \ref mp4_meta_get_artist().
1633 * \return See \ref mp4_meta_get_artist().
1635 char *mp4_meta_get_title(const struct mp4 *f)
1637 return meta_find_by_name(f, "title");
1641 * Return the value of the date meta tag of an mp4 file.
1643 * \param f See \ref mp4_meta_get_artist().
1644 * \return See \ref mp4_meta_get_artist().
1646 char *mp4_meta_get_date(const struct mp4 *f)
1648 return meta_find_by_name(f, "date");
1652 * Return the value of the album meta tag of an mp4 file.
1654 * \param f See \ref mp4_meta_get_artist().
1655 * \return See \ref mp4_meta_get_artist().
1657 char *mp4_meta_get_album(const struct mp4 *f)
1659 return meta_find_by_name(f, "album");
1663 * Return the value of the comment meta tag of an mp4 file.
1665 * \param f See \ref mp4_meta_get_artist().
1666 * \return See \ref mp4_meta_get_artist().
1668 char *mp4_meta_get_comment(const struct mp4 *f)
1670 return meta_find_by_name(f, "comment");