From 20a1da18550ed0f61737f4790f04a732ec037b2e Mon Sep 17 00:00:00 2001 From: gnattu Date: Thu, 16 May 2024 11:49:41 +0800 Subject: [PATCH 1/2] fix quality parameter for vaapi Hardware encoders has different expectations about quality input. VAAPI's mjpeg encoder excepts JPEG quality divided by QP2LAMBDA as input. Signed-off-by: gnattu --- MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs b/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs index 39431a9fc6..47e6564042 100644 --- a/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs +++ b/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs @@ -871,6 +871,15 @@ namespace MediaBrowser.MediaEncoding.Encoder throw new InvalidOperationException("Empty or invalid input argument."); } + float? encoderQuality = qualityScale; + if (vidEncoder.Contains("vaapi", StringComparison.InvariantCultureIgnoreCase)) + { + // vaapi's mjpeg encoder uses jpeg quality divided by QP2LAMBDA (118) as input, instead of ffmpeg defined qscale + // ffmpeg qscale is a value from 1-31, with 1 being best quality and 31 being worst + // jpeg quality is a value from 0-100, with 0 being worst quality and 100 being best + encoderQuality = (100 - ((qualityScale - 1) * (100 / 30))) / 118; + } + // Output arguments var targetDirectory = Path.Combine(_configurationManager.ApplicationPaths.TempDirectory, Guid.NewGuid().ToString("N")); Directory.CreateDirectory(targetDirectory); @@ -884,7 +893,7 @@ namespace MediaBrowser.MediaEncoding.Encoder filterParam, outputThreads.GetValueOrDefault(_threads), vidEncoder, - qualityScale.HasValue ? "-qscale:v " + qualityScale.Value.ToString(CultureInfo.InvariantCulture) + " " : string.Empty, + qualityScale.HasValue ? "-qscale:v " + encoderQuality.Value.ToString(CultureInfo.InvariantCulture) + " " : string.Empty, "image2", outputPath); From 9b65d243a8e2686997404bf4799b215ca06e3f5e Mon Sep 17 00:00:00 2001 From: gnattu Date: Thu, 16 May 2024 20:18:36 +0800 Subject: [PATCH 2/2] Use OrdinalIgnoreCase comparison Signed-off-by: gnattu --- MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs b/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs index 47e6564042..8ea0f58ea3 100644 --- a/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs +++ b/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs @@ -872,7 +872,7 @@ namespace MediaBrowser.MediaEncoding.Encoder } float? encoderQuality = qualityScale; - if (vidEncoder.Contains("vaapi", StringComparison.InvariantCultureIgnoreCase)) + if (vidEncoder.Contains("vaapi", StringComparison.OrdinalIgnoreCase)) { // vaapi's mjpeg encoder uses jpeg quality divided by QP2LAMBDA (118) as input, instead of ffmpeg defined qscale // ffmpeg qscale is a value from 1-31, with 1 being best quality and 31 being worst