blob: be52a1db3483cf86d9dd679a0fe22831b2b7dd43 [file] [log] [blame]
Pawin Vongmasa36653902018-11-15 00:10:25 -08001/*
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
30namespace android {
31
Rakesh Kumar66d9d062019-03-12 17:46:17 +053032namespace {
33
34constexpr char COMPONENT_NAME[] = "c2.android.aac.encoder";
35
36} // namespace
37
38class C2SoftAacEnc::IntfImpl : public SimpleInterface<void>::BaseParams {
Pawin Vongmasa36653902018-11-15 00:10:25 -080039public:
40 explicit IntfImpl(const std::shared_ptr<C2ReflectorHelper> &helper)
Rakesh Kumar66d9d062019-03-12 17:46:17 +053041 : 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 Vongmasa36653902018-11-15 00:10:25 -080052 setDerivedInstance(this);
53
54 addParameter(
Rakesh Kumar66d9d062019-03-12 17:46:17 +053055 DefineParam(mAttrib, C2_PARAMKEY_COMPONENT_ATTRIBUTES)
56 .withConstValue(new C2ComponentAttributesSetting(
57 C2Component::ATTRIB_IS_TEMPORAL))
Pawin Vongmasa36653902018-11-15 00:10:25 -080058 .build());
59
60 addParameter(
Lajos Molnar3bb81cd2019-02-20 15:10:30 -080061 DefineParam(mSampleRate, C2_PARAMKEY_SAMPLE_RATE)
Pawin Vongmasa36653902018-11-15 00:10:25 -080062 .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 Molnar3bb81cd2019-02-20 15:10:30 -080070 DefineParam(mChannelCount, C2_PARAMKEY_CHANNEL_COUNT)
Pawin Vongmasa36653902018-11-15 00:10:25 -080071 .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 Molnar3bb81cd2019-02-20 15:10:30 -080077 DefineParam(mBitrate, C2_PARAMKEY_BITRATE)
78 .withDefault(new C2StreamBitrateInfo::output(0u, 64000))
Pawin Vongmasa36653902018-11-15 00:10:25 -080079 .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 Jajoob09409a2019-05-23 18:57:52 +0530106
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 Vongmasa36653902018-11-15 00:10:25 -0800117 }
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 Jajoob09409a2019-05-23 18:57:52 +0530122 uint32_t getSBRMode() const { return mSBRMode->value; }
123 uint32_t getProfile() const { return mProfileLevel->profile; }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800124 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
139private:
Pawin Vongmasa36653902018-11-15 00:10:25 -0800140 std::shared_ptr<C2StreamSampleRateInfo::input> mSampleRate;
141 std::shared_ptr<C2StreamChannelCountInfo::input> mChannelCount;
Lajos Molnar3bb81cd2019-02-20 15:10:30 -0800142 std::shared_ptr<C2StreamBitrateInfo::output> mBitrate;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800143 std::shared_ptr<C2StreamMaxBufferSizeInfo::input> mInputMaxBufSize;
144 std::shared_ptr<C2StreamProfileLevelInfo::output> mProfileLevel;
Manisha Jajoob09409a2019-05-23 18:57:52 +0530145 std::shared_ptr<C2StreamAacSbrModeTuning::input> mSBRMode;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800146};
147
Pawin Vongmasa36653902018-11-15 00:10:25 -0800148C2SoftAacEnc::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 Vongmasa36653902018-11-15 00:10:25 -0800155 mNumBytesPerInputFrame(0u),
156 mOutBufferSize(0u),
157 mSentCodecSpecificData(false),
Wonsik Kim353e1672019-01-07 16:31:29 -0800158 mInputTimeSet(false),
Pawin Vongmasa36653902018-11-15 00:10:25 -0800159 mInputSize(0),
Wonsik Kim8c886ae2019-07-15 12:36:22 -0700160 mNextFrameTimestampUs(0),
Pawin Vongmasa36653902018-11-15 00:10:25 -0800161 mSignalledError(false),
Wonsik Kim3dd7bd32019-08-09 10:35:55 -0700162 mOutIndex(0u),
163 mRemainderLen(0u) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800164}
165
166C2SoftAacEnc::~C2SoftAacEnc() {
167 onReset();
168}
169
170c2_status_t C2SoftAacEnc::onInit() {
171 status_t err = initEncoder();
172 return err == OK ? C2_OK : C2_CORRUPTED;
173}
174
175status_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
183c2_status_t C2SoftAacEnc::onStop() {
184 mSentCodecSpecificData = false;
Wonsik Kim353e1672019-01-07 16:31:29 -0800185 mInputTimeSet = false;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800186 mInputSize = 0u;
Wonsik Kim8c886ae2019-07-15 12:36:22 -0700187 mNextFrameTimestampUs = 0;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800188 mSignalledError = false;
Wonsik Kim3dd7bd32019-08-09 10:35:55 -0700189 mRemainderLen = 0;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800190 return C2_OK;
191}
192
193void C2SoftAacEnc::onReset() {
194 (void)onStop();
195 aacEncClose(&mAACEncoder);
196}
197
198void C2SoftAacEnc::onRelease() {
199 // no-op
200}
201
202c2_status_t C2SoftAacEnc::onFlush_sm() {
203 mSentCodecSpecificData = false;
Wonsik Kim353e1672019-01-07 16:31:29 -0800204 mInputTimeSet = false;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800205 mInputSize = 0u;
Wonsik Kim8c886ae2019-07-15 12:36:22 -0700206 mNextFrameTimestampUs = 0;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800207 return C2_OK;
208}
209
210static 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 Jajoob09409a2019-05-23 18:57:52 +0530224static 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 Vongmasa36653902018-11-15 00:10:25 -0800240
241status_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 Jajoob09409a2019-05-23 18:57:52 +0530244 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 Vongmasa36653902018-11-15 00:10:25 -0800248
249 ALOGV("setAudioParams: %u Hz, %u channels, %u bps, %i sbr mode, %i sbr ratio",
Manisha Jajoob09409a2019-05-23 18:57:52 +0530250 mIntf->getSampleRate(), mIntf->getChannelCount(), mIntf->getBitrate(),
251 sbrMode, sbrRatio);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800252
Manisha Jajoob09409a2019-05-23 18:57:52 +0530253 uint32_t aacProfile = mIntf->getProfile();
254 if (AACENC_OK != aacEncoder_SetParam(mAACEncoder, AACENC_AOT, getAOTFromProfile(aacProfile))) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800255 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 Jajoob09409a2019-05-23 18:57:52 +0530277 if (sbrMode != -1 && aacProfile == C2Config::PROFILE_AAC_ELD) {
278 if (AACENC_OK != aacEncoder_SetParam(mAACEncoder, AACENC_SBR_MODE, sbrMode)) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800279 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 Jajoob09409a2019-05-23 18:57:52 +0530290 if (AACENC_OK != aacEncoder_SetParam(mAACEncoder, AACENC_SBR_RATIO, sbrRatio)) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800291 ALOGE("Failed to set AAC encoder parameters");
292 return UNKNOWN_ERROR;
293 }
294
295 return OK;
296}
297
298void 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 Molnar3bb81cd2019-02-20 15:10:30 -0800339 std::unique_ptr<C2StreamInitDataInfo::output> csd =
340 C2StreamInitDataInfo::output::AllocUnique(encInfo.confSize, 0u);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800341 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 Vongmasa36653902018-11-15 00:10:25 -0800356
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 Kim353e1672019-01-07 16:31:29 -0800369 if (!mInputTimeSet && capacity > 0) {
Wonsik Kim8c886ae2019-07-15 12:36:22 -0700370 mNextFrameTimestampUs = work->input.ordinal.timestamp;
Wonsik Kim353e1672019-01-07 16:31:29 -0800371 mInputTimeSet = true;
372 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800373
Wonsik Kim3dd7bd32019-08-09 10:35:55 -0700374 size_t numFrames =
375 (mRemainderLen + capacity + mInputSize + (eos ? mNumBytesPerInputFrame - 1 : 0))
376 / mNumBytesPerInputFrame;
Wonsik Kim8c886ae2019-07-15 12:36:22 -0700377 ALOGV("capacity = %zu; mInputSize = %zu; numFrames = %zu "
Wonsik Kim3dd7bd32019-08-09 10:35:55 -0700378 "mNumBytesPerInputFrame = %u inputTS = %lld remaining = %zu",
Wonsik Kim8c886ae2019-07-15 12:36:22 -0700379 capacity, mInputSize, numFrames,
Wonsik Kim3dd7bd32019-08-09 10:35:55 -0700380 mNumBytesPerInputFrame, work->input.ordinal.timestamp.peekll(),
381 mRemainderLen);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800382
383 std::shared_ptr<C2LinearBlock> block;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800384 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 Kim3dd7bd32019-08-09 10:35:55 -0700388 size_t bytesPerSample = channelCount * sizeof(int16_t);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800389
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 Kim8c886ae2019-07-15 12:36:22 -0700451 struct OutputBuffer {
452 std::shared_ptr<C2Buffer> buffer;
453 c2_cntr64_t timestampUs;
454 };
455 std::list<OutputBuffer> outputBuffers;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800456
Wonsik Kim3dd7bd32019-08-09 10:35:55 -0700457 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 Vongmasa36653902018-11-15 00:10:25 -0800476 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 Vongmasa36653902018-11-15 00:10:25 -0800504 if (outargs.numOutBytes > 0) {
505 mInputSize = 0;
Wonsik Kim84889cb2019-01-03 17:07:54 -0800506 int consumed = (capacity / sizeof(int16_t)) - inargs.numInSamples
507 + outargs.numInSamples;
Wonsik Kim8c886ae2019-07-15 12:36:22 -0700508 c2_cntr64_t currentFrameTimestampUs = mNextFrameTimestampUs;
509 mNextFrameTimestampUs = work->input.ordinal.timestamp
Pawin Vongmasa36653902018-11-15 00:10:25 -0800510 + (consumed * 1000000ll / channelCount / sampleRate);
Wonsik Kim8c886ae2019-07-15 12:36:22 -0700511 std::shared_ptr<C2Buffer> buffer = createLinearBuffer(block, 0, outargs.numOutBytes);
Wonsik Kim3dd7bd32019-08-09 10:35:55 -0700512#if 0
Pawin Vongmasa36653902018-11-15 00:10:25 -0800513 hexdump(outPtr, std::min(outargs.numOutBytes, 256));
514#endif
515 outPtr = temp;
516 outAvailable = 0;
517 block.reset();
Wonsik Kim8c886ae2019-07-15 12:36:22 -0700518
519 outputBuffers.push_back({buffer, currentFrameTimestampUs});
Pawin Vongmasa36653902018-11-15 00:10:25 -0800520 } else {
521 mInputSize += outargs.numInSamples * sizeof(int16_t);
522 }
523
Wonsik Kim3dd7bd32019-08-09 10:35:55 -0700524 if (inBuffer[0] == mRemainder) {
525 inBuffer[0] = const_cast<uint8_t *>(data);
526 inBufferSize[0] = capacity;
527 inargs.numInSamples = capacity / sizeof(int16_t);
Wonsik Kimf15bccb2019-10-23 14:18:39 -0700528 } else if (outargs.numInSamples > 0) {
529 inBuffer[0] = (int16_t *)inBuffer[0] + outargs.numInSamples;
530 inBufferSize[0] -= outargs.numInSamples * sizeof(int16_t);
531 inargs.numInSamples -= outargs.numInSamples;
Wonsik Kim3dd7bd32019-08-09 10:35:55 -0700532 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800533 }
Wonsik Kim8c886ae2019-07-15 12:36:22 -0700534 ALOGV("encoderErr = %d mInputSize = %zu "
535 "inargs.numInSamples = %d, mNextFrameTimestampUs = %lld",
536 encoderErr, mInputSize, inargs.numInSamples, mNextFrameTimestampUs.peekll());
Pawin Vongmasa36653902018-11-15 00:10:25 -0800537 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800538 if (eos && inBufferSize[0] > 0) {
539 if (numFrames && !block) {
540 C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
541 // TODO: error handling, proper usage, etc.
542 c2_status_t err = pool->fetchLinearBlock(mOutBufferSize, usage, &block);
543 if (err != C2_OK) {
544 ALOGE("fetchLinearBlock failed : err = %d", err);
545 work->result = C2_NO_MEMORY;
546 return;
547 }
548
549 wView.reset(new C2WriteView(block->map().get()));
550 outPtr = wView->data();
551 outAvailable = wView->size();
552 --numFrames;
553 }
554
555 memset(&outargs, 0, sizeof(outargs));
556
557 outBuffer[0] = outPtr;
558 outBufferSize[0] = outAvailable;
559
560 // Flush
561 inargs.numInSamples = -1;
562
563 (void)aacEncEncode(mAACEncoder,
564 &inBufDesc,
565 &outBufDesc,
566 &inargs,
567 &outargs);
Wonsik Kim3dd7bd32019-08-09 10:35:55 -0700568 inBufferSize[0] = 0;
569 }
570
571 if (inBufferSize[0] > 0) {
572 for (size_t i = 0; i < inBufferSize[0]; ++i) {
573 mRemainder[i] = static_cast<uint8_t *>(inBuffer[0])[i];
574 }
575 mRemainderLen = inBufferSize[0];
Pawin Vongmasa36653902018-11-15 00:10:25 -0800576 }
577
Wonsik Kim8c886ae2019-07-15 12:36:22 -0700578 while (outputBuffers.size() > 1) {
579 const OutputBuffer& front = outputBuffers.front();
580 C2WorkOrdinalStruct ordinal = work->input.ordinal;
581 ordinal.frameIndex = mOutIndex++;
582 ordinal.timestamp = front.timestampUs;
583 cloneAndSend(
584 inputIndex,
585 work,
586 FillWork(C2FrameData::FLAG_INCOMPLETE, ordinal, front.buffer));
587 outputBuffers.pop_front();
588 }
589 std::shared_ptr<C2Buffer> buffer;
590 C2WorkOrdinalStruct ordinal = work->input.ordinal;
591 ordinal.frameIndex = mOutIndex++;
592 if (!outputBuffers.empty()) {
593 ordinal.timestamp = outputBuffers.front().timestampUs;
594 buffer = outputBuffers.front().buffer;
595 }
596 // Mark the end of frame
Pawin Vongmasa36653902018-11-15 00:10:25 -0800597 FillWork((C2FrameData::flags_t)(eos ? C2FrameData::FLAG_END_OF_STREAM : 0),
Wonsik Kim8c886ae2019-07-15 12:36:22 -0700598 ordinal, buffer)(work);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800599}
600
601c2_status_t C2SoftAacEnc::drain(
602 uint32_t drainMode,
603 const std::shared_ptr<C2BlockPool> &pool) {
604 switch (drainMode) {
605 case DRAIN_COMPONENT_NO_EOS:
606 [[fallthrough]];
607 case NO_DRAIN:
608 // no-op
609 return C2_OK;
610 case DRAIN_CHAIN:
611 return C2_OMITTED;
612 case DRAIN_COMPONENT_WITH_EOS:
613 break;
614 default:
615 return C2_BAD_VALUE;
616 }
617
618 (void)pool;
619 mSentCodecSpecificData = false;
Wonsik Kim353e1672019-01-07 16:31:29 -0800620 mInputTimeSet = false;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800621 mInputSize = 0u;
Wonsik Kim8c886ae2019-07-15 12:36:22 -0700622 mNextFrameTimestampUs = 0;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800623
624 // TODO: we don't have any pending work at this time to drain.
625 return C2_OK;
626}
627
628class C2SoftAacEncFactory : public C2ComponentFactory {
629public:
630 C2SoftAacEncFactory() : mHelper(std::static_pointer_cast<C2ReflectorHelper>(
631 GetCodec2PlatformComponentStore()->getParamReflector())) {
632 }
633
634 virtual c2_status_t createComponent(
635 c2_node_id_t id,
636 std::shared_ptr<C2Component>* const component,
637 std::function<void(C2Component*)> deleter) override {
638 *component = std::shared_ptr<C2Component>(
639 new C2SoftAacEnc(COMPONENT_NAME,
640 id,
641 std::make_shared<C2SoftAacEnc::IntfImpl>(mHelper)),
642 deleter);
643 return C2_OK;
644 }
645
646 virtual c2_status_t createInterface(
647 c2_node_id_t id, std::shared_ptr<C2ComponentInterface>* const interface,
648 std::function<void(C2ComponentInterface*)> deleter) override {
649 *interface = std::shared_ptr<C2ComponentInterface>(
650 new SimpleInterface<C2SoftAacEnc::IntfImpl>(
651 COMPONENT_NAME, id, std::make_shared<C2SoftAacEnc::IntfImpl>(mHelper)),
652 deleter);
653 return C2_OK;
654 }
655
656 virtual ~C2SoftAacEncFactory() override = default;
657
658private:
659 std::shared_ptr<C2ReflectorHelper> mHelper;
660};
661
662} // namespace android
663
664extern "C" ::C2ComponentFactory* CreateCodec2Factory() {
665 ALOGV("in %s", __func__);
666 return new ::android::C2SoftAacEncFactory();
667}
668
669extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) {
670 ALOGV("in %s", __func__);
671 delete factory;
672}