From bbc40674a85ccb3b6951fd45cd8e218839100a54 Mon Sep 17 00:00:00 2001 From: nyanmisaka Date: Fri, 2 Oct 2026 02:47:51 +0800 Subject: [PATCH] Fix video quality at low bitrates when reducing bit depth 10-bit encoded video offers higher efficiency. When reducing the bit depth, compensation is required to maintain quality at low bitrates. A recent change to the bitrate parsing logic exposed this issue, as it prioritizes reading the video stream bitrate from MKV tags rather than relying on the bitrate of the entire container. Signed-off-by: nyanmisaka --- .../MediaEncoding/EncodingHelper.cs | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/MediaBrowser.Controller/MediaEncoding/EncodingHelper.cs b/MediaBrowser.Controller/MediaEncoding/EncodingHelper.cs index 58d7495fa4..af065ee456 100644 --- a/MediaBrowser.Controller/MediaEncoding/EncodingHelper.cs +++ b/MediaBrowser.Controller/MediaEncoding/EncodingHelper.cs @@ -2693,6 +2693,28 @@ namespace MediaBrowser.Controller.MediaEncoding return reasons; } + private static int GetBitDepthCompensatedBitrate(int bitrate, MediaStream videoStream, int targetBitDepth) + { + if (targetBitDepth >= 10 + || (videoStream.BitDepth is int bitDepth && bitDepth <= targetBitDepth) + || videoStream.BitRate is not int sourceBitrate) + { + return bitrate; + } + + // Bit depth reduction requires bit rate compensation because + // 10-bit encoded video is significantly more efficient. + var factor = sourceBitrate switch + { + <= 4_000_000 => 1.5, + <= 8_000_000 => 1.2, + <= 12_000_000 => 1.1, + _ => 1.05 + }; + + return Convert.ToInt32(bitrate * factor); + } + public int GetVideoBitrateParamValue(BaseEncodingJobOptions request, MediaStream videoStream, string outputVideoCodec) { var bitrate = request.VideoBitRate; @@ -2715,6 +2737,7 @@ namespace MediaBrowser.Controller.MediaEncoding if (bitrate.HasValue) { var inputVideoCodec = videoStream.Codec; + bitrate = GetBitDepthCompensatedBitrate(bitrate.Value, videoStream, 8); bitrate = ScaleBitrate(bitrate.Value, inputVideoCodec, outputVideoCodec); // If a max bitrate was requested, don't let the scaled bitrate exceed it