FFmpeg 解码示例中各 API 的作用注释
答案要点
- avformat_open_input 打开输入并解析头部,avformat_find_stream_info 探测补全流信息
- avcodec_find_decoder 找解码器,分配上下文并用 avcodec_parameters_to_context 拷入参数,再 avcodec_open2 打开
- av_read_frame 读取压缩数据包,avcodec_send_packet/avcodec_receive_frame 是新式解码接口,一送多收
- av_register_all 在 FFmpeg 4.0 起废弃,avcodec_decode_video2 等旧接口也已被 send/receive 取代
- 结束时送空包冲刷剩余帧,并依次释放 packet、frame、codec context 和 format context
易错点
- 新版本仍照抄 av_register_all、avcodec_decode_video2 等已废弃接口
- 把 send/receive 当成一包对一帧,没有循环 receive 导致丢帧
- 每轮循环不调用 av_packet_unref 造成内存泄漏
面试官常见追问
- avcodec_receive_frame 返回 AVERROR(EAGAIN) 和 AVERROR_EOF 分别什么意思?
- 为什么读完文件后还要再调用一次 avcodec_send_packet(ctx, NULL)?
- AVPacket 和 AVFrame 有什么区别?
完整的口述示范答案、每个追问的参考回答,在启面刷题中心查看;也可以让 AI 面试官用语音按这道题连环追问你。