@@ -1063,12 +1063,18 @@ VideoDecoder::AVFrameStream VideoDecoder::decodeAVFrame(
10631063 int ffmpegStatus = AVSUCCESS;
10641064 bool reachedEOF = false ;
10651065 while (true ) {
1066+ if (veryFirstCall_) {
1067+ veryFirstCall_ = false ;
1068+ goto av_read_frame_call;
1069+ }
1070+ begin_loop:
10661071 ffmpegStatus =
10671072 avcodec_receive_frame (streamInfo.codecContext .get (), avFrame.get ());
1068- // printf("output of avcodec_receive_frame: %d\n", ffmpegStatus);
1073+ printf (" output of avcodec_receive_frame: %d\n " , ffmpegStatus);
10691074
10701075 if (ffmpegStatus != AVSUCCESS && ffmpegStatus != AVERROR (EAGAIN)) {
10711076 // Non-retriable error
1077+ printf (" Non-retriable error\n " );
10721078 break ;
10731079 }
10741080
@@ -1078,55 +1084,65 @@ VideoDecoder::AVFrameStream VideoDecoder::decodeAVFrame(
10781084 // Yes, this is the frame we'll return; break out of the decoding loop.
10791085 // printf("%ld %ld\n", avFrame->pts, avFrame->duration);
10801086
1087+ printf (" Found frame\n " );
10811088 break ;
10821089 } else if (ffmpegStatus == AVSUCCESS) {
10831090 // No, but we received a valid frame - just not the kind we're looking
10841091 // for. The logic below will read packets and send them to the decoder.
10851092 // But since we did just receive a frame, we should skip reading more
10861093 // packets and sending them to the decoder and just try to receive more
10871094 // frames from the decoder.
1095+ printf (" Got AVSUCCESS, continue\n " );
10881096 continue ;
10891097 }
10901098
10911099 if (reachedEOF) {
10921100 // We don't have any more packets to send to the decoder. So keep on
10931101 // pulling frames from its internal buffers.
1102+ printf (" Reached EOF, continue\n " );
10941103 continue ;
10951104 }
10961105
1097- // We still haven't found the frame we're looking for. So let's read more
1098- // packets and send them to the decoder.
1106+ av_read_frame_call:
10991107 ReferenceAVPacket packet (autoAVPacket);
1100- ffmpegStatus = av_read_frame (formatContext_.get (), packet.get ());
1101- decodeStats_.numPacketsRead ++;
1108+ while (true ) {
1109+ // We still haven't found the frame we're looking for. So let's read more
1110+ // packets and send them to the decoder.
1111+ ffmpegStatus = av_read_frame (formatContext_.get (), packet.get ());
1112+ decodeStats_.numPacketsRead ++;
1113+
1114+ if (ffmpegStatus == AVERROR_EOF) {
1115+ // End of file reached. We must drain the codec by sending a nullptr
1116+ // packet.
1117+ ffmpegStatus = avcodec_send_packet (
1118+ streamInfo.codecContext .get (),
1119+ /* avpkt=*/ nullptr );
1120+ if (ffmpegStatus < AVSUCCESS) {
1121+ throw std::runtime_error (
1122+ " Could not flush decoder: " +
1123+ getFFMPEGErrorStringFromErrorCode (ffmpegStatus));
1124+ }
1125+
1126+ // We've reached the end of file so we can't read any more packets from
1127+ // it, but the decoder may still have frames to read in its buffer.
1128+ // Continue iterating to try reading frames.
1129+ reachedEOF = true ;
1130+ goto begin_loop;
1131+ // continue;
1132+ }
11021133
1103- if (ffmpegStatus == AVERROR_EOF) {
1104- // End of file reached. We must drain the codec by sending a nullptr
1105- // packet.
1106- ffmpegStatus = avcodec_send_packet (
1107- streamInfo.codecContext .get (),
1108- /* avpkt=*/ nullptr );
11091134 if (ffmpegStatus < AVSUCCESS) {
11101135 throw std::runtime_error (
1111- " Could not flush decoder : " +
1136+ " Could not read frame from input file : " +
11121137 getFFMPEGErrorStringFromErrorCode (ffmpegStatus));
11131138 }
11141139
1115- // We've reached the end of file so we can't read any more packets from
1116- // it, but the decoder may still have frames to read in its buffer.
1117- // Continue iterating to try reading frames.
1118- reachedEOF = true ;
1119- continue ;
1120- }
1121-
1122- if (ffmpegStatus < AVSUCCESS) {
1123- throw std::runtime_error (
1124- " Could not read frame from input file: " +
1125- getFFMPEGErrorStringFromErrorCode (ffmpegStatus));
1126- }
1127-
1128- if (packet->stream_index != activeStreamIndex_) {
1129- continue ;
1140+ if (packet->stream_index == activeStreamIndex_) {
1141+ printf (" found packet for stream\n " );
1142+ break ;
1143+ } else {
1144+ printf (" Not for stream, continue\n " );
1145+ }
11301146 }
11311147
11321148 // We got a valid packet. Send it to the decoder, and we'll receive it in
0 commit comments