MediaStream.cs 20 KB

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