Skip to content

Commit 515deb5

Browse files
committed
Make UniqueCUvideodecoder a pointer on CUvideodecoder, not void
1 parent d0192ec commit 515deb5

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

src/torchcodec/_core/BetaCudaDeviceInterface.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,11 @@ static UniqueCUvideodecoder createDecoder(CUVIDEOFORMAT* videoFormat) {
120120
decoder_info.display_area.top = videoFormat->display_area.top;
121121
decoder_info.display_area.bottom = videoFormat->display_area.bottom;
122122

123-
CUvideodecoder rawDecoder;
124-
result = cuvidCreateDecoder(&rawDecoder, &decoder_info);
123+
CUvideodecoder* decoder = new CUvideodecoder();
124+
result = cuvidCreateDecoder(decoder, &decoder_info);
125125
TORCH_CHECK(
126126
result == CUDA_SUCCESS, "Failed to create NVDEC decoder: ", result);
127-
128-
return UniqueCUvideodecoder(rawDecoder, CUvideoDecoderDeleter{});
127+
return UniqueCUvideodecoder(decoder, CUvideoDecoderDeleter{});
129128
}
130129

131130
} // namespace
@@ -317,7 +316,7 @@ int BetaCudaDeviceInterface::frameReadyForDecoding(CUVIDPICPARAMS* pPicParams) {
317316
TORCH_CHECK(decoder_, "Decoder not initialized before picture decode");
318317

319318
// Send frame to be decoded by NVDEC - non-blocking call.
320-
CUresult result = cuvidDecodePicture(decoder_.get(), pPicParams);
319+
CUresult result = cuvidDecodePicture(*decoder_.get(), pPicParams);
321320
if (result != CUDA_SUCCESS) {
322321
return 0; // Yes, you're reading that right, 0 mean error.
323322
}
@@ -383,11 +382,7 @@ int BetaCudaDeviceInterface::receiveFrame(
383382
// blocking calls that waits until the frame is fully decoded and ready to be
384383
// used.
385384
CUresult result = cuvidMapVideoFrame(
386-
static_cast<CUvideodecoder>(decoder_.get()),
387-
dispInfo.picture_index,
388-
&framePtr,
389-
&pitch,
390-
&procParams);
385+
*decoder_.get(), dispInfo.picture_index, &framePtr, &pitch, &procParams);
391386

392387
if (result != CUDA_SUCCESS) {
393388
return AVERROR_EXTERNAL;
@@ -397,7 +392,7 @@ int BetaCudaDeviceInterface::receiveFrame(
397392

398393
// Unmap the frame so that the decoder can reuse its corresponding output
399394
// surface. Whether this is blocking is unclear?
400-
cuvidUnmapVideoFrame(static_cast<CUvideodecoder>(decoder_.get()), framePtr);
395+
cuvidUnmapVideoFrame(*decoder_.get(), framePtr);
401396
// TODONVDEC P0: Get clarity on this:
402397
// We assume that the framePtr is still valid after unmapping. That framePtr
403398
// is now part of the avFrame, which we'll return to the caller, and the

src/torchcodec/_core/NVDECCache.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,16 @@ namespace facebook::torchcodec {
2121
// this NVDEC Cache involves a cache key (the decoder parameters).
2222

2323
struct CUvideoDecoderDeleter {
24-
void operator()(CUvideodecoder decoder) const {
25-
if (decoder) {
26-
cuvidDestroyDecoder(decoder);
24+
void operator()(CUvideodecoder* decoderPtr) const {
25+
if (decoderPtr && *decoderPtr) {
26+
cuvidDestroyDecoder(*decoderPtr);
27+
delete decoderPtr;
2728
}
2829
}
2930
};
3031

31-
using UniqueCUvideodecoder = std::unique_ptr<void, CUvideoDecoderDeleter>;
32+
using UniqueCUvideodecoder =
33+
std::unique_ptr<CUvideodecoder, CUvideoDecoderDeleter>;
3234

3335
// A per-device cache for NVDEC decoders. There is one instance of this class
3436
// per GPU device, and it is accessed through the static getCache() method.

0 commit comments

Comments
 (0)