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