quartz: Only allocate 1 buffer in transform filter.
[wine/wine64.git] / dlls / quartz / waveparser.c
blobdbc096cdb044545ec5f943f0b1612b927cfcb154
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 "control_private.h"
23 #include "pin.h"
25 #include "uuids.h"
26 #include "aviriff.h"
27 #include "vfwmsgs.h"
28 #include "mmsystem.h"
30 #include "wine/unicode.h"
31 #include "wine/debug.h"
33 #include <math.h>
34 #include <assert.h>
36 #include "parser.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
40 static const WCHAR wcsOutputPinName[] = {'o','u','t','p','u','t',' ','p','i','n',0};
42 typedef struct WAVEParserImpl
44 ParserImpl Parser;
45 LONGLONG StartOfFile; /* in media time */
46 LONGLONG EndOfFile;
47 DWORD dwSampleSize;
48 DWORD nSamplesPerSec;
49 DWORD dwLength;
50 } WAVEParserImpl;
52 static LONGLONG bytepos_to_duration(WAVEParserImpl *This, LONGLONG bytepos)
54 LONGLONG duration = BYTES_FROM_MEDIATIME(bytepos - This->StartOfFile);
55 duration *= 10000000;
56 duration /= (This->dwSampleSize * This->nSamplesPerSec);
58 return duration;
61 static LONGLONG duration_to_bytepos(WAVEParserImpl *This, LONGLONG duration)
63 LONGLONG bytepos;
65 bytepos = (This->dwSampleSize * This->nSamplesPerSec);
66 bytepos *= duration;
67 bytepos /= 10000000;
68 bytepos += BYTES_FROM_MEDIATIME(This->StartOfFile);
69 bytepos -= bytepos % This->dwSampleSize;
71 return MEDIATIME_FROM_BYTES(bytepos);
74 static HRESULT WAVEParser_Sample(LPVOID iface, IMediaSample * pSample, DWORD_PTR cookie)
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;
85 IMediaSample_GetPointer(pSample, &pbSrcStream);
86 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
88 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
90 /* Flush occurring */
91 if (cbSrcStream == 0)
93 TRACE(".. Why do I need you?\n");
94 return S_OK;
97 pOutputPin = (Parser_OutputPin *)This->Parser.ppPins[1];
99 if (SUCCEEDED(hr))
100 hr = IMemAllocator_GetBuffer(pin->pAlloc, &newsample, NULL, NULL, 0);
102 if (SUCCEEDED(hr))
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));
108 if (rtSampleStop > pin->rtStop)
109 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(pin->rtStop), pin->cbAlign));
111 hr = IMediaSample_SetTime(newsample, &rtSampleStart, &rtSampleStop);
113 pin->rtCurrent = pin->rtNext;
114 pin->rtNext = rtSampleStop;
116 IMediaSample_SetPreroll(newsample, 0);
117 IMediaSample_SetDiscontinuity(newsample, 0);
118 IMediaSample_SetSyncPoint(newsample, 1);
120 hr = IAsyncReader_Request(pin->pReader, newsample, 0);
123 if (SUCCEEDED(hr))
125 REFERENCE_TIME tAviStart, tAviStop;
127 IMediaSample_SetSyncPoint(pSample, TRUE);
128 pOutputPin->dwSamplesProcessed++;
130 tAviStart = bytepos_to_duration(This, tStart);
131 tAviStop = bytepos_to_duration(This, tStart + MEDIATIME_FROM_BYTES(IMediaSample_GetActualDataLength(pSample)));
133 IMediaSample_SetTime(pSample, &tAviStart, &tAviStop);
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;
143 if (tStop >= This->EndOfFile || (bytepos_to_duration(This, tStop) >= This->Parser.mediaSeeking.llStop) || hr == VFW_E_NOT_CONNECTED)
145 int i;
147 TRACE("End of file reached\n");
149 for (i = 0; i < This->Parser.cStreams; i++)
151 IPin* ppin;
152 HRESULT hr;
154 TRACE("Send End Of Stream to output pin %d\n", i);
156 hr = IPin_ConnectedTo(This->Parser.ppPins[i+1], &ppin);
157 if (SUCCEEDED(hr))
159 hr = IPin_EndOfStream(ppin);
160 IPin_Release(ppin);
162 if (FAILED(hr))
164 ERR("%x\n", hr);
165 break;
169 /* Force the pullpin thread to stop */
170 hr = S_FALSE;
173 return hr;
176 static HRESULT WAVEParser_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
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;
187 static HRESULT WAVEParserImpl_seek(IBaseFilter *iface)
189 WAVEParserImpl *This = (WAVEParserImpl *)iface;
190 PullPin *pPin = This->Parser.pInputPin;
191 IPin *victim = NULL;
192 LONGLONG newpos, curpos, endpos, bytepos;
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);
199 if (newpos > endpos)
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;
205 if (curpos/1000000 == newpos/1000000)
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;
211 TRACE("Moving sound to %08u bytes!\n", (DWORD)BYTES_FROM_MEDIATIME(bytepos));
213 EnterCriticalSection(&pPin->thread_lock);
214 IPin_BeginFlush((IPin *)pPin);
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)
221 IPin_NewSegment(victim, newpos, endpos, pPin->dRate);
222 IPin_Release(victim);
225 pPin->rtStart = pPin->rtCurrent = bytepos;
226 ((Parser_OutputPin *)This->Parser.ppPins[1])->dwSamplesProcessed = 0;
227 LeaveCriticalSection(&This->Parser.csFilter);
229 TRACE("Done flushing\n");
230 IPin_EndFlush((IPin *)pPin);
231 LeaveCriticalSection(&pPin->thread_lock);
233 return S_OK;
236 static HRESULT WAVEParser_InputPin_PreConnect(IPin * iface, IPin * pConnectPin, ALLOCATOR_PROPERTIES *props)
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;
248 piOutput.dir = PINDIR_OUTPUT;
249 piOutput.pFilter = (IBaseFilter *)This;
250 lstrcpynW(piOutput.achName, wcsOutputPinName, sizeof(piOutput.achName) / sizeof(piOutput.achName[0]));
252 hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(list), (BYTE *)&list);
253 pos += sizeof(list);
255 if (list.fcc != FOURCC_RIFF)
257 ERR("Input stream not a RIFF file\n");
258 return E_FAIL;
260 if (list.cb > 1 * 1024 * 1024 * 1024) /* cannot be more than 1Gb in size */
262 ERR("Input stream violates RIFF spec\n");
263 return E_FAIL;
265 if (list.fccListType != mmioFOURCC('W','A','V','E'))
267 ERR("Input stream not an WAVE RIFF file\n");
268 return E_FAIL;
271 hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(chunk), (BYTE *)&chunk);
272 pos += sizeof(chunk);
273 if (chunk.fcc != mmioFOURCC('f','m','t',' '))
275 ERR("Expected 'fmt ' chunk, but got %.04s\n", (LPSTR)&chunk.fcc);
276 return E_FAIL;
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;
288 pos += chunk.cb;
289 hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(chunk), (BYTE *)&chunk);
290 if (chunk.fcc == mmioFOURCC('f','a','c','t'))
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);
296 if (chunk.fcc != mmioFOURCC('d','a','t','a'))
298 ERR("Expected 'data' chunk, but got %.04s\n", (LPSTR)&chunk.fcc);
299 return E_FAIL;
302 if (hr == S_OK)
304 pWAVEParser->StartOfFile = MEDIATIME_FROM_BYTES(pos + sizeof(RIFFCHUNK));
305 pWAVEParser->EndOfFile = MEDIATIME_FROM_BYTES(pos + chunk.cb + sizeof(RIFFCHUNK));
308 if (hr != S_OK)
309 return E_FAIL;
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);
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));
326 This->rtStop = pWAVEParser->EndOfFile;
327 This->rtStart = pWAVEParser->StartOfFile;
329 TRACE("WAVE File ok\n");
331 return hr;
334 static HRESULT WAVEParser_Cleanup(LPVOID iface)
336 WAVEParserImpl *This = (WAVEParserImpl*)iface;
338 TRACE("(%p)->()\n", This);
340 return S_OK;
343 static HRESULT WAVEParser_first_request(LPVOID iface)
345 WAVEParserImpl *This = (WAVEParserImpl *)iface;
346 PullPin *pin = This->Parser.pInputPin;
347 HRESULT hr;
348 IMediaSample *sample;
350 if (pin->rtCurrent >= pin->rtStop)
352 /* Last sample has already been queued, request nothing more */
353 TRACE("Done!\n");
354 return S_OK;
357 hr = IMemAllocator_GetBuffer(pin->pAlloc, &sample, NULL, NULL, 0);
359 pin->rtNext = pin->rtCurrent;
360 if (SUCCEEDED(hr))
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];
367 if (rtSampleStop > pin->rtStop)
368 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(pin->rtStop), pin->cbAlign));
370 hr = IMediaSample_SetTime(sample, &rtSampleStart, &rtSampleStop);
372 pin->rtCurrent = pin->rtNext;
373 pin->rtNext = rtSampleStop;
375 IMediaSample_SetPreroll(sample, FALSE);
376 if (!outpin->dwSamplesProcessed++)
377 IMediaSample_SetDiscontinuity(sample, TRUE);
378 else
379 IMediaSample_SetDiscontinuity(sample, FALSE);
381 hr = IAsyncReader_Request(pin->pReader, sample, 0);
383 if (FAILED(hr))
384 ERR("Horsemen of the apocalypse came to bring error 0x%08x %p\n", hr, sample);
386 return hr;
389 static HRESULT WAVEParser_disconnect(LPVOID iface)
391 /* TODO: Find and plug memory leaks */
392 return S_OK;
395 static const IBaseFilterVtbl WAVEParser_Vtbl =
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
414 HRESULT WAVEParser_create(IUnknown * pUnkOuter, LPVOID * ppv)
416 HRESULT hr;
417 WAVEParserImpl * This;
419 TRACE("(%p, %p)\n", pUnkOuter, ppv);
421 *ppv = NULL;
423 if (pUnkOuter)
424 return CLASS_E_NOAGGREGATION;
426 /* Note: This memory is managed by the transform filter once created */
427 This = CoTaskMemAlloc(sizeof(WAVEParserImpl));
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);
431 if (FAILED(hr))
432 return hr;
434 *ppv = (LPVOID)This;
436 return hr;