1
0

mp3_in_mp4.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // used to decode an MPEG-1 audio object in an MPEG-4 ISO Media file
  2. #include "mp3_in_mp4.h"
  3. #include "api__mp3-mpg123.h"
  4. #include "../nsutil/pcm.h"
  5. // {B6CB4A7C-A8D0-4c55-8E60-9F7A7A23DA0F}
  6. static const GUID playbackConfigGroupGUID =
  7. { 0xb6cb4a7c, 0xa8d0, 0x4c55, { 0x8e, 0x60, 0x9f, 0x7a, 0x7a, 0x23, 0xda, 0xf } };
  8. #define FHG_DELAY 529
  9. MPEG4_MP3::MPEG4_MP3()
  10. {
  11. channels = 0;
  12. gain = 1;
  13. floatingPoint = false;
  14. decoder = 0;
  15. sample_rate = 0;
  16. bits = 16;
  17. pregap = FHG_DELAY;
  18. }
  19. MPEG4_MP3::~MPEG4_MP3()
  20. {
  21. if (decoder) {
  22. mpg123_delete(decoder);
  23. decoder = 0;
  24. }
  25. }
  26. int MPEG4_MP3::OpenEx(size_t _bits, size_t _maxChannels, bool useFloat)
  27. {
  28. _bits = bits;
  29. floatingPoint = useFloat;
  30. if (floatingPoint)
  31. bits = 32;
  32. else
  33. bits = (int)_bits;
  34. decoder = mpg123_new(NULL, NULL);
  35. long flags = MPG123_QUIET|MPG123_FORCE_FLOAT|MPG123_SKIP_ID3V2|MPG123_IGNORE_STREAMLENGTH|MPG123_IGNORE_INFOFRAME;
  36. if (_maxChannels == 1) {
  37. flags |= MPG123_FORCE_MONO;
  38. }
  39. mpg123_param(decoder, MPG123_FLAGS, flags, 0);
  40. mpg123_param(decoder, MPG123_RVA, MPG123_RVA_OFF, 0);
  41. mpg123_open_feed(decoder);
  42. return MP4_SUCCESS;
  43. }
  44. const char *MPEG4_MP3::GetCodecInfoString()
  45. {
  46. return 0;
  47. }
  48. int MPEG4_MP3::CanHandleCodec(const char *codecName)
  49. {
  50. if (!lstrcmpA(codecName, "mp4a"))
  51. return 1;
  52. else
  53. return 0;
  54. }
  55. int MPEG4_MP3::CanHandleType(unsigned __int8 type)
  56. {
  57. switch(type)
  58. {
  59. case MP4_MPEG4_LAYER3_AUDIO:
  60. case MP4_MPEG4_LAYER2_AUDIO:
  61. case MP4_MPEG4_LAYER1_AUDIO:
  62. case MP4_TYPE_MPEG1_AUDIO:
  63. case MP4_TYPE_MPEG2_AUDIO:
  64. //case MP4_TYPE_MPEG4_AUDIO:
  65. return 1;
  66. default:
  67. return 0;
  68. }
  69. }
  70. int MPEG4_MP3::DecodeSample(void *inputBuffer, size_t inputBufferBytes, void *outputBuffer, size_t *outputBufferBytes)
  71. {
  72. if (!decoder)
  73. return MP4_FAILURE;
  74. *outputBufferBytes = 0;
  75. mpg123_feed(decoder, (unsigned char *)inputBuffer, inputBufferBytes);
  76. for (;;) {
  77. // get the decoded data out
  78. size_t pcm_buf_used=0;
  79. float decodeBuf[1152*2];
  80. int err = mpg123_read(decoder, (unsigned char *)decodeBuf, sizeof(decodeBuf), &pcm_buf_used);
  81. if (pcm_buf_used) {
  82. if (!_UpdateProperties()) {
  83. return MP4_FAILURE;
  84. }
  85. // deal with pregap
  86. int numSamples = (int)pcm_buf_used / sizeof(float);
  87. int offset = min(numSamples, pregap * channels);
  88. numSamples -= offset;
  89. pregap -= offset / channels;
  90. float *pcm_buf = decodeBuf + offset;
  91. // convert to destination sample format
  92. nsutil_pcm_FloatToInt_Interleaved(outputBuffer, pcm_buf, bits, numSamples);
  93. *outputBufferBytes += numSamples * bits / 8;
  94. outputBuffer = (char *)outputBuffer + numSamples * bits / 8;
  95. return MP4_SUCCESS;
  96. } else if (err == MPG123_NEED_MORE) {
  97. *outputBufferBytes = 0;
  98. return MP4_NEED_MORE_INPUT;
  99. } else if (err == MPG123_NEW_FORMAT) {
  100. continue;
  101. } else if (err == MPG123_OK) {
  102. continue;
  103. }
  104. else
  105. return MP4_FAILURE;
  106. }
  107. return MP4_SUCCESS;
  108. }
  109. bool MPEG4_MP3::_UpdateProperties()
  110. {
  111. if (decoder && (!channels || !sample_rate)) {
  112. long sample_rate = 44100;
  113. int channels = 2;
  114. int encoding = 0;
  115. if (mpg123_getformat(decoder, &sample_rate, &channels, &encoding) == MPG123_OK) {
  116. this->channels = channels;
  117. this->sample_rate = sample_rate;
  118. }
  119. }
  120. return channels && sample_rate;
  121. }
  122. int MPEG4_MP3::GetOutputPropertiesEx(unsigned int *sampleRate, unsigned int *channels, unsigned int *bitsPerSample, bool *isFloat)
  123. {
  124. if (_UpdateProperties()) {
  125. *sampleRate = this->sample_rate;
  126. *channels = this->channels;
  127. *bitsPerSample = bits;
  128. *isFloat = floatingPoint;
  129. return MP4_SUCCESS;
  130. } else {
  131. return MP4_FAILURE;
  132. }
  133. }
  134. int MPEG4_MP3::GetOutputProperties(unsigned int *sampleRate, unsigned int *channels, unsigned int *bitsPerSample)
  135. {
  136. bool dummy;
  137. return GetOutputPropertiesEx(sampleRate, channels, bitsPerSample, &dummy);
  138. }
  139. void MPEG4_MP3::Close()
  140. {
  141. if (decoder) {
  142. mpg123_delete(decoder);
  143. decoder = 0;
  144. }
  145. }
  146. void MPEG4_MP3::Flush()
  147. {
  148. mpg123_open_feed(decoder);
  149. pregap = FHG_DELAY;
  150. }
  151. int MPEG4_MP3::SetGain(float _gain)
  152. {
  153. gain = _gain;
  154. return MP4_SUCCESS;
  155. }
  156. int MPEG4_MP3::GetCurrentBitrate(unsigned int *bitrate)
  157. {
  158. mpg123_frameinfo frameInfo;
  159. if (mpg123_info(decoder, &frameInfo) == MPG123_OK) {
  160. *bitrate = frameInfo.bitrate;
  161. return MP4_SUCCESS;
  162. } else {
  163. return MP4_FAILURE;
  164. }
  165. }
  166. int MPEG4_MP3::OutputFrameSize(size_t *frameSize)
  167. {
  168. if (_UpdateProperties()) {
  169. *frameSize = (bits/8) * channels * mpg123_spf(decoder);
  170. return MP4_SUCCESS;
  171. } else {
  172. return MP4_FAILURE;
  173. }
  174. }
  175. int MPEG4_MP3::CanHandleMPEG4Type(unsigned __int8 type)
  176. {
  177. switch (type)
  178. {
  179. case MP4_MPEG4_LAYER1_AUDIO:
  180. case MP4_MPEG4_LAYER2_AUDIO:
  181. case MP4_MPEG4_LAYER3_AUDIO:
  182. return 1;
  183. default:
  184. return 0;
  185. }
  186. }
  187. #define CBCLASS MPEG4_MP3
  188. START_DISPATCH;
  189. CB(MPEG4_AUDIO_OPEN_EX, OpenEx)
  190. CB(MPEG4_AUDIO_CODEC_INFO_STRING, GetCodecInfoString)
  191. CB(MPEG4_AUDIO_BITRATE, GetCurrentBitrate)
  192. CB(MPEG4_AUDIO_FRAMESIZE, OutputFrameSize)
  193. CB(MPEG4_AUDIO_OUTPUTINFO, GetOutputProperties)
  194. CB(MPEG4_AUDIO_OUTPUTINFO_EX, GetOutputPropertiesEx)
  195. CB(MPEG4_AUDIO_DECODE, DecodeSample)
  196. VCB(MPEG4_AUDIO_FLUSH, Flush)
  197. VCB(MPEG4_AUDIO_CLOSE, Close)
  198. CB(MPEG4_AUDIO_HANDLES_CODEC, CanHandleCodec)
  199. CB(MPEG4_AUDIO_HANDLES_TYPE, CanHandleType)
  200. CB(MPEG4_AUDIO_HANDLES_MPEG4_TYPE, CanHandleMPEG4Type)
  201. CB(MPEG4_AUDIO_SET_GAIN, SetGain)
  202. END_DISPATCH;