Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 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 | |
Fyodor Kyslov | a056c62 | 2022-09-21 21:41:04 +0000 | [diff] [blame] | 49 | protected: |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 50 | virtual ~C2SoftAomEnc(); |
| 51 | |
Fyodor Kyslov | a056c62 | 2022-09-21 21:41:04 +0000 | [diff] [blame] | 52 | private: |
| 53 | std::shared_ptr<IntfImpl> mIntf; |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 54 | |
Fyodor Kyslov | a056c62 | 2022-09-21 21:41:04 +0000 | [diff] [blame] | 55 | // Initializes aom encoder with available settings. |
| 56 | status_t initEncoder(); |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 57 | |
Fyodor Kyslov | a056c62 | 2022-09-21 21:41:04 +0000 | [diff] [blame] | 58 | // aom specific opaque data structure that |
| 59 | // stores encoder state |
| 60 | aom_codec_ctx_t* mCodecContext; |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 61 | |
Fyodor Kyslov | a056c62 | 2022-09-21 21:41:04 +0000 | [diff] [blame] | 62 | // aom specific data structure that |
| 63 | // stores encoder configuration |
| 64 | aom_codec_enc_cfg_t* mCodecConfiguration; |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 65 | |
Fyodor Kyslov | a056c62 | 2022-09-21 21:41:04 +0000 | [diff] [blame] | 66 | // aom specific read-only data structure |
| 67 | // that specifies algorithm interface |
| 68 | aom_codec_iface_t* mCodecInterface; |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 69 | |
Fyodor Kyslov | a056c62 | 2022-09-21 21:41:04 +0000 | [diff] [blame] | 70 | // align stride to the power of 2 |
| 71 | int32_t mStrideAlign; |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 72 | |
Fyodor Kyslov | a056c62 | 2022-09-21 21:41:04 +0000 | [diff] [blame] | 73 | aom_rc_mode mBitrateControlMode; |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 74 | |
Fyodor Kyslov | a056c62 | 2022-09-21 21:41:04 +0000 | [diff] [blame] | 75 | // Minimum (best quality) quantizer |
| 76 | uint32_t mMinQuantizer; |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 77 | |
Fyodor Kyslov | a056c62 | 2022-09-21 21:41:04 +0000 | [diff] [blame] | 78 | // Maximum (worst quality) quantizer |
| 79 | uint32_t mMaxQuantizer; |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 80 | |
Fyodor Kyslov | a056c62 | 2022-09-21 21:41:04 +0000 | [diff] [blame] | 81 | // Last input buffer timestamp |
| 82 | uint64_t mLastTimestamp; |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 83 | |
Fyodor Kyslov | a056c62 | 2022-09-21 21:41:04 +0000 | [diff] [blame] | 84 | // Number of input frames |
| 85 | int64_t mNumInputFrames; |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 86 | |
Fyodor Kyslov | a056c62 | 2022-09-21 21:41:04 +0000 | [diff] [blame] | 87 | // Conversion buffer is needed to input to |
| 88 | // yuv420 planar format. |
| 89 | MemoryBlock mConversionBuffer; |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 90 | |
Fyodor Kyslov | a056c62 | 2022-09-21 21:41:04 +0000 | [diff] [blame] | 91 | // Signalled End Of Stream |
| 92 | bool mSignalledOutputEos; |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 93 | |
Fyodor Kyslov | a056c62 | 2022-09-21 21:41:04 +0000 | [diff] [blame] | 94 | // Signalled Error |
| 95 | bool mSignalledError; |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 96 | |
Fyodor Kyslov | a056c62 | 2022-09-21 21:41:04 +0000 | [diff] [blame] | 97 | bool mHeadersReceived; |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 98 | |
Fyodor Kyslov | d97e991 | 2022-11-07 19:23:21 +0000 | [diff] [blame] | 99 | bool mIs10Bit; |
| 100 | |
Harish Mahendrakar | 7d482b2 | 2023-08-21 20:08:01 -0700 | [diff] [blame] | 101 | uint32_t mAV1EncLevel; |
| 102 | |
Fyodor Kyslov | a056c62 | 2022-09-21 21:41:04 +0000 | [diff] [blame] | 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; |
Fyodor Kyslov | 58d8dea | 2023-02-10 02:15:54 +0000 | [diff] [blame] | 107 | std::shared_ptr<C2StreamQualityTuning::output> mQuality; |
Fyodor Kyslov | 3e3a92b | 2023-03-15 00:39:36 +0000 | [diff] [blame] | 108 | std::shared_ptr<C2StreamComplexityTuning::output> mComplexity; |
Fyodor Kyslov | a056c62 | 2022-09-21 21:41:04 +0000 | [diff] [blame] | 109 | std::shared_ptr<C2StreamBitrateModeTuning::output> mBitrateMode; |
| 110 | std::shared_ptr<C2StreamRequestSyncFrameTuning::output> mRequestSync; |
Aayush Soni | 643b918 | 2023-01-18 20:23:56 +0530 | [diff] [blame] | 111 | std::shared_ptr<C2StreamColorAspectsInfo::output> mColorAspects; |
Haripriya Deshmukh | e355f2e | 2024-02-23 16:57:14 +0530 | [diff] [blame] | 112 | std::shared_ptr<C2StreamPictureQuantizationTuning::output> mQpBounds; |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 113 | |
Fyodor Kyslov | a056c62 | 2022-09-21 21:41:04 +0000 | [diff] [blame] | 114 | aom_codec_err_t setupCodecParameters(); |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | class C2SoftAomEnc::IntfImpl : public SimpleInterface<void>::BaseParams { |
Fyodor Kyslov | a056c62 | 2022-09-21 21:41:04 +0000 | [diff] [blame] | 118 | public: |
| 119 | explicit IntfImpl(const std::shared_ptr<C2ReflectorHelper>& helper); |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 120 | |
Fyodor Kyslov | a056c62 | 2022-09-21 21:41:04 +0000 | [diff] [blame] | 121 | static C2R BitrateSetter(bool mayBlock, C2P<C2StreamBitrateInfo::output>& me); |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 122 | |
Fyodor Kyslov | a056c62 | 2022-09-21 21:41:04 +0000 | [diff] [blame] | 123 | static C2R SizeSetter(bool mayBlock, const C2P<C2StreamPictureSizeInfo::input>& oldMe, |
| 124 | C2P<C2StreamPictureSizeInfo::input>& me); |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 125 | |
mtk12101 | c1670e9 | 2023-02-10 09:34:02 +0800 | [diff] [blame] | 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); |
Haripriya Deshmukh | e355f2e | 2024-02-23 16:57:14 +0530 | [diff] [blame] | 130 | static C2R PictureQuantizationSetter(bool mayBlock, |
| 131 | C2P<C2StreamPictureQuantizationTuning::output> &me); |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 132 | |
| 133 | // unsafe getters |
| 134 | std::shared_ptr<C2StreamPictureSizeInfo::input> getSize_l() const { return mSize; } |
| 135 | std::shared_ptr<C2StreamIntraRefreshTuning::output> getIntraRefresh_l() const { |
| 136 | return mIntraRefresh; |
| 137 | } |
| 138 | std::shared_ptr<C2StreamFrameRateInfo::output> getFrameRate_l() const { return mFrameRate; } |
| 139 | std::shared_ptr<C2StreamBitrateInfo::output> getBitrate_l() const { return mBitrate; } |
Fyodor Kyslov | 58d8dea | 2023-02-10 02:15:54 +0000 | [diff] [blame] | 140 | std::shared_ptr<C2StreamQualityTuning::output> getQuality_l() const { return mQuality; } |
Fyodor Kyslov | 3e3a92b | 2023-03-15 00:39:36 +0000 | [diff] [blame] | 141 | std::shared_ptr<C2StreamComplexityTuning::output> getComplexity_l() const { |
| 142 | return mComplexity; |
| 143 | } |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 144 | std::shared_ptr<C2StreamBitrateModeTuning::output> getBitrateMode_l() const { |
| 145 | return mBitrateMode; |
| 146 | } |
| 147 | std::shared_ptr<C2StreamRequestSyncFrameTuning::output> getRequestSync_l() const { |
| 148 | return mRequestSync; |
| 149 | } |
| 150 | std::shared_ptr<C2StreamColorAspectsInfo::output> getCodedColorAspects_l() const { |
| 151 | return mCodedColorAspects; |
| 152 | } |
Harish Mahendrakar | 7de78d4 | 2023-02-06 17:12:18 -0800 | [diff] [blame] | 153 | std::shared_ptr<C2StreamPixelFormatInfo::input> getPixelFormat_l() const { |
Fyodor Kyslov | d97e991 | 2022-11-07 19:23:21 +0000 | [diff] [blame] | 154 | return mPixelFormat; |
| 155 | } |
Haripriya Deshmukh | e355f2e | 2024-02-23 16:57:14 +0530 | [diff] [blame] | 156 | std::shared_ptr<C2StreamPictureQuantizationTuning::output> getPictureQuantization_l() const { |
| 157 | return mPictureQuantization; |
| 158 | } |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 159 | uint32_t getSyncFramePeriod() const; |
Fyodor Kyslov | a056c62 | 2022-09-21 21:41:04 +0000 | [diff] [blame] | 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); |
Harish Mahendrakar | 7d482b2 | 2023-08-21 20:08:01 -0700 | [diff] [blame] | 163 | uint32_t getLevel_l() const; |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 164 | |
Fyodor Kyslov | a056c62 | 2022-09-21 21:41:04 +0000 | [diff] [blame] | 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; |
Fyodor Kyslov | 58d8dea | 2023-02-10 02:15:54 +0000 | [diff] [blame] | 173 | std::shared_ptr<C2StreamQualityTuning::output> mQuality; |
Fyodor Kyslov | 3e3a92b | 2023-03-15 00:39:36 +0000 | [diff] [blame] | 174 | std::shared_ptr<C2StreamComplexityTuning::output> mComplexity; |
Fyodor Kyslov | a056c62 | 2022-09-21 21:41:04 +0000 | [diff] [blame] | 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; |
Harish Mahendrakar | 7de78d4 | 2023-02-06 17:12:18 -0800 | [diff] [blame] | 179 | std::shared_ptr<C2StreamPixelFormatInfo::input> mPixelFormat; |
Haripriya Deshmukh | e355f2e | 2024-02-23 16:57:14 +0530 | [diff] [blame] | 180 | std::shared_ptr<C2StreamPictureQuantizationTuning::output> mPictureQuantization; |
Fyodor Kyslov | d97e991 | 2022-11-07 19:23:21 +0000 | [diff] [blame] | 181 | |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 182 | }; |
| 183 | |
| 184 | } // namespace android |
| 185 | #endif // ANDROID_C2_SOFT_AV1_ENC_H_ |