av_buffersink_get_frame函数说明

在视频合成过程中遇到内存泄露问题,根源在于av_buffersink_get_frame函数。该函数需要配合av_frame_alloc预先分配的AVFrame使用,并在之后通过av_frame_unref或av_frame_free释放内存。av_frame_unref主要负责释放AVFrame内部的buffer空间,而av_buffer_unref最终调用av_freep释放内存。正确管理这些函数的使用是避免内存泄露的关键。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在视频合成时,发现存在着内存泄露,最终发现原因出在av_buffersink_get_frame函数,该函数的声明如下:
int av_buffersink_get_frame(AVFilterContext *ctx, AVFrame *frame);
在调用时,我们需要先av_frame_alloc出一个frame,然后直接放进去即可,av_buffersink_get_frame会进行数据内存的分配。
使用完了之后,需要用av_frame_unref或者av_frame_free进行空间释放,否则存在内存泄露。

关于av_frame_unref,其可以释放AVFrame里面的buffer空间,如下所示:
在这里插入图片描述
而av_buffer_unref的调用如下,可以看出,最终会调用到av_freep来进行空间释放。
在这里插入图片描述

我有一个函数用于为一帧YUV420的图像叠加文字: static void mysese(char* yuvBuffer, int width, int height){ AVFrame *frame = av_frame_alloc(); frame->format = AV_PIX_FMT_YUV420P; frame->width = width; frame->height = height; frame->data[0] = yuvBuffer; // Y 数据 frame->data[1] = yuvBuffer + width * height; // U 数据 frame->data[2] = yuvBuffer + width * height * 5 / 4; // V 数据 AVFilterGraph *graph = avfilter_graph_alloc(); AVFilter *input_filter = avfilter_get_by_name("buffer"); AVFilterContext *input_ctx = avfilter_graph_alloc_filter(graph, input_filter, "input"); // 创建叠字滤镜 AVFilter *drawtext_filter = avfilter_get_by_name("drawtext"); AVFilterContext *drawtext_ctx = avfilter_graph_alloc_filter(graph, drawtext_filter, "drawtext"); // 设置叠字参数 av_opt_set(drawtext_ctx, "text", "Hello World", AV_OPT_SEARCH_CHILDREN); av_opt_set(drawtext_ctx, "fontsize", "24", AV_OPT_SEARCH_CHILDREN); av_opt_set(drawtext_ctx, "fontcolor", "red", AV_OPT_SEARCH_CHILDREN); av_opt_set(drawtext_ctx, "alpha", "0.5", AV_OPT_SEARCH_CHILDREN); av_opt_set(drawtext_ctx, "fontfile", "/home/bin/font-file/msyh.ttc", AV_OPT_SEARCH_CHILDREN); AVFilter *output_filter = avfilter_get_by_name("buffersink"); AVFilterContext *output_ctx = avfilter_graph_alloc_filter(graph, output_filter, "output"); // 连接输入滤镜和叠字滤镜 avfilter_link(input_ctx, 0, drawtext_ctx, 0); // 连接叠字滤镜和输出滤镜 avfilter_link(drawtext_ctx, 0, output_ctx, 0); avfilter_graph_config(graph, NULL); av_buffersrc_add_frame(input_ctx, frame); AVFrame *filtered_frame; while (av_buffersink_get_frame(output_ctx, filtered_frame) >= 0){ int y_size = width * height; int uv_size = y_size / 4; memcpy(yuvBuffer, filtered_frame->data[0], y_size); memcpy(yuvBuffer + y_size, filtered_frame->data[1], uv_size); memcpy(yuvBuffer + y_size + uv_size, filtered_frame->data[2], uv_size); av_frame_unref(filtered_frame); } } 但在执行到avfilter_graph_config(graph, NULL);时会报错段错误(吐核)是什么情况
07-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值