@@ -222,6 +222,13 @@ void VideoDecoder::scanFileAndUpdateMetadataAndIndex() {
222222 return ;
223223 }
224224
225+ for (unsigned int i = 0 ; i < formatContext_->nb_streams ; ++i) {
226+ // We want to scan and update the metadata of all streams.
227+ TORCH_CHECK (
228+ formatContext_->streams [i]->discard != AVDISCARD_ALL,
229+ " Did you add a stream before you called for a scan?" );
230+ }
231+
225232 AutoAVPacket autoAVPacket;
226233 while (true ) {
227234 ReferenceAVPacket packet (autoAVPacket);
@@ -542,6 +549,16 @@ void VideoDecoder::addVideoStreamDecoder(
542549 updateMetadataWithCodecContext (streamInfo.streamIndex , codecContext);
543550 streamInfo.videoStreamOptions = videoStreamOptions;
544551
552+ // We will only need packets from the active stream, so we tell FFmpeg to
553+ // discard packets from the other streams. Note that av_read_frame() may still
554+ // return some of those undesired packets under some conditions, so it's still
555+ // important to discard/demux packets correctly in the inner decoding loop.
556+ for (unsigned int i = 0 ; i < formatContext_->nb_streams ; ++i) {
557+ if (i != static_cast <unsigned int >(activeStreamIndex_)) {
558+ formatContext_->streams [i]->discard = AVDISCARD_ALL;
559+ }
560+ }
561+
545562 // By default, we want to use swscale for color conversion because it is
546563 // faster. However, it has width requirements, so we may need to fall back
547564 // to filtergraph. We also need to respect what was requested from the
0 commit comments