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