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