dtsx.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "media/formats/mp4/dtsx.h"
  5. #include "base/logging.h"
  6. #include "media/base/bit_reader.h"
  7. #include "media/formats/mp4/rcheck.h"
  8. namespace media {
  9. namespace mp4 {
  10. DTSX::DTSX() = default;
  11. DTSX::DTSX(const DTSX& other) = default;
  12. DTSX::~DTSX() = default;
  13. bool DTSX::Parse(const std::vector<uint8_t>& data, MediaLog* media_log) {
  14. if (data.empty())
  15. return false;
  16. if (data.size() < (2 + 3 + 5 + 32 + 1 + 2 + 8) / 8)
  17. return false;
  18. DVLOG(3) << "dtsx data.size " << data.size();
  19. // Parse udts box using reader.
  20. BitReader reader(&data[0], data.size());
  21. // Read DecoderProfileCode
  22. RCHECK(reader.ReadBits(6, &decoder_profile_code_));
  23. // Read FrameDurationCode
  24. uint8_t frame_duration_code = 0;
  25. RCHECK(reader.ReadBits(2, &frame_duration_code));
  26. switch (frame_duration_code) {
  27. case 0:
  28. frame_duration_ = 512;
  29. break;
  30. case 1:
  31. frame_duration_ = 1024;
  32. break;
  33. case 2:
  34. frame_duration_ = 2048;
  35. break;
  36. case 3:
  37. frame_duration_ = 4096;
  38. break;
  39. default:
  40. frame_duration_ = 0;
  41. break;
  42. }
  43. // Read MaxPayloadCode
  44. uint8_t max_payload_code = 0;
  45. RCHECK(reader.ReadBits(3, &max_payload_code));
  46. switch (max_payload_code) {
  47. case 0:
  48. max_payload_ = 2048;
  49. break;
  50. case 1:
  51. max_payload_ = 4096;
  52. break;
  53. case 2:
  54. max_payload_ = 8192;
  55. break;
  56. case 3:
  57. max_payload_ = 16384;
  58. break;
  59. case 4:
  60. max_payload_ = 32768;
  61. break;
  62. case 5:
  63. max_payload_ = 65536;
  64. break;
  65. case 6:
  66. max_payload_ = 131072;
  67. break;
  68. case 7:
  69. default:
  70. max_payload_ = 0;
  71. break;
  72. }
  73. // Read NumPresentationsCode
  74. RCHECK(reader.ReadBits(5, &num_presentations_));
  75. // Read ChannelMask
  76. RCHECK(reader.ReadBits(32, &channel_mask_));
  77. // Read BaseSamplingFrequencyCode
  78. int base_sampling_frequency = 0;
  79. uint8_t base_sampling_frequency_code = 0;
  80. RCHECK(reader.ReadBits(1, &base_sampling_frequency_code));
  81. if (base_sampling_frequency_code == 1)
  82. base_sampling_frequency = 48000;
  83. else
  84. base_sampling_frequency = 44100;
  85. // Read SampleRateMod
  86. int sample_rate_mod = 0;
  87. uint8_t sample_rate_mod_code;
  88. RCHECK(reader.ReadBits(2, &sample_rate_mod_code));
  89. switch (sample_rate_mod) {
  90. case 0:
  91. sample_rate_mod = 1;
  92. break;
  93. case 1:
  94. sample_rate_mod = 2;
  95. break;
  96. case 2:
  97. sample_rate_mod = 4;
  98. break;
  99. case 3:
  100. sample_rate_mod = 8;
  101. break;
  102. default:
  103. // error, should not hit default case.
  104. DLOG(ERROR) << "SampleRateMod invalid value";
  105. return false;
  106. }
  107. // Calculate the Sampling Frequency
  108. sampling_frequency_ = base_sampling_frequency * sample_rate_mod;
  109. LogDtsxParameters();
  110. return true;
  111. }
  112. uint8_t DTSX::GetDecoderProfileCode() const {
  113. return decoder_profile_code_;
  114. }
  115. int DTSX::GetFrameDuration() const {
  116. return frame_duration_;
  117. }
  118. int DTSX::GetMaxPayload() const {
  119. return max_payload_;
  120. }
  121. int DTSX::GetNumPresentations() const {
  122. return num_presentations_;
  123. }
  124. uint32_t DTSX::GetChannelMask() const {
  125. return channel_mask_;
  126. }
  127. int DTSX::GetSamplingFrequency() const {
  128. return sampling_frequency_;
  129. }
  130. void DTSX::LogDtsxParameters() {
  131. DVLOG(3) << "DecoderProfileCode " << static_cast<int>(decoder_profile_code_)
  132. << "Frame Duration " << frame_duration_ << "Max Payload "
  133. << max_payload_ << "Num Presentations " << num_presentations_
  134. << "Channel Mask " << channel_mask_ << "Sampling Frequency "
  135. << sampling_frequency_;
  136. }
  137. } // namespace mp4
  138. } // namespace media