MediaStream.cs 22 KB

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