每一个对象上的数据,都可通过“属性(Attributes)”和“特性(Properties)”来设置、描述。属性和特性相似但不同。
属性(Attributes)= key +value
是个GUID
是个变量。变量的类型局限于:
ü UInt32
ü UInt64
ü 64-bit浮点数
ü GUID
ü 以NULL结尾的宽字符类型(wide-character)字符串
ü 字节数组
ü IUnknown指针
以上类型被包含在MF_ATTRIBUTE_TYPE枚举类型中:
typedef enum _MF_ATTRIBUTE_TYPE {
MF_ATTRIBUTE_UINT32 = VT_UI4,
MF_ATTRIBUTE_UINT64 = VT_UI8,
MF_ATTRIBUTE_DOUBLE = VT_R8,
MF_ATTRIBUTE_GUID = VT_CLSID,
MF_ATTRIBUTE_STRING = VT_LPWSTR,
MF_ATTRIBUTE_BLOB = VT_VECTOR | VT_UI1,
MF_ATTRIBUTE_IUNKNOWN = VT_UNKNOWN
} MF_ATTRIBUTE_TYPE;
通过IMFAttributes接口定义属性组,这个接口是类型安全的。例如,设置32位的整形,可调用:IMFAttributes::SetUINT32。
属性(Attributes)中的key是唯一的,如果设置了两个相同key的属性(Attributes),则只有后一个有效。
通常IMFAttributes类型的指针会作为应用程序中某函数的参数,应用程序负责创建属性集,创建一个空属性集可用MFCreateAttribute。
HRESULT MFCreateAttributes(
__out IMFAttributes **ppMFAttributes,
__in UINT32 cInitialSize //存储的属性的初始元素数
);
创建一个新属性集(Attributes),属性集名字为MY_ATTRIBUTE
extern const GUID MY_ATTRIBUTE;
HRESULT ShowCreateAttributeStore(IMFAttributes **ppAttributes)
{
IMFAttributes *pAttributes = NULL;
const UINT32 cElements = 10; // 初始为10个元素
// Create the empty attribute store.
HRESULT hr = MFCreateAttributes(&pAttributes, cElements);
//给key为MY_ATTRIBUTE的属性设置value.,value是字符串类型
if (SUCCEEDED(hr))
{
hr = pAttributes->SetString(
MY_ATTRIBUTE,
L"This is a string value"
);
}
// Return the IMFAttributes pointer to the caller.
if (SUCCEEDED(hr))
{
*ppAttributes = pAttributes;
(*ppAttributes)->AddRef();//注意:增加引用
}
SAFE_RELEASE(pAttributes);
return hr;
}
获取设置的属性的value
HRESULT ShowGetAttributes()
{
IMFAttributes *pAttributes = NULL;
WCHAR *pwszValue = NULL;
UINT32 cchLength = 0;
// Create the attribute store.
HRESULT hr = ShowCreateAttributeStore(&pAttributes);
// Get the attribute.
if (SUCCEEDED(hr))
{
hr = pAttributes->GetAllocatedString(
MY_ATTRIBUTE,//key
&pwszValue,//获取key对应的value,value是字符串类型
&cchLength//获取key对应的value的长度
);
}
CoTaskMemFree(pwszValue);
SAFE_RELEASE(pAttributes);
return hr;
}
可通过两种方式对存储的属性序列化:
ü 将属性集写入字节数组
ü 将属性集通过IStream写入流(Stream)
如果属性的value是IUnknown类型,即对应类型枚举值中的MF_ATTRIBUTE_IUNKNOWN,将不能被序列化。
Operation |
Byte Array |
IStream |
Save |
MFGetAttributesAsBlob |
MFSerializeAttributesToStream |
Load |
MFInitAttributesFromBlob |
MFDeserializeAttributesFromStream |
HRESULT MFGetAttributesAsBlob(
__in IMFAttributes *pAttributes,
__out UINT8 *pBuf,//存储属性数据的数组
__in UINT cbBufSize//pBuf数组的字节长度
);
HRESULT MFSerializeAttributesToStream(
IMFAttributes *pAttr,
DWORD dwOptions,//如果被设置为MF_ATTRIBUTE_SERIALIZE_UNKNOWN_BYREF ,根据pstm的情况,决定如何序列化
IStream *pStm//存储流的指针
);
一个特性也是一个key/value对。
Key是PROPERTYKEY结构体,该结构体由“GUID+DWORD型的值”组成
typedef struct {
GUID fmtid;
DWORD pid;//可设置从2往上的值,0、1被系统保留
} PROPERTYKEY;
设置和获取一个Media Foundation的对象的特性(Properties),要用到IPropertyStore接口。IPropertyStore接口中定义了一些方法:
Commit |
Saves a property change. |
GetAt |
Gets a property key from an item's array of properties. |
GetCount |
Gets the number of properties attached to the file. |
GetValue |
Gets data for a specific property. |
SetValue |
Sets a new property value, or replaces or removes an existing value. |
媒体类型通过IMFMediaType接口描述,IMFMediaType继承自IMFAttribute,所以Media Type就是一些属性(Attribute)
Method |
Description |
FreeRepresentation |
Frees memory that was allocated by the GetRepresentation method. |
GetMajorType |
Retrieves the major type of the format.
|
GetRepresentation |
Retrieves an alternative representation of the media type.目前只能将Media Foundation中的media type转换为Directshow中的AM_MEDIA_TYPE |
IsCompressedFormat |
Queries whether the media type is a compressed format. |
IsEqual |
Compares two media types and determines whether they are identical. |
Directshow、DMO里面都有媒体类型,Media Foundation中的媒体类型相比它们有以下优点,优点源于Media Foundation的媒体类型是属性(Attribute):
1. 可以省略那些不清楚的音视频参数。而Directshow、DMO的媒体类型的各个字段都必须被填写,而造成写错。
2. 扩展安全。以前的媒体类型都基于结构体(structure),如WAVEFORMATEXTENSIBLE是从WAVEFORMATEX扩展而来,指向结构体的指针指来指去就可能会造成异常发生。
MFCreateMediaType用于创建媒体类型(Media Type)。
GUID |
Description |
MFMediaType_Audio |
Audio. |
MFMediaType_Video |
Video. |
MFMediaType_Protected |
Protected media. |
MFMediaType_SAMI |
Synchronized Accessible Media Interchange (SAMI) captions. |
MFMediaType_Script |
Script stream. |
MFMediaType_Image |
Image stream. |
MFMediaType_HTML |
HTML stream. |
MFMediaType_Binary |
Binary stream. |
MFMediaType_FileTransfer |
A stream that contains data files. |
These GUIDs have the form XXXXXXXX-0000-0010-8000-00AA00389B71
, where XXXXXXXX
is the 4-byte FOURCC code or D3DFORMAT value. 根据Fourcc来生成GUID!
If a video format has an associated FOURCC or D3DFORMAT value, you can create the corresponding subtype GUID as follows: Start with the constant MFVideoFormat_Base and replace the first DWORD of the GUID with the video FOURCC or the D3DFORMAT value. You can use the DEFINE_MEDIATYPE_GUID Macro for this purpose.
void DEFINE_MEDIATYPE_GUID(
name,
format
);
举例:
#include
// Declares a GUID named MFVideoFormat_ABCD_Format.
DEFINE_MEDIATYPE_GUID( MFVideoFormat_ABCD_Format, FCC('ABCD') );
Note :Directshow也可以用这种方式生成GUID,但是不压缩的RGB格式除外。但是Media Foundation中的不压缩RGB格式可以用这种方式生成。
The following audio subtype GUIDs are defined. To specify the subtype, set the MF_MT_SUBTYPE attribute on the media type.
GUID |
Description |
MFAudioFormat_Dolby_AC3_SPDIF |
Dolby AC-3 audio over Sony/Philips Digital Interface (S/PDIF). |
MFAudioFormat_DRM |
Uncompressed but encrypted audio data used with secure audio path. |
MFAudioFormat_DTS |
Digital Theater Systems (DTS) audio. |
MFAudioFormat_Float |
Uncompressed IEEE floating-point audio. |
MFAudioFormat_MP3 |
MPEG Audio Layer-3 (MP3). |
MFAudioFormat_MPEG |
MPEG-1 audio payload. |
MFAudioFormat_MSP1 |
Windows Media Audio 9 Voice codec. |
MFAudioFormat_PCM |
Uncompressed PCM audio. |
MFAudioFormat_WMASPDIF |
Windows Media Audio 9 Professional codec over S/PDIF. |
MFAudioFormat_WMAudio_Lossless |
Windows Media Audio 9 Lossless codec or Windows Media Audio 9.1 codec. |
MFAudioFormat_WMAudioV8 |
Windows Media Audio 8 codec, Windows Media Audio 9 codec, or Windows Media Audio 9.1 codec. |
MFAudioFormat_WMAudioV9 |
Windows Media Audio 9 Professional codec or Windows Media Audio 9.1 Professional codec. |
创建音频的subtype,要用MFAudioFormat_Base开头,再加上fourcc。这是规范写法。
下面这些属性是必须设置的:
Attribute |
Description |
MF_MT_MAJOR_TYPE |
Major type. Set to MFMediaType_Audio. |
MF_MT_SUBTYPE |
Subtype. See Audio Subtype GUIDs. |
MF_MT_AUDIO_NUM_CHANNELS |
Number of audio channels |
MF_MT_AUDIO_SAMPLES_PER_SECOND |
Number of audio samples per second |
MF_MT_AUDIO_BLOCK_ALIGNMENT |
Block alignment 音频中最小的处理单位。 例如PCM,该值应为:声道数*每个sample的字节数 |
MF_MT_AUDIO_AVG_BYTES_PER_SECOND |
Average number of bytes per second 码率 |
MF_MT_AUDIO_BITS_PER_SAMPLE |
Number of bits per audio sample 采样位数 |
MF_MT_ALL_SAMPLES_INDEPENDENT |
Specifies whether each audio sample is independent. Set to TRUE for MFAudioFormat_PCM and MFAudioFormat_Float formats. 设置每个音频sample是否是时间相关。 |
创建音频的subtype,要用MFVideoFormat_Base开头,再加上fourcc。这是规范写法。
不压缩的RGB格式
GUID |
Description |
MFVideoFormat_ARGB32 |
RGB, 32 bits per pixel (bpp) with alpha channel. |
MFVideoFormat_RGB24 |
RGB, 24 bpp. |
MFVideoFormat_RGB32 |
RGB, 32 bpp. |
MFVideoFormat_RGB555 |
RGB 555, 16 bpp. (Same memory layout as D3DFMT_X1R5G5B5.) |
MFVideoFormat_RGB565 |
RGB 565, 16 bpp. (Same memory layout as D3DFMT_R5G6B5.) |
Note 这些子类型和以前的不同,比如和Directshow中的子类型
YUV Formats: 8-Bit and Palettized
GUID |
Format |
Sampling |
Packed or planar |
Bits per channel |
MFVideoFormat_AI44 |
AI44 |
4:4:4 |
Packed |
Palettized |
MFVideoFormat_AYUV |
AYUV |
4:4:4 |
Packed |
8 |
MFVideoFormat_NV11 |
NV11 |
4:1:1 |
Planar |
8 |
MFVideoFormat_NV12 |
NV12 |
4:2:0 |
Planar |
8 |
MFVideoFormat_UYVY |
UYVY |
4:2:2 |
Packed |
8 |
MFVideoFormat_YUY2 |
YUY2 |
4:2:2 |
Packed |
8 |
MFVideoFormat_YV12 |
YV12 |
4:2:0 |
Planar |
8 |
Most of these formats are described in detail in the following MSDN article: Video Rendering with 8-Bit YUV Formats. Two others are described here:
· AI44 is a palettized YUV format with 8 bits per sample. Each sample contains an index in the 4 most significant bits (MSBs) and an alpha value in the 4 least significant bits (LSBs). The index refers to an array of YUV palette entries, which must be defined in the media type for the format.
· NV11 is a 4:1:1 planar format with 12 bits per pixel. The Y samples appear first in memory. The Y plane is followed by an array of packed U (Cb) and V (Cr) samples. When the combined U-V array is addressed as an array of little-endian WORD values, the U samples are contained in the LSBs of each WORD, and the V samples are contained in the MSBs. (This memory layout is similar to NV12 although the chroma sampling is different.)
YUV Formats: 10-Bit and 16-Bit
GUID |
Format |
Sampling |
Packed or planar |
Bits per channel |
MFVideoFormat_IYUV |
IYUV |
4:2:0 |
Planar |
16 |
MFVideoFormat_P010 |
P010 |
4:2:0 |
Planar |
10 |
MFVideoFormat_P016 |
P016 |
4:2:0 |
Planar |
16 |
MFVideoFormat_P210 |
P210 |
4:2:2 |
Planar |
10 |
MFVideoFormat_P216 |
P216 |
4:2:2 |
Planar |
16 |
MFVideoFormat_v210 |
v210 |
4:2:2 |
Packed |
10 |
MFVideoFormat_v410 |
v40 |
4:4:4 |
Packed |
10 |
MFVideoFormat_Y210 |
Y210 |
4:2:2 |
Packed |
10 |
MFVideoFormat_Y216 |
Y216 |
4:2:2 |
Packed |
16 |
MFVideoFormat_Y410 |
Y40 |
4:4:4 |
Packed |
10 |
MFVideoFormat_Y416 |
Y416 |
4:4:4 |
Packed |
16 |
For more information about these formats, see 10-bit and 16-bit YUV Video Formats.
压缩视频类型
GUID |
Description |
MFVideoFormat_DV25 |
DVCPRO 25 (525-60 or 625-50). |
MFVideoFormat_DV50 |
DVCPRO 50 (525-60 or 625-50). |
MFVideoFormat_DVH1 |
DVCPRO 100 (1080/60i, 1080/50i, or 720/60P). |
MFVideoFormat_DVSD |
SDL-DVCR (525-60 or 625-50). |
MFVideoFormat_DVSL |
SD-DVCR (525-60 or 625-50). |
MFVideoFormat_MP43 |
Microsoft MPEG 4 codec version 3. This codec is no longer supported. |
MFVideoFormat_MP4S |
ISO MPEG 4 codec version 1. |
MFVideoFormat_MPEG2 |
MPEG-2 video. (Equivalent to MEDIASUBTYPE_MPEG2_VIDEO in DirectShow.) |
MFVideoFormat_MPG1 |
MPEG-1 video. |
MFVideoFormat_MSS1 |
Windows Media Screen codec version 1. |
MFVideoFormat_MSS2 |
Windows Media Video 9 Screen codec. |
MFVideoFormat_WMV1 |
Windows Media Video codec version 7. |
MFVideoFormat_WMV2 |
Windows Media Video 8 codec. |
MFVideoFormat_WMV3 |
Windows Media Video 9 codec. |
请注意每个的声明
#define SAFE_RELEASE(x) { if (x != NULL) { x->Release(); x = NULL; } }
#define SAFE_DELETE(x) { if (x != NULL) { delete x; x = NULL; } }
#define SAFE_ARRAY_DELETE(x) { if (x != NULL) { delete [] x; x = NULL; } }
#define CHECK_HR(hr) { if (FAILED(hr)) { goto done; } }
HRESULT Example()
{
// Declare variables.
HRESULT hr = S_OK;
IUnknown *pUnk = NULL; // Initialize pointers to NULL.
// Do work.
CHECK_HR(hr = CreateAnObject(&pUnk));
CHECK_HR(hr = UseTheObject(pUnk));
done:
// Control always jumps to this point on failure.
// Clean up by releasing resources.
SAFE_RELEASE(pUnk);
return hr;
}
====================================
作者:王琦 [email protected]
以上内容为原创,请尊重原创,如转载,请告知作者并注明作者!