1
0

transfrm.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. {
  45. return AMGetWideString(L"In", Id);
  46. }
  47. // Grab and release extra interfaces if required
  48. HRESULT CheckConnect(IPin *pPin);
  49. HRESULT BreakConnect();
  50. HRESULT CompleteConnect(IPin *pReceivePin);
  51. // check that we can support this output type
  52. HRESULT CheckMediaType(const CMediaType* mtIn);
  53. // set the connection media type
  54. HRESULT SetMediaType(const CMediaType* mt);
  55. // --- IMemInputPin -----
  56. // here's the next block of data from the stream.
  57. // AddRef it yourself if you need to hold it beyond the end
  58. // of this call.
  59. STDMETHODIMP Receive(IMediaSample * pSample);
  60. // provide EndOfStream that passes straight downstream
  61. // (there is no queued data)
  62. STDMETHODIMP EndOfStream(void);
  63. // passes it to CTransformFilter::BeginFlush
  64. STDMETHODIMP BeginFlush(void);
  65. // passes it to CTransformFilter::EndFlush
  66. STDMETHODIMP EndFlush(void);
  67. STDMETHODIMP NewSegment(
  68. REFERENCE_TIME tStart,
  69. REFERENCE_TIME tStop,
  70. double dRate);
  71. // Check if it's OK to process samples
  72. virtual HRESULT CheckStreaming();
  73. // Media type
  74. public:
  75. CMediaType& CurrentMediaType() { return m_mt; };
  76. };
  77. // ==================================================
  78. // Implements the output pin
  79. // ==================================================
  80. class CTransformOutputPin : public CBaseOutputPin
  81. {
  82. friend class CTransformFilter;
  83. protected:
  84. CTransformFilter *m_pTransformFilter;
  85. public:
  86. // implement IMediaPosition by passing upstream
  87. IUnknown * m_pPosition;
  88. CTransformOutputPin(
  89. __in_opt LPCTSTR pObjectName,
  90. __inout CTransformFilter *pTransformFilter,
  91. __inout HRESULT * phr,
  92. __in_opt LPCWSTR pName);
  93. #ifdef UNICODE
  94. CTransformOutputPin(
  95. __in_opt LPCSTR pObjectName,
  96. __inout CTransformFilter *pTransformFilter,
  97. __inout HRESULT * phr,
  98. __in_opt LPCWSTR pName);
  99. #endif
  100. ~CTransformOutputPin();
  101. // override to expose IMediaPosition
  102. STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, __deref_out void **ppv);
  103. // --- CBaseOutputPin ------------
  104. STDMETHODIMP QueryId(__deref_out LPWSTR * Id)
  105. {
  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() { return m_mt; };
  128. };
  129. class AM_NOVTABLE CTransformFilter : public CBaseFilter
  130. {
  131. public:
  132. // map getpin/getpincount for base enum of pins to owner
  133. // override this to return more specialised pin objects
  134. virtual int GetPinCount();
  135. virtual CBasePin * GetPin(int n);
  136. STDMETHODIMP FindPin(LPCWSTR Id, __deref_out IPin **ppPin);
  137. // override state changes to allow derived transform filter
  138. // to control streaming start/stop
  139. STDMETHODIMP Stop();
  140. STDMETHODIMP Pause();
  141. public:
  142. CTransformFilter(__in_opt LPCTSTR , __inout_opt LPUNKNOWN, REFCLSID clsid);
  143. #ifdef UNICODE
  144. CTransformFilter(__in_opt LPCSTR , __inout_opt LPUNKNOWN, REFCLSID clsid);
  145. #endif
  146. ~CTransformFilter();
  147. // =================================================================
  148. // ----- override these bits ---------------------------------------
  149. // =================================================================
  150. // These must be supplied in a derived class
  151. virtual HRESULT Transform(IMediaSample * pIn, IMediaSample *pOut);
  152. // check if you can support mtIn
  153. virtual HRESULT CheckInputType(const CMediaType* mtIn) PURE;
  154. // check if you can support the transform from this input to this output
  155. virtual HRESULT CheckTransform(const CMediaType* mtIn, const CMediaType* mtOut) PURE;
  156. // this goes in the factory template table to create new instances
  157. // static CCOMObject * CreateInstance(__inout_opt LPUNKNOWN, HRESULT *);
  158. // call the SetProperties function with appropriate arguments
  159. virtual HRESULT DecideBufferSize(
  160. IMemAllocator * pAllocator,
  161. __inout ALLOCATOR_PROPERTIES *pprop) PURE;
  162. // override to suggest OUTPUT pin media types
  163. virtual HRESULT GetMediaType(int iPosition, __inout CMediaType *pMediaType) PURE;
  164. // =================================================================
  165. // ----- Optional Override Methods -----------------------
  166. // =================================================================
  167. // you can also override these if you want to know about streaming
  168. virtual HRESULT StartStreaming();
  169. virtual HRESULT StopStreaming();
  170. // override if you can do anything constructive with quality notifications
  171. virtual HRESULT AlterQuality(Quality q);
  172. // override this to know when the media type is actually set
  173. virtual HRESULT SetMediaType(PIN_DIRECTION direction,const CMediaType *pmt);
  174. // chance to grab extra interfaces on connection
  175. virtual HRESULT CheckConnect(PIN_DIRECTION dir,IPin *pPin);
  176. virtual HRESULT BreakConnect(PIN_DIRECTION dir);
  177. virtual HRESULT CompleteConnect(PIN_DIRECTION direction,IPin *pReceivePin);
  178. // chance to customize the transform process
  179. virtual HRESULT Receive(IMediaSample *pSample);
  180. // Standard setup for output sample
  181. HRESULT InitializeOutputSample(IMediaSample *pSample, __deref_out IMediaSample **ppOutSample);
  182. // if you override Receive, you may need to override these three too
  183. virtual HRESULT EndOfStream(void);
  184. virtual HRESULT BeginFlush(void);
  185. virtual HRESULT EndFlush(void);
  186. virtual HRESULT NewSegment(
  187. REFERENCE_TIME tStart,
  188. REFERENCE_TIME tStop,
  189. double dRate);
  190. #ifdef PERF
  191. // Override to register performance measurement with a less generic string
  192. // You should do this to avoid confusion with other filters
  193. virtual void RegisterPerfId()
  194. {m_idTransform = MSR_REGISTER(TEXT("Transform"));}
  195. #endif // PERF
  196. // implementation details
  197. protected:
  198. #ifdef PERF
  199. int m_idTransform; // performance measuring id
  200. #endif
  201. BOOL m_bEOSDelivered; // have we sent EndOfStream
  202. BOOL m_bSampleSkipped; // Did we just skip a frame
  203. BOOL m_bQualityChanged; // Have we degraded?
  204. // critical section protecting filter state.
  205. CCritSec m_csFilter;
  206. // critical section stopping state changes (ie Stop) while we're
  207. // processing a sample.
  208. //
  209. // This critical section is held when processing
  210. // events that occur on the receive thread - Receive() and EndOfStream().
  211. //
  212. // If you want to hold both m_csReceive and m_csFilter then grab
  213. // m_csFilter FIRST - like CTransformFilter::Stop() does.
  214. CCritSec m_csReceive;
  215. // these hold our input and output pins
  216. friend class CTransformInputPin;
  217. friend class CTransformOutputPin;
  218. CTransformInputPin *m_pInput;
  219. CTransformOutputPin *m_pOutput;
  220. };
  221. #endif /* __TRANSFRM__ */