diff --git a/libavcodec/decode.c b/libavcodec/decode.c index 3ef1ece25b..257abc7de4 100644 --- a/libavcodec/decode.c +++ b/libavcodec/decode.c @@ -318,7 +318,7 @@ static int64_t guess_correct_pts(AVCodecContext *ctx, * returning any output, so this function needs to be called in a loop until it * returns EAGAIN. **/ -static inline int decode_simple_internal(AVCodecContext *avctx, AVFrame *frame) +static inline int decode_simple_internal(AVCodecContext *avctx, AVFrame *frame, int64_t *discarded_samples) { AVCodecInternal *avci = avctx->internal; DecodeSimpleContext *ds = &avci->ds; @@ -411,12 +411,14 @@ static inline int decode_simple_internal(AVCodecContext *avctx, AVFrame *frame) !(avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) { avci->skip_samples = FFMAX(0, avci->skip_samples - frame->nb_samples); got_frame = 0; + *discarded_samples += frame->nb_samples; } if (avci->skip_samples > 0 && got_frame && !(avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) { if(frame->nb_samples <= avci->skip_samples){ got_frame = 0; + *discarded_samples += frame->nb_samples; avci->skip_samples -= frame->nb_samples; av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n", avci->skip_samples); @@ -444,6 +446,7 @@ FF_ENABLE_DEPRECATION_WARNINGS } av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n", avci->skip_samples, frame->nb_samples); + *discarded_samples += avci->skip_samples; frame->nb_samples -= avci->skip_samples; avci->skip_samples = 0; } @@ -452,6 +455,7 @@ FF_ENABLE_DEPRECATION_WARNINGS if (discard_padding > 0 && discard_padding <= frame->nb_samples && got_frame && !(avctx->flags2 & AV_CODEC_FLAG2_SKIP_MANUAL)) { if (discard_padding == frame->nb_samples) { + *discarded_samples += frame->nb_samples; got_frame = 0; } else { if(avctx->pkt_timebase.num && avctx->sample_rate) { @@ -544,9 +548,12 @@ FF_ENABLE_DEPRECATION_WARNINGS static int decode_simple_receive_frame(AVCodecContext *avctx, AVFrame *frame) { int ret; + int64_t discarded_samples = 0; while (!frame->buf[0]) { - ret = decode_simple_internal(avctx, frame); + if (discarded_samples > avctx->max_samples) + return AVERROR(EAGAIN); + ret = decode_simple_internal(avctx, frame, &discarded_samples); if (ret < 0) return ret; }