首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FFmpeg (sharpFFmpeg)解码保护的内存错误

FFmpeg (sharpFFmpeg)解码保护的内存错误
EN

Stack Overflow用户
提问于 2011-08-24 19:49:01
回答 1查看 3.6K关注 0票数 1

我正在尝试使用FFmpeg库调用SharpFFmpeg的C#绑定来解码我通过RTP接收到的H264视频流。我认为我正确地从RTP数据包中解封了NALU,但我无法解码完整的帧。函数avcodec_decode_video调用抛出AccessViolationException (尝试读取或写入受保护的内存)。

下面是一些代码行:

代码语言:javascript
复制
    //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

函数的导入方式如下:

代码语言:javascript
复制
    [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接收到的会话描述来填充此结构。但我不知道哪个(或哪些)字段存储此记录。

如果有任何帮助,我将很高兴。

另外,请原谅我的英语

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-08-25 14:27:56

我检测到(IntPtr)buf指向托管内存。我已经将我的代码更新如下:

代码语言:javascript
复制
  IntPtr frame = FFmpeg.avcodec_alloc_frame();
  IntPtr buffer = Marshal.AllocHGlobal(buf.Length + FFmpeg.FF_INPUT_BUFFER_PADDING_SIZE);
  for (int i = 0; i < buf.Length; i++)
      Marshal.StructureToPtr(buf[i], buffer + i, true);
  avcodec_decode_video(codecCont, frame, ref success, buffer, buf.Length + FFmpeg.FF_INPUT_BUFFER_PADDING_SIZE);

现在函数使success变量等于零,但不抛出异常。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7174912

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档