From 4aea3ce7da9effbdfed4ea8d35c9fdd65ae49043 Mon Sep 17 00:00:00 2001 From: jp9000 Date: Wed, 17 May 2017 17:10:32 -0700 Subject: [PATCH] deps/media-playback: Always check for new frame first Fixes potential decoding errors with FFmpeg's new decode API. Because avcodec_send_packet may process multiple packets, you must call avcodec_receive_frame continually until no more frames are left. --- deps/media-playback/media-playback/decode.c | 31 +++++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/deps/media-playback/media-playback/decode.c b/deps/media-playback/media-playback/decode.c index 3086749c8..1ab44f0e4 100644 --- a/deps/media-playback/media-playback/decode.c +++ b/deps/media-playback/media-playback/decode.c @@ -210,13 +210,6 @@ static int decode_packet(struct mp_decode *d, int *got_frame) *got_frame = 0; #ifdef USE_NEW_FFMPEG_DECODE_API - ret = avcodec_send_packet(d->decoder, &d->pkt); - if (ret != 0 && ret != AVERROR(EAGAIN)) { - if (ret == AVERROR_EOF) - ret = 0; - return ret; - } - ret = avcodec_receive_frame(d->decoder, d->frame); if (ret != 0 && ret != AVERROR(EAGAIN)) { if (ret == AVERROR_EOF) @@ -224,8 +217,28 @@ static int decode_packet(struct mp_decode *d, int *got_frame) return ret; } - *got_frame = (ret == 0); - ret = d->pkt.size; + if (ret != 0) { + ret = avcodec_send_packet(d->decoder, &d->pkt); + if (ret != 0 && ret != AVERROR(EAGAIN)) { + if (ret == AVERROR_EOF) + ret = 0; + return ret; + } + + ret = avcodec_receive_frame(d->decoder, d->frame); + if (ret != 0 && ret != AVERROR(EAGAIN)) { + if (ret == AVERROR_EOF) + ret = 0; + return ret; + } + + *got_frame = (ret == 0); + ret = d->pkt.size; + } else { + ret = 0; + *got_frame = 1; + } + #else if (d->audio) { ret = avcodec_decode_audio4(d->decoder,