TSStreamClipFile.cs 9.9 KB

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