d3dxof: Do not expect a separator when there is no element.
[wine/multimedia.git] / dlls / quartz / avidec.c
blob68b1908d6e6891b497bb256261477ac2ade04083
1 /*
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 "config.h"
23 #include "quartz_private.h"
24 #include "pin.h"
26 #include "uuids.h"
27 #include "amvideo.h"
28 #include "windef.h"
29 #include "winbase.h"
30 #include "dshow.h"
31 #include "strmif.h"
32 #include "vfwmsgs.h"
33 #include "vfw.h"
34 #include "dvdmedia.h"
36 #include <assert.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;
48 HIC hvid;
49 BITMAPINFOHEADER* pBihIn;
50 BITMAPINFOHEADER* pBihOut;
51 } AVIDecImpl;
53 static HRESULT AVIDec_ProcessBegin(TransformFilterImpl* pTransformFilter)
55 AVIDecImpl* This = (AVIDecImpl*)pTransformFilter;
56 DWORD result;
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);
64 return E_FAIL;
66 return S_OK;
69 static HRESULT AVIDec_ProcessSampleData(InputPin *pin, IMediaSample *pSample)
71 AVIDecImpl* This = (AVIDecImpl *)pin->pin.pinInfo.pFilter;
72 AM_MEDIA_TYPE amt;
73 HRESULT hr;
74 DWORD res;
75 IMediaSample* pOutSample = NULL;
76 DWORD cbDstStream;
77 LPBYTE pbDstStream;
78 DWORD cbSrcStream;
79 LPBYTE pbSrcStream;
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);
92 return S_FALSE;
95 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
96 if (FAILED(hr))
98 ERR("Cannot get pointer to sample data (%x)\n", hr);
99 goto error;
102 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
104 TRACE("Sample data ptr = %p, size = %ld\n", pbSrcStream, (long)cbSrcStream);
106 hr = IPin_ConnectionMediaType(This->tf.ppPins[0], &amt);
107 if (FAILED(hr)) {
108 ERR("Unable to retrieve media type\n");
109 goto error;
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);
116 if (FAILED(hr)) {
117 ERR("Unable to get delivery buffer (%x)\n", hr);
118 goto error;
121 hr = IMediaSample_SetActualDataLength(pOutSample, 0);
122 assert(hr == S_OK);
124 hr = IMediaSample_GetPointer(pOutSample, &pbDstStream);
125 if (FAILED(hr)) {
126 ERR("Unable to get pointer to buffer (%x)\n", hr);
127 goto error;
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);
132 hr = E_FAIL;
133 goto error;
136 res = ICDecompress(This->hvid, 0, This->pBihIn, pbSrcStream, This->pBihOut, pbDstStream);
137 if (res != ICERR_OK)
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);
148 else
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);
156 return hr;
158 error:
159 if (pOutSample)
160 IMediaSample_Release(pOutSample);
162 LeaveCriticalSection(&This->tf.csFilter);
163 return hr;
166 static HRESULT AVIDec_ProcessEnd(TransformFilterImpl* pTransformFilter)
168 AVIDecImpl* This = (AVIDecImpl*)pTransformFilter;
169 DWORD result;
171 TRACE("(%p)->()\n", This);
173 if (!This->hvid)
174 return S_OK;
176 result = ICDecompressEnd(This->hvid);
177 if (result != ICERR_OK)
179 ERR("Cannot stop processing (%d)\n", result);
180 return E_FAIL;
182 return S_OK;
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;
204 else
205 goto failed;
206 TRACE("Fourcc: %s\n", debugstr_an((char *)&pmt->subtype.Data1, 4));
208 This->hvid = ICLocate(pmt->majortype.Data1, pmt->subtype.Data1, bmi, NULL, ICMODE_DECOMPRESS);
209 if (This->hvid)
211 AM_MEDIA_TYPE* outpmt = &This->tf.pmt;
212 const CLSID* outsubtype;
213 DWORD bih_size;
214 DWORD output_depth = bmi->biBitCount;
215 DWORD result;
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;
224 default:
225 WARN("Non standard input depth %d, forced output depth to 32\n", bmi->biBitCount);
226 outsubtype = &MEDIASUBTYPE_RGB32;
227 output_depth = 32;
228 break;
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);
234 if (!This->pBihIn)
236 hr = E_OUTOFMEMORY;
237 goto failed;
239 This->pBihOut = CoTaskMemAlloc(bih_size);
240 if (!This->pBihOut)
242 hr = E_OUTOFMEMORY;
243 goto failed;
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);
257 goto failed;
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);
268 else
269 assert(0);
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");
275 return S_OK;
277 TRACE("Unable to find a suitable VFW decompressor\n");
280 failed:
282 TRACE("Connection refused\n");
283 return hr;
286 static HRESULT AVIDec_Cleanup(InputPin *pin)
288 AVIDecImpl *This = (AVIDecImpl *)pin->pin.pinInfo.pFilter;
290 TRACE("(%p)->()\n", This);
292 if (This->hvid)
293 ICClose(This->hvid);
294 if (This->pBihIn)
295 CoTaskMemFree(This->pBihIn);
296 if (This->pBihOut)
297 CoTaskMemFree(This->pBihOut);
299 This->hvid = NULL;
300 This->pBihIn = NULL;
301 This->pBihOut = NULL;
303 return S_OK;
306 static const TransformFuncsTable AVIDec_FuncsTable = {
307 AVIDec_ProcessBegin,
308 AVIDec_ProcessSampleData,
309 AVIDec_ProcessEnd,
310 NULL,
311 AVIDec_ConnectInput,
312 AVIDec_Cleanup
315 HRESULT AVIDec_create(IUnknown * pUnkOuter, LPVOID * ppv)
317 HRESULT hr;
318 AVIDecImpl * This;
320 TRACE("(%p, %p)\n", pUnkOuter, ppv);
322 *ppv = NULL;
324 if (pUnkOuter)
325 return CLASS_E_NOAGGREGATION;
327 /* Note: This memory is managed by the transform filter once created */
328 This = CoTaskMemAlloc(sizeof(AVIDecImpl));
330 This->hvid = NULL;
331 This->pBihIn = NULL;
332 This->pBihOut = NULL;
334 hr = TransformFilter_Create(&(This->tf), &CLSID_AVIDec, &AVIDec_FuncsTable, NULL, NULL, NULL);
336 if (FAILED(hr))
337 return hr;
339 *ppv = This;
341 return hr;