MediaStream.cs 32 KB

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