首先上官方filter的链接:https://ffmpeg.org/ffmpeg-filters.html
关于filter命令行:FFmpeg-4.0 的filter机制的架构与实现.之一 Filter原理
1、下面是一个avfilter的graph
上图是ffmpeg中doc/examples中filtering_video.c案例的示意图。
特别注意上面蓝色方块箭头,其就是query_format()后的结果,也是filter协商fmt的关键步骤。
本章节主要查看avfilter中的数据是怎么进入的,然后又是怎么出来的。
主要考察两个函数:
av_buffersrc_add_frame_flags()
av_buffersink_get_frame()
下面是其具体用法:
/* read all packets */
while (1) {
if ((ret = av_read_frame(fmt_ctx, &packet)) < 0)
break;
if (packet.stream_index == video_stream_index) {
ret = avcodec_send_packet(dec_ctx, &packet);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Error while sending a packet to the decoder\n");
break;
}
while (ret >= 0) {
ret = avcodec_receive_frame(dec_ctx, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
break;
} else if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Error while receiving a frame from the decoder\n");
goto end;
}
frame->pts = frame->best_effort_timestamp;
/* push the decoded frame into the filtergraph */
if (av_buffersrc_add_frame_flags(buffersrc_ctx, frame, AV_BUFFERSRC_FLAG_KEEP_REF) < 0) {
av_log(NULL, AV_LOG_ERROR, "Error while feeding the filtergraph\n");
break;
}
/* pull filtered frames from the filtergraph */
while (1) {
ret = av_buffersink_get_frame(buffersink_ctx, filt_frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
break;
if (ret < 0)
goto end;
display_frame(filt_frame, buffersink_ctx->inputs[0]->time_base);
av_frame_unref(filt_frame);
}
av_frame_unref(frame);
}
}
av_packet_unref(&packet);
}
整个函数关系调用图如下: