Exemplo n.º 1
0
 public int CheckMediaType(AMMediaType pmt)
 {
     if (pmt == null) return E_POINTER;
     if (pmt.formatPtr == IntPtr.Zero) return VFW_E_INVALIDMEDIATYPE;
     if (pmt.majorType != MediaType.Video)
     {
         return VFW_E_INVALIDMEDIATYPE;
     }
     if (
             pmt.subType != MediaSubType.RGB24
         &&  pmt.subType != MediaSubType.RGB32
         &&  pmt.subType != MediaSubType.ARGB32
         )
     {
         return VFW_E_INVALIDMEDIATYPE;
     }
     BitmapInfoHeader _bmi = pmt;
     if (_bmi == null)
     {
         return E_UNEXPECTED;
     }
     if (_bmi.Compression != BI_RGB)
     {
         return VFW_E_TYPE_NOT_ACCEPTED;
     }
     if (_bmi.BitCount != 24 && _bmi.BitCount != 32)
     {
         return VFW_E_TYPE_NOT_ACCEPTED;
     }
     VideoStreamConfigCaps _caps;
     GetDefaultCaps(0, out _caps);
     if (
             _bmi.Width < _caps.MinOutputSize.Width
         || _bmi.Width > _caps.MaxOutputSize.Width
         )
     {
         return VFW_E_INVALIDMEDIATYPE;
     }
     long _rate = 0;
     {
         VideoInfoHeader _pvi = pmt;
         if (_pvi != null)
         {
             _rate = _pvi.AvgTimePerFrame;
         }
     }
     {
         VideoInfoHeader2 _pvi = pmt;
         if (_pvi != null)
         {
             _rate = _pvi.AvgTimePerFrame;
         }
     }
     if (_rate < _caps.MinFrameInterval || _rate > _caps.MaxFrameInterval)
     {
         return VFW_E_INVALIDMEDIATYPE;
     }
     return NOERROR;
 }
 public int Load(string pszFileName, AMMediaType pmt)
 {
   if (_sourceStream != null)
     return E_UNEXPECTED;
   _fileName = pszFileName;
   _sourceStream = new FileStream(pszFileName, FileMode.Open, FileAccess.Read, FileShare.Read);
   _streamCreated = true;
   return NOERROR;
 }
 /// <summary>
 /// Tries to create a matching <see cref="AMMediaType"/> for the given <paramref name="streamInfo"/>.
 /// </summary>
 /// <param name="streamInfo">stream</param>
 /// <param name="mediaType">media type</param>
 /// <returns><c>true</c> if successful</returns>
 public static bool TryGetType(InputstreamInfo streamInfo, out AMMediaType mediaType)
 {
   Func<InputstreamInfo, AMMediaType> mediaTypeFn;
   if (TYPE_MAPPINGS.TryGetValue(streamInfo.CodecInternalName, out mediaTypeFn) || TYPE_MAPPINGS.TryGetValue(streamInfo.CodecName, out mediaTypeFn))
   {
     mediaType = mediaTypeFn(streamInfo);
     return true;
   }
   mediaType = null;
   return false;
 }
        public int CheckMediaType(AMMediaType pmt)
        {
            if (pmt == null) return E_POINTER;
            if (pmt.formatPtr == IntPtr.Zero) return VFW_E_INVALIDMEDIATYPE;
            if (pmt.majorType != MediaType.Video)
            {
                return VFW_E_INVALIDMEDIATYPE;
            }
            if (
                pmt.subType != MediaSubType.RGB24
                && pmt.subType != MediaSubType.RGB32
                && pmt.subType != MediaSubType.ARGB32
                )
            {
                return VFW_E_INVALIDMEDIATYPE;
            }
            BitmapInfoHeader bmi = pmt;
            if (bmi.Compression != BI_RGB)
            {
                return VFW_E_TYPE_NOT_ACCEPTED;
            }
            if (bmi.BitCount != 24 && bmi.BitCount != 32)
            {
                return VFW_E_TYPE_NOT_ACCEPTED;
            }
            VideoStreamConfigCaps caps;
            GetDefaultCaps(0, out caps);
            if (
                bmi.Width < caps.MinOutputSize.Width
                || bmi.Width > caps.MaxOutputSize.Width
                )
            {
                return VFW_E_INVALIDMEDIATYPE;
            }

            var rate = 0L;
            VideoInfoHeader pvi = pmt;
            if (pvi != null)
            {
                rate = pvi.AvgTimePerFrame;
            }
            VideoInfoHeader2 pvi2 = pmt;
            if (pvi2 != null)
            {
                rate = pvi2.AvgTimePerFrame;
            }

            if (rate < caps.MinFrameInterval || rate > caps.MaxFrameInterval)
            {
                return VFW_E_INVALIDMEDIATYPE;
            }
            return NOERROR;
        }
    /// <summary>
    /// AnnexB formatted h264 bitstream
    /// </summary>
    /// <param name="streamInfo"></param>
    /// <returns></returns>
    public static AMMediaType H264_AnnexB(InputstreamInfo streamInfo)
    {
      int width = (int)streamInfo.Width;
      int height = (int)streamInfo.Height;

      if (streamInfo.ExtraData.Length > 0)
      {
        var codecData = new H264CodecData(streamInfo.ExtraData);

        SPSUnit spsUnit = new SPSUnit(codecData.SPS);
        width = spsUnit.Width();
        height = spsUnit.Height();
      }

      VideoInfoHeader2 vi = new VideoInfoHeader2();
      vi.SrcRect.right = width;
      vi.SrcRect.bottom = height;
      vi.TargetRect.right = width;
      vi.TargetRect.bottom = height;

      int hcf = HCF(width, height);
      vi.PictAspectRatioX = width / hcf;
      vi.PictAspectRatioY = height / hcf;

      vi.BmiHeader.Width = width;
      vi.BmiHeader.Height = height;
      vi.BmiHeader.Planes = 1;
      vi.BmiHeader.Compression = FOURCC_H264;

      AMMediaType amt = new AMMediaType();
      amt.majorType = MediaType.Video;
      amt.subType = MediaSubType.H264;
      amt.temporalCompression = true;
      amt.fixedSizeSamples = false;
      amt.sampleSize = 1;
      amt.SetFormat(vi);
      return amt;
    }
Exemplo n.º 6
0
        public ImageOverFilter()
            : base("CSharp Image Overlay Filter")
        {
            vid.DSplugin = true;

            AllocConsole();
            IntPtr stdHandle = GetStdHandle(STD_OUTPUT_HANDLE);
            SafeFileHandle safeFileHandle = new SafeFileHandle(stdHandle, true);
            FileStream fileStream = new FileStream(safeFileHandle, FileAccess.Write);
            Encoding encoding = System.Text.Encoding.GetEncoding(MY_CODE_PAGE);
            StreamWriter standardOutput = new StreamWriter(fileStream, encoding);
            standardOutput.AutoFlush = true;
            Console.SetOut(standardOutput);
            Console.WriteLine("This text you can see in console window.");

            AMMediaType pmt = new AMMediaType() { majorType = MediaType.Video, subType = MediaSubType.YUY2, formatType = MediaType.Video, formatPtr = IntPtr.Zero };
            SetMediaType(PinDirection.Input, pmt);
            pmt.Free();

            pmt = new AMMediaType() { majorType = MediaType.Video, subType = MediaSubType.RGB24, formatType = MediaType.Video, formatPtr = IntPtr.Zero };
            SetMediaType(PinDirection.Output, pmt);
            pmt.Free();
        }
Exemplo n.º 7
0
 public static void SetFormat(ref AMMediaType mt, IntPtr pFormat, int nSize)
 {
     AllocFormat(ref mt, nSize);
     if (mt != null && pFormat != IntPtr.Zero)
     {
         COMHelper.API.CopyMemory(mt.formatPtr, pFormat, mt.formatSize);
     }
 }
Exemplo n.º 8
0
 public static void Free(ref AMMediaType mt)
 {
     if (mt != null)
     {
         FreeFormat(ref mt);
         if (mt.unkPtr != IntPtr.Zero)
         {
             Marshal.Release(mt.unkPtr);
             mt.unkPtr = IntPtr.Zero;
         }
     }
 }
Exemplo n.º 9
0
 public static void AllocFormat(ref AMMediaType mt, int nSize)
 {
     FreeFormat(ref mt);
     if (mt != null && nSize > 0)
     {
         mt.formatPtr = Marshal.AllocCoTaskMem(nSize);
         mt.formatSize = nSize;
     }
 }
Exemplo n.º 10
0
    public override int CheckMediaType(AMMediaType pmt)
    {
      lock (m_Lock)
      {
        // The given mediatype as acceptable when the major type is Stream, no subtype and no specific format
        if (pmt.majorType == MediaType.Stream && pmt.subType == MediaSubType.Null && pmt.formatType == FormatType.None)
          return NOERROR;

        return E_FAIL;
      }
    }
Exemplo n.º 11
0
        public static void Copy(AMMediaType mt, ref AMMediaType _dest)
        {
            if (((object)_dest) == null)
            {
                _dest = new AMMediaType();
            }
            else
            {
                Free(ref _dest);
            }

            _dest.majorType = mt.majorType;
            _dest.subType = mt.subType;
            _dest.fixedSizeSamples = mt.fixedSizeSamples;
            _dest.temporalCompression = mt.temporalCompression;
            _dest.sampleSize = mt.sampleSize;
            _dest.formatType = mt.formatType;
            _dest.unkPtr = mt.unkPtr;
            _dest.formatPtr = IntPtr.Zero;
            _dest.formatSize = mt.formatSize;
            if (_dest.unkPtr != IntPtr.Zero)
            {
                Marshal.AddRef(_dest.unkPtr);
            }
            if (_dest.formatSize > 0)
            {
                _dest.formatPtr = Marshal.AllocCoTaskMem(_dest.formatSize);
                COMHelper.API.CopyMemory(_dest.formatPtr, mt.formatPtr, _dest.formatSize);
            }
        }
Exemplo n.º 12
0
        public int SetMediaType(AMMediaType pMediaType)
        {
            if (m_pUnknown == IntPtr.Zero) return E_NOINTERFACE;

            SetMediaTypeProc _Proc = GetProcDelegate<SetMediaTypeProc>(14);

            if (_Proc == null) return E_UNEXPECTED;

            return (HRESULT)_Proc(
                        m_pUnknown,
                        pMediaType
                        );
        }
Exemplo n.º 13
0
        public int ConnectionMediaType(AMMediaType pmt)
        {
            if (m_pUnknown == IntPtr.Zero) return E_NOINTERFACE;

            ConnectionMediaTypeProc _Proc = GetProcDelegate<ConnectionMediaTypeProc>(7);

            if (_Proc == null) return E_UNEXPECTED;

            return (HRESULT)_Proc(
                        m_pUnknown,
                        pmt
                        );
        }
Exemplo n.º 14
0
 public static void FreeFormat(ref AMMediaType mt)
 {
     if (mt != null)
     {
         if (mt.formatPtr != IntPtr.Zero)
         {
             Marshal.FreeCoTaskMem(mt.formatPtr);
             mt.formatPtr = IntPtr.Zero;
         }
         mt.formatSize = 0;
     }
 }
Exemplo n.º 15
0
 public static bool AreEquals(AMMediaType _src, AMMediaType _dst)
 {
     if ((_dst.majorType != _src.majorType))
     {
         return false;
     }
     if (_src.subType != _dst.subType)
     {
         return false;
     }
     if (_src.formatType != _dst.formatType)
     {
         return false;
     }
     if (_src.formatSize != _dst.formatSize)
     {
         return false;
     }
     if (_src.formatSize > 0)
     {
         byte[] _source = new byte[_src.formatSize];
         byte[] _dest = new byte[_src.formatSize];
         Marshal.Copy(_src.formatPtr, _source, 0, _source.Length);
         Marshal.Copy(_dst.formatPtr, _dest, 0, _dest.Length);
         for (int i = 0; i < _source.Length; i++)
         {
             if (_dest[i] != _source[i]) return false;
         }
     }
     return true;
 }
Exemplo n.º 16
0
 public static void SetFormat(ref AMMediaType mt, ref WaveFormatEx wfx)
 {
     if (wfx != null)
     {
         int cb = Marshal.SizeOf(wfx);
         IntPtr _ptr = Marshal.AllocCoTaskMem(cb);
         try
         {
             Marshal.StructureToPtr(wfx, _ptr, true);
             SetFormat(ref mt, _ptr, cb);
             if (mt != null)
             {
                 mt.formatType = FormatType.WaveEx;
             }
         }
         finally
         {
             Marshal.FreeCoTaskMem(_ptr);
         }
     }
 }
Exemplo n.º 17
0
        /*
        public override int OnReceive(ref IMediaSampleImpl _sample)
        {
            Console.WriteLine("OnReceive ");

            Output.Deliver(ref _sample);

            return S_OK;
        }
        */
        public override int GetMediaType(int iPosition, ref AMMediaType pMediaType)
        {
            Console.WriteLine("GetMediaType");
            if (iPosition > 0) return VFW_S_NO_MORE_ITEMS;
            if (pMediaType == null) return E_INVALIDARG;
            if (!Input.IsConnected) return VFW_E_NOT_CONNECTED;

            AMMediaType.Copy(Input.CurrentMediaType, ref pMediaType);

            VideoInfoHeader vhi = new VideoInfoHeader();
            Marshal.PtrToStructure(pMediaType.formatPtr, vhi);
            vhi.BmiHeader.Compression = 0;
            vhi.BmiHeader.BitCount = 24;
            vhi.BmiHeader.ImageSize = vhi.BmiHeader.Width * vhi.BmiHeader.Height * 3;
            pMediaType.formatPtr = Marshal.AllocCoTaskMem(pMediaType.formatSize);
            Marshal.StructureToPtr(vhi, pMediaType.formatPtr, false);

            pMediaType.majorType = MediaType.Video;
            pMediaType.subType = MediaSubType.RGB24;
            pMediaType.formatType = FormatType.VideoInfo;
            pMediaType.sampleSize = vhi.BmiHeader.ImageSize;

            return NOERROR;
        }
Exemplo n.º 18
0
        public override int CheckTransform(AMMediaType mtIn, AMMediaType mtOut)
        {
            Console.WriteLine("CheckTransform");
            if (mtIn.subType == MediaSubType.YUY2 && mtOut.subType == MediaSubType.RGB24)
            {
                return NOERROR;
            }

            return VFW_E_INVALIDMEDIATYPE;

            //return AMMediaType.AreEquals(mtIn, mtOut) ? NOERROR : VFW_E_INVALIDMEDIATYPE;
        }
Exemplo n.º 19
0
        public override int CheckInputType(AMMediaType pmt)
        {
            Console.WriteLine("CheckInputType");

            if (pmt.majorType != MediaType.Video)
            {
                return VFW_E_TYPE_NOT_ACCEPTED;
            }
            if (pmt.subType != MediaSubType.YUY2)
            {
                return VFW_E_TYPE_NOT_ACCEPTED;
            }
            if (pmt.formatType != FormatType.VideoInfo)
            {
                return VFW_E_TYPE_NOT_ACCEPTED;
            }
            if (pmt.formatPtr == IntPtr.Zero)
            {
                return VFW_E_TYPE_NOT_ACCEPTED;
            }

            vid.Show();

            return NOERROR;
        }
Exemplo n.º 20
0
    public override int GetMediaType(int iPosition, ref AMMediaType pMediaType)
    {
      lock (m_Lock)
      {
        if (iPosition < 0)
        {
          return E_INVALIDARG;
        }
        if (iPosition > 0)
        {
          return VFW_S_NO_MORE_ITEMS;
        }
        if (_sourceStream == null)
        {
          return E_UNEXPECTED;
        }
        // Set our MediaType requirements
        pMediaType.majorType = MediaType.Stream;
        pMediaType.subType = MediaSubType.Null;
        pMediaType.formatType = FormatType.None;
        pMediaType.temporalCompression = false;
        pMediaType.fixedSizeSamples = false;
        pMediaType.sampleSize = 0;

        return NOERROR;
      }
    }
Exemplo n.º 21
0
 public static void SetFormat(ref AMMediaType mt, ref VideoInfoHeader vih)
 {
     if (vih != null)
     {
         int cb = Marshal.SizeOf(vih);
         IntPtr _ptr = Marshal.AllocCoTaskMem(cb);
         try
         {
             Marshal.StructureToPtr(vih, _ptr, true);
             SetFormat(ref mt, _ptr, cb);
             if (mt != null)
             {
                 mt.formatType = FormatType.VideoInfo;
             }
         }
         finally
         {
             Marshal.FreeCoTaskMem(_ptr);
         }
     }
 }
Exemplo n.º 22
0
 public AMMediaType(AMMediaType mt)
     : this()
 {
     Set(mt);
 }
Exemplo n.º 23
0
 public static bool IsValid(AMMediaType mt)
 {
     return (mt.majorType != null && mt.majorType != Guid.Empty);
 }
Exemplo n.º 24
0
        public void Set(AMMediaType mt)
        {
            Free();

            majorType = mt.majorType;
            subType = mt.subType;
            fixedSizeSamples = mt.fixedSizeSamples;
            temporalCompression = mt.temporalCompression;
            sampleSize = mt.sampleSize;
            formatType = mt.formatType;
            unkPtr = mt.unkPtr;
            formatPtr = IntPtr.Zero;
            formatSize = mt.formatSize;
            if (unkPtr != IntPtr.Zero)
            {
                Marshal.AddRef(unkPtr);
            }
            if (formatSize > 0)
            {
                SetFormat(mt.formatPtr, formatSize);
            }
        }
Exemplo n.º 25
0
        public int GetMediaType(out AMMediaType ppMediaType)
        {
            ppMediaType = null;
            if (m_pUnknown == IntPtr.Zero) return E_NOINTERFACE;

            GetMediaTypeProc _Proc = GetProcDelegate<GetMediaTypeProc>(13);

            if (_Proc == null) return E_UNEXPECTED;

            return (HRESULT)_Proc(
                        m_pUnknown,
                        out ppMediaType
                        );
        }
Exemplo n.º 26
0
        public bool MatchesPartial(AMMediaType _dst)
        {
            if ((_dst.majorType != Guid.Empty) && (majorType != _dst.majorType))
            {
                return false;
            }
            if ((_dst.subType != Guid.Empty) && (subType != _dst.subType))
            {
                return false;
            }
            if (_dst.formatType != Guid.Empty)
            {
                if (formatType != _dst.formatType)
                {
                    return false;
                }
                if (formatSize != _dst.formatSize)
                {
                    return false;
                }
                if (formatSize > 0)
                {
                    byte[] _source = new byte[formatSize];
                    byte[] _dest = new byte[formatSize];
                    Marshal.Copy(formatPtr, _source, 0, _source.Length);
                    Marshal.Copy(_dst.formatPtr, _dest, 0, _dest.Length);
                    for (int i = 0; i < _source.Length; i++)
                    {
                        if (_dest[i] != _source[i]) return false;
                    }
                }

            }
            return true;
        }
Exemplo n.º 27
0
        public int ReceiveConnection(IntPtr pReceivePin, AMMediaType pmt)
        {
            if (m_pUnknown == IntPtr.Zero) return E_NOINTERFACE;

            ReceiveConnectionProc _Proc = GetProcDelegate<ReceiveConnectionProc>(4);

            if (_Proc == null) return E_UNEXPECTED;

            return (HRESULT)_Proc(
                        m_pUnknown,
                        pReceivePin, pmt
                        );
        }
Exemplo n.º 28
0
 public static bool IsPartiallySpecified(AMMediaType mt)
 {
     if ((mt.majorType == Guid.Empty) || (mt.formatType == Guid.Empty))
     {
         return true;
     }
     else
     {
         return false;
     }
 }
Exemplo n.º 29
0
        public int QueryAccept(AMMediaType pmt)
        {
            if (m_pUnknown == IntPtr.Zero) return E_NOINTERFACE;

            QueryAcceptProc _Proc = GetProcDelegate<QueryAcceptProc>(11);

            if (_Proc == null) return E_UNEXPECTED;

            return (HRESULT)_Proc(
                        m_pUnknown,
                        pmt
                        );
        }
Exemplo n.º 30
0
 public static void Init(ref AMMediaType mt)
 {
     if (((object)mt) == null)
     {
         mt = new AMMediaType();
     }
     else
     {
         Free(ref mt);
     }
     mt.majorType = Guid.Empty;
     mt.subType = Guid.Empty;
     mt.fixedSizeSamples = true;
     mt.temporalCompression = false;
     mt.sampleSize = 0;
     mt.formatType = Guid.Empty;
     mt.unkPtr = IntPtr.Zero;
     mt.formatPtr = IntPtr.Zero;
     mt.formatSize = 0;
 }