MediaFormatProfileResolver.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. using System;
  2. namespace MediaBrowser.Model.Dlna
  3. {
  4. public class MediaFormatProfileResolver
  5. {
  6. public MediaFormatProfile ResolveVideoFormat(string container, string videoCodec, string audioCodec, int? width, int? height, int? bitrate, TransportStreamTimestamp timestampType)
  7. {
  8. if (string.Equals(container, "asf", StringComparison.OrdinalIgnoreCase))
  9. return ResolveVideoASFFormat(videoCodec, audioCodec, width, height, bitrate);
  10. if (string.Equals(container, "mp4", StringComparison.OrdinalIgnoreCase))
  11. return ResolveVideoMP4Format(videoCodec, audioCodec, width, height, bitrate);
  12. if (string.Equals(container, "avi", StringComparison.OrdinalIgnoreCase))
  13. return MediaFormatProfile.AVI;
  14. if (string.Equals(container, "mkv", StringComparison.OrdinalIgnoreCase))
  15. return MediaFormatProfile.MATROSKA;
  16. if (string.Equals(container, "mpeg2ps", StringComparison.OrdinalIgnoreCase) || string.Equals(container, "ts", StringComparison.OrdinalIgnoreCase))
  17. // MediaFormatProfile.MPEG_PS_PAL, MediaFormatProfile.MPEG_PS_NTSC
  18. return MediaFormatProfile.MPEG_PS_NTSC;
  19. if (string.Equals(container, "mpeg1video", StringComparison.OrdinalIgnoreCase))
  20. return MediaFormatProfile.MPEG1;
  21. if (string.Equals(container, "mpeg2ts", StringComparison.OrdinalIgnoreCase) || string.Equals(container, "mpegts", StringComparison.OrdinalIgnoreCase) || string.Equals(container, "m2ts", StringComparison.OrdinalIgnoreCase))
  22. return ResolveVideoMPEG2TSFormat(videoCodec, audioCodec, width, height, bitrate, timestampType);
  23. if (string.Equals(container, "flv", StringComparison.OrdinalIgnoreCase))
  24. return MediaFormatProfile.FLV;
  25. if (string.Equals(container, "wtv", StringComparison.OrdinalIgnoreCase))
  26. return MediaFormatProfile.WTV;
  27. if (string.Equals(container, "3gp", StringComparison.OrdinalIgnoreCase))
  28. return ResolveVideo3GPFormat(videoCodec, audioCodec, width, height, bitrate);
  29. if (string.Equals(container, "ogv", StringComparison.OrdinalIgnoreCase) || string.Equals(container, "ogg", StringComparison.OrdinalIgnoreCase))
  30. return MediaFormatProfile.OGV;
  31. throw new ArgumentException("Unsupported container: " + container);
  32. }
  33. private MediaFormatProfile ResolveVideoMPEG2TSFormat(string videoCodec, string audioCodec, int? width, int? height, int? bitrate, TransportStreamTimestamp timestampType)
  34. {
  35. // String suffix = "";
  36. // if (isNoTimestamp(timestampType))
  37. // suffix = "_ISO";
  38. // else if (timestampType == TransportStreamTimestamp.VALID) {
  39. // suffix = "_T";
  40. // }
  41. // String resolution = "S";
  42. // if ((width.intValue() > 720) || (height.intValue() > 576)) {
  43. // resolution = "H";
  44. // }
  45. // if (videoCodec == VideoCodec.MPEG2)
  46. // {
  47. // List!(MediaFormatProfile) profiles = Arrays.asList(cast(MediaFormatProfile[])[ MediaFormatProfile.valueOf("MPEG_TS_SD_EU" + suffix), MediaFormatProfile.valueOf("MPEG_TS_SD_NA" + suffix), MediaFormatProfile.valueOf("MPEG_TS_SD_KO" + suffix) ]);
  48. // if ((timestampType == TransportStreamTimestamp.VALID) && (audioCodec == AudioCodec.AAC)) {
  49. // profiles.add(MediaFormatProfile.MPEG_TS_JP_T);
  50. // }
  51. // return profiles;
  52. // }if (videoCodec == VideoCodec.H264)
  53. // {
  54. // if (audioCodec == AudioCodec.LPCM)
  55. // return Collections.singletonList(MediaFormatProfile.AVC_TS_HD_50_LPCM_T);
  56. // if (audioCodec == AudioCodec.DTS) {
  57. // if (isNoTimestamp(timestampType)) {
  58. // return Collections.singletonList(MediaFormatProfile.AVC_TS_HD_DTS_ISO);
  59. // }
  60. // return Collections.singletonList(MediaFormatProfile.AVC_TS_HD_DTS_T);
  61. // }
  62. // if (audioCodec == AudioCodec.MP2) {
  63. // if (isNoTimestamp(timestampType)) {
  64. // return Collections.singletonList(MediaFormatProfile.valueOf(String.format("AVC_TS_HP_%sD_MPEG1_L2_ISO", cast(Object[])[ resolution ])));
  65. // }
  66. // return Collections.singletonList(MediaFormatProfile.valueOf(String.format("AVC_TS_HP_%sD_MPEG1_L2_T", cast(Object[])[ resolution ])));
  67. // }
  68. // if (audioCodec == AudioCodec.AAC)
  69. // return Collections.singletonList(MediaFormatProfile.valueOf(String.format("AVC_TS_MP_%sD_AAC_MULT5%s", cast(Object[])[ resolution, suffix ])));
  70. // if (audioCodec == AudioCodec.MP3)
  71. // return Collections.singletonList(MediaFormatProfile.valueOf(String.format("AVC_TS_MP_%sD_MPEG1_L3%s", cast(Object[])[ resolution, suffix ])));
  72. // if ((audioCodec is null) || (audioCodec == AudioCodec.AC3)) {
  73. // return Collections.singletonList(MediaFormatProfile.valueOf(String.format("AVC_TS_MP_%sD_AC3%s", cast(Object[])[ resolution, suffix ])));
  74. // }
  75. // }
  76. // else if (videoCodec == VideoCodec.VC1) {
  77. // if ((audioCodec is null) || (audioCodec == AudioCodec.AC3))
  78. // {
  79. // if ((width.intValue() > 720) || (height.intValue() > 576)) {
  80. // return Collections.singletonList(MediaFormatProfile.VC1_TS_AP_L2_AC3_ISO);
  81. // }
  82. // return Collections.singletonList(MediaFormatProfile.VC1_TS_AP_L1_AC3_ISO);
  83. // }
  84. // if (audioCodec == AudioCodec.DTS) {
  85. // suffix = suffix.equals("_ISO") ? suffix : "_T";
  86. // return Collections.singletonList(MediaFormatProfile.valueOf(String.format("VC1_TS_HD_DTS%s", cast(Object[])[ suffix ])));
  87. // }
  88. // } else if ((videoCodec == VideoCodec.MPEG4) || (videoCodec == VideoCodec.MSMPEG4)) {
  89. // if (audioCodec == AudioCodec.AAC)
  90. // return Collections.singletonList(MediaFormatProfile.valueOf(String.format("MPEG4_P2_TS_ASP_AAC%s", cast(Object[])[ suffix ])));
  91. // if (audioCodec == AudioCodec.MP3)
  92. // return Collections.singletonList(MediaFormatProfile.valueOf(String.format("MPEG4_P2_TS_ASP_MPEG1_L3%s", cast(Object[])[ suffix ])));
  93. // if (audioCodec == AudioCodec.MP2)
  94. // return Collections.singletonList(MediaFormatProfile.valueOf(String.format("MPEG4_P2_TS_ASP_MPEG2_L2%s", cast(Object[])[ suffix ])));
  95. // if ((audioCodec is null) || (audioCodec == AudioCodec.AC3)) {
  96. // return Collections.singletonList(MediaFormatProfile.valueOf(String.format("MPEG4_P2_TS_ASP_AC3%s", cast(Object[])[ suffix ])));
  97. // }
  98. // }
  99. throw new ArgumentException("Mpeg video file does not match any supported DLNA profile");
  100. }
  101. private MediaFormatProfile ResolveVideoMP4Format(string videoCodec, string audioCodec, int? width, int? height, int? bitrate)
  102. {
  103. if (string.Equals(videoCodec, "h264", StringComparison.OrdinalIgnoreCase))
  104. {
  105. if (string.Equals(audioCodec, "lpcm", StringComparison.OrdinalIgnoreCase))
  106. return MediaFormatProfile.AVC_MP4_LPCM;
  107. if (string.IsNullOrEmpty(audioCodec) ||
  108. string.Equals(audioCodec, "ac3", StringComparison.OrdinalIgnoreCase))
  109. {
  110. return MediaFormatProfile.AVC_MP4_MP_SD_AC3;
  111. }
  112. if (string.Equals(audioCodec, "mp3", StringComparison.OrdinalIgnoreCase))
  113. {
  114. return MediaFormatProfile.AVC_MP4_MP_SD_MPEG1_L3;
  115. }
  116. if (width.HasValue && height.HasValue)
  117. {
  118. if ((width.Value <= 720) && (height.Value <= 576))
  119. {
  120. if (string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase))
  121. return MediaFormatProfile.AVC_MP4_MP_SD_AAC_MULT5;
  122. }
  123. else if ((width.Value <= 1280) && (height.Value <= 720))
  124. {
  125. if (string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase))
  126. return MediaFormatProfile.AVC_MP4_MP_HD_720p_AAC;
  127. }
  128. else if ((width.Value <= 1920) && (height.Value <= 1080))
  129. {
  130. if (string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase))
  131. {
  132. return MediaFormatProfile.AVC_MP4_MP_HD_1080i_AAC;
  133. }
  134. }
  135. }
  136. }
  137. else if (string.Equals(videoCodec, "mpeg4", StringComparison.OrdinalIgnoreCase) ||
  138. string.Equals(videoCodec, "msmpeg4", StringComparison.OrdinalIgnoreCase))
  139. {
  140. if (width.HasValue && height.HasValue && width.Value <= 720 && height.Value <= 576)
  141. {
  142. if (string.IsNullOrEmpty(audioCodec) || string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase))
  143. return MediaFormatProfile.MPEG4_P2_MP4_ASP_AAC;
  144. if (string.Equals(audioCodec, "ac3", StringComparison.OrdinalIgnoreCase) || string.Equals(audioCodec, "mp3", StringComparison.OrdinalIgnoreCase))
  145. {
  146. return MediaFormatProfile.MPEG4_P2_MP4_NDSD;
  147. }
  148. }
  149. else if (string.IsNullOrEmpty(audioCodec) || string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase))
  150. {
  151. return MediaFormatProfile.MPEG4_P2_MP4_SP_L6_AAC;
  152. }
  153. }
  154. else if (string.Equals(videoCodec, "h263", StringComparison.OrdinalIgnoreCase) && string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase))
  155. {
  156. return MediaFormatProfile.MPEG4_H263_MP4_P0_L10_AAC;
  157. }
  158. throw new ArgumentException("MP4 video file does not match any supported DLNA profile");
  159. }
  160. private MediaFormatProfile ResolveVideo3GPFormat(string videoCodec, string audioCodec, int? width, int? height, int? bitrate)
  161. {
  162. if (string.Equals(videoCodec, "h264", StringComparison.OrdinalIgnoreCase))
  163. {
  164. if (string.IsNullOrEmpty(audioCodec) || string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase))
  165. return MediaFormatProfile.AVC_3GPP_BL_QCIF15_AAC;
  166. }
  167. else if (string.Equals(videoCodec, "mpeg4", StringComparison.OrdinalIgnoreCase) ||
  168. string.Equals(videoCodec, "msmpeg4", StringComparison.OrdinalIgnoreCase))
  169. {
  170. if (string.IsNullOrEmpty(audioCodec) || string.Equals(audioCodec, "wma", StringComparison.OrdinalIgnoreCase))
  171. return MediaFormatProfile.MPEG4_P2_3GPP_SP_L0B_AAC;
  172. if (string.Equals(audioCodec, "amrnb", StringComparison.OrdinalIgnoreCase))
  173. return MediaFormatProfile.MPEG4_P2_3GPP_SP_L0B_AMR;
  174. }
  175. else if (string.Equals(videoCodec, "h263", StringComparison.OrdinalIgnoreCase) && string.Equals(audioCodec, "amrnb", StringComparison.OrdinalIgnoreCase))
  176. {
  177. return MediaFormatProfile.MPEG4_H263_3GPP_P0_L10_AMR;
  178. }
  179. throw new ArgumentException("3GP video file does not match any supported DLNA profile");
  180. }
  181. private MediaFormatProfile ResolveVideoASFFormat(string videoCodec, string audioCodec, int? width, int? height, int? bitrate)
  182. {
  183. if (string.Equals(videoCodec, "wmv", StringComparison.OrdinalIgnoreCase) &&
  184. (string.IsNullOrEmpty(audioCodec) || string.Equals(audioCodec, "wma", StringComparison.OrdinalIgnoreCase) || string.Equals(videoCodec, "wmapro", StringComparison.OrdinalIgnoreCase)))
  185. {
  186. if (width.HasValue && height.HasValue)
  187. {
  188. if ((width.Value <= 720) && (height.Value <= 576))
  189. {
  190. if (string.IsNullOrEmpty(audioCodec) || string.Equals(audioCodec, "wma", StringComparison.OrdinalIgnoreCase))
  191. {
  192. return MediaFormatProfile.WMVMED_FULL;
  193. }
  194. return MediaFormatProfile.WMVMED_PRO;
  195. }
  196. }
  197. if (string.IsNullOrEmpty(audioCodec) || string.Equals(audioCodec, "wma", StringComparison.OrdinalIgnoreCase))
  198. {
  199. return MediaFormatProfile.WMVHIGH_FULL;
  200. }
  201. return MediaFormatProfile.WMVHIGH_PRO;
  202. }
  203. if (string.Equals(videoCodec, "vc1", StringComparison.OrdinalIgnoreCase))
  204. {
  205. if (width.HasValue && height.HasValue)
  206. {
  207. if ((width.Value <= 720) && (height.Value <= 576))
  208. return MediaFormatProfile.VC1_ASF_AP_L1_WMA;
  209. if ((width.Value <= 1280) && (height.Value <= 720))
  210. return MediaFormatProfile.VC1_ASF_AP_L2_WMA;
  211. if ((width.Value <= 1920) && (height.Value <= 1080))
  212. return MediaFormatProfile.VC1_ASF_AP_L3_WMA;
  213. }
  214. }
  215. else if (string.Equals(videoCodec, "mpeg2video", StringComparison.OrdinalIgnoreCase))
  216. {
  217. return MediaFormatProfile.DVR_MS;
  218. }
  219. throw new ArgumentException("ASF video file does not match any supported DLNA profile");
  220. }
  221. public MediaFormatProfile ResolveAudioFormat(string container, int? bitrate, int? frequency, int? channels)
  222. {
  223. if (string.Equals(container, "asf", StringComparison.OrdinalIgnoreCase))
  224. return ResolveAudioASFFormat(bitrate, frequency, channels);
  225. if (string.Equals(container, "mp3", StringComparison.OrdinalIgnoreCase))
  226. return MediaFormatProfile.MP3;
  227. if (string.Equals(container, "lpcm", StringComparison.OrdinalIgnoreCase))
  228. return ResolveAudioLPCMFormat(bitrate, frequency, channels);
  229. if (string.Equals(container, "mp4", StringComparison.OrdinalIgnoreCase))
  230. return ResolveAudioMP4Format(bitrate, frequency, channels);
  231. if (string.Equals(container, "adts", StringComparison.OrdinalIgnoreCase))
  232. return ResolveAudioADTSFormat(bitrate, frequency, channels);
  233. if (string.Equals(container, "flac", StringComparison.OrdinalIgnoreCase))
  234. return MediaFormatProfile.FLAC;
  235. if (string.Equals(container, "oga", StringComparison.OrdinalIgnoreCase) || string.Equals(container, "ogg", StringComparison.OrdinalIgnoreCase))
  236. return MediaFormatProfile.OGG;
  237. throw new ArgumentException("Unsupported container: " + container);
  238. }
  239. private MediaFormatProfile ResolveAudioASFFormat(int? bitrate, int? frequency, int? channels)
  240. {
  241. if (bitrate.HasValue && bitrate.Value <= 193)
  242. {
  243. return MediaFormatProfile.WMA_BASE;
  244. }
  245. return MediaFormatProfile.WMA_FULL;
  246. }
  247. private MediaFormatProfile ResolveAudioLPCMFormat(int? bitrate, int? frequency, int? channels)
  248. {
  249. if (frequency.HasValue && channels.HasValue)
  250. {
  251. if (frequency.Value == 44100 && channels.Value == 1)
  252. {
  253. return MediaFormatProfile.LPCM16_44_MONO;
  254. }
  255. if (frequency.Value == 44100 && channels.Value == 2)
  256. {
  257. return MediaFormatProfile.LPCM16_44_STEREO;
  258. }
  259. if (frequency.Value == 48000 && channels.Value == 1)
  260. {
  261. return MediaFormatProfile.LPCM16_48_MONO;
  262. }
  263. if (frequency.Value == 48000 && channels.Value == 1)
  264. {
  265. return MediaFormatProfile.LPCM16_48_STEREO;
  266. }
  267. throw new ArgumentException("Unsupported LPCM format of file %s. Only 44100 / 48000 Hz and Mono / Stereo files are allowed.");
  268. }
  269. return MediaFormatProfile.LPCM16_48_STEREO;
  270. }
  271. private MediaFormatProfile ResolveAudioMP4Format(int? bitrate, int? frequency, int? channels)
  272. {
  273. if (bitrate.HasValue && bitrate.Value <= 320)
  274. {
  275. return MediaFormatProfile.AAC_ISO_320;
  276. }
  277. return MediaFormatProfile.AAC_ISO;
  278. }
  279. private MediaFormatProfile ResolveAudioADTSFormat(int? bitrate, int? frequency, int? channels)
  280. {
  281. if (bitrate.HasValue && bitrate.Value <= 320)
  282. {
  283. return MediaFormatProfile.AAC_ADTS_320;
  284. }
  285. return MediaFormatProfile.AAC_ADTS;
  286. }
  287. public MediaFormatProfile ResolveImageFormat(string container, int? width, int? height)
  288. {
  289. if (string.Equals(container, "jpeg", StringComparison.OrdinalIgnoreCase) ||
  290. string.Equals(container, "jpg", StringComparison.OrdinalIgnoreCase))
  291. return ResolveImageJPGFormat(width, height);
  292. if (string.Equals(container, "png", StringComparison.OrdinalIgnoreCase))
  293. return MediaFormatProfile.PNG_LRG;
  294. if (string.Equals(container, "gif", StringComparison.OrdinalIgnoreCase))
  295. return MediaFormatProfile.GIF_LRG;
  296. if (string.Equals(container, "raw", StringComparison.OrdinalIgnoreCase))
  297. return MediaFormatProfile.RAW;
  298. throw new ArgumentException("Unsupported container: " + container);
  299. }
  300. private MediaFormatProfile ResolveImageJPGFormat(int? width, int? height)
  301. {
  302. if (width.HasValue && height.HasValue)
  303. {
  304. if ((width.Value <= 640) && (height.Value <= 480))
  305. return MediaFormatProfile.JPEG_SM;
  306. if ((width.Value <= 1024) && (height.Value <= 768))
  307. {
  308. return MediaFormatProfile.JPEG_MED;
  309. }
  310. }
  311. return MediaFormatProfile.JPEG_LRG;
  312. }
  313. }
  314. }