下面例子是关于YUV420P的操作,需要把一帧YUV420P需要拆分成三段 Y ,U, V
提供两种方法.关于效率请自行测试
一: BlockCopy()
byte[] data = new byte[Width * Height * 3 / 2];
byte[] dataY = new byte[Width * Height];
byte[] dataU = new byte[Width * Height / 4];
byte[] dataV = new byte[Width * Height / 4];
Buffer.BlockCopy(data, 0, dataY, 0, Width * Height);
Buffer.BlockCopy(data, Width * Height, dataU, 0, Width * Height / 4);
Buffer.BlockCopy(data, Width * Height * 5 / 4, dataV, 0, Width * Height / 4);
二: ReadBytes()
byte[] data = new byte[Width * Height * 3 / 2];
MemoryStream ms = new MemoryStream(data);
BinaryReader reader = new BinaryReader(ms);
byte[] dataY = reader.ReadBytes(Width * Height);
byte[] dataU = reader.ReadBytes(Width * Height / 4);
byte[] dataV = reader.ReadBytes(Width * Height / 4);
ps:
已知BlockCopy()方法需要指明偏移量,否则可能会被覆盖
而ReadBytes()每次写入偏移量都会跟着递进。