Skip to content

Commit 6529f2b

Browse files
committed
GOTOs for the win
1 parent 79c3934 commit 6529f2b

File tree

1 file changed

+29
-25
lines changed

1 file changed

+29
-25
lines changed

src/torchcodec/decoders/_core/VideoDecoder.cpp

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -948,8 +948,12 @@ VideoDecoder::AVFrameStream VideoDecoder::decodeAVFrame(
948948
int ffmpegStatus = AVSUCCESS;
949949
bool reachedEOF = false;
950950
while (true) {
951+
outerLoopStart:
951952
ffmpegStatus =
952953
avcodec_receive_frame(streamInfo.codecContext.get(), avFrame.get());
954+
if (ffmpegStatus == AVERROR(EAGAIN)) {
955+
printf("Yes\n");
956+
}
953957

954958
if (ffmpegStatus != AVSUCCESS && ffmpegStatus != AVERROR(EAGAIN)) {
955959
// Non-retriable error
@@ -979,36 +983,36 @@ VideoDecoder::AVFrameStream VideoDecoder::decodeAVFrame(
979983
// We still haven't found the frame we're looking for. So let's read more
980984
// packets and send them to the decoder.
981985
ReferenceAVPacket packet(autoAVPacket);
982-
ffmpegStatus = av_read_frame(formatContext_.get(), packet.get());
983-
decodeStats_.numPacketsRead++;
986+
bool foundPacketForStream = false;
987+
while (!foundPacketForStream) {
988+
ffmpegStatus = av_read_frame(formatContext_.get(), packet.get());
989+
decodeStats_.numPacketsRead++;
990+
991+
if (ffmpegStatus == AVERROR_EOF) {
992+
// End of file reached. We must drain the codec by sending a nullptr
993+
// packet.
994+
ffmpegStatus = avcodec_send_packet(
995+
streamInfo.codecContext.get(),
996+
/*avpkt=*/nullptr);
997+
if (ffmpegStatus < AVSUCCESS) {
998+
throw std::runtime_error(
999+
"Could not flush decoder: " +
1000+
getFFMPEGErrorStringFromErrorCode(ffmpegStatus));
1001+
}
1002+
1003+
// We've reached the end of file so we can't read any more packets from
1004+
// it, but the decoder may still have frames to read in its buffer.
1005+
// Continue iterating to try reading frames.
1006+
reachedEOF = true;
1007+
goto outerLoopStart;
1008+
}
9841009

985-
if (ffmpegStatus == AVERROR_EOF) {
986-
// End of file reached. We must drain the codec by sending a nullptr
987-
// packet.
988-
ffmpegStatus = avcodec_send_packet(
989-
streamInfo.codecContext.get(),
990-
/*avpkt=*/nullptr);
9911010
if (ffmpegStatus < AVSUCCESS) {
9921011
throw std::runtime_error(
993-
"Could not flush decoder: " +
1012+
"Could not read frame from input file: " +
9941013
getFFMPEGErrorStringFromErrorCode(ffmpegStatus));
9951014
}
996-
997-
// We've reached the end of file so we can't read any more packets from
998-
// it, but the decoder may still have frames to read in its buffer.
999-
// Continue iterating to try reading frames.
1000-
reachedEOF = true;
1001-
continue;
1002-
}
1003-
1004-
if (ffmpegStatus < AVSUCCESS) {
1005-
throw std::runtime_error(
1006-
"Could not read frame from input file: " +
1007-
getFFMPEGErrorStringFromErrorCode(ffmpegStatus));
1008-
}
1009-
1010-
if (packet->stream_index != activeStreamIndex_) {
1011-
continue;
1015+
foundPacketForStream = packet->stream_index == activeStreamIndex_;
10121016
}
10131017

10141018
// We got a valid packet. Send it to the decoder, and we'll receive it in

0 commit comments

Comments
 (0)