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