C FFmpeg filtergraph内存泄漏
我有一个FFmpeg程序:C FFmpeg filtergraph内存泄漏,c,video,memory,ffmpeg,racket,C,Video,Memory,Ffmpeg,Racket,我有一个FFmpeg程序: 解复用和解码视频文件 将其通过过滤器记录器 对新视频进行编码和多路复用 filtergraph本身相当复杂,因此可以直接从命令行运行: ffmpeg -i demo.mp4 -filter_complex \ "[audio3]atrim=end=30:start=10[audio2];\ [video5]trim=end=30:start=10[video4];[audio2]anull[audio6];\ [video4]scale=width=1920
ffmpeg -i demo.mp4 -filter_complex \
"[audio3]atrim=end=30:start=10[audio2];\
[video5]trim=end=30:start=10[video4];[audio2]anull[audio6];\
[video4]scale=width=1920:height=1080[video7];[audio6]anull[audio8];\
[video7]fps=fps=30[video9];[audio8]anull[audio10];\
[video9]format=pix_fmts=yuv420p[video11];\
[audio10]asetpts=expr=PTS-STARTPTS[audio12];\
[video11]setpts=expr=PTS-STARTPTS[video13];\
[audio15]concat=v=0:a=1:n=1[audio14];\
[video17]concat=v=1:a=0:n=1[video16];\
[audio12]afifo[audio15];[video13]fifo[video17];\
[audio14]afifo[audio18];[video16]fifo[video19];\
[audio18]anull[audio20];\
[video19]pad=width=1920:height=1080[video21];\
[audio20]anull[audio22];[video21]fps=fps=25[video23];\
[audio22]aformat=sample_fmts=fltp:sample_rates=44100:channel_layouts=stereo[fa];\
[video23]format=pix_fmts=yuv420p[fv];[0:a]afifo[audio3];\
[0:v]fifo[video5]" \
-map "[fv]" -map "[fa]" out.mp4
我意识到这是一个巨大的filtergraph,有很多无操作过滤器,它是自动生成的,而不是手工编写的。(这是一个graphviz文件,您可以在命令行或中运行它。)
不管怎样,当我运行使用这个filtergraph的程序时,我的内存使用率会出现峰值。我最终使用了大约7GB的内存来制作一个30秒的剪辑。然而,当我使用上面的ffmpeg命令运行程序时,它的峰值大约为600 MB的RAM。这使我相信问题不在于filtergraph的大小,而在于我的程序如何使用它
程序设置filtergraph(使用av_filter\u parse_ptr
,给出上面显示的filtergraph字符串)、编码器、muxer、解码器和解复用器,然后生成两个线程,一个线程向filtergraph发送帧,另一个线程接收帧。发送它们的帧类似于:
void decode () {
while(... more_frames ...) {
AVFrame *frame = av_frame_alloc();
... fill next frame of stream ...
av_buffersrc_write_frame(ctx, frame);
av_frame_free(&frame);
}
}
(我省略了av\u send\u packet/av\u receive\u frame
函数,因为它们似乎没有泄漏内存。我还省略了刷新缓冲区src的过程,因为这在结束之前不会发生,而且内存在这之前很久就会出现峰值。)
编码器线程看起来很相似:
void encode() {
while(... nodes_in_graph ...) {
AVFrame *frame = av_frame_alloc();
av_buffersink_get_frame(ctx, frame);
... ensure frame actually was filled ...
... send frame to encoder ...
av_frame_free(&frame);
}
}
与解码器一样,我省略了send\u frame/receive\u packet
组合,因为它们似乎没有泄漏内存。此外,我还省略了确保框架实际填充的细节。代码循环直到帧最终被填充
我分配的每一帧都会相当快地取消分配。此外,我还处理了ffmpeg可以给出的所有错误情况(在示例中省略)
我还尝试过只为编码器和解码器设置一个帧(并在循环的每次迭代中调用av_frame\u unref
)
我是否忘记释放某些内容,或者我只是错误地使用了对libavfilter的调用,以至于它必须缓冲所有数据?我不认为泄漏是由内存图引起的,因为从命令行运行它似乎不会导致相同的内存爆炸
FWIW,实际代码是,尽管它是用Racket编写的。我确实有一个最小的例子,似乎也复制了这种行为(从ffmpeg代码的doc/example/filtering\u video.c
文件中修改):
#include <unistd.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavfilter/avfiltergraph.h>
#include <libavfilter/buffersink.h>
#include <libavfilter/buffersrc.h>
#include <libavutil/opt.h>
const char *filter_descr = "trim=start=10:end=30,scale=78:24,transpose=cclock";
static AVFormatContext *fmt_ctx;
static AVCodecContext *dec_ctx;
AVFilterContext *buffersink_ctx;
AVFilterContext *buffersrc_ctx;
AVFilterGraph *filter_graph;
static int video_stream_index = -1;
static int64_t last_pts = AV_NOPTS_VALUE;
static int open_input_file(const char *filename)
{
int ret;
AVCodec *dec;
if ((ret = avformat_open_input(&fmt_ctx, filename, NULL, NULL)) < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n");
return ret;
}
if ((ret = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n");
return ret;
}
/* select the video stream */
ret = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &dec, 0);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot find a video stream in the input file\n");
return ret;
}
video_stream_index = ret;
/* create decoding context */
dec_ctx = avcodec_alloc_context3(dec);
if (!dec_ctx)
return AVERROR(ENOMEM);
avcodec_parameters_to_context(dec_ctx, fmt_ctx->streams[video_stream_index]->codecpar);
av_opt_set_int(dec_ctx, "refcounted_frames", 1, 0);
/* init the video decoder */
if ((ret = avcodec_open2(dec_ctx, dec, NULL)) < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot open video decoder\n");
return ret;
}
return 0;
}
static int init_filters(const char *filters_descr)
{
char args[512];
int ret = 0;
AVFilter *buffersrc = avfilter_get_by_name("buffer");
AVFilter *buffersink = avfilter_get_by_name("buffersink");
AVFilterInOut *outputs = avfilter_inout_alloc();
AVFilterInOut *inputs = avfilter_inout_alloc();
AVRational time_base = fmt_ctx->streams[video_stream_index]->time_base;
enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
filter_graph = avfilter_graph_alloc();
if (!outputs || !inputs || !filter_graph) {
ret = AVERROR(ENOMEM);
goto end;
}
/* buffer video source: the decoded frames from the decoder will be inserted here. */
snprintf(args, sizeof(args),
"video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
dec_ctx->width, dec_ctx->height, dec_ctx->pix_fmt,
time_base.num, time_base.den,
dec_ctx->sample_aspect_ratio.num, dec_ctx->sample_aspect_ratio.den);
ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
args, NULL, filter_graph);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot create buffer source\n");
goto end;
}
/* buffer video sink: to terminate the filter chain. */
ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
NULL, NULL, filter_graph);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot create buffer sink\n");
goto end;
}
ret = av_opt_set_int_list(buffersink_ctx, "pix_fmts", pix_fmts,
AV_PIX_FMT_NONE, AV_OPT_SEARCH_CHILDREN);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot set output pixel format\n");
goto end;
}
outputs->name = av_strdup("in");
outputs->filter_ctx = buffersrc_ctx;
outputs->pad_idx = 0;
outputs->next = NULL;
inputs->name = av_strdup("out");
inputs->filter_ctx = buffersink_ctx;
inputs->pad_idx = 0;
inputs->next = NULL;
if ((ret = avfilter_graph_parse_ptr(filter_graph, filters_descr,
&inputs, &outputs, NULL)) < 0)
goto end;
if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0)
goto end;
end:
avfilter_inout_free(&inputs);
avfilter_inout_free(&outputs);
return ret;
}
int main(int argc, char **argv)
{
int ret;
AVPacket packet;
AVFrame *frame = av_frame_alloc();
AVFrame *filt_frame = av_frame_alloc();
if (!frame || !filt_frame) {
perror("Could not allocate frame");
exit(1);
}
if (argc != 2) {
fprintf(stderr, "Usage: %s file\n", argv[0]);
exit(1);
}
av_register_all();
avfilter_register_all();
if ((ret = open_input_file(argv[1])) < 0)
goto end;
if ((ret = init_filters(filter_descr)) < 0)
goto end;
/* 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;
}
if (ret >= 0) {
frame->pts = av_frame_get_best_effort_timestamp(frame);
/* 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;
av_frame_unref(filt_frame);
}
av_frame_unref(frame);
}
}
}
av_packet_unref(&packet);
}
end:
avfilter_graph_free(&filter_graph);
avcodec_free_context(&dec_ctx);
avformat_close_input(&fmt_ctx);
av_frame_free(&frame);
av_frame_free(&filt_frame);
return ret;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
const char*filter_descr=“trim=start=10:end=30,scale=78:24,transpose=cclock”;
静态AVFormatContext*fmt_ctx;
静态AVCodecContext*dec_ctx;
AVFilterContext*buffersink_ctx;
AVFilterContext*缓冲区src_ctx;
AVFilterGraph*过滤器图;
静态int视频流索引=-1;
静态int64\u t last\u pts=AV\u NOPTS\u值;
静态整型打开输入文件(常量字符*文件名)
{
int ret;
AVCodec*dec;
如果((ret=avformat\u open\u输入(&fmt\u ctx,文件名,NULL,NULL))<0){
av_日志(空,av_日志错误,“无法打开输入文件”\n);
返回ret;
}
if((ret=avformat\u find\u stream\u info(fmt\u ctx,NULL))<0){
av_日志(NULL,av_日志错误,“找不到流信息\n”);
返回ret;
}
/*选择视频流*/
ret=av查找最佳流(fmt ctx,AVMEDIA类型视频,-1,-1,&dec,0);
如果(ret<0){
av_日志(NULL,av_日志错误,“在输入文件中找不到视频流\n”);
返回ret;
}
视频流索引=ret;
/*创建解码上下文*/
dec_ctx=avcodec_alloc_context3(dec);
如果(!dec_ctx)
返回平均值(ENOMEM);
avcodec_参数_到_上下文(dec_ctx,fmt_ctx->streams[video_stream_index]->codepar);
av_opt_set_int(12月ctx,“参考计数帧”,1,0);
/*初始化视频解码器*/
if((ret=avcodec_open2(dec_ctx,dec,NULL))<0){
av_日志(空,av_日志错误,“无法打开视频解码器”\n);
返回ret;
}
返回0;
}
静态int init_过滤器(const char*filters_descr)
{
char-args[512];
int-ret=0;
AVFilter*buffersrc=AVFilter_get_by_name(“buffer”);
AVFilter*buffersink=AVFilter_get_by_name(“buffersink”);
AVFilterInOut*输出=avfilter_inout_alloc();
AVFilterInOut*输入=avfilter_inout_alloc();
AVRational time\u base=fmt\u ctx->streams[video\u stream\u index]->time\u base;
枚举AVPixelFormat pix_fmts[]={AV_pix_FMT_GRAY8,AV_pix_FMT_NONE};
filter_graph=avfilter_graph_alloc();
如果(!输出| | |!输入| |!过滤器|图形){
ret=平均值(ENOMEM);
转到终点;
}
/*缓冲视频源:解码器的解码帧将插入此处*/
snprintf(args,sizeof(args),
“视频大小=%dx%d:pix\u fmt=%d:time\u base=%d/%d:pixel\u aspect=%d/%d”,
dec_ctx->宽度,dec_ctx->高度,dec_ctx->pix_fmt,
time_base.num,time_base.den,
dec\u ctx->sample\u aspect\u ratio.num,dec\u ctx->sample\u aspect\u ratio.den);
ret=avfilter\u graph\u create\u filter(&buffersrc\u ctx,buffersrc,“in”,
args、NULL、filter_图);
如果(ret<0){
av_日志(NULL,av_日志错误,“无法创建缓冲区源\n”);
转到终点;
}
/*缓冲区视频接收器:终止过滤器链*/
ret=avfilter\u graph\u create\u filter(&buffersink\u ctx,buffersink,“out”,
NULL,NULL,filter_图);
如果(ret<0){
av_日志(空,av_日志错误,“无法创建缓冲区接收器\n”);
转到终点;
}
ret=av_opt_set_int_list(缓冲接收器ctx、“pix_fmts”、pix_fmts、,
AV_PIX_FMT_NONE,AV_OPT_SEARCH_CHILDREN);
如果(ret<0){
av_日志(空,av_日志错误,“无法设置输出像素格式\n”);
转到终点;
}
输出->名称=av_标准设置(“in”);
输出