使用
从 FFmpeg 3.x 开始,avcodec_decode_video2 就被废弃了,取而代之的是 avcodec_send_packet 和 avcodec_receive_frame。使用方法很简单,可以查看 ffmpeg 源码文件夹 ffmpeg-< version>/doc/example/decode_video.c 下的代码,这里摘抄关键部分如下:
static void decode(AVCodecContext *dec_ctx, AVFrame *frame, AVPacket *pkt,
const char *filename)
{
char buf[1024];
int ret;
ret = avcodec_send_packet(dec_ctx, pkt);
if (ret < 0) {
fprintf(stderr, "Error sending a packet for decoding\n");
exit(1);
}
while (ret >= 0) {
ret = avcodec_receive_frame(dec_ctx, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
return;
else if (ret < 0) {
fprintf(stderr, "Error during decoding\n");
exit(1);
}
printf("saving frame %3d\n", dec_ctx->frame_number);
}
}
avcodec_decode_video2
在开始分析 avcodec_send_packet 和 avcodec_receive_frame 之前,先看一下 avcodec_decode_video2 的源码实现:
int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
int *got_picture_ptr,
const AVPacket *avpkt)
{
return compat_decode(avctx, picture, got_picture_ptr, avpkt);
}
static int compat_decode(AVCodecContext *avctx, AVFrame *frame,
int *got_frame, const AVPacket *pkt)
{
AVCodecInternal *avci = avctx->internal;
int ret = 0;
av_assert0(avci->compat_decode_consumed == 0);
if (avci->draining_done && pkt && pkt->size != 0) {
av_log(avctx, AV_LOG_WARNING, "Got unexpected packet after EOF\n");
avcodec_flush_buffers(avctx);
}
*got_frame = 0;
avci->compat_decode = 1;
... // 容错处理
if (!avci->compat_decode_partial_size) {
ret = avcodec_send_packet(avctx, pkt);
...
}
while (ret >= 0) {
ret = avcodec_receive_frame(avctx, frame);
if (ret < 0) {
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
ret = 0;
goto finish;
}
if (frame != avci->compat_decode_frame) {
if (!avctx->refcounted_frames) {
ret = unrefcount_frame(avci, frame);
if (ret < 0)
goto finish;
}
*got_frame = 1;
frame = avci->compat_decode_frame;
} else {
...
}
if (avci->draining || (!avctx->codec->bsfs && avci->compat_decode_consumed < pkt->size))
break;
}
finish:
if (ret == 0) {
/* if there are any bsfs then assume full packet is always consumed */
if (avctx->codec->bsfs)
ret = pkt->size;
else
ret = FFMIN(avci->com