TSCodecDTS.cs 5.6 KB

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