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