MediaStream.cs 19 KB

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