问题
I'm using an QOpenGL widget to draw frames. However, I'm struggling to get frames by using avcodec_receive_frame
. It ended within the block else if (ret == AVERROR(EAGAIN))
and returned -11. I have no idea what made this happen. Also I checked that codecs were fine, so I guess the problem wasn't caused by codecs.
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();
}
};
回答1:
There is a delay between sending and receiving a frame. When receive returns EAGAIN, you should call send with the next encoded frame, then call receive again.
回答2:
When you call avcodec_receive_frame
and get EAGAIN
error that means your decoder does not get enough data to decode (e.x you send a B-frame in video). So each time you get that error you should ignore it and go to next avcodec_send_packet
来源:https://stackoverflow.com/questions/55354120/ffmpeg-avcodec-receive-frame-returns-averroreagain