[FFmpeg-devel] [PATCH v3 2/2] lavf/mpegenc: fix termination following a fifo overrun

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Tue Mar 22 19:05:55 EET 2022


Nicolas Gaullier:
> Avoid an infinite 'retry' loop in output_packet when flushing.
> 
> A fatal error mentions the availability of fifo_size_limit option.
> 
> Signed-off-by: Nicolas Gaullier <nicolas.gaullier at cji.paris>
> ---
>  libavformat/mpegenc.c | 18 +++++++++++++++---
>  1 file changed, 15 insertions(+), 3 deletions(-)
> 
> diff --git a/libavformat/mpegenc.c b/libavformat/mpegenc.c
> index 5d755e3bdd..eff4531037 100644
> --- a/libavformat/mpegenc.c
> +++ b/libavformat/mpegenc.c
> @@ -85,6 +85,7 @@ typedef struct MpegMuxContext {
>  
>      int preload;
>      uint32_t fifo_size_limit;
> +    int fifo_size_exceeded;
>  } MpegMuxContext;
>  
>  extern const AVOutputFormat ff_mpeg1vcd_muxer;
> @@ -1153,7 +1154,7 @@ static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt)
>      StreamInfo *stream = st->priv_data;
>      int64_t pts, dts;
>      PacketDesc *pkt_desc;
> -    int preload;
> +    int preload, ret;
>      const int is_iframe = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
>                            (pkt->flags & AV_PKT_FLAG_KEY);
>  
> @@ -1220,10 +1221,17 @@ static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt)
>          }
>      }
>  
> -    av_fifo_write(stream->fifo, buf, size);
> +    ret = av_fifo_write(stream->fifo, buf, size);
> +    if (ret == AVERROR(ENOSPC)) {
> +        s->fifo_size_exceeded = 1;
> +        av_log(s, AV_LOG_FATAL, "Input stream buffer overrun. "
> +                "To avoid, increase fifo_size_limit option.\n");
> +    }
> +    if (ret < 0)
> +        return ret;
>  
>      for (;;) {
> -        int ret = output_packet(ctx, 0);
> +        ret = output_packet(ctx, 0);
>          if (ret <= 0)
>              return ret;
>      }
> @@ -1231,9 +1239,13 @@ static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt)
>  
>  static int mpeg_mux_end(AVFormatContext *ctx)
>  {
> +    MpegMuxContext *s = ctx->priv_data;
>      StreamInfo *stream;
>      int i;
>  
> +    if (s->fifo_size_exceeded)
> +        return 0;
> +
>      for (;;) {
>          int ret = output_packet(ctx, 1);
>          if (ret < 0)

Could this infinite loop also happen before switching to the new API?
Does it happen because avail_data is zero for all streams?

- Andreas


More information about the ffmpeg-devel mailing list