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 
32 namespace {
33 
34 constexpr char COMPONENT_NAME[] = "c2.android.aac.encoder";
35 
36 }  // namespace
37 
38 class C2SoftAacEnc::IntfImpl : public SimpleInterface<void>::BaseParams {
39 public:
IntfImpl(const std::shared_ptr<C2ReflectorHelper> & helper)40     explicit IntfImpl(const std::shared_ptr<C2ReflectorHelper> &helper)
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();
52         setDerivedInstance(this);
53 
54         addParameter(
55                 DefineParam(mAttrib, C2_PARAMKEY_COMPONENT_ATTRIBUTES)
56                 .withConstValue(new C2ComponentAttributesSetting(
57                     C2Component::ATTRIB_IS_TEMPORAL))
58                 .build());
59 
60         addParameter(
61                 DefineParam(mSampleRate, C2_PARAMKEY_SAMPLE_RATE)
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(
70                 DefineParam(mChannelCount, C2_PARAMKEY_CHANNEL_COUNT)
71                 .withDefault(new C2StreamChannelCountInfo::input(0u, 1))
72                 .withFields({C2F(mChannelCount, value).inRange(1, kMaxChannelCount)})
73                 .withSetter(Setter<decltype(*mChannelCount)>::StrictValueWithNoDeps)
74                 .build());
75 
76         addParameter(
77                 DefineParam(mBitrate, C2_PARAMKEY_BITRATE)
78                 .withDefault(new C2StreamBitrateInfo::output(0u, 64000))
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());
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());
117     }
118 
getSampleRate() const119     uint32_t getSampleRate() const { return mSampleRate->value; }
getChannelCount() const120     uint32_t getChannelCount() const { return mChannelCount->value; }
getBitrate() const121     uint32_t getBitrate() const { return mBitrate->value; }
getSBRMode() const122     uint32_t getSBRMode() const { return mSBRMode->value; }
getProfile() const123     uint32_t getProfile() const { return mProfileLevel->profile; }
ProfileLevelSetter(bool mayBlock,C2P<C2StreamProfileLevelInfo::output> & me)124     static C2R ProfileLevelSetter(bool mayBlock, C2P<C2StreamProfileLevelInfo::output> &me) {
125         (void)mayBlock;
126         (void)me;  // TODO: validate
127         return C2R::Ok();
128     }
129 
MaxBufSizeCalculator(bool mayBlock,C2P<C2StreamMaxBufferSizeInfo::input> & me,const C2P<C2StreamChannelCountInfo::input> & channelCount)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:
140     std::shared_ptr<C2StreamSampleRateInfo::input> mSampleRate;
141     std::shared_ptr<C2StreamChannelCountInfo::input> mChannelCount;
142     std::shared_ptr<C2StreamBitrateInfo::output> mBitrate;
143     std::shared_ptr<C2StreamMaxBufferSizeInfo::input> mInputMaxBufSize;
144     std::shared_ptr<C2StreamProfileLevelInfo::output> mProfileLevel;
145     std::shared_ptr<C2StreamAacSbrModeTuning::input> mSBRMode;
146 };
147 
C2SoftAacEnc(const char * name,c2_node_id_t id,const std::shared_ptr<IntfImpl> & intfImpl)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),
155       mNumBytesPerInputFrame(0u),
156       mOutBufferSize(0u),
157       mSentCodecSpecificData(false),
158       mInputSize(0),
159       mSignalledError(false),
160       mOutIndex(0u),
161       mRemainderLen(0u) {
162 }
163 
~C2SoftAacEnc()164 C2SoftAacEnc::~C2SoftAacEnc() {
165     onReset();
166 }
167 
onInit()168 c2_status_t C2SoftAacEnc::onInit() {
169     status_t err = initEncoder();
170     return err == OK ? C2_OK : C2_CORRUPTED;
171 }
172 
initEncoder()173 status_t C2SoftAacEnc::initEncoder() {
174     if (AACENC_OK != aacEncOpen(&mAACEncoder, 0, 0)) {
175         ALOGE("Failed to init AAC encoder");
176         return UNKNOWN_ERROR;
177     }
178     return setAudioParams();
179 }
180 
onStop()181 c2_status_t C2SoftAacEnc::onStop() {
182     mSentCodecSpecificData = false;
183     mInputSize = 0u;
184     mNextFrameTimestampUs.reset();
185     mLastFrameEndTimestampUs.reset();
186     mSignalledError = false;
187     mRemainderLen = 0;
188     return C2_OK;
189 }
190 
onReset()191 void C2SoftAacEnc::onReset() {
192     (void)onStop();
193     aacEncClose(&mAACEncoder);
194 }
195 
onRelease()196 void C2SoftAacEnc::onRelease() {
197     // no-op
198 }
199 
onFlush_sm()200 c2_status_t C2SoftAacEnc::onFlush_sm() {
201     if (mAACEncoder != nullptr) {
202         /* encoder's internal input buffer needs to be reset during flush */
203         if (AACENC_OK != aacEncoder_SetParam(mAACEncoder, AACENC_CONTROL_STATE, AACENC_INIT_ALL)) {
204             ALOGE("Failed to reset AAC encoder");
205         }
206     }
207     mSentCodecSpecificData = false;
208     mInputSize = 0u;
209     mNextFrameTimestampUs.reset();
210     mLastFrameEndTimestampUs.reset();
211     mRemainderLen = 0;
212     return C2_OK;
213 }
214 
getChannelMode(uint32_t nChannels)215 static CHANNEL_MODE getChannelMode(uint32_t nChannels) {
216     CHANNEL_MODE chMode = MODE_INVALID;
217     switch (nChannels) {
218         case 1: chMode = MODE_1; break;
219         case 2: chMode = MODE_2; break;
220         case 3: chMode = MODE_1_2; break;
221         case 4: chMode = MODE_1_2_1; break;
222         case 5: chMode = MODE_1_2_2; break;
223         case 6: chMode = MODE_1_2_2_1; break;
224         default: chMode = MODE_INVALID;
225     }
226     return chMode;
227 }
228 
getAOTFromProfile(uint32_t profile)229 static AUDIO_OBJECT_TYPE getAOTFromProfile(uint32_t profile) {
230    if (profile == C2Config::PROFILE_AAC_LC) {
231        return AOT_AAC_LC;
232    } else if (profile == C2Config::PROFILE_AAC_HE) {
233        return AOT_SBR;
234    } else if (profile == C2Config::PROFILE_AAC_HE_PS) {
235        return AOT_PS;
236    } else if (profile == C2Config::PROFILE_AAC_LD) {
237        return AOT_ER_AAC_LD;
238    } else if (profile == C2Config::PROFILE_AAC_ELD) {
239        return AOT_ER_AAC_ELD;
240    } else {
241        ALOGW("Unsupported AAC profile - defaulting to AAC-LC");
242        return AOT_AAC_LC;
243    }
244 }
245 
setAudioParams()246 status_t C2SoftAacEnc::setAudioParams() {
247     // We call this whenever sample rate, number of channels, bitrate or SBR mode change
248     // in reponse to setParameter calls.
249     int32_t sbrRatio = 0;
250     uint32_t sbrMode = mIntf->getSBRMode();
251     if (sbrMode == AAC_SBR_SINGLE_RATE) sbrRatio = 1;
252     else if (sbrMode == AAC_SBR_DUAL_RATE) sbrRatio = 2;
253 
254     ALOGV("setAudioParams: %u Hz, %u channels, %u bps, %i sbr mode, %i sbr ratio",
255          mIntf->getSampleRate(), mIntf->getChannelCount(), mIntf->getBitrate(),
256          sbrMode, sbrRatio);
257 
258     uint32_t aacProfile = mIntf->getProfile();
259     if (AACENC_OK != aacEncoder_SetParam(mAACEncoder, AACENC_AOT, getAOTFromProfile(aacProfile))) {
260         ALOGE("Failed to set AAC encoder parameters");
261         return UNKNOWN_ERROR;
262     }
263 
264     if (AACENC_OK != aacEncoder_SetParam(mAACEncoder, AACENC_SAMPLERATE, mIntf->getSampleRate())) {
265         ALOGE("Failed to set AAC encoder parameters");
266         return UNKNOWN_ERROR;
267     }
268     if (AACENC_OK != aacEncoder_SetParam(mAACEncoder, AACENC_BITRATE, mIntf->getBitrate())) {
269         ALOGE("Failed to set AAC encoder parameters");
270         return UNKNOWN_ERROR;
271     }
272     if (AACENC_OK != aacEncoder_SetParam(mAACEncoder, AACENC_CHANNELMODE,
273             getChannelMode(mIntf->getChannelCount()))) {
274         ALOGE("Failed to set AAC encoder parameters");
275         return UNKNOWN_ERROR;
276     }
277     if (AACENC_OK != aacEncoder_SetParam(mAACEncoder, AACENC_TRANSMUX, TT_MP4_RAW)) {
278         ALOGE("Failed to set AAC encoder parameters");
279         return UNKNOWN_ERROR;
280     }
281 
282     if (sbrMode != C2Config::AAC_SBR_AUTO && aacProfile == C2Config::PROFILE_AAC_ELD) {
283         int aacSbrMode = sbrMode != C2Config::AAC_SBR_OFF;
284         if (AACENC_OK != aacEncoder_SetParam(mAACEncoder, AACENC_SBR_MODE, aacSbrMode)) {
285             ALOGE("Failed to set AAC encoder parameters");
286             return UNKNOWN_ERROR;
287         }
288     }
289 
290     /* SBR ratio parameter configurations:
291        0: Default configuration wherein SBR ratio is configured depending on audio object type by
292           the FDK.
293        1: Downsampled SBR (default for ELD)
294        2: Dualrate SBR (default for HE-AAC)
295      */
296     if (AACENC_OK != aacEncoder_SetParam(mAACEncoder, AACENC_SBR_RATIO, sbrRatio)) {
297         ALOGE("Failed to set AAC encoder parameters");
298         return UNKNOWN_ERROR;
299     }
300 
301     return OK;
302 }
303 
MaybeLogTimestampWarning(long long lastFrameEndTimestampUs,long long inputTimestampUs)304 static void MaybeLogTimestampWarning(
305         long long lastFrameEndTimestampUs, long long inputTimestampUs) {
306     using Clock = std::chrono::steady_clock;
307     thread_local Clock::time_point sLastLogTimestamp{};
308     thread_local int32_t sOverlapCount = -1;
309     if (Clock::now() - sLastLogTimestamp > std::chrono::minutes(1) || sOverlapCount < 0) {
310         AString countMessage = "";
311         if (sOverlapCount > 0) {
312             countMessage = AStringPrintf(
313                     "(%d overlapping timestamp detected since last log)", sOverlapCount);
314         }
315         ALOGI("Correcting overlapping timestamp: last frame ended at %lldus but "
316                 "current frame is starting at %lldus. Using the last frame's end timestamp %s",
317                 lastFrameEndTimestampUs, inputTimestampUs, countMessage.c_str());
318         sLastLogTimestamp = Clock::now();
319         sOverlapCount = 0;
320     } else {
321         ALOGV("Correcting overlapping timestamp: last frame ended at %lldus but "
322                 "current frame is starting at %lldus. Using the last frame's end timestamp",
323                 lastFrameEndTimestampUs, inputTimestampUs);
324         ++sOverlapCount;
325     }
326 }
327 
process(const std::unique_ptr<C2Work> & work,const std::shared_ptr<C2BlockPool> & pool)328 void C2SoftAacEnc::process(
329         const std::unique_ptr<C2Work> &work,
330         const std::shared_ptr<C2BlockPool> &pool) {
331     // Initialize output work
332     work->result = C2_OK;
333     work->workletsProcessed = 1u;
334     work->worklets.front()->output.flags = work->input.flags;
335 
336     if (mSignalledError) {
337         return;
338     }
339     bool eos = (work->input.flags & C2FrameData::FLAG_END_OF_STREAM) != 0;
340 
341     uint32_t sampleRate = mIntf->getSampleRate();
342     uint32_t channelCount = mIntf->getChannelCount();
343 
344     if (!mSentCodecSpecificData) {
345         // The very first thing we want to output is the codec specific
346         // data.
347 
348         if (AACENC_OK != aacEncEncode(mAACEncoder, nullptr, nullptr, nullptr, nullptr)) {
349             ALOGE("Unable to initialize encoder for profile / sample-rate / bit-rate / channels");
350             mSignalledError = true;
351             work->result = C2_CORRUPTED;
352             return;
353         }
354 
355         uint32_t bitrate = mIntf->getBitrate();
356         uint32_t actualBitRate = aacEncoder_GetParam(mAACEncoder, AACENC_BITRATE);
357         if (bitrate != actualBitRate) {
358             ALOGW("Requested bitrate %u unsupported, using %u", bitrate, actualBitRate);
359         }
360 
361         AACENC_InfoStruct encInfo;
362         if (AACENC_OK != aacEncInfo(mAACEncoder, &encInfo)) {
363             ALOGE("Failed to get AAC encoder info");
364             mSignalledError = true;
365             work->result = C2_CORRUPTED;
366             return;
367         }
368 
369         std::unique_ptr<C2StreamInitDataInfo::output> csd =
370             C2StreamInitDataInfo::output::AllocUnique(encInfo.confSize, 0u);
371         if (!csd) {
372             ALOGE("CSD allocation failed");
373             mSignalledError = true;
374             work->result = C2_NO_MEMORY;
375             return;
376         }
377         memcpy(csd->m.value, encInfo.confBuf, encInfo.confSize);
378         ALOGV("put csd");
379 #if defined(LOG_NDEBUG) && !LOG_NDEBUG
380         hexdump(csd->m.value, csd->flexCount());
381 #endif
382         work->worklets.front()->output.configUpdate.push_back(std::move(csd));
383 
384         mOutBufferSize = encInfo.maxOutBufBytes;
385         mNumBytesPerInputFrame = encInfo.frameLength * channelCount * sizeof(int16_t);
386 
387         mSentCodecSpecificData = true;
388     }
389 
390     uint8_t temp[1];
391     C2ReadView view = mDummyReadView;
392     const uint8_t *data = temp;
393     size_t capacity = 0u;
394     if (!work->input.buffers.empty()) {
395         view = work->input.buffers[0]->data().linearBlocks().front().map().get();
396         data = view.data();
397         capacity = view.capacity();
398     }
399     c2_cntr64_t inputTimestampUs = work->input.ordinal.timestamp;
400     if (inputTimestampUs < mLastFrameEndTimestampUs.value_or(inputTimestampUs)) {
401         MaybeLogTimestampWarning(mLastFrameEndTimestampUs->peekll(), inputTimestampUs.peekll());
402         inputTimestampUs = *mLastFrameEndTimestampUs;
403     }
404     if (capacity > 0) {
405         if (!mNextFrameTimestampUs) {
406             mNextFrameTimestampUs = work->input.ordinal.timestamp;
407         }
408         mLastFrameEndTimestampUs = inputTimestampUs
409                 + (capacity / sizeof(int16_t) * 1000000ll / channelCount / sampleRate);
410     }
411 
412     size_t numFrames =
413         (mRemainderLen + capacity + mInputSize + (eos ? mNumBytesPerInputFrame - 1 : 0))
414         / mNumBytesPerInputFrame;
415     ALOGV("capacity = %zu; mInputSize = %zu; numFrames = %zu "
416           "mNumBytesPerInputFrame = %u inputTS = %lld remaining = %zu",
417           capacity, mInputSize, numFrames, mNumBytesPerInputFrame, inputTimestampUs.peekll(),
418           mRemainderLen);
419 
420     std::shared_ptr<C2LinearBlock> block;
421     std::unique_ptr<C2WriteView> wView;
422     uint8_t *outPtr = temp;
423     size_t outAvailable = 0u;
424     uint64_t inputIndex = work->input.ordinal.frameIndex.peeku();
425     size_t bytesPerSample = channelCount * sizeof(int16_t);
426 
427     AACENC_InArgs inargs;
428     AACENC_OutArgs outargs;
429     memset(&inargs, 0, sizeof(inargs));
430     memset(&outargs, 0, sizeof(outargs));
431     inargs.numInSamples = capacity / sizeof(int16_t);
432 
433     void* inBuffer[]        = { (unsigned char *)data };
434     INT   inBufferIds[]     = { IN_AUDIO_DATA };
435     INT   inBufferSize[]    = { (INT)capacity };
436     INT   inBufferElSize[]  = { sizeof(int16_t) };
437 
438     AACENC_BufDesc inBufDesc;
439     inBufDesc.numBufs           = sizeof(inBuffer) / sizeof(void*);
440     inBufDesc.bufs              = (void**)&inBuffer;
441     inBufDesc.bufferIdentifiers = inBufferIds;
442     inBufDesc.bufSizes          = inBufferSize;
443     inBufDesc.bufElSizes        = inBufferElSize;
444 
445     void* outBuffer[]       = { outPtr };
446     INT   outBufferIds[]    = { OUT_BITSTREAM_DATA };
447     INT   outBufferSize[]   = { 0 };
448     INT   outBufferElSize[] = { sizeof(UCHAR) };
449 
450     AACENC_BufDesc outBufDesc;
451     outBufDesc.numBufs           = sizeof(outBuffer) / sizeof(void*);
452     outBufDesc.bufs              = (void**)&outBuffer;
453     outBufDesc.bufferIdentifiers = outBufferIds;
454     outBufDesc.bufSizes          = outBufferSize;
455     outBufDesc.bufElSizes        = outBufferElSize;
456 
457     AACENC_ERROR encoderErr = AACENC_OK;
458 
459     class FillWork {
460     public:
461         FillWork(uint32_t flags, C2WorkOrdinalStruct ordinal,
462                  const std::shared_ptr<C2Buffer> &buffer)
463             : mFlags(flags), mOrdinal(ordinal), mBuffer(buffer) {
464         }
465         ~FillWork() = default;
466 
467         void operator()(const std::unique_ptr<C2Work> &work) {
468             work->worklets.front()->output.flags = (C2FrameData::flags_t)mFlags;
469             work->worklets.front()->output.buffers.clear();
470             work->worklets.front()->output.ordinal = mOrdinal;
471             work->workletsProcessed = 1u;
472             work->result = C2_OK;
473             if (mBuffer) {
474                 work->worklets.front()->output.buffers.push_back(mBuffer);
475             }
476             ALOGV("timestamp = %lld, index = %lld, w/%s buffer",
477                   mOrdinal.timestamp.peekll(),
478                   mOrdinal.frameIndex.peekll(),
479                   mBuffer ? "" : "o");
480         }
481 
482     private:
483         const uint32_t mFlags;
484         const C2WorkOrdinalStruct mOrdinal;
485         const std::shared_ptr<C2Buffer> mBuffer;
486     };
487 
488     struct OutputBuffer {
489         std::shared_ptr<C2Buffer> buffer;
490         c2_cntr64_t timestampUs;
491     };
492     std::list<OutputBuffer> outputBuffers;
493 
494     if (mRemainderLen > 0) {
495         size_t offset = 0;
496         for (; mRemainderLen < bytesPerSample && offset < capacity; ++offset) {
497             mRemainder[mRemainderLen++] = data[offset];
498         }
499         data += offset;
500         capacity -= offset;
501         if (mRemainderLen == bytesPerSample) {
502             inBuffer[0] = mRemainder;
503             inBufferSize[0] = bytesPerSample;
504             inargs.numInSamples = channelCount;
505             mRemainderLen = 0;
506             ALOGV("Processing remainder");
507         } else {
508             // We have exhausted the input already
509             inargs.numInSamples = 0;
510         }
511     }
512     while (encoderErr == AACENC_OK && inargs.numInSamples >= channelCount) {
513         if (numFrames && !block) {
514             C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
515             // TODO: error handling, proper usage, etc.
516             c2_status_t err = pool->fetchLinearBlock(mOutBufferSize, usage, &block);
517             if (err != C2_OK) {
518                 ALOGE("fetchLinearBlock failed : err = %d", err);
519                 work->result = C2_NO_MEMORY;
520                 return;
521             }
522 
523             wView.reset(new C2WriteView(block->map().get()));
524             outPtr = wView->data();
525             outAvailable = wView->size();
526             --numFrames;
527         }
528 
529         memset(&outargs, 0, sizeof(outargs));
530 
531         outBuffer[0] = outPtr;
532         outBufferSize[0] = outAvailable;
533 
534         encoderErr = aacEncEncode(mAACEncoder,
535                                   &inBufDesc,
536                                   &outBufDesc,
537                                   &inargs,
538                                   &outargs);
539 
540         if (encoderErr == AACENC_OK) {
541             if (outargs.numOutBytes > 0) {
542                 mInputSize = 0;
543                 int consumed = (capacity / sizeof(int16_t)) - inargs.numInSamples
544                         + outargs.numInSamples;
545                 ALOGV("consumed = %d, capacity = %zu, inSamples = %d, outSamples = %d",
546                       consumed, capacity, inargs.numInSamples, outargs.numInSamples);
547                 c2_cntr64_t currentFrameTimestampUs = *mNextFrameTimestampUs;
548                 mNextFrameTimestampUs = inputTimestampUs
549                         + (consumed * 1000000ll / channelCount / sampleRate);
550                 std::shared_ptr<C2Buffer> buffer = createLinearBuffer(block, 0, outargs.numOutBytes);
551 #if 0
552                 hexdump(outPtr, std::min(outargs.numOutBytes, 256));
553 #endif
554                 outPtr = temp;
555                 outAvailable = 0;
556                 block.reset();
557 
558                 outputBuffers.push_back({buffer, currentFrameTimestampUs});
559             } else {
560                 mInputSize += outargs.numInSamples * sizeof(int16_t);
561             }
562 
563             if (inBuffer[0] == mRemainder) {
564                 inBuffer[0] = const_cast<uint8_t *>(data);
565                 inBufferSize[0] = capacity;
566                 inargs.numInSamples = capacity / sizeof(int16_t);
567             } else if (outargs.numInSamples > 0) {
568                 inBuffer[0] = (int16_t *)inBuffer[0] + outargs.numInSamples;
569                 inBufferSize[0] -= outargs.numInSamples * sizeof(int16_t);
570                 inargs.numInSamples -= outargs.numInSamples;
571             }
572         } else {
573             // In case of error in encode call, discard remaining input bytes.
574             inBuffer[0] = nullptr;
575             inBufferSize[0] = 0;
576             inargs.numInSamples = 0;
577         }
578         ALOGV("encoderErr = %d mInputSize = %zu "
579               "inargs.numInSamples = %d, mNextFrameTimestampUs = %lld",
580               encoderErr, mInputSize, inargs.numInSamples, mNextFrameTimestampUs->peekll());
581     }
582     if (eos && inBufferSize[0] > 0) {
583         if (numFrames && !block) {
584             C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
585             // TODO: error handling, proper usage, etc.
586             c2_status_t err = pool->fetchLinearBlock(mOutBufferSize, usage, &block);
587             if (err != C2_OK) {
588                 ALOGE("fetchLinearBlock failed : err = %d", err);
589                 work->result = C2_NO_MEMORY;
590                 return;
591             }
592 
593             wView.reset(new C2WriteView(block->map().get()));
594             outPtr = wView->data();
595             outAvailable = wView->size();
596             --numFrames;
597         }
598 
599         memset(&outargs, 0, sizeof(outargs));
600 
601         outBuffer[0] = outPtr;
602         outBufferSize[0] = outAvailable;
603 
604         // Flush
605         inargs.numInSamples = -1;
606 
607         (void)aacEncEncode(mAACEncoder,
608                            &inBufDesc,
609                            &outBufDesc,
610                            &inargs,
611                            &outargs);
612 
613         // after flush, discard remaining input bytes.
614         inBuffer[0] = nullptr;
615         inBufferSize[0] = 0;
616     }
617 
618     if (inBufferSize[0] > 0) {
619         if (inBufferSize[0] > kRemainderBufSize) {
620             ALOGE("Remaining bytes %d greater than remainder buffer size %zu", inBufferSize[0],
621                     kRemainderBufSize);
622             work->result = C2_CORRUPTED;
623             return;
624         }
625         for (size_t i = 0; i < inBufferSize[0]; ++i) {
626             mRemainder[i] = static_cast<uint8_t *>(inBuffer[0])[i];
627         }
628         mRemainderLen = inBufferSize[0];
629     }
630 
631     while (outputBuffers.size() > 1) {
632         const OutputBuffer& front = outputBuffers.front();
633         C2WorkOrdinalStruct ordinal = work->input.ordinal;
634         ordinal.frameIndex = mOutIndex++;
635         ordinal.timestamp = front.timestampUs;
636         cloneAndSend(
637                 inputIndex,
638                 work,
639                 FillWork(C2FrameData::FLAG_INCOMPLETE, ordinal, front.buffer));
640         outputBuffers.pop_front();
641     }
642     std::shared_ptr<C2Buffer> buffer;
643     C2WorkOrdinalStruct ordinal = work->input.ordinal;
644     ordinal.frameIndex = mOutIndex++;
645     if (!outputBuffers.empty()) {
646         ordinal.timestamp = outputBuffers.front().timestampUs;
647         buffer = outputBuffers.front().buffer;
648     }
649     // Mark the end of frame
650     FillWork((C2FrameData::flags_t)(eos ? C2FrameData::FLAG_END_OF_STREAM : 0),
651              ordinal, buffer)(work);
652 }
653 
drain(uint32_t drainMode,const std::shared_ptr<C2BlockPool> & pool)654 c2_status_t C2SoftAacEnc::drain(
655         uint32_t drainMode,
656         const std::shared_ptr<C2BlockPool> &pool) {
657     switch (drainMode) {
658         case DRAIN_COMPONENT_NO_EOS:
659             [[fallthrough]];
660         case NO_DRAIN:
661             // no-op
662             return C2_OK;
663         case DRAIN_CHAIN:
664             return C2_OMITTED;
665         case DRAIN_COMPONENT_WITH_EOS:
666             break;
667         default:
668             return C2_BAD_VALUE;
669     }
670 
671     (void)pool;
672     mSentCodecSpecificData = false;
673     mInputSize = 0u;
674     mNextFrameTimestampUs.reset();
675     mLastFrameEndTimestampUs.reset();
676 
677     // TODO: we don't have any pending work at this time to drain.
678     return C2_OK;
679 }
680 
681 class C2SoftAacEncFactory : public C2ComponentFactory {
682 public:
C2SoftAacEncFactory()683     C2SoftAacEncFactory() : mHelper(std::static_pointer_cast<C2ReflectorHelper>(
684             GetCodec2PlatformComponentStore()->getParamReflector())) {
685     }
686 
createComponent(c2_node_id_t id,std::shared_ptr<C2Component> * const component,std::function<void (C2Component *)> deleter)687     virtual c2_status_t createComponent(
688             c2_node_id_t id,
689             std::shared_ptr<C2Component>* const component,
690             std::function<void(C2Component*)> deleter) override {
691         *component = std::shared_ptr<C2Component>(
692                 new C2SoftAacEnc(COMPONENT_NAME,
693                                  id,
694                                  std::make_shared<C2SoftAacEnc::IntfImpl>(mHelper)),
695                 deleter);
696         return C2_OK;
697     }
698 
createInterface(c2_node_id_t id,std::shared_ptr<C2ComponentInterface> * const interface,std::function<void (C2ComponentInterface *)> deleter)699     virtual c2_status_t createInterface(
700             c2_node_id_t id, std::shared_ptr<C2ComponentInterface>* const interface,
701             std::function<void(C2ComponentInterface*)> deleter) override {
702         *interface = std::shared_ptr<C2ComponentInterface>(
703                 new SimpleInterface<C2SoftAacEnc::IntfImpl>(
704                         COMPONENT_NAME, id, std::make_shared<C2SoftAacEnc::IntfImpl>(mHelper)),
705                 deleter);
706         return C2_OK;
707     }
708 
709     virtual ~C2SoftAacEncFactory() override = default;
710 
711 private:
712     std::shared_ptr<C2ReflectorHelper> mHelper;
713 };
714 
715 }  // namespace android
716 
717 __attribute__((cfi_canonical_jump_table))
CreateCodec2Factory()718 extern "C" ::C2ComponentFactory* CreateCodec2Factory() {
719     ALOGV("in %s", __func__);
720     return new ::android::C2SoftAacEncFactory();
721 }
722 
723 __attribute__((cfi_canonical_jump_table))
DestroyCodec2Factory(::C2ComponentFactory * factory)724 extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) {
725     ALOGV("in %s", __func__);
726     delete factory;
727 }
728