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