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);
{
int ret;
int32_t i;
- uint8_t atom_type = 0;
- uint8_t header_size = 0;
struct mp4_track *t;
if (f->total_tracks == 0)
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)
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;
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) {
{
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;
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)
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;
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 */
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;
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;
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)
- break;
- } 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;
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;
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;