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 HRESULT WINAPI
AVIDec_StartStreaming(TransformFilter
* pTransformFilter
)
58 AVIDecImpl
* This
= (AVIDecImpl
*)pTransformFilter
;
61 TRACE("(%p)->()\n", This
);
64 result
= ICDecompressBegin(This
->hvid
, This
->pBihIn
, This
->pBihOut
);
65 if (result
!= ICERR_OK
)
67 ERR("Cannot start processing (%d)\n", result
);
73 static HRESULT WINAPI
AVIDec_EndFlush(TransformFilter
*pTransformFilter
) {
74 AVIDecImpl
* This
= (AVIDecImpl
*)pTransformFilter
;
79 static HRESULT WINAPI
AVIDec_NotifyDrop(TransformFilter
*pTransformFilter
, IBaseFilter
*sender
, Quality qm
) {
80 AVIDecImpl
*This
= (AVIDecImpl
*)pTransformFilter
;
82 EnterCriticalSection(&This
->tf
.filter
.csFilter
);
84 This
->late
= qm
.Late
+ qm
.TimeStamp
;
87 LeaveCriticalSection(&This
->tf
.filter
.csFilter
);
91 static int AVIDec_DropSample(AVIDecImpl
*This
, REFERENCE_TIME tStart
) {
95 if (tStart
< This
->late
) {
96 TRACE("Dropping sample\n");
103 static HRESULT WINAPI
AVIDec_Receive(TransformFilter
*tf
, IMediaSample
*pSample
)
105 AVIDecImpl
* This
= (AVIDecImpl
*)tf
;
109 IMediaSample
* pOutSample
= NULL
;
114 LONGLONG tStart
, tStop
;
117 EnterCriticalSection(&This
->tf
.filter
.csFilter
);
118 hr
= IMediaSample_GetPointer(pSample
, &pbSrcStream
);
121 ERR("Cannot get pointer to sample data (%x)\n", hr
);
125 cbSrcStream
= IMediaSample_GetActualDataLength(pSample
);
127 TRACE("Sample data ptr = %p, size = %d\n", pbSrcStream
, cbSrcStream
);
129 hr
= IPin_ConnectionMediaType(This
->tf
.ppPins
[0], &amt
);
131 ERR("Unable to retrieve media type\n");
135 /* Update input size to match sample size */
136 This
->pBihIn
->biSizeImage
= cbSrcStream
;
138 hr
= BaseOutputPinImpl_GetDeliveryBuffer((BaseOutputPin
*)This
->tf
.ppPins
[1], &pOutSample
, NULL
, NULL
, 0);
140 ERR("Unable to get delivery buffer (%x)\n", hr
);
144 hr
= IMediaSample_SetActualDataLength(pOutSample
, 0);
147 hr
= IMediaSample_GetPointer(pOutSample
, &pbDstStream
);
149 ERR("Unable to get pointer to buffer (%x)\n", hr
);
152 cbDstStream
= IMediaSample_GetSize(pOutSample
);
153 if (cbDstStream
< This
->pBihOut
->biSizeImage
) {
154 ERR("Sample size is too small %d < %d\n", cbDstStream
, This
->pBihOut
->biSizeImage
);
159 if (IMediaSample_IsPreroll(pSample
) == S_OK
)
160 flags
|= ICDECOMPRESS_PREROLL
;
161 if (IMediaSample_IsSyncPoint(pSample
) != S_OK
)
162 flags
|= ICDECOMPRESS_NOTKEYFRAME
;
163 if (IMediaSample_GetTime(pSample
, &tStart
, NULL
) == S_OK
&&
164 AVIDec_DropSample(This
, tStart
))
165 flags
|= ICDECOMPRESS_HURRYUP
;
167 res
= ICDecompress(This
->hvid
, flags
, This
->pBihIn
, pbSrcStream
, This
->pBihOut
, pbDstStream
);
169 ERR("Error occurred during the decompression (%x)\n", res
);
171 /* Drop sample if its intended to be dropped */
172 if (flags
& ICDECOMPRESS_HURRYUP
)
175 IMediaSample_SetActualDataLength(pOutSample
, This
->pBihOut
->biSizeImage
);
177 IMediaSample_SetPreroll(pOutSample
, (IMediaSample_IsPreroll(pSample
) == S_OK
));
178 IMediaSample_SetDiscontinuity(pOutSample
, (IMediaSample_IsDiscontinuity(pSample
) == S_OK
));
179 IMediaSample_SetSyncPoint(pOutSample
, (IMediaSample_IsSyncPoint(pSample
) == S_OK
));
181 if (IMediaSample_GetTime(pSample
, &tStart
, &tStop
) == S_OK
)
182 IMediaSample_SetTime(pOutSample
, &tStart
, &tStop
);
184 IMediaSample_SetTime(pOutSample
, NULL
, NULL
);
186 if (IMediaSample_GetMediaTime(pSample
, &tStart
, &tStop
) == S_OK
)
187 IMediaSample_SetMediaTime(pOutSample
, &tStart
, &tStop
);
189 IMediaSample_SetMediaTime(pOutSample
, NULL
, NULL
);
191 LeaveCriticalSection(&This
->tf
.filter
.csFilter
);
192 hr
= BaseOutputPinImpl_Deliver((BaseOutputPin
*)This
->tf
.ppPins
[1], pOutSample
);
193 if (hr
!= S_OK
&& hr
!= VFW_E_NOT_CONNECTED
)
194 ERR("Error sending sample (%x)\n", hr
);
195 IMediaSample_Release(pOutSample
);
200 IMediaSample_Release(pOutSample
);
202 LeaveCriticalSection(&This
->tf
.filter
.csFilter
);
206 static HRESULT WINAPI
AVIDec_StopStreaming(TransformFilter
* pTransformFilter
)
208 AVIDecImpl
* This
= (AVIDecImpl
*)pTransformFilter
;
211 TRACE("(%p)->()\n", This
);
216 result
= ICDecompressEnd(This
->hvid
);
217 if (result
!= ICERR_OK
)
219 ERR("Cannot stop processing (%d)\n", result
);
225 static HRESULT WINAPI
AVIDec_SetMediaType(TransformFilter
*tf
, PIN_DIRECTION dir
, const AM_MEDIA_TYPE
* pmt
)
227 AVIDecImpl
* This
= (AVIDecImpl
*)tf
;
228 HRESULT hr
= VFW_E_TYPE_NOT_ACCEPTED
;
230 TRACE("(%p)->(%p)\n", This
, pmt
);
232 if (dir
!= PINDIR_INPUT
)
235 /* Check root (GUID w/o FOURCC) */
236 if ((IsEqualIID(&pmt
->majortype
, &MEDIATYPE_Video
)) &&
237 (!memcmp(((const char *)&pmt
->subtype
)+4, ((const char *)&MEDIATYPE_Video
)+4, sizeof(GUID
)-4)))
239 VIDEOINFOHEADER
*format1
= (VIDEOINFOHEADER
*)pmt
->pbFormat
;
240 VIDEOINFOHEADER2
*format2
= (VIDEOINFOHEADER2
*)pmt
->pbFormat
;
241 BITMAPINFOHEADER
*bmi
;
243 if (IsEqualIID(&pmt
->formattype
, &FORMAT_VideoInfo
))
244 bmi
= &format1
->bmiHeader
;
245 else if (IsEqualIID(&pmt
->formattype
, &FORMAT_VideoInfo2
))
246 bmi
= &format2
->bmiHeader
;
249 TRACE("Fourcc: %s\n", debugstr_an((const char *)&pmt
->subtype
.Data1
, 4));
251 This
->hvid
= ICLocate(pmt
->majortype
.Data1
, pmt
->subtype
.Data1
, bmi
, NULL
, ICMODE_DECOMPRESS
);
254 AM_MEDIA_TYPE
* outpmt
= &This
->tf
.pmt
;
255 const CLSID
* outsubtype
;
257 DWORD output_depth
= bmi
->biBitCount
;
259 FreeMediaType(outpmt
);
261 switch(bmi
->biBitCount
)
263 case 32: outsubtype
= &MEDIASUBTYPE_RGB32
; break;
264 case 24: outsubtype
= &MEDIASUBTYPE_RGB24
; break;
265 case 16: outsubtype
= &MEDIASUBTYPE_RGB565
; break;
266 case 8: outsubtype
= &MEDIASUBTYPE_RGB8
; break;
268 WARN("Non standard input depth %d, forced output depth to 32\n", bmi
->biBitCount
);
269 outsubtype
= &MEDIASUBTYPE_RGB32
;
274 /* Copy bitmap header from media type to 1 for input and 1 for output */
275 bih_size
= bmi
->biSize
+ bmi
->biClrUsed
* 4;
276 This
->pBihIn
= CoTaskMemAlloc(bih_size
);
282 This
->pBihOut
= CoTaskMemAlloc(bih_size
);
288 memcpy(This
->pBihIn
, bmi
, bih_size
);
289 memcpy(This
->pBihOut
, bmi
, bih_size
);
291 /* Update output format as non compressed bitmap */
292 This
->pBihOut
->biCompression
= 0;
293 This
->pBihOut
->biBitCount
= output_depth
;
294 This
->pBihOut
->biSizeImage
= This
->pBihOut
->biWidth
* This
->pBihOut
->biHeight
* This
->pBihOut
->biBitCount
/ 8;
295 TRACE("Size: %u\n", This
->pBihIn
->biSize
);
296 result
= ICDecompressQuery(This
->hvid
, This
->pBihIn
, This
->pBihOut
);
297 if (result
!= ICERR_OK
)
299 ERR("Unable to found a suitable output format (%d)\n", result
);
303 /* Update output media type */
304 CopyMediaType(outpmt
, pmt
);
305 outpmt
->subtype
= *outsubtype
;
307 if (IsEqualIID(&pmt
->formattype
, &FORMAT_VideoInfo
))
308 memcpy(&(((VIDEOINFOHEADER
*)outpmt
->pbFormat
)->bmiHeader
), This
->pBihOut
, This
->pBihOut
->biSize
);
309 else if (IsEqualIID(&pmt
->formattype
, &FORMAT_VideoInfo2
))
310 memcpy(&(((VIDEOINFOHEADER2
*)outpmt
->pbFormat
)->bmiHeader
), This
->pBihOut
, This
->pBihOut
->biSize
);
314 TRACE("Connection accepted\n");
317 TRACE("Unable to find a suitable VFW decompressor\n");
322 TRACE("Connection refused\n");
326 static HRESULT WINAPI
AVIDec_CompleteConnect(TransformFilter
*tf
, PIN_DIRECTION dir
, IPin
*pin
)
328 AVIDecImpl
* This
= (AVIDecImpl
*)tf
;
330 TRACE("(%p)\n", This
);
335 static HRESULT WINAPI
AVIDec_BreakConnect(TransformFilter
*tf
, PIN_DIRECTION dir
)
337 AVIDecImpl
*This
= (AVIDecImpl
*)tf
;
339 TRACE("(%p)->()\n", This
);
341 if (dir
== PINDIR_INPUT
)
346 CoTaskMemFree(This
->pBihIn
);
348 CoTaskMemFree(This
->pBihOut
);
352 This
->pBihOut
= NULL
;
358 static HRESULT WINAPI
AVIDec_DecideBufferSize(TransformFilter
*tf
, IMemAllocator
*pAlloc
, ALLOCATOR_PROPERTIES
*ppropInputRequest
)
360 AVIDecImpl
*pAVI
= (AVIDecImpl
*)tf
;
361 ALLOCATOR_PROPERTIES actual
;
363 if (!ppropInputRequest
->cbAlign
)
364 ppropInputRequest
->cbAlign
= 1;
366 if (ppropInputRequest
->cbBuffer
< pAVI
->pBihOut
->biSizeImage
)
367 ppropInputRequest
->cbBuffer
= pAVI
->pBihOut
->biSizeImage
;
369 if (!ppropInputRequest
->cBuffers
)
370 ppropInputRequest
->cBuffers
= 1;
372 return IMemAllocator_SetProperties(pAlloc
, ppropInputRequest
, &actual
);
375 static const TransformFilterFuncTable AVIDec_FuncsTable
= {
376 AVIDec_DecideBufferSize
,
377 AVIDec_StartStreaming
,
379 AVIDec_StopStreaming
,
382 AVIDec_CompleteConnect
,
391 HRESULT
AVIDec_create(IUnknown
* pUnkOuter
, LPVOID
* ppv
)
396 TRACE("(%p, %p)\n", pUnkOuter
, ppv
);
401 return CLASS_E_NOAGGREGATION
;
403 hr
= TransformFilter_Construct(&AVIDec_Vtbl
, sizeof(AVIDecImpl
), &CLSID_AVIDec
, &AVIDec_FuncsTable
, (IBaseFilter
**)&This
);
409 ISeekingPassThru
*passthru
;
410 hr
= CoCreateInstance(&CLSID_SeekingPassThru
, (IUnknown
*)This
, CLSCTX_INPROC_SERVER
, &IID_IUnknown
, (void**)&This
->seekthru_unk
);
411 IUnknown_QueryInterface(This
->seekthru_unk
, &IID_ISeekingPassThru
, (void**)&passthru
);
412 ISeekingPassThru_Init(passthru
, FALSE
, (IPin
*)This
->tf
.ppPins
[0]);
413 ISeekingPassThru_Release(passthru
);
418 This
->pBihOut
= NULL
;
425 HRESULT WINAPI
AVIDec_QueryInterface(IBaseFilter
* iface
, REFIID riid
, LPVOID
* ppv
)
428 AVIDecImpl
*This
= (AVIDecImpl
*)iface
;
429 TRACE("(%p/%p)->(%s, %p)\n", This
, iface
, qzdebugstr_guid(riid
), ppv
);
431 if (IsEqualIID(riid
, &IID_IMediaSeeking
))
432 return IUnknown_QueryInterface(This
->seekthru_unk
, riid
, ppv
);
434 hr
= TransformFilterImpl_QueryInterface(iface
, riid
, ppv
);
439 static const IBaseFilterVtbl AVIDec_Vtbl
=
441 AVIDec_QueryInterface
,
442 BaseFilterImpl_AddRef
,
443 TransformFilterImpl_Release
,
444 BaseFilterImpl_GetClassID
,
445 TransformFilterImpl_Stop
,
446 TransformFilterImpl_Pause
,
447 TransformFilterImpl_Run
,
448 BaseFilterImpl_GetState
,
449 BaseFilterImpl_SetSyncSource
,
450 BaseFilterImpl_GetSyncSource
,
451 BaseFilterImpl_EnumPins
,
452 TransformFilterImpl_FindPin
,
453 BaseFilterImpl_QueryFilterInfo
,
454 BaseFilterImpl_JoinFilterGraph
,
455 BaseFilterImpl_QueryVendorInfo