[FFmpeg-cvslog] avformat/iamf_writer, iamfenc: Avoid allocations when using dyn buffers

Andreas Rheinhardt git at videolan.org
Sat Feb 3 01:38:24 EET 2024


ffmpeg | branch: master | Andreas Rheinhardt <andreas.rheinhardt at outlook.com> | Wed Jan 31 09:21:55 2024 +0100| [8d4b22edc7b5b2cab2d42f3072aaa6b0244d581d] | committer: Andreas Rheinhardt

avformat/iamf_writer, iamfenc: Avoid allocations when using dyn buffers

Use avio_get_dyn_buf()+ffio_free_dyn_buf() instead of
avio_close_dyn_buf()+av_free(). This saves an allocation
(and memcpy) in case all the data fits in the AVIOContext's
write buffer.

Reviewed-by: James Almer <jamrial at gmail.com>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=8d4b22edc7b5b2cab2d42f3072aaa6b0244d581d
---

 libavformat/iamf_writer.c | 12 ++++++------
 libavformat/iamfenc.c     |  8 ++++----
 2 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/libavformat/iamf_writer.c b/libavformat/iamf_writer.c
index f665f45b7a..5850c53c8e 100644
--- a/libavformat/iamf_writer.c
+++ b/libavformat/iamf_writer.c
@@ -453,11 +453,11 @@ static int iamf_write_codec_config(const IAMFContext *iamf,
     put_bits(&pbc, 3, 0);
     flush_put_bits(&pbc);
 
-    dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
+    dyn_size = avio_get_dyn_buf(dyn_bc, &dyn_buf);
     avio_write(pb, header, put_bytes_count(&pbc, 1));
     ffio_write_leb(pb, dyn_size);
     avio_write(pb, dyn_buf, dyn_size);
-    av_free(dyn_buf);
+    ffio_free_dyn_buf(&dyn_bc);
 
     return 0;
 }
@@ -660,11 +660,11 @@ static int iamf_write_audio_element(const IAMFContext *iamf,
     put_bits(&pbc, 3, 0);
     flush_put_bits(&pbc);
 
-    dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
+    dyn_size = avio_get_dyn_buf(dyn_bc, &dyn_buf);
     avio_write(pb, header, put_bytes_count(&pbc, 1));
     ffio_write_leb(pb, dyn_size);
     avio_write(pb, dyn_buf, dyn_size);
-    av_free(dyn_buf);
+    ffio_free_dyn_buf(&dyn_bc);
 
     return 0;
 }
@@ -796,11 +796,11 @@ static int iamf_write_mixing_presentation(const IAMFContext *iamf,
     put_bits(&pbc, 3, 0);
     flush_put_bits(&pbc);
 
-    dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
+    dyn_size = avio_get_dyn_buf(dyn_bc, &dyn_buf);
     avio_write(pb, header, put_bytes_count(&pbc, 1));
     ffio_write_leb(pb, dyn_size);
     avio_write(pb, dyn_buf, dyn_size);
-    av_free(dyn_buf);
+    ffio_free_dyn_buf(&dyn_bc);
 
     return 0;
 }
diff --git a/libavformat/iamfenc.c b/libavformat/iamfenc.c
index a02f84035a..e1003d5495 100644
--- a/libavformat/iamfenc.c
+++ b/libavformat/iamfenc.c
@@ -257,10 +257,10 @@ static int write_parameter_block(AVFormatContext *s, const AVIAMFParamDefinition
         }
     }
 
-    dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
+    dyn_size = avio_get_dyn_buf(dyn_bc, &dyn_buf);
     ffio_write_leb(s->pb, dyn_size);
     avio_write(s->pb, dyn_buf, dyn_size);
-    av_free(dyn_buf);
+    ffio_free_dyn_buf(&dyn_bc);
 
     return 0;
 }
@@ -340,10 +340,10 @@ static int iamf_write_packet(AVFormatContext *s, AVPacket *pkt)
     if (st->id > 17)
         ffio_write_leb(dyn_bc, st->id);
 
-    dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
+    dyn_size = avio_get_dyn_buf(dyn_bc, &dyn_buf);
     ffio_write_leb(s->pb, dyn_size + pkt->size);
     avio_write(s->pb, dyn_buf, dyn_size);
-    av_free(dyn_buf);
+    ffio_free_dyn_buf(&dyn_bc);
     avio_write(s->pb, pkt->data, pkt->size);
 
     return 0;



More information about the ffmpeg-cvslog mailing list