Files
wjones af1152b001 Refactor: standardize namespace and using directive style
Refactored all C# test files to use explicit namespace declarations and moved all using directives inside the namespace block for consistency. Updated assembly info and cache files to reflect the new build. Adjusted MvcTestingAppManifest.json to use fully qualified assembly names. No functional changes; all updates are related to code style, organization, and project metadata.
2026-02-20 16:26:53 -05:00

99 lines
3.7 KiB
C#

// <copyright file="ResolutionNormalizer.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
#nullable disable
#pragma warning disable CS1591
using global::System;
using global::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)
];
/// <summary>
/// Normalizes resolution options based on bitrate and dimensions.
/// </summary>
/// <param name="inputBitrate">The input bitrate.</param>
/// <param name="outputBitrate">The output bitrate.</param>
/// <param name="h264EquivalentOutputBitrate">The H264 equivalent output bitrate.</param>
/// <param name="maxWidth">The maximum width.</param>
/// <param name="maxHeight">The maximum height.</param>
/// <param name="targetFps">The target frames per second.</param>
/// <param name="isHdr">Whether the content is HDR.</param>
/// <returns>The normalized resolution options.</returns>
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,
};
}
}
// Our reference bitrate is based on SDR h264 at 30fps
var referenceFps = targetFps ?? 30.0f;
var referenceScale = referenceFps <= 30.0f
? 30.0f / referenceFps
: 1.0f / MathF.Sqrt(referenceFps / 30.0f);
var referenceBitrate = h264EquivalentOutputBitrate * referenceScale;
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);
}
}