1 /*
2 * Copyright (c) 2022 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 "vcodecfile_fuzzer.h"
17 #include <iostream>
18 #include "string_ex.h"
19 #include "media_errors.h"
20 #include "directory_ex.h"
21 #include "ui/rs_surface_node.h"
22 #include "window_option.h"
23 #include "aw_common.h"
24
25 using namespace std;
26 using namespace OHOS;
27 using namespace OHOS::Media;
28 using namespace OHOS::Media::VCodecTestParam;
29 using namespace OHOS::Media::PlayerTestParam;
30
VCodecFileFuzzer()31 VCodecFileFuzzer::VCodecFileFuzzer()
32 {
33 }
34
~VCodecFileFuzzer()35 VCodecFileFuzzer::~VCodecFileFuzzer()
36 {
37 }
38
FuzzVideoFile(uint8_t * data,size_t size)39 bool VCodecFileFuzzer::FuzzVideoFile(uint8_t *data, size_t size)
40 {
41 constexpr int32_t WAITTING_TIME = 2;
42 std::shared_ptr<VDecSignal> vdecSignal = std::make_shared<VDecSignal>();
43 vdecCallback_ = std::make_shared<VDecCallbackTest>(vdecSignal);
44 CHECK_INSTANCE_AND_RETURN_RET(vdecCallback_, false);
45 videoDec_ = std::make_shared<VDecMock>(vdecSignal);
46 CHECK_INSTANCE_AND_RETURN_RET(videoDec_, false);
47 CHECK_BOOL_AND_RETURN_RET(videoDec_->CreateVideoDecMockByMime("video/avc"), false);
48 CHECK_STATE_AND_RETURN_RET(videoDec_->SetCallback(vdecCallback_), false);
49
50 std::shared_ptr<VEncSignal> vencSignal = std::make_shared<VEncSignal>();
51 vencCallback_ = std::make_shared<VEncCallbackTest>(vencSignal);
52 CHECK_INSTANCE_AND_RETURN_RET(vencCallback_, false);
53 videoEnc_ = std::make_shared<VEncMock>(vencSignal);
54 CHECK_INSTANCE_AND_RETURN_RET(videoEnc_, false);
55 CHECK_BOOL_AND_RETURN_RET(videoEnc_->CreateVideoEncMockByMime("video/avc"), false);
56 CHECK_STATE_AND_RETURN_RET(videoEnc_->SetCallback(vencCallback_), false);
57
58 videoEnc_->SetOutPath("/data/test/media/avc_configurefuzz_out.es");
59 std::shared_ptr<FormatMock> format = AVCodecMockFactory::CreateFormat();
60 CHECK_INSTANCE_AND_RETURN_RET(format, false);
61 (void)format->PutIntValue("width", DEFAULT_WIDTH);
62 (void)format->PutIntValue("height", DEFAULT_HEIGHT);
63 (void)format->PutIntValue("pixel_format", NV12);
64 (void)format->PutIntValue("frame_rate", DEFAULT_FRAME_RATE);
65 const string path = "/data/test/media/fuzztest.h264";
66 CHECK_STATE_AND_RETURN_RET(WriteDataToFile(path, data, size), false);
67 videoDec_->SetSource(path, ES_H264, ES_LENGTH_H264);
68 videoEnc_->Configure(format);
69 videoDec_->Configure(format);
70 std::shared_ptr<SurfaceMock> surface = videoEnc_->GetInputSurface();
71 videoDec_->SetOutputSurface(surface);
72 videoDec_->Prepare();
73 videoEnc_->Prepare();
74 videoDec_->Start();
75 videoEnc_->Start();
76 sleep(WAITTING_TIME);
77 if (videoDec_ != nullptr) {
78 videoDec_->Reset();
79 videoDec_->Release();
80 }
81 if (videoEnc_ != nullptr) {
82 videoEnc_->Reset();
83 videoEnc_->Release();
84 }
85 return true;
86 }
87
FuzzVCodecFile(uint8_t * data,size_t size)88 bool OHOS::Media::FuzzVCodecFile(uint8_t *data, size_t size)
89 {
90 auto codecfuzzer = std::make_unique<VCodecFileFuzzer>();
91 if (codecfuzzer == nullptr) {
92 cout << "codecfuzzer is null" << endl;
93 return 0;
94 }
95 if (size < sizeof(int32_t)) {
96 return 0;
97 }
98 return codecfuzzer->FuzzVideoFile(data, size);
99 }
100
101 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)102 extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size)
103 {
104 /* Run your code on data */
105 OHOS::Media::FuzzVCodecFile(data, size);
106 return 0;
107 }