简体   繁体   中英

FFMPEG: decode h264 with multiple frames

Please excuse my knowledge of video decoding, I am new to this.

I need to decode video frames from h264 to bitmap images in C#. I am using FFmpeg.AutoGen for this. But unfortunately, I failed to get any results.

I have the following data at my disposal.

  • nFrameType = IFrame, PFrame or BFrame
  • nSequence = Frame sequence
  • nWidth = width of resolution
  • nHeight = Height of resolution
  • nVideoSize = size of video data
  • pVideo = video data

My method at current is the following (Similar to the example shown here ):

initialization

private unsafe AVCodecContext* _codecContext;
private unsafe AVFormatContext* _formatContext;
private unsafe AVFrame* _frame;
AVCodecID codecID;
private unsafe AVCodec* _avCodec;
private bool decoderInitialized = false;

    if (!decoderInitialized)
    {
        if (nCodecType == 2)
        {
            codecID = AVCodecID.AV_CODEC_ID_H264;
        }
        unsafe
        {
            _avCodec = ffmpeg.avcodec_find_decoder(codecID);
            _codecContext = ffmpeg.avcodec_alloc_context3(_avCodec);
            ffmpeg.avcodec_open2(_codecContext, _avCodec, null);
            _frame = ffmpeg.av_frame_alloc();
        }
        decoderInitialized = true;

    }

decoding

            AVPacket packet;
            ffmpeg.av_init_packet(&packet);
            packet.data = (byte*)pVideo;
            packet.size = (int)nVideoSize;
            int isFrameFinished = 0;

            int response = ffmpeg.avcodec_decode_video2(_codecContext, _frame, &isFrameFinished, &packet);

I always get response = -22 and isFrameFinished = 0 . I do have a hunch that I am doing something wrong here. But I am unable to find a resource to guide me in some direction. Eg I know, I need to use nFrameType (IFrame, BFrame and PFrame). But I don't know how? Further, I know that I need to use width and height to decode the image properly, but again, I don't know how to go about it.

luckily, I resolved the issue. I was missing registration of codecs ffmpeg.avcodec_register_all() before ffmpeg.avcodec_find_decoder(codecID) . Hope it helps others.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM