[FFmpeg-devel] [PATCHv2] avcodec/utils: do not reallocate packet buffer for AV_CODEC_ID_WRAPPED_AVFRAME

Marton Balint cus at passwd.hu
Mon Feb 20 22:11:50 EET 2017


On Sun, 19 Feb 2017, Hendrik Leppkes wrote:

> On Sun, Feb 19, 2017 at 2:41 PM, wm4 <nfxjfg at googlemail.com> wrote:
>> On Sun, 19 Feb 2017 14:35:42 +0100
>> Marton Balint <cus at passwd.hu> wrote:
>>
>>> Reallocating a wrapped avframe invalidates internal pointers, such as extended
>>> data.
>>>
>>> FFmpeg has another way of passing AVFrames to muxers, but it seems the API
>>> (av_write_uncoded_frame) is not implemented in the ffmpeg CLI yet.
>>>
>>> Signed-off-by: Marton Balint <cus at passwd.hu>
>>> ---
>>>  libavcodec/utils.c | 4 ++--
>>>  1 file changed, 2 insertions(+), 2 deletions(-)
>>>
>>> diff --git a/libavcodec/utils.c b/libavcodec/utils.c
>>> index f4085bf..184821a 100644
>>> --- a/libavcodec/utils.c
>>> +++ b/libavcodec/utils.c
>>> @@ -1820,7 +1820,7 @@ int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
>>>      AVFrame *padded_frame = NULL;
>>>      int ret;
>>>      AVPacket user_pkt = *avpkt;
>>> -    int needs_realloc = !user_pkt.data;
>>> +    int needs_realloc = !user_pkt.data && avctx->codec_id != AV_CODEC_ID_WRAPPED_AVFRAME;
>>>
>>>      *got_packet_ptr = 0;
>>>
>>> @@ -1964,7 +1964,7 @@ int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
>>>  {
>>>      int ret;
>>>      AVPacket user_pkt = *avpkt;
>>> -    int needs_realloc = !user_pkt.data;
>>> +    int needs_realloc = !user_pkt.data && avctx->codec_id != AV_CODEC_ID_WRAPPED_AVFRAME;
>>>
>>>      *got_packet_ptr = 0;
>>>
>>
>> I don't understand this logic in the first place. If nothing was
>> encoded (!ret), and avpkt->data is set (why is it set?), then the
>> AVPacket.buf is realllocated (why?) to the packet size (why???) - how
>> does it make sense?
>
> ret = 0 indicates successfull encode, ie. not an error code. What
> result code did you expect?
> AFAIK the realloc is performed to shrink over-sized pre-allocated
> packets down and save memory.
>

So is it OK to apply the patch?

Thanks,
Marton


More information about the ffmpeg-devel mailing list