TSCodecDTS.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 TSCodecDTS
  22. {
  23. private static int[] dca_sample_rates =
  24. {
  25. 0, 8000, 16000, 32000, 0, 0, 11025, 22050, 44100, 0, 0,
  26. 12000, 24000, 48000, 96000, 192000
  27. };
  28. private static int[] dca_bit_rates =
  29. {
  30. 32000, 56000, 64000, 96000, 112000, 128000,
  31. 192000, 224000, 256000, 320000, 384000,
  32. 448000, 512000, 576000, 640000, 768000,
  33. 896000, 1024000, 1152000, 1280000, 1344000,
  34. 1408000, 1411200, 1472000, 1509000, 1920000,
  35. 2048000, 3072000, 3840000, 1/*open*/, 2/*variable*/, 3/*lossless*/
  36. };
  37. private static int[] dca_channels =
  38. {
  39. 1, 2, 2, 2, 2, 3, 3, 4, 4, 5, 6, 6, 6, 7, 8, 8
  40. };
  41. private static int[] dca_bits_per_sample =
  42. {
  43. 16, 16, 20, 20, 0, 24, 24
  44. };
  45. public static void Scan(
  46. TSAudioStream stream,
  47. TSStreamBuffer buffer,
  48. long bitrate,
  49. ref string tag)
  50. {
  51. if (stream.IsInitialized) return;
  52. bool syncFound = false;
  53. uint sync = 0;
  54. for (int i = 0; i < buffer.Length; i++)
  55. {
  56. sync = (sync << 8) + buffer.ReadByte();
  57. if (sync == 0x7FFE8001)
  58. {
  59. syncFound = true;
  60. break;
  61. }
  62. }
  63. if (!syncFound) return;
  64. int frame_type = buffer.ReadBits(1);
  65. int samples_deficit = buffer.ReadBits(5);
  66. int crc_present = buffer.ReadBits(1);
  67. int sample_blocks = buffer.ReadBits(7);
  68. int frame_size = buffer.ReadBits(14);
  69. if (frame_size < 95)
  70. {
  71. return;
  72. }
  73. int amode = buffer.ReadBits(6);
  74. int sample_rate = buffer.ReadBits(4);
  75. if (sample_rate < 0 || sample_rate >= dca_sample_rates.Length)
  76. {
  77. return;
  78. }
  79. int bit_rate = buffer.ReadBits(5);
  80. if (bit_rate < 0 || bit_rate >= dca_bit_rates.Length)
  81. {
  82. return;
  83. }
  84. int downmix = buffer.ReadBits(1);
  85. int dynrange = buffer.ReadBits(1);
  86. int timestamp = buffer.ReadBits(1);
  87. int aux_data = buffer.ReadBits(1);
  88. int hdcd = buffer.ReadBits(1);
  89. int ext_descr = buffer.ReadBits(3);
  90. int ext_coding = buffer.ReadBits(1);
  91. int aspf = buffer.ReadBits(1);
  92. int lfe = buffer.ReadBits(2);
  93. int predictor_history = buffer.ReadBits(1);
  94. if (crc_present == 1)
  95. {
  96. int crc = buffer.ReadBits(16);
  97. }
  98. int multirate_inter = buffer.ReadBits(1);
  99. int version = buffer.ReadBits(4);
  100. int copy_history = buffer.ReadBits(2);
  101. int source_pcm_res = buffer.ReadBits(3);
  102. int front_sum = buffer.ReadBits(1);
  103. int surround_sum = buffer.ReadBits(1);
  104. int dialog_norm = buffer.ReadBits(4);
  105. if (source_pcm_res < 0 || source_pcm_res >= dca_bits_per_sample.Length)
  106. {
  107. return;
  108. }
  109. int subframes = buffer.ReadBits(4);
  110. int total_channels = buffer.ReadBits(3) + 1 + ext_coding;
  111. stream.SampleRate = dca_sample_rates[sample_rate];
  112. stream.ChannelCount = total_channels;
  113. stream.LFE = (lfe > 0 ? 1 : 0);
  114. stream.BitDepth = dca_bits_per_sample[source_pcm_res];
  115. stream.DialNorm = -dialog_norm;
  116. if ((source_pcm_res & 0x1) == 0x1)
  117. {
  118. stream.AudioMode = TSAudioMode.Extended;
  119. }
  120. stream.BitRate = (uint)dca_bit_rates[bit_rate];
  121. switch (stream.BitRate)
  122. {
  123. case 1:
  124. if (bitrate > 0)
  125. {
  126. stream.BitRate = bitrate;
  127. stream.IsVBR = false;
  128. stream.IsInitialized = true;
  129. }
  130. else
  131. {
  132. stream.BitRate = 0;
  133. }
  134. break;
  135. case 2:
  136. case 3:
  137. stream.IsVBR = true;
  138. stream.IsInitialized = true;
  139. break;
  140. default:
  141. stream.IsVBR = false;
  142. stream.IsInitialized = true;
  143. break;
  144. }
  145. }
  146. }
  147. }