private byte[] GetFrameBuffer(FrameBitstreamInfo info) { int currentArrayIndex = 0; byte[] ret = new byte[info.FrameSizeInBytes]; for (int iLayer = 0; iLayer < info.LayerNum; iLayer++) { LayerBitstreamInfo pLayerBsInfo = info.LayerInfo[iLayer]; int iLayerSize = 0; int iNalIdx = pLayerBsInfo.NalCount - 1; int[] nalLengths = new int[4]; Marshal.Copy(pLayerBsInfo.NalLengthInByte, nalLengths, 0, 4); do { iLayerSize += nalLengths[iNalIdx]; --iNalIdx; } while (iNalIdx >= 0); Marshal.Copy(pLayerBsInfo.BsBuffer, ret, currentArrayIndex, iLayerSize); currentArrayIndex += iLayerSize; } return(ret); }
public (byte[] frameBuffer, FrameBitstreamInfo frameInfo) EncodeFrameH264(byte[] imageBuffer) { FrameBitstreamInfo ret = new FrameBitstreamInfo(); if (imageBuffer.Length != (SourcePicture.PicWidth * SourcePicture.PicHeight * 3 / 2)) { throw new H264NetEncodingError(String.Format("The input buffer size must be equal to Width * Image * 3 / 2 ({0} bytes)", SourcePicture.PicWidth * SourcePicture.PicHeight * 3 / 2)); } if (ImageBufferHandle == IntPtr.Zero) { throw new H264NetEncodingError("The image buffer handle has not been initialized did you call SetSourcePictureParameters ?"); } if (KeyframeInterval >= 0 && (FrameIntervalHolder++ > KeyframeInterval)) { ForceIntraFrame(EncoderPointer, true); FrameIntervalHolder = 0; } Marshal.Copy(imageBuffer, 0, ImageBufferHandle, imageBuffer.Length); var result = EncodeFrame(EncoderPointer, ref SourcePicture, ref ret); if (result != 0) { throw new H264NetEncodingError(String.Format("Couldn't encode frame. Return code: {0}", result)); } byte[] retFrameBuffer; try { retFrameBuffer = GetFrameBuffer(ret); } catch (Exception ex) { throw new H264NetFramebufferError("Failed to retrieve encoded frame buffer", ex); } return(retFrameBuffer, ret); }
private static extern int EncodeFrame(IntPtr encoder, ref SourcePicture SourcePicture, ref FrameBitstreamInfo FrameInfo);