FFmpeg decode raw buffer with avcodec_decode_video2

与世无争的帅哥 提交于 2019-11-28 07:51:34

It is more or less easy to decode a stream. This code is working perfect for me:

class ffmpegstreamdestination
{
public:
    ffmpegstreamdestination(AVCodecID decoder): 
    {       
        m_pCodec= avcodec_find_decoder(decoder);
        m_pCodecCtx = avcodec_alloc_context3(m_pCodec);
        avcodec_open2(m_pCodecCtx,m_pCodec,0);
        m_pFrame=avcodec_alloc_frame();
    }

    ~ffmpegstreamdestination()
    {
        av_free(m_pFrame);
        avcodec_close(m_pCodecCtx);
    }

    void decodeStreamData(unsigned char * pData, size_t sz)
    {
        AVPacket        packet;
        av_init_packet(&packet);

        packet.data=pData;
        packet.size=(int)sz;
        int framefinished=0;
        int nres=avcodec_decode_video2(m_pCodecCtx,m_pFrame,&framefinished,&packet);

        if(framefinished)
        {
                   // do the yuv magic and call a consumer
        }

        return;
    }

protected:
    AVCodecContext  *m_pCodecCtx;
    AVCodec         *m_pCodec;
    AVFrame         *m_pFrame;
};

the call decodeStreamData expect the data as frames. You have to search your stream for the NAL (more or less a header magic number) which is in h264 case 0x00000001 .Its the beginning of a frame. Filling the AVPacket is not as problematic as I thought. If you do not have the information, you can left the attributes in it as the are init by av_init_packet. There are only problems if you have a different framerate for example.

As always, if you see a bug or think something would work otherwise better, a short message would be welcome.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!