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