我正在尝试使用FFmpeg库调用SharpFFmpeg的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接收到的会话描述来填充此结构。但我不知道哪个(或哪些)字段存储此记录。
如果有任何帮助,我将很高兴。
另外,请原谅我的英语
发布于 2011-08-25 14:27:56
我检测到(IntPtr)buf指向托管内存。我已经将我的代码更新如下:
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变量等于零,但不抛出异常。
https://stackoverflow.com/questions/7174912
复制相似问题