From c601bacb7f147a14bcc8200b24aa69201cb6b89e Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Sat, 14 Aug 2021 18:42:16 +0200 Subject: [PATCH] mp4: Introduce mp4_get_meta(). Simplify the API by returning a pointer to the metadata structure stored inside struct mp4. This is easier than having two accessor functions. --- aac_afh.c | 37 +++++++++++++------------------------ error.h | 1 - mp4.c | 30 +++++++++++------------------- mp4.h | 4 +--- 4 files changed, 25 insertions(+), 47 deletions(-) diff --git a/aac_afh.c b/aac_afh.c index 84aeaa85..1522b5f0 100644 --- a/aac_afh.c +++ b/aac_afh.c @@ -221,8 +221,7 @@ static int aac_afh_rewrite_tags(const char *map, size_t mapsize, struct taginfo *tags, int fd, __a_unused const char *filename) { int ret, i; - int32_t rv; - struct mp4_metadata metadata; + struct mp4_metadata *metadata; struct mp4 *mp4; struct mp4_callback cb = { .read = aac_afh_meta_read_cb, @@ -242,22 +241,12 @@ static int aac_afh_rewrite_tags(const char *map, size_t mapsize, mp4 = mp4_open_meta(&cb); if (!mp4) return -E_MP4_OPEN; - - ret = -E_MP4_META_READ; - rv = mp4_meta_get_num_items(mp4); - if (rv < 0) - goto close; - metadata.count = rv; - PARA_NOTICE_LOG("%d metadata item(s) found\n", rv); - - metadata.tags = para_malloc((metadata.count + 5) * sizeof(struct mp4_tag)); - for (i = 0; i < metadata.count; i++) { - struct mp4_tag *tag = metadata.tags + i; - - ret = -E_MP4_META_READ; - if (!mp4_meta_get_by_index(mp4, i, &tag->item, &tag->value)) - goto close; - PARA_INFO_LOG("found: %s: %s\n", tag->item, tag->value); + metadata = mp4_get_meta(mp4); + PARA_NOTICE_LOG("%u metadata item(s) found\n", metadata->count); + metadata->tags = para_realloc(metadata->tags, + (metadata->count + 5) * sizeof(struct mp4_tag)); + for (i = 0; i < metadata->count; i++) { + struct mp4_tag *tag = metadata->tags + i; if (!strcmp(tag->item, "artist")) replace_tag(tag, tags->artist, &found_artist); else if (!strcmp(tag->item, "title")) @@ -270,17 +259,17 @@ static int aac_afh_rewrite_tags(const char *map, size_t mapsize, replace_tag(tag, tags->comment, &found_comment); } if (!found_artist) - add_tag(&metadata, "artist", tags->artist); + add_tag(metadata, "artist", tags->artist); if (!found_title) - add_tag(&metadata, "title", tags->title); + add_tag(metadata, "title", tags->title); if (!found_album) - add_tag(&metadata, "album", tags->album); + add_tag(metadata, "album", tags->album); if (!found_year) - add_tag(&metadata, "date", tags->year); + add_tag(metadata, "date", tags->year); if (!found_comment) - add_tag(&metadata, "comment", tags->comment); + add_tag(metadata, "comment", tags->comment); ret = -E_MP4_META_WRITE; - if (!mp4_meta_update(mp4, &metadata)) + if (!mp4_meta_update(mp4, metadata)) goto close; ret = 1; close: diff --git a/error.h b/error.h index c69a192e..6e10e8eb 100644 --- a/error.h +++ b/error.h @@ -141,7 +141,6 @@ PARA_ERROR(MP4_BAD_SAMPLE, "mp4: invalid sample number"), \ PARA_ERROR(MP4_BAD_SAMPLERATE, "mp4: invalid sample rate"), \ PARA_ERROR(MP4_BAD_SAMPLE_COUNT, "mp4: invalid number of samples"), \ - PARA_ERROR(MP4_META_READ, "mp4: could not read mp4 metadata"), \ PARA_ERROR(MP4_META_WRITE, "mp4: could not update mp4 metadata"), \ PARA_ERROR(MP4_OPEN, "mp4: open failed"), \ PARA_ERROR(MP4_TRACK, "mp4: no audio track"), \ diff --git a/mp4.c b/mp4.c index 37a700ba..49fb22b9 100644 --- a/mp4.c +++ b/mp4.c @@ -1105,23 +1105,17 @@ struct mp4 *mp4_open_meta(const struct mp4_callback *cb) return f; } -int32_t mp4_meta_get_num_items(const struct mp4 *f) +/** + * Return the metadata of an mp4 file. + * + * \param f As returned by either \ref mp4_open_read() or \ref mp4_open_meta(). + * + * The caller is allowed to add, delete or modify the entries of the returned + * structure in order to pass the modified version to \ref mp4_meta_update(). + */ +struct mp4_metadata *mp4_get_meta(struct mp4 *f) { - return f->tags.count; -} - -int32_t mp4_meta_get_by_index(const struct mp4 *f, uint32_t index, - char **item, char **value) -{ - if (index >= f->tags.count) { - *item = NULL; - *value = NULL; - return 0; - } else { - *item = para_strdup(f->tags.tags[index].item); - *value = para_strdup(f->tags.tags[index].value); - return 1; - } + return &f->tags; } static uint32_t find_atom(struct mp4 *f, uint64_t base, uint32_t size, @@ -1619,6 +1613,7 @@ static uint32_t modify_moov(struct mp4 *f, const struct mp4_metadata *data, struct membuffer *buf; void *new_meta_buffer; uint32_t new_meta_size; + if (!create_meta(data, &new_meta_buffer, &new_meta_size)) return 0; @@ -1714,14 +1709,11 @@ int32_t mp4_meta_update(struct mp4 *f, struct mp4_metadata *meta) void *new_moov_data; uint32_t new_moov_size; - tag_delete(&f->tags); - f->tags = *meta; set_position(f, 0); if (!modify_moov(f, meta, &new_moov_data, &new_moov_size)) { mp4_close(f); return 0; } - /* copy moov atom to end of the file */ if (f->last_atom != ATOM_MOOV) { char *free_data = "free"; diff --git a/mp4.h b/mp4.h index 71fd65d9..eb8cf87a 100644 --- a/mp4.h +++ b/mp4.h @@ -31,10 +31,8 @@ int32_t mp4_num_samples(const struct mp4 *f, int track); uint64_t mp4_get_duration(const struct mp4 *f, int32_t track); struct mp4 *mp4_open_meta(const struct mp4_callback *cb); -int mp4_meta_get_by_index(const struct mp4 *f, unsigned int index, - char **item, char **value); +struct mp4_metadata *mp4_get_meta(struct mp4 *f); int32_t mp4_meta_update(struct mp4 *f, struct mp4_metadata *meta); -int mp4_meta_get_num_items(const struct mp4 *f); char *mp4_meta_get_artist(const struct mp4 *f); char *mp4_meta_get_title(const struct mp4 *f); char *mp4_meta_get_date(const struct mp4 *f); -- 2.39.5