MediaFormatProfileResolver.cs 19 KB

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