| | 1 | | using System; |
| | 2 | | using System.Globalization; |
| | 3 | | using System.Text; |
| | 4 | |
|
| | 5 | | namespace 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> |
| | 12 | | public 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 | | { |
| 0 | 50 | | 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 | | { |
| 0 | 60 | | StringBuilder result = new StringBuilder("mp4a", 9); |
| | 61 | |
|
| 0 | 62 | | if (string.Equals(profile, "HE", StringComparison.OrdinalIgnoreCase)) |
| | 63 | | { |
| 0 | 64 | | result.Append(".40.5"); |
| | 65 | | } |
| | 66 | | else |
| | 67 | | { |
| | 68 | | // Default to LC if profile is invalid |
| 0 | 69 | | result.Append(".40.2"); |
| | 70 | | } |
| | 71 | |
|
| 0 | 72 | | 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 | | { |
| 0 | 81 | | 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 | | { |
| 0 | 90 | | 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 | | { |
| 0 | 99 | | 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 | | { |
| 0 | 108 | | 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 | | { |
| 0 | 117 | | 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 | | { |
| 0 | 128 | | StringBuilder result = new StringBuilder("avc1", 11); |
| | 129 | |
|
| 0 | 130 | | if (string.Equals(profile, "high", StringComparison.OrdinalIgnoreCase)) |
| | 131 | | { |
| 0 | 132 | | result.Append(".6400"); |
| | 133 | | } |
| 0 | 134 | | else if (string.Equals(profile, "main", StringComparison.OrdinalIgnoreCase)) |
| | 135 | | { |
| 0 | 136 | | result.Append(".4D40"); |
| | 137 | | } |
| 0 | 138 | | else if (string.Equals(profile, "baseline", StringComparison.OrdinalIgnoreCase)) |
| | 139 | | { |
| 0 | 140 | | result.Append(".42E0"); |
| | 141 | | } |
| | 142 | | else |
| | 143 | | { |
| | 144 | | // Default to constrained baseline if profile is invalid |
| 0 | 145 | | result.Append(".4240"); |
| | 146 | | } |
| | 147 | |
|
| 0 | 148 | | string levelHex = level.ToString("X2", CultureInfo.InvariantCulture); |
| 0 | 149 | | result.Append(levelHex); |
| | 150 | |
|
| 0 | 151 | | 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] |
| 0 | 165 | | StringBuilder result = new StringBuilder("hvc1", 16); |
| | 166 | |
|
| 0 | 167 | | if (string.Equals(profile, "main10", StringComparison.OrdinalIgnoreCase) |
| 0 | 168 | | || string.Equals(profile, "main 10", StringComparison.OrdinalIgnoreCase)) |
| | 169 | | { |
| 0 | 170 | | result.Append(".2.4"); |
| | 171 | | } |
| | 172 | | else |
| | 173 | | { |
| | 174 | | // Default to main if profile is invalid |
| 0 | 175 | | result.Append(".1.4"); |
| | 176 | | } |
| | 177 | |
|
| 0 | 178 | | result.Append(".L") |
| 0 | 179 | | .Append(level) |
| 0 | 180 | | .Append(".B0"); |
| | 181 | |
|
| 0 | 182 | | 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/ |
| 0 | 197 | | StringBuilder result = new StringBuilder("vp09", 13); |
| | 198 | |
|
| 0 | 199 | | var profileString = pixelFormat switch |
| 0 | 200 | | { |
| 0 | 201 | | "yuv420p" => "00", |
| 0 | 202 | | "yuvj420p" => "00", |
| 0 | 203 | | "yuv422p" => "01", |
| 0 | 204 | | "yuv444p" => "01", |
| 0 | 205 | | "yuv420p10le" => "02", |
| 0 | 206 | | "yuv420p12le" => "02", |
| 0 | 207 | | "yuv422p10le" => "03", |
| 0 | 208 | | "yuv422p12le" => "03", |
| 0 | 209 | | "yuv444p10le" => "03", |
| 0 | 210 | | "yuv444p12le" => "03", |
| 0 | 211 | | _ => "00" |
| 0 | 212 | | }; |
| | 213 | |
|
| 0 | 214 | | var lumaPictureSize = width * height; |
| 0 | 215 | | var lumaSampleRate = lumaPictureSize * framerate; |
| 0 | 216 | | var levelString = lumaPictureSize switch |
| 0 | 217 | | { |
| 0 | 218 | | <= 0 => "00", |
| 0 | 219 | | <= 36864 => "10", |
| 0 | 220 | | <= 73728 => "11", |
| 0 | 221 | | <= 122880 => "20", |
| 0 | 222 | | <= 245760 => "21", |
| 0 | 223 | | <= 552960 => "30", |
| 0 | 224 | | <= 983040 => "31", |
| 0 | 225 | | <= 2228224 => lumaSampleRate <= 83558400 ? "40" : "41", |
| 0 | 226 | | <= 8912896 => lumaSampleRate <= 311951360 ? "50" : (lumaSampleRate <= 588251136 ? "51" : "52"), |
| 0 | 227 | | <= 35651584 => lumaSampleRate <= 1176502272 ? "60" : (lumaSampleRate <= 4706009088 ? "61" : "62"), |
| 0 | 228 | | _ => "00" // This should not happen |
| 0 | 229 | | }; |
| | 230 | |
|
| 0 | 231 | | if (bitDepth != 8 |
| 0 | 232 | | && bitDepth != 10 |
| 0 | 233 | | && bitDepth != 12) |
| | 234 | | { |
| | 235 | | // Default to 8 bits |
| 0 | 236 | | bitDepth = 8; |
| | 237 | | } |
| | 238 | |
|
| 0 | 239 | | result.Append('.').Append(profileString).Append('.').Append(levelString); |
| 0 | 240 | | var bitDepthD2 = bitDepth.ToString("D2", CultureInfo.InvariantCulture); |
| 0 | 241 | | result.Append('.') |
| 0 | 242 | | .Append(bitDepthD2); |
| | 243 | |
|
| 0 | 244 | | 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] |
| 0 | 259 | | StringBuilder result = new StringBuilder("av01", 13); |
| | 260 | |
|
| 0 | 261 | | if (string.Equals(profile, "Main", StringComparison.OrdinalIgnoreCase)) |
| | 262 | | { |
| 0 | 263 | | result.Append(".0"); |
| | 264 | | } |
| 0 | 265 | | else if (string.Equals(profile, "High", StringComparison.OrdinalIgnoreCase)) |
| | 266 | | { |
| 0 | 267 | | result.Append(".1"); |
| | 268 | | } |
| 0 | 269 | | else if (string.Equals(profile, "Professional", StringComparison.OrdinalIgnoreCase)) |
| | 270 | | { |
| 0 | 271 | | result.Append(".2"); |
| | 272 | | } |
| | 273 | | else |
| | 274 | | { |
| | 275 | | // Default to Main |
| 0 | 276 | | result.Append(".0"); |
| | 277 | | } |
| | 278 | |
|
| 0 | 279 | | if (level is <= 0 or > 31) |
| | 280 | | { |
| | 281 | | // Default to the maximum defined level 6.3 |
| 0 | 282 | | level = 19; |
| | 283 | | } |
| | 284 | |
|
| 0 | 285 | | if (bitDepth != 8 |
| 0 | 286 | | && bitDepth != 10 |
| 0 | 287 | | && bitDepth != 12) |
| | 288 | | { |
| | 289 | | // Default to 8 bits |
| 0 | 290 | | bitDepth = 8; |
| | 291 | | } |
| | 292 | |
|
| 0 | 293 | | result.Append('.') |
| 0 | 294 | | // Needed to pad it double digits; otherwise, browsers will reject the stream. |
| 0 | 295 | | .AppendFormat(CultureInfo.InvariantCulture, "{0:D2}", level) |
| 0 | 296 | | .Append(tierFlag ? 'H' : 'M'); |
| | 297 | |
|
| 0 | 298 | | string bitDepthD2 = bitDepth.ToString("D2", CultureInfo.InvariantCulture); |
| 0 | 299 | | result.Append('.') |
| 0 | 300 | | .Append(bitDepthD2); |
| | 301 | |
|
| 0 | 302 | | return result.ToString(); |
| | 303 | | } |
| | 304 | | } |