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