mp4: Get rid of find_standard_meta().
authorAndre Noll <maan@tuebingen.mpg.de>
Sat, 21 Aug 2021 11:30:02 +0000 (13:30 +0200)
committerAndre Noll <maan@tuebingen.mpg.de>
Mon, 30 May 2022 19:37:35 +0000 (21:37 +0200)
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

diff --git a/mp4.c b/mp4.c
index 02dd5a190ff519179b5b94aabe7ae66bcf55cb91..92be4c766724b471c7d31036b41b3408f3e36406 100644 (file)
--- 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);