forked from synther/gdcl-mpeg-4-mux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MuxFilter.h
264 lines (222 loc) · 7.8 KB
/
MuxFilter.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
//
// MuxFilter.h
//
// Declaration of classes for DirectShow MPEG-4 Multiplexor filter
//
// Geraint Davies, May 2004
//
// Copyright (c) GDCL 2004-6. All Rights Reserved.
// You are free to re-use this as the basis for your own filter development,
// provided you retain this copyright notice in the source.
// http://www.gdcl.co.uk
//////////////////////////////////////////////////////////////////////
#pragma once
#include "MovieWriter.h"
// forward declarations
class Mpeg4Mux;
class MuxInput;
class MuxOutput;
class MuxAllocator;
// override the standard allocator to
// force use of a larger number of buffers.
// We use the input buffers to queue the chunks for
// interleaving, so the input connection must allow
// us to hold at least 2 seconds of data.
class MuxAllocator : public CMemAllocator
{
public:
MuxAllocator(LPUNKNOWN pUnk, HRESULT* phr, const CMediaType* pmt);
// we override this just to increase the requested buffer count
STDMETHODIMP SetProperties(
ALLOCATOR_PROPERTIES* pRequest,
ALLOCATOR_PROPERTIES* pActual);
private:
CMediaType m_mt;
};
// input pin, receives data corresponding to one
// media track in the file.
// Pins are created and deleted dynamically to
// ensure that there is always one unconnected pin.
class MuxInput
: public CBaseInputPin,
public IAMStreamControl
{
public:
MuxInput(Mpeg4Mux* pFilter, CCritSec* pLock, HRESULT* phr, LPCWSTR pName, int index);
// lifetime management for pins is normally delegated to the filter, but
// we need to be able to create and delete them independently, so keep
// a separate refcount.
STDMETHODIMP_(ULONG) NonDelegatingRelease()
{
return CUnknown::NonDelegatingRelease();
}
STDMETHODIMP_(ULONG) NonDelegatingAddRef()
{
return CUnknown::NonDelegatingAddRef();
}
DECLARE_IUNKNOWN
STDMETHODIMP NonDelegatingQueryInterface(REFIID iid, void** ppv)
{
if (iid == IID_IAMStreamControl)
{
return GetInterface((IAMStreamControl*) this, ppv);
}
return __super::NonDelegatingQueryInterface(iid, ppv);
}
// CBasePin overrides
HRESULT CheckMediaType(const CMediaType* pmt);
HRESULT GetMediaType(int iPosition, CMediaType* pmt);
// input
STDMETHODIMP Receive(IMediaSample* pSample);
STDMETHODIMP EndOfStream();
STDMETHODIMP BeginFlush();
STDMETHODIMP EndFlush();
// state change
HRESULT Active();
HRESULT Inactive();
// connection management -- used to maintain one free pin
HRESULT BreakConnect();
HRESULT CompleteConnect(IPin *pReceivePin);
// support custom allocator
STDMETHODIMP GetAllocator(IMemAllocator** ppAllocator);
STDMETHODIMP NotifyAllocator(IMemAllocator* pAlloc, BOOL bReadOnly);
// IAMStreamControl methods
STDMETHOD(StartAt)(const REFERENCE_TIME* ptStart, DWORD dwCookie);
STDMETHOD(StopAt)(const REFERENCE_TIME* ptStop, BOOL bSendExtra, DWORD dwCookie);
STDMETHOD(GetInfo)(AM_STREAM_INFO* pInfo);
private:
bool ShouldDiscard(IMediaSample* pSample);
HRESULT CopySample(IMediaSample* pIn, IMediaSample* pOut);
private:
Mpeg4Mux* m_pMux;
int m_index;
TrackWriter* m_pTrack;
CCritSec m_csStreamControl;
AM_STREAM_INFO m_StreamInfo;
IMemAllocatorPtr m_pCopyAlloc; // private allocator if source has too few buffers
};
// output pin, writes multiplexed data downstream
// using IMemOutputPin while running, and then writes
// metadata using IStream::Write when stopping.
class MuxOutput
: public CBaseOutputPin,
public AtomWriter
{
public:
MuxOutput(Mpeg4Mux* pFilter, CCritSec* pLock, HRESULT* phr);
// CBaseOutputPin overrides
HRESULT CheckMediaType(const CMediaType* pmt);
HRESULT GetMediaType(int iPosition, CMediaType* pmt);
HRESULT DecideBufferSize(IMemAllocator * pAlloc, ALLOCATOR_PROPERTIES * pprop);
HRESULT CompleteConnect(IPin *pReceivePin);
HRESULT BreakConnect();
// called from filter
void Reset();
void UseIStream();
void FillSpace();
// AtomWriter methods
LONGLONG Length();
LONGLONG Position();
HRESULT Replace(LONGLONG pos, const BYTE* pBuffer, long cBytes);
HRESULT Append(const BYTE* pBuffer, long cBytes);
private:
Mpeg4Mux* m_pMux;
CCritSec m_csWrite;
bool m_bUseIStream;
LONGLONG m_llBytes;
IStreamPtr m_pIStream;
};
// To pass seeking calls upstream we must try all connected input pins.
// Where two input pins lead to the same splitter, only one will be
// allowed to SetTimeFormat at once, so we must call this on all
// pins and store them, then call SetTimeFormat(NULL) once the
// operation is complete.
//
// This class manages that list of seekable pins.
// It is also used for seeking calls that do not need to set
// the time format.
class SeekingAggregator
{
public:
SeekingAggregator(CBaseFilter* pFilter, bool bSetTimeFormat = false);
~SeekingAggregator();
typedef list<IMediaSeeking*>::iterator iterator;
iterator Begin()
{
return m_Pins.begin();
}
iterator End()
{
return m_Pins.end();
}
private:
bool m_bSetTimeFormat;
list<IMediaSeeking*> m_Pins;
};
class DECLSPEC_UUID("5FD85181-E542-4e52-8D9D-5D613C30131B")
Mpeg4Mux
: public CBaseFilter,
public IMediaSeeking
{
public:
// constructor method used by class factory
static CUnknown* WINAPI CreateInstance(LPUNKNOWN pUnk, HRESULT* phr);
DECLARE_IUNKNOWN
STDMETHODIMP NonDelegatingQueryInterface(REFIID iid, void** ppv);
// filter registration tables
static const AMOVIESETUP_MEDIATYPE m_sudType[];
static const AMOVIESETUP_PIN m_sudPin[];
static const AMOVIESETUP_FILTER m_sudFilter;
// CBaseFilter methods
int GetPinCount();
CBasePin *GetPin(int n);
STDMETHODIMP Stop();
STDMETHODIMP Pause();
void CreateInput();
// called from input pin
void OnDisconnect(int index);
void OnConnect(int index);
bool CanReceive(const CMediaType* pmt);
TrackWriter* MakeTrack(int index, const CMediaType* pmt);
void OnEOS();
REFERENCE_TIME Start() { return m_tStart;}
// we implement IMediaSeeking to allow encoding
// of specific portions of an input clip, and
// to report progress via the current position.
// Calls (apart from current position) are
// passed upstream to any pins that support seeking
// IMediaSeeking
public:
STDMETHODIMP GetCapabilities(DWORD * pCapabilities );
STDMETHODIMP CheckCapabilities(DWORD * pCapabilities );
STDMETHODIMP IsFormatSupported(const GUID * pFormat);
STDMETHODIMP QueryPreferredFormat(GUID * pFormat);
STDMETHODIMP GetTimeFormat(GUID *pFormat);
STDMETHODIMP IsUsingTimeFormat(const GUID * pFormat);
STDMETHODIMP SetTimeFormat(const GUID * pFormat);
STDMETHODIMP GetDuration(LONGLONG *pDuration);
STDMETHODIMP GetStopPosition(LONGLONG *pStop);
STDMETHODIMP GetCurrentPosition(LONGLONG *pCurrent);
STDMETHODIMP ConvertTimeFormat(LONGLONG * pTarget, const GUID * pTargetFormat,
LONGLONG Source, const GUID * pSourceFormat );
STDMETHODIMP SetPositions(LONGLONG * pCurrent, DWORD dwCurrentFlags
, LONGLONG * pStop, DWORD dwStopFlags );
STDMETHODIMP GetPositions(LONGLONG * pCurrent,
LONGLONG * pStop );
STDMETHODIMP GetAvailable(LONGLONG * pEarliest, LONGLONG * pLatest );
STDMETHODIMP SetRate(double dRate);
STDMETHODIMP GetRate(double * pdRate);
STDMETHODIMP GetPreroll(LONGLONG * pllPreroll);
private:
// construct only via class factory
Mpeg4Mux(LPUNKNOWN pUnk, HRESULT* phr);
~Mpeg4Mux();
private:
CCritSec m_csFilter;
CCritSec m_csTracks;
MuxOutput* m_pOutput;
vector<MuxInput*> m_pInputs;
smart_ptr<MovieWriter> m_pMovie;
// for reporting (via GetCurrentPosition) after completion
REFERENCE_TIME m_tWritten;
};