TSStreamClipFile.cs 10 KB

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