TSStreamClipFile.cs 9.8 KB

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