1
0

MP3Coder.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #include "MP3Coder.h"
  2. AudioCoderMP3::~AudioCoderMP3()
  3. {
  4. if (hbeStream) beCloseStream(hbeStream); hbeStream = 0;
  5. if (bs) free(bs); bs = 0;
  6. }
  7. int AudioCoderMP3::GetLastError() { return m_err; };
  8. void AudioCoderMP3::setVbrFilename(char *filename)
  9. {
  10. if (hbeStream) beCloseStream(hbeStream);
  11. hbeStream = 0;
  12. if (filename)
  13. {
  14. beWriteVBRHeader(filename);
  15. }
  16. }
  17. int AudioCoderMP3::Encode(int framepos, void *in, int in_avail, int *in_used, void *out, int out_avail)
  18. {
  19. if (m_err) return -1;
  20. if (!hbeStream)
  21. {
  22. if (beInitStream(&beConfig, &ibuf_size_spls, &obuf_size, (PHBE_STREAM) &hbeStream) != BE_ERR_SUCCESSFUL)
  23. {
  24. m_err++;
  25. return -1;
  26. }
  27. ibuf_size = ibuf_size_spls * bytesPerSample;
  28. if (is_downmix) ibuf_size *= 2;
  29. bs = (char*)malloc(ibuf_size);
  30. bs_size = 0;
  31. }
  32. *in_used = 0;
  33. int needbytes = ibuf_size - bs_size;
  34. if (needbytes > 0 && in_avail > 0)
  35. {
  36. if (needbytes > in_avail)
  37. needbytes = in_avail;
  38. memcpy(bs + bs_size, in, needbytes);
  39. bs_size += needbytes;
  40. *in_used = needbytes;
  41. }
  42. if (!done)
  43. {
  44. if (bs_size < (int)ibuf_size) return 0;
  45. }
  46. if (out_avail < (int)obuf_size) return 0;
  47. int feedBytes = min(bs_size, (int)ibuf_size);
  48. int feedSamples = feedBytes / bytesPerSample;
  49. bs_size -= feedBytes;
  50. if (is_downmix)
  51. {
  52. int x;
  53. int l = feedBytes / 4;
  54. short *b = (short*)bs;
  55. for (x = 0; x < l; x ++)
  56. {
  57. b[x] = b[x * 2] / 2 + b[x * 2 + 1] / 2;
  58. }
  59. feedSamples/=2;
  60. }
  61. DWORD dwo = 0;
  62. if (feedSamples)
  63. {
  64. if (beEncodeChunk(hbeStream, feedSamples, (short*)bs, (unsigned char*)out, &dwo) != BE_ERR_SUCCESSFUL)
  65. {
  66. m_err++;
  67. return -1;
  68. }
  69. }
  70. if (!dwo && done==1)
  71. {
  72. if (beDeinitStream(hbeStream, (unsigned char *)out, &dwo) != BE_ERR_SUCCESSFUL)
  73. {
  74. m_err++;
  75. return -1;
  76. }
  77. done++;
  78. }
  79. return dwo;
  80. }
  81. void AudioCoderMP3::PrepareToFinish()
  82. {
  83. done = 1;
  84. }
  85. AudioCoderMP3::AudioCoderMP3(int nch, int srate, int bps, configtype *cfg)
  86. : obuf_size(0), ibuf_size(0), ibuf_size_spls(0), done(false), bs_size(0)
  87. {
  88. m_err = 0;
  89. hbeStream = 0;
  90. bs = 0;
  91. is_downmix = 0;
  92. mono_input=0;
  93. memset(&beConfig, 0, sizeof(beConfig)); // clear all fields
  94. if (srate != 32000
  95. && srate != 44100
  96. && srate != 48000
  97. && srate != 16000 /* MPEG 2 sample rates */
  98. && srate != 22050
  99. && srate != 24000
  100. && srate != 11025 /* MPEG 2.5 sample rates */
  101. && srate != 12000
  102. && srate != 8000)
  103. {
  104. //MessageBox(NULL, "The only valid audio sampling rates for the LAME mp3 encoder are:\r\t16000\r\t22050\r\t24000\r\t32000\r\t44100\r\t48000\r\rPlease modify the encoding profile to use one of these sampling rates, and then try again.", "Invalid sampling rate", MB_OK);
  105. m_err++;
  106. return ;
  107. }
  108. // use the LAME config structure
  109. beConfig.dwConfig = BE_CONFIG_LAME;
  110. // this are the default settings for testcase.wav
  111. beConfig.format.LHV1.dwStructVersion = 1;
  112. beConfig.format.LHV1.dwStructSize = sizeof(beConfig);
  113. beConfig.format.LHV1.dwSampleRate = srate; // INPUT FREQUENCY
  114. beConfig.format.LHV1.nMode = cfg->stereo_mode;
  115. if (nch < 2)
  116. {
  117. beConfig.format.LHV1.nMode = BE_MP3_MODE_MONO;
  118. mono_input=1;
  119. }
  120. else
  121. {
  122. if (beConfig.format.LHV1.nMode == BE_MP3_MODE_MONO)
  123. is_downmix = 1;
  124. // beConfig.format.LHV1.nMode = BE_MP3_MODE_STEREO;
  125. }
  126. beConfig.format.LHV1.dwBitrate = cfg->bitrate;
  127. beConfig.format.LHV1.dwMaxBitrate = (cfg->vbr_method != -1 ? cfg->vbr_max_bitrate : cfg->bitrate);
  128. beConfig.format.LHV1.dwReSampleRate = 0; // DOWNSAMPLERATE, 0=ENCODER DECIDES
  129. if (beConfig.format.LHV1.dwMaxBitrate < 32) // less than 32, let's force mono
  130. {
  131. if (nch > 1)
  132. {
  133. beConfig.format.LHV1.nMode = BE_MP3_MODE_MONO;
  134. is_downmix = 1;
  135. }
  136. }
  137. /*int effective_nch = (beConfig.format.LHV1.nMode == BE_MP3_MODE_MONO) ? 1 : 2;
  138. if (beConfig.format.LHV1.dwReSampleRate >= 32000 &&
  139. beConfig.format.LHV1.dwMaxBitrate / effective_nch <= 32)
  140. {
  141. beConfig.format.LHV1.dwReSampleRate /= 2;
  142. }*/
  143. /*
  144. if (beConfig.format.LHV1.dwReSampleRate < 32000)
  145. beConfig.format.LHV1.dwMpegVersion = MPEG2; // MPEG VERSION (I or II)
  146. else
  147. beConfig.format.LHV1.dwMpegVersion = MPEG1; // MPEG VERSION (I or II)
  148. */
  149. beConfig.format.LHV1.nPreset = cfg->quality;
  150. beConfig.format.LHV1.dwPsyModel = 0; // USE DEFAULT PSYCHOACOUSTIC MODEL
  151. beConfig.format.LHV1.dwEmphasis = 0; // NO EMPHASIS TURNED ON
  152. beConfig.format.LHV1.bOriginal = 0; // SET ORIGINAL FLAG
  153. beConfig.format.LHV1.bCRC = 0; // INSERT CRC
  154. beConfig.format.LHV1.bCopyright = 0; // SET COPYRIGHT FLAG
  155. beConfig.format.LHV1.bPrivate = 0; // SET PRIVATE FLAG
  156. beConfig.format.LHV1.bNoRes = 0;
  157. beConfig.format.LHV1.bWriteVBRHeader = 1;
  158. beConfig.format.LHV1.bEnableVBR = cfg->vbr_method != VBR_METHOD_NONE;
  159. beConfig.format.LHV1.nVBRQuality = cfg->vbr;
  160. //beConfig.format.LHV1.dwVbrAbr_bps = (cfg->vbr_method != VBR_METHOD_NONE ? 1000 * cfg->abr_bitrate : 0);
  161. beConfig.format.LHV1.dwVbrAbr_bps = (cfg->vbr_method == VBR_METHOD_ABR ? 1000 * cfg->abr_bitrate : 0);
  162. beConfig.format.LHV1.nVbrMethod = (VBRMETHOD)cfg->vbr_method;
  163. bytesPerSample = bps / 8;
  164. }