SoftG711.cpp revision 84333e0475bc911adc16417f4ca327c975cf6c36
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            pcmParams->ePCMMode = OMX_AUDIO_PCMModeLinear;
121            pcmParams->eChannelMapping[0] = OMX_AUDIO_ChannelLF;
122            pcmParams->eChannelMapping[1] = OMX_AUDIO_ChannelRF;
123
124            pcmParams->nChannels = mNumChannels;
125            pcmParams->nSamplingRate = 8000;
126
127            return OMX_ErrorNone;
128        }
129
130        default:
131            return SimpleSoftOMXComponent::internalGetParameter(index, params);
132    }
133}
134
135OMX_ERRORTYPE SoftG711::internalSetParameter(
136        OMX_INDEXTYPE index, const OMX_PTR params) {
137    switch (index) {
138        case OMX_IndexParamAudioPcm:
139        {
140            OMX_AUDIO_PARAM_PCMMODETYPE *pcmParams =
141                (OMX_AUDIO_PARAM_PCMMODETYPE *)params;
142
143            if (pcmParams->nPortIndex != 0 && pcmParams->nPortIndex != 1) {
144                return OMX_ErrorUndefined;
145            }
146
147            if (pcmParams->nChannels < 1 || pcmParams->nChannels > 2) {
148                return OMX_ErrorUndefined;
149            }
150
151            if(pcmParams->nPortIndex == 0) {
152                mNumChannels = pcmParams->nChannels;
153            }
154
155            return OMX_ErrorNone;
156        }
157
158        case OMX_IndexParamStandardComponentRole:
159        {
160            const OMX_PARAM_COMPONENTROLETYPE *roleParams =
161                (const OMX_PARAM_COMPONENTROLETYPE *)params;
162
163            if (mIsMLaw) {
164                if (strncmp((const char *)roleParams->cRole,
165                            "audio_decoder.g711mlaw",
166                            OMX_MAX_STRINGNAME_SIZE - 1)) {
167                    return OMX_ErrorUndefined;
168                }
169            } else {
170                if (strncmp((const char *)roleParams->cRole,
171                            "audio_decoder.g711alaw",
172                            OMX_MAX_STRINGNAME_SIZE - 1)) {
173                    return OMX_ErrorUndefined;
174                }
175            }
176
177            return OMX_ErrorNone;
178        }
179
180        default:
181            return SimpleSoftOMXComponent::internalSetParameter(index, params);
182    }
183}
184
185void SoftG711::onQueueFilled(OMX_U32 /* portIndex */) {
186    if (mSignalledError) {
187        return;
188    }
189
190    List<BufferInfo *> &inQueue = getPortQueue(0);
191    List<BufferInfo *> &outQueue = getPortQueue(1);
192
193    while (!inQueue.empty() && !outQueue.empty()) {
194        BufferInfo *inInfo = *inQueue.begin();
195        OMX_BUFFERHEADERTYPE *inHeader = inInfo->mHeader;
196
197        BufferInfo *outInfo = *outQueue.begin();
198        OMX_BUFFERHEADERTYPE *outHeader = outInfo->mHeader;
199
200        if (inHeader->nFlags & OMX_BUFFERFLAG_EOS) {
201            inQueue.erase(inQueue.begin());
202            inInfo->mOwnedByUs = false;
203            notifyEmptyBufferDone(inHeader);
204
205            outHeader->nFilledLen = 0;
206            outHeader->nFlags = OMX_BUFFERFLAG_EOS;
207
208            outQueue.erase(outQueue.begin());
209            outInfo->mOwnedByUs = false;
210            notifyFillBufferDone(outHeader);
211            return;
212        }
213
214        if (inHeader->nFilledLen > kMaxNumSamplesPerFrame) {
215            ALOGE("input buffer too large (%ld).", inHeader->nFilledLen);
216
217            notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL);
218            mSignalledError = true;
219        }
220
221        const uint8_t *inputptr = inHeader->pBuffer + inHeader->nOffset;
222
223        if (mIsMLaw) {
224            DecodeMLaw(
225                    reinterpret_cast<int16_t *>(outHeader->pBuffer),
226                    inputptr, inHeader->nFilledLen);
227        } else {
228            DecodeALaw(
229                    reinterpret_cast<int16_t *>(outHeader->pBuffer),
230                    inputptr, inHeader->nFilledLen);
231        }
232
233        outHeader->nTimeStamp = inHeader->nTimeStamp;
234        outHeader->nOffset = 0;
235        outHeader->nFilledLen = inHeader->nFilledLen * sizeof(int16_t);
236        outHeader->nFlags = 0;
237
238        inInfo->mOwnedByUs = false;
239        inQueue.erase(inQueue.begin());
240        inInfo = NULL;
241        notifyEmptyBufferDone(inHeader);
242        inHeader = NULL;
243
244        outInfo->mOwnedByUs = false;
245        outQueue.erase(outQueue.begin());
246        outInfo = NULL;
247        notifyFillBufferDone(outHeader);
248        outHeader = NULL;
249    }
250}
251
252// static
253void SoftG711::DecodeALaw(
254        int16_t *out, const uint8_t *in, size_t inSize) {
255    while (inSize-- > 0) {
256        int32_t x = *in++;
257
258        int32_t ix = x ^ 0x55;
259        ix &= 0x7f;
260
261        int32_t iexp = ix >> 4;
262        int32_t mant = ix & 0x0f;
263
264        if (iexp > 0) {
265            mant += 16;
266        }
267
268        mant = (mant << 4) + 8;
269
270        if (iexp > 1) {
271            mant = mant << (iexp - 1);
272        }
273
274        *out++ = (x > 127) ? mant : -mant;
275    }
276}
277
278// static
279void SoftG711::DecodeMLaw(
280        int16_t *out, const uint8_t *in, size_t inSize) {
281    while (inSize-- > 0) {
282        int32_t x = *in++;
283
284        int32_t mantissa = ~x;
285        int32_t exponent = (mantissa >> 4) & 7;
286        int32_t segment = exponent + 1;
287        mantissa &= 0x0f;
288
289        int32_t step = 4 << segment;
290
291        int32_t abs = (0x80l << exponent) + step * mantissa + step / 2 - 4 * 33;
292
293        *out++ = (x < 0x80) ? -abs : abs;
294    }
295}
296
297}  // namespace android
298
299android::SoftOMXComponent *createSoftOMXComponent(
300        const char *name, const OMX_CALLBACKTYPE *callbacks,
301        OMX_PTR appData, OMX_COMPONENTTYPE **component) {
302    return new android::SoftG711(name, callbacks, appData, component);
303}
304
305