X-Git-Url: http://git.tuebingen.mpg.de/?a=blobdiff_plain;f=mp4.c;h=92be4c766724b471c7d31036b41b3408f3e36406;hb=70941e3cfbdb5af190d76dc8aabb6d57b86638ae;hp=71858305517cac98f2093050749dbadb1d0d94a1;hpb=623699153a7718e29d6571a9fea1b96244813ef9;p=paraslash.git diff --git a/mp4.c b/mp4.c index 71858305..92be4c76 100644 --- a/mp4.c +++ b/mp4.c @@ -14,7 +14,6 @@ #include "mp4.h" struct mp4_track { - bool is_audio; uint16_t channelCount; uint16_t sampleRate; @@ -26,13 +25,11 @@ struct mp4_track { /* stts */ uint32_t stts_entry_count; uint32_t *stts_sample_count; - uint32_t *stts_sample_delta; /* stsc */ uint32_t stsc_entry_count; uint32_t *stsc_first_chunk; uint32_t *stsc_samples_per_chunk; - uint32_t *stsc_sample_desc_index; /* stsc */ uint32_t stco_entry_count; @@ -57,6 +54,8 @@ struct mp4 { int32_t total_tracks; /* track data */ struct mp4_track *track[MAX_TRACKS]; + /* the first audio track found */ + struct mp4_track *audio_track; /* metadata */ struct mp4_metadata meta; @@ -409,27 +408,6 @@ static int64_t get_position(const struct mp4 *f) return f->current_position; } -static int need_parse_when_meta_only(uint8_t atom_type) -{ - switch (atom_type) { - case ATOM_EDTS: - case ATOM_DRMS: - case ATOM_SINF: - case ATOM_SCHI: - case ATOM_STTS: - case ATOM_STSZ: - case ATOM_STZ2: - case ATOM_STCO: - case ATOM_STSC: - case ATOM_FRMA: - case ATOM_IVIV: - case ATOM_PRIV: - return 0; - default: - return 1; - } -} - static int32_t set_position(struct mp4 *f, int64_t position) { f->cb->seek(f->cb->user_data, position); @@ -492,13 +470,11 @@ static int read_stts(struct mp4 *f) return ret; t->stts_sample_count = para_malloc(t->stts_entry_count * sizeof(int32_t)); - t->stts_sample_delta = para_malloc(t->stts_entry_count - * sizeof (int32_t)); for (i = 0; i < t->stts_entry_count; i++) { ret = read_int32(f, &t->stts_sample_count[i]); if (ret <= 0) return ret; - ret = read_int32(f, &t->stts_sample_delta[i]); + ret = read_int32(f, NULL); /* sample delta */ if (ret <= 0) return ret; } @@ -527,9 +503,6 @@ static int read_stsc(struct mp4 *f) t->stsc_first_chunk = para_malloc(t->stsc_entry_count * sizeof(int32_t)); t->stsc_samples_per_chunk = para_malloc(t->stsc_entry_count * sizeof (int32_t)); - t->stsc_sample_desc_index = para_malloc(t->stsc_entry_count * - sizeof (int32_t)); - for (i = 0; i < t->stsc_entry_count; i++) { ret = read_int32(f, &t->stsc_first_chunk[i]); if (ret <= 0) @@ -537,7 +510,7 @@ static int read_stsc(struct mp4 *f) ret = read_int32(f, &t->stsc_samples_per_chunk[i]); if (ret <= 0) return ret; - ret = read_int32(f, &t->stsc_sample_desc_index[i]); + ret = read_int32(f, NULL); /* sample desc index */ if (ret <= 0) return ret; } @@ -577,8 +550,6 @@ static int read_mp4a(struct mp4 *f) { int ret; int32_t i; - uint8_t atom_type = 0; - uint8_t header_size = 0; struct mp4_track *t; if (f->total_tracks == 0) @@ -611,20 +582,13 @@ static int read_mp4a(struct mp4 *f) ret = read_int16(f, NULL); if (ret <= 0) return ret; - ret = read_int16(f, &t->sampleRate); - if (ret <= 0) - return ret; - ret = read_int16(f, NULL); - if (ret <= 0) - return ret; - return atom_read_header(f, &atom_type, &header_size, NULL); + return read_int16(f, &t->sampleRate); } static int read_stsd(struct mp4 *f) { int ret; uint32_t i, entry_count; - uint8_t header_size = 0; struct mp4_track *t; if (f->total_tracks == 0) @@ -643,51 +607,19 @@ static int read_stsd(struct mp4 *f) uint64_t skip = get_position(f); uint64_t size; uint8_t atom_type = 0; - ret = atom_read_header(f, &atom_type, &header_size, &size); + ret = atom_read_header(f, &atom_type, NULL, &size); if (ret <= 0) return ret; skip += size; - t->is_audio = atom_type == ATOM_MP4A; - if (t->is_audio) + if (!f->audio_track && atom_type == ATOM_MP4A) { + f->audio_track = t; read_mp4a(f); + } set_position(f, skip); } return 1; } -static int32_t tag_add_field(struct mp4_metadata *meta, const char *item, - const char *value, int32_t len) -{ - meta->tags = para_realloc(meta->tags, - (meta->count + 1) * sizeof(struct mp4_tag)); - meta->tags[meta->count].item = para_strdup(item); - meta->tags[meta->count].len = len; - if (len >= 0) { - meta->tags[meta->count].value = para_malloc(len + 1); - memcpy(meta->tags[meta->count].value, value, len); - meta->tags[meta->count].value[len] = 0; - } else { - meta->tags[meta->count].value = para_strdup(value); - } - meta->count++; - return 1; -} - -static int read_string(struct mp4 *f, uint32_t length, char **result) -{ - char *str = para_malloc(length + 1); - int ret = read_data(f, str, length); - - if (ret <= 0) { - free(str); - *result = NULL; - } else { - str[length] = '\0'; - *result = str; - } - return ret; -} - static const char *get_metadata_name(uint8_t atom_type) { switch (atom_type) { @@ -704,9 +636,10 @@ static int parse_tag(struct mp4 *f, uint8_t parent, int32_t size) { int ret; uint64_t subsize, sumsize; - char *data = NULL; + char *value = NULL; uint32_t len = 0; uint64_t destpos; + struct mp4_tag *tag; for ( sumsize = 0; @@ -717,30 +650,43 @@ static int parse_tag(struct mp4 *f, uint8_t parent, int32_t size) uint8_t header_size = 0; ret = atom_read_header(f, &atom_type, &header_size, &subsize); if (ret <= 0) - return ret; + goto fail; destpos = get_position(f) + subsize - header_size; if (atom_type != ATOM_DATA) continue; ret = read_int8(f, NULL); /* version */ if (ret <= 0) - return ret; + goto fail; ret = read_int24(f, NULL); /* flags */ if (ret <= 0) - return ret; + goto fail; ret = read_int32(f, NULL); /* reserved */ if (ret <= 0) - return ret; - free(data); - ret = read_string(f, subsize - (header_size + 8), &data); - if (ret <= 0) - return ret; + goto fail; + ret = -ERRNO_TO_PARA_ERROR(EINVAL); + if (subsize < header_size + 8 || subsize > UINT_MAX) + goto fail; len = subsize - (header_size + 8); + free(value); + value = para_malloc(len + 1); + ret = read_data(f, value, len); + if (ret <= 0) + goto fail; + value[len] = '\0'; } - if (!data) - return -1; - tag_add_field(&f->meta, get_metadata_name(parent), data, len); - free(data); + if (!value) + return -ERRNO_TO_PARA_ERROR(EINVAL); + f->meta.tags = para_realloc(f->meta.tags, (f->meta.count + 1) + * sizeof(struct mp4_tag)); + tag = f->meta.tags + f->meta.count; + tag->item = para_strdup(get_metadata_name(parent)); + tag->value = value; + tag->len = len; + f->meta.count++; return 1; +fail: + free(value); + return ret; } static int read_mdhd(struct mp4 *f) @@ -815,7 +761,9 @@ static int32_t read_ilst(struct mp4 *f, int32_t size) case ATOM_ALBUM: case ATOM_COMMENT: case ATOM_DATE: - parse_tag(f, atom_type, subsize - header_size); + ret = parse_tag(f, atom_type, subsize - header_size); + if (ret <= 0) + return ret; } set_position(f, destpos); sumsize += subsize; @@ -842,16 +790,18 @@ static int32_t read_meta(struct mp4 *f, uint64_t size) return ret; if (subsize <= header_size + 4) return 1; - if (atom_type == ATOM_ILST) - read_ilst(f, subsize - (header_size + 4)); - else + if (atom_type == ATOM_ILST) { + ret = read_ilst(f, subsize - (header_size + 4)); + if (ret <= 0) + return ret; + } else set_position(f, get_position(f) + subsize - header_size); sumsize += subsize; } return 1; } -static int atom_read(struct mp4 *f, uint64_t size, uint8_t atom_type) +static int parse_leaf_atom(struct mp4 *f, uint64_t size, uint8_t atom_type) { uint64_t dest_position = get_position(f) + size - 8; int ret = 1; /* return success for atoms we don't care about */ @@ -869,8 +819,36 @@ static int atom_read(struct mp4 *f, uint64_t size, uint8_t atom_type) return ret; } +static bool need_atom(uint8_t atom_type, bool meta_only) +{ + /* these are needed in any case */ + switch (atom_type) { + case ATOM_STSD: + case ATOM_META: + case ATOM_TRAK: + case ATOM_MDIA: + case ATOM_MINF: + case ATOM_STBL: + return true; + } + /* meta-only opens don't need anything else */ + if (meta_only) + return false; + /* these are only required for regular opens */ + switch (atom_type) { + case ATOM_STTS: + case ATOM_STSZ: + case ATOM_STCO: + case ATOM_STSC: + case ATOM_MDHD: + case ATOM_UDTA: + return true; + } + return false; +} + /* parse atoms that are sub atoms of other atoms */ -static int parse_sub_atoms(struct mp4 *f, uint64_t total_size, int meta_only) +static int parse_sub_atoms(struct mp4 *f, uint64_t total_size, bool meta_only) { int ret; uint64_t size; @@ -892,24 +870,21 @@ static int parse_sub_atoms(struct mp4 *f, uint64_t total_size, int meta_only) f->track[f->total_tracks - 1] = para_calloc( sizeof(struct mp4_track)); } - /* parse subatoms */ - if (meta_only && !need_parse_when_meta_only(atom_type)) { + if (!need_atom(atom_type, meta_only)) { set_position(f, get_position(f) + size - header_size); - } else if (atom_type < SUBATOMIC) { - ret = parse_sub_atoms(f, size - header_size, meta_only); - if (ret <= 0) - return ret; - } else { - ret = atom_read(f, size, atom_type); - if (ret <= 0) - return ret; + continue; } + if (atom_type < SUBATOMIC) /* atom contains subatoms */ + ret = parse_sub_atoms(f, size - header_size, meta_only); + else + ret = parse_leaf_atom(f, size, atom_type); + if (ret <= 0) + return ret; } return 1; } -/* parse root atoms */ -static int parse_atoms(struct mp4 *f, int meta_only) +static int parse_root_atoms(struct mp4 *f, bool meta_only) { int ret; uint64_t size; @@ -921,24 +896,20 @@ static int parse_atoms(struct mp4 *f, int meta_only) while ((ret = atom_read_header(f, &atom_type, &header_size, &size)) > 0) { f->file_size += size; f->last_atom = atom_type; - - if (atom_type == ATOM_MOOV && size > header_size) { - f->moov_offset = get_position(f) - header_size; - f->moov_size = size; - } - - /* parse subatoms */ - if (meta_only && !need_parse_when_meta_only(atom_type)) { - set_position(f, get_position(f) + size - header_size); - } else if (atom_type < SUBATOMIC) { - ret = parse_sub_atoms(f, size - header_size, meta_only); - if (ret <= 0) - return ret; - } else { - /* skip this atom */ + if (atom_type != ATOM_MOOV || size <= header_size) { /* skip */ set_position(f, get_position(f) + size - header_size); + continue; } + f->moov_offset = get_position(f) - header_size; + f->moov_size = size; + ret = parse_sub_atoms(f, size - header_size, meta_only); + if (ret <= 0) + break; } + if (ret < 0) + return ret; + if (!f->audio_track) + return -E_MP4_TRACK; return ret; } @@ -948,7 +919,7 @@ struct mp4 *mp4_open_read(const struct mp4_callback *cb) struct mp4 *f = para_calloc(sizeof(struct mp4)); f->cb = cb; - ret = parse_atoms(f, 0); + ret = parse_root_atoms(f, false); if (ret < 0) { free(f); return NULL; @@ -964,10 +935,8 @@ void mp4_close(struct mp4 *f) if (f->track[i]) { free(f->track[i]->stsz_table); free(f->track[i]->stts_sample_count); - free(f->track[i]->stts_sample_delta); free(f->track[i]->stsc_first_chunk); free(f->track[i]->stsc_samples_per_chunk); - free(f->track[i]->stsc_sample_desc_index); free(f->track[i]->stco_chunk_offset); free(f->track[i]); } @@ -980,10 +949,10 @@ void mp4_close(struct mp4 *f) free(f); } -static int32_t chunk_of_sample(const struct mp4 *f, int32_t track, - int32_t sample, int32_t *chunk) +static int32_t chunk_of_sample(const struct mp4 *f, int32_t sample, + int32_t *chunk) { - const struct mp4_track *t = f->track[track]; + const struct mp4_track *t = f->audio_track; uint32_t *fc = t->stsc_first_chunk, *spc = t->stsc_samples_per_chunk; int32_t chunk1, chunk1samples, n, total, i; @@ -1002,51 +971,28 @@ static int32_t chunk_of_sample(const struct mp4 *f, int32_t track, } /** - * Return the number of milliseconds of the given track. + * Return the number of milliseconds of the audio track. * * \param f As returned by \ref mp4_open_read(), must not be NULL. - * \param track Between zero and the value returned by \ref mp4_get_total_tracks(). - * - * The function returns zero if the audio file is of zero length or contains a - * corrupt track header. */ -uint64_t mp4_get_duration(const struct mp4 *f, int32_t track) +uint64_t mp4_get_duration(const struct mp4 *f) { - const struct mp4_track *t = f->track[track]; + const struct mp4_track *t = f->audio_track; if (t->timeScale == 0) return 0; return t->duration * 1000 / t->timeScale; } -int32_t mp4_get_total_tracks(const struct mp4 *f) -{ - return f->total_tracks; -} - -/** - * Check whether the given track number corresponds to an audio track. - * - * \param f See \ref mp4_get_duration(). - * \param track See \ref mp4_get_duration(). - * - * Besides audio tracks, an mp4 file may contain video and system tracks. For - * those the function returns false. - */ -bool mp4_is_audio_track(const struct mp4 *f, int32_t track) -{ - return f->track[track]->is_audio; -} - -int mp4_set_sample_position(struct mp4 *f, uint32_t track, int32_t sample) +int mp4_set_sample_position(struct mp4 *f, int32_t sample) { - const struct mp4_track *t = f->track[track]; + const struct mp4_track *t = f->audio_track; int32_t offset, chunk, chunk_sample; uint32_t n, srs; /* sample range size */ - if (sample >= t->stsz_sample_count || track >= f->total_tracks) + if (sample >= t->stsz_sample_count) return -ERRNO_TO_PARA_ERROR(EINVAL); - chunk_sample = chunk_of_sample(f, track, sample, &chunk); + chunk_sample = chunk_of_sample(f, sample, &chunk); if (t->stsz_sample_size > 0) srs = (sample - chunk_sample) * t->stsz_sample_size; else { @@ -1063,33 +1009,33 @@ int mp4_set_sample_position(struct mp4 *f, uint32_t track, int32_t sample) return 1; } -int32_t mp4_get_sample_size(const struct mp4 *f, int track, int sample) +int32_t mp4_get_sample_size(const struct mp4 *f, int sample) { - const struct mp4_track *t = f->track[track]; + const struct mp4_track *t = f->audio_track; if (t->stsz_sample_size != 0) return t->stsz_sample_size; return t->stsz_table[sample]; } -uint32_t mp4_get_sample_rate(const struct mp4 *f, int32_t track) +uint32_t mp4_get_sample_rate(const struct mp4 *f) { - return f->track[track]->sampleRate; + return f->audio_track->sampleRate; } -uint32_t mp4_get_channel_count(const struct mp4 *f, int32_t track) +uint32_t mp4_get_channel_count(const struct mp4 *f) { - return f->track[track]->channelCount; + return f->audio_track->channelCount ; } -int32_t mp4_num_samples(const struct mp4 *f, int32_t track) +int32_t mp4_num_samples(const struct mp4 *f) { + const struct mp4_track *t = f->audio_track; int32_t i; int32_t total = 0; - for (i = 0; i < f->track[track]->stts_entry_count; i++) { - total += f->track[track]->stts_sample_count[i]; - } + for (i = 0; i < t->stts_entry_count; i++) + total += t->stts_sample_count[i]; return total; } @@ -1099,7 +1045,7 @@ struct mp4 *mp4_open_meta(const struct mp4_callback *cb) struct mp4 *f = para_calloc(sizeof(struct mp4)); f->cb = cb; - ret = parse_atoms(f, 1); + ret = parse_root_atoms(f, true); if (ret < 0) { free(f); return NULL; @@ -1271,44 +1217,30 @@ static void *membuffer_detach(struct membuffer *buf) return ret; } -struct stdmeta_entry { - const char *atom; - const char *name; -}; - -static const char *find_standard_meta(const char *name) -{ - const struct stdmeta_entry stdmetas[] = { - {"\xA9" "nam", "title"}, - {"\xA9" "ART", "artist"}, - {"\xA9" "alb", "album"}, - {"\xA9" "day", "date"}, - {"\xA9" "cmt", "comment"}, - }; - - for (unsigned n = 0; n < ARRAY_SIZE(stdmetas); n++) - if (!strcasecmp(name, stdmetas[n].name)) - return stdmetas[n].atom; - return NULL; -} - -static uint32_t create_ilst(const struct mp4_metadata *meta, void **out_buffer, - uint32_t * out_size) +static void *create_ilst(const struct mp4_metadata *meta, uint32_t *out_size) { struct membuffer *buf = membuffer_create(); - unsigned metaptr; - - for (metaptr = 0; metaptr < meta->count; metaptr++) { - struct mp4_tag *tag = meta->tags + metaptr; - const char *std_meta_atom = find_standard_meta(tag->item); - if (std_meta_atom) - membuffer_write_std_tag(buf, std_meta_atom, tag->value); + unsigned n; + + for (n = 0; n < meta->count; n++) { + struct mp4_tag *tag = meta->tags + n; + const char *atom_name; + if (!strcasecmp(tag->item, "title")) + atom_name = "\xA9" "nam"; + else if (!strcasecmp(tag->item, "artist")) + atom_name = "\xA9" "ART"; + else if (!strcasecmp(tag->item, "album")) + atom_name = "\xA9" "alb"; + else if (!strcasecmp(tag->item, "date")) + atom_name = "\xA9" "day"; + else if (!strcasecmp(tag->item, "comment")) + atom_name = "\xA9" "cmt"; else - PARA_ERROR_LOG("invalid tag item: %s\n", tag->item); + assert(false); + membuffer_write_std_tag(buf, atom_name, tag->value); } *out_size = membuffer_get_size(buf); - *out_buffer = membuffer_detach(buf); - return 1; + return membuffer_detach(buf); } static void membuffer_write_atom(struct membuffer *buf, const char *name, unsigned size, @@ -1347,11 +1279,9 @@ static uint32_t create_meta(const struct mp4_metadata *meta, void **out_buffer, uint32_t ilst_size; void *ilst_buffer; - if (!create_ilst(meta, &ilst_buffer, &ilst_size)) - return 0; + ilst_buffer = create_ilst(meta, &ilst_size); buf = membuffer_create(); - membuffer_write_int32(buf, 0); membuffer_write_atom(buf, "ilst", ilst_size, ilst_buffer); free(ilst_buffer); @@ -1471,8 +1401,7 @@ static void *modify_moov(struct mp4 *f, uint32_t *out_size) ret = read_int32(f, &ilst_size); if (ret <= 0) return NULL; - if (!create_ilst(&f->meta, &new_ilst_buffer, &new_ilst_size)) - return NULL; + new_ilst_buffer = create_ilst(&f->meta, &new_ilst_size); size_delta = new_ilst_size - (ilst_size - 8); *out_size = total_size + size_delta; out_buffer = para_malloc(*out_size);