• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     // Initialize output work
192     work->result = C2_OK;
193     work->workletsProcessed = 1u;
194     work->worklets.front()->output.flags = work->input.flags;
195 
196     if (mSignalledError || mSignalledOutputEos) {
197         work->result = C2_BAD_VALUE;
198         return;
199     }
200 
201     bool eos = ((work->input.flags & C2FrameData::FLAG_END_OF_STREAM) != 0);
202     size_t inOffset = 0u;
203     size_t inSize = 0u;
204     C2ReadView rView = mDummyReadView;
205     if (!work->input.buffers.empty()) {
206         rView = work->input.buffers[0]->data().linearBlocks().front().map().get();
207         inSize = rView.capacity();
208         if (inSize && rView.error()) {
209             ALOGE("read view map failed %d", rView.error());
210             work->result = C2_CORRUPTED;
211             return;
212         }
213     }
214 
215     ALOGV("in buffer attr. size %zu timestamp %d frameindex %d, flags %x",
216           inSize, (int)work->input.ordinal.timestamp.peeku(),
217           (int)work->input.ordinal.frameIndex.peeku(), work->input.flags);
218 
219     size_t outCapacity = kNumBytesPerInputFrame;
220     outCapacity += mFilledLen + inSize;
221     std::shared_ptr<C2LinearBlock> outputBlock;
222     C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
223     c2_status_t err = pool->fetchLinearBlock(outCapacity, usage, &outputBlock);
224     if (err != C2_OK) {
225         ALOGE("fetchLinearBlock for Output failed with status %d", err);
226         work->result = C2_NO_MEMORY;
227         return;
228     }
229     C2WriteView wView = outputBlock->map().get();
230     if (wView.error()) {
231         ALOGE("write view map failed %d", wView.error());
232         work->result = C2_CORRUPTED;
233         return;
234     }
235     uint64_t outTimeStamp =
236         mProcessedSamples * 1000000ll / mIntf->getSampleRate();
237     size_t inPos = 0;
238     size_t outPos = 0;
239     while (inPos < inSize) {
240         const uint8_t *inPtr = rView.data() + inOffset;
241         int validSamples = mFilledLen / sizeof(int16_t);
242         if ((inPos + (kNumBytesPerInputFrame - mFilledLen)) <= inSize) {
243             memcpy(mInputFrame + validSamples, inPtr + inPos,
244                    (kNumBytesPerInputFrame - mFilledLen));
245             inPos += (kNumBytesPerInputFrame - mFilledLen);
246         } else {
247             memcpy(mInputFrame + validSamples, inPtr + inPos, (inSize - inPos));
248             mFilledLen += (inSize - inPos);
249             inPos += (inSize - inPos);
250             if (eos) {
251                 validSamples = mFilledLen / sizeof(int16_t);
252                 memset(mInputFrame + validSamples, 0, (kNumBytesPerInputFrame - mFilledLen));
253             } else break;
254 
255         }
256         Frame_Type_3GPP frameType;
257         int numEncBytes = AMREncode(mEncState, mSidState, mMode, mInputFrame,
258                                     wView.data() + outPos, &frameType,
259                                     AMR_TX_WMF);
260         if (numEncBytes < 0 || numEncBytes > ((int)outCapacity - (int)outPos)) {
261             ALOGE("encodeFrame call failed, state [%d %zu %zu]", numEncBytes, outPos, outCapacity);
262             mSignalledError = true;
263             work->result = C2_CORRUPTED;
264             return;
265         }
266         // Convert header byte from WMF to IETF format.
267         if (numEncBytes > 0)
268             wView.data()[outPos] = ((wView.data()[outPos] << 3) | 4) & 0x7c;
269         outPos += numEncBytes;
270         mProcessedSamples += kNumSamplesPerFrame;
271         mFilledLen = 0;
272     }
273     ALOGV("causal sample size %d", mFilledLen);
274     if (mIsFirst) {
275         mIsFirst = false;
276         mAnchorTimeStamp = work->input.ordinal.timestamp.peekull();
277     }
278     fillEmptyWork(work);
279     if (outPos != 0) {
280         work->worklets.front()->output.buffers.push_back(
281                 createLinearBuffer(std::move(outputBlock), 0, outPos));
282         work->worklets.front()->output.ordinal.timestamp = mAnchorTimeStamp + outTimeStamp;
283 
284     }
285     if (eos) {
286         mSignalledOutputEos = true;
287         ALOGV("signalled EOS");
288         if (mFilledLen) ALOGV("Discarding trailing %d bytes", mFilledLen);
289     }
290 }
291 
drain(uint32_t drainMode,const std::shared_ptr<C2BlockPool> & pool)292 c2_status_t C2SoftAmrNbEnc::drain(
293         uint32_t drainMode,
294         const std::shared_ptr<C2BlockPool> &pool) {
295     (void) pool;
296     if (drainMode == NO_DRAIN) {
297         ALOGW("drain with NO_DRAIN: no-op");
298         return C2_OK;
299     }
300     if (drainMode == DRAIN_CHAIN) {
301         ALOGW("DRAIN_CHAIN not supported");
302         return C2_OMITTED;
303     }
304 
305     onFlush_sm();
306     return C2_OK;
307 }
308 
309 class C2SoftAmrNbEncFactory : public C2ComponentFactory {
310 public:
C2SoftAmrNbEncFactory()311     C2SoftAmrNbEncFactory()
312         : mHelper(std::static_pointer_cast<C2ReflectorHelper>(
313               GetCodec2PlatformComponentStore()->getParamReflector())) {}
314 
createComponent(c2_node_id_t id,std::shared_ptr<C2Component> * const component,std::function<void (C2Component *)> deleter)315     virtual c2_status_t createComponent(
316             c2_node_id_t id,
317             std::shared_ptr<C2Component>* const component,
318             std::function<void(C2Component*)> deleter) override {
319         *component = std::shared_ptr<C2Component>(
320             new C2SoftAmrNbEnc(
321                 COMPONENT_NAME, id,
322                 std::make_shared<C2SoftAmrNbEnc::IntfImpl>(mHelper)),
323             deleter);
324         return C2_OK;
325     }
326 
createInterface(c2_node_id_t id,std::shared_ptr<C2ComponentInterface> * const interface,std::function<void (C2ComponentInterface *)> deleter)327     virtual c2_status_t createInterface(
328             c2_node_id_t id,
329             std::shared_ptr<C2ComponentInterface>* const interface,
330             std::function<void(C2ComponentInterface*)> deleter) override {
331         *interface = std::shared_ptr<C2ComponentInterface>(
332             new SimpleInterface<C2SoftAmrNbEnc::IntfImpl>(
333                 COMPONENT_NAME, id,
334                 std::make_shared<C2SoftAmrNbEnc::IntfImpl>(mHelper)),
335             deleter);
336         return C2_OK;
337     }
338 
339     virtual ~C2SoftAmrNbEncFactory() override = default;
340 
341 private:
342     std::shared_ptr<C2ReflectorHelper> mHelper;
343 };
344 
345 }  // namespace android
346 
CreateCodec2Factory()347 extern "C" ::C2ComponentFactory* CreateCodec2Factory() {
348     ALOGV("in %s", __func__);
349     return new ::android::C2SoftAmrNbEncFactory();
350 }
351 
DestroyCodec2Factory(::C2ComponentFactory * factory)352 extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) {
353     ALOGV("in %s", __func__);
354     delete factory;
355 }
356