wevtapi: Fix typo in spec file.
[wine/zf.git] / dlls / quartz / waveparser.c
blobfa9cd45d276a2bf96ba50c86bdbe8fe4506ee355
1 /*
2 * WAVE Parser Filter
4 * Copyright 2005 Christian Costa
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.
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.
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
21 #include "quartz_private.h"
22 #include "pin.h"
24 #include "uuids.h"
25 #include "aviriff.h"
26 #include "vfwmsgs.h"
27 #include "mmsystem.h"
29 #include "wine/unicode.h"
30 #include "wine/debug.h"
32 #include <math.h>
33 #include <assert.h>
35 #include "parser.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
39 static const WCHAR wcsOutputPinName[] = {'o','u','t','p','u','t',' ','p','i','n',0};
41 typedef struct WAVEParserImpl
43 ParserImpl Parser;
44 LONGLONG StartOfFile; /* in media time */
45 LONGLONG EndOfFile;
46 DWORD nAvgBytesPerSec;
47 DWORD nBlockAlign;
48 } WAVEParserImpl;
50 static inline WAVEParserImpl *impl_from_IMediaSeeking( IMediaSeeking *iface )
52 return CONTAINING_RECORD(iface, WAVEParserImpl, Parser.sourceSeeking.IMediaSeeking_iface);
55 static inline WAVEParserImpl *impl_from_IBaseFilter( IBaseFilter *iface )
57 return CONTAINING_RECORD(iface, WAVEParserImpl, Parser.filter.IBaseFilter_iface);
60 static LONGLONG bytepos_to_duration(WAVEParserImpl *This, LONGLONG bytepos)
62 LONGLONG duration = BYTES_FROM_MEDIATIME(bytepos - This->StartOfFile);
63 duration *= 10000000;
64 duration /= This->nAvgBytesPerSec;
66 return duration;
69 static LONGLONG duration_to_bytepos(WAVEParserImpl *This, LONGLONG duration)
71 LONGLONG bytepos;
73 bytepos = This->nAvgBytesPerSec;
74 bytepos *= duration;
75 bytepos /= 10000000;
76 bytepos -= bytepos % This->nBlockAlign;
77 bytepos += BYTES_FROM_MEDIATIME(This->StartOfFile);
79 return MEDIATIME_FROM_BYTES(bytepos);
82 static HRESULT WAVEParser_Sample(LPVOID iface, IMediaSample * pSample, DWORD_PTR cookie)
84 WAVEParserImpl *This = iface;
85 LPBYTE pbSrcStream = NULL;
86 ULONG cbSrcStream = 0;
87 REFERENCE_TIME tStart, tStop;
88 HRESULT hr;
89 IMediaSample *newsample = NULL;
90 Parser_OutputPin *pOutputPin;
91 PullPin *pin = This->Parser.pInputPin;
93 IMediaSample_GetPointer(pSample, &pbSrcStream);
94 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
96 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
98 /* Flush occurring */
99 if (cbSrcStream == 0)
101 TRACE(".. Why do I need you?\n");
102 return S_OK;
105 pOutputPin = unsafe_impl_Parser_OutputPin_from_IPin(This->Parser.ppPins[1]);
107 if (SUCCEEDED(hr))
108 hr = IMemAllocator_GetBuffer(pin->pAlloc, &newsample, NULL, NULL, 0);
110 if (SUCCEEDED(hr))
112 LONGLONG rtSampleStart = pin->rtNext;
113 /* Add 4 for the next header, which should hopefully work */
114 LONGLONG rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(IMediaSample_GetSize(newsample));
116 if (rtSampleStop > pin->rtStop)
117 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(pin->rtStop), pin->cbAlign));
119 IMediaSample_SetTime(newsample, &rtSampleStart, &rtSampleStop);
121 pin->rtCurrent = pin->rtNext;
122 pin->rtNext = rtSampleStop;
124 IMediaSample_SetPreroll(newsample, FALSE);
125 IMediaSample_SetDiscontinuity(newsample, FALSE);
126 IMediaSample_SetSyncPoint(newsample, TRUE);
128 hr = IAsyncReader_Request(pin->pReader, newsample, 0);
131 if (SUCCEEDED(hr))
133 REFERENCE_TIME tAviStart, tAviStop;
135 IMediaSample_SetSyncPoint(pSample, TRUE);
136 pOutputPin->dwSamplesProcessed++;
138 tAviStart = bytepos_to_duration(This, tStart);
139 tAviStop = bytepos_to_duration(This, tStart + MEDIATIME_FROM_BYTES(IMediaSample_GetActualDataLength(pSample)));
141 IMediaSample_SetTime(pSample, &tAviStart, &tAviStop);
143 hr = BaseOutputPinImpl_Deliver(&pOutputPin->pin, pSample);
144 if (hr != S_OK && hr != S_FALSE && hr != VFW_E_WRONG_STATE)
145 ERR("Error sending sample (%x)\n", hr);
146 else if (hr != S_OK)
147 /* Unset progression if denied! */
148 This->Parser.pInputPin->rtCurrent = tStart;
151 if (tStop >= This->EndOfFile || (bytepos_to_duration(This, tStop) >= This->Parser.sourceSeeking.llStop) || hr == VFW_E_NOT_CONNECTED)
153 unsigned int i;
155 TRACE("End of file reached\n");
157 for (i = 0; i < This->Parser.cStreams; i++)
159 IPin* ppin;
160 HRESULT hr;
162 TRACE("Send End Of Stream to output pin %u\n", i);
164 hr = IPin_ConnectedTo(This->Parser.ppPins[i+1], &ppin);
165 if (SUCCEEDED(hr))
167 hr = IPin_EndOfStream(ppin);
168 IPin_Release(ppin);
170 if (FAILED(hr))
172 ERR("%x\n", hr);
173 break;
177 /* Force the pullpin thread to stop */
178 hr = S_FALSE;
181 return hr;
184 static HRESULT WAVEParser_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
186 if (!IsEqualIID(&pmt->majortype, &MEDIATYPE_Stream))
187 return S_FALSE;
188 if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_WAVE))
189 return S_OK;
190 if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_AU) || IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_AIFF))
191 FIXME("AU and AIFF files not supported yet!\n");
192 return S_FALSE;
195 static HRESULT WINAPI WAVEParserImpl_seek(IMediaSeeking *iface)
197 WAVEParserImpl *This = impl_from_IMediaSeeking(iface);
198 PullPin *pPin = This->Parser.pInputPin;
199 IPin *victim = NULL;
200 LONGLONG newpos, curpos, endpos, bytepos;
202 newpos = This->Parser.sourceSeeking.llCurrent;
203 curpos = bytepos_to_duration(This, pPin->rtCurrent);
204 endpos = bytepos_to_duration(This, This->EndOfFile);
205 bytepos = duration_to_bytepos(This, newpos);
207 if (newpos > endpos)
209 WARN("Requesting position %s beyond end of stream %s\n",
210 wine_dbgstr_longlong(newpos), wine_dbgstr_longlong(endpos));
211 return E_INVALIDARG;
214 if (curpos/1000000 == newpos/1000000)
216 TRACE("Requesting position %s same as current position %s\n",
217 wine_dbgstr_longlong(newpos), wine_dbgstr_longlong(curpos));
218 return S_OK;
221 TRACE("Moving sound to %08u bytes!\n", (DWORD)BYTES_FROM_MEDIATIME(bytepos));
223 EnterCriticalSection(&pPin->thread_lock);
224 IPin_BeginFlush(&pPin->pin.IPin_iface);
226 /* Make sure this is done while stopped, BeginFlush takes care of this */
227 EnterCriticalSection(&This->Parser.filter.csFilter);
228 IPin_ConnectedTo(This->Parser.ppPins[1], &victim);
229 if (victim)
231 IPin_NewSegment(victim, newpos, endpos, pPin->dRate);
232 IPin_Release(victim);
235 pPin->rtStart = pPin->rtCurrent = bytepos;
236 unsafe_impl_Parser_OutputPin_from_IPin(This->Parser.ppPins[1])->dwSamplesProcessed = 0;
237 LeaveCriticalSection(&This->Parser.filter.csFilter);
239 TRACE("Done flushing\n");
240 IPin_EndFlush(&pPin->pin.IPin_iface);
241 LeaveCriticalSection(&pPin->thread_lock);
243 return S_OK;
246 static HRESULT WAVEParser_InputPin_PreConnect(IPin * iface, IPin * pConnectPin, ALLOCATOR_PROPERTIES *props)
248 PullPin *This = impl_PullPin_from_IPin(iface);
249 HRESULT hr;
250 RIFFLIST list;
251 RIFFCHUNK chunk;
252 LONGLONG pos = 0; /* in bytes */
253 PIN_INFO piOutput;
254 AM_MEDIA_TYPE amt;
255 WAVEParserImpl * pWAVEParser = impl_from_IBaseFilter(This->pin.pinInfo.pFilter);
257 piOutput.dir = PINDIR_OUTPUT;
258 piOutput.pFilter = &pWAVEParser->Parser.filter.IBaseFilter_iface;
259 lstrcpynW(piOutput.achName, wcsOutputPinName, ARRAY_SIZE(piOutput.achName));
261 hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(list), (BYTE *)&list);
262 pos += sizeof(list);
264 if (list.fcc != FOURCC_RIFF)
266 ERR("Input stream not a RIFF file\n");
267 return E_FAIL;
269 if (list.cb > 1 * 1024 * 1024 * 1024) /* cannot be more than 1Gb in size */
271 ERR("Input stream violates RIFF spec\n");
272 return E_FAIL;
274 if (list.fccListType != mmioFOURCC('W','A','V','E'))
276 ERR("Input stream not an WAVE RIFF file\n");
277 return E_FAIL;
280 hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(chunk), (BYTE *)&chunk);
281 pos += sizeof(chunk);
282 if (chunk.fcc != mmioFOURCC('f','m','t',' '))
284 ERR("Expected 'fmt ' chunk, but got %.04s\n", (LPSTR)&chunk.fcc);
285 return E_FAIL;
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 IAsyncReader_SyncRead(This->pReader, pos, amt.cbFormat, amt.pbFormat);
294 amt.subtype = MEDIATYPE_Audio;
295 amt.subtype.Data1 = ((WAVEFORMATEX*)amt.pbFormat)->wFormatTag;
297 pos += chunk.cb;
298 hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(chunk), (BYTE *)&chunk);
299 if (chunk.fcc == mmioFOURCC('f','a','c','t'))
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);
305 if (chunk.fcc != mmioFOURCC('d','a','t','a'))
307 ERR("Expected 'data' chunk, but got %.04s\n", (LPSTR)&chunk.fcc);
308 return E_FAIL;
311 if (hr != S_OK)
312 return E_FAIL;
314 pWAVEParser->StartOfFile = MEDIATIME_FROM_BYTES(pos + sizeof(RIFFCHUNK));
315 pWAVEParser->EndOfFile = MEDIATIME_FROM_BYTES(pos + chunk.cb + sizeof(RIFFCHUNK));
317 props->cbAlign = ((WAVEFORMATEX*)amt.pbFormat)->nBlockAlign;
318 props->cbPrefix = 0;
319 props->cbBuffer = 4096;
320 props->cBuffers = 3;
321 pWAVEParser->nBlockAlign = ((WAVEFORMATEX*)amt.pbFormat)->nBlockAlign;
322 pWAVEParser->nAvgBytesPerSec = ((WAVEFORMATEX*)amt.pbFormat)->nAvgBytesPerSec;
323 hr = Parser_AddPin(&(pWAVEParser->Parser), &piOutput, props, &amt);
324 CoTaskMemFree(amt.pbFormat);
326 pWAVEParser->Parser.sourceSeeking.llCurrent = 0;
327 pWAVEParser->Parser.sourceSeeking.llStop = pWAVEParser->Parser.sourceSeeking.llDuration = bytepos_to_duration(pWAVEParser, pWAVEParser->EndOfFile);
328 TRACE("Duration: %u seconds\n", (DWORD)(pWAVEParser->Parser.sourceSeeking.llDuration / (LONGLONG)10000000));
330 This->rtStop = pWAVEParser->EndOfFile;
331 This->rtStart = pWAVEParser->StartOfFile;
333 TRACE("WAVE File ok\n");
335 return hr;
338 static HRESULT WAVEParser_Cleanup(LPVOID iface)
340 WAVEParserImpl *This = iface;
342 TRACE("(%p)->()\n", This);
344 return S_OK;
347 static HRESULT WAVEParser_first_request(LPVOID iface)
349 WAVEParserImpl *This = iface;
350 PullPin *pin = This->Parser.pInputPin;
351 HRESULT hr;
352 IMediaSample *sample;
354 if (pin->rtCurrent >= pin->rtStop)
356 /* Last sample has already been queued, request nothing more */
357 TRACE("Done!\n");
358 return S_OK;
361 hr = IMemAllocator_GetBuffer(pin->pAlloc, &sample, NULL, NULL, 0);
363 pin->rtNext = pin->rtCurrent;
364 if (SUCCEEDED(hr))
366 LONGLONG rtSampleStart = pin->rtNext;
367 /* Add 4 for the next header, which should hopefully work */
368 LONGLONG rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(IMediaSample_GetSize(sample));
369 Parser_OutputPin *outpin = unsafe_impl_Parser_OutputPin_from_IPin(This->Parser.ppPins[1]);
371 if (rtSampleStop > pin->rtStop)
372 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(pin->rtStop), pin->cbAlign));
374 IMediaSample_SetTime(sample, &rtSampleStart, &rtSampleStop);
376 pin->rtCurrent = pin->rtNext;
377 pin->rtNext = rtSampleStop;
379 IMediaSample_SetPreroll(sample, FALSE);
380 if (!outpin->dwSamplesProcessed++)
381 IMediaSample_SetDiscontinuity(sample, TRUE);
382 else
383 IMediaSample_SetDiscontinuity(sample, FALSE);
385 hr = IAsyncReader_Request(pin->pReader, sample, 0);
387 if (FAILED(hr))
388 ERR("Horsemen of the apocalypse came to bring error 0x%08x %p\n", hr, sample);
390 return hr;
393 static HRESULT WAVEParser_disconnect(LPVOID iface)
395 /* TODO: Find and plug memory leaks */
396 return S_OK;
399 static const IBaseFilterVtbl WAVEParser_Vtbl =
401 Parser_QueryInterface,
402 Parser_AddRef,
403 Parser_Release,
404 Parser_GetClassID,
405 Parser_Stop,
406 Parser_Pause,
407 Parser_Run,
408 Parser_GetState,
409 Parser_SetSyncSource,
410 Parser_GetSyncSource,
411 Parser_EnumPins,
412 BaseFilterImpl_FindPin,
413 Parser_QueryFilterInfo,
414 Parser_JoinFilterGraph,
415 Parser_QueryVendorInfo
418 HRESULT WAVEParser_create(IUnknown * pUnkOuter, LPVOID * ppv)
420 HRESULT hr;
421 WAVEParserImpl * This;
423 TRACE("(%p, %p)\n", pUnkOuter, ppv);
425 *ppv = NULL;
427 if (pUnkOuter)
428 return CLASS_E_NOAGGREGATION;
430 /* Note: This memory is managed by the transform filter once created */
431 This = CoTaskMemAlloc(sizeof(WAVEParserImpl));
433 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);
435 if (FAILED(hr))
436 return hr;
438 *ppv = &This->Parser.filter.IBaseFilter_iface;
440 return hr;