By using avio_get_dyn_buf() + ffio_free_dyn_buf() instead of
avio_close_dyn_buf() + av_free() one can avoid an allocation + copy for
small tags. Furthermore, it simplifies freeing.

Signed-off-by: Andreas Rheinhardt <andreas.rheinha...@gmail.com>
---
 libavformat/id3v2enc.c | 37 ++++++++++++++++---------------------
 1 file changed, 16 insertions(+), 21 deletions(-)

diff --git a/libavformat/id3v2enc.c b/libavformat/id3v2enc.c
index c13b93a7d8..9040501869 100644
--- a/libavformat/id3v2enc.c
+++ b/libavformat/id3v2enc.c
@@ -81,7 +81,7 @@ static int id3v2_put_ttag(ID3v2EncContext *id3, AVIOContext 
*avioc, const char *
     id3v2_encode_string(dyn_buf, str1, enc);
     if (str2)
         id3v2_encode_string(dyn_buf, str2, enc);
-    len = avio_close_dyn_buf(dyn_buf, &pb);
+    len = avio_get_dyn_buf(dyn_buf, &pb);
 
     avio_wb32(avioc, tag);
     /* ID3v2.3 frame size is not sync-safe */
@@ -92,7 +92,7 @@ static int id3v2_put_ttag(ID3v2EncContext *id3, AVIOContext 
*avioc, const char *
     avio_wb16(avioc, 0);
     avio_write(avioc, pb, len);
 
-    av_freep(&pb);
+    ffio_free_dyn_buf(&dyn_buf);
     return len + ID3v2_HEADER_SIZE;
 }
 
@@ -134,7 +134,7 @@ static int id3v2_put_priv(ID3v2EncContext *id3, AVIOContext 
*avioc, const char *
         }
     }
 
-    len = avio_close_dyn_buf(dyn_buf, &pb);
+    len = avio_get_dyn_buf(dyn_buf, &pb);
 
     avio_wb32(avioc, MKBETAG('P', 'R', 'I', 'V'));
     if (id3->version == 3)
@@ -144,7 +144,7 @@ static int id3v2_put_priv(ID3v2EncContext *id3, AVIOContext 
*avioc, const char *
     avio_wb16(avioc, 0);
     avio_write(avioc, pb, len);
 
-    av_free(pb);
+    ffio_free_dyn_buf(&dyn_buf);
 
     return len + ID3v2_HEADER_SIZE;
 }
@@ -257,8 +257,8 @@ static int write_metadata(AVIOContext *pb, AVDictionary 
**metadata,
 
 static int write_ctoc(AVFormatContext *s, ID3v2EncContext *id3, int enc)
 {
-    uint8_t *dyn_buf = NULL;
-    AVIOContext *dyn_bc = NULL;
+    uint8_t *dyn_buf;
+    AVIOContext *dyn_bc;
     char name[123];
     int len, ret;
 
@@ -266,7 +266,7 @@ static int write_ctoc(AVFormatContext *s, ID3v2EncContext 
*id3, int enc)
         return 0;
 
     if ((ret = avio_open_dyn_buf(&dyn_bc)) < 0)
-        goto fail;
+        return ret;
 
     id3->len += avio_put_str(dyn_bc, "toc");
     avio_w8(dyn_bc, 0x03);
@@ -275,7 +275,7 @@ static int write_ctoc(AVFormatContext *s, ID3v2EncContext 
*id3, int enc)
         snprintf(name, 122, "ch%d", i);
         id3->len += avio_put_str(dyn_bc, name);
     }
-    len = avio_close_dyn_buf(dyn_bc, &dyn_buf);
+    len = avio_get_dyn_buf(dyn_bc, &dyn_buf);
     id3->len += 16 + ID3v2_HEADER_SIZE;
 
     avio_wb32(s->pb, MKBETAG('C', 'T', 'O', 'C'));
@@ -283,10 +283,7 @@ static int write_ctoc(AVFormatContext *s, ID3v2EncContext 
*id3, int enc)
     avio_wb16(s->pb, 0);
     avio_write(s->pb, dyn_buf, len);
 
-fail:
-    if (dyn_bc && !dyn_buf)
-        avio_close_dyn_buf(dyn_bc, &dyn_buf);
-    av_freep(&dyn_buf);
+    ffio_free_dyn_buf(&dyn_bc);
 
     return ret;
 }
@@ -295,13 +292,13 @@ static int write_chapter(AVFormatContext *s, 
ID3v2EncContext *id3, int id, int e
 {
     const AVRational time_base = {1, 1000};
     AVChapter *ch = s->chapters[id];
-    uint8_t *dyn_buf = NULL;
-    AVIOContext *dyn_bc = NULL;
+    uint8_t *dyn_buf;
+    AVIOContext *dyn_bc;
     char name[123];
     int len, start, end, ret;
 
     if ((ret = avio_open_dyn_buf(&dyn_bc)) < 0)
-        goto fail;
+        return ret;
 
     start = av_rescale_q(ch->start, ch->time_base, time_base);
     end   = av_rescale_q(ch->end,   ch->time_base, time_base);
@@ -316,7 +313,7 @@ static int write_chapter(AVFormatContext *s, 
ID3v2EncContext *id3, int id, int e
     if ((ret = write_metadata(dyn_bc, &ch->metadata, id3, enc)) < 0)
         goto fail;
 
-    len = avio_close_dyn_buf(dyn_bc, &dyn_buf);
+    len = avio_get_dyn_buf(dyn_bc, &dyn_buf);
     id3->len += 16 + ID3v2_HEADER_SIZE;
 
     avio_wb32(s->pb, MKBETAG('C', 'H', 'A', 'P'));
@@ -325,9 +322,7 @@ static int write_chapter(AVFormatContext *s, 
ID3v2EncContext *id3, int id, int e
     avio_write(s->pb, dyn_buf, len);
 
 fail:
-    if (dyn_bc && !dyn_buf)
-        avio_close_dyn_buf(dyn_bc, &dyn_buf);
-    av_freep(&dyn_buf);
+    ffio_free_dyn_buf(&dyn_bc);
 
     return ret;
 }
@@ -406,7 +401,7 @@ int ff_id3v2_write_apic(AVFormatContext *s, ID3v2EncContext 
*id3, AVPacket *pkt)
     avio_w8(dyn_buf, type);
     id3v2_encode_string(dyn_buf, desc, enc);
     avio_write(dyn_buf, pkt->data, pkt->size);
-    len = avio_close_dyn_buf(dyn_buf, &buf);
+    len = avio_get_dyn_buf(dyn_buf, &buf);
 
     avio_wb32(s->pb, MKBETAG('A', 'P', 'I', 'C'));
     if (id3->version == 3)
@@ -415,7 +410,7 @@ int ff_id3v2_write_apic(AVFormatContext *s, ID3v2EncContext 
*id3, AVPacket *pkt)
         id3v2_put_size(s->pb, len);
     avio_wb16(s->pb, 0);
     avio_write(s->pb, buf, len);
-    av_freep(&buf);
+    ffio_free_dyn_buf(&dyn_buf);
 
     id3->len += len + ID3v2_HEADER_SIZE;
 
-- 
2.20.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

Reply via email to