1 /*
2 * Copyright (C) 2018 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 "C2SoftAmrWbEnc"
19 #include <log/log.h>
20
21 #include <media/stagefright/foundation/MediaDefs.h>
22
23 #include <C2Debug.h>
24 #include <C2PlatformSupport.h>
25 #include <SimpleC2Interface.h>
26
27 #include "C2SoftAmrWbEnc.h"
28 #include "cmnMemory.h"
29
30 namespace android {
31
32 namespace {
33
34 constexpr char COMPONENT_NAME[] = "c2.android.amrwb.encoder";
35
36 } // namespace
37
38 class C2SoftAmrWbEnc::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_AMR_WB) {
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(mChannelCount, C2_PARAMKEY_CHANNEL_COUNT)
62 .withDefault(new C2StreamChannelCountInfo::input(0u, 1))
63 .withFields({C2F(mChannelCount, value).equalTo(1)})
64 .withSetter((Setter<decltype(*mChannelCount)>::StrictValueWithNoDeps))
65 .build());
66
67 addParameter(
68 DefineParam(mSampleRate, C2_PARAMKEY_SAMPLE_RATE)
69 .withDefault(new C2StreamSampleRateInfo::input(0u, 16000))
70 .withFields({C2F(mSampleRate, value).equalTo(16000)})
71 .withSetter(
72 (Setter<decltype(*mSampleRate)>::StrictValueWithNoDeps))
73 .build());
74
75 addParameter(
76 DefineParam(mBitrate, C2_PARAMKEY_BITRATE)
77 .withDefault(new C2StreamBitrateInfo::output(0u, 6600))
78 .withFields({C2F(mBitrate, value).inRange(6600, 23850)})
79 .withSetter(Setter<decltype(*mBitrate)>::NonStrictValueWithNoDeps)
80 .build());
81
82 addParameter(
83 DefineParam(mInputMaxBufSize, C2_PARAMKEY_INPUT_MAX_BUFFER_SIZE)
84 .withConstValue(new C2StreamMaxBufferSizeInfo::input(0u, 8192))
85 .build());
86 }
87
getSampleRate() const88 uint32_t getSampleRate() const { return mSampleRate->value; }
getChannelCount() const89 uint32_t getChannelCount() const { return mChannelCount->value; }
getBitrate() const90 uint32_t getBitrate() const { return mBitrate->value; }
91
92 private:
93 std::shared_ptr<C2StreamSampleRateInfo::input> mSampleRate;
94 std::shared_ptr<C2StreamChannelCountInfo::input> mChannelCount;
95 std::shared_ptr<C2StreamBitrateInfo::output> mBitrate;
96 std::shared_ptr<C2StreamMaxBufferSizeInfo::input> mInputMaxBufSize;
97 };
98
C2SoftAmrWbEnc(const char * name,c2_node_id_t id,const std::shared_ptr<IntfImpl> & intfImpl)99 C2SoftAmrWbEnc::C2SoftAmrWbEnc(const char* name, c2_node_id_t id,
100 const std::shared_ptr<IntfImpl>& intfImpl)
101 : SimpleC2Component(
102 std::make_shared<SimpleInterface<IntfImpl>>(name, id, intfImpl)),
103 mIntf(intfImpl),
104 mEncoderHandle(nullptr),
105 mApiHandle(nullptr),
106 mMemOperator(nullptr) {
107 }
108
C2SoftAmrWbEnc(const char * name,c2_node_id_t id,const std::shared_ptr<C2ReflectorHelper> & helper)109 C2SoftAmrWbEnc::C2SoftAmrWbEnc(const char* name, c2_node_id_t id,
110 const std::shared_ptr<C2ReflectorHelper>& helper)
111 : C2SoftAmrWbEnc(name, id, std::make_shared<IntfImpl>(helper)) {
112 }
113
~C2SoftAmrWbEnc()114 C2SoftAmrWbEnc::~C2SoftAmrWbEnc() {
115 onRelease();
116 }
117
onInit()118 c2_status_t C2SoftAmrWbEnc::onInit() {
119 // TODO: get mode directly from config
120 switch(mIntf->getBitrate()) {
121 case 6600: mMode = VOAMRWB_MD66;
122 break;
123 case 8850: mMode = VOAMRWB_MD885;
124 break;
125 case 12650: mMode = VOAMRWB_MD1265;
126 break;
127 case 14250: mMode = VOAMRWB_MD1425;
128 break;
129 case 15850: mMode = VOAMRWB_MD1585;
130 break;
131 case 18250: mMode = VOAMRWB_MD1825;
132 break;
133 case 19850: mMode = VOAMRWB_MD1985;
134 break;
135 case 23050: mMode = VOAMRWB_MD2305;
136 break;
137 case 23850: mMode = VOAMRWB_MD2385;
138 break;
139 default: mMode = VOAMRWB_MD2305;
140 }
141 status_t err = initEncoder();
142 mIsFirst = true;
143 mSignalledError = false;
144 mSignalledOutputEos = false;
145 mAnchorTimeStamp = 0;
146 mProcessedSamples = 0;
147 mFilledLen = 0;
148
149 return err == OK ? C2_OK : C2_NO_MEMORY;
150 }
151
onRelease()152 void C2SoftAmrWbEnc::onRelease() {
153 if (mEncoderHandle) {
154 CHECK_EQ((VO_U32)VO_ERR_NONE, mApiHandle->Uninit(mEncoderHandle));
155 mEncoderHandle = nullptr;
156 }
157 if (mApiHandle) {
158 delete mApiHandle;
159 mApiHandle = nullptr;
160 }
161 if (mMemOperator) {
162 delete mMemOperator;
163 mMemOperator = nullptr;
164 }
165 }
166
onStop()167 c2_status_t C2SoftAmrWbEnc::onStop() {
168 for (int i = 0; i < kNumSamplesPerFrame; i++) {
169 mInputFrame[i] = 0x0008; /* EHF_MASK */
170 }
171 uint8_t outBuffer[kNumBytesPerInputFrame];
172 (void) encodeInput(outBuffer, kNumBytesPerInputFrame);
173 mIsFirst = true;
174 mSignalledError = false;
175 mSignalledOutputEos = false;
176 mAnchorTimeStamp = 0;
177 mProcessedSamples = 0;
178 mFilledLen = 0;
179
180 return C2_OK;
181 }
182
onReset()183 void C2SoftAmrWbEnc::onReset() {
184 (void) onStop();
185 }
186
onFlush_sm()187 c2_status_t C2SoftAmrWbEnc::onFlush_sm() {
188 return onStop();
189 }
190
initEncoder()191 status_t C2SoftAmrWbEnc::initEncoder() {
192 mApiHandle = new VO_AUDIO_CODECAPI;
193 if (!mApiHandle) return NO_MEMORY;
194
195 if (VO_ERR_NONE != voGetAMRWBEncAPI(mApiHandle)) {
196 ALOGE("Failed to get api handle");
197 return UNKNOWN_ERROR;
198 }
199
200 mMemOperator = new VO_MEM_OPERATOR;
201 if (!mMemOperator) return NO_MEMORY;
202
203 mMemOperator->Alloc = cmnMemAlloc;
204 mMemOperator->Copy = cmnMemCopy;
205 mMemOperator->Free = cmnMemFree;
206 mMemOperator->Set = cmnMemSet;
207 mMemOperator->Check = cmnMemCheck;
208
209 VO_CODEC_INIT_USERDATA userData;
210 memset(&userData, 0, sizeof(userData));
211 userData.memflag = VO_IMF_USERMEMOPERATOR;
212 userData.memData = (VO_PTR) mMemOperator;
213
214 if (VO_ERR_NONE != mApiHandle->Init(
215 &mEncoderHandle, VO_AUDIO_CodingAMRWB, &userData)) {
216 ALOGE("Failed to init AMRWB encoder");
217 return UNKNOWN_ERROR;
218 }
219
220 VOAMRWBFRAMETYPE type = VOAMRWB_RFC3267;
221 if (VO_ERR_NONE != mApiHandle->SetParam(
222 mEncoderHandle, VO_PID_AMRWB_FRAMETYPE, &type)) {
223 ALOGE("Failed to set AMRWB encoder frame type to %d", type);
224 return UNKNOWN_ERROR;
225 }
226
227 if (VO_ERR_NONE !=
228 mApiHandle->SetParam(
229 mEncoderHandle, VO_PID_AMRWB_MODE, &mMode)) {
230 ALOGE("Failed to set AMRWB encoder mode to %d", mMode);
231 return UNKNOWN_ERROR;
232 }
233
234 return OK;
235 }
236
encodeInput(uint8_t * buffer,uint32_t length)237 int C2SoftAmrWbEnc::encodeInput(uint8_t *buffer, uint32_t length) {
238 VO_CODECBUFFER inputData;
239 memset(&inputData, 0, sizeof(inputData));
240 inputData.Buffer = (unsigned char *) mInputFrame;
241 inputData.Length = kNumBytesPerInputFrame;
242
243 CHECK_EQ((VO_U32)VO_ERR_NONE,
244 mApiHandle->SetInputData(mEncoderHandle, &inputData));
245
246 VO_AUDIO_OUTPUTINFO outputInfo;
247 memset(&outputInfo, 0, sizeof(outputInfo));
248 VO_CODECBUFFER outputData;
249 memset(&outputData, 0, sizeof(outputData));
250 outputData.Buffer = buffer;
251 outputData.Length = length;
252 VO_U32 ret = mApiHandle->GetOutputData(
253 mEncoderHandle, &outputData, &outputInfo);
254 if (ret != VO_ERR_NONE && ret != VO_ERR_INPUT_BUFFER_SMALL) {
255 ALOGD("encountered error during encode call");
256 return -1;
257 }
258 return outputData.Length;
259 }
260
fillEmptyWork(const std::unique_ptr<C2Work> & work)261 static void fillEmptyWork(const std::unique_ptr<C2Work> &work) {
262 work->worklets.front()->output.flags = work->input.flags;
263 work->worklets.front()->output.buffers.clear();
264 work->worklets.front()->output.ordinal = work->input.ordinal;
265 work->workletsProcessed = 1u;
266 }
267
process(const std::unique_ptr<C2Work> & work,const std::shared_ptr<C2BlockPool> & pool)268 void C2SoftAmrWbEnc::process(
269 const std::unique_ptr<C2Work> &work,
270 const std::shared_ptr<C2BlockPool> &pool) {
271 // Initialize output work
272 work->result = C2_OK;
273 work->workletsProcessed = 1u;
274 work->worklets.front()->output.flags = work->input.flags;
275
276 if (mSignalledError || mSignalledOutputEos) {
277 work->result = C2_BAD_VALUE;
278 return;
279 }
280
281 size_t inOffset = 0u;
282 size_t inSize = 0u;
283 C2ReadView rView = mDummyReadView;
284 if (!work->input.buffers.empty()) {
285 rView = work->input.buffers[0]->data().linearBlocks().front().map().get();
286 inSize = rView.capacity();
287 if (inSize && rView.error()) {
288 ALOGE("read view map failed %d", rView.error());
289 work->result = rView.error();
290 return;
291 }
292 }
293 bool eos = (work->input.flags & C2FrameData::FLAG_END_OF_STREAM) != 0;
294
295 ALOGV("in buffer attr. size %zu timestamp %d frameindex %d, flags %x",
296 inSize, (int)work->input.ordinal.timestamp.peeku(),
297 (int)work->input.ordinal.frameIndex.peeku(), work->input.flags);
298
299 size_t outCapacity = kNumBytesPerInputFrame;
300 outCapacity += mFilledLen + inSize;
301 std::shared_ptr<C2LinearBlock> outputBlock;
302 C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
303 c2_status_t err = pool->fetchLinearBlock(outCapacity, usage, &outputBlock);
304 if (err != C2_OK) {
305 ALOGE("fetchLinearBlock for Output failed with status %d", err);
306 work->result = C2_NO_MEMORY;
307 return;
308 }
309 C2WriteView wView = outputBlock->map().get();
310 if (wView.error()) {
311 ALOGE("write view map failed %d", wView.error());
312 work->result = wView.error();
313 return;
314 }
315 int64_t outTimeStamp =
316 mProcessedSamples * 1000000ll / mIntf->getSampleRate();
317 size_t inPos = 0;
318 size_t outPos = 0;
319 while (inPos < inSize || eos) {
320 const uint8_t *inPtr = rView.data() + inOffset;
321 int validSamples = mFilledLen / sizeof(int16_t);
322 if ((inPos + (kNumBytesPerInputFrame - mFilledLen)) <= inSize) {
323 memcpy(mInputFrame + validSamples, inPtr + inPos,
324 (kNumBytesPerInputFrame - mFilledLen));
325 inPos += (kNumBytesPerInputFrame - mFilledLen);
326 } else {
327 memcpy(mInputFrame + validSamples, inPtr + inPos, (inSize - inPos));
328 mFilledLen += (inSize - inPos);
329 inPos += (inSize - inPos);
330 if (eos && (mFilledLen > 0)) {
331 validSamples = mFilledLen / sizeof(int16_t);
332 memset(mInputFrame + validSamples, 0, (kNumBytesPerInputFrame - mFilledLen));
333 } else break;
334 }
335 int numEncBytes = encodeInput((wView.data() + outPos), outCapacity - outPos);
336 if (numEncBytes < 0) {
337 ALOGE("encodeFrame call failed, state [%d %zu %zu]", numEncBytes, outPos, outCapacity);
338 mSignalledError = true;
339 work->result = C2_CORRUPTED;
340 return;
341 }
342 outPos += numEncBytes;
343 mProcessedSamples += kNumSamplesPerFrame;
344 mFilledLen = 0;
345 }
346 ALOGV("causal sample size %d", mFilledLen);
347 if (mIsFirst && outPos != 0) {
348 mIsFirst = false;
349 mAnchorTimeStamp = work->input.ordinal.timestamp.peekll();
350 }
351 fillEmptyWork(work);
352 if (outPos != 0) {
353 work->worklets.front()->output.buffers.push_back(
354 createLinearBuffer(std::move(outputBlock), 0, outPos));
355 work->worklets.front()->output.ordinal.timestamp = mAnchorTimeStamp + outTimeStamp;
356 }
357 if (eos) {
358 mSignalledOutputEos = true;
359 ALOGV("signalled EOS");
360 }
361 }
362
drain(uint32_t drainMode,const std::shared_ptr<C2BlockPool> & pool)363 c2_status_t C2SoftAmrWbEnc::drain(
364 uint32_t drainMode,
365 const std::shared_ptr<C2BlockPool> &pool) {
366 (void) pool;
367 if (drainMode == NO_DRAIN) {
368 ALOGW("drain with NO_DRAIN: no-op");
369 return C2_OK;
370 }
371 if (drainMode == DRAIN_CHAIN) {
372 ALOGW("DRAIN_CHAIN not supported");
373 return C2_OMITTED;
374 }
375
376 onFlush_sm();
377 return C2_OK;
378 }
379
380 class C2SoftAmrWbEncFactory : public C2ComponentFactory {
381 public:
C2SoftAmrWbEncFactory()382 C2SoftAmrWbEncFactory()
383 : mHelper(std::static_pointer_cast<C2ReflectorHelper>(
384 GetCodec2PlatformComponentStore()->getParamReflector())) {}
385
createComponent(c2_node_id_t id,std::shared_ptr<C2Component> * const component,std::function<void (C2Component *)> deleter)386 virtual c2_status_t createComponent(
387 c2_node_id_t id,
388 std::shared_ptr<C2Component>* const component,
389 std::function<void(C2Component*)> deleter) override {
390 *component = std::shared_ptr<C2Component>(
391 new C2SoftAmrWbEnc(
392 COMPONENT_NAME, id,
393 std::make_shared<C2SoftAmrWbEnc::IntfImpl>(mHelper)),
394 deleter);
395 return C2_OK;
396 }
397
createInterface(c2_node_id_t id,std::shared_ptr<C2ComponentInterface> * const interface,std::function<void (C2ComponentInterface *)> deleter)398 virtual c2_status_t createInterface(
399 c2_node_id_t id,
400 std::shared_ptr<C2ComponentInterface>* const interface,
401 std::function<void(C2ComponentInterface*)> deleter) override {
402 *interface = std::shared_ptr<C2ComponentInterface>(
403 new SimpleInterface<C2SoftAmrWbEnc::IntfImpl>(
404 COMPONENT_NAME, id,
405 std::make_shared<C2SoftAmrWbEnc::IntfImpl>(mHelper)),
406 deleter);
407 return C2_OK;
408 }
409
410 virtual ~C2SoftAmrWbEncFactory() override = default;
411
412 private:
413 std::shared_ptr<C2ReflectorHelper> mHelper;
414 };
415
416 } // namespace android
417
418 __attribute__((cfi_canonical_jump_table))
CreateCodec2Factory()419 extern "C" ::C2ComponentFactory* CreateCodec2Factory() {
420 ALOGV("in %s", __func__);
421 return new ::android::C2SoftAmrWbEncFactory();
422 }
423
424 __attribute__((cfi_canonical_jump_table))
DestroyCodec2Factory(::C2ComponentFactory * factory)425 extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) {
426 ALOGV("in %s", __func__);
427 delete factory;
428 }
429