BDROM.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. //============================================================================
  2. // BDInfo - Blu-ray Video and Audio Analysis Tool
  3. // Copyright © 2010 Cinema Squid
  4. //
  5. // This library is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU Lesser General Public
  7. // License as published by the Free Software Foundation; either
  8. // version 2.1 of the License, or (at your option) any later version.
  9. //
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. // Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public
  16. // License along with this library; if not, write to the Free Software
  17. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. //=============================================================================
  19. using System;
  20. using System.Collections.Generic;
  21. using System.IO;
  22. using System.Linq;
  23. using MediaBrowser.Model.IO;
  24. using MediaBrowser.Model.Text;
  25. namespace BDInfo
  26. {
  27. public class BDROM
  28. {
  29. public FileSystemMetadata DirectoryRoot = null;
  30. public FileSystemMetadata DirectoryBDMV = null;
  31. public FileSystemMetadata DirectoryBDJO = null;
  32. public FileSystemMetadata DirectoryCLIPINF = null;
  33. public FileSystemMetadata DirectoryPLAYLIST = null;
  34. public FileSystemMetadata DirectorySNP = null;
  35. public FileSystemMetadata DirectorySSIF = null;
  36. public FileSystemMetadata DirectorySTREAM = null;
  37. public string VolumeLabel = null;
  38. public ulong Size = 0;
  39. public bool IsBDPlus = false;
  40. public bool IsBDJava = false;
  41. public bool IsDBOX = false;
  42. public bool IsPSP = false;
  43. public bool Is3D = false;
  44. public bool Is50Hz = false;
  45. private readonly IFileSystem _fileSystem;
  46. public Dictionary<string, TSPlaylistFile> PlaylistFiles =
  47. new Dictionary<string, TSPlaylistFile>();
  48. public Dictionary<string, TSStreamClipFile> StreamClipFiles =
  49. new Dictionary<string, TSStreamClipFile>();
  50. public Dictionary<string, TSStreamFile> StreamFiles =
  51. new Dictionary<string, TSStreamFile>();
  52. public Dictionary<string, TSInterleavedFile> InterleavedFiles =
  53. new Dictionary<string, TSInterleavedFile>();
  54. private static List<string> ExcludeDirs = new List<string> { "ANY!", "AACS", "BDSVM", "ANYVM", "SLYVM" };
  55. public delegate bool OnStreamClipFileScanError(
  56. TSStreamClipFile streamClipFile, Exception ex);
  57. public event OnStreamClipFileScanError StreamClipFileScanError;
  58. public delegate bool OnStreamFileScanError(
  59. TSStreamFile streamClipFile, Exception ex);
  60. public event OnStreamFileScanError StreamFileScanError;
  61. public delegate bool OnPlaylistFileScanError(
  62. TSPlaylistFile playlistFile, Exception ex);
  63. public event OnPlaylistFileScanError PlaylistFileScanError;
  64. public BDROM(
  65. string path, IFileSystem fileSystem, ITextEncoding textEncoding)
  66. {
  67. _fileSystem = fileSystem;
  68. //
  69. // Locate BDMV directories.
  70. //
  71. DirectoryBDMV =
  72. GetDirectoryBDMV(path);
  73. if (DirectoryBDMV == null)
  74. {
  75. throw new Exception("Unable to locate BD structure.");
  76. }
  77. DirectoryRoot =
  78. _fileSystem.GetDirectoryInfo(Path.GetDirectoryName(DirectoryBDMV.FullName));
  79. DirectoryBDJO =
  80. GetDirectory("BDJO", DirectoryBDMV, 0);
  81. DirectoryCLIPINF =
  82. GetDirectory("CLIPINF", DirectoryBDMV, 0);
  83. DirectoryPLAYLIST =
  84. GetDirectory("PLAYLIST", DirectoryBDMV, 0);
  85. DirectorySNP =
  86. GetDirectory("SNP", DirectoryRoot, 0);
  87. DirectorySTREAM =
  88. GetDirectory("STREAM", DirectoryBDMV, 0);
  89. DirectorySSIF =
  90. GetDirectory("SSIF", DirectorySTREAM, 0);
  91. if (DirectoryCLIPINF == null
  92. || DirectoryPLAYLIST == null)
  93. {
  94. throw new Exception("Unable to locate BD structure.");
  95. }
  96. //
  97. // Initialize basic disc properties.
  98. //
  99. VolumeLabel = GetVolumeLabel(DirectoryRoot);
  100. Size = (ulong)GetDirectorySize(DirectoryRoot);
  101. if (null != GetDirectory("BDSVM", DirectoryRoot, 0))
  102. {
  103. IsBDPlus = true;
  104. }
  105. if (null != GetDirectory("SLYVM", DirectoryRoot, 0))
  106. {
  107. IsBDPlus = true;
  108. }
  109. if (null != GetDirectory("ANYVM", DirectoryRoot, 0))
  110. {
  111. IsBDPlus = true;
  112. }
  113. if (DirectoryBDJO != null &&
  114. _fileSystem.GetFiles(DirectoryBDJO.FullName).Any())
  115. {
  116. IsBDJava = true;
  117. }
  118. if (DirectorySNP != null &&
  119. GetFiles(DirectorySNP.FullName, ".mnv").Any())
  120. {
  121. IsPSP = true;
  122. }
  123. if (DirectorySSIF != null &&
  124. _fileSystem.GetFiles(DirectorySSIF.FullName).Any())
  125. {
  126. Is3D = true;
  127. }
  128. if (_fileSystem.FileExists(Path.Combine(DirectoryRoot.FullName, "FilmIndex.xml")))
  129. {
  130. IsDBOX = true;
  131. }
  132. //
  133. // Initialize file lists.
  134. //
  135. if (DirectoryPLAYLIST != null)
  136. {
  137. FileSystemMetadata[] files = GetFiles(DirectoryPLAYLIST.FullName, ".mpls").ToArray();
  138. foreach (FileSystemMetadata file in files)
  139. {
  140. PlaylistFiles.Add(
  141. file.Name.ToUpper(), new TSPlaylistFile(this, file, _fileSystem, textEncoding));
  142. }
  143. }
  144. if (DirectorySTREAM != null)
  145. {
  146. FileSystemMetadata[] files = GetFiles(DirectorySTREAM.FullName, ".m2ts").ToArray();
  147. foreach (FileSystemMetadata file in files)
  148. {
  149. StreamFiles.Add(
  150. file.Name.ToUpper(), new TSStreamFile(file, _fileSystem));
  151. }
  152. }
  153. if (DirectoryCLIPINF != null)
  154. {
  155. FileSystemMetadata[] files = GetFiles(DirectoryCLIPINF.FullName, ".clpi").ToArray();
  156. foreach (FileSystemMetadata file in files)
  157. {
  158. StreamClipFiles.Add(
  159. file.Name.ToUpper(), new TSStreamClipFile(file, _fileSystem, textEncoding));
  160. }
  161. }
  162. if (DirectorySSIF != null)
  163. {
  164. FileSystemMetadata[] files = GetFiles(DirectorySSIF.FullName, ".ssif").ToArray();
  165. foreach (FileSystemMetadata file in files)
  166. {
  167. InterleavedFiles.Add(
  168. file.Name.ToUpper(), new TSInterleavedFile(file));
  169. }
  170. }
  171. }
  172. private IEnumerable<FileSystemMetadata> GetFiles(string path, string extension)
  173. {
  174. return _fileSystem.GetFiles(path).Where(i => string.Equals(i.Extension, extension, StringComparison.OrdinalIgnoreCase));
  175. }
  176. public void Scan()
  177. {
  178. List<TSStreamClipFile> errorStreamClipFiles = new List<TSStreamClipFile>();
  179. foreach (TSStreamClipFile streamClipFile in StreamClipFiles.Values)
  180. {
  181. try
  182. {
  183. streamClipFile.Scan();
  184. }
  185. catch (Exception ex)
  186. {
  187. errorStreamClipFiles.Add(streamClipFile);
  188. if (StreamClipFileScanError != null)
  189. {
  190. if (StreamClipFileScanError(streamClipFile, ex))
  191. {
  192. continue;
  193. }
  194. else
  195. {
  196. break;
  197. }
  198. }
  199. else throw ex;
  200. }
  201. }
  202. foreach (TSStreamFile streamFile in StreamFiles.Values)
  203. {
  204. string ssifName = Path.GetFileNameWithoutExtension(streamFile.Name) + ".SSIF";
  205. if (InterleavedFiles.ContainsKey(ssifName))
  206. {
  207. streamFile.InterleavedFile = InterleavedFiles[ssifName];
  208. }
  209. }
  210. TSStreamFile[] streamFiles = new TSStreamFile[StreamFiles.Count];
  211. StreamFiles.Values.CopyTo(streamFiles, 0);
  212. Array.Sort(streamFiles, CompareStreamFiles);
  213. List<TSPlaylistFile> errorPlaylistFiles = new List<TSPlaylistFile>();
  214. foreach (TSPlaylistFile playlistFile in PlaylistFiles.Values)
  215. {
  216. try
  217. {
  218. playlistFile.Scan(StreamFiles, StreamClipFiles);
  219. }
  220. catch (Exception ex)
  221. {
  222. errorPlaylistFiles.Add(playlistFile);
  223. if (PlaylistFileScanError != null)
  224. {
  225. if (PlaylistFileScanError(playlistFile, ex))
  226. {
  227. continue;
  228. }
  229. else
  230. {
  231. break;
  232. }
  233. }
  234. else throw ex;
  235. }
  236. }
  237. List<TSStreamFile> errorStreamFiles = new List<TSStreamFile>();
  238. foreach (TSStreamFile streamFile in streamFiles)
  239. {
  240. try
  241. {
  242. List<TSPlaylistFile> playlists = new List<TSPlaylistFile>();
  243. foreach (TSPlaylistFile playlist in PlaylistFiles.Values)
  244. {
  245. foreach (TSStreamClip streamClip in playlist.StreamClips)
  246. {
  247. if (streamClip.Name == streamFile.Name)
  248. {
  249. playlists.Add(playlist);
  250. break;
  251. }
  252. }
  253. }
  254. streamFile.Scan(playlists, false);
  255. }
  256. catch (Exception ex)
  257. {
  258. errorStreamFiles.Add(streamFile);
  259. if (StreamFileScanError != null)
  260. {
  261. if (StreamFileScanError(streamFile, ex))
  262. {
  263. continue;
  264. }
  265. else
  266. {
  267. break;
  268. }
  269. }
  270. else throw ex;
  271. }
  272. }
  273. foreach (TSPlaylistFile playlistFile in PlaylistFiles.Values)
  274. {
  275. playlistFile.Initialize();
  276. if (!Is50Hz)
  277. {
  278. foreach (TSVideoStream videoStream in playlistFile.VideoStreams)
  279. {
  280. if (videoStream.FrameRate == TSFrameRate.FRAMERATE_25 ||
  281. videoStream.FrameRate == TSFrameRate.FRAMERATE_50)
  282. {
  283. Is50Hz = true;
  284. }
  285. }
  286. }
  287. }
  288. }
  289. private FileSystemMetadata GetDirectoryBDMV(
  290. string path)
  291. {
  292. FileSystemMetadata dir = _fileSystem.GetDirectoryInfo(path);
  293. while (dir != null)
  294. {
  295. if (dir.Name == "BDMV")
  296. {
  297. return dir;
  298. }
  299. dir = _fileSystem.GetDirectoryInfo(Path.GetDirectoryName(dir.FullName));
  300. }
  301. return GetDirectory("BDMV", _fileSystem.GetDirectoryInfo(path), 0);
  302. }
  303. private FileSystemMetadata GetDirectory(
  304. string name,
  305. FileSystemMetadata dir,
  306. int searchDepth)
  307. {
  308. if (dir != null)
  309. {
  310. FileSystemMetadata[] children = _fileSystem.GetDirectories(dir.FullName).ToArray();
  311. foreach (FileSystemMetadata child in children)
  312. {
  313. if (child.Name == name)
  314. {
  315. return child;
  316. }
  317. }
  318. if (searchDepth > 0)
  319. {
  320. foreach (FileSystemMetadata child in children)
  321. {
  322. GetDirectory(
  323. name, child, searchDepth - 1);
  324. }
  325. }
  326. }
  327. return null;
  328. }
  329. private long GetDirectorySize(FileSystemMetadata directoryInfo)
  330. {
  331. long size = 0;
  332. //if (!ExcludeDirs.Contains(directoryInfo.Name.ToUpper())) // TODO: Keep?
  333. {
  334. FileSystemMetadata[] pathFiles = _fileSystem.GetFiles(directoryInfo.FullName).ToArray();
  335. foreach (FileSystemMetadata pathFile in pathFiles)
  336. {
  337. if (pathFile.Extension.ToUpper() == ".SSIF")
  338. {
  339. continue;
  340. }
  341. size += pathFile.Length;
  342. }
  343. FileSystemMetadata[] pathChildren = _fileSystem.GetDirectories(directoryInfo.FullName).ToArray();
  344. foreach (FileSystemMetadata pathChild in pathChildren)
  345. {
  346. size += GetDirectorySize(pathChild);
  347. }
  348. }
  349. return size;
  350. }
  351. private string GetVolumeLabel(FileSystemMetadata dir)
  352. {
  353. return dir.Name;
  354. }
  355. public static int CompareStreamFiles(
  356. TSStreamFile x,
  357. TSStreamFile y)
  358. {
  359. // TODO: Use interleaved file sizes
  360. if ((x == null || x.FileInfo == null) && (y == null || y.FileInfo == null))
  361. {
  362. return 0;
  363. }
  364. else if ((x == null || x.FileInfo == null) && (y != null && y.FileInfo != null))
  365. {
  366. return 1;
  367. }
  368. else if ((x != null || x.FileInfo != null) && (y == null || y.FileInfo == null))
  369. {
  370. return -1;
  371. }
  372. else
  373. {
  374. if (x.FileInfo.Length > y.FileInfo.Length)
  375. {
  376. return 1;
  377. }
  378. else if (y.FileInfo.Length > x.FileInfo.Length)
  379. {
  380. return -1;
  381. }
  382. else
  383. {
  384. return 0;
  385. }
  386. }
  387. }
  388. }
  389. }