MediaFormatProfileResolver.cs 22 KB

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