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
140 ImageDescriptionHandle hImageDescription;
141 CFMutableDictionaryRef outputBufferAttributes;
142 ICMDecompressionSessionRef decompressionSession;
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,
164 ICMDecompressionTrackingFlags decompressionTrackingFlags,
165 CVPixelBufferRef pixelBuffer,
166 TimeValue64 displayTime,
167 TimeValue64 displayDuration,
168 ICMValidTimeFlags validTimeFlags,
170 void *sourceFrameRefCon )
172 QTVDecoderImpl *This = (QTVDecoderImpl*)decompressionTrackingRefCon;
173 IMediaSample *pSample = (IMediaSample*)sourceFrameRefCon;
175 IMediaSample* pOutSample = NULL;
181 ERR("Error from Codec, no frame decompressed\n");
187 ERR("No pixel buffer, no frame decompressed\n");
191 EnterCriticalSection(&This->tf.csReceive);
192 hr = BaseOutputPinImpl_GetDeliveryBuffer((BaseOutputPin*)This->tf.ppPins[1], &pOutSample, NULL, NULL, 0);
194 ERR("Unable to get delivery buffer (%x)\n", hr);
198 hr = IMediaSample_SetActualDataLength(pOutSample, 0);
201 hr = IMediaSample_GetPointer(pOutSample, &pbDstStream);
203 ERR("Unable to get pointer to buffer (%x)\n", hr);
207 cbDstStream = IMediaSample_GetSize(pOutSample);
208 if (cbDstStream < This->outputSize) {
209 ERR("Sample size is too small %d < %d\n", cbDstStream, This->outputSize);
214 hr = AccessPixelBufferPixels(pixelBuffer, pbDstStream);
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));
225 IMediaSample_SetTime(pOutSample, NULL, NULL);
228 LONGLONG tStart, tStop;
230 if (validTimeFlags & kICMValidTime_DisplayTimeStampIsValid)
231 tStart = displayTime;
234 if (validTimeFlags & kICMValidTime_DisplayDurationIsValid)
235 tStop = tStart + displayDuration;
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);
249 LeaveCriticalSection(&This->tf.csReceive);
251 IMediaSample_Release(pOutSample);
256 static HRESULT WINAPI QTVDecoder_StartStreaming(TransformFilter* pTransformFilter)
258 QTVDecoderImpl* This = impl_from_TransformFilter(pTransformFilter);
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);
272 ERR("Error with ICMDecompressionSessionCreate %i\n",err);
279 static HRESULT WINAPI QTVDecoder_Receive(TransformFilter *tf, IMediaSample *pSample)
281 QTVDecoderImpl* This = impl_from_TransformFilter(tf);
286 ICMFrameTimeRecord frameTime = {{0}};
288 TimeScale timeScale = 1;
289 OSStatus err = noErr;
290 LONGLONG tStart, tStop;
292 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
295 ERR("Cannot get pointer to sample data (%x)\n", hr);
299 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
301 if (IMediaSample_GetTime(pSample, &tStart, &tStop) != S_OK)
306 frameTime.recordSize = sizeof(ICMFrameTimeRecord);
307 *(TimeValue64 *)&frameTime.value = tStart;
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);
319 ERR("Error with ICMDecompressionSessionDecodeFrame\n");
324 ICMDecompressionSessionSetNonScheduledDisplayTime(This->decompressionSession, time, timeScale, 0);
325 ICMDecompressionSessionFlush(This->decompressionSession);
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;
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;
350 AM_MEDIA_TYPE *outpmt = &This->tf.pmt;
351 CFNumberRef n = NULL;
353 TRACE("(%p)->(%p)\n", This, pmt);
355 if (dir != PINDIR_INPUT)
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;
376 ERR("Failed to create ImageDescription\n");
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;
388 DecompressorComponent dc;
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;
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");
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");
432 n = CFNumberCreate(NULL, kCFNumberIntType, &outputWidth);
433 CFDictionaryAddValue(This->outputBufferAttributes, kCVPixelBufferWidthKey, n);
436 n = CFNumberCreate(NULL, kCFNumberIntType, &outputHeight);
437 CFDictionaryAddValue(This->outputBufferAttributes, kCVPixelBufferHeightKey, 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);
446 CFDictionaryAddValue(This->outputBufferAttributes, kCVPixelBufferCGBitmapContextCompatibilityKey, kCFBooleanTrue);
447 CFDictionaryAddValue(This->outputBufferAttributes, kCVPixelBufferCGImageCompatibilityKey, kCFBooleanTrue);
450 This->outputSize = outputWidth * outputHeight * outputDepth;
451 bmi->biCompression = BI_RGB;
452 bmi->biBitCount = 24;
453 outpmt->subtype = MEDIASUBTYPE_RGB24;
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");
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;
491 static HRESULT WINAPI QTVDecoder_DecideBufferSize(TransformFilter *tf, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
493 QTVDecoderImpl *This = impl_from_TransformFilter(tf);
494 ALLOCATOR_PROPERTIES actual;
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,
514 QTVDecoder_StopStreaming,
516 QTVDecoder_SetMediaType,
518 QTVDecoder_BreakConnect,
525 IUnknown * CALLBACK QTVDecoder_create(IUnknown * pUnkOuter, HRESULT* phr)
528 QTVDecoderImpl * This;
530 TRACE("(%p, %p)\n", pUnkOuter, phr);
536 *phr = CLASS_E_NOAGGREGATION;
540 hr = TransformFilter_Construct(&QTVDecoder_Vtbl, sizeof(QTVDecoderImpl), &CLSID_QTVDecoder, &QTVDecoder_FuncsTable, (IBaseFilter**)&This);
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