flv_mp3_decoder.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #include "flv_mp3_decoder.h"
  2. #include "../nsutil/pcm.h"
  3. int FLVDecoderCreator::CreateAudioDecoder(int stereo, int bits, int sample_rate, int format_type, ifc_flvaudiodecoder **decoder)
  4. {
  5. if (format_type == FLV::AUDIO_FORMAT_MP3 || format_type == FLV::AUDIO_FORMAT_MP3_8KHZ)
  6. {
  7. mpg123_handle *ctx = mpg123_new(NULL, NULL);
  8. if (!ctx)
  9. return CREATEDECODER_FAILURE;
  10. long flags = MPG123_QUIET|MPG123_FORCE_FLOAT|MPG123_SKIP_ID3V2|MPG123_IGNORE_STREAMLENGTH|MPG123_IGNORE_INFOFRAME;
  11. if (!stereo) {
  12. flags |= MPG123_FORCE_MONO;
  13. }
  14. mpg123_param(ctx, MPG123_FLAGS, flags, 0);
  15. mpg123_param(ctx, MPG123_RVA, MPG123_RVA_OFF, 0);
  16. *decoder = new FLVMP3(ctx);
  17. return CREATEDECODER_SUCCESS;
  18. }
  19. return CREATEDECODER_NOT_MINE;
  20. }
  21. int FLVDecoderCreator::HandlesAudio(int format_type)
  22. {
  23. if (format_type == FLV::AUDIO_FORMAT_MP3 || format_type == FLV::AUDIO_FORMAT_MP3_8KHZ)
  24. {
  25. return CREATEDECODER_SUCCESS;
  26. }
  27. return CREATEDECODER_NOT_MINE;
  28. }
  29. #define CBCLASS FLVDecoderCreator
  30. START_DISPATCH;
  31. CB(CREATE_AUDIO_DECODER, CreateAudioDecoder)
  32. CB(HANDLES_AUDIO, HandlesAudio)
  33. END_DISPATCH;
  34. #undef CBCLASS
  35. /* --- */
  36. #define FHG_DELAY 529
  37. FLVMP3::FLVMP3(mpg123_handle *mp3) : mp3(mp3)
  38. {
  39. mpg123_open_feed(mp3);
  40. bits = 16;
  41. pregap = FHG_DELAY;
  42. max_channels = 2;
  43. channels = 0;
  44. decode_buffer = 0;
  45. decode_buffer_length = 0;
  46. }
  47. FLVMP3::~FLVMP3()
  48. {
  49. if (mp3) {
  50. mpg123_delete(mp3);
  51. mp3 = 0;
  52. }
  53. free(decode_buffer);
  54. }
  55. int FLVMP3::GetOutputFormat(unsigned int *sample_rate, unsigned int *_channels, unsigned int *_bits)
  56. {
  57. mpg123_frameinfo frameInfo;
  58. if (mpg123_info(mp3, &frameInfo) == MPG123_OK)
  59. {
  60. *sample_rate = frameInfo.rate;
  61. channels = (frameInfo.mode == MPG123_M_MONO)?1:2;
  62. *_channels = channels;
  63. *_bits = bits;
  64. return FLV_AUDIO_SUCCESS;
  65. }
  66. else
  67. {
  68. return FLV_AUDIO_FAILURE;
  69. }
  70. }
  71. int FLVMP3::DecodeSample(const void *input_buffer, size_t input_buffer_bytes, void *samples, size_t *samples_size_bytes, double *bitrate)
  72. {
  73. if (!mp3)
  74. return FLV_AUDIO_FAILURE;
  75. mpg123_feed(mp3, (unsigned char *)input_buffer, input_buffer_bytes);
  76. *samples_size_bytes = 0;
  77. *bitrate = 0;
  78. for (;;) {
  79. if (!decode_buffer) {
  80. int channels = 2;
  81. long sample_rate;
  82. int encoding;
  83. if (mpg123_getformat(mp3, &sample_rate, &channels, &encoding) == MPG123_OK) {
  84. this->channels = channels;
  85. decode_buffer_length = sizeof(float) * channels * mpg123_spf(mp3);
  86. decode_buffer = (float *)malloc(decode_buffer_length);
  87. if (!decode_buffer) {
  88. return FLV_AUDIO_FAILURE;
  89. }
  90. }
  91. }
  92. // get the decoded data out
  93. size_t pcm_buf_used=0;
  94. int err = mpg123_read(mp3, (unsigned char *)decode_buffer, decode_buffer_length, &pcm_buf_used);
  95. if (pcm_buf_used) {
  96. if (!channels) {
  97. long sample_rate = 44100;
  98. int channels = 2;
  99. int encoding = 0;
  100. if (mpg123_getformat(mp3, &sample_rate, &channels, &encoding) == MPG123_OK) {
  101. this->channels = channels;
  102. } else {
  103. return FLV_AUDIO_FAILURE;
  104. }
  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(samples, pcm_buf, bits, numSamples);
  114. *samples_size_bytes += numSamples * bits / 8;
  115. samples = (char *)samples + numSamples * bits / 8;
  116. mpg123_frameinfo frameInfo;
  117. if (mpg123_info(mp3, &frameInfo) == MPG123_OK) {
  118. *bitrate = frameInfo.bitrate;
  119. }
  120. return FLV_AUDIO_SUCCESS;
  121. } else if (err == MPG123_NEED_MORE) {
  122. *samples_size_bytes = 0;
  123. return FLV_AUDIO_NEEDS_MORE_INPUT;
  124. } else if (err == MPG123_NEW_FORMAT) {
  125. continue;
  126. } else if (err == MPG123_OK) {
  127. continue;
  128. }
  129. else
  130. return FLV_AUDIO_FAILURE;
  131. }
  132. return FLV_AUDIO_SUCCESS;
  133. }
  134. void FLVMP3::Flush()
  135. {
  136. mpg123_open_feed(mp3);
  137. pregap = FHG_DELAY;
  138. }
  139. void FLVMP3::Close()
  140. {
  141. if (mp3) {
  142. mpg123_delete(mp3);
  143. mp3 = 0;
  144. }
  145. delete this;
  146. }
  147. void FLVMP3::SetPreferences(unsigned int _max_channels, unsigned int preferred_bits)
  148. {
  149. if (preferred_bits)
  150. bits = preferred_bits;
  151. if (max_channels > _max_channels)
  152. max_channels = _max_channels;
  153. long flags = MPG123_QUIET|MPG123_FORCE_FLOAT|MPG123_SKIP_ID3V2|MPG123_IGNORE_STREAMLENGTH|MPG123_IGNORE_INFOFRAME;
  154. if (max_channels == 1) {
  155. flags |= MPG123_FORCE_MONO;
  156. }
  157. mpg123_param(mp3, MPG123_FLAGS, flags, 0);
  158. mpg123_param(mp3, MPG123_RVA, MPG123_RVA_OFF, 0);
  159. }
  160. #define CBCLASS FLVMP3
  161. START_DISPATCH;
  162. CB(FLV_AUDIO_GETOUTPUTFORMAT, GetOutputFormat)
  163. CB(FLV_AUDIO_DECODE, DecodeSample)
  164. VCB(FLV_AUDIO_FLUSH, Flush)
  165. VCB(FLV_AUDIO_CLOSE, Close)
  166. VCB(FLV_AUDIO_SETPREFERENCES, SetPreferences)
  167. END_DISPATCH;
  168. #undef CBCLASS