avcodec_receive_frame和avcodec_send_packet函数本身不会阻塞。它们只是处理输入数据包和输出帧的函数。阻塞通常是由其他因素引起的,比如网络延迟、硬件性能等。下面是一个简单的FFmpeg解码循环示例:
// 初始化解码器和上下文
AVCodecContext *codecContext = avcodec_alloc_context3(NULL);
AVCodec *codec = avcodec_find_decoder(AV_CODEC_ID_H264);
avcodec_open2(codecContext, codec, NULL);
// 创建数据包和帧
AVPacket *packet = av_packet_alloc();
AVFrame *frame = av_frame_alloc();
while (1) {
// 从输入源读取数据包
if (av_read_frame(formatContext, packet) < 0) {
break;
}
// 将数据包发送给解码器
int ret = avcodec_send_packet(codecContext, packet);
if (ret < 0) {
// 发送失败,处理错误
break;
}
// 循环接收解码后的帧
while (ret >= 0) {
ret = avcodec_receive_frame(codecContext, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
// 数据包已全部解码或需要更多数据包
break;
} else if (ret < 0) {
// 解码失败,处理错误
break;
}
// 处理解码后的帧
// ...
av_frame_unref(frame);
}
av_packet_unref(packet);
}
// 释放资源
av_frame_free(&frame);
av_packet_free(&packet);
avcodec_close(codecContext);
avcodec_free_context(&codecContext);
上述代码中,av_read_frame函数从输入源读取数据包,avcodec_send_packet函数将数据包发送给解码器,avcodec_receive_frame函数接收解码后的帧。循环中的ret变量用于检查函数调用是否成功。循环会一直进行,直到没有更多数据包,或者发生错误。在处理完每个解码后的帧后,需要使用av_frame_unref函数释放帧的引用。最后,需要释放所有分配的资源。
请注意,此示例仅为演示目的,并未处理所有错误情况。在实际应用中,还需要根据具体情况进行错误处理和资源释放。