• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "gtest/gtest.h"
17 #include "audio_g711mu_encoder_plugin.h"
18 #include "media_description.h"
19 #include "avcodec_errors.h"
20 #include "avcodec_trace.h"
21 #include "avcodec_log.h"
22 #include "avcodec_mime_type.h"
23 #include "avcodec_audio_common.h"
24 
25 namespace {
26 constexpr int32_t SUPPORT_CHANNELS = 1;
27 constexpr int SUPPORT_SAMPLE_RATE = 8000;
28 }  // namespace
29 
30 using namespace std;
31 using namespace testing::ext;
32 
33 namespace OHOS {
34 namespace MediaAVCodec {
35 class G711EncPluginUnitTest : public testing::Test {
36 public:
37     static void SetUpTestCase(void);
38     static void TearDownTestCase(void);
39     void SetUp();
40     void TearDown();
41     std::shared_ptr<AudioG711muEncoderPlugin> g711EncPlugin_;
42 };
43 
SetUpTestCase(void)44 void G711EncPluginUnitTest::SetUpTestCase(void)
45 {
46     cout << "[SetUpTestCase]: " << endl;
47 }
48 
TearDownTestCase(void)49 void G711EncPluginUnitTest::TearDownTestCase(void)
50 {
51     cout << "[TearDownTestCase]: " << endl;
52 }
53 
SetUp(void)54 void G711EncPluginUnitTest::SetUp(void)
55 {
56     g711EncPlugin_ = std::make_shared<AudioG711muEncoderPlugin>();
57     cout << "[SetUp]: SetUp!!!" << endl;
58 }
59 
TearDown(void)60 void G711EncPluginUnitTest::TearDown(void)
61 {
62     g711EncPlugin_->Release();
63     g711EncPlugin_->Reset();
64     cout << "[TearDown]: over!!!" << endl;
65 }
66 
67 /**
68  * @tc.name: G711MuLawEncode_001
69  * @tc.desc: negative value
70  * @tc.type: FUNC
71  */
72 HWTEST_F(G711EncPluginUnitTest, G711MuLawEncode_001, TestSize.Level1)
73 {
74     Format format;
75     format.PutIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, SUPPORT_SAMPLE_RATE);
76     format.PutIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, SUPPORT_CHANNELS);
77     format.PutIntValue(MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT, SAMPLE_S16LE);
78     auto ret = g711EncPlugin_->Init(format);
79     EXPECT_EQ(AVCodecServiceErrCode::AVCS_ERR_OK, ret);
80 
81     ret = g711EncPlugin_->CheckFormat(format);
82     EXPECT_EQ(AVCodecServiceErrCode::AVCS_ERR_OK, ret);
83 
84     int16_t pcmValue = -3000; // negative value
85     uint8_t result = g711EncPlugin_->G711MuLawEncode(pcmValue);
86     EXPECT_EQ(result, 0x37);
87 }
88 
89 /**
90  * @tc.name: G711MuLawEncode_002
91  * @tc.desc: zero value
92  * @tc.type: FUNC
93  */
94 HWTEST_F(G711EncPluginUnitTest, G711MuLawEncode_002, TestSize.Level1)
95 {
96     Format format;
97     format.PutIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, SUPPORT_SAMPLE_RATE);
98     format.PutIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, SUPPORT_CHANNELS);
99     format.PutIntValue(MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT, SAMPLE_S16LE);
100     auto ret = g711EncPlugin_->Init(format);
101     EXPECT_EQ(AVCodecServiceErrCode::AVCS_ERR_OK, ret);
102 
103     ret = g711EncPlugin_->CheckFormat(format);
104     EXPECT_EQ(AVCodecServiceErrCode::AVCS_ERR_OK, ret);
105 
106     int16_t pcmValue = 0; // zero value
107     uint8_t result = g711EncPlugin_->G711MuLawEncode(pcmValue);
108     EXPECT_EQ(result, 0xFF);
109 }
110 
111 /**
112  * @tc.name: G711MuLawEncode_003
113  * @tc.desc: positive and LT AVCODEC_G711MU_CLIP
114  * @tc.type: FUNC
115  */
116 HWTEST_F(G711EncPluginUnitTest, G711MuLawEncode_003, TestSize.Level1)
117 {
118     Format format;
119     format.PutIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, SUPPORT_SAMPLE_RATE);
120     format.PutIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, SUPPORT_CHANNELS);
121     format.PutIntValue(MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT, SAMPLE_S16LE);
122     auto ret = g711EncPlugin_->Init(format);
123     EXPECT_EQ(AVCodecServiceErrCode::AVCS_ERR_OK, ret);
124 
125     ret = g711EncPlugin_->CheckFormat(format);
126     EXPECT_EQ(AVCodecServiceErrCode::AVCS_ERR_OK, ret);
127 
128     int16_t pcmValue = 3000; // positive and LT AVCODEC_G711MU_CLIP
129     uint8_t result = g711EncPlugin_->G711MuLawEncode(pcmValue);
130     EXPECT_EQ(result, 0xB7);
131 }
132 /**
133  * @tc.name: G711MuLawEncode_004
134  * @tc.desc: // positive and GT AVCODEC_G711MU_CLIP
135  * @tc.type: FUNC
136  */
137 HWTEST_F(G711EncPluginUnitTest, G711MuLawEncode_004, TestSize.Level1)
138 {
139     Format format;
140     format.PutIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, SUPPORT_SAMPLE_RATE);
141     format.PutIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, SUPPORT_CHANNELS);
142     format.PutIntValue(MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT, SAMPLE_S16LE);
143     auto ret = g711EncPlugin_->Init(format);
144     EXPECT_EQ(AVCodecServiceErrCode::AVCS_ERR_OK, ret);
145 
146     ret = g711EncPlugin_->CheckFormat(format);
147     EXPECT_EQ(AVCodecServiceErrCode::AVCS_ERR_OK, ret);
148 
149     int16_t pcmValue = 10000; // positive and GT AVCODEC_G711MU_CLIP
150     uint8_t result = g711EncPlugin_->G711MuLawEncode(pcmValue);
151     EXPECT_EQ(result, 0x9C);
152 }
153 
154 /**
155  * @tc.name: G711MuLawEncode_005
156  * @tc.desc: GT AVCODEC_G711MU_CLIP and LE AVCODEC_G711MU_SEG_END[7]
157  * @tc.type: FUNC
158  */
159 HWTEST_F(G711EncPluginUnitTest, G711MuLawEncode_005, TestSize.Level1)
160 {
161     Format format;
162     format.PutIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, SUPPORT_SAMPLE_RATE);
163     format.PutIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, SUPPORT_CHANNELS);
164     format.PutIntValue(MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT, SAMPLE_S16LE);
165     auto ret = g711EncPlugin_->Init(format);
166     EXPECT_EQ(AVCodecServiceErrCode::AVCS_ERR_OK, ret);
167 
168     ret = g711EncPlugin_->CheckFormat(format);
169     EXPECT_EQ(AVCodecServiceErrCode::AVCS_ERR_OK, ret);
170 
171     int16_t pcmValue = 8500; // GT AVCODEC_G711MU_CLIP and LE AVCODEC_G711MU_SEG_END[7]
172     uint8_t result = g711EncPlugin_->G711MuLawEncode(pcmValue);
173     EXPECT_EQ(result, 0x9F);
174 }
175 
176 /**
177  * @tc.name: G711MuLawEncode_006
178  * @tc.desc: GT AVCODEC_G711MU_SEG_END[7], seg = 8
179  * @tc.type: FUNC
180  */
181 HWTEST_F(G711EncPluginUnitTest, G711MuLawEncode_006, TestSize.Level1)
182 {
183     Format format;
184     format.PutIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, SUPPORT_SAMPLE_RATE);
185     format.PutIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, SUPPORT_CHANNELS);
186     format.PutIntValue(MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT, SAMPLE_S16LE);
187     auto ret = g711EncPlugin_->Init(format);
188     EXPECT_EQ(AVCodecServiceErrCode::AVCS_ERR_OK, ret);
189 
190     ret = g711EncPlugin_->CheckFormat(format);
191     EXPECT_EQ(AVCodecServiceErrCode::AVCS_ERR_OK, ret);
192 
193     int16_t pcmValue = 32670; // GT AVCODEC_G711MU_SEG_END[7], seg EQ 8
194     uint8_t result = g711EncPlugin_->G711MuLawEncode(pcmValue);
195     EXPECT_EQ(result, 0x80);
196 }
197 
198 /**
199  * @tc.name: G711MuLawEncode_007
200  * @tc.desc: AVCODEC_G711MU_CLIP
201  * @tc.type: FUNC
202  */
203 HWTEST_F(G711EncPluginUnitTest, G711MuLawEncode_007, TestSize.Level1)
204 {
205     Format format;
206     format.PutIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, SUPPORT_SAMPLE_RATE);
207     format.PutIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, SUPPORT_CHANNELS);
208     format.PutIntValue(MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT, SAMPLE_S16LE);
209     auto ret = g711EncPlugin_->Init(format);
210     EXPECT_EQ(AVCodecServiceErrCode::AVCS_ERR_OK, ret);
211 
212     ret = g711EncPlugin_->CheckFormat(format);
213     EXPECT_EQ(AVCodecServiceErrCode::AVCS_ERR_OK, ret);
214 
215     int16_t pcmValue = 8159; // EQ AVCODEC_G711MU_CLIP
216     uint8_t result = g711EncPlugin_->G711MuLawEncode(pcmValue);
217     EXPECT_EQ(result, 0x9F);
218 }
219 
220 /**
221  * @tc.name: G711MuLawEncode_008
222  * @tc.desc: GT AVCODEC_G711MU_CLIP and LE AVCODEC_G711MU_SEG_END[8]
223  * @tc.type: FUNC
224  */
225 HWTEST_F(G711EncPluginUnitTest, G711MuLawEncode_008, TestSize.Level1)
226 {
227     Format format;
228     format.PutIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, SUPPORT_SAMPLE_RATE);
229     format.PutIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, SUPPORT_CHANNELS);
230     format.PutIntValue(MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT, SAMPLE_S16LE);
231     auto ret = g711EncPlugin_->Init(format);
232     EXPECT_EQ(AVCodecServiceErrCode::AVCS_ERR_OK, ret);
233 
234     ret = g711EncPlugin_->CheckFormat(format);
235     EXPECT_EQ(AVCodecServiceErrCode::AVCS_ERR_OK, ret);
236 
237     int16_t pcmValue = 8200; // GT AVCODEC_G711MU_CLIP and LE AVCODEC_G711MU_SEG_END[8]
238     uint8_t result = g711EncPlugin_->G711MuLawEncode(pcmValue);
239     EXPECT_EQ(result, 0x9F);
240 }
241 }  // namespace MediaAVCodec
242 }  // namespace OHOS