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