[FFmpeg-devel] [PATCH 3/5] mp3enc: write tags as UTF-16

Anton Khirnov anton
Fri Jan 21 14:53:47 CET 2011


This makes supporting both id3 v2.3 and v2.4 simpler, as they both
support UTF-16, whereas UTF-8 is only supported by v2.4.

Also it gets rid of some mysterious magic numbers in code.
---
 libavformat/mp3enc.c |   42 ++++++++++++++++++++++++++----------------
 1 files changed, 26 insertions(+), 16 deletions(-)

diff --git a/libavformat/mp3enc.c b/libavformat/mp3enc.c
index b4fb876..db6e01e 100644
--- a/libavformat/mp3enc.c
+++ b/libavformat/mp3enc.c
@@ -76,14 +76,34 @@ static void id3v2_put_size(AVFormatContext *s, int size)
     put_byte(s->pb, size       & 0x7f);
 }
 
-static void id3v2_put_ttag(AVFormatContext *s, const char *buf, int len,
+/**
+ * Write a text frame with one (normal frames) or two (TXXX frames) strings.
+ * str1 and str2 are written as UTF-16LE with BOM.
+ * @return number of bytes written.
+ */
+static int id3v2_put_ttag(AVFormatContext *s, const char *str1, const char *str2,
                            uint32_t tag)
 {
+    int len;
+    uint8_t *pb;
+    ByteIOContext *dyn_buf;
+    if (url_open_dyn_buf(&dyn_buf) < 0)
+        return 0;
+
+    put_byte(dyn_buf, ID3v2_ENCODING_UTF16BOM);
+    put_le16(dyn_buf, 0xFEFF);      /* BOM */
+    ff_put_strz16le(dyn_buf, str1);
+    if (str2)
+        ff_put_strz16le(dyn_buf, str2);
+    len = url_close_dyn_buf(dyn_buf, &pb);
+
     put_be32(s->pb, tag);
-    id3v2_put_size(s, len + 1);
+    id3v2_put_size(s, len);
     put_be16(s->pb, 0);
-    put_byte(s->pb, 3); /* UTF-8 */
-    put_buffer(s->pb, buf, len);
+    put_buffer(s->pb, pb, len);
+
+    av_freep(&pb);
+    return len + ID3v2_HEADER_SIZE;
 }
 
 
@@ -148,25 +168,15 @@ static int mp3_write_header(struct AVFormatContext *s)
             int i;
             for (i = 0; *ff_id3v2_tags[i]; i++)
                 if (AV_RB32(t->key) == AV_RB32(ff_id3v2_tags[i])) {
-                    int len = strlen(t->value);
                     tag = AV_RB32(t->key);
-                    totlen += len + ID3v2_HEADER_SIZE + 2;
-                    id3v2_put_ttag(s, t->value, len + 1, tag);
+                    totlen += id3v2_put_ttag(s, t->value, NULL, tag);
                     break;
                 }
         }
 
         if (!tag) { /* unknown tag, write as TXXX frame */
-            int   len = strlen(t->key), len1 = strlen(t->value);
-            char *buf = av_malloc(len + len1 + 2);
-            if (!buf)
-                return AVERROR(ENOMEM);
             tag = MKBETAG('T', 'X', 'X', 'X');
-            strcpy(buf,           t->key);
-            strcpy(buf + len + 1, t->value);
-            id3v2_put_ttag(s, buf, len + len1 + 2, tag);
-            totlen += len + len1 + ID3v2_HEADER_SIZE + 3;
-            av_free(buf);
+            totlen += id3v2_put_ttag(s, t->key, t->value, tag);
         }
     }
 
-- 
1.7.2.3




More information about the ffmpeg-devel mailing list