TSStreamClipFile.cs 10 KB

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