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, 6)})
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 mSentCodecSpecificData = false;
202 mInputSize = 0u;
203 mNextFrameTimestampUs.reset();
204 mLastFrameEndTimestampUs.reset();
205 return C2_OK;
206 }
207
getChannelMode(uint32_t nChannels)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
getAOTFromProfile(uint32_t profile)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 }
238
setAudioParams()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.
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;
246
247 ALOGV("setAudioParams: %u Hz, %u channels, %u bps, %i sbr mode, %i sbr ratio",
248 mIntf->getSampleRate(), mIntf->getChannelCount(), mIntf->getBitrate(),
249 sbrMode, sbrRatio);
250
251 uint32_t aacProfile = mIntf->getProfile();
252 if (AACENC_OK != aacEncoder_SetParam(mAACEncoder, AACENC_AOT, getAOTFromProfile(aacProfile))) {
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
275 if (sbrMode != C2Config::AAC_SBR_AUTO && aacProfile == C2Config::PROFILE_AAC_ELD) {
276 int aacSbrMode = sbrMode != C2Config::AAC_SBR_OFF;
277 if (AACENC_OK != aacEncoder_SetParam(mAACEncoder, AACENC_SBR_MODE, aacSbrMode)) {
278 ALOGE("Failed to set AAC encoder parameters");
279 return UNKNOWN_ERROR;
280 }
281 }
282
283 /* SBR ratio parameter configurations:
284 0: Default configuration wherein SBR ratio is configured depending on audio object type by
285 the FDK.
286 1: Downsampled SBR (default for ELD)
287 2: Dualrate SBR (default for HE-AAC)
288 */
289 if (AACENC_OK != aacEncoder_SetParam(mAACEncoder, AACENC_SBR_RATIO, sbrRatio)) {
290 ALOGE("Failed to set AAC encoder parameters");
291 return UNKNOWN_ERROR;
292 }
293
294 return OK;
295 }
296
MaybeLogTimestampWarning(long long lastFrameEndTimestampUs,long long inputTimestampUs)297 static void MaybeLogTimestampWarning(
298 long long lastFrameEndTimestampUs, long long inputTimestampUs) {
299 using Clock = std::chrono::steady_clock;
300 thread_local Clock::time_point sLastLogTimestamp{};
301 thread_local int32_t sOverlapCount = -1;
302 if (Clock::now() - sLastLogTimestamp > std::chrono::minutes(1) || sOverlapCount < 0) {
303 AString countMessage = "";
304 if (sOverlapCount > 0) {
305 countMessage = AStringPrintf(
306 "(%d overlapping timestamp detected since last log)", sOverlapCount);
307 }
308 ALOGI("Correcting overlapping timestamp: last frame ended at %lldus but "
309 "current frame is starting at %lldus. Using the last frame's end timestamp %s",
310 lastFrameEndTimestampUs, inputTimestampUs, countMessage.c_str());
311 sLastLogTimestamp = Clock::now();
312 sOverlapCount = 0;
313 } else {
314 ALOGV("Correcting overlapping timestamp: last frame ended at %lldus but "
315 "current frame is starting at %lldus. Using the last frame's end timestamp",
316 lastFrameEndTimestampUs, inputTimestampUs);
317 ++sOverlapCount;
318 }
319 }
320
process(const std::unique_ptr<C2Work> & work,const std::shared_ptr<C2BlockPool> & pool)321 void C2SoftAacEnc::process(
322 const std::unique_ptr<C2Work> &work,
323 const std::shared_ptr<C2BlockPool> &pool) {
324 // Initialize output work
325 work->result = C2_OK;
326 work->workletsProcessed = 1u;
327 work->worklets.front()->output.flags = work->input.flags;
328
329 if (mSignalledError) {
330 return;
331 }
332 bool eos = (work->input.flags & C2FrameData::FLAG_END_OF_STREAM) != 0;
333
334 uint32_t sampleRate = mIntf->getSampleRate();
335 uint32_t channelCount = mIntf->getChannelCount();
336
337 if (!mSentCodecSpecificData) {
338 // The very first thing we want to output is the codec specific
339 // data.
340
341 if (AACENC_OK != aacEncEncode(mAACEncoder, nullptr, nullptr, nullptr, nullptr)) {
342 ALOGE("Unable to initialize encoder for profile / sample-rate / bit-rate / channels");
343 mSignalledError = true;
344 work->result = C2_CORRUPTED;
345 return;
346 }
347
348 uint32_t bitrate = mIntf->getBitrate();
349 uint32_t actualBitRate = aacEncoder_GetParam(mAACEncoder, AACENC_BITRATE);
350 if (bitrate != actualBitRate) {
351 ALOGW("Requested bitrate %u unsupported, using %u", bitrate, actualBitRate);
352 }
353
354 AACENC_InfoStruct encInfo;
355 if (AACENC_OK != aacEncInfo(mAACEncoder, &encInfo)) {
356 ALOGE("Failed to get AAC encoder info");
357 mSignalledError = true;
358 work->result = C2_CORRUPTED;
359 return;
360 }
361
362 std::unique_ptr<C2StreamInitDataInfo::output> csd =
363 C2StreamInitDataInfo::output::AllocUnique(encInfo.confSize, 0u);
364 if (!csd) {
365 ALOGE("CSD allocation failed");
366 mSignalledError = true;
367 work->result = C2_NO_MEMORY;
368 return;
369 }
370 memcpy(csd->m.value, encInfo.confBuf, encInfo.confSize);
371 ALOGV("put csd");
372 #if defined(LOG_NDEBUG) && !LOG_NDEBUG
373 hexdump(csd->m.value, csd->flexCount());
374 #endif
375 work->worklets.front()->output.configUpdate.push_back(std::move(csd));
376
377 mOutBufferSize = encInfo.maxOutBufBytes;
378 mNumBytesPerInputFrame = encInfo.frameLength * channelCount * sizeof(int16_t);
379
380 mSentCodecSpecificData = true;
381 }
382
383 uint8_t temp[1];
384 C2ReadView view = mDummyReadView;
385 const uint8_t *data = temp;
386 size_t capacity = 0u;
387 if (!work->input.buffers.empty()) {
388 view = work->input.buffers[0]->data().linearBlocks().front().map().get();
389 data = view.data();
390 capacity = view.capacity();
391 }
392 c2_cntr64_t inputTimestampUs = work->input.ordinal.timestamp;
393 if (inputTimestampUs < mLastFrameEndTimestampUs.value_or(inputTimestampUs)) {
394 MaybeLogTimestampWarning(mLastFrameEndTimestampUs->peekll(), inputTimestampUs.peekll());
395 inputTimestampUs = *mLastFrameEndTimestampUs;
396 }
397 if (capacity > 0) {
398 if (!mNextFrameTimestampUs) {
399 mNextFrameTimestampUs = work->input.ordinal.timestamp;
400 }
401 mLastFrameEndTimestampUs = inputTimestampUs
402 + (capacity / sizeof(int16_t) * 1000000ll / channelCount / sampleRate);
403 }
404
405 size_t numFrames =
406 (mRemainderLen + capacity + mInputSize + (eos ? mNumBytesPerInputFrame - 1 : 0))
407 / mNumBytesPerInputFrame;
408 ALOGV("capacity = %zu; mInputSize = %zu; numFrames = %zu "
409 "mNumBytesPerInputFrame = %u inputTS = %lld remaining = %zu",
410 capacity, mInputSize, numFrames, mNumBytesPerInputFrame, inputTimestampUs.peekll(),
411 mRemainderLen);
412
413 std::shared_ptr<C2LinearBlock> block;
414 std::unique_ptr<C2WriteView> wView;
415 uint8_t *outPtr = temp;
416 size_t outAvailable = 0u;
417 uint64_t inputIndex = work->input.ordinal.frameIndex.peeku();
418 size_t bytesPerSample = channelCount * sizeof(int16_t);
419
420 AACENC_InArgs inargs;
421 AACENC_OutArgs outargs;
422 memset(&inargs, 0, sizeof(inargs));
423 memset(&outargs, 0, sizeof(outargs));
424 inargs.numInSamples = capacity / sizeof(int16_t);
425
426 void* inBuffer[] = { (unsigned char *)data };
427 INT inBufferIds[] = { IN_AUDIO_DATA };
428 INT inBufferSize[] = { (INT)capacity };
429 INT inBufferElSize[] = { sizeof(int16_t) };
430
431 AACENC_BufDesc inBufDesc;
432 inBufDesc.numBufs = sizeof(inBuffer) / sizeof(void*);
433 inBufDesc.bufs = (void**)&inBuffer;
434 inBufDesc.bufferIdentifiers = inBufferIds;
435 inBufDesc.bufSizes = inBufferSize;
436 inBufDesc.bufElSizes = inBufferElSize;
437
438 void* outBuffer[] = { outPtr };
439 INT outBufferIds[] = { OUT_BITSTREAM_DATA };
440 INT outBufferSize[] = { 0 };
441 INT outBufferElSize[] = { sizeof(UCHAR) };
442
443 AACENC_BufDesc outBufDesc;
444 outBufDesc.numBufs = sizeof(outBuffer) / sizeof(void*);
445 outBufDesc.bufs = (void**)&outBuffer;
446 outBufDesc.bufferIdentifiers = outBufferIds;
447 outBufDesc.bufSizes = outBufferSize;
448 outBufDesc.bufElSizes = outBufferElSize;
449
450 AACENC_ERROR encoderErr = AACENC_OK;
451
452 class FillWork {
453 public:
454 FillWork(uint32_t flags, C2WorkOrdinalStruct ordinal,
455 const std::shared_ptr<C2Buffer> &buffer)
456 : mFlags(flags), mOrdinal(ordinal), mBuffer(buffer) {
457 }
458 ~FillWork() = default;
459
460 void operator()(const std::unique_ptr<C2Work> &work) {
461 work->worklets.front()->output.flags = (C2FrameData::flags_t)mFlags;
462 work->worklets.front()->output.buffers.clear();
463 work->worklets.front()->output.ordinal = mOrdinal;
464 work->workletsProcessed = 1u;
465 work->result = C2_OK;
466 if (mBuffer) {
467 work->worklets.front()->output.buffers.push_back(mBuffer);
468 }
469 ALOGV("timestamp = %lld, index = %lld, w/%s buffer",
470 mOrdinal.timestamp.peekll(),
471 mOrdinal.frameIndex.peekll(),
472 mBuffer ? "" : "o");
473 }
474
475 private:
476 const uint32_t mFlags;
477 const C2WorkOrdinalStruct mOrdinal;
478 const std::shared_ptr<C2Buffer> mBuffer;
479 };
480
481 struct OutputBuffer {
482 std::shared_ptr<C2Buffer> buffer;
483 c2_cntr64_t timestampUs;
484 };
485 std::list<OutputBuffer> outputBuffers;
486
487 if (mRemainderLen > 0) {
488 size_t offset = 0;
489 for (; mRemainderLen < bytesPerSample && offset < capacity; ++offset) {
490 mRemainder[mRemainderLen++] = data[offset];
491 }
492 data += offset;
493 capacity -= offset;
494 if (mRemainderLen == bytesPerSample) {
495 inBuffer[0] = mRemainder;
496 inBufferSize[0] = bytesPerSample;
497 inargs.numInSamples = channelCount;
498 mRemainderLen = 0;
499 ALOGV("Processing remainder");
500 } else {
501 // We have exhausted the input already
502 inargs.numInSamples = 0;
503 }
504 }
505 while (encoderErr == AACENC_OK && inargs.numInSamples >= channelCount) {
506 if (numFrames && !block) {
507 C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
508 // TODO: error handling, proper usage, etc.
509 c2_status_t err = pool->fetchLinearBlock(mOutBufferSize, usage, &block);
510 if (err != C2_OK) {
511 ALOGE("fetchLinearBlock failed : err = %d", err);
512 work->result = C2_NO_MEMORY;
513 return;
514 }
515
516 wView.reset(new C2WriteView(block->map().get()));
517 outPtr = wView->data();
518 outAvailable = wView->size();
519 --numFrames;
520 }
521
522 memset(&outargs, 0, sizeof(outargs));
523
524 outBuffer[0] = outPtr;
525 outBufferSize[0] = outAvailable;
526
527 encoderErr = aacEncEncode(mAACEncoder,
528 &inBufDesc,
529 &outBufDesc,
530 &inargs,
531 &outargs);
532
533 if (encoderErr == AACENC_OK) {
534 if (outargs.numOutBytes > 0) {
535 mInputSize = 0;
536 int consumed = (capacity / sizeof(int16_t)) - inargs.numInSamples
537 + outargs.numInSamples;
538 ALOGV("consumed = %d, capacity = %zu, inSamples = %d, outSamples = %d",
539 consumed, capacity, inargs.numInSamples, outargs.numInSamples);
540 c2_cntr64_t currentFrameTimestampUs = *mNextFrameTimestampUs;
541 mNextFrameTimestampUs = inputTimestampUs
542 + (consumed * 1000000ll / channelCount / sampleRate);
543 std::shared_ptr<C2Buffer> buffer = createLinearBuffer(block, 0, outargs.numOutBytes);
544 #if 0
545 hexdump(outPtr, std::min(outargs.numOutBytes, 256));
546 #endif
547 outPtr = temp;
548 outAvailable = 0;
549 block.reset();
550
551 outputBuffers.push_back({buffer, currentFrameTimestampUs});
552 } else {
553 mInputSize += outargs.numInSamples * sizeof(int16_t);
554 }
555
556 if (inBuffer[0] == mRemainder) {
557 inBuffer[0] = const_cast<uint8_t *>(data);
558 inBufferSize[0] = capacity;
559 inargs.numInSamples = capacity / sizeof(int16_t);
560 } else if (outargs.numInSamples > 0) {
561 inBuffer[0] = (int16_t *)inBuffer[0] + outargs.numInSamples;
562 inBufferSize[0] -= outargs.numInSamples * sizeof(int16_t);
563 inargs.numInSamples -= outargs.numInSamples;
564 }
565 }
566 ALOGV("encoderErr = %d mInputSize = %zu "
567 "inargs.numInSamples = %d, mNextFrameTimestampUs = %lld",
568 encoderErr, mInputSize, inargs.numInSamples, mNextFrameTimestampUs->peekll());
569 }
570 if (eos && inBufferSize[0] > 0) {
571 if (numFrames && !block) {
572 C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
573 // TODO: error handling, proper usage, etc.
574 c2_status_t err = pool->fetchLinearBlock(mOutBufferSize, usage, &block);
575 if (err != C2_OK) {
576 ALOGE("fetchLinearBlock failed : err = %d", err);
577 work->result = C2_NO_MEMORY;
578 return;
579 }
580
581 wView.reset(new C2WriteView(block->map().get()));
582 outPtr = wView->data();
583 outAvailable = wView->size();
584 --numFrames;
585 }
586
587 memset(&outargs, 0, sizeof(outargs));
588
589 outBuffer[0] = outPtr;
590 outBufferSize[0] = outAvailable;
591
592 // Flush
593 inargs.numInSamples = -1;
594
595 (void)aacEncEncode(mAACEncoder,
596 &inBufDesc,
597 &outBufDesc,
598 &inargs,
599 &outargs);
600 inBufferSize[0] = 0;
601 }
602
603 if (inBufferSize[0] > 0) {
604 for (size_t i = 0; i < inBufferSize[0]; ++i) {
605 mRemainder[i] = static_cast<uint8_t *>(inBuffer[0])[i];
606 }
607 mRemainderLen = inBufferSize[0];
608 }
609
610 while (outputBuffers.size() > 1) {
611 const OutputBuffer& front = outputBuffers.front();
612 C2WorkOrdinalStruct ordinal = work->input.ordinal;
613 ordinal.frameIndex = mOutIndex++;
614 ordinal.timestamp = front.timestampUs;
615 cloneAndSend(
616 inputIndex,
617 work,
618 FillWork(C2FrameData::FLAG_INCOMPLETE, ordinal, front.buffer));
619 outputBuffers.pop_front();
620 }
621 std::shared_ptr<C2Buffer> buffer;
622 C2WorkOrdinalStruct ordinal = work->input.ordinal;
623 ordinal.frameIndex = mOutIndex++;
624 if (!outputBuffers.empty()) {
625 ordinal.timestamp = outputBuffers.front().timestampUs;
626 buffer = outputBuffers.front().buffer;
627 }
628 // Mark the end of frame
629 FillWork((C2FrameData::flags_t)(eos ? C2FrameData::FLAG_END_OF_STREAM : 0),
630 ordinal, buffer)(work);
631 }
632
drain(uint32_t drainMode,const std::shared_ptr<C2BlockPool> & pool)633 c2_status_t C2SoftAacEnc::drain(
634 uint32_t drainMode,
635 const std::shared_ptr<C2BlockPool> &pool) {
636 switch (drainMode) {
637 case DRAIN_COMPONENT_NO_EOS:
638 [[fallthrough]];
639 case NO_DRAIN:
640 // no-op
641 return C2_OK;
642 case DRAIN_CHAIN:
643 return C2_OMITTED;
644 case DRAIN_COMPONENT_WITH_EOS:
645 break;
646 default:
647 return C2_BAD_VALUE;
648 }
649
650 (void)pool;
651 mSentCodecSpecificData = false;
652 mInputSize = 0u;
653 mNextFrameTimestampUs.reset();
654 mLastFrameEndTimestampUs.reset();
655
656 // TODO: we don't have any pending work at this time to drain.
657 return C2_OK;
658 }
659
660 class C2SoftAacEncFactory : public C2ComponentFactory {
661 public:
C2SoftAacEncFactory()662 C2SoftAacEncFactory() : mHelper(std::static_pointer_cast<C2ReflectorHelper>(
663 GetCodec2PlatformComponentStore()->getParamReflector())) {
664 }
665
createComponent(c2_node_id_t id,std::shared_ptr<C2Component> * const component,std::function<void (C2Component *)> deleter)666 virtual c2_status_t createComponent(
667 c2_node_id_t id,
668 std::shared_ptr<C2Component>* const component,
669 std::function<void(C2Component*)> deleter) override {
670 *component = std::shared_ptr<C2Component>(
671 new C2SoftAacEnc(COMPONENT_NAME,
672 id,
673 std::make_shared<C2SoftAacEnc::IntfImpl>(mHelper)),
674 deleter);
675 return C2_OK;
676 }
677
createInterface(c2_node_id_t id,std::shared_ptr<C2ComponentInterface> * const interface,std::function<void (C2ComponentInterface *)> deleter)678 virtual c2_status_t createInterface(
679 c2_node_id_t id, std::shared_ptr<C2ComponentInterface>* const interface,
680 std::function<void(C2ComponentInterface*)> deleter) override {
681 *interface = std::shared_ptr<C2ComponentInterface>(
682 new SimpleInterface<C2SoftAacEnc::IntfImpl>(
683 COMPONENT_NAME, id, std::make_shared<C2SoftAacEnc::IntfImpl>(mHelper)),
684 deleter);
685 return C2_OK;
686 }
687
688 virtual ~C2SoftAacEncFactory() override = default;
689
690 private:
691 std::shared_ptr<C2ReflectorHelper> mHelper;
692 };
693
694 } // namespace android
695
696 __attribute__((cfi_canonical_jump_table))
CreateCodec2Factory()697 extern "C" ::C2ComponentFactory* CreateCodec2Factory() {
698 ALOGV("in %s", __func__);
699 return new ::android::C2SoftAacEncFactory();
700 }
701
702 __attribute__((cfi_canonical_jump_table))
DestroyCodec2Factory(::C2ComponentFactory * factory)703 extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) {
704 ALOGV("in %s", __func__);
705 delete factory;
706 }
707