transfrm.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. //------------------------------------------------------------------------------
  2. // File: Transfrm.h
  3. //
  4. // Desc: DirectShow base classes - defines classes from which simple
  5. // transform codecs may be derived.
  6. //
  7. // Copyright (c) 1992-2001 Microsoft Corporation. All rights reserved.
  8. //------------------------------------------------------------------------------
  9. // It assumes the codec has one input and one output stream, and has no
  10. // interest in memory management, interface negotiation or anything else.
  11. //
  12. // derive your class from this, and supply Transform and the media type/format
  13. // negotiation functions. Implement that class, compile and link and
  14. // you're done.
  15. #ifndef __TRANSFRM__
  16. #define __TRANSFRM__
  17. // ======================================================================
  18. // This is the com object that represents a simple transform filter. It
  19. // supports IBaseFilter, IMediaFilter and two pins through nested interfaces
  20. // ======================================================================
  21. class CTransformFilter;
  22. // ==================================================
  23. // Implements the input pin
  24. // ==================================================
  25. class CTransformInputPin : public CBaseInputPin
  26. {
  27. friend class CTransformFilter;
  28. protected:
  29. CTransformFilter *m_pTransformFilter;
  30. public:
  31. CTransformInputPin(
  32. __in_opt LPCTSTR pObjectName,
  33. __inout CTransformFilter *pTransformFilter,
  34. __inout HRESULT * phr,
  35. __in_opt LPCWSTR pName);
  36. #ifdef UNICODE
  37. CTransformInputPin(
  38. __in_opt LPCSTR pObjectName,
  39. __inout CTransformFilter *pTransformFilter,
  40. __inout HRESULT * phr,
  41. __in_opt LPCWSTR pName);
  42. #endif
  43. STDMETHODIMP QueryId(__deref_out LPWSTR * Id) {
  44. return AMGetWideString(L"In", Id);
  45. }
  46. // Grab and release extra interfaces if required
  47. HRESULT CheckConnect(IPin *pPin);
  48. HRESULT BreakConnect();
  49. HRESULT CompleteConnect(IPin *pReceivePin);
  50. // check that we can support this output type
  51. HRESULT CheckMediaType(const CMediaType* mtIn);
  52. // set the connection media type
  53. HRESULT SetMediaType(const CMediaType* mt);
  54. // --- IMemInputPin -----
  55. // here's the next block of data from the stream.
  56. // AddRef it yourself if you need to hold it beyond the end
  57. // of this call.
  58. STDMETHODIMP Receive(IMediaSample * pSample);
  59. // provide EndOfStream that passes straight downstream
  60. // (there is no queued data)
  61. STDMETHODIMP EndOfStream(void);
  62. // passes it to CTransformFilter::BeginFlush
  63. STDMETHODIMP BeginFlush(void);
  64. // passes it to CTransformFilter::EndFlush
  65. STDMETHODIMP EndFlush(void);
  66. STDMETHODIMP NewSegment(
  67. REFERENCE_TIME tStart,
  68. REFERENCE_TIME tStop,
  69. double dRate);
  70. // Check if it's OK to process samples
  71. virtual HRESULT CheckStreaming();
  72. // Media type
  73. public:
  74. CMediaType& CurrentMediaType() {
  75. return m_mt;
  76. };
  77. };
  78. // ==================================================
  79. // Implements the output pin
  80. // ==================================================
  81. class CTransformOutputPin : public CBaseOutputPin
  82. {
  83. friend class CTransformFilter;
  84. protected:
  85. CTransformFilter *m_pTransformFilter;
  86. public:
  87. // implement IMediaPosition by passing upstream
  88. IUnknown * m_pPosition;
  89. CTransformOutputPin(
  90. __in_opt LPCTSTR pObjectName,
  91. __inout CTransformFilter *pTransformFilter,
  92. __inout HRESULT * phr,
  93. __in_opt LPCWSTR pName);
  94. #ifdef UNICODE
  95. CTransformOutputPin(
  96. __in_opt LPCSTR pObjectName,
  97. __inout CTransformFilter *pTransformFilter,
  98. __inout HRESULT * phr,
  99. __in_opt LPCWSTR pName);
  100. #endif
  101. ~CTransformOutputPin();
  102. // override to expose IMediaPosition
  103. STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, __deref_out void **ppv);
  104. // --- CBaseOutputPin ------------
  105. STDMETHODIMP QueryId(__deref_out LPWSTR * Id) {
  106. return AMGetWideString(L"Out", Id);
  107. }
  108. // Grab and release extra interfaces if required
  109. HRESULT CheckConnect(IPin *pPin);
  110. HRESULT BreakConnect();
  111. HRESULT CompleteConnect(IPin *pReceivePin);
  112. // check that we can support this output type
  113. HRESULT CheckMediaType(const CMediaType* mtOut);
  114. // set the connection media type
  115. HRESULT SetMediaType(const CMediaType *pmt);
  116. // called from CBaseOutputPin during connection to ask for
  117. // the count and size of buffers we need.
  118. HRESULT DecideBufferSize(
  119. IMemAllocator * pAlloc,
  120. __inout ALLOCATOR_PROPERTIES *pProp);
  121. // returns the preferred formats for a pin
  122. HRESULT GetMediaType(int iPosition, __inout CMediaType *pMediaType);
  123. // inherited from IQualityControl via CBasePin
  124. STDMETHODIMP Notify(IBaseFilter * pSender, Quality q);
  125. // Media type
  126. public:
  127. CMediaType& CurrentMediaType() {
  128. return m_mt;
  129. };
  130. };
  131. class AM_NOVTABLE CTransformFilter : public CBaseFilter
  132. {
  133. public:
  134. // map getpin/getpincount for base enum of pins to owner
  135. // override this to return more specialised pin objects
  136. virtual int GetPinCount();
  137. virtual CBasePin * GetPin(int n);
  138. STDMETHODIMP FindPin(LPCWSTR Id, __deref_out IPin **ppPin);
  139. // override state changes to allow derived transform filter
  140. // to control streaming start/stop
  141. STDMETHODIMP Stop();
  142. STDMETHODIMP Pause();
  143. public:
  144. CTransformFilter(__in_opt LPCTSTR , __inout_opt LPUNKNOWN, REFCLSID clsid);
  145. #ifdef UNICODE
  146. CTransformFilter(__in_opt LPCSTR , __inout_opt LPUNKNOWN, REFCLSID clsid);
  147. #endif
  148. ~CTransformFilter();
  149. // =================================================================
  150. // ----- override these bits ---------------------------------------
  151. // =================================================================
  152. // These must be supplied in a derived class
  153. virtual HRESULT Transform(IMediaSample * pIn, IMediaSample *pOut);
  154. // check if you can support mtIn
  155. virtual HRESULT CheckInputType(const CMediaType* mtIn) PURE;
  156. // check if you can support the transform from this input to this output
  157. virtual HRESULT CheckTransform(const CMediaType* mtIn, const CMediaType* mtOut) PURE;
  158. // this goes in the factory template table to create new instances
  159. // static CCOMObject * CreateInstance(__inout_opt LPUNKNOWN, HRESULT *);
  160. // call the SetProperties function with appropriate arguments
  161. virtual HRESULT DecideBufferSize(
  162. IMemAllocator * pAllocator,
  163. __inout ALLOCATOR_PROPERTIES *pprop) PURE;
  164. // override to suggest OUTPUT pin media types
  165. virtual HRESULT GetMediaType(int iPosition, __inout CMediaType *pMediaType) PURE;
  166. // =================================================================
  167. // ----- Optional Override Methods -----------------------
  168. // =================================================================
  169. // you can also override these if you want to know about streaming
  170. virtual HRESULT StartStreaming();
  171. virtual HRESULT StopStreaming();
  172. // override if you can do anything constructive with quality notifications
  173. virtual HRESULT AlterQuality(Quality q);
  174. // override this to know when the media type is actually set
  175. virtual HRESULT SetMediaType(PIN_DIRECTION direction,const CMediaType *pmt);
  176. // chance to grab extra interfaces on connection
  177. virtual HRESULT CheckConnect(PIN_DIRECTION dir,IPin *pPin);
  178. virtual HRESULT BreakConnect(PIN_DIRECTION dir);
  179. virtual HRESULT CompleteConnect(PIN_DIRECTION direction,IPin *pReceivePin);
  180. // chance to customize the transform process
  181. virtual HRESULT Receive(IMediaSample *pSample);
  182. // Standard setup for output sample
  183. HRESULT InitializeOutputSample(IMediaSample *pSample, __deref_out IMediaSample **ppOutSample);
  184. // if you override Receive, you may need to override these three too
  185. virtual HRESULT EndOfStream(void);
  186. virtual HRESULT BeginFlush(void);
  187. virtual HRESULT EndFlush(void);
  188. virtual HRESULT NewSegment(
  189. REFERENCE_TIME tStart,
  190. REFERENCE_TIME tStop,
  191. double dRate);
  192. #ifdef PERF
  193. // Override to register performance measurement with a less generic string
  194. // You should do this to avoid confusion with other filters
  195. virtual void RegisterPerfId() {
  196. m_idTransform = MSR_REGISTER(TEXT("Transform"));
  197. }
  198. #endif // PERF
  199. // implementation details
  200. protected:
  201. #ifdef PERF
  202. int m_idTransform; // performance measuring id
  203. #endif
  204. BOOL m_bEOSDelivered; // have we sent EndOfStream
  205. BOOL m_bSampleSkipped; // Did we just skip a frame
  206. BOOL m_bQualityChanged; // Have we degraded?
  207. // critical section protecting filter state.
  208. CCritSec m_csFilter;
  209. // critical section stopping state changes (ie Stop) while we're
  210. // processing a sample.
  211. //
  212. // This critical section is held when processing
  213. // events that occur on the receive thread - Receive() and EndOfStream().
  214. //
  215. // If you want to hold both m_csReceive and m_csFilter then grab
  216. // m_csFilter FIRST - like CTransformFilter::Stop() does.
  217. CCritSec m_csReceive;
  218. // these hold our input and output pins
  219. friend class CTransformInputPin;
  220. friend class CTransformOutputPin;
  221. CTransformInputPin *m_pInput;
  222. CTransformOutputPin *m_pOutput;
  223. };
  224. #endif /* __TRANSFRM__ */