2
0

TSCodecTrueHD.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 TSCodecTrueHD
  25. {
  26. public static void Scan(
  27. TSAudioStream stream,
  28. TSStreamBuffer buffer,
  29. ref string tag)
  30. {
  31. if (stream.IsInitialized &&
  32. stream.CoreStream != null &&
  33. stream.CoreStream.IsInitialized) return;
  34. bool syncFound = false;
  35. uint sync = 0;
  36. for (int i = 0; i < buffer.Length; i++)
  37. {
  38. sync = (sync << 8) + buffer.ReadByte();
  39. if (sync == 0xF8726FBA)
  40. {
  41. syncFound = true;
  42. break;
  43. }
  44. }
  45. if (!syncFound)
  46. {
  47. tag = "CORE";
  48. if (stream.CoreStream == null)
  49. {
  50. stream.CoreStream = new TSAudioStream();
  51. stream.CoreStream.StreamType = TSStreamType.AC3_AUDIO;
  52. }
  53. if (!stream.CoreStream.IsInitialized)
  54. {
  55. buffer.BeginRead();
  56. TSCodecAC3.Scan(stream.CoreStream, buffer, ref tag);
  57. }
  58. return;
  59. }
  60. tag = "HD";
  61. int ratebits = buffer.ReadBits(4);
  62. if (ratebits != 0xF)
  63. {
  64. stream.SampleRate =
  65. (((ratebits & 8) > 0 ? 44100 : 48000) << (ratebits & 7));
  66. }
  67. int temp1 = buffer.ReadBits(8);
  68. int channels_thd_stream1 = buffer.ReadBits(5);
  69. int temp2 = buffer.ReadBits(2);
  70. stream.ChannelCount = 0;
  71. stream.LFE = 0;
  72. int c_LFE2 = buffer.ReadBits(1);
  73. if (c_LFE2 == 1)
  74. {
  75. stream.LFE += 1;
  76. }
  77. int c_Cvh = buffer.ReadBits(1);
  78. if (c_Cvh == 1)
  79. {
  80. stream.ChannelCount += 1;
  81. }
  82. int c_LRw = buffer.ReadBits(1);
  83. if (c_LRw == 1)
  84. {
  85. stream.ChannelCount += 2;
  86. }
  87. int c_LRsd = buffer.ReadBits(1);
  88. if (c_LRsd == 1)
  89. {
  90. stream.ChannelCount += 2;
  91. }
  92. int c_Ts = buffer.ReadBits(1);
  93. if (c_Ts == 1)
  94. {
  95. stream.ChannelCount += 1;
  96. }
  97. int c_Cs = buffer.ReadBits(1);
  98. if (c_Cs == 1)
  99. {
  100. stream.ChannelCount += 1;
  101. }
  102. int c_LRrs = buffer.ReadBits(1);
  103. if (c_LRrs == 1)
  104. {
  105. stream.ChannelCount += 2;
  106. }
  107. int c_LRc = buffer.ReadBits(1);
  108. if (c_LRc == 1)
  109. {
  110. stream.ChannelCount += 2;
  111. }
  112. int c_LRvh = buffer.ReadBits(1);
  113. if (c_LRvh == 1)
  114. {
  115. stream.ChannelCount += 2;
  116. }
  117. int c_LRs = buffer.ReadBits(1);
  118. if (c_LRs == 1)
  119. {
  120. stream.ChannelCount += 2;
  121. }
  122. int c_LFE = buffer.ReadBits(1);
  123. if (c_LFE == 1)
  124. {
  125. stream.LFE += 1;
  126. }
  127. int c_C = buffer.ReadBits(1);
  128. if (c_C == 1)
  129. {
  130. stream.ChannelCount += 1;
  131. }
  132. int c_LR = buffer.ReadBits(1);
  133. if (c_LR == 1)
  134. {
  135. stream.ChannelCount += 2;
  136. }
  137. int access_unit_size = 40 << (ratebits & 7);
  138. int access_unit_size_pow2 = 64 << (ratebits & 7);
  139. int a1 = buffer.ReadBits(16);
  140. int a2 = buffer.ReadBits(16);
  141. int a3 = buffer.ReadBits(16);
  142. int is_vbr = buffer.ReadBits(1);
  143. int peak_bitrate = buffer.ReadBits(15);
  144. peak_bitrate = (peak_bitrate * stream.SampleRate) >> 4;
  145. double peak_bitdepth =
  146. (double)peak_bitrate /
  147. (stream.ChannelCount + stream.LFE) /
  148. stream.SampleRate;
  149. if (peak_bitdepth > 14)
  150. {
  151. stream.BitDepth = 24;
  152. }
  153. else
  154. {
  155. stream.BitDepth = 16;
  156. }
  157. #if DEBUG
  158. System.Diagnostics.Debug.WriteLine(string.Format(
  159. "{0}\t{1}\t{2:F2}",
  160. stream.PID, peak_bitrate, peak_bitdepth));
  161. #endif
  162. /*
  163. // TODO: Get THD dialnorm from metadata
  164. if (stream.CoreStream != null)
  165. {
  166. TSAudioStream coreStream = (TSAudioStream)stream.CoreStream;
  167. if (coreStream.DialNorm != 0)
  168. {
  169. stream.DialNorm = coreStream.DialNorm;
  170. }
  171. }
  172. */
  173. stream.IsVBR = true;
  174. stream.IsInitialized = true;
  175. }
  176. }
  177. }