VideoEditorAudioDecoder.cpp revision f80b313837cca881971bb0afebf23a0381fb99a0
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************************************************************************* 18* @file VideoEditorAudioDecoder.cpp 19* @brief StageFright shell Audio Decoder 20************************************************************************* 21*/ 22 23#define LOG_NDEBUG 1 24#define LOG_TAG "VIDEOEDITOR_AUDIODECODER" 25 26#include "M4OSA_Debug.h" 27#include "VideoEditorAudioDecoder.h" 28#include "VideoEditorUtils.h" 29#include "M4MCS_InternalTypes.h" 30 31#include "utils/Log.h" 32#include "utils/Vector.h" 33#include <media/stagefright/MediaSource.h> 34#include <media/stagefright/MediaDebug.h> 35#include <media/stagefright/MediaDefs.h> 36#include <media/stagefright/MetaData.h> 37#include <media/stagefright/OMXClient.h> 38#include <media/stagefright/OMXCodec.h> 39 40/******************** 41 * DEFINITIONS * 42 ********************/ 43// Version 44#define VIDEOEDITOR_AUDIO_DECODER_VERSION_MAJOR 1 45#define VIDEOEDITOR_AUDIO_DECODER_VERSION_MINOR 0 46#define VIDEOEDITOR_AUDIO_DECODER_VERSION_REV 0 47 48// Force using software decoder as engine does not support prefetch 49#define VIDEOEDITOR_FORCECODEC kSoftwareCodecsOnly 50 51namespace android { 52 53struct VideoEditorAudioDecoderSource : public MediaSource { 54 public: 55 static sp<VideoEditorAudioDecoderSource> Create( 56 const sp<MetaData>& format, void *decoderShellContext); 57 virtual status_t start(MetaData *params = NULL); 58 virtual status_t stop(); 59 virtual sp<MetaData> getFormat(); 60 virtual status_t read(MediaBuffer **buffer, 61 const ReadOptions *options = NULL); 62 virtual void storeBuffer(MediaBuffer *buffer); 63 64 protected: 65 virtual ~VideoEditorAudioDecoderSource(); 66 67 private: 68 enum State { 69 CREATED, 70 STARTED, 71 ERROR 72 }; 73 VideoEditorAudioDecoderSource(const sp<MetaData>& format, 74 void *decoderShellContext); 75 sp<MetaData> mFormat; 76 Vector<MediaBuffer*> mBuffers; 77 Mutex mLock; // protects mBuffers 78 bool mIsEOS; 79 State mState; 80 void* mDecShellContext; 81 // Don't call me. 82 VideoEditorAudioDecoderSource(const VideoEditorAudioDecoderSource&); 83 VideoEditorAudioDecoderSource& operator=( 84 const VideoEditorAudioDecoderSource &); 85}; 86 87/** 88 ****************************************************************************** 89 * structure VideoEditorAudioDecoder_Context 90 * @brief This structure defines the context of the StageFright audio decoder 91 * shell 92 ****************************************************************************** 93*/ 94 95typedef struct { 96 M4AD_Type mDecoderType; 97 M4_AudioStreamHandler* mAudioStreamHandler; 98 sp<VideoEditorAudioDecoderSource> mDecoderSource; 99 OMXClient mClient; 100 sp<MediaSource> mDecoder; 101 int32_t mNbOutputChannels; 102 uint32_t mNbInputFrames; 103 uint32_t mNbOutputFrames; 104 M4READER_DataInterface *m_pReader; 105 M4_AccessUnit* m_pNextAccessUnitToDecode; 106 M4OSA_ERR readerErrCode; 107 int32_t timeStampMs; 108 109} VideoEditorAudioDecoder_Context; 110 111sp<VideoEditorAudioDecoderSource> VideoEditorAudioDecoderSource::Create( 112 const sp<MetaData>& format, void *decoderShellContext) { 113 114 sp<VideoEditorAudioDecoderSource> aSource = 115 new VideoEditorAudioDecoderSource(format, decoderShellContext); 116 117 return aSource; 118} 119 120VideoEditorAudioDecoderSource::VideoEditorAudioDecoderSource( 121 const sp<MetaData>& format, void* decoderShellContext): 122 mFormat(format), 123 mIsEOS(false), 124 mState(CREATED), 125 mDecShellContext(decoderShellContext) { 126} 127 128VideoEditorAudioDecoderSource::~VideoEditorAudioDecoderSource() { 129 130 if( STARTED == mState ) { 131 stop(); 132 } 133} 134 135status_t VideoEditorAudioDecoderSource::start(MetaData *meta) { 136 status_t err = OK; 137 138 if( CREATED != mState ) { 139 LOGV("VideoEditorAudioDecoderSource::start: invalid state %d", mState); 140 return UNKNOWN_ERROR; 141 } 142 143 mState = STARTED; 144 145cleanUp: 146 LOGV("VideoEditorAudioDecoderSource::start END (0x%x)", err); 147 return err; 148} 149 150status_t VideoEditorAudioDecoderSource::stop() { 151 Mutex::Autolock autolock(mLock); 152 status_t err = OK; 153 154 LOGV("VideoEditorAudioDecoderSource::stop begin"); 155 156 if( STARTED != mState ) { 157 LOGV("VideoEditorAudioDecoderSource::stop: invalid state %d", mState); 158 return UNKNOWN_ERROR; 159 } 160 161 if (!mBuffers.empty()) { 162 int n = mBuffers.size(); 163 for (int i = 0; i < n; i++) { 164 mBuffers.itemAt(i)->release(); 165 } 166 LOGW("VideoEditorAudioDecoderSource::stop : %d buffer remained", n); 167 mBuffers.clear(); 168 } 169 170 mState = CREATED; 171 172 LOGV("VideoEditorAudioDecoderSource::stop END (0x%x)", err); 173 return err; 174} 175 176sp<MetaData> VideoEditorAudioDecoderSource::getFormat() { 177 178 LOGV("VideoEditorAudioDecoderSource::getFormat"); 179 return mFormat; 180} 181 182static MediaBuffer* readBufferFromReader( 183 VideoEditorAudioDecoder_Context* pDecContext) { 184 M4OSA_ERR lerr = M4NO_ERROR; 185 M4_AccessUnit* pAccessUnit = pDecContext->m_pNextAccessUnitToDecode; 186 187 // Get next AU from reader. 188 lerr = pDecContext->m_pReader->m_pFctGetNextAu( 189 pDecContext->m_pReader->m_readerContext, 190 (M4_StreamHandler*)pDecContext->mAudioStreamHandler, 191 pAccessUnit); 192 193 if (lerr == M4WAR_NO_MORE_AU) { 194 LOGV("readBufferFromReader : EOS"); 195 return NULL; 196 } 197 198 pDecContext->timeStampMs = pAccessUnit->m_CTS; 199 200 MediaBuffer* newBuffer = new MediaBuffer((size_t)pAccessUnit->m_size); 201 memcpy((void *)((M4OSA_Int8*)newBuffer->data() + newBuffer->range_offset()), 202 (void *)pAccessUnit->m_dataAddress, pAccessUnit->m_size); 203 newBuffer->meta_data()->setInt64(kKeyTime, (pAccessUnit->m_CTS * 1000LL)); 204 return newBuffer; 205} 206 207status_t VideoEditorAudioDecoderSource::read(MediaBuffer **buffer, 208 const ReadOptions *options) { 209 Mutex::Autolock autolock(mLock); 210 MediaSource::ReadOptions readOptions; 211 212 VideoEditorAudioDecoder_Context* pDecContext = 213 (VideoEditorAudioDecoder_Context *)mDecShellContext; 214 215 if ( STARTED != mState ) { 216 LOGV("VideoEditorAudioDecoderSource::read invalid state %d", mState); 217 return UNKNOWN_ERROR; 218 } 219 220 // Get a buffer from the reader if we don't have any 221 if(mBuffers.empty()) { 222 MediaBuffer* newBuffer = readBufferFromReader(pDecContext); 223 if (!newBuffer) { 224 *buffer = NULL; 225 pDecContext->readerErrCode = M4WAR_NO_MORE_AU; 226 return ERROR_END_OF_STREAM; 227 } 228 mBuffers.push(newBuffer); 229 } 230 *buffer = mBuffers.itemAt(0); 231 mBuffers.removeAt(0); 232 233 return OK; 234} 235 236void VideoEditorAudioDecoderSource::storeBuffer(MediaBuffer *buffer) { 237 Mutex::Autolock autolock(mLock); 238 VideoEditorAudioDecoder_Context* pDecContext = 239 (VideoEditorAudioDecoder_Context *)mDecShellContext; 240 241 LOGV("VideoEditorAudioDecoderSource::storeBuffer begin"); 242 243 // If the user didn't give us a buffer, get it from the reader. 244 if(buffer == NULL) { 245 MediaBuffer* newBuffer = readBufferFromReader(pDecContext); 246 if (!newBuffer) { 247 pDecContext->readerErrCode = M4WAR_NO_MORE_AU; 248 return; 249 } 250 buffer = newBuffer; 251 } 252 253 mBuffers.push(buffer); 254 LOGV("VideoEditorAudioDecoderSource::storeBuffer END"); 255} 256 257/******************** 258 * TOOLS * 259 ********************/ 260 261M4OSA_ERR VideoEditorAudioDecoder_getBits(M4OSA_Int8* pData, 262 M4OSA_UInt32 dataSize, M4OSA_UInt8 nbBits, M4OSA_Int32* pResult, 263 M4OSA_UInt32* pOffset) { 264 265 M4OSA_ERR err = M4NO_ERROR; 266 M4OSA_UInt32 startByte = 0; 267 M4OSA_UInt32 startBit = 0; 268 M4OSA_UInt32 endByte = 0; 269 M4OSA_UInt32 endBit = 0; 270 M4OSA_UInt32 currentByte = 0; 271 M4OSA_UInt32 result = 0; 272 M4OSA_UInt32 ui32Tmp = 0; 273 M4OSA_UInt32 ui32Mask = 0; 274 275 // Input parameters check 276 VIDEOEDITOR_CHECK(M4OSA_NULL != pData, M4ERR_PARAMETER); 277 VIDEOEDITOR_CHECK(M4OSA_NULL != pOffset, M4ERR_PARAMETER); 278 VIDEOEDITOR_CHECK(32 >= nbBits, M4ERR_PARAMETER); 279 VIDEOEDITOR_CHECK((*pOffset + nbBits) <= 8*dataSize, M4ERR_PARAMETER); 280 281 LOGV("VideoEditorAudioDecoder_getBits begin"); 282 283 startByte = (*pOffset) >> 3; 284 endByte = (*pOffset + nbBits) >> 3; 285 startBit = (*pOffset) % 8; 286 endBit = (*pOffset + nbBits) % 8; 287 currentByte = startByte; 288 289 // Extract the requested nunber of bits from memory 290 while( currentByte <= endByte) { 291 ui32Mask = 0x000000FF; 292 if( currentByte == startByte ) { 293 ui32Mask >>= startBit; 294 } 295 ui32Tmp = ui32Mask & ((M4OSA_UInt32)pData[currentByte]); 296 if( currentByte == endByte ) { 297 ui32Tmp >>= (8-endBit); 298 result <<= endBit; 299 } else { 300 result <<= 8; 301 } 302 result |= ui32Tmp; 303 currentByte++; 304 } 305 306 *pResult = result; 307 *pOffset += nbBits; 308 309cleanUp: 310 if( M4NO_ERROR == err ) { 311 LOGV("VideoEditorAudioDecoder_getBits no error"); 312 } else { 313 LOGV("VideoEditorAudioDecoder_getBits ERROR 0x%X", err); 314 } 315 LOGV("VideoEditorAudioDecoder_getBits end"); 316 return err; 317} 318 319 320#define FREQ_TABLE_SIZE 16 321const M4OSA_UInt32 AD_AAC_FREQ_TABLE[FREQ_TABLE_SIZE] = 322 {96000, 88200, 64000, 48000, 44100, 323 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350, 0, 0, 0}; 324 325 326M4OSA_ERR VideoEditorAudioDecoder_parse_AAC_DSI(M4OSA_Int8* pDSI, 327 M4OSA_UInt32 dsiSize, AAC_DEC_STREAM_PROPS* pProperties) { 328 329 M4OSA_ERR err = M4NO_ERROR; 330 M4OSA_UInt32 offset = 0; 331 M4OSA_Int32 result = 0; 332 333 LOGV("VideoEditorAudioDecoder_parse_AAC_DSI begin"); 334 335 // Input parameters check 336 VIDEOEDITOR_CHECK(M4OSA_NULL != pDSI, M4ERR_PARAMETER); 337 VIDEOEDITOR_CHECK(M4OSA_NULL != pProperties, M4ERR_PARAMETER); 338 339 // Get the object type 340 err = VideoEditorAudioDecoder_getBits(pDSI, dsiSize, 5, &result, &offset); 341 VIDEOEDITOR_CHECK(M4NO_ERROR == err, err); 342 switch( result ) { 343 case 2: 344 pProperties->aPSPresent = 0; 345 pProperties->aSBRPresent = 0; 346 break; 347 default: 348 LOGV("parse_AAC_DSI ERROR : object type %d is not supported", 349 result); 350 VIDEOEDITOR_CHECK(!"invalid AAC object type", M4ERR_BAD_OPTION_ID); 351 break; 352 } 353 pProperties->aAudioObjectType = (M4OSA_Int32)result; 354 355 // Get the frequency index 356 err = VideoEditorAudioDecoder_getBits(pDSI, dsiSize, 4, &result, &offset); 357 VIDEOEDITOR_CHECK(M4NO_ERROR == err, err); 358 VIDEOEDITOR_CHECK((0 <= result) && (FREQ_TABLE_SIZE > result), 359 M4ERR_PARAMETER); 360 pProperties->aSampFreq = AD_AAC_FREQ_TABLE[result]; 361 pProperties->aExtensionSampFreq = 0; 362 363 // Get the number of channels 364 err = VideoEditorAudioDecoder_getBits(pDSI, dsiSize, 4, &result, &offset); 365 VIDEOEDITOR_CHECK(M4NO_ERROR == err, err); 366 pProperties->aNumChan = (M4OSA_UInt32)result; 367 368 // Set the max PCM samples per channel 369 pProperties->aMaxPCMSamplesPerCh = (pProperties->aSBRPresent) ? 2048 : 1024; 370 371cleanUp: 372 if( M4NO_ERROR == err ) { 373 LOGV("VideoEditorAudioDecoder_parse_AAC_DSI no error"); 374 } else { 375 LOGV("VideoEditorAudioDecoder_parse_AAC_DSI ERROR 0x%X", err); 376 } 377 LOGV("VideoEditorAudioDecoder_parse_AAC_DSI end"); 378 return err; 379} 380 381/******************** 382 * ENGINE INTERFACE * 383 ********************/ 384 385M4OSA_ERR VideoEditorAudioDecoder_destroy(M4AD_Context pContext) { 386 M4OSA_ERR err = M4NO_ERROR; 387 VideoEditorAudioDecoder_Context* pDecoderContext = M4OSA_NULL; 388 389 LOGV("VideoEditorAudioDecoder_destroy begin"); 390 // Input parameters check 391 VIDEOEDITOR_CHECK(M4OSA_NULL != pContext, M4ERR_PARAMETER); 392 393 pDecoderContext = (VideoEditorAudioDecoder_Context*)pContext; 394 395 // Stop the graph 396 if( M4OSA_NULL != pDecoderContext->mDecoder.get() ) { 397 pDecoderContext->mDecoder->stop(); 398 } 399 400 // Destroy the graph 401 pDecoderContext->mDecoderSource.clear(); 402 pDecoderContext->mDecoder.clear(); 403 pDecoderContext->mClient.disconnect(); 404 405 SAFE_FREE(pDecoderContext); 406 pContext = M4OSA_NULL; 407 LOGV("VideoEditorAudioDecoder_destroy : DONE"); 408 409cleanUp: 410 if( M4NO_ERROR == err ) { 411 LOGV("VideoEditorAudioDecoder_destroy no error"); 412 } else { 413 LOGV("VideoEditorAudioDecoder_destroy ERROR 0x%X", err); 414 } 415 LOGV("VideoEditorAudioDecoder_destroy : end"); 416 return err; 417} 418 419M4OSA_ERR VideoEditorAudioDecoder_create(M4AD_Type decoderType, 420 M4AD_Context* pContext, M4_AudioStreamHandler* pStreamHandler, 421 void* pUserData) { 422 M4OSA_ERR err = M4NO_ERROR; 423 VideoEditorAudioDecoder_Context* pDecoderContext = M4OSA_NULL; 424 AAC_DEC_STREAM_PROPS aacProperties; 425 status_t result = OK; 426 sp<MetaData> decoderMetaData = NULL; 427 const char* mime = NULL; 428 uint32_t codecFlags = 0; 429 430 LOGV("VideoEditorAudioDecoder_create begin: decoderType %d", decoderType); 431 432 // Input parameters check 433 VIDEOEDITOR_CHECK(M4OSA_NULL != pContext, M4ERR_PARAMETER); 434 VIDEOEDITOR_CHECK(M4OSA_NULL != pStreamHandler, M4ERR_PARAMETER); 435 436 // Context allocation & initialization 437 SAFE_MALLOC(pDecoderContext, VideoEditorAudioDecoder_Context, 1, 438 "AudioDecoder"); 439 pDecoderContext->mDecoderType = decoderType; 440 pDecoderContext->mAudioStreamHandler = pStreamHandler; 441 442 pDecoderContext->mNbInputFrames = 0; 443 pDecoderContext->mNbOutputFrames = 0; 444 pDecoderContext->readerErrCode = M4NO_ERROR; 445 pDecoderContext->timeStampMs = -1; 446 447 LOGV("VideoEditorAudioDecoder_create : maxAUSize %d", 448 pDecoderContext->mAudioStreamHandler->m_basicProperties.m_maxAUSize); 449 450 // Create the meta data for the decoder 451 decoderMetaData = new MetaData; 452 switch( pDecoderContext->mDecoderType ) { 453 case M4AD_kTypeAMRNB: 454 // StageFright parameters 455 mime = MEDIA_MIMETYPE_AUDIO_AMR_NB; 456 // Engine parameters 457 pDecoderContext->mAudioStreamHandler->m_byteFrameLength = 160; 458 // Number of bytes per sample 459 pDecoderContext->mAudioStreamHandler->m_byteSampleSize = 2; 460 pDecoderContext->mAudioStreamHandler->m_samplingFrequency = 8000; 461 pDecoderContext->mAudioStreamHandler->m_nbChannels = 1; 462 break; 463 464 case M4AD_kTypeAMRWB: 465 // StageFright parameters 466 mime = MEDIA_MIMETYPE_AUDIO_AMR_WB; 467 468 pDecoderContext->mAudioStreamHandler->m_byteFrameLength = 160; 469 // Number of bytes per sample 470 pDecoderContext->mAudioStreamHandler->m_byteSampleSize = 2; 471 pDecoderContext->mAudioStreamHandler->m_samplingFrequency = 16000; 472 pDecoderContext->mAudioStreamHandler->m_nbChannels = 1; 473 break; 474 475 case M4AD_kTypeAAC: 476 // Reject ADTS & ADIF (or any incorrect type) 477 VIDEOEDITOR_CHECK(M4DA_StreamTypeAudioAac == 478 pDecoderContext->mAudioStreamHandler->\ 479 m_basicProperties.m_streamType,M4ERR_PARAMETER); 480 481 // StageFright parameters 482 mime = MEDIA_MIMETYPE_AUDIO_AAC; 483 484 decoderMetaData->setData(kKeyESDS, kTypeESDS, 485 pStreamHandler->m_basicProperties.m_pESDSInfo, 486 pStreamHandler->m_basicProperties.m_ESDSInfoSize); 487 488 // Engine parameters 489 // Retrieve sampling frequency and number of channels from the DSI 490 err = VideoEditorAudioDecoder_parse_AAC_DSI( 491 (M4OSA_Int8*)pStreamHandler->m_basicProperties.\ 492 m_pDecoderSpecificInfo, 493 pStreamHandler->m_basicProperties.m_decoderSpecificInfoSize, 494 &aacProperties); 495 496 VIDEOEDITOR_CHECK(M4NO_ERROR == err, err); 497 pDecoderContext->mAudioStreamHandler->m_byteFrameLength = 1024; 498 // Number of bytes per sample 499 pDecoderContext->mAudioStreamHandler->m_byteSampleSize = 2; 500 pDecoderContext->mAudioStreamHandler->m_samplingFrequency = 501 aacProperties.aSampFreq; 502 pDecoderContext->mAudioStreamHandler->m_nbChannels = 503 aacProperties.aNumChan; 504 505 // Copy the stream properties into userdata 506 if( M4OSA_NULL != pUserData ) { 507 memcpy((void *)pUserData, 508 (void *)&aacProperties, 509 sizeof(AAC_DEC_STREAM_PROPS)); 510 } 511 break; 512 513 case M4AD_kTypeMP3: 514 // StageFright parameters 515 mime = MEDIA_MIMETYPE_AUDIO_MPEG; 516 break; 517 518 default: 519 VIDEOEDITOR_CHECK(!"AudioDecoder_open : incorrect input format", 520 M4ERR_STATE); 521 break; 522 } 523 decoderMetaData->setCString(kKeyMIMEType, mime); 524 decoderMetaData->setInt32(kKeySampleRate, 525 (int32_t)pDecoderContext->mAudioStreamHandler->m_samplingFrequency); 526 decoderMetaData->setInt32(kKeyChannelCount, 527 pDecoderContext->mAudioStreamHandler->m_nbChannels); 528 decoderMetaData->setInt64(kKeyDuration, 529 (int64_t)pDecoderContext->mAudioStreamHandler->\ 530 m_basicProperties.m_duration); 531 532 // Create the decoder source 533 pDecoderContext->mDecoderSource = VideoEditorAudioDecoderSource::Create( 534 decoderMetaData, (void *)pDecoderContext); 535 VIDEOEDITOR_CHECK(NULL != pDecoderContext->mDecoderSource.get(), 536 M4ERR_STATE); 537 538 // Connect to the OMX client 539 result = pDecoderContext->mClient.connect(); 540 VIDEOEDITOR_CHECK(OK == result, M4ERR_STATE); 541 542 // Create the OMX codec 543#ifdef VIDEOEDITOR_FORCECODEC 544 codecFlags |= OMXCodec::VIDEOEDITOR_FORCECODEC; 545#endif /* VIDEOEDITOR_FORCECODEC */ 546 547 pDecoderContext->mDecoder = OMXCodec::Create(pDecoderContext->\ 548 mClient.interface(), 549 decoderMetaData, false, pDecoderContext->mDecoderSource, NULL, 550 codecFlags); 551 VIDEOEDITOR_CHECK(NULL != pDecoderContext->mDecoder.get(), M4ERR_STATE); 552 553 // Get the output channels, the decoder might overwrite the input metadata 554 pDecoderContext->mDecoder->getFormat()->findInt32(kKeyChannelCount, 555 &pDecoderContext->mNbOutputChannels); 556 LOGV("VideoEditorAudioDecoder_create : output chan %d", 557 pDecoderContext->mNbOutputChannels); 558 559 // Start the decoder 560 result = pDecoderContext->mDecoder->start(); 561 VIDEOEDITOR_CHECK(OK == result, M4ERR_STATE); 562 563 *pContext = pDecoderContext; 564 LOGV("VideoEditorAudioDecoder_create : DONE"); 565 566cleanUp: 567 if( M4NO_ERROR == err ) { 568 LOGV("VideoEditorAudioDecoder_create no error"); 569 } else { 570 VideoEditorAudioDecoder_destroy(pDecoderContext); 571 *pContext = M4OSA_NULL; 572 LOGV("VideoEditorAudioDecoder_create ERROR 0x%X", err); 573 } 574 return err; 575} 576 577M4OSA_ERR VideoEditorAudioDecoder_create_AAC(M4AD_Context* pContext, 578 M4_AudioStreamHandler* pStreamHandler, void* pUserData) { 579 580 return VideoEditorAudioDecoder_create( 581 M4AD_kTypeAAC, pContext, pStreamHandler,pUserData); 582} 583 584 585M4OSA_ERR VideoEditorAudioDecoder_create_AMRNB(M4AD_Context* pContext, 586 M4_AudioStreamHandler* pStreamHandler, void* pUserData) { 587 588 return VideoEditorAudioDecoder_create( 589 M4AD_kTypeAMRNB, pContext, pStreamHandler, pUserData); 590} 591 592 593M4OSA_ERR VideoEditorAudioDecoder_create_AMRWB(M4AD_Context* pContext, 594 M4_AudioStreamHandler* pStreamHandler, void* pUserData) { 595 596 return VideoEditorAudioDecoder_create( 597 M4AD_kTypeAMRWB, pContext, pStreamHandler, pUserData); 598} 599 600 601M4OSA_ERR VideoEditorAudioDecoder_create_MP3(M4AD_Context* pContext, 602 M4_AudioStreamHandler* pStreamHandler, void* pUserData) { 603 604 return VideoEditorAudioDecoder_create( 605 M4AD_kTypeMP3, pContext, pStreamHandler, pUserData); 606} 607 608M4OSA_ERR VideoEditorAudioDecoder_processInputBuffer( 609 M4AD_Context pContext, M4AD_Buffer* pInputBuffer) { 610 M4OSA_ERR err = M4NO_ERROR; 611 VideoEditorAudioDecoder_Context* pDecoderContext = M4OSA_NULL; 612 MediaBuffer* buffer = NULL; 613 614 LOGV("VideoEditorAudioDecoder_processInputBuffer begin"); 615 // Input parameters check 616 VIDEOEDITOR_CHECK(M4OSA_NULL != pContext, M4ERR_PARAMETER); 617 618 619 pDecoderContext = (VideoEditorAudioDecoder_Context*)pContext; 620 621 if( M4OSA_NULL != pInputBuffer ) { 622 buffer = new MediaBuffer((size_t)pInputBuffer->m_bufferSize); 623 memcpy((void *)((M4OSA_Int8*)buffer->data() + buffer->range_offset()), 624 (void *)pInputBuffer->m_dataAddress, pInputBuffer->m_bufferSize); 625 buffer->meta_data()->setInt64(kKeyTime, pInputBuffer->m_timeStampUs); 626 } 627 pDecoderContext->mDecoderSource->storeBuffer(buffer); 628 629cleanUp: 630 if( M4NO_ERROR == err ) { 631 LOGV("VideoEditorAudioDecoder_processInputBuffer no error"); 632 } else { 633 LOGV("VideoEditorAudioDecoder_processInputBuffer ERROR 0x%X", err); 634 } 635 LOGV("VideoEditorAudioDecoder_processInputBuffer end"); 636 return err; 637} 638 639M4OSA_ERR VideoEditorAudioDecoder_processOutputBuffer(M4AD_Context pContext, 640 MediaBuffer* buffer, M4AD_Buffer* pOuputBuffer) { 641 M4OSA_ERR err = M4NO_ERROR; 642 VideoEditorAudioDecoder_Context* pDecoderContext = M4OSA_NULL; 643 int32_t i32Tmp = 0; 644 int64_t i64Tmp = 0; 645 status_t result = OK; 646 647 LOGV("VideoEditorAudioDecoder_processOutputBuffer begin"); 648 // Input parameters check 649 VIDEOEDITOR_CHECK(M4OSA_NULL != pContext, M4ERR_PARAMETER); 650 VIDEOEDITOR_CHECK(M4OSA_NULL != buffer, M4ERR_PARAMETER); 651 VIDEOEDITOR_CHECK(M4OSA_NULL != pOuputBuffer, M4ERR_PARAMETER); 652 653 pDecoderContext = (VideoEditorAudioDecoder_Context*)pContext; 654 655 // Process the returned data 656 if( 0 == buffer->range_length() ) { 657 // Decoder has no data yet, nothing unusual 658 goto cleanUp; 659 } 660 661 pDecoderContext->mNbOutputFrames++; 662 663 if( pDecoderContext->mAudioStreamHandler->m_nbChannels == 664 (M4OSA_UInt32)pDecoderContext->mNbOutputChannels ) { 665 // Just copy the PCMs 666 pOuputBuffer->m_bufferSize = (M4OSA_UInt32)buffer->range_length(); 667 memcpy((void *)pOuputBuffer->m_dataAddress, 668 (void *)(((M4OSA_MemAddr8)buffer->data())+buffer->range_offset()), 669 buffer->range_length()); 670 } else if( pDecoderContext->mAudioStreamHandler->m_nbChannels < 671 (M4OSA_UInt32)pDecoderContext->mNbOutputChannels ) { 672 // The decoder forces stereo output, downsample 673 pOuputBuffer->m_bufferSize = (M4OSA_UInt32)(buffer->range_length()/2); 674 M4OSA_Int16* pDataIn = ((M4OSA_Int16*)buffer->data()) + 675 buffer->range_offset(); 676 M4OSA_Int16* pDataOut = (M4OSA_Int16*)pOuputBuffer->m_dataAddress; 677 M4OSA_Int16* pDataEnd = pDataIn + \ 678 (buffer->range_length()/sizeof(M4OSA_Int16)); 679 while( pDataIn < pDataEnd ) { 680 *pDataOut = *pDataIn; 681 pDataIn+=2; 682 pDataOut++; 683 } 684 } else { 685 // The decoder forces mono output, not supported 686 VIDEOEDITOR_CHECK(M4OSA_FALSE, M4ERR_PARAMETER); 687 } 688 689cleanUp: 690 // Release the buffer 691 buffer->release(); 692 if( M4NO_ERROR == err ) { 693 LOGV("VideoEditorAudioDecoder_processOutputBuffer no error"); 694 } else { 695 pOuputBuffer->m_bufferSize = 0; 696 LOGV("VideoEditorAudioDecoder_processOutputBuffer ERROR 0x%X", err); 697 } 698 LOGV("VideoEditorAudioDecoder_processOutputBuffer end"); 699 return err; 700} 701 702M4OSA_ERR VideoEditorAudioDecoder_step(M4AD_Context pContext, 703 M4AD_Buffer* pInputBuffer, M4AD_Buffer* pOutputBuffer, 704 M4OSA_Bool bJump) { 705 M4OSA_ERR err = M4NO_ERROR; 706 VideoEditorAudioDecoder_Context* pDecoderContext = M4OSA_NULL; 707 status_t result = OK; 708 MediaBuffer* outputBuffer = NULL; 709 710 LOGV("VideoEditorAudioDecoder_step begin"); 711 // Input parameters check 712 VIDEOEDITOR_CHECK(M4OSA_NULL != pContext, M4ERR_PARAMETER); 713 714 pDecoderContext = (VideoEditorAudioDecoder_Context*)pContext; 715 pDecoderContext->mNbInputFrames++; 716 717 // Push the input buffer to the decoder source 718 err = VideoEditorAudioDecoder_processInputBuffer(pDecoderContext, 719 pInputBuffer); 720 VIDEOEDITOR_CHECK(M4NO_ERROR == err, err); 721 722 // Read 723 result = pDecoderContext->mDecoder->read(&outputBuffer, NULL); 724 if (INFO_FORMAT_CHANGED == result) { 725 LOGV("VideoEditorAudioDecoder_step: Audio decoder \ 726 returned INFO_FORMAT_CHANGED"); 727 CHECK(outputBuffer == NULL); 728 sp<MetaData> meta = pDecoderContext->mDecoder->getFormat(); 729 int32_t sampleRate, channelCount; 730 731 CHECK(meta->findInt32(kKeySampleRate, &sampleRate)); 732 CHECK(meta->findInt32(kKeyChannelCount, &channelCount)); 733 LOGV("VideoEditorAudioDecoder_step: samplingFreq = %d", sampleRate); 734 LOGV("VideoEditorAudioDecoder_step: channelCnt = %d", channelCount); 735 pDecoderContext->mAudioStreamHandler->m_samplingFrequency = 736 (uint32_t)sampleRate; 737 pDecoderContext->mAudioStreamHandler->m_nbChannels = 738 (uint32_t)channelCount; 739 pDecoderContext->mNbOutputChannels = channelCount; 740 741 return M4WAR_INFO_FORMAT_CHANGE; 742 } else if (ERROR_END_OF_STREAM == result) { 743 LOGV("VideoEditorAudioDecoder_step: Audio decoder \ 744 returned ERROR_END_OF_STREAM"); 745 pDecoderContext->readerErrCode = M4WAR_NO_MORE_AU; 746 return M4WAR_NO_MORE_AU; 747 } else if (OK != result) { 748 return M4ERR_STATE; 749 } 750 751 // Convert the PCM buffer 752 err = VideoEditorAudioDecoder_processOutputBuffer(pDecoderContext, 753 outputBuffer, pOutputBuffer); 754 VIDEOEDITOR_CHECK(M4NO_ERROR == err, err); 755 756cleanUp: 757 if( M4NO_ERROR == err ) { 758 LOGV("VideoEditorAudioDecoder_step no error"); 759 } else { 760 LOGV("VideoEditorAudioDecoder_step ERROR 0x%X", err); 761 } 762 LOGV("VideoEditorAudioDecoder_step end"); 763 return err; 764} 765 766M4OSA_ERR VideoEditorAudioDecoder_getVersion(M4_VersionInfo* pVersionInfo) { 767 M4OSA_ERR err = M4NO_ERROR; 768 769 LOGV("VideoEditorAudioDecoder_getVersion begin"); 770 // Input parameters check 771 VIDEOEDITOR_CHECK(M4OSA_NULL != pVersionInfo, M4ERR_PARAMETER); 772 773 pVersionInfo->m_major = VIDEOEDITOR_AUDIO_DECODER_VERSION_MAJOR; 774 pVersionInfo->m_minor = VIDEOEDITOR_AUDIO_DECODER_VERSION_MINOR; 775 pVersionInfo->m_revision = VIDEOEDITOR_AUDIO_DECODER_VERSION_REV; 776 pVersionInfo->m_structSize = sizeof(M4_VersionInfo); 777 778cleanUp: 779 if( M4NO_ERROR == err ) { 780 LOGV("VideoEditorAudioDecoder_getVersion no error"); 781 } else { 782 LOGV("VideoEditorAudioDecoder_getVersion ERROR 0x%X", err); 783 } 784 LOGV("VideoEditorAudioDecoder_getVersion end"); 785 return err; 786} 787 788M4OSA_ERR VideoEditorAudioDecoder_setOption(M4AD_Context pContext, 789 M4OSA_UInt32 optionID, M4OSA_DataOption optionValue) { 790 791 M4OSA_ERR err = M4NO_ERROR; 792 VideoEditorAudioDecoder_Context* pDecoderContext = M4OSA_NULL; 793 794 LOGV("VideoEditorAudioDecoder_setOption begin 0x%X", optionID); 795 // Input parameters check 796 VIDEOEDITOR_CHECK(M4OSA_NULL != pContext, M4ERR_PARAMETER); 797 798 pDecoderContext = (VideoEditorAudioDecoder_Context*)pContext; 799 800 switch( optionID ) { 801 case M4AD_kOptionID_UserParam: 802 LOGV("VideoEditorAudioDecodersetOption UserParam is not supported"); 803 err = M4ERR_NOT_IMPLEMENTED; 804 break; 805 806 case M4AD_kOptionID_3gpReaderInterface: 807 LOGV("VideoEditorAudioDecodersetOption 3gpReaderInterface"); 808 pDecoderContext->m_pReader = 809 (M4READER_DataInterface *)optionValue; 810 break; 811 812 case M4AD_kOptionID_AudioAU: 813 LOGV("VideoEditorAudioDecodersetOption AudioAU"); 814 pDecoderContext->m_pNextAccessUnitToDecode = 815 (M4_AccessUnit *)optionValue; 816 break; 817 818 default: 819 LOGV("VideoEditorAudioDecoder_setOption unsupported optionId 0x%X", 820 optionID); 821 VIDEOEDITOR_CHECK(M4OSA_FALSE, M4ERR_BAD_OPTION_ID); 822 break; 823 } 824 825cleanUp: 826 if( ((M4OSA_UInt32)M4NO_ERROR == err) || ((M4OSA_UInt32)M4ERR_NOT_IMPLEMENTED == err) ) { 827 LOGV("VideoEditorAudioDecoder_setOption error 0x%X", err); 828 } else { 829 LOGV("VideoEditorAudioDecoder_setOption ERROR 0x%X", err); 830 } 831 LOGV("VideoEditorAudioDecoder_setOption end"); 832 return err; 833} 834 835M4OSA_ERR VideoEditorAudioDecoder_getOption(M4AD_Context pContext, 836 M4OSA_UInt32 optionID, M4OSA_DataOption optionValue) { 837 838 M4OSA_ERR err = M4NO_ERROR; 839 VideoEditorAudioDecoder_Context* pDecoderContext = M4OSA_NULL; 840 841 LOGV("VideoEditorAudioDecoder_getOption begin: optionID 0x%X", optionID); 842 // Input parameters check 843 VIDEOEDITOR_CHECK(M4OSA_NULL != pContext, M4ERR_PARAMETER); 844 845 pDecoderContext = (VideoEditorAudioDecoder_Context*)pContext; 846 847 switch( optionID ) { 848 849 case M4AD_kOptionID_GetAudioAUErrCode: 850 *(uint32_t *)optionValue = pDecoderContext->readerErrCode; 851 break; 852 853 case M4AD_kOptionID_AudioNbChannels: 854 *(uint32_t *)optionValue = 855 pDecoderContext->mAudioStreamHandler->m_nbChannels; 856 break; 857 858 case M4AD_kOptionID_AudioSampFrequency: 859 *(uint32_t *)optionValue = 860 pDecoderContext->mAudioStreamHandler->m_samplingFrequency; 861 break; 862 863 case M4AD_kOptionID_AuCTS: 864 *(uint32_t *)optionValue = pDecoderContext->timeStampMs; 865 break; 866 867 default: 868 LOGV("VideoEditorAudioDecoder_getOption unsupported optionId 0x%X", 869 optionID); 870 VIDEOEDITOR_CHECK(M4OSA_FALSE, M4ERR_BAD_OPTION_ID); 871 break; 872 } 873 874cleanUp: 875 if( M4NO_ERROR == err ) { 876 LOGV("VideoEditorAudioDecoder_getOption no error"); 877 } else { 878 LOGV("VideoEditorAudioDecoder_getOption ERROR 0x%X", err); 879 } 880 LOGV("VideoEditorAudioDecoder_getOption end"); 881 return err; 882} 883 884M4OSA_ERR VideoEditorAudioDecoder_getInterface(M4AD_Type decoderType, 885 M4AD_Type* pDecoderType, M4AD_Interface** pDecoderInterface) { 886 887 M4OSA_ERR err = M4NO_ERROR; 888 889 // Input parameters check 890 VIDEOEDITOR_CHECK(M4OSA_NULL != pDecoderType, M4ERR_PARAMETER); 891 VIDEOEDITOR_CHECK(M4OSA_NULL != pDecoderInterface, M4ERR_PARAMETER); 892 893 LOGV("VideoEditorAudioDecoder_getInterface begin %d 0x%x 0x%x", 894 decoderType, pDecoderType, pDecoderInterface); 895 896 SAFE_MALLOC(*pDecoderInterface, M4AD_Interface, 1, 897 "VideoEditorAudioDecoder"); 898 899 *pDecoderType = decoderType; 900 901 switch( decoderType ) { 902 case M4AD_kTypeAMRNB: 903 (*pDecoderInterface)->m_pFctCreateAudioDec = 904 VideoEditorAudioDecoder_create_AMRNB; 905 break; 906 case M4AD_kTypeAMRWB: 907 (*pDecoderInterface)->m_pFctCreateAudioDec = 908 VideoEditorAudioDecoder_create_AMRWB; 909 break; 910 case M4AD_kTypeAAC: 911 (*pDecoderInterface)->m_pFctCreateAudioDec = 912 VideoEditorAudioDecoder_create_AAC; 913 break; 914 case M4AD_kTypeMP3: 915 (*pDecoderInterface)->m_pFctCreateAudioDec = 916 VideoEditorAudioDecoder_create_MP3; 917 break; 918 default: 919 LOGV("VEAD_getInterface ERROR: unsupported type %d", decoderType); 920 VIDEOEDITOR_CHECK(M4OSA_FALSE, M4ERR_PARAMETER); 921 break; 922 } 923 (*pDecoderInterface)->m_pFctDestroyAudioDec = 924 VideoEditorAudioDecoder_destroy; 925 (*pDecoderInterface)->m_pFctResetAudioDec = M4OSA_NULL; 926 (*pDecoderInterface)->m_pFctStartAudioDec = M4OSA_NULL; 927 (*pDecoderInterface)->m_pFctStepAudioDec = 928 VideoEditorAudioDecoder_step; 929 (*pDecoderInterface)->m_pFctGetVersionAudioDec = 930 VideoEditorAudioDecoder_getVersion; 931 (*pDecoderInterface)->m_pFctSetOptionAudioDec = 932 VideoEditorAudioDecoder_setOption; 933 (*pDecoderInterface)->m_pFctGetOptionAudioDec = 934 VideoEditorAudioDecoder_getOption; 935 936cleanUp: 937 if( M4NO_ERROR == err ) { 938 LOGV("VideoEditorAudioDecoder_getInterface no error"); 939 } else { 940 *pDecoderInterface = M4OSA_NULL; 941 LOGV("VideoEditorAudioDecoder_getInterface ERROR 0x%X", err); 942 } 943 LOGV("VideoEditorAudioDecoder_getInterface end"); 944 return err; 945} 946 947 948extern "C" { 949 950M4OSA_ERR VideoEditorAudioDecoder_getInterface_AAC(M4AD_Type* pDecoderType, 951 M4AD_Interface** pDecoderInterface) { 952 LOGV("TEST: AAC VideoEditorAudioDecoder_getInterface no error"); 953 return VideoEditorAudioDecoder_getInterface( 954 M4AD_kTypeAAC, pDecoderType, pDecoderInterface); 955} 956 957M4OSA_ERR VideoEditorAudioDecoder_getInterface_AMRNB(M4AD_Type* pDecoderType, 958 M4AD_Interface** pDecoderInterface) { 959 LOGV("TEST: AMR VideoEditorAudioDecoder_getInterface no error"); 960 return VideoEditorAudioDecoder_getInterface( 961 M4AD_kTypeAMRNB, pDecoderType, pDecoderInterface); 962} 963 964M4OSA_ERR VideoEditorAudioDecoder_getInterface_AMRWB(M4AD_Type* pDecoderType, 965 M4AD_Interface** pDecoderInterface) { 966 967 return VideoEditorAudioDecoder_getInterface( 968 M4AD_kTypeAMRWB, pDecoderType, pDecoderInterface); 969} 970 971M4OSA_ERR VideoEditorAudioDecoder_getInterface_MP3(M4AD_Type* pDecoderType, 972 M4AD_Interface** pDecoderInterface) { 973 974 return VideoEditorAudioDecoder_getInterface( 975 M4AD_kTypeMP3, pDecoderType, pDecoderInterface); 976} 977 978} // extern "C" 979 980} // namespace android 981