MediaStream.cs 29 KB

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