FFmpeg解码原始缓冲区与avcodec_decode_video2 [英] FFmpeg decode raw buffer with avcodec_decode_video2
问题描述
我收到一个h264流,我至少知道一个帧的大小。流进来了,因为我可以将它存储在一个文件,并与vlc播放。播放文件对我来说没有问题,因为我包括libavformat。但libavformat给了我一个AVPacket,我可以直接给avcodec_decode_video2。在这种情况下,我得到一个字节。如何将原始h264流给avcodec_decode_video2?如何将我的数据包装到AVPacket。 VLC不需要猜测任何数据。
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 file is no problem for me as I include the libavformat. But libavformat gives me back an AVPacket which I can directly give to avcodec_decode_video2. In this case I got a bytestream. How can I give the raw h264 stream to the avcodec_decode_video2? How can I wrap my data into a AVPacket. VLC does not need to guess any data.
推荐答案
解码流或多或少是容易的。此代码适用于我:
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;
};
调用decodeStreamData将数据视为帧。你必须在你的流中搜索NAL(或多或少一个头幻数),它在h264 case 0x00000001。它是一个帧的开始。填充AVPacket没有像我想象的问题。如果你没有这些信息,你可以将其中的属性作为init通过av_init_packet。
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.
这篇关于FFmpeg解码原始缓冲区与avcodec_decode_video2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!