Encoding raw frame to H264 using SharpFFmpeg

1k views Asked by At

I want to encode raw frame (byte[]) to H264 using SharpFFmpeg. Code is:

    public void Encoder(byte[] capturedFrame)
    {
        int returnValue;
        FFmpeg.avcodec_init();
        FFmpeg.avcodec_register_all();
        IntPtr codec = FFmpeg.avcodec_find_encoder(FFmpeg.CodecID.CODEC_ID_H264);
        IntPtr codecCont = FFmpeg.avcodec_alloc_context(); //AVCodecContext
        FFmpeg.avcodec_open(codecCont, codec);
        IntPtr frame = FFmpeg.avcodec_alloc_frame();  //AVFrame
        IntPtr encodedFramePtr = new IntPtr();

        ///// byte[] to IntPtr
        IntPtr unmanagedPointer = Marshal.AllocHGlobal(capturedFrame.Length);
        Marshal.Copy(capturedFrame, 0, unmanagedPointer, capturedFrame.Length);

        returnValue = FFmpeg.avcodec_encode_video(codecCont, encodedFramePtr, capturedFrame.Length, unmanagedPointer);

        Console.WriteLine("Return value is {0}", returnValue);  // return value is always "-1"
        Console.Read();            

        // Call unmanaged code
        Marshal.FreeHGlobal(unmanagedPointer);
    }

but the value returned by encode function is always -1 (error). Can someone point out what I am missing here. Any help will be highly appreciated. Thanks.

0

There are 0 answers