• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 <cstddef>
17 #include <cstdint>
18 #include "avcodec_common.h"
19 #include "avcodec_audio_common.h"
20 #include "native_avcodec_audioencoder.h"
21 #include "common/native_mfmagic.h"
22 #include "native_avcodec_audiocodec.h"
23 #include "avcodec_audio_encoder.h"
24 #include <fuzzer/FuzzedDataProvider.h>
25 #define FUZZ_PROJECT_NAME "audiodecoderConfigure_fuzzer"
26 namespace OHOS {
AudioAACConfigureFuzzTest(const uint8_t * data,size_t size)27 bool AudioAACConfigureFuzzTest(const uint8_t *data, size_t size)
28 {
29     if (size < sizeof(int64_t)) {
30         return false;
31     }
32     OH_AVCodec *source =  OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AAC, true);
33 
34     FuzzedDataProvider fdp(data, size);
35     int32_t intData = fdp.ConsumeIntegral<int32_t>();
36     int64_t longData = fdp.ConsumeIntegral<int64_t>();
37     OH_AVFormat *format = OH_AVFormat_Create();
38     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, intData);
39     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, intData);
40     OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, longData);
41     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, intData);
42     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITS_PER_CODED_SAMPLE, intData);
43     OH_AVFormat_SetIntValue(format, OH_MD_KEY_COMPLIANCE_LEVEL, intData);
44     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AAC_IS_ADTS, 1); //aactest
45 
46     OH_AudioCodec_Configure(source, format);
47     if (source) {
48         OH_AudioCodec_Destroy(source);
49     }
50     if (format != nullptr) {
51         OH_AVFormat_Destroy(format);
52         format = nullptr;
53     }
54 
55     OH_AVCodec *encodersource =  OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AAC, false);
56     if (encodersource == nullptr) {
57         return false;
58     }
59 
60     format = OH_AVFormat_Create();
61     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, intData);
62     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, intData);
63     OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, longData);
64     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, intData);
65     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITS_PER_CODED_SAMPLE, intData);
66     OH_AVFormat_SetIntValue(format, OH_MD_KEY_COMPLIANCE_LEVEL, intData);
67 
68     OH_AudioCodec_Configure(encodersource, format);
69     if (encodersource) {
70         OH_AudioCodec_Destroy(encodersource);
71     }
72     if (format != nullptr) {
73         OH_AVFormat_Destroy(format);
74         format = nullptr;
75     }
76     return true;
77 }
78 
AudioFlacConfigureFuzzTest(const uint8_t * data,size_t size)79 bool AudioFlacConfigureFuzzTest(const uint8_t *data, size_t size)
80 {
81     if (size < sizeof(int64_t)) {
82         return false;
83     }
84     OH_AVCodec *decodersource =  OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_FLAC, true);
85     if (decodersource == nullptr) {
86         return false;
87     }
88     FuzzedDataProvider fdp(data, size);
89     int32_t intData = fdp.ConsumeIntegral<int32_t>();
90     int64_t longData = fdp.ConsumeIntegral<int64_t>();
91     OH_AVFormat *format = OH_AVFormat_Create();
92     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, intData);
93     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, intData);
94     OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, longData);
95     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, intData);
96     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITS_PER_CODED_SAMPLE, intData);
97     OH_AVFormat_SetIntValue(format, OH_MD_KEY_COMPLIANCE_LEVEL, intData);
98 
99     OH_AudioCodec_Configure(decodersource, format);
100     if (decodersource) {
101         OH_AudioCodec_Destroy(decodersource);
102     }
103     if (format != nullptr) {
104         OH_AVFormat_Destroy(format);
105         format = nullptr;
106     }
107 
108     OH_AVCodec *encodersource =  OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_FLAC, false);
109     if (encodersource == nullptr) {
110         return false;
111     }
112 
113     format = OH_AVFormat_Create();
114     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, intData);
115     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, intData);
116     OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, longData);
117     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, intData);
118     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITS_PER_CODED_SAMPLE, intData);
119     OH_AVFormat_SetIntValue(format, OH_MD_KEY_COMPLIANCE_LEVEL, intData);
120 
121     OH_AudioCodec_Configure(encodersource, format);
122     if (encodersource) {
123         OH_AudioCodec_Destroy(encodersource);
124     }
125     if (format != nullptr) {
126         OH_AVFormat_Destroy(format);
127         format = nullptr;
128     }
129     return true;
130 }
131 
AudioMP3ConfigureFuzzTest(const uint8_t * data,size_t size)132 bool AudioMP3ConfigureFuzzTest(const uint8_t *data, size_t size)
133 {
134     if (size < sizeof(int64_t)) {
135         return false;
136     }
137     OH_AVCodec *decodersource =  OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_MPEG, true);
138     if (decodersource == nullptr) {
139         return false;
140     }
141     FuzzedDataProvider fdp(data, size);
142     int32_t intData = fdp.ConsumeIntegral<int32_t>();
143     int64_t longData = fdp.ConsumeIntegral<int64_t>();
144     OH_AVFormat *format = OH_AVFormat_Create();
145     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, intData);
146     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, intData);
147     OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, longData);
148     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, intData);
149     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITS_PER_CODED_SAMPLE, intData);
150     OH_AVFormat_SetIntValue(format, OH_MD_KEY_COMPLIANCE_LEVEL, intData);
151 
152     OH_AudioCodec_Configure(decodersource, format);
153     if (decodersource) {
154         OH_AudioCodec_Destroy(decodersource);
155     }
156     if (format != nullptr) {
157         OH_AVFormat_Destroy(format);
158         format = nullptr;
159     }
160     return true;
161 }
162 
AudioVorbisConfigureFuzzTest(const uint8_t * data,size_t size)163 bool AudioVorbisConfigureFuzzTest(const uint8_t *data, size_t size)
164 {
165     if (size < sizeof(int64_t)) {
166         return false;
167     }
168     OH_AVCodec *decodersource =  OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_VORBIS, true);
169     if (decodersource == nullptr) {
170         return false;
171     }
172     FuzzedDataProvider fdp(data, size);
173     int32_t intData = fdp.ConsumeIntegral<int32_t>();
174     int64_t longData = fdp.ConsumeIntegral<int64_t>();
175     OH_AVFormat *format = OH_AVFormat_Create();
176     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, intData);
177     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, intData);
178     OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, longData);
179     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, intData);
180     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITS_PER_CODED_SAMPLE, intData);
181     OH_AVFormat_SetIntValue(format, OH_MD_KEY_COMPLIANCE_LEVEL, intData);
182 
183     OH_AudioCodec_Configure(decodersource, format);
184     if (decodersource) {
185         OH_AudioCodec_Destroy(decodersource);
186     }
187     if (format != nullptr) {
188         OH_AVFormat_Destroy(format);
189         format = nullptr;
190     }
191     return true;
192 }
193 
AudioLBVCConfigureFuzzTest(const uint8_t * data,size_t size)194 bool AudioLBVCConfigureFuzzTest(const uint8_t *data, size_t size)
195 {
196     if (size < sizeof(int64_t)) {
197         return false;
198     }
199     OH_AVCodec *decodersource =  OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_LBVC, true);
200     if (decodersource == nullptr) {
201         return false;
202     }
203     FuzzedDataProvider fdp(data, size);
204     int32_t intData = fdp.ConsumeIntegral<int32_t>();
205     int64_t longData = fdp.ConsumeIntegral<int64_t>();
206     OH_AVFormat *format = OH_AVFormat_Create();
207     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, intData);
208     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, intData);
209     OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, longData);
210     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, intData);
211     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITS_PER_CODED_SAMPLE, intData);
212     OH_AVFormat_SetIntValue(format, OH_MD_KEY_COMPLIANCE_LEVEL, intData);
213 
214     OH_AudioCodec_Configure(decodersource, format);
215     if (decodersource) {
216         OH_AudioCodec_Destroy(decodersource);
217     }
218     if (format != nullptr) {
219         OH_AVFormat_Destroy(format);
220         format = nullptr;
221     }
222 
223     OH_AVCodec *encodersource =  OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_LBVC, false);
224     if (encodersource == nullptr) {
225         return false;
226     }
227 
228     format = OH_AVFormat_Create();
229     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, intData);
230     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, intData);
231     OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, longData);
232     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, intData);
233     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITS_PER_CODED_SAMPLE, intData);
234     OH_AVFormat_SetIntValue(format, OH_MD_KEY_COMPLIANCE_LEVEL, intData);
235 
236     OH_AudioCodec_Configure(encodersource, format);
237     if (encodersource) {
238         OH_AudioCodec_Destroy(encodersource);
239     }
240     if (format != nullptr) {
241         OH_AVFormat_Destroy(format);
242         format = nullptr;
243     }
244     return true;
245 }
246 
AudioAMRNBConfigureFuzzTest(const uint8_t * data,size_t size)247 bool AudioAMRNBConfigureFuzzTest(const uint8_t *data, size_t size)
248 {
249     if (size < sizeof(int64_t)) {
250         return false;
251     }
252     OH_AVCodec *decodersource =  OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AMR_NB, true);
253     if (decodersource == nullptr) {
254         return false;
255     }
256     FuzzedDataProvider fdp(data, size);
257     int32_t intData = fdp.ConsumeIntegral<int32_t>();
258     int64_t longData = fdp.ConsumeIntegral<int64_t>();
259     OH_AVFormat *format = OH_AVFormat_Create();
260     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, intData);
261     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, intData);
262     OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, longData);
263     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, intData);
264     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITS_PER_CODED_SAMPLE, intData);
265     OH_AVFormat_SetIntValue(format, OH_MD_KEY_COMPLIANCE_LEVEL, intData);
266 
267     OH_AudioCodec_Configure(decodersource, format);
268     if (decodersource) {
269         OH_AudioCodec_Destroy(decodersource);
270     }
271     if (format != nullptr) {
272         OH_AVFormat_Destroy(format);
273         format = nullptr;
274     }
275 
276     OH_AVCodec *encodersource =  OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AMR_NB, false);
277     if (encodersource == nullptr) {
278         return false;
279     }
280 
281     format = OH_AVFormat_Create();
282     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, intData);
283     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, intData);
284     OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, longData);
285     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, intData);
286     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITS_PER_CODED_SAMPLE, intData);
287     OH_AVFormat_SetIntValue(format, OH_MD_KEY_COMPLIANCE_LEVEL, intData);
288 
289     OH_AudioCodec_Configure(encodersource, format);
290     if (encodersource) {
291         OH_AudioCodec_Destroy(encodersource);
292     }
293     if (format != nullptr) {
294         OH_AVFormat_Destroy(format);
295         format = nullptr;
296     }
297     return true;
298 }
299 
AudioAMRWBConfigureFuzzTest(const uint8_t * data,size_t size)300 bool AudioAMRWBConfigureFuzzTest(const uint8_t *data, size_t size)
301 {
302     if (size < sizeof(int64_t)) {
303         return false;
304     }
305     OH_AVCodec *decodersource =  OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AMR_WB, true);
306     if (decodersource == nullptr) {
307         return false;
308     }
309     FuzzedDataProvider fdp(data, size);
310     int32_t intData = fdp.ConsumeIntegral<int32_t>();
311     int64_t longData = fdp.ConsumeIntegral<int64_t>();
312     OH_AVFormat *format = OH_AVFormat_Create();
313     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, intData);
314     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, intData);
315     OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, longData);
316     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, intData);
317     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITS_PER_CODED_SAMPLE, intData);
318     OH_AVFormat_SetIntValue(format, OH_MD_KEY_COMPLIANCE_LEVEL, intData);
319 
320     OH_AudioCodec_Configure(decodersource, format);
321     if (decodersource) {
322         OH_AudioCodec_Destroy(decodersource);
323     }
324     if (format != nullptr) {
325         OH_AVFormat_Destroy(format);
326         format = nullptr;
327     }
328 
329     OH_AVCodec *encodersource =  OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AMR_WB, false);
330     if (encodersource == nullptr) {
331         return false;
332     }
333 
334     format = OH_AVFormat_Create();
335     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, intData);
336     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, intData);
337     OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, longData);
338     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, intData);
339     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITS_PER_CODED_SAMPLE, intData);
340     OH_AVFormat_SetIntValue(format, OH_MD_KEY_COMPLIANCE_LEVEL, intData);
341 
342     OH_AudioCodec_Configure(encodersource, format);
343     if (encodersource) {
344         OH_AudioCodec_Destroy(encodersource);
345     }
346     if (format != nullptr) {
347         OH_AVFormat_Destroy(format);
348         format = nullptr;
349     }
350     return true;
351 }
352 
AudioAPEConfigureFuzzTest(const uint8_t * data,size_t size)353 bool AudioAPEConfigureFuzzTest(const uint8_t *data, size_t size)
354 {
355     if (size < sizeof(int64_t)) {
356         return false;
357     }
358     OH_AVCodec *decodersource =  OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_APE, true);
359     if (decodersource == nullptr) {
360         return false;
361     }
362     FuzzedDataProvider fdp(data, size);
363     int32_t intData = fdp.ConsumeIntegral<int32_t>();
364     int64_t longData = fdp.ConsumeIntegral<int64_t>();
365     OH_AVFormat *format = OH_AVFormat_Create();
366     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, intData);
367     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, intData);
368     OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, longData);
369     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, intData);
370     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITS_PER_CODED_SAMPLE, intData);
371     OH_AVFormat_SetIntValue(format, OH_MD_KEY_COMPLIANCE_LEVEL, intData);
372 
373     OH_AudioCodec_Configure(decodersource, format);
374     if (decodersource) {
375         OH_AudioCodec_Destroy(decodersource);
376     }
377     if (format != nullptr) {
378         OH_AVFormat_Destroy(format);
379         format = nullptr;
380     }
381 
382     OH_AVCodec *encodersource =  OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_APE, false);
383     if (encodersource == nullptr) {
384         return false;
385     }
386 
387     format = OH_AVFormat_Create();
388     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, intData);
389     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, intData);
390     OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, longData);
391     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, intData);
392     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITS_PER_CODED_SAMPLE, intData);
393     OH_AVFormat_SetIntValue(format, OH_MD_KEY_COMPLIANCE_LEVEL, intData);
394 
395     OH_AudioCodec_Configure(encodersource, format);
396     if (encodersource) {
397         OH_AudioCodec_Destroy(encodersource);
398     }
399     if (format != nullptr) {
400         OH_AVFormat_Destroy(format);
401         format = nullptr;
402     }
403     return true;
404 }
405 
AudioOPUSConfigureFuzzTest(const uint8_t * data,size_t size)406 bool AudioOPUSConfigureFuzzTest(const uint8_t *data, size_t size)
407 {
408     if (size < sizeof(int64_t)) {
409         return false;
410     }
411     OH_AVCodec *decodersource =  OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_OPUS, true);
412     if (decodersource == nullptr) {
413         return false;
414     }
415     FuzzedDataProvider fdp(data, size);
416     int32_t intData = fdp.ConsumeIntegral<int32_t>();
417     int64_t longData = fdp.ConsumeIntegral<int64_t>();
418     OH_AVFormat *format = OH_AVFormat_Create();
419     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, intData);
420     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, intData);
421     OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, longData);
422     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, intData);
423     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITS_PER_CODED_SAMPLE, intData);
424     OH_AVFormat_SetIntValue(format, OH_MD_KEY_COMPLIANCE_LEVEL, intData);
425 
426     OH_AudioCodec_Configure(decodersource, format);
427     if (decodersource) {
428         OH_AudioCodec_Destroy(decodersource);
429     }
430     if (format != nullptr) {
431         OH_AVFormat_Destroy(format);
432         format = nullptr;
433     }
434 
435     OH_AVCodec *encodersource =  OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_OPUS, false);
436     if (encodersource == nullptr) {
437         return false;
438     }
439 
440     format = OH_AVFormat_Create();
441     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, intData);
442     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, intData);
443     OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, longData);
444     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, intData);
445     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITS_PER_CODED_SAMPLE, intData);
446     OH_AVFormat_SetIntValue(format, OH_MD_KEY_COMPLIANCE_LEVEL, intData);
447 
448     OH_AudioCodec_Configure(encodersource, format);
449     if (encodersource) {
450         OH_AudioCodec_Destroy(encodersource);
451     }
452     if (format != nullptr) {
453         OH_AVFormat_Destroy(format);
454         format = nullptr;
455     }
456     return true;
457 }
458 
AudioG711ConfigureFuzzTest(const uint8_t * data,size_t size)459 bool AudioG711ConfigureFuzzTest(const uint8_t *data, size_t size)
460 {
461     if (size < sizeof(int64_t)) {
462         return false;
463     }
464     OH_AVCodec *decodersource =  OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_G711MU, true);
465     if (decodersource == nullptr) {
466         return false;
467     }
468     FuzzedDataProvider fdp(data, size);
469     int32_t intData = fdp.ConsumeIntegral<int32_t>();
470     int64_t longData = fdp.ConsumeIntegral<int64_t>();
471     OH_AVFormat *format = OH_AVFormat_Create();
472     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, intData);
473     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, intData);
474     OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, longData);
475     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, intData);
476     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITS_PER_CODED_SAMPLE, intData);
477     OH_AVFormat_SetIntValue(format, OH_MD_KEY_COMPLIANCE_LEVEL, intData);
478 
479     OH_AudioCodec_Configure(decodersource, format);
480     if (decodersource) {
481         OH_AudioCodec_Destroy(decodersource);
482     }
483     if (format != nullptr) {
484         OH_AVFormat_Destroy(format);
485         format = nullptr;
486     }
487 
488     OH_AVCodec *encodersource =  OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_G711MU, false);
489     if (encodersource == nullptr) {
490         return false;
491     }
492 
493     format = OH_AVFormat_Create();
494     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, intData);
495     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, intData);
496     OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, longData);
497     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, intData);
498     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITS_PER_CODED_SAMPLE, intData);
499     OH_AVFormat_SetIntValue(format, OH_MD_KEY_COMPLIANCE_LEVEL, intData);
500 
501     OH_AudioCodec_Configure(encodersource, format);
502     if (encodersource) {
503         OH_AudioCodec_Destroy(encodersource);
504     }
505     if (format != nullptr) {
506         OH_AVFormat_Destroy(format);
507         format = nullptr;
508     }
509     return true;
510 }
511 
AudioVividConfigureFuzzTest(const uint8_t * data,size_t size)512 bool AudioVividConfigureFuzzTest(const uint8_t *data, size_t size)
513 {
514     if (size < sizeof(int64_t)) {
515         return false;
516     }
517     OH_AVCodec *decodersource =  OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_VIVID, true);
518     if (decodersource == nullptr) {
519         return false;
520     }
521     FuzzedDataProvider fdp(data, size);
522     int32_t intData = fdp.ConsumeIntegral<int32_t>();
523     int64_t longData = fdp.ConsumeIntegral<int64_t>();
524     OH_AVFormat *format = OH_AVFormat_Create();
525     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, intData);
526     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, intData);
527     OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, longData);
528     OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, intData);
529     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITS_PER_CODED_SAMPLE, intData);
530     OH_AVFormat_SetIntValue(format, OH_MD_KEY_COMPLIANCE_LEVEL, intData);
531 
532     OH_AudioCodec_Configure(decodersource, format);
533     if (decodersource) {
534         OH_AudioCodec_Destroy(decodersource);
535     }
536     if (format != nullptr) {
537         OH_AVFormat_Destroy(format);
538         format = nullptr;
539     }
540     return true;
541 }
542 
543 } // namespace OHOS
544 
545 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)546 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
547 {
548     /* Run your code on data */
549     OHOS::AudioAACConfigureFuzzTest(data, size);
550     OHOS::AudioFlacConfigureFuzzTest(data, size);
551     OHOS::AudioMP3ConfigureFuzzTest(data, size);
552     OHOS::AudioVorbisConfigureFuzzTest(data, size);
553     OHOS::AudioLBVCConfigureFuzzTest(data, size);
554     OHOS::AudioAMRNBConfigureFuzzTest(data, size);
555     OHOS::AudioAMRWBConfigureFuzzTest(data, size);
556     OHOS::AudioAPEConfigureFuzzTest(data, size);
557     OHOS::AudioOPUSConfigureFuzzTest(data, size);
558     OHOS::AudioG711ConfigureFuzzTest(data, size);
559     OHOS::AudioVividConfigureFuzzTest(data, size);
560     return 0;
561 }
562