Skip to content

Commit 52a5347

Browse files
committed
Flushing cleanups, add comments
1 parent 993d510 commit 52a5347

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

src/torchcodec/_core/BetaCudaDeviceInterface.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,13 @@ BetaCudaDeviceInterface::BetaCudaDeviceInterface(const torch::Device& device)
166166
}
167167

168168
BetaCudaDeviceInterface::~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.
407410
int 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

556554
void 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

569568
void BetaCudaDeviceInterface::convertAVFrameToFrameOutput(

src/torchcodec/_core/BetaCudaDeviceInterface.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class BetaCudaDeviceInterface : public DeviceInterface {
6363

6464
private:
6565
int sendCuvidPacket(CUVIDSOURCEDATAPACKET& cuvidPacket);
66+
6667
// Apply bitstream filter, modifies packet in-place
6768
void applyBSF(ReferenceAVPacket& packet);
6869
void initializeBSF(
@@ -82,10 +83,6 @@ class BetaCudaDeviceInterface : public DeviceInterface {
8283

8384
bool eofSent_ = false;
8485

85-
// Flush flag to prevent decode operations during flush (like DALI's
86-
// isFlushing_)
87-
bool isFlushing_ = false;
88-
8986
AVRational timeBase_ = {0, 1};
9087
AVRational frameRateAvgFromFFmpeg_ = {0, 1};
9188

0 commit comments

Comments
 (0)