我正在使用QOpenGL小部件绘制框架。但是,我正在努力使用avcodec_receive_frame
获取帧。它在块else if (ret == AVERROR(EAGAIN))
中结束并返回-11。我不知道是什么导致了这种情况。我还检查了编解码器是否正常,所以我猜问题不是由编解码器引起的。
MyDemux.cpp
AVPacket* MyDemux::allocatePacket()
{
AVPacket* packet = av_packet_alloc();
return packet;
}
AVFrame* MyDemux::allocateFrame()
{
AVFrame* frame = av_frame_alloc();
return frame;
}
int MyDemux::readFrame(AVPacket* packet)
{
mux.lock();
if (!formatCtx) {
std::cout << "formaetCtx is null" << std::endl;
mux.unlock();
return -1;
}
int ret = av_read_frame(formatCtx, packet);
if (ret == AVERROR_EOF) {
return -2;
}
if (ret != 0) {
mux.unlock();
av_packet_free(&packet);
return -1;
}
media_type = packet->stream_index;
mux.unlock();
return 0;
}
MyDecode.cpp
void MyDecode::decode(AVFrame* frame, AVPacket* packet)
{
int ret;
ret = avcodec_send_packet(codecCtx, packet); // this returned 0
while (ret == 0) {
ret = avcodec_receive_frame(codecCtx, frame); // this returned -11
if (ret == AVERROR(EINVAL)) {
std::cout << "codec issue" << std::endl;
av_frame_free(&frame);
return;
}
else if (ret == AVERROR(EAGAIN)) { // program ends here
std::cout << "output is not available this state" << std::endl;
av_frame_free(&frame);
return;
}
else if (ret == AVERROR(EINVAL)) {
std::cout << "no more frames" << std::endl;
av_frame_free(&frame);
return;
}
}
}
main.cpp
class MyThread : public QThread
{
public:
MyDemux demux;
MyDecode video_decode;
myDecode audio_decode;
MyVideoWidget* videoWidget;
AVPacket* packet;
AVFrame* frame;
void initThread()
{
char* url = "demo.mp4";
demux.openFile(url);
video_decode.openCodec(demux.copy_video_codec_par());
audio_decode.openCodec(demux.copy_audio_codec_par());
packet = demux.allocatePacket();
frame = demux.allocateFrame();
}
void run()
{
while (demux.readFrame(packet) != -2) {
if (demux.get_media_type() == 0) {
video_decode.decode(frame, packet);
videoWidget->paintFrame(frame);
}
else if (demux.get_media_type() == 1) {
}
}
video_decode.decode(frame, nullptr);
demux.clear();
demux.close();
}
};
答案 0 :(得分:1)
在发送和接收帧之间存在延迟。当接收返回EAGAIN时,应使用下一个编码帧调用发送,然后再次调用接收。
答案 1 :(得分:0)
当您致电avcodec_receive_frame
并收到EAGAIN
错误时,这意味着您的解码器没有足够的数据进行解码(例如,您在视频中发送了B帧)。因此,每次收到该错误时,都应该忽略该错误并转到下一个avcodec_send_packet