Skip to content

Commit df726f5

Browse files
committed
test cleanup, qp, failing on ffmpeg4/cuda12.6
1 parent eff78e4 commit df726f5

File tree

3 files changed

+16
-26
lines changed

3 files changed

+16
-26
lines changed

src/torchcodec/_core/Encoder.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,9 +677,11 @@ void VideoEncoder::initializeEncoder(
677677
// Apply videoStreamOptions
678678
AVDictionary* options = nullptr;
679679
if (videoStreamOptions.crf.has_value()) {
680+
// nvenc encoders use qp, others use crf (for C++ tests)
681+
std::string_view quality_param = (strstr(avCodec->name, "nvenc") == nullptr) ? "crf" : "qp";
680682
av_dict_set(
681683
&options,
682-
"crf",
684+
quality_param.data(),
683685
std::to_string(videoStreamOptions.crf.value()).c_str(),
684686
0);
685687
}

test/test_encoders.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -645,11 +645,9 @@ def test_contiguity(self, method, tmp_path, device):
645645
# frame tensors, one is contiguous while the other is non-contiguous.
646646

647647
num_frames, channels, height, width = 5, 3, 256, 256
648-
contiguous_frames = (
649-
(torch.rand(num_frames, channels, height, width) * 255)
650-
.to(torch.uint8)
651-
.contiguous()
652-
)
648+
contiguous_frames = torch.randint(
649+
0, 256, size=(num_frames, channels, height, width), dtype=torch.uint8
650+
).contiguous()
653651
assert contiguous_frames.is_contiguous()
654652

655653
# Permute NCHW to NHWC, then update the memory layout, then permute back

test/test_ops.py

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,20 +1212,15 @@ def test_video_encoder_round_trip(self, tmp_path, format, method, device):
12121212
assert source_frames.shape == round_trip_frames.shape
12131213
assert source_frames.dtype == round_trip_frames.dtype
12141214

1215-
# If FFmpeg selects a codec or pixel format that does lossy encoding, assert 99% of pixels
1216-
# are within a higher tolerance.
1217-
if ffmpeg_version == 6 or device == "cuda":
1218-
atol = 15
1219-
percentage = 98 if device == "cuda" else 99
1215+
# If encoding on GPU, assert 99% of pixels are within a strict tolerance.
1216+
if device == "cuda":
1217+
assert_close = partial(assert_tensor_close_on_at_least, atol=3, percentage=99)
1218+
else:
12201219
assert_close = partial(
1221-
assert_tensor_close_on_at_least, percentage=percentage
1220+
torch.testing.assert_close, atol=2, rtol=0
12221221
)
1223-
else:
1224-
assert_close = torch.testing.assert_close
1225-
atol = 2
12261222
for s_frame, rt_frame in zip(source_frames, round_trip_frames):
1227-
assert psnr(s_frame, rt_frame) > 30
1228-
assert_close(s_frame, rt_frame, atol=atol, rtol=0)
1223+
assert_close(s_frame, rt_frame)
12291224

12301225
@pytest.mark.parametrize(
12311226
"format",
@@ -1340,14 +1335,10 @@ def test_video_encoder_against_ffmpeg_cli(self, tmp_path, format, device):
13401335

13411336
if codec_str:
13421337
ffmpeg_cmd.extend(codec_str.split())
1338+
quality_param = "qp" if device == "cuda" else "crf"
13431339

1344-
ffmpeg_cmd.extend(
1345-
[
1346-
"-crf",
1347-
str(crf),
1348-
ffmpeg_encoded_path,
1349-
]
1350-
)
1340+
ffmpeg_cmd.extend([f"-{quality_param}", str(crf)])
1341+
ffmpeg_cmd.extend([ffmpeg_encoded_path])
13511342
subprocess.run(ffmpeg_cmd, check=True)
13521343

13531344
# Encode with our video encoder
@@ -1372,8 +1363,7 @@ def test_video_encoder_against_ffmpeg_cli(self, tmp_path, format, device):
13721363

13731364
# Check that PSNR between both encoded versions is high
13741365
for ff_frame, enc_frame in zip(ffmpeg_frames, encoder_frames):
1375-
res = psnr(ff_frame, enc_frame)
1376-
assert res > 30
1366+
assert psnr(ff_frame, enc_frame) > 30
13771367
assert_tensor_close_on_at_least(
13781368
ff_frame, enc_frame, percentage=percentage, atol=2
13791369
)

0 commit comments

Comments
 (0)