Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 1 | /* |
Ray Essick | 0d11a7e | 2019-03-02 20:10:30 -0800 | [diff] [blame] | 2 | * Copyright (C) 2019 The Android Open Source Project |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 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 "C2SoftHevcEnc" |
| 19 | #include <log/log.h> |
| 20 | |
| 21 | #include <media/hardware/VideoAPI.h> |
| 22 | #include <media/stagefright/MediaDefs.h> |
| 23 | #include <media/stagefright/MediaErrors.h> |
| 24 | #include <media/stagefright/MetaData.h> |
| 25 | #include <media/stagefright/foundation/AUtils.h> |
| 26 | |
| 27 | #include <C2Debug.h> |
| 28 | #include <C2PlatformSupport.h> |
| 29 | #include <Codec2BufferUtils.h> |
| 30 | #include <SimpleC2Interface.h> |
| 31 | #include <util/C2InterfaceHelper.h> |
| 32 | |
| 33 | #include "ihevc_typedefs.h" |
| 34 | #include "itt_video_api.h" |
| 35 | #include "ihevce_api.h" |
| 36 | #include "ihevce_plugin.h" |
| 37 | #include "C2SoftHevcEnc.h" |
| 38 | |
| 39 | namespace android { |
| 40 | |
Manisha Jajoo | 9acd73d | 2019-03-12 14:08:05 +0530 | [diff] [blame] | 41 | namespace { |
| 42 | |
| 43 | constexpr char COMPONENT_NAME[] = "c2.android.hevc.encoder"; |
| 44 | |
| 45 | } // namepsace |
| 46 | |
| 47 | class C2SoftHevcEnc::IntfImpl : public SimpleInterface<void>::BaseParams { |
Ray Essick | 0d11a7e | 2019-03-02 20:10:30 -0800 | [diff] [blame] | 48 | public: |
Manisha Jajoo | 9acd73d | 2019-03-12 14:08:05 +0530 | [diff] [blame] | 49 | explicit IntfImpl(const std::shared_ptr<C2ReflectorHelper> &helper) |
| 50 | : SimpleInterface<void>::BaseParams( |
| 51 | helper, |
| 52 | COMPONENT_NAME, |
| 53 | C2Component::KIND_ENCODER, |
| 54 | C2Component::DOMAIN_VIDEO, |
| 55 | MEDIA_MIMETYPE_VIDEO_HEVC) { |
| 56 | noPrivateBuffers(); // TODO: account for our buffers here |
| 57 | noInputReferences(); |
| 58 | noOutputReferences(); |
Manisha Jajoo | 9acd73d | 2019-03-12 14:08:05 +0530 | [diff] [blame] | 59 | noTimeStretch(); |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 60 | setDerivedInstance(this); |
| 61 | |
| 62 | addParameter( |
Manisha Jajoo | 863bcfc | 2019-03-22 16:32:55 +0530 | [diff] [blame^] | 63 | DefineParam(mActualInputDelay, C2_PARAMKEY_INPUT_DELAY) |
| 64 | .withDefault(new C2PortActualDelayTuning::input( |
| 65 | DEFAULT_B_FRAMES + DEFAULT_RC_LOOKAHEAD)) |
| 66 | .withFields({C2F(mActualInputDelay, value).inRange( |
| 67 | 0, MAX_B_FRAMES + MAX_RC_LOOKAHEAD)}) |
| 68 | .withSetter( |
| 69 | Setter<decltype(*mActualInputDelay)>::StrictValueWithNoDeps) |
| 70 | .build()); |
| 71 | |
| 72 | addParameter( |
Manisha Jajoo | 9acd73d | 2019-03-12 14:08:05 +0530 | [diff] [blame] | 73 | DefineParam(mAttrib, C2_PARAMKEY_COMPONENT_ATTRIBUTES) |
| 74 | .withConstValue(new C2ComponentAttributesSetting( |
| 75 | C2Component::ATTRIB_IS_TEMPORAL)) |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 76 | .build()); |
| 77 | |
| 78 | addParameter( |
Manisha Jajoo | 9acd73d | 2019-03-12 14:08:05 +0530 | [diff] [blame] | 79 | DefineParam(mUsage, C2_PARAMKEY_INPUT_STREAM_USAGE) |
| 80 | .withConstValue(new C2StreamUsageTuning::input( |
| 81 | 0u, (uint64_t)C2MemoryUsage::CPU_READ)) |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 82 | .build()); |
| 83 | |
Ray Essick | 0d11a7e | 2019-03-02 20:10:30 -0800 | [diff] [blame] | 84 | // matches size limits in codec library |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 85 | addParameter( |
Lajos Molnar | 3bb81cd | 2019-02-20 15:10:30 -0800 | [diff] [blame] | 86 | DefineParam(mSize, C2_PARAMKEY_PICTURE_SIZE) |
| 87 | .withDefault(new C2StreamPictureSizeInfo::input(0u, 320, 240)) |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 88 | .withFields({ |
| 89 | C2F(mSize, width).inRange(320, 1920, 2), |
| 90 | C2F(mSize, height).inRange(128, 1088, 2), |
| 91 | }) |
| 92 | .withSetter(SizeSetter) |
| 93 | .build()); |
| 94 | |
| 95 | addParameter( |
Lajos Molnar | 3bb81cd | 2019-02-20 15:10:30 -0800 | [diff] [blame] | 96 | DefineParam(mFrameRate, C2_PARAMKEY_FRAME_RATE) |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 97 | .withDefault(new C2StreamFrameRateInfo::output(0u, 30.)) |
| 98 | .withFields({C2F(mFrameRate, value).greaterThan(0.)}) |
| 99 | .withSetter( |
| 100 | Setter<decltype(*mFrameRate)>::StrictValueWithNoDeps) |
| 101 | .build()); |
| 102 | |
Ray Essick | 0d11a7e | 2019-03-02 20:10:30 -0800 | [diff] [blame] | 103 | // matches limits in codec library |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 104 | addParameter( |
Harish Mahendrakar | 57d7cc9 | 2019-02-05 08:34:55 -0800 | [diff] [blame] | 105 | DefineParam(mBitrateMode, C2_PARAMKEY_BITRATE_MODE) |
| 106 | .withDefault(new C2StreamBitrateModeTuning::output( |
| 107 | 0u, C2Config::BITRATE_VARIABLE)) |
| 108 | .withFields({ |
| 109 | C2F(mBitrateMode, value).oneOf({ |
| 110 | C2Config::BITRATE_CONST, |
| 111 | C2Config::BITRATE_VARIABLE, |
| 112 | C2Config::BITRATE_IGNORE}) |
| 113 | }) |
| 114 | .withSetter( |
| 115 | Setter<decltype(*mBitrateMode)>::StrictValueWithNoDeps) |
| 116 | .build()); |
| 117 | |
| 118 | addParameter( |
Lajos Molnar | 3bb81cd | 2019-02-20 15:10:30 -0800 | [diff] [blame] | 119 | DefineParam(mBitrate, C2_PARAMKEY_BITRATE) |
| 120 | .withDefault(new C2StreamBitrateInfo::output(0u, 64000)) |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 121 | .withFields({C2F(mBitrate, value).inRange(4096, 12000000)}) |
| 122 | .withSetter(BitrateSetter) |
| 123 | .build()); |
| 124 | |
Ray Essick | 0d11a7e | 2019-03-02 20:10:30 -0800 | [diff] [blame] | 125 | // matches levels allowed within codec library |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 126 | addParameter( |
Harish Mahendrakar | 57d7cc9 | 2019-02-05 08:34:55 -0800 | [diff] [blame] | 127 | DefineParam(mComplexity, C2_PARAMKEY_COMPLEXITY) |
| 128 | .withDefault(new C2StreamComplexityTuning::output(0u, 0)) |
| 129 | .withFields({C2F(mComplexity, value).inRange(0, 10)}) |
| 130 | .withSetter(Setter<decltype(*mComplexity)>::NonStrictValueWithNoDeps) |
| 131 | .build()); |
| 132 | |
| 133 | addParameter( |
| 134 | DefineParam(mQuality, C2_PARAMKEY_QUALITY) |
| 135 | .withDefault(new C2StreamQualityTuning::output(0u, 80)) |
| 136 | .withFields({C2F(mQuality, value).inRange(0, 100)}) |
| 137 | .withSetter(Setter<decltype(*mQuality)>::NonStrictValueWithNoDeps) |
| 138 | .build()); |
| 139 | |
| 140 | addParameter( |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 141 | DefineParam(mProfileLevel, C2_PARAMKEY_PROFILE_LEVEL) |
| 142 | .withDefault(new C2StreamProfileLevelInfo::output( |
| 143 | 0u, PROFILE_HEVC_MAIN, LEVEL_HEVC_MAIN_1)) |
| 144 | .withFields({ |
| 145 | C2F(mProfileLevel, profile) |
| 146 | .oneOf({C2Config::PROFILE_HEVC_MAIN, |
| 147 | C2Config::PROFILE_HEVC_MAIN_STILL}), |
| 148 | C2F(mProfileLevel, level) |
| 149 | .oneOf({LEVEL_HEVC_MAIN_1, LEVEL_HEVC_MAIN_2, |
| 150 | LEVEL_HEVC_MAIN_2_1, LEVEL_HEVC_MAIN_3, |
| 151 | LEVEL_HEVC_MAIN_3_1, LEVEL_HEVC_MAIN_4, |
| 152 | LEVEL_HEVC_MAIN_4_1, LEVEL_HEVC_MAIN_5, |
| 153 | LEVEL_HEVC_MAIN_5_1, LEVEL_HEVC_MAIN_5_2}), |
| 154 | }) |
| 155 | .withSetter(ProfileLevelSetter, mSize, mFrameRate, mBitrate) |
| 156 | .build()); |
| 157 | |
| 158 | addParameter( |
| 159 | DefineParam(mRequestSync, C2_PARAMKEY_REQUEST_SYNC_FRAME) |
| 160 | .withDefault(new C2StreamRequestSyncFrameTuning::output(0u, C2_FALSE)) |
| 161 | .withFields({C2F(mRequestSync, value).oneOf({ C2_FALSE, C2_TRUE }) }) |
| 162 | .withSetter(Setter<decltype(*mRequestSync)>::NonStrictValueWithNoDeps) |
| 163 | .build()); |
| 164 | |
| 165 | addParameter( |
| 166 | DefineParam(mSyncFramePeriod, C2_PARAMKEY_SYNC_FRAME_INTERVAL) |
| 167 | .withDefault( |
| 168 | new C2StreamSyncFrameIntervalTuning::output(0u, 1000000)) |
| 169 | .withFields({C2F(mSyncFramePeriod, value).any()}) |
| 170 | .withSetter( |
| 171 | Setter<decltype(*mSyncFramePeriod)>::StrictValueWithNoDeps) |
| 172 | .build()); |
| 173 | } |
| 174 | |
| 175 | static C2R BitrateSetter(bool mayBlock, |
| 176 | C2P<C2StreamBitrateInfo::output>& me) { |
| 177 | (void)mayBlock; |
| 178 | C2R res = C2R::Ok(); |
Ray Essick | 0d11a7e | 2019-03-02 20:10:30 -0800 | [diff] [blame] | 179 | if (me.v.value < 4096) { |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 180 | me.set().value = 4096; |
| 181 | } |
| 182 | return res; |
| 183 | } |
| 184 | |
| 185 | static C2R SizeSetter(bool mayBlock, |
| 186 | const C2P<C2StreamPictureSizeInfo::input>& oldMe, |
| 187 | C2P<C2StreamPictureSizeInfo::input>& me) { |
| 188 | (void)mayBlock; |
| 189 | C2R res = C2R::Ok(); |
| 190 | if (!me.F(me.v.width).supportsAtAll(me.v.width)) { |
| 191 | res = res.plus(C2SettingResultBuilder::BadValue(me.F(me.v.width))); |
| 192 | me.set().width = oldMe.v.width; |
| 193 | } |
| 194 | if (!me.F(me.v.height).supportsAtAll(me.v.height)) { |
| 195 | res = res.plus(C2SettingResultBuilder::BadValue(me.F(me.v.height))); |
| 196 | me.set().height = oldMe.v.height; |
| 197 | } |
| 198 | return res; |
| 199 | } |
| 200 | |
| 201 | static C2R ProfileLevelSetter( |
| 202 | bool mayBlock, |
| 203 | C2P<C2StreamProfileLevelInfo::output> &me, |
Lajos Molnar | 3bb81cd | 2019-02-20 15:10:30 -0800 | [diff] [blame] | 204 | const C2P<C2StreamPictureSizeInfo::input> &size, |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 205 | const C2P<C2StreamFrameRateInfo::output> &frameRate, |
Lajos Molnar | 3bb81cd | 2019-02-20 15:10:30 -0800 | [diff] [blame] | 206 | const C2P<C2StreamBitrateInfo::output> &bitrate) { |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 207 | (void)mayBlock; |
| 208 | if (!me.F(me.v.profile).supportsAtAll(me.v.profile)) { |
| 209 | me.set().profile = PROFILE_HEVC_MAIN; |
| 210 | } |
| 211 | |
| 212 | struct LevelLimits { |
| 213 | C2Config::level_t level; |
| 214 | uint64_t samplesPerSec; |
| 215 | uint64_t samples; |
| 216 | uint32_t bitrate; |
| 217 | }; |
| 218 | |
| 219 | constexpr LevelLimits kLimits[] = { |
| 220 | { LEVEL_HEVC_MAIN_1, 552960, 36864, 128000 }, |
| 221 | { LEVEL_HEVC_MAIN_2, 3686400, 122880, 1500000 }, |
| 222 | { LEVEL_HEVC_MAIN_2_1, 7372800, 245760, 3000000 }, |
| 223 | { LEVEL_HEVC_MAIN_3, 16588800, 552960, 6000000 }, |
| 224 | { LEVEL_HEVC_MAIN_3_1, 33177600, 983040, 10000000 }, |
| 225 | { LEVEL_HEVC_MAIN_4, 66846720, 2228224, 12000000 }, |
| 226 | { LEVEL_HEVC_MAIN_4_1, 133693440, 2228224, 20000000 }, |
| 227 | { LEVEL_HEVC_MAIN_5, 267386880, 8912896, 25000000 }, |
| 228 | { LEVEL_HEVC_MAIN_5_1, 534773760, 8912896, 40000000 }, |
| 229 | { LEVEL_HEVC_MAIN_5_2, 1069547520, 8912896, 60000000 }, |
| 230 | { LEVEL_HEVC_MAIN_6, 1069547520, 35651584, 60000000 }, |
| 231 | { LEVEL_HEVC_MAIN_6_1, 2139095040, 35651584, 120000000 }, |
| 232 | { LEVEL_HEVC_MAIN_6_2, 4278190080, 35651584, 240000000 }, |
| 233 | }; |
| 234 | |
| 235 | uint64_t samples = size.v.width * size.v.height; |
| 236 | uint64_t samplesPerSec = samples * frameRate.v.value; |
| 237 | |
| 238 | // Check if the supplied level meets the MB / bitrate requirements. If |
| 239 | // not, update the level with the lowest level meeting the requirements. |
| 240 | |
| 241 | bool found = false; |
| 242 | // By default needsUpdate = false in case the supplied level does meet |
| 243 | // the requirements. |
| 244 | bool needsUpdate = false; |
| 245 | for (const LevelLimits &limit : kLimits) { |
| 246 | if (samples <= limit.samples && samplesPerSec <= limit.samplesPerSec && |
| 247 | bitrate.v.value <= limit.bitrate) { |
| 248 | // This is the lowest level that meets the requirements, and if |
| 249 | // we haven't seen the supplied level yet, that means we don't |
| 250 | // need the update. |
| 251 | if (needsUpdate) { |
| 252 | ALOGD("Given level %x does not cover current configuration: " |
| 253 | "adjusting to %x", me.v.level, limit.level); |
| 254 | me.set().level = limit.level; |
| 255 | } |
| 256 | found = true; |
| 257 | break; |
| 258 | } |
| 259 | if (me.v.level == limit.level) { |
| 260 | // We break out of the loop when the lowest feasible level is |
| 261 | // found. The fact that we're here means that our level doesn't |
| 262 | // meet the requirement and needs to be updated. |
| 263 | needsUpdate = true; |
| 264 | } |
| 265 | } |
| 266 | if (!found) { |
| 267 | // We set to the highest supported level. |
| 268 | me.set().level = LEVEL_HEVC_MAIN_5_2; |
| 269 | } |
| 270 | return C2R::Ok(); |
| 271 | } |
| 272 | |
| 273 | UWORD32 getProfile_l() const { |
| 274 | switch (mProfileLevel->profile) { |
| 275 | case PROFILE_HEVC_MAIN: [[fallthrough]]; |
| 276 | case PROFILE_HEVC_MAIN_STILL: return 1; |
| 277 | default: |
| 278 | ALOGD("Unrecognized profile: %x", mProfileLevel->profile); |
| 279 | return 1; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | UWORD32 getLevel_l() const { |
| 284 | struct Level { |
| 285 | C2Config::level_t c2Level; |
| 286 | UWORD32 hevcLevel; |
| 287 | }; |
| 288 | constexpr Level levels[] = { |
| 289 | { LEVEL_HEVC_MAIN_1, 30 }, |
| 290 | { LEVEL_HEVC_MAIN_2, 60 }, |
| 291 | { LEVEL_HEVC_MAIN_2_1, 63 }, |
| 292 | { LEVEL_HEVC_MAIN_3, 90 }, |
| 293 | { LEVEL_HEVC_MAIN_3_1, 93 }, |
| 294 | { LEVEL_HEVC_MAIN_4, 120 }, |
| 295 | { LEVEL_HEVC_MAIN_4_1, 123 }, |
| 296 | { LEVEL_HEVC_MAIN_5, 150 }, |
| 297 | { LEVEL_HEVC_MAIN_5_1, 153 }, |
| 298 | { LEVEL_HEVC_MAIN_5_2, 156 }, |
| 299 | { LEVEL_HEVC_MAIN_6, 180 }, |
| 300 | { LEVEL_HEVC_MAIN_6_1, 183 }, |
| 301 | { LEVEL_HEVC_MAIN_6_2, 186 }, |
| 302 | }; |
| 303 | for (const Level &level : levels) { |
| 304 | if (mProfileLevel->level == level.c2Level) { |
| 305 | return level.hevcLevel; |
| 306 | } |
| 307 | } |
| 308 | ALOGD("Unrecognized level: %x", mProfileLevel->level); |
| 309 | return 156; |
| 310 | } |
| 311 | uint32_t getSyncFramePeriod_l() const { |
| 312 | if (mSyncFramePeriod->value < 0 || |
| 313 | mSyncFramePeriod->value == INT64_MAX) { |
| 314 | return 0; |
| 315 | } |
| 316 | double period = mSyncFramePeriod->value / 1e6 * mFrameRate->value; |
| 317 | return (uint32_t)c2_max(c2_min(period + 0.5, double(UINT32_MAX)), 1.); |
| 318 | } |
| 319 | |
Ray Essick | 0d11a7e | 2019-03-02 20:10:30 -0800 | [diff] [blame] | 320 | std::shared_ptr<C2StreamPictureSizeInfo::input> getSize_l() const { |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 321 | return mSize; |
| 322 | } |
| 323 | std::shared_ptr<C2StreamFrameRateInfo::output> getFrameRate_l() const { |
| 324 | return mFrameRate; |
| 325 | } |
Harish Mahendrakar | 57d7cc9 | 2019-02-05 08:34:55 -0800 | [diff] [blame] | 326 | std::shared_ptr<C2StreamBitrateModeTuning::output> getBitrateMode_l() const { |
| 327 | return mBitrateMode; |
| 328 | } |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 329 | std::shared_ptr<C2StreamBitrateInfo::output> getBitrate_l() const { |
| 330 | return mBitrate; |
| 331 | } |
| 332 | std::shared_ptr<C2StreamRequestSyncFrameTuning::output> getRequestSync_l() const { |
| 333 | return mRequestSync; |
| 334 | } |
Harish Mahendrakar | 57d7cc9 | 2019-02-05 08:34:55 -0800 | [diff] [blame] | 335 | std::shared_ptr<C2StreamComplexityTuning::output> getComplexity_l() const { |
| 336 | return mComplexity; |
| 337 | } |
| 338 | std::shared_ptr<C2StreamQualityTuning::output> getQuality_l() const { |
| 339 | return mQuality; |
| 340 | } |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 341 | |
| 342 | private: |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 343 | std::shared_ptr<C2StreamUsageTuning::input> mUsage; |
Lajos Molnar | 3bb81cd | 2019-02-20 15:10:30 -0800 | [diff] [blame] | 344 | std::shared_ptr<C2StreamPictureSizeInfo::input> mSize; |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 345 | std::shared_ptr<C2StreamFrameRateInfo::output> mFrameRate; |
| 346 | std::shared_ptr<C2StreamRequestSyncFrameTuning::output> mRequestSync; |
Lajos Molnar | 3bb81cd | 2019-02-20 15:10:30 -0800 | [diff] [blame] | 347 | std::shared_ptr<C2StreamBitrateInfo::output> mBitrate; |
Harish Mahendrakar | 57d7cc9 | 2019-02-05 08:34:55 -0800 | [diff] [blame] | 348 | std::shared_ptr<C2StreamBitrateModeTuning::output> mBitrateMode; |
| 349 | std::shared_ptr<C2StreamComplexityTuning::output> mComplexity; |
| 350 | std::shared_ptr<C2StreamQualityTuning::output> mQuality; |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 351 | std::shared_ptr<C2StreamProfileLevelInfo::output> mProfileLevel; |
| 352 | std::shared_ptr<C2StreamSyncFrameIntervalTuning::output> mSyncFramePeriod; |
| 353 | }; |
Ray Essick | 0d11a7e | 2019-03-02 20:10:30 -0800 | [diff] [blame] | 354 | |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 355 | static size_t GetCPUCoreCount() { |
Ray Essick | 0d11a7e | 2019-03-02 20:10:30 -0800 | [diff] [blame] | 356 | long cpuCoreCount = 0; |
| 357 | |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 358 | #if defined(_SC_NPROCESSORS_ONLN) |
| 359 | cpuCoreCount = sysconf(_SC_NPROCESSORS_ONLN); |
| 360 | #else |
| 361 | // _SC_NPROC_ONLN must be defined... |
| 362 | cpuCoreCount = sysconf(_SC_NPROC_ONLN); |
| 363 | #endif |
Ray Essick | 0d11a7e | 2019-03-02 20:10:30 -0800 | [diff] [blame] | 364 | |
| 365 | if (cpuCoreCount < 1) |
| 366 | cpuCoreCount = 1; |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 367 | return (size_t)cpuCoreCount; |
| 368 | } |
| 369 | |
| 370 | C2SoftHevcEnc::C2SoftHevcEnc(const char* name, c2_node_id_t id, |
| 371 | const std::shared_ptr<IntfImpl>& intfImpl) |
| 372 | : SimpleC2Component( |
| 373 | std::make_shared<SimpleInterface<IntfImpl>>(name, id, intfImpl)), |
| 374 | mIntf(intfImpl), |
| 375 | mIvVideoColorFormat(IV_YUV_420P), |
| 376 | mHevcEncProfile(1), |
| 377 | mHevcEncLevel(30), |
| 378 | mStarted(false), |
| 379 | mSpsPpsHeaderReceived(false), |
| 380 | mSignalledEos(false), |
| 381 | mSignalledError(false), |
| 382 | mCodecCtx(nullptr) { |
| 383 | // If dump is enabled, then create an empty file |
| 384 | GENERATE_FILE_NAMES(); |
| 385 | CREATE_DUMP_FILE(mInFile); |
| 386 | CREATE_DUMP_FILE(mOutFile); |
| 387 | |
| 388 | gettimeofday(&mTimeStart, nullptr); |
| 389 | gettimeofday(&mTimeEnd, nullptr); |
| 390 | } |
| 391 | |
| 392 | C2SoftHevcEnc::~C2SoftHevcEnc() { |
| 393 | releaseEncoder(); |
| 394 | } |
| 395 | |
| 396 | c2_status_t C2SoftHevcEnc::onInit() { |
| 397 | return initEncoder(); |
| 398 | } |
| 399 | |
| 400 | c2_status_t C2SoftHevcEnc::onStop() { |
| 401 | if (!mStarted) { |
| 402 | return C2_OK; |
| 403 | } |
| 404 | return releaseEncoder(); |
| 405 | } |
| 406 | |
| 407 | void C2SoftHevcEnc::onReset() { |
| 408 | onStop(); |
| 409 | initEncoder(); |
| 410 | } |
| 411 | |
| 412 | void C2SoftHevcEnc::onRelease() { |
| 413 | onStop(); |
| 414 | } |
| 415 | |
| 416 | c2_status_t C2SoftHevcEnc::onFlush_sm() { |
| 417 | return C2_OK; |
| 418 | } |
| 419 | |
| 420 | static void fillEmptyWork(const std::unique_ptr<C2Work>& work) { |
| 421 | uint32_t flags = 0; |
| 422 | if (work->input.flags & C2FrameData::FLAG_END_OF_STREAM) { |
| 423 | flags |= C2FrameData::FLAG_END_OF_STREAM; |
| 424 | ALOGV("Signalling EOS"); |
| 425 | } |
| 426 | work->worklets.front()->output.flags = (C2FrameData::flags_t)flags; |
| 427 | work->worklets.front()->output.buffers.clear(); |
| 428 | work->worklets.front()->output.ordinal = work->input.ordinal; |
| 429 | work->workletsProcessed = 1u; |
| 430 | } |
| 431 | |
Harish Mahendrakar | 57d7cc9 | 2019-02-05 08:34:55 -0800 | [diff] [blame] | 432 | static int getQpFromQuality(int quality) { |
| 433 | int qp; |
| 434 | #define MIN_QP 4 |
| 435 | #define MAX_QP 50 |
| 436 | /* Quality: 100 -> Qp : MIN_QP |
| 437 | * Quality: 0 -> Qp : MAX_QP |
| 438 | * Qp = ((MIN_QP - MAX_QP) * quality / 100) + MAX_QP; |
| 439 | */ |
| 440 | qp = ((MIN_QP - MAX_QP) * quality / 100) + MAX_QP; |
| 441 | qp = std::min(qp, MAX_QP); |
| 442 | qp = std::max(qp, MIN_QP); |
| 443 | return qp; |
| 444 | } |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 445 | c2_status_t C2SoftHevcEnc::initEncParams() { |
| 446 | mCodecCtx = nullptr; |
Ray Essick | 0d11a7e | 2019-03-02 20:10:30 -0800 | [diff] [blame] | 447 | mNumCores = std::min(GetCPUCoreCount(), (size_t) CODEC_MAX_CORES); |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 448 | memset(&mEncParams, 0, sizeof(ihevce_static_cfg_params_t)); |
| 449 | |
| 450 | // default configuration |
| 451 | IHEVCE_PLUGIN_STATUS_T err = ihevce_set_def_params(&mEncParams); |
| 452 | if (IHEVCE_EOK != err) { |
| 453 | ALOGE("HEVC default init failed : 0x%x", err); |
| 454 | return C2_CORRUPTED; |
| 455 | } |
| 456 | |
| 457 | // update configuration |
| 458 | mEncParams.s_src_prms.i4_width = mSize->width; |
| 459 | mEncParams.s_src_prms.i4_height = mSize->height; |
| 460 | mEncParams.s_src_prms.i4_frm_rate_denom = 1000; |
Ray Essick | 0d11a7e | 2019-03-02 20:10:30 -0800 | [diff] [blame] | 461 | mEncParams.s_src_prms.i4_frm_rate_num = |
| 462 | mFrameRate->value * mEncParams.s_src_prms.i4_frm_rate_denom; |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 463 | mEncParams.s_tgt_lyr_prms.as_tgt_params[0].i4_quality_preset = IHEVCE_QUALITY_P5; |
| 464 | mEncParams.s_tgt_lyr_prms.as_tgt_params[0].ai4_tgt_bitrate[0] = |
| 465 | mBitrate->value; |
| 466 | mEncParams.s_tgt_lyr_prms.as_tgt_params[0].ai4_peak_bitrate[0] = |
| 467 | mBitrate->value << 1; |
| 468 | mEncParams.s_tgt_lyr_prms.as_tgt_params[0].i4_codec_level = mHevcEncLevel; |
| 469 | mEncParams.s_coding_tools_prms.i4_max_i_open_gop_period = mIDRInterval; |
| 470 | mEncParams.s_coding_tools_prms.i4_max_cra_open_gop_period = mIDRInterval; |
| 471 | mIvVideoColorFormat = IV_YUV_420P; |
| 472 | mEncParams.s_multi_thrd_prms.i4_max_num_cores = mNumCores; |
| 473 | mEncParams.s_out_strm_prms.i4_codec_profile = mHevcEncProfile; |
Manisha Jajoo | 863bcfc | 2019-03-22 16:32:55 +0530 | [diff] [blame^] | 474 | mEncParams.s_lap_prms.i4_rc_look_ahead_pics = DEFAULT_RC_LOOKAHEAD; |
| 475 | mEncParams.s_coding_tools_prms.i4_max_temporal_layers = DEFAULT_B_FRAMES; |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 476 | |
Harish Mahendrakar | 57d7cc9 | 2019-02-05 08:34:55 -0800 | [diff] [blame] | 477 | switch (mBitrateMode->value) { |
| 478 | case C2Config::BITRATE_IGNORE: |
| 479 | mEncParams.s_config_prms.i4_rate_control_mode = 3; |
| 480 | mEncParams.s_tgt_lyr_prms.as_tgt_params[0].ai4_frame_qp[0] = |
| 481 | getQpFromQuality(mQuality->value); |
| 482 | break; |
| 483 | case C2Config::BITRATE_CONST: |
| 484 | mEncParams.s_config_prms.i4_rate_control_mode = 5; |
| 485 | break; |
| 486 | case C2Config::BITRATE_VARIABLE: |
| 487 | [[fallthrough]]; |
| 488 | default: |
| 489 | mEncParams.s_config_prms.i4_rate_control_mode = 2; |
| 490 | break; |
| 491 | break; |
| 492 | } |
| 493 | |
| 494 | if (mComplexity->value == 10) { |
| 495 | mEncParams.s_tgt_lyr_prms.as_tgt_params[0].i4_quality_preset = IHEVCE_QUALITY_P0; |
| 496 | } else if (mComplexity->value >= 8) { |
| 497 | mEncParams.s_tgt_lyr_prms.as_tgt_params[0].i4_quality_preset = IHEVCE_QUALITY_P2; |
| 498 | } else if (mComplexity->value >= 7) { |
| 499 | mEncParams.s_tgt_lyr_prms.as_tgt_params[0].i4_quality_preset = IHEVCE_QUALITY_P3; |
| 500 | } else if (mComplexity->value >= 5) { |
| 501 | mEncParams.s_tgt_lyr_prms.as_tgt_params[0].i4_quality_preset = IHEVCE_QUALITY_P4; |
| 502 | } else { |
| 503 | mEncParams.s_tgt_lyr_prms.as_tgt_params[0].i4_quality_preset = IHEVCE_QUALITY_P5; |
| 504 | } |
| 505 | |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 506 | return C2_OK; |
| 507 | } |
| 508 | |
| 509 | c2_status_t C2SoftHevcEnc::releaseEncoder() { |
| 510 | mSpsPpsHeaderReceived = false; |
| 511 | mSignalledEos = false; |
| 512 | mSignalledError = false; |
| 513 | mStarted = false; |
| 514 | |
| 515 | if (mCodecCtx) { |
| 516 | IHEVCE_PLUGIN_STATUS_T err = ihevce_close(mCodecCtx); |
| 517 | if (IHEVCE_EOK != err) return C2_CORRUPTED; |
| 518 | mCodecCtx = nullptr; |
| 519 | } |
| 520 | return C2_OK; |
| 521 | } |
| 522 | |
| 523 | c2_status_t C2SoftHevcEnc::drain(uint32_t drainMode, |
| 524 | const std::shared_ptr<C2BlockPool>& pool) { |
Manisha Jajoo | 863bcfc | 2019-03-22 16:32:55 +0530 | [diff] [blame^] | 525 | return drainInternal(drainMode, pool, nullptr); |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 526 | } |
Manisha Jajoo | 863bcfc | 2019-03-22 16:32:55 +0530 | [diff] [blame^] | 527 | |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 528 | c2_status_t C2SoftHevcEnc::initEncoder() { |
| 529 | CHECK(!mCodecCtx); |
| 530 | { |
| 531 | IntfImpl::Lock lock = mIntf->lock(); |
| 532 | mSize = mIntf->getSize_l(); |
Harish Mahendrakar | 57d7cc9 | 2019-02-05 08:34:55 -0800 | [diff] [blame] | 533 | mBitrateMode = mIntf->getBitrateMode_l(); |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 534 | mBitrate = mIntf->getBitrate_l(); |
| 535 | mFrameRate = mIntf->getFrameRate_l(); |
| 536 | mHevcEncProfile = mIntf->getProfile_l(); |
| 537 | mHevcEncLevel = mIntf->getLevel_l(); |
| 538 | mIDRInterval = mIntf->getSyncFramePeriod_l(); |
Harish Mahendrakar | 57d7cc9 | 2019-02-05 08:34:55 -0800 | [diff] [blame] | 539 | mComplexity = mIntf->getComplexity_l(); |
| 540 | mQuality = mIntf->getQuality_l(); |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | c2_status_t status = initEncParams(); |
| 544 | |
| 545 | if (C2_OK != status) { |
| 546 | ALOGE("Failed to initialize encoder params : 0x%x", status); |
| 547 | mSignalledError = true; |
| 548 | return status; |
| 549 | } |
| 550 | |
| 551 | IHEVCE_PLUGIN_STATUS_T err = IHEVCE_EOK; |
| 552 | err = ihevce_init(&mEncParams, &mCodecCtx); |
| 553 | if (IHEVCE_EOK != err) { |
| 554 | ALOGE("HEVC encoder init failed : 0x%x", err); |
| 555 | return C2_CORRUPTED; |
| 556 | } |
| 557 | |
| 558 | mStarted = true; |
| 559 | return C2_OK; |
| 560 | } |
| 561 | |
| 562 | c2_status_t C2SoftHevcEnc::setEncodeArgs(ihevce_inp_buf_t* ps_encode_ip, |
| 563 | const C2GraphicView* const input, |
Manisha Jajoo | 863bcfc | 2019-03-22 16:32:55 +0530 | [diff] [blame^] | 564 | uint64_t workIndex) { |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 565 | ihevce_static_cfg_params_t* params = &mEncParams; |
Ray Essick | 0d11a7e | 2019-03-02 20:10:30 -0800 | [diff] [blame] | 566 | memset(ps_encode_ip, 0, sizeof(*ps_encode_ip)); |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 567 | |
| 568 | if (!input) { |
| 569 | return C2_OK; |
| 570 | } |
| 571 | |
| 572 | if (input->width() < mSize->width || |
| 573 | input->height() < mSize->height) { |
| 574 | /* Expect width height to be configured */ |
| 575 | ALOGW("unexpected Capacity Aspect %d(%d) x %d(%d)", input->width(), |
| 576 | mSize->width, input->height(), mSize->height); |
| 577 | return C2_BAD_VALUE; |
| 578 | } |
| 579 | |
| 580 | const C2PlanarLayout& layout = input->layout(); |
| 581 | uint8_t* yPlane = |
| 582 | const_cast<uint8_t *>(input->data()[C2PlanarLayout::PLANE_Y]); |
| 583 | uint8_t* uPlane = |
| 584 | const_cast<uint8_t *>(input->data()[C2PlanarLayout::PLANE_U]); |
| 585 | uint8_t* vPlane = |
| 586 | const_cast<uint8_t *>(input->data()[C2PlanarLayout::PLANE_V]); |
| 587 | int32_t yStride = layout.planes[C2PlanarLayout::PLANE_Y].rowInc; |
| 588 | int32_t uStride = layout.planes[C2PlanarLayout::PLANE_U].rowInc; |
| 589 | int32_t vStride = layout.planes[C2PlanarLayout::PLANE_V].rowInc; |
| 590 | |
Ray Essick | 0d11a7e | 2019-03-02 20:10:30 -0800 | [diff] [blame] | 591 | const uint32_t width = mSize->width; |
| 592 | const uint32_t height = mSize->height; |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 593 | |
Ray Essick | 0d11a7e | 2019-03-02 20:10:30 -0800 | [diff] [blame] | 594 | // width and height must be even |
| 595 | if (width & 1u || height & 1u) { |
| 596 | ALOGW("height(%u) and width(%u) must both be even", height, width); |
| 597 | return C2_BAD_VALUE; |
| 598 | } |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 599 | |
| 600 | size_t yPlaneSize = width * height; |
| 601 | |
| 602 | switch (layout.type) { |
| 603 | case C2PlanarLayout::TYPE_RGB: |
| 604 | [[fallthrough]]; |
| 605 | case C2PlanarLayout::TYPE_RGBA: { |
| 606 | MemoryBlock conversionBuffer = |
| 607 | mConversionBuffers.fetch(yPlaneSize * 3 / 2); |
| 608 | mConversionBuffersInUse.emplace(conversionBuffer.data(), |
| 609 | conversionBuffer); |
| 610 | yPlane = conversionBuffer.data(); |
| 611 | uPlane = yPlane + yPlaneSize; |
| 612 | vPlane = uPlane + yPlaneSize / 4; |
| 613 | yStride = width; |
| 614 | uStride = vStride = yStride / 2; |
| 615 | ConvertRGBToPlanarYUV(yPlane, yStride, height, |
| 616 | conversionBuffer.size(), *input); |
| 617 | break; |
| 618 | } |
| 619 | case C2PlanarLayout::TYPE_YUV: { |
| 620 | if (!IsYUV420(*input)) { |
| 621 | ALOGE("input is not YUV420"); |
| 622 | return C2_BAD_VALUE; |
| 623 | } |
| 624 | |
| 625 | if (layout.planes[layout.PLANE_Y].colInc == 1 && |
| 626 | layout.planes[layout.PLANE_U].colInc == 1 && |
| 627 | layout.planes[layout.PLANE_V].colInc == 1 && |
| 628 | uStride == vStride && yStride == 2 * vStride) { |
| 629 | // I420 compatible - already set up above |
| 630 | break; |
| 631 | } |
| 632 | |
| 633 | // copy to I420 |
| 634 | yStride = width; |
| 635 | uStride = vStride = yStride / 2; |
| 636 | MemoryBlock conversionBuffer = |
| 637 | mConversionBuffers.fetch(yPlaneSize * 3 / 2); |
| 638 | mConversionBuffersInUse.emplace(conversionBuffer.data(), |
| 639 | conversionBuffer); |
| 640 | MediaImage2 img = |
| 641 | CreateYUV420PlanarMediaImage2(width, height, yStride, height); |
| 642 | status_t err = ImageCopy(conversionBuffer.data(), &img, *input); |
| 643 | if (err != OK) { |
| 644 | ALOGE("Buffer conversion failed: %d", err); |
| 645 | return C2_BAD_VALUE; |
| 646 | } |
| 647 | yPlane = conversionBuffer.data(); |
| 648 | uPlane = yPlane + yPlaneSize; |
| 649 | vPlane = uPlane + yPlaneSize / 4; |
| 650 | break; |
| 651 | } |
| 652 | |
| 653 | case C2PlanarLayout::TYPE_YUVA: |
| 654 | ALOGE("YUVA plane type is not supported"); |
| 655 | return C2_BAD_VALUE; |
| 656 | |
| 657 | default: |
| 658 | ALOGE("Unrecognized plane type: %d", layout.type); |
| 659 | return C2_BAD_VALUE; |
| 660 | } |
| 661 | |
| 662 | switch (mIvVideoColorFormat) { |
| 663 | case IV_YUV_420P: { |
| 664 | // input buffer is supposed to be const but Ittiam API wants bare |
| 665 | // pointer. |
| 666 | ps_encode_ip->apv_inp_planes[0] = yPlane; |
| 667 | ps_encode_ip->apv_inp_planes[1] = uPlane; |
| 668 | ps_encode_ip->apv_inp_planes[2] = vPlane; |
| 669 | |
| 670 | ps_encode_ip->ai4_inp_strd[0] = yStride; |
| 671 | ps_encode_ip->ai4_inp_strd[1] = uStride; |
| 672 | ps_encode_ip->ai4_inp_strd[2] = vStride; |
| 673 | |
| 674 | ps_encode_ip->ai4_inp_size[0] = yStride * height; |
| 675 | ps_encode_ip->ai4_inp_size[1] = uStride * height >> 1; |
| 676 | ps_encode_ip->ai4_inp_size[2] = vStride * height >> 1; |
| 677 | break; |
| 678 | } |
| 679 | |
| 680 | case IV_YUV_422ILE: { |
| 681 | // TODO |
| 682 | break; |
| 683 | } |
| 684 | |
| 685 | case IV_YUV_420SP_UV: |
| 686 | case IV_YUV_420SP_VU: |
| 687 | default: { |
| 688 | ps_encode_ip->apv_inp_planes[0] = yPlane; |
| 689 | ps_encode_ip->apv_inp_planes[1] = uPlane; |
| 690 | ps_encode_ip->apv_inp_planes[2] = nullptr; |
| 691 | |
| 692 | ps_encode_ip->ai4_inp_strd[0] = yStride; |
| 693 | ps_encode_ip->ai4_inp_strd[1] = uStride; |
| 694 | ps_encode_ip->ai4_inp_strd[2] = 0; |
| 695 | |
| 696 | ps_encode_ip->ai4_inp_size[0] = yStride * height; |
| 697 | ps_encode_ip->ai4_inp_size[1] = uStride * height >> 1; |
| 698 | ps_encode_ip->ai4_inp_size[2] = 0; |
| 699 | break; |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | ps_encode_ip->i4_curr_bitrate = |
| 704 | params->s_tgt_lyr_prms.as_tgt_params[0].ai4_tgt_bitrate[0]; |
| 705 | ps_encode_ip->i4_curr_peak_bitrate = |
| 706 | params->s_tgt_lyr_prms.as_tgt_params[0].ai4_peak_bitrate[0]; |
| 707 | ps_encode_ip->i4_curr_rate_factor = params->s_config_prms.i4_rate_factor; |
Manisha Jajoo | 863bcfc | 2019-03-22 16:32:55 +0530 | [diff] [blame^] | 708 | ps_encode_ip->u8_pts = workIndex; |
| 709 | return C2_OK; |
| 710 | } |
| 711 | |
| 712 | void C2SoftHevcEnc::finishWork(uint64_t index, |
| 713 | const std::unique_ptr<C2Work>& work, |
| 714 | const std::shared_ptr<C2BlockPool>& pool, |
| 715 | ihevce_out_buf_t* ps_encode_op) { |
| 716 | std::shared_ptr<C2LinearBlock> block; |
| 717 | C2MemoryUsage usage = {C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE}; |
| 718 | c2_status_t status = |
| 719 | pool->fetchLinearBlock(ps_encode_op->i4_bytes_generated, usage, &block); |
| 720 | if (C2_OK != status) { |
| 721 | ALOGE("fetchLinearBlock for Output failed with status 0x%x", status); |
| 722 | mSignalledError = true; |
| 723 | work->result = status; |
| 724 | work->workletsProcessed = 1u; |
| 725 | return; |
| 726 | } |
| 727 | C2WriteView wView = block->map().get(); |
| 728 | if (C2_OK != wView.error()) { |
| 729 | ALOGE("write view map failed with status 0x%x", wView.error()); |
| 730 | mSignalledError = true; |
| 731 | work->result = wView.error(); |
| 732 | work->workletsProcessed = 1u; |
| 733 | return; |
| 734 | } |
| 735 | memcpy(wView.data(), ps_encode_op->pu1_output_buf, |
| 736 | ps_encode_op->i4_bytes_generated); |
| 737 | |
| 738 | std::shared_ptr<C2Buffer> buffer = |
| 739 | createLinearBuffer(block, 0, ps_encode_op->i4_bytes_generated); |
| 740 | |
| 741 | DUMP_TO_FILE(mOutFile, ps_encode_op->pu1_output_buf, |
| 742 | ps_encode_op->i4_bytes_generated); |
| 743 | |
| 744 | if (ps_encode_op->i4_is_key_frame) { |
| 745 | ALOGV("IDR frame produced"); |
| 746 | buffer->setInfo(std::make_shared<C2StreamPictureTypeMaskInfo::output>( |
| 747 | 0u /* stream id */, C2Config::SYNC_FRAME)); |
| 748 | } |
| 749 | |
| 750 | auto fillWork = [buffer](const std::unique_ptr<C2Work>& work) { |
| 751 | work->worklets.front()->output.flags = (C2FrameData::flags_t)0; |
| 752 | work->worklets.front()->output.buffers.clear(); |
| 753 | work->worklets.front()->output.buffers.push_back(buffer); |
| 754 | work->worklets.front()->output.ordinal = work->input.ordinal; |
| 755 | work->workletsProcessed = 1u; |
| 756 | }; |
| 757 | if (work && c2_cntr64_t(index) == work->input.ordinal.frameIndex) { |
| 758 | fillWork(work); |
| 759 | if (mSignalledEos) { |
| 760 | work->worklets.front()->output.flags = |
| 761 | C2FrameData::FLAG_END_OF_STREAM; |
| 762 | } |
| 763 | } else { |
| 764 | finish(index, fillWork); |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | c2_status_t C2SoftHevcEnc::drainInternal( |
| 769 | uint32_t drainMode, |
| 770 | const std::shared_ptr<C2BlockPool> &pool, |
| 771 | const std::unique_ptr<C2Work> &work) { |
| 772 | |
| 773 | if (drainMode == NO_DRAIN) { |
| 774 | ALOGW("drain with NO_DRAIN: no-op"); |
| 775 | return C2_OK; |
| 776 | } |
| 777 | if (drainMode == DRAIN_CHAIN) { |
| 778 | ALOGW("DRAIN_CHAIN not supported"); |
| 779 | return C2_OMITTED; |
| 780 | } |
| 781 | |
| 782 | while (true) { |
| 783 | ihevce_out_buf_t s_encode_op{}; |
| 784 | memset(&s_encode_op, 0, sizeof(s_encode_op)); |
| 785 | |
| 786 | ihevce_encode(mCodecCtx, nullptr, &s_encode_op); |
| 787 | if (s_encode_op.i4_bytes_generated) { |
| 788 | finishWork(s_encode_op.u8_pts, work, pool, &s_encode_op); |
| 789 | } else { |
| 790 | if (work->workletsProcessed != 1u) fillEmptyWork(work); |
| 791 | break; |
| 792 | } |
| 793 | } |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 794 | return C2_OK; |
| 795 | } |
| 796 | |
| 797 | void C2SoftHevcEnc::process(const std::unique_ptr<C2Work>& work, |
| 798 | const std::shared_ptr<C2BlockPool>& pool) { |
| 799 | // Initialize output work |
| 800 | work->result = C2_OK; |
Manisha Jajoo | 863bcfc | 2019-03-22 16:32:55 +0530 | [diff] [blame^] | 801 | work->workletsProcessed = 0u; |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 802 | work->worklets.front()->output.flags = work->input.flags; |
| 803 | |
| 804 | if (mSignalledError || mSignalledEos) { |
| 805 | work->result = C2_BAD_VALUE; |
| 806 | ALOGD("Signalled Error / Signalled Eos"); |
| 807 | return; |
| 808 | } |
| 809 | c2_status_t status = C2_OK; |
| 810 | |
| 811 | // Initialize encoder if not already initialized |
| 812 | if (!mStarted) { |
| 813 | status = initEncoder(); |
| 814 | if (C2_OK != status) { |
| 815 | ALOGE("Failed to initialize encoder : 0x%x", status); |
| 816 | mSignalledError = true; |
| 817 | work->result = status; |
Manisha Jajoo | 863bcfc | 2019-03-22 16:32:55 +0530 | [diff] [blame^] | 818 | work->workletsProcessed = 1u; |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 819 | return; |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | std::shared_ptr<const C2GraphicView> view; |
| 824 | std::shared_ptr<C2Buffer> inputBuffer = nullptr; |
| 825 | bool eos = ((work->input.flags & C2FrameData::FLAG_END_OF_STREAM) != 0); |
Manisha Jajoo | 863bcfc | 2019-03-22 16:32:55 +0530 | [diff] [blame^] | 826 | if (eos) mSignalledEos = true; |
| 827 | |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 828 | if (!work->input.buffers.empty()) { |
| 829 | inputBuffer = work->input.buffers[0]; |
| 830 | view = std::make_shared<const C2GraphicView>( |
| 831 | inputBuffer->data().graphicBlocks().front().map().get()); |
| 832 | if (view->error() != C2_OK) { |
| 833 | ALOGE("graphic view map err = %d", view->error()); |
| 834 | mSignalledError = true; |
Ray Essick | 0d11a7e | 2019-03-02 20:10:30 -0800 | [diff] [blame] | 835 | work->result = C2_CORRUPTED; |
Manisha Jajoo | 863bcfc | 2019-03-22 16:32:55 +0530 | [diff] [blame^] | 836 | work->workletsProcessed = 1u; |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 837 | return; |
| 838 | } |
| 839 | } |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 840 | IHEVCE_PLUGIN_STATUS_T err = IHEVCE_EOK; |
| 841 | |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 842 | if (!mSpsPpsHeaderReceived) { |
| 843 | ihevce_out_buf_t s_header_op{}; |
| 844 | err = ihevce_encode_header(mCodecCtx, &s_header_op); |
| 845 | if (err == IHEVCE_EOK && s_header_op.i4_bytes_generated) { |
Lajos Molnar | 3bb81cd | 2019-02-20 15:10:30 -0800 | [diff] [blame] | 846 | std::unique_ptr<C2StreamInitDataInfo::output> csd = |
| 847 | C2StreamInitDataInfo::output::AllocUnique( |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 848 | s_header_op.i4_bytes_generated, 0u); |
| 849 | if (!csd) { |
| 850 | ALOGE("CSD allocation failed"); |
| 851 | mSignalledError = true; |
| 852 | work->result = C2_NO_MEMORY; |
Manisha Jajoo | 863bcfc | 2019-03-22 16:32:55 +0530 | [diff] [blame^] | 853 | work->workletsProcessed = 1u; |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 854 | return; |
| 855 | } |
| 856 | memcpy(csd->m.value, s_header_op.pu1_output_buf, |
| 857 | s_header_op.i4_bytes_generated); |
| 858 | DUMP_TO_FILE(mOutFile, csd->m.value, csd->flexCount()); |
| 859 | work->worklets.front()->output.configUpdate.push_back( |
| 860 | std::move(csd)); |
| 861 | mSpsPpsHeaderReceived = true; |
| 862 | } |
| 863 | if (!inputBuffer) { |
Manisha Jajoo | 863bcfc | 2019-03-22 16:32:55 +0530 | [diff] [blame^] | 864 | work->workletsProcessed = 1u; |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 865 | return; |
| 866 | } |
| 867 | } |
| 868 | ihevce_inp_buf_t s_encode_ip{}; |
| 869 | ihevce_out_buf_t s_encode_op{}; |
Manisha Jajoo | 863bcfc | 2019-03-22 16:32:55 +0530 | [diff] [blame^] | 870 | uint64_t workIndex = work->input.ordinal.frameIndex.peekull(); |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 871 | |
Manisha Jajoo | 863bcfc | 2019-03-22 16:32:55 +0530 | [diff] [blame^] | 872 | status = setEncodeArgs(&s_encode_ip, view.get(), workIndex); |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 873 | if (C2_OK != status) { |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 874 | ALOGE("setEncodeArgs failed : 0x%x", status); |
Ray Essick | 0d11a7e | 2019-03-02 20:10:30 -0800 | [diff] [blame] | 875 | mSignalledError = true; |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 876 | work->result = status; |
Manisha Jajoo | 863bcfc | 2019-03-22 16:32:55 +0530 | [diff] [blame^] | 877 | work->workletsProcessed = 1u; |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 878 | return; |
| 879 | } |
| 880 | |
| 881 | uint64_t timeDelay = 0; |
| 882 | uint64_t timeTaken = 0; |
Manisha Jajoo | 863bcfc | 2019-03-22 16:32:55 +0530 | [diff] [blame^] | 883 | memset(&s_encode_op, 0, sizeof(s_encode_op)); |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 884 | GETTIME(&mTimeStart, nullptr); |
| 885 | TIME_DIFF(mTimeEnd, mTimeStart, timeDelay); |
| 886 | |
Manisha Jajoo | 863bcfc | 2019-03-22 16:32:55 +0530 | [diff] [blame^] | 887 | if (inputBuffer) { |
| 888 | err = ihevce_encode(mCodecCtx, &s_encode_ip, &s_encode_op); |
| 889 | if (IHEVCE_EOK != err) { |
| 890 | ALOGE("Encode Frame failed : 0x%x", err); |
| 891 | mSignalledError = true; |
| 892 | work->result = C2_CORRUPTED; |
| 893 | work->workletsProcessed = 1u; |
| 894 | return; |
| 895 | } |
| 896 | } else if (!eos) { |
| 897 | fillEmptyWork(work); |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | GETTIME(&mTimeEnd, nullptr); |
| 901 | /* Compute time taken for decode() */ |
| 902 | TIME_DIFF(mTimeStart, mTimeEnd, timeTaken); |
| 903 | |
| 904 | ALOGV("timeTaken=%6d delay=%6d numBytes=%6d", (int)timeTaken, |
| 905 | (int)timeDelay, s_encode_op.i4_bytes_generated); |
| 906 | |
| 907 | if (s_encode_op.i4_bytes_generated) { |
Manisha Jajoo | 863bcfc | 2019-03-22 16:32:55 +0530 | [diff] [blame^] | 908 | finishWork(s_encode_op.u8_pts, work, pool, &s_encode_op); |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 909 | } |
Manisha Jajoo | 863bcfc | 2019-03-22 16:32:55 +0530 | [diff] [blame^] | 910 | |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 911 | if (eos) { |
Manisha Jajoo | 863bcfc | 2019-03-22 16:32:55 +0530 | [diff] [blame^] | 912 | drainInternal(DRAIN_COMPONENT_WITH_EOS, pool, work); |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 913 | } |
| 914 | } |
| 915 | |
| 916 | class C2SoftHevcEncFactory : public C2ComponentFactory { |
| 917 | public: |
| 918 | C2SoftHevcEncFactory() |
| 919 | : mHelper(std::static_pointer_cast<C2ReflectorHelper>( |
| 920 | GetCodec2PlatformComponentStore()->getParamReflector())) {} |
| 921 | |
Ray Essick | 0d11a7e | 2019-03-02 20:10:30 -0800 | [diff] [blame] | 922 | c2_status_t createComponent( |
| 923 | c2_node_id_t id, |
| 924 | std::shared_ptr<C2Component>* const component, |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 925 | std::function<void(C2Component*)> deleter) override { |
| 926 | *component = std::shared_ptr<C2Component>( |
| 927 | new C2SoftHevcEnc( |
| 928 | COMPONENT_NAME, id, |
| 929 | std::make_shared<C2SoftHevcEnc::IntfImpl>(mHelper)), |
| 930 | deleter); |
| 931 | return C2_OK; |
| 932 | } |
| 933 | |
Ray Essick | 0d11a7e | 2019-03-02 20:10:30 -0800 | [diff] [blame] | 934 | c2_status_t createInterface( |
| 935 | c2_node_id_t id, |
| 936 | std::shared_ptr<C2ComponentInterface>* const interface, |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 937 | std::function<void(C2ComponentInterface*)> deleter) override { |
| 938 | *interface = std::shared_ptr<C2ComponentInterface>( |
| 939 | new SimpleInterface<C2SoftHevcEnc::IntfImpl>( |
| 940 | COMPONENT_NAME, id, |
| 941 | std::make_shared<C2SoftHevcEnc::IntfImpl>(mHelper)), |
| 942 | deleter); |
| 943 | return C2_OK; |
| 944 | } |
| 945 | |
Ray Essick | 0d11a7e | 2019-03-02 20:10:30 -0800 | [diff] [blame] | 946 | ~C2SoftHevcEncFactory() override = default; |
Roma Kaul | dfe650a | 2018-08-02 17:48:51 +0530 | [diff] [blame] | 947 | |
| 948 | private: |
| 949 | std::shared_ptr<C2ReflectorHelper> mHelper; |
| 950 | }; |
| 951 | |
| 952 | } // namespace android |
| 953 | |
| 954 | extern "C" ::C2ComponentFactory* CreateCodec2Factory() { |
| 955 | ALOGV("in %s", __func__); |
| 956 | return new ::android::C2SoftHevcEncFactory(); |
| 957 | } |
| 958 | |
| 959 | extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) { |
| 960 | ALOGV("in %s", __func__); |
| 961 | delete factory; |
| 962 | } |