MediaStream.cs 30 KB

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