winepulse: Set pulse master volume to 0 when session is muted.
[wine.git] / dlls / qcap / avico.c
blobfbcce49a441f42cbbb56131ef45c356c39f2fc38
1 /*
2 * Copyright 2013 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "qcap_private.h"
20 #include "vfw.h"
21 #include "aviriff.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(qcap);
25 typedef struct {
26 struct strmbase_filter filter;
27 IPersistPropertyBag IPersistPropertyBag_iface;
29 struct strmbase_sink sink;
30 struct strmbase_source source;
32 DWORD fcc_handler;
33 HIC hic;
35 VIDEOINFOHEADER *videoinfo;
36 size_t videoinfo_size;
37 DWORD driver_flags;
38 DWORD max_frame_size;
40 DWORD frame_cnt;
41 } AVICompressor;
43 static inline AVICompressor *impl_from_strmbase_filter(struct strmbase_filter *filter)
45 return CONTAINING_RECORD(filter, AVICompressor, filter);
48 static inline AVICompressor *impl_from_strmbase_pin(struct strmbase_pin *pin)
50 return impl_from_strmbase_filter(pin->filter);
53 static HRESULT ensure_driver(AVICompressor *This)
55 if(This->hic)
56 return S_OK;
58 This->hic = ICOpen(FCC('v','i','d','c'), This->fcc_handler, ICMODE_COMPRESS);
59 if(!This->hic) {
60 FIXME("ICOpen failed\n");
61 return E_FAIL;
64 return S_OK;
67 static HRESULT fill_format_info(AVICompressor *This, VIDEOINFOHEADER *src_videoinfo)
69 DWORD size;
70 ICINFO icinfo;
71 HRESULT hres;
73 hres = ensure_driver(This);
74 if(hres != S_OK)
75 return hres;
77 size = ICGetInfo(This->hic, &icinfo, sizeof(icinfo));
78 if(size != sizeof(icinfo))
79 return E_FAIL;
81 size = ICCompressGetFormatSize(This->hic, &src_videoinfo->bmiHeader);
82 if(!size) {
83 FIXME("ICCompressGetFormatSize failed\n");
84 return E_FAIL;
87 size += FIELD_OFFSET(VIDEOINFOHEADER, bmiHeader);
88 if (!(This->videoinfo = calloc(1, size)))
89 return E_OUTOFMEMORY;
91 This->videoinfo_size = size;
92 This->driver_flags = icinfo.dwFlags;
93 ICCompressGetFormat(This->hic, &src_videoinfo->bmiHeader, &This->videoinfo->bmiHeader);
95 This->videoinfo->dwBitRate = 10000000/src_videoinfo->AvgTimePerFrame * This->videoinfo->bmiHeader.biSizeImage * 8;
96 This->videoinfo->AvgTimePerFrame = src_videoinfo->AvgTimePerFrame;
97 This->max_frame_size = This->videoinfo->bmiHeader.biSizeImage;
98 return S_OK;
101 static struct strmbase_pin *avi_compressor_get_pin(struct strmbase_filter *iface, unsigned int index)
103 AVICompressor *filter = impl_from_strmbase_filter(iface);
105 if (index == 0)
106 return &filter->sink.pin;
107 else if (index == 1)
108 return &filter->source.pin;
109 return NULL;
112 static void avi_compressor_destroy(struct strmbase_filter *iface)
114 AVICompressor *filter = impl_from_strmbase_filter(iface);
116 if (filter->hic)
117 ICClose(filter->hic);
118 free(filter->videoinfo);
119 strmbase_sink_cleanup(&filter->sink);
120 strmbase_source_cleanup(&filter->source);
121 strmbase_filter_cleanup(&filter->filter);
122 free(filter);
125 static HRESULT avi_compressor_query_interface(struct strmbase_filter *iface, REFIID iid, void **out)
127 AVICompressor *filter = impl_from_strmbase_filter(iface);
129 if (IsEqualGUID(iid, &IID_IPersistPropertyBag))
130 *out = &filter->IPersistPropertyBag_iface;
131 else
132 return E_NOINTERFACE;
134 IUnknown_AddRef((IUnknown *)*out);
135 return S_OK;
138 static HRESULT avi_compressor_init_stream(struct strmbase_filter *iface)
140 AVICompressor *filter = impl_from_strmbase_filter(iface);
141 HRESULT hr;
143 if (filter->source.pAllocator && FAILED(hr = IMemAllocator_Commit(filter->source.pAllocator)))
145 ERR("Failed to commit allocator, hr %#x.\n", hr);
146 return hr;
149 filter->frame_cnt = 0;
151 return S_OK;
154 static HRESULT avi_compressor_cleanup_stream(struct strmbase_filter *iface)
156 AVICompressor *filter = impl_from_strmbase_filter(iface);
158 ICCompressEnd(filter->hic);
159 return S_OK;
162 static const struct strmbase_filter_ops filter_ops =
164 .filter_get_pin = avi_compressor_get_pin,
165 .filter_destroy = avi_compressor_destroy,
166 .filter_query_interface = avi_compressor_query_interface,
167 .filter_init_stream = avi_compressor_init_stream,
168 .filter_cleanup_stream = avi_compressor_cleanup_stream,
171 static AVICompressor *impl_from_IPersistPropertyBag(IPersistPropertyBag *iface)
173 return CONTAINING_RECORD(iface, AVICompressor, IPersistPropertyBag_iface);
176 static HRESULT WINAPI AVICompressorPropertyBag_QueryInterface(IPersistPropertyBag *iface, REFIID riid, void **ppv)
178 AVICompressor *This = impl_from_IPersistPropertyBag(iface);
179 return IBaseFilter_QueryInterface(&This->filter.IBaseFilter_iface, riid, ppv);
182 static ULONG WINAPI AVICompressorPropertyBag_AddRef(IPersistPropertyBag *iface)
184 AVICompressor *This = impl_from_IPersistPropertyBag(iface);
185 return IBaseFilter_AddRef(&This->filter.IBaseFilter_iface);
188 static ULONG WINAPI AVICompressorPropertyBag_Release(IPersistPropertyBag *iface)
190 AVICompressor *This = impl_from_IPersistPropertyBag(iface);
191 return IBaseFilter_Release(&This->filter.IBaseFilter_iface);
194 static HRESULT WINAPI AVICompressorPropertyBag_GetClassID(IPersistPropertyBag *iface, CLSID *pClassID)
196 AVICompressor *This = impl_from_IPersistPropertyBag(iface);
197 return IBaseFilter_GetClassID(&This->filter.IBaseFilter_iface, pClassID);
200 static HRESULT WINAPI AVICompressorPropertyBag_InitNew(IPersistPropertyBag *iface)
202 AVICompressor *This = impl_from_IPersistPropertyBag(iface);
203 FIXME("(%p)->()\n", This);
204 return E_NOTIMPL;
207 static HRESULT WINAPI AVICompressorPropertyBag_Load(IPersistPropertyBag *iface, IPropertyBag *pPropBag, IErrorLog *pErrorLog)
209 AVICompressor *This = impl_from_IPersistPropertyBag(iface);
210 BSTR str;
211 VARIANT v;
212 HRESULT hres;
214 TRACE("(%p)->(%p %p)\n", This, pPropBag, pErrorLog);
216 V_VT(&v) = VT_BSTR;
217 hres = IPropertyBag_Read(pPropBag, L"FccHandler", &v, NULL);
218 if(FAILED(hres)) {
219 WARN("Could not read FccHandler: %08x\n", hres);
220 return hres;
223 if(V_VT(&v) != VT_BSTR) {
224 FIXME("Got vt %d\n", V_VT(&v));
225 VariantClear(&v);
226 return E_FAIL;
229 str = V_BSTR(&v);
230 TRACE("FccHandler = %s\n", debugstr_w(str));
231 if(SysStringLen(str) != 4) {
232 FIXME("Invalid FccHandler len\n");
233 SysFreeString(str);
234 return E_FAIL;
237 This->fcc_handler = FCC(str[0], str[1], str[2], str[3]);
238 SysFreeString(str);
239 return S_OK;
242 static HRESULT WINAPI AVICompressorPropertyBag_Save(IPersistPropertyBag *iface, IPropertyBag *pPropBag,
243 BOOL fClearDirty, BOOL fSaveAllProperties)
245 AVICompressor *This = impl_from_IPersistPropertyBag(iface);
246 FIXME("(%p)->(%p %x %x)\n", This, pPropBag, fClearDirty, fSaveAllProperties);
247 return E_NOTIMPL;
250 static const IPersistPropertyBagVtbl PersistPropertyBagVtbl = {
251 AVICompressorPropertyBag_QueryInterface,
252 AVICompressorPropertyBag_AddRef,
253 AVICompressorPropertyBag_Release,
254 AVICompressorPropertyBag_GetClassID,
255 AVICompressorPropertyBag_InitNew,
256 AVICompressorPropertyBag_Load,
257 AVICompressorPropertyBag_Save
260 static HRESULT sink_query_accept(struct strmbase_pin *base, const AM_MEDIA_TYPE *pmt)
262 AVICompressor *This = impl_from_strmbase_pin(base);
263 VIDEOINFOHEADER *videoinfo;
264 HRESULT hres;
265 DWORD res;
267 TRACE("(%p)->(AM_MEDIA_TYPE(%p))\n", base, pmt);
269 if(!IsEqualIID(&pmt->majortype, &MEDIATYPE_Video))
270 return S_FALSE;
272 if(!IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo)) {
273 FIXME("formattype %s unsupported\n", debugstr_guid(&pmt->formattype));
274 return S_FALSE;
277 hres = ensure_driver(This);
278 if(hres != S_OK)
279 return hres;
281 videoinfo = (VIDEOINFOHEADER*)pmt->pbFormat;
282 res = ICCompressQuery(This->hic, &videoinfo->bmiHeader, NULL);
283 return res == ICERR_OK ? S_OK : S_FALSE;
286 static HRESULT sink_query_interface(struct strmbase_pin *iface, REFIID iid, void **out)
288 AVICompressor *filter = impl_from_strmbase_pin(iface);
290 if (IsEqualGUID(iid, &IID_IMemInputPin))
291 *out = &filter->sink.IMemInputPin_iface;
292 else
293 return E_NOINTERFACE;
295 IUnknown_AddRef((IUnknown *)*out);
296 return S_OK;
299 static HRESULT WINAPI AVICompressorIn_Receive(struct strmbase_sink *base, IMediaSample *pSample)
301 AVICompressor *This = impl_from_strmbase_pin(&base->pin);
302 IMemInputPin *meminput = This->source.pMemInputPin;
303 VIDEOINFOHEADER *src_videoinfo;
304 REFERENCE_TIME start, stop;
305 IMediaSample *out_sample;
306 AM_MEDIA_TYPE *mt;
307 IMediaSample2 *sample2;
308 DWORD comp_flags = 0;
309 BOOL is_preroll;
310 BOOL sync_point;
311 BYTE *ptr, *buf;
312 DWORD res;
313 HRESULT hres;
315 TRACE("(%p)->(%p)\n", base, pSample);
317 if (!meminput)
319 WARN("Source is not connected, returning VFW_E_NOT_CONNECTED.\n");
320 return VFW_E_NOT_CONNECTED;
323 if(!This->hic) {
324 FIXME("Driver not loaded\n");
325 return E_UNEXPECTED;
328 hres = IMediaSample_QueryInterface(pSample, &IID_IMediaSample2, (void**)&sample2);
329 if(SUCCEEDED(hres)) {
330 FIXME("Use IMediaSample2\n");
331 IMediaSample2_Release(sample2);
334 is_preroll = IMediaSample_IsPreroll(pSample) == S_OK;
335 sync_point = IMediaSample_IsSyncPoint(pSample) == S_OK;
337 hres = IMediaSample_GetTime(pSample, &start, &stop);
338 if(FAILED(hres)) {
339 WARN("GetTime failed: %08x\n", hres);
340 return hres;
343 hres = IMediaSample_GetMediaType(pSample, &mt);
344 if(FAILED(hres))
345 return hres;
347 hres = IMediaSample_GetPointer(pSample, &ptr);
348 if(FAILED(hres)) {
349 WARN("GetPointer failed: %08x\n", hres);
350 return hres;
353 hres = BaseOutputPinImpl_GetDeliveryBuffer(&This->source, &out_sample, &start, &stop, 0);
354 if(FAILED(hres))
355 return hres;
357 hres = IMediaSample_GetPointer(out_sample, &buf);
358 if(FAILED(hres))
359 return hres;
361 if((This->driver_flags & VIDCF_TEMPORAL) && !(This->driver_flags & VIDCF_FASTTEMPORALC))
362 FIXME("Unsupported temporal compression\n");
364 src_videoinfo = (VIDEOINFOHEADER *)This->sink.pin.mt.pbFormat;
365 This->videoinfo->bmiHeader.biSizeImage = This->max_frame_size;
366 res = ICCompress(This->hic, sync_point ? ICCOMPRESS_KEYFRAME : 0, &This->videoinfo->bmiHeader, buf,
367 &src_videoinfo->bmiHeader, ptr, 0, &comp_flags, This->frame_cnt, 0, 0, NULL, NULL);
368 if(res != ICERR_OK) {
369 WARN("ICCompress failed: %d\n", res);
370 IMediaSample_Release(out_sample);
371 return E_FAIL;
374 IMediaSample_SetActualDataLength(out_sample, This->videoinfo->bmiHeader.biSizeImage);
375 IMediaSample_SetPreroll(out_sample, is_preroll);
376 IMediaSample_SetSyncPoint(out_sample, (comp_flags&AVIIF_KEYFRAME) != 0);
377 IMediaSample_SetDiscontinuity(out_sample, (IMediaSample_IsDiscontinuity(pSample) == S_OK));
379 if (IMediaSample_GetMediaTime(pSample, &start, &stop) == S_OK)
380 IMediaSample_SetMediaTime(out_sample, &start, &stop);
381 else
382 IMediaSample_SetMediaTime(out_sample, NULL, NULL);
384 hres = IMemInputPin_Receive(meminput, out_sample);
385 if(FAILED(hres))
386 WARN("Deliver failed: %08x\n", hres);
388 IMediaSample_Release(out_sample);
389 This->frame_cnt++;
390 return hres;
393 static HRESULT sink_connect(struct strmbase_sink *iface, IPin *peer, const AM_MEDIA_TYPE *mt)
395 AVICompressor *filter = impl_from_strmbase_pin(&iface->pin);
396 return fill_format_info(filter, (VIDEOINFOHEADER *)mt->pbFormat);
399 static void sink_disconnect(struct strmbase_sink *iface)
401 AVICompressor *filter = impl_from_strmbase_pin(&iface->pin);
402 free(filter->videoinfo);
403 filter->videoinfo = NULL;
406 static const struct strmbase_sink_ops sink_ops =
408 .base.pin_query_accept = sink_query_accept,
409 .base.pin_query_interface = sink_query_interface,
410 .pfnReceive = AVICompressorIn_Receive,
411 .sink_connect = sink_connect,
412 .sink_disconnect = sink_disconnect,
415 static HRESULT source_get_media_type(struct strmbase_pin *base, unsigned int iPosition, AM_MEDIA_TYPE *amt)
417 AVICompressor *This = impl_from_strmbase_filter(base->filter);
419 if(iPosition || !This->videoinfo)
420 return S_FALSE;
422 amt->majortype = MEDIATYPE_Video;
423 amt->subtype = MEDIASUBTYPE_PCM;
424 amt->bFixedSizeSamples = FALSE;
425 amt->bTemporalCompression = (This->driver_flags & VIDCF_TEMPORAL) != 0;
426 amt->lSampleSize = This->sink.pin.mt.lSampleSize;
427 amt->formattype = FORMAT_VideoInfo;
428 amt->pUnk = NULL;
429 amt->cbFormat = This->videoinfo_size;
430 amt->pbFormat = (BYTE*)This->videoinfo;
431 return S_OK;
434 static HRESULT WINAPI AVICompressorOut_DecideBufferSize(struct strmbase_source *base,
435 IMemAllocator *alloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
437 AVICompressor *This = impl_from_strmbase_pin(&base->pin);
438 ALLOCATOR_PROPERTIES actual;
440 TRACE("(%p)\n", This);
442 if (!ppropInputRequest->cBuffers)
443 ppropInputRequest->cBuffers = 1;
444 if (ppropInputRequest->cbBuffer < This->max_frame_size)
445 ppropInputRequest->cbBuffer = This->max_frame_size;
446 if (!ppropInputRequest->cbAlign)
447 ppropInputRequest->cbAlign = 1;
449 return IMemAllocator_SetProperties(alloc, ppropInputRequest, &actual);
452 static HRESULT WINAPI AVICompressorOut_DecideAllocator(struct strmbase_source *base,
453 IMemInputPin *pPin, IMemAllocator **pAlloc)
455 TRACE("(%p)->(%p %p)\n", base, pPin, pAlloc);
456 return BaseOutputPinImpl_DecideAllocator(base, pPin, pAlloc);
459 static const struct strmbase_source_ops source_ops =
461 .base.pin_get_media_type = source_get_media_type,
462 .pfnAttemptConnection = BaseOutputPinImpl_AttemptConnection,
463 .pfnDecideBufferSize = AVICompressorOut_DecideBufferSize,
464 .pfnDecideAllocator = AVICompressorOut_DecideAllocator,
467 HRESULT avi_compressor_create(IUnknown *outer, IUnknown **out)
469 AVICompressor *object;
471 if (!(object = calloc(1, sizeof(*object))))
472 return E_OUTOFMEMORY;
474 strmbase_filter_init(&object->filter, outer, &CLSID_AVICo, &filter_ops);
475 object->IPersistPropertyBag_iface.lpVtbl = &PersistPropertyBagVtbl;
477 strmbase_sink_init(&object->sink, &object->filter, L"In", &sink_ops, NULL);
478 strmbase_source_init(&object->source, &object->filter, L"Out", &source_ops);
480 TRACE("Created AVI compressor %p.\n", object);
481 *out = &object->filter.IUnknown_inner;
482 return S_OK;