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 <algorithm> 22 #include <map> 23 #include <media/stagefright/foundation/ColorUtils.h> 24 #include <utils/Vector.h> 25 26 #include "ihevc_typedefs.h" 27 28 namespace android { 29 30 /** Get time */ 31 #define GETTIME(a, b) gettimeofday(a, b) 32 33 /** Compute difference between start and end */ 34 #define TIME_DIFF(start, end, diff) \ 35 diff = (((end).tv_sec - (start).tv_sec) * 1000000) + \ 36 ((end).tv_usec - (start).tv_usec); 37 38 #define CODEC_MAX_CORES 4 39 #define MAX_B_FRAMES 1 40 #define MAX_RC_LOOKAHEAD 1 41 42 #define DEFAULT_B_FRAMES 0 43 #define DEFAULT_RC_LOOKAHEAD 0 44 45 struct C2SoftHevcEnc : public SimpleC2Component { 46 class IntfImpl; 47 48 C2SoftHevcEnc(const char* name, c2_node_id_t id, 49 const std::shared_ptr<IntfImpl>& intfImpl); 50 51 // From SimpleC2Component 52 c2_status_t onInit() override; 53 c2_status_t onStop() override; 54 void onReset() override; 55 void onRelease() override; 56 c2_status_t onFlush_sm() override; 57 void process(const std::unique_ptr<C2Work>& work, 58 const std::shared_ptr<C2BlockPool>& pool) override; 59 c2_status_t drain(uint32_t drainMode, 60 const std::shared_ptr<C2BlockPool>& pool) override; 61 62 protected: 63 ~C2SoftHevcEnc() override; 64 65 private: 66 std::shared_ptr<IntfImpl> mIntf; 67 ihevce_static_cfg_params_t mEncParams; 68 size_t mNumCores; 69 UWORD32 mIDRInterval; 70 UWORD32 mIInterval; 71 UWORD32 mBframes; 72 IV_COLOR_FORMAT_T mIvVideoColorFormat; 73 UWORD32 mHevcEncProfile; 74 UWORD32 mHevcEncLevel; 75 bool mStarted; 76 bool mSpsPpsHeaderReceived; 77 bool mSignalledEos; 78 bool mSignalledError; 79 void* mCodecCtx; 80 MemoryBlockPool mConversionBuffers; 81 std::map<void*, MemoryBlock> mConversionBuffersInUse; 82 // configurations used by component in process 83 // (TODO: keep this in intf but make them internal only) 84 std::shared_ptr<C2StreamPictureSizeInfo::input> mSize; 85 std::shared_ptr<C2StreamFrameRateInfo::output> mFrameRate; 86 std::shared_ptr<C2StreamBitrateInfo::output> mBitrate; 87 std::shared_ptr<C2StreamBitrateModeTuning::output> mBitrateMode; 88 std::shared_ptr<C2StreamComplexityTuning::output> mComplexity; 89 std::shared_ptr<C2StreamQualityTuning::output> mQuality; 90 std::shared_ptr<C2StreamGopTuning::output> mGop; 91 std::shared_ptr<C2StreamRequestSyncFrameTuning::output> mRequestSync; 92 std::shared_ptr<C2StreamColorAspectsInfo::output> mColorAspects; 93 #ifdef FILE_DUMP_ENABLE 94 char mInFile[200]; 95 char mOutFile[200]; 96 #endif /* FILE_DUMP_ENABLE */ 97 98 // profile 99 struct timeval mTimeStart; 100 struct timeval mTimeEnd; 101 102 c2_status_t initEncParams(); 103 c2_status_t initEncoder(); 104 c2_status_t releaseEncoder(); 105 c2_status_t setEncodeArgs(ihevce_inp_buf_t* ps_encode_ip, 106 const C2GraphicView* const input, 107 uint64_t workIndex); 108 void finishWork(uint64_t index, const std::unique_ptr<C2Work>& work, 109 const std::shared_ptr<C2BlockPool>& pool, 110 ihevce_out_buf_t* ps_encode_op); 111 c2_status_t drainInternal(uint32_t drainMode, 112 const std::shared_ptr<C2BlockPool>& pool, 113 const std::unique_ptr<C2Work>& work); 114 C2_DO_NOT_COPY(C2SoftHevcEnc); 115 }; 116 #ifdef FILE_DUMP_ENABLE 117 118 #define INPUT_DUMP_PATH "/data/local/tmp/hevc" 119 #define INPUT_DUMP_EXT "yuv" 120 #define OUTPUT_DUMP_PATH "/data/local/tmp/hevc" 121 #define OUTPUT_DUMP_EXT "h265" 122 #define GENERATE_FILE_NAMES() \ 123 { \ 124 GETTIME(&mTimeStart, NULL); \ 125 strcpy(mInFile, ""); \ 126 ALOGD("GENERATE_FILE_NAMES"); \ 127 sprintf(mInFile, "%s_%ld.%ld.%s", INPUT_DUMP_PATH, mTimeStart.tv_sec, \ 128 mTimeStart.tv_usec, INPUT_DUMP_EXT); \ 129 strcpy(mOutFile, ""); \ 130 sprintf(mOutFile, "%s_%ld.%ld.%s", OUTPUT_DUMP_PATH, \ 131 mTimeStart.tv_sec, mTimeStart.tv_usec, 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