public unsafe void Scale() { var sourceFormat = new VideoFormatInfo(PixelFormat.Bgra32, 1000, 1000); var targetFormat = new VideoFormatInfo(PixelFormat.Rgb24, 100, 100); using (var source = new VideoFrame(sourceFormat)) using (var target = new VideoFrame(targetFormat)) using (var resampler = new VideoResampler(sourceFormat, targetFormat, SwsFlags.Lanczos)) { Assert.Equal(300, target.Strides[0]); var pixels = MemoryMarshal.Cast <byte, Bgra32>(source.Memory.Span); pixels.Fill(new Bgra32(new Rgba32(1, 2, 3, 4))); Assert.Equal(1000 * 1000 * 4, source.Memory.Length); Assert.Equal(100 * 100 * 3, target.Memory.Length); resampler.Resample(source, target); int i = 0; foreach (Rgb24 pixel in MemoryMarshal.Cast <byte, Rgb24>(target.Memory.Span)) { if (!pixel.Equals(new Rgb24(1, 2, 3))) { throw new Exception($"{i} | {pixel.R},{pixel.B},{pixel.G})"); } Assert.Equal(new Rgb24(1, 2, 3), pixel); i++; } } }
public unsafe void ChangePixelFormat() { var sourceFormat = new VideoFormatInfo(PixelFormat.Bgra32, 100, 100); var targetFormat = new VideoFormatInfo(PixelFormat.Rgb24, 100, 100); using (var source = new VideoFrame(sourceFormat)) using (var target = new VideoFrame(targetFormat)) using (var resampler = new VideoResampler(sourceFormat, targetFormat)) { Assert.Equal(300, target.Strides[0]); var pixels = MemoryMarshal.Cast <byte, Bgra32>(source.Memory.Span); pixels.Fill(new Bgra32(new Rgba32(1, 2, 3, 4))); Assert.Equal(100 * 100 * 4, source.Memory.Length); Assert.Equal(100 * 100 * 3, target.Memory.Length); resampler.Resample(source, target); foreach (Rgb24 pixel in MemoryMarshal.Cast <byte, Rgb24>(target.Memory.Span)) { Assert.Equal(new Rgb24(1, 2, 3), pixel); } } }
static void Main(string[] args) { Bitmap bitmap = new Bitmap(@"Z:\1.png"); VideoFrame inFrame = new VideoFrame(new VideoFormat(bitmap.Width, bitmap.Height, AVPixelFormat.Bgr24, 4)); VideoFrame outFrame = new VideoFrame(new VideoFormat(1366, 768, AVPixelFormat.Yuv420p, 16)); var bitmapData = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); inFrame.Update(bitmapData.Scan0); bitmap.UnlockBits(bitmapData); var resampler = new VideoResampler(inFrame.Format, outFrame.Format, SwsFlags.Bicublin); resampler.Resample(inFrame, outFrame); using (var file = File.OpenWrite(@"Z:\1.yuv")) { Write(file, outFrame.Data[0], outFrame.Format.Strides[0] * outFrame.Format.Height); Write(file, outFrame.Data[2], outFrame.Format.Strides[2] * outFrame.Format.Height / 2); //V Write(file, outFrame.Data[1], outFrame.Format.Strides[1] * outFrame.Format.Height / 2); //U } }