Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 "C2SoftAacEnc" |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include <inttypes.h> |
| 22 | |
| 23 | #include <C2PlatformSupport.h> |
| 24 | #include <SimpleC2Interface.h> |
| 25 | #include <media/stagefright/foundation/MediaDefs.h> |
| 26 | #include <media/stagefright/foundation/hexdump.h> |
| 27 | |
| 28 | #include "C2SoftAacEnc.h" |
| 29 | |
| 30 | namespace android { |
| 31 | |
Rakesh Kumar | 66d9d06 | 2019-03-12 17:46:17 +0530 | [diff] [blame] | 32 | namespace { |
| 33 | |
| 34 | constexpr char COMPONENT_NAME[] = "c2.android.aac.encoder"; |
| 35 | |
| 36 | } // namespace |
| 37 | |
| 38 | class C2SoftAacEnc::IntfImpl : public SimpleInterface<void>::BaseParams { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 39 | public: |
| 40 | explicit IntfImpl(const std::shared_ptr<C2ReflectorHelper> &helper) |
Rakesh Kumar | 66d9d06 | 2019-03-12 17:46:17 +0530 | [diff] [blame] | 41 | : SimpleInterface<void>::BaseParams( |
| 42 | helper, |
| 43 | COMPONENT_NAME, |
| 44 | C2Component::KIND_ENCODER, |
| 45 | C2Component::DOMAIN_AUDIO, |
| 46 | MEDIA_MIMETYPE_AUDIO_AAC) { |
| 47 | noPrivateBuffers(); |
| 48 | noInputReferences(); |
| 49 | noOutputReferences(); |
| 50 | noInputLatency(); |
| 51 | noTimeStretch(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 52 | setDerivedInstance(this); |
| 53 | |
| 54 | addParameter( |
Rakesh Kumar | 66d9d06 | 2019-03-12 17:46:17 +0530 | [diff] [blame] | 55 | DefineParam(mAttrib, C2_PARAMKEY_COMPONENT_ATTRIBUTES) |
| 56 | .withConstValue(new C2ComponentAttributesSetting( |
| 57 | C2Component::ATTRIB_IS_TEMPORAL)) |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 58 | .build()); |
| 59 | |
| 60 | addParameter( |
Lajos Molnar | 3bb81cd | 2019-02-20 15:10:30 -0800 | [diff] [blame] | 61 | DefineParam(mSampleRate, C2_PARAMKEY_SAMPLE_RATE) |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 62 | .withDefault(new C2StreamSampleRateInfo::input(0u, 44100)) |
| 63 | .withFields({C2F(mSampleRate, value).oneOf({ |
| 64 | 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000 |
| 65 | })}) |
| 66 | .withSetter((Setter<decltype(*mSampleRate)>::StrictValueWithNoDeps)) |
| 67 | .build()); |
| 68 | |
| 69 | addParameter( |
Lajos Molnar | 3bb81cd | 2019-02-20 15:10:30 -0800 | [diff] [blame] | 70 | DefineParam(mChannelCount, C2_PARAMKEY_CHANNEL_COUNT) |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 71 | .withDefault(new C2StreamChannelCountInfo::input(0u, 1)) |
| 72 | .withFields({C2F(mChannelCount, value).inRange(1, 6)}) |
| 73 | .withSetter(Setter<decltype(*mChannelCount)>::StrictValueWithNoDeps) |
| 74 | .build()); |
| 75 | |
| 76 | addParameter( |
Lajos Molnar | 3bb81cd | 2019-02-20 15:10:30 -0800 | [diff] [blame] | 77 | DefineParam(mBitrate, C2_PARAMKEY_BITRATE) |
| 78 | .withDefault(new C2StreamBitrateInfo::output(0u, 64000)) |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 79 | .withFields({C2F(mBitrate, value).inRange(8000, 960000)}) |
| 80 | .withSetter(Setter<decltype(*mBitrate)>::NonStrictValueWithNoDeps) |
| 81 | .build()); |
| 82 | |
| 83 | addParameter( |
| 84 | DefineParam(mInputMaxBufSize, C2_PARAMKEY_INPUT_MAX_BUFFER_SIZE) |
| 85 | .withDefault(new C2StreamMaxBufferSizeInfo::input(0u, 8192)) |
| 86 | .calculatedAs(MaxBufSizeCalculator, mChannelCount) |
| 87 | .build()); |
| 88 | |
| 89 | addParameter( |
| 90 | DefineParam(mProfileLevel, C2_PARAMKEY_PROFILE_LEVEL) |
| 91 | .withDefault(new C2StreamProfileLevelInfo::output(0u, |
| 92 | C2Config::PROFILE_AAC_LC, C2Config::LEVEL_UNUSED)) |
| 93 | .withFields({ |
| 94 | C2F(mProfileLevel, profile).oneOf({ |
| 95 | C2Config::PROFILE_AAC_LC, |
| 96 | C2Config::PROFILE_AAC_HE, |
| 97 | C2Config::PROFILE_AAC_HE_PS, |
| 98 | C2Config::PROFILE_AAC_LD, |
| 99 | C2Config::PROFILE_AAC_ELD}), |
| 100 | C2F(mProfileLevel, level).oneOf({ |
| 101 | C2Config::LEVEL_UNUSED |
| 102 | }) |
| 103 | }) |
| 104 | .withSetter(ProfileLevelSetter) |
| 105 | .build()); |
Manisha Jajoo | b09409a | 2019-05-23 18:57:52 +0530 | [diff] [blame] | 106 | |
| 107 | addParameter( |
| 108 | DefineParam(mSBRMode, C2_PARAMKEY_AAC_SBR_MODE) |
| 109 | .withDefault(new C2StreamAacSbrModeTuning::input(0u, AAC_SBR_AUTO)) |
| 110 | .withFields({C2F(mSBRMode, value).oneOf({ |
| 111 | C2Config::AAC_SBR_OFF, |
| 112 | C2Config::AAC_SBR_SINGLE_RATE, |
| 113 | C2Config::AAC_SBR_DUAL_RATE, |
| 114 | C2Config::AAC_SBR_AUTO })}) |
| 115 | .withSetter(Setter<decltype(*mSBRMode)>::NonStrictValueWithNoDeps) |
| 116 | .build()); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | uint32_t getSampleRate() const { return mSampleRate->value; } |
| 120 | uint32_t getChannelCount() const { return mChannelCount->value; } |
| 121 | uint32_t getBitrate() const { return mBitrate->value; } |
Manisha Jajoo | b09409a | 2019-05-23 18:57:52 +0530 | [diff] [blame] | 122 | uint32_t getSBRMode() const { return mSBRMode->value; } |
| 123 | uint32_t getProfile() const { return mProfileLevel->profile; } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 124 | static C2R ProfileLevelSetter(bool mayBlock, C2P<C2StreamProfileLevelInfo::output> &me) { |
| 125 | (void)mayBlock; |
| 126 | (void)me; // TODO: validate |
| 127 | return C2R::Ok(); |
| 128 | } |
| 129 | |
| 130 | static C2R MaxBufSizeCalculator( |
| 131 | bool mayBlock, |
| 132 | C2P<C2StreamMaxBufferSizeInfo::input> &me, |
| 133 | const C2P<C2StreamChannelCountInfo::input> &channelCount) { |
| 134 | (void)mayBlock; |
| 135 | me.set().value = 1024 * sizeof(short) * channelCount.v.value; |
| 136 | return C2R::Ok(); |
| 137 | } |
| 138 | |
| 139 | private: |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 140 | std::shared_ptr<C2StreamSampleRateInfo::input> mSampleRate; |
| 141 | std::shared_ptr<C2StreamChannelCountInfo::input> mChannelCount; |
Lajos Molnar | 3bb81cd | 2019-02-20 15:10:30 -0800 | [diff] [blame] | 142 | std::shared_ptr<C2StreamBitrateInfo::output> mBitrate; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 143 | std::shared_ptr<C2StreamMaxBufferSizeInfo::input> mInputMaxBufSize; |
| 144 | std::shared_ptr<C2StreamProfileLevelInfo::output> mProfileLevel; |
Manisha Jajoo | b09409a | 2019-05-23 18:57:52 +0530 | [diff] [blame] | 145 | std::shared_ptr<C2StreamAacSbrModeTuning::input> mSBRMode; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 146 | }; |
| 147 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 148 | C2SoftAacEnc::C2SoftAacEnc( |
| 149 | const char *name, |
| 150 | c2_node_id_t id, |
| 151 | const std::shared_ptr<IntfImpl> &intfImpl) |
| 152 | : SimpleC2Component(std::make_shared<SimpleInterface<IntfImpl>>(name, id, intfImpl)), |
| 153 | mIntf(intfImpl), |
| 154 | mAACEncoder(nullptr), |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 155 | mNumBytesPerInputFrame(0u), |
| 156 | mOutBufferSize(0u), |
| 157 | mSentCodecSpecificData(false), |
Wonsik Kim | 353e167 | 2019-01-07 16:31:29 -0800 | [diff] [blame] | 158 | mInputTimeSet(false), |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 159 | mInputSize(0), |
Wonsik Kim | 8ead39d | 2019-04-01 13:56:12 -0700 | [diff] [blame] | 160 | mInputTimeUs(0), |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 161 | mSignalledError(false), |
| 162 | mOutIndex(0u) { |
| 163 | } |
| 164 | |
| 165 | C2SoftAacEnc::~C2SoftAacEnc() { |
| 166 | onReset(); |
| 167 | } |
| 168 | |
| 169 | c2_status_t C2SoftAacEnc::onInit() { |
| 170 | status_t err = initEncoder(); |
| 171 | return err == OK ? C2_OK : C2_CORRUPTED; |
| 172 | } |
| 173 | |
| 174 | status_t C2SoftAacEnc::initEncoder() { |
| 175 | if (AACENC_OK != aacEncOpen(&mAACEncoder, 0, 0)) { |
| 176 | ALOGE("Failed to init AAC encoder"); |
| 177 | return UNKNOWN_ERROR; |
| 178 | } |
| 179 | return setAudioParams(); |
| 180 | } |
| 181 | |
| 182 | c2_status_t C2SoftAacEnc::onStop() { |
| 183 | mSentCodecSpecificData = false; |
Wonsik Kim | 353e167 | 2019-01-07 16:31:29 -0800 | [diff] [blame] | 184 | mInputTimeSet = false; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 185 | mInputSize = 0u; |
Wonsik Kim | 8ead39d | 2019-04-01 13:56:12 -0700 | [diff] [blame] | 186 | mInputTimeUs = 0; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 187 | mSignalledError = false; |
| 188 | return C2_OK; |
| 189 | } |
| 190 | |
| 191 | void C2SoftAacEnc::onReset() { |
| 192 | (void)onStop(); |
| 193 | aacEncClose(&mAACEncoder); |
| 194 | } |
| 195 | |
| 196 | void C2SoftAacEnc::onRelease() { |
| 197 | // no-op |
| 198 | } |
| 199 | |
| 200 | c2_status_t C2SoftAacEnc::onFlush_sm() { |
| 201 | mSentCodecSpecificData = false; |
Wonsik Kim | 353e167 | 2019-01-07 16:31:29 -0800 | [diff] [blame] | 202 | mInputTimeSet = false; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 203 | mInputSize = 0u; |
Wonsik Kim | 8ead39d | 2019-04-01 13:56:12 -0700 | [diff] [blame] | 204 | mInputTimeUs = 0; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 205 | return C2_OK; |
| 206 | } |
| 207 | |
| 208 | static CHANNEL_MODE getChannelMode(uint32_t nChannels) { |
| 209 | CHANNEL_MODE chMode = MODE_INVALID; |
| 210 | switch (nChannels) { |
| 211 | case 1: chMode = MODE_1; break; |
| 212 | case 2: chMode = MODE_2; break; |
| 213 | case 3: chMode = MODE_1_2; break; |
| 214 | case 4: chMode = MODE_1_2_1; break; |
| 215 | case 5: chMode = MODE_1_2_2; break; |
| 216 | case 6: chMode = MODE_1_2_2_1; break; |
| 217 | default: chMode = MODE_INVALID; |
| 218 | } |
| 219 | return chMode; |
| 220 | } |
| 221 | |
Manisha Jajoo | b09409a | 2019-05-23 18:57:52 +0530 | [diff] [blame] | 222 | static AUDIO_OBJECT_TYPE getAOTFromProfile(uint32_t profile) { |
| 223 | if (profile == C2Config::PROFILE_AAC_LC) { |
| 224 | return AOT_AAC_LC; |
| 225 | } else if (profile == C2Config::PROFILE_AAC_HE) { |
| 226 | return AOT_SBR; |
| 227 | } else if (profile == C2Config::PROFILE_AAC_HE_PS) { |
| 228 | return AOT_PS; |
| 229 | } else if (profile == C2Config::PROFILE_AAC_LD) { |
| 230 | return AOT_ER_AAC_LD; |
| 231 | } else if (profile == C2Config::PROFILE_AAC_ELD) { |
| 232 | return AOT_ER_AAC_ELD; |
| 233 | } else { |
| 234 | ALOGW("Unsupported AAC profile - defaulting to AAC-LC"); |
| 235 | return AOT_AAC_LC; |
| 236 | } |
| 237 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 238 | |
| 239 | status_t C2SoftAacEnc::setAudioParams() { |
| 240 | // We call this whenever sample rate, number of channels, bitrate or SBR mode change |
| 241 | // in reponse to setParameter calls. |
Manisha Jajoo | b09409a | 2019-05-23 18:57:52 +0530 | [diff] [blame] | 242 | int32_t sbrRatio = 0; |
| 243 | uint32_t sbrMode = mIntf->getSBRMode(); |
| 244 | if (sbrMode == AAC_SBR_SINGLE_RATE) sbrRatio = 1; |
| 245 | else if (sbrMode == AAC_SBR_DUAL_RATE) sbrRatio = 2; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 246 | |
| 247 | ALOGV("setAudioParams: %u Hz, %u channels, %u bps, %i sbr mode, %i sbr ratio", |
Manisha Jajoo | b09409a | 2019-05-23 18:57:52 +0530 | [diff] [blame] | 248 | mIntf->getSampleRate(), mIntf->getChannelCount(), mIntf->getBitrate(), |
| 249 | sbrMode, sbrRatio); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 250 | |
Manisha Jajoo | b09409a | 2019-05-23 18:57:52 +0530 | [diff] [blame] | 251 | uint32_t aacProfile = mIntf->getProfile(); |
| 252 | if (AACENC_OK != aacEncoder_SetParam(mAACEncoder, AACENC_AOT, getAOTFromProfile(aacProfile))) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 253 | ALOGE("Failed to set AAC encoder parameters"); |
| 254 | return UNKNOWN_ERROR; |
| 255 | } |
| 256 | |
| 257 | if (AACENC_OK != aacEncoder_SetParam(mAACEncoder, AACENC_SAMPLERATE, mIntf->getSampleRate())) { |
| 258 | ALOGE("Failed to set AAC encoder parameters"); |
| 259 | return UNKNOWN_ERROR; |
| 260 | } |
| 261 | if (AACENC_OK != aacEncoder_SetParam(mAACEncoder, AACENC_BITRATE, mIntf->getBitrate())) { |
| 262 | ALOGE("Failed to set AAC encoder parameters"); |
| 263 | return UNKNOWN_ERROR; |
| 264 | } |
| 265 | if (AACENC_OK != aacEncoder_SetParam(mAACEncoder, AACENC_CHANNELMODE, |
| 266 | getChannelMode(mIntf->getChannelCount()))) { |
| 267 | ALOGE("Failed to set AAC encoder parameters"); |
| 268 | return UNKNOWN_ERROR; |
| 269 | } |
| 270 | if (AACENC_OK != aacEncoder_SetParam(mAACEncoder, AACENC_TRANSMUX, TT_MP4_RAW)) { |
| 271 | ALOGE("Failed to set AAC encoder parameters"); |
| 272 | return UNKNOWN_ERROR; |
| 273 | } |
| 274 | |
Manisha Jajoo | b09409a | 2019-05-23 18:57:52 +0530 | [diff] [blame] | 275 | if (sbrMode != -1 && aacProfile == C2Config::PROFILE_AAC_ELD) { |
| 276 | if (AACENC_OK != aacEncoder_SetParam(mAACEncoder, AACENC_SBR_MODE, sbrMode)) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 277 | ALOGE("Failed to set AAC encoder parameters"); |
| 278 | return UNKNOWN_ERROR; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | /* SBR ratio parameter configurations: |
| 283 | 0: Default configuration wherein SBR ratio is configured depending on audio object type by |
| 284 | the FDK. |
| 285 | 1: Downsampled SBR (default for ELD) |
| 286 | 2: Dualrate SBR (default for HE-AAC) |
| 287 | */ |
Manisha Jajoo | b09409a | 2019-05-23 18:57:52 +0530 | [diff] [blame] | 288 | if (AACENC_OK != aacEncoder_SetParam(mAACEncoder, AACENC_SBR_RATIO, sbrRatio)) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 289 | ALOGE("Failed to set AAC encoder parameters"); |
| 290 | return UNKNOWN_ERROR; |
| 291 | } |
| 292 | |
| 293 | return OK; |
| 294 | } |
| 295 | |
| 296 | void C2SoftAacEnc::process( |
| 297 | const std::unique_ptr<C2Work> &work, |
| 298 | const std::shared_ptr<C2BlockPool> &pool) { |
| 299 | // Initialize output work |
| 300 | work->result = C2_OK; |
| 301 | work->workletsProcessed = 1u; |
| 302 | work->worklets.front()->output.flags = work->input.flags; |
| 303 | |
| 304 | if (mSignalledError) { |
| 305 | return; |
| 306 | } |
| 307 | bool eos = (work->input.flags & C2FrameData::FLAG_END_OF_STREAM) != 0; |
| 308 | |
| 309 | uint32_t sampleRate = mIntf->getSampleRate(); |
| 310 | uint32_t channelCount = mIntf->getChannelCount(); |
| 311 | |
| 312 | if (!mSentCodecSpecificData) { |
| 313 | // The very first thing we want to output is the codec specific |
| 314 | // data. |
| 315 | |
| 316 | if (AACENC_OK != aacEncEncode(mAACEncoder, nullptr, nullptr, nullptr, nullptr)) { |
| 317 | ALOGE("Unable to initialize encoder for profile / sample-rate / bit-rate / channels"); |
| 318 | mSignalledError = true; |
| 319 | work->result = C2_CORRUPTED; |
| 320 | return; |
| 321 | } |
| 322 | |
| 323 | uint32_t bitrate = mIntf->getBitrate(); |
| 324 | uint32_t actualBitRate = aacEncoder_GetParam(mAACEncoder, AACENC_BITRATE); |
| 325 | if (bitrate != actualBitRate) { |
| 326 | ALOGW("Requested bitrate %u unsupported, using %u", bitrate, actualBitRate); |
| 327 | } |
| 328 | |
| 329 | AACENC_InfoStruct encInfo; |
| 330 | if (AACENC_OK != aacEncInfo(mAACEncoder, &encInfo)) { |
| 331 | ALOGE("Failed to get AAC encoder info"); |
| 332 | mSignalledError = true; |
| 333 | work->result = C2_CORRUPTED; |
| 334 | return; |
| 335 | } |
| 336 | |
Lajos Molnar | 3bb81cd | 2019-02-20 15:10:30 -0800 | [diff] [blame] | 337 | std::unique_ptr<C2StreamInitDataInfo::output> csd = |
| 338 | C2StreamInitDataInfo::output::AllocUnique(encInfo.confSize, 0u); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 339 | if (!csd) { |
| 340 | ALOGE("CSD allocation failed"); |
| 341 | mSignalledError = true; |
| 342 | work->result = C2_NO_MEMORY; |
| 343 | return; |
| 344 | } |
| 345 | memcpy(csd->m.value, encInfo.confBuf, encInfo.confSize); |
| 346 | ALOGV("put csd"); |
| 347 | #if defined(LOG_NDEBUG) && !LOG_NDEBUG |
| 348 | hexdump(csd->m.value, csd->flexCount()); |
| 349 | #endif |
| 350 | work->worklets.front()->output.configUpdate.push_back(std::move(csd)); |
| 351 | |
| 352 | mOutBufferSize = encInfo.maxOutBufBytes; |
| 353 | mNumBytesPerInputFrame = encInfo.frameLength * channelCount * sizeof(int16_t); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 354 | |
| 355 | mSentCodecSpecificData = true; |
| 356 | } |
| 357 | |
| 358 | uint8_t temp[1]; |
| 359 | C2ReadView view = mDummyReadView; |
| 360 | const uint8_t *data = temp; |
| 361 | size_t capacity = 0u; |
| 362 | if (!work->input.buffers.empty()) { |
| 363 | view = work->input.buffers[0]->data().linearBlocks().front().map().get(); |
| 364 | data = view.data(); |
| 365 | capacity = view.capacity(); |
| 366 | } |
Wonsik Kim | 353e167 | 2019-01-07 16:31:29 -0800 | [diff] [blame] | 367 | if (!mInputTimeSet && capacity > 0) { |
| 368 | mInputTimeUs = work->input.ordinal.timestamp; |
| 369 | mInputTimeSet = true; |
| 370 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 371 | |
| 372 | size_t numFrames = (capacity + mInputSize + (eos ? mNumBytesPerInputFrame - 1 : 0)) |
| 373 | / mNumBytesPerInputFrame; |
| 374 | ALOGV("capacity = %zu; mInputSize = %zu; numFrames = %zu mNumBytesPerInputFrame = %u", |
| 375 | capacity, mInputSize, numFrames, mNumBytesPerInputFrame); |
| 376 | |
| 377 | std::shared_ptr<C2LinearBlock> block; |
| 378 | std::shared_ptr<C2Buffer> buffer; |
| 379 | std::unique_ptr<C2WriteView> wView; |
| 380 | uint8_t *outPtr = temp; |
| 381 | size_t outAvailable = 0u; |
| 382 | uint64_t inputIndex = work->input.ordinal.frameIndex.peeku(); |
| 383 | |
| 384 | AACENC_InArgs inargs; |
| 385 | AACENC_OutArgs outargs; |
| 386 | memset(&inargs, 0, sizeof(inargs)); |
| 387 | memset(&outargs, 0, sizeof(outargs)); |
| 388 | inargs.numInSamples = capacity / sizeof(int16_t); |
| 389 | |
| 390 | void* inBuffer[] = { (unsigned char *)data }; |
| 391 | INT inBufferIds[] = { IN_AUDIO_DATA }; |
| 392 | INT inBufferSize[] = { (INT)capacity }; |
| 393 | INT inBufferElSize[] = { sizeof(int16_t) }; |
| 394 | |
| 395 | AACENC_BufDesc inBufDesc; |
| 396 | inBufDesc.numBufs = sizeof(inBuffer) / sizeof(void*); |
| 397 | inBufDesc.bufs = (void**)&inBuffer; |
| 398 | inBufDesc.bufferIdentifiers = inBufferIds; |
| 399 | inBufDesc.bufSizes = inBufferSize; |
| 400 | inBufDesc.bufElSizes = inBufferElSize; |
| 401 | |
| 402 | void* outBuffer[] = { outPtr }; |
| 403 | INT outBufferIds[] = { OUT_BITSTREAM_DATA }; |
| 404 | INT outBufferSize[] = { 0 }; |
| 405 | INT outBufferElSize[] = { sizeof(UCHAR) }; |
| 406 | |
| 407 | AACENC_BufDesc outBufDesc; |
| 408 | outBufDesc.numBufs = sizeof(outBuffer) / sizeof(void*); |
| 409 | outBufDesc.bufs = (void**)&outBuffer; |
| 410 | outBufDesc.bufferIdentifiers = outBufferIds; |
| 411 | outBufDesc.bufSizes = outBufferSize; |
| 412 | outBufDesc.bufElSizes = outBufferElSize; |
| 413 | |
| 414 | AACENC_ERROR encoderErr = AACENC_OK; |
| 415 | |
| 416 | class FillWork { |
| 417 | public: |
| 418 | FillWork(uint32_t flags, C2WorkOrdinalStruct ordinal, |
| 419 | const std::shared_ptr<C2Buffer> &buffer) |
| 420 | : mFlags(flags), mOrdinal(ordinal), mBuffer(buffer) { |
| 421 | } |
| 422 | ~FillWork() = default; |
| 423 | |
| 424 | void operator()(const std::unique_ptr<C2Work> &work) { |
| 425 | work->worklets.front()->output.flags = (C2FrameData::flags_t)mFlags; |
| 426 | work->worklets.front()->output.buffers.clear(); |
| 427 | work->worklets.front()->output.ordinal = mOrdinal; |
| 428 | work->workletsProcessed = 1u; |
| 429 | work->result = C2_OK; |
| 430 | if (mBuffer) { |
| 431 | work->worklets.front()->output.buffers.push_back(mBuffer); |
| 432 | } |
| 433 | ALOGV("timestamp = %lld, index = %lld, w/%s buffer", |
| 434 | mOrdinal.timestamp.peekll(), |
| 435 | mOrdinal.frameIndex.peekll(), |
| 436 | mBuffer ? "" : "o"); |
| 437 | } |
| 438 | |
| 439 | private: |
| 440 | const uint32_t mFlags; |
| 441 | const C2WorkOrdinalStruct mOrdinal; |
| 442 | const std::shared_ptr<C2Buffer> mBuffer; |
| 443 | }; |
| 444 | |
| 445 | C2WorkOrdinalStruct outOrdinal = work->input.ordinal; |
| 446 | |
| 447 | while (encoderErr == AACENC_OK && inargs.numInSamples > 0) { |
| 448 | if (numFrames && !block) { |
| 449 | C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE }; |
| 450 | // TODO: error handling, proper usage, etc. |
| 451 | c2_status_t err = pool->fetchLinearBlock(mOutBufferSize, usage, &block); |
| 452 | if (err != C2_OK) { |
| 453 | ALOGE("fetchLinearBlock failed : err = %d", err); |
| 454 | work->result = C2_NO_MEMORY; |
| 455 | return; |
| 456 | } |
| 457 | |
| 458 | wView.reset(new C2WriteView(block->map().get())); |
| 459 | outPtr = wView->data(); |
| 460 | outAvailable = wView->size(); |
| 461 | --numFrames; |
| 462 | } |
| 463 | |
| 464 | memset(&outargs, 0, sizeof(outargs)); |
| 465 | |
| 466 | outBuffer[0] = outPtr; |
| 467 | outBufferSize[0] = outAvailable; |
| 468 | |
| 469 | encoderErr = aacEncEncode(mAACEncoder, |
| 470 | &inBufDesc, |
| 471 | &outBufDesc, |
| 472 | &inargs, |
| 473 | &outargs); |
| 474 | |
| 475 | if (encoderErr == AACENC_OK) { |
| 476 | if (buffer) { |
| 477 | outOrdinal.frameIndex = mOutIndex++; |
| 478 | outOrdinal.timestamp = mInputTimeUs; |
| 479 | cloneAndSend( |
| 480 | inputIndex, |
| 481 | work, |
| 482 | FillWork(C2FrameData::FLAG_INCOMPLETE, outOrdinal, buffer)); |
| 483 | buffer.reset(); |
| 484 | } |
| 485 | |
| 486 | if (outargs.numOutBytes > 0) { |
| 487 | mInputSize = 0; |
Wonsik Kim | 84889cb | 2019-01-03 17:07:54 -0800 | [diff] [blame] | 488 | int consumed = (capacity / sizeof(int16_t)) - inargs.numInSamples |
| 489 | + outargs.numInSamples; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 490 | mInputTimeUs = work->input.ordinal.timestamp |
| 491 | + (consumed * 1000000ll / channelCount / sampleRate); |
| 492 | buffer = createLinearBuffer(block, 0, outargs.numOutBytes); |
| 493 | #if defined(LOG_NDEBUG) && !LOG_NDEBUG |
| 494 | hexdump(outPtr, std::min(outargs.numOutBytes, 256)); |
| 495 | #endif |
| 496 | outPtr = temp; |
| 497 | outAvailable = 0; |
| 498 | block.reset(); |
| 499 | } else { |
| 500 | mInputSize += outargs.numInSamples * sizeof(int16_t); |
| 501 | } |
| 502 | |
| 503 | if (outargs.numInSamples > 0) { |
| 504 | inBuffer[0] = (int16_t *)inBuffer[0] + outargs.numInSamples; |
| 505 | inBufferSize[0] -= outargs.numInSamples * sizeof(int16_t); |
| 506 | inargs.numInSamples -= outargs.numInSamples; |
| 507 | } |
| 508 | } |
| 509 | ALOGV("encoderErr = %d mInputSize = %zu inargs.numInSamples = %d, mInputTimeUs = %lld", |
| 510 | encoderErr, mInputSize, inargs.numInSamples, mInputTimeUs.peekll()); |
| 511 | } |
| 512 | |
| 513 | if (eos && inBufferSize[0] > 0) { |
| 514 | if (numFrames && !block) { |
| 515 | C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE }; |
| 516 | // TODO: error handling, proper usage, etc. |
| 517 | c2_status_t err = pool->fetchLinearBlock(mOutBufferSize, usage, &block); |
| 518 | if (err != C2_OK) { |
| 519 | ALOGE("fetchLinearBlock failed : err = %d", err); |
| 520 | work->result = C2_NO_MEMORY; |
| 521 | return; |
| 522 | } |
| 523 | |
| 524 | wView.reset(new C2WriteView(block->map().get())); |
| 525 | outPtr = wView->data(); |
| 526 | outAvailable = wView->size(); |
| 527 | --numFrames; |
| 528 | } |
| 529 | |
| 530 | memset(&outargs, 0, sizeof(outargs)); |
| 531 | |
| 532 | outBuffer[0] = outPtr; |
| 533 | outBufferSize[0] = outAvailable; |
| 534 | |
| 535 | // Flush |
| 536 | inargs.numInSamples = -1; |
| 537 | |
| 538 | (void)aacEncEncode(mAACEncoder, |
| 539 | &inBufDesc, |
| 540 | &outBufDesc, |
| 541 | &inargs, |
| 542 | &outargs); |
| 543 | } |
| 544 | |
| 545 | outOrdinal.frameIndex = mOutIndex++; |
| 546 | outOrdinal.timestamp = mInputTimeUs; |
| 547 | FillWork((C2FrameData::flags_t)(eos ? C2FrameData::FLAG_END_OF_STREAM : 0), |
| 548 | outOrdinal, buffer)(work); |
| 549 | } |
| 550 | |
| 551 | c2_status_t C2SoftAacEnc::drain( |
| 552 | uint32_t drainMode, |
| 553 | const std::shared_ptr<C2BlockPool> &pool) { |
| 554 | switch (drainMode) { |
| 555 | case DRAIN_COMPONENT_NO_EOS: |
| 556 | [[fallthrough]]; |
| 557 | case NO_DRAIN: |
| 558 | // no-op |
| 559 | return C2_OK; |
| 560 | case DRAIN_CHAIN: |
| 561 | return C2_OMITTED; |
| 562 | case DRAIN_COMPONENT_WITH_EOS: |
| 563 | break; |
| 564 | default: |
| 565 | return C2_BAD_VALUE; |
| 566 | } |
| 567 | |
| 568 | (void)pool; |
| 569 | mSentCodecSpecificData = false; |
Wonsik Kim | 353e167 | 2019-01-07 16:31:29 -0800 | [diff] [blame] | 570 | mInputTimeSet = false; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 571 | mInputSize = 0u; |
Wonsik Kim | 8ead39d | 2019-04-01 13:56:12 -0700 | [diff] [blame] | 572 | mInputTimeUs = 0; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 573 | |
| 574 | // TODO: we don't have any pending work at this time to drain. |
| 575 | return C2_OK; |
| 576 | } |
| 577 | |
| 578 | class C2SoftAacEncFactory : public C2ComponentFactory { |
| 579 | public: |
| 580 | C2SoftAacEncFactory() : mHelper(std::static_pointer_cast<C2ReflectorHelper>( |
| 581 | GetCodec2PlatformComponentStore()->getParamReflector())) { |
| 582 | } |
| 583 | |
| 584 | virtual c2_status_t createComponent( |
| 585 | c2_node_id_t id, |
| 586 | std::shared_ptr<C2Component>* const component, |
| 587 | std::function<void(C2Component*)> deleter) override { |
| 588 | *component = std::shared_ptr<C2Component>( |
| 589 | new C2SoftAacEnc(COMPONENT_NAME, |
| 590 | id, |
| 591 | std::make_shared<C2SoftAacEnc::IntfImpl>(mHelper)), |
| 592 | deleter); |
| 593 | return C2_OK; |
| 594 | } |
| 595 | |
| 596 | virtual c2_status_t createInterface( |
| 597 | c2_node_id_t id, std::shared_ptr<C2ComponentInterface>* const interface, |
| 598 | std::function<void(C2ComponentInterface*)> deleter) override { |
| 599 | *interface = std::shared_ptr<C2ComponentInterface>( |
| 600 | new SimpleInterface<C2SoftAacEnc::IntfImpl>( |
| 601 | COMPONENT_NAME, id, std::make_shared<C2SoftAacEnc::IntfImpl>(mHelper)), |
| 602 | deleter); |
| 603 | return C2_OK; |
| 604 | } |
| 605 | |
| 606 | virtual ~C2SoftAacEncFactory() override = default; |
| 607 | |
| 608 | private: |
| 609 | std::shared_ptr<C2ReflectorHelper> mHelper; |
| 610 | }; |
| 611 | |
| 612 | } // namespace android |
| 613 | |
| 614 | extern "C" ::C2ComponentFactory* CreateCodec2Factory() { |
| 615 | ALOGV("in %s", __func__); |
| 616 | return new ::android::C2SoftAacEncFactory(); |
| 617 | } |
| 618 | |
| 619 | extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) { |
| 620 | ALOGV("in %s", __func__); |
| 621 | delete factory; |
| 622 | } |