TSStreamClipFile.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. #undef DEBUG
  20. using System;
  21. using System.Collections.Generic;
  22. using System.IO;
  23. using System.Text;
  24. using MediaBrowser.Model.IO;
  25. namespace BDInfo
  26. {
  27. public class TSStreamClipFile
  28. {
  29. private readonly IFileSystem _fileSystem;
  30. public FileSystemMetadata FileInfo = null;
  31. public string FileType = null;
  32. public bool IsValid = false;
  33. public string Name = null;
  34. public Dictionary<ushort, TSStream> Streams =
  35. new Dictionary<ushort, TSStream>();
  36. public TSStreamClipFile(FileSystemMetadata fileInfo, IFileSystem fileSystem)
  37. {
  38. FileInfo = fileInfo;
  39. _fileSystem = fileSystem;
  40. Name = fileInfo.Name.ToUpper();
  41. }
  42. public void Scan()
  43. {
  44. Stream fileStream = null;
  45. BinaryReader fileReader = null;
  46. try
  47. {
  48. #if DEBUG
  49. Debug.WriteLine(string.Format(
  50. "Scanning {0}...", Name));
  51. #endif
  52. Streams.Clear();
  53. fileStream = File.OpenRead(FileInfo.FullName);
  54. fileReader = new BinaryReader(fileStream);
  55. byte[] data = new byte[fileStream.Length];
  56. fileReader.Read(data, 0, data.Length);
  57. byte[] fileType = new byte[8];
  58. Array.Copy(data, 0, fileType, 0, fileType.Length);
  59. FileType = Encoding.ASCII.GetString(fileType, 0, fileType.Length);
  60. if (FileType != "HDMV0100" &&
  61. FileType != "HDMV0200")
  62. {
  63. throw new Exception(string.Format(
  64. "Clip info file {0} has an unknown file type {1}.",
  65. FileInfo.Name, FileType));
  66. }
  67. #if DEBUG
  68. Debug.WriteLine(string.Format(
  69. "\tFileType: {0}", FileType));
  70. #endif
  71. int clipIndex =
  72. ((int)data[12] << 24) +
  73. ((int)data[13] << 16) +
  74. ((int)data[14] << 8) +
  75. ((int)data[15]);
  76. int clipLength =
  77. ((int)data[clipIndex] << 24) +
  78. ((int)data[clipIndex + 1] << 16) +
  79. ((int)data[clipIndex + 2] << 8) +
  80. ((int)data[clipIndex + 3]);
  81. byte[] clipData = new byte[clipLength];
  82. Array.Copy(data, clipIndex + 4, clipData, 0, clipData.Length);
  83. int streamCount = clipData[8];
  84. #if DEBUG
  85. Debug.WriteLine(string.Format(
  86. "\tStreamCount: {0}", streamCount));
  87. #endif
  88. int streamOffset = 10;
  89. for (int streamIndex = 0;
  90. streamIndex < streamCount;
  91. streamIndex++)
  92. {
  93. TSStream stream = null;
  94. ushort PID = (ushort)
  95. ((clipData[streamOffset] << 8) +
  96. clipData[streamOffset + 1]);
  97. streamOffset += 2;
  98. var streamType = (TSStreamType)
  99. clipData[streamOffset + 1];
  100. switch (streamType)
  101. {
  102. case TSStreamType.MVC_VIDEO:
  103. // TODO
  104. break;
  105. case TSStreamType.AVC_VIDEO:
  106. case TSStreamType.MPEG1_VIDEO:
  107. case TSStreamType.MPEG2_VIDEO:
  108. case TSStreamType.VC1_VIDEO:
  109. {
  110. var videoFormat = (TSVideoFormat)
  111. (clipData[streamOffset + 2] >> 4);
  112. var frameRate = (TSFrameRate)
  113. (clipData[streamOffset + 2] & 0xF);
  114. var aspectRatio = (TSAspectRatio)
  115. (clipData[streamOffset + 3] >> 4);
  116. stream = new TSVideoStream();
  117. ((TSVideoStream)stream).VideoFormat = videoFormat;
  118. ((TSVideoStream)stream).AspectRatio = aspectRatio;
  119. ((TSVideoStream)stream).FrameRate = frameRate;
  120. #if DEBUG
  121. Debug.WriteLine(string.Format(
  122. "\t{0} {1} {2} {3} {4}",
  123. PID,
  124. streamType,
  125. videoFormat,
  126. frameRate,
  127. aspectRatio));
  128. #endif
  129. }
  130. break;
  131. case TSStreamType.AC3_AUDIO:
  132. case TSStreamType.AC3_PLUS_AUDIO:
  133. case TSStreamType.AC3_PLUS_SECONDARY_AUDIO:
  134. case TSStreamType.AC3_TRUE_HD_AUDIO:
  135. case TSStreamType.DTS_AUDIO:
  136. case TSStreamType.DTS_HD_AUDIO:
  137. case TSStreamType.DTS_HD_MASTER_AUDIO:
  138. case TSStreamType.DTS_HD_SECONDARY_AUDIO:
  139. case TSStreamType.LPCM_AUDIO:
  140. case TSStreamType.MPEG1_AUDIO:
  141. case TSStreamType.MPEG2_AUDIO:
  142. {
  143. byte[] languageBytes = new byte[3];
  144. Array.Copy(clipData, streamOffset + 3,
  145. languageBytes, 0, languageBytes.Length);
  146. string languageCode = Encoding.ASCII.GetString(languageBytes, 0, languageBytes.Length);
  147. var channelLayout = (TSChannelLayout)
  148. (clipData[streamOffset + 2] >> 4);
  149. var sampleRate = (TSSampleRate)
  150. (clipData[streamOffset + 2] & 0xF);
  151. stream = new TSAudioStream();
  152. ((TSAudioStream)stream).LanguageCode = languageCode;
  153. ((TSAudioStream)stream).ChannelLayout = channelLayout;
  154. ((TSAudioStream)stream).SampleRate = TSAudioStream.ConvertSampleRate(sampleRate);
  155. ((TSAudioStream)stream).LanguageCode = languageCode;
  156. #if DEBUG
  157. Debug.WriteLine(string.Format(
  158. "\t{0} {1} {2} {3} {4}",
  159. PID,
  160. streamType,
  161. languageCode,
  162. channelLayout,
  163. sampleRate));
  164. #endif
  165. }
  166. break;
  167. case TSStreamType.INTERACTIVE_GRAPHICS:
  168. case TSStreamType.PRESENTATION_GRAPHICS:
  169. {
  170. byte[] languageBytes = new byte[3];
  171. Array.Copy(clipData, streamOffset + 2,
  172. languageBytes, 0, languageBytes.Length);
  173. string languageCode = Encoding.ASCII.GetString(languageBytes, 0, languageBytes.Length);
  174. stream = new TSGraphicsStream();
  175. stream.LanguageCode = languageCode;
  176. #if DEBUG
  177. Debug.WriteLine(string.Format(
  178. "\t{0} {1} {2}",
  179. PID,
  180. streamType,
  181. languageCode));
  182. #endif
  183. }
  184. break;
  185. case TSStreamType.SUBTITLE:
  186. {
  187. byte[] languageBytes = new byte[3];
  188. Array.Copy(clipData, streamOffset + 3,
  189. languageBytes, 0, languageBytes.Length);
  190. string languageCode = Encoding.ASCII.GetString(languageBytes, 0, languageBytes.Length);
  191. #if DEBUG
  192. Debug.WriteLine(string.Format(
  193. "\t{0} {1} {2}",
  194. PID,
  195. streamType,
  196. languageCode));
  197. #endif
  198. stream = new TSTextStream();
  199. stream.LanguageCode = languageCode;
  200. }
  201. break;
  202. }
  203. if (stream != null)
  204. {
  205. stream.PID = PID;
  206. stream.StreamType = streamType;
  207. Streams.Add(PID, stream);
  208. }
  209. streamOffset += clipData[streamOffset] + 1;
  210. }
  211. IsValid = true;
  212. }
  213. finally
  214. {
  215. if (fileReader != null) fileReader.Dispose();
  216. if (fileStream != null) fileStream.Dispose();
  217. }
  218. }
  219. }
  220. }