private void CreateH264Decoder() { HResult hr; // create H.264 decoder var comobject = new ResamplerMediaComObject(); decodertransform = (IMFTransform)comobject; // setup input media type for decoder MFExtern.MFCreateMediaType(out decinputmediatype); // setup media type manualy IMFMediaType testdecinputmediatype, testdecoutputmediatype; decinputmediatype.SetGUID(MFAttributesClsid.MF_MT_MAJOR_TYPE, MFMediaType.Video); decinputmediatype.SetGUID(MFAttributesClsid.MF_MT_SUBTYPE, MFMediaType.H264); decinputmediatype.SetUINT32(MFAttributesClsid.MF_MT_INTERLACE_MODE, (int)MFVideoInterlaceMode.Progressive); MFExtern.MFSetAttributeSize(decinputmediatype, MFAttributesClsid.MF_MT_FRAME_SIZE, VIDEO_SAMPLE_WIDTH, VIDEO_SAMPLE_HEIGHT); uint fixedSampleSize = VIDEO_SAMPLE_WIDTH * (16 * ((VIDEO_SAMPLE_HEIGHT + 15) / 16)) + VIDEO_SAMPLE_WIDTH * (VIDEO_SAMPLE_HEIGHT / 2);//for Y, U and V decinputmediatype.SetUINT32(MFAttributesClsid.MF_MT_SAMPLE_SIZE, fixedSampleSize); decinputmediatype.SetUINT32(MFAttributesClsid.MF_MT_DEFAULT_STRIDE, VIDEO_SAMPLE_WIDTH); decinputmediatype.SetUINT32(MFAttributesClsid.MF_MT_FIXED_SIZE_SAMPLES, 1); decinputmediatype.SetUINT32(MFAttributesClsid.MF_MT_ALL_SAMPLES_INDEPENDENT, 1); MFExtern.MFSetAttributeRatio(decinputmediatype, MFAttributesClsid.MF_MT_PIXEL_ASPECT_RATIO, 1, 1); hr = decodertransform.SetInputType(0, decinputmediatype, 0); decodertransform.GetInputAvailableType(0, 0, out testdecinputmediatype); // setup media type for output of decoder MFExtern.MFCreateMediaType(out decoutputmediatype); decoutputmediatype.SetGUID(MFAttributesClsid.MF_MT_MAJOR_TYPE, MFMediaType.Video); decoutputmediatype.SetGUID(MFAttributesClsid.MF_MT_SUBTYPE, MFMediaType.IYUV); MFExtern.MFSetAttributeSize(decoutputmediatype, MFAttributesClsid.MF_MT_FRAME_SIZE, VIDEO_SAMPLE_WIDTH, VIDEO_SAMPLE_HEIGHT); MFExtern.MFSetAttributeRatio(decoutputmediatype, MFAttributesClsid.MF_MT_FRAME_RATE, 30, 1); MFExtern.MFSetAttributeRatio(decoutputmediatype, MFAttributesClsid.MF_MT_PIXEL_ASPECT_RATIO, 1, 1); decoutputmediatype.SetUINT32(MFAttributesClsid.MF_MT_INTERLACE_MODE, 2); hr = decodertransform.SetOutputType(0, decoutputmediatype, 0); decodertransform.GetOutputAvailableType(0, 0, out testdecoutputmediatype); decodertransform.GetInputStatus(0, out mftStatus); if (mftStatus != MFTInputStatusFlags.AcceptData) { Debug.WriteLine("DECODER NOT ACCEPT INPUT DATA"); return; } else { Debug.WriteLine("PROCESS INPUT DONE>>>> " + mftStatus); } decodertransform.ProcessMessage(MFTMessageType.CommandFlush, (IntPtr)null); decodertransform.ProcessMessage(MFTMessageType.NotifyBeginStreaming, (IntPtr)null); decodertransform.ProcessMessage(MFTMessageType.NotifyStartOfStream, (IntPtr)null); }
public void initialize(int streamId, IMFMediaType mediaType) { //process input. ThrowIfError(MFExtern.MFGetAttributeSize(mediaType, MFAttributesClsid.MF_MT_FRAME_SIZE, out _videoWitdh, out _videoHeight)); ThrowIfError(MFExtern.MFGetAttributeRatio(mediaType, MFAttributesClsid.MF_MT_PIXEL_ASPECT_RATIO, out _videoRatioN, out _videoRatioD)); ThrowIfError(pDecoderTransform.SetInputType(streamId, mediaType, MFTSetTypeFlags.None)); //process output IMFMediaType h264OutputType; ThrowIfError(MFExtern.MFCreateMediaType(out h264OutputType)); ThrowIfError(h264OutputType.SetGUID(MF_MT_MAJOR_TYPE, MFMediaType.Video)); ThrowIfError(h264OutputType.SetGUID(MF_MT_SUBTYPE, MFMediaType.YUY2)); IMFAttributes attr = h264OutputType as IMFAttributes; ThrowIfError(MFExtern.MFSetAttributeSize(attr, MF_MT_FRAME_SIZE, _videoWitdh, _videoHeight)); ThrowIfError(MFExtern.MFSetAttributeRatio(attr, MF_MT_FRAME_RATE, 30, 1)); ThrowIfError(MFExtern.MFSetAttributeRatio(attr, MF_MT_PIXEL_ASPECT_RATIO, _videoRatioN, _videoRatioD)); ThrowIfError(attr.SetUINT32(MF_MT_INTERLACE_MODE, 2)); ThrowIfError(pDecoderTransform.SetOutputType(streamId, h264OutputType, MFTSetTypeFlags.None)); MFTInputStatusFlags mftStatus; ThrowIfError(pDecoderTransform.GetInputStatus(streamId, out mftStatus)); if (MFTInputStatusFlags.AcceptData != mftStatus) { throw new Exception("H.264 decoder MFT is not accepting data.\n"); } ThrowIfError(pDecoderTransform.ProcessMessage(MFTMessageType.CommandFlush, IntPtr.Zero)); ThrowIfError(pDecoderTransform.ProcessMessage(MFTMessageType.NotifyBeginStreaming, IntPtr.Zero)); ThrowIfError(pDecoderTransform.ProcessMessage(MFTMessageType.NotifyStartOfStream, IntPtr.Zero)); ThrowIfError(MFExtern.MFCreateSample(out _mftOutSample)); _mftOutBufferContainer = new MFTOutputDataBuffer[1]; //todo:set the stream id again when receive media stream later. _mftOutBufferContainer[0].dwStreamID = streamId; _mftOutBufferContainer[0].dwStatus = 0; _mftOutBufferContainer[0].pEvents = null; _mftOutBufferContainer[0].pSample = Marshal.GetIUnknownForObject(_mftOutSample); _videoStreamId = streamId; _streamMediaType = mediaType; }