dsound: Get rid of the DSOUND_QueryInterface() helper.
[wine/multimedia.git] / dlls / quartz / avidec.c
blob5537db92a91ef75447f211fe74806f0436073867
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 REFERENCE_TIME late;
52 } AVIDecImpl;
54 static const IBaseFilterVtbl AVIDec_Vtbl;
56 static inline AVIDecImpl *impl_from_IBaseFilter( IBaseFilter *iface )
58 return CONTAINING_RECORD(iface, AVIDecImpl, tf.filter.IBaseFilter_iface);
61 static inline AVIDecImpl *impl_from_TransformFilter( TransformFilter *iface )
63 return CONTAINING_RECORD(iface, AVIDecImpl, tf.filter);
66 static HRESULT WINAPI AVIDec_StartStreaming(TransformFilter* pTransformFilter)
68 AVIDecImpl* This = impl_from_TransformFilter(pTransformFilter);
69 DWORD result;
71 TRACE("(%p)->()\n", This);
72 This->late = -1;
74 result = ICDecompressBegin(This->hvid, This->pBihIn, This->pBihOut);
75 if (result != ICERR_OK)
77 ERR("Cannot start processing (%d)\n", result);
78 return E_FAIL;
80 return S_OK;
83 static HRESULT WINAPI AVIDec_EndFlush(TransformFilter *pTransformFilter) {
84 AVIDecImpl* This = impl_from_TransformFilter(pTransformFilter);
85 This->late = -1;
86 return S_OK;
89 static HRESULT WINAPI AVIDec_NotifyDrop(TransformFilter *pTransformFilter, IBaseFilter *sender, Quality qm) {
90 AVIDecImpl *This = impl_from_TransformFilter(pTransformFilter);
92 EnterCriticalSection(&This->tf.filter.csFilter);
93 if (qm.Late > 0)
94 This->late = qm.Late + qm.TimeStamp;
95 else
96 This->late = -1;
97 LeaveCriticalSection(&This->tf.filter.csFilter);
98 return S_OK;
101 static int AVIDec_DropSample(AVIDecImpl *This, REFERENCE_TIME tStart) {
102 if (This->late < 0)
103 return 0;
105 if (tStart < This->late) {
106 TRACE("Dropping sample\n");
107 return 1;
109 This->late = -1;
110 return 0;
113 static HRESULT WINAPI AVIDec_Receive(TransformFilter *tf, IMediaSample *pSample)
115 AVIDecImpl* This = impl_from_TransformFilter(tf);
116 AM_MEDIA_TYPE amt;
117 HRESULT hr;
118 DWORD res;
119 IMediaSample* pOutSample = NULL;
120 DWORD cbDstStream;
121 LPBYTE pbDstStream;
122 DWORD cbSrcStream;
123 LPBYTE pbSrcStream;
124 LONGLONG tStart, tStop;
125 DWORD flags = 0;
127 EnterCriticalSection(&This->tf.csReceive);
128 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
129 if (FAILED(hr))
131 ERR("Cannot get pointer to sample data (%x)\n", hr);
132 goto error;
135 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
137 TRACE("Sample data ptr = %p, size = %d\n", pbSrcStream, cbSrcStream);
139 hr = IPin_ConnectionMediaType(This->tf.ppPins[0], &amt);
140 if (FAILED(hr)) {
141 ERR("Unable to retrieve media type\n");
142 goto error;
145 /* Update input size to match sample size */
146 This->pBihIn->biSizeImage = cbSrcStream;
148 hr = BaseOutputPinImpl_GetDeliveryBuffer((BaseOutputPin*)This->tf.ppPins[1], &pOutSample, NULL, NULL, 0);
149 if (FAILED(hr)) {
150 ERR("Unable to get delivery buffer (%x)\n", hr);
151 goto error;
154 hr = IMediaSample_SetActualDataLength(pOutSample, 0);
155 assert(hr == S_OK);
157 hr = IMediaSample_GetPointer(pOutSample, &pbDstStream);
158 if (FAILED(hr)) {
159 ERR("Unable to get pointer to buffer (%x)\n", hr);
160 goto error;
162 cbDstStream = IMediaSample_GetSize(pOutSample);
163 if (cbDstStream < This->pBihOut->biSizeImage) {
164 ERR("Sample size is too small %d < %d\n", cbDstStream, This->pBihOut->biSizeImage);
165 hr = E_FAIL;
166 goto error;
169 if (IMediaSample_IsPreroll(pSample) == S_OK)
170 flags |= ICDECOMPRESS_PREROLL;
171 if (IMediaSample_IsSyncPoint(pSample) != S_OK)
172 flags |= ICDECOMPRESS_NOTKEYFRAME;
173 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
174 if (hr == S_OK && AVIDec_DropSample(This, tStart))
175 flags |= ICDECOMPRESS_HURRYUP;
177 res = ICDecompress(This->hvid, flags, This->pBihIn, pbSrcStream, This->pBihOut, pbDstStream);
178 if (res != ICERR_OK)
179 ERR("Error occurred during the decompression (%x)\n", res);
181 /* Drop sample if its intended to be dropped */
182 if (flags & ICDECOMPRESS_HURRYUP) {
183 hr = S_OK;
184 goto error;
187 IMediaSample_SetActualDataLength(pOutSample, This->pBihOut->biSizeImage);
189 IMediaSample_SetPreroll(pOutSample, (IMediaSample_IsPreroll(pSample) == S_OK));
190 IMediaSample_SetDiscontinuity(pOutSample, (IMediaSample_IsDiscontinuity(pSample) == S_OK));
191 IMediaSample_SetSyncPoint(pOutSample, (IMediaSample_IsSyncPoint(pSample) == S_OK));
193 if (hr == S_OK)
194 IMediaSample_SetTime(pOutSample, &tStart, &tStop);
195 else if (hr == VFW_S_NO_STOP_TIME)
196 IMediaSample_SetTime(pOutSample, &tStart, NULL);
197 else
198 IMediaSample_SetTime(pOutSample, NULL, NULL);
200 if (IMediaSample_GetMediaTime(pSample, &tStart, &tStop) == S_OK)
201 IMediaSample_SetMediaTime(pOutSample, &tStart, &tStop);
202 else
203 IMediaSample_SetMediaTime(pOutSample, NULL, NULL);
205 LeaveCriticalSection(&This->tf.csReceive);
206 hr = BaseOutputPinImpl_Deliver((BaseOutputPin*)This->tf.ppPins[1], pOutSample);
207 EnterCriticalSection(&This->tf.csReceive);
208 if (hr != S_OK && hr != VFW_E_NOT_CONNECTED)
209 ERR("Error sending sample (%x)\n", hr);
211 error:
212 if (pOutSample)
213 IMediaSample_Release(pOutSample);
215 LeaveCriticalSection(&This->tf.csReceive);
216 return hr;
219 static HRESULT WINAPI AVIDec_StopStreaming(TransformFilter* pTransformFilter)
221 AVIDecImpl* This = impl_from_TransformFilter(pTransformFilter);
222 DWORD result;
224 TRACE("(%p)->()\n", This);
226 if (!This->hvid)
227 return S_OK;
229 result = ICDecompressEnd(This->hvid);
230 if (result != ICERR_OK)
232 ERR("Cannot stop processing (%d)\n", result);
233 return E_FAIL;
235 return S_OK;
238 static HRESULT WINAPI AVIDec_SetMediaType(TransformFilter *tf, PIN_DIRECTION dir, const AM_MEDIA_TYPE * pmt)
240 AVIDecImpl* This = impl_from_TransformFilter(tf);
241 HRESULT hr = VFW_E_TYPE_NOT_ACCEPTED;
243 TRACE("(%p)->(%p)\n", This, pmt);
245 if (dir != PINDIR_INPUT)
246 return S_OK;
248 /* Check root (GUID w/o FOURCC) */
249 if ((IsEqualIID(&pmt->majortype, &MEDIATYPE_Video)) &&
250 (!memcmp(((const char *)&pmt->subtype)+4, ((const char *)&MEDIATYPE_Video)+4, sizeof(GUID)-4)))
252 VIDEOINFOHEADER *format1 = (VIDEOINFOHEADER *)pmt->pbFormat;
253 VIDEOINFOHEADER2 *format2 = (VIDEOINFOHEADER2 *)pmt->pbFormat;
254 BITMAPINFOHEADER *bmi;
256 if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo))
257 bmi = &format1->bmiHeader;
258 else if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo2))
259 bmi = &format2->bmiHeader;
260 else
261 goto failed;
262 TRACE("Fourcc: %s\n", debugstr_an((const char *)&pmt->subtype.Data1, 4));
264 This->hvid = ICLocate(pmt->majortype.Data1, pmt->subtype.Data1, bmi, NULL, ICMODE_DECOMPRESS);
265 if (This->hvid)
267 AM_MEDIA_TYPE* outpmt = &This->tf.pmt;
268 const CLSID* outsubtype;
269 DWORD bih_size;
270 DWORD output_depth = bmi->biBitCount;
271 DWORD result;
272 FreeMediaType(outpmt);
274 switch(bmi->biBitCount)
276 case 32: outsubtype = &MEDIASUBTYPE_RGB32; break;
277 case 24: outsubtype = &MEDIASUBTYPE_RGB24; break;
278 case 16: outsubtype = &MEDIASUBTYPE_RGB565; break;
279 case 8: outsubtype = &MEDIASUBTYPE_RGB8; break;
280 default:
281 WARN("Non standard input depth %d, forced output depth to 32\n", bmi->biBitCount);
282 outsubtype = &MEDIASUBTYPE_RGB32;
283 output_depth = 32;
284 break;
287 /* Copy bitmap header from media type to 1 for input and 1 for output */
288 bih_size = bmi->biSize + bmi->biClrUsed * 4;
289 This->pBihIn = CoTaskMemAlloc(bih_size);
290 if (!This->pBihIn)
292 hr = E_OUTOFMEMORY;
293 goto failed;
295 This->pBihOut = CoTaskMemAlloc(bih_size);
296 if (!This->pBihOut)
298 hr = E_OUTOFMEMORY;
299 goto failed;
301 memcpy(This->pBihIn, bmi, bih_size);
302 memcpy(This->pBihOut, bmi, bih_size);
304 /* Update output format as non compressed bitmap */
305 This->pBihOut->biCompression = 0;
306 This->pBihOut->biBitCount = output_depth;
307 This->pBihOut->biSizeImage = This->pBihOut->biWidth * This->pBihOut->biHeight * This->pBihOut->biBitCount / 8;
308 TRACE("Size: %u\n", This->pBihIn->biSize);
309 result = ICDecompressQuery(This->hvid, This->pBihIn, This->pBihOut);
310 if (result != ICERR_OK)
312 ERR("Unable to found a suitable output format (%d)\n", result);
313 goto failed;
316 /* Update output media type */
317 CopyMediaType(outpmt, pmt);
318 outpmt->subtype = *outsubtype;
320 if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo))
321 memcpy(&(((VIDEOINFOHEADER *)outpmt->pbFormat)->bmiHeader), This->pBihOut, This->pBihOut->biSize);
322 else if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo2))
323 memcpy(&(((VIDEOINFOHEADER2 *)outpmt->pbFormat)->bmiHeader), This->pBihOut, This->pBihOut->biSize);
324 else
325 assert(0);
327 TRACE("Connection accepted\n");
328 return S_OK;
330 TRACE("Unable to find a suitable VFW decompressor\n");
333 failed:
335 TRACE("Connection refused\n");
336 return hr;
339 static HRESULT WINAPI AVIDec_CompleteConnect(TransformFilter *tf, PIN_DIRECTION dir, IPin *pin)
341 AVIDecImpl* This = impl_from_TransformFilter(tf);
343 TRACE("(%p)\n", This);
345 return S_OK;
348 static HRESULT WINAPI AVIDec_BreakConnect(TransformFilter *tf, PIN_DIRECTION dir)
350 AVIDecImpl *This = impl_from_TransformFilter(tf);
352 TRACE("(%p)->()\n", This);
354 if (dir == PINDIR_INPUT)
356 if (This->hvid)
357 ICClose(This->hvid);
358 if (This->pBihIn)
359 CoTaskMemFree(This->pBihIn);
360 if (This->pBihOut)
361 CoTaskMemFree(This->pBihOut);
363 This->hvid = NULL;
364 This->pBihIn = NULL;
365 This->pBihOut = NULL;
368 return S_OK;
371 static HRESULT WINAPI AVIDec_DecideBufferSize(TransformFilter *tf, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
373 AVIDecImpl *pAVI = impl_from_TransformFilter(tf);
374 ALLOCATOR_PROPERTIES actual;
376 if (!ppropInputRequest->cbAlign)
377 ppropInputRequest->cbAlign = 1;
379 if (ppropInputRequest->cbBuffer < pAVI->pBihOut->biSizeImage)
380 ppropInputRequest->cbBuffer = pAVI->pBihOut->biSizeImage;
382 if (!ppropInputRequest->cBuffers)
383 ppropInputRequest->cBuffers = 1;
385 return IMemAllocator_SetProperties(pAlloc, ppropInputRequest, &actual);
388 static const TransformFilterFuncTable AVIDec_FuncsTable = {
389 AVIDec_DecideBufferSize,
390 AVIDec_StartStreaming,
391 AVIDec_Receive,
392 AVIDec_StopStreaming,
393 NULL,
394 AVIDec_SetMediaType,
395 AVIDec_CompleteConnect,
396 AVIDec_BreakConnect,
397 NULL,
398 NULL,
399 AVIDec_EndFlush,
400 NULL,
401 AVIDec_NotifyDrop
404 HRESULT AVIDec_create(IUnknown * pUnkOuter, LPVOID * ppv)
406 HRESULT hr;
407 AVIDecImpl * This;
409 TRACE("(%p, %p)\n", pUnkOuter, ppv);
411 *ppv = NULL;
413 if (pUnkOuter)
414 return CLASS_E_NOAGGREGATION;
416 hr = TransformFilter_Construct(&AVIDec_Vtbl, sizeof(AVIDecImpl), &CLSID_AVIDec, &AVIDec_FuncsTable, (IBaseFilter**)&This);
418 if (FAILED(hr))
419 return hr;
420 else
422 ISeekingPassThru *passthru;
423 hr = CoCreateInstance(&CLSID_SeekingPassThru, (IUnknown*)This, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void**)&This->seekthru_unk);
424 IUnknown_QueryInterface(This->seekthru_unk, &IID_ISeekingPassThru, (void**)&passthru);
425 ISeekingPassThru_Init(passthru, FALSE, This->tf.ppPins[0]);
426 ISeekingPassThru_Release(passthru);
429 This->hvid = NULL;
430 This->pBihIn = NULL;
431 This->pBihOut = NULL;
433 *ppv = This;
435 return hr;
438 static HRESULT WINAPI AVIDec_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
440 HRESULT hr;
441 AVIDecImpl *This = impl_from_IBaseFilter(iface);
442 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
444 if (IsEqualIID(riid, &IID_IMediaSeeking))
445 return IUnknown_QueryInterface(This->seekthru_unk, riid, ppv);
447 hr = TransformFilterImpl_QueryInterface(iface, riid, ppv);
449 return hr;
452 static const IBaseFilterVtbl AVIDec_Vtbl =
454 AVIDec_QueryInterface,
455 BaseFilterImpl_AddRef,
456 TransformFilterImpl_Release,
457 BaseFilterImpl_GetClassID,
458 TransformFilterImpl_Stop,
459 TransformFilterImpl_Pause,
460 TransformFilterImpl_Run,
461 BaseFilterImpl_GetState,
462 BaseFilterImpl_SetSyncSource,
463 BaseFilterImpl_GetSyncSource,
464 BaseFilterImpl_EnumPins,
465 TransformFilterImpl_FindPin,
466 BaseFilterImpl_QueryFilterInfo,
467 BaseFilterImpl_JoinFilterGraph,
468 BaseFilterImpl_QueryVendorInfo