• 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 "C2SoftGsmDec"
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 "C2SoftGsmDec.h"
27 
28 namespace android {
29 
30 constexpr char COMPONENT_NAME[] = "c2.android.gsm.decoder";
31 
32 class C2SoftGsmDec::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(new C2StreamFormatConfig::input(0u, C2FormatCompressed))
41                 .build());
42 
43         addParameter(
44                 DefineParam(mOutputFormat, C2_NAME_OUTPUT_STREAM_FORMAT_SETTING)
45                 .withConstValue(new C2StreamFormatConfig::output(0u, C2FormatAudio))
46                 .build());
47 
48         addParameter(
49                 DefineParam(mInputMediaType, C2_NAME_INPUT_PORT_MIME_SETTING)
50                 .withConstValue(AllocSharedString<C2PortMimeConfig::input>(
51                         MEDIA_MIMETYPE_AUDIO_MSGSM))
52                 .build());
53 
54         addParameter(
55                 DefineParam(mOutputMediaType, C2_NAME_OUTPUT_PORT_MIME_SETTING)
56                 .withConstValue(AllocSharedString<C2PortMimeConfig::output>(
57                         MEDIA_MIMETYPE_AUDIO_RAW))
58                 .build());
59 
60         addParameter(
61                 DefineParam(mSampleRate, C2_NAME_STREAM_SAMPLE_RATE_SETTING)
62                 .withDefault(new C2StreamSampleRateInfo::output(0u, 8000))
63                 .withFields({C2F(mSampleRate, value).equalTo(8000)})
64                 .withSetter((Setter<decltype(*mSampleRate)>::StrictValueWithNoDeps))
65                 .build());
66 
67         addParameter(
68                 DefineParam(mChannelCount, C2_NAME_STREAM_CHANNEL_COUNT_SETTING)
69                 .withDefault(new C2StreamChannelCountInfo::output(0u, 1))
70                 .withFields({C2F(mChannelCount, value).equalTo(1)})
71                 .withSetter(Setter<decltype(*mChannelCount)>::StrictValueWithNoDeps)
72                 .build());
73 
74         addParameter(
75                 DefineParam(mBitrate, C2_NAME_STREAM_BITRATE_SETTING)
76                 .withDefault(new C2BitrateTuning::input(0u, 13200))
77                 .withFields({C2F(mBitrate, value).equalTo(13200)})
78                 .withSetter(Setter<decltype(*mBitrate)>::NonStrictValueWithNoDeps)
79                 .build());
80     }
81 
82    private:
83     std::shared_ptr<C2StreamFormatConfig::input> mInputFormat;
84     std::shared_ptr<C2StreamFormatConfig::output> mOutputFormat;
85     std::shared_ptr<C2PortMimeConfig::input> mInputMediaType;
86     std::shared_ptr<C2PortMimeConfig::output> mOutputMediaType;
87     std::shared_ptr<C2StreamSampleRateInfo::output> mSampleRate;
88     std::shared_ptr<C2StreamChannelCountInfo::output> mChannelCount;
89     std::shared_ptr<C2BitrateTuning::input> mBitrate;
90 };
91 
C2SoftGsmDec(const char * name,c2_node_id_t id,const std::shared_ptr<IntfImpl> & intfImpl)92 C2SoftGsmDec::C2SoftGsmDec(const char *name, c2_node_id_t id,
93                      const std::shared_ptr<IntfImpl>& intfImpl)
94     : SimpleC2Component(
95         std::make_shared<SimpleInterface<IntfImpl>>(name, id, intfImpl)),
96       mIntf(intfImpl),
97       mGsm(nullptr) {
98 }
99 
~C2SoftGsmDec()100 C2SoftGsmDec::~C2SoftGsmDec() {
101     onRelease();
102 }
103 
onInit()104 c2_status_t C2SoftGsmDec::onInit() {
105     if (!mGsm) mGsm = gsm_create();
106     if (!mGsm) return C2_NO_MEMORY;
107     int msopt = 1;
108     (void)gsm_option(mGsm, GSM_OPT_WAV49, &msopt);
109     mSignalledError = false;
110     mSignalledEos = false;
111     return C2_OK;
112 }
113 
onStop()114 c2_status_t C2SoftGsmDec::onStop() {
115     if (mGsm) {
116         gsm_destroy(mGsm);
117         mGsm = nullptr;
118     }
119     if (!mGsm) mGsm = gsm_create();
120     if (!mGsm) return C2_NO_MEMORY;
121     int msopt = 1;
122     (void)gsm_option(mGsm, GSM_OPT_WAV49, &msopt);
123     mSignalledError = false;
124     mSignalledEos = false;
125     return C2_OK;
126 }
127 
onReset()128 void C2SoftGsmDec::onReset() {
129     (void)onStop();
130 }
131 
onRelease()132 void C2SoftGsmDec::onRelease() {
133     if (mGsm) {
134         gsm_destroy(mGsm);
135         mGsm = nullptr;
136     }
137 }
138 
onFlush_sm()139 c2_status_t C2SoftGsmDec::onFlush_sm() {
140     return onStop();
141 }
142 
decodeGSM(gsm handle,int16_t * out,size_t outCapacity,uint8_t * in,size_t inSize)143 static size_t decodeGSM(gsm handle, int16_t *out, size_t outCapacity,
144                         uint8_t *in, size_t inSize) {
145     size_t outSize = 0;
146 
147     if (inSize % MSGSM_IN_FRM_SZ == 0
148             && (inSize / MSGSM_IN_FRM_SZ * MSGSM_OUT_FRM_SZ * sizeof(*out)
149                     <= outCapacity)) {
150         while (inSize > 0) {
151             gsm_decode(handle, in, out);
152             in += FRGSM_IN_FRM_SZ;
153             inSize -= FRGSM_IN_FRM_SZ;
154             out += FRGSM_OUT_FRM_SZ;
155             outSize += FRGSM_OUT_FRM_SZ;
156 
157             gsm_decode(handle, in, out);
158             in += FRGSM_IN_FRM_SZ_MINUS_1;
159             inSize -= FRGSM_IN_FRM_SZ_MINUS_1;
160             out += FRGSM_OUT_FRM_SZ;
161             outSize += FRGSM_OUT_FRM_SZ;
162         }
163     }
164 
165     return outSize * sizeof(int16_t);
166 }
167 
process(const std::unique_ptr<C2Work> & work,const std::shared_ptr<C2BlockPool> & pool)168 void C2SoftGsmDec::process(
169         const std::unique_ptr<C2Work> &work,
170         const std::shared_ptr<C2BlockPool> &pool) {
171     work->result = C2_OK;
172     work->workletsProcessed = 0u;
173     if (mSignalledError || mSignalledEos) {
174         work->result = C2_BAD_VALUE;
175         return;
176     }
177 
178     bool eos = ((work->input.flags & C2FrameData::FLAG_END_OF_STREAM) != 0);
179     C2ReadView rView = mDummyReadView;
180     size_t inOffset = 0u;
181     size_t inSize = 0u;
182     if (work->input.buffers.empty()) {
183         rView = work->input.buffers[0]->data().linearBlocks().front().map().get();
184         inSize = rView.capacity();
185         if (inSize && rView.error()) {
186             ALOGE("read view map failed %d", rView.error());
187             work->result = rView.error();
188             return;
189         }
190     }
191 
192     if (inSize == 0) {
193         work->worklets.front()->output.flags = work->input.flags;
194         work->worklets.front()->output.buffers.clear();
195         work->worklets.front()->output.ordinal = work->input.ordinal;
196         work->workletsProcessed = 1u;
197         if (eos) {
198             mSignalledEos = true;
199             ALOGV("signalled EOS");
200         }
201         return;
202     }
203     ALOGV("in buffer attr. size %zu timestamp %d frameindex %d", inSize,
204           (int)work->input.ordinal.timestamp.peeku(), (int)work->input.ordinal.frameIndex.peeku());
205 
206     size_t outCapacity = (inSize / MSGSM_IN_FRM_SZ ) * MSGSM_OUT_FRM_SZ * sizeof(int16_t);
207     std::shared_ptr<C2LinearBlock> block;
208     C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
209     c2_status_t err = pool->fetchLinearBlock(outCapacity, usage, &block);
210     if (err != C2_OK) {
211         ALOGE("fetchLinearBlock for Output failed with status %d", err);
212         work->result = C2_NO_MEMORY;
213         return;
214     }
215     C2WriteView wView = block->map().get();
216     if (wView.error()) {
217         ALOGE("write view map failed %d", wView.error());
218         work->result = wView.error();
219         return;
220     }
221 
222     int16_t *output = reinterpret_cast<int16_t *>(wView.data());
223     uint8_t *input = const_cast<uint8_t *>(rView.data() + inOffset);
224     size_t outSize = decodeGSM(mGsm, output, outCapacity, input, inSize);
225     if (!outSize) {
226         ALOGE("encountered improper insize or outsize");
227         mSignalledError = true;
228         work->result = C2_CORRUPTED;
229         return;
230     }
231     ALOGV("out buffer attr. size %zu", outSize);
232     work->worklets.front()->output.flags = work->input.flags;
233     work->worklets.front()->output.buffers.clear();
234     work->worklets.front()->output.buffers.push_back(createLinearBuffer(block, 0, outSize));
235     work->worklets.front()->output.ordinal = work->input.ordinal;
236     work->workletsProcessed = 1u;
237     if (eos) {
238         mSignalledEos = true;
239         ALOGV("signalled EOS");
240     }
241 }
242 
drain(uint32_t drainMode,const std::shared_ptr<C2BlockPool> & pool)243 c2_status_t C2SoftGsmDec::drain(
244         uint32_t drainMode,
245         const std::shared_ptr<C2BlockPool> &pool) {
246     (void) pool;
247     if (drainMode == NO_DRAIN) {
248         ALOGW("drain with NO_DRAIN: no-op");
249         return C2_OK;
250     }
251     if (drainMode == DRAIN_CHAIN) {
252         ALOGW("DRAIN_CHAIN not supported");
253         return C2_OMITTED;
254     }
255 
256     return C2_OK;
257 }
258 
259 class C2SoftGSMDecFactory : public C2ComponentFactory {
260 public:
C2SoftGSMDecFactory()261     C2SoftGSMDecFactory() : mHelper(std::static_pointer_cast<C2ReflectorHelper>(
262             GetCodec2PlatformComponentStore()->getParamReflector())) {
263     }
264 
createComponent(c2_node_id_t id,std::shared_ptr<C2Component> * const component,std::function<void (C2Component *)> deleter)265     virtual c2_status_t createComponent(
266             c2_node_id_t id,
267             std::shared_ptr<C2Component>* const component,
268             std::function<void(C2Component*)> deleter) override {
269         *component = std::shared_ptr<C2Component>(
270                 new C2SoftGsmDec(COMPONENT_NAME,
271                               id,
272                               std::make_shared<C2SoftGsmDec::IntfImpl>(mHelper)),
273                 deleter);
274         return C2_OK;
275     }
276 
createInterface(c2_node_id_t id,std::shared_ptr<C2ComponentInterface> * const interface,std::function<void (C2ComponentInterface *)> deleter)277     virtual c2_status_t createInterface(
278             c2_node_id_t id,
279             std::shared_ptr<C2ComponentInterface>* const interface,
280             std::function<void(C2ComponentInterface*)> deleter) override {
281         *interface = std::shared_ptr<C2ComponentInterface>(
282                 new SimpleInterface<C2SoftGsmDec::IntfImpl>(
283                         COMPONENT_NAME, id, std::make_shared<C2SoftGsmDec::IntfImpl>(mHelper)),
284                 deleter);
285         return C2_OK;
286     }
287 
288     virtual ~C2SoftGSMDecFactory() override = default;
289 
290 private:
291     std::shared_ptr<C2ReflectorHelper> mHelper;
292 };
293 
294 }  // namespace android
295 
CreateCodec2Factory()296 extern "C" ::C2ComponentFactory* CreateCodec2Factory() {
297     ALOGV("in %s", __func__);
298     return new ::android::C2SoftGSMDecFactory();
299 }
300 
DestroyCodec2Factory(::C2ComponentFactory * factory)301 extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) {
302     ALOGV("in %s", __func__);
303     delete factory;
304 }
305