MediaStream.cs 27 KB

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