BDROM.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. namespace BDInfo
  23. {
  24. public class BDROM
  25. {
  26. public DirectoryInfo DirectoryRoot = null;
  27. public DirectoryInfo DirectoryBDMV = null;
  28. public DirectoryInfo DirectoryBDJO = null;
  29. public DirectoryInfo DirectoryCLIPINF = null;
  30. public DirectoryInfo DirectoryPLAYLIST = null;
  31. public DirectoryInfo DirectorySNP = null;
  32. public DirectoryInfo DirectorySSIF = null;
  33. public DirectoryInfo DirectorySTREAM = null;
  34. public string VolumeLabel = null;
  35. public ulong Size = 0;
  36. public bool IsBDPlus = false;
  37. public bool IsBDJava = false;
  38. public bool IsDBOX = false;
  39. public bool IsPSP = false;
  40. public bool Is3D = false;
  41. public bool Is50Hz = false;
  42. public Dictionary<string, TSPlaylistFile> PlaylistFiles =
  43. new Dictionary<string, TSPlaylistFile>();
  44. public Dictionary<string, TSStreamClipFile> StreamClipFiles =
  45. new Dictionary<string, TSStreamClipFile>();
  46. public Dictionary<string, TSStreamFile> StreamFiles =
  47. new Dictionary<string, TSStreamFile>();
  48. public Dictionary<string, TSInterleavedFile> InterleavedFiles =
  49. new Dictionary<string, TSInterleavedFile>();
  50. private static List<string> ExcludeDirs = new List<string> { "ANY!", "AACS", "BDSVM", "ANYVM", "SLYVM" };
  51. public delegate bool OnStreamClipFileScanError(
  52. TSStreamClipFile streamClipFile, Exception ex);
  53. public event OnStreamClipFileScanError StreamClipFileScanError;
  54. public delegate bool OnStreamFileScanError(
  55. TSStreamFile streamClipFile, Exception ex);
  56. public event OnStreamFileScanError StreamFileScanError;
  57. public delegate bool OnPlaylistFileScanError(
  58. TSPlaylistFile playlistFile, Exception ex);
  59. public event OnPlaylistFileScanError PlaylistFileScanError;
  60. public BDROM(
  61. string path)
  62. {
  63. //
  64. // Locate BDMV directories.
  65. //
  66. DirectoryBDMV =
  67. GetDirectoryBDMV(path);
  68. if (DirectoryBDMV == null)
  69. {
  70. throw new Exception("Unable to locate BD structure.");
  71. }
  72. DirectoryRoot =
  73. DirectoryBDMV.Parent;
  74. DirectoryBDJO =
  75. GetDirectory("BDJO", DirectoryBDMV, 0);
  76. DirectoryCLIPINF =
  77. GetDirectory("CLIPINF", DirectoryBDMV, 0);
  78. DirectoryPLAYLIST =
  79. GetDirectory("PLAYLIST", DirectoryBDMV, 0);
  80. DirectorySNP =
  81. GetDirectory("SNP", DirectoryRoot, 0);
  82. DirectorySTREAM =
  83. GetDirectory("STREAM", DirectoryBDMV, 0);
  84. DirectorySSIF =
  85. GetDirectory("SSIF", DirectorySTREAM, 0);
  86. if (DirectoryCLIPINF == null
  87. || DirectoryPLAYLIST == null)
  88. {
  89. throw new Exception("Unable to locate BD structure.");
  90. }
  91. //
  92. // Initialize basic disc properties.
  93. //
  94. VolumeLabel = GetVolumeLabel(DirectoryRoot);
  95. Size = (ulong)GetDirectorySize(DirectoryRoot);
  96. if (null != GetDirectory("BDSVM", DirectoryRoot, 0))
  97. {
  98. IsBDPlus = true;
  99. }
  100. if (null != GetDirectory("SLYVM", DirectoryRoot, 0))
  101. {
  102. IsBDPlus = true;
  103. }
  104. if (null != GetDirectory("ANYVM", DirectoryRoot, 0))
  105. {
  106. IsBDPlus = true;
  107. }
  108. if (DirectoryBDJO != null &&
  109. DirectoryBDJO.GetFiles().Length > 0)
  110. {
  111. IsBDJava = true;
  112. }
  113. if (DirectorySNP != null &&
  114. (DirectorySNP.GetFiles("*.mnv").Length > 0 || DirectorySNP.GetFiles("*.MNV").Length > 0))
  115. {
  116. IsPSP = true;
  117. }
  118. if (DirectorySSIF != null &&
  119. DirectorySSIF.GetFiles().Length > 0)
  120. {
  121. Is3D = true;
  122. }
  123. if (File.Exists(Path.Combine(DirectoryRoot.FullName, "FilmIndex.xml")))
  124. {
  125. IsDBOX = true;
  126. }
  127. //
  128. // Initialize file lists.
  129. //
  130. if (DirectoryPLAYLIST != null)
  131. {
  132. FileInfo[] files = DirectoryPLAYLIST.GetFiles("*.mpls");
  133. if (files.Length == 0)
  134. {
  135. files = DirectoryPLAYLIST.GetFiles("*.MPLS");
  136. }
  137. foreach (FileInfo file in files)
  138. {
  139. PlaylistFiles.Add(
  140. file.Name.ToUpper(), new TSPlaylistFile(this, file));
  141. }
  142. }
  143. if (DirectorySTREAM != null)
  144. {
  145. FileInfo[] files = DirectorySTREAM.GetFiles("*.m2ts");
  146. if (files.Length == 0)
  147. {
  148. files = DirectoryPLAYLIST.GetFiles("*.M2TS");
  149. }
  150. foreach (FileInfo file in files)
  151. {
  152. StreamFiles.Add(
  153. file.Name.ToUpper(), new TSStreamFile(file));
  154. }
  155. }
  156. if (DirectoryCLIPINF != null)
  157. {
  158. FileInfo[] files = DirectoryCLIPINF.GetFiles("*.clpi");
  159. if (files.Length == 0)
  160. {
  161. files = DirectoryPLAYLIST.GetFiles("*.CLPI");
  162. }
  163. foreach (FileInfo file in files)
  164. {
  165. StreamClipFiles.Add(
  166. file.Name.ToUpper(), new TSStreamClipFile(file));
  167. }
  168. }
  169. if (DirectorySSIF != null)
  170. {
  171. FileInfo[] files = DirectorySSIF.GetFiles("*.ssif");
  172. if (files.Length == 0)
  173. {
  174. files = DirectorySSIF.GetFiles("*.SSIF");
  175. }
  176. foreach (FileInfo file in files)
  177. {
  178. InterleavedFiles.Add(
  179. file.Name.ToUpper(), new TSInterleavedFile(file));
  180. }
  181. }
  182. }
  183. public void Scan()
  184. {
  185. List<TSStreamClipFile> errorStreamClipFiles = new List<TSStreamClipFile>();
  186. foreach (TSStreamClipFile streamClipFile in StreamClipFiles.Values)
  187. {
  188. try
  189. {
  190. streamClipFile.Scan();
  191. }
  192. catch (Exception ex)
  193. {
  194. errorStreamClipFiles.Add(streamClipFile);
  195. if (StreamClipFileScanError != null)
  196. {
  197. if (StreamClipFileScanError(streamClipFile, ex))
  198. {
  199. continue;
  200. }
  201. else
  202. {
  203. break;
  204. }
  205. }
  206. else throw ex;
  207. }
  208. }
  209. foreach (TSStreamFile streamFile in StreamFiles.Values)
  210. {
  211. string ssifName = Path.GetFileNameWithoutExtension(streamFile.Name) + ".SSIF";
  212. if (InterleavedFiles.ContainsKey(ssifName))
  213. {
  214. streamFile.InterleavedFile = InterleavedFiles[ssifName];
  215. }
  216. }
  217. TSStreamFile[] streamFiles = new TSStreamFile[StreamFiles.Count];
  218. StreamFiles.Values.CopyTo(streamFiles, 0);
  219. Array.Sort(streamFiles, CompareStreamFiles);
  220. List<TSPlaylistFile> errorPlaylistFiles = new List<TSPlaylistFile>();
  221. foreach (TSPlaylistFile playlistFile in PlaylistFiles.Values)
  222. {
  223. try
  224. {
  225. playlistFile.Scan(StreamFiles, StreamClipFiles);
  226. }
  227. catch (Exception ex)
  228. {
  229. errorPlaylistFiles.Add(playlistFile);
  230. if (PlaylistFileScanError != null)
  231. {
  232. if (PlaylistFileScanError(playlistFile, ex))
  233. {
  234. continue;
  235. }
  236. else
  237. {
  238. break;
  239. }
  240. }
  241. else throw ex;
  242. }
  243. }
  244. List<TSStreamFile> errorStreamFiles = new List<TSStreamFile>();
  245. foreach (TSStreamFile streamFile in streamFiles)
  246. {
  247. try
  248. {
  249. List<TSPlaylistFile> playlists = new List<TSPlaylistFile>();
  250. foreach (TSPlaylistFile playlist in PlaylistFiles.Values)
  251. {
  252. foreach (TSStreamClip streamClip in playlist.StreamClips)
  253. {
  254. if (streamClip.Name == streamFile.Name)
  255. {
  256. playlists.Add(playlist);
  257. break;
  258. }
  259. }
  260. }
  261. streamFile.Scan(playlists, false);
  262. }
  263. catch (Exception ex)
  264. {
  265. errorStreamFiles.Add(streamFile);
  266. if (StreamFileScanError != null)
  267. {
  268. if (StreamFileScanError(streamFile, ex))
  269. {
  270. continue;
  271. }
  272. else
  273. {
  274. break;
  275. }
  276. }
  277. else throw ex;
  278. }
  279. }
  280. foreach (TSPlaylistFile playlistFile in PlaylistFiles.Values)
  281. {
  282. playlistFile.Initialize();
  283. if (!Is50Hz)
  284. {
  285. foreach (TSVideoStream videoStream in playlistFile.VideoStreams)
  286. {
  287. if (videoStream.FrameRate == TSFrameRate.FRAMERATE_25 ||
  288. videoStream.FrameRate == TSFrameRate.FRAMERATE_50)
  289. {
  290. Is50Hz = true;
  291. }
  292. }
  293. }
  294. }
  295. }
  296. private DirectoryInfo GetDirectoryBDMV(
  297. string path)
  298. {
  299. DirectoryInfo dir = new DirectoryInfo(path);
  300. while (dir != null)
  301. {
  302. if (dir.Name == "BDMV")
  303. {
  304. return dir;
  305. }
  306. dir = dir.Parent;
  307. }
  308. return GetDirectory("BDMV", new DirectoryInfo(path), 0);
  309. }
  310. private DirectoryInfo GetDirectory(
  311. string name,
  312. DirectoryInfo dir,
  313. int searchDepth)
  314. {
  315. if (dir != null)
  316. {
  317. DirectoryInfo[] children = dir.GetDirectories();
  318. foreach (DirectoryInfo child in children)
  319. {
  320. if (child.Name == name)
  321. {
  322. return child;
  323. }
  324. }
  325. if (searchDepth > 0)
  326. {
  327. foreach (DirectoryInfo child in children)
  328. {
  329. GetDirectory(
  330. name, child, searchDepth - 1);
  331. }
  332. }
  333. }
  334. return null;
  335. }
  336. private long GetDirectorySize(DirectoryInfo directoryInfo)
  337. {
  338. long size = 0;
  339. //if (!ExcludeDirs.Contains(directoryInfo.Name.ToUpper())) // TODO: Keep?
  340. {
  341. FileInfo[] pathFiles = directoryInfo.GetFiles();
  342. foreach (FileInfo pathFile in pathFiles)
  343. {
  344. if (pathFile.Extension.ToUpper() == ".SSIF")
  345. {
  346. continue;
  347. }
  348. size += pathFile.Length;
  349. }
  350. DirectoryInfo[] pathChildren = directoryInfo.GetDirectories();
  351. foreach (DirectoryInfo pathChild in pathChildren)
  352. {
  353. size += GetDirectorySize(pathChild);
  354. }
  355. }
  356. return size;
  357. }
  358. private string GetVolumeLabel(DirectoryInfo dir)
  359. {
  360. return dir.Name;
  361. }
  362. public static int CompareStreamFiles(
  363. TSStreamFile x,
  364. TSStreamFile y)
  365. {
  366. // TODO: Use interleaved file sizes
  367. if ((x == null || x.FileInfo == null) && (y == null || y.FileInfo == null))
  368. {
  369. return 0;
  370. }
  371. else if ((x == null || x.FileInfo == null) && (y != null && y.FileInfo != null))
  372. {
  373. return 1;
  374. }
  375. else if ((x != null || x.FileInfo != null) && (y == null || y.FileInfo == null))
  376. {
  377. return -1;
  378. }
  379. else
  380. {
  381. if (x.FileInfo.Length > y.FileInfo.Length)
  382. {
  383. return 1;
  384. }
  385. else if (y.FileInfo.Length > x.FileInfo.Length)
  386. {
  387. return -1;
  388. }
  389. else
  390. {
  391. return 0;
  392. }
  393. }
  394. }
  395. }
  396. }