我正在尝试播放我存档中的视频。播放正常,除了第一帧: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。
最佳答案
复制粘贴自
Multiple frames lost if I use av_read_frame in FFmpeg
您所看到的是正确的行为。解码器缓冲几帧以提高多线程效率。并且可能需要几帧才能“启动泵”。基本上,为了让您的程序保持响应,avcodec_decode_video2 将帧排队等待解码,然后返回。这可以防止您的程序长时间阻塞。在解码顺序可能与显示顺序不同的B帧的情况下,也绝对需要延迟解码。
那么,如何不丢失这些帧呢?在 av_read_frame 停止返回新帧后,您必须通过使用空数据包调用 avcodec_decode_video2 来刷新解码器,直到没有更多帧被重新调整。
关于android - FFMpeg avcodec_decode_video2 第一帧没有结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34438471/