SoftAAC2.cpp revision 51d7547944967d1157b7fe18e1fb8f7ee6810412
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 <cutils/properties.h> 23#include <media/stagefright/foundation/ADebug.h> 24#include <media/stagefright/foundation/hexdump.h> 25#include <media/stagefright/MediaErrors.h> 26 27#define FILEREAD_MAX_LAYERS 2 28 29namespace android { 30 31template<class T> 32static void InitOMXParams(T *params) { 33 params->nSize = sizeof(T); 34 params->nVersion.s.nVersionMajor = 1; 35 params->nVersion.s.nVersionMinor = 0; 36 params->nVersion.s.nRevision = 0; 37 params->nVersion.s.nStep = 0; 38} 39 40SoftAAC2::SoftAAC2( 41 const char *name, 42 const OMX_CALLBACKTYPE *callbacks, 43 OMX_PTR appData, 44 OMX_COMPONENTTYPE **component) 45 : SimpleSoftOMXComponent(name, callbacks, appData, component), 46 mAACDecoder(NULL), 47 mStreamInfo(NULL), 48 mIsADTS(false), 49 mInputBufferCount(0), 50 mSignalledError(false), 51 mAnchorTimeUs(0), 52 mNumSamplesOutput(0), 53 mOutputPortSettingsChange(NONE) { 54 initPorts(); 55 CHECK_EQ(initDecoder(), (status_t)OK); 56} 57 58SoftAAC2::~SoftAAC2() { 59 aacDecoder_Close(mAACDecoder); 60} 61 62void SoftAAC2::initPorts() { 63 OMX_PARAM_PORTDEFINITIONTYPE def; 64 InitOMXParams(&def); 65 66 def.nPortIndex = 0; 67 def.eDir = OMX_DirInput; 68 def.nBufferCountMin = kNumInputBuffers; 69 def.nBufferCountActual = def.nBufferCountMin; 70 def.nBufferSize = 8192; 71 def.bEnabled = OMX_TRUE; 72 def.bPopulated = OMX_FALSE; 73 def.eDomain = OMX_PortDomainAudio; 74 def.bBuffersContiguous = OMX_FALSE; 75 def.nBufferAlignment = 1; 76 77 def.format.audio.cMIMEType = const_cast<char *>("audio/aac"); 78 def.format.audio.pNativeRender = NULL; 79 def.format.audio.bFlagErrorConcealment = OMX_FALSE; 80 def.format.audio.eEncoding = OMX_AUDIO_CodingAAC; 81 82 addPort(def); 83 84 def.nPortIndex = 1; 85 def.eDir = OMX_DirOutput; 86 def.nBufferCountMin = kNumOutputBuffers; 87 def.nBufferCountActual = def.nBufferCountMin; 88 def.nBufferSize = 8192 * 2; 89 def.bEnabled = OMX_TRUE; 90 def.bPopulated = OMX_FALSE; 91 def.eDomain = OMX_PortDomainAudio; 92 def.bBuffersContiguous = OMX_FALSE; 93 def.nBufferAlignment = 2; 94 95 def.format.audio.cMIMEType = const_cast<char *>("audio/raw"); 96 def.format.audio.pNativeRender = NULL; 97 def.format.audio.bFlagErrorConcealment = OMX_FALSE; 98 def.format.audio.eEncoding = OMX_AUDIO_CodingPCM; 99 100 addPort(def); 101} 102 103status_t SoftAAC2::initDecoder() { 104 status_t status = UNKNOWN_ERROR; 105 mAACDecoder = aacDecoder_Open(TT_MP4_ADIF, /* num layers */ 1); 106 if (mAACDecoder != NULL) { 107 mStreamInfo = aacDecoder_GetStreamInfo(mAACDecoder); 108 if (mStreamInfo != NULL) { 109 status = OK; 110 } 111 } 112 mIsFirst = true; 113 return status; 114} 115 116OMX_ERRORTYPE SoftAAC2::internalGetParameter( 117 OMX_INDEXTYPE index, OMX_PTR params) { 118 switch (index) { 119 case OMX_IndexParamAudioAac: 120 { 121 OMX_AUDIO_PARAM_AACPROFILETYPE *aacParams = 122 (OMX_AUDIO_PARAM_AACPROFILETYPE *)params; 123 124 if (aacParams->nPortIndex != 0) { 125 return OMX_ErrorUndefined; 126 } 127 128 aacParams->nBitRate = 0; 129 aacParams->nAudioBandWidth = 0; 130 aacParams->nAACtools = 0; 131 aacParams->nAACERtools = 0; 132 aacParams->eAACProfile = OMX_AUDIO_AACObjectMain; 133 134 aacParams->eAACStreamFormat = 135 mIsADTS 136 ? OMX_AUDIO_AACStreamFormatMP4ADTS 137 : OMX_AUDIO_AACStreamFormatMP4FF; 138 139 aacParams->eChannelMode = OMX_AUDIO_ChannelModeStereo; 140 141 if (!isConfigured()) { 142 aacParams->nChannels = 1; 143 aacParams->nSampleRate = 44100; 144 aacParams->nFrameLength = 0; 145 } else { 146 aacParams->nChannels = mStreamInfo->numChannels; 147 aacParams->nSampleRate = mStreamInfo->sampleRate; 148 aacParams->nFrameLength = mStreamInfo->frameSize; 149 } 150 151 return OMX_ErrorNone; 152 } 153 154 case OMX_IndexParamAudioPcm: 155 { 156 OMX_AUDIO_PARAM_PCMMODETYPE *pcmParams = 157 (OMX_AUDIO_PARAM_PCMMODETYPE *)params; 158 159 if (pcmParams->nPortIndex != 1) { 160 return OMX_ErrorUndefined; 161 } 162 163 pcmParams->eNumData = OMX_NumericalDataSigned; 164 pcmParams->eEndian = OMX_EndianBig; 165 pcmParams->bInterleaved = OMX_TRUE; 166 pcmParams->nBitPerSample = 16; 167 pcmParams->ePCMMode = OMX_AUDIO_PCMModeLinear; 168 pcmParams->eChannelMapping[0] = OMX_AUDIO_ChannelLF; 169 pcmParams->eChannelMapping[1] = OMX_AUDIO_ChannelRF; 170 pcmParams->eChannelMapping[2] = OMX_AUDIO_ChannelCF; 171 pcmParams->eChannelMapping[3] = OMX_AUDIO_ChannelLFE; 172 pcmParams->eChannelMapping[4] = OMX_AUDIO_ChannelLS; 173 pcmParams->eChannelMapping[5] = OMX_AUDIO_ChannelRS; 174 175 if (!isConfigured()) { 176 pcmParams->nChannels = 1; 177 pcmParams->nSamplingRate = 44100; 178 } else { 179 pcmParams->nChannels = mStreamInfo->numChannels; 180 pcmParams->nSamplingRate = mStreamInfo->sampleRate; 181 } 182 183 return OMX_ErrorNone; 184 } 185 186 default: 187 return SimpleSoftOMXComponent::internalGetParameter(index, params); 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::maybeConfigureDownmix() const { 251 if (mStreamInfo->numChannels > 2) { 252 char value[PROPERTY_VALUE_MAX]; 253 if (!(property_get("media.aac_51_output_enabled", value, NULL) && 254 (!strcmp(value, "1") || !strcasecmp(value, "true")))) { 255 ALOGI("Downmixing multichannel AAC to stereo"); 256 aacDecoder_SetParam(mAACDecoder, AAC_PCM_OUTPUT_CHANNELS, 2); 257 mStreamInfo->numChannels = 2; 258 } 259 } 260} 261 262void SoftAAC2::onQueueFilled(OMX_U32 portIndex) { 263 if (mSignalledError || mOutputPortSettingsChange != NONE) { 264 return; 265 } 266 267 UCHAR* inBuffer[FILEREAD_MAX_LAYERS]; 268 UINT inBufferLength[FILEREAD_MAX_LAYERS] = {0}; 269 UINT bytesValid[FILEREAD_MAX_LAYERS] = {0}; 270 271 List<BufferInfo *> &inQueue = getPortQueue(0); 272 List<BufferInfo *> &outQueue = getPortQueue(1); 273 274 if (portIndex == 0 && mInputBufferCount == 0) { 275 ++mInputBufferCount; 276 BufferInfo *info = *inQueue.begin(); 277 OMX_BUFFERHEADERTYPE *header = info->mHeader; 278 279 inBuffer[0] = header->pBuffer + header->nOffset; 280 inBufferLength[0] = header->nFilledLen; 281 282 AAC_DECODER_ERROR decoderErr = 283 aacDecoder_ConfigRaw(mAACDecoder, 284 inBuffer, 285 inBufferLength); 286 287 if (decoderErr != AAC_DEC_OK) { 288 mSignalledError = true; 289 notify(OMX_EventError, OMX_ErrorUndefined, decoderErr, NULL); 290 return; 291 } 292 inQueue.erase(inQueue.begin()); 293 info->mOwnedByUs = false; 294 notifyEmptyBufferDone(header); 295 296 // Only send out port settings changed event if both sample rate 297 // and numChannels are valid. 298 if (mStreamInfo->sampleRate && mStreamInfo->numChannels) { 299 maybeConfigureDownmix(); 300 ALOGI("Initially configuring decoder: %d Hz, %d channels", 301 mStreamInfo->sampleRate, 302 mStreamInfo->numChannels); 303 304 notify(OMX_EventPortSettingsChanged, 1, 0, NULL); 305 mOutputPortSettingsChange = AWAITING_DISABLED; 306 } 307 308 return; 309 } 310 311 while (!inQueue.empty() && !outQueue.empty()) { 312 BufferInfo *inInfo = *inQueue.begin(); 313 OMX_BUFFERHEADERTYPE *inHeader = inInfo->mHeader; 314 315 BufferInfo *outInfo = *outQueue.begin(); 316 OMX_BUFFERHEADERTYPE *outHeader = outInfo->mHeader; 317 318 if (inHeader->nFlags & OMX_BUFFERFLAG_EOS) { 319 inQueue.erase(inQueue.begin()); 320 inInfo->mOwnedByUs = false; 321 notifyEmptyBufferDone(inHeader); 322 323 if (!mIsFirst) { 324 // flush out the decoder's delayed data by calling DecodeFrame 325 // one more time, with the AACDEC_FLUSH flag set 326 INT_PCM *outBuffer = 327 reinterpret_cast<INT_PCM *>( 328 outHeader->pBuffer + outHeader->nOffset); 329 330 AAC_DECODER_ERROR decoderErr = 331 aacDecoder_DecodeFrame(mAACDecoder, 332 outBuffer, 333 outHeader->nAllocLen, 334 AACDEC_FLUSH); 335 336 if (decoderErr != AAC_DEC_OK) { 337 mSignalledError = true; 338 339 notify(OMX_EventError, OMX_ErrorUndefined, decoderErr, 340 NULL); 341 342 return; 343 } 344 345 outHeader->nFilledLen = 346 mStreamInfo->frameSize 347 * sizeof(int16_t) 348 * mStreamInfo->numChannels; 349 } else { 350 // Since we never discarded frames from the start, we won't have 351 // to add any padding at the end either. 352 353 outHeader->nFilledLen = 0; 354 } 355 356 outHeader->nFlags = OMX_BUFFERFLAG_EOS; 357 358 outQueue.erase(outQueue.begin()); 359 outInfo->mOwnedByUs = false; 360 notifyFillBufferDone(outHeader); 361 return; 362 } 363 364 if (inHeader->nOffset == 0) { 365 mAnchorTimeUs = inHeader->nTimeStamp; 366 mNumSamplesOutput = 0; 367 } 368 369 size_t adtsHeaderSize = 0; 370 if (mIsADTS) { 371 // skip 30 bits, aac_frame_length follows. 372 // ssssssss ssssiiip ppffffPc ccohCCll llllllll lll????? 373 374 const uint8_t *adtsHeader = inHeader->pBuffer + inHeader->nOffset; 375 376 bool signalError = false; 377 if (inHeader->nFilledLen < 7) { 378 ALOGE("Audio data too short to contain even the ADTS header. " 379 "Got %ld bytes.", inHeader->nFilledLen); 380 hexdump(adtsHeader, inHeader->nFilledLen); 381 signalError = true; 382 } else { 383 bool protectionAbsent = (adtsHeader[1] & 1); 384 385 unsigned aac_frame_length = 386 ((adtsHeader[3] & 3) << 11) 387 | (adtsHeader[4] << 3) 388 | (adtsHeader[5] >> 5); 389 390 if (inHeader->nFilledLen < aac_frame_length) { 391 ALOGE("Not enough audio data for the complete frame. " 392 "Got %ld bytes, frame size according to the ADTS " 393 "header is %u bytes.", 394 inHeader->nFilledLen, aac_frame_length); 395 hexdump(adtsHeader, inHeader->nFilledLen); 396 signalError = true; 397 } else { 398 adtsHeaderSize = (protectionAbsent ? 7 : 9); 399 400 inBuffer[0] = (UCHAR *)adtsHeader + adtsHeaderSize; 401 inBufferLength[0] = aac_frame_length - adtsHeaderSize; 402 403 inHeader->nOffset += adtsHeaderSize; 404 inHeader->nFilledLen -= adtsHeaderSize; 405 } 406 } 407 408 if (signalError) { 409 mSignalledError = true; 410 411 notify(OMX_EventError, 412 OMX_ErrorStreamCorrupt, 413 ERROR_MALFORMED, 414 NULL); 415 416 return; 417 } 418 } else { 419 inBuffer[0] = inHeader->pBuffer + inHeader->nOffset; 420 inBufferLength[0] = inHeader->nFilledLen; 421 } 422 423 // Fill and decode 424 INT_PCM *outBuffer = reinterpret_cast<INT_PCM *>( 425 outHeader->pBuffer + outHeader->nOffset); 426 427 bytesValid[0] = inBufferLength[0]; 428 429 int prevSampleRate = mStreamInfo->sampleRate; 430 int prevNumChannels = mStreamInfo->numChannels; 431 432 AAC_DECODER_ERROR decoderErr = AAC_DEC_NOT_ENOUGH_BITS; 433 while (bytesValid[0] > 0 && decoderErr == AAC_DEC_NOT_ENOUGH_BITS) { 434 aacDecoder_Fill(mAACDecoder, 435 inBuffer, 436 inBufferLength, 437 bytesValid); 438 439 decoderErr = aacDecoder_DecodeFrame(mAACDecoder, 440 outBuffer, 441 outHeader->nAllocLen, 442 0 /* flags */); 443 444 if (decoderErr == AAC_DEC_NOT_ENOUGH_BITS) { 445 ALOGW("Not enough bits, bytesValid %d", bytesValid[0]); 446 } 447 } 448 449 /* 450 * AAC+/eAAC+ streams can be signalled in two ways: either explicitly 451 * or implicitly, according to MPEG4 spec. AAC+/eAAC+ is a dual 452 * rate system and the sampling rate in the final output is actually 453 * doubled compared with the core AAC decoder sampling rate. 454 * 455 * Explicit signalling is done by explicitly defining SBR audio object 456 * type in the bitstream. Implicit signalling is done by embedding 457 * SBR content in AAC extension payload specific to SBR, and hence 458 * requires an AAC decoder to perform pre-checks on actual audio frames. 459 * 460 * Thus, we could not say for sure whether a stream is 461 * AAC+/eAAC+ until the first data frame is decoded. 462 */ 463 if (mInputBufferCount <= 2) { 464 if (mStreamInfo->sampleRate != prevSampleRate || 465 mStreamInfo->numChannels != prevNumChannels) { 466 maybeConfigureDownmix(); 467 ALOGI("Reconfiguring decoder: %d Hz, %d channels", 468 mStreamInfo->sampleRate, 469 mStreamInfo->numChannels); 470 471 // We're going to want to revisit this input buffer, but 472 // may have already advanced the offset. Undo that if 473 // necessary. 474 inHeader->nOffset -= adtsHeaderSize; 475 inHeader->nFilledLen += adtsHeaderSize; 476 477 notify(OMX_EventPortSettingsChanged, 1, 0, NULL); 478 mOutputPortSettingsChange = AWAITING_DISABLED; 479 return; 480 } 481 } else if (!mStreamInfo->sampleRate || !mStreamInfo->numChannels) { 482 ALOGW("Invalid AAC stream"); 483 mSignalledError = true; 484 notify(OMX_EventError, OMX_ErrorUndefined, decoderErr, NULL); 485 return; 486 } 487 488 size_t numOutBytes = 489 mStreamInfo->frameSize * sizeof(int16_t) * mStreamInfo->numChannels; 490 491 if (decoderErr == AAC_DEC_OK) { 492 UINT inBufferUsedLength = inBufferLength[0] - bytesValid[0]; 493 inHeader->nFilledLen -= inBufferUsedLength; 494 inHeader->nOffset += inBufferUsedLength; 495 } else { 496 ALOGW("AAC decoder returned error %d, substituting silence", 497 decoderErr); 498 499 memset(outHeader->pBuffer + outHeader->nOffset, 0, numOutBytes); 500 501 // Discard input buffer. 502 inHeader->nFilledLen = 0; 503 504 aacDecoder_SetParam(mAACDecoder, AAC_TPDEC_CLEAR_BUFFER, 1); 505 506 // fall through 507 } 508 509 if (decoderErr == AAC_DEC_OK || mNumSamplesOutput > 0) { 510 // We'll only output data if we successfully decoded it or 511 // we've previously decoded valid data, in the latter case 512 // (decode failed) we'll output a silent frame. 513 if (mIsFirst) { 514 mIsFirst = false; 515 // the first decoded frame should be discarded to account 516 // for decoder delay 517 numOutBytes = 0; 518 } 519 520 outHeader->nFilledLen = numOutBytes; 521 outHeader->nFlags = 0; 522 523 outHeader->nTimeStamp = 524 mAnchorTimeUs 525 + (mNumSamplesOutput * 1000000ll) / mStreamInfo->sampleRate; 526 527 mNumSamplesOutput += mStreamInfo->frameSize; 528 529 outInfo->mOwnedByUs = false; 530 outQueue.erase(outQueue.begin()); 531 outInfo = NULL; 532 notifyFillBufferDone(outHeader); 533 outHeader = NULL; 534 } 535 536 if (inHeader->nFilledLen == 0) { 537 inInfo->mOwnedByUs = false; 538 inQueue.erase(inQueue.begin()); 539 inInfo = NULL; 540 notifyEmptyBufferDone(inHeader); 541 inHeader = NULL; 542 } 543 544 if (decoderErr == AAC_DEC_OK) { 545 ++mInputBufferCount; 546 } 547 } 548} 549 550void SoftAAC2::onPortFlushCompleted(OMX_U32 portIndex) { 551 if (portIndex == 0) { 552 // Make sure that the next buffer output does not still 553 // depend on fragments from the last one decoded. 554 aacDecoder_SetParam(mAACDecoder, AAC_TPDEC_CLEAR_BUFFER, 1); 555 mIsFirst = true; 556 } 557} 558 559void SoftAAC2::onPortEnableCompleted(OMX_U32 portIndex, bool enabled) { 560 if (portIndex != 1) { 561 return; 562 } 563 564 switch (mOutputPortSettingsChange) { 565 case NONE: 566 break; 567 568 case AWAITING_DISABLED: 569 { 570 CHECK(!enabled); 571 mOutputPortSettingsChange = AWAITING_ENABLED; 572 break; 573 } 574 575 default: 576 { 577 CHECK_EQ((int)mOutputPortSettingsChange, (int)AWAITING_ENABLED); 578 CHECK(enabled); 579 mOutputPortSettingsChange = NONE; 580 break; 581 } 582 } 583} 584 585} // namespace android 586 587android::SoftOMXComponent *createSoftOMXComponent( 588 const char *name, const OMX_CALLBACKTYPE *callbacks, 589 OMX_PTR appData, OMX_COMPONENTTYPE **component) { 590 return new android::SoftAAC2(name, callbacks, appData, component); 591} 592