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