taskmgr: Remove a useless commented line.
[wine.git] / dlls / quartz / avidec.c
blob1b51f46730221a150588a7f64bee47bd8e9dd909
1 /*
2 * AVI Decompressor (VFW decompressors wrapper)
4 * Copyright 2004-2005 Christian Costa
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.
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.
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
21 #include "config.h"
23 #include "quartz_private.h"
24 #include "pin.h"
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"
36 #include <assert.h>
38 #include "wine/unicode.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
43 typedef struct AVIDecImpl
45 TransformFilter tf;
46 IUnknown *seekthru_unk;
48 HIC hvid;
49 BITMAPINFOHEADER* pBihIn;
50 BITMAPINFOHEADER* pBihOut;
51 } AVIDecImpl;
53 static const IBaseFilterVtbl AVIDec_Vtbl;
55 static HRESULT WINAPI AVIDec_StartStreaming(TransformFilter* pTransformFilter)
57 AVIDecImpl* This = (AVIDecImpl*)pTransformFilter;
58 DWORD result;
60 TRACE("(%p)->()\n", This);
62 result = ICDecompressBegin(This->hvid, This->pBihIn, This->pBihOut);
63 if (result != ICERR_OK)
65 ERR("Cannot start processing (%d)\n", result);
66 return E_FAIL;
68 return S_OK;
71 static HRESULT WINAPI AVIDec_Receive(TransformFilter *tf, IMediaSample *pSample)
73 AVIDecImpl* This = (AVIDecImpl *)tf;
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;
84 EnterCriticalSection(&This->tf.filter.csFilter);
85 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
86 if (FAILED(hr))
88 ERR("Cannot get pointer to sample data (%x)\n", hr);
89 goto error;
92 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
94 TRACE("Sample data ptr = %p, size = %d\n", pbSrcStream, cbSrcStream);
96 hr = IPin_ConnectionMediaType(This->tf.ppPins[0], &amt);
97 if (FAILED(hr)) {
98 ERR("Unable to retrieve media type\n");
99 goto error;
102 /* Update input size to match sample size */
103 This->pBihIn->biSizeImage = cbSrcStream;
105 hr = BaseOutputPinImpl_GetDeliveryBuffer((BaseOutputPin*)This->tf.ppPins[1], &pOutSample, NULL, NULL, 0);
106 if (FAILED(hr)) {
107 ERR("Unable to get delivery buffer (%x)\n", hr);
108 goto error;
111 hr = IMediaSample_SetActualDataLength(pOutSample, 0);
112 assert(hr == S_OK);
114 hr = IMediaSample_GetPointer(pOutSample, &pbDstStream);
115 if (FAILED(hr)) {
116 ERR("Unable to get pointer to buffer (%x)\n", hr);
117 goto error;
119 cbDstStream = IMediaSample_GetSize(pOutSample);
120 if (cbDstStream < This->pBihOut->biSizeImage) {
121 ERR("Sample size is too small %d < %d\n", cbDstStream, This->pBihOut->biSizeImage);
122 hr = E_FAIL;
123 goto error;
126 res = ICDecompress(This->hvid, 0, This->pBihIn, pbSrcStream, This->pBihOut, pbDstStream);
127 if (res != ICERR_OK)
128 ERR("Error occurred during the decompression (%x)\n", res);
130 IMediaSample_SetActualDataLength(pOutSample, This->pBihOut->biSizeImage);
132 IMediaSample_SetPreroll(pOutSample, (IMediaSample_IsPreroll(pSample) == S_OK));
133 IMediaSample_SetDiscontinuity(pOutSample, (IMediaSample_IsDiscontinuity(pSample) == S_OK));
134 IMediaSample_SetSyncPoint(pOutSample, (IMediaSample_IsSyncPoint(pSample) == S_OK));
136 if (IMediaSample_GetTime(pSample, &tStart, &tStop) == S_OK)
137 IMediaSample_SetTime(pOutSample, &tStart, &tStop);
138 else
139 IMediaSample_SetTime(pOutSample, NULL, NULL);
141 LeaveCriticalSection(&This->tf.filter.csFilter);
142 hr = BaseOutputPinImpl_Deliver((BaseOutputPin*)This->tf.ppPins[1], pOutSample);
143 if (hr != S_OK && hr != VFW_E_NOT_CONNECTED)
144 ERR("Error sending sample (%x)\n", hr);
145 IMediaSample_Release(pOutSample);
146 return hr;
148 error:
149 if (pOutSample)
150 IMediaSample_Release(pOutSample);
152 LeaveCriticalSection(&This->tf.filter.csFilter);
153 return hr;
156 static HRESULT WINAPI AVIDec_StopStreaming(TransformFilter* pTransformFilter)
158 AVIDecImpl* This = (AVIDecImpl*)pTransformFilter;
159 DWORD result;
161 TRACE("(%p)->()\n", This);
163 if (!This->hvid)
164 return S_OK;
166 result = ICDecompressEnd(This->hvid);
167 if (result != ICERR_OK)
169 ERR("Cannot stop processing (%d)\n", result);
170 return E_FAIL;
172 return S_OK;
175 static HRESULT WINAPI AVIDec_SetMediaType(TransformFilter *tf, PIN_DIRECTION dir, const AM_MEDIA_TYPE * pmt)
177 AVIDecImpl* This = (AVIDecImpl*)tf;
178 HRESULT hr = VFW_E_TYPE_NOT_ACCEPTED;
180 TRACE("(%p)->(%p)\n", This, pmt);
182 if (dir != PINDIR_INPUT)
183 return S_OK;
185 /* Check root (GUID w/o FOURCC) */
186 if ((IsEqualIID(&pmt->majortype, &MEDIATYPE_Video)) &&
187 (!memcmp(((const char *)&pmt->subtype)+4, ((const char *)&MEDIATYPE_Video)+4, sizeof(GUID)-4)))
189 VIDEOINFOHEADER *format1 = (VIDEOINFOHEADER *)pmt->pbFormat;
190 VIDEOINFOHEADER2 *format2 = (VIDEOINFOHEADER2 *)pmt->pbFormat;
191 BITMAPINFOHEADER *bmi;
193 if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo))
194 bmi = &format1->bmiHeader;
195 else if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo2))
196 bmi = &format2->bmiHeader;
197 else
198 goto failed;
199 TRACE("Fourcc: %s\n", debugstr_an((const char *)&pmt->subtype.Data1, 4));
201 This->hvid = ICLocate(pmt->majortype.Data1, pmt->subtype.Data1, bmi, NULL, ICMODE_DECOMPRESS);
202 if (This->hvid)
204 AM_MEDIA_TYPE* outpmt = &This->tf.pmt;
205 const CLSID* outsubtype;
206 DWORD bih_size;
207 DWORD output_depth = bmi->biBitCount;
208 DWORD result;
209 FreeMediaType(outpmt);
211 switch(bmi->biBitCount)
213 case 32: outsubtype = &MEDIASUBTYPE_RGB32; break;
214 case 24: outsubtype = &MEDIASUBTYPE_RGB24; break;
215 case 16: outsubtype = &MEDIASUBTYPE_RGB565; break;
216 case 8: outsubtype = &MEDIASUBTYPE_RGB8; break;
217 default:
218 WARN("Non standard input depth %d, forced output depth to 32\n", bmi->biBitCount);
219 outsubtype = &MEDIASUBTYPE_RGB32;
220 output_depth = 32;
221 break;
224 /* Copy bitmap header from media type to 1 for input and 1 for output */
225 bih_size = bmi->biSize + bmi->biClrUsed * 4;
226 This->pBihIn = CoTaskMemAlloc(bih_size);
227 if (!This->pBihIn)
229 hr = E_OUTOFMEMORY;
230 goto failed;
232 This->pBihOut = CoTaskMemAlloc(bih_size);
233 if (!This->pBihOut)
235 hr = E_OUTOFMEMORY;
236 goto failed;
238 memcpy(This->pBihIn, bmi, bih_size);
239 memcpy(This->pBihOut, bmi, bih_size);
241 /* Update output format as non compressed bitmap */
242 This->pBihOut->biCompression = 0;
243 This->pBihOut->biBitCount = output_depth;
244 This->pBihOut->biSizeImage = This->pBihOut->biWidth * This->pBihOut->biHeight * This->pBihOut->biBitCount / 8;
245 TRACE("Size: %u\n", This->pBihIn->biSize);
246 result = ICDecompressQuery(This->hvid, This->pBihIn, This->pBihOut);
247 if (result != ICERR_OK)
249 ERR("Unable to found a suitable output format (%d)\n", result);
250 goto failed;
253 /* Update output media type */
254 CopyMediaType(outpmt, pmt);
255 outpmt->subtype = *outsubtype;
257 if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo))
258 memcpy(&(((VIDEOINFOHEADER *)outpmt->pbFormat)->bmiHeader), This->pBihOut, This->pBihOut->biSize);
259 else if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo2))
260 memcpy(&(((VIDEOINFOHEADER2 *)outpmt->pbFormat)->bmiHeader), This->pBihOut, This->pBihOut->biSize);
261 else
262 assert(0);
264 TRACE("Connection accepted\n");
265 return S_OK;
267 TRACE("Unable to find a suitable VFW decompressor\n");
270 failed:
272 TRACE("Connection refused\n");
273 return hr;
276 static HRESULT WINAPI AVIDec_CompleteConnect(TransformFilter *tf, PIN_DIRECTION dir, IPin *pin)
278 AVIDecImpl* This = (AVIDecImpl*)tf;
280 TRACE("(%p)\n", This);
282 if (dir == PINDIR_INPUT)
284 /* Update buffer size of media samples in output */
285 ((BaseOutputPin*)This->tf.ppPins[1])->allocProps.cbBuffer = This->pBihOut->biSizeImage;
287 return S_OK;
290 static HRESULT WINAPI AVIDec_BreakConnect(TransformFilter *tf, PIN_DIRECTION dir)
292 AVIDecImpl *This = (AVIDecImpl *)tf;
294 TRACE("(%p)->()\n", This);
296 if (dir == PINDIR_INPUT)
298 if (This->hvid)
299 ICClose(This->hvid);
300 if (This->pBihIn)
301 CoTaskMemFree(This->pBihIn);
302 if (This->pBihOut)
303 CoTaskMemFree(This->pBihOut);
305 This->hvid = NULL;
306 This->pBihIn = NULL;
307 This->pBihOut = NULL;
310 return S_OK;
313 static const TransformFilterFuncTable AVIDec_FuncsTable = {
314 AVIDec_StartStreaming,
315 AVIDec_Receive,
316 AVIDec_StopStreaming,
317 NULL,
318 AVIDec_SetMediaType,
319 AVIDec_CompleteConnect,
320 AVIDec_BreakConnect,
321 NULL,
322 NULL,
323 NULL,
324 NULL
327 HRESULT AVIDec_create(IUnknown * pUnkOuter, LPVOID * ppv)
329 HRESULT hr;
330 AVIDecImpl * This;
332 TRACE("(%p, %p)\n", pUnkOuter, ppv);
334 *ppv = NULL;
336 if (pUnkOuter)
337 return CLASS_E_NOAGGREGATION;
339 hr = TransformFilter_Construct(&AVIDec_Vtbl, sizeof(AVIDecImpl), &CLSID_AVIDec, &AVIDec_FuncsTable, (IBaseFilter**)&This);
341 if (FAILED(hr))
342 return hr;
343 else
345 ISeekingPassThru *passthru;
346 hr = CoCreateInstance(&CLSID_SeekingPassThru, (IUnknown*)This, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void**)&This->seekthru_unk);
347 IUnknown_QueryInterface(This->seekthru_unk, &IID_ISeekingPassThru, (void**)&passthru);
348 ISeekingPassThru_Init(passthru, FALSE, (IPin*)This->tf.ppPins[0]);
349 ISeekingPassThru_Release(passthru);
352 This->hvid = NULL;
353 This->pBihIn = NULL;
354 This->pBihOut = NULL;
356 *ppv = This;
358 return hr;
361 HRESULT WINAPI AVIDec_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
363 HRESULT hr;
364 AVIDecImpl *This = (AVIDecImpl *)iface;
365 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
367 if (IsEqualIID(riid, &IID_IMediaSeeking))
368 return IUnknown_QueryInterface(This->seekthru_unk, riid, ppv);
370 hr = TransformFilterImpl_QueryInterface(iface, riid, ppv);
372 return hr;
375 static const IBaseFilterVtbl AVIDec_Vtbl =
377 AVIDec_QueryInterface,
378 BaseFilterImpl_AddRef,
379 TransformFilterImpl_Release,
380 BaseFilterImpl_GetClassID,
381 TransformFilterImpl_Stop,
382 TransformFilterImpl_Pause,
383 TransformFilterImpl_Run,
384 BaseFilterImpl_GetState,
385 BaseFilterImpl_SetSyncSource,
386 BaseFilterImpl_GetSyncSource,
387 BaseFilterImpl_EnumPins,
388 TransformFilterImpl_FindPin,
389 BaseFilterImpl_QueryFilterInfo,
390 BaseFilterImpl_JoinFilterGraph,
391 BaseFilterImpl_QueryVendorInfo