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