[FFmpeg-devel] [PATCH 12/35] lavc/avcodec: switch to new FIFO API

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Thu Jan 13 20:21:31 EET 2022


Anton Khirnov:
> ---
>  libavcodec/avcodec.c | 15 ++++++---------
>  libavcodec/decode.c  | 22 ++++++++--------------
>  2 files changed, 14 insertions(+), 23 deletions(-)
> 
> diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c
> index c00a9b2af8..a75bbe721f 100644
> --- a/libavcodec/avcodec.c
> +++ b/libavcodec/avcodec.c
> @@ -183,7 +183,8 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code
>      avci->es.in_frame = av_frame_alloc();
>      avci->in_pkt = av_packet_alloc();
>      avci->last_pkt_props = av_packet_alloc();
> -    avci->pkt_props = av_fifo_alloc(sizeof(*avci->last_pkt_props));
> +    avci->pkt_props = av_fifo_alloc2(1, sizeof(*avci->last_pkt_props),
> +                                     AV_FIFO_FLAG_AUTO_GROW);
>      if (!avci->buffer_frame || !avci->buffer_pkt          ||
>          !avci->es.in_frame  || !avci->in_pkt     ||
>          !avci->last_pkt_props || !avci->pkt_props) {
> @@ -399,13 +400,10 @@ void avcodec_flush_buffers(AVCodecContext *avctx)
>      av_packet_unref(avci->buffer_pkt);
>  
>      av_packet_unref(avci->last_pkt_props);
> -    while (av_fifo_size(avci->pkt_props) >= sizeof(*avci->last_pkt_props)) {
> -        av_fifo_generic_read(avci->pkt_props,
> -                             avci->last_pkt_props, sizeof(*avci->last_pkt_props),
> -                             NULL);
> +    while (av_fifo_can_read(avci->pkt_props)) {
> +        av_fifo_read(avci->pkt_props, avci->last_pkt_props, 1);
>          av_packet_unref(avci->last_pkt_props);

Could be simplified to
do {
    av_packet_unref(avci->last_pkt_props);
} while (av_fifo_read(avci->pkt_props, avci->last_pkt_props, 1) >= 0);

The same goes for other loop below and probably more loops in the next
patches.
(This of course presumes that one is allowed to call av_fifo_read() to
tentatively read more elements than are currently available. I think
this should be allowed.)

>      }
> -    av_fifo_reset(avci->pkt_props);
>  
>      av_frame_unref(avci->es.in_frame);
>      av_packet_unref(avci->in_pkt);
> @@ -464,10 +462,9 @@ av_cold int avcodec_close(AVCodecContext *avctx)
>          av_frame_free(&avci->buffer_frame);
>          av_packet_free(&avci->buffer_pkt);
>          if (avci->pkt_props) {
> -            while (av_fifo_size(avci->pkt_props) >= sizeof(*avci->last_pkt_props)) {
> +            while (av_fifo_can_read(avci->pkt_props)) {
>                  av_packet_unref(avci->last_pkt_props);
> -                av_fifo_generic_read(avci->pkt_props, avci->last_pkt_props,
> -                                     sizeof(*avci->last_pkt_props), NULL);
> +                av_fifo_read(avci->pkt_props, avci->last_pkt_props, 1);
>              }
>              av_fifo_freep(&avci->pkt_props);
>          }
> diff --git a/libavcodec/decode.c b/libavcodec/decode.c
> index 0912f86a14..9f6f2e7fa6 100644
> --- a/libavcodec/decode.c
> +++ b/libavcodec/decode.c
> @@ -165,26 +165,21 @@ static int extract_packet_props(AVCodecInternal *avci, const AVPacket *pkt)
>      int ret = 0;
>  
>      if (IS_EMPTY(avci->last_pkt_props)) {
> -        if (av_fifo_size(avci->pkt_props) >= sizeof(*pkt)) {
> -            av_fifo_generic_read(avci->pkt_props, avci->last_pkt_props,
> -                                 sizeof(*avci->last_pkt_props), NULL);
> +        if (av_fifo_can_read(avci->pkt_props)) {
> +            av_fifo_read(avci->pkt_props, avci->last_pkt_props, 1);
>          } else

if (av_fifo_read(avci->pkt_props, avci->last_pkt_props, 1) < 0)

>              return copy_packet_props(avci->last_pkt_props, pkt);
>      }
>  
> -    if (av_fifo_space(avci->pkt_props) < sizeof(*pkt)) {
> -        ret = av_fifo_grow(avci->pkt_props, sizeof(*pkt));
> -        if (ret < 0)
> -            return ret;
> -    }
> -
>      ret = copy_packet_props(&tmp, pkt);
>      if (ret < 0)
>          return ret;
>  
> -    av_fifo_generic_write(avci->pkt_props, &tmp, sizeof(tmp), NULL);
> +    ret = av_fifo_write(avci->pkt_props, &tmp, 1);
> +    if (ret < 0)
> +        av_packet_unref(&tmp);
>  
> -    return 0;
> +    return ret;
>  }
>  
>  static int decode_bsfs_init(AVCodecContext *avctx)
> @@ -543,9 +538,8 @@ static int decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
>          avci->draining_done = 1;
>  
>      if (!(avctx->codec->caps_internal & FF_CODEC_CAP_SETS_FRAME_PROPS) &&
> -        IS_EMPTY(avci->last_pkt_props) && av_fifo_size(avci->pkt_props) >= sizeof(*avci->last_pkt_props))
> -        av_fifo_generic_read(avci->pkt_props,
> -                             avci->last_pkt_props, sizeof(*avci->last_pkt_props), NULL);
> +        IS_EMPTY(avci->last_pkt_props) && av_fifo_can_read(avci->pkt_props))

The av_fifo_can_read() check could be dropped.

> +        av_fifo_read(avci->pkt_props, avci->last_pkt_props, 1);
>  
>      if (!ret) {
>          frame->best_effort_timestamp = guess_correct_pts(avctx,
> 



More information about the ffmpeg-devel mailing list