FFmpeg - avcodec_receive_frame返回AVERROR(EAGAIN)

问题描述 投票:1回答:1

我正在使用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();
    }
};
ffmpeg decoding
1个回答
1
投票

发送和接收帧之间存在延迟。当接收返回EAGAIN时,您应该使用下一个编码帧调用send,然后再次调用receive。

© www.soinside.com 2019 - 2024. All rights reserved.