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
23 #include "quartz_private.h"
38 #include "wine/unicode.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
43 typedef struct AVIDecImpl
46 IUnknown *seekthru_unk;
49 BITMAPINFOHEADER* pBihIn;
50 BITMAPINFOHEADER* pBihOut;
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);
71 TRACE("(%p)->()\n", This);
74 result = ICDecompressBegin(This->hvid, This->pBihIn, This->pBihOut);
75 if (result != ICERR_OK)
77 ERR("Cannot start processing (%d)\n", result);
83 static HRESULT WINAPI AVIDec_EndFlush(TransformFilter *pTransformFilter) {
84 AVIDecImpl* This = impl_from_TransformFilter(pTransformFilter);
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);
94 This->late = qm.Late + qm.TimeStamp;
97 LeaveCriticalSection(&This->tf.filter.csFilter);
101 static int AVIDec_DropSample(AVIDecImpl *This, REFERENCE_TIME tStart) {
105 if (tStart < This->late) {
106 TRACE("Dropping sample\n");
113 static HRESULT WINAPI AVIDec_Receive(TransformFilter *tf, IMediaSample *pSample)
115 AVIDecImpl* This = impl_from_TransformFilter(tf);
119 IMediaSample* pOutSample = NULL;
124 LONGLONG tStart, tStop;
127 EnterCriticalSection(&This->tf.csReceive);
128 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
131 ERR("Cannot get pointer to sample data (%x)\n", hr);
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);
141 ERR("Unable to retrieve media type\n");
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);
150 ERR("Unable to get delivery buffer (%x)\n", hr);
154 hr = IMediaSample_SetActualDataLength(pOutSample, 0);
157 hr = IMediaSample_GetPointer(pOutSample, &pbDstStream);
159 ERR("Unable to get pointer to buffer (%x)\n", hr);
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);
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);
179 ERR("Error occurred during the decompression (%x)\n", res);
181 /* Drop sample if its intended to be dropped */
182 if (flags & ICDECOMPRESS_HURRYUP) {
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));
194 IMediaSample_SetTime(pOutSample, &tStart, &tStop);
195 else if (hr == VFW_S_NO_STOP_TIME)
196 IMediaSample_SetTime(pOutSample, &tStart, NULL);
198 IMediaSample_SetTime(pOutSample, NULL, NULL);
200 if (IMediaSample_GetMediaTime(pSample, &tStart, &tStop) == S_OK)
201 IMediaSample_SetMediaTime(pOutSample, &tStart, &tStop);
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);
213 IMediaSample_Release(pOutSample);
215 LeaveCriticalSection(&This->tf.csReceive);
219 static HRESULT WINAPI AVIDec_StopStreaming(TransformFilter* pTransformFilter)
221 AVIDecImpl* This = impl_from_TransformFilter(pTransformFilter);
224 TRACE("(%p)->()\n", This);
229 result = ICDecompressEnd(This->hvid);
230 if (result != ICERR_OK)
232 ERR("Cannot stop processing (%d)\n", result);
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)
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;
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);
267 AM_MEDIA_TYPE* outpmt = &This->tf.pmt;
268 const CLSID* outsubtype;
270 DWORD output_depth = bmi->biBitCount;
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;
281 WARN("Non standard input depth %d, forced output depth to 32\n", bmi->biBitCount);
282 outsubtype = &MEDIASUBTYPE_RGB32;
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);
295 This->pBihOut = CoTaskMemAlloc(bih_size);
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);
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);
327 TRACE("Connection accepted\n");
330 TRACE("Unable to find a suitable VFW decompressor\n");
335 TRACE("Connection refused\n");
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);
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)
359 CoTaskMemFree(This->pBihIn);
361 CoTaskMemFree(This->pBihOut);
365 This->pBihOut = NULL;
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,
392 AVIDec_StopStreaming,
395 AVIDec_CompleteConnect,
404 HRESULT AVIDec_create(IUnknown * pUnkOuter, LPVOID * ppv)
409 TRACE("(%p, %p)\n", pUnkOuter, ppv);
414 return CLASS_E_NOAGGREGATION;
416 hr = TransformFilter_Construct(&AVIDec_Vtbl, sizeof(AVIDecImpl), &CLSID_AVIDec, &AVIDec_FuncsTable, (IBaseFilter**)&This);
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);
431 This->pBihOut = NULL;
438 static HRESULT WINAPI AVIDec_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
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);
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