@@ -166,10 +166,13 @@ BetaCudaDeviceInterface::BetaCudaDeviceInterface(const torch::Device& device)
166166}
167167
168168BetaCudaDeviceInterface::~BetaCudaDeviceInterface () {
169- // TODONVDEC P0: we probably need to free the frames that have been decoded by
170- // NVDEC but not yet "mapped" - i.e. those that are still in readyFrames_?
171-
172169 if (decoder_) {
170+ // DALI doesn't seem to do any particular cleanup of the decoder before
171+ // sending it to the cache, so we probably don't need to do anything either.
172+ // Just to be safe, we flush.
173+ // What happens to those decode surfaces that haven't yet been mapped is
174+ // unclear.
175+ flush ();
173176 NVDECCache::getCache (device_.index ())
174177 .returnDecoder (&videoFormat_, std::move (decoder_));
175178 }
@@ -320,7 +323,7 @@ int BetaCudaDeviceInterface::streamPropertyChange(CUVIDEOFORMAT* videoFormat) {
320323 decoder_ = NVDECCache::getCache (device_.index ()).getDecoder (videoFormat);
321324
322325 if (!decoder_) {
323- // TODONVDEC P0 : consider re-configuring an existing decoder instead of
326+ // TODONVDEC P2 : consider re-configuring an existing decoder instead of
324327 // re-creating one. See docs, see DALI.
325328 decoder_ = createDecoder (videoFormat);
326329 }
@@ -405,13 +408,8 @@ void BetaCudaDeviceInterface::applyBSF(ReferenceAVPacket& packet) {
405408// given frame. It means we can send that frame to be decoded by the hardware
406409// NVDEC decoder by calling cuvidDecodePicture which is non-blocking.
407410int BetaCudaDeviceInterface::frameReadyForDecoding (CUVIDPICPARAMS* picParams) {
408- if (isFlushing_) {
409- return 0 ;
410- }
411-
412411 TORCH_CHECK (picParams != nullptr , " Invalid picture parameters" );
413412 TORCH_CHECK (decoder_, " Decoder not initialized before picture decode" );
414-
415413 // Send frame to be decoded by NVDEC - non-blocking call.
416414 CUresult result = cuvidDecodePicture (*decoder_.get (), picParams);
417415
@@ -554,16 +552,17 @@ UniqueAVFrame BetaCudaDeviceInterface::convertCudaFrameToAVFrame(
554552}
555553
556554void BetaCudaDeviceInterface::flush () {
557- isFlushing_ = true ;
558-
555+ // The NVCUVID docs mention that after seeking, i.e. when flush() is called,
556+ // we should send a packet with the CUVID_PKT_DISCONTINUITY flag. The docs
557+ // don't say whether this should be an empty packet, or whether it should be a
558+ // flag on the next non-empty packet. It doesn't matter: neither work :)
559+ // Sending an EOF packet, however, does work. So we do that. And we re-set the
560+ // eofSent_ flag to false because that's not a true EOF notification.
559561 sendEOFPacket ();
560-
561- isFlushing_ = false ;
562+ eofSent_ = false ;
562563
563564 std::queue<CUVIDPARSERDISPINFO> emptyQueue;
564565 std::swap (readyFrames_, emptyQueue);
565-
566- eofSent_ = false ;
567566}
568567
569568void BetaCudaDeviceInterface::convertAVFrameToFrameOutput (
0 commit comments