aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Model/Dlna/ResolutionNormalizer.cs
blob: 1a636b2403c2d3628eafe49b816187b3ef1acfe4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#nullable disable
#pragma warning disable CS1591

using System;
using System.Linq;

namespace MediaBrowser.Model.Dlna
{
    public static class ResolutionNormalizer
    {
        // Please note: all bitrate here are in the scale of SDR h264 bitrate at 30fps
        private static readonly ResolutionConfiguration[] _configurations =
        [
            new ResolutionConfiguration(416, 365000),
            new ResolutionConfiguration(640, 730000),
            new ResolutionConfiguration(768, 1100000),
            new ResolutionConfiguration(960, 3000000),
            new ResolutionConfiguration(1280, 6000000),
            new ResolutionConfiguration(1920, 13500000),
            new ResolutionConfiguration(2560, 28000000),
            new ResolutionConfiguration(3840, 50000000)
        ];

        public static ResolutionOptions Normalize(
            int? inputBitrate,
            int outputBitrate,
            int h264EquivalentOutputBitrate,
            int? maxWidth,
            int? maxHeight,
            float? targetFps,
            bool isHdr = false) // We are not doing HDR transcoding for now, leave for future use
        {
            // If the bitrate isn't changing, then don't downscale the resolution
            if (inputBitrate.HasValue && outputBitrate >= inputBitrate.Value)
            {
                if (maxWidth.HasValue || maxHeight.HasValue)
                {
                    return new ResolutionOptions
                    {
                        MaxWidth = maxWidth,
                        MaxHeight = maxHeight
                    };
                }
            }

            var referenceBitrate = h264EquivalentOutputBitrate * (30.0f / (targetFps ?? 30.0f));

            if (isHdr)
            {
                referenceBitrate *= 0.8f;
            }

            var resolutionConfig = GetResolutionConfiguration(Convert.ToInt32(referenceBitrate));

            if (resolutionConfig is null)
            {
                return new ResolutionOptions { MaxWidth = maxWidth, MaxHeight = maxHeight };
            }

            var originWidthValue = maxWidth;

            maxWidth = Math.Min(resolutionConfig.MaxWidth, maxWidth ?? resolutionConfig.MaxWidth);
            if (!originWidthValue.HasValue || originWidthValue.Value != maxWidth.Value)
            {
                maxHeight = null;
            }

            return new ResolutionOptions
            {
                MaxWidth = maxWidth,
                MaxHeight = maxHeight
            };
        }

        private static ResolutionConfiguration GetResolutionConfiguration(int outputBitrate)
        {
            return _configurations.FirstOrDefault(config => outputBitrate <= config.MaxBitrate);
        }
    }
}