MediaStream.cs 30 KB

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