quartz: Only query for event interface when there is a filtergraph.
[wine] / dlls / quartz / avidec.c
1 /*
2  * AVI Decompressor (VFW decompressors wrapper)
3  *
4  * Copyright 2004-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 "amvideo.h"
28 #include "windef.h"
29 #include "winbase.h"
30 #include "dshow.h"
31 #include "strmif.h"
32 #include "vfwmsgs.h"
33 #include "vfw.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 static HRESULT AVIDec_Cleanup(TransformFilterImpl* pTransformFilter);
45
46 typedef struct AVIDecImpl
47 {
48     TransformFilterImpl tf;
49     HIC hvid;
50     BITMAPINFOHEADER* pBihIn;
51     BITMAPINFOHEADER* pBihOut;
52 } AVIDecImpl;
53
54 static HRESULT AVIDec_ProcessBegin(TransformFilterImpl* pTransformFilter)
55 {
56     AVIDecImpl* This = (AVIDecImpl*)pTransformFilter;
57     DWORD result;
58
59     TRACE("(%p)->()\n", This);
60
61     result = ICDecompressBegin(This->hvid, This->pBihIn, This->pBihOut);
62     if (result != ICERR_OK)
63     {
64         ERR("Cannot start processing (%d)\n", result);
65         return E_FAIL;
66     }
67     return S_OK;
68 }
69
70 static HRESULT AVIDec_ProcessSampleData(TransformFilterImpl* pTransformFilter, IMediaSample *pSample)
71 {
72     AVIDecImpl* This = (AVIDecImpl*)pTransformFilter;
73     AM_MEDIA_TYPE amt;
74     HRESULT hr;
75     DWORD res;
76     IMediaSample* pOutSample = NULL;
77     DWORD cbDstStream;
78     LPBYTE pbDstStream;
79     DWORD cbSrcStream;
80     LPBYTE pbSrcStream;
81     LONGLONG tStart, tStop;
82
83     hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
84     if (FAILED(hr))
85     {
86         ERR("Cannot get pointer to sample data (%x)\n", hr);
87         return hr;
88     }
89
90     cbSrcStream = IMediaSample_GetActualDataLength(pSample);
91
92     TRACE("Sample data ptr = %p, size = %ld\n", pbSrcStream, (long)cbSrcStream);
93
94     hr = IPin_ConnectionMediaType(This->tf.ppPins[0], &amt);
95     if (FAILED(hr)) {
96         ERR("Unable to retrieve media type\n");
97         goto error;
98     }
99
100     /* Update input size to match sample size */
101     This->pBihIn->biSizeImage = cbSrcStream;
102
103     hr = OutputPin_GetDeliveryBuffer((OutputPin*)This->tf.ppPins[1], &pOutSample, NULL, NULL, 0);
104     if (FAILED(hr)) {
105         ERR("Unable to get delivery buffer (%x)\n", hr);
106         goto error;
107     }
108
109     hr = IMediaSample_SetActualDataLength(pOutSample, 0);
110     assert(hr == S_OK);
111
112     hr = IMediaSample_GetPointer(pOutSample, &pbDstStream);
113     if (FAILED(hr)) {
114         ERR("Unable to get pointer to buffer (%x)\n", hr);
115         goto error;
116     }
117     cbDstStream = IMediaSample_GetSize(pOutSample);
118     if (cbDstStream < This->pBihOut->biSizeImage) {
119         ERR("Sample size is too small %d < %d\n", cbDstStream, This->pBihOut->biSizeImage);
120         hr = E_FAIL;
121         goto error;
122     }
123
124     res = ICDecompress(This->hvid, 0, This->pBihIn, pbSrcStream, This->pBihOut, pbDstStream);
125     if (res != ICERR_OK)
126         ERR("Error occurred during the decompression (%x)\n", res);
127
128     IMediaSample_SetActualDataLength(pOutSample, This->pBihOut->biSizeImage);
129
130     IMediaSample_SetPreroll(pOutSample, (IMediaSample_IsPreroll(pSample) == S_OK));
131     IMediaSample_SetDiscontinuity(pOutSample, (IMediaSample_IsDiscontinuity(pSample) == S_OK));
132     IMediaSample_SetSyncPoint(pOutSample, (IMediaSample_IsSyncPoint(pSample) == S_OK));
133
134     if (IMediaSample_GetTime(pSample, &tStart, &tStop) == S_OK)
135         IMediaSample_SetTime(pOutSample, &tStart, &tStop);
136     else
137         IMediaSample_SetTime(pOutSample, NULL, NULL);
138
139     hr = OutputPin_SendSample((OutputPin*)This->tf.ppPins[1], pOutSample);
140     if (hr != S_OK && hr != VFW_E_NOT_CONNECTED) {
141         ERR("Error sending sample (%x)\n", hr);
142         goto error;
143     }
144
145 error:
146     if (pOutSample)
147         IMediaSample_Release(pOutSample);
148
149     return hr;
150 }
151
152 static HRESULT AVIDec_ProcessEnd(TransformFilterImpl* pTransformFilter)
153 {
154     AVIDecImpl* This = (AVIDecImpl*)pTransformFilter;
155     DWORD result;
156
157     TRACE("(%p)->()\n", This);
158
159     if (!This->hvid)
160         return S_OK;
161
162     result = ICDecompressEnd(This->hvid);
163     if (result != ICERR_OK)
164     {
165         ERR("Cannot stop processing (%d)\n", result);
166         return E_FAIL;
167     }
168     return S_OK;
169 }
170
171 static HRESULT AVIDec_ConnectInput(TransformFilterImpl* pTransformFilter, const AM_MEDIA_TYPE * pmt)
172 {
173     AVIDecImpl* This = (AVIDecImpl*)pTransformFilter;
174     HRESULT hr = VFW_E_TYPE_NOT_ACCEPTED;
175
176     TRACE("(%p)->(%p)\n", This, pmt);
177
178     AVIDec_Cleanup(pTransformFilter);
179
180     /* Check root (GUID w/o FOURCC) */
181     if ((IsEqualIID(&pmt->majortype, &MEDIATYPE_Video)) &&
182         (!memcmp(((const char *)&pmt->subtype)+4, ((const char *)&MEDIATYPE_Video)+4, sizeof(GUID)-4)) &&
183         (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo)))
184     {
185         VIDEOINFOHEADER* format = (VIDEOINFOHEADER*)pmt->pbFormat;
186
187         This->hvid = ICLocate(pmt->majortype.Data1, pmt->subtype.Data1, &format->bmiHeader, NULL, ICMODE_DECOMPRESS);
188         if (This->hvid)
189         {
190             AM_MEDIA_TYPE* outpmt = &((OutputPin*)This->tf.ppPins[1])->pin.mtCurrent;
191             const CLSID* outsubtype;
192             DWORD bih_size;
193             DWORD output_depth = format->bmiHeader.biBitCount;
194             DWORD result;
195
196             switch(format->bmiHeader.biBitCount)
197             {
198                 case 32: outsubtype = &MEDIASUBTYPE_RGB32; break;
199                 case 24: outsubtype = &MEDIASUBTYPE_RGB24; break;
200                 case 16: outsubtype = &MEDIASUBTYPE_RGB565; break;
201                 case 8:  outsubtype = &MEDIASUBTYPE_RGB8; break;
202                 default:
203                     TRACE("Non standard input depth %d, forced ouptut depth to 32\n", format->bmiHeader.biBitCount);
204                     outsubtype = &MEDIASUBTYPE_RGB32;
205                     output_depth = 32;
206                     break;
207             }
208
209             /* Copy bitmap header from media type to 1 for input and 1 for output */
210             bih_size = format->bmiHeader.biSize + format->bmiHeader.biClrUsed * 4;
211             This->pBihIn = CoTaskMemAlloc(bih_size);
212             if (!This->pBihIn)
213             {
214                 hr = E_OUTOFMEMORY;
215                 goto failed;
216             }
217             This->pBihOut = CoTaskMemAlloc(bih_size);
218             if (!This->pBihOut)
219             {
220                 hr = E_OUTOFMEMORY;
221                 goto failed;
222             }
223             memcpy(This->pBihIn, &format->bmiHeader, bih_size);
224             memcpy(This->pBihOut, &format->bmiHeader, bih_size);
225
226             /* Update output format as non compressed bitmap */
227             This->pBihOut->biCompression = 0;
228             This->pBihOut->biBitCount = output_depth;
229             This->pBihOut->biSizeImage = This->pBihOut->biWidth * This->pBihOut->biHeight * This->pBihOut->biBitCount / 8;
230
231             result = ICDecompressQuery(This->hvid, This->pBihIn, This->pBihOut);
232             if (result != ICERR_OK)
233             {
234                 TRACE("Unable to found a suitable output format (%d)\n", result);
235                 goto failed;
236             }
237
238             /* Update output media type */
239             CopyMediaType(outpmt, pmt);
240             outpmt->subtype = *outsubtype;
241             memcpy(&(((VIDEOINFOHEADER*)outpmt->pbFormat)->bmiHeader), This->pBihOut, This->pBihOut->biSize);
242
243             /* Update buffer size of media samples in output */
244             ((OutputPin*)This->tf.ppPins[1])->allocProps.cbBuffer = This->pBihOut->biSizeImage;
245
246             TRACE("Connection accepted\n");
247             return S_OK;
248         }
249         TRACE("Unable to find a suitable VFW decompressor\n");
250     }
251
252 failed:
253     AVIDec_Cleanup(pTransformFilter);
254     
255     TRACE("Connection refused\n");
256     return hr;
257 }
258
259 static HRESULT AVIDec_Cleanup(TransformFilterImpl* pTransformFilter)
260 {
261     AVIDecImpl* This = (AVIDecImpl*)pTransformFilter;
262
263     TRACE("(%p)->()\n", This);
264     
265     if (This->hvid)
266         ICClose(This->hvid);
267     if (This->pBihIn)
268         CoTaskMemFree(This->pBihIn);
269     if (This->pBihOut)
270         CoTaskMemFree(This->pBihOut);
271
272     This->hvid = NULL;
273     This->pBihIn = NULL;
274     This->pBihOut = NULL;
275
276     return S_OK;
277 }
278
279 static const TransformFuncsTable AVIDec_FuncsTable = {
280     AVIDec_ProcessBegin,
281     AVIDec_ProcessSampleData,
282     AVIDec_ProcessEnd,
283     NULL,
284     AVIDec_ConnectInput,
285     AVIDec_Cleanup
286 };
287
288 HRESULT AVIDec_create(IUnknown * pUnkOuter, LPVOID * ppv)
289 {
290     HRESULT hr;
291     AVIDecImpl * This;
292
293     TRACE("(%p, %p)\n", pUnkOuter, ppv);
294
295     *ppv = NULL;
296
297     if (pUnkOuter)
298         return CLASS_E_NOAGGREGATION;
299
300     /* Note: This memory is managed by the transform filter once created */
301     This = CoTaskMemAlloc(sizeof(AVIDecImpl));
302
303     This->hvid = NULL;
304     This->pBihIn = NULL;
305     This->pBihOut = NULL;
306
307     hr = TransformFilter_Create(&(This->tf), &CLSID_AVIDec, &AVIDec_FuncsTable, NULL, NULL, NULL);
308
309     if (FAILED(hr))
310         return hr;
311
312     *ppv = (LPVOID)This;
313
314     return hr;
315 }