FFmpeg (sharpFFmpeg) 解码 - 受保护的内存错误
我正在尝试使用 FFmpeg 库的 C# 绑定调用 SharpFFmpeg 来解码我通过 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。有人写道,需要使用 RTSP 接收到的会话描述通过 AVCDCR 来填充此结构。但我不知道哪个字段存储此记录。
我很乐意提供任何帮助。
PS请原谅我的英语
I'm trying to use C# binding of FFmpeg library calling SharpFFmpeg to decode H264 video stream that I receive by RTP. I think I correctly decapsulate NALUs from RTP-packets, but I can't decode complete frames. Function avcodec_decode_video calling throws an AccessViolationException (Attempted to read or write protected memory).
Here are some code lines:
//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
Function has been imported as follows:
[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);
Unfortunately, I don't know what do I have to do with the codecCont. Somebody wrote that it is needed to fill this structure by AVCDCR using session description received by RTSP. But I don't know which field(s) store this record.
I'll be glad for any help.
P.S. Excuse me for my English
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我检测到 (IntPtr)buf[0] 指向托管内存。我已经更新了我的代码如下:
现在函数使 success 变量等于零,但不抛出异常。
I've detected that (IntPtr)buf[0] is pointing to the managed memory. And I've updated my code as follows:
Now function makes success variable equal zero, but do not throws exception.