diff mbox series

[FFmpeg-devel,3/3] avcodec/avcodec: free decoded_side_data in ff_codec_close() when decoding

Message ID 20240501190156.36095-3-jamrial@gmail.com
State New
Headers show
Series [FFmpeg-devel,1/3] avcodec/avcodec: prevent ch_layout from being uninitialized in ff_codec_close() | expand

Commit Message

James Almer May 1, 2024, 7:01 p.m. UTC
It's set by the library, so it should be freed when closing the context.

Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavcodec/avcodec.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Comments

Andreas Rheinhardt May 1, 2024, 8:43 p.m. UTC | #1
James Almer:
> It's set by the library, so it should be freed when closing the context.
> 
> Signed-off-by: James Almer <jamrial@gmail.com>
> ---
>  libavcodec/avcodec.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c
> index e560efff6a..189a0a2193 100644
> --- a/libavcodec/avcodec.c
> +++ b/libavcodec/avcodec.c
> @@ -463,7 +463,9 @@ av_cold void ff_codec_close(AVCodecContext *avctx)
>              av_freep(&avctx->coded_side_data[i].data);
>          av_freep(&avctx->coded_side_data);
>          avctx->nb_coded_side_data = 0;
> -    }
> +    } else if (av_codec_is_decoder(avctx->codec))
> +        av_frame_side_data_free(&avctx->decoded_side_data,
> +                                &avctx->nb_decoded_side_data);
>  
>      av_buffer_unref(&avctx->hw_frames_ctx);
>      av_buffer_unref(&avctx->hw_device_ctx);

The documentation actually states that it is "owned and freed by the
encoder" for encoding, so your restriction to decoders is wrong. And
without it, the corresponding code in avcodec_free_context() is redundant.

- Andreas
James Almer May 1, 2024, 8:45 p.m. UTC | #2
On 5/1/2024 5:43 PM, Andreas Rheinhardt wrote:
> James Almer:
>> It's set by the library, so it should be freed when closing the context.
>>
>> Signed-off-by: James Almer <jamrial@gmail.com>
>> ---
>>   libavcodec/avcodec.c | 4 +++-
>>   1 file changed, 3 insertions(+), 1 deletion(-)
>>
>> diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c
>> index e560efff6a..189a0a2193 100644
>> --- a/libavcodec/avcodec.c
>> +++ b/libavcodec/avcodec.c
>> @@ -463,7 +463,9 @@ av_cold void ff_codec_close(AVCodecContext *avctx)
>>               av_freep(&avctx->coded_side_data[i].data);
>>           av_freep(&avctx->coded_side_data);
>>           avctx->nb_coded_side_data = 0;
>> -    }
>> +    } else if (av_codec_is_decoder(avctx->codec))
>> +        av_frame_side_data_free(&avctx->decoded_side_data,
>> +                                &avctx->nb_decoded_side_data);
>>   
>>       av_buffer_unref(&avctx->hw_frames_ctx);
>>       av_buffer_unref(&avctx->hw_device_ctx);
> 
> The documentation actually states that it is "owned and freed by the
> encoder" for encoding, so your restriction to decoders is wrong. And
> without it, the corresponding code in avcodec_free_context() is redundant.
> 
> - Andreas

Do you suggest freeing it in ff_codec_close() for both scenarios then?
Andreas Rheinhardt May 1, 2024, 8:55 p.m. UTC | #3
James Almer:
> On 5/1/2024 5:43 PM, Andreas Rheinhardt wrote:
>> James Almer:
>>> It's set by the library, so it should be freed when closing the context.
>>>
>>> Signed-off-by: James Almer <jamrial@gmail.com>
>>> ---
>>>   libavcodec/avcodec.c | 4 +++-
>>>   1 file changed, 3 insertions(+), 1 deletion(-)
>>>
>>> diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c
>>> index e560efff6a..189a0a2193 100644
>>> --- a/libavcodec/avcodec.c
>>> +++ b/libavcodec/avcodec.c
>>> @@ -463,7 +463,9 @@ av_cold void ff_codec_close(AVCodecContext *avctx)
>>>               av_freep(&avctx->coded_side_data[i].data);
>>>           av_freep(&avctx->coded_side_data);
>>>           avctx->nb_coded_side_data = 0;
>>> -    }
>>> +    } else if (av_codec_is_decoder(avctx->codec))
>>> +        av_frame_side_data_free(&avctx->decoded_side_data,
>>> +                                &avctx->nb_decoded_side_data);
>>>         av_buffer_unref(&avctx->hw_frames_ctx);
>>>       av_buffer_unref(&avctx->hw_device_ctx);
>>
>> The documentation actually states that it is "owned and freed by the
>> encoder" for encoding, so your restriction to decoders is wrong. And
>> without it, the corresponding code in avcodec_free_context() is
>> redundant.
>>
>> - Andreas
> 
> Do you suggest freeing it in ff_codec_close() for both scenarios then?

Yes.

- Andreas
diff mbox series

Patch

diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c
index e560efff6a..189a0a2193 100644
--- a/libavcodec/avcodec.c
+++ b/libavcodec/avcodec.c
@@ -463,7 +463,9 @@  av_cold void ff_codec_close(AVCodecContext *avctx)
             av_freep(&avctx->coded_side_data[i].data);
         av_freep(&avctx->coded_side_data);
         avctx->nb_coded_side_data = 0;
-    }
+    } else if (av_codec_is_decoder(avctx->codec))
+        av_frame_side_data_free(&avctx->decoded_side_data,
+                                &avctx->nb_decoded_side_data);
 
     av_buffer_unref(&avctx->hw_frames_ctx);
     av_buffer_unref(&avctx->hw_device_ctx);