Skip to content

Commit 4516b35

Browse files
author
Daniel Flores
committed
remove qscale, adjust tests
1 parent 266f9f5 commit 4516b35

File tree

2 files changed

+15
-26
lines changed

2 files changed

+15
-26
lines changed

src/torchcodec/_core/Encoder.cpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -629,23 +629,12 @@ void VideoEncoder::initializeEncoder(
629629

630630
// Apply videoStreamOptions
631631
AVDictionary* options = nullptr;
632-
if (videoStreamOptions.crf.has_value() &&
633-
(avCodec->id != AV_CODEC_ID_MPEG4 && avCodec->id != AV_CODEC_ID_FLV1)) {
632+
if (videoStreamOptions.crf.has_value()) {
634633
av_dict_set(
635634
&options,
636635
"crf",
637636
std::to_string(videoStreamOptions.crf.value()).c_str(),
638637
0);
639-
} else {
640-
// For codecs that don't support CRF (mpeg4, flv1),
641-
// use quality-based encoding via global_quality + qscale flag
642-
avCodecContext_->flags |= AV_CODEC_FLAG_QSCALE;
643-
// Reuse of crf below is only intended to work in tests where crf = 0
644-
// Use qmin as lower bound for best possible quality
645-
int qp = videoStreamOptions.crf.value() <= avCodecContext_->qmin
646-
? avCodecContext_->qmin
647-
: videoStreamOptions.crf.value();
648-
avCodecContext_->global_quality = FF_QP2LAMBDA * qp;
649638
}
650639
int status = avcodec_open2(avCodecContext_.get(), avCodec, &options);
651640
av_dict_free(&options);

test/test_ops.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,19 +1384,20 @@ def decode(self, file_path) -> torch.Tensor:
13841384
frames, *_ = get_frames_in_range(decoder, start=0, stop=60)
13851385
return frames
13861386

1387-
@pytest.mark.parametrize("format", ("mov", "mp4", "avi", "mkv", "webm", "flv"))
1387+
@pytest.mark.parametrize("format", ("mov", "mp4", "mkv", "webm"))
13881388
def test_video_encoder_round_trip(self, tmp_path, format):
13891389
# Test that decode(encode(decode(asset))) == decode(asset)
13901390
ffmpeg_version = get_ffmpeg_major_version()
1391-
if format == "webm":
1392-
if ffmpeg_version == 4:
1393-
pytest.skip(
1394-
"Codec for webm is not available in the FFmpeg4 installation."
1395-
)
1396-
if IS_WINDOWS and ffmpeg_version in (6, 7):
1397-
pytest.skip(
1398-
"Codec for webm is not available in the FFmpeg6/7 installation on Windows."
1399-
)
1391+
# In FFmpeg6, the default codec's best pixel format is lossy for all container formats but webm.
1392+
# As a result, we skip the round trip test.
1393+
if ffmpeg_version == 6 and format != "webm":
1394+
pytest.skip(
1395+
f"FFmpeg6 defaults to lossy encoding for {format}, skipping round-trip test."
1396+
)
1397+
if format == "webm" and (
1398+
ffmpeg_version == 4 or (IS_WINDOWS and ffmpeg_version in (6, 7))
1399+
):
1400+
pytest.skip("Codec for webm is not available in this FFmpeg installation.")
14001401
asset = TEST_SRC_2_720P
14011402
source_frames = self.decode(str(asset.path)).data
14021403

@@ -1448,9 +1449,8 @@ def test_video_encoder_against_ffmpeg_cli(self, tmp_path, format):
14481449
ffmpeg_encoded_path = str(tmp_path / f"ffmpeg_output.{format}")
14491450
crf = 0
14501451
quality_params = ["-crf", str(crf)]
1451-
# Some codecs (ex. MPEG4) do not support CRF, qscale is used for lossless encoding.
1452-
# Flags not supported by the selected codec will be ignored, so we set both crf and qscale.
1453-
quality_params += ["-q:v", str(crf)]
1452+
# Some codecs (ex. MPEG4) do not support CRF.
1453+
# Flags not supported by the selected codec will be ignored.
14541454
ffmpeg_cmd = [
14551455
"ffmpeg",
14561456
"-y",
@@ -1486,7 +1486,7 @@ def test_video_encoder_against_ffmpeg_cli(self, tmp_path, format):
14861486
# If FFmpeg selects a codec or pixel format that uses qscale (not crf),
14871487
# the VideoEncoder outputs *slightly* different frames.
14881488
# There may be additional subtle differences in the encoder.
1489-
percentage = 97 if ffmpeg_version == 6 or format in ("avi") else 99
1489+
percentage = 95 if ffmpeg_version == 6 or format in ("avi") else 99
14901490

14911491
# Check that PSNR between both encoded versions is high
14921492
for ff_frame, enc_frame in zip(ffmpeg_frames, encoder_frames):

0 commit comments

Comments
 (0)