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