aac_unittest.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. // Copyright 2014 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 <stdint.h>
  5. #include <string>
  6. #include "media/base/mock_media_log.h"
  7. #include "media/formats/mp4/aac.h"
  8. #include "testing/gmock/include/gmock/gmock.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. using ::testing::AllOf;
  11. using ::testing::HasSubstr;
  12. using ::testing::InSequence;
  13. using ::testing::StrictMock;
  14. namespace media {
  15. namespace mp4 {
  16. MATCHER_P(UnsupportedFrequencyIndexLog, frequency_index, "") {
  17. return CONTAINS_STRING(
  18. arg,
  19. "Sampling Frequency Index(0x" +
  20. std::string(frequency_index) + ") is not supported.");
  21. }
  22. MATCHER_P(UnsupportedExtensionFrequencyIndexLog, frequency_index, "") {
  23. return CONTAINS_STRING(
  24. arg,
  25. "Extension Sampling Frequency Index(0x" +
  26. std::string(frequency_index) + ") is not supported.");
  27. }
  28. MATCHER_P(UnsupportedChannelConfigLog, channel_index, "") {
  29. return CONTAINS_STRING(
  30. arg,
  31. "Channel Configuration(" + std::string(channel_index) +
  32. ") is not supported");
  33. }
  34. MATCHER_P(UnsupportedAudioProfileLog, profile_string, "") {
  35. return CONTAINS_STRING(
  36. arg,
  37. "Audio codec(" + std::string(profile_string) + ") is not supported");
  38. }
  39. class AACTest : public testing::Test {
  40. public:
  41. AACTest() = default;
  42. bool Parse(const std::vector<uint8_t>& data) {
  43. return aac_.Parse(data, &media_log_);
  44. }
  45. StrictMock<MockMediaLog> media_log_;
  46. AAC aac_;
  47. };
  48. TEST_F(AACTest, BasicProfileTest) {
  49. uint8_t buffer[] = {0x12, 0x10};
  50. std::vector<uint8_t> data;
  51. data.assign(buffer, buffer + sizeof(buffer));
  52. EXPECT_TRUE(Parse(data));
  53. EXPECT_EQ(aac_.GetOutputSamplesPerSecond(false), 44100);
  54. EXPECT_EQ(aac_.GetChannelLayout(false), CHANNEL_LAYOUT_STEREO);
  55. EXPECT_EQ(aac_.GetProfile(), AudioCodecProfile::kUnknown);
  56. }
  57. TEST_F(AACTest, ExtensionTest) {
  58. uint8_t buffer[] = {0x13, 0x08, 0x56, 0xe5, 0x9d, 0x48, 0x80};
  59. std::vector<uint8_t> data;
  60. data.assign(buffer, buffer + sizeof(buffer));
  61. EXPECT_TRUE(Parse(data));
  62. EXPECT_EQ(aac_.GetOutputSamplesPerSecond(false), 48000);
  63. EXPECT_EQ(aac_.GetOutputSamplesPerSecond(true), 48000);
  64. EXPECT_EQ(aac_.GetChannelLayout(false), CHANNEL_LAYOUT_STEREO);
  65. EXPECT_EQ(aac_.GetProfile(), AudioCodecProfile::kUnknown);
  66. }
  67. // Test implicit SBR with mono channel config.
  68. // Mono channel layout should only be reported if SBR is not
  69. // specified. Otherwise stereo should be reported.
  70. // See ISO 14496-3:2005 Section 1.6.5.3 for details about this special casing.
  71. TEST_F(AACTest, ImplicitSBR_ChannelConfig0) {
  72. uint8_t buffer[] = {0x13, 0x08};
  73. std::vector<uint8_t> data;
  74. data.assign(buffer, buffer + sizeof(buffer));
  75. EXPECT_TRUE(Parse(data));
  76. // Test w/o implict SBR.
  77. EXPECT_EQ(aac_.GetOutputSamplesPerSecond(false), 24000);
  78. EXPECT_EQ(aac_.GetChannelLayout(false), CHANNEL_LAYOUT_MONO);
  79. EXPECT_EQ(aac_.GetProfile(), AudioCodecProfile::kUnknown);
  80. // Test implicit SBR.
  81. EXPECT_EQ(aac_.GetOutputSamplesPerSecond(true), 48000);
  82. EXPECT_EQ(aac_.GetChannelLayout(true), CHANNEL_LAYOUT_STEREO);
  83. EXPECT_EQ(aac_.GetProfile(), AudioCodecProfile::kUnknown);
  84. }
  85. // Tests implicit SBR with a stereo channel config.
  86. TEST_F(AACTest, ImplicitSBR_ChannelConfig1) {
  87. uint8_t buffer[] = {0x13, 0x10};
  88. std::vector<uint8_t> data;
  89. data.assign(buffer, buffer + sizeof(buffer));
  90. EXPECT_TRUE(Parse(data));
  91. // Test w/o implict SBR.
  92. EXPECT_EQ(aac_.GetOutputSamplesPerSecond(false), 24000);
  93. EXPECT_EQ(aac_.GetChannelLayout(false), CHANNEL_LAYOUT_STEREO);
  94. EXPECT_EQ(aac_.GetProfile(), AudioCodecProfile::kUnknown);
  95. // Test implicit SBR.
  96. EXPECT_EQ(aac_.GetOutputSamplesPerSecond(true), 48000);
  97. EXPECT_EQ(aac_.GetChannelLayout(true), CHANNEL_LAYOUT_STEREO);
  98. EXPECT_EQ(aac_.GetProfile(), AudioCodecProfile::kUnknown);
  99. }
  100. TEST_F(AACTest, SixChannelTest) {
  101. uint8_t buffer[] = {0x11, 0xb0};
  102. std::vector<uint8_t> data;
  103. data.assign(buffer, buffer + sizeof(buffer));
  104. EXPECT_TRUE(Parse(data));
  105. EXPECT_EQ(aac_.GetOutputSamplesPerSecond(false), 48000);
  106. EXPECT_EQ(aac_.GetChannelLayout(false), CHANNEL_LAYOUT_5_1_BACK);
  107. EXPECT_EQ(aac_.GetProfile(), AudioCodecProfile::kUnknown);
  108. }
  109. TEST_F(AACTest, DataTooShortTest) {
  110. std::vector<uint8_t> data;
  111. EXPECT_FALSE(Parse(data));
  112. data.push_back(0x12);
  113. EXPECT_FALSE(Parse(data));
  114. }
  115. TEST_F(AACTest, IncorrectProfileTest) {
  116. InSequence s;
  117. uint8_t buffer[] = {0x0, 0x08};
  118. std::vector<uint8_t> data;
  119. data.assign(buffer, buffer + sizeof(buffer));
  120. EXPECT_MEDIA_LOG(UnsupportedAudioProfileLog("mp4a.40.0"));
  121. EXPECT_FALSE(Parse(data));
  122. data[0] = 0x08;
  123. EXPECT_TRUE(Parse(data));
  124. data[0] = 0x28;
  125. // No media log for this profile 5, since not enough bits are in |data| to
  126. // first parse profile 5's extension frequency index.
  127. EXPECT_FALSE(Parse(data));
  128. }
  129. TEST_F(AACTest, IncorrectFrequencyTest) {
  130. uint8_t buffer[] = {0x0f, 0x88};
  131. std::vector<uint8_t> data;
  132. data.assign(buffer, buffer + sizeof(buffer));
  133. EXPECT_FALSE(Parse(data));
  134. data[0] = 0x0e;
  135. data[1] = 0x08;
  136. EXPECT_TRUE(Parse(data));
  137. }
  138. TEST_F(AACTest, IncorrectChannelTest) {
  139. uint8_t buffer[] = {0x0e, 0x00};
  140. std::vector<uint8_t> data;
  141. data.assign(buffer, buffer + sizeof(buffer));
  142. EXPECT_FALSE(Parse(data));
  143. data[1] = 0x08;
  144. EXPECT_TRUE(Parse(data));
  145. }
  146. TEST_F(AACTest, UnsupportedProfileTest) {
  147. InSequence s;
  148. uint8_t buffer[] = {0x3a, 0x08};
  149. std::vector<uint8_t> data;
  150. data.assign(buffer, buffer + sizeof(buffer));
  151. EXPECT_MEDIA_LOG(UnsupportedAudioProfileLog("mp4a.40.7"));
  152. EXPECT_FALSE(Parse(data));
  153. data[0] = 0x12;
  154. data[1] = 0x18;
  155. EXPECT_TRUE(Parse(data));
  156. }
  157. TEST_F(AACTest, UnsupportedChannelLayoutTest) {
  158. InSequence s;
  159. uint8_t buffer[] = {0x12, 0x78};
  160. std::vector<uint8_t> data;
  161. data.assign(buffer, buffer + sizeof(buffer));
  162. EXPECT_MEDIA_LOG(UnsupportedChannelConfigLog("15"));
  163. EXPECT_FALSE(Parse(data));
  164. data[1] = 0x18;
  165. EXPECT_TRUE(Parse(data));
  166. }
  167. TEST_F(AACTest, UnsupportedFrequencyIndexTest) {
  168. InSequence s;
  169. uint8_t buffer[] = {0x17, 0x10};
  170. std::vector<uint8_t> data;
  171. data.assign(buffer, buffer + sizeof(buffer));
  172. EXPECT_MEDIA_LOG(UnsupportedFrequencyIndexLog("e"));
  173. EXPECT_FALSE(Parse(data));
  174. data[0] = 0x13;
  175. EXPECT_TRUE(Parse(data));
  176. }
  177. TEST_F(AACTest, UnsupportedExFrequencyIndexTest) {
  178. InSequence s;
  179. uint8_t buffer[] = {0x29, 0x17, 0x08, 0x0};
  180. std::vector<uint8_t> data;
  181. data.assign(buffer, buffer + sizeof(buffer));
  182. EXPECT_MEDIA_LOG(UnsupportedExtensionFrequencyIndexLog("e"));
  183. EXPECT_FALSE(Parse(data));
  184. data[1] = 0x11;
  185. EXPECT_TRUE(Parse(data));
  186. }
  187. TEST_F(AACTest, XHE_AAC) {
  188. InSequence s;
  189. uint8_t buffer[] = {0xf9, 0x46, 0x43, 0x22, 0x2c, 0xc0, 0x4c, 0x00,
  190. 0x85, 0xa0, 0x01, 0x13, 0x84, 0x00, 0x20, 0x00,
  191. 0x02, 0x50, 0x01, 0x19, 0x72, 0xc0, 0x00};
  192. std::vector<uint8_t> data;
  193. data.assign(buffer, buffer + sizeof(buffer));
  194. EXPECT_TRUE(Parse(data));
  195. EXPECT_EQ(aac_.GetOutputSamplesPerSecond(false), 48000);
  196. EXPECT_EQ(aac_.GetChannelLayout(false), CHANNEL_LAYOUT_STEREO);
  197. EXPECT_EQ(aac_.GetProfile(), AudioCodecProfile::kXHE_AAC);
  198. // ADTS conversion should do nothing since xHE-AAC can't be represented with
  199. // only two bits for the profile.
  200. EXPECT_TRUE(aac_.ConvertEsdsToADTS(&data));
  201. EXPECT_EQ(data.size(), sizeof(buffer));
  202. }
  203. } // namespace mp4
  204. } // namespace media