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