d3dx9: Allow setting texture to NULL.
[wine.git] / dlls / quartz / avidec.c
blobe7e1de72204a8ba0b06497deeb19a37a50e117f3
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 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
43 typedef struct AVIDecImpl
45 TransformFilter tf;
46 IUnknown *seekthru_unk;
48 HIC hvid;
49 BITMAPINFOHEADER* pBihIn;
50 BITMAPINFOHEADER* pBihOut;
51 REFERENCE_TIME late;
52 } AVIDecImpl;
54 static const IBaseFilterVtbl AVIDec_Vtbl;
56 static HRESULT WINAPI AVIDec_StartStreaming(TransformFilter* pTransformFilter)
58 AVIDecImpl* This = (AVIDecImpl*)pTransformFilter;
59 DWORD result;
61 TRACE("(%p)->()\n", This);
62 This->late = -1;
64 result = ICDecompressBegin(This->hvid, This->pBihIn, This->pBihOut);
65 if (result != ICERR_OK)
67 ERR("Cannot start processing (%d)\n", result);
68 return E_FAIL;
70 return S_OK;
73 static HRESULT WINAPI AVIDec_EndFlush(TransformFilter *pTransformFilter) {
74 AVIDecImpl* This = (AVIDecImpl*)pTransformFilter;
75 This->late = -1;
76 return S_OK;
79 static HRESULT WINAPI AVIDec_NotifyDrop(TransformFilter *pTransformFilter, IBaseFilter *sender, Quality qm) {
80 AVIDecImpl *This = (AVIDecImpl*)pTransformFilter;
82 EnterCriticalSection(&This->tf.filter.csFilter);
83 if (qm.Late > 0)
84 This->late = qm.Late + qm.TimeStamp;
85 else
86 This->late = -1;
87 LeaveCriticalSection(&This->tf.filter.csFilter);
88 return S_OK;
91 static int AVIDec_DropSample(AVIDecImpl *This, REFERENCE_TIME tStart) {
92 if (This->late < 0)
93 return 0;
95 if (tStart < This->late) {
96 TRACE("Dropping sample\n");
97 return 1;
99 This->late = -1;
100 return 0;
103 static HRESULT WINAPI AVIDec_Receive(TransformFilter *tf, IMediaSample *pSample)
105 AVIDecImpl* This = (AVIDecImpl *)tf;
106 AM_MEDIA_TYPE amt;
107 HRESULT hr;
108 DWORD res;
109 IMediaSample* pOutSample = NULL;
110 DWORD cbDstStream;
111 LPBYTE pbDstStream;
112 DWORD cbSrcStream;
113 LPBYTE pbSrcStream;
114 LONGLONG tStart, tStop;
115 DWORD flags = 0;
117 EnterCriticalSection(&This->tf.filter.csFilter);
118 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
119 if (FAILED(hr))
121 ERR("Cannot get pointer to sample data (%x)\n", hr);
122 goto error;
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);
130 if (FAILED(hr)) {
131 ERR("Unable to retrieve media type\n");
132 goto error;
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);
139 if (FAILED(hr)) {
140 ERR("Unable to get delivery buffer (%x)\n", hr);
141 goto error;
144 hr = IMediaSample_SetActualDataLength(pOutSample, 0);
145 assert(hr == S_OK);
147 hr = IMediaSample_GetPointer(pOutSample, &pbDstStream);
148 if (FAILED(hr)) {
149 ERR("Unable to get pointer to buffer (%x)\n", hr);
150 goto error;
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);
155 hr = E_FAIL;
156 goto error;
159 if (IMediaSample_IsPreroll(pSample) == S_OK)
160 flags |= ICDECOMPRESS_PREROLL;
161 if (IMediaSample_IsSyncPoint(pSample) != S_OK)
162 flags |= ICDECOMPRESS_NOTKEYFRAME;
163 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
164 if (hr == S_OK && AVIDec_DropSample(This, tStart))
165 flags |= ICDECOMPRESS_HURRYUP;
167 res = ICDecompress(This->hvid, flags, This->pBihIn, pbSrcStream, This->pBihOut, pbDstStream);
168 if (res != ICERR_OK)
169 ERR("Error occurred during the decompression (%x)\n", res);
171 /* Drop sample if its intended to be dropped */
172 if (flags & ICDECOMPRESS_HURRYUP) {
173 hr = S_OK;
174 goto error;
177 IMediaSample_SetActualDataLength(pOutSample, This->pBihOut->biSizeImage);
179 IMediaSample_SetPreroll(pOutSample, (IMediaSample_IsPreroll(pSample) == S_OK));
180 IMediaSample_SetDiscontinuity(pOutSample, (IMediaSample_IsDiscontinuity(pSample) == S_OK));
181 IMediaSample_SetSyncPoint(pOutSample, (IMediaSample_IsSyncPoint(pSample) == S_OK));
183 if (hr == S_OK)
184 IMediaSample_SetTime(pOutSample, &tStart, &tStop);
185 else if (hr == VFW_S_NO_STOP_TIME)
186 IMediaSample_SetTime(pOutSample, &tStart, NULL);
187 else
188 IMediaSample_SetTime(pOutSample, NULL, NULL);
190 if (IMediaSample_GetMediaTime(pSample, &tStart, &tStop) == S_OK)
191 IMediaSample_SetMediaTime(pOutSample, &tStart, &tStop);
192 else
193 IMediaSample_SetMediaTime(pOutSample, NULL, NULL);
195 LeaveCriticalSection(&This->tf.filter.csFilter);
196 hr = BaseOutputPinImpl_Deliver((BaseOutputPin*)This->tf.ppPins[1], pOutSample);
197 if (hr != S_OK && hr != VFW_E_NOT_CONNECTED)
198 ERR("Error sending sample (%x)\n", hr);
199 IMediaSample_Release(pOutSample);
200 return hr;
202 error:
203 if (pOutSample)
204 IMediaSample_Release(pOutSample);
206 LeaveCriticalSection(&This->tf.filter.csFilter);
207 return hr;
210 static HRESULT WINAPI AVIDec_StopStreaming(TransformFilter* pTransformFilter)
212 AVIDecImpl* This = (AVIDecImpl*)pTransformFilter;
213 DWORD result;
215 TRACE("(%p)->()\n", This);
217 if (!This->hvid)
218 return S_OK;
220 result = ICDecompressEnd(This->hvid);
221 if (result != ICERR_OK)
223 ERR("Cannot stop processing (%d)\n", result);
224 return E_FAIL;
226 return S_OK;
229 static HRESULT WINAPI AVIDec_SetMediaType(TransformFilter *tf, PIN_DIRECTION dir, const AM_MEDIA_TYPE * pmt)
231 AVIDecImpl* This = (AVIDecImpl*)tf;
232 HRESULT hr = VFW_E_TYPE_NOT_ACCEPTED;
234 TRACE("(%p)->(%p)\n", This, pmt);
236 if (dir != PINDIR_INPUT)
237 return S_OK;
239 /* Check root (GUID w/o FOURCC) */
240 if ((IsEqualIID(&pmt->majortype, &MEDIATYPE_Video)) &&
241 (!memcmp(((const char *)&pmt->subtype)+4, ((const char *)&MEDIATYPE_Video)+4, sizeof(GUID)-4)))
243 VIDEOINFOHEADER *format1 = (VIDEOINFOHEADER *)pmt->pbFormat;
244 VIDEOINFOHEADER2 *format2 = (VIDEOINFOHEADER2 *)pmt->pbFormat;
245 BITMAPINFOHEADER *bmi;
247 if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo))
248 bmi = &format1->bmiHeader;
249 else if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo2))
250 bmi = &format2->bmiHeader;
251 else
252 goto failed;
253 TRACE("Fourcc: %s\n", debugstr_an((const char *)&pmt->subtype.Data1, 4));
255 This->hvid = ICLocate(pmt->majortype.Data1, pmt->subtype.Data1, bmi, NULL, ICMODE_DECOMPRESS);
256 if (This->hvid)
258 AM_MEDIA_TYPE* outpmt = &This->tf.pmt;
259 const CLSID* outsubtype;
260 DWORD bih_size;
261 DWORD output_depth = bmi->biBitCount;
262 DWORD result;
263 FreeMediaType(outpmt);
265 switch(bmi->biBitCount)
267 case 32: outsubtype = &MEDIASUBTYPE_RGB32; break;
268 case 24: outsubtype = &MEDIASUBTYPE_RGB24; break;
269 case 16: outsubtype = &MEDIASUBTYPE_RGB565; break;
270 case 8: outsubtype = &MEDIASUBTYPE_RGB8; break;
271 default:
272 WARN("Non standard input depth %d, forced output depth to 32\n", bmi->biBitCount);
273 outsubtype = &MEDIASUBTYPE_RGB32;
274 output_depth = 32;
275 break;
278 /* Copy bitmap header from media type to 1 for input and 1 for output */
279 bih_size = bmi->biSize + bmi->biClrUsed * 4;
280 This->pBihIn = CoTaskMemAlloc(bih_size);
281 if (!This->pBihIn)
283 hr = E_OUTOFMEMORY;
284 goto failed;
286 This->pBihOut = CoTaskMemAlloc(bih_size);
287 if (!This->pBihOut)
289 hr = E_OUTOFMEMORY;
290 goto failed;
292 memcpy(This->pBihIn, bmi, bih_size);
293 memcpy(This->pBihOut, bmi, bih_size);
295 /* Update output format as non compressed bitmap */
296 This->pBihOut->biCompression = 0;
297 This->pBihOut->biBitCount = output_depth;
298 This->pBihOut->biSizeImage = This->pBihOut->biWidth * This->pBihOut->biHeight * This->pBihOut->biBitCount / 8;
299 TRACE("Size: %u\n", This->pBihIn->biSize);
300 result = ICDecompressQuery(This->hvid, This->pBihIn, This->pBihOut);
301 if (result != ICERR_OK)
303 ERR("Unable to found a suitable output format (%d)\n", result);
304 goto failed;
307 /* Update output media type */
308 CopyMediaType(outpmt, pmt);
309 outpmt->subtype = *outsubtype;
311 if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo))
312 memcpy(&(((VIDEOINFOHEADER *)outpmt->pbFormat)->bmiHeader), This->pBihOut, This->pBihOut->biSize);
313 else if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo2))
314 memcpy(&(((VIDEOINFOHEADER2 *)outpmt->pbFormat)->bmiHeader), This->pBihOut, This->pBihOut->biSize);
315 else
316 assert(0);
318 TRACE("Connection accepted\n");
319 return S_OK;
321 TRACE("Unable to find a suitable VFW decompressor\n");
324 failed:
326 TRACE("Connection refused\n");
327 return hr;
330 static HRESULT WINAPI AVIDec_CompleteConnect(TransformFilter *tf, PIN_DIRECTION dir, IPin *pin)
332 AVIDecImpl* This = (AVIDecImpl*)tf;
334 TRACE("(%p)\n", This);
336 return S_OK;
339 static HRESULT WINAPI AVIDec_BreakConnect(TransformFilter *tf, PIN_DIRECTION dir)
341 AVIDecImpl *This = (AVIDecImpl *)tf;
343 TRACE("(%p)->()\n", This);
345 if (dir == PINDIR_INPUT)
347 if (This->hvid)
348 ICClose(This->hvid);
349 if (This->pBihIn)
350 CoTaskMemFree(This->pBihIn);
351 if (This->pBihOut)
352 CoTaskMemFree(This->pBihOut);
354 This->hvid = NULL;
355 This->pBihIn = NULL;
356 This->pBihOut = NULL;
359 return S_OK;
362 static HRESULT WINAPI AVIDec_DecideBufferSize(TransformFilter *tf, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
364 AVIDecImpl *pAVI = (AVIDecImpl*)tf;
365 ALLOCATOR_PROPERTIES actual;
367 if (!ppropInputRequest->cbAlign)
368 ppropInputRequest->cbAlign = 1;
370 if (ppropInputRequest->cbBuffer < pAVI->pBihOut->biSizeImage)
371 ppropInputRequest->cbBuffer = pAVI->pBihOut->biSizeImage;
373 if (!ppropInputRequest->cBuffers)
374 ppropInputRequest->cBuffers = 1;
376 return IMemAllocator_SetProperties(pAlloc, ppropInputRequest, &actual);
379 static const TransformFilterFuncTable AVIDec_FuncsTable = {
380 AVIDec_DecideBufferSize,
381 AVIDec_StartStreaming,
382 AVIDec_Receive,
383 AVIDec_StopStreaming,
384 NULL,
385 AVIDec_SetMediaType,
386 AVIDec_CompleteConnect,
387 AVIDec_BreakConnect,
388 NULL,
389 NULL,
390 AVIDec_EndFlush,
391 NULL,
392 AVIDec_NotifyDrop
395 HRESULT AVIDec_create(IUnknown * pUnkOuter, LPVOID * ppv)
397 HRESULT hr;
398 AVIDecImpl * This;
400 TRACE("(%p, %p)\n", pUnkOuter, ppv);
402 *ppv = NULL;
404 if (pUnkOuter)
405 return CLASS_E_NOAGGREGATION;
407 hr = TransformFilter_Construct(&AVIDec_Vtbl, sizeof(AVIDecImpl), &CLSID_AVIDec, &AVIDec_FuncsTable, (IBaseFilter**)&This);
409 if (FAILED(hr))
410 return hr;
411 else
413 ISeekingPassThru *passthru;
414 hr = CoCreateInstance(&CLSID_SeekingPassThru, (IUnknown*)This, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void**)&This->seekthru_unk);
415 IUnknown_QueryInterface(This->seekthru_unk, &IID_ISeekingPassThru, (void**)&passthru);
416 ISeekingPassThru_Init(passthru, FALSE, (IPin*)This->tf.ppPins[0]);
417 ISeekingPassThru_Release(passthru);
420 This->hvid = NULL;
421 This->pBihIn = NULL;
422 This->pBihOut = NULL;
424 *ppv = This;
426 return hr;
429 static HRESULT WINAPI AVIDec_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
431 HRESULT hr;
432 AVIDecImpl *This = (AVIDecImpl *)iface;
433 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
435 if (IsEqualIID(riid, &IID_IMediaSeeking))
436 return IUnknown_QueryInterface(This->seekthru_unk, riid, ppv);
438 hr = TransformFilterImpl_QueryInterface(iface, riid, ppv);
440 return hr;
443 static const IBaseFilterVtbl AVIDec_Vtbl =
445 AVIDec_QueryInterface,
446 BaseFilterImpl_AddRef,
447 TransformFilterImpl_Release,
448 BaseFilterImpl_GetClassID,
449 TransformFilterImpl_Stop,
450 TransformFilterImpl_Pause,
451 TransformFilterImpl_Run,
452 BaseFilterImpl_GetState,
453 BaseFilterImpl_SetSyncSource,
454 BaseFilterImpl_GetSyncSource,
455 BaseFilterImpl_EnumPins,
456 TransformFilterImpl_FindPin,
457 BaseFilterImpl_QueryFilterInfo,
458 BaseFilterImpl_JoinFilterGraph,
459 BaseFilterImpl_QueryVendorInfo