From 70941e3cfbdb5af190d76dc8aabb6d57b86638ae Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Sat, 21 Aug 2021 13:30:02 +0200 Subject: [PATCH] mp4: Get rid of find_standard_meta(). We don't need a dedicated function and data structure for that. Just open-code the logic in create_ilst() and clean up this function a bit while at it. Specifically: * Call the loop variable "n" rather than "metaptr" since it is not a pointer but an unsigned integer. * Abort if we encounter a tag item name which is not one of the five standard names. This can never occur because the origin of these strings is the code in aac_afh.c which only passes standard names. * Drop the integer return value, since the function can never fail. Make it return the buffer pointer instead and get rid of the corresponding parameter. --- mp4.c | 59 +++++++++++++++++++++-------------------------------------- 1 file changed, 21 insertions(+), 38 deletions(-) diff --git a/mp4.c b/mp4.c index 02dd5a19..92be4c76 100644 --- a/mp4.c +++ b/mp4.c @@ -1217,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, @@ -1293,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); @@ -1417,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); -- 2.39.2