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;
53 static const IBaseFilterVtbl AVIDec_Vtbl;
55 static HRESULT WINAPI AVIDec_StartStreaming(TransformFilter* pTransformFilter)
57 AVIDecImpl* This = (AVIDecImpl*)pTransformFilter;
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);
71 static HRESULT WINAPI AVIDec_Receive(TransformFilter *tf, IMediaSample *pSample)
73 AVIDecImpl* This = (AVIDecImpl *)tf;
77 IMediaSample* pOutSample = NULL;
82 LONGLONG tStart, tStop;
84 EnterCriticalSection(&This->tf.filter.csFilter);
85 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
88 ERR("Cannot get pointer to sample data (%x)\n", hr);
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);
98 ERR("Unable to retrieve media type\n");
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);
107 ERR("Unable to get delivery buffer (%x)\n", hr);
111 hr = IMediaSample_SetActualDataLength(pOutSample, 0);
114 hr = IMediaSample_GetPointer(pOutSample, &pbDstStream);
116 ERR("Unable to get pointer to buffer (%x)\n", hr);
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);
126 res = ICDecompress(This->hvid, 0, This->pBihIn, pbSrcStream, This->pBihOut, pbDstStream);
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);
139 IMediaSample_SetTime(pOutSample, NULL, NULL);
141 if (IMediaSample_GetMediaTime(pSample, &tStart, &tStop) == S_OK)
142 IMediaSample_SetMediaTime(pOutSample, &tStart, &tStop);
144 IMediaSample_SetMediaTime(pOutSample, NULL, NULL);
146 LeaveCriticalSection(&This->tf.filter.csFilter);
147 hr = BaseOutputPinImpl_Deliver((BaseOutputPin*)This->tf.ppPins[1], pOutSample);
148 if (hr != S_OK && hr != VFW_E_NOT_CONNECTED)
149 ERR("Error sending sample (%x)\n", hr);
150 IMediaSample_Release(pOutSample);
155 IMediaSample_Release(pOutSample);
157 LeaveCriticalSection(&This->tf.filter.csFilter);
161 static HRESULT WINAPI AVIDec_StopStreaming(TransformFilter* pTransformFilter)
163 AVIDecImpl* This = (AVIDecImpl*)pTransformFilter;
166 TRACE("(%p)->()\n", This);
171 result = ICDecompressEnd(This->hvid);
172 if (result != ICERR_OK)
174 ERR("Cannot stop processing (%d)\n", result);
180 static HRESULT WINAPI AVIDec_SetMediaType(TransformFilter *tf, PIN_DIRECTION dir, const AM_MEDIA_TYPE * pmt)
182 AVIDecImpl* This = (AVIDecImpl*)tf;
183 HRESULT hr = VFW_E_TYPE_NOT_ACCEPTED;
185 TRACE("(%p)->(%p)\n", This, pmt);
187 if (dir != PINDIR_INPUT)
190 /* Check root (GUID w/o FOURCC) */
191 if ((IsEqualIID(&pmt->majortype, &MEDIATYPE_Video)) &&
192 (!memcmp(((const char *)&pmt->subtype)+4, ((const char *)&MEDIATYPE_Video)+4, sizeof(GUID)-4)))
194 VIDEOINFOHEADER *format1 = (VIDEOINFOHEADER *)pmt->pbFormat;
195 VIDEOINFOHEADER2 *format2 = (VIDEOINFOHEADER2 *)pmt->pbFormat;
196 BITMAPINFOHEADER *bmi;
198 if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo))
199 bmi = &format1->bmiHeader;
200 else if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo2))
201 bmi = &format2->bmiHeader;
204 TRACE("Fourcc: %s\n", debugstr_an((const char *)&pmt->subtype.Data1, 4));
206 This->hvid = ICLocate(pmt->majortype.Data1, pmt->subtype.Data1, bmi, NULL, ICMODE_DECOMPRESS);
209 AM_MEDIA_TYPE* outpmt = &This->tf.pmt;
210 const CLSID* outsubtype;
212 DWORD output_depth = bmi->biBitCount;
214 FreeMediaType(outpmt);
216 switch(bmi->biBitCount)
218 case 32: outsubtype = &MEDIASUBTYPE_RGB32; break;
219 case 24: outsubtype = &MEDIASUBTYPE_RGB24; break;
220 case 16: outsubtype = &MEDIASUBTYPE_RGB565; break;
221 case 8: outsubtype = &MEDIASUBTYPE_RGB8; break;
223 WARN("Non standard input depth %d, forced output depth to 32\n", bmi->biBitCount);
224 outsubtype = &MEDIASUBTYPE_RGB32;
229 /* Copy bitmap header from media type to 1 for input and 1 for output */
230 bih_size = bmi->biSize + bmi->biClrUsed * 4;
231 This->pBihIn = CoTaskMemAlloc(bih_size);
237 This->pBihOut = CoTaskMemAlloc(bih_size);
243 memcpy(This->pBihIn, bmi, bih_size);
244 memcpy(This->pBihOut, bmi, bih_size);
246 /* Update output format as non compressed bitmap */
247 This->pBihOut->biCompression = 0;
248 This->pBihOut->biBitCount = output_depth;
249 This->pBihOut->biSizeImage = This->pBihOut->biWidth * This->pBihOut->biHeight * This->pBihOut->biBitCount / 8;
250 TRACE("Size: %u\n", This->pBihIn->biSize);
251 result = ICDecompressQuery(This->hvid, This->pBihIn, This->pBihOut);
252 if (result != ICERR_OK)
254 ERR("Unable to found a suitable output format (%d)\n", result);
258 /* Update output media type */
259 CopyMediaType(outpmt, pmt);
260 outpmt->subtype = *outsubtype;
262 if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo))
263 memcpy(&(((VIDEOINFOHEADER *)outpmt->pbFormat)->bmiHeader), This->pBihOut, This->pBihOut->biSize);
264 else if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo2))
265 memcpy(&(((VIDEOINFOHEADER2 *)outpmt->pbFormat)->bmiHeader), This->pBihOut, This->pBihOut->biSize);
269 TRACE("Connection accepted\n");
272 TRACE("Unable to find a suitable VFW decompressor\n");
277 TRACE("Connection refused\n");
281 static HRESULT WINAPI AVIDec_CompleteConnect(TransformFilter *tf, PIN_DIRECTION dir, IPin *pin)
283 AVIDecImpl* This = (AVIDecImpl*)tf;
285 TRACE("(%p)\n", This);
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)
301 CoTaskMemFree(This->pBihIn);
303 CoTaskMemFree(This->pBihOut);
307 This->pBihOut = NULL;
313 static HRESULT WINAPI AVIDec_DecideBufferSize(TransformFilter *tf, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
315 AVIDecImpl *pAVI = (AVIDecImpl*)tf;
316 ALLOCATOR_PROPERTIES actual;
318 if (!ppropInputRequest->cbAlign)
319 ppropInputRequest->cbAlign = 1;
321 if (ppropInputRequest->cbBuffer < pAVI->pBihOut->biSizeImage)
322 ppropInputRequest->cbBuffer = pAVI->pBihOut->biSizeImage;
324 if (!ppropInputRequest->cBuffers)
325 ppropInputRequest->cBuffers = 1;
327 return IMemAllocator_SetProperties(pAlloc, ppropInputRequest, &actual);
330 static const TransformFilterFuncTable AVIDec_FuncsTable = {
331 AVIDec_DecideBufferSize,
332 AVIDec_StartStreaming,
334 AVIDec_StopStreaming,
337 AVIDec_CompleteConnect,
345 HRESULT AVIDec_create(IUnknown * pUnkOuter, LPVOID * ppv)
350 TRACE("(%p, %p)\n", pUnkOuter, ppv);
355 return CLASS_E_NOAGGREGATION;
357 hr = TransformFilter_Construct(&AVIDec_Vtbl, sizeof(AVIDecImpl), &CLSID_AVIDec, &AVIDec_FuncsTable, (IBaseFilter**)&This);
363 ISeekingPassThru *passthru;
364 hr = CoCreateInstance(&CLSID_SeekingPassThru, (IUnknown*)This, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void**)&This->seekthru_unk);
365 IUnknown_QueryInterface(This->seekthru_unk, &IID_ISeekingPassThru, (void**)&passthru);
366 ISeekingPassThru_Init(passthru, FALSE, (IPin*)This->tf.ppPins[0]);
367 ISeekingPassThru_Release(passthru);
372 This->pBihOut = NULL;
379 HRESULT WINAPI AVIDec_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
382 AVIDecImpl *This = (AVIDecImpl *)iface;
383 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
385 if (IsEqualIID(riid, &IID_IMediaSeeking))
386 return IUnknown_QueryInterface(This->seekthru_unk, riid, ppv);
388 hr = TransformFilterImpl_QueryInterface(iface, riid, ppv);
393 static const IBaseFilterVtbl AVIDec_Vtbl =
395 AVIDec_QueryInterface,
396 BaseFilterImpl_AddRef,
397 TransformFilterImpl_Release,
398 BaseFilterImpl_GetClassID,
399 TransformFilterImpl_Stop,
400 TransformFilterImpl_Pause,
401 TransformFilterImpl_Run,
402 BaseFilterImpl_GetState,
403 BaseFilterImpl_SetSyncSource,
404 BaseFilterImpl_GetSyncSource,
405 BaseFilterImpl_EnumPins,
406 TransformFilterImpl_FindPin,
407 BaseFilterImpl_QueryFilterInfo,
408 BaseFilterImpl_JoinFilterGraph,
409 BaseFilterImpl_QueryVendorInfo