1
0

mkv_mp3_decoder.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include "mkv_mp3_decoder.h"
  2. #include "../nsutil/pcm.h"
  3. 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)
  4. {
  5. if (!strcmp(codec_id, "A_MPEG/L3")
  6. || !strcmp(codec_id, "A_MPEG/L2")
  7. || !strcmp(codec_id, "A_MPEG/L1"))
  8. {
  9. mpg123_handle *ctx = mpg123_new(NULL, NULL);
  10. if (!ctx)
  11. return CREATEDECODER_FAILURE;
  12. long flags = MPG123_QUIET|MPG123_FORCE_FLOAT|MPG123_SKIP_ID3V2|MPG123_IGNORE_STREAMLENGTH|MPG123_IGNORE_INFOFRAME;
  13. if (max_channels == 1) {
  14. flags |= MPG123_FORCE_MONO;
  15. }
  16. mpg123_param(ctx, MPG123_FLAGS, flags, 0);
  17. mpg123_param(ctx, MPG123_RVA, MPG123_RVA_OFF, 0);
  18. *decoder = new MKVMP3Decoder(ctx, preferred_bits, max_channels, floating_point);
  19. return CREATEDECODER_SUCCESS;
  20. }
  21. return CREATEDECODER_NOT_MINE;
  22. }
  23. #define CBCLASS MKVDecoder
  24. START_DISPATCH;
  25. CB(CREATE_AUDIO_DECODER, CreateAudioDecoder)
  26. END_DISPATCH;
  27. #undef CBCLASS
  28. #define FHG_DELAY 529
  29. MKVMP3Decoder::MKVMP3Decoder(mpg123_handle *mp3, unsigned int bps, unsigned max_channels, bool floating_point)
  30. : mp3(mp3), bits(bps?bps:16), max_channels(max_channels?max_channels:2), floating_point(floating_point)
  31. {
  32. decode_buffer=0;
  33. decode_buffer_length=0;
  34. sample_rate = 0;
  35. channels = 0;
  36. pregap = FHG_DELAY;
  37. mpg123_open_feed(mp3);
  38. }
  39. MKVMP3Decoder::~MKVMP3Decoder()
  40. {
  41. if (mp3) {
  42. mpg123_delete(mp3);
  43. mp3 = 0;
  44. }
  45. free(decode_buffer);
  46. }
  47. bool MKVMP3Decoder::_UpdateProperties()
  48. {
  49. if (mp3 && (!channels || !sample_rate)) {
  50. long sample_rate = 44100;
  51. int channels = 2;
  52. int encoding = 0;
  53. if (mpg123_getformat(mp3, &sample_rate, &channels, &encoding) == MPG123_OK) {
  54. this->channels = channels;
  55. this->sample_rate = sample_rate;
  56. }
  57. }
  58. return channels && sample_rate;
  59. }
  60. int MKVMP3Decoder::OutputFrameSize(size_t *frame_size)
  61. {
  62. if (_UpdateProperties()) {
  63. *frame_size = (bits/8) * channels * mpg123_spf(mp3);
  64. return MKV_SUCCESS;
  65. } else {
  66. return MKV_FAILURE;
  67. }
  68. }
  69. int MKVMP3Decoder::GetOutputProperties(unsigned int *sampleRate, unsigned int *channels, unsigned int *bitsPerSample, bool *isFloat)
  70. {
  71. if (_UpdateProperties()) {
  72. *sampleRate = this->sample_rate;
  73. *channels = this->channels;
  74. *bitsPerSample = bits;
  75. *isFloat = floating_point;
  76. return MKV_SUCCESS;
  77. }
  78. else
  79. {
  80. return MKV_FAILURE;
  81. }
  82. }
  83. int MKVMP3Decoder::DecodeBlock(void *inputBuffer, size_t inputBufferBytes, void *outputBuffer, size_t *outputBufferBytes)
  84. {
  85. if (!mp3)
  86. return MKV_FAILURE;
  87. *outputBufferBytes = 0;
  88. mpg123_feed(mp3, (unsigned char *)inputBuffer, inputBufferBytes);
  89. for (;;) {
  90. if (!decode_buffer) {
  91. if (_UpdateProperties()) {
  92. decode_buffer_length = sizeof(float) * channels * mpg123_spf(mp3);
  93. decode_buffer = (float *)malloc(decode_buffer_length);
  94. if (!decode_buffer) {
  95. return MKV_FAILURE;
  96. }
  97. }
  98. }
  99. // get the decoded data out
  100. size_t pcm_buf_used=0;
  101. int err = mpg123_read(mp3, (unsigned char *)decode_buffer, decode_buffer_length, &pcm_buf_used);
  102. if (pcm_buf_used) {
  103. if (!_UpdateProperties()) {
  104. return MKV_FAILURE;
  105. }
  106. // deal with pregap
  107. size_t numSamples = pcm_buf_used / sizeof(float);
  108. size_t offset = min(numSamples, pregap * channels);
  109. numSamples -= offset;
  110. pregap -= (int)offset / channels;
  111. float *pcm_buf = decode_buffer + offset;
  112. // convert to destination sample format
  113. nsutil_pcm_FloatToInt_Interleaved(outputBuffer, pcm_buf, bits, numSamples);
  114. *outputBufferBytes += numSamples * bits / 8;
  115. outputBuffer = (char *)outputBuffer + numSamples * bits / 8;
  116. return MKV_SUCCESS;
  117. } else if (err == MPG123_NEED_MORE) {
  118. *outputBufferBytes = 0;
  119. return MKV_NEED_MORE_INPUT;
  120. } else if (err == MPG123_NEW_FORMAT) {
  121. continue;
  122. } else if (err == MPG123_OK) {
  123. continue;
  124. }
  125. else
  126. return MKV_FAILURE;
  127. }
  128. return MKV_SUCCESS;
  129. }
  130. void MKVMP3Decoder::Flush()
  131. {
  132. mpg123_open_feed(mp3);
  133. pregap = FHG_DELAY;
  134. }
  135. void MKVMP3Decoder::Close()
  136. {
  137. delete this;
  138. }
  139. #define CBCLASS MKVMP3Decoder
  140. START_DISPATCH;
  141. CB(OUTPUT_FRAME_SIZE, OutputFrameSize)
  142. CB(GET_OUTPUT_PROPERTIES, GetOutputProperties)
  143. CB(DECODE_BLOCK, DecodeBlock)
  144. VCB(FLUSH, Flush)
  145. VCB(CLOSE, Close)
  146. END_DISPATCH;
  147. #undef CBCLASS