MediaStream.cs 19 KB

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