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 #include <cstddef>
16 #include <cstdint>
17 #include <fuzzer/FuzzedDataProvider.h>
18 #include "native_avcapability.h"
19 #include "venc_sync_sample.h"
20
21
22 using namespace std;
23 using namespace OHOS;
24 using namespace OHOS::MediaAVCodec;
25
26 #define FUZZ_PROJECT_NAME "syncresourcecapienc_fuzzer"
27
28 std::string sourcePath = "/data/test/media/1280_720_nv.yuv";
29 uint32_t DEFAULT_WIDTH = 1280;
30 uint32_t DEFAULT_HEIGHT = 720;
31 std::shared_ptr<FormatMock> format = nullptr;
32 std::shared_ptr<VideoEncSyncSample> videoEnc = nullptr;
33
SetSync()34 void SetSync()
35 {
36 format->PutIntValue(MediaDescriptionKey::MD_KEY_WIDTH, DEFAULT_WIDTH);
37 format->PutIntValue(MediaDescriptionKey::MD_KEY_HEIGHT, DEFAULT_HEIGHT);
38 format->PutIntValue(MediaDescriptionKey::MD_KEY_PIXEL_FORMAT, static_cast<int32_t>(VideoPixelFormat::NV12));
39 format->PutIntValue(Media::Tag::AV_CODEC_ENABLE_SYNC_MODE, 1);
40 }
41
VideoEncoderFuzzTest(const uint8_t * data,size_t size)42 bool VideoEncoderFuzzTest(const uint8_t *data, size_t size)
43 {
44 std::shared_ptr<VEncSignal> vencSignal = std::make_shared<VEncSignal>();
45 videoEnc = std::make_shared<VideoEncSyncSample>(vencSignal);
46 if (videoEnc == nullptr) {
47 return false;
48 }
49
50 format = OHOS::MediaAVCodec::FormatMockFactory::CreateFormat();
51 if (!videoEnc->CreateVideoEncMockByMime(CodecMimeType::VIDEO_AVC.data())) {
52 return false;
53 }
54 SetSync();
55 FuzzedDataProvider fdp(data, size);
56 uint32_t rangeFlag = fdp.ConsumeIntegral<uint32_t>();
57 format->PutIntValue(MediaDescriptionKey::MD_KEY_RANGE_FLAG, rangeFlag);
58 videoEnc->Configure(format);
59 videoEnc->Prepare();
60 videoEnc->Start();
61
62 format = nullptr;
63 videoEnc = nullptr;
64 return true;
65 }
66
VideoEncoderResourceFuzzTest(const uint8_t * data,size_t size)67 bool VideoEncoderResourceFuzzTest(const uint8_t *data, size_t size)
68 {
69 std::shared_ptr<OHOS::MediaAVCodec::VEncSignal> vencSignal = std::make_shared<OHOS::MediaAVCodec::VEncSignal>();
70 videoEnc = std::make_shared<OHOS::MediaAVCodec::VideoEncSyncSample>(vencSignal);
71 if (videoEnc == nullptr) {
72 return false;
73 }
74
75 format = FormatMockFactory::CreateFormat();
76 if (!videoEnc->CreateVideoEncMockByMime(CodecMimeType::VIDEO_AVC.data())) {
77 return false;
78 }
79 SetSync();
80 videoEnc->Configure(format);
81 videoEnc->Prepare();
82 videoEnc->FuzzStart();
83 int ret = videoEnc->InputFuncFUZZ(data, size);
84 if (ret != 0) {
85 return false;
86 }
87
88 videoEnc->Stop();
89 format = nullptr;
90 videoEnc = nullptr;
91 return true;
92 }
93
94 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)95 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
96 {
97 /* Run your code on data */
98 VideoEncoderFuzzTest(data, size);
99 VideoEncoderResourceFuzzTest(data, size);
100 return 0;
101 }
102