MediaFormatProfileResolver.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using MediaBrowser.Model.MediaInfo;
  7. namespace MediaBrowser.Model.Dlna
  8. {
  9. public static class MediaFormatProfileResolver
  10. {
  11. public static MediaFormatProfile[] ResolveVideoFormat(string container, string videoCodec, string audioCodec, int? width, int? height, TransportStreamTimestamp timestampType)
  12. {
  13. if (string.Equals(container, "asf", StringComparison.OrdinalIgnoreCase))
  14. {
  15. MediaFormatProfile? val = ResolveVideoASFFormat(videoCodec, audioCodec, width, height);
  16. return val.HasValue ? new MediaFormatProfile[] { val.Value } : Array.Empty<MediaFormatProfile>();
  17. }
  18. if (string.Equals(container, "mp4", StringComparison.OrdinalIgnoreCase))
  19. {
  20. MediaFormatProfile? val = ResolveVideoMP4Format(videoCodec, audioCodec, width, height);
  21. return val.HasValue ? new MediaFormatProfile[] { val.Value } : Array.Empty<MediaFormatProfile>();
  22. }
  23. if (string.Equals(container, "avi", StringComparison.OrdinalIgnoreCase))
  24. {
  25. return new MediaFormatProfile[] { MediaFormatProfile.AVI };
  26. }
  27. if (string.Equals(container, "mkv", StringComparison.OrdinalIgnoreCase))
  28. {
  29. return new MediaFormatProfile[] { MediaFormatProfile.MATROSKA };
  30. }
  31. if (string.Equals(container, "mpeg2ps", StringComparison.OrdinalIgnoreCase) ||
  32. string.Equals(container, "ts", StringComparison.OrdinalIgnoreCase))
  33. {
  34. return new MediaFormatProfile[] { MediaFormatProfile.MPEG_PS_NTSC, MediaFormatProfile.MPEG_PS_PAL };
  35. }
  36. if (string.Equals(container, "mpeg1video", StringComparison.OrdinalIgnoreCase))
  37. {
  38. return new MediaFormatProfile[] { MediaFormatProfile.MPEG1 };
  39. }
  40. if (string.Equals(container, "mpeg2ts", StringComparison.OrdinalIgnoreCase) ||
  41. string.Equals(container, "mpegts", StringComparison.OrdinalIgnoreCase) ||
  42. string.Equals(container, "m2ts", StringComparison.OrdinalIgnoreCase))
  43. {
  44. return ResolveVideoMPEG2TSFormat(videoCodec, audioCodec, width, height, timestampType);
  45. }
  46. if (string.Equals(container, "flv", StringComparison.OrdinalIgnoreCase))
  47. {
  48. return new MediaFormatProfile[] { MediaFormatProfile.FLV };
  49. }
  50. if (string.Equals(container, "wtv", StringComparison.OrdinalIgnoreCase))
  51. {
  52. return new MediaFormatProfile[] { MediaFormatProfile.WTV };
  53. }
  54. if (string.Equals(container, "3gp", StringComparison.OrdinalIgnoreCase))
  55. {
  56. MediaFormatProfile? val = ResolveVideo3GPFormat(videoCodec, audioCodec);
  57. return val.HasValue ? new MediaFormatProfile[] { val.Value } : Array.Empty<MediaFormatProfile>();
  58. }
  59. if (string.Equals(container, "ogv", StringComparison.OrdinalIgnoreCase) || string.Equals(container, "ogg", StringComparison.OrdinalIgnoreCase))
  60. {
  61. return new MediaFormatProfile[] { MediaFormatProfile.OGV };
  62. }
  63. return Array.Empty<MediaFormatProfile>();
  64. }
  65. private static MediaFormatProfile[] ResolveVideoMPEG2TSFormat(string videoCodec, string audioCodec, int? width, int? height, TransportStreamTimestamp timestampType)
  66. {
  67. string suffix = string.Empty;
  68. switch (timestampType)
  69. {
  70. case TransportStreamTimestamp.None:
  71. suffix = "_ISO";
  72. break;
  73. case TransportStreamTimestamp.Valid:
  74. suffix = "_T";
  75. break;
  76. }
  77. string resolution = "S";
  78. if ((width.HasValue && width.Value > 720) || (height.HasValue && height.Value > 576))
  79. {
  80. resolution = "H";
  81. }
  82. if (string.Equals(videoCodec, "mpeg2video", StringComparison.OrdinalIgnoreCase))
  83. {
  84. var list = new List<MediaFormatProfile>
  85. {
  86. ValueOf("MPEG_TS_SD_NA" + suffix),
  87. ValueOf("MPEG_TS_SD_EU" + suffix),
  88. ValueOf("MPEG_TS_SD_KO" + suffix)
  89. };
  90. if ((timestampType == TransportStreamTimestamp.Valid) && string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase))
  91. {
  92. list.Add(MediaFormatProfile.MPEG_TS_JP_T);
  93. }
  94. return list.ToArray();
  95. }
  96. if (string.Equals(videoCodec, "h264", StringComparison.OrdinalIgnoreCase))
  97. {
  98. if (string.Equals(audioCodec, "lpcm", StringComparison.OrdinalIgnoreCase))
  99. {
  100. return new MediaFormatProfile[] { MediaFormatProfile.AVC_TS_HD_50_LPCM_T };
  101. }
  102. if (string.Equals(audioCodec, "dts", StringComparison.OrdinalIgnoreCase))
  103. {
  104. if (timestampType == TransportStreamTimestamp.None)
  105. {
  106. return new MediaFormatProfile[] { MediaFormatProfile.AVC_TS_HD_DTS_ISO };
  107. }
  108. return new MediaFormatProfile[] { MediaFormatProfile.AVC_TS_HD_DTS_T };
  109. }
  110. if (string.Equals(audioCodec, "mp2", StringComparison.OrdinalIgnoreCase))
  111. {
  112. if (timestampType == TransportStreamTimestamp.None)
  113. {
  114. return new MediaFormatProfile[] { ValueOf(string.Format(CultureInfo.InvariantCulture, "AVC_TS_HP_{0}D_MPEG1_L2_ISO", resolution)) };
  115. }
  116. return new MediaFormatProfile[] { ValueOf(string.Format(CultureInfo.InvariantCulture, "AVC_TS_HP_{0}D_MPEG1_L2_T", resolution)) };
  117. }
  118. if (string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase))
  119. {
  120. return new MediaFormatProfile[] { ValueOf(string.Format(CultureInfo.InvariantCulture, "AVC_TS_MP_{0}D_AAC_MULT5{1}", resolution, suffix)) };
  121. }
  122. if (string.Equals(audioCodec, "mp3", StringComparison.OrdinalIgnoreCase))
  123. {
  124. return new MediaFormatProfile[] { ValueOf(string.Format(CultureInfo.InvariantCulture, "AVC_TS_MP_{0}D_MPEG1_L3{1}", resolution, suffix)) };
  125. }
  126. if (string.IsNullOrEmpty(audioCodec) ||
  127. string.Equals(audioCodec, "ac3", StringComparison.OrdinalIgnoreCase))
  128. {
  129. return new MediaFormatProfile[] { ValueOf(string.Format(CultureInfo.InvariantCulture, "AVC_TS_MP_{0}D_AC3{1}", resolution, suffix)) };
  130. }
  131. }
  132. else if (string.Equals(videoCodec, "vc1", StringComparison.OrdinalIgnoreCase))
  133. {
  134. if (string.IsNullOrEmpty(audioCodec) || string.Equals(audioCodec, "ac3", StringComparison.OrdinalIgnoreCase))
  135. {
  136. if ((width.HasValue && width.Value > 720) || (height.HasValue && height.Value > 576))
  137. {
  138. return new MediaFormatProfile[] { MediaFormatProfile.VC1_TS_AP_L2_AC3_ISO };
  139. }
  140. return new MediaFormatProfile[] { MediaFormatProfile.VC1_TS_AP_L1_AC3_ISO };
  141. }
  142. if (string.Equals(audioCodec, "dts", StringComparison.OrdinalIgnoreCase))
  143. {
  144. suffix = string.Equals(suffix, "_ISO", StringComparison.OrdinalIgnoreCase) ? suffix : "_T";
  145. return new MediaFormatProfile[] { ValueOf(string.Format(CultureInfo.InvariantCulture, "VC1_TS_HD_DTS{0}", suffix)) };
  146. }
  147. }
  148. else if (string.Equals(videoCodec, "mpeg4", StringComparison.OrdinalIgnoreCase) || string.Equals(videoCodec, "msmpeg4", StringComparison.OrdinalIgnoreCase))
  149. {
  150. if (string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase))
  151. {
  152. return new MediaFormatProfile[] { ValueOf(string.Format(CultureInfo.InvariantCulture, "MPEG4_P2_TS_ASP_AAC{0}", suffix)) };
  153. }
  154. if (string.Equals(audioCodec, "mp3", StringComparison.OrdinalIgnoreCase))
  155. {
  156. return new MediaFormatProfile[] { ValueOf(string.Format(CultureInfo.InvariantCulture, "MPEG4_P2_TS_ASP_MPEG1_L3{0}", suffix)) };
  157. }
  158. if (string.Equals(audioCodec, "mp2", StringComparison.OrdinalIgnoreCase))
  159. {
  160. return new MediaFormatProfile[] { ValueOf(string.Format(CultureInfo.InvariantCulture, "MPEG4_P2_TS_ASP_MPEG2_L2{0}", suffix)) };
  161. }
  162. if (string.Equals(audioCodec, "ac3", StringComparison.OrdinalIgnoreCase))
  163. {
  164. return new MediaFormatProfile[] { ValueOf(string.Format(CultureInfo.InvariantCulture, "MPEG4_P2_TS_ASP_AC3{0}", suffix)) };
  165. }
  166. }
  167. return Array.Empty<MediaFormatProfile>();
  168. }
  169. private static MediaFormatProfile ValueOf(string value)
  170. {
  171. return (MediaFormatProfile)Enum.Parse(typeof(MediaFormatProfile), value, true);
  172. }
  173. private static MediaFormatProfile? ResolveVideoMP4Format(string videoCodec, string audioCodec, int? width, int? height)
  174. {
  175. if (string.Equals(videoCodec, "h264", StringComparison.OrdinalIgnoreCase))
  176. {
  177. if (string.Equals(audioCodec, "lpcm", StringComparison.OrdinalIgnoreCase))
  178. {
  179. return MediaFormatProfile.AVC_MP4_LPCM;
  180. }
  181. if (string.IsNullOrEmpty(audioCodec) ||
  182. string.Equals(audioCodec, "ac3", StringComparison.OrdinalIgnoreCase))
  183. {
  184. return MediaFormatProfile.AVC_MP4_MP_SD_AC3;
  185. }
  186. if (string.Equals(audioCodec, "mp3", StringComparison.OrdinalIgnoreCase))
  187. {
  188. return MediaFormatProfile.AVC_MP4_MP_SD_MPEG1_L3;
  189. }
  190. if (width.HasValue && height.HasValue)
  191. {
  192. if ((width.Value <= 720) && (height.Value <= 576))
  193. {
  194. if (string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase))
  195. {
  196. return MediaFormatProfile.AVC_MP4_MP_SD_AAC_MULT5;
  197. }
  198. }
  199. else if ((width.Value <= 1280) && (height.Value <= 720))
  200. {
  201. if (string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase))
  202. {
  203. return MediaFormatProfile.AVC_MP4_MP_HD_720p_AAC;
  204. }
  205. }
  206. else if ((width.Value <= 1920) && (height.Value <= 1080))
  207. {
  208. if (string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase))
  209. {
  210. return MediaFormatProfile.AVC_MP4_MP_HD_1080i_AAC;
  211. }
  212. }
  213. }
  214. }
  215. else if (string.Equals(videoCodec, "mpeg4", StringComparison.OrdinalIgnoreCase) ||
  216. string.Equals(videoCodec, "msmpeg4", StringComparison.OrdinalIgnoreCase))
  217. {
  218. if (width.HasValue && height.HasValue && width.Value <= 720 && height.Value <= 576)
  219. {
  220. if (string.IsNullOrEmpty(audioCodec) || string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase))
  221. {
  222. return MediaFormatProfile.MPEG4_P2_MP4_ASP_AAC;
  223. }
  224. if (string.Equals(audioCodec, "ac3", StringComparison.OrdinalIgnoreCase) || string.Equals(audioCodec, "mp3", StringComparison.OrdinalIgnoreCase))
  225. {
  226. return MediaFormatProfile.MPEG4_P2_MP4_NDSD;
  227. }
  228. }
  229. else if (string.IsNullOrEmpty(audioCodec) || string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase))
  230. {
  231. return MediaFormatProfile.MPEG4_P2_MP4_SP_L6_AAC;
  232. }
  233. }
  234. else if (string.Equals(videoCodec, "h263", StringComparison.OrdinalIgnoreCase) && string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase))
  235. {
  236. return MediaFormatProfile.MPEG4_H263_MP4_P0_L10_AAC;
  237. }
  238. return null;
  239. }
  240. private static MediaFormatProfile? ResolveVideo3GPFormat(string videoCodec, string audioCodec)
  241. {
  242. if (string.Equals(videoCodec, "h264", StringComparison.OrdinalIgnoreCase))
  243. {
  244. if (string.IsNullOrEmpty(audioCodec) || string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase))
  245. {
  246. return MediaFormatProfile.AVC_3GPP_BL_QCIF15_AAC;
  247. }
  248. }
  249. else if (string.Equals(videoCodec, "mpeg4", StringComparison.OrdinalIgnoreCase) ||
  250. string.Equals(videoCodec, "msmpeg4", StringComparison.OrdinalIgnoreCase))
  251. {
  252. if (string.IsNullOrEmpty(audioCodec) || string.Equals(audioCodec, "wma", StringComparison.OrdinalIgnoreCase))
  253. {
  254. return MediaFormatProfile.MPEG4_P2_3GPP_SP_L0B_AAC;
  255. }
  256. if (string.Equals(audioCodec, "amrnb", StringComparison.OrdinalIgnoreCase))
  257. {
  258. return MediaFormatProfile.MPEG4_P2_3GPP_SP_L0B_AMR;
  259. }
  260. }
  261. else if (string.Equals(videoCodec, "h263", StringComparison.OrdinalIgnoreCase) && string.Equals(audioCodec, "amrnb", StringComparison.OrdinalIgnoreCase))
  262. {
  263. return MediaFormatProfile.MPEG4_H263_3GPP_P0_L10_AMR;
  264. }
  265. return null;
  266. }
  267. private static MediaFormatProfile? ResolveVideoASFFormat(string videoCodec, string audioCodec, int? width, int? height)
  268. {
  269. if (string.Equals(videoCodec, "wmv", StringComparison.OrdinalIgnoreCase) &&
  270. (string.IsNullOrEmpty(audioCodec) || string.Equals(audioCodec, "wma", StringComparison.OrdinalIgnoreCase) || string.Equals(videoCodec, "wmapro", StringComparison.OrdinalIgnoreCase)))
  271. {
  272. if (width.HasValue && height.HasValue)
  273. {
  274. if ((width.Value <= 720) && (height.Value <= 576))
  275. {
  276. if (string.IsNullOrEmpty(audioCodec) || string.Equals(audioCodec, "wma", StringComparison.OrdinalIgnoreCase))
  277. {
  278. return MediaFormatProfile.WMVMED_FULL;
  279. }
  280. return MediaFormatProfile.WMVMED_PRO;
  281. }
  282. }
  283. if (string.IsNullOrEmpty(audioCodec) || string.Equals(audioCodec, "wma", StringComparison.OrdinalIgnoreCase))
  284. {
  285. return MediaFormatProfile.WMVHIGH_FULL;
  286. }
  287. return MediaFormatProfile.WMVHIGH_PRO;
  288. }
  289. if (string.Equals(videoCodec, "vc1", StringComparison.OrdinalIgnoreCase))
  290. {
  291. if (width.HasValue && height.HasValue)
  292. {
  293. if ((width.Value <= 720) && (height.Value <= 576))
  294. {
  295. return MediaFormatProfile.VC1_ASF_AP_L1_WMA;
  296. }
  297. if ((width.Value <= 1280) && (height.Value <= 720))
  298. {
  299. return MediaFormatProfile.VC1_ASF_AP_L2_WMA;
  300. }
  301. if ((width.Value <= 1920) && (height.Value <= 1080))
  302. {
  303. return MediaFormatProfile.VC1_ASF_AP_L3_WMA;
  304. }
  305. }
  306. }
  307. else if (string.Equals(videoCodec, "mpeg2video", StringComparison.OrdinalIgnoreCase))
  308. {
  309. return MediaFormatProfile.DVR_MS;
  310. }
  311. return null;
  312. }
  313. public static MediaFormatProfile? ResolveAudioFormat(string container, int? bitrate, int? frequency, int? channels)
  314. {
  315. if (string.Equals(container, "asf", StringComparison.OrdinalIgnoreCase))
  316. {
  317. return ResolveAudioASFFormat(bitrate);
  318. }
  319. if (string.Equals(container, "mp3", StringComparison.OrdinalIgnoreCase))
  320. {
  321. return MediaFormatProfile.MP3;
  322. }
  323. if (string.Equals(container, "lpcm", StringComparison.OrdinalIgnoreCase))
  324. {
  325. return ResolveAudioLPCMFormat(frequency, channels);
  326. }
  327. if (string.Equals(container, "mp4", StringComparison.OrdinalIgnoreCase) ||
  328. string.Equals(container, "aac", StringComparison.OrdinalIgnoreCase))
  329. {
  330. return ResolveAudioMP4Format(bitrate);
  331. }
  332. if (string.Equals(container, "adts", StringComparison.OrdinalIgnoreCase))
  333. {
  334. return ResolveAudioADTSFormat(bitrate);
  335. }
  336. if (string.Equals(container, "flac", StringComparison.OrdinalIgnoreCase))
  337. {
  338. return MediaFormatProfile.FLAC;
  339. }
  340. if (string.Equals(container, "oga", StringComparison.OrdinalIgnoreCase) ||
  341. string.Equals(container, "ogg", StringComparison.OrdinalIgnoreCase))
  342. {
  343. return MediaFormatProfile.OGG;
  344. }
  345. return null;
  346. }
  347. private static MediaFormatProfile ResolveAudioASFFormat(int? bitrate)
  348. {
  349. if (bitrate.HasValue && bitrate.Value <= 193)
  350. {
  351. return MediaFormatProfile.WMA_BASE;
  352. }
  353. return MediaFormatProfile.WMA_FULL;
  354. }
  355. private static MediaFormatProfile? ResolveAudioLPCMFormat(int? frequency, int? channels)
  356. {
  357. if (frequency.HasValue && channels.HasValue)
  358. {
  359. if (frequency.Value == 44100 && channels.Value == 1)
  360. {
  361. return MediaFormatProfile.LPCM16_44_MONO;
  362. }
  363. if (frequency.Value == 44100 && channels.Value == 2)
  364. {
  365. return MediaFormatProfile.LPCM16_44_STEREO;
  366. }
  367. if (frequency.Value == 48000 && channels.Value == 1)
  368. {
  369. return MediaFormatProfile.LPCM16_48_MONO;
  370. }
  371. if (frequency.Value == 48000 && channels.Value == 2)
  372. {
  373. return MediaFormatProfile.LPCM16_48_STEREO;
  374. }
  375. return null;
  376. }
  377. return MediaFormatProfile.LPCM16_48_STEREO;
  378. }
  379. private static MediaFormatProfile ResolveAudioMP4Format(int? bitrate)
  380. {
  381. if (bitrate.HasValue && bitrate.Value <= 320)
  382. {
  383. return MediaFormatProfile.AAC_ISO_320;
  384. }
  385. return MediaFormatProfile.AAC_ISO;
  386. }
  387. private static MediaFormatProfile ResolveAudioADTSFormat(int? bitrate)
  388. {
  389. if (bitrate.HasValue && bitrate.Value <= 320)
  390. {
  391. return MediaFormatProfile.AAC_ADTS_320;
  392. }
  393. return MediaFormatProfile.AAC_ADTS;
  394. }
  395. public static MediaFormatProfile? ResolveImageFormat(string container, int? width, int? height)
  396. {
  397. if (string.Equals(container, "jpeg", StringComparison.OrdinalIgnoreCase) ||
  398. string.Equals(container, "jpg", StringComparison.OrdinalIgnoreCase))
  399. {
  400. return ResolveImageJPGFormat(width, height);
  401. }
  402. if (string.Equals(container, "png", StringComparison.OrdinalIgnoreCase))
  403. {
  404. return ResolveImagePNGFormat(width, height);
  405. }
  406. if (string.Equals(container, "gif", StringComparison.OrdinalIgnoreCase))
  407. {
  408. return MediaFormatProfile.GIF_LRG;
  409. }
  410. if (string.Equals(container, "raw", StringComparison.OrdinalIgnoreCase))
  411. {
  412. return MediaFormatProfile.RAW;
  413. }
  414. return null;
  415. }
  416. private static MediaFormatProfile ResolveImageJPGFormat(int? width, int? height)
  417. {
  418. if (width.HasValue && height.HasValue)
  419. {
  420. if ((width.Value <= 160) && (height.Value <= 160))
  421. {
  422. return MediaFormatProfile.JPEG_TN;
  423. }
  424. if ((width.Value <= 640) && (height.Value <= 480))
  425. {
  426. return MediaFormatProfile.JPEG_SM;
  427. }
  428. if ((width.Value <= 1024) && (height.Value <= 768))
  429. {
  430. return MediaFormatProfile.JPEG_MED;
  431. }
  432. return MediaFormatProfile.JPEG_LRG;
  433. }
  434. return MediaFormatProfile.JPEG_SM;
  435. }
  436. private static MediaFormatProfile ResolveImagePNGFormat(int? width, int? height)
  437. {
  438. if (width.HasValue && height.HasValue)
  439. {
  440. if ((width.Value <= 160) && (height.Value <= 160))
  441. {
  442. return MediaFormatProfile.PNG_TN;
  443. }
  444. }
  445. return MediaFormatProfile.PNG_LRG;
  446. }
  447. }
  448. }