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
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>
68 #undef GetCurrentThread
71 #undef GetCurrentProcess
94 #undef IsWindowVisible
107 #undef STDMETHODCALLTYPE
122 #include "dvdmedia.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
139 IUnknown *seekthru_unk;
141 ImageDescriptionHandle hImageDescription;
142 CFMutableDictionaryRef outputBufferAttributes;
143 ICMDecompressionSessionRef decompressionSession;
150 static const IBaseFilterVtbl QTVDecoder_Vtbl;
152 static void trackingCallback(
153 void *decompressionTrackingRefCon,
155 ICMDecompressionTrackingFlags decompressionTrackingFlags,
156 CVPixelBufferRef pixelBuffer,
157 TimeValue64 displayTime,
158 TimeValue64 displayDuration,
159 ICMValidTimeFlags validTimeFlags,
161 void *sourceFrameRefCon )
163 QTVDecoderImpl *This = (QTVDecoderImpl*)decompressionTrackingRefCon;
164 IMediaSample *pSample = (IMediaSample*)sourceFrameRefCon;
166 IMediaSample* pOutSample = NULL;
172 ERR("Error from Codec, no frame decompressed\n");
178 ERR("No pixel buffer, no frame decompressed\n");
182 EnterCriticalSection(&This->tf.csReceive);
183 hr = BaseOutputPinImpl_GetDeliveryBuffer((BaseOutputPin*)This->tf.ppPins[1], &pOutSample, NULL, NULL, 0);
185 ERR("Unable to get delivery buffer (%x)\n", hr);
189 hr = IMediaSample_SetActualDataLength(pOutSample, 0);
192 hr = IMediaSample_GetPointer(pOutSample, &pbDstStream);
194 ERR("Unable to get pointer to buffer (%x)\n", hr);
198 cbDstStream = IMediaSample_GetSize(pOutSample);
199 if (cbDstStream < This->outputSize) {
200 ERR("Sample size is too small %d < %d\n", cbDstStream, This->outputSize);
205 hr = AccessPixelBufferPixels(pixelBuffer, pbDstStream);
209 IMediaSample_SetActualDataLength(pOutSample, This->outputSize);
211 IMediaSample_SetPreroll(pOutSample, (IMediaSample_IsPreroll(pSample) == S_OK));
212 IMediaSample_SetDiscontinuity(pOutSample, (IMediaSample_IsDiscontinuity(pSample) == S_OK));
213 IMediaSample_SetSyncPoint(pOutSample, (IMediaSample_IsSyncPoint(pSample) == S_OK));
216 IMediaSample_SetTime(pOutSample, NULL, NULL);
219 LONGLONG tStart, tStop;
221 if (validTimeFlags & kICMValidTime_DisplayTimeStampIsValid)
222 tStart = displayTime;
225 if (validTimeFlags & kICMValidTime_DisplayDurationIsValid)
226 tStop = tStart + displayDuration;
230 IMediaSample_SetTime(pOutSample, &tStart, &tStop);
233 hr = BaseOutputPinImpl_Deliver((BaseOutputPin*)This->tf.ppPins[1], pOutSample);
234 if (hr != S_OK && hr != VFW_E_NOT_CONNECTED)
235 ERR("Error sending sample (%x)\n", hr);
238 LeaveCriticalSection(&This->tf.csReceive);
240 IMediaSample_Release(pOutSample);
245 static HRESULT WINAPI QTVDecoder_StartStreaming(TransformFilter* pTransformFilter)
247 QTVDecoderImpl* This = (QTVDecoderImpl*)pTransformFilter;
249 ICMDecompressionSessionOptionsRef sessionOptions = NULL;
250 ICMDecompressionTrackingCallbackRecord trackingCallbackRecord;
252 TRACE("(%p)->()\n", This);
254 trackingCallbackRecord.decompressionTrackingCallback = trackingCallback;
255 trackingCallbackRecord.decompressionTrackingRefCon = (void*)This;
257 err = ICMDecompressionSessionCreate(NULL, This->hImageDescription, sessionOptions, This->outputBufferAttributes, &trackingCallbackRecord, &This->decompressionSession);
261 ERR("Error with ICMDecompressionSessionCreate %i\n",err);
268 static HRESULT WINAPI QTVDecoder_Receive(TransformFilter *tf, IMediaSample *pSample)
270 QTVDecoderImpl* This = (QTVDecoderImpl *)tf;
275 ICMFrameTimeRecord frameTime = {{0}};
277 TimeScale timeScale = 1;
278 OSStatus err = noErr;
279 LONGLONG tStart, tStop;
281 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
284 ERR("Cannot get pointer to sample data (%x)\n", hr);
288 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
290 if (IMediaSample_GetTime(pSample, &tStart, &tStop) != S_OK)
295 frameTime.recordSize = sizeof(ICMFrameTimeRecord);
296 *(TimeValue64 *)&frameTime.value = tStart;
298 frameTime.rate = fixed1;
299 frameTime.duration = tStop - tStart;
300 frameTime.frameNumber = 0;
301 frameTime.flags = icmFrameTimeIsNonScheduledDisplayTime;
303 err = ICMDecompressionSessionDecodeFrame(This->decompressionSession,
304 (UInt8 *)pbSrcStream, cbSrcStream, NULL, &frameTime, pSample);
308 ERR("Error with ICMDecompressionSessionDecodeFrame\n");
313 ICMDecompressionSessionSetNonScheduledDisplayTime(This->decompressionSession, time, timeScale, 0);
314 ICMDecompressionSessionFlush(This->decompressionSession);
321 static HRESULT WINAPI QTVDecoder_StopStreaming(TransformFilter* pTransformFilter)
323 QTVDecoderImpl* This = (QTVDecoderImpl*)pTransformFilter;
325 TRACE("(%p)->()\n", This);
327 if (This->decompressionSession)
328 ICMDecompressionSessionRelease(This->decompressionSession);
329 This->decompressionSession = NULL;
334 static HRESULT WINAPI QTVDecoder_SetMediaType(TransformFilter *tf, PIN_DIRECTION dir, const AM_MEDIA_TYPE * pmt)
336 QTVDecoderImpl* This = (QTVDecoderImpl*)tf;
337 HRESULT hr = VFW_E_TYPE_NOT_ACCEPTED;
339 AM_MEDIA_TYPE *outpmt = &This->tf.pmt;
340 CFNumberRef n = NULL;
342 TRACE("(%p)->(%p)\n", This, pmt);
344 if (dir != PINDIR_INPUT)
347 FreeMediaType(outpmt);
348 CopyMediaType(outpmt, pmt);
350 if (This->hImageDescription)
351 DisposeHandle((Handle)This->hImageDescription);
353 This->hImageDescription = (ImageDescriptionHandle)
354 NewHandleClear(sizeof(ImageDescription));
356 if (This->hImageDescription != NULL)
358 (**This->hImageDescription).idSize = sizeof(ImageDescription);
359 (**This->hImageDescription).spatialQuality = codecNormalQuality;
360 (**This->hImageDescription).frameCount = 1;
361 (**This->hImageDescription).clutID = -1;
365 ERR("Failed to create ImageDescription\n");
369 /* Check root (GUID w/o FOURCC) */
370 if ((IsEqualIID(&pmt->majortype, &MEDIATYPE_Video)) &&
371 (!memcmp(((const char *)&pmt->subtype)+4, ((const char *)&MEDIATYPE_Video)+4, sizeof(GUID)-4)))
373 VIDEOINFOHEADER *format1 = (VIDEOINFOHEADER *)outpmt->pbFormat;
374 VIDEOINFOHEADER2 *format2 = (VIDEOINFOHEADER2 *)outpmt->pbFormat;
375 BITMAPINFOHEADER *bmi;
377 DecompressorComponent dc;
379 DWORD outputWidth, outputHeight, outputDepth;
381 if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo))
382 bmi = &format1->bmiHeader;
383 else if (IsEqualIID(&pmt->formattype, &FORMAT_VideoInfo2))
384 bmi = &format2->bmiHeader;
388 TRACE("Fourcc: %s\n", debugstr_an((const char *)&pmt->subtype.Data1, 4));
389 fourCC = ((const char *)&pmt->subtype.Data1)[3] |
390 (((const char *)&pmt->subtype.Data1)[2]<<8) |
391 (((const char *)&pmt->subtype.Data1)[1]<<16) |
392 (((const char *)&pmt->subtype.Data1)[0]<<24);
394 err = FindCodec(fourCC,NULL,NULL,&dc);
395 if (err != noErr || dc == 0x0)
397 TRACE("Codec not found\n");
401 outputWidth = bmi->biWidth;
402 outputHeight = bmi->biHeight;
404 (**This->hImageDescription).cType = fourCC;
405 (**This->hImageDescription).width = outputWidth;
406 (**This->hImageDescription).height = outputHeight;
407 (**This->hImageDescription).depth = bmi->biBitCount;
408 (**This->hImageDescription).hRes = 72<<16;
409 (**This->hImageDescription).vRes = 72<<16;
411 if (This->outputBufferAttributes)
412 CFRelease(This->outputBufferAttributes);
414 This->outputBufferAttributes = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
415 if (!This->outputBufferAttributes)
417 ERR("Failed to create outputBufferAttributes\n");
421 n = CFNumberCreate(NULL, kCFNumberIntType, &outputWidth);
422 CFDictionaryAddValue(This->outputBufferAttributes, kCVPixelBufferWidthKey, n);
425 n = CFNumberCreate(NULL, kCFNumberIntType, &outputHeight);
426 CFDictionaryAddValue(This->outputBufferAttributes, kCVPixelBufferHeightKey, n);
429 /* yes this looks wrong. but 32ARGB is 24 RGB with an alpha channel */
430 format = k32ARGBPixelFormat;
431 n = CFNumberCreate(NULL, kCFNumberIntType, &format);
432 CFDictionaryAddValue(This->outputBufferAttributes, kCVPixelBufferPixelFormatTypeKey, n);
435 CFDictionaryAddValue(This->outputBufferAttributes, kCVPixelBufferCGBitmapContextCompatibilityKey, kCFBooleanTrue);
436 CFDictionaryAddValue(This->outputBufferAttributes, kCVPixelBufferCGImageCompatibilityKey, kCFBooleanTrue);
439 This->outputSize = outputWidth * outputHeight * outputDepth;
440 bmi->biCompression = BI_RGB;
441 bmi->biBitCount = 24;
442 outpmt->subtype = MEDIASUBTYPE_RGB24;
448 if (This->hImageDescription)
450 DisposeHandle((Handle)This->hImageDescription);
451 This->hImageDescription = NULL;
453 if (This->outputBufferAttributes)
455 CFRelease(This->outputBufferAttributes);
456 This->outputBufferAttributes = NULL;
459 TRACE("Connection refused\n");
463 static HRESULT WINAPI QTVDecoder_BreakConnect(TransformFilter *tf, PIN_DIRECTION dir)
465 QTVDecoderImpl *This = (QTVDecoderImpl *)tf;
467 TRACE("(%p)->()\n", This);
469 if (This->hImageDescription)
470 DisposeHandle((Handle)This->hImageDescription);
471 if (This->outputBufferAttributes)
472 CFRelease(This->outputBufferAttributes);
474 This->hImageDescription = NULL;
475 This->outputBufferAttributes = NULL;
480 static HRESULT WINAPI QTVDecoder_DecideBufferSize(TransformFilter *tf, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
482 QTVDecoderImpl *This = (QTVDecoderImpl*)tf;
483 ALLOCATOR_PROPERTIES actual;
487 if (!ppropInputRequest->cbAlign)
488 ppropInputRequest->cbAlign = 1;
490 if (ppropInputRequest->cbBuffer < This->outputSize)
491 ppropInputRequest->cbBuffer = This->outputSize + ppropInputRequest->cbAlign;
493 if (!ppropInputRequest->cBuffers)
494 ppropInputRequest->cBuffers = 1;
496 return IMemAllocator_SetProperties(pAlloc, ppropInputRequest, &actual);
499 static const TransformFilterFuncTable QTVDecoder_FuncsTable = {
500 QTVDecoder_DecideBufferSize,
501 QTVDecoder_StartStreaming,
503 QTVDecoder_StopStreaming,
505 QTVDecoder_SetMediaType,
507 QTVDecoder_BreakConnect,
514 IUnknown * CALLBACK QTVDecoder_create(IUnknown * pUnkOuter, HRESULT* phr)
517 QTVDecoderImpl * This;
519 TRACE("(%p, %p)\n", pUnkOuter, phr);
525 *phr = CLASS_E_NOAGGREGATION;
529 hr = TransformFilter_Construct(&QTVDecoder_Vtbl, sizeof(QTVDecoderImpl), &CLSID_QTVDecoder, &QTVDecoder_FuncsTable, (IBaseFilter**)&This);
538 ISeekingPassThru *passthru;
539 hr = CoCreateInstance(&CLSID_SeekingPassThru, (IUnknown*)This, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void**)&This->seekthru_unk);
540 IUnknown_QueryInterface(This->seekthru_unk, &IID_ISeekingPassThru, (void**)&passthru);
541 ISeekingPassThru_Init(passthru, FALSE, (IPin*)This->tf.ppPins[0]);
542 ISeekingPassThru_Release(passthru);
546 return (IUnknown*)This;
549 HRESULT WINAPI QTVDecoder_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
552 QTVDecoderImpl *This = (QTVDecoderImpl *)iface;
553 TRACE("(%p/%p)->(%s, %p)\n", This, iface, debugstr_guid(riid), ppv);
555 if (IsEqualIID(riid, &IID_IMediaSeeking))
556 return IUnknown_QueryInterface(This->seekthru_unk, riid, ppv);
558 hr = TransformFilterImpl_QueryInterface(iface, riid, ppv);
563 static const IBaseFilterVtbl QTVDecoder_Vtbl =
565 QTVDecoder_QueryInterface,
566 BaseFilterImpl_AddRef,
567 TransformFilterImpl_Release,
568 BaseFilterImpl_GetClassID,
569 TransformFilterImpl_Stop,
570 TransformFilterImpl_Pause,
571 TransformFilterImpl_Run,
572 BaseFilterImpl_GetState,
573 BaseFilterImpl_SetSyncSource,
574 BaseFilterImpl_GetSyncSource,
575 BaseFilterImpl_EnumPins,
576 TransformFilterImpl_FindPin,
577 BaseFilterImpl_QueryFilterInfo,
578 BaseFilterImpl_JoinFilterGraph,
579 BaseFilterImpl_QueryVendorInfo