ver: Use the 16-bit resource function in GetFileVersionInfo16().
[wine.git] / dlls / quartz / avidec.c
blobabed957fa6d7f4de81e14c6476e36dcfb0c8de47
1 /*
2 * AVI Decompressor (VFW decompressors wrapper)
4 * Copyright 2004-2005 Christian Costa
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.
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.
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
21 #include "quartz_private.h"
23 #include "uuids.h"
24 #include "amvideo.h"
25 #include "windef.h"
26 #include "winbase.h"
27 #include "dshow.h"
28 #include "strmif.h"
29 #include "vfwmsgs.h"
30 #include "vfw.h"
31 #include "dvdmedia.h"
33 #include <assert.h>
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
39 typedef struct AVIDecImpl
41 TransformFilter tf;
43 HIC hvid;
44 BITMAPINFOHEADER* pBihIn;
45 BITMAPINFOHEADER* pBihOut;
46 REFERENCE_TIME late;
47 } AVIDecImpl;
49 static inline AVIDecImpl *impl_from_TransformFilter( TransformFilter *iface )
51 return CONTAINING_RECORD(iface, AVIDecImpl, tf);
54 static HRESULT WINAPI AVIDec_StartStreaming(TransformFilter* pTransformFilter)
56 AVIDecImpl* This = impl_from_TransformFilter(pTransformFilter);
57 DWORD result;
59 TRACE("(%p)->()\n", This);
60 This->late = -1;
62 result = ICDecompressBegin(This->hvid, This->pBihIn, This->pBihOut);
63 if (result != ICERR_OK)
65 ERR("Cannot start processing (%d)\n", result);
66 return E_FAIL;
68 return S_OK;
71 static HRESULT WINAPI AVIDec_EndFlush(TransformFilter *pTransformFilter) {
72 AVIDecImpl* This = impl_from_TransformFilter(pTransformFilter);
73 This->late = -1;
74 return S_OK;
77 static HRESULT WINAPI AVIDec_NotifyDrop(TransformFilter *pTransformFilter, IBaseFilter *sender, Quality qm) {
78 AVIDecImpl *This = impl_from_TransformFilter(pTransformFilter);
80 EnterCriticalSection(&This->tf.filter.csFilter);
81 if (qm.Late > 0)
82 This->late = qm.Late + qm.TimeStamp;
83 else
84 This->late = -1;
85 LeaveCriticalSection(&This->tf.filter.csFilter);
86 return S_OK;
89 static int AVIDec_DropSample(AVIDecImpl *This, REFERENCE_TIME tStart) {
90 if (This->late < 0)
91 return 0;
93 if (tStart < This->late) {
94 TRACE("Dropping sample\n");
95 return 1;
97 This->late = -1;
98 return 0;
101 static HRESULT WINAPI AVIDec_Receive(TransformFilter *tf, IMediaSample *pSample)
103 AVIDecImpl* This = impl_from_TransformFilter(tf);
104 HRESULT hr;
105 DWORD res;
106 IMediaSample* pOutSample = NULL;
107 DWORD cbDstStream;
108 LPBYTE pbDstStream;
109 DWORD cbSrcStream;
110 LPBYTE pbSrcStream;
111 LONGLONG tStart, tStop;
112 DWORD flags = 0;
114 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
115 if (FAILED(hr))
117 ERR("Cannot get pointer to sample data (%x)\n", hr);
118 return hr;
121 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
123 TRACE("Sample data ptr = %p, size = %d\n", pbSrcStream, cbSrcStream);
125 /* Update input size to match sample size */
126 This->pBihIn->biSizeImage = cbSrcStream;
128 hr = BaseOutputPinImpl_GetDeliveryBuffer(&This->tf.source, &pOutSample, NULL, NULL, 0);
129 if (FAILED(hr)) {
130 ERR("Unable to get delivery buffer (%x)\n", hr);
131 return hr;
134 hr = IMediaSample_SetActualDataLength(pOutSample, 0);
135 assert(hr == S_OK);
137 hr = IMediaSample_GetPointer(pOutSample, &pbDstStream);
138 if (FAILED(hr)) {
139 ERR("Unable to get pointer to buffer (%x)\n", hr);
140 IMediaSample_Release(pOutSample);
141 return hr;
143 cbDstStream = IMediaSample_GetSize(pOutSample);
144 if (cbDstStream < This->pBihOut->biSizeImage) {
145 ERR("Sample size is too small %d < %d\n", cbDstStream, This->pBihOut->biSizeImage);
146 IMediaSample_Release(pOutSample);
147 return E_FAIL;
150 if (IMediaSample_IsPreroll(pSample) == S_OK)
151 flags |= ICDECOMPRESS_PREROLL;
152 if (IMediaSample_IsSyncPoint(pSample) != S_OK)
153 flags |= ICDECOMPRESS_NOTKEYFRAME;
154 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
155 if (hr == S_OK && AVIDec_DropSample(This, tStart))
156 flags |= ICDECOMPRESS_HURRYUP;
158 res = ICDecompress(This->hvid, flags, This->pBihIn, pbSrcStream, This->pBihOut, pbDstStream);
159 if (res != ICERR_OK)
160 ERR("Error occurred during the decompression (%x)\n", res);
162 /* Drop sample if it's intended to be dropped */
163 if (flags & ICDECOMPRESS_HURRYUP) {
164 IMediaSample_Release(pOutSample);
165 return S_OK;
168 IMediaSample_SetActualDataLength(pOutSample, This->pBihOut->biSizeImage);
170 IMediaSample_SetPreroll(pOutSample, (IMediaSample_IsPreroll(pSample) == S_OK));
171 IMediaSample_SetDiscontinuity(pOutSample, (IMediaSample_IsDiscontinuity(pSample) == S_OK));
172 IMediaSample_SetSyncPoint(pOutSample, (IMediaSample_IsSyncPoint(pSample) == S_OK));
174 if (hr == S_OK)
175 IMediaSample_SetTime(pOutSample, &tStart, &tStop);
176 else if (hr == VFW_S_NO_STOP_TIME)
177 IMediaSample_SetTime(pOutSample, &tStart, NULL);
178 else
179 IMediaSample_SetTime(pOutSample, NULL, NULL);
181 if (IMediaSample_GetMediaTime(pSample, &tStart, &tStop) == S_OK)
182 IMediaSample_SetMediaTime(pOutSample, &tStart, &tStop);
183 else
184 IMediaSample_SetMediaTime(pOutSample, NULL, NULL);
186 hr = IMemInputPin_Receive(This->tf.source.pMemInputPin, pOutSample);
187 if (hr != S_OK && hr != VFW_E_NOT_CONNECTED)
188 ERR("Error sending sample (%x)\n", hr);
190 IMediaSample_Release(pOutSample);
191 return hr;
194 static HRESULT WINAPI AVIDec_StopStreaming(TransformFilter* pTransformFilter)
196 AVIDecImpl* This = impl_from_TransformFilter(pTransformFilter);
197 DWORD result;
199 TRACE("(%p)->()\n", This);
201 if (!This->hvid)
202 return S_OK;
204 result = ICDecompressEnd(This->hvid);
205 if (result != ICERR_OK)
207 ERR("Cannot stop processing (%d)\n", result);
208 return E_FAIL;
210 return S_OK;
213 static HRESULT avi_dec_connect_sink(TransformFilter *tf, const AM_MEDIA_TYPE *pmt)
215 AVIDecImpl* This = impl_from_TransformFilter(tf);
216 HRESULT hr = VFW_E_TYPE_NOT_ACCEPTED;
218 /* Check root (GUID w/o FOURCC) */
219 if ((IsEqualIID(&pmt->majortype, &MEDIATYPE_Video)) &&
220 (!memcmp(((const char *)&pmt->subtype)+4, ((const char *)&MEDIATYPE_Video)+4, sizeof(GUID)-4)))
222 VIDEOINFOHEADER *format1 = (VIDEOINFOHEADER *)pmt->pbFormat;
223 VIDEOINFOHEADER2 *format2 = (VIDEOINFOHEADER2 *)pmt->pbFormat;
224 BITMAPINFOHEADER *bmi;
226 if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo))
227 bmi = &format1->bmiHeader;
228 else if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo2))
229 bmi = &format2->bmiHeader;
230 else
231 goto failed;
233 This->hvid = ICLocate(pmt->majortype.Data1, pmt->subtype.Data1, bmi, NULL, ICMODE_DECOMPRESS);
234 if (This->hvid)
236 AM_MEDIA_TYPE* outpmt = &This->tf.pmt;
237 const CLSID* outsubtype;
238 DWORD bih_size;
239 DWORD output_depth = bmi->biBitCount;
240 DWORD result;
241 FreeMediaType(outpmt);
243 switch(bmi->biBitCount)
245 case 32: outsubtype = &MEDIASUBTYPE_RGB32; break;
246 case 24: outsubtype = &MEDIASUBTYPE_RGB24; break;
247 case 16: outsubtype = &MEDIASUBTYPE_RGB565; break;
248 case 8: outsubtype = &MEDIASUBTYPE_RGB8; break;
249 default:
250 WARN("Non standard input depth %d, forced output depth to 32\n", bmi->biBitCount);
251 outsubtype = &MEDIASUBTYPE_RGB32;
252 output_depth = 32;
253 break;
256 /* Copy bitmap header from media type to 1 for input and 1 for output */
257 bih_size = bmi->biSize + bmi->biClrUsed * 4;
258 This->pBihIn = CoTaskMemAlloc(bih_size);
259 if (!This->pBihIn)
261 hr = E_OUTOFMEMORY;
262 goto failed;
264 This->pBihOut = CoTaskMemAlloc(bih_size);
265 if (!This->pBihOut)
267 hr = E_OUTOFMEMORY;
268 goto failed;
270 memcpy(This->pBihIn, bmi, bih_size);
271 memcpy(This->pBihOut, bmi, bih_size);
273 /* Update output format as non compressed bitmap */
274 This->pBihOut->biCompression = 0;
275 This->pBihOut->biBitCount = output_depth;
276 This->pBihOut->biSizeImage = This->pBihOut->biWidth * This->pBihOut->biHeight * This->pBihOut->biBitCount / 8;
277 TRACE("Size: %u\n", This->pBihIn->biSize);
278 result = ICDecompressQuery(This->hvid, This->pBihIn, This->pBihOut);
279 if (result != ICERR_OK)
281 ERR("Unable to found a suitable output format (%d)\n", result);
282 goto failed;
285 /* Update output media type */
286 CopyMediaType(outpmt, pmt);
287 outpmt->subtype = *outsubtype;
289 if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo))
290 memcpy(&(((VIDEOINFOHEADER *)outpmt->pbFormat)->bmiHeader), This->pBihOut, This->pBihOut->biSize);
291 else if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo2))
292 memcpy(&(((VIDEOINFOHEADER2 *)outpmt->pbFormat)->bmiHeader), This->pBihOut, This->pBihOut->biSize);
293 else
294 assert(0);
296 TRACE("Connection accepted\n");
297 return S_OK;
299 TRACE("Unable to find a suitable VFW decompressor\n");
302 failed:
304 TRACE("Connection refused\n");
305 return hr;
308 static HRESULT WINAPI AVIDec_BreakConnect(TransformFilter *tf, PIN_DIRECTION dir)
310 AVIDecImpl *This = impl_from_TransformFilter(tf);
312 TRACE("(%p)->()\n", This);
314 if (dir == PINDIR_INPUT)
316 if (This->hvid)
317 ICClose(This->hvid);
318 CoTaskMemFree(This->pBihIn);
319 CoTaskMemFree(This->pBihOut);
320 This->hvid = NULL;
321 This->pBihIn = NULL;
322 This->pBihOut = NULL;
325 return S_OK;
328 static HRESULT WINAPI AVIDec_DecideBufferSize(TransformFilter *tf, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
330 AVIDecImpl *pAVI = impl_from_TransformFilter(tf);
331 ALLOCATOR_PROPERTIES actual;
333 if (!ppropInputRequest->cbAlign)
334 ppropInputRequest->cbAlign = 1;
336 if (ppropInputRequest->cbBuffer < pAVI->pBihOut->biSizeImage)
337 ppropInputRequest->cbBuffer = pAVI->pBihOut->biSizeImage;
339 if (!ppropInputRequest->cBuffers)
340 ppropInputRequest->cBuffers = 1;
342 return IMemAllocator_SetProperties(pAlloc, ppropInputRequest, &actual);
345 static const TransformFilterFuncTable AVIDec_FuncsTable = {
346 .pfnDecideBufferSize = AVIDec_DecideBufferSize,
347 .pfnStartStreaming = AVIDec_StartStreaming,
348 .pfnReceive = AVIDec_Receive,
349 .pfnStopStreaming = AVIDec_StopStreaming,
350 .transform_connect_sink = avi_dec_connect_sink,
351 .pfnBreakConnect = AVIDec_BreakConnect,
352 .pfnEndFlush = AVIDec_EndFlush,
353 .pfnNotify = AVIDec_NotifyDrop,
356 HRESULT AVIDec_create(IUnknown *outer, void **out)
358 HRESULT hr;
359 AVIDecImpl * This;
361 *out = NULL;
363 hr = strmbase_transform_create(sizeof(AVIDecImpl), outer, &CLSID_AVIDec,
364 &AVIDec_FuncsTable, (IBaseFilter **)&This);
366 if (FAILED(hr))
367 return hr;
369 This->hvid = NULL;
370 This->pBihIn = NULL;
371 This->pBihOut = NULL;
373 *out = &This->tf.filter.IUnknown_inner;
375 return hr;