1 /*
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"
27 #include "ih264e.h"
28 
29 namespace 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
35 #define MAX_B_FRAMES              1
36 
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
41 #define DEFAULT_MAX_BITRATE         240000000
42 #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 
102 /** limits as specified by h264
103  *  (QP_MIN==4 is actually a limitation of this SW codec, not the H.264 standard)
104  **/
105 #define CODEC_QP_MIN                4
106 #define CODEC_QP_MAX                51
107 
108 
109 #define MIN(a, b) ((a) < (b))? (a) : (b)
110 #define MAX(a, b) ((a) > (b))? (a) : (b)
111 #define ALIGN16(x) ((((x) + 15) >> 4) << 4)
112 #define ALIGN128(x) ((((x) + 127) >> 7) << 7)
113 #define ALIGN4096(x) ((((x) + 4095) >> 12) << 12)
114 
115 /** Used to remove warnings about unused parameters */
116 #define UNUSED(x) ((void)(x))
117 
118 /** Get time */
119 #define GETTIME(a, b) gettimeofday(a, b);
120 
121 /** Compute difference between start and end */
122 #define TIME_DIFF(start, end, diff) \
123     diff = (((end).tv_sec - (start).tv_sec) * 1000000) + \
124             ((end).tv_usec - (start).tv_usec);
125 
126 #define ive_aligned_malloc(alignment, size) memalign(alignment, size)
127 #define ive_aligned_free(buf) free(buf)
128 
129 struct C2SoftAvcEnc : public SimpleC2Component {
130     class IntfImpl;
131 
132     C2SoftAvcEnc(const char *name, c2_node_id_t id, const std::shared_ptr<IntfImpl> &intfImpl);
133 
134     // From SimpleC2Component
135     c2_status_t onInit() override;
136     c2_status_t onStop() override;
137     void onReset() override;
138     void onRelease() override;
139     c2_status_t onFlush_sm() override;
140     void process(
141             const std::unique_ptr<C2Work> &work,
142             const std::shared_ptr<C2BlockPool> &pool) override;
143     c2_status_t drain(
144             uint32_t drainMode,
145             const std::shared_ptr<C2BlockPool> &pool) override;
146 
147 protected:
148     virtual ~C2SoftAvcEnc();
149 
150 private:
151     // OMX input buffer's timestamp and flags
152     typedef struct {
153         int64_t mTimeUs;
154         int32_t mFlags;
155     } InputBufferInfo;
156 
157     std::shared_ptr<IntfImpl> mIntf;
158 
159     int32_t mStride;
160 
161     struct timeval mTimeStart;   // Time at the start of decode()
162     struct timeval mTimeEnd;     // Time at the end of decode()
163 
164 #ifdef FILE_DUMP_ENABLE
165     char mInFile[200];
166     char mOutFile[200];
167 #endif /* FILE_DUMP_ENABLE */
168 
169     IV_COLOR_FORMAT_T mIvVideoColorFormat;
170 
171     IV_PROFILE_T mAVCEncProfile __unused;
172     WORD32   mAVCEncLevel;
173     bool     mStarted;
174     bool     mSpsPpsHeaderReceived;
175 
176     bool     mSawInputEOS;
177     bool     mSignalledError;
178     bool     mIntra4x4;
179     bool     mEnableFastSad;
180     bool     mEnableAltRef;
181     bool     mReconEnable;
182     bool     mPSNREnable;
183     bool     mEntropyMode;
184     bool     mConstrainedIntraFlag;
185     IVE_SPEED_CONFIG     mEncSpeed;
186 
187     iv_obj_t *mCodecCtx;         // Codec context
188     iv_mem_rec_t *mMemRecords;   // Memory records requested by the codec
189     size_t mNumMemRecords;       // Number of memory records requested by codec
190     size_t mNumCores;            // Number of cores used by the codec
191 
192     std::shared_ptr<C2LinearBlock> mOutBlock;
193 
194     // configurations used by component in process
195     // (TODO: keep this in intf but make them internal only)
196     std::shared_ptr<C2StreamPictureSizeInfo::input> mSize;
197     std::shared_ptr<C2StreamIntraRefreshTuning::output> mIntraRefresh;
198     std::shared_ptr<C2StreamFrameRateInfo::output> mFrameRate;
199     std::shared_ptr<C2StreamBitrateInfo::output> mBitrate;
200     std::shared_ptr<C2StreamRequestSyncFrameTuning::output> mRequestSync;
201     std::shared_ptr<C2StreamColorAspectsInfo::output> mColorAspects;
202 
203     uint32_t mOutBufferSize;
204     UWORD32 mHeaderGenerated;
205     UWORD32 mBframes;
206     IV_ARCH_T mArch;
207     IVE_SLICE_MODE_T mSliceMode;
208     UWORD32 mSliceParam;
209     bool mHalfPelEnable;
210     UWORD32 mIInterval;
211     UWORD32 mIDRInterval;
212     UWORD32 mDisableDeblkLevel;
213     std::map<const void *, std::shared_ptr<C2Buffer>> mBuffers;
214     MemoryBlockPool mConversionBuffers;
215     std::map<const void *, MemoryBlock> mConversionBuffersInUse;
216 
217     void initEncParams();
218     c2_status_t initEncoder();
219     c2_status_t releaseEncoder();
220 
221     c2_status_t setFrameType(IV_PICTURE_CODING_TYPE_T  e_frame_type);
222     c2_status_t setQp();
223     c2_status_t setEncMode(IVE_ENC_MODE_T e_enc_mode);
224     c2_status_t setDimensions();
225     c2_status_t setNumCores();
226     c2_status_t setFrameRate();
227     c2_status_t setIpeParams();
228     c2_status_t setBitRate();
229     c2_status_t setAirParams();
230     c2_status_t setMeParams();
231     c2_status_t setGopParams();
232     c2_status_t setProfileParams();
233     c2_status_t setDeblockParams();
234     c2_status_t setVbvParams();
235     c2_status_t setVuiParams();
236     void logVersion();
237     c2_status_t setEncodeArgs(
238             ive_video_encode_ip_t *ps_encode_ip,
239             ive_video_encode_op_t *ps_encode_op,
240             const C2GraphicView *const input,
241             uint8_t *base,
242             uint32_t capacity,
243             uint64_t workIndex);
244     void finishWork(uint64_t workIndex,
245             const std::unique_ptr<C2Work> &work,
246             ive_video_encode_op_t *ps_encode_op);
247     c2_status_t drainInternal(uint32_t drainMode,
248             const std::shared_ptr<C2BlockPool> &pool,
249             const std::unique_ptr<C2Work> &work);
250 
251     C2_DO_NOT_COPY(C2SoftAvcEnc);
252 };
253 
254 #ifdef FILE_DUMP_ENABLE
255 
256 #define INPUT_DUMP_PATH     "/sdcard/media/avce_input"
257 #define INPUT_DUMP_EXT      "yuv"
258 #define OUTPUT_DUMP_PATH    "/sdcard/media/avce_output"
259 #define OUTPUT_DUMP_EXT     "h264"
260 
261 #define GENERATE_FILE_NAMES() {                         \
262     GETTIME(&mTimeStart, NULL);                         \
263     strcpy(mInFile, "");                                \
264     sprintf(mInFile, "%s_%ld.%ld.%s", INPUT_DUMP_PATH,  \
265             mTimeStart.tv_sec, mTimeStart.tv_usec,      \
266             INPUT_DUMP_EXT);                            \
267     strcpy(mOutFile, "");                               \
268     sprintf(mOutFile, "%s_%ld.%ld.%s", OUTPUT_DUMP_PATH,\
269             mTimeStart.tv_sec, mTimeStart.tv_usec,      \
270             OUTPUT_DUMP_EXT);                           \
271 }
272 
273 #define CREATE_DUMP_FILE(m_filename) {                  \
274     FILE *fp = fopen(m_filename, "wb");                 \
275     if (fp != NULL) {                                   \
276         ALOGD("Opened file %s", m_filename);            \
277         fclose(fp);                                     \
278     } else {                                            \
279         ALOGD("Could not open file %s", m_filename);    \
280     }                                                   \
281 }
282 #define DUMP_TO_FILE(m_filename, m_buf, m_size)         \
283 {                                                       \
284     FILE *fp = fopen(m_filename, "ab");                 \
285     if (fp != NULL && m_buf != NULL) {                  \
286         int i;                                          \
287         i = fwrite(m_buf, 1, m_size, fp);               \
288         ALOGD("fwrite ret %d to write %d", i, m_size);  \
289         if (i != (int)m_size) {                         \
290             ALOGD("Error in fwrite, returned %d", i);   \
291             perror("Error in write to file");           \
292         }                                               \
293         fclose(fp);                                     \
294     } else {                                            \
295         ALOGD("Could not write to file %s", m_filename);\
296         if (fp != NULL)                                 \
297             fclose(fp);                                 \
298     }                                                   \
299 }
300 #else /* FILE_DUMP_ENABLE */
301 #define INPUT_DUMP_PATH
302 #define INPUT_DUMP_EXT
303 #define OUTPUT_DUMP_PATH
304 #define OUTPUT_DUMP_EXT
305 #define GENERATE_FILE_NAMES()
306 #define CREATE_DUMP_FILE(m_filename)
307 #define DUMP_TO_FILE(m_filename, m_buf, m_size)
308 #endif /* FILE_DUMP_ENABLE */
309 
310 }  // namespace android
311 
312 #endif  // ANDROID_C2_SOFT_AVC_ENC_H__
313