HlsCodecStringHelpers.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 = "mp4a.a5";
  20. /// <summary>
  21. /// Codec name for E-AC-3.
  22. /// </summary>
  23. public const string EAC3 = "mp4a.a6";
  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 an AV1 codec string.
  162. /// </summary>
  163. /// <param name="profile">AV1 profile.</param>
  164. /// <param name="level">AV1 level.</param>
  165. /// <param name="tierFlag">AV1 tier flag.</param>
  166. /// <param name="bitDepth">AV1 bit depth.</param>
  167. /// <returns>The AV1 codec string.</returns>
  168. public static string GetAv1String(string? profile, int level, bool tierFlag, int bitDepth)
  169. {
  170. // https://aomedia.org/av1/specification/annex-a/
  171. // FORMAT: [codecTag].[profile].[level][tier].[bitDepth]
  172. StringBuilder result = new StringBuilder("av01", 13);
  173. if (string.Equals(profile, "Main", StringComparison.OrdinalIgnoreCase))
  174. {
  175. result.Append(".0");
  176. }
  177. else if (string.Equals(profile, "High", StringComparison.OrdinalIgnoreCase))
  178. {
  179. result.Append(".1");
  180. }
  181. else if (string.Equals(profile, "Professional", StringComparison.OrdinalIgnoreCase))
  182. {
  183. result.Append(".2");
  184. }
  185. else
  186. {
  187. // Default to Main
  188. result.Append(".0");
  189. }
  190. if (level <= 0
  191. || level > 31)
  192. {
  193. // Default to the maximum defined level 6.3
  194. level = 19;
  195. }
  196. if (bitDepth != 8
  197. && bitDepth != 10
  198. && bitDepth != 12)
  199. {
  200. // Default to 8 bits
  201. bitDepth = 8;
  202. }
  203. result.Append('.')
  204. .Append(level)
  205. .Append(tierFlag ? 'H' : 'M');
  206. string bitDepthD2 = bitDepth.ToString("D2", CultureInfo.InvariantCulture);
  207. result.Append('.')
  208. .Append(bitDepthD2);
  209. return result.ToString();
  210. }
  211. }