HlsCodecStringHelpers.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. using System;
  2. using System.Globalization;
  3. using System.Text;
  4. namespace Jellyfin.Api.Helpers;
  5. /// <summary>
  6. /// Helpers to generate HLS codec strings according to
  7. /// <a href="https://datatracker.ietf.org/doc/html/rfc6381#section-3.3">RFC 6381 section 3.3</a>
  8. /// and the <a href="https://mp4ra.org">MP4 Registration Authority</a>.
  9. /// </summary>
  10. public static class HlsCodecStringHelpers
  11. {
  12. /// <summary>
  13. /// Codec name for MP3.
  14. /// </summary>
  15. public const string MP3 = "mp4a.40.34";
  16. /// <summary>
  17. /// Codec name for AC-3.
  18. /// </summary>
  19. public const string AC3 = "ac-3";
  20. /// <summary>
  21. /// Codec name for E-AC-3.
  22. /// </summary>
  23. public const string EAC3 = "ec-3";
  24. /// <summary>
  25. /// Codec name for FLAC.
  26. /// </summary>
  27. public const string FLAC = "fLaC";
  28. /// <summary>
  29. /// Codec name for ALAC.
  30. /// </summary>
  31. public const string ALAC = "alac";
  32. /// <summary>
  33. /// Codec name for OPUS.
  34. /// </summary>
  35. public const string OPUS = "Opus";
  36. /// <summary>
  37. /// Gets a MP3 codec string.
  38. /// </summary>
  39. /// <returns>MP3 codec string.</returns>
  40. public static string GetMP3String()
  41. {
  42. return MP3;
  43. }
  44. /// <summary>
  45. /// Gets an AAC codec string.
  46. /// </summary>
  47. /// <param name="profile">AAC profile.</param>
  48. /// <returns>AAC codec string.</returns>
  49. public static string GetAACString(string? profile)
  50. {
  51. StringBuilder result = new StringBuilder("mp4a", 9);
  52. if (string.Equals(profile, "HE", StringComparison.OrdinalIgnoreCase))
  53. {
  54. result.Append(".40.5");
  55. }
  56. else
  57. {
  58. // Default to LC if profile is invalid
  59. result.Append(".40.2");
  60. }
  61. return result.ToString();
  62. }
  63. /// <summary>
  64. /// Gets an AC-3 codec string.
  65. /// </summary>
  66. /// <returns>AC-3 codec string.</returns>
  67. public static string GetAC3String()
  68. {
  69. return AC3;
  70. }
  71. /// <summary>
  72. /// Gets an E-AC-3 codec string.
  73. /// </summary>
  74. /// <returns>E-AC-3 codec string.</returns>
  75. public static string GetEAC3String()
  76. {
  77. return EAC3;
  78. }
  79. /// <summary>
  80. /// Gets an FLAC codec string.
  81. /// </summary>
  82. /// <returns>FLAC codec string.</returns>
  83. public static string GetFLACString()
  84. {
  85. return FLAC;
  86. }
  87. /// <summary>
  88. /// Gets an ALAC codec string.
  89. /// </summary>
  90. /// <returns>ALAC codec string.</returns>
  91. public static string GetALACString()
  92. {
  93. return ALAC;
  94. }
  95. /// <summary>
  96. /// Gets an OPUS codec string.
  97. /// </summary>
  98. /// <returns>OPUS codec string.</returns>
  99. public static string GetOPUSString()
  100. {
  101. return OPUS;
  102. }
  103. /// <summary>
  104. /// Gets a H.264 codec string.
  105. /// </summary>
  106. /// <param name="profile">H.264 profile.</param>
  107. /// <param name="level">H.264 level.</param>
  108. /// <returns>H.264 string.</returns>
  109. public static string GetH264String(string? profile, int level)
  110. {
  111. StringBuilder result = new StringBuilder("avc1", 11);
  112. if (string.Equals(profile, "high", StringComparison.OrdinalIgnoreCase))
  113. {
  114. result.Append(".6400");
  115. }
  116. else if (string.Equals(profile, "main", StringComparison.OrdinalIgnoreCase))
  117. {
  118. result.Append(".4D40");
  119. }
  120. else if (string.Equals(profile, "baseline", StringComparison.OrdinalIgnoreCase))
  121. {
  122. result.Append(".42E0");
  123. }
  124. else
  125. {
  126. // Default to constrained baseline if profile is invalid
  127. result.Append(".4240");
  128. }
  129. string levelHex = level.ToString("X2", CultureInfo.InvariantCulture);
  130. result.Append(levelHex);
  131. return result.ToString();
  132. }
  133. /// <summary>
  134. /// Gets a H.265 codec string.
  135. /// </summary>
  136. /// <param name="profile">H.265 profile.</param>
  137. /// <param name="level">H.265 level.</param>
  138. /// <returns>H.265 string.</returns>
  139. public static string GetH265String(string? profile, int level)
  140. {
  141. // The h265 syntax is a bit of a mystery at the time this comment was written.
  142. // This is what I've found through various sources:
  143. // FORMAT: [codecTag].[profile].[constraint?].L[level * 30].[UNKNOWN]
  144. StringBuilder result = new StringBuilder("hvc1", 16);
  145. if (string.Equals(profile, "main10", StringComparison.OrdinalIgnoreCase)
  146. || string.Equals(profile, "main 10", StringComparison.OrdinalIgnoreCase))
  147. {
  148. result.Append(".2.4");
  149. }
  150. else
  151. {
  152. // Default to main if profile is invalid
  153. result.Append(".1.4");
  154. }
  155. result.Append(".L")
  156. .Append(level)
  157. .Append(".B0");
  158. return result.ToString();
  159. }
  160. /// <summary>
  161. /// Gets a VP9 codec string.
  162. /// </summary>
  163. /// <param name="width">Video width.</param>
  164. /// <param name="height">Video height.</param>
  165. /// <param name="pixelFormat">Video pixel format.</param>
  166. /// <param name="framerate">Video framerate.</param>
  167. /// <param name="bitDepth">Video bitDepth.</param>
  168. /// <returns>The VP9 codec string.</returns>
  169. public static string GetVp9String(int width, int height, string pixelFormat, float framerate, int bitDepth)
  170. {
  171. // refer: https://www.webmproject.org/vp9/mp4/
  172. StringBuilder result = new StringBuilder("vp09", 13);
  173. var profileString = pixelFormat switch
  174. {
  175. "yuv420p" => "00",
  176. "yuvj420p" => "00",
  177. "yuv422p" => "01",
  178. "yuv444p" => "01",
  179. "yuv420p10le" => "02",
  180. "yuv420p12le" => "02",
  181. "yuv422p10le" => "03",
  182. "yuv422p12le" => "03",
  183. "yuv444p10le" => "03",
  184. "yuv444p12le" => "03",
  185. _ => "00"
  186. };
  187. var lumaPictureSize = width * height;
  188. var lumaSampleRate = lumaPictureSize * framerate;
  189. var levelString = lumaPictureSize switch
  190. {
  191. <= 0 => "00",
  192. <= 36864 => "10",
  193. <= 73728 => "11",
  194. <= 122880 => "20",
  195. <= 245760 => "21",
  196. <= 552960 => "30",
  197. <= 983040 => "31",
  198. <= 2228224 => lumaSampleRate <= 83558400 ? "40" : "41",
  199. <= 8912896 => lumaSampleRate <= 311951360 ? "50" : (lumaSampleRate <= 588251136 ? "51" : "52"),
  200. <= 35651584 => lumaSampleRate <= 1176502272 ? "60" : (lumaSampleRate <= 4706009088 ? "61" : "62"),
  201. _ => "00" // This should not happen
  202. };
  203. if (bitDepth != 8
  204. && bitDepth != 10
  205. && bitDepth != 12)
  206. {
  207. // Default to 8 bits
  208. bitDepth = 8;
  209. }
  210. result.Append('.').Append(profileString).Append('.').Append(levelString);
  211. var bitDepthD2 = bitDepth.ToString("D2", CultureInfo.InvariantCulture);
  212. result.Append('.')
  213. .Append(bitDepthD2);
  214. return result.ToString();
  215. }
  216. /// <summary>
  217. /// Gets an AV1 codec string.
  218. /// </summary>
  219. /// <param name="profile">AV1 profile.</param>
  220. /// <param name="level">AV1 level.</param>
  221. /// <param name="tierFlag">AV1 tier flag.</param>
  222. /// <param name="bitDepth">AV1 bit depth.</param>
  223. /// <returns>The AV1 codec string.</returns>
  224. public static string GetAv1String(string? profile, int level, bool tierFlag, int bitDepth)
  225. {
  226. // https://aomediacodec.github.io/av1-isobmff/#codecsparam
  227. // FORMAT: [codecTag].[profile].[level][tier].[bitDepth]
  228. StringBuilder result = new StringBuilder("av01", 13);
  229. if (string.Equals(profile, "Main", StringComparison.OrdinalIgnoreCase))
  230. {
  231. result.Append(".0");
  232. }
  233. else if (string.Equals(profile, "High", StringComparison.OrdinalIgnoreCase))
  234. {
  235. result.Append(".1");
  236. }
  237. else if (string.Equals(profile, "Professional", StringComparison.OrdinalIgnoreCase))
  238. {
  239. result.Append(".2");
  240. }
  241. else
  242. {
  243. // Default to Main
  244. result.Append(".0");
  245. }
  246. if (level is <= 0 or > 31)
  247. {
  248. // Default to the maximum defined level 6.3
  249. level = 19;
  250. }
  251. if (bitDepth != 8
  252. && bitDepth != 10
  253. && bitDepth != 12)
  254. {
  255. // Default to 8 bits
  256. bitDepth = 8;
  257. }
  258. result.Append('.')
  259. // Needed to pad it double digits; otherwise, browsers will reject the stream.
  260. .AppendFormat(CultureInfo.InvariantCulture, "{0:D2}", level)
  261. .Append(tierFlag ? 'H' : 'M');
  262. string bitDepthD2 = bitDepth.ToString("D2", CultureInfo.InvariantCulture);
  263. result.Append('.')
  264. .Append(bitDepthD2);
  265. return result.ToString();
  266. }
  267. }