SoftG711.cpp revision 10d023beb0544591d0f5ff556f7f67356f0d9189
1/*
2 * Copyright (C) 2011 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 "SoftG711"
19#include <utils/Log.h>
20
21#include "SoftG711.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
37SoftG711::SoftG711(
38        const char *name,
39        const OMX_CALLBACKTYPE *callbacks,
40        OMX_PTR appData,
41        OMX_COMPONENTTYPE **component)
42    : SimpleSoftOMXComponent(name, callbacks, appData, component),
43      mIsMLaw(true),
44      mNumChannels(1),
45      mSignalledError(false) {
46    if (!strcmp(name, "OMX.google.g711.alaw.decoder")) {
47        mIsMLaw = false;
48    } else {
49        CHECK(!strcmp(name, "OMX.google.g711.mlaw.decoder"));
50    }
51
52    initPorts();
53}
54
55SoftG711::~SoftG711() {
56}
57
58void SoftG711::initPorts() {
59    OMX_PARAM_PORTDEFINITIONTYPE def;
60    InitOMXParams(&def);
61
62    def.nPortIndex = 0;
63    def.eDir = OMX_DirInput;
64    def.nBufferCountMin = kNumBuffers;
65    def.nBufferCountActual = def.nBufferCountMin;
66    def.nBufferSize = 8192;
67    def.bEnabled = OMX_TRUE;
68    def.bPopulated = OMX_FALSE;
69    def.eDomain = OMX_PortDomainAudio;
70    def.bBuffersContiguous = OMX_FALSE;
71    def.nBufferAlignment = 1;
72
73    def.format.audio.cMIMEType =
74        const_cast<char *>(
75                mIsMLaw
76                    ? MEDIA_MIMETYPE_AUDIO_G711_MLAW
77                    : MEDIA_MIMETYPE_AUDIO_G711_ALAW);
78
79    def.format.audio.pNativeRender = NULL;
80    def.format.audio.bFlagErrorConcealment = OMX_FALSE;
81    def.format.audio.eEncoding = OMX_AUDIO_CodingG711;
82
83    addPort(def);
84
85    def.nPortIndex = 1;
86    def.eDir = OMX_DirOutput;
87    def.nBufferCountMin = kNumBuffers;
88    def.nBufferCountActual = def.nBufferCountMin;
89    def.nBufferSize = kMaxNumSamplesPerFrame * sizeof(int16_t);
90    def.bEnabled = OMX_TRUE;
91    def.bPopulated = OMX_FALSE;
92    def.eDomain = OMX_PortDomainAudio;
93    def.bBuffersContiguous = OMX_FALSE;
94    def.nBufferAlignment = 2;
95
96    def.format.audio.cMIMEType = const_cast<char *>("audio/raw");
97    def.format.audio.pNativeRender = NULL;
98    def.format.audio.bFlagErrorConcealment = OMX_FALSE;
99    def.format.audio.eEncoding = OMX_AUDIO_CodingPCM;
100
101    addPort(def);
102}
103
104OMX_ERRORTYPE SoftG711::internalGetParameter(
105        OMX_INDEXTYPE index, OMX_PTR params) {
106    switch (index) {
107        case OMX_IndexParamAudioPcm:
108        {
109            OMX_AUDIO_PARAM_PCMMODETYPE *pcmParams =
110                (OMX_AUDIO_PARAM_PCMMODETYPE *)params;
111
112            if (pcmParams->nPortIndex > 1) {
113                return OMX_ErrorUndefined;
114            }
115
116            pcmParams->eNumData = OMX_NumericalDataSigned;
117            pcmParams->eEndian = OMX_EndianBig;
118            pcmParams->bInterleaved = OMX_TRUE;
119            pcmParams->nBitPerSample = 16;
120            if (pcmParams->nPortIndex == 0) {
121                // input port
122                pcmParams->ePCMMode = mIsMLaw ? OMX_AUDIO_PCMModeMULaw
123                                              : OMX_AUDIO_PCMModeALaw;
124            } else {
125                // output port
126                pcmParams->ePCMMode = OMX_AUDIO_PCMModeLinear;
127            }
128            pcmParams->eChannelMapping[0] = OMX_AUDIO_ChannelLF;
129            pcmParams->eChannelMapping[1] = OMX_AUDIO_ChannelRF;
130
131            pcmParams->nChannels = mNumChannels;
132            pcmParams->nSamplingRate = 8000;
133
134            return OMX_ErrorNone;
135        }
136
137        default:
138            return SimpleSoftOMXComponent::internalGetParameter(index, params);
139    }
140}
141
142OMX_ERRORTYPE SoftG711::internalSetParameter(
143        OMX_INDEXTYPE index, const OMX_PTR params) {
144    switch (index) {
145        case OMX_IndexParamAudioPcm:
146        {
147            OMX_AUDIO_PARAM_PCMMODETYPE *pcmParams =
148                (OMX_AUDIO_PARAM_PCMMODETYPE *)params;
149
150            if (pcmParams->nPortIndex != 0 && pcmParams->nPortIndex != 1) {
151                return OMX_ErrorUndefined;
152            }
153
154            if (pcmParams->nChannels < 1 || pcmParams->nChannels > 2) {
155                return OMX_ErrorUndefined;
156            }
157
158            if(pcmParams->nPortIndex == 0) {
159                mNumChannels = pcmParams->nChannels;
160            }
161
162            return OMX_ErrorNone;
163        }
164
165        case OMX_IndexParamStandardComponentRole:
166        {
167            const OMX_PARAM_COMPONENTROLETYPE *roleParams =
168                (const OMX_PARAM_COMPONENTROLETYPE *)params;
169
170            if (mIsMLaw) {
171                if (strncmp((const char *)roleParams->cRole,
172                            "audio_decoder.g711mlaw",
173                            OMX_MAX_STRINGNAME_SIZE - 1)) {
174                    return OMX_ErrorUndefined;
175                }
176            } else {
177                if (strncmp((const char *)roleParams->cRole,
178                            "audio_decoder.g711alaw",
179                            OMX_MAX_STRINGNAME_SIZE - 1)) {
180                    return OMX_ErrorUndefined;
181                }
182            }
183
184            return OMX_ErrorNone;
185        }
186
187        default:
188            return SimpleSoftOMXComponent::internalSetParameter(index, params);
189    }
190}
191
192void SoftG711::onQueueFilled(OMX_U32 /* portIndex */) {
193    if (mSignalledError) {
194        return;
195    }
196
197    List<BufferInfo *> &inQueue = getPortQueue(0);
198    List<BufferInfo *> &outQueue = getPortQueue(1);
199
200    while (!inQueue.empty() && !outQueue.empty()) {
201        BufferInfo *inInfo = *inQueue.begin();
202        OMX_BUFFERHEADERTYPE *inHeader = inInfo->mHeader;
203
204        BufferInfo *outInfo = *outQueue.begin();
205        OMX_BUFFERHEADERTYPE *outHeader = outInfo->mHeader;
206
207        if (inHeader->nFlags & OMX_BUFFERFLAG_EOS) {
208            inQueue.erase(inQueue.begin());
209            inInfo->mOwnedByUs = false;
210            notifyEmptyBufferDone(inHeader);
211
212            outHeader->nFilledLen = 0;
213            outHeader->nFlags = OMX_BUFFERFLAG_EOS;
214
215            outQueue.erase(outQueue.begin());
216            outInfo->mOwnedByUs = false;
217            notifyFillBufferDone(outHeader);
218            return;
219        }
220
221        if (inHeader->nFilledLen > kMaxNumSamplesPerFrame) {
222            ALOGE("input buffer too large (%d).", inHeader->nFilledLen);
223
224            notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL);
225            mSignalledError = true;
226        }
227
228        const uint8_t *inputptr = inHeader->pBuffer + inHeader->nOffset;
229
230        if (mIsMLaw) {
231            DecodeMLaw(
232                    reinterpret_cast<int16_t *>(outHeader->pBuffer),
233                    inputptr, inHeader->nFilledLen);
234        } else {
235            DecodeALaw(
236                    reinterpret_cast<int16_t *>(outHeader->pBuffer),
237                    inputptr, inHeader->nFilledLen);
238        }
239
240        outHeader->nTimeStamp = inHeader->nTimeStamp;
241        outHeader->nOffset = 0;
242        outHeader->nFilledLen = inHeader->nFilledLen * sizeof(int16_t);
243        outHeader->nFlags = 0;
244
245        inInfo->mOwnedByUs = false;
246        inQueue.erase(inQueue.begin());
247        inInfo = NULL;
248        notifyEmptyBufferDone(inHeader);
249        inHeader = NULL;
250
251        outInfo->mOwnedByUs = false;
252        outQueue.erase(outQueue.begin());
253        outInfo = NULL;
254        notifyFillBufferDone(outHeader);
255        outHeader = NULL;
256    }
257}
258
259// static
260void SoftG711::DecodeALaw(
261        int16_t *out, const uint8_t *in, size_t inSize) {
262    while (inSize-- > 0) {
263        int32_t x = *in++;
264
265        int32_t ix = x ^ 0x55;
266        ix &= 0x7f;
267
268        int32_t iexp = ix >> 4;
269        int32_t mant = ix & 0x0f;
270
271        if (iexp > 0) {
272            mant += 16;
273        }
274
275        mant = (mant << 4) + 8;
276
277        if (iexp > 1) {
278            mant = mant << (iexp - 1);
279        }
280
281        *out++ = (x > 127) ? mant : -mant;
282    }
283}
284
285// static
286void SoftG711::DecodeMLaw(
287        int16_t *out, const uint8_t *in, size_t inSize) {
288    while (inSize-- > 0) {
289        int32_t x = *in++;
290
291        int32_t mantissa = ~x;
292        int32_t exponent = (mantissa >> 4) & 7;
293        int32_t segment = exponent + 1;
294        mantissa &= 0x0f;
295
296        int32_t step = 4 << segment;
297
298        int32_t abs = (0x80l << exponent) + step * mantissa + step / 2 - 4 * 33;
299
300        *out++ = (x < 0x80) ? -abs : abs;
301    }
302}
303
304}  // namespace android
305
306android::SoftOMXComponent *createSoftOMXComponent(
307        const char *name, const OMX_CALLBACKTYPE *callbacks,
308        OMX_PTR appData, OMX_COMPONENTTYPE **component) {
309    return new android::SoftG711(name, callbacks, appData, component);
310}
311
312