MediaFormatProfileResolver.cs 20 KB

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