MediaStream.cs 24 KB

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