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