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 "C2SoftAmrNbEnc"
19 #include <log/log.h>
20
21 #include <media/stagefright/foundation/MediaDefs.h>
22
23 #include <C2PlatformSupport.h>
24 #include <SimpleC2Interface.h>
25
26 #include "C2SoftAmrNbEnc.h"
27
28 namespace android {
29
30 constexpr char COMPONENT_NAME[] = "c2.android.amrnb.encoder";
31
32 class C2SoftAmrNbEnc::IntfImpl : public C2InterfaceHelper {
33 public:
IntfImpl(const std::shared_ptr<C2ReflectorHelper> & helper)34 explicit IntfImpl(const std::shared_ptr<C2ReflectorHelper>& helper)
35 : C2InterfaceHelper(helper) {
36 setDerivedInstance(this);
37
38 addParameter(
39 DefineParam(mInputFormat, C2_NAME_INPUT_STREAM_FORMAT_SETTING)
40 .withConstValue(
41 new C2StreamFormatConfig::input(0u, C2FormatAudio))
42 .build());
43
44 addParameter(
45 DefineParam(mOutputFormat, C2_NAME_OUTPUT_STREAM_FORMAT_SETTING)
46 .withConstValue(
47 new C2StreamFormatConfig::output(0u, C2FormatCompressed))
48 .build());
49
50 addParameter(
51 DefineParam(mInputMediaType, C2_NAME_INPUT_PORT_MIME_SETTING)
52 .withConstValue(AllocSharedString<C2PortMimeConfig::input>(
53 MEDIA_MIMETYPE_AUDIO_RAW))
54 .build());
55
56 addParameter(
57 DefineParam(mOutputMediaType, C2_NAME_OUTPUT_PORT_MIME_SETTING)
58 .withConstValue(AllocSharedString<C2PortMimeConfig::output>(
59 MEDIA_MIMETYPE_AUDIO_AMR_NB))
60 .build());
61
62 addParameter(
63 DefineParam(mChannelCount, C2_NAME_STREAM_CHANNEL_COUNT_SETTING)
64 .withDefault(new C2StreamChannelCountInfo::input(0u, 1))
65 .withFields({C2F(mChannelCount, value).equalTo(1)})
66 .withSetter((Setter<decltype(*mChannelCount)>::StrictValueWithNoDeps))
67 .build());
68
69 addParameter(
70 DefineParam(mSampleRate, C2_NAME_STREAM_SAMPLE_RATE_SETTING)
71 .withDefault(new C2StreamSampleRateInfo::input(0u, 8000))
72 .withFields({C2F(mSampleRate, value).equalTo(8000)})
73 .withSetter(
74 (Setter<decltype(*mSampleRate)>::StrictValueWithNoDeps))
75 .build());
76
77 addParameter(
78 DefineParam(mBitrate, C2_NAME_STREAM_BITRATE_SETTING)
79 .withDefault(new C2BitrateTuning::output(0u, 4750))
80 .withFields({C2F(mBitrate, value).inRange(4750, 12200)})
81 .withSetter(Setter<decltype(*mBitrate)>::NonStrictValueWithNoDeps)
82 .build());
83
84 addParameter(
85 DefineParam(mInputMaxBufSize, C2_PARAMKEY_INPUT_MAX_BUFFER_SIZE)
86 .withConstValue(new C2StreamMaxBufferSizeInfo::input(0u, 8192))
87 .build());
88 }
89
getSampleRate() const90 uint32_t getSampleRate() const { return mSampleRate->value; }
getChannelCount() const91 uint32_t getChannelCount() const { return mChannelCount->value; }
getBitrate() const92 uint32_t getBitrate() const { return mBitrate->value; }
93
94 private:
95 std::shared_ptr<C2StreamFormatConfig::input> mInputFormat;
96 std::shared_ptr<C2StreamFormatConfig::output> mOutputFormat;
97 std::shared_ptr<C2PortMimeConfig::input> mInputMediaType;
98 std::shared_ptr<C2PortMimeConfig::output> mOutputMediaType;
99 std::shared_ptr<C2StreamSampleRateInfo::input> mSampleRate;
100 std::shared_ptr<C2StreamChannelCountInfo::input> mChannelCount;
101 std::shared_ptr<C2BitrateTuning::output> mBitrate;
102 std::shared_ptr<C2StreamMaxBufferSizeInfo::input> mInputMaxBufSize;
103 };
104
C2SoftAmrNbEnc(const char * name,c2_node_id_t id,const std::shared_ptr<IntfImpl> & intfImpl)105 C2SoftAmrNbEnc::C2SoftAmrNbEnc(const char* name, c2_node_id_t id,
106 const std::shared_ptr<IntfImpl>& intfImpl)
107 : SimpleC2Component(
108 std::make_shared<SimpleInterface<IntfImpl>>(name, id, intfImpl)),
109 mIntf(intfImpl),
110 mEncState(nullptr),
111 mSidState(nullptr) {
112 }
113
~C2SoftAmrNbEnc()114 C2SoftAmrNbEnc::~C2SoftAmrNbEnc() {
115 onRelease();
116 }
117
onInit()118 c2_status_t C2SoftAmrNbEnc::onInit() {
119 bool dtx_enable = false;
120
121 if (AMREncodeInit(&mEncState, &mSidState, dtx_enable) != 0)
122 return C2_CORRUPTED;
123 // TODO: get mode directly from config
124 switch(mIntf->getBitrate()) {
125 case 4750: mMode = MR475;
126 break;
127 case 5150: mMode = MR515;
128 break;
129 case 5900: mMode = MR59;
130 break;
131 case 6700: mMode = MR67;
132 break;
133 case 7400: mMode = MR74;
134 break;
135 case 7950: mMode = MR795;
136 break;
137 case 10200: mMode = MR102;
138 break;
139 case 12200: mMode = MR122;
140 break;
141 default: mMode = MR795;
142 }
143 mIsFirst = true;
144 mSignalledError = false;
145 mSignalledOutputEos = false;
146 mAnchorTimeStamp = 0;
147 mProcessedSamples = 0;
148 mFilledLen = 0;
149
150 return C2_OK;
151 }
152
onRelease()153 void C2SoftAmrNbEnc::onRelease() {
154 if (mEncState) {
155 AMREncodeExit(&mEncState, &mSidState);
156 mEncState = mSidState = nullptr;
157 }
158 }
159
onStop()160 c2_status_t C2SoftAmrNbEnc::onStop() {
161 if (AMREncodeReset(mEncState, mSidState) != 0)
162 return C2_CORRUPTED;
163 mIsFirst = true;
164 mSignalledError = false;
165 mSignalledOutputEos = false;
166 mAnchorTimeStamp = 0;
167 mProcessedSamples = 0;
168 mFilledLen = 0;
169
170 return C2_OK;
171 }
172
onReset()173 void C2SoftAmrNbEnc::onReset() {
174 (void) onStop();
175 }
176
onFlush_sm()177 c2_status_t C2SoftAmrNbEnc::onFlush_sm() {
178 return onStop();
179 }
180
fillEmptyWork(const std::unique_ptr<C2Work> & work)181 static void fillEmptyWork(const std::unique_ptr<C2Work> &work) {
182 work->worklets.front()->output.flags = work->input.flags;
183 work->worklets.front()->output.buffers.clear();
184 work->worklets.front()->output.ordinal = work->input.ordinal;
185 work->workletsProcessed = 1u;
186 }
187
process(const std::unique_ptr<C2Work> & work,const std::shared_ptr<C2BlockPool> & pool)188 void C2SoftAmrNbEnc::process(
189 const std::unique_ptr<C2Work> &work,
190 const std::shared_ptr<C2BlockPool> &pool) {
191 work->result = C2_OK;
192 work->workletsProcessed = 0u;
193 if (mSignalledError || mSignalledOutputEos) {
194 work->result = C2_BAD_VALUE;
195 return;
196 }
197
198 bool eos = ((work->input.flags & C2FrameData::FLAG_END_OF_STREAM) != 0);
199 size_t inOffset = 0u;
200 size_t inSize = 0u;
201 C2ReadView rView = mDummyReadView;
202 if (!work->input.buffers.empty()) {
203 rView = work->input.buffers[0]->data().linearBlocks().front().map().get();
204 inSize = rView.capacity();
205 if (inSize && rView.error()) {
206 ALOGE("read view map failed %d", rView.error());
207 work->result = C2_CORRUPTED;
208 return;
209 }
210 }
211
212 ALOGV("in buffer attr. size %zu timestamp %d frameindex %d, flags %x",
213 inSize, (int)work->input.ordinal.timestamp.peeku(),
214 (int)work->input.ordinal.frameIndex.peeku(), work->input.flags);
215
216 size_t outCapacity = kNumBytesPerInputFrame;
217 outCapacity += mFilledLen + inSize;
218 std::shared_ptr<C2LinearBlock> outputBlock;
219 C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
220 c2_status_t err = pool->fetchLinearBlock(outCapacity, usage, &outputBlock);
221 if (err != C2_OK) {
222 ALOGE("fetchLinearBlock for Output failed with status %d", err);
223 work->result = C2_NO_MEMORY;
224 return;
225 }
226 C2WriteView wView = outputBlock->map().get();
227 if (wView.error()) {
228 ALOGE("write view map failed %d", wView.error());
229 work->result = C2_CORRUPTED;
230 return;
231 }
232 uint64_t outTimeStamp =
233 mProcessedSamples * 1000000ll / mIntf->getSampleRate();
234 size_t inPos = 0;
235 size_t outPos = 0;
236 while (inPos < inSize) {
237 const uint8_t *inPtr = rView.data() + inOffset;
238 int validSamples = mFilledLen / sizeof(int16_t);
239 if ((inPos + (kNumBytesPerInputFrame - mFilledLen)) <= inSize) {
240 memcpy(mInputFrame + validSamples, inPtr + inPos,
241 (kNumBytesPerInputFrame - mFilledLen));
242 inPos += (kNumBytesPerInputFrame - mFilledLen);
243 } else {
244 memcpy(mInputFrame + validSamples, inPtr + inPos, (inSize - inPos));
245 mFilledLen += (inSize - inPos);
246 inPos += (inSize - inPos);
247 if (eos) {
248 validSamples = mFilledLen / sizeof(int16_t);
249 memset(mInputFrame + validSamples, 0, (kNumBytesPerInputFrame - mFilledLen));
250 } else break;
251
252 }
253 Frame_Type_3GPP frameType;
254 int numEncBytes = AMREncode(mEncState, mSidState, mMode, mInputFrame,
255 wView.data() + outPos, &frameType,
256 AMR_TX_WMF);
257 if (numEncBytes < 0 || numEncBytes > ((int)outCapacity - (int)outPos)) {
258 ALOGE("encodeFrame call failed, state [%d %zu %zu]", numEncBytes, outPos, outCapacity);
259 mSignalledError = true;
260 work->result = C2_CORRUPTED;
261 return;
262 }
263 // Convert header byte from WMF to IETF format.
264 if (numEncBytes > 0)
265 wView.data()[outPos] = ((wView.data()[outPos] << 3) | 4) & 0x7c;
266 outPos += numEncBytes;
267 mProcessedSamples += kNumSamplesPerFrame;
268 mFilledLen = 0;
269 }
270 ALOGV("causal sample size %d", mFilledLen);
271 if (mIsFirst) {
272 mIsFirst = false;
273 mAnchorTimeStamp = work->input.ordinal.timestamp.peekull();
274 }
275 fillEmptyWork(work);
276 if (outPos != 0) {
277 work->worklets.front()->output.buffers.push_back(
278 createLinearBuffer(std::move(outputBlock), 0, outPos));
279 work->worklets.front()->output.ordinal.timestamp = mAnchorTimeStamp + outTimeStamp;
280
281 }
282 if (eos) {
283 mSignalledOutputEos = true;
284 ALOGV("signalled EOS");
285 if (mFilledLen) ALOGV("Discarding trailing %d bytes", mFilledLen);
286 }
287 }
288
drain(uint32_t drainMode,const std::shared_ptr<C2BlockPool> & pool)289 c2_status_t C2SoftAmrNbEnc::drain(
290 uint32_t drainMode,
291 const std::shared_ptr<C2BlockPool> &pool) {
292 (void) pool;
293 if (drainMode == NO_DRAIN) {
294 ALOGW("drain with NO_DRAIN: no-op");
295 return C2_OK;
296 }
297 if (drainMode == DRAIN_CHAIN) {
298 ALOGW("DRAIN_CHAIN not supported");
299 return C2_OMITTED;
300 }
301
302 onFlush_sm();
303 return C2_OK;
304 }
305
306 class C2SoftAmrNbEncFactory : public C2ComponentFactory {
307 public:
C2SoftAmrNbEncFactory()308 C2SoftAmrNbEncFactory()
309 : mHelper(std::static_pointer_cast<C2ReflectorHelper>(
310 GetCodec2PlatformComponentStore()->getParamReflector())) {}
311
createComponent(c2_node_id_t id,std::shared_ptr<C2Component> * const component,std::function<void (C2Component *)> deleter)312 virtual c2_status_t createComponent(
313 c2_node_id_t id,
314 std::shared_ptr<C2Component>* const component,
315 std::function<void(C2Component*)> deleter) override {
316 *component = std::shared_ptr<C2Component>(
317 new C2SoftAmrNbEnc(
318 COMPONENT_NAME, id,
319 std::make_shared<C2SoftAmrNbEnc::IntfImpl>(mHelper)),
320 deleter);
321 return C2_OK;
322 }
323
createInterface(c2_node_id_t id,std::shared_ptr<C2ComponentInterface> * const interface,std::function<void (C2ComponentInterface *)> deleter)324 virtual c2_status_t createInterface(
325 c2_node_id_t id,
326 std::shared_ptr<C2ComponentInterface>* const interface,
327 std::function<void(C2ComponentInterface*)> deleter) override {
328 *interface = std::shared_ptr<C2ComponentInterface>(
329 new SimpleInterface<C2SoftAmrNbEnc::IntfImpl>(
330 COMPONENT_NAME, id,
331 std::make_shared<C2SoftAmrNbEnc::IntfImpl>(mHelper)),
332 deleter);
333 return C2_OK;
334 }
335
336 virtual ~C2SoftAmrNbEncFactory() override = default;
337
338 private:
339 std::shared_ptr<C2ReflectorHelper> mHelper;
340 };
341
342 } // namespace android
343
CreateCodec2Factory()344 extern "C" ::C2ComponentFactory* CreateCodec2Factory() {
345 ALOGV("in %s", __func__);
346 return new ::android::C2SoftAmrNbEncFactory();
347 }
348
DestroyCodec2Factory(::C2ComponentFactory * factory)349 extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) {
350 ALOGV("in %s", __func__);
351 delete factory;
352 }
353