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