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