ws2_32/tests: Wait for thread termination before cleaning up winsock.
[wine.git] / dlls / wineqtdecoder / qtvdecoder.c
blob6e2ceb169a227fb1dae04592a6e87ee652439c97
1 /*
2 * QuickTime Toolkit decoder filter for video
4 * Copyright 2010 Aric Stewart, CodeWeavers
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 "config.h"
23 #define ULONG CoreFoundation_ULONG
24 #define HRESULT CoreFoundation_HRESULT
26 #define LoadResource __carbon_LoadResource
27 #define CompareString __carbon_CompareString
28 #define GetCurrentThread __carbon_GetCurrentThread
29 #define GetCurrentProcess __carbon_GetCurrentProcess
30 #define AnimatePalette __carbon_AnimatePalette
31 #define EqualRgn __carbon_EqualRgn
32 #define FillRgn __carbon_FillRgn
33 #define FrameRgn __carbon_FrameRgn
34 #define GetPixel __carbon_GetPixel
35 #define InvertRgn __carbon_InvertRgn
36 #define LineTo __carbon_LineTo
37 #define OffsetRgn __carbon_OffsetRgn
38 #define PaintRgn __carbon_PaintRgn
39 #define Polygon __carbon_Polygon
40 #define ResizePalette __carbon_ResizePalette
41 #define SetRectRgn __carbon_SetRectRgn
43 #define CheckMenuItem __carbon_CheckMenuItem
44 #define DeleteMenu __carbon_DeleteMenu
45 #define DrawMenuBar __carbon_DrawMenuBar
46 #define EnableMenuItem __carbon_EnableMenuItem
47 #define EqualRect __carbon_EqualRect
48 #define FillRect __carbon_FillRect
49 #define FrameRect __carbon_FrameRect
50 #define GetCursor __carbon_GetCursor
51 #define GetMenu __carbon_GetMenu
52 #define InvertRect __carbon_InvertRect
53 #define IsWindowVisible __carbon_IsWindowVisible
54 #define MoveWindow __carbon_MoveWindow
55 #define OffsetRect __carbon_OffsetRect
56 #define PtInRect __carbon_PtInRect
57 #define SetCursor __carbon_SetCursor
58 #define SetRect __carbon_SetRect
59 #define ShowCursor __carbon_ShowCursor
60 #define ShowWindow __carbon_ShowWindow
61 #define UnionRect __carbon_UnionRect
63 #include <QuickTime/ImageCompression.h>
64 #include <CoreVideo/CVPixelBuffer.h>
66 #undef LoadResource
67 #undef CompareString
68 #undef GetCurrentThread
69 #undef _CDECL
70 #undef DPRINTF
71 #undef GetCurrentProcess
72 #undef AnimatePalette
73 #undef EqualRgn
74 #undef FillRgn
75 #undef FrameRgn
76 #undef GetPixel
77 #undef InvertRgn
78 #undef LineTo
79 #undef OffsetRgn
80 #undef PaintRgn
81 #undef Polygon
82 #undef ResizePalette
83 #undef SetRectRgn
84 #undef CheckMenuItem
85 #undef DeleteMenu
86 #undef DrawMenuBar
87 #undef EnableMenuItem
88 #undef EqualRect
89 #undef FillRect
90 #undef FrameRect
91 #undef GetCursor
92 #undef GetMenu
93 #undef InvertRect
94 #undef IsWindowVisible
95 #undef MoveWindow
96 #undef OffsetRect
97 #undef PtInRect
98 #undef SetCursor
99 #undef SetRect
100 #undef ShowCursor
101 #undef ShowWindow
102 #undef UnionRect
104 #undef ULONG
105 #undef HRESULT
106 #undef DPRINTF
107 #undef STDMETHODCALLTYPE
109 #define COBJMACROS
111 #include "windef.h"
112 #include "winbase.h"
113 #include "wtypes.h"
114 #include "winuser.h"
115 #include "dshow.h"
117 #include "uuids.h"
118 #include "amvideo.h"
119 #include "strmif.h"
120 #include "vfwmsgs.h"
121 #include "vfw.h"
122 #include "dvdmedia.h"
124 #include <assert.h>
126 #include "wine/unicode.h"
127 #include "wine/debug.h"
128 #include "wine/strmbase.h"
130 #include "qtprivate.h"
132 extern CLSID CLSID_QTVDecoder;
134 WINE_DEFAULT_DEBUG_CHANNEL(qtdecoder);
136 typedef struct QTVDecoderImpl
138 TransformFilter tf;
140 ImageDescriptionHandle hImageDescription;
141 CFMutableDictionaryRef outputBufferAttributes;
142 ICMDecompressionSessionRef decompressionSession;
143 HRESULT decodeHR;
145 DWORD outputSize;
147 } QTVDecoderImpl;
149 static inline QTVDecoderImpl *impl_from_IBaseFilter( IBaseFilter *iface )
151 return CONTAINING_RECORD(iface, QTVDecoderImpl, tf.filter.IBaseFilter_iface);
154 static inline QTVDecoderImpl *impl_from_TransformFilter( TransformFilter *iface )
156 return CONTAINING_RECORD(iface, QTVDecoderImpl, tf.filter);
159 static const IBaseFilterVtbl QTVDecoder_Vtbl;
161 static void trackingCallback(
162 void *decompressionTrackingRefCon,
163 OSStatus result,
164 ICMDecompressionTrackingFlags decompressionTrackingFlags,
165 CVPixelBufferRef pixelBuffer,
166 TimeValue64 displayTime,
167 TimeValue64 displayDuration,
168 ICMValidTimeFlags validTimeFlags,
169 void *reserved,
170 void *sourceFrameRefCon )
172 QTVDecoderImpl *This = (QTVDecoderImpl*)decompressionTrackingRefCon;
173 IMediaSample *pSample = (IMediaSample*)sourceFrameRefCon;
174 HRESULT hr = S_OK;
175 IMediaSample* pOutSample = NULL;
176 LPBYTE pbDstStream;
177 DWORD cbDstStream;
179 if (result != noErr)
181 ERR("Error from Codec, no frame decompressed\n");
182 return;
185 if (!pixelBuffer)
187 ERR("No pixel buffer, no frame decompressed\n");
188 return;
191 EnterCriticalSection(&This->tf.csReceive);
192 hr = BaseOutputPinImpl_GetDeliveryBuffer((BaseOutputPin*)This->tf.ppPins[1], &pOutSample, NULL, NULL, 0);
193 if (FAILED(hr)) {
194 ERR("Unable to get delivery buffer (%x)\n", hr);
195 goto error;
198 hr = IMediaSample_SetActualDataLength(pOutSample, 0);
199 assert(hr == S_OK);
201 hr = IMediaSample_GetPointer(pOutSample, &pbDstStream);
202 if (FAILED(hr)) {
203 ERR("Unable to get pointer to buffer (%x)\n", hr);
204 goto error;
207 cbDstStream = IMediaSample_GetSize(pOutSample);
208 if (cbDstStream < This->outputSize) {
209 ERR("Sample size is too small %d < %d\n", cbDstStream, This->outputSize);
210 hr = E_FAIL;
211 goto error;
214 hr = AccessPixelBufferPixels(pixelBuffer, pbDstStream);
215 if (FAILED(hr))
216 goto error;
218 IMediaSample_SetActualDataLength(pOutSample, This->outputSize);
220 IMediaSample_SetPreroll(pOutSample, (IMediaSample_IsPreroll(pSample) == S_OK));
221 IMediaSample_SetDiscontinuity(pOutSample, (IMediaSample_IsDiscontinuity(pSample) == S_OK));
222 IMediaSample_SetSyncPoint(pOutSample, (IMediaSample_IsSyncPoint(pSample) == S_OK));
224 if (!validTimeFlags)
225 IMediaSample_SetTime(pOutSample, NULL, NULL);
226 else
228 LONGLONG tStart, tStop;
230 if (validTimeFlags & kICMValidTime_DisplayTimeStampIsValid)
231 tStart = displayTime;
232 else
233 tStart = 0;
234 if (validTimeFlags & kICMValidTime_DisplayDurationIsValid)
235 tStop = tStart + displayDuration;
236 else
237 tStop = tStart;
239 IMediaSample_SetTime(pOutSample, &tStart, &tStop);
242 LeaveCriticalSection(&This->tf.csReceive);
243 hr = BaseOutputPinImpl_Deliver((BaseOutputPin*)This->tf.ppPins[1], pOutSample);
244 EnterCriticalSection(&This->tf.csReceive);
245 if (hr != S_OK && hr != VFW_E_NOT_CONNECTED)
246 ERR("Error sending sample (%x)\n", hr);
248 error:
249 LeaveCriticalSection(&This->tf.csReceive);
250 if (pOutSample)
251 IMediaSample_Release(pOutSample);
253 This->decodeHR = hr;
256 static HRESULT WINAPI QTVDecoder_StartStreaming(TransformFilter* pTransformFilter)
258 QTVDecoderImpl* This = impl_from_TransformFilter(pTransformFilter);
259 OSErr err = noErr;
260 ICMDecompressionSessionOptionsRef sessionOptions = NULL;
261 ICMDecompressionTrackingCallbackRecord trackingCallbackRecord;
263 TRACE("(%p)->()\n", This);
265 trackingCallbackRecord.decompressionTrackingCallback = trackingCallback;
266 trackingCallbackRecord.decompressionTrackingRefCon = (void*)This;
268 err = ICMDecompressionSessionCreate(NULL, This->hImageDescription, sessionOptions, This->outputBufferAttributes, &trackingCallbackRecord, &This->decompressionSession);
270 if (err != noErr)
272 ERR("Error with ICMDecompressionSessionCreate %i\n",err);
273 return E_FAIL;
276 return S_OK;
279 static HRESULT WINAPI QTVDecoder_Receive(TransformFilter *tf, IMediaSample *pSample)
281 QTVDecoderImpl* This = impl_from_TransformFilter(tf);
282 HRESULT hr;
283 DWORD cbSrcStream;
284 LPBYTE pbSrcStream;
286 ICMFrameTimeRecord frameTime = {{0}};
287 TimeValue time = 1;
288 TimeScale timeScale = 1;
289 OSStatus err = noErr;
290 LONGLONG tStart, tStop;
292 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
293 if (FAILED(hr))
295 ERR("Cannot get pointer to sample data (%x)\n", hr);
296 goto error;
299 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
301 if (IMediaSample_GetTime(pSample, &tStart, &tStop) != S_OK)
302 tStart = tStop = 0;
304 time = tStart;
306 frameTime.recordSize = sizeof(ICMFrameTimeRecord);
307 *(TimeValue64 *)&frameTime.value = tStart;
308 frameTime.scale = 1;
309 frameTime.rate = fixed1;
310 frameTime.duration = tStop - tStart;
311 frameTime.frameNumber = 0;
312 frameTime.flags = icmFrameTimeIsNonScheduledDisplayTime;
314 err = ICMDecompressionSessionDecodeFrame(This->decompressionSession,
315 (UInt8 *)pbSrcStream, cbSrcStream, NULL, &frameTime, pSample);
317 if (err != noErr)
319 ERR("Error with ICMDecompressionSessionDecodeFrame\n");
320 hr = E_FAIL;
321 goto error;
324 ICMDecompressionSessionSetNonScheduledDisplayTime(This->decompressionSession, time, timeScale, 0);
325 ICMDecompressionSessionFlush(This->decompressionSession);
326 hr = This->decodeHR;
328 error:
329 return hr;
332 static HRESULT WINAPI QTVDecoder_StopStreaming(TransformFilter* pTransformFilter)
334 QTVDecoderImpl* This = impl_from_TransformFilter(pTransformFilter);
336 TRACE("(%p)->()\n", This);
338 if (This->decompressionSession)
339 ICMDecompressionSessionRelease(This->decompressionSession);
340 This->decompressionSession = NULL;
342 return S_OK;
345 static HRESULT WINAPI QTVDecoder_SetMediaType(TransformFilter *tf, PIN_DIRECTION dir, const AM_MEDIA_TYPE * pmt)
347 QTVDecoderImpl* This = impl_from_TransformFilter(tf);
348 HRESULT hr = VFW_E_TYPE_NOT_ACCEPTED;
349 OSErr err = noErr;
350 AM_MEDIA_TYPE *outpmt = &This->tf.pmt;
351 CFNumberRef n = NULL;
353 TRACE("(%p)->(%p)\n", This, pmt);
355 if (dir != PINDIR_INPUT)
356 return S_OK;
358 FreeMediaType(outpmt);
359 CopyMediaType(outpmt, pmt);
361 if (This->hImageDescription)
362 DisposeHandle((Handle)This->hImageDescription);
364 This->hImageDescription = (ImageDescriptionHandle)
365 NewHandleClear(sizeof(ImageDescription));
367 if (This->hImageDescription != NULL)
369 (**This->hImageDescription).idSize = sizeof(ImageDescription);
370 (**This->hImageDescription).spatialQuality = codecNormalQuality;
371 (**This->hImageDescription).frameCount = 1;
372 (**This->hImageDescription).clutID = -1;
374 else
376 ERR("Failed to create ImageDescription\n");
377 goto failed;
380 /* Check root (GUID w/o FOURCC) */
381 if ((IsEqualIID(&pmt->majortype, &MEDIATYPE_Video)) &&
382 (!memcmp(((const char *)&pmt->subtype)+4, ((const char *)&MEDIATYPE_Video)+4, sizeof(GUID)-4)))
384 VIDEOINFOHEADER *format1 = (VIDEOINFOHEADER *)outpmt->pbFormat;
385 VIDEOINFOHEADER2 *format2 = (VIDEOINFOHEADER2 *)outpmt->pbFormat;
386 BITMAPINFOHEADER *bmi;
387 OSType fourCC;
388 DecompressorComponent dc;
389 OSType format;
390 DWORD outputWidth, outputHeight, outputDepth;
392 if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo))
393 bmi = &format1->bmiHeader;
394 else if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo2))
395 bmi = &format2->bmiHeader;
396 else
397 goto failed;
399 TRACE("Fourcc: %s\n", debugstr_an((const char *)&pmt->subtype.Data1, 4));
400 fourCC = ((const char *)&pmt->subtype.Data1)[3] |
401 (((const char *)&pmt->subtype.Data1)[2]<<8) |
402 (((const char *)&pmt->subtype.Data1)[1]<<16) |
403 (((const char *)&pmt->subtype.Data1)[0]<<24);
405 err = FindCodec(fourCC,NULL,NULL,&dc);
406 if (err != noErr || dc == 0x0)
408 TRACE("Codec not found\n");
409 goto failed;
412 outputWidth = bmi->biWidth;
413 outputHeight = bmi->biHeight;
415 (**This->hImageDescription).cType = fourCC;
416 (**This->hImageDescription).width = outputWidth;
417 (**This->hImageDescription).height = outputHeight;
418 (**This->hImageDescription).depth = bmi->biBitCount;
419 (**This->hImageDescription).hRes = 72<<16;
420 (**This->hImageDescription).vRes = 72<<16;
422 if (This->outputBufferAttributes)
423 CFRelease(This->outputBufferAttributes);
425 This->outputBufferAttributes = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
426 if (!This->outputBufferAttributes)
428 ERR("Failed to create outputBufferAttributes\n");
429 goto failed;
432 n = CFNumberCreate(NULL, kCFNumberIntType, &outputWidth);
433 CFDictionaryAddValue(This->outputBufferAttributes, kCVPixelBufferWidthKey, n);
434 CFRelease(n);
436 n = CFNumberCreate(NULL, kCFNumberIntType, &outputHeight);
437 CFDictionaryAddValue(This->outputBufferAttributes, kCVPixelBufferHeightKey, n);
438 CFRelease(n);
440 /* yes this looks wrong. but 32ARGB is 24 RGB with an alpha channel */
441 format = k32ARGBPixelFormat;
442 n = CFNumberCreate(NULL, kCFNumberIntType, &format);
443 CFDictionaryAddValue(This->outputBufferAttributes, kCVPixelBufferPixelFormatTypeKey, n);
444 CFRelease(n);
446 CFDictionaryAddValue(This->outputBufferAttributes, kCVPixelBufferCGBitmapContextCompatibilityKey, kCFBooleanTrue);
447 CFDictionaryAddValue(This->outputBufferAttributes, kCVPixelBufferCGImageCompatibilityKey, kCFBooleanTrue);
449 outputDepth = 3;
450 This->outputSize = outputWidth * outputHeight * outputDepth;
451 bmi->biCompression = BI_RGB;
452 bmi->biBitCount = 24;
453 outpmt->subtype = MEDIASUBTYPE_RGB24;
455 return S_OK;
458 failed:
459 if (This->hImageDescription)
461 DisposeHandle((Handle)This->hImageDescription);
462 This->hImageDescription = NULL;
464 if (This->outputBufferAttributes)
466 CFRelease(This->outputBufferAttributes);
467 This->outputBufferAttributes = NULL;
470 TRACE("Connection refused\n");
471 return hr;
474 static HRESULT WINAPI QTVDecoder_BreakConnect(TransformFilter *tf, PIN_DIRECTION dir)
476 QTVDecoderImpl *This = impl_from_TransformFilter(tf);
478 TRACE("(%p)->()\n", This);
480 if (This->hImageDescription)
481 DisposeHandle((Handle)This->hImageDescription);
482 if (This->outputBufferAttributes)
483 CFRelease(This->outputBufferAttributes);
485 This->hImageDescription = NULL;
486 This->outputBufferAttributes = NULL;
488 return S_OK;
491 static HRESULT WINAPI QTVDecoder_DecideBufferSize(TransformFilter *tf, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
493 QTVDecoderImpl *This = impl_from_TransformFilter(tf);
494 ALLOCATOR_PROPERTIES actual;
496 TRACE("()\n");
498 if (!ppropInputRequest->cbAlign)
499 ppropInputRequest->cbAlign = 1;
501 if (ppropInputRequest->cbBuffer < This->outputSize)
502 ppropInputRequest->cbBuffer = This->outputSize + ppropInputRequest->cbAlign;
504 if (!ppropInputRequest->cBuffers)
505 ppropInputRequest->cBuffers = 1;
507 return IMemAllocator_SetProperties(pAlloc, ppropInputRequest, &actual);
510 static const TransformFilterFuncTable QTVDecoder_FuncsTable = {
511 QTVDecoder_DecideBufferSize,
512 QTVDecoder_StartStreaming,
513 QTVDecoder_Receive,
514 QTVDecoder_StopStreaming,
515 NULL,
516 QTVDecoder_SetMediaType,
517 NULL,
518 QTVDecoder_BreakConnect,
519 NULL,
520 NULL,
521 NULL,
522 NULL
525 IUnknown * CALLBACK QTVDecoder_create(IUnknown * pUnkOuter, HRESULT* phr)
527 HRESULT hr;
528 QTVDecoderImpl * This;
530 TRACE("(%p, %p)\n", pUnkOuter, phr);
532 *phr = S_OK;
534 if (pUnkOuter)
536 *phr = CLASS_E_NOAGGREGATION;
537 return NULL;
540 hr = TransformFilter_Construct(&QTVDecoder_Vtbl, sizeof(QTVDecoderImpl), &CLSID_QTVDecoder, &QTVDecoder_FuncsTable, (IBaseFilter**)&This);
542 if (FAILED(hr))
544 *phr = hr;
545 return NULL;
548 *phr = hr;
549 return (IUnknown*)This;
552 static const IBaseFilterVtbl QTVDecoder_Vtbl =
554 TransformFilterImpl_QueryInterface,
555 BaseFilterImpl_AddRef,
556 TransformFilterImpl_Release,
557 BaseFilterImpl_GetClassID,
558 TransformFilterImpl_Stop,
559 TransformFilterImpl_Pause,
560 TransformFilterImpl_Run,
561 BaseFilterImpl_GetState,
562 BaseFilterImpl_SetSyncSource,
563 BaseFilterImpl_GetSyncSource,
564 BaseFilterImpl_EnumPins,
565 TransformFilterImpl_FindPin,
566 BaseFilterImpl_QueryFilterInfo,
567 BaseFilterImpl_JoinFilterGraph,
568 BaseFilterImpl_QueryVendorInfo