private void GetInterface()
        {
            IMFASFMutualExclusion me;
            IMFASFProfile prof;
            IMFMediaType mt;
            IMFASFStreamConfig pStream;
            FourCC cc4 = new FourCC("YUY2");

            int hr = MFExtern.MFCreateASFProfile(out prof);
            MFError.ThrowExceptionForHR(hr);

            hr = prof.CreateMutualExclusion(out me);
            MFError.ThrowExceptionForHR(hr);
            hr = prof.AddMutualExclusion(me);
            MFError.ThrowExceptionForHR(hr);

            hr = MFExtern.MFCreateMediaType(out mt);
            MFError.ThrowExceptionForHR(hr);

            hr = mt.SetGUID(MFAttributesClsid.MF_MT_SUBTYPE, cc4.ToMediaSubtype());
            MFError.ThrowExceptionForHR(hr);
            hr = mt.SetGUID(MFAttributesClsid.MF_MT_MAJOR_TYPE, MFMediaType.Video);
            MFError.ThrowExceptionForHR(hr);

            hr = prof.CreateStream(mt, out pStream);
            MFError.ThrowExceptionForHR(hr);

            hr = prof.SetStream(pStream);
            MFError.ThrowExceptionForHR(hr);

            hr = MFExtern.MFCreateASFStreamSelector(prof, out m_ss);
            MFError.ThrowExceptionForHR(hr);
        }
Esempio n. 2
0
        private void TestCreateStream()
        {
            short s;
            IMFMediaType mt;
            IMFASFStreamConfig pStream, pStream2, pStream3;
            FourCC cc4 = new FourCC("YUY2");

            int hr = MFExtern.MFCreateMediaType(out mt);
            MFError.ThrowExceptionForHR(hr);

            hr = mt.SetGUID(MFAttributesClsid.MF_MT_SUBTYPE, cc4.ToMediaSubtype());
            MFError.ThrowExceptionForHR(hr);
            hr = mt.SetGUID(MFAttributesClsid.MF_MT_MAJOR_TYPE, MFMediaType.Video);
            MFError.ThrowExceptionForHR(hr);

            hr = m_p.CreateStream(mt, out pStream);
            MFError.ThrowExceptionForHR(hr);

            Debug.Assert(pStream != null);

            hr = m_p.SetStream(pStream);
            MFError.ThrowExceptionForHR(hr);

            int i, i2;
            hr = m_p.GetStreamCount(out i);
            MFError.ThrowExceptionForHR(hr);

            Debug.Assert(i == 1);

            hr = m_p.GetStream(0, out s, out pStream2);
            MFError.ThrowExceptionForHR(hr);

            Debug.Assert(pStream2 != null);

            hr = m_p.GetStreamByNumber(0, out pStream3);
            MFError.ThrowExceptionForHR(hr);
            Debug.Assert(pStream2 == pStream3);

            IMFASFProfile p2;

            hr = m_p.Clone(out p2);
            MFError.ThrowExceptionForHR(hr);

            Debug.Assert(p2 != null);
            hr = p2.GetStreamCount(out i);
            MFError.ThrowExceptionForHR(hr);
            hr = p2.RemoveStream(0);
            MFError.ThrowExceptionForHR(hr);
            hr = p2.GetStreamCount(out i2);
            MFError.ThrowExceptionForHR(hr);

            Debug.Assert(i2 == i - 1);
        }
Esempio n. 3
0
        /// <summary>
        /// CRiffParser constructor
        /// </summary>
        /// <param name="pStream">Stream to read from RIFF file</param>
        /// <param name="id">FOURCC of the RIFF container. Should be 'RIFF' or 'LIST'.</param>
        /// <param name="cbStartOfContainer">Start of the container, as an offset into the stream.</param>
        public CRiffParser(IMFByteStream pStream, FourCC id, long cbStartOfContainer)
        {
            m_chunk = new CRiffChunk();
            m_fccID = id;
            m_llContainerOffset = cbStartOfContainer;

            if (pStream == null)
            {
                throw new COMException("invalid IMFByteStream", E_Pointer);
            }
            else
            {
                m_pStream = pStream;

                ReadRiffHeader();
            }
        }
        private void GetInterface()
        {
            FourCC cc4 = new FourCC("YUY2");
            IMFMediaType mt;

            IMFASFProfile ap;

            int hr = MFExtern.MFCreateASFProfile(out ap);
            MFError.ThrowExceptionForHR(hr);
            hr = MFExtern.MFCreateMediaType(out mt);
            MFError.ThrowExceptionForHR(hr);

            hr = mt.SetGUID(MFAttributesClsid.MF_MT_SUBTYPE, cc4.ToMediaSubtype());
            MFError.ThrowExceptionForHR(hr);
            hr = mt.SetGUID(MFAttributesClsid.MF_MT_MAJOR_TYPE, MFMediaType.Video);
            MFError.ThrowExceptionForHR(hr);

            hr = ap.CreateStream(mt, out m_sc);
            MFError.ThrowExceptionForHR(hr);
        }
        private void Alt()
        {
            FourCC cc4 = new FourCC("YUY2");
            IMFASFProfile ap;
            IMFASFStreamPrioritization sp;
            IMFASFStreamSelector ss;
            IMFMediaType mt;
            IMFASFStreamConfig pStream;

            int hr = MFExtern.MFCreateASFProfile(out ap);
            MFError.ThrowExceptionForHR(hr);
            hr = ap.GetStreamPrioritization(out sp);
            MFError.ThrowExceptionForHR(hr);
            //ap.CreateStreamPrioritization(out sp);

            hr = MFExtern.MFCreateMediaType(out mt);
            MFError.ThrowExceptionForHR(hr);

            hr = mt.SetGUID(MFAttributesClsid.MF_MT_SUBTYPE, cc4.ToMediaSubtype());
            MFError.ThrowExceptionForHR(hr);
            hr = mt.SetGUID(MFAttributesClsid.MF_MT_MAJOR_TYPE, MFMediaType.Video);
            MFError.ThrowExceptionForHR(hr);

            hr = ap.CreateStream(mt, out pStream);
            MFError.ThrowExceptionForHR(hr);

            hr = ap.SetStream(pStream);
            MFError.ThrowExceptionForHR(hr);

            hr = MFExtern.MFCreateASFStreamSelector(ap, out ss);
            MFError.ThrowExceptionForHR(hr);
            short[] w = new short[1];
            w[0] = 3;
            hr = ss.GetOutputStreamNumbers(0, w);
            MFError.ThrowExceptionForHR(hr);
            Debug.Assert(w[0] != 3);
        }
Esempio n. 6
0
        // This returns the default or attempts to compute it, in its absence.
        public void GetDefaultStride(out int pnStride)
        {
            int nStride = 0;
            bool bFailed = false;

            // First try to get it from the attribute.
            try
            {
                int hr = GetMediaType().GetUINT32(MFAttributesClsid.MF_MT_DEFAULT_STRIDE, out nStride);
                MFError.ThrowExceptionForHR(hr);
            }
            catch
            {
                bFailed = true;
            }
            if (bFailed)
            {
                // Attribute not set. See if we can calculate the default stride.
                Guid subtype;

                int width = 0;
                int height = 0;

                // First we need the subtype .
                GetSubType(out subtype);

                // Now we need the image width and height.
                GetFrameDimensions(out width, out height);

                // Now compute the stride for a particular bitmap type
                FourCC f = new FourCC(subtype);
                int hr = MFExtern.MFGetStrideForBitmapInfoHeader(f.ToInt32(), width, out nStride);
                MFError.ThrowExceptionForHR(hr);

                // Set the attribute for later reference.
                SetDefaultStride(nStride);
            }

            pnStride = nStride;
        }
Esempio n. 7
0
        // Extracts the FOURCC code from the subtype.
        // Not all subtypes follow this pattern.
        public void GetFourCC(out int pFourCC)
        {
            Debug.Assert(IsValid());
            Guid guidSubType;

            GetSubType(out guidSubType);

            FourCC f = new FourCC(guidSubType);

            pFourCC = f.ToInt32();
        }
Esempio n. 8
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;
        }
Esempio n. 9
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;
        }
Esempio 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;
        }
Esempio n. 11
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;
        }
Esempio n. 12
0
        private void TestVF()
        {
            int hr;
            IMFVideoMediaType vmt;
            MFMediaEqual me;
            int i;
            MFVideoFormat vf;
            IMFMediaType mt, mt2, mt3;
            FourCC cc4 = new FourCC("YUY2");

            hr = MFExtern.MFCreateMediaType(out mt);
            MFError.ThrowExceptionForHR(hr);

            hr = mt.SetGUID(MFAttributesClsid.MF_MT_SUBTYPE, cc4.ToMediaSubtype());
            MFError.ThrowExceptionForHR(hr);
            hr = mt.SetGUID(MFAttributesClsid.MF_MT_MAJOR_TYPE, MFMediaType.Video);
            MFError.ThrowExceptionForHR(hr);

            hr = MFExtern.MFCreateMFVideoFormatFromMFMediaType(mt, out vf, out i);
            MFError.ThrowExceptionForHR(hr);
            int cc = MFExtern.MFGetUncompressedVideoFormat(vf);

            hr = MFExtern.MFCreateMediaType(out mt2);
            MFError.ThrowExceptionForHR(hr);
            hr = MFExtern.MFInitMediaTypeFromMFVideoFormat(mt2, vf, i);
            MFError.ThrowExceptionForHR(hr);

            int iRet = mt.IsEqual(mt2, out me);
            Debug.Assert(iRet == 0);

            AMMediaType amt = new AMMediaType();
            hr = MFExtern.MFInitAMMediaTypeFromMFMediaType(mt, Guid.Empty, amt);
            MFError.ThrowExceptionForHR(hr);

            AMMediaType amt2;
            hr = MFExtern.MFCreateAMMediaTypeFromMFMediaType(mt, MFRepresentation.VideoInfo2, out amt2);
            MFError.ThrowExceptionForHR(hr);

            hr = MFExtern.MFCreateMediaType(out mt3);
            MFError.ThrowExceptionForHR(hr);
            hr = MFExtern.MFInitMediaTypeFromAMMediaType(mt3, amt2);
            MFError.ThrowExceptionForHR(hr);

            iRet = mt.IsEqual(mt3, out me);
            Debug.Assert(iRet == 0);

            hr = MFExtern.MFCreateVideoMediaType(vf, out vmt);

            VideoInfoHeader vih = new VideoInfoHeader();
            Marshal.PtrToStructure(amt.formatPtr, vih);

            IMFMediaType mt4;
            hr = MFExtern.MFCreateMediaType(out mt4);
            MFError.ThrowExceptionForHR(hr);
            hr = MFExtern.MFInitMediaTypeFromVideoInfoHeader(mt4, vih, amt.formatSize, Guid.Empty);
            MFError.ThrowExceptionForHR(hr);

            iRet = mt.IsEqual(mt4, out me);
            Debug.Assert(iRet == 1 && me == (MediaFoundation.MFMediaEqual.MajorTypes | MediaFoundation.MFMediaEqual.FormatUserData));

            VideoInfoHeader2 vih2 = new VideoInfoHeader2();
            Marshal.PtrToStructure(amt2.formatPtr, vih2);

            IMFMediaType mt5;
            hr = MFExtern.MFCreateMediaType(out mt5);
            MFError.ThrowExceptionForHR(hr);
            hr = MFExtern.MFInitMediaTypeFromVideoInfoHeader2(mt5, vih2, amt2.formatSize, Guid.Empty);
            MFError.ThrowExceptionForHR(hr);

            iRet = mt.IsEqual(mt5, out me);
            Debug.Assert(iRet == 1 && me == (MediaFoundation.MFMediaEqual.MajorTypes | MediaFoundation.MFMediaEqual.FormatUserData));

            IntPtr ip;
            hr = vmt.SetGUID(MFAttributesClsid.MF_MT_SUBTYPE, cc4.ToMediaSubtype());
            MFError.ThrowExceptionForHR(hr);
            hr = vmt.SetGUID(MFAttributesClsid.MF_MT_MAJOR_TYPE, MFMediaType.Video);
            MFError.ThrowExceptionForHR(hr);

            vf = vmt.GetVideoFormat();

            // This statement seems to cause crashes in 64bit code.  The crash comes on leaving DoTests.
            // Since this doesn't occur in 32 bit, and since this is a deprecated method, I don't think
            // I care.
            vmt.GetVideoRepresentation(MFRepresentation.VideoInfo, out ip, 10);

            AMMediaType amt5 = new AMMediaType();
            Marshal.PtrToStructure(ip, amt5);
            Debug.Assert(amt5.formatType == MFRepresentation.VideoInfo);
            hr = vmt.FreeRepresentation(MFRepresentation.VideoInfo, ip);
            MFError.ThrowExceptionForHR(hr);
        }
Esempio n. 13
0
        //-------------------------------------------------------------------
        // Name: ReadRiffHeader
        // Description:
        // Read the container header section. (The 'RIFF' or 'LIST' header.)
        //
        // This method verifies the header is well-formed and caches the
        // container's FOURCC type.
        //-------------------------------------------------------------------
        private void ReadRiffHeader()
        {
            int hr;
            int iRiffSize = Marshal.SizeOf(typeof(RIFFLIST));

            // Riff chunks must be WORD aligned
            if (!Utils.IsAligned(m_llContainerOffset, 2))
            {
                throw new COMException("bad alignment", E_InvalidArgument);
            }

            // Offset must be positive.
            if (m_llContainerOffset < 0)
            {
                throw new COMException("negative offset", E_InvalidArgument);
            }

            // Offset + the size of header must not overflow.
            if (long.MaxValue - m_llContainerOffset <= iRiffSize)
            {
                throw new COMException("overflow chunk", E_InvalidArgument);
            }

            RIFFLIST header = new RIFFLIST();
            int cbRead = 0;

            // Seek to the start of the container.
            hr = m_pStream.SetCurrentPosition(m_llContainerOffset);
            MFError.ThrowExceptionForHR(hr);

            // Read the header.
            IntPtr ip = Marshal.AllocCoTaskMem(iRiffSize);
            try
            {
                hr = m_pStream.Read(ip, iRiffSize, out cbRead);
                MFError.ThrowExceptionForHR(hr);

                // Make sure we read the number of bytes we expected.
                if (cbRead == iRiffSize)
                {
                    Marshal.PtrToStructure(ip, header);
                }
                else
                {
                    throw new COMException("read riff failure", E_InvalidArgument);
                }
            }
            finally
            {
                Marshal.FreeCoTaskMem(ip);
            }

            // Make sure the header ID matches what the caller expected.
            if (header.fcc != m_fccID)
            {
                throw new COMException("bad header id", E_InvalidArgument);
            }

            // The size given in the RIFF header does not include the 8-byte header.
            // However, our m_llContainerOffset is the offset from the start of the
            // header. Therefore our container size = listed size + size of header.

            m_dwContainerSize = header.cb + Marshal.SizeOf(typeof(RIFFCHUNK));
            m_fccType = header.fccListType;

            // Start of the first chunk = start of container + size of container header
            m_llCurrentChunkOffset = m_llContainerOffset + iRiffSize;

            ReadChunkHeader();
        }
Esempio n. 14
0
 public CRiffChunk()
 {
     fcc = new FourCC(0);
     cb = 0;
 }
Esempio n. 15
0
        //-------------------------------------------------------------------
        // Name: ParseWAVEHeader
        // Description: Parsers the RIFF WAVE header.
        //
        // Note:
        // .wav files should look like this:
        //
        // RIFF ('WAVE'
        //       'fmt ' = WAVEFORMATEX structure
        //       'data' = audio data
        //       )
        //-------------------------------------------------------------------
        public void ParseWAVEHeader()
        {
            bool bFoundData = false;
            FourCC fmt = new FourCC("fmt ");
            FourCC data = new FourCC("data");

            // Iterate through the RIFF chunks. Ignore chunks we don't recognize.
            while (true)
            {
                if (Chunk().FourCC() == fmt)
                {
                    // Read the WAVEFORMATEX structure allegedly contained in this chunk.
                    // This method does NOT validate the contents of the structure.
                    ReadFormatBlock();
                }
                else if (Chunk().FourCC() == data)
                {
                    // Found the start of the audio data. The format chunk should precede the
                    // data chunk. If we did not find the formt chunk yet, that is a failure
                    // case (see below)
                    bFoundData = true;
                    break;
                }

                MoveToNextChunk();
            }

            // To be valid, the file must have a format chunk and a data chunk.
            // Fail if either of these conditions is not met.
            if (m_pWaveFormat == null || !bFoundData)
            {
                throw new COMException("no format/data chunk found", MFError.MF_E_INVALID_FILE_FORMAT);
            }

            m_rtDuration = Utils.AudioDurationFromBufferSize(m_pWaveFormat, Chunk().DataSize());
        }