MediaStream.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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 comment.
  67. /// </summary>
  68. /// <value>The comment.</value>
  69. public string Comment { get; set; }
  70. /// <summary>
  71. /// Gets or sets the time base.
  72. /// </summary>
  73. /// <value>The time base.</value>
  74. public string TimeBase { get; set; }
  75. /// <summary>
  76. /// Gets or sets the codec time base.
  77. /// </summary>
  78. /// <value>The codec time base.</value>
  79. public string CodecTimeBase { get; set; }
  80. /// <summary>
  81. /// Gets or sets the title.
  82. /// </summary>
  83. /// <value>The title.</value>
  84. public string Title { get; set; }
  85. /// <summary>
  86. /// Gets the video range.
  87. /// </summary>
  88. /// <value>The video range.</value>
  89. public string VideoRange
  90. {
  91. get
  92. {
  93. if (Type != MediaStreamType.Video)
  94. {
  95. return null;
  96. }
  97. var colorTransfer = ColorTransfer;
  98. if (string.Equals(colorTransfer, "smpte2084", StringComparison.OrdinalIgnoreCase)
  99. || string.Equals(colorTransfer, "arib-std-b67", StringComparison.OrdinalIgnoreCase))
  100. {
  101. return "HDR";
  102. }
  103. // For some Dolby Vision files, no color transfer is provided, so check the codec
  104. var codecTag = CodecTag;
  105. if (string.Equals(codecTag, "dva1", StringComparison.OrdinalIgnoreCase)
  106. || string.Equals(codecTag, "dvav", StringComparison.OrdinalIgnoreCase)
  107. || string.Equals(codecTag, "dvh1", StringComparison.OrdinalIgnoreCase)
  108. || string.Equals(codecTag, "dvhe", StringComparison.OrdinalIgnoreCase)
  109. || string.Equals(codecTag, "dav1", StringComparison.OrdinalIgnoreCase))
  110. {
  111. return "HDR";
  112. }
  113. return "SDR";
  114. }
  115. }
  116. public string LocalizedUndefined { get; set; }
  117. public string LocalizedDefault { get; set; }
  118. public string LocalizedForced { get; set; }
  119. public string LocalizedExternal { get; set; }
  120. public string DisplayTitle
  121. {
  122. get
  123. {
  124. switch (Type)
  125. {
  126. case MediaStreamType.Audio:
  127. {
  128. var attributes = new List<string>();
  129. // 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).
  130. if (!string.IsNullOrEmpty(Language) && !_specialCodes.Contains(Language, StringComparison.OrdinalIgnoreCase))
  131. {
  132. // Get full language string i.e. eng -> English. Will not work for some languages which use ISO 639-2/B instead of /T codes.
  133. string fullLanguage = CultureInfo
  134. .GetCultures(CultureTypes.NeutralCultures)
  135. .FirstOrDefault(r => r.ThreeLetterISOLanguageName.Equals(Language, StringComparison.OrdinalIgnoreCase))
  136. ?.DisplayName;
  137. attributes.Add(StringHelper.FirstToUpper(fullLanguage ?? Language));
  138. }
  139. if (!string.IsNullOrEmpty(Codec) && !string.Equals(Codec, "dca", StringComparison.OrdinalIgnoreCase) && !string.Equals(Codec, "dts", StringComparison.OrdinalIgnoreCase))
  140. {
  141. attributes.Add(AudioCodec.GetFriendlyName(Codec));
  142. }
  143. else if (!string.IsNullOrEmpty(Profile) && !string.Equals(Profile, "lc", StringComparison.OrdinalIgnoreCase))
  144. {
  145. attributes.Add(Profile);
  146. }
  147. if (!string.IsNullOrEmpty(ChannelLayout))
  148. {
  149. attributes.Add(StringHelper.FirstToUpper(ChannelLayout));
  150. }
  151. else if (Channels.HasValue)
  152. {
  153. attributes.Add(Channels.Value.ToString(CultureInfo.InvariantCulture) + " ch");
  154. }
  155. if (IsDefault)
  156. {
  157. attributes.Add(string.IsNullOrEmpty(LocalizedDefault) ? "Default" : LocalizedDefault);
  158. }
  159. if (IsExternal)
  160. {
  161. attributes.Add(string.IsNullOrEmpty(LocalizedExternal) ? "External" : LocalizedExternal);
  162. }
  163. if (!string.IsNullOrEmpty(Title))
  164. {
  165. var result = new StringBuilder(Title);
  166. foreach (var tag in attributes)
  167. {
  168. // Keep Tags that are not already in Title.
  169. if (!Title.Contains(tag, StringComparison.OrdinalIgnoreCase))
  170. {
  171. result.Append(" - ").Append(tag);
  172. }
  173. }
  174. return result.ToString();
  175. }
  176. return string.Join(" - ", attributes);
  177. }
  178. case MediaStreamType.Video:
  179. {
  180. var attributes = new List<string>();
  181. var resolutionText = GetResolutionText();
  182. if (!string.IsNullOrEmpty(resolutionText))
  183. {
  184. attributes.Add(resolutionText);
  185. }
  186. if (!string.IsNullOrEmpty(Codec))
  187. {
  188. attributes.Add(Codec.ToUpperInvariant());
  189. }
  190. if (!string.IsNullOrEmpty(VideoRange))
  191. {
  192. attributes.Add(VideoRange.ToUpperInvariant());
  193. }
  194. if (!string.IsNullOrEmpty(Title))
  195. {
  196. var result = new StringBuilder(Title);
  197. foreach (var tag in attributes)
  198. {
  199. // Keep Tags that are not already in Title.
  200. if (!Title.Contains(tag, StringComparison.OrdinalIgnoreCase))
  201. {
  202. result.Append(" - ").Append(tag);
  203. }
  204. }
  205. return result.ToString();
  206. }
  207. return string.Join(' ', attributes);
  208. }
  209. case MediaStreamType.Subtitle:
  210. {
  211. var attributes = new List<string>();
  212. if (!string.IsNullOrEmpty(Language))
  213. {
  214. // Get full language string i.e. eng -> English. Will not work for some languages which use ISO 639-2/B instead of /T codes.
  215. string fullLanguage = CultureInfo
  216. .GetCultures(CultureTypes.NeutralCultures)
  217. .FirstOrDefault(r => r.ThreeLetterISOLanguageName.Equals(Language, StringComparison.OrdinalIgnoreCase))
  218. ?.DisplayName;
  219. attributes.Add(StringHelper.FirstToUpper(fullLanguage ?? Language));
  220. }
  221. else
  222. {
  223. attributes.Add(string.IsNullOrEmpty(LocalizedUndefined) ? "Und" : LocalizedUndefined);
  224. }
  225. if (IsDefault)
  226. {
  227. attributes.Add(string.IsNullOrEmpty(LocalizedDefault) ? "Default" : LocalizedDefault);
  228. }
  229. if (IsForced)
  230. {
  231. attributes.Add(string.IsNullOrEmpty(LocalizedForced) ? "Forced" : LocalizedForced);
  232. }
  233. if (!string.IsNullOrEmpty(Codec))
  234. {
  235. attributes.Add(Codec.ToUpperInvariant());
  236. }
  237. if (IsExternal)
  238. {
  239. attributes.Add(string.IsNullOrEmpty(LocalizedExternal) ? "External" : LocalizedExternal);
  240. }
  241. if (!string.IsNullOrEmpty(Title))
  242. {
  243. var result = new StringBuilder(Title);
  244. foreach (var tag in attributes)
  245. {
  246. // Keep Tags that are not already in Title.
  247. if (!Title.Contains(tag, StringComparison.OrdinalIgnoreCase))
  248. {
  249. result.Append(" - ").Append(tag);
  250. }
  251. }
  252. return result.ToString();
  253. }
  254. return string.Join(" - ", attributes);
  255. }
  256. default:
  257. return null;
  258. }
  259. }
  260. }
  261. public string NalLengthSize { get; set; }
  262. /// <summary>
  263. /// Gets or sets a value indicating whether this instance is interlaced.
  264. /// </summary>
  265. /// <value><c>true</c> if this instance is interlaced; otherwise, <c>false</c>.</value>
  266. public bool IsInterlaced { get; set; }
  267. public bool? IsAVC { get; set; }
  268. /// <summary>
  269. /// Gets or sets the channel layout.
  270. /// </summary>
  271. /// <value>The channel layout.</value>
  272. public string ChannelLayout { get; set; }
  273. /// <summary>
  274. /// Gets or sets the bit rate.
  275. /// </summary>
  276. /// <value>The bit rate.</value>
  277. public int? BitRate { get; set; }
  278. /// <summary>
  279. /// Gets or sets the bit depth.
  280. /// </summary>
  281. /// <value>The bit depth.</value>
  282. public int? BitDepth { get; set; }
  283. /// <summary>
  284. /// Gets or sets the reference frames.
  285. /// </summary>
  286. /// <value>The reference frames.</value>
  287. public int? RefFrames { get; set; }
  288. /// <summary>
  289. /// Gets or sets the length of the packet.
  290. /// </summary>
  291. /// <value>The length of the packet.</value>
  292. public int? PacketLength { get; set; }
  293. /// <summary>
  294. /// Gets or sets the channels.
  295. /// </summary>
  296. /// <value>The channels.</value>
  297. public int? Channels { get; set; }
  298. /// <summary>
  299. /// Gets or sets the sample rate.
  300. /// </summary>
  301. /// <value>The sample rate.</value>
  302. public int? SampleRate { get; set; }
  303. /// <summary>
  304. /// Gets or sets a value indicating whether this instance is default.
  305. /// </summary>
  306. /// <value><c>true</c> if this instance is default; otherwise, <c>false</c>.</value>
  307. public bool IsDefault { get; set; }
  308. /// <summary>
  309. /// Gets or sets a value indicating whether this instance is forced.
  310. /// </summary>
  311. /// <value><c>true</c> if this instance is forced; otherwise, <c>false</c>.</value>
  312. public bool IsForced { get; set; }
  313. /// <summary>
  314. /// Gets or sets the height.
  315. /// </summary>
  316. /// <value>The height.</value>
  317. public int? Height { get; set; }
  318. /// <summary>
  319. /// Gets or sets the width.
  320. /// </summary>
  321. /// <value>The width.</value>
  322. public int? Width { get; set; }
  323. /// <summary>
  324. /// Gets or sets the average frame rate.
  325. /// </summary>
  326. /// <value>The average frame rate.</value>
  327. public float? AverageFrameRate { get; set; }
  328. /// <summary>
  329. /// Gets or sets the real frame rate.
  330. /// </summary>
  331. /// <value>The real frame rate.</value>
  332. public float? RealFrameRate { get; set; }
  333. /// <summary>
  334. /// Gets or sets the profile.
  335. /// </summary>
  336. /// <value>The profile.</value>
  337. public string Profile { get; set; }
  338. /// <summary>
  339. /// Gets or sets the type.
  340. /// </summary>
  341. /// <value>The type.</value>
  342. public MediaStreamType Type { get; set; }
  343. /// <summary>
  344. /// Gets or sets the aspect ratio.
  345. /// </summary>
  346. /// <value>The aspect ratio.</value>
  347. public string AspectRatio { get; set; }
  348. /// <summary>
  349. /// Gets or sets the index.
  350. /// </summary>
  351. /// <value>The index.</value>
  352. public int Index { get; set; }
  353. /// <summary>
  354. /// Gets or sets the score.
  355. /// </summary>
  356. /// <value>The score.</value>
  357. public int? Score { get; set; }
  358. /// <summary>
  359. /// Gets or sets a value indicating whether this instance is external.
  360. /// </summary>
  361. /// <value><c>true</c> if this instance is external; otherwise, <c>false</c>.</value>
  362. public bool IsExternal { get; set; }
  363. /// <summary>
  364. /// Gets or sets the method.
  365. /// </summary>
  366. /// <value>The method.</value>
  367. public SubtitleDeliveryMethod? DeliveryMethod { get; set; }
  368. /// <summary>
  369. /// Gets or sets the delivery URL.
  370. /// </summary>
  371. /// <value>The delivery URL.</value>
  372. public string DeliveryUrl { get; set; }
  373. /// <summary>
  374. /// Gets or sets a value indicating whether this instance is external URL.
  375. /// </summary>
  376. /// <value><c>null</c> if [is external URL] contains no value, <c>true</c> if [is external URL]; otherwise, <c>false</c>.</value>
  377. public bool? IsExternalUrl { get; set; }
  378. public bool IsTextSubtitleStream
  379. {
  380. get
  381. {
  382. if (Type != MediaStreamType.Subtitle)
  383. {
  384. return false;
  385. }
  386. if (string.IsNullOrEmpty(Codec) && !IsExternal)
  387. {
  388. return false;
  389. }
  390. return IsTextFormat(Codec);
  391. }
  392. }
  393. /// <summary>
  394. /// Gets or sets a value indicating whether [supports external stream].
  395. /// </summary>
  396. /// <value><c>true</c> if [supports external stream]; otherwise, <c>false</c>.</value>
  397. public bool SupportsExternalStream { get; set; }
  398. /// <summary>
  399. /// Gets or sets the filename.
  400. /// </summary>
  401. /// <value>The filename.</value>
  402. public string Path { get; set; }
  403. /// <summary>
  404. /// Gets or sets the pixel format.
  405. /// </summary>
  406. /// <value>The pixel format.</value>
  407. public string PixelFormat { get; set; }
  408. /// <summary>
  409. /// Gets or sets the level.
  410. /// </summary>
  411. /// <value>The level.</value>
  412. public double? Level { get; set; }
  413. /// <summary>
  414. /// Gets or sets whether this instance is anamorphic.
  415. /// </summary>
  416. /// <value><c>true</c> if this instance is anamorphic; otherwise, <c>false</c>.</value>
  417. public bool? IsAnamorphic { get; set; }
  418. internal string GetResolutionText()
  419. {
  420. if (!Width.HasValue || !Height.HasValue)
  421. {
  422. return null;
  423. }
  424. return Width switch
  425. {
  426. <= 720 when Height <= 480 => IsInterlaced ? "480i" : "480p",
  427. // 720x576 (PAL) (768 when rescaled for square pixels)
  428. <= 768 when Height <= 576 => IsInterlaced ? "576i" : "576p",
  429. // 960x540 (sometimes 544 which is multiple of 16)
  430. <= 960 when Height <= 544 => IsInterlaced ? "540i" : "540p",
  431. // 1280x720
  432. <= 1280 when Height <= 962 => IsInterlaced ? "720i" : "720p",
  433. // 1920x1080
  434. <= 1920 when Height <= 1440 => IsInterlaced ? "1080i" : "1080p",
  435. // 4K
  436. <= 4096 when Height <= 3072 => "4K",
  437. // 8K
  438. <= 8192 when Height <= 6144 => "8K",
  439. _ => null
  440. };
  441. }
  442. public static bool IsTextFormat(string format)
  443. {
  444. string codec = format ?? string.Empty;
  445. // sub = external .sub file
  446. return !codec.Contains("pgs", StringComparison.OrdinalIgnoreCase) &&
  447. !codec.Contains("dvd", StringComparison.OrdinalIgnoreCase) &&
  448. !codec.Contains("dvbsub", StringComparison.OrdinalIgnoreCase) &&
  449. !string.Equals(codec, "sub", StringComparison.OrdinalIgnoreCase) &&
  450. !string.Equals(codec, "dvb_subtitle", StringComparison.OrdinalIgnoreCase);
  451. }
  452. public bool SupportsSubtitleConversionTo(string toCodec)
  453. {
  454. if (!IsTextSubtitleStream)
  455. {
  456. return false;
  457. }
  458. var fromCodec = Codec;
  459. // Can't convert from this
  460. if (string.Equals(fromCodec, "ass", StringComparison.OrdinalIgnoreCase))
  461. {
  462. return false;
  463. }
  464. if (string.Equals(fromCodec, "ssa", StringComparison.OrdinalIgnoreCase))
  465. {
  466. return false;
  467. }
  468. // Can't convert to this
  469. if (string.Equals(toCodec, "ass", StringComparison.OrdinalIgnoreCase))
  470. {
  471. return false;
  472. }
  473. if (string.Equals(toCodec, "ssa", StringComparison.OrdinalIgnoreCase))
  474. {
  475. return false;
  476. }
  477. return true;
  478. }
  479. }
  480. }