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