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 | 8c886ae | 2019-07-15 12:36:22 -0700 | [diff] [blame] | 160 | mNextFrameTimestampUs(0), |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 161 | mSignalledError(false), |
Wonsik Kim | 1f5583e | 2019-08-09 10:35:55 -0700 | [diff] [blame] | 162 | mOutIndex(0u), |
| 163 | mRemainderLen(0u) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | C2SoftAacEnc::~C2SoftAacEnc() { |
| 167 | onReset(); |
| 168 | } |
| 169 | |
| 170 | c2_status_t C2SoftAacEnc::onInit() { |
| 171 | status_t err = initEncoder(); |
| 172 | return err == OK ? C2_OK : C2_CORRUPTED; |
| 173 | } |
| 174 | |
| 175 | status_t C2SoftAacEnc::initEncoder() { |
| 176 | if (AACENC_OK != aacEncOpen(&mAACEncoder, 0, 0)) { |
| 177 | ALOGE("Failed to init AAC encoder"); |
| 178 | return UNKNOWN_ERROR; |
| 179 | } |
| 180 | return setAudioParams(); |
| 181 | } |
| 182 | |
| 183 | c2_status_t C2SoftAacEnc::onStop() { |
| 184 | mSentCodecSpecificData = false; |
Wonsik Kim | 353e167 | 2019-01-07 16:31:29 -0800 | [diff] [blame] | 185 | mInputTimeSet = false; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 186 | mInputSize = 0u; |
Wonsik Kim | 8c886ae | 2019-07-15 12:36:22 -0700 | [diff] [blame] | 187 | mNextFrameTimestampUs = 0; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 188 | mSignalledError = false; |
Wonsik Kim | 1f5583e | 2019-08-09 10:35:55 -0700 | [diff] [blame] | 189 | mRemainderLen = 0; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 190 | return C2_OK; |
| 191 | } |
| 192 | |
| 193 | void C2SoftAacEnc::onReset() { |
| 194 | (void)onStop(); |
| 195 | aacEncClose(&mAACEncoder); |
| 196 | } |
| 197 | |
| 198 | void C2SoftAacEnc::onRelease() { |
| 199 | // no-op |
| 200 | } |
| 201 | |
| 202 | c2_status_t C2SoftAacEnc::onFlush_sm() { |
| 203 | mSentCodecSpecificData = false; |
Wonsik Kim | 353e167 | 2019-01-07 16:31:29 -0800 | [diff] [blame] | 204 | mInputTimeSet = false; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 205 | mInputSize = 0u; |
Wonsik Kim | 8c886ae | 2019-07-15 12:36:22 -0700 | [diff] [blame] | 206 | mNextFrameTimestampUs = 0; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 207 | return C2_OK; |
| 208 | } |
| 209 | |
| 210 | static CHANNEL_MODE getChannelMode(uint32_t nChannels) { |
| 211 | CHANNEL_MODE chMode = MODE_INVALID; |
| 212 | switch (nChannels) { |
| 213 | case 1: chMode = MODE_1; break; |
| 214 | case 2: chMode = MODE_2; break; |
| 215 | case 3: chMode = MODE_1_2; break; |
| 216 | case 4: chMode = MODE_1_2_1; break; |
| 217 | case 5: chMode = MODE_1_2_2; break; |
| 218 | case 6: chMode = MODE_1_2_2_1; break; |
| 219 | default: chMode = MODE_INVALID; |
| 220 | } |
| 221 | return chMode; |
| 222 | } |
| 223 | |
Manisha Jajoo | b09409a | 2019-05-23 18:57:52 +0530 | [diff] [blame] | 224 | static AUDIO_OBJECT_TYPE getAOTFromProfile(uint32_t profile) { |
| 225 | if (profile == C2Config::PROFILE_AAC_LC) { |
| 226 | return AOT_AAC_LC; |
| 227 | } else if (profile == C2Config::PROFILE_AAC_HE) { |
| 228 | return AOT_SBR; |
| 229 | } else if (profile == C2Config::PROFILE_AAC_HE_PS) { |
| 230 | return AOT_PS; |
| 231 | } else if (profile == C2Config::PROFILE_AAC_LD) { |
| 232 | return AOT_ER_AAC_LD; |
| 233 | } else if (profile == C2Config::PROFILE_AAC_ELD) { |
| 234 | return AOT_ER_AAC_ELD; |
| 235 | } else { |
| 236 | ALOGW("Unsupported AAC profile - defaulting to AAC-LC"); |
| 237 | return AOT_AAC_LC; |
| 238 | } |
| 239 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 240 | |
| 241 | status_t C2SoftAacEnc::setAudioParams() { |
| 242 | // We call this whenever sample rate, number of channels, bitrate or SBR mode change |
| 243 | // in reponse to setParameter calls. |
Manisha Jajoo | b09409a | 2019-05-23 18:57:52 +0530 | [diff] [blame] | 244 | int32_t sbrRatio = 0; |
| 245 | uint32_t sbrMode = mIntf->getSBRMode(); |
| 246 | if (sbrMode == AAC_SBR_SINGLE_RATE) sbrRatio = 1; |
| 247 | else if (sbrMode == AAC_SBR_DUAL_RATE) sbrRatio = 2; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 248 | |
| 249 | 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] | 250 | mIntf->getSampleRate(), mIntf->getChannelCount(), mIntf->getBitrate(), |
| 251 | sbrMode, sbrRatio); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 252 | |
Manisha Jajoo | b09409a | 2019-05-23 18:57:52 +0530 | [diff] [blame] | 253 | uint32_t aacProfile = mIntf->getProfile(); |
| 254 | if (AACENC_OK != aacEncoder_SetParam(mAACEncoder, AACENC_AOT, getAOTFromProfile(aacProfile))) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 255 | ALOGE("Failed to set AAC encoder parameters"); |
| 256 | return UNKNOWN_ERROR; |
| 257 | } |
| 258 | |
| 259 | if (AACENC_OK != aacEncoder_SetParam(mAACEncoder, AACENC_SAMPLERATE, mIntf->getSampleRate())) { |
| 260 | ALOGE("Failed to set AAC encoder parameters"); |
| 261 | return UNKNOWN_ERROR; |
| 262 | } |
| 263 | if (AACENC_OK != aacEncoder_SetParam(mAACEncoder, AACENC_BITRATE, mIntf->getBitrate())) { |
| 264 | ALOGE("Failed to set AAC encoder parameters"); |
| 265 | return UNKNOWN_ERROR; |
| 266 | } |
| 267 | if (AACENC_OK != aacEncoder_SetParam(mAACEncoder, AACENC_CHANNELMODE, |
| 268 | getChannelMode(mIntf->getChannelCount()))) { |
| 269 | ALOGE("Failed to set AAC encoder parameters"); |
| 270 | return UNKNOWN_ERROR; |
| 271 | } |
| 272 | if (AACENC_OK != aacEncoder_SetParam(mAACEncoder, AACENC_TRANSMUX, TT_MP4_RAW)) { |
| 273 | ALOGE("Failed to set AAC encoder parameters"); |
| 274 | return UNKNOWN_ERROR; |
| 275 | } |
| 276 | |
Manisha Jajoo | b09409a | 2019-05-23 18:57:52 +0530 | [diff] [blame] | 277 | if (sbrMode != -1 && aacProfile == C2Config::PROFILE_AAC_ELD) { |
| 278 | if (AACENC_OK != aacEncoder_SetParam(mAACEncoder, AACENC_SBR_MODE, sbrMode)) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 279 | ALOGE("Failed to set AAC encoder parameters"); |
| 280 | return UNKNOWN_ERROR; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | /* SBR ratio parameter configurations: |
| 285 | 0: Default configuration wherein SBR ratio is configured depending on audio object type by |
| 286 | the FDK. |
| 287 | 1: Downsampled SBR (default for ELD) |
| 288 | 2: Dualrate SBR (default for HE-AAC) |
| 289 | */ |
Manisha Jajoo | b09409a | 2019-05-23 18:57:52 +0530 | [diff] [blame] | 290 | if (AACENC_OK != aacEncoder_SetParam(mAACEncoder, AACENC_SBR_RATIO, sbrRatio)) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 291 | ALOGE("Failed to set AAC encoder parameters"); |
| 292 | return UNKNOWN_ERROR; |
| 293 | } |
| 294 | |
| 295 | return OK; |
| 296 | } |
| 297 | |
| 298 | void C2SoftAacEnc::process( |
| 299 | const std::unique_ptr<C2Work> &work, |
| 300 | const std::shared_ptr<C2BlockPool> &pool) { |
| 301 | // Initialize output work |
| 302 | work->result = C2_OK; |
| 303 | work->workletsProcessed = 1u; |
| 304 | work->worklets.front()->output.flags = work->input.flags; |
| 305 | |
| 306 | if (mSignalledError) { |
| 307 | return; |
| 308 | } |
| 309 | bool eos = (work->input.flags & C2FrameData::FLAG_END_OF_STREAM) != 0; |
| 310 | |
| 311 | uint32_t sampleRate = mIntf->getSampleRate(); |
| 312 | uint32_t channelCount = mIntf->getChannelCount(); |
| 313 | |
| 314 | if (!mSentCodecSpecificData) { |
| 315 | // The very first thing we want to output is the codec specific |
| 316 | // data. |
| 317 | |
| 318 | if (AACENC_OK != aacEncEncode(mAACEncoder, nullptr, nullptr, nullptr, nullptr)) { |
| 319 | ALOGE("Unable to initialize encoder for profile / sample-rate / bit-rate / channels"); |
| 320 | mSignalledError = true; |
| 321 | work->result = C2_CORRUPTED; |
| 322 | return; |
| 323 | } |
| 324 | |
| 325 | uint32_t bitrate = mIntf->getBitrate(); |
| 326 | uint32_t actualBitRate = aacEncoder_GetParam(mAACEncoder, AACENC_BITRATE); |
| 327 | if (bitrate != actualBitRate) { |
| 328 | ALOGW("Requested bitrate %u unsupported, using %u", bitrate, actualBitRate); |
| 329 | } |
| 330 | |
| 331 | AACENC_InfoStruct encInfo; |
| 332 | if (AACENC_OK != aacEncInfo(mAACEncoder, &encInfo)) { |
| 333 | ALOGE("Failed to get AAC encoder info"); |
| 334 | mSignalledError = true; |
| 335 | work->result = C2_CORRUPTED; |
| 336 | return; |
| 337 | } |
| 338 | |
Lajos Molnar | 3bb81cd | 2019-02-20 15:10:30 -0800 | [diff] [blame] | 339 | std::unique_ptr<C2StreamInitDataInfo::output> csd = |
| 340 | C2StreamInitDataInfo::output::AllocUnique(encInfo.confSize, 0u); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 341 | if (!csd) { |
| 342 | ALOGE("CSD allocation failed"); |
| 343 | mSignalledError = true; |
| 344 | work->result = C2_NO_MEMORY; |
| 345 | return; |
| 346 | } |
| 347 | memcpy(csd->m.value, encInfo.confBuf, encInfo.confSize); |
| 348 | ALOGV("put csd"); |
| 349 | #if defined(LOG_NDEBUG) && !LOG_NDEBUG |
| 350 | hexdump(csd->m.value, csd->flexCount()); |
| 351 | #endif |
| 352 | work->worklets.front()->output.configUpdate.push_back(std::move(csd)); |
| 353 | |
| 354 | mOutBufferSize = encInfo.maxOutBufBytes; |
| 355 | mNumBytesPerInputFrame = encInfo.frameLength * channelCount * sizeof(int16_t); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 356 | |
| 357 | mSentCodecSpecificData = true; |
| 358 | } |
| 359 | |
| 360 | uint8_t temp[1]; |
| 361 | C2ReadView view = mDummyReadView; |
| 362 | const uint8_t *data = temp; |
| 363 | size_t capacity = 0u; |
| 364 | if (!work->input.buffers.empty()) { |
| 365 | view = work->input.buffers[0]->data().linearBlocks().front().map().get(); |
| 366 | data = view.data(); |
| 367 | capacity = view.capacity(); |
| 368 | } |
Wonsik Kim | 353e167 | 2019-01-07 16:31:29 -0800 | [diff] [blame] | 369 | if (!mInputTimeSet && capacity > 0) { |
Wonsik Kim | 8c886ae | 2019-07-15 12:36:22 -0700 | [diff] [blame] | 370 | mNextFrameTimestampUs = work->input.ordinal.timestamp; |
Wonsik Kim | 353e167 | 2019-01-07 16:31:29 -0800 | [diff] [blame] | 371 | mInputTimeSet = true; |
| 372 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 373 | |
Wonsik Kim | 1f5583e | 2019-08-09 10:35:55 -0700 | [diff] [blame] | 374 | size_t numFrames = |
| 375 | (mRemainderLen + capacity + mInputSize + (eos ? mNumBytesPerInputFrame - 1 : 0)) |
| 376 | / mNumBytesPerInputFrame; |
Wonsik Kim | 8c886ae | 2019-07-15 12:36:22 -0700 | [diff] [blame] | 377 | ALOGV("capacity = %zu; mInputSize = %zu; numFrames = %zu " |
Wonsik Kim | 1f5583e | 2019-08-09 10:35:55 -0700 | [diff] [blame] | 378 | "mNumBytesPerInputFrame = %u inputTS = %lld remaining = %zu", |
Wonsik Kim | 8c886ae | 2019-07-15 12:36:22 -0700 | [diff] [blame] | 379 | capacity, mInputSize, numFrames, |
Wonsik Kim | 1f5583e | 2019-08-09 10:35:55 -0700 | [diff] [blame] | 380 | mNumBytesPerInputFrame, work->input.ordinal.timestamp.peekll(), |
| 381 | mRemainderLen); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 382 | |
| 383 | std::shared_ptr<C2LinearBlock> block; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 384 | std::unique_ptr<C2WriteView> wView; |
| 385 | uint8_t *outPtr = temp; |
| 386 | size_t outAvailable = 0u; |
| 387 | uint64_t inputIndex = work->input.ordinal.frameIndex.peeku(); |
Wonsik Kim | 1f5583e | 2019-08-09 10:35:55 -0700 | [diff] [blame] | 388 | size_t bytesPerSample = channelCount * sizeof(int16_t); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 389 | |
| 390 | AACENC_InArgs inargs; |
| 391 | AACENC_OutArgs outargs; |
| 392 | memset(&inargs, 0, sizeof(inargs)); |
| 393 | memset(&outargs, 0, sizeof(outargs)); |
| 394 | inargs.numInSamples = capacity / sizeof(int16_t); |
| 395 | |
| 396 | void* inBuffer[] = { (unsigned char *)data }; |
| 397 | INT inBufferIds[] = { IN_AUDIO_DATA }; |
| 398 | INT inBufferSize[] = { (INT)capacity }; |
| 399 | INT inBufferElSize[] = { sizeof(int16_t) }; |
| 400 | |
| 401 | AACENC_BufDesc inBufDesc; |
| 402 | inBufDesc.numBufs = sizeof(inBuffer) / sizeof(void*); |
| 403 | inBufDesc.bufs = (void**)&inBuffer; |
| 404 | inBufDesc.bufferIdentifiers = inBufferIds; |
| 405 | inBufDesc.bufSizes = inBufferSize; |
| 406 | inBufDesc.bufElSizes = inBufferElSize; |
| 407 | |
| 408 | void* outBuffer[] = { outPtr }; |
| 409 | INT outBufferIds[] = { OUT_BITSTREAM_DATA }; |
| 410 | INT outBufferSize[] = { 0 }; |
| 411 | INT outBufferElSize[] = { sizeof(UCHAR) }; |
| 412 | |
| 413 | AACENC_BufDesc outBufDesc; |
| 414 | outBufDesc.numBufs = sizeof(outBuffer) / sizeof(void*); |
| 415 | outBufDesc.bufs = (void**)&outBuffer; |
| 416 | outBufDesc.bufferIdentifiers = outBufferIds; |
| 417 | outBufDesc.bufSizes = outBufferSize; |
| 418 | outBufDesc.bufElSizes = outBufferElSize; |
| 419 | |
| 420 | AACENC_ERROR encoderErr = AACENC_OK; |
| 421 | |
| 422 | class FillWork { |
| 423 | public: |
| 424 | FillWork(uint32_t flags, C2WorkOrdinalStruct ordinal, |
| 425 | const std::shared_ptr<C2Buffer> &buffer) |
| 426 | : mFlags(flags), mOrdinal(ordinal), mBuffer(buffer) { |
| 427 | } |
| 428 | ~FillWork() = default; |
| 429 | |
| 430 | void operator()(const std::unique_ptr<C2Work> &work) { |
| 431 | work->worklets.front()->output.flags = (C2FrameData::flags_t)mFlags; |
| 432 | work->worklets.front()->output.buffers.clear(); |
| 433 | work->worklets.front()->output.ordinal = mOrdinal; |
| 434 | work->workletsProcessed = 1u; |
| 435 | work->result = C2_OK; |
| 436 | if (mBuffer) { |
| 437 | work->worklets.front()->output.buffers.push_back(mBuffer); |
| 438 | } |
| 439 | ALOGV("timestamp = %lld, index = %lld, w/%s buffer", |
| 440 | mOrdinal.timestamp.peekll(), |
| 441 | mOrdinal.frameIndex.peekll(), |
| 442 | mBuffer ? "" : "o"); |
| 443 | } |
| 444 | |
| 445 | private: |
| 446 | const uint32_t mFlags; |
| 447 | const C2WorkOrdinalStruct mOrdinal; |
| 448 | const std::shared_ptr<C2Buffer> mBuffer; |
| 449 | }; |
| 450 | |
Wonsik Kim | 8c886ae | 2019-07-15 12:36:22 -0700 | [diff] [blame] | 451 | struct OutputBuffer { |
| 452 | std::shared_ptr<C2Buffer> buffer; |
| 453 | c2_cntr64_t timestampUs; |
| 454 | }; |
| 455 | std::list<OutputBuffer> outputBuffers; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 456 | |
Wonsik Kim | 1f5583e | 2019-08-09 10:35:55 -0700 | [diff] [blame] | 457 | if (mRemainderLen > 0) { |
| 458 | size_t offset = 0; |
| 459 | for (; mRemainderLen < bytesPerSample && offset < capacity; ++offset) { |
| 460 | mRemainder[mRemainderLen++] = data[offset]; |
| 461 | } |
| 462 | data += offset; |
| 463 | capacity -= offset; |
| 464 | if (mRemainderLen == bytesPerSample) { |
| 465 | inBuffer[0] = mRemainder; |
| 466 | inBufferSize[0] = bytesPerSample; |
| 467 | inargs.numInSamples = channelCount; |
| 468 | mRemainderLen = 0; |
| 469 | ALOGV("Processing remainder"); |
| 470 | } else { |
| 471 | // We have exhausted the input already |
| 472 | inargs.numInSamples = 0; |
| 473 | } |
| 474 | } |
| 475 | while (encoderErr == AACENC_OK && inargs.numInSamples >= channelCount) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 476 | if (numFrames && !block) { |
| 477 | C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE }; |
| 478 | // TODO: error handling, proper usage, etc. |
| 479 | c2_status_t err = pool->fetchLinearBlock(mOutBufferSize, usage, &block); |
| 480 | if (err != C2_OK) { |
| 481 | ALOGE("fetchLinearBlock failed : err = %d", err); |
| 482 | work->result = C2_NO_MEMORY; |
| 483 | return; |
| 484 | } |
| 485 | |
| 486 | wView.reset(new C2WriteView(block->map().get())); |
| 487 | outPtr = wView->data(); |
| 488 | outAvailable = wView->size(); |
| 489 | --numFrames; |
| 490 | } |
| 491 | |
| 492 | memset(&outargs, 0, sizeof(outargs)); |
| 493 | |
| 494 | outBuffer[0] = outPtr; |
| 495 | outBufferSize[0] = outAvailable; |
| 496 | |
| 497 | encoderErr = aacEncEncode(mAACEncoder, |
| 498 | &inBufDesc, |
| 499 | &outBufDesc, |
| 500 | &inargs, |
| 501 | &outargs); |
| 502 | |
| 503 | if (encoderErr == AACENC_OK) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 504 | if (outargs.numOutBytes > 0) { |
| 505 | mInputSize = 0; |
Wonsik Kim | 84889cb | 2019-01-03 17:07:54 -0800 | [diff] [blame] | 506 | int consumed = (capacity / sizeof(int16_t)) - inargs.numInSamples |
| 507 | + outargs.numInSamples; |
Wonsik Kim | 8c886ae | 2019-07-15 12:36:22 -0700 | [diff] [blame] | 508 | c2_cntr64_t currentFrameTimestampUs = mNextFrameTimestampUs; |
| 509 | mNextFrameTimestampUs = work->input.ordinal.timestamp |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 510 | + (consumed * 1000000ll / channelCount / sampleRate); |
Wonsik Kim | 8c886ae | 2019-07-15 12:36:22 -0700 | [diff] [blame] | 511 | std::shared_ptr<C2Buffer> buffer = createLinearBuffer(block, 0, outargs.numOutBytes); |
Wonsik Kim | 1f5583e | 2019-08-09 10:35:55 -0700 | [diff] [blame] | 512 | #if 0 |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 513 | hexdump(outPtr, std::min(outargs.numOutBytes, 256)); |
| 514 | #endif |
| 515 | outPtr = temp; |
| 516 | outAvailable = 0; |
| 517 | block.reset(); |
Wonsik Kim | 8c886ae | 2019-07-15 12:36:22 -0700 | [diff] [blame] | 518 | |
| 519 | outputBuffers.push_back({buffer, currentFrameTimestampUs}); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 520 | } else { |
| 521 | mInputSize += outargs.numInSamples * sizeof(int16_t); |
| 522 | } |
| 523 | |
| 524 | if (outargs.numInSamples > 0) { |
| 525 | inBuffer[0] = (int16_t *)inBuffer[0] + outargs.numInSamples; |
| 526 | inBufferSize[0] -= outargs.numInSamples * sizeof(int16_t); |
| 527 | inargs.numInSamples -= outargs.numInSamples; |
| 528 | } |
Wonsik Kim | 1f5583e | 2019-08-09 10:35:55 -0700 | [diff] [blame] | 529 | |
| 530 | if (inBuffer[0] == mRemainder) { |
| 531 | inBuffer[0] = const_cast<uint8_t *>(data); |
| 532 | inBufferSize[0] = capacity; |
| 533 | inargs.numInSamples = capacity / sizeof(int16_t); |
| 534 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 535 | } |
Wonsik Kim | 8c886ae | 2019-07-15 12:36:22 -0700 | [diff] [blame] | 536 | ALOGV("encoderErr = %d mInputSize = %zu " |
| 537 | "inargs.numInSamples = %d, mNextFrameTimestampUs = %lld", |
| 538 | encoderErr, mInputSize, inargs.numInSamples, mNextFrameTimestampUs.peekll()); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 539 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 540 | if (eos && inBufferSize[0] > 0) { |
| 541 | if (numFrames && !block) { |
| 542 | C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE }; |
| 543 | // TODO: error handling, proper usage, etc. |
| 544 | c2_status_t err = pool->fetchLinearBlock(mOutBufferSize, usage, &block); |
| 545 | if (err != C2_OK) { |
| 546 | ALOGE("fetchLinearBlock failed : err = %d", err); |
| 547 | work->result = C2_NO_MEMORY; |
| 548 | return; |
| 549 | } |
| 550 | |
| 551 | wView.reset(new C2WriteView(block->map().get())); |
| 552 | outPtr = wView->data(); |
| 553 | outAvailable = wView->size(); |
| 554 | --numFrames; |
| 555 | } |
| 556 | |
| 557 | memset(&outargs, 0, sizeof(outargs)); |
| 558 | |
| 559 | outBuffer[0] = outPtr; |
| 560 | outBufferSize[0] = outAvailable; |
| 561 | |
| 562 | // Flush |
| 563 | inargs.numInSamples = -1; |
| 564 | |
| 565 | (void)aacEncEncode(mAACEncoder, |
| 566 | &inBufDesc, |
| 567 | &outBufDesc, |
| 568 | &inargs, |
| 569 | &outargs); |
Wonsik Kim | 1f5583e | 2019-08-09 10:35:55 -0700 | [diff] [blame] | 570 | inBufferSize[0] = 0; |
| 571 | } |
| 572 | |
| 573 | if (inBufferSize[0] > 0) { |
| 574 | for (size_t i = 0; i < inBufferSize[0]; ++i) { |
| 575 | mRemainder[i] = static_cast<uint8_t *>(inBuffer[0])[i]; |
| 576 | } |
| 577 | mRemainderLen = inBufferSize[0]; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 578 | } |
| 579 | |
Wonsik Kim | 8c886ae | 2019-07-15 12:36:22 -0700 | [diff] [blame] | 580 | while (outputBuffers.size() > 1) { |
| 581 | const OutputBuffer& front = outputBuffers.front(); |
| 582 | C2WorkOrdinalStruct ordinal = work->input.ordinal; |
| 583 | ordinal.frameIndex = mOutIndex++; |
| 584 | ordinal.timestamp = front.timestampUs; |
| 585 | cloneAndSend( |
| 586 | inputIndex, |
| 587 | work, |
| 588 | FillWork(C2FrameData::FLAG_INCOMPLETE, ordinal, front.buffer)); |
| 589 | outputBuffers.pop_front(); |
| 590 | } |
| 591 | std::shared_ptr<C2Buffer> buffer; |
| 592 | C2WorkOrdinalStruct ordinal = work->input.ordinal; |
| 593 | ordinal.frameIndex = mOutIndex++; |
| 594 | if (!outputBuffers.empty()) { |
| 595 | ordinal.timestamp = outputBuffers.front().timestampUs; |
| 596 | buffer = outputBuffers.front().buffer; |
| 597 | } |
| 598 | // Mark the end of frame |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 599 | FillWork((C2FrameData::flags_t)(eos ? C2FrameData::FLAG_END_OF_STREAM : 0), |
Wonsik Kim | 8c886ae | 2019-07-15 12:36:22 -0700 | [diff] [blame] | 600 | ordinal, buffer)(work); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | c2_status_t C2SoftAacEnc::drain( |
| 604 | uint32_t drainMode, |
| 605 | const std::shared_ptr<C2BlockPool> &pool) { |
| 606 | switch (drainMode) { |
| 607 | case DRAIN_COMPONENT_NO_EOS: |
| 608 | [[fallthrough]]; |
| 609 | case NO_DRAIN: |
| 610 | // no-op |
| 611 | return C2_OK; |
| 612 | case DRAIN_CHAIN: |
| 613 | return C2_OMITTED; |
| 614 | case DRAIN_COMPONENT_WITH_EOS: |
| 615 | break; |
| 616 | default: |
| 617 | return C2_BAD_VALUE; |
| 618 | } |
| 619 | |
| 620 | (void)pool; |
| 621 | mSentCodecSpecificData = false; |
Wonsik Kim | 353e167 | 2019-01-07 16:31:29 -0800 | [diff] [blame] | 622 | mInputTimeSet = false; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 623 | mInputSize = 0u; |
Wonsik Kim | 8c886ae | 2019-07-15 12:36:22 -0700 | [diff] [blame] | 624 | mNextFrameTimestampUs = 0; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 625 | |
| 626 | // TODO: we don't have any pending work at this time to drain. |
| 627 | return C2_OK; |
| 628 | } |
| 629 | |
| 630 | class C2SoftAacEncFactory : public C2ComponentFactory { |
| 631 | public: |
| 632 | C2SoftAacEncFactory() : mHelper(std::static_pointer_cast<C2ReflectorHelper>( |
| 633 | GetCodec2PlatformComponentStore()->getParamReflector())) { |
| 634 | } |
| 635 | |
| 636 | virtual c2_status_t createComponent( |
| 637 | c2_node_id_t id, |
| 638 | std::shared_ptr<C2Component>* const component, |
| 639 | std::function<void(C2Component*)> deleter) override { |
| 640 | *component = std::shared_ptr<C2Component>( |
| 641 | new C2SoftAacEnc(COMPONENT_NAME, |
| 642 | id, |
| 643 | std::make_shared<C2SoftAacEnc::IntfImpl>(mHelper)), |
| 644 | deleter); |
| 645 | return C2_OK; |
| 646 | } |
| 647 | |
| 648 | virtual c2_status_t createInterface( |
| 649 | c2_node_id_t id, std::shared_ptr<C2ComponentInterface>* const interface, |
| 650 | std::function<void(C2ComponentInterface*)> deleter) override { |
| 651 | *interface = std::shared_ptr<C2ComponentInterface>( |
| 652 | new SimpleInterface<C2SoftAacEnc::IntfImpl>( |
| 653 | COMPONENT_NAME, id, std::make_shared<C2SoftAacEnc::IntfImpl>(mHelper)), |
| 654 | deleter); |
| 655 | return C2_OK; |
| 656 | } |
| 657 | |
| 658 | virtual ~C2SoftAacEncFactory() override = default; |
| 659 | |
| 660 | private: |
| 661 | std::shared_ptr<C2ReflectorHelper> mHelper; |
| 662 | }; |
| 663 | |
| 664 | } // namespace android |
| 665 | |
| 666 | extern "C" ::C2ComponentFactory* CreateCodec2Factory() { |
| 667 | ALOGV("in %s", __func__); |
| 668 | return new ::android::C2SoftAacEncFactory(); |
| 669 | } |
| 670 | |
| 671 | extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) { |
| 672 | ALOGV("in %s", __func__); |
| 673 | delete factory; |
| 674 | } |