FFmpeg decodes raw buffer with avcodec_decode_video2

I get a h264 stream where I at least know the size of one frame. The stream goes to the right, since I can save it in a file and play it using vlc. Playing the file is not a problem for me, since I enable libavformat. But libavformat returns me an AVPacket, which I can directly pass avcodec_decode_video2. In this case, I got a side stream. How to transfer h264 source stream to avcodec_decode_video2? How to transfer data to AVPacket. VLC does not need to guess any data.

+4
source share
1 answer

It is more or less easy to decode a stream. This code works 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;
};

decodeStreamData . NAL ( ), h264 0x00000001. . AVPacket , . , , init - av_init_packet. , , .

, , - , .

+5

All Articles