266 lines
7.8 KiB
C++
266 lines
7.8 KiB
C++
// Defines the transform class.
|
|
//
|
|
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
|
|
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
|
|
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
|
|
// PARTICULAR PURPOSE.
|
|
//
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
#ifndef GRAYSCALE_H
|
|
#define GRAYSCALE_H
|
|
|
|
#include <new>
|
|
#include <mfapi.h>
|
|
#include <mftransform.h>
|
|
#include <mfidl.h>
|
|
#include <mferror.h>
|
|
#include <strsafe.h>
|
|
#include <assert.h>
|
|
|
|
// Note: The Direct2D helper library is included for its 2D matrix operations.
|
|
#include <D2d1helper.h>
|
|
|
|
#include <wrl\implements.h>
|
|
#include <wrl\module.h>
|
|
#include <windows.media.h>
|
|
|
|
#include "GrayscaleTransform.h"
|
|
|
|
// CLSID of the MFT.
|
|
DEFINE_GUID(CLSID_GrayscaleMFT,
|
|
0x2f3dbc05, 0xc011, 0x4a8f, 0xb2, 0x64, 0xe4, 0x2e, 0x35, 0xc6, 0x7b, 0xf4);
|
|
|
|
//
|
|
// * IMPORTANT: If you implement your own MFT, create a new GUID for the CLSID. *
|
|
//
|
|
|
|
|
|
// Configuration attributes
|
|
|
|
// {7BBBB051-133B-41F5-B6AA-5AFF9B33A2CB}
|
|
DEFINE_GUID(MFT_GRAYSCALE_DESTINATION_RECT,
|
|
0x7bbbb051, 0x133b, 0x41f5, 0xb6, 0xaa, 0x5a, 0xff, 0x9b, 0x33, 0xa2, 0xcb);
|
|
|
|
|
|
// {14782342-93E8-4565-872C-D9A2973D5CBF}
|
|
DEFINE_GUID(MFT_GRAYSCALE_SATURATION,
|
|
0x14782342, 0x93e8, 0x4565, 0x87, 0x2c, 0xd9, 0xa2, 0x97, 0x3d, 0x5c, 0xbf);
|
|
|
|
// {E0BADE5D-E4B9-4689-9DBA-E2F00D9CED0E}
|
|
DEFINE_GUID(MFT_GRAYSCALE_CHROMA_ROTATION,
|
|
0xe0bade5d, 0xe4b9, 0x4689, 0x9d, 0xba, 0xe2, 0xf0, 0xd, 0x9c, 0xed, 0xe);
|
|
|
|
|
|
template <class T> void SafeRelease(T **ppT)
|
|
{
|
|
if (*ppT)
|
|
{
|
|
(*ppT)->Release();
|
|
*ppT = NULL;
|
|
}
|
|
}
|
|
|
|
// Function pointer for the function that transforms the image.
|
|
typedef void (*IMAGE_TRANSFORM_FN)(
|
|
const D2D1::Matrix3x2F& mat, // Chroma transform matrix.
|
|
const D2D_RECT_U& rcDest, // Destination rectangle for the transformation.
|
|
BYTE* pDest, // Destination buffer.
|
|
LONG lDestStride, // Destination stride.
|
|
const BYTE* pSrc, // Source buffer.
|
|
LONG lSrcStride, // Source stride.
|
|
DWORD dwWidthInPixels, // Image width in pixels.
|
|
DWORD dwHeightInPixels // Image height in pixels.
|
|
);
|
|
|
|
// CGrayscale class:
|
|
// Implements a grayscale video effect.
|
|
|
|
class CGrayscale
|
|
: public Microsoft::WRL::RuntimeClass<
|
|
Microsoft::WRL::RuntimeClassFlags< Microsoft::WRL::RuntimeClassType::WinRtClassicComMix >,
|
|
ABI::Windows::Media::IMediaExtension,
|
|
IMFTransform >
|
|
{
|
|
InspectableClass(RuntimeClass_GrayscaleTransform_GrayscaleEffect, BaseTrust)
|
|
|
|
public:
|
|
CGrayscale();
|
|
|
|
~CGrayscale();
|
|
|
|
STDMETHOD(RuntimeClassInitialize)();
|
|
|
|
// IMediaExtension
|
|
STDMETHODIMP SetProperties(ABI::Windows::Foundation::Collections::IPropertySet *pConfiguration);
|
|
|
|
// IMFTransform
|
|
STDMETHODIMP GetStreamLimits(
|
|
DWORD *pdwInputMinimum,
|
|
DWORD *pdwInputMaximum,
|
|
DWORD *pdwOutputMinimum,
|
|
DWORD *pdwOutputMaximum
|
|
);
|
|
|
|
STDMETHODIMP GetStreamCount(
|
|
DWORD *pcInputStreams,
|
|
DWORD *pcOutputStreams
|
|
);
|
|
|
|
STDMETHODIMP GetStreamIDs(
|
|
DWORD dwInputIDArraySize,
|
|
DWORD *pdwInputIDs,
|
|
DWORD dwOutputIDArraySize,
|
|
DWORD *pdwOutputIDs
|
|
);
|
|
|
|
STDMETHODIMP GetInputStreamInfo(
|
|
DWORD dwInputStreamID,
|
|
MFT_INPUT_STREAM_INFO * pStreamInfo
|
|
);
|
|
|
|
STDMETHODIMP GetOutputStreamInfo(
|
|
DWORD dwOutputStreamID,
|
|
MFT_OUTPUT_STREAM_INFO * pStreamInfo
|
|
);
|
|
|
|
STDMETHODIMP GetAttributes(IMFAttributes** pAttributes);
|
|
|
|
STDMETHODIMP GetInputStreamAttributes(
|
|
DWORD dwInputStreamID,
|
|
IMFAttributes **ppAttributes
|
|
);
|
|
|
|
STDMETHODIMP GetOutputStreamAttributes(
|
|
DWORD dwOutputStreamID,
|
|
IMFAttributes **ppAttributes
|
|
);
|
|
|
|
STDMETHODIMP DeleteInputStream(DWORD dwStreamID);
|
|
|
|
STDMETHODIMP AddInputStreams(
|
|
DWORD cStreams,
|
|
DWORD *adwStreamIDs
|
|
);
|
|
|
|
STDMETHODIMP GetInputAvailableType(
|
|
DWORD dwInputStreamID,
|
|
DWORD dwTypeIndex, // 0-based
|
|
IMFMediaType **ppType
|
|
);
|
|
|
|
STDMETHODIMP GetOutputAvailableType(
|
|
DWORD dwOutputStreamID,
|
|
DWORD dwTypeIndex, // 0-based
|
|
IMFMediaType **ppType
|
|
);
|
|
|
|
STDMETHODIMP SetInputType(
|
|
DWORD dwInputStreamID,
|
|
IMFMediaType *pType,
|
|
DWORD dwFlags
|
|
);
|
|
|
|
STDMETHODIMP SetOutputType(
|
|
DWORD dwOutputStreamID,
|
|
IMFMediaType *pType,
|
|
DWORD dwFlags
|
|
);
|
|
|
|
STDMETHODIMP GetInputCurrentType(
|
|
DWORD dwInputStreamID,
|
|
IMFMediaType **ppType
|
|
);
|
|
|
|
STDMETHODIMP GetOutputCurrentType(
|
|
DWORD dwOutputStreamID,
|
|
IMFMediaType **ppType
|
|
);
|
|
|
|
STDMETHODIMP GetInputStatus(
|
|
DWORD dwInputStreamID,
|
|
DWORD *pdwFlags
|
|
);
|
|
|
|
STDMETHODIMP GetOutputStatus(DWORD *pdwFlags);
|
|
|
|
STDMETHODIMP SetOutputBounds(
|
|
LONGLONG hnsLowerBound,
|
|
LONGLONG hnsUpperBound
|
|
);
|
|
|
|
STDMETHODIMP ProcessEvent(
|
|
DWORD dwInputStreamID,
|
|
IMFMediaEvent *pEvent
|
|
);
|
|
|
|
STDMETHODIMP ProcessMessage(
|
|
MFT_MESSAGE_TYPE eMessage,
|
|
ULONG_PTR ulParam
|
|
);
|
|
|
|
STDMETHODIMP ProcessInput(
|
|
DWORD dwInputStreamID,
|
|
IMFSample *pSample,
|
|
DWORD dwFlags
|
|
);
|
|
|
|
STDMETHODIMP ProcessOutput(
|
|
DWORD dwFlags,
|
|
DWORD cOutputBufferCount,
|
|
MFT_OUTPUT_DATA_BUFFER *pOutputSamples, // one per stream
|
|
DWORD *pdwStatus
|
|
);
|
|
|
|
|
|
private:
|
|
// HasPendingOutput: Returns TRUE if the MFT is holding an input sample.
|
|
BOOL HasPendingOutput() const { return m_pSample != NULL; }
|
|
|
|
// IsValidInputStream: Returns TRUE if dwInputStreamID is a valid input stream identifier.
|
|
BOOL IsValidInputStream(DWORD dwInputStreamID) const
|
|
{
|
|
return dwInputStreamID == 0;
|
|
}
|
|
|
|
// IsValidOutputStream: Returns TRUE if dwOutputStreamID is a valid output stream identifier.
|
|
BOOL IsValidOutputStream(DWORD dwOutputStreamID) const
|
|
{
|
|
return dwOutputStreamID == 0;
|
|
}
|
|
|
|
HRESULT OnGetPartialType(DWORD dwTypeIndex, IMFMediaType **ppmt);
|
|
HRESULT OnCheckInputType(IMFMediaType *pmt);
|
|
HRESULT OnCheckOutputType(IMFMediaType *pmt);
|
|
HRESULT OnCheckMediaType(IMFMediaType *pmt);
|
|
void OnSetInputType(IMFMediaType *pmt);
|
|
void OnSetOutputType(IMFMediaType *pmt);
|
|
HRESULT BeginStreaming();
|
|
HRESULT EndStreaming();
|
|
HRESULT OnProcessOutput(IMFMediaBuffer *pIn, IMFMediaBuffer *pOut);
|
|
HRESULT OnFlush();
|
|
HRESULT UpdateFormatInfo();
|
|
|
|
CRITICAL_SECTION m_critSec;
|
|
|
|
// Transformation parameters
|
|
D2D1::Matrix3x2F m_transform; // Chroma transform matrix.
|
|
D2D_RECT_U m_rcDest; // Destination rectangle for the effect.
|
|
|
|
// Streaming
|
|
bool m_bStreamingInitialized;
|
|
IMFSample *m_pSample; // Input sample.
|
|
IMFMediaType *m_pInputType; // Input media type.
|
|
IMFMediaType *m_pOutputType; // Output media type.
|
|
|
|
// Fomat information
|
|
UINT32 m_imageWidthInPixels;
|
|
UINT32 m_imageHeightInPixels;
|
|
DWORD m_cbImageSize; // Image size, in bytes.
|
|
|
|
IMFAttributes *m_pAttributes;
|
|
|
|
// Image transform function. (Changes based on the media type.)
|
|
IMAGE_TRANSFORM_FN m_pTransformFn;
|
|
};
|
|
#endif |