1

我正在尝试使用调用 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对不起我的英语

4

1 回答 1

1

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

  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);

现在函数使成功变量为零,但不抛出异常。

于 2011-08-25T06:27:56.470 回答