MediaStream.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Globalization;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Text.Json.Serialization;
  10. using Jellyfin.Data.Enums;
  11. using Jellyfin.Extensions;
  12. using MediaBrowser.Model.Dlna;
  13. using MediaBrowser.Model.Extensions;
  14. using MediaBrowser.Model.MediaInfo;
  15. namespace MediaBrowser.Model.Entities
  16. {
  17. /// <summary>
  18. /// Class MediaStream.
  19. /// </summary>
  20. public class MediaStream
  21. {
  22. private static readonly string[] _specialCodes =
  23. {
  24. // Uncoded languages.
  25. "mis",
  26. // Multiple languages.
  27. "mul",
  28. // Undetermined.
  29. "und",
  30. // No linguistic content; not applicable.
  31. "zxx"
  32. };
  33. /// <summary>
  34. /// Gets or sets the codec.
  35. /// </summary>
  36. /// <value>The codec.</value>
  37. public string Codec { get; set; }
  38. /// <summary>
  39. /// Gets or sets the codec tag.
  40. /// </summary>
  41. /// <value>The codec tag.</value>
  42. public string CodecTag { get; set; }
  43. /// <summary>
  44. /// Gets or sets the language.
  45. /// </summary>
  46. /// <value>The language.</value>
  47. public string Language { get; set; }
  48. /// <summary>
  49. /// Gets or sets the color range.
  50. /// </summary>
  51. /// <value>The color range.</value>
  52. public string ColorRange { get; set; }
  53. /// <summary>
  54. /// Gets or sets the color space.
  55. /// </summary>
  56. /// <value>The color space.</value>
  57. public string ColorSpace { get; set; }
  58. /// <summary>
  59. /// Gets or sets the color transfer.
  60. /// </summary>
  61. /// <value>The color transfer.</value>
  62. public string ColorTransfer { get; set; }
  63. /// <summary>
  64. /// Gets or sets the color primaries.
  65. /// </summary>
  66. /// <value>The color primaries.</value>
  67. public string ColorPrimaries { get; set; }
  68. /// <summary>
  69. /// Gets or sets the Dolby Vision version major.
  70. /// </summary>
  71. /// <value>The Dolby Vision version major.</value>
  72. public int? DvVersionMajor { get; set; }
  73. /// <summary>
  74. /// Gets or sets the Dolby Vision version minor.
  75. /// </summary>
  76. /// <value>The Dolby Vision version minor.</value>
  77. public int? DvVersionMinor { get; set; }
  78. /// <summary>
  79. /// Gets or sets the Dolby Vision profile.
  80. /// </summary>
  81. /// <value>The Dolby Vision profile.</value>
  82. public int? DvProfile { get; set; }
  83. /// <summary>
  84. /// Gets or sets the Dolby Vision level.
  85. /// </summary>
  86. /// <value>The Dolby Vision level.</value>
  87. public int? DvLevel { get; set; }
  88. /// <summary>
  89. /// Gets or sets the Dolby Vision rpu present flag.
  90. /// </summary>
  91. /// <value>The Dolby Vision rpu present flag.</value>
  92. public int? RpuPresentFlag { get; set; }
  93. /// <summary>
  94. /// Gets or sets the Dolby Vision el present flag.
  95. /// </summary>
  96. /// <value>The Dolby Vision el present flag.</value>
  97. public int? ElPresentFlag { get; set; }
  98. /// <summary>
  99. /// Gets or sets the Dolby Vision bl present flag.
  100. /// </summary>
  101. /// <value>The Dolby Vision bl present flag.</value>
  102. public int? BlPresentFlag { get; set; }
  103. /// <summary>
  104. /// Gets or sets the Dolby Vision bl signal compatibility id.
  105. /// </summary>
  106. /// <value>The Dolby Vision bl signal compatibility id.</value>
  107. public int? DvBlSignalCompatibilityId { get; set; }
  108. /// <summary>
  109. /// Gets or sets the Rotation in degrees.
  110. /// </summary>
  111. /// <value>The video rotation.</value>
  112. public int? Rotation { get; set; }
  113. /// <summary>
  114. /// Gets or sets the comment.
  115. /// </summary>
  116. /// <value>The comment.</value>
  117. public string Comment { get; set; }
  118. /// <summary>
  119. /// Gets or sets the time base.
  120. /// </summary>
  121. /// <value>The time base.</value>
  122. public string TimeBase { get; set; }
  123. /// <summary>
  124. /// Gets or sets the codec time base.
  125. /// </summary>
  126. /// <value>The codec time base.</value>
  127. public string CodecTimeBase { get; set; }
  128. /// <summary>
  129. /// Gets or sets the title.
  130. /// </summary>
  131. /// <value>The title.</value>
  132. public string Title { get; set; }
  133. /// <summary>
  134. /// Gets the video range.
  135. /// </summary>
  136. /// <value>The video range.</value>
  137. [DefaultValue(VideoRange.Unknown)]
  138. public VideoRange VideoRange
  139. {
  140. get
  141. {
  142. var (videoRange, _) = GetVideoColorRange();
  143. return videoRange;
  144. }
  145. }
  146. /// <summary>
  147. /// Gets the video range type.
  148. /// </summary>
  149. /// <value>The video range type.</value>
  150. [DefaultValue(VideoRangeType.Unknown)]
  151. public VideoRangeType VideoRangeType
  152. {
  153. get
  154. {
  155. var (_, videoRangeType) = GetVideoColorRange();
  156. return videoRangeType;
  157. }
  158. }
  159. /// <summary>
  160. /// Gets the video dovi title.
  161. /// </summary>
  162. /// <value>The video dovi title.</value>
  163. public string VideoDoViTitle
  164. {
  165. get
  166. {
  167. var dvProfile = DvProfile;
  168. var rpuPresentFlag = RpuPresentFlag == 1;
  169. var blPresentFlag = BlPresentFlag == 1;
  170. var dvBlCompatId = DvBlSignalCompatibilityId;
  171. if (rpuPresentFlag
  172. && blPresentFlag
  173. && (dvProfile == 4
  174. || dvProfile == 5
  175. || dvProfile == 7
  176. || dvProfile == 8
  177. || dvProfile == 9
  178. || dvProfile == 10))
  179. {
  180. var title = "Dolby Vision Profile " + dvProfile;
  181. if (dvBlCompatId > 0)
  182. {
  183. title += "." + dvBlCompatId;
  184. }
  185. return dvBlCompatId switch
  186. {
  187. 1 => title + " (HDR10)",
  188. 2 => title + " (SDR)",
  189. 4 => title + " (HLG)",
  190. 6 => title + " (HDR10)", // Technically means Blu-ray, but practically always HDR10
  191. _ => title
  192. };
  193. }
  194. return null;
  195. }
  196. }
  197. /// <summary>
  198. /// Gets the audio spatial format.
  199. /// </summary>
  200. /// <value>The audio spatial format.</value>
  201. [DefaultValue(AudioSpatialFormat.None)]
  202. public AudioSpatialFormat AudioSpatialFormat
  203. {
  204. get
  205. {
  206. if (Type != MediaStreamType.Audio || string.IsNullOrEmpty(Profile))
  207. {
  208. return AudioSpatialFormat.None;
  209. }
  210. return
  211. Profile.Contains("Dolby Atmos", StringComparison.OrdinalIgnoreCase) ? AudioSpatialFormat.DolbyAtmos :
  212. Profile.Contains("DTS:X", StringComparison.OrdinalIgnoreCase) ? AudioSpatialFormat.DTSX :
  213. AudioSpatialFormat.None;
  214. }
  215. }
  216. public string LocalizedUndefined { get; set; }
  217. public string LocalizedDefault { get; set; }
  218. public string LocalizedForced { get; set; }
  219. public string LocalizedExternal { get; set; }
  220. public string LocalizedHearingImpaired { get; set; }
  221. public string DisplayTitle
  222. {
  223. get
  224. {
  225. switch (Type)
  226. {
  227. case MediaStreamType.Audio:
  228. {
  229. var attributes = new List<string>();
  230. // 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).
  231. if (!string.IsNullOrEmpty(Language) && !_specialCodes.Contains(Language, StringComparison.OrdinalIgnoreCase))
  232. {
  233. // Get full language string i.e. eng -> English. Will not work for some languages which use ISO 639-2/B instead of /T codes.
  234. string fullLanguage = CultureInfo
  235. .GetCultures(CultureTypes.NeutralCultures)
  236. .FirstOrDefault(r => r.ThreeLetterISOLanguageName.Equals(Language, StringComparison.OrdinalIgnoreCase))
  237. ?.DisplayName;
  238. attributes.Add(StringHelper.FirstToUpper(fullLanguage ?? Language));
  239. }
  240. if (!string.IsNullOrEmpty(Profile) && !string.Equals(Profile, "lc", StringComparison.OrdinalIgnoreCase))
  241. {
  242. attributes.Add(Profile);
  243. }
  244. else if (!string.IsNullOrEmpty(Codec))
  245. {
  246. attributes.Add(AudioCodec.GetFriendlyName(Codec));
  247. }
  248. if (!string.IsNullOrEmpty(ChannelLayout))
  249. {
  250. attributes.Add(StringHelper.FirstToUpper(ChannelLayout));
  251. }
  252. else if (Channels.HasValue)
  253. {
  254. attributes.Add(Channels.Value.ToString(CultureInfo.InvariantCulture) + " ch");
  255. }
  256. if (IsDefault)
  257. {
  258. attributes.Add(string.IsNullOrEmpty(LocalizedDefault) ? "Default" : LocalizedDefault);
  259. }
  260. if (IsExternal)
  261. {
  262. attributes.Add(string.IsNullOrEmpty(LocalizedExternal) ? "External" : LocalizedExternal);
  263. }
  264. if (!string.IsNullOrEmpty(Title))
  265. {
  266. var result = new StringBuilder(Title);
  267. foreach (var tag in attributes)
  268. {
  269. // Keep Tags that are not already in Title.
  270. if (!Title.Contains(tag, StringComparison.OrdinalIgnoreCase))
  271. {
  272. result.Append(" - ").Append(tag);
  273. }
  274. }
  275. return result.ToString();
  276. }
  277. return string.Join(" - ", attributes);
  278. }
  279. case MediaStreamType.Video:
  280. {
  281. var attributes = new List<string>();
  282. var resolutionText = GetResolutionText();
  283. if (!string.IsNullOrEmpty(resolutionText))
  284. {
  285. attributes.Add(resolutionText);
  286. }
  287. if (!string.IsNullOrEmpty(Codec))
  288. {
  289. attributes.Add(Codec.ToUpperInvariant());
  290. }
  291. if (VideoDoViTitle is not null)
  292. {
  293. attributes.Add(VideoDoViTitle);
  294. }
  295. else if (VideoRange != VideoRange.Unknown)
  296. {
  297. attributes.Add(VideoRange.ToString());
  298. }
  299. if (!string.IsNullOrEmpty(Title))
  300. {
  301. var result = new StringBuilder(Title);
  302. foreach (var tag in attributes)
  303. {
  304. // Keep Tags that are not already in Title.
  305. if (!Title.Contains(tag, StringComparison.OrdinalIgnoreCase))
  306. {
  307. result.Append(" - ").Append(tag);
  308. }
  309. }
  310. return result.ToString();
  311. }
  312. return string.Join(' ', attributes);
  313. }
  314. case MediaStreamType.Subtitle:
  315. {
  316. var attributes = new List<string>();
  317. if (!string.IsNullOrEmpty(Language))
  318. {
  319. // Get full language string i.e. eng -> English. Will not work for some languages which use ISO 639-2/B instead of /T codes.
  320. string fullLanguage = CultureInfo
  321. .GetCultures(CultureTypes.NeutralCultures)
  322. .FirstOrDefault(r => r.ThreeLetterISOLanguageName.Equals(Language, StringComparison.OrdinalIgnoreCase))
  323. ?.DisplayName;
  324. attributes.Add(StringHelper.FirstToUpper(fullLanguage ?? Language));
  325. }
  326. else
  327. {
  328. attributes.Add(string.IsNullOrEmpty(LocalizedUndefined) ? "Und" : LocalizedUndefined);
  329. }
  330. if (IsHearingImpaired)
  331. {
  332. attributes.Add(string.IsNullOrEmpty(LocalizedHearingImpaired) ? "Hearing Impaired" : LocalizedHearingImpaired);
  333. }
  334. if (IsDefault)
  335. {
  336. attributes.Add(string.IsNullOrEmpty(LocalizedDefault) ? "Default" : LocalizedDefault);
  337. }
  338. if (IsForced)
  339. {
  340. attributes.Add(string.IsNullOrEmpty(LocalizedForced) ? "Forced" : LocalizedForced);
  341. }
  342. if (!string.IsNullOrEmpty(Codec))
  343. {
  344. attributes.Add(Codec.ToUpperInvariant());
  345. }
  346. if (IsExternal)
  347. {
  348. attributes.Add(string.IsNullOrEmpty(LocalizedExternal) ? "External" : LocalizedExternal);
  349. }
  350. if (!string.IsNullOrEmpty(Title))
  351. {
  352. var result = new StringBuilder(Title);
  353. foreach (var tag in attributes)
  354. {
  355. // Keep Tags that are not already in Title.
  356. if (!Title.Contains(tag, StringComparison.OrdinalIgnoreCase))
  357. {
  358. result.Append(" - ").Append(tag);
  359. }
  360. }
  361. return result.ToString();
  362. }
  363. return string.Join(" - ", attributes);
  364. }
  365. default:
  366. return null;
  367. }
  368. }
  369. }
  370. public string NalLengthSize { get; set; }
  371. /// <summary>
  372. /// Gets or sets a value indicating whether this instance is interlaced.
  373. /// </summary>
  374. /// <value><c>true</c> if this instance is interlaced; otherwise, <c>false</c>.</value>
  375. public bool IsInterlaced { get; set; }
  376. public bool? IsAVC { get; set; }
  377. /// <summary>
  378. /// Gets or sets the channel layout.
  379. /// </summary>
  380. /// <value>The channel layout.</value>
  381. public string ChannelLayout { get; set; }
  382. /// <summary>
  383. /// Gets or sets the bit rate.
  384. /// </summary>
  385. /// <value>The bit rate.</value>
  386. public int? BitRate { get; set; }
  387. /// <summary>
  388. /// Gets or sets the bit depth.
  389. /// </summary>
  390. /// <value>The bit depth.</value>
  391. public int? BitDepth { get; set; }
  392. /// <summary>
  393. /// Gets or sets the reference frames.
  394. /// </summary>
  395. /// <value>The reference frames.</value>
  396. public int? RefFrames { get; set; }
  397. /// <summary>
  398. /// Gets or sets the length of the packet.
  399. /// </summary>
  400. /// <value>The length of the packet.</value>
  401. public int? PacketLength { get; set; }
  402. /// <summary>
  403. /// Gets or sets the channels.
  404. /// </summary>
  405. /// <value>The channels.</value>
  406. public int? Channels { get; set; }
  407. /// <summary>
  408. /// Gets or sets the sample rate.
  409. /// </summary>
  410. /// <value>The sample rate.</value>
  411. public int? SampleRate { get; set; }
  412. /// <summary>
  413. /// Gets or sets a value indicating whether this instance is default.
  414. /// </summary>
  415. /// <value><c>true</c> if this instance is default; otherwise, <c>false</c>.</value>
  416. public bool IsDefault { get; set; }
  417. /// <summary>
  418. /// Gets or sets a value indicating whether this instance is forced.
  419. /// </summary>
  420. /// <value><c>true</c> if this instance is forced; otherwise, <c>false</c>.</value>
  421. public bool IsForced { get; set; }
  422. /// <summary>
  423. /// Gets or sets a value indicating whether this instance is for the hearing impaired.
  424. /// </summary>
  425. /// <value><c>true</c> if this instance is for the hearing impaired; otherwise, <c>false</c>.</value>
  426. public bool IsHearingImpaired { get; set; }
  427. /// <summary>
  428. /// Gets or sets the height.
  429. /// </summary>
  430. /// <value>The height.</value>
  431. public int? Height { get; set; }
  432. /// <summary>
  433. /// Gets or sets the width.
  434. /// </summary>
  435. /// <value>The width.</value>
  436. public int? Width { get; set; }
  437. /// <summary>
  438. /// Gets or sets the average frame rate.
  439. /// </summary>
  440. /// <value>The average frame rate.</value>
  441. public float? AverageFrameRate { get; set; }
  442. /// <summary>
  443. /// Gets or sets the real frame rate.
  444. /// </summary>
  445. /// <value>The real frame rate.</value>
  446. public float? RealFrameRate { get; set; }
  447. /// <summary>
  448. /// Gets the framerate used as reference.
  449. /// Prefer AverageFrameRate, if that is null or an unrealistic value
  450. /// then fallback to RealFrameRate.
  451. /// </summary>
  452. /// <value>The reference frame rate.</value>
  453. public float? ReferenceFrameRate
  454. {
  455. get
  456. {
  457. // In some cases AverageFrameRate for videos will be read as 1000fps even if it is not.
  458. // This is probably due to a library compatability issue.
  459. // See https://github.com/jellyfin/jellyfin/pull/12603#discussion_r1748044018 for more info.
  460. return AverageFrameRate < 1000 ? AverageFrameRate : RealFrameRate;
  461. }
  462. }
  463. /// <summary>
  464. /// Gets or sets the profile.
  465. /// </summary>
  466. /// <value>The profile.</value>
  467. public string Profile { get; set; }
  468. /// <summary>
  469. /// Gets or sets the type.
  470. /// </summary>
  471. /// <value>The type.</value>
  472. public MediaStreamType Type { get; set; }
  473. /// <summary>
  474. /// Gets or sets the aspect ratio.
  475. /// </summary>
  476. /// <value>The aspect ratio.</value>
  477. public string AspectRatio { get; set; }
  478. /// <summary>
  479. /// Gets or sets the index.
  480. /// </summary>
  481. /// <value>The index.</value>
  482. public int Index { get; set; }
  483. /// <summary>
  484. /// Gets or sets the score.
  485. /// </summary>
  486. /// <value>The score.</value>
  487. public int? Score { get; set; }
  488. /// <summary>
  489. /// Gets or sets a value indicating whether this instance is external.
  490. /// </summary>
  491. /// <value><c>true</c> if this instance is external; otherwise, <c>false</c>.</value>
  492. public bool IsExternal { get; set; }
  493. /// <summary>
  494. /// Gets or sets the method.
  495. /// </summary>
  496. /// <value>The method.</value>
  497. public SubtitleDeliveryMethod? DeliveryMethod { get; set; }
  498. /// <summary>
  499. /// Gets or sets the delivery URL.
  500. /// </summary>
  501. /// <value>The delivery URL.</value>
  502. public string DeliveryUrl { get; set; }
  503. /// <summary>
  504. /// Gets or sets a value indicating whether this instance is external URL.
  505. /// </summary>
  506. /// <value><c>null</c> if [is external URL] contains no value, <c>true</c> if [is external URL]; otherwise, <c>false</c>.</value>
  507. public bool? IsExternalUrl { get; set; }
  508. public bool IsTextSubtitleStream
  509. {
  510. get
  511. {
  512. if (Type != MediaStreamType.Subtitle)
  513. {
  514. return false;
  515. }
  516. if (string.IsNullOrEmpty(Codec) && !IsExternal)
  517. {
  518. return false;
  519. }
  520. return IsTextFormat(Codec);
  521. }
  522. }
  523. [JsonIgnore]
  524. public bool IsPgsSubtitleStream
  525. {
  526. get
  527. {
  528. if (Type != MediaStreamType.Subtitle)
  529. {
  530. return false;
  531. }
  532. if (string.IsNullOrEmpty(Codec) && !IsExternal)
  533. {
  534. return false;
  535. }
  536. return IsPgsFormat(Codec);
  537. }
  538. }
  539. /// <summary>
  540. /// Gets a value indicating whether this is a subtitle steam that is extractable by ffmpeg.
  541. /// All text-based and pgs subtitles can be extracted.
  542. /// </summary>
  543. /// <value><c>true</c> if this is a extractable subtitle steam otherwise, <c>false</c>.</value>
  544. [JsonIgnore]
  545. public bool IsExtractableSubtitleStream => IsTextSubtitleStream || IsPgsSubtitleStream;
  546. /// <summary>
  547. /// Gets or sets a value indicating whether [supports external stream].
  548. /// </summary>
  549. /// <value><c>true</c> if [supports external stream]; otherwise, <c>false</c>.</value>
  550. public bool SupportsExternalStream { get; set; }
  551. /// <summary>
  552. /// Gets or sets the filename.
  553. /// </summary>
  554. /// <value>The filename.</value>
  555. public string Path { get; set; }
  556. /// <summary>
  557. /// Gets or sets the pixel format.
  558. /// </summary>
  559. /// <value>The pixel format.</value>
  560. public string PixelFormat { get; set; }
  561. /// <summary>
  562. /// Gets or sets the level.
  563. /// </summary>
  564. /// <value>The level.</value>
  565. public double? Level { get; set; }
  566. /// <summary>
  567. /// Gets or sets whether this instance is anamorphic.
  568. /// </summary>
  569. /// <value><c>true</c> if this instance is anamorphic; otherwise, <c>false</c>.</value>
  570. public bool? IsAnamorphic { get; set; }
  571. internal string GetResolutionText()
  572. {
  573. if (!Width.HasValue || !Height.HasValue)
  574. {
  575. return null;
  576. }
  577. return Width switch
  578. {
  579. // 256x144 (16:9 square pixel format)
  580. <= 256 when Height <= 144 => IsInterlaced ? "144i" : "144p",
  581. // 426x240 (16:9 square pixel format)
  582. <= 426 when Height <= 240 => IsInterlaced ? "240i" : "240p",
  583. // 640x360 (16:9 square pixel format)
  584. <= 640 when Height <= 360 => IsInterlaced ? "360i" : "360p",
  585. // 682x384 (16:9 square pixel format)
  586. <= 682 when Height <= 384 => IsInterlaced ? "384i" : "384p",
  587. // 720x404 (16:9 square pixel format)
  588. <= 720 when Height <= 404 => IsInterlaced ? "404i" : "404p",
  589. // 854x480 (16:9 square pixel format)
  590. <= 854 when Height <= 480 => IsInterlaced ? "480i" : "480p",
  591. // 960x544 (16:9 square pixel format)
  592. <= 960 when Height <= 544 => IsInterlaced ? "540i" : "540p",
  593. // 1024x576 (16:9 square pixel format)
  594. <= 1024 when Height <= 576 => IsInterlaced ? "576i" : "576p",
  595. // 1280x720
  596. <= 1280 when Height <= 962 => IsInterlaced ? "720i" : "720p",
  597. // 2560x1080 (FHD ultra wide 21:9) using 1440px width to accommodate WQHD
  598. <= 2560 when Height <= 1440 => IsInterlaced ? "1080i" : "1080p",
  599. // 4K
  600. <= 4096 when Height <= 3072 => "4K",
  601. // 8K
  602. <= 8192 when Height <= 6144 => "8K",
  603. _ => null
  604. };
  605. }
  606. public static bool IsTextFormat(string format)
  607. {
  608. string codec = format ?? string.Empty;
  609. // microdvd and dvdsub/vobsub share the ".sub" file extension, but it's text-based.
  610. return codec.Contains("microdvd", StringComparison.OrdinalIgnoreCase)
  611. || (!codec.Contains("pgs", StringComparison.OrdinalIgnoreCase)
  612. && !codec.Contains("dvdsub", StringComparison.OrdinalIgnoreCase)
  613. && !codec.Contains("dvbsub", StringComparison.OrdinalIgnoreCase)
  614. && !string.Equals(codec, "sup", StringComparison.OrdinalIgnoreCase)
  615. && !string.Equals(codec, "sub", StringComparison.OrdinalIgnoreCase));
  616. }
  617. public static bool IsPgsFormat(string format)
  618. {
  619. string codec = format ?? string.Empty;
  620. return codec.Contains("pgs", StringComparison.OrdinalIgnoreCase)
  621. || string.Equals(codec, "sup", StringComparison.OrdinalIgnoreCase);
  622. }
  623. public bool SupportsSubtitleConversionTo(string toCodec)
  624. {
  625. if (!IsTextSubtitleStream)
  626. {
  627. return false;
  628. }
  629. var fromCodec = Codec;
  630. // Can't convert from this
  631. if (string.Equals(fromCodec, "ass", StringComparison.OrdinalIgnoreCase))
  632. {
  633. return false;
  634. }
  635. if (string.Equals(fromCodec, "ssa", StringComparison.OrdinalIgnoreCase))
  636. {
  637. return false;
  638. }
  639. // Can't convert to this
  640. if (string.Equals(toCodec, "ass", StringComparison.OrdinalIgnoreCase))
  641. {
  642. return false;
  643. }
  644. if (string.Equals(toCodec, "ssa", StringComparison.OrdinalIgnoreCase))
  645. {
  646. return false;
  647. }
  648. return true;
  649. }
  650. public (VideoRange VideoRange, VideoRangeType VideoRangeType) GetVideoColorRange()
  651. {
  652. if (Type != MediaStreamType.Video)
  653. {
  654. return (VideoRange.Unknown, VideoRangeType.Unknown);
  655. }
  656. var codecTag = CodecTag;
  657. var dvProfile = DvProfile;
  658. var rpuPresentFlag = RpuPresentFlag == 1;
  659. var blPresentFlag = BlPresentFlag == 1;
  660. var dvBlCompatId = DvBlSignalCompatibilityId;
  661. var isDoViProfile = dvProfile == 5 || dvProfile == 7 || dvProfile == 8 || dvProfile == 10;
  662. var isDoViFlag = rpuPresentFlag && blPresentFlag && (dvBlCompatId == 0 || dvBlCompatId == 1 || dvBlCompatId == 4 || dvBlCompatId == 2 || dvBlCompatId == 6);
  663. if ((isDoViProfile && isDoViFlag)
  664. || string.Equals(codecTag, "dovi", StringComparison.OrdinalIgnoreCase)
  665. || string.Equals(codecTag, "dvh1", StringComparison.OrdinalIgnoreCase)
  666. || string.Equals(codecTag, "dvhe", StringComparison.OrdinalIgnoreCase)
  667. || string.Equals(codecTag, "dav1", StringComparison.OrdinalIgnoreCase))
  668. {
  669. return dvProfile switch
  670. {
  671. 5 => (VideoRange.HDR, VideoRangeType.DOVI),
  672. 8 => dvBlCompatId switch
  673. {
  674. 1 => (VideoRange.HDR, VideoRangeType.DOVIWithHDR10),
  675. 4 => (VideoRange.HDR, VideoRangeType.DOVIWithHLG),
  676. 2 => (VideoRange.SDR, VideoRangeType.DOVIWithSDR),
  677. // While not in Dolby Spec, Profile 8 CCid 6 media are possible to create, and since CCid 6 stems from Bluray (Profile 7 originally) an HDR10 base layer is guaranteed to exist.
  678. 6 => (VideoRange.HDR, VideoRangeType.DOVIWithHDR10),
  679. // There is no other case to handle here as per Dolby Spec. Default case included for completeness and linting purposes
  680. _ => (VideoRange.SDR, VideoRangeType.SDR)
  681. },
  682. 7 => (VideoRange.HDR, VideoRangeType.HDR10),
  683. 10 => dvBlCompatId switch
  684. {
  685. 0 => (VideoRange.HDR, VideoRangeType.DOVI),
  686. 1 => (VideoRange.HDR, VideoRangeType.DOVIWithHDR10),
  687. 2 => (VideoRange.SDR, VideoRangeType.DOVIWithSDR),
  688. 4 => (VideoRange.HDR, VideoRangeType.DOVIWithHLG),
  689. // While not in Dolby Spec, Profile 8 CCid 6 media are possible to create, and since CCid 6 stems from Bluray (Profile 7 originally) an HDR10 base layer is guaranteed to exist.
  690. 6 => (VideoRange.HDR, VideoRangeType.DOVIWithHDR10),
  691. // There is no other case to handle here as per Dolby Spec. Default case included for completeness and linting purposes
  692. _ => (VideoRange.SDR, VideoRangeType.SDR)
  693. },
  694. _ => (VideoRange.SDR, VideoRangeType.SDR)
  695. };
  696. }
  697. var colorTransfer = ColorTransfer;
  698. if (string.Equals(colorTransfer, "smpte2084", StringComparison.OrdinalIgnoreCase))
  699. {
  700. return (VideoRange.HDR, VideoRangeType.HDR10);
  701. }
  702. else if (string.Equals(colorTransfer, "arib-std-b67", StringComparison.OrdinalIgnoreCase))
  703. {
  704. return (VideoRange.HDR, VideoRangeType.HLG);
  705. }
  706. return (VideoRange.SDR, VideoRangeType.SDR);
  707. }
  708. }
  709. }