SoftGSM.cpp revision cafe4d8511d4e8185d177b367c1f0b764f9a1789
1/*
2 * Copyright (C) 2012 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 "SoftGSM"
19#include <utils/Log.h>
20
21#include "SoftGSM.h"
22
23#include <media/stagefright/foundation/ADebug.h>
24#include <media/stagefright/MediaDefs.h>
25
26namespace android {
27
28template<class T>
29static void InitOMXParams(T *params) {
30    params->nSize = sizeof(T);
31    params->nVersion.s.nVersionMajor = 1;
32    params->nVersion.s.nVersionMinor = 0;
33    params->nVersion.s.nRevision = 0;
34    params->nVersion.s.nStep = 0;
35}
36
37// Microsoft WAV GSM encoding packs two GSM frames into 65 bytes.
38static const int kMSGSMFrameSize = 65;
39
40SoftGSM::SoftGSM(
41        const char *name,
42        const OMX_CALLBACKTYPE *callbacks,
43        OMX_PTR appData,
44        OMX_COMPONENTTYPE **component)
45    : SimpleSoftOMXComponent(name, callbacks, appData, component),
46      mSignalledError(false) {
47
48    CHECK(!strcmp(name, "OMX.google.gsm.decoder"));
49
50    mGsm = gsm_create();
51    CHECK(mGsm);
52    int msopt = 1;
53    gsm_option(mGsm, GSM_OPT_WAV49, &msopt);
54
55    initPorts();
56}
57
58SoftGSM::~SoftGSM() {
59    gsm_destroy(mGsm);
60}
61
62void SoftGSM::initPorts() {
63    OMX_PARAM_PORTDEFINITIONTYPE def;
64    InitOMXParams(&def);
65
66    def.nPortIndex = 0;
67    def.eDir = OMX_DirInput;
68    def.nBufferCountMin = kNumBuffers;
69    def.nBufferCountActual = def.nBufferCountMin;
70    def.nBufferSize = 1024 / kMSGSMFrameSize * kMSGSMFrameSize;
71    def.bEnabled = OMX_TRUE;
72    def.bPopulated = OMX_FALSE;
73    def.eDomain = OMX_PortDomainAudio;
74    def.bBuffersContiguous = OMX_FALSE;
75    def.nBufferAlignment = 1;
76
77    def.format.audio.cMIMEType =
78        const_cast<char *>(MEDIA_MIMETYPE_AUDIO_MSGSM);
79
80    def.format.audio.pNativeRender = NULL;
81    def.format.audio.bFlagErrorConcealment = OMX_FALSE;
82    def.format.audio.eEncoding = OMX_AUDIO_CodingGSMFR;
83
84    addPort(def);
85
86    def.nPortIndex = 1;
87    def.eDir = OMX_DirOutput;
88    def.nBufferCountMin = kNumBuffers;
89    def.nBufferCountActual = def.nBufferCountMin;
90    def.nBufferSize = kMaxNumSamplesPerFrame * sizeof(int16_t);
91    def.bEnabled = OMX_TRUE;
92    def.bPopulated = OMX_FALSE;
93    def.eDomain = OMX_PortDomainAudio;
94    def.bBuffersContiguous = OMX_FALSE;
95    def.nBufferAlignment = 2;
96
97    def.format.audio.cMIMEType = const_cast<char *>("audio/raw");
98    def.format.audio.pNativeRender = NULL;
99    def.format.audio.bFlagErrorConcealment = OMX_FALSE;
100    def.format.audio.eEncoding = OMX_AUDIO_CodingPCM;
101
102    addPort(def);
103}
104
105OMX_ERRORTYPE SoftGSM::internalGetParameter(
106        OMX_INDEXTYPE index, OMX_PTR params) {
107    switch (index) {
108        case OMX_IndexParamAudioPcm:
109        {
110            OMX_AUDIO_PARAM_PCMMODETYPE *pcmParams =
111                (OMX_AUDIO_PARAM_PCMMODETYPE *)params;
112
113            if (pcmParams->nPortIndex > 1) {
114                return OMX_ErrorUndefined;
115            }
116
117            pcmParams->eNumData = OMX_NumericalDataSigned;
118            pcmParams->eEndian = OMX_EndianBig;
119            pcmParams->bInterleaved = OMX_TRUE;
120            pcmParams->nBitPerSample = 16;
121            pcmParams->ePCMMode = OMX_AUDIO_PCMModeLinear;
122            pcmParams->eChannelMapping[0] = OMX_AUDIO_ChannelLF;
123            pcmParams->eChannelMapping[1] = OMX_AUDIO_ChannelRF;
124
125            pcmParams->nChannels = 1;
126            pcmParams->nSamplingRate = 8000;
127
128            return OMX_ErrorNone;
129        }
130
131        default:
132            return SimpleSoftOMXComponent::internalGetParameter(index, params);
133    }
134}
135
136OMX_ERRORTYPE SoftGSM::internalSetParameter(
137        OMX_INDEXTYPE index, const OMX_PTR params) {
138    switch (index) {
139        case OMX_IndexParamAudioPcm:
140        {
141            OMX_AUDIO_PARAM_PCMMODETYPE *pcmParams =
142                (OMX_AUDIO_PARAM_PCMMODETYPE *)params;
143
144            if (pcmParams->nPortIndex != 0 && pcmParams->nPortIndex != 1) {
145                return OMX_ErrorUndefined;
146            }
147
148            if (pcmParams->nChannels != 1) {
149                return OMX_ErrorUndefined;
150            }
151
152            if (pcmParams->nSamplingRate != 8000) {
153                return OMX_ErrorUndefined;
154            }
155
156            return OMX_ErrorNone;
157        }
158
159        case OMX_IndexParamStandardComponentRole:
160        {
161            const OMX_PARAM_COMPONENTROLETYPE *roleParams =
162                (const OMX_PARAM_COMPONENTROLETYPE *)params;
163
164            if (strncmp((const char *)roleParams->cRole,
165                        "audio_decoder.gsm",
166                        OMX_MAX_STRINGNAME_SIZE - 1)) {
167                return OMX_ErrorUndefined;
168            }
169
170            return OMX_ErrorNone;
171        }
172
173        default:
174            return SimpleSoftOMXComponent::internalSetParameter(index, params);
175    }
176}
177
178void SoftGSM::onQueueFilled(OMX_U32 /* portIndex */) {
179    if (mSignalledError) {
180        return;
181    }
182
183    List<BufferInfo *> &inQueue = getPortQueue(0);
184    List<BufferInfo *> &outQueue = getPortQueue(1);
185
186    while (!inQueue.empty() && !outQueue.empty()) {
187        BufferInfo *inInfo = *inQueue.begin();
188        OMX_BUFFERHEADERTYPE *inHeader = inInfo->mHeader;
189
190        BufferInfo *outInfo = *outQueue.begin();
191        OMX_BUFFERHEADERTYPE *outHeader = outInfo->mHeader;
192
193        if (inHeader->nFlags & OMX_BUFFERFLAG_EOS) {
194            inQueue.erase(inQueue.begin());
195            inInfo->mOwnedByUs = false;
196            notifyEmptyBufferDone(inHeader);
197
198            outHeader->nFilledLen = 0;
199            outHeader->nFlags = OMX_BUFFERFLAG_EOS;
200
201            outQueue.erase(outQueue.begin());
202            outInfo->mOwnedByUs = false;
203            notifyFillBufferDone(outHeader);
204            return;
205        }
206
207        if (inHeader->nFilledLen > kMaxNumSamplesPerFrame) {
208            ALOGE("input buffer too large (%d).", inHeader->nFilledLen);
209            notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL);
210            mSignalledError = true;
211        }
212
213        if(((inHeader->nFilledLen / kMSGSMFrameSize) * kMSGSMFrameSize) != inHeader->nFilledLen) {
214            ALOGE("input buffer not multiple of %d (%d).", kMSGSMFrameSize, inHeader->nFilledLen);
215            notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL);
216            mSignalledError = true;
217        }
218
219        uint8_t *inputptr = inHeader->pBuffer + inHeader->nOffset;
220
221        int n = mSignalledError ? 0 : DecodeGSM(mGsm,
222                  reinterpret_cast<int16_t *>(outHeader->pBuffer), inputptr, inHeader->nFilledLen);
223
224        outHeader->nTimeStamp = inHeader->nTimeStamp;
225        outHeader->nOffset = 0;
226        outHeader->nFilledLen = n * sizeof(int16_t);
227        outHeader->nFlags = 0;
228
229        inInfo->mOwnedByUs = false;
230        inQueue.erase(inQueue.begin());
231        inInfo = NULL;
232        notifyEmptyBufferDone(inHeader);
233        inHeader = NULL;
234
235        outInfo->mOwnedByUs = false;
236        outQueue.erase(outQueue.begin());
237        outInfo = NULL;
238        notifyFillBufferDone(outHeader);
239        outHeader = NULL;
240    }
241}
242
243
244// static
245int SoftGSM::DecodeGSM(gsm handle,
246        int16_t *out, uint8_t *in, size_t inSize) {
247
248    int ret = 0;
249    while (inSize > 0) {
250        gsm_decode(handle, in, out);
251        in += 33;
252        inSize -= 33;
253        out += 160;
254        ret += 160;
255        gsm_decode(handle, in, out);
256        in += 32;
257        inSize -= 32;
258        out += 160;
259        ret += 160;
260    }
261    return ret;
262}
263
264void SoftGSM::onPortFlushCompleted(OMX_U32 portIndex) {
265    if (portIndex == 0) {
266        gsm_destroy(mGsm);
267        mGsm = gsm_create();
268        int msopt = 1;
269        gsm_option(mGsm, GSM_OPT_WAV49, &msopt);
270    }
271}
272
273void SoftGSM::onReset() {
274    gsm_destroy(mGsm);
275    mGsm = gsm_create();
276    int msopt = 1;
277    gsm_option(mGsm, GSM_OPT_WAV49, &msopt);
278    mSignalledError = false;
279}
280
281
282
283
284}  // namespace android
285
286android::SoftOMXComponent *createSoftOMXComponent(
287        const char *name, const OMX_CALLBACKTYPE *callbacks,
288        OMX_PTR appData, OMX_COMPONENTTYPE **component) {
289    return new android::SoftGSM(name, callbacks, appData, component);
290}
291
292