wined3d: IWineD3DBuffer_Unmap() can't fail.
[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     LeaveCriticalSection(&This->tf.filter.csFilter);
142     hr = BaseOutputPinImpl_Deliver((BaseOutputPin*)This->tf.ppPins[1], pOutSample);
143     if (hr != S_OK && hr != VFW_E_NOT_CONNECTED)
144         ERR("Error sending sample (%x)\n", hr);
145     IMediaSample_Release(pOutSample);
146     return hr;
147
148 error:
149     if (pOutSample)
150         IMediaSample_Release(pOutSample);
151
152     LeaveCriticalSection(&This->tf.filter.csFilter);
153     return hr;
154 }
155
156 static HRESULT WINAPI AVIDec_StopStreaming(TransformFilter* pTransformFilter)
157 {
158     AVIDecImpl* This = (AVIDecImpl*)pTransformFilter;
159     DWORD result;
160
161     TRACE("(%p)->()\n", This);
162
163     if (!This->hvid)
164         return S_OK;
165
166     result = ICDecompressEnd(This->hvid);
167     if (result != ICERR_OK)
168     {
169         ERR("Cannot stop processing (%d)\n", result);
170         return E_FAIL;
171     }
172     return S_OK;
173 }
174
175 static HRESULT WINAPI AVIDec_SetMediaType(TransformFilter *tf, PIN_DIRECTION dir, const AM_MEDIA_TYPE * pmt)
176 {
177     AVIDecImpl* This = (AVIDecImpl*)tf;
178     HRESULT hr = VFW_E_TYPE_NOT_ACCEPTED;
179
180     TRACE("(%p)->(%p)\n", This, pmt);
181
182     if (dir != PINDIR_INPUT)
183         return S_OK;
184
185     /* Check root (GUID w/o FOURCC) */
186     if ((IsEqualIID(&pmt->majortype, &MEDIATYPE_Video)) &&
187         (!memcmp(((const char *)&pmt->subtype)+4, ((const char *)&MEDIATYPE_Video)+4, sizeof(GUID)-4)))
188     {
189         VIDEOINFOHEADER *format1 = (VIDEOINFOHEADER *)pmt->pbFormat;
190         VIDEOINFOHEADER2 *format2 = (VIDEOINFOHEADER2 *)pmt->pbFormat;
191         BITMAPINFOHEADER *bmi;
192
193         if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo))
194             bmi = &format1->bmiHeader;
195         else if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo2))
196             bmi = &format2->bmiHeader;
197         else
198             goto failed;
199         TRACE("Fourcc: %s\n", debugstr_an((const char *)&pmt->subtype.Data1, 4));
200
201         This->hvid = ICLocate(pmt->majortype.Data1, pmt->subtype.Data1, bmi, NULL, ICMODE_DECOMPRESS);
202         if (This->hvid)
203         {
204             AM_MEDIA_TYPE* outpmt = &This->tf.pmt;
205             const CLSID* outsubtype;
206             DWORD bih_size;
207             DWORD output_depth = bmi->biBitCount;
208             DWORD result;
209             FreeMediaType(outpmt);
210
211             switch(bmi->biBitCount)
212             {
213                 case 32: outsubtype = &MEDIASUBTYPE_RGB32; break;
214                 case 24: outsubtype = &MEDIASUBTYPE_RGB24; break;
215                 case 16: outsubtype = &MEDIASUBTYPE_RGB565; break;
216                 case 8:  outsubtype = &MEDIASUBTYPE_RGB8; break;
217                 default:
218                     WARN("Non standard input depth %d, forced output depth to 32\n", bmi->biBitCount);
219                     outsubtype = &MEDIASUBTYPE_RGB32;
220                     output_depth = 32;
221                     break;
222             }
223
224             /* Copy bitmap header from media type to 1 for input and 1 for output */
225             bih_size = bmi->biSize + bmi->biClrUsed * 4;
226             This->pBihIn = CoTaskMemAlloc(bih_size);
227             if (!This->pBihIn)
228             {
229                 hr = E_OUTOFMEMORY;
230                 goto failed;
231             }
232             This->pBihOut = CoTaskMemAlloc(bih_size);
233             if (!This->pBihOut)
234             {
235                 hr = E_OUTOFMEMORY;
236                 goto failed;
237             }
238             memcpy(This->pBihIn, bmi, bih_size);
239             memcpy(This->pBihOut, bmi, bih_size);
240
241             /* Update output format as non compressed bitmap */
242             This->pBihOut->biCompression = 0;
243             This->pBihOut->biBitCount = output_depth;
244             This->pBihOut->biSizeImage = This->pBihOut->biWidth * This->pBihOut->biHeight * This->pBihOut->biBitCount / 8;
245             TRACE("Size: %u\n", This->pBihIn->biSize);
246             result = ICDecompressQuery(This->hvid, This->pBihIn, This->pBihOut);
247             if (result != ICERR_OK)
248             {
249                 ERR("Unable to found a suitable output format (%d)\n", result);
250                 goto failed;
251             }
252
253             /* Update output media type */
254             CopyMediaType(outpmt, pmt);
255             outpmt->subtype = *outsubtype;
256
257             if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo))
258                 memcpy(&(((VIDEOINFOHEADER *)outpmt->pbFormat)->bmiHeader), This->pBihOut, This->pBihOut->biSize);
259             else if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo2))
260                 memcpy(&(((VIDEOINFOHEADER2 *)outpmt->pbFormat)->bmiHeader), This->pBihOut, This->pBihOut->biSize);
261             else
262                 assert(0);
263
264             TRACE("Connection accepted\n");
265             return S_OK;
266         }
267         TRACE("Unable to find a suitable VFW decompressor\n");
268     }
269
270 failed:
271
272     TRACE("Connection refused\n");
273     return hr;
274 }
275
276 static HRESULT WINAPI AVIDec_CompleteConnect(TransformFilter *tf, PIN_DIRECTION dir, IPin *pin)
277 {
278     AVIDecImpl* This = (AVIDecImpl*)tf;
279
280     TRACE("(%p)\n", This);
281
282     return S_OK;
283 }
284
285 static HRESULT WINAPI AVIDec_BreakConnect(TransformFilter *tf, PIN_DIRECTION dir)
286 {
287     AVIDecImpl *This = (AVIDecImpl *)tf;
288
289     TRACE("(%p)->()\n", This);
290
291     if (dir == PINDIR_INPUT)
292     {
293         if (This->hvid)
294             ICClose(This->hvid);
295         if (This->pBihIn)
296             CoTaskMemFree(This->pBihIn);
297         if (This->pBihOut)
298             CoTaskMemFree(This->pBihOut);
299
300         This->hvid = NULL;
301         This->pBihIn = NULL;
302         This->pBihOut = NULL;
303     }
304
305     return S_OK;
306 }
307
308 static HRESULT WINAPI AVIDec_DecideBufferSize(TransformFilter *tf, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
309 {
310     AVIDecImpl *pAVI = (AVIDecImpl*)tf;
311     ALLOCATOR_PROPERTIES actual;
312
313     if (!ppropInputRequest->cbAlign)
314         ppropInputRequest->cbAlign = 1;
315
316     if (ppropInputRequest->cbBuffer < pAVI->pBihOut->biSizeImage)
317             ppropInputRequest->cbBuffer = pAVI->pBihOut->biSizeImage;
318
319     if (!ppropInputRequest->cBuffers)
320         ppropInputRequest->cBuffers = 1;
321
322     return IMemAllocator_SetProperties(pAlloc, ppropInputRequest, &actual);
323 }
324
325 static const TransformFilterFuncTable AVIDec_FuncsTable = {
326     AVIDec_DecideBufferSize,
327     AVIDec_StartStreaming,
328     AVIDec_Receive,
329     AVIDec_StopStreaming,
330     NULL,
331     AVIDec_SetMediaType,
332     AVIDec_CompleteConnect,
333     AVIDec_BreakConnect,
334     NULL,
335     NULL,
336     NULL,
337     NULL
338 };
339
340 HRESULT AVIDec_create(IUnknown * pUnkOuter, LPVOID * ppv)
341 {
342     HRESULT hr;
343     AVIDecImpl * This;
344
345     TRACE("(%p, %p)\n", pUnkOuter, ppv);
346
347     *ppv = NULL;
348
349     if (pUnkOuter)
350         return CLASS_E_NOAGGREGATION;
351
352     hr = TransformFilter_Construct(&AVIDec_Vtbl, sizeof(AVIDecImpl), &CLSID_AVIDec, &AVIDec_FuncsTable, (IBaseFilter**)&This);
353
354     if (FAILED(hr))
355         return hr;
356     else
357     {
358         ISeekingPassThru *passthru;
359         hr = CoCreateInstance(&CLSID_SeekingPassThru, (IUnknown*)This, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void**)&This->seekthru_unk);
360         IUnknown_QueryInterface(This->seekthru_unk, &IID_ISeekingPassThru, (void**)&passthru);
361         ISeekingPassThru_Init(passthru, FALSE, (IPin*)This->tf.ppPins[0]);
362         ISeekingPassThru_Release(passthru);
363     }
364
365     This->hvid = NULL;
366     This->pBihIn = NULL;
367     This->pBihOut = NULL;
368
369     *ppv = This;
370
371     return hr;
372 }
373
374 HRESULT WINAPI AVIDec_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
375 {
376     HRESULT hr;
377     AVIDecImpl *This = (AVIDecImpl *)iface;
378     TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
379
380     if (IsEqualIID(riid, &IID_IMediaSeeking))
381         return IUnknown_QueryInterface(This->seekthru_unk, riid, ppv);
382
383     hr = TransformFilterImpl_QueryInterface(iface, riid, ppv);
384
385     return hr;
386 }
387
388 static const IBaseFilterVtbl AVIDec_Vtbl =
389 {
390     AVIDec_QueryInterface,
391     BaseFilterImpl_AddRef,
392     TransformFilterImpl_Release,
393     BaseFilterImpl_GetClassID,
394     TransformFilterImpl_Stop,
395     TransformFilterImpl_Pause,
396     TransformFilterImpl_Run,
397     BaseFilterImpl_GetState,
398     BaseFilterImpl_SetSyncSource,
399     BaseFilterImpl_GetSyncSource,
400     BaseFilterImpl_EnumPins,
401     TransformFilterImpl_FindPin,
402     BaseFilterImpl_QueryFilterInfo,
403     BaseFilterImpl_JoinFilterGraph,
404     BaseFilterImpl_QueryVendorInfo
405 };