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