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