1
0

AVIAACDecoder.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include "AVIAACDecoder.h"
  2. #include <math.h>
  3. #include "../nsutil/pcm.h"
  4. int AVIDecoder::CreateAudioDecoder(const nsavi::AVIH *avi_header,
  5. const nsavi::STRH *stream_header, const nsavi::STRF *stream_format, const nsavi::STRD *stream_data,
  6. unsigned int preferred_bits, unsigned int max_channels, bool floating_point,
  7. ifc_aviaudiodecoder **decoder)
  8. {
  9. nsavi::audio_format *waveformat = (nsavi::audio_format *)stream_format;
  10. if (waveformat->format == nsavi::audio_format_aac)
  11. {
  12. AVIAACDecoder *aac_decoder = AVIAACDecoder::Create(waveformat, preferred_bits, max_channels, floating_point);
  13. if (aac_decoder)
  14. {
  15. *decoder = aac_decoder;
  16. return CREATEDECODER_SUCCESS;
  17. }
  18. return CREATEDECODER_SUCCESS;
  19. }
  20. return CREATEDECODER_NOT_MINE;
  21. }
  22. #define CBCLASS AVIDecoder
  23. START_DISPATCH;
  24. CB(CREATE_AUDIO_DECODER, CreateAudioDecoder)
  25. END_DISPATCH;
  26. #undef CBCLASS
  27. AVIAACDecoder *AVIAACDecoder::Create(const nsavi::audio_format *waveformat, unsigned int preferred_bits, unsigned int max_channels, bool floating_point)
  28. {
  29. if (!floating_point)
  30. {
  31. if (preferred_bits >= 24)
  32. preferred_bits=24;
  33. else
  34. preferred_bits=16;
  35. }
  36. /*if (!max_channels)
  37. max_channels = 8;*/
  38. if (waveformat->extra_size_bytes)
  39. {
  40. AVIAACDecoder * decoder = new AVIAACDecoder(preferred_bits, floating_point);
  41. if (decoder && SUCCEEDED(decoder->decoder.Open((const unsigned char *)(waveformat + 1), waveformat->extra_size_bytes))) {
  42. return decoder;
  43. }
  44. delete decoder;
  45. }
  46. return 0;
  47. }
  48. AVIAACDecoder::AVIAACDecoder(unsigned int bps, bool floating_point)
  49. : bps(bps), floating_point(floating_point)
  50. {
  51. }
  52. int AVIAACDecoder::OutputFrameSize(size_t *frame_size)
  53. {
  54. size_t local_frame_size;
  55. if (FAILED(decoder.OutputBlockSizeSamples(&local_frame_size))) {
  56. return AVI_FAILURE;
  57. }
  58. *frame_size = local_frame_size;
  59. return AVI_SUCCESS;
  60. }
  61. int AVIAACDecoder::GetOutputProperties(unsigned int *sampleRate, unsigned int *channels, unsigned int *bitsPerSample, bool *isFloat)
  62. {
  63. uint32_t local_sample_rate, local_channels;
  64. HRESULT hr = decoder.GetOutputProperties(&local_sample_rate, &local_channels);
  65. if (FAILED(hr)) {
  66. return AVI_FAILURE;
  67. }
  68. *sampleRate = local_sample_rate;
  69. *channels = local_channels;
  70. *bitsPerSample = bps;
  71. *isFloat = floating_point;
  72. return AVI_SUCCESS;
  73. }
  74. int AVIAACDecoder::DecodeChunk(uint16_t type, void **inputBuffer, size_t *inputBufferBytes, void *outputBuffer, size_t *outputBufferBytes)
  75. {
  76. if (SUCCEEDED(decoder.Feed(*inputBuffer, *inputBufferBytes))
  77. && SUCCEEDED(decoder.Decode(outputBuffer, outputBufferBytes, bps, false, 1.0))) {
  78. *inputBufferBytes = 0;
  79. return AVI_SUCCESS;
  80. }
  81. return AVI_FAILURE;
  82. }
  83. void AVIAACDecoder::Flush()
  84. {
  85. decoder.Flush();
  86. }
  87. void AVIAACDecoder::Close()
  88. {
  89. delete this;
  90. }
  91. #define CBCLASS AVIAACDecoder
  92. START_DISPATCH;
  93. CB(OUTPUT_FRAME_SIZE, OutputFrameSize)
  94. CB(GET_OUTPUT_PROPERTIES, GetOutputProperties)
  95. CB(DECODE_CHUNK, DecodeChunk)
  96. VCB(FLUSH, Flush)
  97. VCB(CLOSE, Close)
  98. END_DISPATCH;
  99. #undef CBCLASS