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 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "C2SoftAomEnc" |
| 19 | #include <log/log.h> |
| 20 | |
| 21 | #include <media/stagefright/foundation/AUtils.h> |
| 22 | #include <media/stagefright/foundation/MediaDefs.h> |
| 23 | |
| 24 | #include <C2Debug.h> |
Harish Mahendrakar | 7de78d4 | 2023-02-06 17:12:18 -0800 | [diff] [blame] | 25 | #include <Codec2CommonUtils.h> |
Aayush Soni | 643b918 | 2023-01-18 20:23:56 +0530 | [diff] [blame] | 26 | #include <Codec2Mapper.h> |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 27 | #include <C2PlatformSupport.h> |
| 28 | #include <SimpleC2Interface.h> |
| 29 | |
| 30 | #include "C2SoftAomEnc.h" |
| 31 | |
| 32 | namespace android { |
| 33 | |
| 34 | constexpr char COMPONENT_NAME[] = "c2.android.av1.encoder"; |
| 35 | |
| 36 | #define DEFAULT_SPEED 10 |
| 37 | |
| 38 | C2SoftAomEnc::IntfImpl::IntfImpl(const std::shared_ptr<C2ReflectorHelper>& helper) |
| 39 | : SimpleInterface<void>::BaseParams(helper, COMPONENT_NAME, C2Component::KIND_ENCODER, |
| 40 | C2Component::DOMAIN_VIDEO, MEDIA_MIMETYPE_VIDEO_AV1) { |
| 41 | noPrivateBuffers(); // TODO: account for our buffers here |
| 42 | noInputReferences(); |
| 43 | noOutputReferences(); |
| 44 | noInputLatency(); |
| 45 | noTimeStretch(); |
| 46 | setDerivedInstance(this); |
| 47 | |
| 48 | addParameter(DefineParam(mUsage, C2_PARAMKEY_INPUT_STREAM_USAGE) |
| 49 | .withConstValue(new C2StreamUsageTuning::input( |
| 50 | 0u, (uint64_t)C2MemoryUsage::CPU_READ)) |
| 51 | .build()); |
| 52 | |
Harish Mahendrakar | fc3f741 | 2024-03-21 17:29:37 +0000 | [diff] [blame] | 53 | // Odd dimension support in encoders requires Android V and above |
| 54 | size_t stepSize = isAtLeastV() ? 1 : 2; |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 55 | addParameter(DefineParam(mSize, C2_PARAMKEY_PICTURE_SIZE) |
| 56 | .withDefault(new C2StreamPictureSizeInfo::input(0u, 320, 240)) |
| 57 | .withFields({ |
Harish Mahendrakar | fc3f741 | 2024-03-21 17:29:37 +0000 | [diff] [blame] | 58 | C2F(mSize, width).inRange(2, 2048, stepSize), |
| 59 | C2F(mSize, height).inRange(2, 2048, stepSize), |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 60 | }) |
| 61 | .withSetter(SizeSetter) |
| 62 | .build()); |
| 63 | |
| 64 | addParameter(DefineParam(mBitrateMode, C2_PARAMKEY_BITRATE_MODE) |
| 65 | .withDefault(new C2StreamBitrateModeTuning::output( |
| 66 | 0u, C2Config::BITRATE_VARIABLE)) |
| 67 | .withFields({C2F(mBitrateMode, value) |
| 68 | .oneOf({C2Config::BITRATE_CONST, |
Fyodor Kyslov | 58d8dea | 2023-02-10 02:15:54 +0000 | [diff] [blame] | 69 | C2Config::BITRATE_VARIABLE, |
| 70 | C2Config::BITRATE_IGNORE})}) |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 71 | .withSetter(Setter<decltype(*mBitrateMode)>::StrictValueWithNoDeps) |
| 72 | .build()); |
| 73 | |
| 74 | addParameter(DefineParam(mFrameRate, C2_PARAMKEY_FRAME_RATE) |
| 75 | .withDefault(new C2StreamFrameRateInfo::output(0u, 30.)) |
| 76 | // TODO: More restriction? |
| 77 | .withFields({C2F(mFrameRate, value).greaterThan(0.)}) |
| 78 | .withSetter(Setter<decltype(*mFrameRate)>::StrictValueWithNoDeps) |
| 79 | .build()); |
| 80 | |
| 81 | addParameter(DefineParam(mSyncFramePeriod, C2_PARAMKEY_SYNC_FRAME_INTERVAL) |
| 82 | .withDefault(new C2StreamSyncFrameIntervalTuning::output(0u, 1000000)) |
| 83 | .withFields({C2F(mSyncFramePeriod, value).any()}) |
| 84 | .withSetter(Setter<decltype(*mSyncFramePeriod)>::StrictValueWithNoDeps) |
| 85 | .build()); |
| 86 | |
| 87 | addParameter(DefineParam(mBitrate, C2_PARAMKEY_BITRATE) |
| 88 | .withDefault(new C2StreamBitrateInfo::output(0u, 64000)) |
| 89 | .withFields({C2F(mBitrate, value).inRange(4096, 40000000)}) |
| 90 | .withSetter(BitrateSetter) |
| 91 | .build()); |
| 92 | |
Fyodor Kyslov | 3e3a92b | 2023-03-15 00:39:36 +0000 | [diff] [blame] | 93 | addParameter(DefineParam(mComplexity, C2_PARAMKEY_COMPLEXITY) |
| 94 | .withDefault(new C2StreamComplexityTuning::output(0u, 0)) |
| 95 | .withFields({C2F(mComplexity, value).inRange(0, 5)}) |
| 96 | .withSetter(Setter<decltype(*mComplexity)>::NonStrictValueWithNoDeps) |
| 97 | .build()); |
| 98 | |
Fyodor Kyslov | 58d8dea | 2023-02-10 02:15:54 +0000 | [diff] [blame] | 99 | addParameter(DefineParam(mQuality, C2_PARAMKEY_QUALITY) |
| 100 | .withDefault(new C2StreamQualityTuning::output(0u, 80)) |
| 101 | .withFields({C2F(mQuality, value).inRange(0, 100)}) |
| 102 | .withSetter(Setter<decltype(*mQuality)>::NonStrictValueWithNoDeps) |
| 103 | .build()); |
| 104 | |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 105 | addParameter(DefineParam(mIntraRefresh, C2_PARAMKEY_INTRA_REFRESH) |
| 106 | .withConstValue(new C2StreamIntraRefreshTuning::output( |
| 107 | 0u, C2Config::INTRA_REFRESH_DISABLED, 0.)) |
| 108 | .build()); |
| 109 | |
| 110 | addParameter(DefineParam(mProfileLevel, C2_PARAMKEY_PROFILE_LEVEL) |
| 111 | .withDefault(new C2StreamProfileLevelInfo::output(0u, PROFILE_AV1_0, |
Harish Mahendrakar | 7d482b2 | 2023-08-21 20:08:01 -0700 | [diff] [blame] | 112 | LEVEL_AV1_2)) |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 113 | .withFields({ |
| 114 | C2F(mProfileLevel, profile).equalTo(PROFILE_AV1_0), |
Fyodor Kyslov | a809232 | 2022-10-09 02:47:33 +0000 | [diff] [blame] | 115 | C2F(mProfileLevel, level) |
| 116 | .oneOf({LEVEL_AV1_2, LEVEL_AV1_2_1, LEVEL_AV1_2_2, |
| 117 | LEVEL_AV1_2_3, LEVEL_AV1_3, LEVEL_AV1_3_1, |
| 118 | LEVEL_AV1_3_2, LEVEL_AV1_3_3, LEVEL_AV1_4, |
| 119 | LEVEL_AV1_4_1}), |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 120 | }) |
mtk12101 | c1670e9 | 2023-02-10 09:34:02 +0800 | [diff] [blame] | 121 | .withSetter(ProfileLevelSetter, mSize, mFrameRate, mBitrate) |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 122 | .build()); |
| 123 | |
Harish Mahendrakar | 7de78d4 | 2023-02-06 17:12:18 -0800 | [diff] [blame] | 124 | std::vector<uint32_t> pixelFormats = {HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, |
| 125 | HAL_PIXEL_FORMAT_YCBCR_420_888}; |
| 126 | if (isHalPixelFormatSupported((AHardwareBuffer_Format)HAL_PIXEL_FORMAT_YCBCR_P010)) { |
| 127 | pixelFormats.push_back(HAL_PIXEL_FORMAT_YCBCR_P010); |
| 128 | } |
Fyodor Kyslov | d97e991 | 2022-11-07 19:23:21 +0000 | [diff] [blame] | 129 | addParameter(DefineParam(mPixelFormat, C2_PARAMKEY_PIXEL_FORMAT) |
Harish Mahendrakar | 7de78d4 | 2023-02-06 17:12:18 -0800 | [diff] [blame] | 130 | .withDefault(new C2StreamPixelFormatInfo::input( |
Fyodor Kyslov | d97e991 | 2022-11-07 19:23:21 +0000 | [diff] [blame] | 131 | 0u, HAL_PIXEL_FORMAT_YCBCR_420_888)) |
Harish Mahendrakar | 7de78d4 | 2023-02-06 17:12:18 -0800 | [diff] [blame] | 132 | .withFields({C2F(mPixelFormat, value).oneOf({pixelFormats})}) |
Fyodor Kyslov | d97e991 | 2022-11-07 19:23:21 +0000 | [diff] [blame] | 133 | .withSetter((Setter<decltype(*mPixelFormat)>::StrictValueWithNoDeps)) |
| 134 | .build()); |
| 135 | |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 136 | addParameter(DefineParam(mRequestSync, C2_PARAMKEY_REQUEST_SYNC_FRAME) |
| 137 | .withDefault(new C2StreamRequestSyncFrameTuning::output(0u, C2_FALSE)) |
| 138 | .withFields({C2F(mRequestSync, value).oneOf({C2_FALSE, C2_TRUE})}) |
| 139 | .withSetter(Setter<decltype(*mRequestSync)>::NonStrictValueWithNoDeps) |
| 140 | .build()); |
| 141 | addParameter( |
| 142 | DefineParam(mColorAspects, C2_PARAMKEY_COLOR_ASPECTS) |
| 143 | .withDefault(new C2StreamColorAspectsInfo::input( |
| 144 | 0u, C2Color::RANGE_UNSPECIFIED, C2Color::PRIMARIES_UNSPECIFIED, |
| 145 | C2Color::TRANSFER_UNSPECIFIED, C2Color::MATRIX_UNSPECIFIED)) |
| 146 | .withFields( |
| 147 | {C2F(mColorAspects, range) |
| 148 | .inRange(C2Color::RANGE_UNSPECIFIED, C2Color::RANGE_OTHER), |
| 149 | C2F(mColorAspects, primaries) |
| 150 | .inRange(C2Color::PRIMARIES_UNSPECIFIED, |
| 151 | C2Color::PRIMARIES_OTHER), |
| 152 | C2F(mColorAspects, transfer) |
| 153 | .inRange(C2Color::TRANSFER_UNSPECIFIED, |
| 154 | C2Color::TRANSFER_OTHER), |
| 155 | C2F(mColorAspects, matrix) |
| 156 | .inRange(C2Color::MATRIX_UNSPECIFIED, C2Color::MATRIX_OTHER)}) |
| 157 | .withSetter(ColorAspectsSetter) |
| 158 | .build()); |
| 159 | |
| 160 | addParameter( |
| 161 | DefineParam(mCodedColorAspects, C2_PARAMKEY_VUI_COLOR_ASPECTS) |
| 162 | .withDefault(new C2StreamColorAspectsInfo::output( |
| 163 | 0u, C2Color::RANGE_LIMITED, C2Color::PRIMARIES_UNSPECIFIED, |
| 164 | C2Color::TRANSFER_UNSPECIFIED, C2Color::MATRIX_UNSPECIFIED)) |
| 165 | .withFields( |
| 166 | {C2F(mCodedColorAspects, range) |
| 167 | .inRange(C2Color::RANGE_UNSPECIFIED, C2Color::RANGE_OTHER), |
| 168 | C2F(mCodedColorAspects, primaries) |
| 169 | .inRange(C2Color::PRIMARIES_UNSPECIFIED, |
| 170 | C2Color::PRIMARIES_OTHER), |
| 171 | C2F(mCodedColorAspects, transfer) |
| 172 | .inRange(C2Color::TRANSFER_UNSPECIFIED, |
| 173 | C2Color::TRANSFER_OTHER), |
| 174 | C2F(mCodedColorAspects, matrix) |
| 175 | .inRange(C2Color::MATRIX_UNSPECIFIED, C2Color::MATRIX_OTHER)}) |
| 176 | .withSetter(CodedColorAspectsSetter, mColorAspects) |
| 177 | .build()); |
| 178 | } |
| 179 | |
| 180 | C2R C2SoftAomEnc::IntfImpl::BitrateSetter(bool mayBlock, C2P<C2StreamBitrateInfo::output>& me) { |
| 181 | (void)mayBlock; |
| 182 | C2R res = C2R::Ok(); |
| 183 | if (me.v.value < 4096) { |
| 184 | me.set().value = 4096; |
| 185 | } |
| 186 | return res; |
| 187 | } |
| 188 | |
| 189 | C2R C2SoftAomEnc::IntfImpl::SizeSetter(bool mayBlock, |
| 190 | const C2P<C2StreamPictureSizeInfo::input>& oldMe, |
| 191 | C2P<C2StreamPictureSizeInfo::input>& me) { |
| 192 | (void)mayBlock; |
| 193 | C2R res = C2R::Ok(); |
| 194 | if (!me.F(me.v.width).supportsAtAll(me.v.width)) { |
| 195 | res = res.plus(C2SettingResultBuilder::BadValue(me.F(me.v.width))); |
| 196 | me.set().width = oldMe.v.width; |
| 197 | } |
| 198 | if (!me.F(me.v.height).supportsAtAll(me.v.height)) { |
| 199 | res = res.plus(C2SettingResultBuilder::BadValue(me.F(me.v.height))); |
| 200 | me.set().height = oldMe.v.height; |
| 201 | } |
| 202 | return res; |
| 203 | } |
| 204 | |
| 205 | C2R C2SoftAomEnc::IntfImpl::ProfileLevelSetter(bool mayBlock, |
mtk12101 | c1670e9 | 2023-02-10 09:34:02 +0800 | [diff] [blame] | 206 | C2P<C2StreamProfileLevelInfo::output>& me, |
| 207 | const C2P<C2StreamPictureSizeInfo::input>& size, |
| 208 | const C2P<C2StreamFrameRateInfo::output>& frameRate, |
| 209 | const C2P<C2StreamBitrateInfo::output>& bitrate) { |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 210 | (void)mayBlock; |
| 211 | if (!me.F(me.v.profile).supportsAtAll(me.v.profile)) { |
| 212 | me.set().profile = PROFILE_AV1_0; |
| 213 | } |
mtk12101 | c1670e9 | 2023-02-10 09:34:02 +0800 | [diff] [blame] | 214 | struct LevelLimits { |
| 215 | C2Config::level_t level; |
| 216 | float samplesPerSec; |
| 217 | uint64_t samples; |
| 218 | uint32_t bitrate; |
| 219 | size_t maxHSize; |
| 220 | size_t maxVSize; |
| 221 | }; |
| 222 | constexpr LevelLimits kLimits[] = { |
| 223 | {LEVEL_AV1_2, 4423680, 147456, 1500000, 2048, 1152}, |
| 224 | {LEVEL_AV1_2_1, 8363520, 278784, 3000000, 2816, 1584}, |
| 225 | {LEVEL_AV1_3, 19975680, 665856, 6000000, 4352, 2448}, |
| 226 | {LEVEL_AV1_3_1, 37950720, 1065024, 10000000, 5504, 3096}, |
| 227 | {LEVEL_AV1_4, 70778880, 2359296, 12000000, 6144, 3456}, |
| 228 | {LEVEL_AV1_4_1, 141557760, 2359296, 20000000, 6144, 3456}, |
| 229 | }; |
| 230 | |
| 231 | uint64_t samples = size.v.width * size.v.height; |
| 232 | float samplesPerSec = float(samples) * frameRate.v.value; |
| 233 | |
| 234 | // Check if the supplied level meets the samples / bitrate requirements. |
| 235 | // If not, update the level with the lowest level meeting the requirements. |
| 236 | bool found = false; |
| 237 | |
| 238 | // By default needsUpdate = false in case the supplied level does meet |
| 239 | // the requirements. |
| 240 | bool needsUpdate = false; |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 241 | if (!me.F(me.v.level).supportsAtAll(me.v.level)) { |
mtk12101 | c1670e9 | 2023-02-10 09:34:02 +0800 | [diff] [blame] | 242 | needsUpdate = true; |
| 243 | } |
| 244 | for (const LevelLimits& limit : kLimits) { |
| 245 | if (samples <= limit.samples && samplesPerSec <= limit.samplesPerSec && |
| 246 | bitrate.v.value <= limit.bitrate && size.v.width <= limit.maxHSize && |
| 247 | size.v.height <= limit.maxVSize) { |
| 248 | // This is the lowest level that meets the requirements, and if |
| 249 | // we haven't seen the supplied level yet, that means we don't |
| 250 | // need the update. |
| 251 | if (needsUpdate) { |
| 252 | ALOGD("Given level %x does not cover current configuration: " |
| 253 | "adjusting to %x", |
| 254 | me.v.level, limit.level); |
| 255 | me.set().level = limit.level; |
| 256 | } |
| 257 | found = true; |
| 258 | break; |
| 259 | } |
| 260 | if (me.v.level == limit.level) { |
| 261 | // We break out of the loop when the lowest feasible level is |
| 262 | // found. The fact that we're here means that our level doesn't |
| 263 | // meet the requirement and needs to be updated. |
| 264 | needsUpdate = true; |
| 265 | } |
| 266 | } |
| 267 | if (!found) { |
| 268 | // We set to the highest supported level. |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 269 | me.set().level = LEVEL_AV1_4_1; |
| 270 | } |
| 271 | return C2R::Ok(); |
| 272 | } |
| 273 | |
| 274 | uint32_t C2SoftAomEnc::IntfImpl::getSyncFramePeriod() const { |
| 275 | if (mSyncFramePeriod->value < 0 || mSyncFramePeriod->value == INT64_MAX) { |
| 276 | return 0; |
| 277 | } |
| 278 | double period = mSyncFramePeriod->value / 1e6 * mFrameRate->value; |
| 279 | return (uint32_t)c2_max(c2_min(period + 0.5, double(UINT32_MAX)), 1.); |
| 280 | } |
| 281 | |
| 282 | C2R C2SoftAomEnc::IntfImpl::ColorAspectsSetter(bool mayBlock, |
| 283 | C2P<C2StreamColorAspectsInfo::input>& me) { |
| 284 | (void)mayBlock; |
| 285 | if (me.v.range > C2Color::RANGE_OTHER) { |
| 286 | me.set().range = C2Color::RANGE_OTHER; |
| 287 | } |
| 288 | if (me.v.primaries > C2Color::PRIMARIES_OTHER) { |
| 289 | me.set().primaries = C2Color::PRIMARIES_OTHER; |
| 290 | } |
| 291 | if (me.v.transfer > C2Color::TRANSFER_OTHER) { |
| 292 | me.set().transfer = C2Color::TRANSFER_OTHER; |
| 293 | } |
| 294 | if (me.v.matrix > C2Color::MATRIX_OTHER) { |
| 295 | me.set().matrix = C2Color::MATRIX_OTHER; |
| 296 | } |
| 297 | return C2R::Ok(); |
| 298 | } |
| 299 | C2R C2SoftAomEnc::IntfImpl::CodedColorAspectsSetter( |
| 300 | bool mayBlock, C2P<C2StreamColorAspectsInfo::output>& me, |
| 301 | const C2P<C2StreamColorAspectsInfo::input>& coded) { |
| 302 | (void)mayBlock; |
| 303 | me.set().range = coded.v.range; |
| 304 | me.set().primaries = coded.v.primaries; |
| 305 | me.set().transfer = coded.v.transfer; |
| 306 | me.set().matrix = coded.v.matrix; |
| 307 | return C2R::Ok(); |
| 308 | } |
| 309 | |
Harish Mahendrakar | 7d482b2 | 2023-08-21 20:08:01 -0700 | [diff] [blame] | 310 | uint32_t C2SoftAomEnc::IntfImpl::getLevel_l() const { |
| 311 | return mProfileLevel->level - LEVEL_AV1_2; |
| 312 | } |
| 313 | |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 314 | C2SoftAomEnc::C2SoftAomEnc(const char* name, c2_node_id_t id, |
| 315 | const std::shared_ptr<IntfImpl>& intfImpl) |
| 316 | : SimpleC2Component(std::make_shared<SimpleInterface<IntfImpl>>(name, id, intfImpl)), |
| 317 | mIntf(intfImpl), |
| 318 | mCodecContext(nullptr), |
| 319 | mCodecConfiguration(nullptr), |
| 320 | mCodecInterface(nullptr), |
| 321 | mStrideAlign(2), |
| 322 | mBitrateControlMode(AOM_VBR), |
| 323 | mMinQuantizer(0), |
| 324 | mMaxQuantizer(0), |
Fyodor Kyslov | d97e991 | 2022-11-07 19:23:21 +0000 | [diff] [blame] | 325 | mLastTimestamp(INT64_MAX), |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 326 | mSignalledOutputEos(false), |
| 327 | mSignalledError(false), |
Fyodor Kyslov | d97e991 | 2022-11-07 19:23:21 +0000 | [diff] [blame] | 328 | mHeadersReceived(false), |
| 329 | mIs10Bit(false) { |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 330 | ALOGV("Constructor"); |
| 331 | } |
| 332 | |
| 333 | C2SoftAomEnc::~C2SoftAomEnc() { |
| 334 | ALOGV("Destructor"); |
| 335 | onRelease(); |
| 336 | } |
| 337 | |
| 338 | c2_status_t C2SoftAomEnc::onInit() { |
Fyodor Kyslov | d97e991 | 2022-11-07 19:23:21 +0000 | [diff] [blame] | 339 | return C2_OK; |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | c2_status_t C2SoftAomEnc::onStop() { |
Aayush Soni | 5d27869 | 2023-10-27 15:49:37 +0530 | [diff] [blame] | 343 | IntfImpl::Lock lock = mIntf->lock(); |
| 344 | std::shared_ptr<C2StreamRequestSyncFrameTuning::output> requestSync = mIntf->getRequestSync_l(); |
| 345 | lock.unlock(); |
| 346 | if (requestSync != mRequestSync) { |
| 347 | // we can handle IDR immediately |
| 348 | if (requestSync->value) { |
| 349 | // unset request |
| 350 | C2StreamRequestSyncFrameTuning::output clearSync(0u, C2_FALSE); |
| 351 | std::vector<std::unique_ptr<C2SettingResult>> failures; |
| 352 | mIntf->config({ &clearSync }, C2_MAY_BLOCK, &failures); |
| 353 | } |
| 354 | mRequestSync = requestSync; |
| 355 | } |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 356 | onRelease(); |
| 357 | return C2_OK; |
| 358 | } |
| 359 | |
| 360 | void C2SoftAomEnc::onReset() { |
| 361 | (void)onStop(); |
| 362 | } |
| 363 | |
| 364 | void C2SoftAomEnc::onRelease() { |
| 365 | if (mCodecContext) { |
| 366 | aom_codec_destroy(mCodecContext); |
| 367 | delete mCodecContext; |
| 368 | mCodecContext = nullptr; |
| 369 | } |
| 370 | |
| 371 | if (mCodecConfiguration) { |
| 372 | delete mCodecConfiguration; |
| 373 | mCodecConfiguration = nullptr; |
| 374 | } |
| 375 | |
| 376 | // this one is not allocated by us |
| 377 | mCodecInterface = nullptr; |
Fyodor Kyslov | d97e991 | 2022-11-07 19:23:21 +0000 | [diff] [blame] | 378 | mHeadersReceived = false; |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | c2_status_t C2SoftAomEnc::onFlush_sm() { |
| 382 | return onStop(); |
| 383 | } |
| 384 | |
Fyodor Kyslov | 58d8dea | 2023-02-10 02:15:54 +0000 | [diff] [blame] | 385 | // c2Quality is in range of 0-100 (the more - the better), |
| 386 | // for AOM quality we are using a range of 15-50 (the less - the better) |
| 387 | static int MapC2QualityToAOMQuality (int c2Quality) { |
| 388 | return 15 + 35 * (100 - c2Quality) / 100; |
| 389 | } |
| 390 | |
Fyodor Kyslov | 3e3a92b | 2023-03-15 00:39:36 +0000 | [diff] [blame] | 391 | static int MapC2ComplexityToAOMSpeed (int c2Complexity) { |
| 392 | int mapping[6] = {10, 9, 8, 7, 6, 6}; |
| 393 | if (c2Complexity > 5 || c2Complexity < 0) { |
| 394 | ALOGW("Wrong complexity setting. Falling back to speed 10"); |
| 395 | return 10; |
| 396 | } |
| 397 | return mapping[c2Complexity]; |
| 398 | } |
| 399 | |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 400 | aom_codec_err_t C2SoftAomEnc::setupCodecParameters() { |
| 401 | aom_codec_err_t codec_return = AOM_CODEC_OK; |
| 402 | |
Harish Mahendrakar | 7d482b2 | 2023-08-21 20:08:01 -0700 | [diff] [blame] | 403 | codec_return = aom_codec_control(mCodecContext, AV1E_SET_TARGET_SEQ_LEVEL_IDX, mAV1EncLevel); |
| 404 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 405 | |
Fyodor Kyslov | 3e3a92b | 2023-03-15 00:39:36 +0000 | [diff] [blame] | 406 | codec_return = aom_codec_control(mCodecContext, AOME_SET_CPUUSED, |
| 407 | MapC2ComplexityToAOMSpeed(mComplexity->value)); |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 408 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 409 | |
| 410 | codec_return = aom_codec_control(mCodecContext, AV1E_SET_ROW_MT, 1); |
| 411 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 412 | |
| 413 | codec_return = aom_codec_control(mCodecContext, AV1E_SET_ENABLE_CDEF, 1); |
| 414 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 415 | |
| 416 | codec_return = aom_codec_control(mCodecContext, AV1E_SET_ENABLE_TPL_MODEL, 0); |
| 417 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 418 | |
| 419 | codec_return = aom_codec_control(mCodecContext, AV1E_SET_DELTAQ_MODE, 0); |
| 420 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 421 | |
| 422 | codec_return = aom_codec_control(mCodecContext, AV1E_SET_ENABLE_ORDER_HINT, 0); |
| 423 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 424 | |
| 425 | codec_return = aom_codec_control(mCodecContext, AV1E_SET_AQ_MODE, 3); |
| 426 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 427 | |
| 428 | codec_return = aom_codec_control(mCodecContext, AV1E_SET_COEFF_COST_UPD_FREQ, 3); |
| 429 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 430 | |
| 431 | codec_return = aom_codec_control(mCodecContext, AV1E_SET_MODE_COST_UPD_FREQ, 3); |
| 432 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 433 | |
| 434 | codec_return = aom_codec_control(mCodecContext, AV1E_SET_MV_COST_UPD_FREQ, 3); |
| 435 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 436 | |
| 437 | codec_return = aom_codec_control(mCodecContext, AV1E_SET_ENABLE_PALETTE, 0); |
| 438 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 439 | |
| 440 | codec_return = aom_codec_control(mCodecContext, AV1E_SET_ENABLE_OBMC, 0); |
| 441 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 442 | |
| 443 | codec_return = aom_codec_control(mCodecContext, AV1E_SET_NOISE_SENSITIVITY, 0); |
| 444 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 445 | |
| 446 | codec_return = aom_codec_control(mCodecContext, AV1E_SET_ENABLE_WARPED_MOTION, 0); |
| 447 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 448 | |
| 449 | codec_return = aom_codec_control(mCodecContext, AV1E_SET_ENABLE_GLOBAL_MOTION, 0); |
| 450 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 451 | |
| 452 | codec_return = aom_codec_control(mCodecContext, AV1E_SET_ENABLE_REF_FRAME_MVS, 0); |
| 453 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 454 | |
| 455 | codec_return = aom_codec_control(mCodecContext, AV1E_SET_ENABLE_CFL_INTRA, 0); |
| 456 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 457 | |
| 458 | codec_return = aom_codec_control(mCodecContext, AV1E_SET_ENABLE_SMOOTH_INTRA, 0); |
| 459 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 460 | codec_return = aom_codec_control(mCodecContext, AV1E_SET_ENABLE_ANGLE_DELTA, 0); |
| 461 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 462 | codec_return = aom_codec_control(mCodecContext, AV1E_SET_ENABLE_FILTER_INTRA, 0); |
| 463 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 464 | |
| 465 | codec_return = aom_codec_control(mCodecContext, AV1E_SET_INTRA_DEFAULT_TX_ONLY, 1); |
| 466 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 467 | codec_return = aom_codec_control(mCodecContext, AV1E_SET_DISABLE_TRELLIS_QUANT, 1); |
| 468 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 469 | |
| 470 | codec_return = aom_codec_control(mCodecContext, AV1E_SET_ENABLE_DIST_WTD_COMP, 0); |
| 471 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 472 | codec_return = aom_codec_control(mCodecContext, AV1E_SET_ENABLE_DIFF_WTD_COMP, 0); |
| 473 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 474 | codec_return = aom_codec_control(mCodecContext, AV1E_SET_ENABLE_DUAL_FILTER, 0); |
| 475 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 476 | codec_return = aom_codec_control(mCodecContext, AV1E_SET_ENABLE_INTERINTRA_COMP, 0); |
| 477 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 478 | codec_return = aom_codec_control(mCodecContext, AV1E_SET_ENABLE_INTERINTRA_WEDGE, 0); |
| 479 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 480 | codec_return = aom_codec_control(mCodecContext, AV1E_SET_ENABLE_INTRA_EDGE_FILTER, 0); |
| 481 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 482 | codec_return = aom_codec_control(mCodecContext, AV1E_SET_ENABLE_INTRABC, 0); |
| 483 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 484 | codec_return = aom_codec_control(mCodecContext, AV1E_SET_ENABLE_MASKED_COMP, 0); |
| 485 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 486 | codec_return = aom_codec_control(mCodecContext, AV1E_SET_ENABLE_PAETH_INTRA, 0); |
| 487 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 488 | codec_return = aom_codec_control(mCodecContext, AV1E_SET_ENABLE_QM, 0); |
| 489 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 490 | codec_return = aom_codec_control(mCodecContext, AV1E_SET_ENABLE_RECT_PARTITIONS, 0); |
| 491 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 492 | codec_return = aom_codec_control(mCodecContext, AV1E_SET_ENABLE_RESTORATION, 0); |
| 493 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 494 | codec_return = aom_codec_control(mCodecContext, AV1E_SET_ENABLE_SMOOTH_INTERINTRA, 0); |
| 495 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 496 | codec_return = aom_codec_control(mCodecContext, AV1E_SET_ENABLE_TX64, 0); |
| 497 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 498 | |
| 499 | codec_return = aom_codec_control(mCodecContext, AV1E_SET_MAX_REFERENCE_FRAMES, 3); |
| 500 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 501 | |
Fyodor Kyslov | 58d8dea | 2023-02-10 02:15:54 +0000 | [diff] [blame] | 502 | if (mBitrateControlMode == AOM_Q) { |
| 503 | const int aomCQLevel = MapC2QualityToAOMQuality(mQuality->value); |
| 504 | ALOGV("Set Q from %d to CQL %d", |
| 505 | mQuality->value, aomCQLevel); |
| 506 | |
| 507 | codec_return = aom_codec_control(mCodecContext, AOME_SET_CQ_LEVEL, aomCQLevel); |
| 508 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 509 | } |
| 510 | |
Aayush Soni | 643b918 | 2023-01-18 20:23:56 +0530 | [diff] [blame] | 511 | ColorAspects sfAspects; |
| 512 | if (!C2Mapper::map(mColorAspects->primaries, &sfAspects.mPrimaries)) { |
| 513 | sfAspects.mPrimaries = android::ColorAspects::PrimariesUnspecified; |
| 514 | } |
| 515 | if (!C2Mapper::map(mColorAspects->range, &sfAspects.mRange)) { |
| 516 | sfAspects.mRange = android::ColorAspects::RangeUnspecified; |
| 517 | } |
| 518 | if (!C2Mapper::map(mColorAspects->matrix, &sfAspects.mMatrixCoeffs)) { |
| 519 | sfAspects.mMatrixCoeffs = android::ColorAspects::MatrixUnspecified; |
| 520 | } |
| 521 | if (!C2Mapper::map(mColorAspects->transfer, &sfAspects.mTransfer)) { |
| 522 | sfAspects.mTransfer = android::ColorAspects::TransferUnspecified; |
| 523 | } |
| 524 | int32_t primaries, transfer, matrixCoeffs; |
| 525 | bool range; |
| 526 | ColorUtils::convertCodecColorAspectsToIsoAspects(sfAspects, |
| 527 | &primaries, |
| 528 | &transfer, |
| 529 | &matrixCoeffs, |
| 530 | &range); |
| 531 | |
| 532 | codec_return = aom_codec_control(mCodecContext, AV1E_SET_COLOR_RANGE, range); |
| 533 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 534 | codec_return = aom_codec_control(mCodecContext, AV1E_SET_COLOR_PRIMARIES, primaries); |
| 535 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 536 | codec_return = aom_codec_control(mCodecContext, AV1E_SET_TRANSFER_CHARACTERISTICS, transfer); |
| 537 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 538 | codec_return = aom_codec_control(mCodecContext, AV1E_SET_MATRIX_COEFFICIENTS, matrixCoeffs); |
| 539 | if (codec_return != AOM_CODEC_OK) goto BailOut; |
| 540 | |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 541 | BailOut: |
| 542 | return codec_return; |
| 543 | } |
| 544 | |
| 545 | status_t C2SoftAomEnc::initEncoder() { |
| 546 | aom_codec_err_t codec_return; |
| 547 | status_t result = UNKNOWN_ERROR; |
| 548 | { |
| 549 | IntfImpl::Lock lock = mIntf->lock(); |
| 550 | // Fetch config |
| 551 | mSize = mIntf->getSize_l(); |
| 552 | mBitrate = mIntf->getBitrate_l(); |
| 553 | mBitrateMode = mIntf->getBitrateMode_l(); |
| 554 | mFrameRate = mIntf->getFrameRate_l(); |
| 555 | mIntraRefresh = mIntf->getIntraRefresh_l(); |
| 556 | mRequestSync = mIntf->getRequestSync_l(); |
Aayush Soni | 643b918 | 2023-01-18 20:23:56 +0530 | [diff] [blame] | 557 | mColorAspects = mIntf->getCodedColorAspects_l(); |
Fyodor Kyslov | 58d8dea | 2023-02-10 02:15:54 +0000 | [diff] [blame] | 558 | mQuality = mIntf->getQuality_l(); |
Fyodor Kyslov | 3e3a92b | 2023-03-15 00:39:36 +0000 | [diff] [blame] | 559 | mComplexity = mIntf->getComplexity_l(); |
Harish Mahendrakar | 7d482b2 | 2023-08-21 20:08:01 -0700 | [diff] [blame] | 560 | mAV1EncLevel = mIntf->getLevel_l(); |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Fyodor Kyslov | d97e991 | 2022-11-07 19:23:21 +0000 | [diff] [blame] | 563 | |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 564 | switch (mBitrateMode->value) { |
| 565 | case C2Config::BITRATE_CONST: |
| 566 | mBitrateControlMode = AOM_CBR; |
| 567 | break; |
Fyodor Kyslov | 58d8dea | 2023-02-10 02:15:54 +0000 | [diff] [blame] | 568 | case C2Config::BITRATE_IGNORE: |
| 569 | mBitrateControlMode = AOM_Q; |
| 570 | break; |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 571 | case C2Config::BITRATE_VARIABLE: |
| 572 | [[fallthrough]]; |
| 573 | default: |
| 574 | mBitrateControlMode = AOM_VBR; |
| 575 | break; |
| 576 | } |
| 577 | |
| 578 | mCodecInterface = aom_codec_av1_cx(); |
| 579 | if (!mCodecInterface) goto CleanUp; |
| 580 | |
Fyodor Kyslov | 3e3a92b | 2023-03-15 00:39:36 +0000 | [diff] [blame] | 581 | ALOGD("AOM: initEncoder. BRMode: %u. KF: %u. QP: %u - %u, 10Bit: %d, comlexity %d", |
Fyodor Kyslov | d97e991 | 2022-11-07 19:23:21 +0000 | [diff] [blame] | 582 | (uint32_t)mBitrateControlMode, |
Fyodor Kyslov | 3e3a92b | 2023-03-15 00:39:36 +0000 | [diff] [blame] | 583 | mIntf->getSyncFramePeriod(), mMinQuantizer, mMaxQuantizer, mIs10Bit, mComplexity->value); |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 584 | |
| 585 | mCodecConfiguration = new aom_codec_enc_cfg_t; |
| 586 | if (!mCodecConfiguration) goto CleanUp; |
| 587 | |
| 588 | codec_return = aom_codec_enc_config_default(mCodecInterface, mCodecConfiguration, |
| 589 | AOM_USAGE_REALTIME); // RT mode |
| 590 | if (codec_return != AOM_CODEC_OK) { |
| 591 | ALOGE("Error populating default configuration for aom encoder."); |
| 592 | goto CleanUp; |
| 593 | } |
| 594 | |
| 595 | mCodecConfiguration->g_w = mSize->width; |
| 596 | mCodecConfiguration->g_h = mSize->height; |
Fyodor Kyslov | d97e991 | 2022-11-07 19:23:21 +0000 | [diff] [blame] | 597 | mCodecConfiguration->g_bit_depth = mIs10Bit ? AOM_BITS_10 : AOM_BITS_8; |
| 598 | mCodecConfiguration->g_input_bit_depth = mIs10Bit ? 10 : 8; |
| 599 | |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 600 | |
| 601 | mCodecConfiguration->g_threads = 0; |
| 602 | mCodecConfiguration->g_error_resilient = 0; |
| 603 | |
| 604 | // timebase unit is microsecond |
| 605 | // g_timebase is in seconds (i.e. 1/1000000 seconds) |
| 606 | mCodecConfiguration->g_timebase.num = 1; |
| 607 | mCodecConfiguration->g_timebase.den = 1000000; |
| 608 | // rc_target_bitrate is in kbps, mBitrate in bps |
| 609 | mCodecConfiguration->rc_target_bitrate = (mBitrate->value + 500) / 1000; |
Ram Mohan | 6c6901d | 2024-03-13 02:29:32 +0530 | [diff] [blame] | 610 | mCodecConfiguration->rc_end_usage = mBitrateControlMode; |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 611 | // Disable frame drop - not allowed in MediaCodec now. |
| 612 | mCodecConfiguration->rc_dropframe_thresh = 0; |
| 613 | // Disable lagged encoding. |
| 614 | mCodecConfiguration->g_lag_in_frames = 0; |
| 615 | |
| 616 | // Disable spatial resizing. |
| 617 | mCodecConfiguration->rc_resize_mode = 0; |
| 618 | // Single-pass mode. |
| 619 | mCodecConfiguration->g_pass = AOM_RC_ONE_PASS; |
| 620 | |
| 621 | // Maximum key frame interval - for CBR boost to 3000 |
| 622 | mCodecConfiguration->kf_max_dist = 3000; |
| 623 | // Encoder determines optimal key frame placement automatically. |
| 624 | mCodecConfiguration->kf_mode = AOM_KF_AUTO; |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 625 | // The amount of data that may be buffered by the decoding |
| 626 | // application in ms. |
| 627 | mCodecConfiguration->rc_buf_sz = 1000; |
| 628 | |
| 629 | if (mBitrateControlMode == AOM_CBR) { |
Fyodor Kyslov | 90d99d6 | 2023-05-02 23:28:45 +0000 | [diff] [blame] | 630 | // Initial value of the buffer level in ms. |
| 631 | mCodecConfiguration->rc_buf_initial_sz = 500; |
| 632 | // Amount of data that the encoder should try to maintain in ms. |
| 633 | mCodecConfiguration->rc_buf_optimal_sz = 600; |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 634 | // Maximum amount of bits that can be subtracted from the target |
| 635 | // bitrate - expressed as percentage of the target bitrate. |
| 636 | mCodecConfiguration->rc_undershoot_pct = 100; |
| 637 | // Maximum amount of bits that can be added to the target |
| 638 | // bitrate - expressed as percentage of the target bitrate. |
| 639 | mCodecConfiguration->rc_overshoot_pct = 10; |
| 640 | } else { |
| 641 | // Maximum amount of bits that can be subtracted from the target |
| 642 | // bitrate - expressed as percentage of the target bitrate. |
| 643 | mCodecConfiguration->rc_undershoot_pct = 100; |
| 644 | // Maximum amount of bits that can be added to the target |
| 645 | // bitrate - expressed as percentage of the target bitrate. |
Fyodor Kyslov | 90d99d6 | 2023-05-02 23:28:45 +0000 | [diff] [blame] | 646 | mCodecConfiguration->rc_overshoot_pct = 100; |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | if (mIntf->getSyncFramePeriod() >= 0) { |
| 650 | mCodecConfiguration->kf_max_dist = mIntf->getSyncFramePeriod(); |
| 651 | mCodecConfiguration->kf_min_dist = mIntf->getSyncFramePeriod(); |
| 652 | mCodecConfiguration->kf_mode = AOM_KF_AUTO; |
| 653 | } |
| 654 | if (mMinQuantizer > 0) { |
| 655 | mCodecConfiguration->rc_min_quantizer = mMinQuantizer; |
| 656 | } |
| 657 | if (mMaxQuantizer > 0) { |
| 658 | mCodecConfiguration->rc_max_quantizer = mMaxQuantizer; |
Fyodor Kyslov | 90d99d6 | 2023-05-02 23:28:45 +0000 | [diff] [blame] | 659 | } else { |
| 660 | if (mBitrateControlMode == AOM_VBR) { |
| 661 | // For VBR we are limiting MaxQP to 52 (down 11 steps) to maintain quality |
| 662 | // 52 comes from experiments done on libaom standalone app |
| 663 | mCodecConfiguration->rc_max_quantizer = 52; |
| 664 | } |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | mCodecContext = new aom_codec_ctx_t; |
| 668 | if (!mCodecContext) goto CleanUp; |
| 669 | codec_return = aom_codec_enc_init(mCodecContext, mCodecInterface, mCodecConfiguration, |
Fyodor Kyslov | d97e991 | 2022-11-07 19:23:21 +0000 | [diff] [blame] | 670 | mIs10Bit ? AOM_CODEC_USE_HIGHBITDEPTH : 0); |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 671 | if (codec_return != AOM_CODEC_OK) { |
| 672 | ALOGE("Error initializing aom encoder"); |
| 673 | goto CleanUp; |
| 674 | } |
| 675 | |
| 676 | codec_return = setupCodecParameters(); |
| 677 | if (codec_return != AOM_CODEC_OK) { |
| 678 | ALOGE("Error setting up codec parameters"); |
| 679 | goto CleanUp; |
| 680 | } |
| 681 | |
| 682 | mHeadersReceived = false; |
| 683 | |
| 684 | { |
| 685 | uint32_t width = mSize->width; |
| 686 | uint32_t height = mSize->height; |
| 687 | if (((uint64_t)width * height) > ((uint64_t)INT32_MAX / 3)) { |
| 688 | ALOGE("b/25812794, Buffer size is too big, width=%u, height=%u.", width, height); |
| 689 | } else { |
| 690 | uint32_t stride = (width + mStrideAlign - 1) & ~(mStrideAlign - 1); |
| 691 | uint32_t vstride = (height + mStrideAlign - 1) & ~(mStrideAlign - 1); |
Fyodor Kyslov | d97e991 | 2022-11-07 19:23:21 +0000 | [diff] [blame] | 692 | mConversionBuffer = MemoryBlock::Allocate(stride * vstride * 3 / (mIs10Bit? 1 : 2)); |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 693 | if (!mConversionBuffer.size()) { |
| 694 | ALOGE("Allocating conversion buffer failed."); |
| 695 | } else { |
| 696 | mNumInputFrames = -1; |
| 697 | return OK; |
| 698 | } |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | CleanUp: |
| 703 | onRelease(); |
| 704 | return result; |
| 705 | } |
| 706 | |
| 707 | void C2SoftAomEnc::process(const std::unique_ptr<C2Work>& work, |
| 708 | const std::shared_ptr<C2BlockPool>& pool) { |
| 709 | // Initialize output work |
| 710 | work->result = C2_OK; |
| 711 | work->workletsProcessed = 1u; |
| 712 | work->worklets.front()->output.flags = work->input.flags; |
| 713 | |
| 714 | if (mSignalledError || mSignalledOutputEos) { |
| 715 | work->result = C2_BAD_VALUE; |
| 716 | return; |
| 717 | } |
Fyodor Kyslov | d97e991 | 2022-11-07 19:23:21 +0000 | [diff] [blame] | 718 | |
Suyog Pawar | 0ed520b | 2023-05-12 14:16:24 +0000 | [diff] [blame] | 719 | std::shared_ptr<C2GraphicView> rView; |
Fyodor Kyslov | d97e991 | 2022-11-07 19:23:21 +0000 | [diff] [blame] | 720 | std::shared_ptr<C2Buffer> inputBuffer; |
| 721 | if (!work->input.buffers.empty()) { |
| 722 | inputBuffer = work->input.buffers[0]; |
Suyog Pawar | 0ed520b | 2023-05-12 14:16:24 +0000 | [diff] [blame] | 723 | rView = std::make_shared<C2GraphicView>( |
Fyodor Kyslov | d97e991 | 2022-11-07 19:23:21 +0000 | [diff] [blame] | 724 | inputBuffer->data().graphicBlocks().front().map().get()); |
| 725 | if (rView->error() != C2_OK) { |
| 726 | ALOGE("graphic view map err = %d", rView->error()); |
| 727 | work->result = C2_CORRUPTED; |
| 728 | return; |
| 729 | } |
| 730 | } else { |
| 731 | ALOGV("Empty input Buffer"); |
| 732 | uint32_t flags = 0; |
| 733 | if (work->input.flags & C2FrameData::FLAG_END_OF_STREAM) { |
| 734 | flags |= C2FrameData::FLAG_END_OF_STREAM; |
| 735 | } |
| 736 | work->worklets.front()->output.flags = (C2FrameData::flags_t)flags; |
| 737 | work->worklets.front()->output.buffers.clear(); |
| 738 | work->worklets.front()->output.ordinal = work->input.ordinal; |
| 739 | work->workletsProcessed = 1u; |
| 740 | return; |
| 741 | } |
| 742 | |
| 743 | bool end_of_stream = ((work->input.flags & C2FrameData::FLAG_END_OF_STREAM) != 0); |
| 744 | aom_image_t raw_frame; |
| 745 | const C2PlanarLayout& layout = rView->layout(); |
| 746 | if (!mHeadersReceived) { |
| 747 | mIs10Bit = (layout.planes[layout.PLANE_Y].bitDepth == 10); |
| 748 | |
| 749 | // Re-Initialize encoder |
| 750 | if (mCodecContext){ |
| 751 | onRelease(); |
| 752 | } |
| 753 | } |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 754 | if (!mCodecContext && OK != initEncoder()) { |
| 755 | ALOGE("Failed to initialize encoder"); |
| 756 | mSignalledError = true; |
| 757 | work->result = C2_CORRUPTED; |
| 758 | return; |
| 759 | } |
| 760 | |
Suyog Pawar | 0ed520b | 2023-05-12 14:16:24 +0000 | [diff] [blame] | 761 | //(b/279387842) |
| 762 | //workaround for incorrect crop size in view when using surface mode |
| 763 | rView->setCrop_be(C2Rect(mSize->width, mSize->height)); |
| 764 | |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 765 | if (!mHeadersReceived) { |
| 766 | Av1Config av1_config; |
| 767 | constexpr uint32_t header_length = 2048; |
| 768 | uint8_t header[header_length]; |
| 769 | size_t header_bytes; |
| 770 | aom_fixed_buf_t* obu_sequence_header = aom_codec_get_global_headers(mCodecContext); |
| 771 | int ret = 1; |
| 772 | if (obu_sequence_header) { |
| 773 | if (get_av1config_from_obu(reinterpret_cast<const uint8_t*>(obu_sequence_header->buf), |
| 774 | obu_sequence_header->sz, false, &av1_config) == 0) { |
| 775 | ret = write_av1config(&av1_config, header_length, &header_bytes, header); |
| 776 | |
| 777 | } else { |
| 778 | ALOGE("Can not get config"); |
| 779 | } |
| 780 | free(obu_sequence_header->buf); |
| 781 | free(obu_sequence_header); |
| 782 | } |
| 783 | |
| 784 | if (ret) { |
| 785 | ALOGE("Can not write config"); |
| 786 | mSignalledError = true; |
| 787 | work->result = C2_NO_MEMORY; |
| 788 | work->workletsProcessed = 1u; |
| 789 | return; |
| 790 | } |
| 791 | |
| 792 | mHeadersReceived = true; |
| 793 | std::unique_ptr<C2StreamInitDataInfo::output> csd = |
| 794 | C2StreamInitDataInfo::output::AllocUnique(header_bytes, 0u); |
| 795 | if (!csd) { |
| 796 | ALOGE("CSD allocation failed"); |
| 797 | mSignalledError = true; |
| 798 | work->result = C2_NO_MEMORY; |
| 799 | work->workletsProcessed = 1u; |
| 800 | return; |
| 801 | } |
| 802 | memcpy(csd->m.value, header, header_bytes); |
| 803 | work->worklets.front()->output.configUpdate.push_back(std::move(csd)); |
| 804 | ALOGV("CSD Produced of size %zu bytes", header_bytes); |
| 805 | } |
| 806 | |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 807 | const C2ConstGraphicBlock inBuffer = inputBuffer->data().graphicBlocks().front(); |
| 808 | if (inBuffer.width() < mSize->width || inBuffer.height() < mSize->height) { |
| 809 | ALOGE("unexpected Input buffer attributes %d(%d) x %d(%d)", inBuffer.width(), mSize->width, |
| 810 | inBuffer.height(), mSize->height); |
| 811 | mSignalledError = true; |
| 812 | work->result = C2_BAD_VALUE; |
| 813 | return; |
| 814 | } |
Fyodor Kyslov | d97e991 | 2022-11-07 19:23:21 +0000 | [diff] [blame] | 815 | |
| 816 | |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 817 | uint32_t width = mSize->width; |
| 818 | uint32_t height = mSize->height; |
| 819 | if (width > 0x8000 || height > 0x8000) { |
| 820 | ALOGE("Image too big: %u x %u", width, height); |
| 821 | work->result = C2_BAD_VALUE; |
| 822 | return; |
| 823 | } |
| 824 | uint32_t stride = (width + mStrideAlign - 1) & ~(mStrideAlign - 1); |
| 825 | uint32_t vstride = (height + mStrideAlign - 1) & ~(mStrideAlign - 1); |
| 826 | switch (layout.type) { |
| 827 | case C2PlanarLayout::TYPE_RGB: |
| 828 | case C2PlanarLayout::TYPE_RGBA: { |
| 829 | std::shared_ptr<C2StreamColorAspectsInfo::output> colorAspects; |
| 830 | { |
| 831 | IntfImpl::Lock lock = mIntf->lock(); |
| 832 | colorAspects = mIntf->getCodedColorAspects_l(); |
| 833 | } |
| 834 | ConvertRGBToPlanarYUV(mConversionBuffer.data(), stride, vstride, |
| 835 | mConversionBuffer.size(), *rView.get(), colorAspects->matrix, |
| 836 | colorAspects->range); |
| 837 | aom_img_wrap(&raw_frame, AOM_IMG_FMT_I420, width, height, mStrideAlign, |
| 838 | mConversionBuffer.data()); |
| 839 | break; |
| 840 | } |
| 841 | case C2PlanarLayout::TYPE_YUV: { |
Fyodor Kyslov | d97e991 | 2022-11-07 19:23:21 +0000 | [diff] [blame] | 842 | const bool isYUV420_10bit = IsYUV420_10bit(*rView); |
| 843 | if (!IsYUV420(*rView) && !isYUV420_10bit) { |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 844 | ALOGE("input is not YUV420"); |
| 845 | work->result = C2_BAD_VALUE; |
| 846 | return; |
| 847 | } |
Fyodor Kyslov | d97e991 | 2022-11-07 19:23:21 +0000 | [diff] [blame] | 848 | if (!isYUV420_10bit) { |
| 849 | if (IsI420(*rView)) { |
| 850 | // I420 compatible - though with custom offset and stride |
| 851 | aom_img_wrap(&raw_frame, AOM_IMG_FMT_I420, width, height, mStrideAlign, |
| 852 | (uint8_t*)rView->data()[0]); |
| 853 | raw_frame.planes[1] = (uint8_t*)rView->data()[1]; |
| 854 | raw_frame.planes[2] = (uint8_t*)rView->data()[2]; |
| 855 | raw_frame.stride[0] = layout.planes[layout.PLANE_Y].rowInc; |
| 856 | raw_frame.stride[1] = layout.planes[layout.PLANE_U].rowInc; |
| 857 | raw_frame.stride[2] = layout.planes[layout.PLANE_V].rowInc; |
| 858 | } else { |
| 859 | // TODO(kyslov): Add image wrap for NV12 |
| 860 | // copy to I420 |
| 861 | MediaImage2 img = CreateYUV420PlanarMediaImage2(width, height, stride, vstride); |
| 862 | if (mConversionBuffer.size() >= stride * vstride * 3 / 2) { |
| 863 | status_t err = ImageCopy(mConversionBuffer.data(), &img, *rView); |
| 864 | if (err != OK) { |
| 865 | ALOGE("Buffer conversion failed: %d", err); |
| 866 | work->result = C2_BAD_VALUE; |
| 867 | return; |
| 868 | } |
| 869 | aom_img_wrap(&raw_frame, AOM_IMG_FMT_I420, stride, vstride, mStrideAlign, |
| 870 | mConversionBuffer.data()); |
| 871 | aom_img_set_rect(&raw_frame, 0, 0, width, height, 0); |
| 872 | } else { |
| 873 | ALOGE("Conversion buffer is too small: %u x %u for %zu", stride, vstride, |
| 874 | mConversionBuffer.size()); |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 875 | work->result = C2_BAD_VALUE; |
| 876 | return; |
| 877 | } |
Fyodor Kyslov | d97e991 | 2022-11-07 19:23:21 +0000 | [diff] [blame] | 878 | } |
| 879 | } else { // 10 bits |
| 880 | if (IsP010(*rView)) { |
| 881 | if (mConversionBuffer.size() >= stride * vstride * 3) { |
| 882 | uint16_t *dstY, *dstU, *dstV; |
| 883 | dstY = (uint16_t*)mConversionBuffer.data(); |
Aayush Soni | 698e2f2 | 2023-02-18 01:10:53 +0530 | [diff] [blame] | 884 | dstU = dstY + stride * vstride; |
| 885 | dstV = dstU + (stride * vstride) / 4; |
Fyodor Kyslov | d97e991 | 2022-11-07 19:23:21 +0000 | [diff] [blame] | 886 | convertP010ToYUV420Planar16(dstY, dstU, dstV, (uint16_t*)(rView->data()[0]), |
Aayush Soni | 698e2f2 | 2023-02-18 01:10:53 +0530 | [diff] [blame] | 887 | (uint16_t*)(rView->data()[1]), |
| 888 | layout.planes[layout.PLANE_Y].rowInc / 2, |
| 889 | layout.planes[layout.PLANE_U].rowInc / 2, |
Fyodor Kyslov | d97e991 | 2022-11-07 19:23:21 +0000 | [diff] [blame] | 890 | stride, stride / 2, stride / 2, stride, |
| 891 | vstride); |
| 892 | aom_img_wrap(&raw_frame, AOM_IMG_FMT_I42016, stride, vstride, mStrideAlign, |
| 893 | mConversionBuffer.data()); |
| 894 | aom_img_set_rect(&raw_frame, 0, 0, width, height, 0); |
| 895 | } else { |
| 896 | ALOGE("Conversion buffer is too small: %u x %u for %zu", stride, vstride, |
| 897 | mConversionBuffer.size()); |
| 898 | work->result = C2_BAD_VALUE; |
| 899 | return; |
| 900 | } |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 901 | } else { |
Fyodor Kyslov | d97e991 | 2022-11-07 19:23:21 +0000 | [diff] [blame] | 902 | ALOGE("Image format conversion is not supported."); |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 903 | work->result = C2_BAD_VALUE; |
| 904 | return; |
| 905 | } |
| 906 | } |
| 907 | break; |
| 908 | } |
Aayush Soni | 5e82a6c | 2023-02-27 18:34:23 +0530 | [diff] [blame] | 909 | case C2PlanarLayout::TYPE_YUVA: { |
| 910 | if (mConversionBuffer.size() >= stride * vstride * 3) { |
| 911 | uint16_t *dstY, *dstU, *dstV; |
| 912 | dstY = (uint16_t*)mConversionBuffer.data(); |
| 913 | dstU = dstY + stride * vstride; |
| 914 | dstV = dstU + (stride * vstride) / 4; |
| 915 | convertRGBA1010102ToYUV420Planar16(dstY, dstU, dstV, (uint32_t*)(rView->data()[0]), |
| 916 | layout.planes[layout.PLANE_Y].rowInc / 4, stride, |
| 917 | vstride, mColorAspects->matrix, |
| 918 | mColorAspects->range); |
| 919 | aom_img_wrap(&raw_frame, AOM_IMG_FMT_I42016, stride, vstride, mStrideAlign, |
| 920 | mConversionBuffer.data()); |
| 921 | aom_img_set_rect(&raw_frame, 0, 0, width, height, 0); |
| 922 | } else { |
| 923 | ALOGE("Conversion buffer is too small: %u x %u for %zu", stride, vstride, |
| 924 | mConversionBuffer.size()); |
| 925 | work->result = C2_BAD_VALUE; |
| 926 | return; |
| 927 | } |
| 928 | break; |
| 929 | } |
| 930 | |
Fyodor Kyslov | adc7114 | 2022-09-15 16:47:03 +0000 | [diff] [blame] | 931 | default: |
| 932 | ALOGE("Unrecognized plane type: %d", layout.type); |
| 933 | work->result = C2_BAD_VALUE; |
| 934 | return; |
| 935 | } |
| 936 | |
| 937 | aom_enc_frame_flags_t flags = 0; |
| 938 | // handle dynamic config parameters |
| 939 | { |
| 940 | IntfImpl::Lock lock = mIntf->lock(); |
| 941 | std::shared_ptr<C2StreamIntraRefreshTuning::output> intraRefresh = |
| 942 | mIntf->getIntraRefresh_l(); |
| 943 | std::shared_ptr<C2StreamBitrateInfo::output> bitrate = mIntf->getBitrate_l(); |
| 944 | std::shared_ptr<C2StreamRequestSyncFrameTuning::output> requestSync = |
| 945 | mIntf->getRequestSync_l(); |
| 946 | lock.unlock(); |
| 947 | |
| 948 | if (intraRefresh != mIntraRefresh) { |
| 949 | mIntraRefresh = intraRefresh; |
| 950 | ALOGV("Got mIntraRefresh request"); |
| 951 | } |
| 952 | |
| 953 | if (requestSync != mRequestSync) { |
| 954 | // we can handle IDR immediately |
| 955 | if (requestSync->value) { |
| 956 | // unset request |
| 957 | C2StreamRequestSyncFrameTuning::output clearSync(0u, C2_FALSE); |
| 958 | std::vector<std::unique_ptr<C2SettingResult>> failures; |
| 959 | mIntf->config({&clearSync}, C2_MAY_BLOCK, &failures); |
| 960 | ALOGV("Got sync request"); |
| 961 | flags |= AOM_EFLAG_FORCE_KF; |
| 962 | } |
| 963 | mRequestSync = requestSync; |
| 964 | } |
| 965 | |
| 966 | if (bitrate != mBitrate) { |
| 967 | mBitrate = bitrate; |
| 968 | mCodecConfiguration->rc_target_bitrate = (mBitrate->value + 500) / 1000; |
| 969 | aom_codec_err_t res = aom_codec_enc_config_set(mCodecContext, mCodecConfiguration); |
| 970 | if (res != AOM_CODEC_OK) { |
| 971 | ALOGE("aom encoder failed to update bitrate: %s", aom_codec_err_to_string(res)); |
| 972 | mSignalledError = true; |
| 973 | work->result = C2_CORRUPTED; |
| 974 | return; |
| 975 | } |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | uint64_t input_timestamp = work->input.ordinal.timestamp.peekull(); |
| 980 | uint32_t frame_duration; |
| 981 | if (input_timestamp > mLastTimestamp) { |
| 982 | frame_duration = (uint32_t)(input_timestamp - mLastTimestamp); |
| 983 | } else { |
| 984 | // Use default of 30 fps in case of 0 frame rate. |
| 985 | float frame_rate = mFrameRate->value; |
| 986 | if (frame_rate < 0.001) { |
| 987 | frame_rate = 30.0; |
| 988 | } |
| 989 | frame_duration = (uint32_t)(1000000 / frame_rate + 0.5); |
| 990 | } |
| 991 | mLastTimestamp = input_timestamp; |
| 992 | |
| 993 | aom_codec_err_t codec_return = |
| 994 | aom_codec_encode(mCodecContext, &raw_frame, input_timestamp, frame_duration, flags); |
| 995 | if (codec_return != AOM_CODEC_OK) { |
| 996 | ALOGE("aom encoder failed to encode frame"); |
| 997 | mSignalledError = true; |
| 998 | work->result = C2_CORRUPTED; |
| 999 | return; |
| 1000 | } |
| 1001 | |
| 1002 | bool populated = false; |
| 1003 | aom_codec_iter_t encoded_packet_iterator = nullptr; |
| 1004 | const aom_codec_cx_pkt_t* encoded_packet; |
| 1005 | while ((encoded_packet = aom_codec_get_cx_data(mCodecContext, &encoded_packet_iterator))) { |
| 1006 | if (encoded_packet->kind == AOM_CODEC_CX_FRAME_PKT) { |
| 1007 | std::shared_ptr<C2LinearBlock> block; |
| 1008 | C2MemoryUsage usage = {C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE}; |
| 1009 | c2_status_t err = pool->fetchLinearBlock(encoded_packet->data.frame.sz, usage, &block); |
| 1010 | if (err != C2_OK) { |
| 1011 | ALOGE("fetchLinearBlock for Output failed with status %d", err); |
| 1012 | work->result = C2_NO_MEMORY; |
| 1013 | return; |
| 1014 | } |
| 1015 | C2WriteView wView = block->map().get(); |
| 1016 | if (wView.error()) { |
| 1017 | ALOGE("write view map failed %d", wView.error()); |
| 1018 | work->result = C2_CORRUPTED; |
| 1019 | return; |
| 1020 | } |
| 1021 | |
| 1022 | memcpy(wView.data(), encoded_packet->data.frame.buf, encoded_packet->data.frame.sz); |
| 1023 | ++mNumInputFrames; |
| 1024 | |
| 1025 | ALOGD("bytes generated %zu", encoded_packet->data.frame.sz); |
| 1026 | uint32_t flags = 0; |
| 1027 | if (end_of_stream) { |
| 1028 | flags |= C2FrameData::FLAG_END_OF_STREAM; |
| 1029 | } |
| 1030 | |
| 1031 | work->worklets.front()->output.flags = (C2FrameData::flags_t)flags; |
| 1032 | work->worklets.front()->output.buffers.clear(); |
| 1033 | std::shared_ptr<C2Buffer> buffer = |
| 1034 | createLinearBuffer(block, 0, encoded_packet->data.frame.sz); |
| 1035 | if (encoded_packet->data.frame.flags & AOM_FRAME_IS_KEY) { |
| 1036 | buffer->setInfo(std::make_shared<C2StreamPictureTypeMaskInfo::output>( |
| 1037 | 0u /* stream id */, C2Config::SYNC_FRAME)); |
| 1038 | } |
| 1039 | work->worklets.front()->output.buffers.push_back(buffer); |
| 1040 | work->worklets.front()->output.ordinal = work->input.ordinal; |
| 1041 | work->worklets.front()->output.ordinal.timestamp = encoded_packet->data.frame.pts; |
| 1042 | work->workletsProcessed = 1u; |
| 1043 | populated = true; |
| 1044 | if (end_of_stream) { |
| 1045 | mSignalledOutputEos = true; |
| 1046 | ALOGV("signalled End Of Stream"); |
| 1047 | } |
| 1048 | } |
| 1049 | } |
| 1050 | if (!populated) { |
| 1051 | work->workletsProcessed = 0u; |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | c2_status_t C2SoftAomEnc::drain(uint32_t drainMode, const std::shared_ptr<C2BlockPool>& pool) { |
| 1056 | (void)pool; |
| 1057 | if (drainMode == NO_DRAIN) { |
| 1058 | ALOGW("drain with NO_DRAIN: no-op"); |
| 1059 | return C2_OK; |
| 1060 | } |
| 1061 | if (drainMode == DRAIN_CHAIN) { |
| 1062 | ALOGW("DRAIN_CHAIN not supported"); |
| 1063 | return C2_OMITTED; |
| 1064 | } |
| 1065 | |
| 1066 | return C2_OK; |
| 1067 | } |
| 1068 | |
| 1069 | class C2SoftAomEncFactory : public C2ComponentFactory { |
| 1070 | public: |
| 1071 | C2SoftAomEncFactory() |
| 1072 | : mHelper(std::static_pointer_cast<C2ReflectorHelper>( |
| 1073 | GetCodec2PlatformComponentStore()->getParamReflector())) {} |
| 1074 | |
| 1075 | virtual c2_status_t createComponent(c2_node_id_t id, |
| 1076 | std::shared_ptr<C2Component>* const component, |
| 1077 | std::function<void(C2Component*)> deleter) override { |
| 1078 | *component = std::shared_ptr<C2Component>( |
| 1079 | new C2SoftAomEnc(COMPONENT_NAME, id, |
| 1080 | std::make_shared<C2SoftAomEnc::IntfImpl>(mHelper)), |
| 1081 | deleter); |
| 1082 | return C2_OK; |
| 1083 | } |
| 1084 | |
| 1085 | virtual c2_status_t createInterface( |
| 1086 | c2_node_id_t id, std::shared_ptr<C2ComponentInterface>* const interface, |
| 1087 | std::function<void(C2ComponentInterface*)> deleter) override { |
| 1088 | *interface = std::shared_ptr<C2ComponentInterface>( |
| 1089 | new SimpleInterface<C2SoftAomEnc::IntfImpl>( |
| 1090 | COMPONENT_NAME, id, std::make_shared<C2SoftAomEnc::IntfImpl>(mHelper)), |
| 1091 | deleter); |
| 1092 | return C2_OK; |
| 1093 | } |
| 1094 | |
| 1095 | virtual ~C2SoftAomEncFactory() override = default; |
| 1096 | |
| 1097 | private: |
| 1098 | std::shared_ptr<C2ReflectorHelper> mHelper; |
| 1099 | }; |
| 1100 | |
| 1101 | } // namespace android |
| 1102 | |
| 1103 | __attribute__((cfi_canonical_jump_table)) extern "C" ::C2ComponentFactory* CreateCodec2Factory() { |
| 1104 | ALOGV("in %s", __func__); |
| 1105 | return new ::android::C2SoftAomEncFactory(); |
| 1106 | } |
| 1107 | |
| 1108 | __attribute__((cfi_canonical_jump_table)) extern "C" void DestroyCodec2Factory( |
| 1109 | ::C2ComponentFactory* factory) { |
| 1110 | ALOGV("in %s", __func__); |
| 1111 | delete factory; |
| 1112 | } |