quartz: Pass the media sample to the individual transform filter callbacks.
[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, IMediaSample *pSample)
63 {
64     ACMWrapperImpl* This = (ACMWrapperImpl*)pTransformFilter;
65     AM_MEDIA_TYPE amt;
66     IMediaSample* pOutSample = NULL;
67     DWORD cbDstStream;
68     LPBYTE pbDstStream;
69     DWORD cbSrcStream = 0;
70     LPBYTE pbSrcStream = NULL;
71     ACMSTREAMHEADER ash;
72     DWORD offset = 0;
73     BOOL stop = FALSE;
74     BOOL unprepare_header = FALSE;
75     MMRESULT res;
76     HRESULT hr;
77
78     hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
79     if (FAILED(hr))
80     {
81         ERR("Cannot get pointer to sample data (%x)\n", hr);
82         return hr;
83     }
84
85     cbSrcStream = IMediaSample_GetActualDataLength(pSample);
86
87     TRACE("Sample data ptr = %p, size = %ld\n", pbSrcStream, (long)cbSrcStream);
88
89     hr = IPin_ConnectionMediaType(This->tf.ppPins[0], &amt);
90     if (FAILED(hr)) {
91         ERR("Unable to retrieve media type\n");
92         return hr;
93     }
94
95     while(hr == S_OK && !stop)
96     {
97         DWORD rem_buf = This->max_size - This->current_size;
98         DWORD rem_smp = cbSrcStream - offset;
99         DWORD copy_size = min(rem_buf, rem_smp);
100
101         memcpy(This->buffer + This->current_size, pbSrcStream + offset, copy_size);
102         This->current_size += copy_size;
103         offset += copy_size;
104
105         if (offset >= cbSrcStream)
106             stop = TRUE;
107         if (This->current_size < This->max_size)
108             break;
109   
110         hr = OutputPin_GetDeliveryBuffer((OutputPin*)This->tf.ppPins[1], &pOutSample, NULL, NULL, 0);
111         if (FAILED(hr)) {
112             ERR("Unable to get delivery buffer (%x)\n", hr);
113             return hr;
114         }
115
116         hr = IMediaSample_SetActualDataLength(pOutSample, 0);
117         assert(hr == S_OK);
118
119         hr = IMediaSample_GetPointer(pOutSample, &pbDstStream);
120         if (FAILED(hr)) {
121             ERR("Unable to get pointer to buffer (%x)\n", hr);
122             goto error;
123         }
124         cbDstStream = IMediaSample_GetSize(pOutSample);
125
126         ash.cbStruct = sizeof(ash);
127         ash.fdwStatus = 0;
128         ash.dwUser = 0;
129         ash.pbSrc = This->buffer;
130         ash.cbSrcLength = This->current_size;
131         ash.pbDst = pbDstStream;
132         ash.cbDstLength = cbDstStream;
133
134         if ((res = acmStreamPrepareHeader(This->has, &ash, 0))) {
135             ERR("Cannot prepare header %d\n", res);
136             goto error;
137         }
138
139         unprepare_header = TRUE;
140
141         if ((res = acmStreamConvert(This->has, &ash, This->reinit_codec ? ACM_STREAMCONVERTF_START : 0))) {
142             ERR("Cannot convert data header %d\n", res);
143             goto error;
144         }
145         This->reinit_codec = FALSE;
146
147         TRACE("used in %u, used out %u\n", ash.cbSrcLengthUsed, ash.cbDstLengthUsed);
148
149         hr = IMediaSample_SetActualDataLength(pOutSample, ash.cbDstLengthUsed);
150         assert(hr == S_OK);
151
152         if (ash.cbSrcLengthUsed < ash.cbSrcLength) {
153             This->current_size = ash.cbSrcLength - ash.cbSrcLengthUsed;
154             memmove(This->buffer, This->buffer + ash.cbSrcLengthUsed, This->current_size);
155         }
156         else
157             This->current_size = 0;
158
159         hr = OutputPin_SendSample((OutputPin*)This->tf.ppPins[1], pOutSample);
160         if (hr != S_OK && hr != VFW_E_NOT_CONNECTED) {
161             ERR("Error sending sample (%x)\n", hr);
162             goto error;
163         }
164
165 error:
166         if (unprepare_header && (res = acmStreamUnprepareHeader(This->has, &ash, 0)))
167             ERR("Cannot unprepare header %d\n", res);
168         unprepare_header = FALSE;
169
170         if (pOutSample)
171             IMediaSample_Release(pOutSample);
172         pOutSample = NULL;
173     }
174
175     return hr;
176 }
177
178 static HRESULT ACMWrapper_ConnectInput(TransformFilterImpl* pTransformFilter, const AM_MEDIA_TYPE * pmt)
179 {
180     ACMWrapperImpl* This = (ACMWrapperImpl*)pTransformFilter;
181     MMRESULT res;
182
183     TRACE("(%p)->(%p)\n", This, pmt);
184
185     /* Check root (GUID w/o FOURCC) */
186     if ((IsEqualIID(&pmt->majortype, &MEDIATYPE_Audio)) &&
187         (!memcmp(((const char *)&pmt->subtype)+4, ((const char *)&MEDIATYPE_Audio)+4, sizeof(GUID)-4)) &&
188         (IsEqualIID(&pmt->formattype, &FORMAT_WaveFormatEx)))
189     {
190         HACMSTREAM drv;
191         AM_MEDIA_TYPE* outpmt = &((OutputPin*)This->tf.ppPins[1])->pin.mtCurrent;
192         This->pWfIn = (LPWAVEFORMATEX)pmt->pbFormat;
193
194         /* HACK */
195         /* TRACE("ALIGN = %d\n", pACMWrapper->pWfIn->nBlockAlign); */
196         /* pACMWrapper->pWfIn->nBlockAlign = 1; */
197
198         /* Set output audio data to PCM */
199         CopyMediaType(outpmt, pmt);
200         outpmt->subtype.Data1 = WAVE_FORMAT_PCM;
201         This->pWfOut = (WAVEFORMATEX*)outpmt->pbFormat;
202         This->pWfOut->wFormatTag = WAVE_FORMAT_PCM;
203         This->pWfOut->wBitsPerSample = 16;
204         This->pWfOut->nBlockAlign = 4;
205         This->pWfOut->cbSize = 0;
206         This->pWfOut->nAvgBytesPerSec = This->pWfOut->nChannels * This->pWfOut->nSamplesPerSec
207                                                 * (This->pWfOut->wBitsPerSample/8);
208
209         if (!(res = acmStreamOpen(&drv, NULL, This->pWfIn, This->pWfOut, NULL, 0, 0, 0)))
210         {
211             This->has = drv;
212
213             if ((res = acmStreamSize(drv, OUTPUT_BUFFER_SIZE, &This->max_size, ACM_STREAMSIZEF_DESTINATION))) {
214                 ERR("Cannot retrieve input buffer size error %d!\n", res);
215                 This->max_size = INPUT_BUFFER_SIZE;
216             }
217
218             TRACE("input buffer size %d\n", This->max_size);
219
220             /* Update buffer size of media samples in output */
221             ((OutputPin*)This->tf.ppPins[1])->allocProps.cbBuffer = OUTPUT_BUFFER_SIZE;
222             
223             TRACE("Connection accepted\n");
224             return S_OK;
225         }
226         else
227             FIXME("acmStreamOpen returned %d\n", res);
228         FreeMediaType(outpmt);
229         TRACE("Unable to find a suitable ACM decompressor\n");
230     }
231
232     TRACE("Connection refused\n");
233     return S_FALSE;
234 }
235
236 static HRESULT ACMWrapper_Cleanup(TransformFilterImpl* pTransformFilter)
237 {
238     ACMWrapperImpl* This = (ACMWrapperImpl*)pTransformFilter;
239
240     TRACE("(%p)->()\n", This);
241     
242     if (This->has)
243         acmStreamClose(This->has, 0);
244
245     This->has = 0;
246     
247     return S_OK;
248 }
249
250 static const TransformFuncsTable ACMWrapper_FuncsTable = {
251     NULL,
252     ACMWrapper_ProcessSampleData,
253     NULL,
254     NULL,
255     ACMWrapper_ConnectInput,
256     ACMWrapper_Cleanup
257 };
258
259 HRESULT ACMWrapper_create(IUnknown * pUnkOuter, LPVOID * ppv)
260 {
261     HRESULT hr;
262     ACMWrapperImpl* This;
263
264     TRACE("(%p, %p)\n", pUnkOuter, ppv);
265
266     *ppv = NULL;
267
268     if (pUnkOuter)
269         return CLASS_E_NOAGGREGATION;
270
271     /* Note: This memory is managed by the transform filter once created */
272     This = CoTaskMemAlloc(sizeof(ACMWrapperImpl));
273     ZeroMemory(This, sizeof(ACMWrapperImpl));
274
275     This->reinit_codec = TRUE;
276
277     hr = TransformFilter_Create(&(This->tf), &CLSID_ACMWrapper, &ACMWrapper_FuncsTable);
278
279     if (FAILED(hr))
280         return hr;
281
282     *ppv = (LPVOID)This;
283
284     return hr;
285 }