diff mbox series

[FFmpeg-devel,2/3] avformat/ttaenc: Defer freeing dynamic buffer

Message ID 20200521023647.27847-2-andreas.rheinhardt@gmail.com
State Accepted
Commit dbacecd347599aa421be94ad5e16521aa51f7014
Headers show
Series [FFmpeg-devel,1/3] avformat/matroskadec: Beautify matroska_parse_laces()
Related show

Checks

Context Check Description
andriy/default pending
andriy/make success Make finished
andriy/make_fate success Make fate finished

Commit Message

Andreas Rheinhardt May 21, 2020, 2:36 a.m. UTC
The TTA muxer writes a seektable in a dynamic buffer as it receives
packets and when writing the trailer, closes the dynamic buffer using
avio_close_dyn_buf(), writes the seektable and frees the buffer. But
the TTA muxer already has a deinit function which unconditionally
calls ffio_free_dyn_buf() on the dynamic buffer, so switching to
avio_get_dyn_buf() means that one can remove the code to free the
buffer; furthermore, it also might save an allocation if the seektable
is so small that it fits into the dynamic buffer's write buffer or if
adding the padding that avio_close_dyn_buf() adds necessitated
reallocating of the underlying buffer.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 libavformat/ttaenc.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

Comments

James Almer May 21, 2020, 2:46 a.m. UTC | #1
On 5/20/2020 11:36 PM, Andreas Rheinhardt wrote:
> The TTA muxer writes a seektable in a dynamic buffer as it receives
> packets and when writing the trailer, closes the dynamic buffer using
> avio_close_dyn_buf(), writes the seektable and frees the buffer. But
> the TTA muxer already has a deinit function which unconditionally
> calls ffio_free_dyn_buf() on the dynamic buffer, so switching to
> avio_get_dyn_buf() means that one can remove the code to free the
> buffer; furthermore, it also might save an allocation if the seektable
> is so small that it fits into the dynamic buffer's write buffer or if
> adding the padding that avio_close_dyn_buf() adds necessitated
> reallocating of the underlying buffer.
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
> ---
>  libavformat/ttaenc.c | 4 +---
>  1 file changed, 1 insertion(+), 3 deletions(-)
> 
> diff --git a/libavformat/ttaenc.c b/libavformat/ttaenc.c
> index 4860aab4c1..becd3e7153 100644
> --- a/libavformat/ttaenc.c
> +++ b/libavformat/ttaenc.c
> @@ -145,10 +145,8 @@ static int tta_write_trailer(AVFormatContext *s)
>      /* Write Seek table */
>      crc = ffio_get_checksum(tta->seek_table) ^ UINT32_MAX;
>      avio_wl32(tta->seek_table, crc);
> -    size = avio_close_dyn_buf(tta->seek_table, &ptr);
> +    size = avio_get_dyn_buf(tta->seek_table, &ptr);
>      avio_write(s->pb, ptr, size);
> -    tta->seek_table = NULL;
> -    av_free(ptr);
>  
>      /* Write audio data */
>      tta_queue_flush(s);

LGTM
Andreas Rheinhardt May 21, 2020, 3:34 a.m. UTC | #2
James Almer:
> On 5/20/2020 11:36 PM, Andreas Rheinhardt wrote:
>> The TTA muxer writes a seektable in a dynamic buffer as it receives
>> packets and when writing the trailer, closes the dynamic buffer using
>> avio_close_dyn_buf(), writes the seektable and frees the buffer. But
>> the TTA muxer already has a deinit function which unconditionally
>> calls ffio_free_dyn_buf() on the dynamic buffer, so switching to
>> avio_get_dyn_buf() means that one can remove the code to free the
>> buffer; furthermore, it also might save an allocation if the seektable
>> is so small that it fits into the dynamic buffer's write buffer or if
>> adding the padding that avio_close_dyn_buf() adds necessitated
>> reallocating of the underlying buffer.
>>
>> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
>> ---
>>  libavformat/ttaenc.c | 4 +---
>>  1 file changed, 1 insertion(+), 3 deletions(-)
>>
>> diff --git a/libavformat/ttaenc.c b/libavformat/ttaenc.c
>> index 4860aab4c1..becd3e7153 100644
>> --- a/libavformat/ttaenc.c
>> +++ b/libavformat/ttaenc.c
>> @@ -145,10 +145,8 @@ static int tta_write_trailer(AVFormatContext *s)
>>      /* Write Seek table */
>>      crc = ffio_get_checksum(tta->seek_table) ^ UINT32_MAX;
>>      avio_wl32(tta->seek_table, crc);
>> -    size = avio_close_dyn_buf(tta->seek_table, &ptr);
>> +    size = avio_get_dyn_buf(tta->seek_table, &ptr);
>>      avio_write(s->pb, ptr, size);
>> -    tta->seek_table = NULL;
>> -    av_free(ptr);
>>  
>>      /* Write audio data */
>>      tta_queue_flush(s);
> 
> LGTM

Applied, thanks.

- Andreas
diff mbox series

Patch

diff --git a/libavformat/ttaenc.c b/libavformat/ttaenc.c
index 4860aab4c1..becd3e7153 100644
--- a/libavformat/ttaenc.c
+++ b/libavformat/ttaenc.c
@@ -145,10 +145,8 @@  static int tta_write_trailer(AVFormatContext *s)
     /* Write Seek table */
     crc = ffio_get_checksum(tta->seek_table) ^ UINT32_MAX;
     avio_wl32(tta->seek_table, crc);
-    size = avio_close_dyn_buf(tta->seek_table, &ptr);
+    size = avio_get_dyn_buf(tta->seek_table, &ptr);
     avio_write(s->pb, ptr, size);
-    tta->seek_table = NULL;
-    av_free(ptr);
 
     /* Write audio data */
     tta_queue_flush(s);