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