2
0

AACDecoder.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "AACDecoder.h"
  2. #include <assert.h>
  3. #include <stdlib.h> // for free, malloc
  4. #include <string.h>
  5. #include "e_tmp4audioobjecttype.h"
  6. #include "pvmp4audiodecoder_api.h"
  7. namespace bell {
  8. class AudioContainer;
  9. } // namespace bell
  10. using namespace bell;
  11. AACDecoder::AACDecoder() {
  12. aacDecoder =
  13. (tPVMP4AudioDecoderExternal*)malloc(sizeof(tPVMP4AudioDecoderExternal));
  14. int32_t pMemRequirement = PVMP4AudioDecoderGetMemRequirements();
  15. pMem = malloc(pMemRequirement);
  16. memset(aacDecoder, 0, sizeof(tPVMP4AudioDecoderExternal));
  17. memset(pMem, 0, pMemRequirement);
  18. // Initialize the decoder buffers
  19. outputBuffer.resize(4096);
  20. aacDecoder->pOutputBuffer_plus = &outputBuffer[2048];
  21. aacDecoder->pOutputBuffer = &outputBuffer[0];
  22. aacDecoder->inputBufferMaxLength = PVMP4AUDIODECODER_INBUFSIZE;
  23. // Settings
  24. aacDecoder->desiredChannels = 2;
  25. aacDecoder->outputFormat = OUTPUTFORMAT_16PCM_INTERLEAVED;
  26. aacDecoder->aacPlusEnabled = TRUE;
  27. // State
  28. aacDecoder->inputBufferCurrentLength = 0;
  29. aacDecoder->inputBufferUsedLength = 0;
  30. aacDecoder->remainderBits = 0;
  31. firstFrame = true;
  32. assert(PVMP4AudioDecoderInitLibrary(aacDecoder, pMem) == MP4AUDEC_SUCCESS);
  33. }
  34. AACDecoder::~AACDecoder() {
  35. free(pMem);
  36. free(aacDecoder);
  37. }
  38. int AACDecoder::getDecodedStreamType() {
  39. switch (aacDecoder->extendedAudioObjectType) {
  40. case MP4AUDIO_AAC_LC:
  41. case MP4AUDIO_LTP:
  42. return AAC;
  43. case MP4AUDIO_SBR:
  44. return AACPLUS;
  45. case MP4AUDIO_PS:
  46. return ENH_AACPLUS;
  47. default:
  48. return -1;
  49. }
  50. }
  51. bool AACDecoder::setup(uint32_t sampleRate, uint8_t channelCount,
  52. uint8_t bitDepth) {
  53. PVMP4AudioDecoderResetBuffer(pMem);
  54. assert(PVMP4AudioDecoderInitLibrary(aacDecoder, pMem) == MP4AUDEC_SUCCESS);
  55. firstFrame = true;
  56. return true;
  57. }
  58. bool AACDecoder::setup(AudioContainer* container) {
  59. PVMP4AudioDecoderResetBuffer(pMem);
  60. assert(PVMP4AudioDecoderInitLibrary(aacDecoder, pMem) == MP4AUDEC_SUCCESS);
  61. firstFrame = true;
  62. return true;
  63. }
  64. uint8_t* AACDecoder::decode(uint8_t* inData, uint32_t& inLen,
  65. uint32_t& outLen) {
  66. if (!inData || inLen == 0)
  67. return nullptr;
  68. aacDecoder->inputBufferCurrentLength = inLen;
  69. aacDecoder->inputBufferUsedLength = 0;
  70. aacDecoder->inputBufferMaxLength = inLen;
  71. aacDecoder->pInputBuffer = inData;
  72. aacDecoder->remainderBits = 0;
  73. aacDecoder->repositionFlag = true;
  74. int32_t status;
  75. status = PVMP4AudioDecodeFrame(aacDecoder, pMem);
  76. if (status != MP4AUDEC_SUCCESS) {
  77. outLen = 0;
  78. inLen = 0;
  79. return nullptr;
  80. } else {
  81. inLen -= aacDecoder->inputBufferUsedLength;
  82. }
  83. outLen = aacDecoder->frameLength * sizeof(int16_t);
  84. // Handle AAC+
  85. if (aacDecoder->aacPlusUpsamplingFactor == 2) {
  86. outLen *= 2;
  87. }
  88. outLen *= aacDecoder->desiredChannels;
  89. return (uint8_t*)&outputBuffer[0];
  90. }