BDROM.cs 15 KB

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