xref: /aosp_15_r20/external/libwebm/webm_parser/tests/audio_parser_test.cc (revision 103e46e4cd4b6efcf6001f23fa8665fb110abf8d)
1 // Copyright (c) 2016 The WebM project authors. All Rights Reserved.
2 //
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the LICENSE file in the root of the source
5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS.  All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree.
8 #include "src/audio_parser.h"
9 
10 #include "gtest/gtest.h"
11 
12 #include "test_utils/element_parser_test.h"
13 #include "webm/id.h"
14 
15 using webm::Audio;
16 using webm::AudioParser;
17 using webm::ElementParserTest;
18 using webm::Id;
19 
20 namespace {
21 
22 class AudioParserTest : public ElementParserTest<AudioParser, Id::kAudio> {};
23 
TEST_F(AudioParserTest,DefaultParse)24 TEST_F(AudioParserTest, DefaultParse) {
25   ParseAndVerify();
26 
27   const Audio audio = parser_.value();
28 
29   EXPECT_FALSE(audio.sampling_frequency.is_present());
30   EXPECT_EQ(8000, audio.sampling_frequency.value());
31 
32   EXPECT_FALSE(audio.output_frequency.is_present());
33   EXPECT_EQ(8000, audio.output_frequency.value());
34 
35   EXPECT_FALSE(audio.channels.is_present());
36   EXPECT_EQ(static_cast<std::uint64_t>(1), audio.channels.value());
37 
38   EXPECT_FALSE(audio.bit_depth.is_present());
39   EXPECT_EQ(static_cast<std::uint64_t>(0), audio.bit_depth.value());
40 }
41 
TEST_F(AudioParserTest,DefaultValues)42 TEST_F(AudioParserTest, DefaultValues) {
43   SetReaderData({
44       0xB5,  // ID = 0x85 (SamplingFrequency).
45       0x40, 0x00,  // Size = 0.
46 
47       0x78, 0xB5,  // ID = 0x78B5 (OutputSamplingFrequency).
48       0x80,  // Size = 0.
49 
50       0x9F,  // ID = 0x9F (Channels).
51       0x40, 0x00,  // Size = 0.
52 
53       0x62, 0x64,  // ID = 0x6264 (BitDepth).
54       0x80,  // Size = 0.
55   });
56 
57   ParseAndVerify();
58 
59   const Audio audio = parser_.value();
60 
61   EXPECT_TRUE(audio.sampling_frequency.is_present());
62   EXPECT_EQ(8000, audio.sampling_frequency.value());
63 
64   EXPECT_TRUE(audio.output_frequency.is_present());
65   EXPECT_EQ(8000, audio.output_frequency.value());
66 
67   EXPECT_TRUE(audio.channels.is_present());
68   EXPECT_EQ(static_cast<std::uint64_t>(1), audio.channels.value());
69 
70   EXPECT_TRUE(audio.bit_depth.is_present());
71   EXPECT_EQ(static_cast<std::uint64_t>(0), audio.bit_depth.value());
72 }
73 
TEST_F(AudioParserTest,CustomValues)74 TEST_F(AudioParserTest, CustomValues) {
75   SetReaderData({
76       0xB5,  // ID = 0x85 (SamplingFrequency).
77       0x84,  // Size = 4.
78       0x3F, 0x80, 0x00, 0x00,  // Body (value = 1.0f).
79 
80       0x78, 0xB5,  // ID = 0x78B5 (OutputSamplingFrequency).
81       0x84,  // Size = 4.
82       0x3F, 0xDD, 0xB3, 0xD7,  // Body (value = 1.73205077648162841796875f).
83 
84       0x9F,  // ID = 0x9F (Channels).
85       0x10, 0x00, 0x00, 0x01,  // Size = 1.
86       0x02,  // Body (value = 2).
87 
88       0x62, 0x64,  // ID = 0x6264 (BitDepth).
89       0x10, 0x00, 0x00, 0x01,  // Size = 1.
90       0x01,  // Body (value = 1).
91   });
92 
93   ParseAndVerify();
94 
95   const Audio audio = parser_.value();
96 
97   EXPECT_TRUE(audio.sampling_frequency.is_present());
98   EXPECT_EQ(static_cast<std::uint64_t>(1), audio.sampling_frequency.value());
99 
100   EXPECT_TRUE(audio.output_frequency.is_present());
101   EXPECT_EQ(1.73205077648162841796875, audio.output_frequency.value());
102 
103   EXPECT_TRUE(audio.channels.is_present());
104   EXPECT_EQ(static_cast<std::uint64_t>(2), audio.channels.value());
105 
106   EXPECT_TRUE(audio.bit_depth.is_present());
107   EXPECT_EQ(static_cast<std::uint64_t>(1), audio.bit_depth.value());
108 }
109 
TEST_F(AudioParserTest,AbsentOutputSamplingFrequency)110 TEST_F(AudioParserTest, AbsentOutputSamplingFrequency) {
111   SetReaderData({
112       0xB5,  // ID = 0x85 (SamplingFrequency).
113       0x84,  // Size = 4.
114       0x3F, 0x80, 0x00, 0x00,  // Body (value = 1.0f).
115   });
116 
117   ParseAndVerify();
118 
119   const Audio audio = parser_.value();
120 
121   EXPECT_TRUE(audio.sampling_frequency.is_present());
122   EXPECT_EQ(static_cast<std::uint64_t>(1), audio.sampling_frequency.value());
123 
124   EXPECT_FALSE(audio.output_frequency.is_present());
125   EXPECT_EQ(static_cast<std::uint64_t>(1), audio.output_frequency.value());
126 
127   EXPECT_FALSE(audio.channels.is_present());
128   EXPECT_EQ(static_cast<std::uint64_t>(1), audio.channels.value());
129 
130   EXPECT_FALSE(audio.bit_depth.is_present());
131   EXPECT_EQ(static_cast<std::uint64_t>(0), audio.bit_depth.value());
132 }
133 
TEST_F(AudioParserTest,DefaultOutputSamplingFrequency)134 TEST_F(AudioParserTest, DefaultOutputSamplingFrequency) {
135   SetReaderData({
136       0xB5,  // ID = 0x85 (SamplingFrequency).
137       0x84,  // Size = 4.
138       0x3F, 0x80, 0x00, 0x00,  // Body (value = 1.0f).
139 
140       0x78, 0xB5,  // ID = 0x78B5 (OutputSamplingFrequency).
141       0x10, 0x00, 0x00, 0x00,  // Size = 0.
142   });
143 
144   ParseAndVerify();
145 
146   const Audio audio = parser_.value();
147 
148   EXPECT_TRUE(audio.sampling_frequency.is_present());
149   EXPECT_EQ(static_cast<std::uint64_t>(1), audio.sampling_frequency.value());
150 
151   EXPECT_TRUE(audio.output_frequency.is_present());
152   EXPECT_EQ(static_cast<std::uint64_t>(1), audio.output_frequency.value());
153 
154   EXPECT_FALSE(audio.channels.is_present());
155   EXPECT_EQ(static_cast<std::uint64_t>(1), audio.channels.value());
156 
157   EXPECT_FALSE(audio.bit_depth.is_present());
158   EXPECT_EQ(static_cast<std::uint64_t>(0), audio.bit_depth.value());
159 }
160 
161 }  // namespace
162