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 inline QTVDecoderImpl
*impl_from_IBaseFilter( IBaseFilter
*iface
)
152 return CONTAINING_RECORD(iface
, QTVDecoderImpl
, tf
.filter
.IBaseFilter_iface
);
155 static inline QTVDecoderImpl
*impl_from_TransformFilter( TransformFilter
*iface
)
157 return CONTAINING_RECORD(iface
, QTVDecoderImpl
, tf
.filter
);
160 static const IBaseFilterVtbl QTVDecoder_Vtbl
;
162 static void trackingCallback(
163 void *decompressionTrackingRefCon
,
165 ICMDecompressionTrackingFlags decompressionTrackingFlags
,
166 CVPixelBufferRef pixelBuffer
,
167 TimeValue64 displayTime
,
168 TimeValue64 displayDuration
,
169 ICMValidTimeFlags validTimeFlags
,
171 void *sourceFrameRefCon
)
173 QTVDecoderImpl
*This
= (QTVDecoderImpl
*)decompressionTrackingRefCon
;
174 IMediaSample
*pSample
= (IMediaSample
*)sourceFrameRefCon
;
176 IMediaSample
* pOutSample
= NULL
;
182 ERR("Error from Codec, no frame decompressed\n");
188 ERR("No pixel buffer, no frame decompressed\n");
192 EnterCriticalSection(&This
->tf
.csReceive
);
193 hr
= BaseOutputPinImpl_GetDeliveryBuffer((BaseOutputPin
*)This
->tf
.ppPins
[1], &pOutSample
, NULL
, NULL
, 0);
195 ERR("Unable to get delivery buffer (%x)\n", hr
);
199 hr
= IMediaSample_SetActualDataLength(pOutSample
, 0);
202 hr
= IMediaSample_GetPointer(pOutSample
, &pbDstStream
);
204 ERR("Unable to get pointer to buffer (%x)\n", hr
);
208 cbDstStream
= IMediaSample_GetSize(pOutSample
);
209 if (cbDstStream
< This
->outputSize
) {
210 ERR("Sample size is too small %d < %d\n", cbDstStream
, This
->outputSize
);
215 hr
= AccessPixelBufferPixels(pixelBuffer
, pbDstStream
);
219 IMediaSample_SetActualDataLength(pOutSample
, This
->outputSize
);
221 IMediaSample_SetPreroll(pOutSample
, (IMediaSample_IsPreroll(pSample
) == S_OK
));
222 IMediaSample_SetDiscontinuity(pOutSample
, (IMediaSample_IsDiscontinuity(pSample
) == S_OK
));
223 IMediaSample_SetSyncPoint(pOutSample
, (IMediaSample_IsSyncPoint(pSample
) == S_OK
));
226 IMediaSample_SetTime(pOutSample
, NULL
, NULL
);
229 LONGLONG tStart
, tStop
;
231 if (validTimeFlags
& kICMValidTime_DisplayTimeStampIsValid
)
232 tStart
= displayTime
;
235 if (validTimeFlags
& kICMValidTime_DisplayDurationIsValid
)
236 tStop
= tStart
+ displayDuration
;
240 IMediaSample_SetTime(pOutSample
, &tStart
, &tStop
);
243 LeaveCriticalSection(&This
->tf
.csReceive
);
244 hr
= BaseOutputPinImpl_Deliver((BaseOutputPin
*)This
->tf
.ppPins
[1], pOutSample
);
245 EnterCriticalSection(&This
->tf
.csReceive
);
246 if (hr
!= S_OK
&& hr
!= VFW_E_NOT_CONNECTED
)
247 ERR("Error sending sample (%x)\n", hr
);
250 LeaveCriticalSection(&This
->tf
.csReceive
);
252 IMediaSample_Release(pOutSample
);
257 static HRESULT WINAPI
QTVDecoder_StartStreaming(TransformFilter
* pTransformFilter
)
259 QTVDecoderImpl
* This
= impl_from_TransformFilter(pTransformFilter
);
261 ICMDecompressionSessionOptionsRef sessionOptions
= NULL
;
262 ICMDecompressionTrackingCallbackRecord trackingCallbackRecord
;
264 TRACE("(%p)->()\n", This
);
266 trackingCallbackRecord
.decompressionTrackingCallback
= trackingCallback
;
267 trackingCallbackRecord
.decompressionTrackingRefCon
= (void*)This
;
269 err
= ICMDecompressionSessionCreate(NULL
, This
->hImageDescription
, sessionOptions
, This
->outputBufferAttributes
, &trackingCallbackRecord
, &This
->decompressionSession
);
273 ERR("Error with ICMDecompressionSessionCreate %i\n",err
);
280 static HRESULT WINAPI
QTVDecoder_Receive(TransformFilter
*tf
, IMediaSample
*pSample
)
282 QTVDecoderImpl
* This
= impl_from_TransformFilter(tf
);
287 ICMFrameTimeRecord frameTime
= {{0}};
289 TimeScale timeScale
= 1;
290 OSStatus err
= noErr
;
291 LONGLONG tStart
, tStop
;
293 hr
= IMediaSample_GetPointer(pSample
, &pbSrcStream
);
296 ERR("Cannot get pointer to sample data (%x)\n", hr
);
300 cbSrcStream
= IMediaSample_GetActualDataLength(pSample
);
302 if (IMediaSample_GetTime(pSample
, &tStart
, &tStop
) != S_OK
)
307 frameTime
.recordSize
= sizeof(ICMFrameTimeRecord
);
308 *(TimeValue64
*)&frameTime
.value
= tStart
;
310 frameTime
.rate
= fixed1
;
311 frameTime
.duration
= tStop
- tStart
;
312 frameTime
.frameNumber
= 0;
313 frameTime
.flags
= icmFrameTimeIsNonScheduledDisplayTime
;
315 err
= ICMDecompressionSessionDecodeFrame(This
->decompressionSession
,
316 (UInt8
*)pbSrcStream
, cbSrcStream
, NULL
, &frameTime
, pSample
);
320 ERR("Error with ICMDecompressionSessionDecodeFrame\n");
325 ICMDecompressionSessionSetNonScheduledDisplayTime(This
->decompressionSession
, time
, timeScale
, 0);
326 ICMDecompressionSessionFlush(This
->decompressionSession
);
333 static HRESULT WINAPI
QTVDecoder_StopStreaming(TransformFilter
* pTransformFilter
)
335 QTVDecoderImpl
* This
= impl_from_TransformFilter(pTransformFilter
);
337 TRACE("(%p)->()\n", This
);
339 if (This
->decompressionSession
)
340 ICMDecompressionSessionRelease(This
->decompressionSession
);
341 This
->decompressionSession
= NULL
;
346 static HRESULT WINAPI
QTVDecoder_SetMediaType(TransformFilter
*tf
, PIN_DIRECTION dir
, const AM_MEDIA_TYPE
* pmt
)
348 QTVDecoderImpl
* This
= impl_from_TransformFilter(tf
);
349 HRESULT hr
= VFW_E_TYPE_NOT_ACCEPTED
;
351 AM_MEDIA_TYPE
*outpmt
= &This
->tf
.pmt
;
352 CFNumberRef n
= NULL
;
354 TRACE("(%p)->(%p)\n", This
, pmt
);
356 if (dir
!= PINDIR_INPUT
)
359 FreeMediaType(outpmt
);
360 CopyMediaType(outpmt
, pmt
);
362 if (This
->hImageDescription
)
363 DisposeHandle((Handle
)This
->hImageDescription
);
365 This
->hImageDescription
= (ImageDescriptionHandle
)
366 NewHandleClear(sizeof(ImageDescription
));
368 if (This
->hImageDescription
!= NULL
)
370 (**This
->hImageDescription
).idSize
= sizeof(ImageDescription
);
371 (**This
->hImageDescription
).spatialQuality
= codecNormalQuality
;
372 (**This
->hImageDescription
).frameCount
= 1;
373 (**This
->hImageDescription
).clutID
= -1;
377 ERR("Failed to create ImageDescription\n");
381 /* Check root (GUID w/o FOURCC) */
382 if ((IsEqualIID(&pmt
->majortype
, &MEDIATYPE_Video
)) &&
383 (!memcmp(((const char *)&pmt
->subtype
)+4, ((const char *)&MEDIATYPE_Video
)+4, sizeof(GUID
)-4)))
385 VIDEOINFOHEADER
*format1
= (VIDEOINFOHEADER
*)outpmt
->pbFormat
;
386 VIDEOINFOHEADER2
*format2
= (VIDEOINFOHEADER2
*)outpmt
->pbFormat
;
387 BITMAPINFOHEADER
*bmi
;
389 DecompressorComponent dc
;
391 DWORD outputWidth
, outputHeight
, outputDepth
;
393 if (IsEqualIID(&pmt
->formattype
, &FORMAT_VideoInfo
))
394 bmi
= &format1
->bmiHeader
;
395 else if (IsEqualIID(&pmt
->formattype
, &FORMAT_VideoInfo2
))
396 bmi
= &format2
->bmiHeader
;
400 TRACE("Fourcc: %s\n", debugstr_an((const char *)&pmt
->subtype
.Data1
, 4));
401 fourCC
= ((const char *)&pmt
->subtype
.Data1
)[3] |
402 (((const char *)&pmt
->subtype
.Data1
)[2]<<8) |
403 (((const char *)&pmt
->subtype
.Data1
)[1]<<16) |
404 (((const char *)&pmt
->subtype
.Data1
)[0]<<24);
406 err
= FindCodec(fourCC
,NULL
,NULL
,&dc
);
407 if (err
!= noErr
|| dc
== 0x0)
409 TRACE("Codec not found\n");
413 outputWidth
= bmi
->biWidth
;
414 outputHeight
= bmi
->biHeight
;
416 (**This
->hImageDescription
).cType
= fourCC
;
417 (**This
->hImageDescription
).width
= outputWidth
;
418 (**This
->hImageDescription
).height
= outputHeight
;
419 (**This
->hImageDescription
).depth
= bmi
->biBitCount
;
420 (**This
->hImageDescription
).hRes
= 72<<16;
421 (**This
->hImageDescription
).vRes
= 72<<16;
423 if (This
->outputBufferAttributes
)
424 CFRelease(This
->outputBufferAttributes
);
426 This
->outputBufferAttributes
= CFDictionaryCreateMutable(NULL
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
427 if (!This
->outputBufferAttributes
)
429 ERR("Failed to create outputBufferAttributes\n");
433 n
= CFNumberCreate(NULL
, kCFNumberIntType
, &outputWidth
);
434 CFDictionaryAddValue(This
->outputBufferAttributes
, kCVPixelBufferWidthKey
, n
);
437 n
= CFNumberCreate(NULL
, kCFNumberIntType
, &outputHeight
);
438 CFDictionaryAddValue(This
->outputBufferAttributes
, kCVPixelBufferHeightKey
, n
);
441 /* yes this looks wrong. but 32ARGB is 24 RGB with an alpha channel */
442 format
= k32ARGBPixelFormat
;
443 n
= CFNumberCreate(NULL
, kCFNumberIntType
, &format
);
444 CFDictionaryAddValue(This
->outputBufferAttributes
, kCVPixelBufferPixelFormatTypeKey
, n
);
447 CFDictionaryAddValue(This
->outputBufferAttributes
, kCVPixelBufferCGBitmapContextCompatibilityKey
, kCFBooleanTrue
);
448 CFDictionaryAddValue(This
->outputBufferAttributes
, kCVPixelBufferCGImageCompatibilityKey
, kCFBooleanTrue
);
451 This
->outputSize
= outputWidth
* outputHeight
* outputDepth
;
452 bmi
->biCompression
= BI_RGB
;
453 bmi
->biBitCount
= 24;
454 outpmt
->subtype
= MEDIASUBTYPE_RGB24
;
460 if (This
->hImageDescription
)
462 DisposeHandle((Handle
)This
->hImageDescription
);
463 This
->hImageDescription
= NULL
;
465 if (This
->outputBufferAttributes
)
467 CFRelease(This
->outputBufferAttributes
);
468 This
->outputBufferAttributes
= NULL
;
471 TRACE("Connection refused\n");
475 static HRESULT WINAPI
QTVDecoder_BreakConnect(TransformFilter
*tf
, PIN_DIRECTION dir
)
477 QTVDecoderImpl
*This
= impl_from_TransformFilter(tf
);
479 TRACE("(%p)->()\n", This
);
481 if (This
->hImageDescription
)
482 DisposeHandle((Handle
)This
->hImageDescription
);
483 if (This
->outputBufferAttributes
)
484 CFRelease(This
->outputBufferAttributes
);
486 This
->hImageDescription
= NULL
;
487 This
->outputBufferAttributes
= NULL
;
492 static HRESULT WINAPI
QTVDecoder_DecideBufferSize(TransformFilter
*tf
, IMemAllocator
*pAlloc
, ALLOCATOR_PROPERTIES
*ppropInputRequest
)
494 QTVDecoderImpl
*This
= impl_from_TransformFilter(tf
);
495 ALLOCATOR_PROPERTIES actual
;
499 if (!ppropInputRequest
->cbAlign
)
500 ppropInputRequest
->cbAlign
= 1;
502 if (ppropInputRequest
->cbBuffer
< This
->outputSize
)
503 ppropInputRequest
->cbBuffer
= This
->outputSize
+ ppropInputRequest
->cbAlign
;
505 if (!ppropInputRequest
->cBuffers
)
506 ppropInputRequest
->cBuffers
= 1;
508 return IMemAllocator_SetProperties(pAlloc
, ppropInputRequest
, &actual
);
511 static const TransformFilterFuncTable QTVDecoder_FuncsTable
= {
512 QTVDecoder_DecideBufferSize
,
513 QTVDecoder_StartStreaming
,
515 QTVDecoder_StopStreaming
,
517 QTVDecoder_SetMediaType
,
519 QTVDecoder_BreakConnect
,
526 IUnknown
* CALLBACK
QTVDecoder_create(IUnknown
* pUnkOuter
, HRESULT
* phr
)
529 QTVDecoderImpl
* This
;
531 TRACE("(%p, %p)\n", pUnkOuter
, phr
);
537 *phr
= CLASS_E_NOAGGREGATION
;
541 hr
= TransformFilter_Construct(&QTVDecoder_Vtbl
, sizeof(QTVDecoderImpl
), &CLSID_QTVDecoder
, &QTVDecoder_FuncsTable
, (IBaseFilter
**)&This
);
550 ISeekingPassThru
*passthru
;
551 hr
= CoCreateInstance(&CLSID_SeekingPassThru
, (IUnknown
*)This
, CLSCTX_INPROC_SERVER
, &IID_IUnknown
, (void**)&This
->seekthru_unk
);
552 IUnknown_QueryInterface(This
->seekthru_unk
, &IID_ISeekingPassThru
, (void**)&passthru
);
553 ISeekingPassThru_Init(passthru
, FALSE
, This
->tf
.ppPins
[0]);
554 ISeekingPassThru_Release(passthru
);
558 return (IUnknown
*)This
;
561 HRESULT WINAPI
QTVDecoder_QueryInterface(IBaseFilter
* iface
, REFIID riid
, LPVOID
* ppv
)
564 QTVDecoderImpl
*This
= impl_from_IBaseFilter(iface
);
565 TRACE("(%p/%p)->(%s, %p)\n", This
, iface
, debugstr_guid(riid
), ppv
);
567 if (IsEqualIID(riid
, &IID_IMediaSeeking
) || IsEqualIID(riid
, &IID_IMediaPosition
))
568 return IUnknown_QueryInterface(This
->seekthru_unk
, riid
, ppv
);
570 hr
= TransformFilterImpl_QueryInterface(iface
, riid
, ppv
);
575 static const IBaseFilterVtbl QTVDecoder_Vtbl
=
577 QTVDecoder_QueryInterface
,
578 BaseFilterImpl_AddRef
,
579 TransformFilterImpl_Release
,
580 BaseFilterImpl_GetClassID
,
581 TransformFilterImpl_Stop
,
582 TransformFilterImpl_Pause
,
583 TransformFilterImpl_Run
,
584 BaseFilterImpl_GetState
,
585 BaseFilterImpl_SetSyncSource
,
586 BaseFilterImpl_GetSyncSource
,
587 BaseFilterImpl_EnumPins
,
588 TransformFilterImpl_FindPin
,
589 BaseFilterImpl_QueryFilterInfo
,
590 BaseFilterImpl_JoinFilterGraph
,
591 BaseFilterImpl_QueryVendorInfo