TSStreamClipFile.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. namespace BDInfo
  25. {
  26. public class TSStreamClipFile
  27. {
  28. public FileInfo FileInfo = null;
  29. public string FileType = null;
  30. public bool IsValid = false;
  31. public string Name = null;
  32. public Dictionary<ushort, TSStream> Streams =
  33. new Dictionary<ushort,TSStream>();
  34. public TSStreamClipFile(
  35. FileInfo fileInfo)
  36. {
  37. FileInfo = fileInfo;
  38. Name = fileInfo.Name.ToUpper();
  39. }
  40. public void Scan()
  41. {
  42. FileStream 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 = ASCIIEncoding.ASCII.GetString(fileType);
  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. TSStreamType 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. TSVideoFormat videoFormat = (TSVideoFormat)
  109. (clipData[streamOffset + 2] >> 4);
  110. TSFrameRate frameRate = (TSFrameRate)
  111. (clipData[streamOffset + 2] & 0xF);
  112. TSAspectRatio 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 =
  145. ASCIIEncoding.ASCII.GetString(languageBytes);
  146. TSChannelLayout channelLayout = (TSChannelLayout)
  147. (clipData[streamOffset + 2] >> 4);
  148. TSSampleRate sampleRate = (TSSampleRate)
  149. (clipData[streamOffset + 2] & 0xF);
  150. stream = new TSAudioStream();
  151. ((TSAudioStream)stream).LanguageCode = languageCode;
  152. ((TSAudioStream)stream).ChannelLayout = channelLayout;
  153. ((TSAudioStream)stream).SampleRate = TSAudioStream.ConvertSampleRate(sampleRate);
  154. ((TSAudioStream)stream).LanguageCode = languageCode;
  155. #if DEBUG
  156. Debug.WriteLine(string.Format(
  157. "\t{0} {1} {2} {3} {4}",
  158. PID,
  159. streamType,
  160. languageCode,
  161. channelLayout,
  162. sampleRate));
  163. #endif
  164. }
  165. break;
  166. case TSStreamType.INTERACTIVE_GRAPHICS:
  167. case TSStreamType.PRESENTATION_GRAPHICS:
  168. {
  169. byte[] languageBytes = new byte[3];
  170. Array.Copy(clipData, streamOffset + 2,
  171. languageBytes, 0, languageBytes.Length);
  172. string languageCode =
  173. ASCIIEncoding.ASCII.GetString(languageBytes);
  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 =
  191. ASCIIEncoding.ASCII.GetString(languageBytes);
  192. #if DEBUG
  193. Debug.WriteLine(string.Format(
  194. "\t{0} {1} {2}",
  195. PID,
  196. streamType,
  197. languageCode));
  198. #endif
  199. stream = new TSTextStream();
  200. stream.LanguageCode = languageCode;
  201. }
  202. break;
  203. }
  204. if (stream != null)
  205. {
  206. stream.PID = PID;
  207. stream.StreamType = streamType;
  208. Streams.Add(PID, stream);
  209. }
  210. streamOffset += clipData[streamOffset] + 1;
  211. }
  212. IsValid = true;
  213. }
  214. finally
  215. {
  216. if (fileReader != null) fileReader.Dispose();
  217. if (fileStream != null) fileStream.Dispose();
  218. }
  219. }
  220. }
  221. }