Encoder.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /*
  2. * LAME MP3 encoder for DirectShow
  3. * LAME encoder wrapper
  4. *
  5. * Copyright (c) 2000-2005 Marie Orlova, Peter Gubanov, Vitaly Ivanov, Elecard Ltd.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Library General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 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. * Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public
  18. * License along with this library; if not, write to the
  19. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. * Boston, MA 02111-1307, USA.
  21. */
  22. #include <streams.h>
  23. #include "Encoder.h"
  24. //////////////////////////////////////////////////////////////////////
  25. // Construction/Destruction
  26. //////////////////////////////////////////////////////////////////////
  27. CEncoder::CEncoder() :
  28. m_bInpuTypeSet(FALSE),
  29. m_bOutpuTypeSet(FALSE),
  30. m_bFinished(FALSE),
  31. m_outOffset(0),
  32. m_outReadOffset(0),
  33. m_frameCount(0),
  34. pgf(NULL)
  35. {
  36. m_outFrameBuf = new unsigned char[OUT_BUFFER_SIZE];
  37. }
  38. CEncoder::~CEncoder()
  39. {
  40. Close(NULL);
  41. if (m_outFrameBuf)
  42. delete [] m_outFrameBuf;
  43. }
  44. //////////////////////////////////////////////////////////////////////
  45. // SetInputType - check if given input type is supported
  46. //////////////////////////////////////////////////////////////////////
  47. HRESULT CEncoder::SetInputType(LPWAVEFORMATEX lpwfex, bool bJustCheck)
  48. {
  49. CAutoLock l(&m_lock);
  50. if (lpwfex->wFormatTag == WAVE_FORMAT_PCM)
  51. {
  52. if (lpwfex->nChannels == 1 || lpwfex->nChannels == 2)
  53. {
  54. if (lpwfex->nSamplesPerSec == 48000 ||
  55. lpwfex->nSamplesPerSec == 44100 ||
  56. lpwfex->nSamplesPerSec == 32000 ||
  57. lpwfex->nSamplesPerSec == 24000 ||
  58. lpwfex->nSamplesPerSec == 22050 ||
  59. lpwfex->nSamplesPerSec == 16000 ||
  60. lpwfex->nSamplesPerSec == 12000 ||
  61. lpwfex->nSamplesPerSec == 11025 ||
  62. lpwfex->nSamplesPerSec == 8000)
  63. {
  64. if (lpwfex->wBitsPerSample == 16)
  65. {
  66. if (!bJustCheck)
  67. {
  68. memcpy(&m_wfex, lpwfex, sizeof(WAVEFORMATEX));
  69. m_bInpuTypeSet = true;
  70. }
  71. return S_OK;
  72. }
  73. }
  74. }
  75. }
  76. if (!bJustCheck)
  77. m_bInpuTypeSet = false;
  78. return E_INVALIDARG;
  79. }
  80. //////////////////////////////////////////////////////////////////////
  81. // SetOutputType - try to initialize encoder with given output type
  82. //////////////////////////////////////////////////////////////////////
  83. HRESULT CEncoder::SetOutputType(MPEG_ENCODER_CONFIG &mabsi)
  84. {
  85. CAutoLock l(&m_lock);
  86. m_mabsi = mabsi;
  87. m_bOutpuTypeSet = true;
  88. return S_OK;
  89. }
  90. //////////////////////////////////////////////////////////////////////
  91. // SetDefaultOutputType - sets default MPEG audio properties according
  92. // to input type
  93. //////////////////////////////////////////////////////////////////////
  94. HRESULT CEncoder::SetDefaultOutputType(LPWAVEFORMATEX lpwfex)
  95. {
  96. CAutoLock l(&m_lock);
  97. if(lpwfex->nChannels == 1 || m_mabsi.bForceMono)
  98. m_mabsi.ChMode = MONO;
  99. if((lpwfex->nSamplesPerSec < m_mabsi.dwSampleRate) || (lpwfex->nSamplesPerSec % m_mabsi.dwSampleRate != 0))
  100. m_mabsi.dwSampleRate = lpwfex->nSamplesPerSec;
  101. return S_OK;
  102. }
  103. //////////////////////////////////////////////////////////////////////
  104. // Init - initialized or reiniyialized encoder SDK with given input
  105. // and output settings
  106. //////////////////////////////////////////////////////////////////////
  107. HRESULT CEncoder::Init()
  108. {
  109. CAutoLock l(&m_lock);
  110. m_outOffset = 0;
  111. m_outReadOffset = 0;
  112. m_bFinished = FALSE;
  113. m_frameCount = 0;
  114. if (!pgf)
  115. {
  116. if (!m_bInpuTypeSet || !m_bOutpuTypeSet)
  117. return E_UNEXPECTED;
  118. // Init Lame library
  119. // note: newer, safer interface which doesn't
  120. // allow or require direct access to 'gf' struct is being written
  121. // see the file 'API' included with LAME.
  122. if (pgf = lame_init())
  123. {
  124. lame_set_num_channels(pgf, m_wfex.nChannels);
  125. lame_set_in_samplerate(pgf, m_wfex.nSamplesPerSec);
  126. lame_set_out_samplerate(pgf, m_mabsi.dwSampleRate);
  127. if ((lame_get_out_samplerate(pgf) >= 32000) && (m_mabsi.dwBitrate < 32))
  128. lame_set_brate(pgf, 32);
  129. else
  130. lame_set_brate(pgf, m_mabsi.dwBitrate);
  131. lame_set_VBR(pgf, m_mabsi.vmVariable);
  132. lame_set_VBR_min_bitrate_kbps(pgf, m_mabsi.dwVariableMin);
  133. lame_set_VBR_max_bitrate_kbps(pgf, m_mabsi.dwVariableMax);
  134. lame_set_copyright(pgf, m_mabsi.bCopyright);
  135. lame_set_original(pgf, m_mabsi.bOriginal);
  136. lame_set_error_protection(pgf, m_mabsi.bCRCProtect);
  137. lame_set_bWriteVbrTag(pgf, m_mabsi.dwXingTag);
  138. lame_set_strict_ISO(pgf, m_mabsi.dwStrictISO);
  139. lame_set_VBR_hard_min(pgf, m_mabsi.dwEnforceVBRmin);
  140. if (lame_get_num_channels(pgf) == 2 && !m_mabsi.bForceMono)
  141. {
  142. //int act_br = pgf->VBR ? pgf->VBR_min_bitrate_kbps + pgf->VBR_max_bitrate_kbps / 2 : pgf->brate;
  143. // Disabled. It's for user's consideration now
  144. //int rel = pgf->out_samplerate / (act_br + 1);
  145. //pgf->mode = rel < 200 ? m_mabsi.ChMode : JOINT_STEREO;
  146. lame_set_mode(pgf, m_mabsi.ChMode);
  147. }
  148. else
  149. lame_set_mode(pgf, MONO);
  150. if (lame_get_mode(pgf) == JOINT_STEREO)
  151. lame_set_force_ms(pgf, m_mabsi.dwForceMS);
  152. else
  153. lame_set_force_ms(pgf, 0);
  154. // pgf->mode_fixed = m_mabsi.dwModeFixed;
  155. if (m_mabsi.dwVoiceMode != 0)
  156. {
  157. lame_set_lowpassfreq(pgf,12000);
  158. ///pgf->VBR_max_bitrate_kbps = 160;
  159. }
  160. if (m_mabsi.dwKeepAllFreq != 0)
  161. {
  162. ///pgf->lowpassfreq = -1;
  163. ///pgf->highpassfreq = -1;
  164. /// not available anymore
  165. }
  166. lame_set_quality(pgf, m_mabsi.dwQuality);
  167. lame_set_VBR_q(pgf, m_mabsi.dwVBRq);
  168. lame_init_params(pgf);
  169. // encoder delay compensation
  170. {
  171. int const nch = lame_get_num_channels(pgf);
  172. short * start_padd = (short *)calloc(48, nch * sizeof(short));
  173. int out_bytes = 0;
  174. if (nch == 2)
  175. out_bytes = lame_encode_buffer_interleaved(pgf, start_padd, 48, m_outFrameBuf, OUT_BUFFER_SIZE);
  176. else
  177. out_bytes = lame_encode_buffer(pgf, start_padd, start_padd, 48, m_outFrameBuf, OUT_BUFFER_SIZE);
  178. if (out_bytes > 0)
  179. m_outOffset += out_bytes;
  180. free(start_padd);
  181. }
  182. return S_OK;
  183. }
  184. return E_FAIL;
  185. }
  186. return S_OK;
  187. }
  188. //////////////////////////////////////////////////////////////////////
  189. // Close - closes encoder
  190. //////////////////////////////////////////////////////////////////////
  191. HRESULT CEncoder::Close(IStream* pStream)
  192. {
  193. CAutoLock l(&m_lock);
  194. if (pgf)
  195. {
  196. if(lame_get_bWriteVbrTag(pgf) && pStream)
  197. {
  198. updateLameTagFrame(pStream);
  199. }
  200. lame_close(pgf);
  201. pgf = NULL;
  202. }
  203. return S_OK;
  204. }
  205. //////////////////////////////////////////////////////////////////////
  206. // Encode - encodes data placed on pdata and returns
  207. // the number of processed bytes
  208. //////////////////////////////////////////////////////////////////////
  209. int CEncoder::Encode(const short * pdata, int data_size)
  210. {
  211. CAutoLock l(&m_lock);
  212. if (!pgf || !m_outFrameBuf || !pdata || data_size < 0 || (data_size & (sizeof(short) - 1)))
  213. return -1;
  214. // some data left in the buffer, shift to start
  215. if (m_outReadOffset > 0)
  216. {
  217. if (m_outOffset > m_outReadOffset)
  218. memmove(m_outFrameBuf, m_outFrameBuf + m_outReadOffset, m_outOffset - m_outReadOffset);
  219. m_outOffset -= m_outReadOffset;
  220. }
  221. m_outReadOffset = 0;
  222. m_bFinished = FALSE;
  223. int bytes_processed = 0;
  224. int const nch = lame_get_num_channels(pgf);
  225. while (1)
  226. {
  227. int nsamples = (data_size - bytes_processed) / (sizeof(short) * nch);
  228. if (nsamples <= 0)
  229. break;
  230. if (nsamples > 1152)
  231. nsamples = 1152;
  232. if (m_outOffset >= OUT_BUFFER_MAX)
  233. break;
  234. int out_bytes = 0;
  235. if (nch == 2)
  236. out_bytes = lame_encode_buffer_interleaved(
  237. pgf,
  238. (short *)(pdata + (bytes_processed / sizeof(short))),
  239. nsamples,
  240. m_outFrameBuf + m_outOffset,
  241. OUT_BUFFER_SIZE - m_outOffset);
  242. else
  243. out_bytes = lame_encode_buffer(
  244. pgf,
  245. pdata + (bytes_processed / sizeof(short)),
  246. pdata + (bytes_processed / sizeof(short)),
  247. nsamples,
  248. m_outFrameBuf + m_outOffset,
  249. OUT_BUFFER_SIZE - m_outOffset);
  250. if (out_bytes < 0)
  251. return -1;
  252. m_outOffset += out_bytes;
  253. bytes_processed += nsamples * nch * sizeof(short);
  254. }
  255. return bytes_processed;
  256. }
  257. //
  258. // Finsh - flush the buffered samples
  259. //
  260. HRESULT CEncoder::Finish()
  261. {
  262. CAutoLock l(&m_lock);
  263. if (!pgf || !m_outFrameBuf || (m_outOffset >= OUT_BUFFER_MAX))
  264. return E_FAIL;
  265. m_outOffset += lame_encode_flush(pgf, m_outFrameBuf + m_outOffset, OUT_BUFFER_SIZE - m_outOffset);
  266. m_bFinished = TRUE;
  267. return S_OK;
  268. }
  269. int getFrameLength(const unsigned char * pdata)
  270. {
  271. if (!pdata || pdata[0] != 0xff || (pdata[1] & 0xe0) != 0xe0)
  272. return -1;
  273. const int sample_rate_tab[4][4] =
  274. {
  275. {11025,12000,8000,1},
  276. {1,1,1,1},
  277. {22050,24000,16000,1},
  278. {44100,48000,32000,1}
  279. };
  280. #define MPEG_VERSION_RESERVED 1
  281. #define MPEG_VERSION_1 3
  282. #define LAYER_III 1
  283. #define BITRATE_FREE 0
  284. #define BITRATE_RESERVED 15
  285. #define SRATE_RESERVED 3
  286. #define EMPHASIS_RESERVED 2
  287. int version_id = (pdata[1] & 0x18) >> 3;
  288. int layer = (pdata[1] & 0x06) >> 1;
  289. int bitrate_id = (pdata[2] & 0xF0) >> 4;
  290. int sample_rate_id = (pdata[2] & 0x0C) >> 2;
  291. int padding = (pdata[2] & 0x02) >> 1;
  292. int emphasis = pdata[3] & 0x03;
  293. if (version_id != MPEG_VERSION_RESERVED &&
  294. layer == LAYER_III &&
  295. bitrate_id != BITRATE_FREE &&
  296. bitrate_id != BITRATE_RESERVED &&
  297. sample_rate_id != SRATE_RESERVED &&
  298. emphasis != EMPHASIS_RESERVED)
  299. {
  300. int spf = (version_id == MPEG_VERSION_1) ? 1152 : 576;
  301. int sample_rate = sample_rate_tab[version_id][sample_rate_id];
  302. int bitrate = dwBitRateValue[version_id != MPEG_VERSION_1][bitrate_id - 1] * 1000;
  303. return (bitrate * spf) / (8 * sample_rate) + padding;
  304. }
  305. return -1;
  306. }
  307. int CEncoder::GetFrame(const unsigned char ** pframe)
  308. {
  309. if (!pgf || !m_outFrameBuf || !pframe)
  310. return -1;
  311. while ((m_outOffset - m_outReadOffset) > 4)
  312. {
  313. int frame_length = getFrameLength(m_outFrameBuf + m_outReadOffset);
  314. if (frame_length < 0)
  315. {
  316. m_outReadOffset++;
  317. }
  318. else if (frame_length <= (m_outOffset - m_outReadOffset))
  319. {
  320. *pframe = m_outFrameBuf + m_outReadOffset;
  321. m_outReadOffset += frame_length;
  322. m_frameCount++;
  323. // don't deliver the first and the last frames
  324. if (m_frameCount != 1 && !(m_bFinished && (m_outOffset - m_outReadOffset) < 5))
  325. return frame_length;
  326. }
  327. else
  328. break;
  329. }
  330. return 0;
  331. }
  332. ////////////////////////////////////////////////////////////////////////////////
  333. // Returns block of a mp3 file, witch size integer multiples of cbAlign
  334. // or not aligned if finished
  335. ////////////////////////////////////////////////////////////////////////////////
  336. int CEncoder::GetBlockAligned(const unsigned char ** pblock, int* piBufferSize, const long& cbAlign)
  337. {
  338. ASSERT(piBufferSize);
  339. if (!pgf || !m_outFrameBuf || !pblock)
  340. return -1;
  341. int iBlockLen = m_outOffset - m_outReadOffset;
  342. ASSERT(iBlockLen >= 0);
  343. if(!m_bFinished)
  344. {
  345. if(cbAlign > 0)
  346. iBlockLen-=iBlockLen%cbAlign;
  347. *piBufferSize = iBlockLen;
  348. }
  349. else
  350. {
  351. if(cbAlign && iBlockLen%cbAlign)
  352. {
  353. *piBufferSize = iBlockLen + cbAlign - iBlockLen%cbAlign;
  354. }
  355. else
  356. {
  357. *piBufferSize = iBlockLen;
  358. }
  359. }
  360. if(iBlockLen) {
  361. *pblock = m_outFrameBuf + m_outReadOffset;
  362. m_outReadOffset+=iBlockLen;
  363. }
  364. return iBlockLen;
  365. }
  366. HRESULT CEncoder::maybeSyncWord(IStream *pStream)
  367. {
  368. HRESULT hr = S_OK;
  369. unsigned char mp3_frame_header[4];
  370. ULONG nbytes;
  371. if(FAILED(hr = pStream->Read(mp3_frame_header, sizeof(mp3_frame_header), &nbytes)))
  372. return hr;
  373. if ( nbytes != sizeof(mp3_frame_header) ) {
  374. return E_FAIL;
  375. }
  376. if ( mp3_frame_header[0] != 0xffu ) {
  377. return S_FALSE; /* doesn't look like a sync word */
  378. }
  379. if ( (mp3_frame_header[1] & 0xE0u) != 0xE0u ) {
  380. return S_FALSE; /* doesn't look like a sync word */
  381. }
  382. return S_OK;
  383. }
  384. HRESULT CEncoder::skipId3v2(IStream *pStream, size_t lametag_frame_size)
  385. {
  386. HRESULT hr = S_OK;
  387. ULONG nbytes;
  388. size_t id3v2TagSize = 0;
  389. unsigned char id3v2Header[10];
  390. LARGE_INTEGER seekTo;
  391. /* seek to the beginning of the stream */
  392. seekTo.QuadPart = 0;
  393. if (FAILED(hr = pStream->Seek(seekTo, STREAM_SEEK_SET, NULL))) {
  394. return hr; /* not seekable, abort */
  395. }
  396. /* read 10 bytes in case there's an ID3 version 2 header here */
  397. hr = pStream->Read(id3v2Header, sizeof(id3v2Header), &nbytes);
  398. if (FAILED(hr))
  399. return hr;
  400. if(nbytes != sizeof(id3v2Header)) {
  401. return E_FAIL; /* not readable, maybe opened Write-Only */
  402. }
  403. /* does the stream begin with the ID3 version 2 file identifier? */
  404. if (!strncmp((char *) id3v2Header, "ID3", 3)) {
  405. /* the tag size (minus the 10-byte header) is encoded into four
  406. * bytes where the most significant bit is clear in each byte
  407. */
  408. id3v2TagSize = (((id3v2Header[6] & 0x7f) << 21)
  409. | ((id3v2Header[7] & 0x7f) << 14)
  410. | ((id3v2Header[8] & 0x7f) << 7)
  411. | (id3v2Header[9] & 0x7f))
  412. + sizeof id3v2Header;
  413. }
  414. /* Seek to the beginning of the audio stream */
  415. seekTo.QuadPart = id3v2TagSize;
  416. if (FAILED(hr = pStream->Seek(seekTo, STREAM_SEEK_SET, NULL))) {
  417. return hr;
  418. }
  419. if (S_OK != (hr = maybeSyncWord(pStream))) {
  420. return SUCCEEDED(hr)?E_FAIL:hr;
  421. }
  422. seekTo.QuadPart = id3v2TagSize+lametag_frame_size;
  423. if (FAILED(hr = pStream->Seek(seekTo, STREAM_SEEK_SET, NULL))) {
  424. return hr;
  425. }
  426. if (S_OK != (hr = maybeSyncWord(pStream))) {
  427. return SUCCEEDED(hr)?E_FAIL:hr;
  428. }
  429. /* OK, it seems we found our LAME-Tag/Xing frame again */
  430. /* Seek to the beginning of the audio stream */
  431. seekTo.QuadPart = id3v2TagSize;
  432. if (FAILED(hr = pStream->Seek(seekTo, STREAM_SEEK_SET, NULL))) {
  433. return hr;
  434. }
  435. return S_OK;
  436. }
  437. // Updates VBR tag
  438. HRESULT CEncoder::updateLameTagFrame(IStream* pStream)
  439. {
  440. HRESULT hr = S_OK;
  441. size_t n = lame_get_lametag_frame( pgf, 0, 0 ); /* ask for bufer size */
  442. if ( n > 0 )
  443. {
  444. unsigned char* buffer = 0;
  445. ULONG m = n;
  446. if ( FAILED(hr = skipId3v2(pStream, n) ))
  447. {
  448. /*DispErr( "Error updating LAME-tag frame:\n\n"
  449. "can't locate old frame\n" );*/
  450. return hr;
  451. }
  452. buffer = (unsigned char*)malloc( n );
  453. if ( buffer == 0 )
  454. {
  455. /*DispErr( "Error updating LAME-tag frame:\n\n"
  456. "can't allocate frame buffer\n" );*/
  457. return E_OUTOFMEMORY;
  458. }
  459. /* Put it all to disk again */
  460. n = lame_get_lametag_frame( pgf, buffer, n );
  461. if ( n > 0 )
  462. {
  463. hr = pStream->Write(buffer, n, &m);
  464. }
  465. free( buffer );
  466. if ( m != n )
  467. {
  468. /*DispErr( "Error updating LAME-tag frame:\n\n"
  469. "couldn't write frame into file\n" );*/
  470. return E_FAIL;
  471. }
  472. }
  473. return hr;
  474. }