[vlc-devel] [PATCH 2/8] opus comment writing: rework a bit

Rafaël Carré funman at videolan.org
Sat Sep 21 16:54:29 CEST 2013


Move single use constants inside functions instead of passing by parameter
Have comment_init return the allocated comment block
Use size_t when appropriate
Use 'type *name' instead of 'type* name'
Free allocated data outside of functions doing realloc
---
 modules/codec/opus_header.c | 59 +++++++++++++++++++++++----------------------
 1 file changed, 30 insertions(+), 29 deletions(-)

diff --git a/modules/codec/opus_header.c b/modules/codec/opus_header.c
index 20b5547..7d48331 100644
--- a/modules/codec/opus_header.c
+++ b/modules/codec/opus_header.c
@@ -236,43 +236,40 @@ The comment header is decoded as follows:
   7) done.
 */
 
-static int comment_init(char **comments, int* length,
-                        const char *vendor_string)
+static char *comment_init(size_t *length)
 {
     /*The 'vendor' field should be the actual encoding library used.*/
+    const char *vendor_string = opus_get_version_string();
     int vendor_length = strlen(vendor_string);
+
     int user_comment_list_length = 0;
     int len = 8 + 4 + vendor_length + 4;
-    char *p = (char *) malloc(len);
+    char *p = malloc(len);
     if (p == NULL)
-        return 1;
+        return NULL;
 
     memcpy(p, "OpusTags", 8);
     SetDWLE(p + 8, vendor_length);
     memcpy(p + 12, vendor_string, vendor_length);
     SetDWLE(p + 12 + vendor_length, user_comment_list_length);
+
     *length = len;
-    *comments = p;
-    return 0;
+    return p;
 }
 
-static int comment_add(char **comments, int* length, const char *tag,
+static int comment_add(char **comments, size_t *length, const char *tag,
                        const char *val)
 {
     char *p = *comments;
     int vendor_length = GetDWLE(p + 8);
-    int user_comment_list_length = GetDWLE(p + 8 + 4 + vendor_length);
-    int tag_len = (tag ? strlen(tag) : 0);
-    int val_len = strlen(val);
-    int len = (*length) + 4 + tag_len + val_len;
+    size_t user_comment_list_length = GetDWLE(p + 8 + 4 + vendor_length);
+    size_t tag_len = (tag ? strlen(tag) : 0);
+    size_t val_len = strlen(val);
+    size_t len = (*length) + 4 + tag_len + val_len;
 
-    p = (char*) realloc(p, len);
+    p = realloc(p, len);
     if (p == NULL)
-    {
-        free(*comments);
-        *comments = 0;
         return 1;
-    }
 
     SetDWLE(p + *length, tag_len + val_len);          /* length of comment */
     if (tag) memcpy(p + *length + 4, tag, tag_len);         /* comment */
@@ -284,19 +281,18 @@ static int comment_add(char **comments, int* length, const char *tag,
 }
 
 /* adds padding so that metadata can be updated without rewriting the whole file */
-static int comment_pad(char **comments, int* length, int amount)
+static int comment_pad(char **comments, size_t *length)
 {
+    const unsigned padding = 512; /* default from opus-tools */
+
     char *p = *comments;
-    /* Make sure there is at least "amount" worth of padding free, and
+    /* Make sure there is at least "padding" worth of padding free, and
        round up to the maximum that fits in the current ogg segments. */
-    int newlen = (*length + amount + 255) / 255 * 255 - 1;
+    size_t newlen = ((*length + padding) / 255 + 1) * 255 - 1;
     p = realloc(p, newlen);
     if (p == NULL)
-    {
-        free(*comments);
-        *comments = 0;
         return 1;
-    }
+
     memset(p + *length, 0, newlen - *length);
     *comments = p;
     *length = newlen;
@@ -329,18 +325,23 @@ int opus_write_header(uint8_t **p_extra, int *i_extra, OpusHeader *header)
     headers[0].e_o_s = 0;
     headers[0].granulepos = 0;
     headers[0].packetno = 0;
-    const char *opus_version = opus_get_version_string();
-    int comments_length;
-    char *comments;
-    if (comment_init(&comments, &comments_length, opus_version))
+
+    size_t comments_length;
+    char *comments = comment_init(&comments_length);
+    if (!comments)
         return 1;
     if (comment_add(&comments, &comments_length, "ENCODER=",
                     "VLC media player"))
+    {
+        free(comments);
         return 1;
+    }
 
-    const unsigned comment_padding = 512; /* default from opus-tools */
-    if (comment_pad(&comments, &comments_length, comment_padding))
+    if (comment_pad(&comments, &comments_length))
+    {
+        free(comments);
         return 1;
+    }
 
     headers[1].packet = (unsigned char *) comments;
     headers[1].bytes = comments_length;
-- 
1.8.1.2




More information about the vlc-devel mailing list