1 /* 2 * Copyright (C) 2022 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_AV1_ENC_H_ 18 #define ANDROID_C2_SOFT_AV1_ENC_H_ 19 20 #include <inttypes.h> 21 22 #include <C2PlatformSupport.h> 23 #include <Codec2BufferUtils.h> 24 #include <SimpleC2Component.h> 25 #include <SimpleC2Interface.h> 26 #include <util/C2InterfaceHelper.h> 27 28 #include "aom/aom_encoder.h" 29 #include "aom/aomcx.h" 30 #include "common/av1_config.h" 31 32 namespace android { 33 struct C2SoftAomEnc : public SimpleC2Component { 34 class IntfImpl; 35 36 C2SoftAomEnc(const char* name, c2_node_id_t id, const std::shared_ptr<IntfImpl>& intfImpl); 37 C2SoftAomEnc(const char* name, c2_node_id_t id, 38 const std::shared_ptr<C2ReflectorHelper>& helper); 39 40 // From SimpleC2Component 41 c2_status_t onInit() override final; 42 c2_status_t onStop() override final; 43 void onReset() override final; 44 void onRelease() override final; 45 c2_status_t onFlush_sm() override final; 46 47 void process(const std::unique_ptr<C2Work>& work, 48 const std::shared_ptr<C2BlockPool>& pool) override final; 49 c2_status_t drain(uint32_t drainMode, const std::shared_ptr<C2BlockPool>& pool) override final; 50 51 protected: 52 virtual ~C2SoftAomEnc(); 53 54 private: 55 std::shared_ptr<IntfImpl> mIntf; 56 57 // Initializes aom encoder with available settings. 58 status_t initEncoder(); 59 60 // aom specific opaque data structure that 61 // stores encoder state 62 aom_codec_ctx_t* mCodecContext; 63 64 // aom specific data structure that 65 // stores encoder configuration 66 aom_codec_enc_cfg_t* mCodecConfiguration; 67 68 // aom specific read-only data structure 69 // that specifies algorithm interface 70 aom_codec_iface_t* mCodecInterface; 71 72 // align stride to the power of 2 73 int32_t mStrideAlign; 74 75 aom_rc_mode mBitrateControlMode; 76 77 // Minimum (best quality) quantizer 78 uint32_t mMinQuantizer; 79 80 // Maximum (worst quality) quantizer 81 uint32_t mMaxQuantizer; 82 83 // Last input buffer timestamp 84 uint64_t mLastTimestamp; 85 86 // Number of input frames 87 int64_t mNumInputFrames; 88 89 // Conversion buffer is needed to input to 90 // yuv420 planar format. 91 MemoryBlock mConversionBuffer; 92 93 // Signalled End Of Stream 94 bool mSignalledOutputEos; 95 96 // Signalled Error 97 bool mSignalledError; 98 99 bool mHeadersReceived; 100 101 bool mIs10Bit; 102 103 uint32_t mAV1EncLevel; 104 105 std::shared_ptr<C2StreamPictureSizeInfo::input> mSize; 106 std::shared_ptr<C2StreamIntraRefreshTuning::output> mIntraRefresh; 107 std::shared_ptr<C2StreamFrameRateInfo::output> mFrameRate; 108 std::shared_ptr<C2StreamBitrateInfo::output> mBitrate; 109 std::shared_ptr<C2StreamQualityTuning::output> mQuality; 110 std::shared_ptr<C2StreamComplexityTuning::output> mComplexity; 111 std::shared_ptr<C2StreamBitrateModeTuning::output> mBitrateMode; 112 std::shared_ptr<C2StreamRequestSyncFrameTuning::output> mRequestSync; 113 std::shared_ptr<C2StreamColorAspectsInfo::output> mColorAspects; 114 std::shared_ptr<C2StreamPictureQuantizationTuning::output> mQpBounds; 115 116 aom_codec_err_t setupCodecParameters(); 117 }; 118 119 class C2SoftAomEnc::IntfImpl : public SimpleInterface<void>::BaseParams { 120 public: 121 explicit IntfImpl(const std::shared_ptr<C2ReflectorHelper>& helper); 122 123 static C2R BitrateSetter(bool mayBlock, C2P<C2StreamBitrateInfo::output>& me); 124 125 static C2R SizeSetter(bool mayBlock, const C2P<C2StreamPictureSizeInfo::input>& oldMe, 126 C2P<C2StreamPictureSizeInfo::input>& me); 127 128 static C2R ProfileLevelSetter(bool mayBlock, C2P<C2StreamProfileLevelInfo::output>& me, 129 const C2P<C2StreamPictureSizeInfo::input>& size, 130 const C2P<C2StreamFrameRateInfo::output>& frameRate, 131 const C2P<C2StreamBitrateInfo::output>& bitrate); 132 static C2R PictureQuantizationSetter(bool mayBlock, 133 C2P<C2StreamPictureQuantizationTuning::output> &me); 134 135 // unsafe getters getSize_l()136 std::shared_ptr<C2StreamPictureSizeInfo::input> getSize_l() const { return mSize; } getIntraRefresh_l()137 std::shared_ptr<C2StreamIntraRefreshTuning::output> getIntraRefresh_l() const { 138 return mIntraRefresh; 139 } getFrameRate_l()140 std::shared_ptr<C2StreamFrameRateInfo::output> getFrameRate_l() const { return mFrameRate; } getBitrate_l()141 std::shared_ptr<C2StreamBitrateInfo::output> getBitrate_l() const { return mBitrate; } getQuality_l()142 std::shared_ptr<C2StreamQualityTuning::output> getQuality_l() const { return mQuality; } getComplexity_l()143 std::shared_ptr<C2StreamComplexityTuning::output> getComplexity_l() const { 144 return mComplexity; 145 } getBitrateMode_l()146 std::shared_ptr<C2StreamBitrateModeTuning::output> getBitrateMode_l() const { 147 return mBitrateMode; 148 } getRequestSync_l()149 std::shared_ptr<C2StreamRequestSyncFrameTuning::output> getRequestSync_l() const { 150 return mRequestSync; 151 } getCodedColorAspects_l()152 std::shared_ptr<C2StreamColorAspectsInfo::output> getCodedColorAspects_l() const { 153 return mCodedColorAspects; 154 } getPixelFormat_l()155 std::shared_ptr<C2StreamPixelFormatInfo::input> getPixelFormat_l() const { 156 return mPixelFormat; 157 } getPictureQuantization_l()158 std::shared_ptr<C2StreamPictureQuantizationTuning::output> getPictureQuantization_l() const { 159 return mPictureQuantization; 160 } 161 uint32_t getSyncFramePeriod() const; 162 static C2R ColorAspectsSetter(bool mayBlock, C2P<C2StreamColorAspectsInfo::input>& me); 163 static C2R CodedColorAspectsSetter(bool mayBlock, C2P<C2StreamColorAspectsInfo::output>& me, 164 const C2P<C2StreamColorAspectsInfo::input>& coded); 165 uint32_t getLevel_l() const; 166 167 private: 168 std::shared_ptr<C2StreamUsageTuning::input> mUsage; 169 std::shared_ptr<C2StreamPictureSizeInfo::input> mSize; 170 std::shared_ptr<C2StreamFrameRateInfo::output> mFrameRate; 171 std::shared_ptr<C2StreamIntraRefreshTuning::output> mIntraRefresh; 172 std::shared_ptr<C2StreamRequestSyncFrameTuning::output> mRequestSync; 173 std::shared_ptr<C2StreamSyncFrameIntervalTuning::output> mSyncFramePeriod; 174 std::shared_ptr<C2StreamBitrateInfo::output> mBitrate; 175 std::shared_ptr<C2StreamQualityTuning::output> mQuality; 176 std::shared_ptr<C2StreamComplexityTuning::output> mComplexity; 177 std::shared_ptr<C2StreamBitrateModeTuning::output> mBitrateMode; 178 std::shared_ptr<C2StreamProfileLevelInfo::output> mProfileLevel; 179 std::shared_ptr<C2StreamColorAspectsInfo::input> mColorAspects; 180 std::shared_ptr<C2StreamColorAspectsInfo::output> mCodedColorAspects; 181 std::shared_ptr<C2StreamPixelFormatInfo::input> mPixelFormat; 182 std::shared_ptr<C2StreamPictureQuantizationTuning::output> mPictureQuantization; 183 184 }; 185 186 } // namespace android 187 #endif // ANDROID_C2_SOFT_AV1_ENC_H_ 188