TSCodecDTSHD.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. using System;
  20. using System.Collections.Generic;
  21. using System.Text;
  22. namespace BDInfo
  23. {
  24. public abstract class TSCodecDTSHD
  25. {
  26. private static int[] SampleRates = new int[]
  27. { 0x1F40, 0x3E80, 0x7D00, 0x0FA00, 0x1F400, 0x5622, 0x0AC44, 0x15888, 0x2B110, 0x56220, 0x2EE0, 0x5DC0, 0x0BB80, 0x17700, 0x2EE00, 0x5DC00 };
  28. public static void Scan(
  29. TSAudioStream stream,
  30. TSStreamBuffer buffer,
  31. long bitrate,
  32. ref string tag)
  33. {
  34. if (stream.IsInitialized &&
  35. (stream.StreamType == TSStreamType.DTS_HD_SECONDARY_AUDIO ||
  36. (stream.CoreStream != null &&
  37. stream.CoreStream.IsInitialized))) return;
  38. bool syncFound = false;
  39. uint sync = 0;
  40. for (int i = 0; i < buffer.Length; i++)
  41. {
  42. sync = (sync << 8) + buffer.ReadByte();
  43. if (sync == 0x64582025)
  44. {
  45. syncFound = true;
  46. break;
  47. }
  48. }
  49. if (!syncFound)
  50. {
  51. tag = "CORE";
  52. if (stream.CoreStream == null)
  53. {
  54. stream.CoreStream = new TSAudioStream();
  55. stream.CoreStream.StreamType = TSStreamType.DTS_AUDIO;
  56. }
  57. if (!stream.CoreStream.IsInitialized)
  58. {
  59. buffer.BeginRead();
  60. TSCodecDTS.Scan(stream.CoreStream, buffer, bitrate, ref tag);
  61. }
  62. return;
  63. }
  64. tag = "HD";
  65. int temp1 = buffer.ReadBits(8);
  66. int nuSubStreamIndex = buffer.ReadBits(2);
  67. int nuExtSSHeaderSize = 0;
  68. int nuExtSSFSize = 0;
  69. int bBlownUpHeader = buffer.ReadBits(1);
  70. if (1 == bBlownUpHeader)
  71. {
  72. nuExtSSHeaderSize = buffer.ReadBits(12) + 1;
  73. nuExtSSFSize = buffer.ReadBits(20) + 1;
  74. }
  75. else
  76. {
  77. nuExtSSHeaderSize = buffer.ReadBits(8) + 1;
  78. nuExtSSFSize = buffer.ReadBits(16) + 1;
  79. }
  80. int nuNumAudioPresent = 1;
  81. int nuNumAssets = 1;
  82. int bStaticFieldsPresent = buffer.ReadBits(1);
  83. if (1 == bStaticFieldsPresent)
  84. {
  85. int nuRefClockCode = buffer.ReadBits(2);
  86. int nuExSSFrameDurationCode = buffer.ReadBits(3) + 1;
  87. long nuTimeStamp = 0;
  88. if (1 == buffer.ReadBits(1))
  89. {
  90. nuTimeStamp = (buffer.ReadBits(18) << 18) + buffer.ReadBits(18);
  91. }
  92. nuNumAudioPresent = buffer.ReadBits(3) + 1;
  93. nuNumAssets = buffer.ReadBits(3) + 1;
  94. int[] nuActiveExSSMask = new int[nuNumAudioPresent];
  95. for (int i = 0; i < nuNumAudioPresent; i++)
  96. {
  97. nuActiveExSSMask[i] = buffer.ReadBits(nuSubStreamIndex + 1); //?
  98. }
  99. for (int i = 0; i < nuNumAudioPresent; i++)
  100. {
  101. for (int j = 0; j < nuSubStreamIndex + 1; j++)
  102. {
  103. if (((j + 1) % 2) == 1)
  104. {
  105. int mask = buffer.ReadBits(8);
  106. }
  107. }
  108. }
  109. if (1 == buffer.ReadBits(1))
  110. {
  111. int nuMixMetadataAdjLevel = buffer.ReadBits(2);
  112. int nuBits4MixOutMask = buffer.ReadBits(2) * 4 + 4;
  113. int nuNumMixOutConfigs = buffer.ReadBits(2) + 1;
  114. int[] nuMixOutChMask = new int[nuNumMixOutConfigs];
  115. for (int i = 0; i < nuNumMixOutConfigs; i++)
  116. {
  117. nuMixOutChMask[i] = buffer.ReadBits(nuBits4MixOutMask);
  118. }
  119. }
  120. }
  121. int[] AssetSizes = new int[nuNumAssets];
  122. for (int i = 0; i < nuNumAssets; i++)
  123. {
  124. if (1 == bBlownUpHeader)
  125. {
  126. AssetSizes[i] = buffer.ReadBits(20) + 1;
  127. }
  128. else
  129. {
  130. AssetSizes[i] = buffer.ReadBits(16) + 1;
  131. }
  132. }
  133. for (int i = 0; i < nuNumAssets; i++)
  134. {
  135. long bufferPosition = buffer.Position;
  136. int nuAssetDescriptorFSIZE = buffer.ReadBits(9) + 1;
  137. int DescriptorDataForAssetIndex = buffer.ReadBits(3);
  138. if (1 == bStaticFieldsPresent)
  139. {
  140. int AssetTypeDescrPresent = buffer.ReadBits(1);
  141. if (1 == AssetTypeDescrPresent)
  142. {
  143. int AssetTypeDescriptor = buffer.ReadBits(4);
  144. }
  145. int LanguageDescrPresent = buffer.ReadBits(1);
  146. if (1 == LanguageDescrPresent)
  147. {
  148. int LanguageDescriptor = buffer.ReadBits(24);
  149. }
  150. int bInfoTextPresent = buffer.ReadBits(1);
  151. if (1 == bInfoTextPresent)
  152. {
  153. int nuInfoTextByteSize = buffer.ReadBits(10) + 1;
  154. int[] InfoText = new int[nuInfoTextByteSize];
  155. for (int j = 0; j < nuInfoTextByteSize; j++)
  156. {
  157. InfoText[j] = buffer.ReadBits(8);
  158. }
  159. }
  160. int nuBitResolution = buffer.ReadBits(5) + 1;
  161. int nuMaxSampleRate = buffer.ReadBits(4);
  162. int nuTotalNumChs = buffer.ReadBits(8) + 1;
  163. int bOne2OneMapChannels2Speakers = buffer.ReadBits(1);
  164. int nuSpkrActivityMask = 0;
  165. if (1 == bOne2OneMapChannels2Speakers)
  166. {
  167. int bEmbeddedStereoFlag = 0;
  168. if (nuTotalNumChs > 2)
  169. {
  170. bEmbeddedStereoFlag = buffer.ReadBits(1);
  171. }
  172. int bEmbeddedSixChFlag = 0;
  173. if (nuTotalNumChs > 6)
  174. {
  175. bEmbeddedSixChFlag = buffer.ReadBits(1);
  176. }
  177. int bSpkrMaskEnabled = buffer.ReadBits(1);
  178. int nuNumBits4SAMask = 0;
  179. if (1 == bSpkrMaskEnabled)
  180. {
  181. nuNumBits4SAMask = buffer.ReadBits(2);
  182. nuNumBits4SAMask = nuNumBits4SAMask * 4 + 4;
  183. nuSpkrActivityMask = buffer.ReadBits(nuNumBits4SAMask);
  184. }
  185. // TODO...
  186. }
  187. stream.SampleRate = SampleRates[nuMaxSampleRate];
  188. stream.BitDepth = nuBitResolution;
  189. stream.LFE = 0;
  190. if ((nuSpkrActivityMask & 0x8) == 0x8)
  191. {
  192. ++stream.LFE;
  193. }
  194. if ((nuSpkrActivityMask & 0x1000) == 0x1000)
  195. {
  196. ++stream.LFE;
  197. }
  198. stream.ChannelCount = nuTotalNumChs - stream.LFE;
  199. }
  200. if (nuNumAssets > 1)
  201. {
  202. // TODO...
  203. break;
  204. }
  205. }
  206. // TODO
  207. if (stream.CoreStream != null)
  208. {
  209. TSAudioStream coreStream = (TSAudioStream)stream.CoreStream;
  210. if (coreStream.AudioMode == TSAudioMode.Extended &&
  211. stream.ChannelCount == 5)
  212. {
  213. stream.AudioMode = TSAudioMode.Extended;
  214. }
  215. /*
  216. if (coreStream.DialNorm != 0)
  217. {
  218. stream.DialNorm = coreStream.DialNorm;
  219. }
  220. */
  221. }
  222. if (stream.StreamType == TSStreamType.DTS_HD_MASTER_AUDIO)
  223. {
  224. stream.IsVBR = true;
  225. stream.IsInitialized = true;
  226. }
  227. else if (bitrate > 0)
  228. {
  229. stream.IsVBR = false;
  230. stream.BitRate = bitrate;
  231. if (stream.CoreStream != null)
  232. {
  233. stream.BitRate += stream.CoreStream.BitRate;
  234. stream.IsInitialized = true;
  235. }
  236. stream.IsInitialized = (stream.BitRate > 0 ? true : false);
  237. }
  238. }
  239. }
  240. }