Reverse the order for deleting the items in resetcontent to correctly
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 "mmreg.h"
28 #include "vfwmsgs.h"
29 #include "mmsystem.h"
30
31 #include "fourcc.h"
32
33 #include "wine/unicode.h"
34 #include "wine/debug.h"
35
36 #include <math.h>
37 #include <assert.h>
38
39 #include "parser.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
42
43 static const WCHAR wcsOutputPinName[] = {'o','u','t','p','u','t',' ','p','i','n',0};
44
45 typedef struct WAVEParserImpl
46 {
47     ParserImpl Parser;
48     IMediaSample * pCurrentSample;
49     LONGLONG StartOfFile; /* in media time */
50     LONGLONG EndOfFile;
51 } WAVEParserImpl;
52
53 static HRESULT WAVEParser_Sample(LPVOID iface, IMediaSample * pSample)
54 {
55     WAVEParserImpl *This = (WAVEParserImpl *)iface;
56     LPBYTE pbSrcStream = NULL;
57     long cbSrcStream = 0;
58     REFERENCE_TIME tStart, tStop;
59     HRESULT hr;
60     BOOL bMoreData = TRUE;
61     Parser_OutputPin * pOutputPin;
62     BYTE * pbDstStream;
63     long cbDstStream;
64     long chunk_remaining_bytes = 0;
65     long offset_src = 0;
66  
67     hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
68
69     hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
70
71     cbSrcStream = IMediaSample_GetActualDataLength(pSample);
72
73     assert(BYTES_FROM_MEDIATIME(tStop - tStart) == cbSrcStream);
74
75     pOutputPin = (Parser_OutputPin *)This->Parser.ppPins[1];
76
77     if (tStop < This->StartOfFile)
78         return S_OK;
79
80     if (tStart < This->StartOfFile)
81         offset_src = BYTES_FROM_MEDIATIME(This->StartOfFile - tStart);
82
83     while (bMoreData)
84     {
85         if (!This->pCurrentSample)
86         {
87             /* cache media sample until it is ready to be despatched
88              * (i.e. we reach the end of the chunk) */
89             hr = OutputPin_GetDeliveryBuffer(&pOutputPin->pin, &This->pCurrentSample, NULL, NULL, 0);
90
91             if (SUCCEEDED(hr))
92             {
93                 hr = IMediaSample_SetActualDataLength(This->pCurrentSample, 0);
94                 assert(hr == S_OK);
95             }
96             else
97             {
98                 TRACE("Skipping sending sample due to error (%lx)\n", hr);
99                 This->pCurrentSample = NULL;
100                 return hr;
101             }
102         }
103
104         hr = IMediaSample_GetPointer(This->pCurrentSample, &pbDstStream);
105
106         if (SUCCEEDED(hr))
107         {
108             cbDstStream = IMediaSample_GetSize(This->pCurrentSample);
109
110             chunk_remaining_bytes = cbDstStream - IMediaSample_GetActualDataLength(This->pCurrentSample);
111         
112             assert(chunk_remaining_bytes >= 0);
113             assert(chunk_remaining_bytes <= cbDstStream - IMediaSample_GetActualDataLength(This->pCurrentSample));
114         }
115
116         if (chunk_remaining_bytes <= cbSrcStream - offset_src)
117         {
118             if (SUCCEEDED(hr))
119             {
120                 memcpy(pbDstStream + IMediaSample_GetActualDataLength(This->pCurrentSample), pbSrcStream + offset_src, chunk_remaining_bytes);
121                 hr = IMediaSample_SetActualDataLength(This->pCurrentSample, chunk_remaining_bytes + IMediaSample_GetActualDataLength(This->pCurrentSample));
122                 assert(hr == S_OK);
123             }
124
125             if (SUCCEEDED(hr))
126             {
127                 REFERENCE_TIME tAviStart, tAviStop;
128
129                 /* FIXME: hack */
130                 if (pOutputPin->dwSamplesProcessed == 0) {
131                     IMediaSample_SetDiscontinuity(This->pCurrentSample, TRUE);
132                     IMediaSample_SetSyncPoint(This->pCurrentSample, TRUE);
133                 }
134
135                 pOutputPin->dwSamplesProcessed++;
136
137                 if (pOutputPin->dwSampleSize)
138                     tAviStart = (LONGLONG)ceil(10000000.0 * (float)(pOutputPin->dwSamplesProcessed - 1) * (float)IMediaSample_GetActualDataLength(This->pCurrentSample) / ((float)pOutputPin->dwSampleSize * pOutputPin->fSamplesPerSec));
139                 else
140                     tAviStart = (LONGLONG)ceil(10000000.0 * (float)(pOutputPin->dwSamplesProcessed - 1) / (float)pOutputPin->fSamplesPerSec);
141                 if (pOutputPin->dwSampleSize)
142                     tAviStop = (LONGLONG)ceil(10000000.0 * (float)pOutputPin->dwSamplesProcessed * (float)IMediaSample_GetActualDataLength(This->pCurrentSample) / ((float)pOutputPin->dwSampleSize * pOutputPin->fSamplesPerSec));
143                 else
144                     tAviStop = (LONGLONG)ceil(10000000.0 * (float)pOutputPin->dwSamplesProcessed / (float)pOutputPin->fSamplesPerSec);
145
146                 IMediaSample_SetTime(This->pCurrentSample, &tAviStart, &tAviStop);
147
148                 hr = OutputPin_SendSample(&pOutputPin->pin, This->pCurrentSample);
149                 if (hr != S_OK && hr != VFW_E_NOT_CONNECTED)
150                     ERR("Error sending sample (%lx)\n", hr);
151             }
152
153             if (This->pCurrentSample)
154                 IMediaSample_Release(This->pCurrentSample);
155                     
156             This->pCurrentSample = NULL;
157         }
158         else
159         {
160             if (SUCCEEDED(hr))
161             {
162                 memcpy(pbDstStream + IMediaSample_GetActualDataLength(This->pCurrentSample), pbSrcStream + offset_src, cbSrcStream - offset_src);
163                 IMediaSample_SetActualDataLength(This->pCurrentSample, cbSrcStream - offset_src + IMediaSample_GetActualDataLength(This->pCurrentSample));
164             }
165             bMoreData = FALSE;
166         }
167         offset_src += chunk_remaining_bytes;
168     }
169
170     return hr;
171 }
172
173 static HRESULT WAVEParser_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
174 {
175     if (!IsEqualIID(&pmt->majortype, &MEDIATYPE_Stream))
176         return S_FALSE;
177     if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_WAVE))
178         return S_OK;
179     if (IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_AU) || IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_AIFF))
180         FIXME("AU and AIFF files not supported yet!\n");
181     return S_FALSE;
182 }
183
184 static HRESULT WAVEParser_InputPin_PreConnect(IPin * iface, IPin * pConnectPin)
185 {
186     PullPin *This = (PullPin *)iface;
187     HRESULT hr;
188     RIFFLIST list;
189     RIFFCHUNK chunk;
190     LONGLONG pos = 0; /* in bytes */
191     PIN_INFO piOutput;
192     ALLOCATOR_PROPERTIES props;
193     AM_MEDIA_TYPE amt;
194     float fSamplesPerSec = 0.0f;
195     DWORD dwSampleSize = 0;
196     DWORD dwLength = 0;
197     WAVEParserImpl * pWAVEParser = (WAVEParserImpl *)This->pin.pinInfo.pFilter;
198
199     piOutput.dir = PINDIR_OUTPUT;
200     piOutput.pFilter = (IBaseFilter *)This;
201     strncpyW(piOutput.achName, wcsOutputPinName, sizeof(piOutput.achName) / sizeof(piOutput.achName[0]));
202     
203     hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(list), (BYTE *)&list);
204     pos += sizeof(list);
205
206     if (list.fcc != ckidRIFF)
207     {
208         ERR("Input stream not a RIFF file\n");
209         return E_FAIL;
210     }
211     if (list.cb > 1 * 1024 * 1024 * 1024) /* cannot be more than 1Gb in size */
212     {
213         ERR("Input stream violates RIFF spec\n");
214         return E_FAIL;
215     }
216     if (list.fccListType != mmioFOURCC('W','A','V','E'))
217     {
218         ERR("Input stream not an WAVE RIFF file\n");
219         return E_FAIL;
220     }
221
222     hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(chunk), (BYTE *)&chunk);
223     pos += sizeof(chunk);
224     if (chunk.fcc != mmioFOURCC('f','m','t',' '))
225     {
226         ERR("Expected 'fmt ' chunk, but got %.04s\n", (LPSTR)&chunk.fcc);
227         return E_FAIL;
228     }
229
230     memcpy(&amt.majortype, &MEDIATYPE_Audio, sizeof(GUID));
231     memcpy(&amt.formattype, &FORMAT_WaveFormatEx, sizeof(GUID));
232     amt.cbFormat = chunk.cb;
233     amt.pbFormat = CoTaskMemAlloc(amt.cbFormat);
234     amt.pUnk = NULL;
235     hr = IAsyncReader_SyncRead(This->pReader, pos, amt.cbFormat, amt.pbFormat);
236     memcpy(&amt.subtype, &MEDIATYPE_Audio, sizeof(GUID));
237     amt.subtype.Data1 = ((WAVEFORMATEX*)amt.pbFormat)->wFormatTag;
238     /* CopyMediaType(&((OutputPin*)pWAVEParser->ppPins[1])->pin.mtCurrent, &amt); */
239     ((Parser_OutputPin*)pWAVEParser->Parser.ppPins[1])->pmt = (AM_MEDIA_TYPE*) CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
240     
241     CopyMediaType(((Parser_OutputPin*)pWAVEParser->Parser.ppPins[1])->pmt, &amt);
242
243     /* Update buffer alignment of media samples in output */
244     ((Parser_OutputPin*)pWAVEParser->Parser.ppPins[1])->pin.allocProps.cbAlign = ((WAVEFORMATEX*)amt.pbFormat)->nBlockAlign;
245
246     pos += chunk.cb;
247     hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(chunk), (BYTE *)&chunk);
248     if (chunk.fcc == mmioFOURCC('f','a','c','t'))
249     {
250         FIXME("'fact' chunk not supported yet\n");
251         pos += sizeof(chunk) + chunk.cb;
252         hr = IAsyncReader_SyncRead(This->pReader, pos, sizeof(chunk), (BYTE *)&chunk);
253     }
254     if (chunk.fcc != mmioFOURCC('d','a','t','a'))
255     {
256         ERR("Expected 'data' chunk, but got %.04s\n", (LPSTR)&chunk.fcc);
257         return E_FAIL;
258     }
259
260     if (hr == S_OK)
261     {
262         pWAVEParser->StartOfFile = MEDIATIME_FROM_BYTES(pos + sizeof(RIFFCHUNK));
263         pWAVEParser->EndOfFile = MEDIATIME_FROM_BYTES(pos + chunk.cb + sizeof(RIFFCHUNK));
264     }
265
266     if (hr != S_OK)
267         return E_FAIL;
268
269     props.cbAlign = 1;
270     props.cbPrefix = 0;
271     props.cbBuffer = 4096;
272     props.cBuffers = 2;
273     
274     hr = Parser_AddPin(&(pWAVEParser->Parser), &piOutput, &props, &amt, fSamplesPerSec, dwSampleSize, dwLength);
275     
276     TRACE("WAVE File ok\n");
277
278     return hr;
279 }
280
281 HRESULT WAVEParser_create(IUnknown * pUnkOuter, LPVOID * ppv)
282 {
283     HRESULT hr;
284     WAVEParserImpl * This;
285
286     TRACE("(%p, %p)\n", pUnkOuter, ppv);
287
288     *ppv = NULL;
289
290     if (pUnkOuter)
291         return CLASS_E_NOAGGREGATION;
292
293     /* Note: This memory is managed by the transform filter once created */
294     This = CoTaskMemAlloc(sizeof(WAVEParserImpl));
295
296     This->pCurrentSample = NULL;
297
298     hr = Parser_Create(&(This->Parser), &CLSID_WAVEParser, WAVEParser_Sample, WAVEParser_QueryAccept, WAVEParser_InputPin_PreConnect);
299
300     if (FAILED(hr))
301         return hr;
302
303     *ppv = (LPVOID)This;
304
305     return hr;
306 }