Encoder.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. #if !defined(AFX_VITECENCODER_H__40DC8A44_B937_11D2_A381_A2FD7C37FA15__INCLUDED_)
  23. #define AFX_VITECENCODER_H__40DC8A44_B937_11D2_A381_A2FD7C37FA15__INCLUDED_
  24. #if _MSC_VER >= 1000
  25. #pragma once
  26. #endif // _MSC_VER >= 1000
  27. #include <lame.h>
  28. const unsigned int dwBitRateValue[2][14] =
  29. {
  30. {32,40,48,56,64,80,96,112,128,160,192,224,256,320}, // MPEG-1
  31. {8,16,24,32,40,48,56,64,80,96,112,128,144,160} // MPEG-2/2.5
  32. };
  33. /*
  34. #define STEREO 0
  35. #define JOINT_STEREO 1
  36. #define DUAL_CHANNEL 2
  37. #define MONO 3
  38. */
  39. #define OUT_BUFFER_SIZE 16384
  40. #define OUT_BUFFER_GUARD 8192
  41. #define OUT_BUFFER_MAX (OUT_BUFFER_SIZE - OUT_BUFFER_GUARD)
  42. typedef struct {
  43. DWORD dwSampleRate; //SF in Hz
  44. DWORD dwBitrate; //BR in bit per second
  45. vbr_mode vmVariable;
  46. DWORD dwVariableMin; //specify a minimum allowed bitrate
  47. DWORD dwVariableMax; //specify a maximum allowed bitrate
  48. DWORD dwQuality; //Encoding quality
  49. DWORD dwVBRq; // VBR quality setting (0=highest quality, 9=lowest)
  50. long lLayer; //Layer: 1 or 2
  51. MPEG_mode ChMode; //Channel coding mode: see doc
  52. DWORD dwForceMS;
  53. DWORD bCRCProtect; //Is CRC protection activated?
  54. DWORD bForceMono;
  55. DWORD bSetDuration;
  56. DWORD bCopyright; //Is the stream protected by copyright?
  57. DWORD bOriginal; //Is the stream an original?
  58. DWORD dwPES; // PES header. Obsolete
  59. DWORD dwEnforceVBRmin;
  60. DWORD dwVoiceMode;
  61. DWORD dwKeepAllFreq;
  62. DWORD dwStrictISO;
  63. DWORD dwNoShortBlock;
  64. DWORD dwXingTag;
  65. DWORD dwModeFixed;
  66. DWORD bSampleOverlap;
  67. } MPEG_ENCODER_CONFIG;
  68. class CEncoder
  69. {
  70. public:
  71. CEncoder();
  72. virtual ~CEncoder();
  73. // Initialize encoder with PCM stream properties
  74. HRESULT SetInputType(LPWAVEFORMATEX lpwfex, bool bJustCheck = FALSE); // returns E_INVALIDARG if not supported
  75. // GetInputType - returns current input type
  76. HRESULT GetInputType(WAVEFORMATEX *pwfex)
  77. {
  78. if(m_bInpuTypeSet)
  79. {
  80. memcpy(pwfex, &m_wfex, sizeof(WAVEFORMATEX));
  81. return S_OK;
  82. }
  83. else
  84. return E_UNEXPECTED;
  85. }
  86. // Set MPEG audio parameters
  87. HRESULT SetOutputType(MPEG_ENCODER_CONFIG &mabsi); // returns E_INVALIDARG if not supported or
  88. // not compatible with input type
  89. // Return current MPEG audio settings
  90. HRESULT GetOutputType(MPEG_ENCODER_CONFIG* pmabsi)
  91. {
  92. if (m_bOutpuTypeSet)
  93. {
  94. memcpy(pmabsi, &m_mabsi, sizeof(MPEG_ENCODER_CONFIG));
  95. return S_OK;
  96. }
  97. else
  98. return E_UNEXPECTED;
  99. }
  100. // Set if output stream is a PES. Obsolete
  101. void SetPES(bool bPES)
  102. {
  103. m_mabsi.dwPES = false;//bPES;
  104. }
  105. // Is output stream a PES. Obsolete
  106. BOOL IsPES() const
  107. {
  108. return (BOOL)m_mabsi.dwPES;
  109. }
  110. // Initialize encoder SDK
  111. HRESULT Init();
  112. // Close encoder SDK
  113. HRESULT Close(IStream* pStream);
  114. // Encode media sample data
  115. int Encode(const short * pdata, int data_size);
  116. int GetFrame(const unsigned char ** pframe);
  117. // Returns block of a mp3 file, witch size integer multiples of cbAlign
  118. int GetBlockAligned(const unsigned char ** pblock, int* piBufferSize, const long& cbAlign);
  119. HRESULT Finish();
  120. protected:
  121. HRESULT updateLameTagFrame(IStream* pStream);
  122. HRESULT skipId3v2(IStream *pStream, size_t lametag_frame_size);
  123. HRESULT maybeSyncWord(IStream *pStream);
  124. HRESULT SetDefaultOutputType(LPWAVEFORMATEX lpwfex);
  125. // Input media type
  126. WAVEFORMATEX m_wfex;
  127. // Output media type
  128. MPEG_ENCODER_CONFIG m_mabsi;
  129. // Compressor private data
  130. lame_global_flags * pgf;
  131. // Compressor miscelaneous state
  132. BOOL m_bInpuTypeSet;
  133. BOOL m_bOutpuTypeSet;
  134. BOOL m_bFinished;
  135. int m_frameCount;
  136. unsigned char * m_outFrameBuf;
  137. int m_outOffset;
  138. int m_outReadOffset;
  139. CCritSec m_lock;
  140. };
  141. #endif // !defined(AFX_VITECENCODER_H__40DC8A44_B937_11D2_A381_A2FD7C37FA15__INCLUDED_)