2011-08-24 36 views
1

我正在嘗試使用調用SharpFFmpeg的FFmpeg庫的C#綁定來解碼通過RTP接收的H264視頻流。我想我正確地從RTP包中解封NALU,但我無法解碼完整的幀。函數avcodec_decode_video調用拋出AccessViolationException(試圖讀取或寫入受保護的內存)。
這裏有一些代碼行:FFmpeg(sharpFFmpeg)解碼 - 受保護的內存錯誤

//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的結構。但我不知道哪個字段存儲此記錄。
我會很樂意提供任何幫助。
P.S.請原諒我的英語

+0

到目前爲止您嘗試過的所有視頻都會發生這種情況嗎?這可能是這個文件損壞。 – Amy

+0

非常感謝您的回答。是不可能的。我有一臺IP攝像機發送給我RTP流。其他程序(如ONVIF設備管理器)正確接收到來自相機的圖像。 –

回答

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

現在函數使成功變量等於零,但不拋出異常。

相關問題