1

SharpFFmpegを呼び出すFFmpegライブラリのC#バインディングを使用して、RTPで受信したH264ビデオストリームをデコードしようとしています。NLUをRTPパケットから正しくカプセル化解除したと思いますが、完全なフレームをデコードできません。関数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すみません英語で

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 に答える