blob: 7b58c9b054e52fd4e61b4cf3d0c4b7355ecb5911 [file] [log] [blame]
Manisha Jajooc237cbc2018-11-16 18:56:20 +05301/*
2 * Copyright (C) 2019 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 "C2SoftOpusEnc"
19#include <utils/Log.h>
20
21#include <C2PlatformSupport.h>
22#include <SimpleC2Interface.h>
23#include <media/stagefright/foundation/MediaDefs.h>
24#include <media/stagefright/foundation/OpusHeader.h>
25#include "C2SoftOpusEnc.h"
26
27extern "C" {
28 #include <opus.h>
29 #include <opus_multistream.h>
30}
31
32#define DEFAULT_FRAME_DURATION_MS 20
33namespace android {
34
Rakesh Kumar66d9d062019-03-12 17:46:17 +053035namespace {
36
Manisha Jajooc237cbc2018-11-16 18:56:20 +053037constexpr char COMPONENT_NAME[] = "c2.android.opus.encoder";
38
Rakesh Kumar66d9d062019-03-12 17:46:17 +053039} // namespace
40
41class C2SoftOpusEnc::IntfImpl : public SimpleInterface<void>::BaseParams {
Manisha Jajooc237cbc2018-11-16 18:56:20 +053042public:
43 explicit IntfImpl(const std::shared_ptr<C2ReflectorHelper> &helper)
Rakesh Kumar66d9d062019-03-12 17:46:17 +053044 : SimpleInterface<void>::BaseParams(
45 helper,
46 COMPONENT_NAME,
47 C2Component::KIND_ENCODER,
48 C2Component::DOMAIN_AUDIO,
49 MEDIA_MIMETYPE_AUDIO_OPUS) {
50 noPrivateBuffers();
51 noInputReferences();
52 noOutputReferences();
53 noInputLatency();
54 noTimeStretch();
Manisha Jajooc237cbc2018-11-16 18:56:20 +053055 setDerivedInstance(this);
56
57 addParameter(
Rakesh Kumar66d9d062019-03-12 17:46:17 +053058 DefineParam(mAttrib, C2_PARAMKEY_COMPONENT_ATTRIBUTES)
59 .withConstValue(new C2ComponentAttributesSetting(
60 C2Component::ATTRIB_IS_TEMPORAL))
Manisha Jajooc237cbc2018-11-16 18:56:20 +053061 .build());
62
63 addParameter(
Lajos Molnar3bb81cd2019-02-20 15:10:30 -080064 DefineParam(mSampleRate, C2_PARAMKEY_SAMPLE_RATE)
Manisha Jajooc237cbc2018-11-16 18:56:20 +053065 .withDefault(new C2StreamSampleRateInfo::input(0u, 48000))
66 .withFields({C2F(mSampleRate, value).oneOf({
67 8000, 12000, 16000, 24000, 48000})})
68 .withSetter((Setter<decltype(*mSampleRate)>::StrictValueWithNoDeps))
69 .build());
70
71 addParameter(
Lajos Molnar3bb81cd2019-02-20 15:10:30 -080072 DefineParam(mChannelCount, C2_PARAMKEY_CHANNEL_COUNT)
Manisha Jajooc237cbc2018-11-16 18:56:20 +053073 .withDefault(new C2StreamChannelCountInfo::input(0u, 1))
74 .withFields({C2F(mChannelCount, value).inRange(1, 8)})
75 .withSetter((Setter<decltype(*mChannelCount)>::StrictValueWithNoDeps))
76 .build());
77
78 addParameter(
Lajos Molnar3bb81cd2019-02-20 15:10:30 -080079 DefineParam(mBitrate, C2_PARAMKEY_BITRATE)
80 .withDefault(new C2StreamBitrateInfo::output(0u, 128000))
Manisha Jajooc237cbc2018-11-16 18:56:20 +053081 .withFields({C2F(mBitrate, value).inRange(500, 512000)})
82 .withSetter(Setter<decltype(*mBitrate)>::NonStrictValueWithNoDeps)
83 .build());
84
85 addParameter(
86 DefineParam(mComplexity, C2_PARAMKEY_COMPLEXITY)
87 .withDefault(new C2StreamComplexityTuning::output(0u, 10))
88 .withFields({C2F(mComplexity, value).inRange(1, 10)})
89 .withSetter(Setter<decltype(*mComplexity)>::NonStrictValueWithNoDeps)
90 .build());
91
92 addParameter(
93 DefineParam(mInputMaxBufSize, C2_PARAMKEY_INPUT_MAX_BUFFER_SIZE)
94 .withConstValue(new C2StreamMaxBufferSizeInfo::input(0u, 3840))
95 .build());
96 }
97
98 uint32_t getSampleRate() const { return mSampleRate->value; }
99 uint32_t getChannelCount() const { return mChannelCount->value; }
100 uint32_t getBitrate() const { return mBitrate->value; }
101 uint32_t getComplexity() const { return mComplexity->value; }
102
103private:
Manisha Jajooc237cbc2018-11-16 18:56:20 +0530104 std::shared_ptr<C2StreamSampleRateInfo::input> mSampleRate;
105 std::shared_ptr<C2StreamChannelCountInfo::input> mChannelCount;
Lajos Molnar3bb81cd2019-02-20 15:10:30 -0800106 std::shared_ptr<C2StreamBitrateInfo::output> mBitrate;
Manisha Jajooc237cbc2018-11-16 18:56:20 +0530107 std::shared_ptr<C2StreamComplexityTuning::output> mComplexity;
108 std::shared_ptr<C2StreamMaxBufferSizeInfo::input> mInputMaxBufSize;
109};
110
111C2SoftOpusEnc::C2SoftOpusEnc(const char* name, c2_node_id_t id,
112 const std::shared_ptr<IntfImpl>& intfImpl)
113 : SimpleC2Component(
114 std::make_shared<SimpleInterface<IntfImpl>>(name, id, intfImpl)),
115 mIntf(intfImpl),
116 mOutputBlock(nullptr),
117 mEncoder(nullptr),
118 mInputBufferPcm16(nullptr),
119 mOutIndex(0u) {
120}
121
122C2SoftOpusEnc::~C2SoftOpusEnc() {
123 onRelease();
124}
125
126c2_status_t C2SoftOpusEnc::onInit() {
127 return initEncoder();
128}
129
130c2_status_t C2SoftOpusEnc::configureEncoder() {
131 unsigned char mono_mapping[256] = {0};
132 unsigned char stereo_mapping[256] = {0, 1};
133 unsigned char surround_mapping[256] = {0, 1, 255};
134 mSampleRate = mIntf->getSampleRate();
135 mChannelCount = mIntf->getChannelCount();
136 uint32_t bitrate = mIntf->getBitrate();
137 int complexity = mIntf->getComplexity();
138 mNumSamplesPerFrame = mSampleRate / (1000 / mFrameDurationMs);
139 mNumPcmBytesPerInputFrame =
140 mChannelCount * mNumSamplesPerFrame * sizeof(int16_t);
141 int err = C2_OK;
142
143 unsigned char* mapping;
144 if (mChannelCount < 2) {
145 mapping = mono_mapping;
146 } else if (mChannelCount == 2) {
147 mapping = stereo_mapping;
148 } else {
149 mapping = surround_mapping;
150 }
151
152 if (mEncoder != nullptr) {
153 opus_multistream_encoder_destroy(mEncoder);
154 }
155
156 mEncoder = opus_multistream_encoder_create(mSampleRate, mChannelCount,
157 1, 1, mapping, OPUS_APPLICATION_AUDIO, &err);
158 if (err) {
159 ALOGE("Could not create libopus encoder. Error code: %i", err);
160 return C2_CORRUPTED;
161 }
162
163 // Complexity
164 if (opus_multistream_encoder_ctl(
165 mEncoder, OPUS_SET_COMPLEXITY(complexity)) != OPUS_OK) {
166 ALOGE("failed to set complexity");
167 return C2_BAD_VALUE;
168 }
169
170 // DTX
171 if (opus_multistream_encoder_ctl(mEncoder, OPUS_SET_DTX(0) != OPUS_OK)) {
172 ALOGE("failed to set dtx");
173 return C2_BAD_VALUE;
174 }
175
176 // Application
177 if (opus_multistream_encoder_ctl(mEncoder,
178 OPUS_SET_APPLICATION(OPUS_APPLICATION_AUDIO)) != OPUS_OK) {
179 ALOGE("failed to set application");
180 return C2_BAD_VALUE;
181 }
182
183 // Signal type
184 if (opus_multistream_encoder_ctl(mEncoder, OPUS_SET_SIGNAL(OPUS_AUTO)) !=
185 OPUS_OK) {
186 ALOGE("failed to set signal");
187 return C2_BAD_VALUE;
188 }
189
James O'Learyffd6cbc2019-04-26 10:56:03 -0400190 // Constrained VBR
191 if (opus_multistream_encoder_ctl(mEncoder, OPUS_SET_VBR(1) != OPUS_OK)) {
Manisha Jajooc237cbc2018-11-16 18:56:20 +0530192 ALOGE("failed to set vbr type");
193 return C2_BAD_VALUE;
194 }
James O'Learyffd6cbc2019-04-26 10:56:03 -0400195 if (opus_multistream_encoder_ctl(mEncoder, OPUS_SET_VBR_CONSTRAINT(1) !=
Manisha Jajooc237cbc2018-11-16 18:56:20 +0530196 OPUS_OK)) {
197 ALOGE("failed to set vbr constraint");
198 return C2_BAD_VALUE;
199 }
200
201 // Bitrate
202 if (opus_multistream_encoder_ctl(mEncoder, OPUS_SET_BITRATE(bitrate)) !=
203 OPUS_OK) {
204 ALOGE("failed to set bitrate");
205 return C2_BAD_VALUE;
206 }
207
208 // Get codecDelay
209 int32_t lookahead;
210 if (opus_multistream_encoder_ctl(mEncoder, OPUS_GET_LOOKAHEAD(&lookahead)) !=
211 OPUS_OK) {
212 ALOGE("failed to get lookahead");
213 return C2_BAD_VALUE;
214 }
215 mCodecDelay = lookahead * 1000000000ll / mSampleRate;
216
217 // Set seek preroll to 80 ms
218 mSeekPreRoll = 80000000;
219 return C2_OK;
220}
221
222c2_status_t C2SoftOpusEnc::initEncoder() {
223 mSignalledEos = false;
224 mSignalledError = false;
225 mHeaderGenerated = false;
226 mIsFirstFrame = true;
227 mEncoderFlushed = false;
228 mBufferAvailable = false;
229 mAnchorTimeStamp = 0ull;
230 mProcessedSamples = 0;
231 mFilledLen = 0;
232 mFrameDurationMs = DEFAULT_FRAME_DURATION_MS;
233 if (!mInputBufferPcm16) {
234 mInputBufferPcm16 =
235 (int16_t*)malloc(kFrameSize * kMaxNumChannels * sizeof(int16_t));
236 }
237 if (!mInputBufferPcm16) return C2_NO_MEMORY;
238
239 /* Default Configurations */
240 c2_status_t status = configureEncoder();
241 return status;
242}
243
244c2_status_t C2SoftOpusEnc::onStop() {
245 mSignalledEos = false;
246 mSignalledError = false;
247 mIsFirstFrame = true;
248 mEncoderFlushed = false;
249 mBufferAvailable = false;
250 mAnchorTimeStamp = 0ull;
251 mProcessedSamples = 0u;
252 mFilledLen = 0;
253 if (mEncoder) {
254 int status = opus_multistream_encoder_ctl(mEncoder, OPUS_RESET_STATE);
255 if (status != OPUS_OK) {
256 ALOGE("OPUS_RESET_STATE failed status = %s", opus_strerror(status));
257 mSignalledError = true;
258 return C2_CORRUPTED;
259 }
260 }
261 if (mOutputBlock) mOutputBlock.reset();
262 mOutputBlock = nullptr;
263
264 return C2_OK;
265}
266
267void C2SoftOpusEnc::onReset() {
268 (void)onStop();
269}
270
271void C2SoftOpusEnc::onRelease() {
272 (void)onStop();
273 if (mInputBufferPcm16) {
274 free(mInputBufferPcm16);
275 mInputBufferPcm16 = nullptr;
276 }
277 if (mEncoder) {
278 opus_multistream_encoder_destroy(mEncoder);
279 mEncoder = nullptr;
280 }
281}
282
283c2_status_t C2SoftOpusEnc::onFlush_sm() {
284 return onStop();
285}
286
287// Drain the encoder to get last frames (if any)
288int C2SoftOpusEnc::drainEncoder(uint8_t* outPtr) {
289 memset((uint8_t *)mInputBufferPcm16 + mFilledLen, 0,
290 (mNumPcmBytesPerInputFrame - mFilledLen));
291 int encodedBytes = opus_multistream_encode(
292 mEncoder, mInputBufferPcm16, mNumSamplesPerFrame, outPtr, kMaxPayload);
293 if (encodedBytes > mOutputBlock->capacity()) {
294 ALOGE("not enough space left to write encoded data, dropping %d bytes",
295 mBytesEncoded);
296 // a fatal error would stop the encoding
297 return -1;
298 }
299 ALOGV("encoded %i Opus bytes from %zu PCM bytes", encodedBytes,
300 mNumPcmBytesPerInputFrame);
301 mEncoderFlushed = true;
302 mFilledLen = 0;
303 return encodedBytes;
304}
305
306void C2SoftOpusEnc::process(const std::unique_ptr<C2Work>& work,
307 const std::shared_ptr<C2BlockPool>& pool) {
308 // Initialize output work
309 work->result = C2_OK;
310 work->workletsProcessed = 1u;
311 work->worklets.front()->output.flags = work->input.flags;
312
313 if (mSignalledError || mSignalledEos) {
314 work->result = C2_BAD_VALUE;
315 return;
316 }
317
318 bool eos = (work->input.flags & C2FrameData::FLAG_END_OF_STREAM) != 0;
319 C2ReadView rView = mDummyReadView;
320 size_t inOffset = 0u;
321 size_t inSize = 0u;
322 c2_status_t err = C2_OK;
323 if (!work->input.buffers.empty()) {
324 rView =
325 work->input.buffers[0]->data().linearBlocks().front().map().get();
326 inSize = rView.capacity();
327 if (inSize && rView.error()) {
328 ALOGE("read view map failed %d", rView.error());
329 work->result = C2_CORRUPTED;
330 return;
331 }
332 }
333
334 ALOGV("in buffer attr. size %zu timestamp %d frameindex %d, flags %x",
335 inSize, (int)work->input.ordinal.timestamp.peeku(),
336 (int)work->input.ordinal.frameIndex.peeku(), work->input.flags);
337
338 if (!mEncoder) {
339 if (initEncoder() != C2_OK) {
340 ALOGE("initEncoder failed with status %d", err);
341 work->result = err;
342 mSignalledError = true;
343 return;
344 }
345 }
Wonsik Kim353e1672019-01-07 16:31:29 -0800346 if (mIsFirstFrame && inSize > 0) {
Manisha Jajooc237cbc2018-11-16 18:56:20 +0530347 mAnchorTimeStamp = work->input.ordinal.timestamp.peekull();
348 mIsFirstFrame = false;
349 }
350
351 C2MemoryUsage usage = {C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE};
352 err = pool->fetchLinearBlock(kMaxPayload, usage, &mOutputBlock);
353 if (err != C2_OK) {
354 ALOGE("fetchLinearBlock for Output failed with status %d", err);
355 work->result = C2_NO_MEMORY;
356 return;
357 }
358
359 C2WriteView wView = mOutputBlock->map().get();
360 if (wView.error()) {
361 ALOGE("write view map failed %d", wView.error());
362 work->result = C2_CORRUPTED;
363 mOutputBlock.reset();
364 return;
365 }
366
367 size_t inPos = 0;
368 size_t processSize = 0;
369 mBytesEncoded = 0;
370 uint64_t outTimeStamp = 0u;
371 std::shared_ptr<C2Buffer> buffer;
372 uint64_t inputIndex = work->input.ordinal.frameIndex.peeku();
373 const uint8_t* inPtr = rView.data() + inOffset;
374
375 class FillWork {
376 public:
377 FillWork(uint32_t flags, C2WorkOrdinalStruct ordinal,
378 const std::shared_ptr<C2Buffer> &buffer)
379 : mFlags(flags), mOrdinal(ordinal), mBuffer(buffer) {
380 }
381 ~FillWork() = default;
382
383 void operator()(const std::unique_ptr<C2Work>& work) {
384 work->worklets.front()->output.flags = (C2FrameData::flags_t)mFlags;
385 work->worklets.front()->output.buffers.clear();
386 work->worklets.front()->output.ordinal = mOrdinal;
387 work->workletsProcessed = 1u;
388 work->result = C2_OK;
389 if (mBuffer) {
390 work->worklets.front()->output.buffers.push_back(mBuffer);
391 }
392 ALOGV("timestamp = %lld, index = %lld, w/%s buffer",
393 mOrdinal.timestamp.peekll(),
394 mOrdinal.frameIndex.peekll(),
395 mBuffer ? "" : "o");
396 }
397
398 private:
399 const uint32_t mFlags;
400 const C2WorkOrdinalStruct mOrdinal;
401 const std::shared_ptr<C2Buffer> mBuffer;
402 };
403
404 C2WorkOrdinalStruct outOrdinal = work->input.ordinal;
405
406 if (!mHeaderGenerated) {
407 uint8_t header[AOPUS_UNIFIED_CSD_MAXSIZE];
408 memset(header, 0, sizeof(header));
409 OpusHeader opusHeader;
410 opusHeader.channels = mChannelCount;
411 opusHeader.num_streams = mChannelCount;
412 opusHeader.num_coupled = 0;
413 opusHeader.channel_mapping = ((mChannelCount > 8) ? 255 : (mChannelCount > 2));
414 opusHeader.gain_db = 0;
415 opusHeader.skip_samples = 0;
416 int headerLen = WriteOpusHeaders(opusHeader, mSampleRate, header,
417 sizeof(header), mCodecDelay, mSeekPreRoll);
418
Lajos Molnar3bb81cd2019-02-20 15:10:30 -0800419 std::unique_ptr<C2StreamInitDataInfo::output> csd =
420 C2StreamInitDataInfo::output::AllocUnique(headerLen, 0u);
Manisha Jajooc237cbc2018-11-16 18:56:20 +0530421 if (!csd) {
422 ALOGE("CSD allocation failed");
423 mSignalledError = true;
424 work->result = C2_NO_MEMORY;
425 return;
426 }
427 ALOGV("put csd, %d bytes", headerLen);
428 memcpy(csd->m.value, header, headerLen);
429 work->worklets.front()->output.configUpdate.push_back(std::move(csd));
430 mHeaderGenerated = true;
431 }
432
433 /*
434 * For buffer size which is not a multiple of mNumPcmBytesPerInputFrame, we will
435 * accumulate the input and keep it. Once the input is filled with expected number
436 * of bytes, we will send it to encoder. mFilledLen manages the bytes of input yet
437 * to be processed. The next call will fill mNumPcmBytesPerInputFrame - mFilledLen
438 * bytes to input and send it to the encoder.
439 */
440 while (inPos < inSize) {
441 const uint8_t* pcmBytes = inPtr + inPos;
442 int filledSamples = mFilledLen / sizeof(int16_t);
443 if ((inPos + (mNumPcmBytesPerInputFrame - mFilledLen)) <= inSize) {
444 processSize = mNumPcmBytesPerInputFrame - mFilledLen;
445 mBufferAvailable = true;
446 } else {
447 processSize = inSize - inPos;
448 mBufferAvailable = false;
449 if (eos) {
450 memset(mInputBufferPcm16 + filledSamples, 0,
451 (mNumPcmBytesPerInputFrame - mFilledLen));
452 mBufferAvailable = true;
453 }
454 }
455 const unsigned nInputSamples = processSize / sizeof(int16_t);
456
457 for (unsigned i = 0; i < nInputSamples; i++) {
458 int32_t data = pcmBytes[2 * i + 1] << 8 | pcmBytes[2 * i];
459 data = ((data & 0xFFFF) ^ 0x8000) - 0x8000;
460 mInputBufferPcm16[i + filledSamples] = data;
461 }
462 inPos += processSize;
463 mFilledLen += processSize;
464 if (!mBufferAvailable) break;
465 uint8_t* outPtr = wView.data() + mBytesEncoded;
466 int encodedBytes =
467 opus_multistream_encode(mEncoder, mInputBufferPcm16,
468 mNumSamplesPerFrame, outPtr, kMaxPayload);
469 ALOGV("encoded %i Opus bytes from %zu PCM bytes", encodedBytes,
470 processSize);
471
472 if (encodedBytes < 0 || encodedBytes > kMaxPayload) {
473 ALOGE("opus_encode failed, encodedBytes : %d", encodedBytes);
474 mSignalledError = true;
475 work->result = C2_CORRUPTED;
476 return;
477 }
478 if (buffer) {
479 outOrdinal.frameIndex = mOutIndex++;
480 outOrdinal.timestamp = mAnchorTimeStamp + outTimeStamp;
481 cloneAndSend(
482 inputIndex, work,
483 FillWork(C2FrameData::FLAG_INCOMPLETE, outOrdinal, buffer));
484 buffer.reset();
485 }
486 if (encodedBytes > 0) {
487 buffer =
488 createLinearBuffer(mOutputBlock, mBytesEncoded, encodedBytes);
489 }
490 mBytesEncoded += encodedBytes;
491 mProcessedSamples += (filledSamples + nInputSamples);
492 outTimeStamp =
493 mProcessedSamples * 1000000ll / mChannelCount / mSampleRate;
494 if ((processSize + mFilledLen) < mNumPcmBytesPerInputFrame)
495 mEncoderFlushed = true;
496 mFilledLen = 0;
497 }
498
499 uint32_t flags = 0;
500 if (eos) {
501 ALOGV("signalled eos");
502 mSignalledEos = true;
503 if (!mEncoderFlushed) {
504 if (buffer) {
505 outOrdinal.frameIndex = mOutIndex++;
506 outOrdinal.timestamp = mAnchorTimeStamp + outTimeStamp;
507 cloneAndSend(
508 inputIndex, work,
509 FillWork(C2FrameData::FLAG_INCOMPLETE, outOrdinal, buffer));
510 buffer.reset();
511 }
512 // drain the encoder for last buffer
513 drainInternal(pool, work);
514 }
515 flags = C2FrameData::FLAG_END_OF_STREAM;
516 }
517 if (buffer) {
518 outOrdinal.frameIndex = mOutIndex++;
519 outOrdinal.timestamp = mAnchorTimeStamp + outTimeStamp;
520 FillWork((C2FrameData::flags_t)(flags), outOrdinal, buffer)(work);
521 buffer.reset();
522 }
523 mOutputBlock = nullptr;
524}
525
526c2_status_t C2SoftOpusEnc::drainInternal(
527 const std::shared_ptr<C2BlockPool>& pool,
528 const std::unique_ptr<C2Work>& work) {
529 mBytesEncoded = 0;
530 std::shared_ptr<C2Buffer> buffer = nullptr;
531 C2WorkOrdinalStruct outOrdinal = work->input.ordinal;
532 bool eos = (work->input.flags & C2FrameData::FLAG_END_OF_STREAM) != 0;
533
534 C2MemoryUsage usage = {C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE};
535 c2_status_t err = pool->fetchLinearBlock(kMaxPayload, usage, &mOutputBlock);
536 if (err != C2_OK) {
537 ALOGE("fetchLinearBlock for Output failed with status %d", err);
538 return C2_NO_MEMORY;
539 }
540
541 C2WriteView wView = mOutputBlock->map().get();
542 if (wView.error()) {
543 ALOGE("write view map failed %d", wView.error());
544 mOutputBlock.reset();
545 return C2_CORRUPTED;
546 }
547
548 int encBytes = drainEncoder(wView.data());
549 if (encBytes > 0) mBytesEncoded += encBytes;
550 if (mBytesEncoded > 0) {
551 buffer = createLinearBuffer(mOutputBlock, 0, mBytesEncoded);
552 mOutputBlock.reset();
553 }
554 mProcessedSamples += (mNumPcmBytesPerInputFrame / sizeof(int16_t));
555 uint64_t outTimeStamp =
556 mProcessedSamples * 1000000ll / mChannelCount / mSampleRate;
557 outOrdinal.frameIndex = mOutIndex++;
558 outOrdinal.timestamp = mAnchorTimeStamp + outTimeStamp;
559 work->worklets.front()->output.flags =
560 (C2FrameData::flags_t)(eos ? C2FrameData::FLAG_END_OF_STREAM : 0);
561 work->worklets.front()->output.buffers.clear();
562 work->worklets.front()->output.ordinal = outOrdinal;
563 work->workletsProcessed = 1u;
564 work->result = C2_OK;
565 if (buffer) {
566 work->worklets.front()->output.buffers.push_back(buffer);
567 }
568 mOutputBlock = nullptr;
569 return C2_OK;
570}
571
572c2_status_t C2SoftOpusEnc::drain(uint32_t drainMode,
573 const std::shared_ptr<C2BlockPool>& pool) {
574 if (drainMode == NO_DRAIN) {
575 ALOGW("drain with NO_DRAIN: no-op");
576 return C2_OK;
577 }
578 if (drainMode == DRAIN_CHAIN) {
579 ALOGW("DRAIN_CHAIN not supported");
580 return C2_OMITTED;
581 }
582 mIsFirstFrame = true;
583 mAnchorTimeStamp = 0ull;
584 mProcessedSamples = 0u;
585 return drainInternal(pool, nullptr);
586}
587
588class C2SoftOpusEncFactory : public C2ComponentFactory {
589public:
590 C2SoftOpusEncFactory()
591 : mHelper(std::static_pointer_cast<C2ReflectorHelper>(
592 GetCodec2PlatformComponentStore()->getParamReflector())) {}
593
594 virtual c2_status_t createComponent(
595 c2_node_id_t id, std::shared_ptr<C2Component>* const component,
596 std::function<void(C2Component*)> deleter) override {
597 *component = std::shared_ptr<C2Component>(
598 new C2SoftOpusEnc(
599 COMPONENT_NAME, id,
600 std::make_shared<C2SoftOpusEnc::IntfImpl>(mHelper)),
601 deleter);
602 return C2_OK;
603 }
604
605 virtual c2_status_t createInterface(
606 c2_node_id_t id, std::shared_ptr<C2ComponentInterface>* const interface,
607 std::function<void(C2ComponentInterface*)> deleter) override {
608 *interface = std::shared_ptr<C2ComponentInterface>(
609 new SimpleInterface<C2SoftOpusEnc::IntfImpl>(
610 COMPONENT_NAME, id,
611 std::make_shared<C2SoftOpusEnc::IntfImpl>(mHelper)),
612 deleter);
613 return C2_OK;
614 }
615
616 virtual ~C2SoftOpusEncFactory() override = default;
617private:
618 std::shared_ptr<C2ReflectorHelper> mHelper;
619};
620
621} // namespace android
622
623extern "C" ::C2ComponentFactory* CreateCodec2Factory() {
624 ALOGV("in %s", __func__);
625 return new ::android::C2SoftOpusEncFactory();
626}
627
628extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) {
629 ALOGV("in %s", __func__);
630 delete factory;
631}