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 constexpr int32_t DATA_INDEX = 0;
46 constexpr int32_t DATA_INDEX_OFFSET = 4;
47
48 std::shared_ptr<AVMetadataHelper> avmetadata = AVMetadataHelperFactory::CreateAVMetadataHelper();
49 if (avmetadata == nullptr) {
50 return false;
51 }
52
53 const string path = "/data/test/media/H264_AAC.mp4";
54 if (MetaDataSetSource(path, avmetadata) != 0) {
55 avmetadata->Release();
56 return false;
57 }
58
59 int32_t avMetadataQueryOption[AV_METADATA_QUERY_OPTION_LIST] {
60 AV_META_QUERY_NEXT_SYNC,
61 AV_META_QUERY_PREVIOUS_SYNC,
62 AV_META_QUERY_CLOSEST_SYNC,
63 AV_META_QUERY_CLOSEST
64 };
65
66 int32_t reproducibleRandom = abs((data[0] << 24) | (data[1] << 16) | (data[2] << 8) | (data[3]));
67 int32_t option = avMetadataQueryOption[reproducibleRandom % AV_METADATA_QUERY_OPTION_LIST];
68 PixelFormat colorFormats[AV_COLOR_FORMAT_LIST] {
69 PixelFormat::UNKNOWN,
70 PixelFormat::ARGB_8888,
71 PixelFormat::RGB_565,
72 PixelFormat::RGBA_8888,
73 PixelFormat::BGRA_8888,
74 PixelFormat::RGB_888,
75 PixelFormat::ALPHA_8,
76 PixelFormat::RGBA_F16,
77 PixelFormat::NV21,
78 PixelFormat::NV12,
79 PixelFormat::CMYK
80 };
81 PixelFormat colorFormat = colorFormats[data[DATA_INDEX] % AV_COLOR_FORMAT_LIST];
82
83 struct PixelMapParams pixelMapParams = {*reinterpret_cast<int32_t *>(data),
84 *reinterpret_cast<int32_t *>(data + DATA_INDEX_OFFSET), colorFormat};
85
86 std::shared_ptr<PixelMap> retFetchFrameAtTime =
87 avmetadata->FetchFrameAtTime(*reinterpret_cast<int64_t *>(data), option, pixelMapParams);
88
89 if (retFetchFrameAtTime != 0) {
90 avmetadata->Release();
91 return true;
92 }
93 avmetadata->Release();
94 return true;
95 }
96 }
97
FuzzTestAVMetadataFetchFrameAtTime(uint8_t * data,size_t size)98 bool FuzzTestAVMetadataFetchFrameAtTime(uint8_t *data, size_t size)
99 {
100 if (data == nullptr) {
101 return 0;
102 }
103
104 if (size < sizeof(int64_t)) {
105 return 0;
106 }
107 AVMetadataFetchFrameAtTimeFuzzer metadata;
108 return metadata.FuzzAVMetadataFetchFrameAtTime(data, size);
109 }
110 }
111
112 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)113 extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size)
114 {
115 /* Run your code on data */
116 OHOS::FuzzTestAVMetadataFetchFrameAtTime(data, size);
117 return 0;
118 }
119