MediaFormatProfileResolver.cs 22 KB

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