- Sync with trunk up to r46941.
[reactos.git] / dll / directx / 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 #include "transform.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
44
45 typedef struct AVIDecImpl
46 {
47 TransformFilterImpl tf;
48 HIC hvid;
49 BITMAPINFOHEADER* pBihIn;
50 BITMAPINFOHEADER* pBihOut;
51 } AVIDecImpl;
52
53 static HRESULT AVIDec_ProcessBegin(TransformFilterImpl* pTransformFilter)
54 {
55 AVIDecImpl* This = (AVIDecImpl*)pTransformFilter;
56 DWORD result;
57
58 TRACE("(%p)->()\n", This);
59
60 result = ICDecompressBegin(This->hvid, This->pBihIn, This->pBihOut);
61 if (result != ICERR_OK)
62 {
63 ERR("Cannot start processing (%d)\n", result);
64 return E_FAIL;
65 }
66 return S_OK;
67 }
68
69 static HRESULT AVIDec_ProcessSampleData(InputPin *pin, IMediaSample *pSample)
70 {
71 AVIDecImpl* This = (AVIDecImpl *)pin->pin.pinInfo.pFilter;
72 AM_MEDIA_TYPE amt;
73 HRESULT hr;
74 DWORD res;
75 IMediaSample* pOutSample = NULL;
76 DWORD cbDstStream;
77 LPBYTE pbDstStream;
78 DWORD cbSrcStream;
79 LPBYTE pbSrcStream;
80 LONGLONG tStart, tStop;
81
82 EnterCriticalSection(&This->tf.csFilter);
83 if (This->tf.state == State_Stopped)
84 {
85 LeaveCriticalSection(&This->tf.csFilter);
86 return VFW_E_WRONG_STATE;
87 }
88
89 if (pin->end_of_stream || pin->flushing)
90 {
91 LeaveCriticalSection(&This->tf.csFilter);
92 return S_FALSE;
93 }
94
95 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
96 if (FAILED(hr))
97 {
98 ERR("Cannot get pointer to sample data (%x)\n", hr);
99 goto error;
100 }
101
102 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
103
104 TRACE("Sample data ptr = %p, size = %ld\n", pbSrcStream, (long)cbSrcStream);
105
106 hr = IPin_ConnectionMediaType(This->tf.ppPins[0], &amt);
107 if (FAILED(hr)) {
108 ERR("Unable to retrieve media type\n");
109 goto error;
110 }
111
112 /* Update input size to match sample size */
113 This->pBihIn->biSizeImage = cbSrcStream;
114
115 hr = OutputPin_GetDeliveryBuffer((OutputPin*)This->tf.ppPins[1], &pOutSample, NULL, NULL, 0);
116 if (FAILED(hr)) {
117 ERR("Unable to get delivery buffer (%x)\n", hr);
118 goto error;
119 }
120
121 hr = IMediaSample_SetActualDataLength(pOutSample, 0);
122 assert(hr == S_OK);
123
124 hr = IMediaSample_GetPointer(pOutSample, &pbDstStream);
125 if (FAILED(hr)) {
126 ERR("Unable to get pointer to buffer (%x)\n", hr);
127 goto error;
128 }
129 cbDstStream = IMediaSample_GetSize(pOutSample);
130 if (cbDstStream < This->pBihOut->biSizeImage) {
131 ERR("Sample size is too small %d < %d\n", cbDstStream, This->pBihOut->biSizeImage);
132 hr = E_FAIL;
133 goto error;
134 }
135
136 res = ICDecompress(This->hvid, 0, This->pBihIn, pbSrcStream, This->pBihOut, pbDstStream);
137 if (res != ICERR_OK)
138 ERR("Error occurred during the decompression (%x)\n", res);
139
140 IMediaSample_SetActualDataLength(pOutSample, This->pBihOut->biSizeImage);
141
142 IMediaSample_SetPreroll(pOutSample, (IMediaSample_IsPreroll(pSample) == S_OK));
143 IMediaSample_SetDiscontinuity(pOutSample, (IMediaSample_IsDiscontinuity(pSample) == S_OK));
144 IMediaSample_SetSyncPoint(pOutSample, (IMediaSample_IsSyncPoint(pSample) == S_OK));
145
146 if (IMediaSample_GetTime(pSample, &tStart, &tStop) == S_OK)
147 IMediaSample_SetTime(pOutSample, &tStart, &tStop);
148 else
149 IMediaSample_SetTime(pOutSample, NULL, NULL);
150
151 LeaveCriticalSection(&This->tf.csFilter);
152 hr = OutputPin_SendSample((OutputPin*)This->tf.ppPins[1], pOutSample);
153 if (hr != S_OK && hr != VFW_E_NOT_CONNECTED)
154 ERR("Error sending sample (%x)\n", hr);
155 IMediaSample_Release(pOutSample);
156 return hr;
157
158 error:
159 if (pOutSample)
160 IMediaSample_Release(pOutSample);
161
162 LeaveCriticalSection(&This->tf.csFilter);
163 return hr;
164 }
165
166 static HRESULT AVIDec_ProcessEnd(TransformFilterImpl* pTransformFilter)
167 {
168 AVIDecImpl* This = (AVIDecImpl*)pTransformFilter;
169 DWORD result;
170
171 TRACE("(%p)->()\n", This);
172
173 if (!This->hvid)
174 return S_OK;
175
176 result = ICDecompressEnd(This->hvid);
177 if (result != ICERR_OK)
178 {
179 ERR("Cannot stop processing (%d)\n", result);
180 return E_FAIL;
181 }
182 return S_OK;
183 }
184
185 static HRESULT AVIDec_ConnectInput(InputPin *pin, const AM_MEDIA_TYPE * pmt)
186 {
187 AVIDecImpl* This = (AVIDecImpl*)pin->pin.pinInfo.pFilter;
188 HRESULT hr = VFW_E_TYPE_NOT_ACCEPTED;
189
190 TRACE("(%p)->(%p)\n", This, pmt);
191
192 /* Check root (GUID w/o FOURCC) */
193 if ((IsEqualIID(&pmt->majortype, &MEDIATYPE_Video)) &&
194 (!memcmp(((const char *)&pmt->subtype)+4, ((const char *)&MEDIATYPE_Video)+4, sizeof(GUID)-4)))
195 {
196 VIDEOINFOHEADER *format1 = (VIDEOINFOHEADER *)pmt->pbFormat;
197 VIDEOINFOHEADER2 *format2 = (VIDEOINFOHEADER2 *)pmt->pbFormat;
198 BITMAPINFOHEADER *bmi;
199
200 if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo))
201 bmi = &format1->bmiHeader;
202 else if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo2))
203 bmi = &format2->bmiHeader;
204 else
205 goto failed;
206 TRACE("Fourcc: %s\n", debugstr_an((const char *)&pmt->subtype.Data1, 4));
207
208 This->hvid = ICLocate(pmt->majortype.Data1, pmt->subtype.Data1, bmi, NULL, ICMODE_DECOMPRESS);
209 if (This->hvid)
210 {
211 AM_MEDIA_TYPE* outpmt = &This->tf.pmt;
212 const CLSID* outsubtype;
213 DWORD bih_size;
214 DWORD output_depth = bmi->biBitCount;
215 DWORD result;
216 FreeMediaType(outpmt);
217
218 switch(bmi->biBitCount)
219 {
220 case 32: outsubtype = &MEDIASUBTYPE_RGB32; break;
221 case 24: outsubtype = &MEDIASUBTYPE_RGB24; break;
222 case 16: outsubtype = &MEDIASUBTYPE_RGB565; break;
223 case 8: outsubtype = &MEDIASUBTYPE_RGB8; break;
224 default:
225 WARN("Non standard input depth %d, forced output depth to 32\n", bmi->biBitCount);
226 outsubtype = &MEDIASUBTYPE_RGB32;
227 output_depth = 32;
228 break;
229 }
230
231 /* Copy bitmap header from media type to 1 for input and 1 for output */
232 bih_size = bmi->biSize + bmi->biClrUsed * 4;
233 This->pBihIn = CoTaskMemAlloc(bih_size);
234 if (!This->pBihIn)
235 {
236 hr = E_OUTOFMEMORY;
237 goto failed;
238 }
239 This->pBihOut = CoTaskMemAlloc(bih_size);
240 if (!This->pBihOut)
241 {
242 hr = E_OUTOFMEMORY;
243 goto failed;
244 }
245 memcpy(This->pBihIn, bmi, bih_size);
246 memcpy(This->pBihOut, bmi, bih_size);
247
248 /* Update output format as non compressed bitmap */
249 This->pBihOut->biCompression = 0;
250 This->pBihOut->biBitCount = output_depth;
251 This->pBihOut->biSizeImage = This->pBihOut->biWidth * This->pBihOut->biHeight * This->pBihOut->biBitCount / 8;
252 TRACE("Size: %u\n", This->pBihIn->biSize);
253 result = ICDecompressQuery(This->hvid, This->pBihIn, This->pBihOut);
254 if (result != ICERR_OK)
255 {
256 ERR("Unable to found a suitable output format (%d)\n", result);
257 goto failed;
258 }
259
260 /* Update output media type */
261 CopyMediaType(outpmt, pmt);
262 outpmt->subtype = *outsubtype;
263
264 if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo))
265 memcpy(&(((VIDEOINFOHEADER *)outpmt->pbFormat)->bmiHeader), This->pBihOut, This->pBihOut->biSize);
266 else if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo2))
267 memcpy(&(((VIDEOINFOHEADER2 *)outpmt->pbFormat)->bmiHeader), This->pBihOut, This->pBihOut->biSize);
268 else
269 assert(0);
270
271 /* Update buffer size of media samples in output */
272 ((OutputPin*)This->tf.ppPins[1])->allocProps.cbBuffer = This->pBihOut->biSizeImage;
273
274 TRACE("Connection accepted\n");
275 return S_OK;
276 }
277 TRACE("Unable to find a suitable VFW decompressor\n");
278 }
279
280 failed:
281
282 TRACE("Connection refused\n");
283 return hr;
284 }
285
286 static HRESULT AVIDec_Cleanup(InputPin *pin)
287 {
288 AVIDecImpl *This = (AVIDecImpl *)pin->pin.pinInfo.pFilter;
289
290 TRACE("(%p)->()\n", This);
291
292 if (This->hvid)
293 ICClose(This->hvid);
294 if (This->pBihIn)
295 CoTaskMemFree(This->pBihIn);
296 if (This->pBihOut)
297 CoTaskMemFree(This->pBihOut);
298
299 This->hvid = NULL;
300 This->pBihIn = NULL;
301 This->pBihOut = NULL;
302
303 return S_OK;
304 }
305
306 static const TransformFuncsTable AVIDec_FuncsTable = {
307 AVIDec_ProcessBegin,
308 AVIDec_ProcessSampleData,
309 AVIDec_ProcessEnd,
310 NULL,
311 AVIDec_ConnectInput,
312 AVIDec_Cleanup
313 };
314
315 HRESULT AVIDec_create(IUnknown * pUnkOuter, LPVOID * ppv)
316 {
317 HRESULT hr;
318 AVIDecImpl * This;
319
320 TRACE("(%p, %p)\n", pUnkOuter, ppv);
321
322 *ppv = NULL;
323
324 if (pUnkOuter)
325 return CLASS_E_NOAGGREGATION;
326
327 /* Note: This memory is managed by the transform filter once created */
328 This = CoTaskMemAlloc(sizeof(AVIDecImpl));
329
330 This->hvid = NULL;
331 This->pBihIn = NULL;
332 This->pBihOut = NULL;
333
334 hr = TransformFilter_Create(&(This->tf), &CLSID_AVIDec, &AVIDec_FuncsTable, NULL, NULL, NULL);
335
336 if (FAILED(hr))
337 return hr;
338
339 *ppv = This;
340
341 return hr;
342 }