TSCodecDTSHD.cs 9.5 KB

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