• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <malloc.h>
21 #include <algorithm>
22 #include <vector>
23 
24 #include "ih264_defs.h"
25 #include "ih264_typedefs.h"
26 #include "ih264e.h"
27 #include "ih264e_error.h"
28 #define ive_api_function ih264e_api_function
29 typedef std::tuple<uint8_t *, uint8_t *, uint8_t *> bufferPtrs;
30 
31 constexpr static int kMaxNumEncodeCalls = 100;
32 constexpr uint32_t kHeaderLength = 0x800;
33 constexpr int16_t kCompressionRatio = 1;
34 
35 constexpr int kMeSpeedPreset[] = {0, 50, 75, 100};
36 constexpr IVE_AIR_MODE_T kAirMode[] = {IVE_AIR_MODE_NONE, IVE_AIR_MODE_CYCLIC, IVE_AIR_MODE_RANDOM};
37 constexpr IVE_SPEED_CONFIG kEncSpeed[] = {IVE_CONFIG, IVE_SLOWEST,    IVE_NORMAL,
38                                           IVE_FAST,   IVE_HIGH_SPEED, IVE_FASTEST};
39 constexpr IV_PROFILE_T kProfle[] = {IV_PROFILE_BASE, IV_PROFILE_MAIN};
40 constexpr IVE_RC_MODE_T kRCMode[] = {IVE_RC_NONE, IVE_RC_STORAGE, IVE_RC_CBR_NON_LOW_DELAY,
41                                      IVE_RC_CBR_LOW_DELAY};
42 constexpr IV_COLOR_FORMAT_T kSupportedColorFormats[] = {IV_YUV_420P, IV_YUV_420SP_UV, IV_YUV_422ILE,
43                                                         IV_YUV_420SP_VU};
44 
45 constexpr size_t kAirModeNum = std::size(kAirMode);
46 constexpr size_t kEncSpeedNum = std::size(kEncSpeed);
47 constexpr size_t kMeSpeedPresetNum = std::size(kMeSpeedPreset);
48 constexpr size_t kProfleNum = std::size(kProfle);
49 constexpr size_t kRCModeNum = std::size(kRCMode);
50 constexpr size_t kSupportedColorFormatsNum = std::size(kSupportedColorFormats);
51 constexpr size_t kMinQP = 4;
52 constexpr size_t kMaxWidth = 10240;
53 constexpr size_t kMaxHeight = 10240;
54 constexpr size_t kMaxBitrate = 500000000;
55 
56 enum {
57     IDX_WD_BYTE_1,
58     IDX_WD_BYTE_2,
59     IDX_HT_BYTE_1,
60     IDX_HT_BYTE_2,
61     IDX_COLOR_FORMAT,
62     IDX_ARCH_TYPE,
63     IDX_RC_MODE,
64     IDX_NUM_CORES,
65     IDX_NUM_B_FRAMES,
66     IDX_ENC_SPEED,
67     IDX_CONSTRAINED_INTRA_FLAG,
68     IDX_INTRA_4x4,
69     IDX_I_FRAME_QP,
70     IDX_P_FRAME_QP,
71     IDX_B_FRAME_QP,
72     IDX_BITRATE_BYTE_1,
73     IDX_BITRATE_BYTE_2,
74     IDX_FRAME_RATE,
75     IDX_INTRA_REFRESH,
76     IDX_ENABLE_HALF_PEL,
77     IDX_ENABLE_Q_PEL,
78     IDX_ME_SPEED_PRESET,
79     IDX_AIR_MODE,
80     IDX_DISABLE_DEBLOCK_LEVEL,
81     IDX_SEARCH_RANGE_X,
82     IDX_SEARCH_RANGE_Y,
83     IDX_I_INTERVAL,
84     IDX_IDR_INTERVAL,
85     IDX_SEI_MDCV_FLAG,
86     IDX_SEI_CLL_FLAG,
87     IDX_SEI_AVE_FLAG,
88     IDX_SEI_CCV_FLAG,
89     IDX_PROFILE,
90     IDX_ASPECT_RATIO_FLAG,
91     IDX_NAL_HRD_FLAG,
92     IDX_VCL_HRD_FLAG,
93     IDX_ENABLE_FORCE_IDR,
94     IDX_ENABLE_DYNAMIC_BITRATE,
95     IDX_ENABLE_DYNAMIC_FRAME_RATE,
96     IDX_FORCE_IDR_INTERVAL,
97     IDX_DYNAMIC_BITRATE_INTERVAL,
98     IDX_DYNAMIC_FRAME_RATE_INTERVAL,
99     IDX_SEND_EOS_WITH_LAST_FRAME,
100     IDX_LAST
101 };
102 
103 class Codec {
104    public:
105     Codec() = default;
~Codec()106     ~Codec() { deInitEncoder(); }
107     bool initEncoder(const uint8_t **pdata, size_t *psize);
108     void encodeFrames(const uint8_t *data, size_t size);
109     void deInitEncoder();
110 
111    private:
112     bufferPtrs setEncParams(iv_raw_buf_t *psInpRawBuf, const uint8_t *data, size_t frameSize);
113     void setFrameType(IV_PICTURE_CODING_TYPE_T eFrameType);
114     void setQp();
115     void setEncMode(IVE_ENC_MODE_T eEncMode);
116     void setDimensions();
117     void setNumCores();
118     void setFrameRate();
119     void setIpeParams();
120     void setBitRate();
121     void setAirParams();
122     void setMeParams();
123     void setGopParams();
124     void setProfileParams();
125     void setDeblockParams();
126     void setVbvParams();
127     void setDefault();
128     void setVuiParams();
129     void getBufInfo();
130     void setSeiMdcvParams();
131     void setSeiCllParams();
132     void setSeiAveParams();
133     void setSeiCcvParams();
134     void logVersion();
135     bool mHalfPelEnable = 1;
136     bool mQPelEnable = 1;
137     bool mIntra4x4 = 0;
138     bool mEnableFastSad = 0;
139     bool mEnableAltRef = 0;
140     bool mConstrainedIntraFlag = 0;
141     bool mSeiCllFlag = 1;
142     bool mSeiAveFlag = 1;
143     bool mSeiCcvFlag = 1;
144     bool mSeiMdcvFlag = 1;
145     bool mAspectRatioFlag = 0;
146     bool mNalHrdFlag = 0;
147     bool mVclHrdFlag = 0;
148     bool mIsForceIdrEnabled = false;
149     bool mIsDynamicBitRateChangeEnabled = false;
150     bool mIsDynamicFrameRateChangeEnabled = false;
151     bool mSendEosWithLastFrame = false;
152     uint32_t mWidth = 2560;
153     uint32_t mHeight = 2560;
154     uint32_t mAvcEncLevel = 41;
155     uint32_t mNumMemRecords = 0;
156     uint32_t mNumCores = 1;
157     uint32_t mBframes = 0;
158     uint32_t mSliceParam = 256;
159     uint32_t mMeSpeedPreset = 100;
160     uint32_t mIInterval = 60;
161     uint32_t mIDRInterval = 60;
162     uint32_t mDisableDeblockLevel = 0;
163     uint32_t m_I_QP = 22;
164     uint32_t m_P_QP = 28;
165     uint32_t m_B_QP = 22;
166     uint32_t mIntraRefresh = 30;
167     uint32_t mSearchRangeX = 64;
168     uint32_t mSearchRangeY = 48;
169     uint32_t mForceIdrInterval = 0;          // in number of frames
170     uint32_t mDynamicBitRateInterval = 0;    // in number of frames
171     uint32_t mDynamicFrameRateInterval = 0;  // in number of frames
172     uint64_t mBitrate = 6000000;
173     float mFrameRate = 30;
174     iv_obj_t *mCodecCtx = nullptr;
175     iv_mem_rec_t *mMemRecords = nullptr;
176     IVE_AIR_MODE_T mAirMode = IVE_AIR_MODE_NONE;
177     IVE_SPEED_CONFIG mEncSpeed = IVE_NORMAL;
178     IVE_RC_MODE_T mRCMode = IVE_RC_STORAGE;
179     IV_ARCH_T mArch = ARCH_NA;
180     IVE_SLICE_MODE_T mSliceMode = IVE_SLICE_MODE_NONE;
181     IV_COLOR_FORMAT_T mIvVideoColorFormat = IV_YUV_420P;
182     IV_COLOR_FORMAT_T mReconFormat = IV_YUV_420P;
183     IV_PROFILE_T mProfile = IV_PROFILE_BASE;
184 };
185 
initEncoder(const uint8_t ** pdata,size_t * psize)186 bool Codec::initEncoder(const uint8_t **pdata, size_t *psize) {
187     uint8_t *data = const_cast<uint8_t *>(*pdata);
188     mWidth = ((data[IDX_WD_BYTE_1] << 8) | data[IDX_WD_BYTE_2]) % kMaxWidth;
189     mHeight = ((data[IDX_HT_BYTE_1] << 8) | data[IDX_HT_BYTE_2]) % kMaxHeight;
190 
191     mIvVideoColorFormat =
192         kSupportedColorFormats[data[IDX_COLOR_FORMAT] % kSupportedColorFormatsNum];
193     mArch = ((data[IDX_ARCH_TYPE] & 0x03) == 0x00) ? ARCH_ARM_NONEON : ARCH_NA;
194     mRCMode = kRCMode[data[IDX_RC_MODE] % kRCModeNum];
195     mNumCores = (data[IDX_NUM_CORES] & 0x07) + 1;
196     mBframes = data[IDX_NUM_B_FRAMES] & 0x07;
197     mEncSpeed = kEncSpeed[data[IDX_ENC_SPEED] % kEncSpeedNum];
198     mConstrainedIntraFlag = data[IDX_CONSTRAINED_INTRA_FLAG] & 0x01;
199     mIntra4x4 = data[IDX_INTRA_4x4] & 0x01;
200     m_I_QP = (data[IDX_I_FRAME_QP] % (MAX_H264_QP - kMinQP)) + kMinQP;
201     m_P_QP = (data[IDX_P_FRAME_QP] % (MAX_H264_QP - kMinQP)) + kMinQP;
202     m_B_QP = (data[IDX_B_FRAME_QP] % (MAX_H264_QP - kMinQP)) + kMinQP;
203     mBitrate = (((data[IDX_BITRATE_BYTE_1] << 8) | data[IDX_BITRATE_BYTE_2]) * 1000) % kMaxBitrate;
204     mFrameRate = data[IDX_FRAME_RATE];
205     mIntraRefresh = data[IDX_INTRA_REFRESH] + 1;
206     mHalfPelEnable = data[IDX_ENABLE_HALF_PEL] & 0x01;
207     mQPelEnable = data[IDX_ENABLE_Q_PEL] & 0x01;
208     mMeSpeedPreset = kMeSpeedPreset[data[IDX_ME_SPEED_PRESET] % kMeSpeedPresetNum];
209     mAirMode = kAirMode[data[IDX_AIR_MODE] % kAirModeNum];
210     mDisableDeblockLevel = data[IDX_DISABLE_DEBLOCK_LEVEL] & 0x03;
211     mSearchRangeX = data[IDX_SEARCH_RANGE_X];
212     mSearchRangeY = data[IDX_SEARCH_RANGE_Y];
213     mIInterval = data[IDX_I_INTERVAL] + 1;
214     mIDRInterval = data[IDX_IDR_INTERVAL] + 1;
215     mSeiMdcvFlag = data[IDX_SEI_MDCV_FLAG] & 0x01;
216     mSeiCllFlag = data[IDX_SEI_CLL_FLAG] & 0x01;
217     mSeiAveFlag = data[IDX_SEI_AVE_FLAG] & 0x01;
218     mSeiCcvFlag = data[IDX_SEI_CCV_FLAG] & 0x01;
219     mProfile = kProfle[data[IDX_PROFILE] % kProfleNum];
220     mAspectRatioFlag = data[IDX_ASPECT_RATIO_FLAG] & 0x01;
221     mNalHrdFlag = data[IDX_NAL_HRD_FLAG] & 0x01;
222     mVclHrdFlag = data[IDX_VCL_HRD_FLAG] & 0x01;
223     mIsForceIdrEnabled = data[IDX_ENABLE_FORCE_IDR] & 0x01;
224     mIsDynamicBitRateChangeEnabled = data[IDX_ENABLE_DYNAMIC_BITRATE] & 0x01;
225     mIsDynamicFrameRateChangeEnabled = data[IDX_ENABLE_DYNAMIC_FRAME_RATE] & 0x01;
226     mSendEosWithLastFrame = data[IDX_SEND_EOS_WITH_LAST_FRAME] & 0x01;
227     mForceIdrInterval = data[IDX_FORCE_IDR_INTERVAL] & 0x07;
228     mDynamicBitRateInterval = data[IDX_DYNAMIC_BITRATE_INTERVAL] & 0x07;
229     mDynamicFrameRateInterval = data[IDX_DYNAMIC_FRAME_RATE_INTERVAL] & 0x07;
230 
231     /* Getting Number of MemRecords */
232     iv_num_mem_rec_ip_t sNumMemRecIp{};
233     iv_num_mem_rec_op_t sNumMemRecOp{};
234 
235     sNumMemRecIp.u4_size = sizeof(iv_num_mem_rec_ip_t);
236     sNumMemRecOp.u4_size = sizeof(iv_num_mem_rec_op_t);
237     sNumMemRecIp.e_cmd = IV_CMD_GET_NUM_MEM_REC;
238 
239     if (IV_SUCCESS != ive_api_function(nullptr, &sNumMemRecIp, &sNumMemRecOp)) {
240         return false;
241     }
242     mNumMemRecords = sNumMemRecOp.u4_num_mem_rec;
243     mMemRecords = (iv_mem_rec_t *)malloc(mNumMemRecords * sizeof(iv_mem_rec_t));
244     if (!mMemRecords) {
245         return false;
246     }
247     iv_mem_rec_t *psMemRec;
248     psMemRec = mMemRecords;
249     for (size_t i = 0; i < mNumMemRecords; ++i) {
250         psMemRec->u4_size = sizeof(iv_mem_rec_t);
251         psMemRec->pv_base = nullptr;
252         psMemRec->u4_mem_size = 0;
253         psMemRec->u4_mem_alignment = 0;
254         psMemRec->e_mem_type = IV_NA_MEM_TYPE;
255         ++psMemRec;
256     }
257 
258     /* Getting MemRecords Attributes */
259     iv_fill_mem_rec_ip_t sFillMemRecIp{};
260     iv_fill_mem_rec_op_t sFillMemRecOp{};
261 
262     sFillMemRecIp.u4_size = sizeof(iv_fill_mem_rec_ip_t);
263     sFillMemRecOp.u4_size = sizeof(iv_fill_mem_rec_op_t);
264 
265     sFillMemRecIp.e_cmd = IV_CMD_FILL_NUM_MEM_REC;
266     sFillMemRecIp.ps_mem_rec = mMemRecords;
267     sFillMemRecIp.u4_num_mem_rec = mNumMemRecords;
268     sFillMemRecIp.u4_max_wd = mWidth;
269     sFillMemRecIp.u4_max_ht = mHeight;
270     sFillMemRecIp.u4_max_level = mAvcEncLevel;
271     sFillMemRecIp.e_color_format = IV_YUV_420SP_VU;
272     sFillMemRecIp.u4_max_ref_cnt = 2;
273     sFillMemRecIp.u4_max_reorder_cnt = 0;
274     sFillMemRecIp.u4_max_srch_rng_x = 256;
275     sFillMemRecIp.u4_max_srch_rng_y = 256;
276 
277     if (IV_SUCCESS != ive_api_function(nullptr, &sFillMemRecIp, &sFillMemRecOp)) {
278         return false;
279     }
280     /* Allocating Memory for Mem Records */
281     psMemRec = mMemRecords;
282     for (size_t i = 0; i < mNumMemRecords; ++i) {
283         posix_memalign(&psMemRec->pv_base, psMemRec->u4_mem_alignment, psMemRec->u4_mem_size);
284         if (!psMemRec->pv_base) {
285             return false;
286         }
287         ++psMemRec;
288     }
289 
290     /* Codec Instance Creation */
291     ive_init_ip_t sInitIp{};
292     ive_init_op_t sInitOp{};
293 
294     mCodecCtx = (iv_obj_t *)mMemRecords[0].pv_base;
295     mCodecCtx->u4_size = sizeof(iv_obj_t);
296     mCodecCtx->pv_fxns = (void *)ive_api_function;
297 
298     sInitIp.u4_size = sizeof(ive_init_ip_t);
299     sInitOp.u4_size = sizeof(ive_init_op_t);
300 
301     sInitIp.e_cmd = IV_CMD_INIT;
302     sInitIp.u4_num_mem_rec = mNumMemRecords;
303     sInitIp.ps_mem_rec = mMemRecords;
304     sInitIp.u4_max_wd = mWidth;
305     sInitIp.u4_max_ht = mHeight;
306     sInitIp.u4_max_ref_cnt = 2;
307     sInitIp.u4_max_reorder_cnt = 0;
308     sInitIp.u4_max_level = mAvcEncLevel;
309     sInitIp.e_inp_color_fmt = mIvVideoColorFormat;
310     sInitIp.u4_enable_recon = 0;
311     sInitIp.e_recon_color_fmt = mReconFormat;
312     sInitIp.e_rc_mode = mRCMode;
313     sInitIp.u4_max_framerate = 120000;
314     sInitIp.u4_max_bitrate = 240000000;
315     sInitIp.u4_num_bframes = mBframes;
316     sInitIp.e_content_type = IV_PROGRESSIVE;
317     sInitIp.u4_max_srch_rng_x = 256;
318     sInitIp.u4_max_srch_rng_y = 256;
319     sInitIp.e_slice_mode = mSliceMode;
320     sInitIp.u4_slice_param = mSliceParam;
321     sInitIp.e_arch = mArch;
322     sInitIp.e_soc = SOC_GENERIC;
323 
324     if (IV_SUCCESS != ive_api_function(mCodecCtx, &sInitIp, &sInitOp)) {
325         return false;
326     }
327 
328     logVersion();
329     setDefault();
330     getBufInfo();
331     setNumCores();
332     setDimensions();
333     setFrameRate();
334     setIpeParams();
335     setBitRate();
336     setQp();
337     setAirParams();
338     setVbvParams();
339     setMeParams();
340     setGopParams();
341     setDeblockParams();
342     setVuiParams();
343     setSeiMdcvParams();
344     setSeiCllParams();
345     setSeiAveParams();
346     setSeiCcvParams();
347     setProfileParams();
348     setEncMode(IVE_ENC_MODE_HEADER);
349 
350     *pdata += IDX_LAST;
351     *psize -= IDX_LAST;
352     return true;
353 }
354 
setDimensions()355 void Codec::setDimensions() {
356     ive_ctl_set_dimensions_ip_t sDimensionsIp{};
357     ive_ctl_set_dimensions_op_t sDimensionsOp{};
358 
359     sDimensionsIp.e_cmd = IVE_CMD_VIDEO_CTL;
360     sDimensionsIp.e_sub_cmd = IVE_CMD_CTL_SET_DIMENSIONS;
361     sDimensionsIp.u4_ht = mHeight;
362     sDimensionsIp.u4_wd = mWidth;
363 
364     sDimensionsIp.u4_timestamp_high = -1;
365     sDimensionsIp.u4_timestamp_low = -1;
366 
367     sDimensionsIp.u4_size = sizeof(ive_ctl_set_dimensions_ip_t);
368     sDimensionsOp.u4_size = sizeof(ive_ctl_set_dimensions_op_t);
369 
370     ive_api_function(mCodecCtx, &sDimensionsIp, &sDimensionsOp);
371     return;
372 }
373 
setNumCores()374 void Codec::setNumCores() {
375     ive_ctl_set_num_cores_ip_t sNumCoresIp{};
376     ive_ctl_set_num_cores_op_t sNumCoresOp{};
377 
378     sNumCoresIp.e_cmd = IVE_CMD_VIDEO_CTL;
379     sNumCoresIp.e_sub_cmd = IVE_CMD_CTL_SET_NUM_CORES;
380     sNumCoresIp.u4_num_cores = mNumCores;
381 
382     sNumCoresIp.u4_timestamp_high = -1;
383     sNumCoresIp.u4_timestamp_low = -1;
384 
385     sNumCoresIp.u4_size = sizeof(ive_ctl_set_num_cores_ip_t);
386     sNumCoresOp.u4_size = sizeof(ive_ctl_set_num_cores_op_t);
387 
388     ive_api_function(mCodecCtx, (void *)&sNumCoresIp, (void *)&sNumCoresOp);
389     return;
390 }
391 
setDefault()392 void Codec::setDefault() {
393     ive_ctl_setdefault_ip_t sDefaultIp{};
394     ive_ctl_setdefault_op_t sDefaultOp{};
395 
396     sDefaultIp.e_cmd = IVE_CMD_VIDEO_CTL;
397     sDefaultIp.e_sub_cmd = IVE_CMD_CTL_SETDEFAULT;
398 
399     sDefaultIp.u4_timestamp_high = -1;
400     sDefaultIp.u4_timestamp_low = -1;
401 
402     sDefaultIp.u4_size = sizeof(ive_ctl_setdefault_ip_t);
403     sDefaultOp.u4_size = sizeof(ive_ctl_setdefault_op_t);
404 
405     ive_api_function(mCodecCtx, &sDefaultIp, &sDefaultOp);
406     return;
407 }
408 
getBufInfo()409 void Codec::getBufInfo() {
410     ih264e_ctl_getbufinfo_ip_t sGetBufInfoIp{};
411     ih264e_ctl_getbufinfo_op_t sGetBufInfoOp{};
412 
413     sGetBufInfoIp.s_ive_ip.u4_size = sizeof(ih264e_ctl_getbufinfo_ip_t);
414     sGetBufInfoOp.s_ive_op.u4_size = sizeof(ih264e_ctl_getbufinfo_op_t);
415 
416     sGetBufInfoIp.s_ive_ip.e_cmd = IVE_CMD_VIDEO_CTL;
417     sGetBufInfoIp.s_ive_ip.e_sub_cmd = IVE_CMD_CTL_GETBUFINFO;
418     sGetBufInfoIp.s_ive_ip.u4_max_ht = mHeight;
419     sGetBufInfoIp.s_ive_ip.u4_max_wd = mWidth;
420     sGetBufInfoIp.s_ive_ip.e_inp_color_fmt = mIvVideoColorFormat;
421 
422     ih264e_api_function(mCodecCtx, &sGetBufInfoIp, &sGetBufInfoOp);
423     return;
424 }
425 
setFrameRate()426 void Codec::setFrameRate() {
427     ive_ctl_set_frame_rate_ip_t sFrameRateIp{};
428     ive_ctl_set_frame_rate_op_t sFrameRateOp{};
429 
430     sFrameRateIp.e_cmd = IVE_CMD_VIDEO_CTL;
431     sFrameRateIp.e_sub_cmd = IVE_CMD_CTL_SET_FRAMERATE;
432     sFrameRateIp.u4_src_frame_rate = mFrameRate;
433     sFrameRateIp.u4_tgt_frame_rate = mFrameRate;
434 
435     sFrameRateIp.u4_timestamp_high = -1;
436     sFrameRateIp.u4_timestamp_low = -1;
437 
438     sFrameRateIp.u4_size = sizeof(ive_ctl_set_frame_rate_ip_t);
439     sFrameRateOp.u4_size = sizeof(ive_ctl_set_frame_rate_op_t);
440 
441     ive_api_function(mCodecCtx, &sFrameRateIp, &sFrameRateOp);
442     return;
443 }
444 
setIpeParams()445 void Codec::setIpeParams() {
446     ive_ctl_set_ipe_params_ip_t sIpeParamsIp{};
447     ive_ctl_set_ipe_params_op_t sIpeParamsOp{};
448 
449     sIpeParamsIp.e_cmd = IVE_CMD_VIDEO_CTL;
450     sIpeParamsIp.e_sub_cmd = IVE_CMD_CTL_SET_IPE_PARAMS;
451     sIpeParamsIp.u4_enable_intra_4x4 = mIntra4x4;
452     sIpeParamsIp.u4_enc_speed_preset = mEncSpeed;
453     sIpeParamsIp.u4_constrained_intra_pred = mConstrainedIntraFlag;
454 
455     sIpeParamsIp.u4_timestamp_high = -1;
456     sIpeParamsIp.u4_timestamp_low = -1;
457 
458     sIpeParamsIp.u4_size = sizeof(ive_ctl_set_ipe_params_ip_t);
459     sIpeParamsOp.u4_size = sizeof(ive_ctl_set_ipe_params_op_t);
460 
461     ive_api_function(mCodecCtx, &sIpeParamsIp, &sIpeParamsOp);
462     return;
463 }
464 
setBitRate()465 void Codec::setBitRate() {
466     ive_ctl_set_bitrate_ip_t sBitrateIp{};
467     ive_ctl_set_bitrate_op_t sBitrateOp{};
468 
469     sBitrateIp.e_cmd = IVE_CMD_VIDEO_CTL;
470     sBitrateIp.e_sub_cmd = IVE_CMD_CTL_SET_BITRATE;
471     sBitrateIp.u4_target_bitrate = mBitrate;
472 
473     sBitrateIp.u4_timestamp_high = -1;
474     sBitrateIp.u4_timestamp_low = -1;
475 
476     sBitrateIp.u4_size = sizeof(ive_ctl_set_bitrate_ip_t);
477     sBitrateOp.u4_size = sizeof(ive_ctl_set_bitrate_op_t);
478 
479     ive_api_function(mCodecCtx, &sBitrateIp, &sBitrateOp);
480     return;
481 }
482 
setFrameType(IV_PICTURE_CODING_TYPE_T eFrameType)483 void Codec::setFrameType(IV_PICTURE_CODING_TYPE_T eFrameType) {
484     ive_ctl_set_frame_type_ip_t sFrameTypeIp{};
485     ive_ctl_set_frame_type_op_t sFrameTypeOp{};
486 
487     sFrameTypeIp.e_cmd = IVE_CMD_VIDEO_CTL;
488     sFrameTypeIp.e_sub_cmd = IVE_CMD_CTL_SET_FRAMETYPE;
489     sFrameTypeIp.e_frame_type = eFrameType;
490 
491     sFrameTypeIp.u4_timestamp_high = -1;
492     sFrameTypeIp.u4_timestamp_low = -1;
493 
494     sFrameTypeIp.u4_size = sizeof(ive_ctl_set_frame_type_ip_t);
495     sFrameTypeOp.u4_size = sizeof(ive_ctl_set_frame_type_op_t);
496 
497     ive_api_function(mCodecCtx, &sFrameTypeIp, &sFrameTypeOp);
498     return;
499 }
500 
setQp()501 void Codec::setQp() {
502     ive_ctl_set_qp_ip_t s_QpIp{};
503     ive_ctl_set_qp_op_t s_QpOp{};
504 
505     s_QpIp.e_cmd = IVE_CMD_VIDEO_CTL;
506     s_QpIp.e_sub_cmd = IVE_CMD_CTL_SET_QP;
507 
508     s_QpIp.u4_i_qp = m_I_QP;
509     s_QpIp.u4_i_qp_max = MAX_H264_QP;
510     s_QpIp.u4_i_qp_min = kMinQP;
511 
512     s_QpIp.u4_p_qp = m_P_QP;
513     s_QpIp.u4_p_qp_max = MAX_H264_QP;
514     s_QpIp.u4_p_qp_min = kMinQP;
515 
516     s_QpIp.u4_b_qp = m_B_QP;
517     s_QpIp.u4_b_qp_max = MAX_H264_QP;
518     s_QpIp.u4_b_qp_min = kMinQP;
519 
520     s_QpIp.u4_timestamp_high = -1;
521     s_QpIp.u4_timestamp_low = -1;
522 
523     s_QpIp.u4_size = sizeof(ive_ctl_set_qp_ip_t);
524     s_QpOp.u4_size = sizeof(ive_ctl_set_qp_op_t);
525 
526     ive_api_function(mCodecCtx, &s_QpIp, &s_QpOp);
527     return;
528 }
529 
setEncMode(IVE_ENC_MODE_T eEncMode)530 void Codec::setEncMode(IVE_ENC_MODE_T eEncMode) {
531     ive_ctl_set_enc_mode_ip_t sEncModeIp{};
532     ive_ctl_set_enc_mode_op_t sEncModeOp{};
533 
534     sEncModeIp.e_cmd = IVE_CMD_VIDEO_CTL;
535     sEncModeIp.e_sub_cmd = IVE_CMD_CTL_SET_ENC_MODE;
536     sEncModeIp.e_enc_mode = eEncMode;
537 
538     sEncModeIp.u4_timestamp_high = -1;
539     sEncModeIp.u4_timestamp_low = -1;
540 
541     sEncModeIp.u4_size = sizeof(ive_ctl_set_enc_mode_ip_t);
542     sEncModeOp.u4_size = sizeof(ive_ctl_set_enc_mode_op_t);
543 
544     ive_api_function(mCodecCtx, &sEncModeIp, &sEncModeOp);
545     return;
546 }
547 
setVbvParams()548 void Codec::setVbvParams() {
549     ive_ctl_set_vbv_params_ip_t sVbvIp{};
550     ive_ctl_set_vbv_params_op_t sVbvOp{};
551 
552     sVbvIp.e_cmd = IVE_CMD_VIDEO_CTL;
553     sVbvIp.e_sub_cmd = IVE_CMD_CTL_SET_VBV_PARAMS;
554     sVbvIp.u4_vbv_buf_size = 0;
555     sVbvIp.u4_vbv_buffer_delay = 1000;
556 
557     sVbvIp.u4_timestamp_high = -1;
558     sVbvIp.u4_timestamp_low = -1;
559 
560     sVbvIp.u4_size = sizeof(ive_ctl_set_vbv_params_ip_t);
561     sVbvOp.u4_size = sizeof(ive_ctl_set_vbv_params_op_t);
562 
563     ive_api_function(mCodecCtx, &sVbvIp, &sVbvOp);
564     return;
565 }
566 
setAirParams()567 void Codec::setAirParams() {
568     ive_ctl_set_air_params_ip_t sAirIp{};
569     ive_ctl_set_air_params_op_t sAirOp{};
570 
571     sAirIp.e_cmd = IVE_CMD_VIDEO_CTL;
572     sAirIp.e_sub_cmd = IVE_CMD_CTL_SET_AIR_PARAMS;
573     sAirIp.e_air_mode = mAirMode;
574     sAirIp.u4_air_refresh_period = mIntraRefresh;
575 
576     sAirIp.u4_timestamp_high = -1;
577     sAirIp.u4_timestamp_low = -1;
578 
579     sAirIp.u4_size = sizeof(ive_ctl_set_air_params_ip_t);
580     sAirOp.u4_size = sizeof(ive_ctl_set_air_params_op_t);
581 
582     ive_api_function(mCodecCtx, &sAirIp, &sAirOp);
583     return;
584 }
585 
setMeParams()586 void Codec::setMeParams() {
587     ive_ctl_set_me_params_ip_t sMeParamsIp{};
588     ive_ctl_set_me_params_op_t sMeParamsOp{};
589 
590     sMeParamsIp.e_cmd = IVE_CMD_VIDEO_CTL;
591     sMeParamsIp.e_sub_cmd = IVE_CMD_CTL_SET_ME_PARAMS;
592     sMeParamsIp.u4_enable_fast_sad = mEnableFastSad;
593     sMeParamsIp.u4_enable_alt_ref = mEnableAltRef;
594 
595     sMeParamsIp.u4_enable_hpel = mHalfPelEnable;
596     sMeParamsIp.u4_enable_qpel = mQPelEnable;
597     sMeParamsIp.u4_me_speed_preset = mMeSpeedPreset;
598     sMeParamsIp.u4_srch_rng_x = mSearchRangeX;
599     sMeParamsIp.u4_srch_rng_y = mSearchRangeY;
600 
601     sMeParamsIp.u4_timestamp_high = -1;
602     sMeParamsIp.u4_timestamp_low = -1;
603 
604     sMeParamsIp.u4_size = sizeof(ive_ctl_set_me_params_ip_t);
605     sMeParamsOp.u4_size = sizeof(ive_ctl_set_me_params_op_t);
606 
607     ive_api_function(mCodecCtx, &sMeParamsIp, &sMeParamsOp);
608     return;
609 }
610 
setGopParams()611 void Codec::setGopParams() {
612     ive_ctl_set_gop_params_ip_t sGopParamsIp{};
613     ive_ctl_set_gop_params_op_t sGopParamsOp{};
614 
615     sGopParamsIp.e_cmd = IVE_CMD_VIDEO_CTL;
616     sGopParamsIp.e_sub_cmd = IVE_CMD_CTL_SET_GOP_PARAMS;
617 
618     sGopParamsIp.u4_i_frm_interval = mIInterval;
619     sGopParamsIp.u4_idr_frm_interval = mIDRInterval;
620 
621     sGopParamsIp.u4_timestamp_high = -1;
622     sGopParamsIp.u4_timestamp_low = -1;
623 
624     sGopParamsIp.u4_size = sizeof(ive_ctl_set_gop_params_ip_t);
625     sGopParamsOp.u4_size = sizeof(ive_ctl_set_gop_params_op_t);
626 
627     ive_api_function(mCodecCtx, &sGopParamsIp, &sGopParamsOp);
628     return;
629 }
630 
setProfileParams()631 void Codec::setProfileParams() {
632     ive_ctl_set_profile_params_ip_t sProfileParamsIp{};
633     ive_ctl_set_profile_params_op_t sProfileParamsOp{};
634 
635     sProfileParamsIp.e_cmd = IVE_CMD_VIDEO_CTL;
636     sProfileParamsIp.e_sub_cmd = IVE_CMD_CTL_SET_PROFILE_PARAMS;
637 
638     sProfileParamsIp.e_profile = mProfile;
639     if (sProfileParamsIp.e_profile == IV_PROFILE_BASE) {
640         sProfileParamsIp.u4_entropy_coding_mode = 0;
641     } else {
642         sProfileParamsIp.u4_entropy_coding_mode = 1;
643     }
644     sProfileParamsIp.u4_timestamp_high = -1;
645     sProfileParamsIp.u4_timestamp_low = -1;
646 
647     sProfileParamsIp.u4_size = sizeof(ive_ctl_set_profile_params_ip_t);
648     sProfileParamsOp.u4_size = sizeof(ive_ctl_set_profile_params_op_t);
649 
650     ive_api_function(mCodecCtx, &sProfileParamsIp, &sProfileParamsOp);
651     return;
652 }
653 
setDeblockParams()654 void Codec::setDeblockParams() {
655     ive_ctl_set_deblock_params_ip_t sDeblockParamsIp{};
656     ive_ctl_set_deblock_params_op_t sDeblockParamsOp{};
657 
658     sDeblockParamsIp.e_cmd = IVE_CMD_VIDEO_CTL;
659     sDeblockParamsIp.e_sub_cmd = IVE_CMD_CTL_SET_DEBLOCK_PARAMS;
660 
661     sDeblockParamsIp.u4_disable_deblock_level = mDisableDeblockLevel;
662 
663     sDeblockParamsIp.u4_timestamp_high = -1;
664     sDeblockParamsIp.u4_timestamp_low = -1;
665 
666     sDeblockParamsIp.u4_size = sizeof(ive_ctl_set_deblock_params_ip_t);
667     sDeblockParamsOp.u4_size = sizeof(ive_ctl_set_deblock_params_op_t);
668 
669     ive_api_function(mCodecCtx, &sDeblockParamsIp, &sDeblockParamsOp);
670     return;
671 }
672 
setVuiParams()673 void Codec::setVuiParams() {
674     ih264e_vui_ip_t sVuiParamsIp{};
675     ih264e_vui_op_t sVuiParamsOp{};
676 
677     sVuiParamsIp.e_cmd = IVE_CMD_VIDEO_CTL;
678     sVuiParamsIp.e_sub_cmd = IVE_CMD_CTL_SET_VUI_PARAMS;
679 
680     sVuiParamsIp.u1_aspect_ratio_info_present_flag = mAspectRatioFlag;
681     sVuiParamsIp.u1_aspect_ratio_idc = 0;
682     sVuiParamsIp.u2_sar_width = 0;
683     sVuiParamsIp.u2_sar_height = 0;
684     sVuiParamsIp.u1_overscan_info_present_flag = 0;
685     sVuiParamsIp.u1_overscan_appropriate_flag = 0;
686     sVuiParamsIp.u1_video_signal_type_present_flag = 1;
687     sVuiParamsIp.u1_video_format = 0;
688     sVuiParamsIp.u1_video_full_range_flag = 0;
689     sVuiParamsIp.u1_colour_description_present_flag = 1;
690     sVuiParamsIp.u1_colour_primaries = 0;
691     sVuiParamsIp.u1_transfer_characteristics = 0;
692     sVuiParamsIp.u1_matrix_coefficients = 0;
693     sVuiParamsIp.u1_chroma_loc_info_present_flag = 0;
694     sVuiParamsIp.u1_chroma_sample_loc_type_top_field = 0;
695     sVuiParamsIp.u1_chroma_sample_loc_type_bottom_field = 0;
696     sVuiParamsIp.u1_vui_timing_info_present_flag = 0;
697     sVuiParamsIp.u4_vui_num_units_in_tick = 0;
698     sVuiParamsIp.u4_vui_time_scale = 0;
699     sVuiParamsIp.u1_fixed_frame_rate_flag = 0;
700     sVuiParamsIp.u1_nal_hrd_parameters_present_flag = mNalHrdFlag;
701     sVuiParamsIp.u1_vcl_hrd_parameters_present_flag = mVclHrdFlag;
702     sVuiParamsIp.u1_low_delay_hrd_flag = 0;
703     sVuiParamsIp.u1_pic_struct_present_flag = 0;
704     sVuiParamsIp.u1_bitstream_restriction_flag = 0;
705     sVuiParamsIp.u1_motion_vectors_over_pic_boundaries_flag = 0;
706     sVuiParamsIp.u1_max_bytes_per_pic_denom = 0;
707     sVuiParamsIp.u1_max_bits_per_mb_denom = 0;
708     sVuiParamsIp.u1_log2_max_mv_length_horizontal = 0;
709     sVuiParamsIp.u1_log2_max_mv_length_vertical = 0;
710     sVuiParamsIp.u1_num_reorder_frames = 0;
711     sVuiParamsIp.u1_max_dec_frame_buffering = 0;
712 
713     sVuiParamsIp.u4_size = sizeof(ih264e_vui_ip_t);
714     sVuiParamsOp.u4_size = sizeof(ih264e_vui_op_t);
715 
716     ive_api_function(mCodecCtx, &sVuiParamsIp, &sVuiParamsOp);
717     return;
718 }
719 
setSeiMdcvParams()720 void Codec::setSeiMdcvParams() {
721     ih264e_ctl_set_sei_mdcv_params_ip_t sSeiMdcvParamsIp{};
722     ih264e_ctl_set_sei_mdcv_params_op_t sSeiMdcvParamsOp{};
723 
724     sSeiMdcvParamsIp.e_cmd = IVE_CMD_VIDEO_CTL;
725     sSeiMdcvParamsIp.e_sub_cmd = IVE_CMD_CTL_SET_SEI_MDCV_PARAMS;
726     sSeiMdcvParamsIp.u1_sei_mdcv_params_present_flag = mSeiMdcvFlag;
727     if (mSeiMdcvFlag) {
728         for (int i4_count = 0; i4_count < NUM_SEI_MDCV_PRIMARIES; ++i4_count) {
729             sSeiMdcvParamsIp.au2_display_primaries_x[i4_count] = 30000;
730             sSeiMdcvParamsIp.au2_display_primaries_y[i4_count] = 35000;
731         }
732         sSeiMdcvParamsIp.u2_white_point_x = 30000;
733         sSeiMdcvParamsIp.u2_white_point_y = 35000;
734         sSeiMdcvParamsIp.u4_max_display_mastering_luminance = 100000000;
735         sSeiMdcvParamsIp.u4_min_display_mastering_luminance = 50000;
736     }
737 
738     sSeiMdcvParamsIp.u4_timestamp_high = -1;
739     sSeiMdcvParamsIp.u4_timestamp_low = -1;
740 
741     sSeiMdcvParamsIp.u4_size = sizeof(ih264e_ctl_set_sei_mdcv_params_ip_t);
742     sSeiMdcvParamsOp.u4_size = sizeof(ih264e_ctl_set_sei_mdcv_params_op_t);
743     ih264e_api_function(mCodecCtx, &sSeiMdcvParamsIp, &sSeiMdcvParamsOp);
744     return;
745 }
746 
setSeiCllParams()747 void Codec::setSeiCllParams() {
748     ih264e_ctl_set_sei_cll_params_ip_t sSeiCllParamsIp{};
749     ih264e_ctl_set_sei_cll_params_op_t sSeiCllParamsOp{};
750 
751     sSeiCllParamsIp.e_cmd = IVE_CMD_VIDEO_CTL;
752     sSeiCllParamsIp.e_sub_cmd = IVE_CMD_CTL_SET_SEI_CLL_PARAMS;
753     sSeiCllParamsIp.u1_sei_cll_params_present_flag = mSeiCllFlag;
754     if (mSeiCllFlag) {
755         sSeiCllParamsIp.u2_max_content_light_level = 0;
756         sSeiCllParamsIp.u2_max_pic_average_light_level = 0;
757     }
758 
759     sSeiCllParamsIp.u4_timestamp_high = -1;
760     sSeiCllParamsIp.u4_timestamp_low = -1;
761 
762     sSeiCllParamsIp.u4_size = sizeof(ih264e_ctl_set_sei_cll_params_ip_t);
763     sSeiCllParamsOp.u4_size = sizeof(ih264e_ctl_set_sei_cll_params_op_t);
764 
765     ih264e_api_function(mCodecCtx, &sSeiCllParamsIp, &sSeiCllParamsOp);
766     return;
767 }
768 
setSeiAveParams()769 void Codec::setSeiAveParams() {
770     ih264e_ctl_set_sei_ave_params_ip_t sSeiAveParamsIp{};
771     ih264e_ctl_set_sei_ave_params_op_t sSeiAveParamsOp{};
772 
773     sSeiAveParamsIp.e_cmd = IVE_CMD_VIDEO_CTL;
774     sSeiAveParamsIp.e_sub_cmd = IVE_CMD_CTL_SET_SEI_AVE_PARAMS;
775     sSeiAveParamsIp.u1_sei_ave_params_present_flag = mSeiAveFlag;
776     if (mSeiAveFlag) {
777         sSeiAveParamsIp.u4_ambient_illuminance = 1;
778         sSeiAveParamsIp.u2_ambient_light_x = 0;
779         sSeiAveParamsIp.u2_ambient_light_y = 0;
780     }
781 
782     sSeiAveParamsIp.u4_timestamp_high = -1;
783     sSeiAveParamsIp.u4_timestamp_low = -1;
784 
785     sSeiAveParamsIp.u4_size = sizeof(ih264e_ctl_set_sei_ave_params_ip_t);
786     sSeiAveParamsOp.u4_size = sizeof(ih264e_ctl_set_sei_ave_params_op_t);
787 
788     ih264e_api_function(mCodecCtx, &sSeiAveParamsIp, &sSeiAveParamsOp);
789     return;
790 }
791 
setSeiCcvParams()792 void Codec::setSeiCcvParams() {
793     ih264e_ctl_set_sei_ccv_params_ip_t sSeiCcvParamsIp{};
794     ih264e_ctl_set_sei_ccv_params_op_t sSeiCcvParamsOp{};
795 
796     sSeiCcvParamsIp.e_cmd = IVE_CMD_VIDEO_CTL;
797     sSeiCcvParamsIp.e_sub_cmd = IVE_CMD_CTL_SET_SEI_CCV_PARAMS;
798     sSeiCcvParamsIp.u1_sei_ccv_params_present_flag = mSeiCcvFlag;
799     if (mSeiCcvFlag) {
800         sSeiCcvParamsIp.u1_ccv_cancel_flag = 0;
801         sSeiCcvParamsIp.u1_ccv_persistence_flag = 1;
802         sSeiCcvParamsIp.u1_ccv_primaries_present_flag = 1;
803         sSeiCcvParamsIp.u1_ccv_min_luminance_value_present_flag = 1;
804         sSeiCcvParamsIp.u1_ccv_max_luminance_value_present_flag = 1;
805         sSeiCcvParamsIp.u1_ccv_avg_luminance_value_present_flag = 1;
806         sSeiCcvParamsIp.u1_ccv_reserved_zero_2bits = 0;
807         for (int i4_count = 0; i4_count < NUM_SEI_CCV_PRIMARIES; ++i4_count) {
808             sSeiCcvParamsIp.ai4_ccv_primaries_x[i4_count] = 1;
809             sSeiCcvParamsIp.ai4_ccv_primaries_y[i4_count] = 1;
810         }
811         sSeiCcvParamsIp.u4_ccv_min_luminance_value = 1;
812         sSeiCcvParamsIp.u4_ccv_max_luminance_value = 1;
813         sSeiCcvParamsIp.u4_ccv_avg_luminance_value = 1;
814     }
815 
816     sSeiCcvParamsIp.u4_timestamp_high = -1;
817     sSeiCcvParamsIp.u4_timestamp_low = -1;
818 
819     sSeiCcvParamsIp.u4_size = sizeof(ih264e_ctl_set_sei_ccv_params_ip_t);
820     sSeiCcvParamsOp.u4_size = sizeof(ih264e_ctl_set_sei_ccv_params_op_t);
821 
822     ih264e_api_function(mCodecCtx, &sSeiCcvParamsIp, &sSeiCcvParamsOp);
823     return;
824 }
825 
logVersion()826 void Codec::logVersion() {
827     ive_ctl_getversioninfo_ip_t sCtlIp{};
828     ive_ctl_getversioninfo_op_t sCtlOp{};
829     UWORD8 au1Buf[512];
830 
831     sCtlIp.e_cmd = IVE_CMD_VIDEO_CTL;
832     sCtlIp.e_sub_cmd = IVE_CMD_CTL_GETVERSION;
833 
834     sCtlIp.u4_size = sizeof(ive_ctl_getversioninfo_ip_t);
835     sCtlOp.u4_size = sizeof(ive_ctl_getversioninfo_op_t);
836     sCtlIp.pu1_version = au1Buf;
837     sCtlIp.u4_version_bufsize = sizeof(au1Buf);
838 
839     ive_api_function(mCodecCtx, (void *)&sCtlIp, (void *)&sCtlOp);
840     return;
841 }
842 
encodeFrames(const uint8_t * data,size_t size)843 void Codec::encodeFrames(const uint8_t *data, size_t size) {
844     size_t frameSize = (mIvVideoColorFormat == IV_YUV_422ILE) ? (mWidth * mHeight * 2)
845                                                               : ((mWidth * mHeight * 3) / 2);
846     ive_video_encode_ip_t sEncodeIp{};
847     ive_video_encode_op_t sEncodeOp{};
848     uint8_t header[kHeaderLength];
849     int32_t numEncodeCalls = 0;
850     iv_raw_buf_t *psInpRawBuf = &sEncodeIp.s_inp_buf;
851     sEncodeIp.s_out_buf.pv_buf = header;
852     sEncodeIp.s_out_buf.u4_bytes = 0;
853     sEncodeIp.s_out_buf.u4_bufsize = kHeaderLength;
854     sEncodeIp.u4_size = sizeof(ive_video_encode_ip_t);
855     sEncodeOp.u4_size = sizeof(ive_video_encode_op_t);
856 
857     sEncodeIp.e_cmd = IVE_CMD_VIDEO_ENCODE;
858     sEncodeIp.pv_bufs = nullptr;
859     sEncodeIp.pv_mb_info = nullptr;
860     sEncodeIp.pv_pic_info = nullptr;
861     sEncodeIp.u4_mb_info_type = 0;
862     sEncodeIp.u4_pic_info_type = 0;
863     sEncodeOp.s_out_buf.pv_buf = nullptr;
864 
865     /* Initialize color formats */
866     memset(psInpRawBuf, 0, sizeof(iv_raw_buf_t));
867     psInpRawBuf->u4_size = sizeof(iv_raw_buf_t);
868     psInpRawBuf->e_color_fmt = mIvVideoColorFormat;
869 
870     ive_api_function(mCodecCtx, &sEncodeIp, &sEncodeOp);
871     size_t numFrame = 0;
872     std::vector<bufferPtrs> inBuffers;
873     uint64_t outputBufferSize = (frameSize / kCompressionRatio);
874     while (!sEncodeOp.u4_is_last && numEncodeCalls < kMaxNumEncodeCalls) {
875         uint8_t *outputBuffer = (uint8_t *)malloc(outputBufferSize);
876         sEncodeIp.s_out_buf.pv_buf = outputBuffer;
877         sEncodeIp.s_out_buf.u4_bufsize = outputBufferSize;
878         if (size > 0) {
879             uint8_t *tmpData = (uint8_t *)malloc(frameSize);
880             size_t bytesConsumed = std::min(size, frameSize);
881             if (bytesConsumed < frameSize) {
882                 memset(&tmpData[bytesConsumed], data[0], frameSize - bytesConsumed);
883             }
884             memcpy(tmpData, data, bytesConsumed);
885             bufferPtrs inBuffer = setEncParams(psInpRawBuf, tmpData, frameSize);
886             inBuffers.push_back(inBuffer);
887             free(tmpData);
888             sEncodeIp.u4_is_last = 0;
889             if (mSendEosWithLastFrame && size == bytesConsumed) {
890                 sEncodeIp.u4_is_last = 1;
891             }
892             if (mIsForceIdrEnabled) {
893                 if (numFrame == mForceIdrInterval) {
894                     setFrameType(IV_IDR_FRAME);
895                 }
896             }
897             if (mIsDynamicBitRateChangeEnabled) {
898                 if (numFrame == mDynamicBitRateInterval) {
899                     if (data[0] & 0x01) {
900                         mBitrate *= 2;
901                     } else {
902                         mBitrate /= 2;
903                     }
904                     setBitRate();
905                 }
906             }
907             if (mIsDynamicFrameRateChangeEnabled) {
908                 if (numFrame == mDynamicFrameRateInterval) {
909                     if (size > 1 && data[1] & 0x01) {
910                         mFrameRate *= 2;
911                     } else {
912                         mFrameRate /= 2;
913                     }
914                     setFrameRate();
915                 }
916             }
917             ++numFrame;
918             data += bytesConsumed;
919             size -= bytesConsumed;
920         } else {
921             sEncodeIp.u4_is_last = 1;
922             psInpRawBuf->apv_bufs[0] = nullptr;
923             psInpRawBuf->apv_bufs[1] = nullptr;
924             psInpRawBuf->apv_bufs[2] = nullptr;
925         }
926         ive_api_function(mCodecCtx, &sEncodeIp, &sEncodeOp);
927         if (sEncodeOp.s_inp_buf.apv_bufs[0]) {
928             std::vector<bufferPtrs>::iterator iter;
929             uint8_t *inputbuf = (uint8_t *)sEncodeOp.s_inp_buf.apv_bufs[0];
930             iter = std::find_if(
931                 inBuffers.begin(), inBuffers.end(),
932                 [=, &inputbuf](const bufferPtrs &buf) { return std::get<0>(buf) == inputbuf; });
933             if (iter != inBuffers.end()) {
934                 inBuffers.erase(iter);
935                 free(sEncodeOp.s_inp_buf.apv_bufs[0]);
936                 if (sEncodeOp.s_inp_buf.apv_bufs[1]) {
937                     free(sEncodeOp.s_inp_buf.apv_bufs[1]);
938                 }
939                 if (sEncodeOp.s_inp_buf.apv_bufs[2]) {
940                     free(sEncodeOp.s_inp_buf.apv_bufs[2]);
941                 }
942             }
943         }
944         ++numEncodeCalls;
945         free(outputBuffer);
946     }
947     for (const auto &buffer : inBuffers) {
948         free(std::get<0>(buffer));
949         if (std::get<1>(buffer)) {
950             free(std::get<1>(buffer));
951         }
952         if (std::get<2>(buffer)) {
953             free(std::get<2>(buffer));
954         }
955     }
956     inBuffers.clear();
957 }
958 
setEncParams(iv_raw_buf_t * psInpRawBuf,const uint8_t * data,size_t frameSize)959 bufferPtrs Codec::setEncParams(iv_raw_buf_t *psInpRawBuf, const uint8_t *data, size_t frameSize) {
960     bufferPtrs inBuffer;
961     switch (mIvVideoColorFormat) {
962         case IV_YUV_420SP_UV:
963             [[fallthrough]];
964         case IV_YUV_420SP_VU: {
965             uint8_t *yPlane = (uint8_t *)malloc(mWidth * mHeight);
966             uint8_t *uPlane = (uint8_t *)malloc(frameSize - (mWidth * mHeight));
967             memcpy(yPlane, data, mWidth * mHeight);
968             memcpy(uPlane, data + (mWidth * mHeight), frameSize - (mWidth * mHeight));
969             int32_t yStride = mWidth;
970             int32_t uStride = mWidth / 2;
971             psInpRawBuf->apv_bufs[0] = yPlane;
972             psInpRawBuf->apv_bufs[1] = uPlane;
973 
974             psInpRawBuf->au4_wd[0] = mWidth;
975             psInpRawBuf->au4_wd[1] = mWidth;
976 
977             psInpRawBuf->au4_ht[0] = mHeight;
978             psInpRawBuf->au4_ht[1] = mHeight / 2;
979 
980             psInpRawBuf->au4_strd[0] = yStride;
981             psInpRawBuf->au4_strd[1] = uStride;
982             inBuffer = std::make_tuple(yPlane, uPlane, nullptr);
983             break;
984         }
985         case IV_YUV_422ILE: {
986             uint8_t *yPlane = (uint8_t *)malloc(frameSize);
987             memcpy(yPlane, data, frameSize);
988             psInpRawBuf->apv_bufs[0] = yPlane;
989 
990             psInpRawBuf->au4_wd[0] = mWidth * 2;
991 
992             psInpRawBuf->au4_ht[0] = mHeight;
993 
994             psInpRawBuf->au4_strd[0] = mWidth * 2;
995             inBuffer = std::make_tuple(yPlane, nullptr, nullptr);
996             break;
997         }
998         case IV_YUV_420P:
999             [[fallthrough]];
1000         default: {
1001             uint8_t *yPlane = (uint8_t *)malloc(mWidth * mHeight);
1002             uint8_t *uPlane = (uint8_t *)malloc((mWidth * mHeight) / 4);
1003             uint8_t *vPlane = (uint8_t *)malloc(frameSize - ((mWidth * mHeight) * 5) / 4);
1004             memcpy(yPlane, data, mWidth * mHeight);
1005             memcpy(uPlane, data + (mWidth * mHeight), (mWidth * mHeight) / 4);
1006             memcpy(vPlane, data + ((mWidth * mHeight) * 5) / 4,
1007                    frameSize - ((mWidth * mHeight) * 5) / 4);
1008             int32_t yStride = mWidth;
1009             int32_t uStride = mWidth / 2;
1010             int32_t vStride = mWidth / 2;
1011 
1012             psInpRawBuf->apv_bufs[0] = yPlane;
1013             psInpRawBuf->apv_bufs[1] = uPlane;
1014             psInpRawBuf->apv_bufs[2] = vPlane;
1015 
1016             psInpRawBuf->au4_wd[0] = mWidth;
1017             psInpRawBuf->au4_wd[1] = mWidth / 2;
1018             psInpRawBuf->au4_wd[2] = mWidth / 2;
1019 
1020             psInpRawBuf->au4_ht[0] = mHeight;
1021             psInpRawBuf->au4_ht[1] = mHeight / 2;
1022             psInpRawBuf->au4_ht[2] = mHeight / 2;
1023 
1024             psInpRawBuf->au4_strd[0] = yStride;
1025             psInpRawBuf->au4_strd[1] = uStride;
1026             psInpRawBuf->au4_strd[2] = vStride;
1027             inBuffer = std::make_tuple(yPlane, uPlane, vPlane);
1028             break;
1029         }
1030     }
1031     return inBuffer;
1032 }
1033 
deInitEncoder()1034 void Codec::deInitEncoder() {
1035     iv_mem_rec_t *ps_mem_rec = mMemRecords;
1036     for (size_t i = 0; i < mNumMemRecords; ++i) {
1037         if (ps_mem_rec) {
1038             free(ps_mem_rec->pv_base);
1039         }
1040         ++ps_mem_rec;
1041     }
1042     if (mMemRecords) {
1043         free(mMemRecords);
1044     }
1045     mCodecCtx = nullptr;
1046     return;
1047 }
1048 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)1049 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
1050     if (size < IDX_LAST) {
1051         return 0;
1052     }
1053     Codec *codec = new Codec();
1054     if (codec->initEncoder(&data, &size)) {
1055         codec->encodeFrames(data, size);
1056     }
1057     delete codec;
1058     return 0;
1059 }
1060