【问题标题】:FFMpeg avcodec_decode_video2 no result for first frameFFMpeg avcodec_decode_video2 第一帧没有结果
【发布时间】:2015-12-24 00:58:15
【问题描述】:

我正在尝试播放我存档中的视频。播放正常,除了第一帧:got_picture_ptr 设置为 0。从第二帧开始一切正常。因此,在使用时间线搜索时,我无法检索图像。从归档功能发送和解码功能中的帧大小相同。为什么解码可能会失败?

AVPacket packet;
    av_init_packet(&packet);
    packet.data = (uint8_t*) frame->data;
    packet.size = frame->size;
    int decode_count = 0;
    bool pushed = false;
//  while(av_read_frame(formatContext, &packet) >= 0) {
        //decode_ret = avcodec_decode_video2(codec_context, pFrame, &got_picture, &packet);
    //}
    LOGDEBUG("Frame len: %d", packet.size);
    while (packet.size > 0) {
        decode_count++;
        int got_picture;
        int decode_ret = 0;
//      LOGDEBUG("Decoding [%d] (%d/%d, %d/%d) %d", getDecoderId(), codec_context->width, width, codec_context->height, height, packet.size);

        try {
            decode_ret = avcodec_decode_video2(codec_context, pFrame, &got_picture, &packet);
            LOGDEBUG("Decoding frame size decode_ret: %d, got_picture: %d, width: %d, height: %d", decode_ret, got_picture, codec_context->width, codec_context->height);
            decoded_frame_number = frame->index;
            if (decode_ret < 0) {
                LOGDEBUG("Decoding error %d", decode_ret);
                return false;
            }
        } catch (...) {
            LOGDEBUG("Error decoding");
            return false;
        }
        if (packet.data) {
            packet.size -= decode_ret;
            packet.data += decode_ret;
        }
        if (!got_picture) {
            LOGDEBUG("Waiting for full picture");
            std::string packet_flags = "";
            if (packet.flags & AV_PKT_FLAG_KEY) {
                packet_flags += "AV_PKT_FLAG_KEY";
            }
            if (packet.flags & AV_PKT_FLAG_CORRUPT) {
                packet_flags += " | AV_PKT_FLAG_CORRUPT";
            }
//          LOGDEBUG("Decoding %d frame[%ld] not complete (%d, %d) flags %s packetsize %d/%d keyframe %d for %d",
//                  decode_ret, frame->index, codec_context->width,
//                  codec_context->height, packet_flags.c_str(), packet.size, frame->size,
//                  pFrame->key_frame, decode_count);
            continue;
        }

这是我的全部功能:http://pastebin.com/TekkZEUa

附:解码第一帧后编解码器上下文宽度和高度为0。

【问题讨论】:

    标签: android ffmpeg h.264


    【解决方案1】:

    Multiple frames lost if I use av_read_frame in FFmpeg

    您看到的是正确的行为。解码器缓冲几帧以提高多线程效率。并且可能需要几帧才能“启动泵”。基本上,为了让您的程序保持响应,avcodec_decode_video2 将帧排队等待解码,然后返回。这可以防止您的程序长时间阻塞。在解码顺序可能与显示顺序不同的B帧情况下,也绝对需要延迟解码。

    那么,如何不丢失这些帧?在 av_read_frame 停止返回新帧后,您必须通过调用 avcodec_decode_video2 以空数据包刷新解码器,直到没有更多帧被重新调整。

    【讨论】:

    • 仍然不明白如何保存第一帧。解码器还没有工作,没有调用冲洗。用空包抽水不起作用。
    • 返回的第一帧是传入的第一帧。只是不会在同一个调用中发生。
    • 有没有办法关闭这种行为?我只想要 1 帧而无需进一步调用。
    猜你喜欢
    • 2018-03-27
    • 2013-12-01
    • 1970-01-01
    • 2020-11-26
    • 2023-03-07
    • 2014-04-05
    • 1970-01-01
    • 2016-08-30
    • 1970-01-01
    相关资源
    最近更新 更多