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