nsvmain.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "../nsv/nsvlib.h"
  2. #include "../nsv/dec_if.h"
  3. #include "nsvmain.h"
  4. #include "../nsutil/pcm.h"
  5. MP3_Decoder::MP3_Decoder()
  6. {
  7. decoder = mpg123_new(NULL, NULL);
  8. long flags = MPG123_QUIET|MPG123_FORCE_FLOAT|MPG123_SKIP_ID3V2|MPG123_IGNORE_STREAMLENGTH|MPG123_IGNORE_INFOFRAME;
  9. mpg123_param(decoder, MPG123_FLAGS, flags, 0);
  10. mpg123_param(decoder, MPG123_RVA, MPG123_RVA_OFF, 0);
  11. memset(pcm_buf, 0, sizeof(pcm_buf));
  12. mpg123_open_feed(decoder);
  13. fused = 0; pcm_buf_used = 0; pcm_offs = 0;
  14. }
  15. int MP3_Decoder::decode(void *in, int in_len,
  16. void *out, int *out_len,
  17. unsigned int out_fmt[8])
  18. {
  19. int rval = 1;
  20. if (fused < in_len)
  21. {
  22. int l = 4096;
  23. if (l > in_len - fused) l = in_len - fused;
  24. if (l) mpg123_feed(decoder, (unsigned char *)in + fused, l);
  25. fused += l;
  26. }
  27. if (!pcm_buf_used)
  28. {
  29. mpg123_read(decoder, (unsigned char *)pcm_buf, sizeof(pcm_buf), &pcm_buf_used);
  30. pcm_offs = 0;
  31. }
  32. if (pcm_buf_used)
  33. {
  34. size_t numSamples = *out_len / 2;
  35. if (numSamples > (pcm_buf_used/sizeof(float)))
  36. numSamples = pcm_buf_used/sizeof(float);
  37. nsutil_pcm_FloatToInt_Interleaved(out, pcm_buf+pcm_offs, 16, numSamples);
  38. pcm_buf_used -= numSamples*sizeof(float);
  39. pcm_offs += (int)numSamples;
  40. *out_len = 2*(int)numSamples;
  41. }
  42. else
  43. {
  44. if (fused >= in_len) { fused = 0; rval = 0; }
  45. *out_len = 0;
  46. }
  47. mpg123_frameinfo frameInfo;
  48. if (mpg123_info(decoder, &frameInfo) == MPG123_OK) {
  49. int nch = (frameInfo.mode == MPG123_M_MONO)?1:2;
  50. int srate = frameInfo.rate;
  51. out_fmt[0] = (nch && srate) ? NSV_MAKETYPE('P', 'C', 'M', ' ') : 0;
  52. out_fmt[1] = srate;
  53. out_fmt[2] = nch;
  54. out_fmt[3] = (nch && srate) ? 16 : 0;
  55. out_fmt[4] = frameInfo.bitrate;
  56. }
  57. return rval;
  58. }
  59. void MP3_Decoder::flush()
  60. {
  61. fused = 0;
  62. pcm_buf_used = 0;
  63. pcm_offs = 0;
  64. mpg123_open_feed(decoder);
  65. }
  66. extern "C"
  67. {
  68. __declspec(dllexport) IAudioDecoder *CreateAudioDecoder(unsigned int fmt, IAudioOutput **output)
  69. {
  70. switch (fmt)
  71. {
  72. case NSV_MAKETYPE('M', 'P', '3', ' '):
  73. return new MP3_Decoder;
  74. default:
  75. return NULL;
  76. }
  77. }
  78. __declspec(dllexport) void DeleteAudioDecoder(IAudioDecoder *decoder)
  79. {
  80. if (decoder)
  81. delete decoder;
  82. }
  83. }