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 #include "transform.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(quartz
);
45 typedef struct AVIDecImpl
47 TransformFilterImpl tf
;
49 BITMAPINFOHEADER
* pBihIn
;
50 BITMAPINFOHEADER
* pBihOut
;
53 static HRESULT
AVIDec_ProcessBegin(TransformFilterImpl
* pTransformFilter
)
55 AVIDecImpl
* This
= (AVIDecImpl
*)pTransformFilter
;
58 TRACE("(%p)->()\n", This
);
60 result
= ICDecompressBegin(This
->hvid
, This
->pBihIn
, This
->pBihOut
);
61 if (result
!= ICERR_OK
)
63 ERR("Cannot start processing (%d)\n", result
);
69 static HRESULT
AVIDec_ProcessSampleData(InputPin
*pin
, IMediaSample
*pSample
)
71 AVIDecImpl
* This
= (AVIDecImpl
*)pin
->pin
.pinInfo
.pFilter
;
75 IMediaSample
* pOutSample
= NULL
;
80 LONGLONG tStart
, tStop
;
82 EnterCriticalSection(&This
->tf
.csFilter
);
83 if (This
->tf
.state
== State_Stopped
)
85 LeaveCriticalSection(&This
->tf
.csFilter
);
86 return VFW_E_WRONG_STATE
;
89 if (pin
->end_of_stream
|| pin
->flushing
)
91 LeaveCriticalSection(&This
->tf
.csFilter
);
95 hr
= IMediaSample_GetPointer(pSample
, &pbSrcStream
);
98 ERR("Cannot get pointer to sample data (%x)\n", hr
);
102 cbSrcStream
= IMediaSample_GetActualDataLength(pSample
);
104 TRACE("Sample data ptr = %p, size = %d\n", pbSrcStream
, cbSrcStream
);
106 hr
= IPin_ConnectionMediaType(This
->tf
.ppPins
[0], &amt
);
108 ERR("Unable to retrieve media type\n");
112 /* Update input size to match sample size */
113 This
->pBihIn
->biSizeImage
= cbSrcStream
;
115 hr
= OutputPin_GetDeliveryBuffer((OutputPin
*)This
->tf
.ppPins
[1], &pOutSample
, NULL
, NULL
, 0);
117 ERR("Unable to get delivery buffer (%x)\n", hr
);
121 hr
= IMediaSample_SetActualDataLength(pOutSample
, 0);
124 hr
= IMediaSample_GetPointer(pOutSample
, &pbDstStream
);
126 ERR("Unable to get pointer to buffer (%x)\n", hr
);
129 cbDstStream
= IMediaSample_GetSize(pOutSample
);
130 if (cbDstStream
< This
->pBihOut
->biSizeImage
) {
131 ERR("Sample size is too small %d < %d\n", cbDstStream
, This
->pBihOut
->biSizeImage
);
136 res
= ICDecompress(This
->hvid
, 0, This
->pBihIn
, pbSrcStream
, This
->pBihOut
, pbDstStream
);
138 ERR("Error occurred during the decompression (%x)\n", res
);
140 IMediaSample_SetActualDataLength(pOutSample
, This
->pBihOut
->biSizeImage
);
142 IMediaSample_SetPreroll(pOutSample
, (IMediaSample_IsPreroll(pSample
) == S_OK
));
143 IMediaSample_SetDiscontinuity(pOutSample
, (IMediaSample_IsDiscontinuity(pSample
) == S_OK
));
144 IMediaSample_SetSyncPoint(pOutSample
, (IMediaSample_IsSyncPoint(pSample
) == S_OK
));
146 if (IMediaSample_GetTime(pSample
, &tStart
, &tStop
) == S_OK
)
147 IMediaSample_SetTime(pOutSample
, &tStart
, &tStop
);
149 IMediaSample_SetTime(pOutSample
, NULL
, NULL
);
151 LeaveCriticalSection(&This
->tf
.csFilter
);
152 hr
= OutputPin_SendSample((OutputPin
*)This
->tf
.ppPins
[1], pOutSample
);
153 if (hr
!= S_OK
&& hr
!= VFW_E_NOT_CONNECTED
)
154 ERR("Error sending sample (%x)\n", hr
);
155 IMediaSample_Release(pOutSample
);
160 IMediaSample_Release(pOutSample
);
162 LeaveCriticalSection(&This
->tf
.csFilter
);
166 static HRESULT
AVIDec_ProcessEnd(TransformFilterImpl
* pTransformFilter
)
168 AVIDecImpl
* This
= (AVIDecImpl
*)pTransformFilter
;
171 TRACE("(%p)->()\n", This
);
176 result
= ICDecompressEnd(This
->hvid
);
177 if (result
!= ICERR_OK
)
179 ERR("Cannot stop processing (%d)\n", result
);
185 static HRESULT
AVIDec_ConnectInput(InputPin
*pin
, const AM_MEDIA_TYPE
* pmt
)
187 AVIDecImpl
* This
= (AVIDecImpl
*)pin
->pin
.pinInfo
.pFilter
;
188 HRESULT hr
= VFW_E_TYPE_NOT_ACCEPTED
;
190 TRACE("(%p)->(%p)\n", This
, pmt
);
192 /* Check root (GUID w/o FOURCC) */
193 if ((IsEqualIID(&pmt
->majortype
, &MEDIATYPE_Video
)) &&
194 (!memcmp(((const char *)&pmt
->subtype
)+4, ((const char *)&MEDIATYPE_Video
)+4, sizeof(GUID
)-4)))
196 VIDEOINFOHEADER
*format1
= (VIDEOINFOHEADER
*)pmt
->pbFormat
;
197 VIDEOINFOHEADER2
*format2
= (VIDEOINFOHEADER2
*)pmt
->pbFormat
;
198 BITMAPINFOHEADER
*bmi
;
200 if (IsEqualIID(&pmt
->formattype
, &FORMAT_VideoInfo
))
201 bmi
= &format1
->bmiHeader
;
202 else if (IsEqualIID(&pmt
->formattype
, &FORMAT_VideoInfo2
))
203 bmi
= &format2
->bmiHeader
;
206 TRACE("Fourcc: %s\n", debugstr_an((const char *)&pmt
->subtype
.Data1
, 4));
208 This
->hvid
= ICLocate(pmt
->majortype
.Data1
, pmt
->subtype
.Data1
, bmi
, NULL
, ICMODE_DECOMPRESS
);
211 AM_MEDIA_TYPE
* outpmt
= &This
->tf
.pmt
;
212 const CLSID
* outsubtype
;
214 DWORD output_depth
= bmi
->biBitCount
;
216 FreeMediaType(outpmt
);
218 switch(bmi
->biBitCount
)
220 case 32: outsubtype
= &MEDIASUBTYPE_RGB32
; break;
221 case 24: outsubtype
= &MEDIASUBTYPE_RGB24
; break;
222 case 16: outsubtype
= &MEDIASUBTYPE_RGB565
; break;
223 case 8: outsubtype
= &MEDIASUBTYPE_RGB8
; break;
225 WARN("Non standard input depth %d, forced output depth to 32\n", bmi
->biBitCount
);
226 outsubtype
= &MEDIASUBTYPE_RGB32
;
231 /* Copy bitmap header from media type to 1 for input and 1 for output */
232 bih_size
= bmi
->biSize
+ bmi
->biClrUsed
* 4;
233 This
->pBihIn
= CoTaskMemAlloc(bih_size
);
239 This
->pBihOut
= CoTaskMemAlloc(bih_size
);
245 memcpy(This
->pBihIn
, bmi
, bih_size
);
246 memcpy(This
->pBihOut
, bmi
, bih_size
);
248 /* Update output format as non compressed bitmap */
249 This
->pBihOut
->biCompression
= 0;
250 This
->pBihOut
->biBitCount
= output_depth
;
251 This
->pBihOut
->biSizeImage
= This
->pBihOut
->biWidth
* This
->pBihOut
->biHeight
* This
->pBihOut
->biBitCount
/ 8;
252 TRACE("Size: %u\n", This
->pBihIn
->biSize
);
253 result
= ICDecompressQuery(This
->hvid
, This
->pBihIn
, This
->pBihOut
);
254 if (result
!= ICERR_OK
)
256 ERR("Unable to found a suitable output format (%d)\n", result
);
260 /* Update output media type */
261 CopyMediaType(outpmt
, pmt
);
262 outpmt
->subtype
= *outsubtype
;
264 if (IsEqualIID(&pmt
->formattype
, &FORMAT_VideoInfo
))
265 memcpy(&(((VIDEOINFOHEADER
*)outpmt
->pbFormat
)->bmiHeader
), This
->pBihOut
, This
->pBihOut
->biSize
);
266 else if (IsEqualIID(&pmt
->formattype
, &FORMAT_VideoInfo2
))
267 memcpy(&(((VIDEOINFOHEADER2
*)outpmt
->pbFormat
)->bmiHeader
), This
->pBihOut
, This
->pBihOut
->biSize
);
271 /* Update buffer size of media samples in output */
272 ((OutputPin
*)This
->tf
.ppPins
[1])->allocProps
.cbBuffer
= This
->pBihOut
->biSizeImage
;
274 TRACE("Connection accepted\n");
277 TRACE("Unable to find a suitable VFW decompressor\n");
282 TRACE("Connection refused\n");
286 static HRESULT
AVIDec_Cleanup(InputPin
*pin
)
288 AVIDecImpl
*This
= (AVIDecImpl
*)pin
->pin
.pinInfo
.pFilter
;
290 TRACE("(%p)->()\n", This
);
295 CoTaskMemFree(This
->pBihIn
);
297 CoTaskMemFree(This
->pBihOut
);
301 This
->pBihOut
= NULL
;
306 static const TransformFuncsTable AVIDec_FuncsTable
= {
308 AVIDec_ProcessSampleData
,
315 HRESULT
AVIDec_create(IUnknown
* pUnkOuter
, LPVOID
* ppv
)
320 TRACE("(%p, %p)\n", pUnkOuter
, ppv
);
325 return CLASS_E_NOAGGREGATION
;
327 /* Note: This memory is managed by the transform filter once created */
328 This
= CoTaskMemAlloc(sizeof(AVIDecImpl
));
332 This
->pBihOut
= NULL
;
334 hr
= TransformFilter_Create(&(This
->tf
), &CLSID_AVIDec
, &AVIDec_FuncsTable
, NULL
, NULL
, NULL
);