BDROM.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. var errorStreamClipFiles = new List<TSStreamClipFile>();
  184. foreach (var streamClipFile in StreamClipFiles.Values)
  185. {
  186. try
  187. {
  188. streamClipFile.Scan();
  189. }
  190. catch (Exception ex)
  191. {
  192. errorStreamClipFiles.Add(streamClipFile);
  193. if (StreamClipFileScanError != null)
  194. {
  195. if (StreamClipFileScanError(streamClipFile, ex))
  196. {
  197. continue;
  198. }
  199. else
  200. {
  201. break;
  202. }
  203. }
  204. else throw;
  205. }
  206. }
  207. foreach (var streamFile in StreamFiles.Values)
  208. {
  209. string ssifName = Path.GetFileNameWithoutExtension(streamFile.Name) + ".SSIF";
  210. if (InterleavedFiles.ContainsKey(ssifName))
  211. {
  212. streamFile.InterleavedFile = InterleavedFiles[ssifName];
  213. }
  214. }
  215. TSStreamFile[] streamFiles = new TSStreamFile[StreamFiles.Count];
  216. StreamFiles.Values.CopyTo(streamFiles, 0);
  217. Array.Sort(streamFiles, CompareStreamFiles);
  218. var errorPlaylistFiles = new List<TSPlaylistFile>();
  219. foreach (var playlistFile in PlaylistFiles.Values)
  220. {
  221. try
  222. {
  223. playlistFile.Scan(StreamFiles, StreamClipFiles);
  224. }
  225. catch (Exception ex)
  226. {
  227. errorPlaylistFiles.Add(playlistFile);
  228. if (PlaylistFileScanError != null)
  229. {
  230. if (PlaylistFileScanError(playlistFile, ex))
  231. {
  232. continue;
  233. }
  234. else
  235. {
  236. break;
  237. }
  238. }
  239. else throw;
  240. }
  241. }
  242. var errorStreamFiles = new List<TSStreamFile>();
  243. foreach (var streamFile in streamFiles)
  244. {
  245. try
  246. {
  247. var playlists = new List<TSPlaylistFile>();
  248. foreach (var playlist in PlaylistFiles.Values)
  249. {
  250. foreach (var streamClip in playlist.StreamClips)
  251. {
  252. if (streamClip.Name == streamFile.Name)
  253. {
  254. playlists.Add(playlist);
  255. break;
  256. }
  257. }
  258. }
  259. streamFile.Scan(playlists, false);
  260. }
  261. catch (Exception ex)
  262. {
  263. errorStreamFiles.Add(streamFile);
  264. if (StreamFileScanError != null)
  265. {
  266. if (StreamFileScanError(streamFile, ex))
  267. {
  268. continue;
  269. }
  270. else
  271. {
  272. break;
  273. }
  274. }
  275. else throw;
  276. }
  277. }
  278. foreach (var playlistFile in PlaylistFiles.Values)
  279. {
  280. playlistFile.Initialize();
  281. if (!Is50Hz)
  282. {
  283. foreach (var videoStream in playlistFile.VideoStreams)
  284. {
  285. if (videoStream.FrameRate == TSFrameRate.FRAMERATE_25 ||
  286. videoStream.FrameRate == TSFrameRate.FRAMERATE_50)
  287. {
  288. Is50Hz = true;
  289. }
  290. }
  291. }
  292. }
  293. }
  294. private FileSystemMetadata GetDirectoryBDMV(
  295. string path)
  296. {
  297. if (string.IsNullOrEmpty(path))
  298. {
  299. throw new ArgumentNullException(nameof(path));
  300. }
  301. FileSystemMetadata dir = _fileSystem.GetDirectoryInfo(path);
  302. while (dir != null)
  303. {
  304. if (string.Equals(dir.Name, "BDMV", StringComparison.OrdinalIgnoreCase))
  305. {
  306. return dir;
  307. }
  308. var parentFolder = Path.GetDirectoryName(dir.FullName);
  309. if (string.IsNullOrEmpty(parentFolder))
  310. {
  311. dir = null;
  312. }
  313. else
  314. {
  315. dir = _fileSystem.GetDirectoryInfo(parentFolder);
  316. }
  317. }
  318. return GetDirectory("BDMV", _fileSystem.GetDirectoryInfo(path), 0);
  319. }
  320. private FileSystemMetadata GetDirectory(
  321. string name,
  322. FileSystemMetadata dir,
  323. int searchDepth)
  324. {
  325. if (dir != null)
  326. {
  327. FileSystemMetadata[] children = _fileSystem.GetDirectories(dir.FullName).ToArray();
  328. foreach (var child in children)
  329. {
  330. if (string.Equals(child.Name, name, StringComparison.OrdinalIgnoreCase))
  331. {
  332. return child;
  333. }
  334. }
  335. if (searchDepth > 0)
  336. {
  337. foreach (var child in children)
  338. {
  339. GetDirectory(
  340. name, child, searchDepth - 1);
  341. }
  342. }
  343. }
  344. return null;
  345. }
  346. private long GetDirectorySize(FileSystemMetadata directoryInfo)
  347. {
  348. long size = 0;
  349. //if (!ExcludeDirs.Contains(directoryInfo.Name.ToUpper())) // TODO: Keep?
  350. {
  351. FileSystemMetadata[] pathFiles = _fileSystem.GetFiles(directoryInfo.FullName).ToArray();
  352. foreach (var pathFile in pathFiles)
  353. {
  354. if (pathFile.Extension.ToUpper() == ".SSIF")
  355. {
  356. continue;
  357. }
  358. size += pathFile.Length;
  359. }
  360. FileSystemMetadata[] pathChildren = _fileSystem.GetDirectories(directoryInfo.FullName).ToArray();
  361. foreach (var pathChild in pathChildren)
  362. {
  363. size += GetDirectorySize(pathChild);
  364. }
  365. }
  366. return size;
  367. }
  368. private string GetVolumeLabel(FileSystemMetadata dir)
  369. {
  370. return dir.Name;
  371. }
  372. public int CompareStreamFiles(
  373. TSStreamFile x,
  374. TSStreamFile y)
  375. {
  376. // TODO: Use interleaved file sizes
  377. if ((x == null || x.FileInfo == null) && (y == null || y.FileInfo == null))
  378. {
  379. return 0;
  380. }
  381. else if ((x == null || x.FileInfo == null) && (y != null && y.FileInfo != null))
  382. {
  383. return 1;
  384. }
  385. else if ((x != null || x.FileInfo != null) && (y == null || y.FileInfo == null))
  386. {
  387. return -1;
  388. }
  389. else
  390. {
  391. if (x.FileInfo.Length > y.FileInfo.Length)
  392. {
  393. return 1;
  394. }
  395. else if (y.FileInfo.Length > x.FileInfo.Length)
  396. {
  397. return -1;
  398. }
  399. else
  400. {
  401. return 0;
  402. }
  403. }
  404. }
  405. }
  406. }