/// <summary> /// Scale a 24bpp interleaved image into another 24bpp interleaved image. /// </summary> private Bitmap RescaleToTargetSize(IntPtr pBuffer) { Bitmap frame = new Bitmap(TargetFrameSize.Width, TargetFrameSize.Height, PixelFormat.Format24bppRgb); Rectangle r = new Rectangle(Point.Empty, TargetFrameSize); BitmapData buf = frame.LockBits(r, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); try { EncoderBridge.InterleavedScale(pBuffer, buf.Scan0, this.Width, this.Height, TargetFrameSize.Width, TargetFrameSize.Height, true); } finally { frame.UnlockBits(buf); } return(frame); }
/// <summary> /// Returns the codec's startup data (other than in frames). /// This may be null or empty. /// </summary> public static byte[] GetVideoCodecData(EncoderJob JobSpec) { int bufsz = EncoderBridge.GetVideoCodecDataSize(ref JobSpec); byte[] buffer = null; if (bufsz > 0) { buffer = new byte[bufsz]; GCHandle bufh = GCHandle.Alloc(buffer, GCHandleType.Pinned); try { EncoderBridge.GetVideoCodecData(ref JobSpec, bufh.AddrOfPinnedObject()); } finally { bufh.Free(); } } return(buffer); }
/// <summary> /// Resample an image into a set of YUV420 buffers. /// </summary> /// <remarks>This is done seperately from frame encoding the improve multi-processor performance</remarks> internal unsafe void ResampleBuffer(Bitmap img, ref byte[] Luma, ref byte[] Cr, ref byte[] Cb, int width, int height) { if (img == null) { return; } Rectangle r = new Rectangle(0, 0, img.Width, img.Height); BitmapData bmp = img.LockBits(r, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); // for speed, always use the bitmap's real pixel format! // Do the RGB -> YUV conversion, with chroma sub-sampling & scaling try { GCHandle _Lx = default(GCHandle), _Crx = default(GCHandle), _Cbx = default(GCHandle); try { _Lx = GCHandle.Alloc(Luma, GCHandleType.Pinned); IntPtr _L = _Lx.AddrOfPinnedObject(); _Crx = GCHandle.Alloc(Cr, GCHandleType.Pinned); IntPtr _Cr = _Crx.AddrOfPinnedObject(); _Cbx = GCHandle.Alloc(Cb, GCHandleType.Pinned); IntPtr _Cb = _Cbx.AddrOfPinnedObject(); EncoderBridge.ScaleAndConvert( bmp.Scan0, bmp.Stride, img.Width, img.Height, _L, _Cr, _Cb, width, height); } finally { if (_Lx.IsAllocated) { _Lx.Free(); } if (_Crx.IsAllocated) { _Crx.Free(); } if (_Cbx.IsAllocated) { _Cbx.Free(); } } } finally { img.UnlockBits(bmp); } }