MediaFormatProfileResolver.cs 20 KB

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