1
0

MKVAACDecoder.cpp 2.9 KB

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