//static public IEnumerable<byte[]> DecodeStream(Stream s, FourCC fourcc = FourCC.NV12, AccelerationLevel acceleration = AccelerationLevel.BestAvailableAccelerationUseGPUorCPU) //{ // return null; //} /// <summary> /// Construct the decoder. /// </summary> /// <param name="stream">Stream be read from</param> /// <param name="codecId">What format the bitstream is in: AVC, HEVC, MJPEG, ...</param> /// <param name="impl">implementation to use</param> /// <param name="outIOPattern">memory type for decoding</param> public StreamDecoder(Stream stream, CodecId codecId, mfxIMPL impl = mfxIMPL.MFX_IMPL_AUTO, IOPattern outIOPattern = IOPattern.MFX_IOPATTERN_OUT_SYSTEM_MEMORY) { long oldposition = stream.Position; var buf = new byte[65536]; //avail after init int n = stream.Read(buf, 0, buf.Length); if (n < buf.Length) { Array.Resize(ref buf, n); } stream.Position = oldposition; this.decoderParameters = QuickSyncStatic.DecodeHeader(buf, codecId, impl); this.decoderParameters.IOPattern = outIOPattern; lowLevelDecoder = new LowLevelDecoder(decoderParameters, null, impl); Init(stream); }
/// <summary>Initializes a new instance of the <see cref="StreamDecoder"/> class. /// Fully specify decode params, and optionally VPP params</summary> /// <param name="stream">The stream.</param> /// <param name="decodeParameters">The decode parameters.</param> /// <param name="mfxVPPParams">The MFX VPP parameters.</param> /// <param name="impl">The implementation.</param> public StreamDecoder(Stream stream, mfxVideoParam decodeParameters, mfxVideoParam?mfxVPPParams = null, mfxIMPL impl = mfxIMPL.MFX_IMPL_AUTO) { this.decoderParameters = decodeParameters; lowLevelDecoder = new LowLevelDecoder(decodeParameters, mfxVPPParams, impl); Init(stream); }
unsafe static public Bitmap GetBitmap(this ILowLevelDecoder decoder, mfxFrameSurface1 surface) { #if false // if ( this.Data.MemId == IntPtr.Zero) { Trace.Assert(this.Info.FourCC == FourCC.NV12, "AsBitmap only works on NV12 currently"); Bitmap bmp = new Bitmap(this.Info.CropW, this.Info.CropH, PixelFormat.Format32bppArgb); Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); // var a = new FrameFormatConverterFromNV12(FourCC.BGR4,bmp.Width, bmp.Height); var a = new NV12ToXXXXLowLevelConverter(); BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat); // Get the address of the first line. IntPtr ptr = bmpData.Scan0; a.ConvertFromNV12FrameSurface(FourCC.BGR4, this.Data.Y_ptr, this.Data.UV_ptr, (byte*)bmpData.Scan0, bmp.Width, bmp.Height, bmpData.Stride * bmpData.Height, this.Info.CropW, bmpData.Stride); return bmp; #else Trace.Assert(surface.Info.FourCC == FourCC.RGB4, "For vidmem, AsBitmap only works on RGB4 currently"); Bitmap bmp = new Bitmap(surface.Info.CropW, surface.Info.CropH, PixelFormat.Format32bppArgb); Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); BitmapData bmpData; bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat); // Get the address of the first line. IntPtr ptr = bmpData.Scan0; mfxFrameData fd = new mfxFrameData(); decoder.LockFrame((IntPtr)(&surface)); Console.WriteLine(fd.B); int pitch = fd.PitchHigh << 16 | fd.PitchLow; var zm = new byte[pitch * surface.Info.CropH]; //for (int i = 0; i < zm.Length; i++) //{ // zm[i] = 1; //} // Marshal.Copy(fd.B, zm, 0, zm.Length); // Console.WriteLine(zm.Sum(z => (long)z)); if (pitch == bmpData.Stride) { FastMemcpyMemmove.memcpy(ptr, fd.B, pitch * surface.Info.CropH); } else { int minpitch = Math.Min(pitch, bmpData.Stride); for (int i = 0; i < surface.Info.CropH; i++) { FastMemcpyMemmove.memcpy(ptr + bmpData.Stride * i, fd.B + pitch * i, minpitch); } } decoder.UnlockFrame((IntPtr)(&surface)); bmp.UnlockBits(bmpData); return bmp; } #endif }