Re: [FFmpeg-devel] [PATCH 02/10] avformat/nutenc: Reuse dynamic buffers when possible

2020-05-05 Thread Michael Niedermayer
On Mon, May 04, 2020 at 08:22:42PM +0200, Andreas Rheinhardt wrote:
> NUT uses variable-length integers in order to for length fields.
> Therefore the NUT muxer often writes data into a dynamic buffer in order
> to get the length of it, then writes the length field using the fewest
> amount of bytes needed. To do this, a new dynamic buffer was opened,
> used and freed for each element which involves lots of allocations. This
> commit changes this: The dynamic buffers are now resetted and reused.
> 
> Signed-off-by: Andreas Rheinhardt 
> ---
>  libavformat/nutenc.c | 24 
>  1 file changed, 8 insertions(+), 16 deletions(-)

probably ok

thx

[...]
-- 
Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

I do not agree with what you have to say, but I'll defend to the death your
right to say it. -- Voltaire


signature.asc
Description: PGP signature
___
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".

[FFmpeg-devel] [PATCH 02/10] avformat/nutenc: Reuse dynamic buffers when possible

2020-05-04 Thread Andreas Rheinhardt
NUT uses variable-length integers in order to for length fields.
Therefore the NUT muxer often writes data into a dynamic buffer in order
to get the length of it, then writes the length field using the fewest
amount of bytes needed. To do this, a new dynamic buffer was opened,
used and freed for each element which involves lots of allocations. This
commit changes this: The dynamic buffers are now resetted and reused.

Signed-off-by: Andreas Rheinhardt 
---
 libavformat/nutenc.c | 24 
 1 file changed, 8 insertions(+), 16 deletions(-)

diff --git a/libavformat/nutenc.c b/libavformat/nutenc.c
index 44ee5d810a..6125429cc3 100644
--- a/libavformat/nutenc.c
+++ b/libavformat/nutenc.c
@@ -303,7 +303,7 @@ static void put_packet(NUTContext *nut, AVIOContext *bc, 
AVIOContext *dyn_bc,
uint64_t startcode)
 {
 uint8_t *dyn_buf = NULL;
-int dyn_size = avio_close_dyn_buf(dyn_bc, _buf);
+int dyn_size = avio_get_dyn_buf(dyn_bc, _buf);
 int forw_ptr = dyn_size + 4;
 
 if (forw_ptr > 4096)
@@ -317,7 +317,7 @@ static void put_packet(NUTContext *nut, AVIOContext *bc, 
AVIOContext *dyn_bc,
 avio_write(bc, dyn_buf, dyn_size);
 avio_wl32(bc, ffio_get_checksum(bc));
 
-av_free(dyn_buf);
+ffio_reset_dyn_buf(dyn_bc);
 }
 
 static void write_mainheader(NUTContext *nut, AVIOContext *bc)
@@ -630,9 +630,6 @@ static int write_headers(AVFormatContext *avctx, 
AVIOContext *bc)
 put_packet(nut, bc, dyn_bc, MAIN_STARTCODE);
 
 for (i = 0; i < nut->avf->nb_streams; i++) {
-ret = avio_open_dyn_buf(_bc);
-if (ret < 0)
-return ret;
 ret = write_streamheader(avctx, dyn_bc, nut->avf->streams[i], i);
 if (ret < 0) {
 ffio_free_dyn_buf(_bc);
@@ -641,30 +638,20 @@ static int write_headers(AVFormatContext *avctx, 
AVIOContext *bc)
 put_packet(nut, bc, dyn_bc, STREAM_STARTCODE);
 }
 
-ret = avio_open_dyn_buf(_bc);
-if (ret < 0)
-return ret;
 write_globalinfo(nut, dyn_bc);
 put_packet(nut, bc, dyn_bc, INFO_STARTCODE);
 
 for (i = 0; i < nut->avf->nb_streams; i++) {
-ret = avio_open_dyn_buf(_bc);
-if (ret < 0)
-return ret;
 ret = write_streaminfo(nut, dyn_bc, i);
 if (ret > 0)
 put_packet(nut, bc, dyn_bc, INFO_STARTCODE);
-else {
+else if (ret < 0) {
 ffio_free_dyn_buf(_bc);
-if (ret < 0)
 return ret;
 }
 }
 
 for (i = 0; i < nut->avf->nb_chapters; i++) {
-ret = avio_open_dyn_buf(_bc);
-if (ret < 0)
-return ret;
 ret = write_chapter(nut, dyn_bc, i);
 if (ret < 0) {
 ffio_free_dyn_buf(_bc);
@@ -675,6 +662,9 @@ static int write_headers(AVFormatContext *avctx, 
AVIOContext *bc)
 
 nut->last_syncpoint_pos = INT_MIN;
 nut->header_count++;
+
+ffio_free_dyn_buf(_bc);
+
 return 0;
 }
 
@@ -1020,6 +1010,7 @@ static int nut_write_packet(AVFormatContext *s, AVPacket 
*pkt)
av_rescale_q(av_gettime(), AV_TIME_BASE_Q, 
*nus->time_base));
 }
 put_packet(nut, bc, dyn_bc, SYNCPOINT_STARTCODE);
+ffio_free_dyn_buf(_bc);
 
 if (nut->write_index) {
 if ((ret = ff_nut_add_sp(nut, nut->last_syncpoint_pos, 0 /*unused*/, 
pkt->dts)) < 0)
@@ -1173,6 +1164,7 @@ static int nut_write_trailer(AVFormatContext *s)
 av_assert1(nut->write_index); // sp_count should be 0 if no index is 
going to be written
 write_index(nut, dyn_bc);
 put_packet(nut, bc, dyn_bc, INDEX_STARTCODE);
+ffio_free_dyn_buf(_bc);
 }
 
 return 0;
-- 
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".