TSCodecTrueHD.cs 5.8 KB

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