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 "avmetadatafetchframeattime_fuzzer.h"
17 #include <iostream>
18 #include "aw_common.h"
19 #include "string_ex.h"
20 #include "media_errors.h"
21 #include "directory_ex.h"
22 #include "window_option.h"
23 #include "image_type.h"
24 
25 
26 using namespace std;
27 using namespace OHOS;
28 using namespace Media;
29 using namespace PlayerTestParam;
30 
31 namespace OHOS {
32 namespace Media {
AVMetadataFetchFrameAtTimeFuzzer()33 AVMetadataFetchFrameAtTimeFuzzer::AVMetadataFetchFrameAtTimeFuzzer()
34 {
35 }
36 
~AVMetadataFetchFrameAtTimeFuzzer()37 AVMetadataFetchFrameAtTimeFuzzer::~AVMetadataFetchFrameAtTimeFuzzer()
38 {
39 }
40 
FuzzAVMetadataFetchFrameAtTime(uint8_t * data,size_t size)41 bool AVMetadataFetchFrameAtTimeFuzzer::FuzzAVMetadataFetchFrameAtTime(uint8_t *data, size_t size)
42 {
43     constexpr int32_t AV_METADATA_QUERY_OPTION_LIST = 4;
44     constexpr int32_t AV_COLOR_FORMAT_LIST = 11;
45 
46     std::shared_ptr<AVMetadataHelper> avmetadata = AVMetadataHelperFactory::CreateAVMetadataHelper();
47     if (avmetadata == nullptr) {
48         return false;
49     }
50 
51     const string path = "/data/test/media/H264_AAC.mp4";
52     if (MetaDataSetSource(path, avmetadata) != 0) {
53         avmetadata->Release();
54         return false;
55     }
56 
57     int32_t avMetadataQueryOption[AV_METADATA_QUERY_OPTION_LIST] {
58         AV_META_QUERY_NEXT_SYNC,
59         AV_META_QUERY_PREVIOUS_SYNC,
60         AV_META_QUERY_CLOSEST_SYNC,
61         AV_META_QUERY_CLOSEST
62     };
63 
64     int32_t option = avMetadataQueryOption[ProduceRandomNumberCrypt() % AV_METADATA_QUERY_OPTION_LIST];
65     PixelFormat colorFormats[AV_COLOR_FORMAT_LIST] {
66         PixelFormat::UNKNOWN,
67         PixelFormat::ARGB_8888,
68         PixelFormat::RGB_565,
69         PixelFormat::RGBA_8888,
70         PixelFormat::BGRA_8888,
71         PixelFormat::RGB_888,
72         PixelFormat::ALPHA_8,
73         PixelFormat::RGBA_F16,
74         PixelFormat::NV21,
75         PixelFormat::NV12,
76         PixelFormat::CMYK
77     };
78     PixelFormat colorFormat = colorFormats[ProduceRandomNumberCrypt() % AV_COLOR_FORMAT_LIST];
79 
80     struct PixelMapParams pixelMapParams = {ProduceRandomNumberCrypt(), ProduceRandomNumberCrypt(), colorFormat};
81 
82     std::shared_ptr<PixelMap> retFetchFrameAtTime =
83         avmetadata->FetchFrameAtTime(*reinterpret_cast<int64_t *>(data), option, pixelMapParams);
84 
85     if (retFetchFrameAtTime != 0) {
86         avmetadata->Release();
87         return true;
88     }
89     avmetadata->Release();
90     return true;
91 }
92 }
93 
FuzzTestAVMetadataFetchFrameAtTime(uint8_t * data,size_t size)94 bool FuzzTestAVMetadataFetchFrameAtTime(uint8_t *data, size_t size)
95 {
96     if (data == nullptr) {
97         return 0;
98     }
99 
100     if (size < sizeof(int64_t)) {
101         return 0;
102     }
103     AVMetadataFetchFrameAtTimeFuzzer metadata;
104     return metadata.FuzzAVMetadataFetchFrameAtTime(data, size);
105 }
106 }
107 
108 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)109 extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size)
110 {
111     /* Run your code on data */
112     OHOS::FuzzTestAVMetadataFetchFrameAtTime(data, size);
113     return 0;
114 }
115