MediaStream.cs 30 KB

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