winewayland.drv: Implement vkGetPhysicalDeviceSurfaceSupportKHR.
[wine.git] / dlls / quartz / avidec.c
blob8cc729748c1a8e597ea4af58d04ceaa842cc9227
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 "quartz_private.h"
23 #include "uuids.h"
24 #include "amvideo.h"
25 #include "windef.h"
26 #include "winbase.h"
27 #include "dshow.h"
28 #include "strmif.h"
29 #include "vfwmsgs.h"
30 #include "vfw.h"
31 #include "dvdmedia.h"
33 #include <assert.h>
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
39 struct avi_decompressor
41 struct strmbase_filter filter;
43 struct strmbase_source source;
44 IQualityControl source_IQualityControl_iface;
45 struct strmbase_passthrough passthrough;
47 struct strmbase_sink sink;
49 HIC hvid;
50 BITMAPINFOHEADER* pBihIn;
51 REFERENCE_TIME late;
54 static struct avi_decompressor *impl_from_strmbase_filter(struct strmbase_filter *iface)
56 return CONTAINING_RECORD(iface, struct avi_decompressor, filter);
59 static HRESULT avi_decompressor_sink_query_interface(struct strmbase_pin *iface, REFIID iid, void **out)
61 struct avi_decompressor *filter = impl_from_strmbase_filter(iface->filter);
63 if (IsEqualGUID(iid, &IID_IMemInputPin))
64 *out = &filter->sink.IMemInputPin_iface;
65 else
66 return E_NOINTERFACE;
68 IUnknown_AddRef((IUnknown *)*out);
69 return S_OK;
72 static HRESULT avi_decompressor_sink_query_accept(struct strmbase_pin *iface, const AM_MEDIA_TYPE *mt)
74 return S_OK;
77 static HRESULT avi_decompressor_sink_end_flush(struct strmbase_sink *iface)
79 struct avi_decompressor *filter = impl_from_strmbase_filter(iface->pin.filter);
80 filter->late = -1;
81 if (filter->source.pin.peer)
82 return IPin_EndFlush(filter->source.pin.peer);
83 return S_OK;
86 static int AVIDec_DropSample(struct avi_decompressor *This, REFERENCE_TIME tStart)
88 if (This->late < 0)
89 return 0;
91 if (tStart < This->late) {
92 TRACE("Dropping sample\n");
93 return 1;
95 This->late = -1;
96 return 0;
99 static HRESULT WINAPI avi_decompressor_sink_Receive(struct strmbase_sink *iface, IMediaSample *pSample)
101 struct avi_decompressor *This = impl_from_strmbase_filter(iface->pin.filter);
102 VIDEOINFOHEADER *source_format;
103 HRESULT hr;
104 IMediaSample* pOutSample = NULL;
105 LONG cbDstStream, cbSrcStream;
106 LPBYTE pbDstStream;
107 LPBYTE pbSrcStream;
108 LONGLONG tStart, tStop;
109 DWORD flags = 0;
110 LRESULT res;
112 /* We do not expect pin connection state to change while the filter is
113 * running. This guarantee is necessary, since otherwise we would have to
114 * take the filter lock, and we can't take the filter lock from a streaming
115 * thread. */
116 if (!This->source.pMemInputPin)
118 WARN("Source is not connected, returning VFW_E_NOT_CONNECTED.\n");
119 return VFW_E_NOT_CONNECTED;
122 source_format = (VIDEOINFOHEADER *)This->source.pin.mt.pbFormat;
124 if (This->filter.state == State_Stopped)
125 return VFW_E_WRONG_STATE;
127 if (This->sink.flushing)
128 return S_FALSE;
130 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
131 if (FAILED(hr))
133 ERR("Failed to get input buffer pointer, hr %#lx.\n", hr);
134 return hr;
137 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
139 /* Update input size to match sample size */
140 This->pBihIn->biSizeImage = cbSrcStream;
142 if (FAILED(hr = IMemAllocator_GetBuffer(This->source.pAllocator, &pOutSample, NULL, NULL, 0)))
144 ERR("Failed to get sample, hr %#lx.\n", hr);
145 return hr;
148 hr = IMediaSample_SetActualDataLength(pOutSample, 0);
149 assert(hr == S_OK);
151 hr = IMediaSample_GetPointer(pOutSample, &pbDstStream);
152 if (FAILED(hr)) {
153 ERR("Failed to get output buffer pointer, hr %#lx.\n", hr);
154 IMediaSample_Release(pOutSample);
155 return hr;
157 cbDstStream = IMediaSample_GetSize(pOutSample);
158 if (cbDstStream < source_format->bmiHeader.biSizeImage)
160 ERR("Sample size is too small (%ld < %lu).\n", cbDstStream, source_format->bmiHeader.biSizeImage);
161 IMediaSample_Release(pOutSample);
162 return E_FAIL;
165 if (IMediaSample_IsPreroll(pSample) == S_OK)
166 flags |= ICDECOMPRESS_PREROLL;
167 if (IMediaSample_IsSyncPoint(pSample) != S_OK)
168 flags |= ICDECOMPRESS_NOTKEYFRAME;
169 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
170 if (hr == S_OK && AVIDec_DropSample(This, tStart))
171 flags |= ICDECOMPRESS_HURRYUP;
173 res = ICDecompress(This->hvid, flags, This->pBihIn, pbSrcStream, &source_format->bmiHeader, pbDstStream);
174 if (res != ICERR_OK)
175 ERR("Failed to decompress, error %Id.\n", res);
177 /* Drop sample if it's intended to be dropped */
178 if (flags & ICDECOMPRESS_HURRYUP) {
179 IMediaSample_Release(pOutSample);
180 return S_OK;
183 IMediaSample_SetActualDataLength(pOutSample, source_format->bmiHeader.biSizeImage);
185 IMediaSample_SetPreroll(pOutSample, (IMediaSample_IsPreroll(pSample) == S_OK));
186 IMediaSample_SetDiscontinuity(pOutSample, (IMediaSample_IsDiscontinuity(pSample) == S_OK));
187 IMediaSample_SetSyncPoint(pOutSample, (IMediaSample_IsSyncPoint(pSample) == S_OK));
189 if (hr == S_OK)
190 IMediaSample_SetTime(pOutSample, &tStart, &tStop);
191 else if (hr == VFW_S_NO_STOP_TIME)
192 IMediaSample_SetTime(pOutSample, &tStart, NULL);
193 else
194 IMediaSample_SetTime(pOutSample, NULL, NULL);
196 hr = IMemInputPin_Receive(This->source.pMemInputPin, pOutSample);
197 if (hr != S_OK && hr != VFW_E_NOT_CONNECTED)
198 ERR("Failed to send sample, hr %#lx.\n", hr);
200 IMediaSample_Release(pOutSample);
201 return hr;
204 static HRESULT avi_decompressor_sink_connect(struct strmbase_sink *iface, IPin *peer, const AM_MEDIA_TYPE *pmt)
206 struct avi_decompressor *This = impl_from_strmbase_filter(iface->pin.filter);
207 HRESULT hr = VFW_E_TYPE_NOT_ACCEPTED;
209 /* Check root (GUID w/o FOURCC) */
210 if ((IsEqualIID(&pmt->majortype, &MEDIATYPE_Video)) &&
211 (!memcmp(((const char *)&pmt->subtype)+4, ((const char *)&MEDIATYPE_Video)+4, sizeof(GUID)-4)))
213 VIDEOINFOHEADER *format1 = (VIDEOINFOHEADER *)pmt->pbFormat;
214 VIDEOINFOHEADER2 *format2 = (VIDEOINFOHEADER2 *)pmt->pbFormat;
215 BITMAPINFOHEADER *bmi;
217 if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo))
218 bmi = &format1->bmiHeader;
219 else if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo2))
220 bmi = &format2->bmiHeader;
221 else
222 goto failed;
224 This->hvid = ICLocate(pmt->majortype.Data1, pmt->subtype.Data1, bmi, NULL, ICMODE_DECOMPRESS);
225 if (This->hvid)
227 DWORD bih_size;
228 LRESULT result;
230 /* Copy bitmap header from media type to 1 for input and 1 for output */
231 bih_size = bmi->biSize + bmi->biClrUsed * 4;
232 This->pBihIn = CoTaskMemAlloc(bih_size);
233 if (!This->pBihIn)
235 hr = E_OUTOFMEMORY;
236 goto failed;
238 memcpy(This->pBihIn, bmi, bih_size);
240 if ((result = ICDecompressQuery(This->hvid, This->pBihIn, NULL)))
242 WARN("No decompressor found, error %Id.\n", result);
243 return VFW_E_TYPE_NOT_ACCEPTED;
246 TRACE("Connection accepted\n");
247 return S_OK;
249 TRACE("Unable to find a suitable VFW decompressor\n");
252 failed:
254 TRACE("Connection refused\n");
255 return hr;
258 static void avi_decompressor_sink_disconnect(struct strmbase_sink *iface)
260 struct avi_decompressor *filter = impl_from_strmbase_filter(iface->pin.filter);
262 if (filter->hvid)
263 ICClose(filter->hvid);
264 CoTaskMemFree(filter->pBihIn);
265 filter->hvid = NULL;
266 filter->pBihIn = NULL;
269 static const struct strmbase_sink_ops sink_ops =
271 .base.pin_query_interface = avi_decompressor_sink_query_interface,
272 .base.pin_query_accept = avi_decompressor_sink_query_accept,
273 .pfnReceive = avi_decompressor_sink_Receive,
274 .sink_connect = avi_decompressor_sink_connect,
275 .sink_disconnect = avi_decompressor_sink_disconnect,
276 .sink_end_flush = avi_decompressor_sink_end_flush,
279 static HRESULT avi_decompressor_source_query_interface(struct strmbase_pin *iface, REFIID iid, void **out)
281 struct avi_decompressor *filter = impl_from_strmbase_filter(iface->filter);
283 if (IsEqualGUID(iid, &IID_IQualityControl))
284 *out = &filter->source_IQualityControl_iface;
285 else if (IsEqualGUID(iid, &IID_IMediaSeeking))
286 *out = &filter->passthrough.IMediaSeeking_iface;
287 else
288 return E_NOINTERFACE;
290 IUnknown_AddRef((IUnknown *)*out);
291 return S_OK;
294 static HRESULT avi_decompressor_source_query_accept(struct strmbase_pin *iface, const AM_MEDIA_TYPE *mt)
296 struct avi_decompressor *filter = impl_from_strmbase_filter(iface->filter);
297 VIDEOINFOHEADER *sink_format, *format;
299 if (!filter->sink.pin.peer || !IsEqualGUID(&mt->formattype, &FORMAT_VideoInfo))
300 return S_FALSE;
302 sink_format = (VIDEOINFOHEADER *)filter->sink.pin.mt.pbFormat;
303 format = (VIDEOINFOHEADER *)mt->pbFormat;
305 if (ICDecompressQuery(filter->hvid, &sink_format->bmiHeader, &format->bmiHeader))
306 return S_FALSE;
308 return S_OK;
311 static HRESULT avi_decompressor_source_get_media_type(struct strmbase_pin *iface,
312 unsigned int index, AM_MEDIA_TYPE *mt)
314 static const struct
316 const GUID *subtype;
317 DWORD compression;
318 WORD bpp;
320 formats[] =
322 {&MEDIASUBTYPE_CLJR, mmioFOURCC('C','L','J','R'), 8},
323 {&MEDIASUBTYPE_UYVY, mmioFOURCC('U','Y','V','Y'), 16},
324 {&MEDIASUBTYPE_YUY2, mmioFOURCC('Y','U','Y','2'), 16},
325 {&MEDIASUBTYPE_RGB32, BI_RGB, 32},
326 {&MEDIASUBTYPE_RGB24, BI_RGB, 24},
327 {&MEDIASUBTYPE_RGB565, BI_BITFIELDS, 16},
328 {&MEDIASUBTYPE_RGB555, BI_RGB, 16},
329 {&MEDIASUBTYPE_RGB8, BI_RGB, 8},
332 struct avi_decompressor *filter = impl_from_strmbase_filter(iface->filter);
333 const VIDEOINFOHEADER *sink_format;
334 VIDEOINFO *format;
336 if (!filter->sink.pin.peer)
337 return VFW_S_NO_MORE_ITEMS;
339 sink_format = (VIDEOINFOHEADER *)filter->sink.pin.mt.pbFormat;
341 memset(mt, 0, sizeof(AM_MEDIA_TYPE));
343 if (index < ARRAY_SIZE(formats))
345 /* In theory we could allocate less than this, but gcc generates
346 * -Warray-bounds warnings if we access the structure through a
347 * VIDEOINFO pointer, even if we only access valid fields. */
348 if (!(format = CoTaskMemAlloc(sizeof(*format))))
349 return E_OUTOFMEMORY;
350 memset(format, 0, sizeof(*format));
352 format->rcSource = sink_format->rcSource;
353 format->rcTarget = sink_format->rcTarget;
354 format->dwBitRate = sink_format->dwBitRate;
355 format->dwBitErrorRate = sink_format->dwBitErrorRate;
356 format->AvgTimePerFrame = sink_format->AvgTimePerFrame;
358 format->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
359 format->bmiHeader.biWidth = sink_format->bmiHeader.biWidth;
360 format->bmiHeader.biHeight = sink_format->bmiHeader.biHeight;
361 format->bmiHeader.biPlanes = sink_format->bmiHeader.biPlanes;
362 format->bmiHeader.biBitCount = formats[index].bpp;
363 format->bmiHeader.biCompression = formats[index].compression;
364 format->bmiHeader.biSizeImage = format->bmiHeader.biWidth
365 * format->bmiHeader.biHeight * formats[index].bpp / 8;
367 if (IsEqualGUID(formats[index].subtype, &MEDIASUBTYPE_RGB565))
369 format->dwBitMasks[iRED] = 0xf800;
370 format->dwBitMasks[iGREEN] = 0x07e0;
371 format->dwBitMasks[iBLUE] = 0x001f;
372 mt->cbFormat = offsetof(VIDEOINFO, dwBitMasks[3]);
374 else
375 mt->cbFormat = sizeof(VIDEOINFOHEADER);
377 mt->majortype = MEDIATYPE_Video;
378 mt->subtype = *formats[index].subtype;
379 mt->bFixedSizeSamples = TRUE;
380 mt->lSampleSize = format->bmiHeader.biSizeImage;
381 mt->formattype = FORMAT_VideoInfo;
382 mt->pbFormat = (BYTE *)format;
384 return S_OK;
387 if (index == ARRAY_SIZE(formats))
389 size_t size = ICDecompressGetFormatSize(filter->hvid, &sink_format->bmiHeader);
391 if (!size)
392 return VFW_S_NO_MORE_ITEMS;
394 mt->cbFormat = offsetof(VIDEOINFOHEADER, bmiHeader) + size;
395 if (!(format = CoTaskMemAlloc(mt->cbFormat)))
396 return E_OUTOFMEMORY;
397 memset(format, 0, mt->cbFormat);
399 format->rcSource = sink_format->rcSource;
400 format->rcTarget = sink_format->rcTarget;
401 format->dwBitRate = sink_format->dwBitRate;
402 format->dwBitErrorRate = sink_format->dwBitErrorRate;
403 format->AvgTimePerFrame = sink_format->AvgTimePerFrame;
405 if (ICDecompressGetFormat(filter->hvid, &sink_format->bmiHeader, &format->bmiHeader))
407 CoTaskMemFree(format);
408 return VFW_S_NO_MORE_ITEMS;
411 mt->majortype = MEDIATYPE_Video;
412 mt->subtype = MEDIATYPE_Video;
413 mt->subtype.Data1 = format->bmiHeader.biCompression;
414 mt->bFixedSizeSamples = TRUE;
415 mt->lSampleSize = format->bmiHeader.biSizeImage;
416 mt->formattype = FORMAT_VideoInfo;
417 mt->pbFormat = (BYTE *)format;
419 return S_OK;
422 return VFW_S_NO_MORE_ITEMS;
425 static HRESULT WINAPI avi_decompressor_source_DecideBufferSize(struct strmbase_source *iface,
426 IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
428 const VIDEOINFOHEADER *source_format = (VIDEOINFOHEADER *)iface->pin.mt.pbFormat;
429 ALLOCATOR_PROPERTIES actual;
431 if (!ppropInputRequest->cbAlign)
432 ppropInputRequest->cbAlign = 1;
434 if (ppropInputRequest->cbBuffer < source_format->bmiHeader.biSizeImage)
435 ppropInputRequest->cbBuffer = source_format->bmiHeader.biSizeImage;
437 if (!ppropInputRequest->cBuffers)
438 ppropInputRequest->cBuffers = 1;
440 return IMemAllocator_SetProperties(pAlloc, ppropInputRequest, &actual);
443 static const struct strmbase_source_ops source_ops =
445 .base.pin_query_interface = avi_decompressor_source_query_interface,
446 .base.pin_query_accept = avi_decompressor_source_query_accept,
447 .base.pin_get_media_type = avi_decompressor_source_get_media_type,
448 .pfnAttemptConnection = BaseOutputPinImpl_AttemptConnection,
449 .pfnDecideAllocator = BaseOutputPinImpl_DecideAllocator,
450 .pfnDecideBufferSize = avi_decompressor_source_DecideBufferSize,
453 static struct avi_decompressor *impl_from_source_IQualityControl(IQualityControl *iface)
455 return CONTAINING_RECORD(iface, struct avi_decompressor, source_IQualityControl_iface);
458 static HRESULT WINAPI avi_decompressor_source_qc_QueryInterface(IQualityControl *iface,
459 REFIID iid, void **out)
461 struct avi_decompressor *filter = impl_from_source_IQualityControl(iface);
462 return IPin_QueryInterface(&filter->source.pin.IPin_iface, iid, out);
465 static ULONG WINAPI avi_decompressor_source_qc_AddRef(IQualityControl *iface)
467 struct avi_decompressor *filter = impl_from_source_IQualityControl(iface);
468 return IPin_AddRef(&filter->source.pin.IPin_iface);
471 static ULONG WINAPI avi_decompressor_source_qc_Release(IQualityControl *iface)
473 struct avi_decompressor *filter = impl_from_source_IQualityControl(iface);
474 return IPin_Release(&filter->source.pin.IPin_iface);
477 static HRESULT WINAPI avi_decompressor_source_qc_Notify(IQualityControl *iface,
478 IBaseFilter *sender, Quality q)
480 struct avi_decompressor *filter = impl_from_source_IQualityControl(iface);
482 TRACE("filter %p, sender %p, type %#x, proportion %ld, late %s, timestamp %s.\n",
483 filter, sender, q.Type, q.Proportion, debugstr_time(q.Late), debugstr_time(q.TimeStamp));
485 EnterCriticalSection(&filter->filter.stream_cs);
486 if (q.Late > 0)
487 filter->late = q.Late + q.TimeStamp;
488 else
489 filter->late = -1;
490 LeaveCriticalSection(&filter->filter.stream_cs);
491 return S_OK;
494 static HRESULT WINAPI avi_decompressor_source_qc_SetSink(IQualityControl *iface, IQualityControl *sink)
496 struct avi_decompressor *filter = impl_from_source_IQualityControl(iface);
498 TRACE("filter %p, sink %p.\n", filter, sink);
500 return S_OK;
503 static const IQualityControlVtbl source_qc_vtbl =
505 avi_decompressor_source_qc_QueryInterface,
506 avi_decompressor_source_qc_AddRef,
507 avi_decompressor_source_qc_Release,
508 avi_decompressor_source_qc_Notify,
509 avi_decompressor_source_qc_SetSink,
512 static struct strmbase_pin *avi_decompressor_get_pin(struct strmbase_filter *iface, unsigned int index)
514 struct avi_decompressor *filter = impl_from_strmbase_filter(iface);
516 if (index == 0)
517 return &filter->sink.pin;
518 else if (index == 1)
519 return &filter->source.pin;
520 return NULL;
523 static void avi_decompressor_destroy(struct strmbase_filter *iface)
525 struct avi_decompressor *filter = impl_from_strmbase_filter(iface);
527 if (filter->sink.pin.peer)
528 IPin_Disconnect(filter->sink.pin.peer);
529 IPin_Disconnect(&filter->sink.pin.IPin_iface);
531 if (filter->source.pin.peer)
532 IPin_Disconnect(filter->source.pin.peer);
533 IPin_Disconnect(&filter->source.pin.IPin_iface);
535 strmbase_sink_cleanup(&filter->sink);
536 strmbase_source_cleanup(&filter->source);
537 strmbase_passthrough_cleanup(&filter->passthrough);
539 strmbase_filter_cleanup(&filter->filter);
540 free(filter);
543 static HRESULT avi_decompressor_init_stream(struct strmbase_filter *iface)
545 struct avi_decompressor *filter = impl_from_strmbase_filter(iface);
546 VIDEOINFOHEADER *source_format;
547 LRESULT res;
548 HRESULT hr;
550 if (!filter->source.pin.peer)
551 return S_OK;
553 filter->late = -1;
555 source_format = (VIDEOINFOHEADER *)filter->sink.pin.mt.pbFormat;
556 if ((res = ICDecompressBegin(filter->hvid, filter->pBihIn, &source_format->bmiHeader)))
558 ERR("ICDecompressBegin() failed, error %Id.\n", res);
559 return E_FAIL;
562 if (FAILED(hr = IMemAllocator_Commit(filter->source.pAllocator)))
563 ERR("Failed to commit allocator, hr %#lx.\n", hr);
565 return S_OK;
568 static HRESULT avi_decompressor_cleanup_stream(struct strmbase_filter *iface)
570 struct avi_decompressor *filter = impl_from_strmbase_filter(iface);
571 LRESULT res;
573 if (!filter->source.pin.peer)
574 return S_OK;
576 if (filter->hvid && (res = ICDecompressEnd(filter->hvid)))
578 ERR("ICDecompressEnd() failed, error %Id.\n", res);
579 return E_FAIL;
582 IMemAllocator_Decommit(filter->source.pAllocator);
584 return S_OK;
587 static const struct strmbase_filter_ops filter_ops =
589 .filter_get_pin = avi_decompressor_get_pin,
590 .filter_destroy = avi_decompressor_destroy,
591 .filter_init_stream = avi_decompressor_init_stream,
592 .filter_cleanup_stream = avi_decompressor_cleanup_stream,
595 HRESULT avi_dec_create(IUnknown *outer, IUnknown **out)
597 struct avi_decompressor *object;
599 if (!(object = calloc(1, sizeof(*object))))
600 return E_OUTOFMEMORY;
602 strmbase_filter_init(&object->filter, outer, &CLSID_AVIDec, &filter_ops);
604 strmbase_sink_init(&object->sink, &object->filter, L"In", &sink_ops, NULL);
605 wcscpy(object->sink.pin.name, L"XForm In");
607 strmbase_source_init(&object->source, &object->filter, L"Out", &source_ops);
608 wcscpy(object->source.pin.name, L"XForm Out");
610 object->source_IQualityControl_iface.lpVtbl = &source_qc_vtbl;
611 strmbase_passthrough_init(&object->passthrough, (IUnknown *)&object->source.pin.IPin_iface);
612 ISeekingPassThru_Init(&object->passthrough.ISeekingPassThru_iface, FALSE,
613 &object->sink.pin.IPin_iface);
615 TRACE("Created AVI decompressor %p.\n", object);
616 *out = &object->filter.IUnknown_inner;
618 return S_OK;