xref: /aosp_15_r20/frameworks/av/media/libstagefright/tests/fuzzers/FrameDecoderHelpers.h (revision ec779b8e0859a360c3d303172224686826e6e0e1)
1 
2 /*
3  * Copyright (C) 2020 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #pragma once
19 
20 #include <media/stagefright/MetaData.h>
21 #include "MediaMimeTypes.h"
22 
23 namespace android {
24 
25 std::vector<std::shared_ptr<char>> generated_mime_types;
26 constexpr uint8_t kMinKeyHeight = 32;
27 constexpr uint8_t kMinKeyWidth = 32;
28 constexpr uint16_t kMaxKeyHeight = 2160;
29 constexpr uint16_t kMaxKeyWidth = 3840;
30 size_t gMaxMediaBufferSize = 0;
31 
32 sp<MetaData> generateMetaData(FuzzedDataProvider* fdp, std::string componentName = std::string()) {
33     sp<MetaData> newMeta = sp<MetaData>::make();
34 
35     const char* mime;
36     if(!componentName.empty())
37     {
38         auto it = decoderToMediaType.find(componentName);
39         mime = it->second;
40     }
41     else{
42         size_t index = fdp->ConsumeIntegralInRange<size_t>(0, kMimeTypes.size());
43         // Let there be a chance of a true random string
44         if (index == kMimeTypes.size()) {
45             std::string mime_str = fdp->ConsumeRandomLengthString(64);
46             std::shared_ptr<char> mime_cstr(new char[mime_str.length()+1]);
47             generated_mime_types.push_back(mime_cstr);
48             strncpy(mime_cstr.get(), mime_str.c_str(), mime_str.length()+1);
49             mime = mime_cstr.get();
50         } else {
51             mime = kMimeTypes[index];
52         }
53     }
54     newMeta->setCString(kKeyMIMEType, mime);
55 
56     auto height = fdp->ConsumeIntegralInRange<uint16_t>(kMinKeyHeight, kMaxKeyHeight);
57     auto width = fdp->ConsumeIntegralInRange<uint16_t>(kMinKeyWidth, kMaxKeyWidth);
58     newMeta->setInt32(kKeyHeight, height);
59     newMeta->setInt32(kKeyWidth, width);
60 
61     gMaxMediaBufferSize = height * width;
62 
63     if (fdp->ConsumeBool()) {
64         newMeta->setInt32(kKeyTileHeight,
65                           fdp->ConsumeIntegralInRange<uint16_t>(kMinKeyHeight, height));
66         newMeta->setInt32(kKeyTileWidth,
67                           fdp->ConsumeIntegralInRange<uint16_t>(kMinKeyWidth, width));
68         newMeta->setInt32(kKeyGridRows, fdp->ConsumeIntegral<uint8_t>());
69         newMeta->setInt32(kKeyGridCols, fdp->ConsumeIntegral<uint8_t>());
70     }
71 
72     if (fdp->ConsumeBool()) {
73         newMeta->setInt32(kKeySARHeight, fdp->ConsumeIntegral<uint8_t>());
74         newMeta->setInt32(kKeySARWidth, fdp->ConsumeIntegral<uint8_t>());
75     }
76 
77     if (fdp->ConsumeBool()) {
78         newMeta->setInt32(kKeyDisplayHeight,
79                           fdp->ConsumeIntegralInRange<uint16_t>(height, UINT16_MAX));
80         newMeta->setInt32(kKeyDisplayWidth,
81                           fdp->ConsumeIntegralInRange<uint16_t>(width, UINT16_MAX));
82     }
83 
84     if (fdp->ConsumeBool()) {
85         newMeta->setRect(kKeyCropRect, fdp->ConsumeIntegral<int32_t>() /* left */,
86                          fdp->ConsumeIntegral<int32_t>() /* top */,
87                          fdp->ConsumeIntegral<int32_t>() /* right */,
88                          fdp->ConsumeIntegral<int32_t>() /* bottom */);
89     }
90 
91     if (fdp->ConsumeBool()) {
92         newMeta->setInt32(kKeyRotation, fdp->ConsumeIntegralInRange<uint8_t>(0, 3) * 90);
93     }
94 
95     if (fdp->ConsumeBool()) {
96         newMeta->setInt64(kKeyThumbnailTime, fdp->ConsumeIntegral<uint64_t>());
97         newMeta->setInt32(kKeyThumbnailHeight, fdp->ConsumeIntegral<uint8_t>());
98         newMeta->setInt32(kKeyThumbnailWidth, fdp->ConsumeIntegral<uint8_t>());
99 
100         size_t thumbnailSize = fdp->ConsumeIntegral<size_t>();
101         std::vector<uint8_t> thumbnailData = fdp->ConsumeBytes<uint8_t>(thumbnailSize);
102         if (mime == MEDIA_MIMETYPE_VIDEO_AV1) {
103             newMeta->setData(kKeyThumbnailAV1C, fdp->ConsumeIntegral<int32_t>() /* type */,
104                              thumbnailData.data(), thumbnailData.size());
105         } else {
106             newMeta->setData(kKeyThumbnailHVCC, fdp->ConsumeIntegral<int32_t>() /* type */,
107                              thumbnailData.data(), thumbnailData.size());
108         }
109     }
110 
111     if (fdp->ConsumeBool()) {
112         size_t profileSize = fdp->ConsumeIntegral<size_t>();
113         std::vector<uint8_t> profileData = fdp->ConsumeBytes<uint8_t>(profileSize);
114         newMeta->setData(kKeyIccProfile, fdp->ConsumeIntegral<int32_t>() /* type */,
115                          profileData.data(), profileData.size());
116     }
117 
118     return newMeta;
119 }
120 
121 }  // namespace android
122