SoftAAC2.cpp revision 94f60e297b9e45fac387cf10b6ce1acc067cb4a1
1/* 2 * Copyright (C) 2012 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_TAG "SoftAAC2" 18#include <utils/Log.h> 19 20#include "SoftAAC2.h" 21 22#include <media/stagefright/foundation/ADebug.h> 23#include <media/stagefright/foundation/hexdump.h> 24 25#define FILEREAD_MAX_LAYERS 2 26 27namespace android { 28 29template<class T> 30static void InitOMXParams(T *params) { 31 params->nSize = sizeof(T); 32 params->nVersion.s.nVersionMajor = 1; 33 params->nVersion.s.nVersionMinor = 0; 34 params->nVersion.s.nRevision = 0; 35 params->nVersion.s.nStep = 0; 36} 37 38SoftAAC2::SoftAAC2( 39 const char *name, 40 const OMX_CALLBACKTYPE *callbacks, 41 OMX_PTR appData, 42 OMX_COMPONENTTYPE **component) 43 : SimpleSoftOMXComponent(name, callbacks, appData, component), 44 mAACDecoder(NULL), 45 mStreamInfo(NULL), 46 mIsADTS(false), 47 mInputBufferCount(0), 48 mSignalledError(false), 49 mInputDiscontinuity(false), 50 mAnchorTimeUs(0), 51 mNumSamplesOutput(0), 52 mOutputPortSettingsChange(NONE) { 53 initPorts(); 54 CHECK_EQ(initDecoder(), (status_t)OK); 55} 56 57SoftAAC2::~SoftAAC2() { 58 aacDecoder_Close(mAACDecoder); 59} 60 61void SoftAAC2::initPorts() { 62 OMX_PARAM_PORTDEFINITIONTYPE def; 63 InitOMXParams(&def); 64 65 def.nPortIndex = 0; 66 def.eDir = OMX_DirInput; 67 def.nBufferCountMin = kNumInputBuffers; 68 def.nBufferCountActual = def.nBufferCountMin; 69 def.nBufferSize = 8192; 70 def.bEnabled = OMX_TRUE; 71 def.bPopulated = OMX_FALSE; 72 def.eDomain = OMX_PortDomainAudio; 73 def.bBuffersContiguous = OMX_FALSE; 74 def.nBufferAlignment = 1; 75 76 def.format.audio.cMIMEType = const_cast<char *>("audio/aac"); 77 def.format.audio.pNativeRender = NULL; 78 def.format.audio.bFlagErrorConcealment = OMX_FALSE; 79 def.format.audio.eEncoding = OMX_AUDIO_CodingAAC; 80 81 addPort(def); 82 83 def.nPortIndex = 1; 84 def.eDir = OMX_DirOutput; 85 def.nBufferCountMin = kNumOutputBuffers; 86 def.nBufferCountActual = def.nBufferCountMin; 87 def.nBufferSize = 8192 * 2; 88 def.bEnabled = OMX_TRUE; 89 def.bPopulated = OMX_FALSE; 90 def.eDomain = OMX_PortDomainAudio; 91 def.bBuffersContiguous = OMX_FALSE; 92 def.nBufferAlignment = 2; 93 94 def.format.audio.cMIMEType = const_cast<char *>("audio/raw"); 95 def.format.audio.pNativeRender = NULL; 96 def.format.audio.bFlagErrorConcealment = OMX_FALSE; 97 def.format.audio.eEncoding = OMX_AUDIO_CodingPCM; 98 99 addPort(def); 100} 101 102status_t SoftAAC2::initDecoder() { 103 status_t status = UNKNOWN_ERROR; 104 mAACDecoder = aacDecoder_Open(TT_MP4_RAW, /* num layers */ 1); 105 if (mAACDecoder != NULL) { 106 mStreamInfo = aacDecoder_GetStreamInfo(mAACDecoder); 107 if (mStreamInfo != NULL) { 108 status = OK; 109 } 110 } 111 mIsFirst = true; 112 return status; 113} 114 115OMX_ERRORTYPE SoftAAC2::internalGetParameter( 116 OMX_INDEXTYPE index, OMX_PTR params) { 117 switch (index) { 118 case OMX_IndexParamAudioAac: 119 { 120 OMX_AUDIO_PARAM_AACPROFILETYPE *aacParams = 121 (OMX_AUDIO_PARAM_AACPROFILETYPE *)params; 122 123 if (aacParams->nPortIndex != 0) { 124 return OMX_ErrorUndefined; 125 } 126 127 aacParams->nBitRate = 0; 128 aacParams->nAudioBandWidth = 0; 129 aacParams->nAACtools = 0; 130 aacParams->nAACERtools = 0; 131 aacParams->eAACProfile = OMX_AUDIO_AACObjectMain; 132 133 aacParams->eAACStreamFormat = 134 mIsADTS 135 ? OMX_AUDIO_AACStreamFormatMP4ADTS 136 : OMX_AUDIO_AACStreamFormatMP4FF; 137 138 aacParams->eChannelMode = OMX_AUDIO_ChannelModeStereo; 139 140 if (!isConfigured()) { 141 aacParams->nChannels = 1; 142 aacParams->nSampleRate = 44100; 143 aacParams->nFrameLength = 0; 144 } else { 145 aacParams->nChannels = mStreamInfo->numChannels; 146 aacParams->nSampleRate = mStreamInfo->sampleRate; 147 aacParams->nFrameLength = mStreamInfo->frameSize; 148 } 149 150 return OMX_ErrorNone; 151 } 152 153 case OMX_IndexParamAudioPcm: 154 { 155 OMX_AUDIO_PARAM_PCMMODETYPE *pcmParams = 156 (OMX_AUDIO_PARAM_PCMMODETYPE *)params; 157 158 if (pcmParams->nPortIndex != 1) { 159 return OMX_ErrorUndefined; 160 } 161 162 pcmParams->eNumData = OMX_NumericalDataSigned; 163 pcmParams->eEndian = OMX_EndianBig; 164 pcmParams->bInterleaved = OMX_TRUE; 165 pcmParams->nBitPerSample = 16; 166 pcmParams->ePCMMode = OMX_AUDIO_PCMModeLinear; 167 pcmParams->eChannelMapping[0] = OMX_AUDIO_ChannelLF; 168 pcmParams->eChannelMapping[1] = OMX_AUDIO_ChannelRF; 169 pcmParams->eChannelMapping[2] = OMX_AUDIO_ChannelCF; 170 pcmParams->eChannelMapping[3] = OMX_AUDIO_ChannelLFE; 171 pcmParams->eChannelMapping[4] = OMX_AUDIO_ChannelLS; 172 pcmParams->eChannelMapping[5] = OMX_AUDIO_ChannelRS; 173 174 if (!isConfigured()) { 175 pcmParams->nChannels = 1; 176 pcmParams->nSamplingRate = 44100; 177 } else { 178 pcmParams->nChannels = mStreamInfo->numChannels; 179 pcmParams->nSamplingRate = mStreamInfo->sampleRate; 180 } 181 182 return OMX_ErrorNone; 183 } 184 185 default: 186 return SimpleSoftOMXComponent::internalGetParameter(index, params); 187 } 188 189} 190 191OMX_ERRORTYPE SoftAAC2::internalSetParameter( 192 OMX_INDEXTYPE index, const OMX_PTR params) { 193 switch (index) { 194 case OMX_IndexParamStandardComponentRole: 195 { 196 const OMX_PARAM_COMPONENTROLETYPE *roleParams = 197 (const OMX_PARAM_COMPONENTROLETYPE *)params; 198 199 if (strncmp((const char *)roleParams->cRole, 200 "audio_decoder.aac", 201 OMX_MAX_STRINGNAME_SIZE - 1)) { 202 return OMX_ErrorUndefined; 203 } 204 205 return OMX_ErrorNone; 206 } 207 208 case OMX_IndexParamAudioAac: 209 { 210 const OMX_AUDIO_PARAM_AACPROFILETYPE *aacParams = 211 (const OMX_AUDIO_PARAM_AACPROFILETYPE *)params; 212 213 if (aacParams->nPortIndex != 0) { 214 return OMX_ErrorUndefined; 215 } 216 217 if (aacParams->eAACStreamFormat == OMX_AUDIO_AACStreamFormatMP4FF) { 218 mIsADTS = false; 219 } else if (aacParams->eAACStreamFormat 220 == OMX_AUDIO_AACStreamFormatMP4ADTS) { 221 mIsADTS = true; 222 } else { 223 return OMX_ErrorUndefined; 224 } 225 226 return OMX_ErrorNone; 227 } 228 229 case OMX_IndexParamAudioPcm: 230 { 231 const OMX_AUDIO_PARAM_PCMMODETYPE *pcmParams = 232 (OMX_AUDIO_PARAM_PCMMODETYPE *)params; 233 234 if (pcmParams->nPortIndex != 1) { 235 return OMX_ErrorUndefined; 236 } 237 238 return OMX_ErrorNone; 239 } 240 241 default: 242 return SimpleSoftOMXComponent::internalSetParameter(index, params); 243 } 244} 245 246bool SoftAAC2::isConfigured() const { 247 return mInputBufferCount > 0; 248} 249 250void SoftAAC2::onQueueFilled(OMX_U32 portIndex) { 251 if (mSignalledError || mOutputPortSettingsChange != NONE) { 252 return; 253 } 254 255 UCHAR* inBuffer[FILEREAD_MAX_LAYERS]; 256 UINT inBufferLength[FILEREAD_MAX_LAYERS] = {0}; 257 UINT bytesValid[FILEREAD_MAX_LAYERS] = {0}; 258 259 List<BufferInfo *> &inQueue = getPortQueue(0); 260 List<BufferInfo *> &outQueue = getPortQueue(1); 261 262 if (portIndex == 0 && mInputBufferCount == 0) { 263 ++mInputBufferCount; 264 BufferInfo *info = *inQueue.begin(); 265 OMX_BUFFERHEADERTYPE *header = info->mHeader; 266 267 inBuffer[0] = header->pBuffer + header->nOffset; 268 inBufferLength[0] = header->nFilledLen; 269 270 AAC_DECODER_ERROR decoderErr = 271 aacDecoder_ConfigRaw(mAACDecoder, 272 inBuffer, 273 inBufferLength); 274 275 if (decoderErr != AAC_DEC_OK) { 276 mSignalledError = true; 277 notify(OMX_EventError, OMX_ErrorUndefined, decoderErr, NULL); 278 return; 279 } 280 inQueue.erase(inQueue.begin()); 281 info->mOwnedByUs = false; 282 notifyEmptyBufferDone(header); 283 284 notify(OMX_EventPortSettingsChanged, 1, 0, NULL); 285 mOutputPortSettingsChange = AWAITING_DISABLED; 286 return; 287 } 288 289 while (!inQueue.empty() && !outQueue.empty()) { 290 BufferInfo *inInfo = *inQueue.begin(); 291 OMX_BUFFERHEADERTYPE *inHeader = inInfo->mHeader; 292 293 BufferInfo *outInfo = *outQueue.begin(); 294 OMX_BUFFERHEADERTYPE *outHeader = outInfo->mHeader; 295 296 if (inHeader->nFlags & OMX_BUFFERFLAG_EOS) { 297 inQueue.erase(inQueue.begin()); 298 inInfo->mOwnedByUs = false; 299 notifyEmptyBufferDone(inHeader); 300 301 // flush out the decoder's delayed data by calling DecodeFrame one more time, with 302 // the AACDEC_FLUSH flag set 303 INT_PCM *outBuffer = 304 reinterpret_cast<INT_PCM *>(outHeader->pBuffer + outHeader->nOffset); 305 AAC_DECODER_ERROR decoderErr = aacDecoder_DecodeFrame(mAACDecoder, 306 outBuffer, 307 outHeader->nAllocLen, 308 AACDEC_FLUSH); 309 if (decoderErr != AAC_DEC_OK) { 310 mSignalledError = true; 311 notify(OMX_EventError, OMX_ErrorUndefined, decoderErr, NULL); 312 return; 313 } 314 315 outHeader->nFilledLen = 316 mStreamInfo->frameSize * sizeof(int16_t) * mStreamInfo->numChannels; 317 outHeader->nFlags = OMX_BUFFERFLAG_EOS; 318 319 outQueue.erase(outQueue.begin()); 320 outInfo->mOwnedByUs = false; 321 notifyFillBufferDone(outHeader); 322 return; 323 } 324 325 if (inHeader->nOffset == 0) { 326 mAnchorTimeUs = inHeader->nTimeStamp; 327 mNumSamplesOutput = 0; 328 } 329 330 size_t adtsHeaderSize = 0; 331 if (mIsADTS) { 332 // skip 30 bits, aac_frame_length follows. 333 // ssssssss ssssiiip ppffffPc ccohCCll llllllll lll????? 334 335 const uint8_t *adtsHeader = inHeader->pBuffer + inHeader->nOffset; 336 337 CHECK_GE(inHeader->nFilledLen, 7); 338 339 bool protectionAbsent = (adtsHeader[1] & 1); 340 341 unsigned aac_frame_length = 342 ((adtsHeader[3] & 3) << 11) 343 | (adtsHeader[4] << 3) 344 | (adtsHeader[5] >> 5); 345 346 CHECK_GE(inHeader->nFilledLen, aac_frame_length); 347 348 adtsHeaderSize = (protectionAbsent ? 7 : 9); 349 350 inBuffer[0] = (UCHAR *)adtsHeader + adtsHeaderSize; 351 inBufferLength[0] = aac_frame_length - adtsHeaderSize; 352 353 inHeader->nOffset += adtsHeaderSize; 354 inHeader->nFilledLen -= adtsHeaderSize; 355 } else { 356 inBuffer[0] = inHeader->pBuffer + inHeader->nOffset; 357 inBufferLength[0] = inHeader->nFilledLen; 358 } 359 360 // Fill and decode 361 INT_PCM *outBuffer = reinterpret_cast<INT_PCM *>(outHeader->pBuffer + outHeader->nOffset); 362 bytesValid[0] = inBufferLength[0]; 363 364 int flags = mInputDiscontinuity ? AACDEC_INTR : 0; 365 int prevSampleRate = mStreamInfo->sampleRate; 366 int prevNumChannels = mStreamInfo->numChannels; 367 368 AAC_DECODER_ERROR decoderErr = AAC_DEC_NOT_ENOUGH_BITS; 369 while (bytesValid[0] > 0 && decoderErr == AAC_DEC_NOT_ENOUGH_BITS) { 370 aacDecoder_Fill(mAACDecoder, 371 inBuffer, 372 inBufferLength, 373 bytesValid); 374 375 decoderErr = aacDecoder_DecodeFrame(mAACDecoder, 376 outBuffer, 377 outHeader->nAllocLen, 378 flags); 379 380 } 381 mInputDiscontinuity = false; 382 383 /* 384 * AAC+/eAAC+ streams can be signalled in two ways: either explicitly 385 * or implicitly, according to MPEG4 spec. AAC+/eAAC+ is a dual 386 * rate system and the sampling rate in the final output is actually 387 * doubled compared with the core AAC decoder sampling rate. 388 * 389 * Explicit signalling is done by explicitly defining SBR audio object 390 * type in the bitstream. Implicit signalling is done by embedding 391 * SBR content in AAC extension payload specific to SBR, and hence 392 * requires an AAC decoder to perform pre-checks on actual audio frames. 393 * 394 * Thus, we could not say for sure whether a stream is 395 * AAC+/eAAC+ until the first data frame is decoded. 396 */ 397 if (mInputBufferCount <= 2) { 398 if (mStreamInfo->sampleRate != prevSampleRate || 399 mStreamInfo->numChannels != prevNumChannels) { 400 // We're going to want to revisit this input buffer, but 401 // may have already advanced the offset. Undo that if 402 // necessary. 403 inHeader->nOffset -= adtsHeaderSize; 404 inHeader->nFilledLen += adtsHeaderSize; 405 406 notify(OMX_EventPortSettingsChanged, 1, 0, NULL); 407 mOutputPortSettingsChange = AWAITING_DISABLED; 408 return; 409 } 410 } 411 412 size_t numOutBytes = 413 mStreamInfo->frameSize * sizeof(int16_t) * mStreamInfo->numChannels; 414 415 if (decoderErr == AAC_DEC_OK) { 416 UINT inBufferUsedLength = inBufferLength[0] - bytesValid[0]; 417 inHeader->nFilledLen -= inBufferUsedLength; 418 inHeader->nOffset += inBufferUsedLength; 419 } else { 420 ALOGW("AAC decoder returned error %d, substituting silence", 421 decoderErr); 422 423 memset(outHeader->pBuffer + outHeader->nOffset, 0, numOutBytes); 424 425 // Discard input buffer. 426 inHeader->nFilledLen = 0; 427 428 // fall through 429 } 430 431 if (decoderErr == AAC_DEC_OK || mNumSamplesOutput > 0) { 432 // We'll only output data if we successfully decoded it or 433 // we've previously decoded valid data, in the latter case 434 // (decode failed) we'll output a silent frame. 435 if (mIsFirst) { 436 mIsFirst = false; 437 // the first decoded frame should be discarded to account for decoder delay 438 numOutBytes = 0; 439 } 440 441 outHeader->nFilledLen = numOutBytes; 442 outHeader->nFlags = 0; 443 444 outHeader->nTimeStamp = 445 mAnchorTimeUs 446 + (mNumSamplesOutput * 1000000ll) / mStreamInfo->sampleRate; 447 448 mNumSamplesOutput += mStreamInfo->frameSize; 449 450 outInfo->mOwnedByUs = false; 451 outQueue.erase(outQueue.begin()); 452 outInfo = NULL; 453 notifyFillBufferDone(outHeader); 454 outHeader = NULL; 455 } 456 457 if (inHeader->nFilledLen == 0) { 458 inInfo->mOwnedByUs = false; 459 inQueue.erase(inQueue.begin()); 460 inInfo = NULL; 461 notifyEmptyBufferDone(inHeader); 462 inHeader = NULL; 463 } 464 465 if (decoderErr == AAC_DEC_OK) { 466 ++mInputBufferCount; 467 } 468 } 469} 470 471void SoftAAC2::onPortFlushCompleted(OMX_U32 portIndex) { 472 if (portIndex == 0) { 473 // Make sure that the next buffer output does not still 474 // depend on fragments from the last one decoded. 475 mInputDiscontinuity = true; 476 mIsFirst = true; 477 } 478} 479 480void SoftAAC2::onPortEnableCompleted(OMX_U32 portIndex, bool enabled) { 481 if (portIndex != 1) { 482 return; 483 } 484 485 switch (mOutputPortSettingsChange) { 486 case NONE: 487 break; 488 489 case AWAITING_DISABLED: 490 { 491 CHECK(!enabled); 492 mOutputPortSettingsChange = AWAITING_ENABLED; 493 break; 494 } 495 496 default: 497 { 498 CHECK_EQ((int)mOutputPortSettingsChange, (int)AWAITING_ENABLED); 499 CHECK(enabled); 500 mOutputPortSettingsChange = NONE; 501 break; 502 } 503 } 504} 505 506} // namespace android 507 508android::SoftOMXComponent *createSoftOMXComponent( 509 const char *name, const OMX_CALLBACKTYPE *callbacks, 510 OMX_PTR appData, OMX_COMPONENTTYPE **component) { 511 return new android::SoftAAC2(name, callbacks, appData, component); 512} 513