1
0

adts_mpg123.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #include "ADTS_MPG123.h"
  2. #include "../winamp/wa_ipc.h"
  3. #include <math.h>
  4. #include "../nsutil/pcm.h"
  5. #include <assert.h>
  6. ADTS_MPG123::ADTS_MPG123() : decoder(0), gain(1.f)
  7. {
  8. decoderDelay = 529;
  9. bitsPerSample = 0;
  10. allowRG = false;
  11. useFloat = false;
  12. channels = 0;
  13. sampleRate = 0;
  14. decode_buffer = 0;
  15. decode_buffer_length = 0;
  16. }
  17. ADTS_MPG123::~ADTS_MPG123()
  18. {
  19. if (decoder) {
  20. mpg123_delete(decoder);
  21. decoder = 0;
  22. }
  23. free(decode_buffer);
  24. }
  25. int ADTS_MPG123::Initialize(bool forceMono, bool reverseStereo, bool allowSurround, int maxBits, bool _allowRG, bool _useFloat, bool _useCRC)
  26. {
  27. allowRG = _allowRG;
  28. useFloat = _useFloat;
  29. int downmix = 0;
  30. if (reverseStereo)
  31. downmix = 2;
  32. else
  33. downmix = forceMono ? 1 : 0;
  34. bitsPerSample = maxBits;
  35. decoder = mpg123_new(NULL, NULL);
  36. long flags = MPG123_QUIET|MPG123_FORCE_FLOAT|MPG123_SKIP_ID3V2|MPG123_IGNORE_STREAMLENGTH|MPG123_IGNORE_INFOFRAME;
  37. if (forceMono) {
  38. flags |= MPG123_FORCE_MONO;
  39. }
  40. mpg123_param(decoder, MPG123_FLAGS, flags, 0);
  41. mpg123_param(decoder, MPG123_RVA, MPG123_RVA_OFF, 0);
  42. return 0;
  43. }
  44. bool ADTS_MPG123::Open(ifc_mpeg_stream_reader *file)
  45. {
  46. mpg123_open_feed(decoder);
  47. if (allowRG) {
  48. gain = file->MPEGStream_Gain();
  49. }
  50. return true;
  51. }
  52. void ADTS_MPG123::Close()
  53. {
  54. if (decoder) {
  55. mpg123_delete(decoder);
  56. decoder = 0;
  57. }
  58. }
  59. bool ADTS_MPG123::_UpdateProperties()
  60. {
  61. if (decoder && (!channels || !sampleRate)) {
  62. long sample_rate = 44100;
  63. int channels = 2;
  64. int encoding = 0;
  65. if (mpg123_getformat(decoder, &sample_rate, &channels, &encoding) == MPG123_OK) {
  66. this->channels = channels;
  67. this->sampleRate = sample_rate;
  68. }
  69. }
  70. return channels && sampleRate;
  71. }
  72. void ADTS_MPG123::GetOutputParameters(size_t *numBits, int *numChannels, int *sampleRate)
  73. {
  74. _UpdateProperties();
  75. *sampleRate = this->sampleRate;
  76. *numChannels = this->channels;
  77. *numBits = bitsPerSample;
  78. }
  79. void ADTS_MPG123::CalculateFrameSize(int *frameSize)
  80. {
  81. _UpdateProperties();
  82. *frameSize = (int)(bitsPerSample/8) * channels * mpg123_spf(decoder);
  83. #if 0 // TODO?
  84. if (decoder->GetStreamInfo()->GetLayer() == 1)
  85. *frameSize *= 3;
  86. #endif
  87. }
  88. void ADTS_MPG123::Flush(ifc_mpeg_stream_reader *file)
  89. {
  90. mpg123_open_feed(decoder);
  91. }
  92. size_t ADTS_MPG123::GetCurrentBitrate()
  93. {
  94. mpg123_frameinfo frameInfo;
  95. if (mpg123_info(decoder, &frameInfo) == MPG123_OK) {
  96. return frameInfo.bitrate;
  97. } else {
  98. return 0;
  99. }
  100. }
  101. size_t ADTS_MPG123::GetDecoderDelay()
  102. {
  103. return decoderDelay;
  104. }
  105. static void Decimate(const float *input, void *output, size_t numSamples, size_t *outputWritten, int bitsPerSample, bool useFloat, float gain)
  106. {
  107. if (!useFloat)
  108. {
  109. // TODO seen a few crashes reported where 'output' is 0
  110. nsutil_pcm_FloatToInt_Interleaved_Gain(output, input, bitsPerSample, numSamples, gain);
  111. }
  112. else if (gain != 1.f)
  113. {
  114. float *data = (float *)output;
  115. for (size_t i=0;i<numSamples;i++)
  116. data[i]*=gain;
  117. //data[i]=input[i]*gain;
  118. }
  119. *outputWritten = numSamples * (bitsPerSample / 8);
  120. }
  121. int ADTS_MPG123::_Read(ifc_mpeg_stream_reader *_file)
  122. {
  123. int err;
  124. do {
  125. unsigned char buffer[4096];
  126. size_t bytes = 0;
  127. _file->MPEGStream_Read(buffer, sizeof(buffer), &bytes);
  128. err = mpg123_feed(decoder, buffer, bytes);
  129. } while (err == MPG123_NEED_MORE);
  130. switch(err) {
  131. case MPG123_OK:
  132. return adts::SUCCESS;
  133. case MPG123_NEED_MORE:
  134. if (_file->MPEGStream_EOF()) {
  135. return adts::ENDOFFILE;
  136. } else {
  137. return adts::NEEDMOREDATA;
  138. }
  139. default:
  140. return adts::FAILURE;
  141. }
  142. }
  143. /*
  144. notes for mp3 surround implementations
  145. need to check the first two frames for ancillary data
  146. store first valid in temp
  147. store second valid frame in delay line
  148. decimate first valid into output buffer
  149. ancillary data is stored one frame behind, so PCM data decoded from mp3 frame n combines with anc data from frame n+1
  150. */
  151. int ADTS_MPG123::Sync(ifc_mpeg_stream_reader *_file, unsigned __int8 *output, size_t outputSize, size_t *outputWritten, size_t *bitrate)
  152. {
  153. if (outputWritten) {
  154. *outputWritten = 0;
  155. }
  156. int err = _Read(_file);
  157. if (err == adts::SUCCESS) {
  158. err = mpg123_read(decoder, 0, 0, 0);
  159. if (err == MPG123_NEED_MORE) {
  160. return adts::NEEDMOREDATA;
  161. }
  162. if (err != MPG123_NEW_FORMAT
  163. && err != MPG123_OK) {
  164. return adts::FAILURE;
  165. }
  166. _UpdateProperties();
  167. mpg123_frameinfo frameInfo;
  168. err = mpg123_info(decoder, &frameInfo);
  169. if (err == MPG123_OK) {
  170. *bitrate = frameInfo.bitrate;
  171. return adts::SUCCESS;
  172. } else {
  173. return adts::NEEDMOREDATA;
  174. }
  175. } else {
  176. return err;
  177. }
  178. }
  179. int ADTS_MPG123::Decode(ifc_mpeg_stream_reader *_file, unsigned __int8 *output, size_t outputSize, size_t *outputWritten, size_t *bitrate, size_t *endCut)
  180. {
  181. bool retried=false;
  182. for (;;) {
  183. if (!decode_buffer) {
  184. int channels = 2;
  185. long sample_rate;
  186. int encoding;
  187. if (mpg123_getformat(decoder, &sample_rate, &channels, &encoding) == MPG123_OK) {
  188. this->channels = channels;
  189. decode_buffer_length = sizeof(float) * channels * mpg123_spf(decoder);
  190. decode_buffer = (float *)malloc(decode_buffer_length);
  191. if (!decode_buffer) {
  192. return adts::FAILURE;
  193. }
  194. }
  195. }
  196. float *flData=useFloat?(float *)output:decode_buffer;
  197. size_t flDataSize=useFloat?outputSize:decode_buffer_length;
  198. size_t done=0;
  199. int err = mpg123_read(decoder, (unsigned char *)flData, flDataSize, &done);
  200. switch(err) {
  201. case MPG123_OK:
  202. {
  203. Decimate(flData, output, done / sizeof(float), outputWritten, (int)bitsPerSample, useFloat, (float)gain);
  204. mpg123_frameinfo frameInfo;
  205. if (mpg123_info(decoder, &frameInfo) == MPG123_OK) {
  206. *bitrate = frameInfo.bitrate;
  207. }
  208. }
  209. return adts::SUCCESS;
  210. case MPG123_NEW_FORMAT:
  211. assert(done == 0);
  212. return adts::SUCCESS;
  213. case MPG123_NEED_MORE:
  214. if (done) {
  215. Decimate(flData, output, done / sizeof(float), outputWritten, (int)bitsPerSample, useFloat, (float)gain);
  216. mpg123_frameinfo frameInfo;
  217. if (mpg123_info(decoder, &frameInfo) == MPG123_OK) {
  218. *bitrate = frameInfo.bitrate;
  219. }
  220. return adts::SUCCESS;
  221. } else if (_file->MPEGStream_EOF()) {
  222. return adts::ENDOFFILE;
  223. } else {
  224. if (retried) {
  225. return adts::NEEDMOREDATA;
  226. } else {
  227. retried=true;
  228. err = _Read(_file);
  229. if (err != adts::SUCCESS) {
  230. return err;
  231. } else {
  232. break;
  233. }
  234. }
  235. }
  236. default:
  237. assert(done == 0);
  238. return adts::FAILURE;
  239. }
  240. }
  241. }
  242. int ADTS_MPG123::GetLayer()
  243. {
  244. if (decoder) {
  245. mpg123_frameinfo frameInfo;
  246. mpg123_info(decoder, &frameInfo);
  247. return frameInfo.layer;
  248. } else {
  249. return 0;
  250. }
  251. }
  252. void ADTS_MPG123::Release()
  253. {
  254. delete this;
  255. }