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