我对 libAV 完全陌生。
我有一个来自其他地方的视频帧(它在内存中,而不是在文件中)。它应该是 H.264 编码的关键帧。
我正在尝试使用 avcodec_decode_video2
进行解码,这个函数是否适合我的需要?
我在使用这段代码时遇到了问题(基本上取自 FFmpeg decode raw buffer with avcodec_decode_video2 ):
AVCodecContext *m_pCodecCtx;
AVCodec *m_pCodec;
AVFrame *m_pFrame;
m_pCodec= avcodec_find_decoder(CODEC_ID_H264);
m_pCodecCtx = avcodec_alloc_context3(m_pCodec);
avcodec_open2(m_pCodecCtx,m_pCodec,0);
// since avcodec_alloc_frame() isn't existing anymore, I changed to following:
//m_pFrame=m_fc.avcodec_alloc_frame(); // and what is/was m_fc?!?
m_pFrame = av_frame_alloc();
AVPacket packet;
av_init_packet(&packet);
packet.data = (unsigned char*)mData;
packet.size=(int)mDataSize;
// this is for decoding a keyframe?
packet.flags |= AV_PKT_FLAG_KEY;
int framefinished=0;
int nres=avcodec_decode_video2(m_pCodecCtx,m_pFrame,&framefinished,&packet);
if(framefinished)
{
std::cout << "decoded frame" << std::endl
}
不幸的是 frameffined
总是返回 0 而 nres
是 18331 等于 mDataSize
我错在哪里? 我是否必须更清楚地指定编解码器信息? 我的包裹是否破损或不完整? 我是否使用了错误的 libAV 函数?
在我使用的框架的示例代码中,使用 av_interleaved_write_frame
(和 AvFormatContext
)将图像数据成功地流式传输到一个文件。这可以帮助我配置数据包或编解码器吗?
最佳答案
nres
为正,且等于mDataSize
意味着一切正常,您的解码已通过此数据包完成。 framefinished == 0
仅表示框架未写入。
可能是因为你的帧数据在多个数据包之间共享,所以你需要用以下数据包调用avcodec_decode_video2
,直到你有一个完整的帧。
另一种可能的解释:你的编解码器在输入和输出之间有延迟:它在第一次调用时没有给你任何东西,并且需要最后一次调用空数据包才能获得最后一帧。你需要做的是调用类似
AVPacket emptyPacket;
av_init_packet(&emptyPacket);
emptyPacket.data = NULL;
emptyPacket.size = 0;
emptyPacket.stream_index = packet.stream_index;
nres=avcodec_decode_video2(m_pCodecCtx,m_pFrame,&framefinished,&emptyPacket);
请参阅关于CODEC_CAP_DELAY
的说明 here
关于c++ - 使用 LibAV : avcodec_decode_video2? 将压缩帧解码到内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25431413/