Mpegac.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * LAME MP3 encoder for DirectShow
  3. * DirectShow filter implementation
  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 <mmreg.h>
  23. #include "Encoder.h"
  24. #define KEY_LAME_ENCODER "SOFTWARE\\GNU\\LAME MPEG Layer III Audio Encoder Filter"
  25. #define VALUE_BITRATE "Bitrate"
  26. #define VALUE_VARIABLE "Variable"
  27. #define VALUE_VARIABLEMIN "VariableMin"
  28. #define VALUE_VARIABLEMAX "VariableMax"
  29. #define VALUE_QUALITY "Quality"
  30. #define VALUE_VBR_QUALITY "VBR Quality"
  31. #define VALUE_SAMPLE_RATE "Sample Rate"
  32. #define VALUE_STEREO_MODE "Stereo Mode"
  33. #define VALUE_FORCE_MS "Force MS"
  34. #define VALUE_LAYER "Layer"
  35. #define VALUE_ORIGINAL "Original"
  36. #define VALUE_COPYRIGHT "Copyright"
  37. #define VALUE_CRC "CRC"
  38. #define VALUE_FORCE_MONO "Force Mono"
  39. #define VALUE_SET_DURATION "Set Duration"
  40. #define VALUE_SAMPLE_OVERLAP "Allow sample overlap"
  41. #define VALUE_PES "PES"
  42. #define VALUE_ENFORCE_MIN "EnforceVBRmin"
  43. #define VALUE_VOICE "Voice Mode"
  44. #define VALUE_KEEP_ALL_FREQ "Keep All Frequencies"
  45. #define VALUE_STRICT_ISO "Strict ISO"
  46. #define VALUE_DISABLE_SHORT_BLOCK "No Short Block"
  47. #define VALUE_XING_TAG "Xing Tag"
  48. #define VALUE_MODE_FIXED "Mode Fixed"
  49. typedef struct
  50. {
  51. DWORD nSampleRate;
  52. DWORD nBitRate;
  53. MPEG_mode ChMode; //Channel coding mode
  54. } current_output_format_t;
  55. typedef struct
  56. {
  57. DWORD nSampleRate;
  58. DWORD nBitRate;
  59. } output_caps_t;
  60. typedef struct
  61. {
  62. LONGLONG sample;
  63. REFERENCE_TIME delta;
  64. BOOL applied;
  65. } resync_point_t;
  66. #define RESYNC_COUNT 4
  67. // The maximum number of capabilities that we can expose in our IAMStreamConfig
  68. // implementation is currently set to 100. This number is larger than we
  69. // should ever realistically need. However, a cleaner implementation might
  70. // be to use a dynamically sized array like std::vector or CAtlArray to
  71. // hold this data.
  72. #define MAX_IAMSTREAMCONFIG_CAPS 100
  73. ///////////////////////////////////////////////////////////////////
  74. // CMpegAudEnc class - implementation for ITransformFilter interface
  75. ///////////////////////////////////////////////////////////////////
  76. class CMpegAudEncOutPin;
  77. class CMpegAudEncPropertyPage;
  78. class CMpegAudEnc : public CTransformFilter,
  79. public ISpecifyPropertyPages,
  80. public IAudioEncoderProperties,
  81. public CPersistStream
  82. {
  83. public:
  84. DECLARE_IUNKNOWN
  85. static CUnknown *CreateInstance(LPUNKNOWN lpunk, HRESULT *phr);
  86. LPAMOVIESETUP_FILTER GetSetupData();
  87. HRESULT Reconnect();
  88. HRESULT Receive(IMediaSample *pSample);
  89. HRESULT CheckInputType(const CMediaType* mtIn);
  90. HRESULT CheckTransform(const CMediaType* mtIn, const CMediaType* mtOut);
  91. HRESULT DecideBufferSize(IMemAllocator * pAllocator, ALLOCATOR_PROPERTIES *pprop);
  92. HRESULT GetMediaType (int iPosition, CMediaType *pMediaType);
  93. HRESULT SetMediaType (PIN_DIRECTION direction,const CMediaType *pmt);
  94. //
  95. HRESULT StartStreaming();
  96. HRESULT StopStreaming();
  97. HRESULT EndOfStream();
  98. HRESULT BeginFlush();
  99. ~CMpegAudEnc(void);
  100. // ISpecifyPropertyPages
  101. STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, void ** ppv);
  102. STDMETHODIMP GetPages(CAUUID *pPages);
  103. // IAudioEncoderProperties
  104. STDMETHODIMP get_PESOutputEnabled(DWORD *dwEnabled); // PES header. Obsolete
  105. STDMETHODIMP set_PESOutputEnabled(DWORD dwEnabled); // PES header. Obsolete
  106. STDMETHODIMP get_MPEGLayer(DWORD *dwLayer);
  107. STDMETHODIMP set_MPEGLayer(DWORD dwLayer);
  108. STDMETHODIMP get_Bitrate(DWORD *dwBitrate);
  109. STDMETHODIMP set_Bitrate(DWORD dwBitrate);
  110. STDMETHODIMP get_Variable(DWORD *dwVariable);
  111. STDMETHODIMP set_Variable(DWORD dwVariable);
  112. STDMETHODIMP get_VariableMin(DWORD *dwMin);
  113. STDMETHODIMP set_VariableMin(DWORD dwMin);
  114. STDMETHODIMP get_VariableMax(DWORD *dwMax);
  115. STDMETHODIMP set_VariableMax(DWORD dwMax);
  116. STDMETHODIMP get_Quality(DWORD *dwQuality);
  117. STDMETHODIMP set_Quality(DWORD dwQuality);
  118. STDMETHODIMP get_VariableQ(DWORD *dwVBRq);
  119. STDMETHODIMP set_VariableQ(DWORD dwVBRq);
  120. STDMETHODIMP get_SourceSampleRate(DWORD *dwSampleRate);
  121. STDMETHODIMP get_SourceChannels(DWORD *dwChannels);
  122. STDMETHODIMP get_SampleRate(DWORD *dwSampleRate);
  123. STDMETHODIMP set_SampleRate(DWORD dwSampleRate);
  124. STDMETHODIMP get_ChannelMode(DWORD *dwChannelMode);
  125. STDMETHODIMP set_ChannelMode(DWORD dwChannelMode);
  126. STDMETHODIMP get_ForceMS(DWORD *dwFlag);
  127. STDMETHODIMP set_ForceMS(DWORD dwFlag);
  128. STDMETHODIMP get_EnforceVBRmin(DWORD *dwFlag);
  129. STDMETHODIMP set_EnforceVBRmin(DWORD dwFlag);
  130. STDMETHODIMP get_VoiceMode(DWORD *dwFlag);
  131. STDMETHODIMP set_VoiceMode(DWORD dwFlag);
  132. STDMETHODIMP get_KeepAllFreq(DWORD *dwFlag);
  133. STDMETHODIMP set_KeepAllFreq(DWORD dwFlag);
  134. STDMETHODIMP get_StrictISO(DWORD *dwFlag);
  135. STDMETHODIMP set_StrictISO(DWORD dwFlag);
  136. STDMETHODIMP get_NoShortBlock(DWORD *dwNoShortBlock);
  137. STDMETHODIMP set_NoShortBlock(DWORD dwNoShortBlock);
  138. STDMETHODIMP get_XingTag(DWORD *dwXingTag);
  139. STDMETHODIMP set_XingTag(DWORD dwXingTag);
  140. STDMETHODIMP get_ModeFixed(DWORD *dwModeFixed);
  141. STDMETHODIMP set_ModeFixed(DWORD dwModeFixed);
  142. STDMETHODIMP get_CRCFlag(DWORD *dwFlag);
  143. STDMETHODIMP set_CRCFlag(DWORD dwFlag);
  144. STDMETHODIMP get_ForceMono(DWORD *dwFlag);
  145. STDMETHODIMP set_ForceMono(DWORD dwFlag);
  146. STDMETHODIMP get_SetDuration(DWORD *dwFlag);
  147. STDMETHODIMP set_SetDuration(DWORD dwFlag);
  148. STDMETHODIMP get_OriginalFlag(DWORD *dwFlag);
  149. STDMETHODIMP set_OriginalFlag(DWORD dwFlag);
  150. STDMETHODIMP get_CopyrightFlag(DWORD *dwFlag);
  151. STDMETHODIMP set_CopyrightFlag(DWORD dwFlag);
  152. STDMETHODIMP get_SampleOverlap(DWORD *dwFlag);
  153. STDMETHODIMP set_SampleOverlap(DWORD dwFlag);
  154. STDMETHODIMP get_ParameterBlockSize(BYTE *pcBlock, DWORD *pdwSize);
  155. STDMETHODIMP set_ParameterBlockSize(BYTE *pcBlock, DWORD dwSize);
  156. STDMETHODIMP DefaultAudioEncoderProperties();
  157. STDMETHODIMP LoadAudioEncoderPropertiesFromRegistry();
  158. STDMETHODIMP SaveAudioEncoderPropertiesToRegistry();
  159. STDMETHODIMP InputTypeDefined();
  160. STDMETHODIMP ApplyChanges();
  161. // CPersistStream
  162. HRESULT WriteToStream(IStream *pStream);
  163. HRESULT ReadFromStream(IStream *pStream);
  164. int SizeMax();
  165. STDMETHODIMP GetClassID(CLSID *pClsid);
  166. private:
  167. CMpegAudEnc(LPUNKNOWN lpunk, HRESULT *phr);
  168. HRESULT FlushEncodedSamples();
  169. void ReadPresetSettings(MPEG_ENCODER_CONFIG *pmabsi);
  170. void LoadOutputCapabilities(DWORD sample_rate);
  171. // Encoder object
  172. CEncoder m_Encoder;
  173. REFERENCE_TIME m_rtStreamTime;
  174. REFERENCE_TIME m_rtFrameTime;
  175. REFERENCE_TIME m_rtEstimated;
  176. // Synchronization data
  177. LONGLONG m_samplesIn;
  178. LONGLONG m_samplesOut;
  179. int m_samplesPerFrame;
  180. int m_bytesPerSample;
  181. float m_bytesToDuration;
  182. resync_point_t m_sync[RESYNC_COUNT];
  183. int m_sync_in_idx;
  184. int m_sync_out_idx;
  185. BOOL m_hasFinished;
  186. CCritSec m_cs;
  187. DWORD m_setDuration;
  188. DWORD m_allowOverlap;
  189. REFERENCE_TIME m_rtBytePos;
  190. BOOL m_bStreamOutput; // Binary stream output
  191. long m_cbStreamAlignment; // Stream block size
  192. int m_CapsNum;
  193. int m_currentMediaTypeIndex;
  194. output_caps_t OutputCaps[MAX_IAMSTREAMCONFIG_CAPS];
  195. protected:
  196. friend class CMpegAudEncOutPin;
  197. friend class CMpegAudEncPropertyPage;
  198. };
  199. class CMpegAudEncOutPin : public CTransformOutputPin, public IAMStreamConfig
  200. {
  201. public:
  202. //////////////////////////////////////////////////////////////////////////
  203. // IUnknown
  204. //////////////////////////////////////////////////////////////////////////
  205. DECLARE_IUNKNOWN
  206. STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, void ** ppv);
  207. //////////////////////////////////////////////////////////////////////////
  208. // IAMStreamConfig
  209. //////////////////////////////////////////////////////////////////////////
  210. HRESULT STDMETHODCALLTYPE SetFormat(AM_MEDIA_TYPE *pmt);
  211. HRESULT STDMETHODCALLTYPE GetFormat(AM_MEDIA_TYPE **ppmt);
  212. HRESULT STDMETHODCALLTYPE GetNumberOfCapabilities(int *piCount, int *piSize);
  213. HRESULT STDMETHODCALLTYPE GetStreamCaps(int iIndex, AM_MEDIA_TYPE **pmt, BYTE *pSCC);
  214. //////////////////////////////////////////////////////////////////////////
  215. // CTransformOutputPin
  216. //////////////////////////////////////////////////////////////////////////
  217. CMpegAudEncOutPin( CMpegAudEnc * pFilter, HRESULT * pHr );
  218. ~CMpegAudEncOutPin();
  219. HRESULT CheckMediaType(const CMediaType *pmtOut);
  220. HRESULT GetMediaType(int iPosition, CMediaType *pmt);
  221. HRESULT SetMediaType(const CMediaType *pmt);
  222. private:
  223. BOOL m_SetFormat;
  224. CMpegAudEnc *m_pFilter;
  225. current_output_format_t m_CurrentOutputFormat;
  226. protected:
  227. friend class CMpegAudEnc;
  228. };