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 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19 */
20 #include <algorithm>
21 #include <memory>
22
23 #include "ihevc_typedefs.h"
24 #include "itt_video_api.h"
25 #include "ihevce_api.h"
26 #include "ihevce_plugin.h"
27 #include "ihevce_profile.h"
28
29 constexpr size_t kRcType[] = {2, 3, 5};
30 constexpr IHEVCE_QUALITY_CONFIG_T kQuality[] = {
31 IHEVCE_QUALITY_P0, IHEVCE_QUALITY_P2, IHEVCE_QUALITY_P3, IHEVCE_QUALITY_P4,
32 IHEVCE_QUALITY_P5, IHEVCE_QUALITY_P6, IHEVCE_QUALITY_P7};
33
34 constexpr size_t kRcTypeNum = std::size(kRcType);
35 constexpr size_t kQualityNum = std::size(kQuality);
36 constexpr size_t kMaxQP = 51;
37 constexpr size_t kMaxGopPeriod = 16;
38 constexpr size_t kMaxWidth = 10240;
39 constexpr size_t kMaxHeight = 10240;
40 constexpr size_t kMaxBitrate = 500000000;
41
42 enum {
43 IDX_WD_BYTE_1,
44 IDX_WD_BYTE_2,
45 IDX_HT_BYTE_1,
46 IDX_HT_BYTE_2,
47 IDX_MAX_INTRA_TX_DEPTH,
48 IDX_MAX_INTER_TX_DEPTH,
49 IDX_CU_RC,
50 IDX_RC_MODE,
51 IDX_FRAME_QP,
52 IDX_PRESET,
53 IDX_BITRATE_BYTE_1,
54 IDX_BITRATE_BYTE_2,
55 IDX_ENABLE_ENTROPY_SYNC,
56 IDX_DEBLOCKING_TYPE,
57 IDX_USE_SC_MTX,
58 IDX_MAX_TEMPORAL_LAYERS,
59 IDX_MAX_CLOSED_GOP,
60 IDX_MIN_CLOSED_GOP,
61 IDX_MAX_I_OPEN_GOP,
62 IDX_MAX_CRA_OPEN_GOP,
63 IDX_ENABLE_SPS_AT_CDR,
64 IDX_ENABLE_VUI,
65 IDX_ENABLE_SEI,
66 IDX_ARCH_TYPE,
67 IDX_ENABLE_FORCE_IDR,
68 IDX_ENABLE_DYNAMIC_BITRATE,
69 IDX_FORCE_IDR_INTERVAL,
70 IDX_DYNAMIC_BITRATE_INTERVAL,
71 IDX_LAST
72 };
73
74 class Codec {
75 public:
76 Codec() = default;
~Codec()77 ~Codec() { deInitEncoder(); }
78 bool initEncoder(const uint8_t *data);
79 void deInitEncoder();
80 void encodeFrames(const uint8_t *data, size_t size);
81
82 private:
83 bool mIsForceIdrEnabled = false;
84 bool mIsDynamicBitrateChangeEnabled = false;
85 size_t mWidth = 352;
86 size_t mHeight = 288;
87 size_t mForceIdrInterval = 0; // in number of frames
88 size_t mDynamicBitrateInterval = 0; // in number of frames
89 uint64_t mBitrate = 5000000;
90 void *mCodecCtx = nullptr;
91 ihevce_static_cfg_params_t mEncParams = {};
92 };
93
initEncoder(const uint8_t * data)94 bool Codec::initEncoder(const uint8_t *data) {
95 // default configuration
96 if (IHEVCE_EOK != ihevce_set_def_params(&mEncParams)) {
97 return false;
98 }
99 mWidth = ((data[IDX_WD_BYTE_1] << 8) | data[IDX_WD_BYTE_2]) % kMaxWidth;
100 mHeight = ((data[IDX_HT_BYTE_1] << 8) | data[IDX_HT_BYTE_2]) % kMaxHeight;
101
102 // update configuration
103 mEncParams.s_src_prms.i4_width = mWidth;
104 mEncParams.s_src_prms.i4_height = mHeight;
105
106 mEncParams.s_config_prms.i4_max_tr_tree_depth_I = (data[IDX_MAX_INTRA_TX_DEPTH] % 3) + 1;
107 mEncParams.s_config_prms.i4_max_tr_tree_depth_nI = (data[IDX_MAX_INTER_TX_DEPTH] & 0x03) + 1;
108 mEncParams.s_config_prms.i4_cu_level_rc = data[IDX_CU_RC] & 0x01;
109 mEncParams.s_config_prms.i4_rate_control_mode = kRcType[data[IDX_RC_MODE] % kRcTypeNum];
110
111 mEncParams.s_tgt_lyr_prms.as_tgt_params[0].ai4_frame_qp[0] = (data[IDX_FRAME_QP] % kMaxQP) + 1;
112 mEncParams.s_tgt_lyr_prms.as_tgt_params[0].i4_quality_preset =
113 kQuality[data[IDX_PRESET] % kQualityNum];
114 mEncParams.s_tgt_lyr_prms.as_tgt_params[0].ai4_tgt_bitrate[0] =
115 (((data[IDX_BITRATE_BYTE_1] << 8) | data[IDX_BITRATE_BYTE_2]) * 1000) % kMaxBitrate;
116 mEncParams.s_tgt_lyr_prms.as_tgt_params[0].ai4_peak_bitrate[0] =
117 ((((data[IDX_BITRATE_BYTE_1] << 8) | data[IDX_BITRATE_BYTE_2]) * 1000) % kMaxBitrate) << 1;
118 mEncParams.s_coding_tools_prms.i4_enable_entropy_sync = data[IDX_ENABLE_ENTROPY_SYNC] & 0x01;
119 mEncParams.s_coding_tools_prms.i4_deblocking_type = data[IDX_DEBLOCKING_TYPE] & 0x01;
120 mEncParams.s_coding_tools_prms.i4_use_default_sc_mtx = data[IDX_USE_SC_MTX] & 0x01;
121 mEncParams.s_coding_tools_prms.i4_max_temporal_layers = data[IDX_MAX_TEMPORAL_LAYERS] & 0x02;
122 mEncParams.s_coding_tools_prms.i4_max_closed_gop_period =
123 data[IDX_MAX_CLOSED_GOP] % kMaxGopPeriod;
124 mEncParams.s_coding_tools_prms.i4_min_closed_gop_period =
125 data[IDX_MIN_CLOSED_GOP] % kMaxGopPeriod;
126 mEncParams.s_coding_tools_prms.i4_max_i_open_gop_period =
127 data[IDX_MAX_I_OPEN_GOP] % kMaxGopPeriod;
128 mEncParams.s_coding_tools_prms.i4_max_cra_open_gop_period =
129 data[IDX_MAX_CRA_OPEN_GOP] % kMaxGopPeriod;
130
131 mEncParams.s_out_strm_prms.i4_sps_at_cdr_enable = data[IDX_ENABLE_SPS_AT_CDR] & 0x01;
132 mEncParams.s_out_strm_prms.i4_vui_enable = data[IDX_ENABLE_VUI] & 0x01;
133 mEncParams.s_out_strm_prms.i4_sei_enable_flag = data[IDX_ENABLE_SEI] & 0x01;
134
135 mEncParams.e_arch_type = ((data[IDX_ARCH_TYPE] & 0x03) == 0x00) ? ARCH_ARM_NONEON : ARCH_NA;
136 mIsForceIdrEnabled = data[IDX_ENABLE_FORCE_IDR] & 0x01;
137 mIsDynamicBitrateChangeEnabled = data[IDX_ENABLE_DYNAMIC_BITRATE] & 0x01;
138 mForceIdrInterval = data[IDX_FORCE_IDR_INTERVAL] & 0x07;
139 mDynamicBitrateInterval = data[IDX_DYNAMIC_BITRATE_INTERVAL] & 0x07;
140
141 if (IHEVCE_EOK != ihevce_init(&mEncParams, &mCodecCtx)) {
142 return false;
143 }
144 return true;
145 }
146
encodeFrames(const uint8_t * data,size_t size)147 void Codec::encodeFrames(const uint8_t *data, size_t size) {
148 size_t frameSize = (mWidth * mHeight * 3) / 2;
149
150 ihevce_out_buf_t sHeaderOp{};
151 ihevce_encode_header(mCodecCtx, &sHeaderOp);
152 size_t frameNumber = 0;
153 uint8_t *tmpData = new uint8_t[frameSize];
154 while (size > 0) {
155 ihevce_inp_buf_t sEncodeIp{};
156 ihevce_out_buf_t sEncodeOp{};
157 size_t bytesConsumed = std::min(size, frameSize);
158 if (bytesConsumed < frameSize) {
159 memset(&tmpData[bytesConsumed], data[0], frameSize - bytesConsumed);
160 }
161 memcpy(tmpData, data, bytesConsumed);
162 int32_t yStride = mWidth;
163 int32_t uStride = mWidth >> 1;
164 int32_t vStride = mWidth >> 1;
165
166 sEncodeIp.apv_inp_planes[0] = tmpData;
167 sEncodeIp.apv_inp_planes[1] = tmpData + (mWidth * mHeight);
168 sEncodeIp.apv_inp_planes[2] = tmpData + ((mWidth * mHeight) * 5) / 4;
169
170 sEncodeIp.ai4_inp_strd[0] = yStride;
171 sEncodeIp.ai4_inp_strd[1] = uStride;
172 sEncodeIp.ai4_inp_strd[2] = vStride;
173
174 sEncodeIp.ai4_inp_size[0] = yStride * mHeight;
175 sEncodeIp.ai4_inp_size[1] = uStride * mHeight >> 1;
176 sEncodeIp.ai4_inp_size[2] = vStride * mHeight >> 1;
177
178 sEncodeIp.i4_force_idr_flag = 0;
179 sEncodeIp.i4_curr_bitrate = mBitrate;
180 sEncodeIp.i4_curr_peak_bitrate = mBitrate << 1;
181 sEncodeIp.u8_pts = 0;
182 if (mIsForceIdrEnabled) {
183 if (frameNumber == mForceIdrInterval) {
184 sEncodeIp.i4_force_idr_flag = 1;
185 }
186 }
187 if (mIsDynamicBitrateChangeEnabled) {
188 if (frameNumber == mDynamicBitrateInterval) {
189 mBitrate = mBitrate << 1;
190 }
191 }
192 ihevce_encode(mCodecCtx, &sEncodeIp, &sEncodeOp);
193 ++frameNumber;
194 data += bytesConsumed;
195 size -= bytesConsumed;
196 }
197 delete[] tmpData;
198 }
199
deInitEncoder()200 void Codec::deInitEncoder() {
201 if (mCodecCtx) {
202 ihevce_close(mCodecCtx);
203 mCodecCtx = nullptr;
204 }
205 return;
206 }
207
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)208 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
209 if (size < IDX_LAST) {
210 return 0;
211 }
212 Codec *codec = new Codec();
213 if (codec->initEncoder(data)) {
214 data += IDX_LAST;
215 size -= IDX_LAST;
216 codec->encodeFrames(data, size);
217 }
218 delete codec;
219 return 0;
220 }
221