Ejemplo n.º 1
0
        /// <summary>
        /// Check Major type and Subtype
        /// </summary>
        /// <param name="pmt">IMFMediaType to check</param>
        /// <param name="gMajorType">MajorType to check for.</param>
        /// <param name="gSubtype">SubType to check for.</param>
        /// <returns>S_Ok if match, else MF_E_INVALIDTYPE.</returns>
        protected static HResult CheckMediaType(IMFMediaType pmt, Guid gMajorType, Guid gSubtype)
        {
            Guid major_type;

            // Major type must be video.
            HResult hr = HResult.S_OK;
            MFError throwonhr;

            throwonhr = pmt.GetGUID(MFAttributesClsid.MF_MT_MAJOR_TYPE, out major_type);

            if (major_type == gMajorType)
            {
                Guid subtype;

                // Get the subtype GUID.
                throwonhr = pmt.GetGUID(MFAttributesClsid.MF_MT_SUBTYPE, out subtype);

                if (subtype != gSubtype)
                {
                    hr = HResult.MF_E_INVALIDTYPE;
                }
            }
            else
            {
                hr = HResult.MF_E_INVALIDTYPE;
            }

            return(hr);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Check Major type and Subtype
        /// </summary>
        /// <param name="pmt">IMFMediaType to check</param>
        /// <param name="gMajorType">MajorType to check for.</param>
        /// <param name="gSubtypes">Array of subTypes to check for.</param>
        /// <returns>S_Ok if match, else MF_E_INVALIDTYPE.</returns>
        protected static HResult CheckMediaType(IMFMediaType pmt, Guid gMajorType, Guid[] gSubTypes)
        {
            Guid major_type;

            // Major type must be video.
            HResult hr = HResult.S_OK;
            MFError throwonhr;

            throwonhr = pmt.GetGUID(MFAttributesClsid.MF_MT_MAJOR_TYPE, out major_type);

            if (major_type == gMajorType)
            {
                Guid subtype;

                // Get the subtype GUID.
                throwonhr = pmt.GetGUID(MFAttributesClsid.MF_MT_SUBTYPE, out subtype);

                // Look for the subtype in our list of accepted types.
                hr = HResult.MF_E_INVALIDTYPE;
                for (int i = 0; i < gSubTypes.Length; i++)
                {
                    if (subtype == gSubTypes[i])
                    {
                        hr = HResult.S_OK;
                        break;
                    }
                }
            }
            else
            {
                hr = HResult.MF_E_INVALIDTYPE;
            }

            return(hr);
        }
        public HResult IsMediaTypeSupported(IMFMediaType pMediaType, IntPtr ppMediaType)
        {
            Debug.WriteLine("StreamSink:IsMediaTypeSupported");

            if (pMediaType == null)
            {
                return(E_INVALIDARG);
            }

            HResult hr;

            lock (this)
            {
                hr = CheckShutdown();
                if (Failed(hr))
                {
                    return(hr);
                }

                Guid MajorType = Guid.Empty;
                Guid MinorType = Guid.Empty;

                hr = pMediaType.GetGUID(MF_MT_MAJOR_TYPE, out MajorType);
                if (Failed(hr))
                {
                    return(hr);
                }

                hr = pMediaType.GetGUID(MF_MT_SUBTYPE, out MinorType);
                if (Failed(hr))
                {
                    return(hr);
                }

                if (MajorType != Video)
                {
                    return(MF_E_INVALIDTYPE);
                }

                if (MinorType != RGB32)
                {
                    return(MF_E_INVALIDTYPE);
                }

                if (ppMediaType != IntPtr.Zero)
                {
                    Marshal.WriteIntPtr(ppMediaType, IntPtr.Zero);
                }

                return(S_OK);
            }
        }
Ejemplo n.º 4
0
        /// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
        /// <summary>
        /// Gets the major media type of a IMFMediaType as a text string
        ///
        /// Adapted from
        /// https://msdn.microsoft.com/en-us/library/windows/desktop/ee663602(v=vs.85).aspx
        /// </summary>
        /// <returns>S_OK for success, nz for fail</returns>
        /// <param name="mediaTypeObj">the media type object</param>
        /// <param name="outSb">The output string</param>
        /// <history>
        ///    01 Nov 18  Cynic - Started
        /// </history>
        public static HResult GetMediaSubTypeAsText(IMFMediaType mediaTypeObj, out StringBuilder outSb)
        {
            Guid    subType;
            HResult hr;

            // we always return something here
            outSb = new StringBuilder();

            // sanity check
            if (mediaTypeObj == null)
            {
                return(HResult.E_FAIL);
            }

            // MF_MT_SUBTYPE
            // Subtype GUID which describes the basic media type, we return this as human readable text
            hr = mediaTypeObj.GetGUID(MFAttributesClsid.MF_MT_SUBTYPE, out subType);
            if (hr == HResult.S_OK)
            {
                // only report success
                outSb.Append("MF_MT_SUBTYPE=" + TantaWMFUtils.ConvertGuidToName(subType));
            }

            return(HResult.S_OK);
        }
Ejemplo n.º 5
0
        public HRESULT GetGUID(Guid guidKey, out Guid pguidValue)
        {
            var hr = _type.GetGUID(guidKey, out pguidValue);

            Trace("guid: " + guidKey.ToName() + " value: " + pguidValue.ToName() + " hr: " + hr);
            return(hr);
        }
Ejemplo n.º 6
0
        private Guid GetGuid(Guid key)
        {
            Guid value;

            mediaType.GetGUID(key, out value);
            return(value);
        }
        private void RetrieveStreamsInformation()
        {
            bool         isSelected      = false;
            IMFMediaType nativeMediaType = null;
            Guid         majorType       = Guid.Empty;

            this.selectedStreams = 0;
            this.streamsInfo.Clear();

            for (uint streamIndex = 0; true; streamIndex++)
            {
                try
                {
                    // Try to get the native media type
                    this.sourceReader.GetStreamSelection(streamIndex, out isSelected);

                    // Get the stream information and add it to the info object
                    this.streamsInfo.Add(streamIndex, new StreamInfo());

                    this.sourceReader.GetNativeMediaType(streamIndex, 0, out nativeMediaType);
                    nativeMediaType.GetGUID(new Guid(Consts.MF_MT_MAJOR_TYPE), out majorType);

                    if (majorType.Equals(new Guid(Consts.MFMediaType_Audio)))
                    {
                        this.streamsInfo[streamIndex].StreamType = StreamType.AUDIO_STREAM;
                    }
                    else if (majorType.Equals(new Guid(Consts.MFMediaType_Video)))
                    {
                        this.streamsInfo[streamIndex].StreamType = StreamType.VIDEO_STREAM;
                    }
                    else
                    {
                        this.streamsInfo[streamIndex].StreamType = StreamType.NON_AV_STREAM;
                        isSelected = false;
                        this.sourceReader.SetStreamSelection(streamIndex, isSelected);
                    }

                    this.streamsInfo[streamIndex].IsSelected = isSelected;

                    if (isSelected)
                    {
                        this.selectedStreams++;
                    }
                }
                catch (COMException ex)
                {
                    // If this error is thrown there are no more streams (0xC00D36B3 == MF_E_INVALIDSTREAMNUMBER)
                    if ((uint)ex.ErrorCode == 0xC00D36B3)
                    {
                        break;
                    }

                    throw new COMException(ex.Message, ex);
                }
            }
        }
Ejemplo n.º 8
0
        //-------------------------------------------------------------------
        // TryMediaType
        //
        // Test a proposed video format.
        //-------------------------------------------------------------------
        protected HResult TryMediaType(IMFMediaType pType)
        {
            HResult hr = HResult.S_OK;

            bool bFound = false;
            Guid subtype;

            hr = pType.GetGUID(MFAttributesClsid.MF_MT_SUBTYPE, out subtype);
            if (Failed(hr))
            {
                return(hr);
            }

            // Do we support this type directly?
            if (m_draw.IsFormatSupported(subtype))
            {
                bFound = true;
            }
            else
            {
                // Can we decode this media type to one of our supported
                // output formats?

                for (int i = 0; ; i++)
                {
                    // Get the i'th format.
                    hr = m_draw.GetFormat(i, out subtype);
                    if (Failed(hr))
                    {
                        break;
                    }

                    hr = pType.SetGUID(MFAttributesClsid.MF_MT_SUBTYPE, subtype);
                    if (Failed(hr))
                    {
                        break;
                    }

                    // Try to set this type on the source reader.
                    hr = m_pReader.SetCurrentMediaType((int)MF_SOURCE_READER.FirstVideoStream, null, pType);
                    if (Succeeded(hr))
                    {
                        bFound = true;
                        break;
                    }
                }
            }

            if (bFound)
            {
                hr = m_draw.SetVideoType(pType);
            }

            return(hr);
        }
Ejemplo n.º 9
0
        public static Guid GetGuid(this IMFMediaType input, Guid key, Guid defaultValue)
        {
            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }

            if (input.GetGUID(key, out var value).IsError)
            {
                return(defaultValue);
            }

            return(value);
        }
Ejemplo n.º 10
0
        //-----------------------------------------------------------------------------
        // GetDefaultStride
        //
        // Gets the default stride for a video frame, assuming no extra padding bytes.
        //
        //-----------------------------------------------------------------------------
        private static int GetDefaultStride(IMFMediaType pType, out int plStride)
        {
            int lStride;

            plStride = 0;

            // Try to get the default stride from the media type.
            var hr = pType.GetUINT32(MFAttributesClsid.MF_MT_DEFAULT_STRIDE, out lStride);

            if (Failed(hr))
            {
                // Attribute not set. Try to calculate the default stride.
                Guid subtype;

                var width = 0;
                // ReSharper disable once TooWideLocalVariableScope
                // ReSharper disable once RedundantAssignment
                var height = 0;

                // Get the subtype and the image size.
                hr = pType.GetGUID(MFAttributesClsid.MF_MT_SUBTYPE, out subtype);
                if (Succeeded(hr))
                {
                    hr = CProcess.MfGetAttributeSize(pType, out width, out height);
                }

                if (Succeeded(hr))
                {
                    var f = new FourCC(subtype);

                    hr = MFExtern.MFGetStrideForBitmapInfoHeader(f.ToInt32(), width, out lStride);
                }

                // Set the attribute for later reference.
                if (Succeeded(hr))
                {
                    hr = pType.SetUINT32(MFAttributesClsid.MF_MT_DEFAULT_STRIDE, lStride);
                }
            }

            if (Succeeded(hr))
            {
                plStride = lStride;
            }

            return(hr);
        }
Ejemplo n.º 11
0
        //-----------------------------------------------------------------------------
        // GetDefaultStride
        //
        // Gets the default stride for a video frame, assuming no extra padding bytes.
        //
        //-----------------------------------------------------------------------------
        private static HResult GetDefaultStride(IMFMediaType pType, out int plStride)
        {
            int lStride = 0;

            plStride = 0;

            // Try to get the default stride from the media type.
            HResult hr = pType.GetUINT32(MFAttributesClsid.MF_MT_DEFAULT_STRIDE, out lStride);

            if (Failed(hr))
            {
                // Attribute not set. Try to calculate the default stride.
                Guid subtype = Guid.Empty;

                int width  = 0;
                int height = 0;

                // Get the subtype and the image size.
                hr = pType.GetGUID(MFAttributesClsid.MF_MT_SUBTYPE, out subtype);
                if (Succeeded(hr))
                {
                    hr = MFExtern.MFGetAttributeSize(pType, MFAttributesClsid.MF_MT_FRAME_SIZE, out width, out height);
                }

                if (Succeeded(hr))
                {
                    FourCC f = new FourCC(subtype);

                    hr = MFExtern.MFGetStrideForBitmapInfoHeader(f.ToInt32(), width, out lStride);
                }

                // Set the attribute for later reference.
                if (Succeeded(hr))
                {
                    hr = pType.SetUINT32(MFAttributesClsid.MF_MT_DEFAULT_STRIDE, lStride);
                }
            }

            if (Succeeded(hr))
            {
                plStride = lStride;
            }

            return(hr);
        }
Ejemplo n.º 12
0
        protected HRESULT GetSwapChainPresentParameters(IMFMediaType pType, out PresentParameters pPP)
        {
            pPP = null;

            HRESULT hr;
            int width = 0, height = 0;
            UInt32 d3dFormat = 0;

            Guid _subtype;
            hr = (HRESULT)pType.GetGUID(MFAttributesClsid.MF_MT_SUBTYPE, out _subtype);
            hr.Assert();
            if (hr.Succeeded)
            {
                hr = MFHelper.MFGetAttribute2UINT32asUINT64(pType, MFAttributesClsid.MF_MT_FRAME_SIZE, out width, out height);
                hr.Assert();

                if (hr.Succeeded)
                {
                    FOURCC _fourcc = new FOURCC(_subtype);
                    d3dFormat = _fourcc;
                    pPP = new PresentParameters();
                    pPP.BackBufferWidth = width;
                    pPP.BackBufferHeight = height;
                    pPP.Windowed = true;
                    pPP.SwapEffect = SwapEffect.Copy;
                    pPP.BackBufferFormat = (Format)d3dFormat;
                    pPP.DeviceWindowHandle = m_VideoControl != null ? m_VideoControl.Handle : m_Device.CreationParameters.Window;
                    pPP.PresentFlags = PresentFlags.Video;
                    pPP.PresentationInterval = PresentInterval.Default;

                    if (m_Device.CreationParameters.DeviceType != DeviceType.Hardware)
                    {
                        pPP.PresentFlags = pPP.PresentFlags | PresentFlags.LockableBackBuffer;
                    }
                }
            }
            return hr;
        }
Ejemplo n.º 13
0
        HResult ConfigureSourceReader(IMFSourceReaderAsync pReader)
        {
            // The list of acceptable types.
            Guid[] subtypes =
            {
                MFMediaType.NV12,  MFMediaType.YUY2,  MFMediaType.UYVY,
                MFMediaType.RGB32, MFMediaType.RGB24, MFMediaType.IYUV
            };

            HResult hr             = HResult.S_OK;
            bool    bUseNativeType = false;

            Guid subtype;

            IMFMediaType pType = null;

            // If the source's native format matches any of the formats in
            // the list, prefer the native format.

            // Note: The camera might support multiple output formats,
            // including a range of frame dimensions. The application could
            // provide a list to the user and have the user select the
            // camera's output format. That is outside the scope of this
            // sample, however.

            hr = pReader.GetNativeMediaType(
                MF_SOURCE_READER_FIRST_VIDEO_STREAM,
                0,  // Type index
                out pType
                );

            if (Failed(hr))
            {
                goto done;
            }

            hr = pType.GetGUID(MFAttributesClsid.MF_MT_SUBTYPE, out subtype);

            if (Failed(hr))
            {
                goto done;
            }

            for (int i = 0; i < subtypes.Length; i++)
            {
                if (subtype == subtypes[i])
                {
                    hr = pReader.SetCurrentMediaType(
                        MF_SOURCE_READER_FIRST_VIDEO_STREAM,
                        null,
                        pType
                        );

                    bUseNativeType = true;
                    break;
                }
            }

            if (!bUseNativeType)
            {
                // None of the native types worked. The camera might offer
                // output of a compressed type such as MJPEG or DV.

                // Try adding a decoder.

                for (int i = 0; i < subtypes.Length; i++)
                {
                    hr = pType.SetGUID(MFAttributesClsid.MF_MT_SUBTYPE, subtypes[i]);

                    if (Failed(hr))
                    {
                        goto done;
                    }

                    hr = pReader.SetCurrentMediaType(
                        MF_SOURCE_READER_FIRST_VIDEO_STREAM,
                        null,
                        pType
                        );

                    if (Succeeded(hr))
                    {
                        break;
                    }
                }
            }

done:
            SafeRelease(pType);
            return(hr);
        }
Ejemplo n.º 14
0
        public HResult SetVideoType(IMFMediaType pType)
        {
            HResult hr = HResult.S_OK;
            Guid    subtype;
            MFRatio PAR = new MFRatio();

            // Find the video subtype.
            hr = pType.GetGUID(MFAttributesClsid.MF_MT_SUBTYPE, out subtype);
            if (Failed(hr))
            {
                goto done;
            }

            // Choose a conversion function.
            // (This also validates the format type.)

            hr = SetConversionFunction(subtype);
            if (Failed(hr))
            {
                goto done;
            }

            //
            // Get some video attributes.
            //

            // Get the frame size.
            hr = MFExtern.MFGetAttributeSize(pType, MFAttributesClsid.MF_MT_FRAME_SIZE, out m_width, out m_height);
            if (Failed(hr))
            {
                goto done;
            }

            // Get the image stride.
            hr = GetDefaultStride(pType, out m_lDefaultStride);
            if (Failed(hr))
            {
                goto done;
            }

            // Get the pixel aspect ratio. Default: Assume square pixels (1:1)
            hr = MFExtern.MFGetAttributeRatio(pType, MFAttributesClsid.MF_MT_PIXEL_ASPECT_RATIO, out PAR.Numerator, out PAR.Denominator);

            if (Succeeded(hr))
            {
                m_PixelAR = PAR;
            }
            else
            {
                m_PixelAR.Numerator = m_PixelAR.Denominator = 1;
            }

            FourCC f = new FourCC(subtype);

            m_format = (Format)f.ToInt32();

            // Create Direct3D swap chains.

            hr = CreateSwapChains();
            if (Failed(hr))
            {
                goto done;
            }

            // Update the destination rectangle for the correct
            // aspect ratio.

            UpdateDestinationRect();

done:
            if (Failed(hr))
            {
                m_format    = Format.Unknown;
                m_convertFn = null;
            }

            return(hr);
        }
Ejemplo n.º 15
0
        //-------------------------------------------------------------------
        // SetVideoType
        //
        // Set the video format.
        //-------------------------------------------------------------------
        public int SetVideoType(IMFMediaType pType)
        {
            int hr = S_Ok;
            Guid subtype;
            MFRatio PAR = new MFRatio();

            // Find the video subtype.
            hr = pType.GetGUID(MFAttributesClsid.MF_MT_SUBTYPE, out subtype);
            if (Failed(hr)) { goto done; }

            // Choose a conversion function.
            // (This also validates the format type.)

            hr = SetConversionFunction(subtype);
            if (Failed(hr)) { goto done; }

            //
            // Get some video attributes.
            //

            // Get the frame size.
            hr = MFGetAttributeSize(pType, MFAttributesClsid.MF_MT_FRAME_SIZE, out m_width, out m_height);
            if (Failed(hr)) { goto done; }

            // Get the image stride.
            hr = GetDefaultStride(pType, out m_lDefaultStride);
            if (Failed(hr)) { goto done; }

            // Get the pixel aspect ratio. Default: Assume square pixels (1:1)
            hr = MFGetAttributeRatio(pType, MFAttributesClsid.MF_MT_PIXEL_ASPECT_RATIO, out PAR.Numerator, out PAR.Denominator);

            if (Succeeded(hr))
            {
                m_PixelAR = PAR;
            }
            else
            {
                m_PixelAR.Numerator = m_PixelAR.Denominator = 1;
            }

            FourCC f = new FourCC(subtype);
            m_format = (Format)f.ToInt32();

            // Create Direct3D swap chains.

            hr = CreateSwapChains();
            if (Failed(hr)) { goto done; }

            // Update the destination rectangle for the correct
            // aspect ratio.

            UpdateDestinationRect();

            done:
            if (Failed(hr))
            {
                m_format = Format.Unknown;
                m_convertFn = null;
                m_bmpconvertFn = null;
            }

            return hr;
        }
Ejemplo n.º 16
0
        //-----------------------------------------------------------------------------
        // GetDefaultStride
        //
        // Gets the default stride for a video frame, assuming no extra padding bytes.
        //
        //-----------------------------------------------------------------------------
        private static int GetDefaultStride(IMFMediaType pType, out int plStride)
        {
            int lStride = 0;
            plStride = 0;

            // Try to get the default stride from the media type.
            int hr = pType.GetUINT32(MFAttributesClsid.MF_MT_DEFAULT_STRIDE, out lStride);

            if (Failed(hr))
            {
                // Attribute not set. Try to calculate the default stride.
                Guid subtype = Guid.Empty;

                int width = 0;
                int height = 0;

                // Get the subtype and the image size.
                hr = pType.GetGUID(MFAttributesClsid.MF_MT_SUBTYPE, out subtype);
                if (Succeeded(hr))
                {
                    hr = MFGetAttributeSize(pType, MFAttributesClsid.MF_MT_FRAME_SIZE, out width, out height);
                }

                if (Succeeded(hr))
                {
                    FourCC f = new FourCC(subtype);

                    hr = MediaFoundation.MFExtern.MFGetStrideForBitmapInfoHeader(f.ToInt32(), width, out lStride);
                }

                // Set the attribute for later reference.
                if (Succeeded(hr))
                {
                    hr = pType.SetUINT32(MFAttributesClsid.MF_MT_DEFAULT_STRIDE, lStride);
                }
            }

            if (Succeeded(hr))
            {
                plStride = lStride;
            }

            return hr;
        }
Ejemplo n.º 17
0
        override protected void OnSetInputType()
        {
            MFError throwonhr;

            m_imageWidthInPixels  = 0;
            m_imageHeightInPixels = 0;
            m_videoFOURCC         = new FourCC(0);
            m_cbImageSize         = 0;
            m_lStrideIfContiguous = 0;

            m_pTransformFn = null;

            IMFMediaType pmt = InputType;

            // type can be null to clear
            if (pmt != null)
            {
                Guid subtype;

                throwonhr = pmt.GetGUID(MFAttributesClsid.MF_MT_SUBTYPE, out subtype);

                throwonhr = MFExtern.MFGetAttributeSize(pmt, MFAttributesClsid.MF_MT_FRAME_SIZE, out m_imageWidthInPixels, out m_imageHeightInPixels);

                m_videoFOURCC = new FourCC(subtype);

                if (m_videoFOURCC == FOURCC_YUY2)
                {
                    m_pTransformFn        = Update_YUY2;
                    m_lStrideIfContiguous = m_imageWidthInPixels * 2;
                }
                else if (m_videoFOURCC == FOURCC_UYVY)
                {
                    m_pTransformFn        = Update_UYVY;
                    m_lStrideIfContiguous = m_imageWidthInPixels * 2;
                }
                else if (m_videoFOURCC == FOURCC_NV12)
                {
                    m_pTransformFn        = Update_NV12;
                    m_lStrideIfContiguous = m_imageWidthInPixels;
                }
                else
                {
                    throw new COMException("Unrecognized type", (int)HResult.E_UNEXPECTED);
                }

                // Calculate the image size (not including padding)
                int lImageSize;
                if (Succeeded(pmt.GetUINT32(MFAttributesClsid.MF_MT_SAMPLE_SIZE, out lImageSize)))
                {
                    m_cbImageSize = lImageSize;
                }
                else
                {
                    m_cbImageSize = GetImageSize(m_videoFOURCC, m_imageWidthInPixels, m_imageHeightInPixels);
                }

                int lStrideIfContiguous;
                if (Succeeded(pmt.GetUINT32(MFAttributesClsid.MF_MT_DEFAULT_STRIDE, out lStrideIfContiguous)))
                {
                    m_lStrideIfContiguous = lStrideIfContiguous;
                }

                // If the output type isn't set yet, we can pre-populate it,
                // since output must always exactly equal input.  This can
                // save a (tiny) bit of time in negotiating types.

                OnSetOutputType();
            }
            else
            {
                // Since the input must be set before the output, nulling the
                // input must also clear the output.  Note that nulling the
                // input is only valid if we are not actively streaming.

                OutputType = null;
            }
        }
Ejemplo n.º 18
0
        //-----------------------------------------------------------------------------
        // GetDefaultStride
        //
        // Gets the default stride for a video frame, assuming no extra padding bytes.
        //
        //-----------------------------------------------------------------------------
        private static int GetDefaultStride(IMFMediaType pType, out int plStride)
        {
            int lStride;
            plStride = 0;

            // Try to get the default stride from the media type.
            var hr = pType.GetUINT32(MFAttributesClsid.MF_MT_DEFAULT_STRIDE, out lStride);

            if (Failed(hr))
            {
                // Attribute not set. Try to calculate the default stride.
                Guid subtype;

                var width = 0;
                // ReSharper disable once TooWideLocalVariableScope
                // ReSharper disable once RedundantAssignment
                var height = 0;

                // Get the subtype and the image size.
                hr = pType.GetGUID(MFAttributesClsid.MF_MT_SUBTYPE, out subtype);
                if (Succeeded(hr))
                {
                    hr = CProcess.MfGetAttributeSize(pType, out width, out height);
                }

                if (Succeeded(hr))
                {
                    var f = new FourCC(subtype);

                    hr = MFExtern.MFGetStrideForBitmapInfoHeader(f.ToInt32(), width, out lStride);
                }

                // Set the attribute for later reference.
                if (Succeeded(hr))
                {
                    hr = pType.SetUINT32(MFAttributesClsid.MF_MT_DEFAULT_STRIDE, lStride);
                }
            }

            if (Succeeded(hr))
            {
                plStride = lStride;
            }

            return hr;
        }
Ejemplo n.º 19
0
        //-------------------------------------------------------------------
        // SetVideoType
        //
        // Set the video snapFormat.
        //-------------------------------------------------------------------
        public int SetVideoType(IMFMediaType pType)
        {
            Guid subtype;
            var  PAR = new MFRatio();

            // Find the video subtype.
            var hr = pType.GetGUID(MFAttributesClsid.MF_MT_SUBTYPE, out subtype);

            try
            {
                if (Failed(hr))
                {
                    throw new Exception();
                }

                // Choose a conversion function.
                // (This also validates the snapFormat type.)

                hr = SetConversionFunction(subtype);
                if (Failed(hr))
                {
                    throw new Exception();
                }

                //
                // Get some video attributes.
                //

                // Get the frame size.
                hr = CProcess.MfGetAttributeSize(pType, out width, out height);
                if (Failed(hr))
                {
                    throw new Exception();
                }

                // Get the image stride.
                hr = GetDefaultStride(pType, out lDefaultStride);
                if (Failed(hr))
                {
                    throw new Exception();
                }

                // Get the pixel aspect ratio. Default: Assume square pixels (1:1)
                hr = CProcess.MfGetAttributeRatio(pType, out PAR.Numerator, out PAR.Denominator);

                if (Succeeded(hr))
                {
                    pixelAR = PAR;
                }
                else
                {
                    pixelAR.Numerator = pixelAR.Denominator = 1;
                }

                var f = new FourCC(subtype);
                format = (Format)f.ToInt32();

                // Create Direct3D swap chains.

                hr = CreateSwapChains();
                if (Failed(hr))
                {
                    throw new Exception();
                }

                // Update the destination rectangle for the correct
                // aspect ratio.

                UpdateDestinationRect();
            }
            finally
            {
                if (Failed(hr))
                {
                    format      = Format.Unknown;
                    m_convertFn = null;
                }
            }
            return(hr);
        }
Ejemplo n.º 20
0
        /// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
        /// <summary>
        /// Gets a list of all supported video formats from a media type
        /// as a nice displayable bit of text. outSb will never be null can be
        /// empty.
        ///
        /// Adapted from
        /// https://msdn.microsoft.com/en-us/library/windows/desktop/ee663602(v=vs.85).aspx
        /// </summary>
        /// <returns>S_OK for success, nz for fail</returns>
        /// <param name="mediaTypeObj">the media type object</param>
        /// <param name="outSb">The output string</param>
        /// <history>
        ///    01 Nov 18  Cynic - Started
        /// </history>
        public static HResult GetSupportedFormatsFromMediaType(IMFMediaType mediaTypeObj, out Guid majorType, out Guid subType, out int attributeCount, out int frameSizeWidth, out int frameSizeHeight, out int frameRate, out int frameRateDenominator, out int frameRateMin, out int frameRateMinDenominator, out int frameRateMax, out int frameRateMaxDenominator)
        {
            // init these
            majorType               = Guid.Empty;
            subType                 = Guid.Empty;
            attributeCount          = 0;
            frameSizeWidth          = 0;
            frameSizeHeight         = 0;
            frameRate               = 0;
            frameRateDenominator    = 0;
            frameRateMin            = 0;
            frameRateMinDenominator = 0;
            frameRateMax            = 0;
            frameRateMaxDenominator = 0;

            // sanity check
            if (mediaTypeObj == null)
            {
                return(HResult.E_FAIL);
            }

            // Retrieves the number of attributes that are set on this object.
            HResult hr = mediaTypeObj.GetCount(out attributeCount);

            if (hr != HResult.S_OK)
            {
                // if we failed here, bail out
                return(HResult.E_FAIL);
            }
            // put in this line now
            //   outSb.Append("attributeCount=" + attributeCount.ToString()+", ");

            // MF_MT_MAJOR_TYPE
            // Major type GUID, we return this as human readable text
            hr = mediaTypeObj.GetMajorType(out majorType);
            if (hr != HResult.S_OK)
            {
                // if we failed here, bail out
                return(HResult.E_FAIL);
            }

            // MF_MT_SUBTYPE
            // Subtype GUID which describes the basic media type, we return this as human readable text
            hr = mediaTypeObj.GetGUID(MFAttributesClsid.MF_MT_SUBTYPE, out subType);
            if (hr != HResult.S_OK)
            {
                // if we failed here, bail out
                return(HResult.E_FAIL);
            }

            // MF_MT_FRAME_SIZE
            // the Width and height of a video frame, in pixels
            hr = MFExtern.MFGetAttributeSize(mediaTypeObj, MFAttributesClsid.MF_MT_FRAME_SIZE, out frameSizeWidth, out frameSizeHeight);
            if (hr != HResult.S_OK)
            {
                // if we failed here, bail out
                return(HResult.E_FAIL);
            }

            // MF_MT_FRAME_RATE
            // The frame rate is expressed as a ratio.The upper 32 bits of the attribute value contain the numerator and the lower 32 bits contain the denominator.
            // For example, if the frame rate is 30 frames per second(fps), the ratio is 30 / 1.If the frame rate is 29.97 fps, the ratio is 30,000 / 1001.
            // we report this back to the user as a decimal
            hr = MFExtern.MFGetAttributeRatio(mediaTypeObj, MFAttributesClsid.MF_MT_FRAME_RATE, out frameRate, out frameRateDenominator);
            if (hr != HResult.S_OK)
            {
                // if we failed here, bail out
                return(HResult.E_FAIL);
            }

            // MF_MT_FRAME_RATE_RANGE_MIN
            // The frame rate is expressed as a ratio.The upper 32 bits of the attribute value contain the numerator and the lower 32 bits contain the denominator.
            // For example, if the frame rate is 30 frames per second(fps), the ratio is 30 / 1.If the frame rate is 29.97 fps, the ratio is 30,000 / 1001.
            // we report this back to the user as a decimal
            hr = MFExtern.MFGetAttributeRatio(mediaTypeObj, MFAttributesClsid.MF_MT_FRAME_RATE_RANGE_MIN, out frameRateMin, out frameRateMinDenominator);
            if (hr != HResult.S_OK)
            {
                // if we failed here, bail out
                return(HResult.E_FAIL);
            }

            // MF_MT_FRAME_RATE_RANGE_MAX
            // The frame rate is expressed as a ratio.The upper 32 bits of the attribute value contain the numerator and the lower 32 bits contain the denominator.
            // For example, if the frame rate is 30 frames per second(fps), the ratio is 30 / 1.If the frame rate is 29.97 fps, the ratio is 30,000 / 1001.
            // we report this back to the user as a decimal
            hr = MFExtern.MFGetAttributeRatio(mediaTypeObj, MFAttributesClsid.MF_MT_FRAME_RATE_RANGE_MAX, out frameRateMax, out frameRateMaxDenominator);
            if (hr != HResult.S_OK)
            {
                // if we failed here, bail out
                return(HResult.E_FAIL);
            }

            return(HResult.S_OK);
        }
Ejemplo n.º 21
0
        /// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
        /// <summary>
        /// Gets a list of all supported video formats from a media type
        /// as a nice displayable bit of text. outSb will never be null can be
        /// empty.
        ///
        /// Adapted from
        /// https://msdn.microsoft.com/en-us/library/windows/desktop/ee663602(v=vs.85).aspx
        /// </summary>
        /// <returns>S_OK for success, nz for fail</returns>
        /// <param name="mediaTypeObj">the media type object</param>
        /// <param name="outSb">The output string</param>
        /// <history>
        ///    01 Nov 18  Cynic - Started
        /// </history>
        public static HResult GetSupportedFormatsFromMediaTypeAsText(IMFMediaType mediaTypeObj, out StringBuilder outSb)
        {
            Guid majorType;
            Guid subType;
            int  attributeCount;
            int  frameSizeWidth;
            int  frameSizeHeight;
            int  frameRate;
            int  frameRateDenominator;
            int  frameRateMin;
            int  frameRateMinDenominator;
            int  frameRateMax;
            int  frameRateMaxDenominator;

            // we always return something here
            outSb = new StringBuilder();

            // sanity check
            if (mediaTypeObj == null)
            {
                return(HResult.E_FAIL);
            }

            // Retrieves the number of attributes that are set on this object.
            HResult hr = mediaTypeObj.GetCount(out attributeCount);

            if (hr != HResult.S_OK)
            {
                // if we failed here, bail out
                outSb.Append("failed getting attributeCount, retVal=" + hr.ToString());
                outSb.Append("\r\n");
                return(HResult.E_FAIL);
            }
            // put in this line now
            //   outSb.Append("attributeCount=" + attributeCount.ToString()+", ");

            // MF_MT_MAJOR_TYPE
            // Major type GUID, we return this as human readable text
            hr = mediaTypeObj.GetMajorType(out majorType);
            if (hr == HResult.S_OK)
            {
                // only report success
                outSb.Append("MF_MT_MAJOR_TYPE=" + TantaWMFUtils.ConvertGuidToName(majorType) + ", ");
            }

            // MF_MT_SUBTYPE
            // Subtype GUID which describes the basic media type, we return this as human readable text
            hr = mediaTypeObj.GetGUID(MFAttributesClsid.MF_MT_SUBTYPE, out subType);
            if (hr == HResult.S_OK)
            {
                // only report success
                outSb.Append("MF_MT_SUBTYPE=" + TantaWMFUtils.ConvertGuidToName(subType) + ", ");
            }

            // MF_MT_FRAME_SIZE
            // the Width and height of a video frame, in pixels
            hr = MFExtern.MFGetAttributeSize(mediaTypeObj, MFAttributesClsid.MF_MT_FRAME_SIZE, out frameSizeWidth, out frameSizeHeight);
            if (hr == HResult.S_OK)
            {
                // only report success
                outSb.Append("MF_MT_FRAME_SIZE (W,H)=(" + frameSizeWidth.ToString() + "," + frameSizeHeight.ToString() + "), ");
            }

            // MF_MT_FRAME_RATE
            // The frame rate is expressed as a ratio.The upper 32 bits of the attribute value contain the numerator and the lower 32 bits contain the denominator.
            // For example, if the frame rate is 30 frames per second(fps), the ratio is 30 / 1.If the frame rate is 29.97 fps, the ratio is 30,000 / 1001.
            // we report this back to the user as a decimal
            hr = MFExtern.MFGetAttributeRatio(mediaTypeObj, MFAttributesClsid.MF_MT_FRAME_RATE, out frameRate, out frameRateDenominator);
            if (hr == HResult.S_OK)
            {
                // only report success
                if (frameRateDenominator < 0)
                {
                    outSb.Append("MF_MT_FRAME_RATE (frames/s)=(undefined),");
                }
                else
                {
                    outSb.Append("MF_MT_FRAME_RATE=" + ((decimal)frameRate / (decimal)frameRateDenominator).ToString() + "f/s, ");
                }
            }

            // MF_MT_FRAME_RATE_RANGE_MIN
            // The frame rate is expressed as a ratio.The upper 32 bits of the attribute value contain the numerator and the lower 32 bits contain the denominator.
            // For example, if the frame rate is 30 frames per second(fps), the ratio is 30 / 1.If the frame rate is 29.97 fps, the ratio is 30,000 / 1001.
            // we report this back to the user as a decimal
            hr = MFExtern.MFGetAttributeRatio(mediaTypeObj, MFAttributesClsid.MF_MT_FRAME_RATE_RANGE_MIN, out frameRateMin, out frameRateMinDenominator);
            if (hr == HResult.S_OK)
            {
                // only report success
                if (frameRateMinDenominator < 0)
                {
                    outSb.Append("MF_MT_FRAME_RATE_RANGE_MIN (frames/s)=(undefined),");
                }
                else
                {
                    outSb.Append("MF_MT_FRAME_RATE_RANGE_MIN=" + ((decimal)frameRateMin / (decimal)frameRateMinDenominator).ToString() + "f/s, ");
                }
            }

            // MF_MT_FRAME_RATE_RANGE_MAX
            // The frame rate is expressed as a ratio.The upper 32 bits of the attribute value contain the numerator and the lower 32 bits contain the denominator.
            // For example, if the frame rate is 30 frames per second(fps), the ratio is 30 / 1.If the frame rate is 29.97 fps, the ratio is 30,000 / 1001.
            // we report this back to the user as a decimal
            hr = MFExtern.MFGetAttributeRatio(mediaTypeObj, MFAttributesClsid.MF_MT_FRAME_RATE_RANGE_MAX, out frameRateMax, out frameRateMaxDenominator);
            if (hr == HResult.S_OK)
            {
                // only report success
                if (frameRateMaxDenominator < 0)
                {
                    outSb.Append("MF_MT_FRAME_RATE_RANGE_MAX (frames/s)=(undefined),");
                }
                else
                {
                    outSb.Append("MF_MT_FRAME_RATE_RANGE_MAX=" + ((decimal)frameRateMax / (decimal)frameRateMaxDenominator).ToString() + "f/s, ");
                }
            }

            // enumerate all of the possible Attributes so we can see which ones are present that we did not report on
            StringBuilder allAttrs = new StringBuilder();

            hr = EnumerateAllAttributeNamesInMediaTypeAsText(mediaTypeObj, attributeCount, out allAttrs);
            if (hr == HResult.S_OK)
            {
                outSb.Append("\r\n");
                outSb.Append("         AllAttrs=" + allAttrs.ToString());
            }

            return(HResult.S_OK);
        }
Ejemplo n.º 22
0
        // use IntPtr since this can be NULL
        //-------------------------------------------------------------------
        // Name: IsMediaTypeSupported
        // Description: Check if a media type is supported.
        //
        // pMediaType: The media type to check.
        // ppMediaType: Optionally, receives a "close match" media type.
        //-------------------------------------------------------------------
        public int IsMediaTypeSupported(
            /* [in] */ IMFMediaType pMediaType,
            // /* [out] */ out IMFMediaType ppMediaType)
            IntPtr ppMediaType)
        {
            TRACE("CWavStream::IsMediaTypeSupported");

            int hr;
            Guid majorType;
            WaveFormatEx pWav;
            int cbSize;

            lock (this)
            {
                CheckShutdown();

                hr = pMediaType.GetGUID(MFAttributesClsid.MF_MT_MAJOR_TYPE, out majorType);
                MFError.ThrowExceptionForHR(hr);

                // First make sure it's audio.
                if (majorType != MFMediaType.Audio)
                {
                    throw new COMException("type not audio", MFError.MF_E_INVALIDTYPE);
                }

                // Get a WAVEFORMATEX structure to validate against.
                hr = MFExtern.MFCreateWaveFormatExFromMFMediaType(pMediaType, out pWav, out cbSize, MFWaveFormatExConvertFlags.Normal);
                MFError.ThrowExceptionForHR(hr);

                // Validate the WAVEFORMATEX structure.
                ValidateWaveFormat(pWav, cbSize);

                // We don't return any "close match" types.
                if (ppMediaType != IntPtr.Zero)
                {
                    Marshal.WriteIntPtr(ppMediaType, IntPtr.Zero);
                }
            }

            //CoTaskMemFree(pWav);
            return S_Ok;
        }
Ejemplo n.º 23
0
        //-------------------------------------------------------------------
        // SetVideoType
        //
        // Set the video snapFormat.
        //-------------------------------------------------------------------
        public int SetVideoType(IMFMediaType pType)
        {
            Guid subtype;
            var PAR = new MFRatio();

            // Find the video subtype.
            var hr = pType.GetGUID(MFAttributesClsid.MF_MT_SUBTYPE, out subtype);

            try
            {
                if (Failed(hr))
                    throw new Exception();

                // Choose a conversion function.
                // (This also validates the snapFormat type.)

                hr = SetConversionFunction(subtype);
                if (Failed(hr))
                    throw new Exception();

                //
                // Get some video attributes.
                //

                // Get the frame size.
                hr = CProcess.MfGetAttributeSize(pType, out width, out height);
                if (Failed(hr))
                    throw new Exception();

                // Get the image stride.
                hr = GetDefaultStride(pType, out lDefaultStride);
                if (Failed(hr))
                    throw new Exception();

                // Get the pixel aspect ratio. Default: Assume square pixels (1:1)
                hr = CProcess.MfGetAttributeRatio(pType, out PAR.Numerator, out PAR.Denominator);

                if (Succeeded(hr))
                {
                    pixelAR = PAR;
                }
                else
                {
                    pixelAR.Numerator = pixelAR.Denominator = 1;
                }

                var f = new FourCC(subtype);
                format = (Format) f.ToInt32();

                // Create Direct3D swap chains.

                hr = CreateSwapChains();
                if (Failed(hr))
                    throw new Exception();

                // Update the destination rectangle for the correct
                // aspect ratio.

                UpdateDestinationRect();

            }
            finally
            {
                if (Failed(hr))
                {
                    format = Format.Unknown;
                    m_convertFn = null;
                }
            }
            return hr;
        }
Ejemplo n.º 24
0
        //-------------------------------------------------------------------
        // TryMediaType
        //
        // Test a proposed video format.
        //-------------------------------------------------------------------
        protected int TryMediaType(IMFMediaType pType)
        {
            int hr = S_Ok;

            bool bFound = false;
            Guid subtype;

            hr = pType.GetGUID(MFAttributesClsid.MF_MT_SUBTYPE, out subtype);
            if (Failed(hr))
            {
                return hr;
            }

            // Do we support this type directly?
            if (m_draw.IsFormatSupported(subtype))
            {
                bFound = true;
            }
            else
            {
                // Can we decode this media type to one of our supported
                // output formats?

                for (int i = 0; ; i++)
                {
                    // Get the i'th format.
                    hr = m_draw.GetFormat(i, out subtype);
                    if (Failed(hr)) { break; }

                    hr = pType.SetGUID(MFAttributesClsid.MF_MT_SUBTYPE, subtype);
                    if (Failed(hr)) { break; }

                    // Try to set this type on the source reader.
                    hr = m_pReader.SetCurrentMediaType((int)MF_SOURCE_READER.FirstVideoStream, IntPtr.Zero, pType);
                    if (Succeeded(hr))
                    {
                        bFound = true;
                        break;
                    }
                }
            }

            if (bFound)
            {
                hr = m_draw.SetVideoType(pType);
            }

            return hr;
        }
Ejemplo n.º 25
0
        /// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
        /// <summary>
        ///  Called when the input type gets set. We record the basic image
        ///  size and format information here.
        ///
        ///  Expects the InputType variable to have been set. This will have been
        ///  done in the base class immediately before this routine gets called
        ///
        ///  An override of the virtual stub in TantaMFTBase_Sync.
        /// </summary>
        /// <history>
        ///    01 Nov 18  Cynic - Ported In
        /// </history>
        override protected void OnSetInputType()
        {
            int     lImageSize;
            int     lStrideIfContiguous;
            Guid    subtype;
            HResult hr;

            // init some things
            m_imageWidthInPixels   = 0;
            m_imageHeightInPixels  = 0;
            m_videoFOURCC          = new FourCC(0);
            m_cbImageSize          = 0;
            m_lStrideIfContiguous  = 0;
            TransformImageFunction = null;

            // get this now, the type can be null to clear
            IMFMediaType pmt = InputType;

            if (pmt == null)
            {
                // Since the input must be set before the output, nulling the
                // input must also clear the output.  Note that nulling the
                // input is only valid if we are not actively streaming.
                OutputType = null;
                return;
            }

            // get the subtype GUID
            hr = pmt.GetGUID(MFAttributesClsid.MF_MT_SUBTYPE, out subtype);
            if (hr != HResult.S_OK)
            {
                throw new Exception("OnSetInputType call to get the subtype guid failed. Err=" + hr.ToString());
            }

            // get the image width and height in pixels. These will become
            // very important later when the time comes to convert to grey scale
            // Note that changing the size of the image on the screen, by resizing
            // the EVR control does not change this image size. The EVR will
            // remove various rows and columns as necssary for display purposes
            hr = MFExtern.MFGetAttributeSize(pmt, MFAttributesClsid.MF_MT_FRAME_SIZE, out m_imageWidthInPixels, out m_imageHeightInPixels);
            if (hr != HResult.S_OK)
            {
                throw new Exception("OnSetInputType call to get the image size failed failed. Err=" + hr.ToString());
            }

            // create a working FourCC subtype from the Guid. This only works because we can always
            // figure out the FOURCC code from the subtype Guid - it is the first four hex digits in
            // reverse order
            m_videoFOURCC = new FourCC(subtype);

            // switch based on the supported formats and set our transform function
            // and other format specific based info
            if (m_videoFOURCC == FOURCC_YUY2)
            {
                TransformImageFunction = TransformImageOfTypeYUY2;
                m_lStrideIfContiguous  = m_imageWidthInPixels * 2;
            }
            else if (m_videoFOURCC == FOURCC_UYVY)
            {
                TransformImageFunction = TransformImageOfTypeUYVY;
                m_lStrideIfContiguous  = m_imageWidthInPixels * 2;
            }
            else if (m_videoFOURCC == FOURCC_NV12)
            {
                TransformImageFunction = TransformImageOfTypeNV12;
                m_lStrideIfContiguous  = m_imageWidthInPixels;
            }
            else
            {
                throw new Exception("OnSetInputType Unrecognized type. Type =" + m_videoFOURCC.ToString());
            }

            // Calculate the image size (not including padding)
            hr = pmt.GetUINT32(MFAttributesClsid.MF_MT_SAMPLE_SIZE, out lImageSize);
            if (hr == HResult.S_OK)
            {
                // we have an attribute with the image size use that
                m_cbImageSize = lImageSize;
            }
            else
            {
                // we calculate the image size from the format
                m_cbImageSize = GetImageSize(m_videoFOURCC, m_imageWidthInPixels, m_imageHeightInPixels);
            }

            // get the default stride. The stride is the number of bytes from one row of pixels in memory
            // to the next row of pixels in memory. If padding bytes are present, the stride is wider
            // than the width of the image. We will need this if the frames come in IMFMediaBuffers,
            // if the frames come in IMF2DBuffers we can get the stride directly from the buffer
            hr = pmt.GetUINT32(MFAttributesClsid.MF_MT_DEFAULT_STRIDE, out lStrideIfContiguous);
            if (hr != HResult.S_OK)
            {
                throw new Exception("OnSetInputType call to MFAttributesClsid.MF_MT_DEFAULT_STRIDE failed. Err=" + hr.ToString());
            }

            // we have an attribute with the default stride
            m_lStrideIfContiguous = lStrideIfContiguous;

            // If the output type isn't set yet, we can pre-populate it,
            // since output must always exactly equal input.  This can
            // save a (tiny) bit of time in negotiating types.

            OnSetOutputType();
        }