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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
/*
* Copyright (C) 2003-2006 Gabest
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Make; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include "stdafx.h"
#include "D2VSource.h"
#include "MPEG2Dec.h"
#include "../../../DSUtil/DSUtil.h"
#ifdef REGISTER_FILTER
const AMOVIESETUP_MEDIATYPE sudPinTypesOut[] =
{
{&MEDIATYPE_Video, &MEDIASUBTYPE_YUY2}
};
const AMOVIESETUP_PIN sudOpPin[] =
{
{L"Output", FALSE, TRUE, FALSE, FALSE, &CLSID_NULL, NULL, countof(sudPinTypesOut), sudPinTypesOut}
};
const AMOVIESETUP_FILTER sudFilter[] =
{
{&__uuidof(CD2VSource), L"MPC - D2VSource", MERIT_NORMAL, countof(sudOpPin), sudOpPin, CLSID_LegacyAmFilterCategory}
};
CFactoryTemplate g_Templates[] =
{
{sudFilter[0].strName, sudFilter[0].clsID, CreateInstance<CD2VSource>, NULL, &sudFilter[0]}
};
int g_cTemplates = countof(g_Templates);
STDAPI DllRegisterServer()
{
SetRegKeyValue(
_T("Media Type\\{e436eb83-524f-11ce-9f53-0020af0ba770}"), _T("{47CE0591-C4D5-4b41-BED7-28F59AD76228}"),
_T("0"), _T("0,18,,4456443241564950726F6A65637446696C65")); // "DVD2AVIProjectFile"
SetRegKeyValue(
_T("Media Type\\{e436eb83-524f-11ce-9f53-0020af0ba770}"), _T("{47CE0591-C4D5-4b41-BED7-28F59AD76228}"),
_T("Source Filter"), _T("{47CE0591-C4D5-4b41-BED7-28F59AD76228}"));
SetRegKeyValue(
_T("Media Type\\Extensions"), _T(".d2v"),
_T("Source Filter"), _T("{47CE0591-C4D5-4b41-BED7-28F59AD76228}"));
return AMovieDllRegisterServer2(TRUE);
}
STDAPI DllUnregisterServer()
{
DeleteRegKey(_T("Media Type\\{e436eb83-524f-11ce-9f53-0020af0ba770}"), _T("{47CE0591-C4D5-4b41-BED7-28F59AD76228}"));
DeleteRegKey(_T("Media Type\\Extensions"), _T(".d2v"));
return AMovieDllRegisterServer2(FALSE);
}
#include "../../FilterApp.h"
CFilterApp theApp;
#endif
//
// CD2VSource
//
CD2VSource::CD2VSource(LPUNKNOWN lpunk, HRESULT* phr)
: CBaseSource<CD2VStream>(NAME("CD2VSource"), lpunk, phr, __uuidof(this))
{
if(phr) *phr = S_OK;
}
CD2VSource::~CD2VSource()
{
}
//
// CD2VStream
//
CD2VStream::CD2VStream(const WCHAR* fn, CSource* pParent, HRESULT* phr)
: CBaseStream(NAME("D2VSourceStream"), pParent, phr)
, m_pFrameBuffer(NULL)
{
CAutoLock cAutoLock(&m_cSharedState);
m_pDecoder.Attach(DNew CMPEG2Dec());
if(!m_pDecoder)
{
if(phr) *phr = E_OUTOFMEMORY;
return;
}
if(!m_pDecoder->Open(CString(fn), CMPEG2Dec::YUY2))
{
if(phr) *phr = E_FAIL;
return;
}
if(!m_pFrameBuffer.Allocate(m_pDecoder->Clip_Width*m_pDecoder->Clip_Height*4))
{
if(phr) *phr = E_OUTOFMEMORY;
return;
}
m_AvgTimePerFrame = 10000000000i64/m_pDecoder->VF_FrameRate;
m_rtDuration = m_rtStop = m_AvgTimePerFrame*m_pDecoder->VF_FrameLimit;
if(phr) *phr = m_rtDuration > 0 ? S_OK : E_FAIL;
}
CD2VStream::~CD2VStream()
{
CAutoLock cAutoLock(&m_cSharedState);
}
HRESULT CD2VStream::DecideBufferSize(IMemAllocator* pAlloc, ALLOCATOR_PROPERTIES* pProperties)
{
// CAutoLock cAutoLock(m_pFilter->pStateLock());
ASSERT(pAlloc);
ASSERT(pProperties);
HRESULT hr = NOERROR;
int w, h, bpp;
if(!GetDim(w, h, bpp))
return E_FAIL;
pProperties->cBuffers = 1;
pProperties->cbBuffer = w*h*bpp>>3;
ALLOCATOR_PROPERTIES Actual;
if(FAILED(hr = pAlloc->SetProperties(pProperties, &Actual))) return hr;
if(Actual.cbBuffer < pProperties->cbBuffer) return E_FAIL;
ASSERT(Actual.cBuffers == pProperties->cBuffers);
return NOERROR;
}
HRESULT CD2VStream::FillBuffer(IMediaSample* pSample, int nFrame, BYTE* pOut, long& len)
{
if(!m_pDecoder)
return S_FALSE;
AM_MEDIA_TYPE* pmt;
if(SUCCEEDED(pSample->GetMediaType(&pmt)) && pmt)
{
CMediaType mt(*pmt);
SetMediaType(&mt);
DeleteMediaType(pmt);
}
int w, h, bpp;
if(!GetDim(w, h, bpp))
return S_FALSE;
BYTE* pIn = m_pFrameBuffer;
int pitchIn, pitchOut = 0;
pitchIn = m_pDecoder->Clip_Width*bpp>>3;
pitchOut = w*bpp>>3;
m_pDecoder->Decode(pIn, (unsigned long)(nFrame), pitchIn);
for(int y = 0, p = min(pitchIn, pitchOut);
y < h;
y++, pIn += pitchIn, pOut += pitchOut)
{
memcpy(pOut, pIn, p);
}
len = pitchOut*h;
return S_OK;
}
HRESULT CD2VStream::GetMediaType(int iPosition, CMediaType* pmt)
{
CAutoLock cAutoLock(m_pFilter->pStateLock());
if(iPosition < 0) return E_INVALIDARG;
if(iPosition > 0) return VFW_S_NO_MORE_ITEMS;
pmt->SetType(&MEDIATYPE_Video);
pmt->SetSubtype(&MEDIASUBTYPE_YUY2);
pmt->SetFormatType(&FORMAT_VideoInfo);
pmt->SetTemporalCompression(FALSE);
VIDEOINFOHEADER* vih = (VIDEOINFOHEADER*)pmt->AllocFormatBuffer(sizeof(VIDEOINFOHEADER));
memset(vih, 0, sizeof(VIDEOINFOHEADER));
vih->AvgTimePerFrame = m_AvgTimePerFrame;
vih->bmiHeader.biSize = sizeof(vih->bmiHeader);
vih->bmiHeader.biWidth = m_pDecoder->Clip_Width;
vih->bmiHeader.biHeight = m_pDecoder->Clip_Height;
vih->bmiHeader.biPlanes = 1;
vih->bmiHeader.biBitCount = 16;
vih->bmiHeader.biCompression = '2YUY';
vih->bmiHeader.biSizeImage = vih->bmiHeader.biWidth*abs(vih->bmiHeader.biHeight)*vih->bmiHeader.biBitCount>>3;
pmt->SetSampleSize(vih->bmiHeader.biSizeImage);
return NOERROR;
}
HRESULT CD2VStream::SetMediaType(const CMediaType* pmt)
{
if(m_pDecoder)
{
if(pmt->subtype == MEDIASUBTYPE_YUY2)
m_pDecoder->m_dstFormat = CMPEG2Dec::YUY2;
else
return E_FAIL;
}
return CSourceStream::SetMediaType(pmt);
}
HRESULT CD2VStream::CheckMediaType(const CMediaType* pmt)
{
return pmt->majortype == MEDIATYPE_Video
&& pmt->subtype == MEDIASUBTYPE_YUY2
&& pmt->formattype == FORMAT_VideoInfo
? S_OK
: E_INVALIDARG;
}
STDMETHODIMP CD2VStream::Notify(IBaseFilter* pSender, Quality q)
{
if(q.Late > 0 && q.Late < 100000000)
{
CAutoLock cAutoLockShared(&m_cSharedState);
m_rtSampleTime += (q.Late/m_AvgTimePerFrame)*m_AvgTimePerFrame;
m_rtPosition += (q.Late/m_AvgTimePerFrame)*m_AvgTimePerFrame;
}
return S_OK;
}
//
bool CD2VStream::GetDim(int& w, int& h, int& bpp)
{
if(m_mt.formattype == FORMAT_VideoInfo)
{
w = ((VIDEOINFOHEADER*)m_mt.pbFormat)->bmiHeader.biWidth;
h = abs(((VIDEOINFOHEADER*)m_mt.pbFormat)->bmiHeader.biHeight);
bpp = ((VIDEOINFOHEADER*)m_mt.pbFormat)->bmiHeader.biBitCount;
}
else if(m_mt.formattype == FORMAT_VideoInfo2)
{
w = ((VIDEOINFOHEADER2*)m_mt.pbFormat)->bmiHeader.biWidth;
h = abs(((VIDEOINFOHEADER2*)m_mt.pbFormat)->bmiHeader.biHeight);
bpp = ((VIDEOINFOHEADER2*)m_mt.pbFormat)->bmiHeader.biBitCount;
}
else
{
return(false);
}
return(true);
}
|