Skip to content

Commit 5bb3756

Browse files
committed
clean up unused param
1 parent 9b81ede commit 5bb3756

File tree

4 files changed

+13
-11
lines changed

4 files changed

+13
-11
lines changed

src/torchcodec/_core/CudaDeviceInterface.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,9 @@ std::string CudaDeviceInterface::getDetails() {
362362
(usingCPUFallback_ ? "CPU fallback." : "NVDEC.");
363363
}
364364

365-
// Below are methods for video encoding:
365+
// --------------------------------------------------------------------------
366+
// Below are methods exclusive to video encoding:
367+
// --------------------------------------------------------------------------
366368
namespace {
367369
// RGB to NV12 color conversion matrix for BT.601 limited range.
368370
// NPP ColorTwist function used below expects the limited range
@@ -378,7 +380,6 @@ const Npp32f defaultLimitedRangeRgbToNv12[3][4] = {
378380

379381
std::optional<UniqueAVFrame> CudaDeviceInterface::convertTensorToAVFrame(
380382
const torch::Tensor& tensor,
381-
[[maybe_unused]] AVPixelFormat targetFormat,
382383
int frameIndex,
383384
AVCodecContext* codecContext) {
384385
TORCH_CHECK(
@@ -447,6 +448,9 @@ std::optional<UniqueAVFrame> CudaDeviceInterface::convertTensorToAVFrame(
447448
return avFrame;
448449
}
449450

451+
// Allocates and initializes AVHWFramesContext, and sets pixel format fields
452+
// to enable encoding with CUDA device. The hw_frames_ctx field is needed by
453+
// FFmpeg to allocate frames on GPU's memory.
450454
void CudaDeviceInterface::setupHardwareFrameContext(
451455
AVCodecContext* codecContext) {
452456
TORCH_CHECK(codecContext != nullptr, "codecContext is null");
@@ -458,10 +462,10 @@ void CudaDeviceInterface::setupHardwareFrameContext(
458462
hwFramesCtxRef != nullptr,
459463
"Failed to allocate hardware frames context for codec");
460464

461-
// Always set pixel formats to options that support CUDA encoding.
462-
// TODO-VideoEncoder: Enable user set pixel formats to be set and properly
463-
// handled with NPP functions below
465+
// TODO-VideoEncoder: Enable user set pixel formats to be set
466+
// (outPixelFormat_) and handled with the appropriate NPP function
464467
codecContext->sw_pix_fmt = AV_PIX_FMT_NV12;
468+
// Always set pixel format to support CUDA encoding.
465469
codecContext->pix_fmt = AV_PIX_FMT_CUDA;
466470

467471
AVHWFramesContext* hwFramesCtx =
@@ -479,7 +483,6 @@ void CudaDeviceInterface::setupHardwareFrameContext(
479483
"Failed to initialize CUDA frames context for codec: ",
480484
getFFMPEGErrorStringFromErrorCode(ret));
481485
}
482-
483486
codecContext->hw_frames_ctx = hwFramesCtxRef;
484487
}
485488

src/torchcodec/_core/CudaDeviceInterface.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ class CudaDeviceInterface : public DeviceInterface {
4343

4444
std::optional<UniqueAVFrame> convertTensorToAVFrame(
4545
const torch::Tensor& tensor,
46-
AVPixelFormat targetFormat,
4746
int frameIndex,
4847
AVCodecContext* codecContext) override;
4948

src/torchcodec/_core/DeviceInterface.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ class DeviceInterface {
141141
// Function used for video encoding, only implemented in CudaDeviceInterface.
142142
virtual std::optional<UniqueAVFrame> convertTensorToAVFrame(
143143
[[maybe_unused]] const torch::Tensor& tensor,
144-
[[maybe_unused]] AVPixelFormat targetFormat,
145144
[[maybe_unused]] int frameIndex,
146145
[[maybe_unused]] AVCodecContext* codecContext) {
147146
return std::nullopt;

src/torchcodec/_core/Encoder.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,8 @@ void VideoEncoder::initializeEncoder(
827827
0);
828828
}
829829

830-
if (frames_.device().is_cuda()) {
830+
// When frames are on a CUDA device, deviceInterface_ will be defined.
831+
if (frames_.device().is_cuda() && deviceInterface_) {
831832
deviceInterface_->registerHardwareDeviceWithCodec(avCodecContext_.get());
832833
deviceInterface_->setupHardwareFrameContext(avCodecContext_.get());
833834
}
@@ -873,9 +874,9 @@ void VideoEncoder::encode() {
873874
for (int i = 0; i < numFrames; ++i) {
874875
torch::Tensor currFrame = frames_[i];
875876
UniqueAVFrame avFrame;
876-
if (deviceInterface_) {
877+
if (frames_.device().is_cuda() && deviceInterface_) {
877878
auto cudaFrame = deviceInterface_->convertTensorToAVFrame(
878-
currFrame, outPixelFormat_, i, avCodecContext_.get());
879+
currFrame, i, avCodecContext_.get());
879880
TORCH_CHECK(
880881
cudaFrame.has_value(),
881882
"convertTensorToAVFrame failed for frame ",

0 commit comments

Comments
 (0)