blob: 293867d00963f9b9ede660cbd5f7fa1e099d33e4 [file] [log] [blame]
Pawin Vongmasa36653902018-11-15 00:10:25 -08001/*
2 * Copyright 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_C2_SOFT_AVC_ENC_H__
18#define ANDROID_C2_SOFT_AVC_ENC_H__
19
20#include <map>
Ray Essick24754942022-04-16 09:50:35 -070021#include <inttypes.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080022
23#include <utils/Vector.h>
24
25#include <SimpleC2Component.h>
26
27#include "ih264_typedefs.h"
Manisha Jajoo93960e52020-12-21 21:00:11 +053028#include "ih264e.h"
Pawin Vongmasa36653902018-11-15 00:10:25 -080029
30namespace android {
31
32#define CODEC_MAX_CORES 4
33#define LEN_STATUS_BUFFER (10 * 1024)
34#define MAX_VBV_BUFF_SIZE (120 * 16384)
35#define MAX_NUM_IO_BUFS 3
Manisha Jajoo26b946d2019-03-15 18:15:11 +053036#define MAX_B_FRAMES 1
Pawin Vongmasa36653902018-11-15 00:10:25 -080037
38#define DEFAULT_MAX_REF_FRM 2
39#define DEFAULT_MAX_REORDER_FRM 0
40#define DEFAULT_QP_MIN 10
41#define DEFAULT_QP_MAX 40
Harish Mahendrakar2915cb32018-09-18 17:59:26 -070042#define DEFAULT_MAX_BITRATE 240000000
Pawin Vongmasa36653902018-11-15 00:10:25 -080043#define DEFAULT_MAX_SRCH_RANGE_X 256
44#define DEFAULT_MAX_SRCH_RANGE_Y 256
45#define DEFAULT_MAX_FRAMERATE 120000
46#define DEFAULT_NUM_CORES 1
47#define DEFAULT_NUM_CORES_PRE_ENC 0
48#define DEFAULT_FPS 30
49#define DEFAULT_ENC_SPEED IVE_NORMAL
50
51#define DEFAULT_MEM_REC_CNT 0
52#define DEFAULT_RECON_ENABLE 0
53#define DEFAULT_CHKSUM_ENABLE 0
54#define DEFAULT_START_FRM 0
55#define DEFAULT_NUM_FRMS 0xFFFFFFFF
56#define DEFAULT_INP_COLOR_FORMAT IV_YUV_420SP_VU
57#define DEFAULT_RECON_COLOR_FORMAT IV_YUV_420P
58#define DEFAULT_LOOPBACK 0
59#define DEFAULT_SRC_FRAME_RATE 30
60#define DEFAULT_TGT_FRAME_RATE 30
61#define DEFAULT_MAX_WD 1920
62#define DEFAULT_MAX_HT 1920
63#define DEFAULT_MAX_LEVEL 41
64#define DEFAULT_STRIDE 0
65#define DEFAULT_WD 1280
66#define DEFAULT_HT 720
67#define DEFAULT_PSNR_ENABLE 0
68#define DEFAULT_ME_SPEED 100
69#define DEFAULT_ENABLE_FAST_SAD 0
70#define DEFAULT_ENABLE_ALT_REF 0
71#define DEFAULT_RC_MODE IVE_RC_STORAGE
72#define DEFAULT_BITRATE 6000000
73#define DEFAULT_I_QP 22
74#define DEFAULT_I_QP_MAX DEFAULT_QP_MAX
75#define DEFAULT_I_QP_MIN DEFAULT_QP_MIN
76#define DEFAULT_P_QP 28
77#define DEFAULT_P_QP_MAX DEFAULT_QP_MAX
78#define DEFAULT_P_QP_MIN DEFAULT_QP_MIN
79#define DEFAULT_B_QP 22
80#define DEFAULT_B_QP_MAX DEFAULT_QP_MAX
81#define DEFAULT_B_QP_MIN DEFAULT_QP_MIN
82#define DEFAULT_AIR IVE_AIR_MODE_NONE
83#define DEFAULT_AIR_REFRESH_PERIOD 30
84#define DEFAULT_SRCH_RNG_X 64
85#define DEFAULT_SRCH_RNG_Y 48
86#define DEFAULT_I_INTERVAL 30
87#define DEFAULT_IDR_INTERVAL 1000
88#define DEFAULT_B_FRAMES 0
89#define DEFAULT_DISABLE_DEBLK_LEVEL 0
90#define DEFAULT_HPEL 1
91#define DEFAULT_QPEL 1
92#define DEFAULT_I4 1
93#define DEFAULT_EPROFILE IV_PROFILE_BASE
94#define DEFAULT_ENTROPY_MODE 0
95#define DEFAULT_SLICE_MODE IVE_SLICE_MODE_NONE
96#define DEFAULT_SLICE_PARAM 256
97#define DEFAULT_ARCH ARCH_ARM_A9Q
98#define DEFAULT_SOC SOC_GENERIC
99#define DEFAULT_INTRA4x4 0
100#define STRLENGTH 500
101#define DEFAULT_CONSTRAINED_INTRA 0
102
Ray Essickb2289942021-06-23 08:50:57 -0700103/** limits as specified by h264
104 * (QP_MIN==4 is actually a limitation of this SW codec, not the H.264 standard)
105 **/
106#define CODEC_QP_MIN 4
Ray Essickf78afc32021-03-10 19:42:49 -0800107#define CODEC_QP_MAX 51
108
109
Pawin Vongmasa36653902018-11-15 00:10:25 -0800110#define MIN(a, b) ((a) < (b))? (a) : (b)
111#define MAX(a, b) ((a) > (b))? (a) : (b)
112#define ALIGN16(x) ((((x) + 15) >> 4) << 4)
113#define ALIGN128(x) ((((x) + 127) >> 7) << 7)
114#define ALIGN4096(x) ((((x) + 4095) >> 12) << 12)
115
116/** Used to remove warnings about unused parameters */
117#define UNUSED(x) ((void)(x))
118
Pawin Vongmasa36653902018-11-15 00:10:25 -0800119#define ive_aligned_malloc(alignment, size) memalign(alignment, size)
120#define ive_aligned_free(buf) free(buf)
121
122struct C2SoftAvcEnc : public SimpleC2Component {
123 class IntfImpl;
124
125 C2SoftAvcEnc(const char *name, c2_node_id_t id, const std::shared_ptr<IntfImpl> &intfImpl);
126
127 // From SimpleC2Component
128 c2_status_t onInit() override;
129 c2_status_t onStop() override;
130 void onReset() override;
131 void onRelease() override;
132 c2_status_t onFlush_sm() override;
133 void process(
134 const std::unique_ptr<C2Work> &work,
135 const std::shared_ptr<C2BlockPool> &pool) override;
136 c2_status_t drain(
137 uint32_t drainMode,
138 const std::shared_ptr<C2BlockPool> &pool) override;
139
140protected:
141 virtual ~C2SoftAvcEnc();
142
143private:
Ray Essick24754942022-04-16 09:50:35 -0700144 // RBE What does OMX have to do with the c2 plugin?
Pawin Vongmasa36653902018-11-15 00:10:25 -0800145 // OMX input buffer's timestamp and flags
146 typedef struct {
147 int64_t mTimeUs;
148 int32_t mFlags;
149 } InputBufferInfo;
150
151 std::shared_ptr<IntfImpl> mIntf;
152
153 int32_t mStride;
154
Ray Essick24754942022-04-16 09:50:35 -0700155 nsecs_t mTimeStart = 0; // Time at the start of decode()
156 nsecs_t mTimeEnd = 0; // Time at the end of decode()
Pawin Vongmasa36653902018-11-15 00:10:25 -0800157
158#ifdef FILE_DUMP_ENABLE
159 char mInFile[200];
160 char mOutFile[200];
161#endif /* FILE_DUMP_ENABLE */
162
163 IV_COLOR_FORMAT_T mIvVideoColorFormat;
164
165 IV_PROFILE_T mAVCEncProfile __unused;
166 WORD32 mAVCEncLevel;
167 bool mStarted;
168 bool mSpsPpsHeaderReceived;
169
170 bool mSawInputEOS;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800171 bool mSignalledError;
172 bool mIntra4x4;
173 bool mEnableFastSad;
174 bool mEnableAltRef;
175 bool mReconEnable;
176 bool mPSNREnable;
177 bool mEntropyMode;
178 bool mConstrainedIntraFlag;
179 IVE_SPEED_CONFIG mEncSpeed;
180
181 iv_obj_t *mCodecCtx; // Codec context
182 iv_mem_rec_t *mMemRecords; // Memory records requested by the codec
183 size_t mNumMemRecords; // Number of memory records requested by codec
184 size_t mNumCores; // Number of cores used by the codec
185
Manisha Jajoo26b946d2019-03-15 18:15:11 +0530186 std::shared_ptr<C2LinearBlock> mOutBlock;
187
Pawin Vongmasa36653902018-11-15 00:10:25 -0800188 // configurations used by component in process
189 // (TODO: keep this in intf but make them internal only)
190 std::shared_ptr<C2StreamPictureSizeInfo::input> mSize;
191 std::shared_ptr<C2StreamIntraRefreshTuning::output> mIntraRefresh;
192 std::shared_ptr<C2StreamFrameRateInfo::output> mFrameRate;
193 std::shared_ptr<C2StreamBitrateInfo::output> mBitrate;
194 std::shared_ptr<C2StreamRequestSyncFrameTuning::output> mRequestSync;
Manisha Jajoo2569a522021-03-08 19:45:42 +0530195 std::shared_ptr<C2StreamColorAspectsInfo::output> mColorAspects;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800196
197 uint32_t mOutBufferSize;
198 UWORD32 mHeaderGenerated;
199 UWORD32 mBframes;
200 IV_ARCH_T mArch;
201 IVE_SLICE_MODE_T mSliceMode;
202 UWORD32 mSliceParam;
203 bool mHalfPelEnable;
204 UWORD32 mIInterval;
205 UWORD32 mIDRInterval;
206 UWORD32 mDisableDeblkLevel;
207 std::map<const void *, std::shared_ptr<C2Buffer>> mBuffers;
208 MemoryBlockPool mConversionBuffers;
209 std::map<const void *, MemoryBlock> mConversionBuffersInUse;
210
211 void initEncParams();
212 c2_status_t initEncoder();
213 c2_status_t releaseEncoder();
214
215 c2_status_t setFrameType(IV_PICTURE_CODING_TYPE_T e_frame_type);
216 c2_status_t setQp();
217 c2_status_t setEncMode(IVE_ENC_MODE_T e_enc_mode);
218 c2_status_t setDimensions();
219 c2_status_t setNumCores();
220 c2_status_t setFrameRate();
221 c2_status_t setIpeParams();
222 c2_status_t setBitRate();
223 c2_status_t setAirParams();
224 c2_status_t setMeParams();
225 c2_status_t setGopParams();
226 c2_status_t setProfileParams();
227 c2_status_t setDeblockParams();
228 c2_status_t setVbvParams();
Manisha Jajoo2569a522021-03-08 19:45:42 +0530229 c2_status_t setVuiParams();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800230 void logVersion();
231 c2_status_t setEncodeArgs(
232 ive_video_encode_ip_t *ps_encode_ip,
233 ive_video_encode_op_t *ps_encode_op,
234 const C2GraphicView *const input,
235 uint8_t *base,
236 uint32_t capacity,
Manisha Jajoo26b946d2019-03-15 18:15:11 +0530237 uint64_t workIndex);
238 void finishWork(uint64_t workIndex,
239 const std::unique_ptr<C2Work> &work,
240 ive_video_encode_op_t *ps_encode_op);
241 c2_status_t drainInternal(uint32_t drainMode,
242 const std::shared_ptr<C2BlockPool> &pool,
243 const std::unique_ptr<C2Work> &work);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800244
245 C2_DO_NOT_COPY(C2SoftAvcEnc);
246};
247
248#ifdef FILE_DUMP_ENABLE
249
250#define INPUT_DUMP_PATH "/sdcard/media/avce_input"
251#define INPUT_DUMP_EXT "yuv"
252#define OUTPUT_DUMP_PATH "/sdcard/media/avce_output"
253#define OUTPUT_DUMP_EXT "h264"
254
255#define GENERATE_FILE_NAMES() { \
Ray Essick24754942022-04-16 09:50:35 -0700256 nsecs_t now = systemTime(); \
Pawin Vongmasa36653902018-11-15 00:10:25 -0800257 strcpy(mInFile, ""); \
Ray Essick24754942022-04-16 09:50:35 -0700258 sprintf(mInFile, "%s_%" PRId64 "d.%s", \
259 INPUT_DUMP_PATH, now, \
Pawin Vongmasa36653902018-11-15 00:10:25 -0800260 INPUT_DUMP_EXT); \
261 strcpy(mOutFile, ""); \
Ray Essick24754942022-04-16 09:50:35 -0700262 sprintf(mOutFile, "%s_%" PRId64 "d.%s", \
263 OUTPUT_DUMP_PATH, now, \
Pawin Vongmasa36653902018-11-15 00:10:25 -0800264 OUTPUT_DUMP_EXT); \
265}
266
267#define CREATE_DUMP_FILE(m_filename) { \
268 FILE *fp = fopen(m_filename, "wb"); \
269 if (fp != NULL) { \
270 ALOGD("Opened file %s", m_filename); \
271 fclose(fp); \
272 } else { \
273 ALOGD("Could not open file %s", m_filename); \
274 } \
275}
276#define DUMP_TO_FILE(m_filename, m_buf, m_size) \
277{ \
278 FILE *fp = fopen(m_filename, "ab"); \
279 if (fp != NULL && m_buf != NULL) { \
280 int i; \
281 i = fwrite(m_buf, 1, m_size, fp); \
282 ALOGD("fwrite ret %d to write %d", i, m_size); \
283 if (i != (int)m_size) { \
284 ALOGD("Error in fwrite, returned %d", i); \
285 perror("Error in write to file"); \
286 } \
287 fclose(fp); \
288 } else { \
289 ALOGD("Could not write to file %s", m_filename);\
290 if (fp != NULL) \
291 fclose(fp); \
292 } \
293}
294#else /* FILE_DUMP_ENABLE */
295#define INPUT_DUMP_PATH
296#define INPUT_DUMP_EXT
297#define OUTPUT_DUMP_PATH
298#define OUTPUT_DUMP_EXT
299#define GENERATE_FILE_NAMES()
300#define CREATE_DUMP_FILE(m_filename)
301#define DUMP_TO_FILE(m_filename, m_buf, m_size)
302#endif /* FILE_DUMP_ENABLE */
303
304} // namespace android
305
306#endif // ANDROID_C2_SOFT_AVC_ENC_H__