Skip to content

Commit 6d88905

Browse files
committed
additional small fixes
1 parent f8b64ea commit 6d88905

File tree

3 files changed

+5
-11
lines changed

3 files changed

+5
-11
lines changed

src/torchcodec/_core/Encoder.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,6 @@ void VideoEncoder::initializeEncoder(
727727
if (frames_.device().is_cuda()) {
728728
gpuEncoder_ = std::make_unique<GpuEncoder>(frames_.device());
729729
}
730-
731730
const AVCodec* avCodec = nullptr;
732731
// If codec arg is provided, find codec using logic similar to FFmpeg:
733732
// https://github.com/FFmpeg/FFmpeg/blob/master/fftools/ffmpeg_opt.c#L804-L835
@@ -892,7 +891,6 @@ void VideoEncoder::encode() {
892891
UniqueAVFrame VideoEncoder::convertTensorToAVFrame(
893892
const torch::Tensor& frame,
894893
int frameIndex) {
895-
TORCH_CHECK(frame.is_cpu(), "CPU encoder requires CPU tensors");
896894
// Initialize and cache scaling context if it does not exist
897895
if (!swsContext_) {
898896
swsContext_.reset(sws_getContext(

src/torchcodec/_core/GpuEncoder.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ UniqueAVBufferRef createHardwareDeviceContext(const torch::Device& device) {
7171

7272
// RGB to NV12 color conversion matrices (inverse of YUV to RGB)
7373
// Note: NPP's ColorTwist function apparently expects "limited range"
74-
// coefficient format even when producing full range output. All matrices below
75-
// use the limited range coefficient format (Y with +16 offset) for NPP
74+
// coefficient format even when producing full range output. The matrix below
75+
// uses the limited range coefficient format (Y with +16 offset) for NPP
7676
// compatibility.
7777

7878
// BT.601 limited range (matches FFmpeg default behavior)
@@ -83,7 +83,7 @@ const Npp32f defaultLimitedRangeRgbToNv12[3][4] = {
8383
{-0.148f, -0.291f, 0.439f, 128.0f},
8484
// V = 0.439*R - 0.368*G - 0.071*B + 128 (BT.601 coefficients)
8585
{0.439f, -0.368f, -0.071f, 128.0f}};
86-
} // anonymous namespace
86+
} // namespace
8787

8888
GpuEncoder::GpuEncoder(const torch::Device& device) : device_(device) {
8989
TORCH_CHECK(
@@ -122,7 +122,7 @@ void GpuEncoder::setupHardwareFrameContext(AVCodecContext* codecContext) {
122122

123123
// Always set pixel formats to options that support CUDA encoding.
124124
// TODO-VideoEncoder: Enable user set pixel formats to be set and properly
125-
// converted with npp functions below
125+
// handled with NPP functions below
126126
codecContext->sw_pix_fmt = AV_PIX_FMT_NV12;
127127
codecContext->pix_fmt = AV_PIX_FMT_CUDA;
128128

@@ -150,20 +150,17 @@ UniqueAVFrame GpuEncoder::convertTensorToAVFrame(
150150
[[maybe_unused]] AVPixelFormat targetFormat,
151151
int frameIndex,
152152
AVCodecContext* codecContext) {
153-
TORCH_CHECK(
154-
tensor.is_cuda(),
155-
"Frame tensor is not stored on GPU, but the GPU method convertTensorToAVFrame was called.");
156153
TORCH_CHECK(
157154
tensor.dim() == 3 && tensor.size(0) == 3,
158155
"Expected 3D RGB tensor (CHW format), got shape: ",
159156
tensor.sizes());
160157

161-
// TODO-VideoEncoder: Unify AVFrame creation with CPU version of this method
162158
UniqueAVFrame avFrame(av_frame_alloc());
163159
TORCH_CHECK(avFrame != nullptr, "Failed to allocate AVFrame");
164160
int height = static_cast<int>(tensor.size(1));
165161
int width = static_cast<int>(tensor.size(2));
166162

163+
// TODO-VideoEncoder: Unify AVFrame creation with CPU version of this method
167164
avFrame->format = AV_PIX_FMT_CUDA;
168165
avFrame->height = height;
169166
avFrame->width = width;

test/test_encoders.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1371,5 +1371,4 @@ def test_nvenc_against_ffmpeg_cli(self, tmp_path, format_codec, method):
13711371
assert ffmpeg_frames.shape[0] == encoder_frames.shape[0]
13721372
for ff_frame, enc_frame in zip(ffmpeg_frames, encoder_frames):
13731373
assert psnr(ff_frame, enc_frame) > 25
1374-
assert_tensor_close_on_at_least(ff_frame, enc_frame, percentage=99, atol=10)
13751374
assert_tensor_close_on_at_least(ff_frame, enc_frame, percentage=95, atol=2)

0 commit comments

Comments
 (0)