MediaFormatProfileResolver.cs 22 KB

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