MediaFormatProfileResolver.cs 18 KB

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