FLVAACDecoder.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include "FLVAACDecoder.h"
  2. #include <math.h>
  3. #include "../nsutil/pcm.h"
  4. int FLVDecoder::CreateAudioDecoder(int stereo, int bits, int sample_rate, int format_type, ifc_flvaudiodecoder **decoder)
  5. {
  6. if (format_type == FLV::AUDIO_FORMAT_AAC)
  7. {
  8. FLVAAC *aac = new FLVAAC();
  9. if (!aac)
  10. {
  11. return CREATEDECODER_FAILURE;
  12. }
  13. *decoder = aac;
  14. return CREATEDECODER_SUCCESS;
  15. }
  16. return CREATEDECODER_NOT_MINE;
  17. }
  18. int FLVDecoder::HandlesAudio(int format_type)
  19. {
  20. if (format_type == FLV::AUDIO_FORMAT_AAC)
  21. {
  22. return CREATEDECODER_SUCCESS;
  23. }
  24. return CREATEDECODER_NOT_MINE;
  25. }
  26. #define CBCLASS FLVDecoder
  27. START_DISPATCH;
  28. CB(CREATE_AUDIO_DECODER, CreateAudioDecoder)
  29. CB(HANDLES_AUDIO, HandlesAudio)
  30. END_DISPATCH;
  31. #undef CBCLASS
  32. /* --- */
  33. FLVAAC::FLVAAC()
  34. {
  35. bps = 16;
  36. preDelay=0;
  37. got_decoder_config = false;
  38. }
  39. int FLVAAC::GetOutputFormat(unsigned int *sample_rate, unsigned int *channels, unsigned int *_bits)
  40. {
  41. uint32_t local_sample_rate, local_channels;
  42. HRESULT hr = decoder.GetOutputProperties(&local_sample_rate, &local_channels);
  43. if (FAILED(hr)) {
  44. return FLV_AUDIO_FAILURE;
  45. }
  46. *sample_rate = local_sample_rate;
  47. *channels = local_channels;
  48. *_bits = bps;
  49. return FLV_AUDIO_SUCCESS;
  50. }
  51. int FLVAAC::DecodeSample(const void *input_buffer, size_t input_buffer_bytes, void *samples, size_t *samples_size_bytes, double *bitrate)
  52. {
  53. const uint8_t *type = (const uint8_t *)input_buffer;
  54. if (type[0] == 0)
  55. {
  56. decoder.Open(type+1, input_buffer_bytes-1);
  57. got_decoder_config=true;
  58. *samples_size_bytes=0;
  59. return FLV_AUDIO_SUCCESS;
  60. return FLV_AUDIO_FAILURE;
  61. }
  62. else if (type[0] == 1)
  63. {
  64. decoder.Feed(input_buffer, input_buffer_bytes);
  65. decoder.Decode(samples, samples_size_bytes, bps, false, 1.0);
  66. *bitrate = 0;
  67. return FLV_AUDIO_SUCCESS;
  68. }
  69. else
  70. return FLV_AUDIO_FAILURE;
  71. }
  72. void FLVAAC::Flush()
  73. {
  74. decoder.Flush();
  75. }
  76. void FLVAAC::Close()
  77. {
  78. delete this;
  79. }
  80. int FLVAAC::Ready()
  81. {
  82. return !!got_decoder_config;
  83. }
  84. void FLVAAC::SetPreferences(unsigned int _max_channels, unsigned int preferred_bits)
  85. {
  86. if (preferred_bits)
  87. bps = preferred_bits;
  88. // TODO: max channels
  89. }
  90. #define CBCLASS FLVAAC
  91. START_DISPATCH;
  92. CB(FLV_AUDIO_GETOUTPUTFORMAT, GetOutputFormat)
  93. CB(FLV_AUDIO_DECODE, DecodeSample)
  94. VCB(FLV_AUDIO_FLUSH, Flush)
  95. VCB(FLV_AUDIO_CLOSE, Close)
  96. CB(FLV_AUDIO_READY, Ready)
  97. VCB(FLV_AUDIO_SETPREFERENCES, SetPreferences)
  98. END_DISPATCH;
  99. #undef CBCLASS