< Summary - Jellyfin

Information
Class: Jellyfin.Api.Helpers.HlsCodecStringHelpers
Assembly: Jellyfin.Api
File(s): /srv/git/jellyfin/Jellyfin.Api/Helpers/HlsCodecStringHelpers.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 93
Coverable lines: 93
Total lines: 304
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 108
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
GetMP3String()100%210%
GetAACString(...)0%620%
GetAC3String()100%210%
GetEAC3String()100%210%
GetFLACString()100%210%
GetALACString()100%210%
GetOPUSString()100%210%
GetH264String(...)0%4260%
GetH265String(...)0%2040%
GetVp9String(...)0%5852760%
GetAv1String(...)0%420200%

File(s)

/srv/git/jellyfin/Jellyfin.Api/Helpers/HlsCodecStringHelpers.cs

#LineLine coverage
 1using System;
 2using System.Globalization;
 3using System.Text;
 4
 5namespace Jellyfin.Api.Helpers;
 6
 7/// <summary>
 8/// Helpers to generate HLS codec strings according to
 9/// <a href="https://datatracker.ietf.org/doc/html/rfc6381#section-3.3">RFC 6381 section 3.3</a>
 10/// and the <a href="https://mp4ra.org">MP4 Registration Authority</a>.
 11/// </summary>
 12public static class HlsCodecStringHelpers
 13{
 14    /// <summary>
 15    /// Codec name for MP3.
 16    /// </summary>
 17    public const string MP3 = "mp4a.40.34";
 18
 19    /// <summary>
 20    /// Codec name for AC-3.
 21    /// </summary>
 22    public const string AC3 = "ac-3";
 23
 24    /// <summary>
 25    /// Codec name for E-AC-3.
 26    /// </summary>
 27    public const string EAC3 = "ec-3";
 28
 29    /// <summary>
 30    /// Codec name for FLAC.
 31    /// </summary>
 32    public const string FLAC = "fLaC";
 33
 34    /// <summary>
 35    /// Codec name for ALAC.
 36    /// </summary>
 37    public const string ALAC = "alac";
 38
 39    /// <summary>
 40    /// Codec name for OPUS.
 41    /// </summary>
 42    public const string OPUS = "Opus";
 43
 44    /// <summary>
 45    /// Gets a MP3 codec string.
 46    /// </summary>
 47    /// <returns>MP3 codec string.</returns>
 48    public static string GetMP3String()
 49    {
 050        return MP3;
 51    }
 52
 53    /// <summary>
 54    /// Gets an AAC codec string.
 55    /// </summary>
 56    /// <param name="profile">AAC profile.</param>
 57    /// <returns>AAC codec string.</returns>
 58    public static string GetAACString(string? profile)
 59    {
 060        StringBuilder result = new StringBuilder("mp4a", 9);
 61
 062        if (string.Equals(profile, "HE", StringComparison.OrdinalIgnoreCase))
 63        {
 064            result.Append(".40.5");
 65        }
 66        else
 67        {
 68            // Default to LC if profile is invalid
 069            result.Append(".40.2");
 70        }
 71
 072        return result.ToString();
 73    }
 74
 75    /// <summary>
 76    /// Gets an AC-3 codec string.
 77    /// </summary>
 78    /// <returns>AC-3 codec string.</returns>
 79    public static string GetAC3String()
 80    {
 081        return AC3;
 82    }
 83
 84    /// <summary>
 85    /// Gets an E-AC-3 codec string.
 86    /// </summary>
 87    /// <returns>E-AC-3 codec string.</returns>
 88    public static string GetEAC3String()
 89    {
 090        return EAC3;
 91    }
 92
 93    /// <summary>
 94    /// Gets an FLAC codec string.
 95    /// </summary>
 96    /// <returns>FLAC codec string.</returns>
 97    public static string GetFLACString()
 98    {
 099        return FLAC;
 100    }
 101
 102    /// <summary>
 103    /// Gets an ALAC codec string.
 104    /// </summary>
 105    /// <returns>ALAC codec string.</returns>
 106    public static string GetALACString()
 107    {
 0108        return ALAC;
 109    }
 110
 111    /// <summary>
 112    /// Gets an OPUS codec string.
 113    /// </summary>
 114    /// <returns>OPUS codec string.</returns>
 115    public static string GetOPUSString()
 116    {
 0117        return OPUS;
 118    }
 119
 120    /// <summary>
 121    /// Gets a H.264 codec string.
 122    /// </summary>
 123    /// <param name="profile">H.264 profile.</param>
 124    /// <param name="level">H.264 level.</param>
 125    /// <returns>H.264 string.</returns>
 126    public static string GetH264String(string? profile, int level)
 127    {
 0128        StringBuilder result = new StringBuilder("avc1", 11);
 129
 0130        if (string.Equals(profile, "high", StringComparison.OrdinalIgnoreCase))
 131        {
 0132            result.Append(".6400");
 133        }
 0134        else if (string.Equals(profile, "main", StringComparison.OrdinalIgnoreCase))
 135        {
 0136            result.Append(".4D40");
 137        }
 0138        else if (string.Equals(profile, "baseline", StringComparison.OrdinalIgnoreCase))
 139        {
 0140            result.Append(".42E0");
 141        }
 142        else
 143        {
 144            // Default to constrained baseline if profile is invalid
 0145            result.Append(".4240");
 146        }
 147
 0148        string levelHex = level.ToString("X2", CultureInfo.InvariantCulture);
 0149        result.Append(levelHex);
 150
 0151        return result.ToString();
 152    }
 153
 154    /// <summary>
 155    /// Gets a H.265 codec string.
 156    /// </summary>
 157    /// <param name="profile">H.265 profile.</param>
 158    /// <param name="level">H.265 level.</param>
 159    /// <returns>H.265 string.</returns>
 160    public static string GetH265String(string? profile, int level)
 161    {
 162        // The h265 syntax is a bit of a mystery at the time this comment was written.
 163        // This is what I've found through various sources:
 164        // FORMAT: [codecTag].[profile].[constraint?].L[level * 30].[UNKNOWN]
 0165        StringBuilder result = new StringBuilder("hvc1", 16);
 166
 0167        if (string.Equals(profile, "main10", StringComparison.OrdinalIgnoreCase)
 0168            || string.Equals(profile, "main 10", StringComparison.OrdinalIgnoreCase))
 169        {
 0170            result.Append(".2.4");
 171        }
 172        else
 173        {
 174            // Default to main if profile is invalid
 0175            result.Append(".1.4");
 176        }
 177
 0178        result.Append(".L")
 0179            .Append(level)
 0180            .Append(".B0");
 181
 0182        return result.ToString();
 183    }
 184
 185    /// <summary>
 186    /// Gets a VP9 codec string.
 187    /// </summary>
 188    /// <param name="width">Video width.</param>
 189    /// <param name="height">Video height.</param>
 190    /// <param name="pixelFormat">Video pixel format.</param>
 191    /// <param name="framerate">Video framerate.</param>
 192    /// <param name="bitDepth">Video bitDepth.</param>
 193    /// <returns>The VP9 codec string.</returns>
 194    public static string GetVp9String(int width, int height, string pixelFormat, float framerate, int bitDepth)
 195    {
 196        // refer: https://www.webmproject.org/vp9/mp4/
 0197        StringBuilder result = new StringBuilder("vp09", 13);
 198
 0199        var profileString = pixelFormat switch
 0200        {
 0201            "yuv420p" => "00",
 0202            "yuvj420p" => "00",
 0203            "yuv422p" => "01",
 0204            "yuv444p" => "01",
 0205            "yuv420p10le" => "02",
 0206            "yuv420p12le" => "02",
 0207            "yuv422p10le" => "03",
 0208            "yuv422p12le" => "03",
 0209            "yuv444p10le" => "03",
 0210            "yuv444p12le" => "03",
 0211            _ => "00"
 0212        };
 213
 0214        var lumaPictureSize = width * height;
 0215        var lumaSampleRate = lumaPictureSize * framerate;
 0216        var levelString = lumaPictureSize switch
 0217        {
 0218            <= 0 => "00",
 0219            <= 36864 => "10",
 0220            <= 73728 => "11",
 0221            <= 122880 => "20",
 0222            <= 245760 => "21",
 0223            <= 552960 => "30",
 0224            <= 983040 => "31",
 0225            <= 2228224 => lumaSampleRate <= 83558400 ? "40" : "41",
 0226            <= 8912896 => lumaSampleRate <= 311951360 ? "50" : (lumaSampleRate <= 588251136 ? "51" : "52"),
 0227            <= 35651584 => lumaSampleRate <= 1176502272 ? "60" : (lumaSampleRate <= 4706009088 ? "61" : "62"),
 0228            _ => "00" // This should not happen
 0229        };
 230
 0231        if (bitDepth != 8
 0232            && bitDepth != 10
 0233            && bitDepth != 12)
 234        {
 235            // Default to 8 bits
 0236            bitDepth = 8;
 237        }
 238
 0239        result.Append('.').Append(profileString).Append('.').Append(levelString);
 0240        var bitDepthD2 = bitDepth.ToString("D2", CultureInfo.InvariantCulture);
 0241        result.Append('.')
 0242            .Append(bitDepthD2);
 243
 0244        return result.ToString();
 245    }
 246
 247    /// <summary>
 248    /// Gets an AV1 codec string.
 249    /// </summary>
 250    /// <param name="profile">AV1 profile.</param>
 251    /// <param name="level">AV1 level.</param>
 252    /// <param name="tierFlag">AV1 tier flag.</param>
 253    /// <param name="bitDepth">AV1 bit depth.</param>
 254    /// <returns>The AV1 codec string.</returns>
 255    public static string GetAv1String(string? profile, int level, bool tierFlag, int bitDepth)
 256    {
 257        // https://aomediacodec.github.io/av1-isobmff/#codecsparam
 258        // FORMAT: [codecTag].[profile].[level][tier].[bitDepth]
 0259        StringBuilder result = new StringBuilder("av01", 13);
 260
 0261        if (string.Equals(profile, "Main", StringComparison.OrdinalIgnoreCase))
 262        {
 0263            result.Append(".0");
 264        }
 0265        else if (string.Equals(profile, "High", StringComparison.OrdinalIgnoreCase))
 266        {
 0267            result.Append(".1");
 268        }
 0269        else if (string.Equals(profile, "Professional", StringComparison.OrdinalIgnoreCase))
 270        {
 0271            result.Append(".2");
 272        }
 273        else
 274        {
 275            // Default to Main
 0276            result.Append(".0");
 277        }
 278
 0279        if (level is <= 0 or > 31)
 280        {
 281            // Default to the maximum defined level 6.3
 0282            level = 19;
 283        }
 284
 0285        if (bitDepth != 8
 0286            && bitDepth != 10
 0287            && bitDepth != 12)
 288        {
 289            // Default to 8 bits
 0290            bitDepth = 8;
 291        }
 292
 0293        result.Append('.')
 0294            // Needed to pad it double digits; otherwise, browsers will reject the stream.
 0295            .AppendFormat(CultureInfo.InvariantCulture, "{0:D2}", level)
 0296            .Append(tierFlag ? 'H' : 'M');
 297
 0298        string bitDepthD2 = bitDepth.ToString("D2", CultureInfo.InvariantCulture);
 0299        result.Append('.')
 0300            .Append(bitDepthD2);
 301
 0302        return result.ToString();
 303    }
 304}