Skip to content

Commit

Permalink
Avoid receiving undesired packets
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasHug committed Feb 7, 2025
1 parent b87f00d commit f0ff127
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/torchcodec/decoders/_core/VideoDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,13 @@ void VideoDecoder::scanFileAndUpdateMetadataAndIndex() {
return;
}

for (unsigned int i = 0; i < formatContext_->nb_streams; ++i) {
// We want to scan and update the metadata of all streams.
TORCH_CHECK(
formatContext_->streams[i]->discard != AVDISCARD_ALL,
"Did you add a stream before you called for a scan?");
}

AutoAVPacket autoAVPacket;
while (true) {
ReferenceAVPacket packet(autoAVPacket);
Expand Down Expand Up @@ -481,6 +488,16 @@ void VideoDecoder::addVideoStreamDecoder(
updateMetadataWithCodecContext(streamInfo.streamIndex, codecContext);
streamInfo.videoStreamOptions = videoStreamOptions;

// We will only need packets from the active stream, so we tell FFmpeg to
// discard packets from the other streams. Note that av_read_frame() may still
// return some of those un-desired packet under some conditions, so it's still
// important to discard/demux correctly in the inner decoding loop.
for (unsigned int i = 0; i < formatContext_->nb_streams; ++i) {
if (i != static_cast<unsigned int>(activeStreamIndex_)) {
formatContext_->streams[i]->discard = AVDISCARD_ALL;
}
}

// By default, we want to use swscale for color conversion because it is
// faster. However, it has width requirements, so we may need to fall back
// to filtergraph. We also need to respect what was requested from the
Expand Down

0 comments on commit f0ff127

Please sign in to comment.