MediaStream.cs 28 KB

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