MediaStream.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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. var (videoRange, _) = GetVideoColorRange();
  94. return videoRange;
  95. }
  96. }
  97. /// <summary>
  98. /// Gets the video range type.
  99. /// </summary>
  100. /// <value>The video range type.</value>
  101. public string VideoRangeType
  102. {
  103. get
  104. {
  105. var (_, videoRangeType) = GetVideoColorRange();
  106. return videoRangeType;
  107. }
  108. }
  109. public string LocalizedUndefined { get; set; }
  110. public string LocalizedDefault { get; set; }
  111. public string LocalizedForced { get; set; }
  112. public string LocalizedExternal { get; set; }
  113. public string DisplayTitle
  114. {
  115. get
  116. {
  117. switch (Type)
  118. {
  119. case MediaStreamType.Audio:
  120. {
  121. var attributes = new List<string>();
  122. // 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).
  123. if (!string.IsNullOrEmpty(Language) && !_specialCodes.Contains(Language, StringComparison.OrdinalIgnoreCase))
  124. {
  125. // Get full language string i.e. eng -> English. Will not work for some languages which use ISO 639-2/B instead of /T codes.
  126. string fullLanguage = CultureInfo
  127. .GetCultures(CultureTypes.NeutralCultures)
  128. .FirstOrDefault(r => r.ThreeLetterISOLanguageName.Equals(Language, StringComparison.OrdinalIgnoreCase))
  129. ?.DisplayName;
  130. attributes.Add(StringHelper.FirstToUpper(fullLanguage ?? Language));
  131. }
  132. if (!string.IsNullOrEmpty(Codec) && !string.Equals(Codec, "dca", StringComparison.OrdinalIgnoreCase) && !string.Equals(Codec, "dts", StringComparison.OrdinalIgnoreCase))
  133. {
  134. attributes.Add(AudioCodec.GetFriendlyName(Codec));
  135. }
  136. else if (!string.IsNullOrEmpty(Profile) && !string.Equals(Profile, "lc", StringComparison.OrdinalIgnoreCase))
  137. {
  138. attributes.Add(Profile);
  139. }
  140. if (!string.IsNullOrEmpty(ChannelLayout))
  141. {
  142. attributes.Add(StringHelper.FirstToUpper(ChannelLayout));
  143. }
  144. else if (Channels.HasValue)
  145. {
  146. attributes.Add(Channels.Value.ToString(CultureInfo.InvariantCulture) + " ch");
  147. }
  148. if (IsDefault)
  149. {
  150. attributes.Add(string.IsNullOrEmpty(LocalizedDefault) ? "Default" : LocalizedDefault);
  151. }
  152. if (IsExternal)
  153. {
  154. attributes.Add(string.IsNullOrEmpty(LocalizedExternal) ? "External" : LocalizedExternal);
  155. }
  156. if (!string.IsNullOrEmpty(Title))
  157. {
  158. var result = new StringBuilder(Title);
  159. foreach (var tag in attributes)
  160. {
  161. // Keep Tags that are not already in Title.
  162. if (!Title.Contains(tag, StringComparison.OrdinalIgnoreCase))
  163. {
  164. result.Append(" - ").Append(tag);
  165. }
  166. }
  167. return result.ToString();
  168. }
  169. return string.Join(" - ", attributes);
  170. }
  171. case MediaStreamType.Video:
  172. {
  173. var attributes = new List<string>();
  174. var resolutionText = GetResolutionText();
  175. if (!string.IsNullOrEmpty(resolutionText))
  176. {
  177. attributes.Add(resolutionText);
  178. }
  179. if (!string.IsNullOrEmpty(Codec))
  180. {
  181. attributes.Add(Codec.ToUpperInvariant());
  182. }
  183. if (!string.IsNullOrEmpty(VideoRange))
  184. {
  185. attributes.Add(VideoRange.ToUpperInvariant());
  186. }
  187. if (!string.IsNullOrEmpty(Title))
  188. {
  189. var result = new StringBuilder(Title);
  190. foreach (var tag in attributes)
  191. {
  192. // Keep Tags that are not already in Title.
  193. if (!Title.Contains(tag, StringComparison.OrdinalIgnoreCase))
  194. {
  195. result.Append(" - ").Append(tag);
  196. }
  197. }
  198. return result.ToString();
  199. }
  200. return string.Join(' ', attributes);
  201. }
  202. case MediaStreamType.Subtitle:
  203. {
  204. var attributes = new List<string>();
  205. if (!string.IsNullOrEmpty(Language))
  206. {
  207. // Get full language string i.e. eng -> English. Will not work for some languages which use ISO 639-2/B instead of /T codes.
  208. string fullLanguage = CultureInfo
  209. .GetCultures(CultureTypes.NeutralCultures)
  210. .FirstOrDefault(r => r.ThreeLetterISOLanguageName.Equals(Language, StringComparison.OrdinalIgnoreCase))
  211. ?.DisplayName;
  212. attributes.Add(StringHelper.FirstToUpper(fullLanguage ?? Language));
  213. }
  214. else
  215. {
  216. attributes.Add(string.IsNullOrEmpty(LocalizedUndefined) ? "Und" : LocalizedUndefined);
  217. }
  218. if (IsDefault)
  219. {
  220. attributes.Add(string.IsNullOrEmpty(LocalizedDefault) ? "Default" : LocalizedDefault);
  221. }
  222. if (IsForced)
  223. {
  224. attributes.Add(string.IsNullOrEmpty(LocalizedForced) ? "Forced" : LocalizedForced);
  225. }
  226. if (!string.IsNullOrEmpty(Codec))
  227. {
  228. attributes.Add(Codec.ToUpperInvariant());
  229. }
  230. if (IsExternal)
  231. {
  232. attributes.Add(string.IsNullOrEmpty(LocalizedExternal) ? "External" : LocalizedExternal);
  233. }
  234. if (!string.IsNullOrEmpty(Title))
  235. {
  236. var result = new StringBuilder(Title);
  237. foreach (var tag in attributes)
  238. {
  239. // Keep Tags that are not already in Title.
  240. if (!Title.Contains(tag, StringComparison.OrdinalIgnoreCase))
  241. {
  242. result.Append(" - ").Append(tag);
  243. }
  244. }
  245. return result.ToString();
  246. }
  247. return string.Join(" - ", attributes);
  248. }
  249. default:
  250. return null;
  251. }
  252. }
  253. }
  254. public string NalLengthSize { get; set; }
  255. /// <summary>
  256. /// Gets or sets a value indicating whether this instance is interlaced.
  257. /// </summary>
  258. /// <value><c>true</c> if this instance is interlaced; otherwise, <c>false</c>.</value>
  259. public bool IsInterlaced { get; set; }
  260. public bool? IsAVC { get; set; }
  261. /// <summary>
  262. /// Gets or sets the channel layout.
  263. /// </summary>
  264. /// <value>The channel layout.</value>
  265. public string ChannelLayout { get; set; }
  266. /// <summary>
  267. /// Gets or sets the bit rate.
  268. /// </summary>
  269. /// <value>The bit rate.</value>
  270. public int? BitRate { get; set; }
  271. /// <summary>
  272. /// Gets or sets the bit depth.
  273. /// </summary>
  274. /// <value>The bit depth.</value>
  275. public int? BitDepth { get; set; }
  276. /// <summary>
  277. /// Gets or sets the reference frames.
  278. /// </summary>
  279. /// <value>The reference frames.</value>
  280. public int? RefFrames { get; set; }
  281. /// <summary>
  282. /// Gets or sets the length of the packet.
  283. /// </summary>
  284. /// <value>The length of the packet.</value>
  285. public int? PacketLength { get; set; }
  286. /// <summary>
  287. /// Gets or sets the channels.
  288. /// </summary>
  289. /// <value>The channels.</value>
  290. public int? Channels { get; set; }
  291. /// <summary>
  292. /// Gets or sets the sample rate.
  293. /// </summary>
  294. /// <value>The sample rate.</value>
  295. public int? SampleRate { get; set; }
  296. /// <summary>
  297. /// Gets or sets a value indicating whether this instance is default.
  298. /// </summary>
  299. /// <value><c>true</c> if this instance is default; otherwise, <c>false</c>.</value>
  300. public bool IsDefault { get; set; }
  301. /// <summary>
  302. /// Gets or sets a value indicating whether this instance is forced.
  303. /// </summary>
  304. /// <value><c>true</c> if this instance is forced; otherwise, <c>false</c>.</value>
  305. public bool IsForced { get; set; }
  306. /// <summary>
  307. /// Gets or sets the height.
  308. /// </summary>
  309. /// <value>The height.</value>
  310. public int? Height { get; set; }
  311. /// <summary>
  312. /// Gets or sets the width.
  313. /// </summary>
  314. /// <value>The width.</value>
  315. public int? Width { get; set; }
  316. /// <summary>
  317. /// Gets or sets the average frame rate.
  318. /// </summary>
  319. /// <value>The average frame rate.</value>
  320. public float? AverageFrameRate { get; set; }
  321. /// <summary>
  322. /// Gets or sets the real frame rate.
  323. /// </summary>
  324. /// <value>The real frame rate.</value>
  325. public float? RealFrameRate { get; set; }
  326. /// <summary>
  327. /// Gets or sets the profile.
  328. /// </summary>
  329. /// <value>The profile.</value>
  330. public string Profile { get; set; }
  331. /// <summary>
  332. /// Gets or sets the type.
  333. /// </summary>
  334. /// <value>The type.</value>
  335. public MediaStreamType Type { get; set; }
  336. /// <summary>
  337. /// Gets or sets the aspect ratio.
  338. /// </summary>
  339. /// <value>The aspect ratio.</value>
  340. public string AspectRatio { get; set; }
  341. /// <summary>
  342. /// Gets or sets the index.
  343. /// </summary>
  344. /// <value>The index.</value>
  345. public int Index { get; set; }
  346. /// <summary>
  347. /// Gets or sets the score.
  348. /// </summary>
  349. /// <value>The score.</value>
  350. public int? Score { get; set; }
  351. /// <summary>
  352. /// Gets or sets a value indicating whether this instance is external.
  353. /// </summary>
  354. /// <value><c>true</c> if this instance is external; otherwise, <c>false</c>.</value>
  355. public bool IsExternal { get; set; }
  356. /// <summary>
  357. /// Gets or sets the method.
  358. /// </summary>
  359. /// <value>The method.</value>
  360. public SubtitleDeliveryMethod? DeliveryMethod { get; set; }
  361. /// <summary>
  362. /// Gets or sets the delivery URL.
  363. /// </summary>
  364. /// <value>The delivery URL.</value>
  365. public string DeliveryUrl { get; set; }
  366. /// <summary>
  367. /// Gets or sets a value indicating whether this instance is external URL.
  368. /// </summary>
  369. /// <value><c>null</c> if [is external URL] contains no value, <c>true</c> if [is external URL]; otherwise, <c>false</c>.</value>
  370. public bool? IsExternalUrl { get; set; }
  371. public bool IsTextSubtitleStream
  372. {
  373. get
  374. {
  375. if (Type != MediaStreamType.Subtitle)
  376. {
  377. return false;
  378. }
  379. if (string.IsNullOrEmpty(Codec) && !IsExternal)
  380. {
  381. return false;
  382. }
  383. return IsTextFormat(Codec);
  384. }
  385. }
  386. /// <summary>
  387. /// Gets or sets a value indicating whether [supports external stream].
  388. /// </summary>
  389. /// <value><c>true</c> if [supports external stream]; otherwise, <c>false</c>.</value>
  390. public bool SupportsExternalStream { get; set; }
  391. /// <summary>
  392. /// Gets or sets the filename.
  393. /// </summary>
  394. /// <value>The filename.</value>
  395. public string Path { get; set; }
  396. /// <summary>
  397. /// Gets or sets the pixel format.
  398. /// </summary>
  399. /// <value>The pixel format.</value>
  400. public string PixelFormat { get; set; }
  401. /// <summary>
  402. /// Gets or sets the level.
  403. /// </summary>
  404. /// <value>The level.</value>
  405. public double? Level { get; set; }
  406. /// <summary>
  407. /// Gets or sets whether this instance is anamorphic.
  408. /// </summary>
  409. /// <value><c>true</c> if this instance is anamorphic; otherwise, <c>false</c>.</value>
  410. public bool? IsAnamorphic { get; set; }
  411. internal string GetResolutionText()
  412. {
  413. if (!Width.HasValue || !Height.HasValue)
  414. {
  415. return null;
  416. }
  417. return Width switch
  418. {
  419. <= 720 when Height <= 480 => IsInterlaced ? "480i" : "480p",
  420. // 720x576 (PAL) (768 when rescaled for square pixels)
  421. <= 768 when Height <= 576 => IsInterlaced ? "576i" : "576p",
  422. // 960x540 (sometimes 544 which is multiple of 16)
  423. <= 960 when Height <= 544 => IsInterlaced ? "540i" : "540p",
  424. // 1280x720
  425. <= 1280 when Height <= 962 => IsInterlaced ? "720i" : "720p",
  426. // 1920x1080
  427. <= 1920 when Height <= 1440 => IsInterlaced ? "1080i" : "1080p",
  428. // 4K
  429. <= 4096 when Height <= 3072 => "4K",
  430. // 8K
  431. <= 8192 when Height <= 6144 => "8K",
  432. _ => null
  433. };
  434. }
  435. public static bool IsTextFormat(string format)
  436. {
  437. string codec = format ?? string.Empty;
  438. // sub = external .sub file
  439. return !codec.Contains("pgs", StringComparison.OrdinalIgnoreCase) &&
  440. !codec.Contains("dvd", StringComparison.OrdinalIgnoreCase) &&
  441. !codec.Contains("dvbsub", StringComparison.OrdinalIgnoreCase) &&
  442. !string.Equals(codec, "sub", StringComparison.OrdinalIgnoreCase) &&
  443. !string.Equals(codec, "dvb_subtitle", StringComparison.OrdinalIgnoreCase);
  444. }
  445. public bool SupportsSubtitleConversionTo(string toCodec)
  446. {
  447. if (!IsTextSubtitleStream)
  448. {
  449. return false;
  450. }
  451. var fromCodec = Codec;
  452. // Can't convert from this
  453. if (string.Equals(fromCodec, "ass", StringComparison.OrdinalIgnoreCase))
  454. {
  455. return false;
  456. }
  457. if (string.Equals(fromCodec, "ssa", StringComparison.OrdinalIgnoreCase))
  458. {
  459. return false;
  460. }
  461. // Can't convert to this
  462. if (string.Equals(toCodec, "ass", StringComparison.OrdinalIgnoreCase))
  463. {
  464. return false;
  465. }
  466. if (string.Equals(toCodec, "ssa", StringComparison.OrdinalIgnoreCase))
  467. {
  468. return false;
  469. }
  470. return true;
  471. }
  472. public (string VideoRange, string VideoRangeType) GetVideoColorRange()
  473. {
  474. if (Type != MediaStreamType.Video)
  475. {
  476. return (null, null);
  477. }
  478. var colorTransfer = ColorTransfer;
  479. if (string.Equals(colorTransfer, "smpte2084", StringComparison.OrdinalIgnoreCase))
  480. {
  481. return ("HDR", "HDR10");
  482. }
  483. if (string.Equals(colorTransfer, "arib-std-b67", StringComparison.OrdinalIgnoreCase))
  484. {
  485. return ("HDR", "HLG");
  486. }
  487. // For some Dolby Vision files, no color transfer is provided, so check the codec
  488. var codecTag = CodecTag;
  489. if (string.Equals(codecTag, "dovi", StringComparison.OrdinalIgnoreCase)
  490. || string.Equals(codecTag, "dvh1", StringComparison.OrdinalIgnoreCase)
  491. || string.Equals(codecTag, "dvhe", StringComparison.OrdinalIgnoreCase)
  492. || string.Equals(codecTag, "dav1", StringComparison.OrdinalIgnoreCase))
  493. {
  494. return ("HDR", "DOVI");
  495. }
  496. return ("SDR", "SDR");
  497. }
  498. }
  499. }