[FFmpeg-devel] [PATCH] avcodec: Allow to query number of consumed bytes using the new API

James Almer jamrial at gmail.com
Wed Jan 30 22:02:08 EET 2019


On 1/30/2019 4:19 PM, jannis_wth wrote:
> 30.01.19 19:51 James Almer:
>> On 1/30/2019 3:43 PM, jannis_wth wrote:
>>> Okay, so how about this one?
>>> This functionality is important if you don't know the packet size you
>>> are feeding. It allows you to reconstruct the size.
>>
>> In what scenario you can't know the size of the AVPacket you're feeding
>> to avcodec_send_packet().
>>
> For example when a mp4 container lost its moov atom.

What value is stored in pkt->size in this scenario at the time you feed
it to libavcodec? This function will return that value after all,
provided it's valid data.

Also, AVCodecParsers are the usual way to reconstruct/assemble broken or
incomplete packets.

> 
>>>
>>> 0001-avcodec-Allow-to-query-number-of-consumed-bytes-usin.patch
>>>
>>> From bad739e4f5e9e78cc6fa799287d758bf27db8208 Mon Sep 17 00:00:00 2001
>>> From: user <user at host>
>>> Date: Wed, 30 Jan 2019 19:33:08 +0100
>>> Subject: [PATCH 1/1] avcodec: Allow to query number of consumed bytes using
>>>  the new API
>>>
>>> Currently the only way to get the number of consumed bytes is by using
>>> the deprecated decode_audio4() API. This patch adds a simple function
>>> to fix this.
>>>
>>> Signed-off-by: Jannis Kambs <jannis_wth at gmx.de>
>>> ---
>>>  libavcodec/avcodec.h | 9 +++++++++
>>>  libavcodec/utils.c   | 5 +++++
>>>  2 files changed, 14 insertions(+)
>>>
>>> diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
>>> index f554c53f0e..43bf84e58b 100644
>>> --- a/libavcodec/avcodec.h
>>> +++ b/libavcodec/avcodec.h
>>> @@ -5714,6 +5714,15 @@ int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes);
>>>   */
>>>  int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes);
>>>  
>>> +/**
>>> + * This function allows to get the number of remaining bytes after
>>> + * avcodec_receive_frame() has been called.
>>> + *
>>> + * @param avctx the codec context
>>> + * @return number of remaining bytes from the input AVPacket.
>>> + */
>>> +int avcodec_get_remaining_size(AVCodecContext *avctx);
>>> +
>>>  #if FF_API_OLD_BSF
>>>  typedef struct AVBitStreamFilterContext {
>>>      void *priv_data;
>>> diff --git a/libavcodec/utils.c b/libavcodec/utils.c
>>> index d519b16092..638154f974 100644
>>> --- a/libavcodec/utils.c
>>> +++ b/libavcodec/utils.c
>>> @@ -1727,6 +1727,11 @@ int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes)
>>>                                      frame_bytes);
>>>  }
>>>  
>>> +int avcodec_get_remaining_size(AVCodecContext *avctx)
>>> +{
>>> +    return avctx->internal->ds.in_pkt->size;
>>
>> This is only used by decoders implementing the AVCodec.decode()
>> callback. It will always be zero for any decoder using
>> AVCodec.receive_frame(), like Bink Audio, libdav1d, etc.
> 
> Well, what would be the correct way then?
> Or is there no good way and thus you won't accept such interface?

Accepting it or not depends on what other developers think about such a
function. Unlike the old API, the new one always fully consumes all
packets you feed to it, so there's no real need to check how many bytes
were consumed.
Trying to expose the internals for this purpose is not really feasible,
as you could see with all the different layers of internal buffering.

I guess avctx->internal->last_pkt_props could work for this, but it may
not be consistent across decoders.


More information about the ffmpeg-devel mailing list