2
0

MediaStream.cs 30 KB

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