今天写了个小栗子,记录一下~
demo作用:将一张图片解码R8G8B8A8的裸数据,保存在AVFrame中,视频是同样的流程。不在赘述。
//解码函数
bool decode(AVPacket* pkt, AVFrame* frm, AVCodecContext* codecContext)
{
int ret = AVERROR(EAGAIN);
for (;;)
{
do {
ret = avcodec_receive_frame(codecContext, frm); //解码出真正的frame
if (ret == AVERROR_EOF)
{
avcodec_flush_buffers(codecContext);
return false;
}
else if (ret >= 0)
{
return true;
}
} while (ret != AVERROR(EAGAIN));
if (avcodec_send_packet(codecContext, pkt) == AVERROR(EAGAIN)) //送packet数据
{
return false;
}
}
return false;
}
int main()
{
AVFormatContext* pFmtCtx = avformat_alloc_context(); //分配format上下文
AVCodecContext* m_codecContext = avcodec_alloc_context3(nullptr); //解码器上下文初始化
AVCode