1
0

avi_mp3_decoder.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #include "avi_mp3_decoder.h"
  2. #include "../nsutil/pcm.h"
  3. #include <assert.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::mp3_format *waveformat = (nsavi::mp3_format *)stream_format;
  10. if (waveformat->format.format == nsavi::audio_format_mp3
  11. ||waveformat->format.format == nsavi::audio_format_mp2)
  12. {
  13. mpg123_handle *ctx = mpg123_new(NULL, NULL);
  14. if (!ctx)
  15. return CREATEDECODER_FAILURE;
  16. long flags = MPG123_QUIET|MPG123_FORCE_FLOAT|MPG123_SKIP_ID3V2|MPG123_IGNORE_STREAMLENGTH|MPG123_IGNORE_INFOFRAME;
  17. if (max_channels == 1) {
  18. flags |= MPG123_FORCE_MONO;
  19. }
  20. mpg123_param(ctx, MPG123_FLAGS, flags, 0);
  21. mpg123_param(ctx, MPG123_RVA, MPG123_RVA_OFF, 0);
  22. *decoder = new AVIMP3Decoder(ctx, preferred_bits, max_channels, floating_point);
  23. return CREATEDECODER_SUCCESS;
  24. }
  25. return CREATEDECODER_NOT_MINE;
  26. }
  27. #define CBCLASS AVIDecoder
  28. START_DISPATCH;
  29. CB(CREATE_AUDIO_DECODER, CreateAudioDecoder)
  30. END_DISPATCH;
  31. #undef CBCLASS
  32. #define FHG_DELAY 529
  33. AVIMP3Decoder::AVIMP3Decoder(mpg123_handle *mp3, unsigned int bps, unsigned max_channels, bool floating_point)
  34. : mp3(mp3), bits(bps?bps:16), max_channels(max_channels?max_channels:2), floating_point(floating_point), channels(0)
  35. {
  36. mpg123_open_feed(mp3);
  37. pregap = FHG_DELAY;
  38. channels = 0;
  39. decode_buffer=0;
  40. decode_buffer_length=0;
  41. }
  42. AVIMP3Decoder::~AVIMP3Decoder()
  43. {
  44. if (mp3) {
  45. mpg123_delete(mp3);
  46. mp3 = 0;
  47. }
  48. free(decode_buffer);
  49. }
  50. int AVIMP3Decoder::OutputFrameSize(size_t *frame_size)
  51. {
  52. long sample_rate = 44100;
  53. int channels = 2;
  54. int encoding = 0;
  55. if (mpg123_getformat(mp3, &sample_rate, &channels, &encoding) == MPG123_OK) {
  56. this->channels = channels;
  57. }
  58. *frame_size = (bits/8) * channels * mpg123_spf(mp3);
  59. #if 0 // TODO?
  60. if (mp3->GetStreamInfo()->GetLayer() == 1)
  61. *frame_size *= 3;
  62. #endif
  63. return AVI_SUCCESS;
  64. }
  65. int AVIMP3Decoder::GetOutputProperties(unsigned int *sampleRate, unsigned int *_channels, unsigned int *bitsPerSample, bool *isFloat)
  66. {
  67. long sample_rate = 44100;
  68. int channels = 2;
  69. int encoding = 0;
  70. if (mpg123_getformat(mp3, &sample_rate, &channels, &encoding) == MPG123_OK) {
  71. this->channels = channels;
  72. *sampleRate = sample_rate;
  73. *_channels = channels;
  74. *bitsPerSample = bits;
  75. *isFloat = floating_point;
  76. return AVI_SUCCESS;
  77. }
  78. else
  79. {
  80. return AVI_FAILURE;
  81. }
  82. }
  83. int AVIMP3Decoder::DecodeChunk(uint16_t type, void **inputBuffer, size_t *inputBufferBytes, void *outputBuffer, size_t *outputBufferBytes)
  84. {
  85. if (!mp3)
  86. return AVI_FAILURE;
  87. if (inputBufferBytes) {
  88. const uint8_t *mp3_buffer = (const uint8_t *)*inputBuffer;
  89. int err = mpg123_feed(mp3, mp3_buffer, *inputBufferBytes);
  90. if (err == MPG123_OK) {
  91. mp3_buffer += *inputBufferBytes;
  92. *inputBuffer = (void *)mp3_buffer;
  93. *inputBufferBytes = 0;
  94. }
  95. }
  96. size_t output_buffer_len = *outputBufferBytes;
  97. *outputBufferBytes = 0;
  98. for (;;) {
  99. if (!decode_buffer) {
  100. int channels = 2;
  101. long sample_rate;
  102. int encoding;
  103. if (mpg123_getformat(mp3, &sample_rate, &channels, &encoding) == MPG123_OK) {
  104. this->channels = channels;
  105. decode_buffer_length = sizeof(float) * channels * mpg123_spf(mp3);
  106. decode_buffer = (float *)malloc(decode_buffer_length);
  107. if (!decode_buffer) {
  108. return AVI_FAILURE;
  109. }
  110. }
  111. }
  112. // get the decoded data out
  113. // TODO: if floating_point is true and pregap is 0, decode straight into outputBuffer
  114. size_t pcm_buf_used=0;
  115. int err = mpg123_read(mp3, (unsigned char *)decode_buffer, decode_buffer_length, &pcm_buf_used);
  116. if (err == MPG123_NEED_MORE && pcm_buf_used == 0) {
  117. if (*outputBufferBytes == 0) {
  118. return AVI_NEED_MORE_INPUT;
  119. } else {
  120. return AVI_SUCCESS;
  121. }
  122. } else if (err == MPG123_NEW_FORMAT) {
  123. assert(pcm_buf_used == 0);
  124. continue;
  125. } else if (err == MPG123_OK || pcm_buf_used) {
  126. if (!channels) {
  127. long sample_rate = 44100;
  128. int channels = 2;
  129. int encoding = 0;
  130. if (mpg123_getformat(mp3, &sample_rate, &channels, &encoding) == MPG123_OK) {
  131. this->channels = channels;
  132. } else {
  133. return AVI_FAILURE;
  134. }
  135. }
  136. // deal with pregap
  137. size_t numSamples = pcm_buf_used / sizeof(float);
  138. size_t offset = min(numSamples, pregap * channels);
  139. numSamples -= offset;
  140. pregap -= (int)offset / channels;
  141. float *pcm_buf = decode_buffer + offset;
  142. if (numSamples * bits / 8 > output_buffer_len) {
  143. return AVI_SUCCESS;
  144. }
  145. // convert to destination sample format
  146. if (floating_point)
  147. {
  148. memcpy(outputBuffer, pcm_buf, numSamples*bits/8);
  149. }
  150. else
  151. {
  152. nsutil_pcm_FloatToInt_Interleaved_Gain(outputBuffer, pcm_buf, bits, numSamples, 1.0);
  153. }
  154. *outputBufferBytes = *outputBufferBytes + numSamples * bits / 8;
  155. outputBuffer = (char *)outputBuffer + numSamples * bits / 8;
  156. output_buffer_len -= numSamples * bits / 8;
  157. //return AVI_SUCCESS;
  158. } else {
  159. assert(pcm_buf_used == 0);
  160. return AVI_FAILURE;
  161. }
  162. }
  163. return AVI_SUCCESS;
  164. }
  165. void AVIMP3Decoder::Flush()
  166. {
  167. mpg123_open_feed(mp3);
  168. pregap = FHG_DELAY;
  169. }
  170. void AVIMP3Decoder::Close()
  171. {
  172. if (mp3) {
  173. mpg123_delete(mp3);
  174. mp3 = 0;
  175. }
  176. delete this;
  177. }
  178. #define CBCLASS AVIMP3Decoder
  179. START_DISPATCH;
  180. CB(OUTPUT_FRAME_SIZE, OutputFrameSize)
  181. CB(GET_OUTPUT_PROPERTIES, GetOutputProperties)
  182. CB(DECODE_CHUNK, DecodeChunk)
  183. VCB(FLUSH, Flush)
  184. VCB(CLOSE, Close)
  185. END_DISPATCH;
  186. #undef CBCLASS