FFmpeg decode raw buffer with avcodec_decode_video2

后端 未结 1 1574
余生分开走
余生分开走 2020-12-08 18:12

I am receiving a h264 stream where I at least know the size of one frame. The stream is coming in right as I can store it in a file and playback with vlc. Playing back a fil

相关标签:
1条回答
  • 2020-12-08 18:26

    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.

    0 讨论(0)
提交回复
热议问题