[CABMAN]
[reactos.git] / reactos / dll / directx / wine / quartz / waveparser.c
1 /*
2 * WAVE Parser Filter
3 *
4 * Copyright 2005 Christian Costa
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include "quartz_private.h"
22 #include "pin.h"
23
24 //#include "uuids.h"
25 #include <aviriff.h>
26 //#include "vfwmsgs.h"
27 //#include "mmsystem.h"
28
29 //#include "wine/unicode.h"
30 #include <wine/debug.h>
31
32 //#include <math.h>
33 //#include <assert.h>
34
35 #include "parser.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
38
39 static const WCHAR wcsOutputPinName[] = {'o','u','t','p','u','t',' ','p','i','n',0};
40
41 typedef struct WAVEParserImpl
42 {
43 ParserImpl Parser;
44 LONGLONG StartOfFile; /* in media time */
45 LONGLONG EndOfFile;
46 DWORD dwSampleSize;
47 DWORD nSamplesPerSec;
48 DWORD dwLength;
49 } WAVEParserImpl;
50
51 static inline WAVEParserImpl *impl_from_IMediaSeeking( IMediaSeeking *iface )
52 {
53 return CONTAINING_RECORD(iface, WAVEParserImpl, Parser.sourceSeeking.IMediaSeeking_iface);
54 }
55
56 static inline WAVEParserImpl *impl_from_IBaseFilter( IBaseFilter *iface )
57 {
58 return CONTAINING_RECORD(iface, WAVEParserImpl, Parser.filter.IBaseFilter_iface);
59 }
60
61 static LONGLONG bytepos_to_duration(WAVEParserImpl *This, LONGLONG bytepos)
62 {
63 LONGLONG duration = BYTES_FROM_MEDIATIME(bytepos - This->StartOfFile);
64 duration *= 10000000;
65 duration /= (This->dwSampleSize * This->nSamplesPerSec);
66
67 return duration;
68 }
69
70 static LONGLONG duration_to_bytepos(WAVEParserImpl *This, LONGLONG duration)
71 {
72 LONGLONG bytepos;
73
74 bytepos = (This->dwSampleSize * This->nSamplesPerSec);
75 bytepos *= duration;
76 bytepos /= 10000000;
77 bytepos += BYTES_FROM_MEDIATIME(This->StartOfFile);
78 bytepos -= bytepos % This->dwSampleSize;
79
80 return MEDIATIME_FROM_BYTES(bytepos);
81 }
82
83 static HRESULT WAVEParser_Sample(LPVOID iface, IMediaSample * pSample, DWORD_PTR cookie)
84 {
85 WAVEParserImpl *This = iface;
86 LPBYTE pbSrcStream = NULL;
87 ULONG cbSrcStream = 0;
88 REFERENCE_TIME tStart, tStop;
89 HRESULT hr;
90 IMediaSample *newsample = NULL;
91 Parser_OutputPin *pOutputPin;
92 PullPin *pin = This->Parser.pInputPin;
93
94 IMediaSample_GetPointer(pSample, &pbSrcStream);
95 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
96
97 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
98
99 /* Flush occurring */
100 if (cbSrcStream == 0)
101 {
102 TRACE(".. Why do I need you?\n");
103 return S_OK;
104 }
105
106 pOutputPin = unsafe_impl_Parser_OutputPin_from_IPin(This->Parser.ppPins[1]);
107
108 if (SUCCEEDED(hr))
109 hr = IMemAllocator_GetBuffer(pin->pAlloc, &newsample, NULL, NULL, 0);
110
111 if (SUCCEEDED(hr))
112 {
113 LONGLONG rtSampleStart = pin->rtNext;
114 /* Add 4 for the next header, which should hopefully work */
115 LONGLONG rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(IMediaSample_GetSize(newsample));
116
117 if (rtSampleStop > pin->rtStop)
118 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(pin->rtStop), pin->cbAlign));
119
120 hr = IMediaSample_SetTime(newsample, &rtSampleStart, &rtSampleStop);
121
122 pin->rtCurrent = pin->rtNext;
123 pin->rtNext = rtSampleStop;
124
125 IMediaSample_SetPreroll(newsample, 0);
126 IMediaSample_SetDiscontinuity(newsample, 0);
127 IMediaSample_SetSyncPoint(newsample, 1);
128
129 hr = IAsyncReader_Request(pin->pReader, newsample, 0);
130 }
131
132 if (SUCCEEDED(hr))
133 {
134 REFERENCE_TIME tAviStart, tAviStop;
135
136 IMediaSample_SetSyncPoint(pSample, TRUE);
137 pOutputPin->dwSamplesProcessed++;
138
139 tAviStart = bytepos_to_duration(This, tStart);
140 tAviStop = bytepos_to_duration(This, tStart + MEDIATIME_FROM_BYTES(IMediaSample_GetActualDataLength(pSample)));
141
142 IMediaSample_SetTime(pSample, &tAviStart, &tAviStop);
143
144 hr = BaseOutputPinImpl_Deliver(&pOutputPin->pin, pSample);
145 if (hr != S_OK && hr != S_FALSE && hr != VFW_E_WRONG_STATE)
146 ERR("Error sending sample (%x)\n", hr);
147 else if (hr != S_OK)
148 /* Unset progression if denied! */
149 This->Parser.pInputPin->rtCurrent = tStart;
150 }
151
152 if (tStop >= This->EndOfFile || (bytepos_to_duration(This, tStop) >= This->Parser.sourceSeeking.llStop) || hr == VFW_E_NOT_CONNECTED)
153 {
154 unsigned int i;
155
156 TRACE("End of file reached\n");
157
158 for (i = 0; i < This->Parser.cStreams; i++)
159 {
160 IPin* ppin;
161 HRESULT hr;
162
163 TRACE("Send End Of Stream to output pin %u\n", i);
164
165 hr = IPin_ConnectedTo(This->Parser.ppPins[i+1], &ppin);
166 if (SUCCEEDED(hr))
167 {
168 hr = IPin_EndOfStream(ppin);
169 IPin_Release(ppin);
170 }
171 if (FAILED(hr))
172 {
173 ERR("%x\n", hr);
174 break;
175 }
176 }
177
178 /* Force the pullpin thread to stop */
179 hr = S_FALSE;
180 }
181
182 return hr;
183 }
184
185 static HRESULT WAVEParser_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
186 {
187 if (!IsEqualIID(&pmt->majortype, &MEDIATYPE_Stream))
188 return S_FALSE;
189 if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_WAVE))
190 return S_OK;
191 if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_AU) || IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_AIFF))
192 FIXME("AU and AIFF files not supported yet!\n");
193 return S_FALSE;
194 }
195
196 static HRESULT WINAPI WAVEParserImpl_seek(IMediaSeeking *iface)
197 {
198 WAVEParserImpl *This = impl_from_IMediaSeeking(iface);
199 PullPin *pPin = This->Parser.pInputPin;
200 IPin *victim = NULL;
201 LONGLONG newpos, curpos, endpos, bytepos;
202
203 newpos = This->Parser.sourceSeeking.llCurrent;
204 curpos = bytepos_to_duration(This, pPin->rtCurrent);
205 endpos = bytepos_to_duration(This, This->EndOfFile);
206 bytepos = duration_to_bytepos(This, newpos);
207
208 if (newpos > endpos)
209 {
210 WARN("Requesting position %x%08x beyond end of stream %x%08x\n", (DWORD)(newpos>>32), (DWORD)newpos, (DWORD)(endpos>>32), (DWORD)endpos);
211 return E_INVALIDARG;
212 }
213
214 if (curpos/1000000 == newpos/1000000)
215 {
216 TRACE("Requesting position %x%08x same as current position %x%08x\n", (DWORD)(newpos>>32), (DWORD)newpos, (DWORD)(curpos>>32), (DWORD)curpos);
217 return S_OK;
218 }
219
220 TRACE("Moving sound to %08u bytes!\n", (DWORD)BYTES_FROM_MEDIATIME(bytepos));
221
222 EnterCriticalSection(&pPin->thread_lock);
223 IPin_BeginFlush(&pPin->pin.IPin_iface);
224
225 /* Make sure this is done while stopped, BeginFlush takes care of this */
226 EnterCriticalSection(&This->Parser.filter.csFilter);
227 IPin_ConnectedTo(This->Parser.ppPins[1], &victim);
228 if (victim)
229 {
230 IPin_NewSegment(victim, newpos, endpos, pPin->dRate);
231 IPin_Release(victim);
232 }
233
234 pPin->rtStart = pPin->rtCurrent = bytepos;
235 unsafe_impl_Parser_OutputPin_from_IPin(This->Parser.ppPins[1])->dwSamplesProcessed = 0;
236 LeaveCriticalSection(&This->Parser.filter.csFilter);
237
238 TRACE("Done flushing\n");
239 IPin_EndFlush(&pPin->pin.IPin_iface);
240 LeaveCriticalSection(&pPin->thread_lock);
241
242 return S_OK;
243 }
244
245 static HRESULT WAVEParser_InputPin_PreConnect(IPin * iface, IPin * pConnectPin, ALLOCATOR_PROPERTIES *props)
246 {
247 PullPin *This = impl_PullPin_from_IPin(iface);
248 HRESULT hr;
249 RIFFLIST list;
250 RIFFCHUNK chunk;
251 LONGLONG pos = 0; /* in bytes */
252 PIN_INFO piOutput;
253 AM_MEDIA_TYPE amt;
254 WAVEParserImpl * pWAVEParser = impl_from_IBaseFilter(This->pin.pinInfo.pFilter);
255 LONGLONG length, avail;
256
257 piOutput.dir = PINDIR_OUTPUT;
258 piOutput.pFilter = &pWAVEParser->Parser.filter.IBaseFilter_iface;
259 lstrcpynW(piOutput.achName, wcsOutputPinName, sizeof(piOutput.achName) / sizeof(piOutput.achName[0]));
260
261 hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(list), (BYTE *)&list);
262 pos += sizeof(list);
263
264 if (list.fcc != FOURCC_RIFF)
265 {
266 ERR("Input stream not a RIFF file\n");
267 return E_FAIL;
268 }
269 if (list.cb > 1 * 1024 * 1024 * 1024) /* cannot be more than 1Gb in size */
270 {
271 ERR("Input stream violates RIFF spec\n");
272 return E_FAIL;
273 }
274 if (list.fccListType != mmioFOURCC('W','A','V','E'))
275 {
276 ERR("Input stream not an WAVE RIFF file\n");
277 return E_FAIL;
278 }
279
280 hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(chunk), (BYTE *)&chunk);
281 pos += sizeof(chunk);
282 if (chunk.fcc != mmioFOURCC('f','m','t',' '))
283 {
284 ERR("Expected 'fmt ' chunk, but got %.04s\n", (LPSTR)&chunk.fcc);
285 return E_FAIL;
286 }
287
288 amt.majortype = MEDIATYPE_Audio;
289 amt.formattype = FORMAT_WaveFormatEx;
290 amt.cbFormat = chunk.cb;
291 amt.pbFormat = CoTaskMemAlloc(amt.cbFormat);
292 amt.pUnk = NULL;
293 hr = IAsyncReader_SyncRead(This->pReader, pos, amt.cbFormat, amt.pbFormat);
294 amt.subtype = MEDIATYPE_Audio;
295 amt.subtype.Data1 = ((WAVEFORMATEX*)amt.pbFormat)->wFormatTag;
296
297 pos += chunk.cb;
298 hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(chunk), (BYTE *)&chunk);
299 if (chunk.fcc == mmioFOURCC('f','a','c','t'))
300 {
301 FIXME("'fact' chunk not supported yet\n");
302 pos += sizeof(chunk) + chunk.cb;
303 hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(chunk), (BYTE *)&chunk);
304 }
305 if (chunk.fcc != mmioFOURCC('d','a','t','a'))
306 {
307 ERR("Expected 'data' chunk, but got %.04s\n", (LPSTR)&chunk.fcc);
308 return E_FAIL;
309 }
310
311 if (hr == S_OK)
312 {
313 pWAVEParser->StartOfFile = MEDIATIME_FROM_BYTES(pos + sizeof(RIFFCHUNK));
314 pWAVEParser->EndOfFile = MEDIATIME_FROM_BYTES(pos + chunk.cb + sizeof(RIFFCHUNK));
315 }
316
317 if (hr != S_OK)
318 return E_FAIL;
319
320 props->cbAlign = ((WAVEFORMATEX*)amt.pbFormat)->nBlockAlign;
321 props->cbPrefix = 0;
322 props->cbBuffer = 4096;
323 props->cBuffers = 3;
324 pWAVEParser->dwSampleSize = ((WAVEFORMATEX*)amt.pbFormat)->nBlockAlign;
325 IAsyncReader_Length(This->pReader, &length, &avail);
326 pWAVEParser->dwLength = length / (ULONGLONG)pWAVEParser->dwSampleSize;
327 pWAVEParser->nSamplesPerSec = ((WAVEFORMATEX*)amt.pbFormat)->nSamplesPerSec;
328 hr = Parser_AddPin(&(pWAVEParser->Parser), &piOutput, props, &amt);
329 CoTaskMemFree(amt.pbFormat);
330
331 pWAVEParser->Parser.sourceSeeking.llCurrent = 0;
332 pWAVEParser->Parser.sourceSeeking.llStop = pWAVEParser->Parser.sourceSeeking.llDuration = bytepos_to_duration(pWAVEParser, pWAVEParser->EndOfFile);
333 TRACE("Duration: %u seconds\n", (DWORD)(pWAVEParser->Parser.sourceSeeking.llDuration / (LONGLONG)10000000));
334
335 This->rtStop = pWAVEParser->EndOfFile;
336 This->rtStart = pWAVEParser->StartOfFile;
337
338 TRACE("WAVE File ok\n");
339
340 return hr;
341 }
342
343 static HRESULT WAVEParser_Cleanup(LPVOID iface)
344 {
345 WAVEParserImpl *This = iface;
346
347 TRACE("(%p)->()\n", This);
348
349 return S_OK;
350 }
351
352 static HRESULT WAVEParser_first_request(LPVOID iface)
353 {
354 WAVEParserImpl *This = iface;
355 PullPin *pin = This->Parser.pInputPin;
356 HRESULT hr;
357 IMediaSample *sample;
358
359 if (pin->rtCurrent >= pin->rtStop)
360 {
361 /* Last sample has already been queued, request nothing more */
362 TRACE("Done!\n");
363 return S_OK;
364 }
365
366 hr = IMemAllocator_GetBuffer(pin->pAlloc, &sample, NULL, NULL, 0);
367
368 pin->rtNext = pin->rtCurrent;
369 if (SUCCEEDED(hr))
370 {
371 LONGLONG rtSampleStart = pin->rtNext;
372 /* Add 4 for the next header, which should hopefully work */
373 LONGLONG rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(IMediaSample_GetSize(sample));
374 Parser_OutputPin *outpin = unsafe_impl_Parser_OutputPin_from_IPin(This->Parser.ppPins[1]);
375
376 if (rtSampleStop > pin->rtStop)
377 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(pin->rtStop), pin->cbAlign));
378
379 hr = IMediaSample_SetTime(sample, &rtSampleStart, &rtSampleStop);
380
381 pin->rtCurrent = pin->rtNext;
382 pin->rtNext = rtSampleStop;
383
384 IMediaSample_SetPreroll(sample, FALSE);
385 if (!outpin->dwSamplesProcessed++)
386 IMediaSample_SetDiscontinuity(sample, TRUE);
387 else
388 IMediaSample_SetDiscontinuity(sample, FALSE);
389
390 hr = IAsyncReader_Request(pin->pReader, sample, 0);
391 }
392 if (FAILED(hr))
393 ERR("Horsemen of the apocalypse came to bring error 0x%08x %p\n", hr, sample);
394
395 return hr;
396 }
397
398 static HRESULT WAVEParser_disconnect(LPVOID iface)
399 {
400 /* TODO: Find and plug memory leaks */
401 return S_OK;
402 }
403
404 static const IBaseFilterVtbl WAVEParser_Vtbl =
405 {
406 Parser_QueryInterface,
407 Parser_AddRef,
408 Parser_Release,
409 Parser_GetClassID,
410 Parser_Stop,
411 Parser_Pause,
412 Parser_Run,
413 Parser_GetState,
414 Parser_SetSyncSource,
415 Parser_GetSyncSource,
416 Parser_EnumPins,
417 Parser_FindPin,
418 Parser_QueryFilterInfo,
419 Parser_JoinFilterGraph,
420 Parser_QueryVendorInfo
421 };
422
423 HRESULT WAVEParser_create(IUnknown * pUnkOuter, LPVOID * ppv)
424 {
425 HRESULT hr;
426 WAVEParserImpl * This;
427
428 TRACE("(%p, %p)\n", pUnkOuter, ppv);
429
430 *ppv = NULL;
431
432 if (pUnkOuter)
433 return CLASS_E_NOAGGREGATION;
434
435 /* Note: This memory is managed by the transform filter once created */
436 This = CoTaskMemAlloc(sizeof(WAVEParserImpl));
437
438 hr = Parser_Create(&(This->Parser), &WAVEParser_Vtbl, &CLSID_WAVEParser, WAVEParser_Sample, WAVEParser_QueryAccept, WAVEParser_InputPin_PreConnect, WAVEParser_Cleanup, WAVEParser_disconnect, WAVEParser_first_request, NULL, NULL, WAVEParserImpl_seek, NULL);
439
440 if (FAILED(hr))
441 return hr;
442
443 *ppv = This;
444
445 return hr;
446 }