MediaStream.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using System.Linq;
  7. using System.Text;
  8. using Jellyfin.Extensions;
  9. using MediaBrowser.Model.Dlna;
  10. using MediaBrowser.Model.Extensions;
  11. using MediaBrowser.Model.MediaInfo;
  12. namespace MediaBrowser.Model.Entities
  13. {
  14. /// <summary>
  15. /// Class MediaStream.
  16. /// </summary>
  17. public class MediaStream
  18. {
  19. private static readonly string[] _specialCodes =
  20. {
  21. // Uncoded languages.
  22. "mis",
  23. // Multiple languages.
  24. "mul",
  25. // Undetermined.
  26. "und",
  27. // No linguistic content; not applicable.
  28. "zxx"
  29. };
  30. /// <summary>
  31. /// Gets or sets the codec.
  32. /// </summary>
  33. /// <value>The codec.</value>
  34. public string Codec { get; set; }
  35. /// <summary>
  36. /// Gets or sets the codec tag.
  37. /// </summary>
  38. /// <value>The codec tag.</value>
  39. public string CodecTag { get; set; }
  40. /// <summary>
  41. /// Gets or sets the language.
  42. /// </summary>
  43. /// <value>The language.</value>
  44. public string Language { get; set; }
  45. /// <summary>
  46. /// Gets or sets the color range.
  47. /// </summary>
  48. /// <value>The color range.</value>
  49. public string ColorRange { get; set; }
  50. /// <summary>
  51. /// Gets or sets the color space.
  52. /// </summary>
  53. /// <value>The color space.</value>
  54. public string ColorSpace { get; set; }
  55. /// <summary>
  56. /// Gets or sets the color transfer.
  57. /// </summary>
  58. /// <value>The color transfer.</value>
  59. public string ColorTransfer { get; set; }
  60. /// <summary>
  61. /// Gets or sets the color primaries.
  62. /// </summary>
  63. /// <value>The color primaries.</value>
  64. public string ColorPrimaries { get; set; }
  65. /// <summary>
  66. /// Gets or sets the comment.
  67. /// </summary>
  68. /// <value>The comment.</value>
  69. public string Comment { get; set; }
  70. /// <summary>
  71. /// Gets or sets the time base.
  72. /// </summary>
  73. /// <value>The time base.</value>
  74. public string TimeBase { get; set; }
  75. /// <summary>
  76. /// Gets or sets the codec time base.
  77. /// </summary>
  78. /// <value>The codec time base.</value>
  79. public string CodecTimeBase { get; set; }
  80. /// <summary>
  81. /// Gets or sets the title.
  82. /// </summary>
  83. /// <value>The title.</value>
  84. public string Title { get; set; }
  85. /// <summary>
  86. /// Gets the video range.
  87. /// </summary>
  88. /// <value>The video range.</value>
  89. public string VideoRange
  90. {
  91. get
  92. {
  93. if (Type != MediaStreamType.Video)
  94. {
  95. return null;
  96. }
  97. var colorTransfer = ColorTransfer;
  98. if (string.Equals(colorTransfer, "smpte2084", StringComparison.OrdinalIgnoreCase)
  99. || string.Equals(colorTransfer, "arib-std-b67", StringComparison.OrdinalIgnoreCase))
  100. {
  101. return "HDR";
  102. }
  103. // For some Dolby Vision files, no color transfer is provided, so check the codec
  104. var codecTag = CodecTag;
  105. if (string.Equals(codecTag, "dva1", StringComparison.OrdinalIgnoreCase)
  106. || string.Equals(codecTag, "dvav", StringComparison.OrdinalIgnoreCase)
  107. || string.Equals(codecTag, "dvh1", StringComparison.OrdinalIgnoreCase)
  108. || string.Equals(codecTag, "dvhe", StringComparison.OrdinalIgnoreCase)
  109. || string.Equals(codecTag, "dav1", StringComparison.OrdinalIgnoreCase))
  110. {
  111. return "HDR";
  112. }
  113. return "SDR";
  114. }
  115. }
  116. public string LocalizedUndefined { get; set; }
  117. public string LocalizedDefault { get; set; }
  118. public string LocalizedForced { get; set; }
  119. public string DisplayTitle
  120. {
  121. get
  122. {
  123. switch (Type)
  124. {
  125. case MediaStreamType.Audio:
  126. {
  127. var attributes = new List<string>();
  128. // Do not display the language code in display titles if unset or set to a special code. Show it in all other cases (possibly expanded).
  129. if (!string.IsNullOrEmpty(Language) && !_specialCodes.Contains(Language, StringComparison.OrdinalIgnoreCase))
  130. {
  131. // Get full language string i.e. eng -> English. Will not work for some languages which use ISO 639-2/B instead of /T codes.
  132. string fullLanguage = CultureInfo
  133. .GetCultures(CultureTypes.NeutralCultures)
  134. .FirstOrDefault(r => r.ThreeLetterISOLanguageName.Equals(Language, StringComparison.OrdinalIgnoreCase))
  135. ?.DisplayName;
  136. attributes.Add(StringHelper.FirstToUpper(fullLanguage ?? Language));
  137. }
  138. if (!string.IsNullOrEmpty(Codec) && !string.Equals(Codec, "dca", StringComparison.OrdinalIgnoreCase) && !string.Equals(Codec, "dts", StringComparison.OrdinalIgnoreCase))
  139. {
  140. attributes.Add(AudioCodec.GetFriendlyName(Codec));
  141. }
  142. else if (!string.IsNullOrEmpty(Profile) && !string.Equals(Profile, "lc", StringComparison.OrdinalIgnoreCase))
  143. {
  144. attributes.Add(Profile);
  145. }
  146. if (!string.IsNullOrEmpty(ChannelLayout))
  147. {
  148. attributes.Add(StringHelper.FirstToUpper(ChannelLayout));
  149. }
  150. else if (Channels.HasValue)
  151. {
  152. attributes.Add(Channels.Value.ToString(CultureInfo.InvariantCulture) + " ch");
  153. }
  154. if (IsDefault)
  155. {
  156. attributes.Add(string.IsNullOrEmpty(LocalizedDefault) ? "Default" : LocalizedDefault);
  157. }
  158. if (!string.IsNullOrEmpty(Title))
  159. {
  160. var result = new StringBuilder(Title);
  161. foreach (var tag in attributes)
  162. {
  163. // Keep Tags that are not already in Title.
  164. if (!Title.Contains(tag, StringComparison.OrdinalIgnoreCase))
  165. {
  166. result.Append(" - ").Append(tag);
  167. }
  168. }
  169. return result.ToString();
  170. }
  171. return string.Join(" - ", attributes);
  172. }
  173. case MediaStreamType.Video:
  174. {
  175. var attributes = new List<string>();
  176. var resolutionText = GetResolutionText();
  177. if (!string.IsNullOrEmpty(resolutionText))
  178. {
  179. attributes.Add(resolutionText);
  180. }
  181. if (!string.IsNullOrEmpty(Codec))
  182. {
  183. attributes.Add(Codec.ToUpperInvariant());
  184. }
  185. if (!string.IsNullOrEmpty(VideoRange))
  186. {
  187. attributes.Add(VideoRange.ToUpperInvariant());
  188. }
  189. if (!string.IsNullOrEmpty(Title))
  190. {
  191. var result = new StringBuilder(Title);
  192. foreach (var tag in attributes)
  193. {
  194. // Keep Tags that are not already in Title.
  195. if (!Title.Contains(tag, StringComparison.OrdinalIgnoreCase))
  196. {
  197. result.Append(" - ").Append(tag);
  198. }
  199. }
  200. return result.ToString();
  201. }
  202. return string.Join(' ', attributes);
  203. }
  204. case MediaStreamType.Subtitle:
  205. {
  206. var attributes = new List<string>();
  207. if (!string.IsNullOrEmpty(Language))
  208. {
  209. // Get full language string i.e. eng -> English. Will not work for some languages which use ISO 639-2/B instead of /T codes.
  210. string fullLanguage = CultureInfo
  211. .GetCultures(CultureTypes.NeutralCultures)
  212. .FirstOrDefault(r => r.ThreeLetterISOLanguageName.Equals(Language, StringComparison.OrdinalIgnoreCase))
  213. ?.DisplayName;
  214. attributes.Add(StringHelper.FirstToUpper(fullLanguage ?? Language));
  215. }
  216. else
  217. {
  218. attributes.Add(string.IsNullOrEmpty(LocalizedUndefined) ? "Und" : LocalizedUndefined);
  219. }
  220. if (IsDefault)
  221. {
  222. attributes.Add(string.IsNullOrEmpty(LocalizedDefault) ? "Default" : LocalizedDefault);
  223. }
  224. if (IsForced)
  225. {
  226. attributes.Add(string.IsNullOrEmpty(LocalizedForced) ? "Forced" : LocalizedForced);
  227. }
  228. if (!string.IsNullOrEmpty(Codec))
  229. {
  230. attributes.Add(Codec.ToUpperInvariant());
  231. }
  232. if (!string.IsNullOrEmpty(Title))
  233. {
  234. var result = new StringBuilder(Title);
  235. foreach (var tag in attributes)
  236. {
  237. // Keep Tags that are not already in Title.
  238. if (!Title.Contains(tag, StringComparison.OrdinalIgnoreCase))
  239. {
  240. result.Append(" - ").Append(tag);
  241. }
  242. }
  243. return result.ToString();
  244. }
  245. return string.Join(" - ", attributes);
  246. }
  247. default:
  248. return null;
  249. }
  250. }
  251. }
  252. public string NalLengthSize { get; set; }
  253. /// <summary>
  254. /// Gets or sets a value indicating whether this instance is interlaced.
  255. /// </summary>
  256. /// <value><c>true</c> if this instance is interlaced; otherwise, <c>false</c>.</value>
  257. public bool IsInterlaced { get; set; }
  258. public bool? IsAVC { get; set; }
  259. /// <summary>
  260. /// Gets or sets the channel layout.
  261. /// </summary>
  262. /// <value>The channel layout.</value>
  263. public string ChannelLayout { get; set; }
  264. /// <summary>
  265. /// Gets or sets the bit rate.
  266. /// </summary>
  267. /// <value>The bit rate.</value>
  268. public int? BitRate { get; set; }
  269. /// <summary>
  270. /// Gets or sets the bit depth.
  271. /// </summary>
  272. /// <value>The bit depth.</value>
  273. public int? BitDepth { get; set; }
  274. /// <summary>
  275. /// Gets or sets the reference frames.
  276. /// </summary>
  277. /// <value>The reference frames.</value>
  278. public int? RefFrames { get; set; }
  279. /// <summary>
  280. /// Gets or sets the length of the packet.
  281. /// </summary>
  282. /// <value>The length of the packet.</value>
  283. public int? PacketLength { get; set; }
  284. /// <summary>
  285. /// Gets or sets the channels.
  286. /// </summary>
  287. /// <value>The channels.</value>
  288. public int? Channels { get; set; }
  289. /// <summary>
  290. /// Gets or sets the sample rate.
  291. /// </summary>
  292. /// <value>The sample rate.</value>
  293. public int? SampleRate { get; set; }
  294. /// <summary>
  295. /// Gets or sets a value indicating whether this instance is default.
  296. /// </summary>
  297. /// <value><c>true</c> if this instance is default; otherwise, <c>false</c>.</value>
  298. public bool IsDefault { get; set; }
  299. /// <summary>
  300. /// Gets or sets a value indicating whether this instance is forced.
  301. /// </summary>
  302. /// <value><c>true</c> if this instance is forced; otherwise, <c>false</c>.</value>
  303. public bool IsForced { get; set; }
  304. /// <summary>
  305. /// Gets or sets the height.
  306. /// </summary>
  307. /// <value>The height.</value>
  308. public int? Height { get; set; }
  309. /// <summary>
  310. /// Gets or sets the width.
  311. /// </summary>
  312. /// <value>The width.</value>
  313. public int? Width { get; set; }
  314. /// <summary>
  315. /// Gets or sets the average frame rate.
  316. /// </summary>
  317. /// <value>The average frame rate.</value>
  318. public float? AverageFrameRate { get; set; }
  319. /// <summary>
  320. /// Gets or sets the real frame rate.
  321. /// </summary>
  322. /// <value>The real frame rate.</value>
  323. public float? RealFrameRate { get; set; }
  324. /// <summary>
  325. /// Gets or sets the profile.
  326. /// </summary>
  327. /// <value>The profile.</value>
  328. public string Profile { get; set; }
  329. /// <summary>
  330. /// Gets or sets the type.
  331. /// </summary>
  332. /// <value>The type.</value>
  333. public MediaStreamType Type { get; set; }
  334. /// <summary>
  335. /// Gets or sets the aspect ratio.
  336. /// </summary>
  337. /// <value>The aspect ratio.</value>
  338. public string AspectRatio { get; set; }
  339. /// <summary>
  340. /// Gets or sets the index.
  341. /// </summary>
  342. /// <value>The index.</value>
  343. public int Index { get; set; }
  344. /// <summary>
  345. /// Gets or sets the score.
  346. /// </summary>
  347. /// <value>The score.</value>
  348. public int? Score { get; set; }
  349. /// <summary>
  350. /// Gets or sets a value indicating whether this instance is external.
  351. /// </summary>
  352. /// <value><c>true</c> if this instance is external; otherwise, <c>false</c>.</value>
  353. public bool IsExternal { get; set; }
  354. /// <summary>
  355. /// Gets or sets the method.
  356. /// </summary>
  357. /// <value>The method.</value>
  358. public SubtitleDeliveryMethod? DeliveryMethod { get; set; }
  359. /// <summary>
  360. /// Gets or sets the delivery URL.
  361. /// </summary>
  362. /// <value>The delivery URL.</value>
  363. public string DeliveryUrl { get; set; }
  364. /// <summary>
  365. /// Gets or sets a value indicating whether this instance is external URL.
  366. /// </summary>
  367. /// <value><c>null</c> if [is external URL] contains no value, <c>true</c> if [is external URL]; otherwise, <c>false</c>.</value>
  368. public bool? IsExternalUrl { get; set; }
  369. public bool IsTextSubtitleStream
  370. {
  371. get
  372. {
  373. if (Type != MediaStreamType.Subtitle)
  374. {
  375. return false;
  376. }
  377. if (string.IsNullOrEmpty(Codec) && !IsExternal)
  378. {
  379. return false;
  380. }
  381. return IsTextFormat(Codec);
  382. }
  383. }
  384. /// <summary>
  385. /// Gets or sets a value indicating whether [supports external stream].
  386. /// </summary>
  387. /// <value><c>true</c> if [supports external stream]; otherwise, <c>false</c>.</value>
  388. public bool SupportsExternalStream { get; set; }
  389. /// <summary>
  390. /// Gets or sets the filename.
  391. /// </summary>
  392. /// <value>The filename.</value>
  393. public string Path { get; set; }
  394. /// <summary>
  395. /// Gets or sets the pixel format.
  396. /// </summary>
  397. /// <value>The pixel format.</value>
  398. public string PixelFormat { get; set; }
  399. /// <summary>
  400. /// Gets or sets the level.
  401. /// </summary>
  402. /// <value>The level.</value>
  403. public double? Level { get; set; }
  404. /// <summary>
  405. /// Gets or sets whether this instance is anamorphic.
  406. /// </summary>
  407. /// <value><c>true</c> if this instance is anamorphic; otherwise, <c>false</c>.</value>
  408. public bool? IsAnamorphic { get; set; }
  409. internal string GetResolutionText()
  410. {
  411. if (!Width.HasValue || !Height.HasValue)
  412. {
  413. return null;
  414. }
  415. return Width switch
  416. {
  417. <= 720 when Height <= 480 => IsInterlaced ? "480i" : "480p",
  418. // 720x576 (PAL) (768 when rescaled for square pixels)
  419. <= 768 when Height <= 576 => IsInterlaced ? "576i" : "576p",
  420. // 960x540 (sometimes 544 which is multiple of 16)
  421. <= 960 when Height <= 544 => IsInterlaced ? "540i" : "540p",
  422. // 1280x720
  423. <= 1280 when Height <= 962 => IsInterlaced ? "720i" : "720p",
  424. // 1920x1080
  425. <= 1920 when Height <= 1440 => IsInterlaced ? "1080i" : "1080p",
  426. // 4K
  427. <= 4096 when Height <= 3072 => "4K",
  428. // 8K
  429. <= 8192 when Height <= 6144 => "8K",
  430. _ => null
  431. };
  432. }
  433. public static bool IsTextFormat(string format)
  434. {
  435. string codec = format ?? string.Empty;
  436. // sub = external .sub file
  437. return !codec.Contains("pgs", StringComparison.OrdinalIgnoreCase) &&
  438. !codec.Contains("dvd", StringComparison.OrdinalIgnoreCase) &&
  439. !codec.Contains("dvbsub", StringComparison.OrdinalIgnoreCase) &&
  440. !string.Equals(codec, "sub", StringComparison.OrdinalIgnoreCase) &&
  441. !string.Equals(codec, "dvb_subtitle", StringComparison.OrdinalIgnoreCase);
  442. }
  443. public bool SupportsSubtitleConversionTo(string toCodec)
  444. {
  445. if (!IsTextSubtitleStream)
  446. {
  447. return false;
  448. }
  449. var fromCodec = Codec;
  450. // Can't convert from this
  451. if (string.Equals(fromCodec, "ass", StringComparison.OrdinalIgnoreCase))
  452. {
  453. return false;
  454. }
  455. if (string.Equals(fromCodec, "ssa", StringComparison.OrdinalIgnoreCase))
  456. {
  457. return false;
  458. }
  459. // Can't convert to this
  460. if (string.Equals(toCodec, "ass", StringComparison.OrdinalIgnoreCase))
  461. {
  462. return false;
  463. }
  464. if (string.Equals(toCodec, "ssa", StringComparison.OrdinalIgnoreCase))
  465. {
  466. return false;
  467. }
  468. return true;
  469. }
  470. }
  471. }