ACMStream.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /**
  2. *
  3. * Lame ACM wrapper, encode/decode MP3 based RIFF/AVI files in MS Windows
  4. *
  5. * Copyright (c) 2002 Steve Lhomme <steve.lhomme at free.fr>
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. /*!
  23. \author Steve Lhomme
  24. \version \$Id$
  25. */
  26. #if !defined(STRICT)
  27. #define STRICT
  28. #endif // STRICT
  29. #include <assert.h>
  30. #include <windows.h>
  31. #include "adebug.h"
  32. #include "ACMStream.h"
  33. #include <lame.h>
  34. // static methods
  35. ACMStream * ACMStream::Create()
  36. {
  37. ACMStream * Result;
  38. Result = new ACMStream;
  39. return Result;
  40. }
  41. const bool ACMStream::Erase(const ACMStream * a_ACMStream)
  42. {
  43. delete a_ACMStream;
  44. return true;
  45. }
  46. // class methods
  47. ACMStream::ACMStream() :
  48. m_WorkingBufferUseSize(0),
  49. gfp(NULL)
  50. {
  51. /// \todo get the debug level from the registry
  52. my_debug = new ADbg(DEBUG_LEVEL_CREATION);
  53. if (my_debug != NULL) {
  54. unsigned char DebugFileName[512];
  55. my_debug->setPrefix("LAMEstream"); /// \todo get it from the registry
  56. my_debug->setIncludeTime(true); /// \todo get it from the registry
  57. // Check in the registry if we have to Output Debug information
  58. DebugFileName[0] = '\0';
  59. HKEY OssKey;
  60. if (RegOpenKeyEx( HKEY_LOCAL_MACHINE, "SOFTWARE\\MUKOLI", 0, KEY_READ , &OssKey ) == ERROR_SUCCESS) {
  61. DWORD DataType;
  62. DWORD DebugFileNameSize = 512;
  63. if (RegQueryValueEx( OssKey, "DebugFile", NULL, &DataType, DebugFileName, &DebugFileNameSize ) == ERROR_SUCCESS) {
  64. if (DataType == REG_SZ) {
  65. my_debug->setUseFile(true);
  66. my_debug->setDebugFile((char *)DebugFileName);
  67. my_debug->OutPut("Debug file is %s",(char *)DebugFileName);
  68. }
  69. }
  70. }
  71. my_debug->OutPut(DEBUG_LEVEL_FUNC_START, "ACMStream Creation (0X%08X)",this);
  72. }
  73. else {
  74. ADbg debug;
  75. debug.OutPut("ACMStream::ACMACMStream : Impossible to create my_debug");
  76. }
  77. }
  78. ACMStream::~ACMStream()
  79. {
  80. // release memory - encoding is finished
  81. if (gfp) lame_close( gfp );
  82. if (my_debug != NULL)
  83. {
  84. my_debug->OutPut(DEBUG_LEVEL_FUNC_START, "ACMStream Deletion (0X%08X)",this);
  85. delete my_debug;
  86. }
  87. }
  88. bool ACMStream::init(const int nSamplesPerSec, const int nOutputSamplesPerSec, const int nChannels, const int nAvgBytesPerSec, const vbr_mode mode)
  89. {
  90. bool bResult = false;
  91. my_SamplesPerSec = nSamplesPerSec;
  92. my_OutBytesPerSec = nOutputSamplesPerSec;
  93. my_Channels = nChannels;
  94. my_AvgBytesPerSec = nAvgBytesPerSec;
  95. my_VBRMode = mode;
  96. bResult = true;
  97. return bResult;
  98. }
  99. bool ACMStream::open(const AEncodeProperties & the_Properties)
  100. {
  101. bool bResult = false;
  102. // Init the MP3 Stream
  103. // Init the global flags structure
  104. gfp = lame_init();
  105. // Set input sample frequency
  106. lame_set_in_samplerate( gfp, my_SamplesPerSec );
  107. // Set output sample frequency
  108. lame_set_out_samplerate( gfp, my_OutBytesPerSec );
  109. lame_set_num_channels( gfp, my_Channels );
  110. if (my_Channels == 1)
  111. lame_set_mode( gfp, MONO );
  112. else
  113. lame_set_mode( gfp, (MPEG_mode_e)the_Properties.GetChannelModeValue()) ; /// \todo Get the mode from the default configuration
  114. // lame_set_VBR( gfp, vbr_off ); /// \note VBR not supported for the moment
  115. lame_set_VBR( gfp, my_VBRMode ); /// \note VBR not supported for the moment
  116. if (my_VBRMode == vbr_abr)
  117. {
  118. lame_set_VBR_q( gfp, 1 );
  119. lame_set_VBR_mean_bitrate_kbps( gfp, (my_AvgBytesPerSec * 8 + 500) / 1000 );
  120. if (24000 > lame_get_in_samplerate( gfp ))
  121. {
  122. // For MPEG-II
  123. lame_set_VBR_min_bitrate_kbps( gfp, 8);
  124. lame_set_VBR_max_bitrate_kbps( gfp, 160);
  125. }
  126. else
  127. {
  128. // For MPEG-I
  129. lame_set_VBR_min_bitrate_kbps( gfp, 32);
  130. lame_set_VBR_max_bitrate_kbps( gfp, 320);
  131. }
  132. }
  133. // Set bitrate
  134. lame_set_brate( gfp, my_AvgBytesPerSec * 8 / 1000 );
  135. /// \todo Get the mode from the default configuration
  136. // Set copyright flag?
  137. lame_set_copyright( gfp, the_Properties.GetCopyrightMode()?1:0 );
  138. // Do we have to tag it as non original
  139. lame_set_original( gfp, the_Properties.GetOriginalMode()?1:0 );
  140. // Add CRC?
  141. lame_set_error_protection( gfp, the_Properties.GetCRCMode()?1:0 );
  142. // Set private bit?
  143. lame_set_extension( gfp, the_Properties.GetPrivateMode()?1:0 );
  144. // INFO tag support not possible in ACM - it requires rewinding
  145. // output stream to the beginning after encoding is finished.
  146. lame_set_bWriteVbrTag( gfp, 0 );
  147. if (0 == lame_init_params( gfp ))
  148. {
  149. //LAME encoding call will accept any number of samples.
  150. if ( 0 == lame_get_version( gfp ) )
  151. {
  152. // For MPEG-II, only 576 samples per frame per channel
  153. my_SamplesPerBlock = 576 * lame_get_num_channels( gfp );
  154. }
  155. else
  156. {
  157. // For MPEG-I, 1152 samples per frame per channel
  158. my_SamplesPerBlock = 1152 * lame_get_num_channels( gfp );
  159. }
  160. }
  161. my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "version =%d",lame_get_version( gfp ) );
  162. my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "Layer =3");
  163. switch ( lame_get_mode( gfp ) )
  164. {
  165. case STEREO: my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "mode =Stereo" ); break;
  166. case JOINT_STEREO: my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "mode =Joint-Stereo" ); break;
  167. case DUAL_CHANNEL: my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "mode =Forced Stereo" ); break;
  168. case MONO: my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "mode =Mono" ); break;
  169. case NOT_SET: /* FALLTROUGH */
  170. default: my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "mode =Error (unknown)" ); break;
  171. }
  172. my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "sampling frequency =%.1f kHz", lame_get_in_samplerate( gfp ) /1000.0 );
  173. my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "bitrate =%d kbps", lame_get_brate( gfp ) );
  174. my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "Vbr Min bitrate =%d kbps", lame_get_VBR_min_bitrate_kbps( gfp ) );
  175. my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "Vbr Max bitrate =%d kbps", lame_get_VBR_max_bitrate_kbps( gfp ) );
  176. my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "Quality Setting =%d", lame_get_quality( gfp ) );
  177. my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "Low pass frequency =%d", lame_get_lowpassfreq( gfp ) );
  178. my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "Low pass width =%d", lame_get_lowpasswidth( gfp ) );
  179. my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "High pass frequency =%d", lame_get_highpassfreq( gfp ) );
  180. my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "High pass width =%d", lame_get_highpasswidth( gfp ) );
  181. my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "No Short Blocks =%d", lame_get_no_short_blocks( gfp ) );
  182. my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "de-emphasis =%d", lame_get_emphasis( gfp ) );
  183. my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "private flag =%d", lame_get_extension( gfp ) );
  184. my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "copyright flag =%d", lame_get_copyright( gfp ) );
  185. my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "original flag =%d", lame_get_original( gfp ) );
  186. my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "CRC =%s", lame_get_error_protection( gfp ) ? "on" : "off" );
  187. my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "Fast mode =%s", ( lame_get_quality( gfp ) )? "enabled" : "disabled" );
  188. my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "Force mid/side stereo =%s", ( lame_get_force_ms( gfp ) )?"enabled":"disabled" );
  189. my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "Disable Resorvoir =%d", lame_get_disable_reservoir( gfp ) );
  190. my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "VBR =%s, VBR_q =%d, VBR method =",
  191. ( lame_get_VBR( gfp ) !=vbr_off ) ? "enabled": "disabled",
  192. lame_get_VBR_q( gfp ) );
  193. switch ( lame_get_VBR( gfp ) )
  194. {
  195. case vbr_off: my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "vbr_off" ); break;
  196. case vbr_mt : my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "vbr_mt" ); break;
  197. case vbr_rh : my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "vbr_rh" ); break;
  198. case vbr_mtrh: my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "vbr_mtrh" ); break;
  199. case vbr_abr:
  200. my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "vbr_abr (average bitrate %d kbps)", lame_get_VBR_mean_bitrate_kbps( gfp ) );
  201. break;
  202. default:
  203. my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "error, unknown VBR setting");
  204. break;
  205. }
  206. my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "Write VBR Header =%s\n", ( lame_get_bWriteVbrTag( gfp ) ) ?"Yes":"No");
  207. #ifdef FROM_DLL
  208. beConfig.format.LHV1.dwReSampleRate = my_OutBytesPerSec; // force the user resampling
  209. #endif // FROM_DLL
  210. bResult = true;
  211. return bResult;
  212. }
  213. bool ACMStream::close(LPBYTE pOutputBuffer, DWORD *pOutputSize)
  214. {
  215. bool bResult = false;
  216. int nOutputSamples = 0;
  217. nOutputSamples = lame_encode_flush( gfp, pOutputBuffer, 0 );
  218. if ( nOutputSamples < 0 )
  219. {
  220. // BUFFER_TOO_SMALL
  221. *pOutputSize = 0;
  222. }
  223. else
  224. {
  225. *pOutputSize = nOutputSamples;
  226. bResult = true;
  227. }
  228. // lame will be closed in destructor
  229. //lame_close( gfp );
  230. return bResult;
  231. }
  232. DWORD ACMStream::GetOutputSizeForInput(const DWORD the_SrcLength) const
  233. {
  234. /* double OutputInputRatio;
  235. if (my_VBRMode == vbr_off)
  236. OutputInputRatio = double(my_AvgBytesPerSec) / double(my_OutBytesPerSec * 2);
  237. else // reserve the space for 320 kbps
  238. OutputInputRatio = 40000.0 / double(my_OutBytesPerSec * 2);
  239. OutputInputRatio *= 1.15; // allow 15% more*/
  240. DWORD Result;
  241. // Result = DWORD(double(the_SrcLength) * OutputInputRatio);
  242. Result = DWORD(1.25*the_SrcLength + 7200);
  243. my_debug->OutPut(DEBUG_LEVEL_FUNC_CODE, "Result = %d",Result);
  244. return Result;
  245. }
  246. bool ACMStream::ConvertBuffer(LPACMDRVSTREAMHEADER a_StreamHeader)
  247. {
  248. bool result;
  249. if (my_debug != NULL)
  250. {
  251. my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "enter ACMStream::ConvertBuffer");
  252. }
  253. DWORD InSize = a_StreamHeader->cbSrcLength / 2, OutSize = a_StreamHeader->cbDstLength; // 2 for 8<->16 bits
  254. // Encode it
  255. int dwSamples;
  256. int nOutputSamples = 0;
  257. dwSamples = InSize / lame_get_num_channels( gfp );
  258. if ( 1 == lame_get_num_channels( gfp ) )
  259. {
  260. nOutputSamples = lame_encode_buffer(gfp,(PSHORT)a_StreamHeader->pbSrc,(PSHORT)a_StreamHeader->pbSrc,dwSamples,a_StreamHeader->pbDst,a_StreamHeader->cbDstLength);
  261. }
  262. else
  263. {
  264. nOutputSamples = lame_encode_buffer_interleaved(gfp,(PSHORT)a_StreamHeader->pbSrc,dwSamples,a_StreamHeader->pbDst,a_StreamHeader->cbDstLength);
  265. }
  266. a_StreamHeader->cbSrcLengthUsed = a_StreamHeader->cbSrcLength;
  267. a_StreamHeader->cbDstLengthUsed = nOutputSamples;
  268. result = a_StreamHeader->cbDstLengthUsed <= a_StreamHeader->cbDstLength;
  269. my_debug->OutPut(DEBUG_LEVEL_FUNC_CODE, "UsedSize = %d / EncodedSize = %d, result = %d (%d <= %d)", InSize, OutSize, result, a_StreamHeader->cbDstLengthUsed, a_StreamHeader->cbDstLength);
  270. if (my_debug != NULL)
  271. {
  272. my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "ACMStream::ConvertBuffer result = %d (0x%02X 0x%02X)",result,a_StreamHeader->pbDst[0],a_StreamHeader->pbDst[1]);
  273. }
  274. return result;
  275. }
  276. /* map frequency to a valid MP3 sample frequency
  277. *
  278. * Robert Hegemann 2000-07-01
  279. */
  280. static int
  281. map2MP3Frequency(int freq)
  282. {
  283. if (freq <= 8000)
  284. return 8000;
  285. if (freq <= 11025)
  286. return 11025;
  287. if (freq <= 12000)
  288. return 12000;
  289. if (freq <= 16000)
  290. return 16000;
  291. if (freq <= 22050)
  292. return 22050;
  293. if (freq <= 24000)
  294. return 24000;
  295. if (freq <= 32000)
  296. return 32000;
  297. if (freq <= 44100)
  298. return 44100;
  299. return 48000;
  300. }
  301. unsigned int ACMStream::GetOutputSampleRate(int samples_per_sec, int bitrate, int channels)
  302. {
  303. if (bitrate==0)
  304. bitrate = (64000*channels)/8;
  305. /// \todo pass through the same LAME routine
  306. unsigned int OutputFrequency;
  307. double compression_ratio = double(samples_per_sec * 16 * channels / (bitrate * 8));
  308. if (compression_ratio > 13.)
  309. OutputFrequency = map2MP3Frequency( (10. * bitrate * 8) / (16 * channels));
  310. else
  311. OutputFrequency = map2MP3Frequency( 0.97 * samples_per_sec );
  312. return OutputFrequency;
  313. }