quartz: Store the stream index in the avi stream for files that have them.
[wine] / dlls / quartz / acmwrapper.c
1 /*
2  * ACM Wrapper
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 "config.h"
22
23 #include "quartz_private.h"
24 #include "pin.h"
25
26 #include "uuids.h"
27 #include "mmreg.h"
28 #include "windef.h"
29 #include "winbase.h"
30 #include "dshow.h"
31 #include "strmif.h"
32 #include "vfwmsgs.h"
33 #include "msacm.h"
34
35 #include <assert.h>
36
37 #include "wine/unicode.h"
38 #include "wine/debug.h"
39
40 #include "transform.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
43
44 typedef struct ACMWrapperImpl
45 {
46     TransformFilterImpl tf;
47     HACMSTREAM has;
48     LPWAVEFORMATEX pWfIn;
49     LPWAVEFORMATEX pWfOut;
50 } ACMWrapperImpl;
51
52 static HRESULT ACMWrapper_ProcessSampleData(TransformFilterImpl* pTransformFilter, IMediaSample *pSample)
53 {
54     ACMWrapperImpl* This = (ACMWrapperImpl*)pTransformFilter;
55     AM_MEDIA_TYPE amt;
56     IMediaSample* pOutSample = NULL;
57     DWORD cbDstStream, cbSrcStream;
58     LPBYTE pbDstStream;
59     LPBYTE pbSrcStream = NULL;
60     ACMSTREAMHEADER ash;
61     BOOL unprepare_header = FALSE;
62     MMRESULT res;
63     HRESULT hr;
64     LONGLONG tStart = -1, tStop = -1, tMed;
65
66     hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
67     if (FAILED(hr))
68     {
69         ERR("Cannot get pointer to sample data (%x)\n", hr);
70         return hr;
71     }
72
73     IMediaSample_GetTime(pSample, &tStart, &tStop);
74     cbSrcStream = IMediaSample_GetActualDataLength(pSample);
75
76     TRACE("Sample data ptr = %p, size = %ld\n", pbSrcStream, (long)cbSrcStream);
77
78     hr = IPin_ConnectionMediaType(This->tf.ppPins[0], &amt);
79     if (FAILED(hr)) {
80         ERR("Unable to retrieve media type\n");
81         return hr;
82     }
83
84     ash.pbSrc = pbSrcStream;
85     ash.cbSrcLength = cbSrcStream;
86
87     while(hr == S_OK && ash.cbSrcLength)
88     {
89         hr = OutputPin_GetDeliveryBuffer((OutputPin*)This->tf.ppPins[1], &pOutSample, NULL, NULL, 0);
90         if (FAILED(hr)) {
91             ERR("Unable to get delivery buffer (%x)\n", hr);
92             return hr;
93         }
94
95         hr = IMediaSample_SetActualDataLength(pOutSample, 0);
96         assert(hr == S_OK);
97
98         hr = IMediaSample_GetPointer(pOutSample, &pbDstStream);
99         if (FAILED(hr)) {
100             ERR("Unable to get pointer to buffer (%x)\n", hr);
101             goto error;
102         }
103         cbDstStream = IMediaSample_GetSize(pOutSample);
104
105         ash.cbStruct = sizeof(ash);
106         ash.fdwStatus = 0;
107         ash.dwUser = 0;
108         ash.pbDst = pbDstStream;
109         ash.cbDstLength = cbDstStream;
110
111         if ((res = acmStreamPrepareHeader(This->has, &ash, 0))) {
112             ERR("Cannot prepare header %d\n", res);
113             goto error;
114         }
115         unprepare_header = TRUE;
116
117         if (IMediaSample_IsDiscontinuity(pSample) == S_OK)
118             res = acmStreamConvert(This->has, &ash, ACM_STREAMCONVERTF_START);
119         else
120             res = acmStreamConvert(This->has, &ash, 0);
121
122
123         if (res)
124         {
125             if(res != MMSYSERR_MOREDATA)
126                 ERR("Cannot convert data header %d\n", res);
127             goto error;
128         }
129
130         TRACE("used in %u/%u, used out %u/%u\n", ash.cbSrcLengthUsed, ash.cbSrcLength, ash.cbDstLengthUsed, ash.cbDstLength);
131
132         hr = IMediaSample_SetActualDataLength(pOutSample, ash.cbDstLengthUsed);
133         assert(hr == S_OK);
134
135         if (!ash.cbSrcLengthUsed)
136         {
137             WARN("Sample was skipped\n");
138             ash.cbSrcLength = 0;
139             goto error;
140         }
141
142         TRACE("Sample start time: %u.%03u\n", (DWORD)(tStart/10000000), (DWORD)((tStart/10000)%1000));
143         if (ash.cbSrcLengthUsed == cbSrcStream)
144         {
145             IMediaSample_SetTime(pOutSample, &tStart, &tStop);
146             tStart = tStop;
147         }
148         else if (tStop != tStart)
149         {
150             tMed = tStop - tStart;
151             tMed = tStart + tMed * ash.cbSrcLengthUsed / cbSrcStream;
152             IMediaSample_SetTime(pOutSample, &tStart, &tMed);
153             tStart = tMed;
154         }
155         else
156         {
157             ERR("No valid timestamp found\n");
158             IMediaSample_SetTime(pOutSample, NULL, NULL);
159         }
160         TRACE("Sample stop time: %u.%03u\n", (DWORD)(tStart/10000000), (DWORD)((tStart/10000)%1000));
161
162         if (IMediaSample_IsDiscontinuity(pSample) == S_FALSE)
163             hr = OutputPin_SendSample((OutputPin*)This->tf.ppPins[1], pOutSample);
164         else
165             TRACE("Skipping first sample: Discontinuity\n");
166
167         if (hr != S_OK && hr != VFW_E_NOT_CONNECTED) {
168             if (FAILED(hr))
169                 ERR("Error sending sample (%x)\n", hr);
170             goto error;
171         }
172
173 error:
174         if (unprepare_header && (res = acmStreamUnprepareHeader(This->has, &ash, 0)))
175             ERR("Cannot unprepare header %d\n", res);
176         unprepare_header = FALSE;
177         ash.pbSrc += ash.cbSrcLengthUsed;
178         ash.cbSrcLength -= ash.cbSrcLengthUsed;
179
180         if (pOutSample)
181             IMediaSample_Release(pOutSample);
182         pOutSample = NULL;
183     }
184
185     return hr;
186 }
187
188 static HRESULT ACMWrapper_ConnectInput(TransformFilterImpl* pTransformFilter, const AM_MEDIA_TYPE * pmt)
189 {
190     ACMWrapperImpl* This = (ACMWrapperImpl*)pTransformFilter;
191     MMRESULT res;
192
193     TRACE("(%p)->(%p)\n", This, pmt);
194
195     /* Check root (GUID w/o FOURCC) */
196     if ((IsEqualIID(&pmt->majortype, &MEDIATYPE_Audio)) &&
197         (!memcmp(((const char *)&pmt->subtype)+4, ((const char *)&MEDIATYPE_Audio)+4, sizeof(GUID)-4)) &&
198         (IsEqualIID(&pmt->formattype, &FORMAT_WaveFormatEx)))
199     {
200         HACMSTREAM drv;
201         AM_MEDIA_TYPE* outpmt = &((OutputPin*)This->tf.ppPins[1])->pin.mtCurrent;
202         This->pWfIn = (LPWAVEFORMATEX)pmt->pbFormat;
203
204         /* HACK */
205         /* TRACE("ALIGN = %d\n", pACMWrapper->pWfIn->nBlockAlign); */
206         /* pACMWrapper->pWfIn->nBlockAlign = 1; */
207
208         /* Set output audio data to PCM */
209         CopyMediaType(outpmt, pmt);
210         outpmt->subtype.Data1 = WAVE_FORMAT_PCM;
211         This->pWfOut = (WAVEFORMATEX*)outpmt->pbFormat;
212         This->pWfOut->wFormatTag = WAVE_FORMAT_PCM;
213         This->pWfOut->wBitsPerSample = 16;
214         This->pWfOut->nBlockAlign = 4;
215         This->pWfOut->cbSize = 0;
216         This->pWfOut->nAvgBytesPerSec = This->pWfOut->nChannels * This->pWfOut->nSamplesPerSec
217                                                 * (This->pWfOut->wBitsPerSample/8);
218
219         if (!(res = acmStreamOpen(&drv, NULL, This->pWfIn, This->pWfOut, NULL, 0, 0, 0)))
220         {
221             This->has = drv;
222
223             /* Update buffer size of media samples in output */
224             ((OutputPin*)This->tf.ppPins[1])->allocProps.cbBuffer = This->pWfOut->nAvgBytesPerSec / 2;
225             TRACE("Connection accepted\n");
226             return S_OK;
227         }
228         else
229             FIXME("acmStreamOpen returned %d\n", res);
230         FreeMediaType(outpmt);
231         TRACE("Unable to find a suitable ACM decompressor\n");
232     }
233
234     TRACE("Connection refused\n");
235     return VFW_E_TYPE_NOT_ACCEPTED;
236 }
237
238 static HRESULT ACMWrapper_Cleanup(TransformFilterImpl* pTransformFilter)
239 {
240     ACMWrapperImpl* This = (ACMWrapperImpl*)pTransformFilter;
241
242     TRACE("(%p)->()\n", This);
243     
244     if (This->has)
245         acmStreamClose(This->has, 0);
246
247     This->has = 0;
248     
249     return S_OK;
250 }
251
252 static const TransformFuncsTable ACMWrapper_FuncsTable = {
253     NULL,
254     ACMWrapper_ProcessSampleData,
255     NULL,
256     NULL,
257     ACMWrapper_ConnectInput,
258     ACMWrapper_Cleanup
259 };
260
261 HRESULT ACMWrapper_create(IUnknown * pUnkOuter, LPVOID * ppv)
262 {
263     HRESULT hr;
264     ACMWrapperImpl* This;
265
266     TRACE("(%p, %p)\n", pUnkOuter, ppv);
267
268     *ppv = NULL;
269
270     if (pUnkOuter)
271         return CLASS_E_NOAGGREGATION;
272
273     /* Note: This memory is managed by the transform filter once created */
274     This = CoTaskMemAlloc(sizeof(ACMWrapperImpl));
275     ZeroMemory(This, sizeof(ACMWrapperImpl));
276
277     hr = TransformFilter_Create(&(This->tf), &CLSID_ACMWrapper, &ACMWrapper_FuncsTable, NULL, NULL, NULL);
278
279     if (FAILED(hr))
280         return hr;
281
282     *ppv = (LPVOID)This;
283
284     return hr;
285 }