Skip to content

Commit 19c39cd

Browse files
committed
Move packet allocation out of decoding loop
1 parent f021687 commit 19c39cd

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/torchcodec/decoders/_core/VideoDecoder.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,7 @@ VideoDecoder::RawDecodedOutput VideoDecoder::getDecodedOutputWithFilter(
763763
}
764764
// Need to get the next frame or error from PopFrame.
765765
UniqueAVFrame frame(av_frame_alloc());
766+
UniqueAVPacket packet(av_packet_alloc());
766767
int ffmpegStatus = AVSUCCESS;
767768
bool reachedEOF = false;
768769
int frameStreamIndex = -1;
@@ -802,7 +803,6 @@ VideoDecoder::RawDecodedOutput VideoDecoder::getDecodedOutputWithFilter(
802803
// pulling frames from its internal buffers.
803804
continue;
804805
}
805-
UniqueAVPacket packet(av_packet_alloc());
806806
ffmpegStatus = av_read_frame(formatContext_.get(), packet.get());
807807
decodeStats_.numPacketsRead++;
808808
if (ffmpegStatus == AVERROR_EOF) {
@@ -820,6 +820,7 @@ VideoDecoder::RawDecodedOutput VideoDecoder::getDecodedOutputWithFilter(
820820
}
821821
}
822822
reachedEOF = true;
823+
av_packet_unref(packet.get());
823824
continue;
824825
}
825826
if (ffmpegStatus < AVSUCCESS) {
@@ -829,16 +830,18 @@ VideoDecoder::RawDecodedOutput VideoDecoder::getDecodedOutputWithFilter(
829830
}
830831
if (activeStreamIndices_.count(packet->stream_index) == 0) {
831832
// This packet is not for any of the active streams.
833+
av_packet_unref(packet.get());
832834
continue;
833835
}
834836
ffmpegStatus = avcodec_send_packet(
835837
streams_[packet->stream_index].codecContext.get(), packet.get());
836-
decodeStats_.numPacketsSentToDecoder++;
837838
if (ffmpegStatus < AVSUCCESS) {
838839
throw std::runtime_error(
839840
"Could not push packet to decoder: " +
840841
getFFMPEGErrorStringFromErrorCode(ffmpegStatus));
841842
}
843+
decodeStats_.numPacketsSentToDecoder++;
844+
av_packet_unref(packet.get());
842845
}
843846
if (ffmpegStatus < AVSUCCESS) {
844847
if (reachedEOF || ffmpegStatus == AVERROR_EOF) {

0 commit comments

Comments
 (0)