blob: 673a282fa24285c0fbf009e1e82f677089e66c2f [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>
21
22#include <utils/Vector.h>
23
24#include <SimpleC2Component.h>
25
26#include "ih264_typedefs.h"
Manisha Jajoo93960e52020-12-21 21:00:11 +053027#include "ih264e.h"
Pawin Vongmasa36653902018-11-15 00:10:25 -080028
29namespace android {
30
31#define CODEC_MAX_CORES 4
32#define LEN_STATUS_BUFFER (10 * 1024)
33#define MAX_VBV_BUFF_SIZE (120 * 16384)
34#define MAX_NUM_IO_BUFS 3
Manisha Jajoo26b946d2019-03-15 18:15:11 +053035#define MAX_B_FRAMES 1
Pawin Vongmasa36653902018-11-15 00:10:25 -080036
37#define DEFAULT_MAX_REF_FRM 2
38#define DEFAULT_MAX_REORDER_FRM 0
39#define DEFAULT_QP_MIN 10
40#define DEFAULT_QP_MAX 40
Harish Mahendrakar2915cb32018-09-18 17:59:26 -070041#define DEFAULT_MAX_BITRATE 240000000
Pawin Vongmasa36653902018-11-15 00:10:25 -080042#define DEFAULT_MAX_SRCH_RANGE_X 256
43#define DEFAULT_MAX_SRCH_RANGE_Y 256
44#define DEFAULT_MAX_FRAMERATE 120000
45#define DEFAULT_NUM_CORES 1
46#define DEFAULT_NUM_CORES_PRE_ENC 0
47#define DEFAULT_FPS 30
48#define DEFAULT_ENC_SPEED IVE_NORMAL
49
50#define DEFAULT_MEM_REC_CNT 0
51#define DEFAULT_RECON_ENABLE 0
52#define DEFAULT_CHKSUM_ENABLE 0
53#define DEFAULT_START_FRM 0
54#define DEFAULT_NUM_FRMS 0xFFFFFFFF
55#define DEFAULT_INP_COLOR_FORMAT IV_YUV_420SP_VU
56#define DEFAULT_RECON_COLOR_FORMAT IV_YUV_420P
57#define DEFAULT_LOOPBACK 0
58#define DEFAULT_SRC_FRAME_RATE 30
59#define DEFAULT_TGT_FRAME_RATE 30
60#define DEFAULT_MAX_WD 1920
61#define DEFAULT_MAX_HT 1920
62#define DEFAULT_MAX_LEVEL 41
63#define DEFAULT_STRIDE 0
64#define DEFAULT_WD 1280
65#define DEFAULT_HT 720
66#define DEFAULT_PSNR_ENABLE 0
67#define DEFAULT_ME_SPEED 100
68#define DEFAULT_ENABLE_FAST_SAD 0
69#define DEFAULT_ENABLE_ALT_REF 0
70#define DEFAULT_RC_MODE IVE_RC_STORAGE
71#define DEFAULT_BITRATE 6000000
72#define DEFAULT_I_QP 22
73#define DEFAULT_I_QP_MAX DEFAULT_QP_MAX
74#define DEFAULT_I_QP_MIN DEFAULT_QP_MIN
75#define DEFAULT_P_QP 28
76#define DEFAULT_P_QP_MAX DEFAULT_QP_MAX
77#define DEFAULT_P_QP_MIN DEFAULT_QP_MIN
78#define DEFAULT_B_QP 22
79#define DEFAULT_B_QP_MAX DEFAULT_QP_MAX
80#define DEFAULT_B_QP_MIN DEFAULT_QP_MIN
81#define DEFAULT_AIR IVE_AIR_MODE_NONE
82#define DEFAULT_AIR_REFRESH_PERIOD 30
83#define DEFAULT_SRCH_RNG_X 64
84#define DEFAULT_SRCH_RNG_Y 48
85#define DEFAULT_I_INTERVAL 30
86#define DEFAULT_IDR_INTERVAL 1000
87#define DEFAULT_B_FRAMES 0
88#define DEFAULT_DISABLE_DEBLK_LEVEL 0
89#define DEFAULT_HPEL 1
90#define DEFAULT_QPEL 1
91#define DEFAULT_I4 1
92#define DEFAULT_EPROFILE IV_PROFILE_BASE
93#define DEFAULT_ENTROPY_MODE 0
94#define DEFAULT_SLICE_MODE IVE_SLICE_MODE_NONE
95#define DEFAULT_SLICE_PARAM 256
96#define DEFAULT_ARCH ARCH_ARM_A9Q
97#define DEFAULT_SOC SOC_GENERIC
98#define DEFAULT_INTRA4x4 0
99#define STRLENGTH 500
100#define DEFAULT_CONSTRAINED_INTRA 0
101
Ray Essickf78afc32021-03-10 19:42:49 -0800102/** limits as specified by h264 */
103#define CODEC_QP_MIN 0
104#define CODEC_QP_MAX 51
105
106
Pawin Vongmasa36653902018-11-15 00:10:25 -0800107#define MIN(a, b) ((a) < (b))? (a) : (b)
108#define MAX(a, b) ((a) > (b))? (a) : (b)
109#define ALIGN16(x) ((((x) + 15) >> 4) << 4)
110#define ALIGN128(x) ((((x) + 127) >> 7) << 7)
111#define ALIGN4096(x) ((((x) + 4095) >> 12) << 12)
112
113/** Used to remove warnings about unused parameters */
114#define UNUSED(x) ((void)(x))
115
116/** Get time */
117#define GETTIME(a, b) gettimeofday(a, b);
118
119/** Compute difference between start and end */
120#define TIME_DIFF(start, end, diff) \
121 diff = (((end).tv_sec - (start).tv_sec) * 1000000) + \
122 ((end).tv_usec - (start).tv_usec);
123
124#define ive_aligned_malloc(alignment, size) memalign(alignment, size)
125#define ive_aligned_free(buf) free(buf)
126
127struct C2SoftAvcEnc : public SimpleC2Component {
128 class IntfImpl;
129
130 C2SoftAvcEnc(const char *name, c2_node_id_t id, const std::shared_ptr<IntfImpl> &intfImpl);
131
132 // From SimpleC2Component
133 c2_status_t onInit() override;
134 c2_status_t onStop() override;
135 void onReset() override;
136 void onRelease() override;
137 c2_status_t onFlush_sm() override;
138 void process(
139 const std::unique_ptr<C2Work> &work,
140 const std::shared_ptr<C2BlockPool> &pool) override;
141 c2_status_t drain(
142 uint32_t drainMode,
143 const std::shared_ptr<C2BlockPool> &pool) override;
144
145protected:
146 virtual ~C2SoftAvcEnc();
147
148private:
149 // OMX input buffer's timestamp and flags
150 typedef struct {
151 int64_t mTimeUs;
152 int32_t mFlags;
153 } InputBufferInfo;
154
155 std::shared_ptr<IntfImpl> mIntf;
156
157 int32_t mStride;
158
159 struct timeval mTimeStart; // Time at the start of decode()
160 struct timeval mTimeEnd; // Time at the end of decode()
161
162#ifdef FILE_DUMP_ENABLE
163 char mInFile[200];
164 char mOutFile[200];
165#endif /* FILE_DUMP_ENABLE */
166
167 IV_COLOR_FORMAT_T mIvVideoColorFormat;
168
169 IV_PROFILE_T mAVCEncProfile __unused;
170 WORD32 mAVCEncLevel;
171 bool mStarted;
172 bool mSpsPpsHeaderReceived;
173
174 bool mSawInputEOS;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800175 bool mSignalledError;
176 bool mIntra4x4;
177 bool mEnableFastSad;
178 bool mEnableAltRef;
179 bool mReconEnable;
180 bool mPSNREnable;
181 bool mEntropyMode;
182 bool mConstrainedIntraFlag;
183 IVE_SPEED_CONFIG mEncSpeed;
184
185 iv_obj_t *mCodecCtx; // Codec context
186 iv_mem_rec_t *mMemRecords; // Memory records requested by the codec
187 size_t mNumMemRecords; // Number of memory records requested by codec
188 size_t mNumCores; // Number of cores used by the codec
189
Manisha Jajoo26b946d2019-03-15 18:15:11 +0530190 std::shared_ptr<C2LinearBlock> mOutBlock;
191
Pawin Vongmasa36653902018-11-15 00:10:25 -0800192 // configurations used by component in process
193 // (TODO: keep this in intf but make them internal only)
194 std::shared_ptr<C2StreamPictureSizeInfo::input> mSize;
195 std::shared_ptr<C2StreamIntraRefreshTuning::output> mIntraRefresh;
196 std::shared_ptr<C2StreamFrameRateInfo::output> mFrameRate;
197 std::shared_ptr<C2StreamBitrateInfo::output> mBitrate;
198 std::shared_ptr<C2StreamRequestSyncFrameTuning::output> mRequestSync;
199
200 uint32_t mOutBufferSize;
201 UWORD32 mHeaderGenerated;
202 UWORD32 mBframes;
203 IV_ARCH_T mArch;
204 IVE_SLICE_MODE_T mSliceMode;
205 UWORD32 mSliceParam;
206 bool mHalfPelEnable;
207 UWORD32 mIInterval;
208 UWORD32 mIDRInterval;
209 UWORD32 mDisableDeblkLevel;
210 std::map<const void *, std::shared_ptr<C2Buffer>> mBuffers;
211 MemoryBlockPool mConversionBuffers;
212 std::map<const void *, MemoryBlock> mConversionBuffersInUse;
213
214 void initEncParams();
215 c2_status_t initEncoder();
216 c2_status_t releaseEncoder();
217
218 c2_status_t setFrameType(IV_PICTURE_CODING_TYPE_T e_frame_type);
219 c2_status_t setQp();
220 c2_status_t setEncMode(IVE_ENC_MODE_T e_enc_mode);
221 c2_status_t setDimensions();
222 c2_status_t setNumCores();
223 c2_status_t setFrameRate();
224 c2_status_t setIpeParams();
225 c2_status_t setBitRate();
226 c2_status_t setAirParams();
227 c2_status_t setMeParams();
228 c2_status_t setGopParams();
229 c2_status_t setProfileParams();
230 c2_status_t setDeblockParams();
231 c2_status_t setVbvParams();
232 void logVersion();
233 c2_status_t setEncodeArgs(
234 ive_video_encode_ip_t *ps_encode_ip,
235 ive_video_encode_op_t *ps_encode_op,
236 const C2GraphicView *const input,
237 uint8_t *base,
238 uint32_t capacity,
Manisha Jajoo26b946d2019-03-15 18:15:11 +0530239 uint64_t workIndex);
240 void finishWork(uint64_t workIndex,
241 const std::unique_ptr<C2Work> &work,
242 ive_video_encode_op_t *ps_encode_op);
243 c2_status_t drainInternal(uint32_t drainMode,
244 const std::shared_ptr<C2BlockPool> &pool,
245 const std::unique_ptr<C2Work> &work);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800246
247 C2_DO_NOT_COPY(C2SoftAvcEnc);
248};
249
250#ifdef FILE_DUMP_ENABLE
251
252#define INPUT_DUMP_PATH "/sdcard/media/avce_input"
253#define INPUT_DUMP_EXT "yuv"
254#define OUTPUT_DUMP_PATH "/sdcard/media/avce_output"
255#define OUTPUT_DUMP_EXT "h264"
256
257#define GENERATE_FILE_NAMES() { \
258 GETTIME(&mTimeStart, NULL); \
259 strcpy(mInFile, ""); \
260 sprintf(mInFile, "%s_%ld.%ld.%s", INPUT_DUMP_PATH, \
261 mTimeStart.tv_sec, mTimeStart.tv_usec, \
262 INPUT_DUMP_EXT); \
263 strcpy(mOutFile, ""); \
264 sprintf(mOutFile, "%s_%ld.%ld.%s", OUTPUT_DUMP_PATH,\
265 mTimeStart.tv_sec, mTimeStart.tv_usec, \
266 OUTPUT_DUMP_EXT); \
267}
268
269#define CREATE_DUMP_FILE(m_filename) { \
270 FILE *fp = fopen(m_filename, "wb"); \
271 if (fp != NULL) { \
272 ALOGD("Opened file %s", m_filename); \
273 fclose(fp); \
274 } else { \
275 ALOGD("Could not open file %s", m_filename); \
276 } \
277}
278#define DUMP_TO_FILE(m_filename, m_buf, m_size) \
279{ \
280 FILE *fp = fopen(m_filename, "ab"); \
281 if (fp != NULL && m_buf != NULL) { \
282 int i; \
283 i = fwrite(m_buf, 1, m_size, fp); \
284 ALOGD("fwrite ret %d to write %d", i, m_size); \
285 if (i != (int)m_size) { \
286 ALOGD("Error in fwrite, returned %d", i); \
287 perror("Error in write to file"); \
288 } \
289 fclose(fp); \
290 } else { \
291 ALOGD("Could not write to file %s", m_filename);\
292 if (fp != NULL) \
293 fclose(fp); \
294 } \
295}
296#else /* FILE_DUMP_ENABLE */
297#define INPUT_DUMP_PATH
298#define INPUT_DUMP_EXT
299#define OUTPUT_DUMP_PATH
300#define OUTPUT_DUMP_EXT
301#define GENERATE_FILE_NAMES()
302#define CREATE_DUMP_FILE(m_filename)
303#define DUMP_TO_FILE(m_filename, m_buf, m_size)
304#endif /* FILE_DUMP_ENABLE */
305
306} // namespace android
307
308#endif // ANDROID_C2_SOFT_AVC_ENC_H__