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