atl80: Added AtlComModuleRegisterServer implementation (based on AtlModuleRegisterSer...
[wine] / dlls / quartz / avidec.c
1 /*
2  * AVI Decompressor (VFW decompressors wrapper)
3  *
4  * Copyright 2004-2005 Christian Costa
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22
23 #include "quartz_private.h"
24 #include "pin.h"
25
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"
35
36 #include <assert.h>
37
38 #include "wine/unicode.h"
39 #include "wine/debug.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
42
43 typedef struct AVIDecImpl
44 {
45     TransformFilter tf;
46     IUnknown *seekthru_unk;
47
48     HIC hvid;
49     BITMAPINFOHEADER* pBihIn;
50     BITMAPINFOHEADER* pBihOut;
51     REFERENCE_TIME late;
52 } AVIDecImpl;
53
54 static const IBaseFilterVtbl AVIDec_Vtbl;
55
56 static inline AVIDecImpl *impl_from_IBaseFilter( IBaseFilter *iface )
57 {
58     return CONTAINING_RECORD(iface, AVIDecImpl, tf.filter.IBaseFilter_iface);
59 }
60
61 static inline AVIDecImpl *impl_from_TransformFilter( TransformFilter *iface )
62 {
63     return CONTAINING_RECORD(iface, AVIDecImpl, tf.filter);
64 }
65
66 static HRESULT WINAPI AVIDec_StartStreaming(TransformFilter* pTransformFilter)
67 {
68     AVIDecImpl* This = impl_from_TransformFilter(pTransformFilter);
69     DWORD result;
70
71     TRACE("(%p)->()\n", This);
72     This->late = -1;
73
74     result = ICDecompressBegin(This->hvid, This->pBihIn, This->pBihOut);
75     if (result != ICERR_OK)
76     {
77         ERR("Cannot start processing (%d)\n", result);
78         return E_FAIL;
79     }
80     return S_OK;
81 }
82
83 static HRESULT WINAPI AVIDec_EndFlush(TransformFilter *pTransformFilter) {
84     AVIDecImpl* This = impl_from_TransformFilter(pTransformFilter);
85     This->late = -1;
86     return S_OK;
87 }
88
89 static HRESULT WINAPI AVIDec_NotifyDrop(TransformFilter *pTransformFilter, IBaseFilter *sender, Quality qm) {
90     AVIDecImpl *This = impl_from_TransformFilter(pTransformFilter);
91
92     EnterCriticalSection(&This->tf.filter.csFilter);
93     if (qm.Late > 0)
94         This->late = qm.Late + qm.TimeStamp;
95     else
96         This->late = -1;
97     LeaveCriticalSection(&This->tf.filter.csFilter);
98     return S_OK;
99 }
100
101 static int AVIDec_DropSample(AVIDecImpl *This, REFERENCE_TIME tStart) {
102     if (This->late < 0)
103         return 0;
104
105     if (tStart < This->late) {
106         TRACE("Dropping sample\n");
107         return 1;
108     }
109     This->late = -1;
110     return 0;
111 }
112
113 static HRESULT WINAPI AVIDec_Receive(TransformFilter *tf, IMediaSample *pSample)
114 {
115     AVIDecImpl* This = impl_from_TransformFilter(tf);
116     AM_MEDIA_TYPE amt;
117     HRESULT hr;
118     DWORD res;
119     IMediaSample* pOutSample = NULL;
120     DWORD cbDstStream;
121     LPBYTE pbDstStream;
122     DWORD cbSrcStream;
123     LPBYTE pbSrcStream;
124     LONGLONG tStart, tStop;
125     DWORD flags = 0;
126
127     EnterCriticalSection(&This->tf.csReceive);
128     hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
129     if (FAILED(hr))
130     {
131         ERR("Cannot get pointer to sample data (%x)\n", hr);
132         goto error;
133     }
134
135     cbSrcStream = IMediaSample_GetActualDataLength(pSample);
136
137     TRACE("Sample data ptr = %p, size = %d\n", pbSrcStream, cbSrcStream);
138
139     hr = IPin_ConnectionMediaType(This->tf.ppPins[0], &amt);
140     if (FAILED(hr)) {
141         ERR("Unable to retrieve media type\n");
142         goto error;
143     }
144
145     /* Update input size to match sample size */
146     This->pBihIn->biSizeImage = cbSrcStream;
147
148     hr = BaseOutputPinImpl_GetDeliveryBuffer((BaseOutputPin*)This->tf.ppPins[1], &pOutSample, NULL, NULL, 0);
149     if (FAILED(hr)) {
150         ERR("Unable to get delivery buffer (%x)\n", hr);
151         goto error;
152     }
153
154     hr = IMediaSample_SetActualDataLength(pOutSample, 0);
155     assert(hr == S_OK);
156
157     hr = IMediaSample_GetPointer(pOutSample, &pbDstStream);
158     if (FAILED(hr)) {
159         ERR("Unable to get pointer to buffer (%x)\n", hr);
160         goto error;
161     }
162     cbDstStream = IMediaSample_GetSize(pOutSample);
163     if (cbDstStream < This->pBihOut->biSizeImage) {
164         ERR("Sample size is too small %d < %d\n", cbDstStream, This->pBihOut->biSizeImage);
165         hr = E_FAIL;
166         goto error;
167     }
168
169     if (IMediaSample_IsPreroll(pSample) == S_OK)
170         flags |= ICDECOMPRESS_PREROLL;
171     if (IMediaSample_IsSyncPoint(pSample) != S_OK)
172         flags |= ICDECOMPRESS_NOTKEYFRAME;
173     hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
174     if (hr == S_OK && AVIDec_DropSample(This, tStart))
175         flags |= ICDECOMPRESS_HURRYUP;
176
177     res = ICDecompress(This->hvid, flags, This->pBihIn, pbSrcStream, This->pBihOut, pbDstStream);
178     if (res != ICERR_OK)
179         ERR("Error occurred during the decompression (%x)\n", res);
180
181     /* Drop sample if its intended to be dropped */
182     if (flags & ICDECOMPRESS_HURRYUP) {
183         hr = S_OK;
184         goto error;
185     }
186
187     IMediaSample_SetActualDataLength(pOutSample, This->pBihOut->biSizeImage);
188
189     IMediaSample_SetPreroll(pOutSample, (IMediaSample_IsPreroll(pSample) == S_OK));
190     IMediaSample_SetDiscontinuity(pOutSample, (IMediaSample_IsDiscontinuity(pSample) == S_OK));
191     IMediaSample_SetSyncPoint(pOutSample, (IMediaSample_IsSyncPoint(pSample) == S_OK));
192
193     if (hr == S_OK)
194         IMediaSample_SetTime(pOutSample, &tStart, &tStop);
195     else if (hr == VFW_S_NO_STOP_TIME)
196         IMediaSample_SetTime(pOutSample, &tStart, NULL);
197     else
198         IMediaSample_SetTime(pOutSample, NULL, NULL);
199
200     if (IMediaSample_GetMediaTime(pSample, &tStart, &tStop) == S_OK)
201         IMediaSample_SetMediaTime(pOutSample, &tStart, &tStop);
202     else
203         IMediaSample_SetMediaTime(pOutSample, NULL, NULL);
204
205     LeaveCriticalSection(&This->tf.csReceive);
206     hr = BaseOutputPinImpl_Deliver((BaseOutputPin*)This->tf.ppPins[1], pOutSample);
207     EnterCriticalSection(&This->tf.csReceive);
208     if (hr != S_OK && hr != VFW_E_NOT_CONNECTED)
209         ERR("Error sending sample (%x)\n", hr);
210
211 error:
212     if (pOutSample)
213         IMediaSample_Release(pOutSample);
214
215     LeaveCriticalSection(&This->tf.csReceive);
216     return hr;
217 }
218
219 static HRESULT WINAPI AVIDec_StopStreaming(TransformFilter* pTransformFilter)
220 {
221     AVIDecImpl* This = impl_from_TransformFilter(pTransformFilter);
222     DWORD result;
223
224     TRACE("(%p)->()\n", This);
225
226     if (!This->hvid)
227         return S_OK;
228
229     result = ICDecompressEnd(This->hvid);
230     if (result != ICERR_OK)
231     {
232         ERR("Cannot stop processing (%d)\n", result);
233         return E_FAIL;
234     }
235     return S_OK;
236 }
237
238 static HRESULT WINAPI AVIDec_SetMediaType(TransformFilter *tf, PIN_DIRECTION dir, const AM_MEDIA_TYPE * pmt)
239 {
240     AVIDecImpl* This = impl_from_TransformFilter(tf);
241     HRESULT hr = VFW_E_TYPE_NOT_ACCEPTED;
242
243     TRACE("(%p)->(%p)\n", This, pmt);
244
245     if (dir != PINDIR_INPUT)
246         return S_OK;
247
248     /* Check root (GUID w/o FOURCC) */
249     if ((IsEqualIID(&pmt->majortype, &MEDIATYPE_Video)) &&
250         (!memcmp(((const char *)&pmt->subtype)+4, ((const char *)&MEDIATYPE_Video)+4, sizeof(GUID)-4)))
251     {
252         VIDEOINFOHEADER *format1 = (VIDEOINFOHEADER *)pmt->pbFormat;
253         VIDEOINFOHEADER2 *format2 = (VIDEOINFOHEADER2 *)pmt->pbFormat;
254         BITMAPINFOHEADER *bmi;
255
256         if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo))
257             bmi = &format1->bmiHeader;
258         else if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo2))
259             bmi = &format2->bmiHeader;
260         else
261             goto failed;
262         TRACE("Fourcc: %s\n", debugstr_an((const char *)&pmt->subtype.Data1, 4));
263
264         This->hvid = ICLocate(pmt->majortype.Data1, pmt->subtype.Data1, bmi, NULL, ICMODE_DECOMPRESS);
265         if (This->hvid)
266         {
267             AM_MEDIA_TYPE* outpmt = &This->tf.pmt;
268             const CLSID* outsubtype;
269             DWORD bih_size;
270             DWORD output_depth = bmi->biBitCount;
271             DWORD result;
272             FreeMediaType(outpmt);
273
274             switch(bmi->biBitCount)
275             {
276                 case 32: outsubtype = &MEDIASUBTYPE_RGB32; break;
277                 case 24: outsubtype = &MEDIASUBTYPE_RGB24; break;
278                 case 16: outsubtype = &MEDIASUBTYPE_RGB565; break;
279                 case 8:  outsubtype = &MEDIASUBTYPE_RGB8; break;
280                 default:
281                     WARN("Non standard input depth %d, forced output depth to 32\n", bmi->biBitCount);
282                     outsubtype = &MEDIASUBTYPE_RGB32;
283                     output_depth = 32;
284                     break;
285             }
286
287             /* Copy bitmap header from media type to 1 for input and 1 for output */
288             bih_size = bmi->biSize + bmi->biClrUsed * 4;
289             This->pBihIn = CoTaskMemAlloc(bih_size);
290             if (!This->pBihIn)
291             {
292                 hr = E_OUTOFMEMORY;
293                 goto failed;
294             }
295             This->pBihOut = CoTaskMemAlloc(bih_size);
296             if (!This->pBihOut)
297             {
298                 hr = E_OUTOFMEMORY;
299                 goto failed;
300             }
301             memcpy(This->pBihIn, bmi, bih_size);
302             memcpy(This->pBihOut, bmi, bih_size);
303
304             /* Update output format as non compressed bitmap */
305             This->pBihOut->biCompression = 0;
306             This->pBihOut->biBitCount = output_depth;
307             This->pBihOut->biSizeImage = This->pBihOut->biWidth * This->pBihOut->biHeight * This->pBihOut->biBitCount / 8;
308             TRACE("Size: %u\n", This->pBihIn->biSize);
309             result = ICDecompressQuery(This->hvid, This->pBihIn, This->pBihOut);
310             if (result != ICERR_OK)
311             {
312                 ERR("Unable to found a suitable output format (%d)\n", result);
313                 goto failed;
314             }
315
316             /* Update output media type */
317             CopyMediaType(outpmt, pmt);
318             outpmt->subtype = *outsubtype;
319
320             if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo))
321                 memcpy(&(((VIDEOINFOHEADER *)outpmt->pbFormat)->bmiHeader), This->pBihOut, This->pBihOut->biSize);
322             else if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo2))
323                 memcpy(&(((VIDEOINFOHEADER2 *)outpmt->pbFormat)->bmiHeader), This->pBihOut, This->pBihOut->biSize);
324             else
325                 assert(0);
326
327             TRACE("Connection accepted\n");
328             return S_OK;
329         }
330         TRACE("Unable to find a suitable VFW decompressor\n");
331     }
332
333 failed:
334
335     TRACE("Connection refused\n");
336     return hr;
337 }
338
339 static HRESULT WINAPI AVIDec_CompleteConnect(TransformFilter *tf, PIN_DIRECTION dir, IPin *pin)
340 {
341     AVIDecImpl* This = impl_from_TransformFilter(tf);
342
343     TRACE("(%p)\n", This);
344
345     return S_OK;
346 }
347
348 static HRESULT WINAPI AVIDec_BreakConnect(TransformFilter *tf, PIN_DIRECTION dir)
349 {
350     AVIDecImpl *This = impl_from_TransformFilter(tf);
351
352     TRACE("(%p)->()\n", This);
353
354     if (dir == PINDIR_INPUT)
355     {
356         if (This->hvid)
357             ICClose(This->hvid);
358         if (This->pBihIn)
359             CoTaskMemFree(This->pBihIn);
360         if (This->pBihOut)
361             CoTaskMemFree(This->pBihOut);
362
363         This->hvid = NULL;
364         This->pBihIn = NULL;
365         This->pBihOut = NULL;
366     }
367
368     return S_OK;
369 }
370
371 static HRESULT WINAPI AVIDec_DecideBufferSize(TransformFilter *tf, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
372 {
373     AVIDecImpl *pAVI = impl_from_TransformFilter(tf);
374     ALLOCATOR_PROPERTIES actual;
375
376     if (!ppropInputRequest->cbAlign)
377         ppropInputRequest->cbAlign = 1;
378
379     if (ppropInputRequest->cbBuffer < pAVI->pBihOut->biSizeImage)
380             ppropInputRequest->cbBuffer = pAVI->pBihOut->biSizeImage;
381
382     if (!ppropInputRequest->cBuffers)
383         ppropInputRequest->cBuffers = 1;
384
385     return IMemAllocator_SetProperties(pAlloc, ppropInputRequest, &actual);
386 }
387
388 static const TransformFilterFuncTable AVIDec_FuncsTable = {
389     AVIDec_DecideBufferSize,
390     AVIDec_StartStreaming,
391     AVIDec_Receive,
392     AVIDec_StopStreaming,
393     NULL,
394     AVIDec_SetMediaType,
395     AVIDec_CompleteConnect,
396     AVIDec_BreakConnect,
397     NULL,
398     NULL,
399     AVIDec_EndFlush,
400     NULL,
401     AVIDec_NotifyDrop
402 };
403
404 HRESULT AVIDec_create(IUnknown * pUnkOuter, LPVOID * ppv)
405 {
406     HRESULT hr;
407     AVIDecImpl * This;
408
409     TRACE("(%p, %p)\n", pUnkOuter, ppv);
410
411     *ppv = NULL;
412
413     if (pUnkOuter)
414         return CLASS_E_NOAGGREGATION;
415
416     hr = TransformFilter_Construct(&AVIDec_Vtbl, sizeof(AVIDecImpl), &CLSID_AVIDec, &AVIDec_FuncsTable, (IBaseFilter**)&This);
417
418     if (FAILED(hr))
419         return hr;
420     else
421     {
422         ISeekingPassThru *passthru;
423         hr = CoCreateInstance(&CLSID_SeekingPassThru, (IUnknown*)This, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void**)&This->seekthru_unk);
424         IUnknown_QueryInterface(This->seekthru_unk, &IID_ISeekingPassThru, (void**)&passthru);
425         ISeekingPassThru_Init(passthru, FALSE, This->tf.ppPins[0]);
426         ISeekingPassThru_Release(passthru);
427     }
428
429     This->hvid = NULL;
430     This->pBihIn = NULL;
431     This->pBihOut = NULL;
432
433     *ppv = This;
434
435     return hr;
436 }
437
438 static HRESULT WINAPI AVIDec_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
439 {
440     HRESULT hr;
441     AVIDecImpl *This = impl_from_IBaseFilter(iface);
442     TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
443
444     if (IsEqualIID(riid, &IID_IMediaSeeking))
445         return IUnknown_QueryInterface(This->seekthru_unk, riid, ppv);
446
447     hr = TransformFilterImpl_QueryInterface(iface, riid, ppv);
448
449     return hr;
450 }
451
452 static const IBaseFilterVtbl AVIDec_Vtbl =
453 {
454     AVIDec_QueryInterface,
455     BaseFilterImpl_AddRef,
456     TransformFilterImpl_Release,
457     BaseFilterImpl_GetClassID,
458     TransformFilterImpl_Stop,
459     TransformFilterImpl_Pause,
460     TransformFilterImpl_Run,
461     BaseFilterImpl_GetState,
462     BaseFilterImpl_SetSyncSource,
463     BaseFilterImpl_GetSyncSource,
464     BaseFilterImpl_EnumPins,
465     TransformFilterImpl_FindPin,
466     BaseFilterImpl_QueryFilterInfo,
467     BaseFilterImpl_JoinFilterGraph,
468     BaseFilterImpl_QueryVendorInfo
469 };