< Summary - Jellyfin

Information
Class: MediaBrowser.Model.Dlna.ResolutionNormalizer
Assembly: MediaBrowser.Model
File(s): /srv/git/jellyfin/MediaBrowser.Model/Dlna/ResolutionNormalizer.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 38
Coverable lines: 38
Total lines: 85
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 20
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%210%
Normalize(...)0%420200%
GetResolutionConfiguration(...)100%210%

File(s)

/srv/git/jellyfin/MediaBrowser.Model/Dlna/ResolutionNormalizer.cs

#LineLine coverage
 1#nullable disable
 2#pragma warning disable CS1591
 3
 4using System;
 5using System.Linq;
 6
 7namespace MediaBrowser.Model.Dlna
 8{
 9    public static class ResolutionNormalizer
 10    {
 11        // Please note: all bitrate here are in the scale of SDR h264 bitrate at 30fps
 012        private static readonly ResolutionConfiguration[] _configurations =
 013        [
 014            new ResolutionConfiguration(416, 365000),
 015            new ResolutionConfiguration(640, 730000),
 016            new ResolutionConfiguration(768, 1100000),
 017            new ResolutionConfiguration(960, 3000000),
 018            new ResolutionConfiguration(1280, 6000000),
 019            new ResolutionConfiguration(1920, 13500000),
 020            new ResolutionConfiguration(2560, 28000000),
 021            new ResolutionConfiguration(3840, 50000000)
 022        ];
 23
 24        public static ResolutionOptions Normalize(
 25            int? inputBitrate,
 26            int outputBitrate,
 27            int h264EquivalentOutputBitrate,
 28            int? maxWidth,
 29            int? maxHeight,
 30            float? targetFps,
 31            bool isHdr = false) // We are not doing HDR transcoding for now, leave for future use
 32        {
 33            // If the bitrate isn't changing, then don't downscale the resolution
 034            if (inputBitrate.HasValue && outputBitrate >= inputBitrate.Value)
 35            {
 036                if (maxWidth.HasValue || maxHeight.HasValue)
 37                {
 038                    return new ResolutionOptions
 039                    {
 040                        MaxWidth = maxWidth,
 041                        MaxHeight = maxHeight
 042                    };
 43                }
 44            }
 45
 46            // Our reference bitrate is based on SDR h264 at 30fps
 047            var referenceFps = targetFps ?? 30.0f;
 048            var referenceScale = referenceFps <= 30.0f
 049                ? 30.0f / referenceFps
 050                : 1.0f / MathF.Sqrt(referenceFps / 30.0f);
 051            var referenceBitrate = h264EquivalentOutputBitrate * referenceScale;
 52
 053            if (isHdr)
 54            {
 055                referenceBitrate *= 0.8f;
 56            }
 57
 058            var resolutionConfig = GetResolutionConfiguration(Convert.ToInt32(referenceBitrate));
 59
 060            if (resolutionConfig is null)
 61            {
 062                return new ResolutionOptions { MaxWidth = maxWidth, MaxHeight = maxHeight };
 63            }
 64
 065            var originWidthValue = maxWidth;
 66
 067            maxWidth = Math.Min(resolutionConfig.MaxWidth, maxWidth ?? resolutionConfig.MaxWidth);
 068            if (!originWidthValue.HasValue || originWidthValue.Value != maxWidth.Value)
 69            {
 070                maxHeight = null;
 71            }
 72
 073            return new ResolutionOptions
 074            {
 075                MaxWidth = maxWidth,
 076                MaxHeight = maxHeight
 077            };
 78        }
 79
 80        private static ResolutionConfiguration GetResolutionConfiguration(int outputBitrate)
 81        {
 082            return _configurations.FirstOrDefault(config => outputBitrate <= config.MaxBitrate);
 83        }
 84    }
 85}