1 /* 2 * Copyright 2019 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_HEVC_ENC_H_ 18 #define ANDROID_C2_SOFT_HEVC_ENC_H_ 19 20 #include <SimpleC2Component.h> 21 #include <util/C2InterfaceHelper.h> 22 #include <algorithm> 23 #include <inttypes.h> 24 #include <map> 25 #include <media/stagefright/foundation/ColorUtils.h> 26 #include <utils/Vector.h> 27 28 #include "ihevc_typedefs.h" 29 30 namespace android { 31 32 #define CODEC_MAX_CORES 4 33 #define MAX_B_FRAMES 1 34 #define MAX_RC_LOOKAHEAD 1 35 36 #define DEFAULT_B_FRAMES 0 37 #define DEFAULT_RC_LOOKAHEAD 0 38 39 #define HEVC_QP_MIN 1 40 #define HEVC_QP_MAX 51 41 42 constexpr int32_t kDefaultInitQP = 32; 43 44 struct C2SoftHevcEnc : public SimpleC2Component { 45 class IntfImpl; 46 47 C2SoftHevcEnc(const char* name, c2_node_id_t id, 48 const std::shared_ptr<IntfImpl>& intfImpl); 49 C2SoftHevcEnc(const char* name, c2_node_id_t id, 50 const std::shared_ptr<C2ReflectorHelper>& helper); 51 52 // From SimpleC2Component 53 c2_status_t onInit() override; 54 c2_status_t onStop() override; 55 void onReset() override; 56 void onRelease() override; 57 c2_status_t onFlush_sm() override; 58 void process(const std::unique_ptr<C2Work>& work, 59 const std::shared_ptr<C2BlockPool>& pool) override; 60 c2_status_t drain(uint32_t drainMode, 61 const std::shared_ptr<C2BlockPool>& pool) override; 62 63 protected: 64 ~C2SoftHevcEnc() override; 65 66 private: 67 std::shared_ptr<IntfImpl> mIntf; 68 ihevce_static_cfg_params_t mEncParams; 69 size_t mNumCores; 70 UWORD32 mIDRInterval; 71 UWORD32 mIInterval; 72 UWORD32 mBframes; 73 IV_COLOR_FORMAT_T mIvVideoColorFormat; 74 UWORD32 mHevcEncProfile; 75 UWORD32 mHevcEncLevel; 76 bool mStarted; 77 bool mSpsPpsHeaderReceived; 78 bool mSignalledEos; 79 bool mSignalledError; 80 void* mCodecCtx; 81 MemoryBlockPool mConversionBuffers; 82 std::map<void*, MemoryBlock> mConversionBuffersInUse; 83 // configurations used by component in process 84 // (TODO: keep this in intf but make them internal only) 85 std::shared_ptr<C2StreamPictureSizeInfo::input> mSize; 86 std::shared_ptr<C2StreamFrameRateInfo::output> mFrameRate; 87 std::shared_ptr<C2StreamBitrateInfo::output> mBitrate; 88 std::shared_ptr<C2StreamBitrateModeTuning::output> mBitrateMode; 89 std::shared_ptr<C2StreamComplexityTuning::output> mComplexity; 90 std::shared_ptr<C2StreamQualityTuning::output> mQuality; 91 std::shared_ptr<C2StreamGopTuning::output> mGop; 92 std::shared_ptr<C2StreamRequestSyncFrameTuning::output> mRequestSync; 93 std::shared_ptr<C2StreamColorAspectsInfo::output> mColorAspects; 94 std::shared_ptr<C2StreamPictureQuantizationTuning::output> mQpBounds; 95 #ifdef FILE_DUMP_ENABLE 96 char mInFile[200]; 97 char mOutFile[200]; 98 #endif /* FILE_DUMP_ENABLE */ 99 100 // profile 101 nsecs_t mTimeStart = 0; 102 nsecs_t mTimeEnd = 0; 103 104 c2_status_t initEncParams(); 105 c2_status_t initEncoder(); 106 c2_status_t releaseEncoder(); 107 c2_status_t setEncodeArgs(ihevce_inp_buf_t* ps_encode_ip, 108 const C2GraphicView* const input, 109 uint64_t workIndex); 110 void finishWork(uint64_t index, const std::unique_ptr<C2Work>& work, 111 const std::shared_ptr<C2BlockPool>& pool, 112 ihevce_out_buf_t* ps_encode_op); 113 c2_status_t drainInternal(uint32_t drainMode, 114 const std::shared_ptr<C2BlockPool>& pool, 115 const std::unique_ptr<C2Work>& work); 116 C2_DO_NOT_COPY(C2SoftHevcEnc); 117 }; 118 #ifdef FILE_DUMP_ENABLE 119 120 #define INPUT_DUMP_PATH "/data/local/tmp/hevc" 121 #define INPUT_DUMP_EXT "yuv" 122 #define OUTPUT_DUMP_PATH "/data/local/tmp/hevc" 123 #define OUTPUT_DUMP_EXT "h265" 124 #define GENERATE_FILE_NAMES() \ 125 { \ 126 nsecs_t now = systemTime(); \ 127 ALOGD("GENERATE_FILE_NAMES"); \ 128 sprintf(mInFile, "%s_%" PRId64 ".%s", INPUT_DUMP_PATH, \ 129 mTimeStart, INPUT_DUMP_EXT); \ 130 sprintf(mutFile, "%s_%" PRId64 ".%s", OUTPUT_DUMP_PATH, \ 131 mTimeStart, OUTPUT_DUMP_EXT); \ 132 } 133 134 #define CREATE_DUMP_FILE(m_filename) \ 135 { \ 136 FILE* fp = fopen(m_filename, "wb"); \ 137 if (fp != NULL) { \ 138 ALOGD("Opened file %s", m_filename); \ 139 fclose(fp); \ 140 } else { \ 141 ALOGD("Could not open file %s", m_filename); \ 142 } \ 143 } 144 #define DUMP_TO_FILE(m_filename, m_buf, m_size) \ 145 { \ 146 FILE* fp = fopen(m_filename, "ab"); \ 147 if (fp != NULL && m_buf != NULL) { \ 148 int i; \ 149 ALOGD("Dump to file!"); \ 150 i = fwrite(m_buf, 1, m_size, fp); \ 151 if (i != (int)m_size) { \ 152 ALOGD("Error in fwrite, returned %d", i); \ 153 perror("Error in write to file"); \ 154 } \ 155 fclose(fp); \ 156 } else { \ 157 ALOGD("Could not write to file %s", m_filename); \ 158 if (fp != NULL) fclose(fp); \ 159 } \ 160 } 161 #else /* FILE_DUMP_ENABLE */ 162 #define INPUT_DUMP_PATH 163 #define INPUT_DUMP_EXT 164 #define OUTPUT_DUMP_PATH 165 #define OUTPUT_DUMP_EXT 166 #define GENERATE_FILE_NAMES() 167 #define CREATE_DUMP_FILE(m_filename) 168 #define DUMP_TO_FILE(m_filename, m_buf, m_size) 169 #endif /* FILE_DUMP_ENABLE */ 170 171 } // namespace android 172 173 #endif // C2_SOFT_HEVC_ENC_H__ 174