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