我正在尝试使用调用 SharpFFmpeg 的 FFmpeg 库的 C# 绑定来解码我通过 RTP 接收的 H264 视频流。我想我正确地从 RTP 数据包中解封装了 NALU,但我无法解码完整的帧。函数 avcodec_decode_video 调用会引发 AccessViolationException(试图读取或写入受保护的内存)。
以下是一些代码行:
//buf is a byte array containing encoded frame
int success;
FFmpeg.avcodec_init();
FFmpeg.avcodec_register_all();
IntPtr codec = FFmpeg.avcodec_find_decoder(FFmpeg.CodecID.CODEC_ID_H264);
IntPtr codecCont = FFmpeg.avcodec_alloc_context(); //AVCodecContext
FFmpeg.avcodec_open(codecCont, codec);
IntPtr frame = FFmpeg.avcodec_alloc_frame(); //AVFrame
FFmpeg.avcodec_decode_video(codecCont, frame, ref success, (IntPtr)buf[0], buf.Length); //exception
函数导入如下:
[DllImport("avcodec.dll", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
public unsafe static extern int avcodec_decode_video(IntPtr pAVCodecContext, IntPtr pAVFrame, ref int got_picture_ptr, IntPtr buf, int buf_size);
不幸的是,我不知道我与 codecCont 有什么关系。有人写道,AVCDCR 需要使用 RTSP 收到的会话描述来填充此结构。但我不知道哪些字段存储此记录。
我会很高兴得到任何帮助。
PS对不起我的英语