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 "quartz_private.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(quartz
);
39 typedef struct AVIDecImpl
44 BITMAPINFOHEADER
* pBihIn
;
45 BITMAPINFOHEADER
* pBihOut
;
49 static inline AVIDecImpl
*impl_from_TransformFilter( TransformFilter
*iface
)
51 return CONTAINING_RECORD(iface
, AVIDecImpl
, tf
);
54 static HRESULT WINAPI
AVIDec_StartStreaming(TransformFilter
* pTransformFilter
)
56 AVIDecImpl
* This
= impl_from_TransformFilter(pTransformFilter
);
59 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_EndFlush(TransformFilter
*pTransformFilter
) {
72 AVIDecImpl
* This
= impl_from_TransformFilter(pTransformFilter
);
77 static HRESULT WINAPI
AVIDec_NotifyDrop(TransformFilter
*pTransformFilter
, IBaseFilter
*sender
, Quality qm
) {
78 AVIDecImpl
*This
= impl_from_TransformFilter(pTransformFilter
);
80 EnterCriticalSection(&This
->tf
.filter
.csFilter
);
82 This
->late
= qm
.Late
+ qm
.TimeStamp
;
85 LeaveCriticalSection(&This
->tf
.filter
.csFilter
);
89 static int AVIDec_DropSample(AVIDecImpl
*This
, REFERENCE_TIME tStart
) {
93 if (tStart
< This
->late
) {
94 TRACE("Dropping sample\n");
101 static HRESULT WINAPI
AVIDec_Receive(TransformFilter
*tf
, IMediaSample
*pSample
)
103 AVIDecImpl
* This
= impl_from_TransformFilter(tf
);
106 IMediaSample
* pOutSample
= NULL
;
111 LONGLONG tStart
, tStop
;
114 hr
= IMediaSample_GetPointer(pSample
, &pbSrcStream
);
117 ERR("Cannot get pointer to sample data (%x)\n", hr
);
121 cbSrcStream
= IMediaSample_GetActualDataLength(pSample
);
123 TRACE("Sample data ptr = %p, size = %d\n", pbSrcStream
, cbSrcStream
);
125 /* Update input size to match sample size */
126 This
->pBihIn
->biSizeImage
= cbSrcStream
;
128 hr
= BaseOutputPinImpl_GetDeliveryBuffer(&This
->tf
.source
, &pOutSample
, NULL
, NULL
, 0);
130 ERR("Unable to get delivery buffer (%x)\n", hr
);
134 hr
= IMediaSample_SetActualDataLength(pOutSample
, 0);
137 hr
= IMediaSample_GetPointer(pOutSample
, &pbDstStream
);
139 ERR("Unable to get pointer to buffer (%x)\n", hr
);
140 IMediaSample_Release(pOutSample
);
143 cbDstStream
= IMediaSample_GetSize(pOutSample
);
144 if (cbDstStream
< This
->pBihOut
->biSizeImage
) {
145 ERR("Sample size is too small %d < %d\n", cbDstStream
, This
->pBihOut
->biSizeImage
);
146 IMediaSample_Release(pOutSample
);
150 if (IMediaSample_IsPreroll(pSample
) == S_OK
)
151 flags
|= ICDECOMPRESS_PREROLL
;
152 if (IMediaSample_IsSyncPoint(pSample
) != S_OK
)
153 flags
|= ICDECOMPRESS_NOTKEYFRAME
;
154 hr
= IMediaSample_GetTime(pSample
, &tStart
, &tStop
);
155 if (hr
== S_OK
&& AVIDec_DropSample(This
, tStart
))
156 flags
|= ICDECOMPRESS_HURRYUP
;
158 res
= ICDecompress(This
->hvid
, flags
, This
->pBihIn
, pbSrcStream
, This
->pBihOut
, pbDstStream
);
160 ERR("Error occurred during the decompression (%x)\n", res
);
162 /* Drop sample if it's intended to be dropped */
163 if (flags
& ICDECOMPRESS_HURRYUP
) {
164 IMediaSample_Release(pOutSample
);
168 IMediaSample_SetActualDataLength(pOutSample
, This
->pBihOut
->biSizeImage
);
170 IMediaSample_SetPreroll(pOutSample
, (IMediaSample_IsPreroll(pSample
) == S_OK
));
171 IMediaSample_SetDiscontinuity(pOutSample
, (IMediaSample_IsDiscontinuity(pSample
) == S_OK
));
172 IMediaSample_SetSyncPoint(pOutSample
, (IMediaSample_IsSyncPoint(pSample
) == S_OK
));
175 IMediaSample_SetTime(pOutSample
, &tStart
, &tStop
);
176 else if (hr
== VFW_S_NO_STOP_TIME
)
177 IMediaSample_SetTime(pOutSample
, &tStart
, NULL
);
179 IMediaSample_SetTime(pOutSample
, NULL
, NULL
);
181 if (IMediaSample_GetMediaTime(pSample
, &tStart
, &tStop
) == S_OK
)
182 IMediaSample_SetMediaTime(pOutSample
, &tStart
, &tStop
);
184 IMediaSample_SetMediaTime(pOutSample
, NULL
, NULL
);
186 hr
= IMemInputPin_Receive(This
->tf
.source
.pMemInputPin
, pOutSample
);
187 if (hr
!= S_OK
&& hr
!= VFW_E_NOT_CONNECTED
)
188 ERR("Error sending sample (%x)\n", hr
);
190 IMediaSample_Release(pOutSample
);
194 static HRESULT WINAPI
AVIDec_StopStreaming(TransformFilter
* pTransformFilter
)
196 AVIDecImpl
* This
= impl_from_TransformFilter(pTransformFilter
);
199 TRACE("(%p)->()\n", This
);
204 result
= ICDecompressEnd(This
->hvid
);
205 if (result
!= ICERR_OK
)
207 ERR("Cannot stop processing (%d)\n", result
);
213 static HRESULT
avi_dec_connect_sink(TransformFilter
*tf
, const AM_MEDIA_TYPE
*pmt
)
215 AVIDecImpl
* This
= impl_from_TransformFilter(tf
);
216 HRESULT hr
= VFW_E_TYPE_NOT_ACCEPTED
;
218 /* Check root (GUID w/o FOURCC) */
219 if ((IsEqualIID(&pmt
->majortype
, &MEDIATYPE_Video
)) &&
220 (!memcmp(((const char *)&pmt
->subtype
)+4, ((const char *)&MEDIATYPE_Video
)+4, sizeof(GUID
)-4)))
222 VIDEOINFOHEADER
*format1
= (VIDEOINFOHEADER
*)pmt
->pbFormat
;
223 VIDEOINFOHEADER2
*format2
= (VIDEOINFOHEADER2
*)pmt
->pbFormat
;
224 BITMAPINFOHEADER
*bmi
;
226 if (IsEqualIID(&pmt
->formattype
, &FORMAT_VideoInfo
))
227 bmi
= &format1
->bmiHeader
;
228 else if (IsEqualIID(&pmt
->formattype
, &FORMAT_VideoInfo2
))
229 bmi
= &format2
->bmiHeader
;
233 This
->hvid
= ICLocate(pmt
->majortype
.Data1
, pmt
->subtype
.Data1
, bmi
, NULL
, ICMODE_DECOMPRESS
);
236 AM_MEDIA_TYPE
* outpmt
= &This
->tf
.pmt
;
237 const CLSID
* outsubtype
;
239 DWORD output_depth
= bmi
->biBitCount
;
241 FreeMediaType(outpmt
);
243 switch(bmi
->biBitCount
)
245 case 32: outsubtype
= &MEDIASUBTYPE_RGB32
; break;
246 case 24: outsubtype
= &MEDIASUBTYPE_RGB24
; break;
247 case 16: outsubtype
= &MEDIASUBTYPE_RGB565
; break;
248 case 8: outsubtype
= &MEDIASUBTYPE_RGB8
; break;
250 WARN("Non standard input depth %d, forced output depth to 32\n", bmi
->biBitCount
);
251 outsubtype
= &MEDIASUBTYPE_RGB32
;
256 /* Copy bitmap header from media type to 1 for input and 1 for output */
257 bih_size
= bmi
->biSize
+ bmi
->biClrUsed
* 4;
258 This
->pBihIn
= CoTaskMemAlloc(bih_size
);
264 This
->pBihOut
= CoTaskMemAlloc(bih_size
);
270 memcpy(This
->pBihIn
, bmi
, bih_size
);
271 memcpy(This
->pBihOut
, bmi
, bih_size
);
273 /* Update output format as non compressed bitmap */
274 This
->pBihOut
->biCompression
= 0;
275 This
->pBihOut
->biBitCount
= output_depth
;
276 This
->pBihOut
->biSizeImage
= This
->pBihOut
->biWidth
* This
->pBihOut
->biHeight
* This
->pBihOut
->biBitCount
/ 8;
277 TRACE("Size: %u\n", This
->pBihIn
->biSize
);
278 result
= ICDecompressQuery(This
->hvid
, This
->pBihIn
, This
->pBihOut
);
279 if (result
!= ICERR_OK
)
281 ERR("Unable to found a suitable output format (%d)\n", result
);
285 /* Update output media type */
286 CopyMediaType(outpmt
, pmt
);
287 outpmt
->subtype
= *outsubtype
;
289 if (IsEqualIID(&pmt
->formattype
, &FORMAT_VideoInfo
))
290 memcpy(&(((VIDEOINFOHEADER
*)outpmt
->pbFormat
)->bmiHeader
), This
->pBihOut
, This
->pBihOut
->biSize
);
291 else if (IsEqualIID(&pmt
->formattype
, &FORMAT_VideoInfo2
))
292 memcpy(&(((VIDEOINFOHEADER2
*)outpmt
->pbFormat
)->bmiHeader
), This
->pBihOut
, This
->pBihOut
->biSize
);
296 TRACE("Connection accepted\n");
299 TRACE("Unable to find a suitable VFW decompressor\n");
304 TRACE("Connection refused\n");
308 static HRESULT WINAPI
AVIDec_BreakConnect(TransformFilter
*tf
, PIN_DIRECTION dir
)
310 AVIDecImpl
*This
= impl_from_TransformFilter(tf
);
312 TRACE("(%p)->()\n", This
);
314 if (dir
== PINDIR_INPUT
)
318 CoTaskMemFree(This
->pBihIn
);
319 CoTaskMemFree(This
->pBihOut
);
322 This
->pBihOut
= NULL
;
328 static HRESULT WINAPI
AVIDec_DecideBufferSize(TransformFilter
*tf
, IMemAllocator
*pAlloc
, ALLOCATOR_PROPERTIES
*ppropInputRequest
)
330 AVIDecImpl
*pAVI
= impl_from_TransformFilter(tf
);
331 ALLOCATOR_PROPERTIES actual
;
333 if (!ppropInputRequest
->cbAlign
)
334 ppropInputRequest
->cbAlign
= 1;
336 if (ppropInputRequest
->cbBuffer
< pAVI
->pBihOut
->biSizeImage
)
337 ppropInputRequest
->cbBuffer
= pAVI
->pBihOut
->biSizeImage
;
339 if (!ppropInputRequest
->cBuffers
)
340 ppropInputRequest
->cBuffers
= 1;
342 return IMemAllocator_SetProperties(pAlloc
, ppropInputRequest
, &actual
);
345 static const TransformFilterFuncTable AVIDec_FuncsTable
= {
346 .pfnDecideBufferSize
= AVIDec_DecideBufferSize
,
347 .pfnStartStreaming
= AVIDec_StartStreaming
,
348 .pfnReceive
= AVIDec_Receive
,
349 .pfnStopStreaming
= AVIDec_StopStreaming
,
350 .transform_connect_sink
= avi_dec_connect_sink
,
351 .pfnBreakConnect
= AVIDec_BreakConnect
,
352 .pfnEndFlush
= AVIDec_EndFlush
,
353 .pfnNotify
= AVIDec_NotifyDrop
,
356 HRESULT
AVIDec_create(IUnknown
*outer
, void **out
)
363 hr
= strmbase_transform_create(sizeof(AVIDecImpl
), outer
, &CLSID_AVIDec
,
364 &AVIDec_FuncsTable
, (IBaseFilter
**)&This
);
371 This
->pBihOut
= NULL
;
373 *out
= &This
->tf
.filter
.IUnknown_inner
;