• 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 #include <cstddef>
16 #include <cstdint>
17 #include "native_avcodec_base.h"
18 #include "native_avformat.h"
19 #include "videodec_sample.h"
20 #include <fuzzer/FuzzedDataProvider.h>
21 #define FUZZ_PROJECT_NAME "swdecodersetparameter_fuzzer"
22 using namespace std;
23 using namespace OHOS;
24 using namespace OHOS::Media;
25 static VDecFuzzSample *vDecSample = nullptr;
26 constexpr uint32_t DEFAULT_WIDTH = 1920;
27 constexpr uint32_t DEFAULT_HEIGHT = 1080;
28 constexpr double DEFAULT_FRAME_RATE = 30.0;
29 OH_AVFormat *format = nullptr;
30 
SetFormatValue(const uint8_t * data,size_t size)31 void SetFormatValue(const uint8_t *data, size_t size)
32 {
33     format = OH_AVFormat_CreateVideoFormat("video/avc", DEFAULT_WIDTH, DEFAULT_HEIGHT);
34     FuzzedDataProvider fdp(data, size);
35     std::string formatKey1 = fdp.ConsumeRandomLengthString();
36     std::string formatKey2 = fdp.ConsumeRandomLengthString();
37     std::string formatKey3 = fdp.ConsumeRandomLengthString();
38     OH_AVFormat_SetIntValue(format, OH_MD_KEY_BITRATE, fdp.ConsumeIntegral<int32_t>());
39     OH_AVFormat_SetIntValue(format, OH_MD_KEY_MAX_INPUT_SIZE, fdp.ConsumeIntegral<int32_t>());
40     OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, fdp.ConsumeIntegral<int32_t>());
41     OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, fdp.ConsumeIntegral<int32_t>());
42     OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, fdp.ConsumeIntegral<int32_t>());
43     OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODE_BITRATE_MODE, fdp.ConsumeIntegral<int32_t>());
44     OH_AVFormat_SetIntValue(format, OH_MD_KEY_PROFILE, fdp.ConsumeIntegral<int32_t>());
45     OH_AVFormat_SetIntValue(format, OH_MD_KEY_I_FRAME_INTERVAL, fdp.ConsumeIntegral<int32_t>());
46     OH_AVFormat_SetIntValue(format, OH_MD_KEY_ROTATION, fdp.ConsumeIntegral<int32_t>());
47     OH_AVFormat_SetLongValue(format, OH_MD_KEY_DURATION, fdp.ConsumeIntegral<int64_t>());
48     OH_AVFormat_SetDoubleValue(format, OH_MD_KEY_FRAME_RATE, fdp.ConsumeFloatingPoint<double>());
49     OH_AVFormat_SetIntValue(format, formatKey1.c_str(), fdp.ConsumeIntegral<int32_t>());
50     OH_AVFormat_SetLongValue(format, formatKey2.c_str(), fdp.ConsumeIntegral<int64_t>());
51     OH_AVFormat_SetDoubleValue(format, formatKey3.c_str(), fdp.ConsumeFloatingPoint<double>());
52 }
53 namespace OHOS {
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)54 bool DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
55 {
56     if (size < sizeof(int64_t)) {
57         return false;
58     }
59     if (!vDecSample) {
60         vDecSample = new VDecFuzzSample();
61         vDecSample->defaultWidth = DEFAULT_WIDTH;
62         vDecSample->defaultHeight = DEFAULT_HEIGHT;
63         vDecSample->defaultFrameRate = DEFAULT_FRAME_RATE;
64         vDecSample->isSurfMode = true;
65         if (vDecSample->CreateVideoDecoder("OH.Media.Codec.Decoder.Video.AVC") != AV_ERR_OK) {
66             delete vDecSample;
67             vDecSample = nullptr;
68             return false;
69         }
70         if (vDecSample->ConfigureVideoDecoder() != AV_ERR_OK) {
71             delete vDecSample;
72             vDecSample = nullptr;
73             return false;
74         }
75         if (vDecSample->SetVideoDecoderCallback() != AV_ERR_OK) {
76             delete vDecSample;
77             vDecSample = nullptr;
78             return false;
79         }
80         if (vDecSample->Start() != 0) {
81             delete vDecSample;
82             vDecSample = nullptr;
83             return false;
84         }
85     }
86 
87     SetFormatValue(data, size);
88     vDecSample->SetParameter(format);
89 
90     OH_AVFormat_Destroy(format);
91     delete vDecSample;
92     vDecSample = nullptr;
93     return true;
94 }
95 } // namespace OHOS
96 
97 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)98 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
99 {
100     /* Run your code on data */
101     OHOS::DoSomethingInterestingWithMyAPI(data, size);
102     return 0;
103 }
104